/* The original wlr_renderer was heavily referenced in making this project https://gitlab.freedesktop.org/wlroots/wlroots/-/tree/master/render/gles2 */ #include #include #include #include #include #include #include #include #include #include #include #include "render/fx_renderer/fx_renderer.h" #include "render/fx_renderer/fx_stencilbuffer.h" #include "render/fx_renderer/matrix.h" // shaders #include "common_vert_src.h" #include "quad_frag_src.h" #include "tex_frag_src.h" #include "stencil_mask_frag_src.h" #include "box_shadow_frag_src.h" static const GLfloat verts[] = { 1, 0, // top right 0, 0, // top left 1, 1, // bottom right 0, 1, // bottom left }; static GLuint compile_shader(GLuint type, const GLchar *src) { GLuint shader = glCreateShader(type); glShaderSource(shader, 1, &src, NULL); glCompileShader(shader); GLint ok; glGetShaderiv(shader, GL_COMPILE_STATUS, &ok); if (ok == GL_FALSE) { wlr_log(WLR_ERROR, "Failed to compile shader"); glDeleteShader(shader); shader = 0; } return shader; } static GLuint link_program(const GLchar *frag_src) { const GLchar *vert_src = common_vert_src; GLuint vert = compile_shader(GL_VERTEX_SHADER, vert_src); if (!vert) { goto error; } GLuint frag = compile_shader(GL_FRAGMENT_SHADER, frag_src); if (!frag) { glDeleteShader(vert); goto error; } GLuint prog = glCreateProgram(); glAttachShader(prog, vert); glAttachShader(prog, frag); glLinkProgram(prog); glDetachShader(prog, vert); glDetachShader(prog, frag); glDeleteShader(vert); glDeleteShader(frag); GLint ok; glGetProgramiv(prog, GL_LINK_STATUS, &ok); if (ok == GL_FALSE) { wlr_log(WLR_ERROR, "Failed to link shader"); glDeleteProgram(prog); goto error; } return prog; error: return 0; } static bool link_quad_program(struct quad_shader *shader) { GLuint prog; shader->program = prog = link_program(quad_frag_src); if (!shader->program) { return false; } shader->proj = glGetUniformLocation(prog, "proj"); shader->color = glGetUniformLocation(prog, "color"); shader->pos_attrib = glGetAttribLocation(prog, "pos"); return true; } static bool link_tex_program(struct tex_shader *shader, enum fx_tex_shader_source source) { GLchar frag_src[2048]; snprintf(frag_src, sizeof(frag_src), "#define SOURCE %d\n%s", source, tex_frag_src); GLuint prog; shader->program = prog = link_program(frag_src); if (!shader->program) { return false; } shader->proj = glGetUniformLocation(prog, "proj"); shader->tex = glGetUniformLocation(prog, "tex"); shader->alpha = glGetUniformLocation(prog, "alpha"); shader->pos_attrib = glGetAttribLocation(prog, "pos"); shader->tex_attrib = glGetAttribLocation(prog, "texcoord"); shader->size = glGetUniformLocation(prog, "size"); shader->position = glGetUniformLocation(prog, "position"); shader->radius = glGetUniformLocation(prog, "radius"); return true; } static bool link_stencil_mask_program(struct stencil_mask_shader *shader) { GLuint prog; shader->program = prog = link_program(stencil_mask_frag_src); if (!shader->program) { return false; } shader->proj = glGetUniformLocation(prog, "proj"); shader->color = glGetUniformLocation(prog, "color"); shader->pos_attrib = glGetAttribLocation(prog, "pos"); shader->position = glGetUniformLocation(prog, "position"); shader->half_size = glGetUniformLocation(prog, "half_size"); shader->radius = glGetUniformLocation(prog, "radius"); return true; } static bool link_box_shadow_program(struct box_shadow_shader *shader) { GLuint prog; shader->program = prog = link_program(box_shadow_frag_src); if (!shader->program) { return false; } shader->proj = glGetUniformLocation(prog, "proj"); shader->color = glGetUniformLocation(prog, "color"); shader->pos_attrib = glGetAttribLocation(prog, "pos"); shader->position = glGetUniformLocation(prog, "position"); shader->size = glGetUniformLocation(prog, "size"); shader->blur_sigma = glGetUniformLocation(prog, "blur_sigma"); shader->corner_radius = glGetUniformLocation(prog, "corner_radius"); return true; } static bool check_gl_ext(const char *exts, const char *ext) { size_t extlen = strlen(ext); const char *end = exts + strlen(exts); while (exts < end) { if (exts[0] == ' ') { exts++; continue; } size_t n = strcspn(exts, " "); if (n == extlen && strncmp(ext, exts, n) == 0) { return true; } exts += n; } return false; } static void load_gl_proc(void *proc_ptr, const char *name) { void *proc = (void *)eglGetProcAddress(name); if (proc == NULL) { wlr_log(WLR_ERROR, "FX RENDERER: eglGetProcAddress(%s) failed", name); abort(); } *(void **)proc_ptr = proc; } static void fx_renderer_handle_destroy(struct wlr_addon *addon) { struct fx_renderer *renderer = wl_container_of(addon, renderer, addon); struct fx_framebuffer *fx_buffer, *fx_buffer_tmp; wl_list_for_each_safe(fx_buffer, fx_buffer_tmp, &renderer->buffers, link) { fx_framebuffer_release(fx_buffer); } struct fx_texture *tex, *tex_tmp; wl_list_for_each_safe(tex, tex_tmp, &renderer->textures, link) { fx_texture_destroy(tex); } fx_stencilbuffer_release(&renderer->stencil_buffer); free(renderer); } static const struct wlr_addon_interface fx_renderer_addon_impl = { .name = "fx_renderer", .destroy = fx_renderer_handle_destroy, }; void fx_renderer_init_addon(struct wlr_egl *egl, struct wlr_output *output, struct wlr_addon_set *addons, const void * owner) { struct fx_renderer *renderer = fx_renderer_create(egl, output); if (!renderer) { wlr_log(WLR_ERROR, "Failed to create fx_renderer"); abort(); } wlr_addon_init(&renderer->addon, addons, owner, &fx_renderer_addon_impl); } struct fx_renderer *fx_renderer_addon_find(struct wlr_addon_set *addons, const void * owner) { struct wlr_addon *addon = wlr_addon_find(addons, owner, &fx_renderer_addon_impl); if (addon == NULL) { return NULL; } struct fx_renderer *renderer = wl_container_of(addon, renderer, addon); return renderer; } struct fx_renderer *fx_renderer_create(struct wlr_egl *egl, struct wlr_output *output) { struct fx_renderer *renderer = calloc(1, sizeof(struct fx_renderer)); if (renderer == NULL) { return NULL; } wl_list_init(&renderer->buffers); wl_list_init(&renderer->textures); renderer->wlr_output = output; renderer->egl = egl; if (!eglMakeCurrent(wlr_egl_get_display(egl), EGL_NO_SURFACE, EGL_NO_SURFACE, wlr_egl_get_context(egl))) { wlr_log(WLR_ERROR, "FX RENDERER: Could not make EGL current"); return NULL; } // Create the stencil buffer renderer->stencil_buffer = fx_stencilbuffer_create(); // Create the FBOs renderer->wlr_main_buffer_fbo = -1; // get extensions const char *exts_str = (const char *)glGetString(GL_EXTENSIONS); if (exts_str == NULL) { wlr_log(WLR_ERROR, "FX RENDERER: Failed to get GL_EXTENSIONS"); return NULL; } wlr_log(WLR_INFO, "Creating scenefx FX renderer"); wlr_log(WLR_INFO, "Using %s", glGetString(GL_VERSION)); wlr_log(WLR_INFO, "GL vendor: %s", glGetString(GL_VENDOR)); wlr_log(WLR_INFO, "GL renderer: %s", glGetString(GL_RENDERER)); wlr_log(WLR_INFO, "Supported FX extensions: %s", exts_str); // TODO: the rest of the gl checks if (check_gl_ext(exts_str, "GL_OES_EGL_image_external")) { renderer->exts.OES_egl_image_external = true; load_gl_proc(&renderer->procs.glEGLImageTargetTexture2DOES, "glEGLImageTargetTexture2DOES"); } if (check_gl_ext(exts_str, "GL_OES_EGL_image")) { renderer->exts.OES_egl_image = true; load_gl_proc(&renderer->procs.glEGLImageTargetRenderbufferStorageOES, "glEGLImageTargetRenderbufferStorageOES"); } // quad fragment shader if (!link_quad_program(&renderer->shaders.quad)) { goto error; } // fragment shaders if (!link_tex_program(&renderer->shaders.tex_rgba, SHADER_SOURCE_TEXTURE_RGBA)) { goto error; } if (!link_tex_program(&renderer->shaders.tex_rgbx, SHADER_SOURCE_TEXTURE_RGBX)) { goto error; } if (!link_tex_program(&renderer->shaders.tex_ext, SHADER_SOURCE_TEXTURE_EXTERNAL)) { goto error; } // stencil mask shader if (!link_stencil_mask_program(&renderer->shaders.stencil_mask)) { goto error; } // box shadow shader if (!link_box_shadow_program(&renderer->shaders.box_shadow)) { goto error; } if (!eglMakeCurrent(wlr_egl_get_display(egl), EGL_NO_SURFACE, EGL_NO_SURFACE, EGL_NO_CONTEXT)) { wlr_log(WLR_ERROR, "FX RENDERER: Could not unset current EGL"); goto error; } wlr_log(WLR_INFO, "FX RENDERER: Shaders Initialized Successfully"); return renderer; error: glDeleteProgram(renderer->shaders.quad.program); glDeleteProgram(renderer->shaders.tex_rgba.program); glDeleteProgram(renderer->shaders.tex_rgbx.program); glDeleteProgram(renderer->shaders.tex_ext.program); glDeleteProgram(renderer->shaders.stencil_mask.program); glDeleteProgram(renderer->shaders.box_shadow.program); if (!eglMakeCurrent(wlr_egl_get_display(egl), EGL_NO_SURFACE, EGL_NO_SURFACE, EGL_NO_CONTEXT)) { wlr_log(WLR_ERROR, "FX RENDERER: Could not unset current EGL"); } // TODO: more freeing? free(renderer); wlr_log(WLR_ERROR, "FX RENDERER: Error Initializing Shaders"); return NULL; } void fx_renderer_begin(struct fx_renderer *renderer, int width, int height) { glViewport(0, 0, width, height); renderer->viewport_width = width; renderer->viewport_height = height; // Store the wlr FBO renderer->wlr_main_buffer_fbo = wlr_gles2_renderer_get_current_fbo(renderer->wlr_output->renderer); // Get the fx_texture struct wlr_texture *wlr_texture = wlr_texture_from_buffer( renderer->wlr_output->renderer, renderer->wlr_output->back_buffer); wlr_gles2_texture_get_attribs(wlr_texture, &renderer->wlr_main_texture_attribs); wlr_texture_destroy(wlr_texture); // Add the stencil to the wlr fbo fx_stencilbuffer_init(&renderer->stencil_buffer, width, height); // Finally bind the main wlr FBO fx_framebuffer_bind_wlr_fbo(renderer); // refresh projection matrix matrix_projection(renderer->projection, width, height, WL_OUTPUT_TRANSFORM_FLIPPED_180); glBlendFunc(GL_ONE, GL_ONE_MINUS_SRC_ALPHA); } void fx_renderer_end(struct fx_renderer *renderer) { } void fx_renderer_clear(const float color[static 4]) { glClearColor(color[0], color[1], color[2], color[3]); glClearStencil(0); glClear(GL_COLOR_BUFFER_BIT | GL_STENCIL_BUFFER_BIT); } void fx_renderer_scissor(struct wlr_box *box) { if (box) { glScissor(box->x, box->y, box->width, box->height); glEnable(GL_SCISSOR_TEST); } else { glDisable(GL_SCISSOR_TEST); } } void fx_renderer_stencil_mask_init(void) { glClearStencil(0); glClear(GL_STENCIL_BUFFER_BIT); glEnable(GL_STENCIL_TEST); glStencilFunc(GL_ALWAYS, 1, 0xFF); glStencilOp(GL_KEEP, GL_KEEP, GL_REPLACE); // Disable writing to color buffer glColorMask(GL_FALSE, GL_FALSE, GL_FALSE, GL_FALSE); } void fx_renderer_stencil_mask_close(bool draw_inside_mask) { // Reenable writing to color buffer glColorMask(GL_TRUE, GL_TRUE, GL_TRUE, GL_TRUE); if (draw_inside_mask) { glStencilFunc(GL_EQUAL, 1, 0xFF); glStencilOp(GL_KEEP, GL_KEEP, GL_REPLACE); return; } glStencilFunc(GL_NOTEQUAL, 1, 0xFF); glStencilOp(GL_KEEP, GL_KEEP, GL_KEEP); } void fx_renderer_stencil_mask_fini(void) { glClearStencil(0); glClear(GL_STENCIL_BUFFER_BIT); glDisable(GL_STENCIL_TEST); } void fx_renderer_stencil_enable(void) { glEnable(GL_STENCIL_TEST); } void fx_renderer_stencil_disable(void) { glDisable(GL_STENCIL_TEST); } bool fx_render_subtexture_with_matrix(struct fx_renderer *renderer, struct wlr_texture *wlr_texture, const struct wlr_fbox *src_box, const struct wlr_box *dst_box, const float matrix[static 9], float opacity, int corner_radius) { struct wlr_gles2_texture_attribs texture_attrs; if (wlr_texture_is_gles2(wlr_texture)) { wlr_gles2_texture_get_attribs(wlr_texture, &texture_attrs); } else if (wlr_texture_is_fx(wlr_texture)) { struct fx_texture *fx_texture = fx_get_texture(wlr_texture); wlr_gles2_texture_get_fx_attribs(fx_texture, &texture_attrs); } else { wlr_log(WLR_ERROR, "Texture not GLES2 or FX. Aborting..."); abort(); } struct tex_shader *shader = NULL; switch (texture_attrs.target) { case GL_TEXTURE_2D: if (texture_attrs.has_alpha) { shader = &renderer->shaders.tex_rgba; } else { shader = &renderer->shaders.tex_rgbx; } break; case GL_TEXTURE_EXTERNAL_OES: shader = &renderer->shaders.tex_ext; if (!renderer->exts.OES_egl_image_external) { wlr_log(WLR_ERROR, "Failed to render texture: " "GL_TEXTURE_EXTERNAL_OES not supported"); return false; } break; default: wlr_log(WLR_ERROR, "Aborting render"); abort(); } float gl_matrix[9]; wlr_matrix_multiply(gl_matrix, renderer->projection, matrix); // OpenGL ES 2 requires the glUniformMatrix3fv transpose parameter to be set // to GL_FALSE wlr_matrix_transpose(gl_matrix, gl_matrix); // if there's no opacity or rounded corners we don't need to blend if (!texture_attrs.has_alpha && opacity == 1.0 && !corner_radius) { glDisable(GL_BLEND); } else { glEnable(GL_BLEND); } glBlendFunc(GL_ONE, GL_ONE_MINUS_SRC_ALPHA); glActiveTexture(GL_TEXTURE0); glBindTexture(texture_attrs.target, texture_attrs.tex); glTexParameteri(texture_attrs.target, GL_TEXTURE_MIN_FILTER, GL_LINEAR); glUseProgram(shader->program); glUniformMatrix3fv(shader->proj, 1, GL_FALSE, gl_matrix); glUniform1i(shader->tex, 0); glUniform2f(shader->size, dst_box->width, dst_box->height); glUniform2f(shader->position, dst_box->x, dst_box->y); glUniform1f(shader->alpha, opacity); glUniform1f(shader->radius, corner_radius); const GLfloat x1 = src_box->x / wlr_texture->width; const GLfloat y1 = src_box->y / wlr_texture->height; const GLfloat x2 = (src_box->x + src_box->width) / wlr_texture->width; const GLfloat y2 = (src_box->y + src_box->height) / wlr_texture->height; const GLfloat texcoord[] = { x2, y1, // top right x1, y1, // top left x2, y2, // bottom right x1, y2, // bottom left }; glVertexAttribPointer(shader->pos_attrib, 2, GL_FLOAT, GL_FALSE, 0, verts); glVertexAttribPointer(shader->tex_attrib, 2, GL_FLOAT, GL_FALSE, 0, texcoord); glEnableVertexAttribArray(shader->pos_attrib); glEnableVertexAttribArray(shader->tex_attrib); glDrawArrays(GL_TRIANGLE_STRIP, 0, 4); glDisableVertexAttribArray(shader->pos_attrib); glDisableVertexAttribArray(shader->tex_attrib); glBindTexture(texture_attrs.target, 0); return true; } void fx_render_rect(struct fx_renderer *renderer, const struct wlr_box *box, const float color[static 4], const float projection[static 9]) { if (box->width == 0 || box->height == 0) { return; } assert(box->width > 0 && box->height > 0); float matrix[9]; wlr_matrix_project_box(matrix, box, WL_OUTPUT_TRANSFORM_NORMAL, 0, projection); float gl_matrix[9]; wlr_matrix_multiply(gl_matrix, renderer->projection, matrix); // TODO: investigate why matrix is flipped prior to this cmd // wlr_matrix_multiply(gl_matrix, flip_180, gl_matrix); wlr_matrix_transpose(gl_matrix, gl_matrix); if (color[3] == 1.0) { glDisable(GL_BLEND); } else { glEnable(GL_BLEND); } struct quad_shader shader = renderer->shaders.quad; glUseProgram(shader.program); glUniformMatrix3fv(shader.proj, 1, GL_FALSE, gl_matrix); glUniform4f(shader.color, color[0], color[1], color[2], color[3]); glVertexAttribPointer(shader.pos_attrib, 2, GL_FLOAT, GL_FALSE, 0, verts); glEnableVertexAttribArray(shader.pos_attrib); glDrawArrays(GL_TRIANGLE_STRIP, 0, 4); glDisableVertexAttribArray(shader.pos_attrib); } static void fx_render_stencil_mask(struct fx_renderer *renderer, const struct wlr_box *box, const float matrix[static 9], int corner_radius) { if (box->width == 0 || box->height == 0) { return; } assert(box->width > 0 && box->height > 0); // TODO: just pass gl_matrix? float gl_matrix[9]; wlr_matrix_multiply(gl_matrix, renderer->projection, matrix); // TODO: investigate why matrix is flipped prior to this cmd // wlr_matrix_multiply(gl_matrix, flip_180, gl_matrix); wlr_matrix_transpose(gl_matrix, gl_matrix); glEnable(GL_BLEND); struct stencil_mask_shader shader = renderer->shaders.stencil_mask; glUseProgram(shader.program); glUniformMatrix3fv(shader.proj, 1, GL_FALSE, gl_matrix); glUniform2f(shader.half_size, box->width * 0.5, box->height * 0.5); glUniform2f(shader.position, box->x, box->y); glUniform1f(shader.radius, corner_radius); glVertexAttribPointer(shader.pos_attrib, 2, GL_FLOAT, GL_FALSE, 0, verts); glEnableVertexAttribArray(shader.pos_attrib); glDrawArrays(GL_TRIANGLE_STRIP, 0, 4); glDisableVertexAttribArray(shader.pos_attrib); } void fx_render_box_shadow(struct fx_renderer *renderer, const struct wlr_box *box, const struct wlr_box *stencil_box, const float matrix[static 9], int corner_radius, struct shadow_data *shadow_data) { if (box->width == 0 || box->height == 0) { return; } assert(box->width > 0 && box->height > 0); float *color = shadow_data->color; float blur_sigma = shadow_data->blur_sigma; float gl_matrix[9]; wlr_matrix_multiply(gl_matrix, renderer->projection, matrix); // TODO: investigate why matrix is flipped prior to this cmd // wlr_matrix_multiply(gl_matrix, flip_180, gl_matrix); wlr_matrix_transpose(gl_matrix, gl_matrix); // Init stencil work fx_renderer_stencil_mask_init(); // Draw the rounded rect as a mask fx_render_stencil_mask(renderer, stencil_box, matrix, corner_radius); fx_renderer_stencil_mask_close(false); // blending will practically always be needed (unless we have a madman // who uses opaque shadows with zero sigma), so just enable it glEnable(GL_BLEND); glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA); struct box_shadow_shader shader = renderer->shaders.box_shadow; glUseProgram(shader.program); glUniformMatrix3fv(shader.proj, 1, GL_FALSE, gl_matrix); glUniform4f(shader.color, color[0], color[1], color[2], color[3]); glUniform1f(shader.blur_sigma, blur_sigma); glUniform1f(shader.corner_radius, corner_radius); glUniform2f(shader.size, box->width, box->height); glUniform2f(shader.position, box->x, box->y); glVertexAttribPointer(shader.pos_attrib, 2, GL_FLOAT, GL_FALSE, 0, verts); glEnableVertexAttribArray(shader.pos_attrib); glDrawArrays(GL_TRIANGLE_STRIP, 0, 4); glDisableVertexAttribArray(shader.pos_attrib); glBlendFunc(GL_ONE, GL_ONE_MINUS_SRC_ALPHA); fx_renderer_stencil_mask_fini(); }