summaryrefslogtreecommitdiff
path: root/render/fx_renderer
diff options
context:
space:
mode:
authorErik Reider <[email protected]>2024-01-06 02:31:14 +0100
committerGitHub <[email protected]>2024-01-06 02:31:14 +0100
commit6759e8da7ab53a46b0eb04e5045b8c67262c3a11 (patch)
treec65ed83ca04b61bdbae7e1b8a7f2c16f29b89730 /render/fx_renderer
parentb929a2bbadf467864796ad4ec90882ce86cfebff (diff)
parentace97585b2b4d8cbb5ead6cd0f72fa8e8889c9d7 (diff)
Merge pull request #24 from wlrfx/wlroots-0.17-rebase
Rebase to wlroots 0.17
Diffstat (limited to 'render/fx_renderer')
-rw-r--r--render/fx_renderer/fx_framebuffer.c137
-rw-r--r--render/fx_renderer/fx_pass.c509
-rw-r--r--render/fx_renderer/fx_renderer.c1016
-rw-r--r--render/fx_renderer/fx_stencilbuffer.c50
-rw-r--r--render/fx_renderer/fx_texture.c368
-rw-r--r--render/fx_renderer/gles2/shaders/box_shadow.frag5
-rw-r--r--render/fx_renderer/gles2/shaders/common.vert9
-rw-r--r--render/fx_renderer/gles2/shaders/quad.frag8
-rw-r--r--render/fx_renderer/gles2/shaders/stencil_mask.frag5
-rw-r--r--render/fx_renderer/gles2/shaders/tex.frag5
-rw-r--r--render/fx_renderer/meson.build8
-rw-r--r--render/fx_renderer/pixel_format.c175
-rw-r--r--render/fx_renderer/shaders.c203
-rw-r--r--render/fx_renderer/util.c113
14 files changed, 2137 insertions, 474 deletions
diff --git a/render/fx_renderer/fx_framebuffer.c b/render/fx_renderer/fx_framebuffer.c
new file mode 100644
index 0000000..fea101b
--- /dev/null
+++ b/render/fx_renderer/fx_framebuffer.c
@@ -0,0 +1,137 @@
+#include <stdio.h>
+#include <stdlib.h>
+#include <wlr/interfaces/wlr_buffer.h>
+#include <wlr/render/allocator.h>
+#include <wlr/render/interface.h>
+#include <wlr/render/swapchain.h>
+#include <wlr/util/log.h>
+
+#include "render/egl.h"
+#include "render/fx_renderer/fx_renderer.h"
+
+static void handle_buffer_destroy(struct wlr_addon *addon) {
+ struct fx_framebuffer *buffer =
+ wl_container_of(addon, buffer, addon);
+ fx_framebuffer_destroy(buffer);
+}
+
+static const struct wlr_addon_interface buffer_addon_impl = {
+ .name = "fx_framebuffer",
+ .destroy = handle_buffer_destroy,
+};
+
+struct fx_framebuffer *fx_framebuffer_get_or_create(struct fx_renderer *renderer,
+ struct wlr_buffer *wlr_buffer) {
+ struct wlr_addon *addon =
+ wlr_addon_find(&wlr_buffer->addons, renderer, &buffer_addon_impl);
+ if (addon) {
+ struct fx_framebuffer *buffer = wl_container_of(addon, buffer, addon);
+ return buffer;
+ }
+
+ struct fx_framebuffer *buffer = calloc(1, sizeof(*buffer));
+ if (buffer == NULL) {
+ wlr_log_errno(WLR_ERROR, "Allocation failed");
+ return NULL;
+ }
+ buffer->buffer = wlr_buffer;
+ buffer->renderer = renderer;
+
+ struct wlr_dmabuf_attributes dmabuf = {0};
+ if (!wlr_buffer_get_dmabuf(wlr_buffer, &dmabuf)) {
+ goto error_buffer;
+ }
+
+ bool external_only;
+ buffer->image = wlr_egl_create_image_from_dmabuf(renderer->egl,
+ &dmabuf, &external_only);
+ if (buffer->image == EGL_NO_IMAGE_KHR) {
+ goto error_buffer;
+ }
+
+ push_fx_debug(renderer);
+
+ glGenRenderbuffers(1, &buffer->rbo);
+ glBindRenderbuffer(GL_RENDERBUFFER, buffer->rbo);
+ renderer->procs.glEGLImageTargetRenderbufferStorageOES(GL_RENDERBUFFER,
+ buffer->image);
+ glBindRenderbuffer(GL_RENDERBUFFER, 0);
+
+ glGenFramebuffers(1, &buffer->fbo);
+ glBindFramebuffer(GL_FRAMEBUFFER, buffer->fbo);
+ glFramebufferRenderbuffer(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0,
+ GL_RENDERBUFFER, buffer->rbo);
+ GLenum fb_status = glCheckFramebufferStatus(GL_FRAMEBUFFER);
+ if (fb_status != GL_FRAMEBUFFER_COMPLETE) {
+ wlr_log(WLR_ERROR, "Failed to create FBO");
+ glBindFramebuffer(GL_FRAMEBUFFER, 0);
+ goto error_image;
+ }
+
+ // Init stencil buffer
+ glGenRenderbuffers(1, &buffer->sb);
+ glBindRenderbuffer(GL_RENDERBUFFER, buffer->sb);
+ glRenderbufferStorage(GL_RENDERBUFFER, GL_STENCIL_INDEX8,
+ wlr_buffer->width, wlr_buffer->height);
+ glFramebufferRenderbuffer(GL_FRAMEBUFFER, GL_STENCIL_ATTACHMENT,
+ GL_RENDERBUFFER, buffer->sb);
+ fb_status = glCheckFramebufferStatus(GL_FRAMEBUFFER);
+ if (fb_status != GL_FRAMEBUFFER_COMPLETE) {
+ wlr_log(WLR_ERROR,
+ "Stencil buffer incomplete, couldn't create! (FB status: %i)",
+ fb_status);
+ glBindFramebuffer(GL_FRAMEBUFFER, 0);
+ goto error_stencil;
+ }
+
+ glBindFramebuffer(GL_FRAMEBUFFER, 0);
+
+ pop_fx_debug(renderer);
+
+ wlr_addon_init(&buffer->addon, &wlr_buffer->addons, renderer,
+ &buffer_addon_impl);
+
+ wl_list_insert(&renderer->buffers, &buffer->link);
+
+ wlr_log(WLR_DEBUG, "Created GL FBO for buffer %dx%d",
+ wlr_buffer->width, wlr_buffer->height);
+
+ return buffer;
+
+error_stencil:
+ glDeleteRenderbuffers(1, &buffer->sb);
+error_image:
+ wlr_egl_destroy_image(renderer->egl, buffer->image);
+error_buffer:
+ free(buffer);
+ return NULL;
+}
+
+void fx_framebuffer_bind(struct fx_framebuffer *fx_buffer) {
+ glBindFramebuffer(GL_FRAMEBUFFER, fx_buffer->fbo);
+}
+
+void fx_framebuffer_bind_wlr_fbo(struct fx_renderer *renderer) {
+ glBindFramebuffer(GL_FRAMEBUFFER, renderer->current_buffer->fbo);
+}
+
+void fx_framebuffer_destroy(struct fx_framebuffer *fx_buffer) {
+ // Release the framebuffer
+ wl_list_remove(&fx_buffer->link);
+ wlr_addon_finish(&fx_buffer->addon);
+
+ struct wlr_egl_context prev_ctx;
+ wlr_egl_save_context(&prev_ctx);
+ wlr_egl_make_current(fx_buffer->renderer->egl);
+
+ glDeleteFramebuffers(1, &fx_buffer->fbo);
+ fx_buffer->fbo = -1;
+ glDeleteRenderbuffers(1, &fx_buffer->rbo);
+ fx_buffer->rbo = -1;
+
+ wlr_egl_destroy_image(fx_buffer->renderer->egl, fx_buffer->image);
+
+ wlr_egl_restore_context(&prev_ctx);
+
+ free(fx_buffer);
+}
diff --git a/render/fx_renderer/fx_pass.c b/render/fx_renderer/fx_pass.c
new file mode 100644
index 0000000..9a8c90c
--- /dev/null
+++ b/render/fx_renderer/fx_pass.c
@@ -0,0 +1,509 @@
+#define _POSIX_C_SOURCE 199309L
+#include <stdio.h>
+#include <stdlib.h>
+#include <assert.h>
+#include <pixman.h>
+#include <time.h>
+#include <wlr/types/wlr_matrix.h>
+#include <wlr/util/log.h>
+
+#include "render/egl.h"
+#include "render/fx_renderer/fx_renderer.h"
+#include "render/fx_renderer/matrix.h"
+#include "render/pass.h"
+#include "types/fx/shadow_data.h"
+
+#define MAX_QUADS 86 // 4kb
+
+struct fx_render_texture_options fx_render_texture_options_default(
+ const struct wlr_render_texture_options *base) {
+ struct fx_render_texture_options options = {
+ .corner_radius = 0,
+ .scale = 1.0f,
+ .clip_box = NULL,
+ };
+ memcpy(&options.base, base, sizeof(*base));
+ return options;
+}
+
+struct fx_render_rect_options fx_render_rect_options_default(
+ const struct wlr_render_rect_options *base) {
+ struct fx_render_rect_options options = {
+ .scale = 1.0f,
+ };
+ memcpy(&options.base, base, sizeof(*base));
+ return options;
+}
+
+///
+/// Base Wlroots pass functions
+///
+
+static const struct wlr_render_pass_impl render_pass_impl;
+
+static struct fx_gles_render_pass *get_render_pass(struct wlr_render_pass *wlr_pass) {
+ assert(wlr_pass->impl == &render_pass_impl);
+ struct fx_gles_render_pass *pass = wl_container_of(wlr_pass, pass, base);
+ return pass;
+}
+
+static bool render_pass_submit(struct wlr_render_pass *wlr_pass) {
+ struct fx_gles_render_pass *pass = get_render_pass(wlr_pass);
+ struct fx_renderer *renderer = pass->buffer->renderer;
+ struct fx_render_timer *timer = pass->timer;
+
+ push_fx_debug(renderer);
+
+ if (timer) {
+ // clear disjoint flag
+ GLint64 disjoint;
+ renderer->procs.glGetInteger64vEXT(GL_GPU_DISJOINT_EXT, &disjoint);
+ // set up the query
+ renderer->procs.glQueryCounterEXT(timer->id, GL_TIMESTAMP_EXT);
+ // get end-of-CPU-work time in GL time domain
+ renderer->procs.glGetInteger64vEXT(GL_TIMESTAMP_EXT, &timer->gl_cpu_end);
+ // get end-of-CPU-work time in CPU time domain
+ clock_gettime(CLOCK_MONOTONIC, &timer->cpu_end);
+ }
+
+ glFlush();
+ glBindFramebuffer(GL_FRAMEBUFFER, 0);
+
+ pop_fx_debug(renderer);
+
+ wlr_buffer_unlock(pass->buffer->buffer);
+ free(pass);
+
+ return true;
+}
+
+static void render_pass_add_texture(struct wlr_render_pass *wlr_pass,
+ const struct wlr_render_texture_options *options) {
+ struct fx_gles_render_pass *pass = get_render_pass(wlr_pass);
+ const struct fx_render_texture_options fx_options =
+ fx_render_texture_options_default(options);
+ // Re-use fx function but with default options
+ fx_render_pass_add_texture(pass, &fx_options);
+}
+
+static void render_pass_add_rect(struct wlr_render_pass *wlr_pass,
+ const struct wlr_render_rect_options *options) {
+ struct fx_gles_render_pass *pass = get_render_pass(wlr_pass);
+ const struct fx_render_rect_options fx_options =
+ fx_render_rect_options_default(options);
+ // Re-use fx function but with default options
+ fx_render_pass_add_rect(pass, &fx_options);
+}
+
+static const struct wlr_render_pass_impl render_pass_impl = {
+ .submit = render_pass_submit,
+ .add_texture = render_pass_add_texture,
+ .add_rect = render_pass_add_rect,
+};
+
+///
+/// FX pass functions
+///
+
+static void render(const struct wlr_box *box, const pixman_region32_t *clip, GLint attrib) {
+ pixman_region32_t region;
+ pixman_region32_init_rect(&region, box->x, box->y, box->width, box->height);
+
+ if (clip) {
+ pixman_region32_intersect(&region, &region, clip);
+ }
+
+ int rects_len;
+ const pixman_box32_t *rects = pixman_region32_rectangles(&region, &rects_len);
+ if (rects_len == 0) {
+ pixman_region32_fini(&region);
+ return;
+ }
+
+ glEnableVertexAttribArray(attrib);
+
+ for (int i = 0; i < rects_len;) {
+ int batch = rects_len - i < MAX_QUADS ? rects_len - i : MAX_QUADS;
+ int batch_end = batch + i;
+
+ size_t vert_index = 0;
+ GLfloat verts[MAX_QUADS * 6 * 2];
+ for (; i < batch_end; i++) {
+ const pixman_box32_t *rect = &rects[i];
+
+ verts[vert_index++] = (GLfloat)(rect->x1 - box->x) / box->width;
+ verts[vert_index++] = (GLfloat)(rect->y1 - box->y) / box->height;
+ verts[vert_index++] = (GLfloat)(rect->x2 - box->x) / box->width;
+ verts[vert_index++] = (GLfloat)(rect->y1 - box->y) / box->height;
+ verts[vert_index++] = (GLfloat)(rect->x1 - box->x) / box->width;
+ verts[vert_index++] = (GLfloat)(rect->y2 - box->y) / box->height;
+ verts[vert_index++] = (GLfloat)(rect->x2 - box->x) / box->width;
+ verts[vert_index++] = (GLfloat)(rect->y1 - box->y) / box->height;
+ verts[vert_index++] = (GLfloat)(rect->x2 - box->x) / box->width;
+ verts[vert_index++] = (GLfloat)(rect->y2 - box->y) / box->height;
+ verts[vert_index++] = (GLfloat)(rect->x1 - box->x) / box->width;
+ verts[vert_index++] = (GLfloat)(rect->y2 - box->y) / box->height;
+ }
+
+ glVertexAttribPointer(attrib, 2, GL_FLOAT, GL_FALSE, 0, verts);
+ glDrawArrays(GL_TRIANGLES, 0, batch * 6);
+ }
+
+ glDisableVertexAttribArray(attrib);
+
+ pixman_region32_fini(&region);
+}
+
+static void set_proj_matrix(GLint loc, float proj[9], const struct wlr_box *box) {
+ float gl_matrix[9];
+ wlr_matrix_identity(gl_matrix);
+ wlr_matrix_translate(gl_matrix, box->x, box->y);
+ wlr_matrix_scale(gl_matrix, box->width, box->height);
+ wlr_matrix_multiply(gl_matrix, proj, gl_matrix);
+ glUniformMatrix3fv(loc, 1, GL_FALSE, gl_matrix);
+}
+
+static void set_tex_matrix(GLint loc, enum wl_output_transform trans,
+ const struct wlr_fbox *box) {
+ float tex_matrix[9];
+ wlr_matrix_identity(tex_matrix);
+ wlr_matrix_translate(tex_matrix, box->x, box->y);
+ wlr_matrix_scale(tex_matrix, box->width, box->height);
+ wlr_matrix_translate(tex_matrix, .5, .5);
+
+ // since textures have a different origin point we have to transform
+ // differently if we are rotating
+ if (trans & WL_OUTPUT_TRANSFORM_90) {
+ wlr_matrix_transform(tex_matrix, wlr_output_transform_invert(trans));
+ } else {
+ wlr_matrix_transform(tex_matrix, trans);
+ }
+ wlr_matrix_translate(tex_matrix, -.5, -.5);
+
+ glUniformMatrix3fv(loc, 1, GL_FALSE, tex_matrix);
+}
+
+static void setup_blending(enum wlr_render_blend_mode mode) {
+ switch (mode) {
+ case WLR_RENDER_BLEND_MODE_PREMULTIPLIED:
+ glEnable(GL_BLEND);
+ break;
+ case WLR_RENDER_BLEND_MODE_NONE:
+ glDisable(GL_BLEND);
+ break;
+ }
+}
+
+// Initialize the stenciling work
+static void 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);
+}
+
+// Close the mask
+static void 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);
+}
+
+// Finish stenciling and clear the buffer
+static void stencil_mask_fini(void) {
+ glClearStencil(0);
+ glClear(GL_STENCIL_BUFFER_BIT);
+ glDisable(GL_STENCIL_TEST);
+}
+
+// make sure the texture source box does not try and sample outside of the
+// texture
+static void check_tex_src_box(const struct wlr_render_texture_options *options) {
+ if (!wlr_fbox_empty(&options->src_box)) {
+ const struct wlr_fbox *box = &options->src_box;
+ assert(box->x >= 0 && box->y >= 0 &&
+ box->x + box->width <= options->texture->width &&
+ box->y + box->height <= options->texture->height);
+ }
+}
+
+void fx_render_pass_add_texture(struct fx_gles_render_pass *pass,
+ const struct fx_render_texture_options *fx_options) {
+ const struct wlr_render_texture_options *options = &fx_options->base;
+
+ check_tex_src_box(options);
+
+ struct fx_renderer *renderer = pass->buffer->renderer;
+ struct fx_texture *texture = fx_get_texture(options->texture);
+
+ struct tex_shader *shader = NULL;
+
+ switch (texture->target) {
+ case GL_TEXTURE_2D:
+ if (texture->has_alpha) {
+ shader = &renderer->shaders.tex_rgba;
+ } else {
+ shader = &renderer->shaders.tex_rgbx;
+ }
+ break;
+ case GL_TEXTURE_EXTERNAL_OES:
+ // EGL_EXT_image_dma_buf_import_modifiers requires
+ // GL_OES_EGL_image_external
+ assert(renderer->exts.OES_egl_image_external);
+ shader = &renderer->shaders.tex_ext;
+ break;
+ default:
+ abort();
+ }
+
+ struct wlr_box dst_box;
+ struct wlr_fbox src_fbox;
+ wlr_render_texture_options_get_src_box(options, &src_fbox);
+ wlr_render_texture_options_get_dst_box(options, &dst_box);
+ float alpha = wlr_render_texture_options_get_alpha(options);
+
+ struct wlr_box *clip_box = &dst_box;
+ if (!wlr_box_empty(fx_options->clip_box)) {
+ clip_box = fx_options->clip_box;
+ }
+
+ src_fbox.x /= options->texture->width;
+ src_fbox.y /= options->texture->height;
+ src_fbox.width /= options->texture->width;
+ src_fbox.height /= options->texture->height;
+
+ push_fx_debug(renderer);
+ setup_blending(!texture->has_alpha && alpha == 1.0 ?
+ WLR_RENDER_BLEND_MODE_NONE : options->blend_mode);
+
+ glUseProgram(shader->program);
+
+ glActiveTexture(GL_TEXTURE0);
+ glBindTexture(texture->target, texture->tex);
+
+ switch (options->filter_mode) {
+ case WLR_SCALE_FILTER_BILINEAR:
+ glTexParameteri(texture->target, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
+ glTexParameteri(texture->target, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
+ break;
+ case WLR_SCALE_FILTER_NEAREST:
+ glTexParameteri(texture->target, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
+ glTexParameteri(texture->target, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
+ break;
+ }
+
+ glUniform1i(shader->tex, 0);
+ glUniform1f(shader->alpha, alpha);
+ glUniform2f(shader->size, clip_box->width, clip_box->height);
+ glUniform2f(shader->position, clip_box->x, clip_box->y);
+ glUniform1f(shader->radius, fx_options->corner_radius);
+
+ set_proj_matrix(shader->proj, pass->projection_matrix, &dst_box);
+ set_tex_matrix(shader->tex_proj, options->transform, &src_fbox);
+
+ render(&dst_box, options->clip, shader->pos_attrib);
+
+ glBindTexture(texture->target, 0);
+ pop_fx_debug(renderer);
+}
+
+void fx_render_pass_add_rect(struct fx_gles_render_pass *pass,
+ const struct fx_render_rect_options *fx_options) {
+ const struct wlr_render_rect_options *options = &fx_options->base;
+
+ struct fx_renderer *renderer = pass->buffer->renderer;
+
+ const struct wlr_render_color *color = &options->color;
+ struct wlr_box box;
+ wlr_render_rect_options_get_box(options, pass->buffer->buffer, &box);
+
+ push_fx_debug(renderer);
+ setup_blending(color->a == 1.0 ? WLR_RENDER_BLEND_MODE_NONE : options->blend_mode);
+
+ glUseProgram(renderer->shaders.quad.program);
+
+ set_proj_matrix(renderer->shaders.quad.proj, pass->projection_matrix, &box);
+ glUniform4f(renderer->shaders.quad.color, color->r, color->g, color->b, color->a);
+
+ render(&box, options->clip, renderer->shaders.quad.pos_attrib);
+
+ pop_fx_debug(renderer);
+}
+
+void fx_render_pass_add_stencil_mask(struct fx_gles_render_pass *pass,
+ const struct fx_render_rect_options *fx_options, int corner_radius) {
+ const struct wlr_render_rect_options *options = &fx_options->base;
+
+ struct fx_renderer *renderer = pass->buffer->renderer;
+
+ const struct wlr_render_color *color = &options->color;
+ struct wlr_box box;
+ wlr_render_rect_options_get_box(options, pass->buffer->buffer, &box);
+ assert(box.width > 0 && box.height > 0);
+
+ push_fx_debug(renderer);
+ setup_blending(WLR_RENDER_BLEND_MODE_PREMULTIPLIED);
+
+ glUseProgram(renderer->shaders.stencil_mask.program);
+
+ set_proj_matrix(renderer->shaders.stencil_mask.proj, pass->projection_matrix, &box);
+ glUniform4f(renderer->shaders.stencil_mask.color, color->r, color->g, color->b, color->a);
+ glUniform2f(renderer->shaders.stencil_mask.half_size, box.width * 0.5, box.height * 0.5);
+ glUniform2f(renderer->shaders.stencil_mask.position, box.x, box.y);
+ glUniform1f(renderer->shaders.stencil_mask.radius, corner_radius);
+
+ render(&box, options->clip, renderer->shaders.stencil_mask.pos_attrib);
+
+ pop_fx_debug(renderer);
+}
+
+void fx_render_pass_add_box_shadow(struct fx_gles_render_pass *pass,
+ const struct fx_render_rect_options *fx_options,
+ int corner_radius, struct shadow_data *shadow_data) {
+ const struct wlr_render_rect_options *options = &fx_options->base;
+
+ struct fx_renderer *renderer = pass->buffer->renderer;
+
+ const struct wlr_render_color *color = &shadow_data->color;
+ struct wlr_box box;
+ wlr_render_rect_options_get_box(options, pass->buffer->buffer, &box);
+ assert(box.width > 0 && box.height > 0);
+ struct wlr_box surface_box = box;
+ float blur_sigma = shadow_data->blur_sigma * fx_options->scale;
+
+ // Extend the size of the box
+ box.x -= blur_sigma;
+ box.y -= blur_sigma;
+ box.width += blur_sigma * 2;
+ box.height += blur_sigma * 2;
+
+ pixman_region32_t render_region;
+ pixman_region32_init(&render_region);
+
+ pixman_region32_t inner_region;
+ pixman_region32_init(&inner_region);
+ pixman_region32_union_rect(&inner_region, &inner_region,
+ surface_box.x + corner_radius * 0.5,
+ surface_box.y + corner_radius * 0.5,
+ surface_box.width - corner_radius,
+ surface_box.height - corner_radius);
+ pixman_region32_subtract(&render_region, options->clip, &inner_region);
+ pixman_region32_fini(&inner_region);
+
+ push_fx_debug(renderer);
+
+ // Init stencil work
+ stencil_mask_init();
+ // Draw the rounded rect as a mask
+ fx_render_pass_add_stencil_mask(pass, fx_options, corner_radius);
+ 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
+ setup_blending(WLR_RENDER_BLEND_MODE_PREMULTIPLIED);
+ glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
+
+ glUseProgram(renderer->shaders.box_shadow.program);
+
+ set_proj_matrix(renderer->shaders.box_shadow.proj, pass->projection_matrix, &box);
+ glUniform4f(renderer->shaders.box_shadow.color, color->r, color->g, color->b, color->a);
+ glUniform1f(renderer->shaders.box_shadow.blur_sigma, blur_sigma);
+ glUniform1f(renderer->shaders.box_shadow.corner_radius, corner_radius);
+ glUniform2f(renderer->shaders.box_shadow.size, box.width, box.height);
+ glUniform2f(renderer->shaders.box_shadow.position, box.x, box.y);
+
+ render(&box, &render_region, renderer->shaders.box_shadow.pos_attrib);
+ pixman_region32_fini(&render_region);
+
+ glBlendFunc(GL_ONE, GL_ONE_MINUS_SRC_ALPHA);
+
+ stencil_mask_fini();
+
+ pop_fx_debug(renderer);
+}
+
+static const char *reset_status_str(GLenum status) {
+ switch (status) {
+ case GL_GUILTY_CONTEXT_RESET_KHR:
+ return "guilty";
+ case GL_INNOCENT_CONTEXT_RESET_KHR:
+ return "innocent";
+ case GL_UNKNOWN_CONTEXT_RESET_KHR:
+ return "unknown";
+ default:
+ return "<invalid>";
+ }
+}
+
+static struct fx_gles_render_pass *begin_buffer_pass(struct fx_framebuffer *buffer,
+ struct fx_render_timer *timer) {
+ struct fx_renderer *renderer = buffer->renderer;
+ struct wlr_buffer *wlr_buffer = buffer->buffer;
+
+ if (renderer->procs.glGetGraphicsResetStatusKHR) {
+ GLenum status = renderer->procs.glGetGraphicsResetStatusKHR();
+ if (status != GL_NO_ERROR) {
+ wlr_log(WLR_ERROR, "GPU reset (%s)", reset_status_str(status));
+ wl_signal_emit_mutable(&renderer->wlr_renderer.events.lost, NULL);
+ return NULL;
+ }
+ }
+
+ struct fx_gles_render_pass *pass = calloc(1, sizeof(*pass));
+ if (pass == NULL) {
+ return NULL;
+ }
+
+ wlr_render_pass_init(&pass->base, &render_pass_impl);
+ wlr_buffer_lock(wlr_buffer);
+ pass->buffer = buffer;
+ pass->timer = timer;
+
+ matrix_projection(pass->projection_matrix, wlr_buffer->width, wlr_buffer->height,
+ WL_OUTPUT_TRANSFORM_FLIPPED_180);
+
+ push_fx_debug(renderer);
+ glBindFramebuffer(GL_FRAMEBUFFER, buffer->fbo);
+
+ glViewport(0, 0, wlr_buffer->width, wlr_buffer->height);
+ glBlendFunc(GL_ONE, GL_ONE_MINUS_SRC_ALPHA);
+ glDisable(GL_SCISSOR_TEST);
+ pop_fx_debug(renderer);
+
+ return pass;
+}
+
+struct fx_gles_render_pass *fx_renderer_begin_buffer_pass(struct wlr_renderer *wlr_renderer,
+ struct wlr_buffer *wlr_buffer, const struct wlr_buffer_pass_options *options) {
+ struct fx_renderer *renderer = fx_get_renderer(wlr_renderer);
+ if (!wlr_egl_make_current(renderer->egl)) {
+ return NULL;
+ }
+
+ struct fx_render_timer *timer = NULL;
+ if (options->timer) {
+ timer = fx_get_render_timer(options->timer);
+ clock_gettime(CLOCK_MONOTONIC, &timer->cpu_start);
+ }
+
+ struct fx_framebuffer *buffer = fx_framebuffer_get_or_create(renderer, wlr_buffer);
+ if (!buffer) {
+ return NULL;
+ }
+
+ struct fx_gles_render_pass *pass = begin_buffer_pass(buffer, timer);
+ if (!pass) {
+ return NULL;
+ }
+ return pass;
+}
diff --git a/render/fx_renderer/fx_renderer.c b/render/fx_renderer/fx_renderer.c
index 730e314..da9437c 100644
--- a/render/fx_renderer/fx_renderer.c
+++ b/render/fx_renderer/fx_renderer.c
@@ -3,27 +3,28 @@
https://gitlab.freedesktop.org/wlroots/wlroots/-/tree/master/render/gles2
*/
+#define _POSIX_C_SOURCE 199309L
#include <assert.h>
+#include <drm_fourcc.h>
#include <GLES2/gl2.h>
#include <stdio.h>
#include <stdlib.h>
+#include <time.h>
+#include <unistd.h>
#include <wlr/backend.h>
+#include <wlr/render/allocator.h>
#include <wlr/render/egl.h>
-#include <wlr/render/gles2.h>
+#include <wlr/render/interface.h>
#include <wlr/types/wlr_matrix.h>
#include <wlr/util/box.h>
#include <wlr/util/log.h>
+#include "render/egl.h"
#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"
+#include "render/fx_renderer/util.h"
+#include "render/pixel_format.h"
+#include "util/time.h"
static const GLfloat verts[] = {
1, 0, // top right
@@ -32,372 +33,179 @@ static const GLfloat verts[] = {
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;
- }
+static const struct wlr_renderer_impl renderer_impl;
+static const struct wlr_render_timer_impl render_timer_impl;
- return shader;
+bool wlr_renderer_is_fx(struct wlr_renderer *wlr_renderer) {
+ return wlr_renderer->impl == &renderer_impl;
}
-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;
+struct fx_renderer *fx_get_renderer(
+ struct wlr_renderer *wlr_renderer) {
+ assert(wlr_renderer_is_fx(wlr_renderer));
+ struct fx_renderer *renderer = wl_container_of(wlr_renderer, renderer, wlr_renderer);
+ return renderer;
}
-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");
+static struct fx_renderer *fx_get_renderer_in_context(
+ struct wlr_renderer *wlr_renderer) {
+ struct fx_renderer *renderer = fx_get_renderer(wlr_renderer);
+ assert(wlr_egl_is_current(renderer->egl));
+ assert(renderer->current_buffer != NULL);
+ return renderer;
+}
- return true;
+bool wlr_render_timer_is_fx(struct wlr_render_timer *timer) {
+ return timer->impl == &render_timer_impl;
}
-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);
+struct fx_render_timer *fx_get_render_timer(struct wlr_render_timer *wlr_timer) {
+ assert(wlr_render_timer_is_fx(wlr_timer));
+ struct fx_render_timer *timer = wl_container_of(wlr_timer, timer, base);
+ return timer;
+}
- GLuint prog;
- shader->program = prog = link_program(frag_src);
- if (!shader->program) {
- return false;
- }
+static bool fx_bind_main_buffer(struct wlr_renderer *wlr_renderer,
+ struct wlr_buffer *wlr_buffer) {
+ struct fx_renderer *renderer = fx_get_renderer(wlr_renderer);
- 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");
+ if (renderer->current_buffer != NULL) {
+ assert(wlr_egl_is_current(renderer->egl));
- return true;
-}
+ push_fx_debug(renderer);
+ glFlush();
+ glBindFramebuffer(GL_FRAMEBUFFER, 0);
+ pop_fx_debug(renderer);
-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;
+ wlr_buffer_unlock(renderer->current_buffer->buffer);
+ renderer->current_buffer = NULL;
}
- 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");
+ if (wlr_buffer == NULL) {
+ wlr_egl_unset_current(renderer->egl);
+ return true;
+ }
- return true;
-}
+ wlr_egl_make_current(renderer->egl);
-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) {
+ struct fx_framebuffer *buffer = fx_framebuffer_get_or_create(renderer, wlr_buffer);
+ if (buffer == NULL) {
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;
-}
+ wlr_buffer_lock(wlr_buffer);
+ renderer->current_buffer = buffer;
-static bool check_gl_ext(const char *exts, const char *ext) {
- size_t extlen = strlen(ext);
- const char *end = exts + strlen(exts);
+ push_fx_debug(renderer);
+ glBindFramebuffer(GL_FRAMEBUFFER, renderer->current_buffer->fbo);
+ pop_fx_debug(renderer);
- 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;
+ return true;
}
-static void load_gl_proc(void *proc_ptr, const char *name) {
- void *proc = (void *)eglGetProcAddress(name);
- if (proc == NULL) {
- wlr_log(WLR_ERROR, "GLES2 RENDERER: eglGetProcAddress(%s) failed", name);
- abort();
+static const char *reset_status_str(GLenum status) {
+ switch (status) {
+ case GL_GUILTY_CONTEXT_RESET_KHR:
+ return "guilty";
+ case GL_INNOCENT_CONTEXT_RESET_KHR:
+ return "innocent";
+ case GL_UNKNOWN_CONTEXT_RESET_KHR:
+ return "unknown";
+ default:
+ return "<invalid>";
}
- *(void **)proc_ptr = proc;
}
-static void fx_renderer_handle_destroy(struct wlr_addon *addon) {
+static bool fx_renderer_begin(struct wlr_renderer *wlr_renderer, uint32_t width,
+ uint32_t height) {
struct fx_renderer *renderer =
- wl_container_of(addon, renderer, addon);
- fx_renderer_fini(renderer);
- 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_addon_set *addons,
- const void * owner) {
- struct fx_renderer *renderer = fx_renderer_create(egl);
- 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 fx_renderer *renderer = calloc(1, sizeof(struct fx_renderer));
- if (renderer == NULL) {
- return NULL;
- }
-
- if (!eglMakeCurrent(wlr_egl_get_display(egl), EGL_NO_SURFACE, EGL_NO_SURFACE,
- wlr_egl_get_context(egl))) {
- wlr_log(WLR_ERROR, "GLES2 RENDERER: Could not make EGL current");
- return NULL;
- }
-
- renderer->stencil_buffer = fx_stencilbuffer_create();
-
- // get extensions
- const char *exts_str = (const char *)glGetString(GL_EXTENSIONS);
- if (exts_str == NULL) {
- wlr_log(WLR_ERROR, "GLES2 RENDERER: Failed to get GL_EXTENSIONS");
- return NULL;
- }
-
- wlr_log(WLR_INFO, "Creating scenefx GLES2 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 GLES2 extensions: %s", exts_str);
+ fx_get_renderer_in_context(wlr_renderer);
- // 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");
- }
+ push_fx_debug(renderer);
- // 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 (renderer->procs.glGetGraphicsResetStatusKHR) {
+ GLenum status = renderer->procs.glGetGraphicsResetStatusKHR();
+ if (status != GL_NO_ERROR) {
+ wlr_log(WLR_ERROR, "GPU reset (%s)", reset_status_str(status));
+ wl_signal_emit_mutable(&wlr_renderer->events.lost, NULL);
+ pop_fx_debug(renderer);
+ return false;
+ }
}
- if (!eglMakeCurrent(wlr_egl_get_display(egl),
- EGL_NO_SURFACE, EGL_NO_SURFACE, EGL_NO_CONTEXT)) {
- wlr_log(WLR_ERROR, "GLES2 RENDERER: Could not unset current EGL");
- goto error;
- }
+ glViewport(0, 0, width, height);
+ renderer->viewport_width = width;
+ renderer->viewport_height = height;
- wlr_log(WLR_INFO, "GLES2 RENDERER: Shaders Initialized Successfully");
- return renderer;
+ // refresh projection matrix
+ matrix_projection(renderer->projection, width, height,
+ WL_OUTPUT_TRANSFORM_FLIPPED_180);
-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);
+ glBlendFunc(GL_ONE, GL_ONE_MINUS_SRC_ALPHA);
- if (!eglMakeCurrent(wlr_egl_get_display(egl),
- EGL_NO_SURFACE, EGL_NO_SURFACE, EGL_NO_CONTEXT)) {
- wlr_log(WLR_ERROR, "GLES2 RENDERER: Could not unset current EGL");
- }
+ // XXX: maybe we should save output projection and remove some of the need
+ // for users to sling matricies themselves
- // TODO: more freeing?
- free(renderer);
+ pop_fx_debug(renderer);
- wlr_log(WLR_ERROR, "GLES2 RENDERER: Error Initializing Shaders");
- return NULL;
+ return true;
}
-void fx_renderer_fini(struct fx_renderer *renderer) {
- fx_stencilbuffer_release(&renderer->stencil_buffer);
+static void fx_renderer_end(struct wlr_renderer *wlr_renderer) {
+ fx_get_renderer_in_context(wlr_renderer);
+ // no-op
}
-void fx_renderer_begin(struct fx_renderer *renderer, int width, int height) {
- fx_stencilbuffer_init(&renderer->stencil_buffer, width, height);
-
- glViewport(0, 0, width, height);
-
- // refresh projection matrix
- matrix_projection(renderer->projection, width, height,
- WL_OUTPUT_TRANSFORM_FLIPPED_180);
-
- glBlendFunc(GL_ONE, GL_ONE_MINUS_SRC_ALPHA);
-}
+static void fx_renderer_clear(struct wlr_renderer *wlr_renderer,
+ const float color[static 4]) {
+ struct fx_renderer *renderer =
+ fx_get_renderer_in_context(wlr_renderer);
-void fx_renderer_clear(const float color[static 4]) {
+ push_fx_debug(renderer);
glClearColor(color[0], color[1], color[2], color[3]);
glClearStencil(0);
glClear(GL_COLOR_BUFFER_BIT | GL_STENCIL_BUFFER_BIT);
+ pop_fx_debug(renderer);
}
-void fx_renderer_scissor(struct wlr_box *box) {
- if (box) {
+static void fx_renderer_scissor(struct wlr_renderer *wlr_renderer,
+ struct wlr_box *box) {
+ struct fx_renderer *renderer =
+ fx_get_renderer_in_context(wlr_renderer);
+
+ push_fx_debug(renderer);
+ if (box != NULL) {
glScissor(box->x, box->y, box->width, box->height);
glEnable(GL_SCISSOR_TEST);
} else {
glDisable(GL_SCISSOR_TEST);
}
+ pop_fx_debug(renderer);
}
-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) {
-
- assert(wlr_texture_is_gles2(wlr_texture));
- struct wlr_gles2_texture_attribs texture_attrs;
- wlr_gles2_texture_get_attribs(wlr_texture, &texture_attrs);
+static bool fx_render_subtexture_with_matrix(
+ struct wlr_renderer *wlr_renderer, struct wlr_texture *wlr_texture,
+ const struct wlr_fbox *box, const float matrix[static 9],
+ float alpha) {
+ struct fx_renderer *renderer = fx_get_renderer_in_context(wlr_renderer);
+ struct fx_texture *texture = fx_get_texture(wlr_texture);
+ assert(texture->fx_renderer == renderer);
struct tex_shader *shader = NULL;
- switch (texture_attrs.target) {
+ switch (texture->target) {
case GL_TEXTURE_2D:
- if (texture_attrs.has_alpha) {
+ if (texture->has_alpha) {
shader = &renderer->shaders.tex_rgba;
} else {
shader = &renderer->shaders.tex_rgbx;
}
break;
case GL_TEXTURE_EXTERNAL_OES:
+ // EGL_EXT_image_dma_buf_import_modifiers requires
+ // GL_OES_EGL_image_external
+ assert(renderer->exts.OES_egl_image_external);
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");
@@ -407,12 +215,9 @@ bool fx_render_subtexture_with_matrix(struct fx_renderer *renderer,
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);
+ push_fx_debug(renderer);
- // if there's no opacity or rounded corners we don't need to blend
- if (!texture_attrs.has_alpha && opacity == 1.0 && !corner_radius) {
+ if (!texture->has_alpha && alpha == 1.0) {
glDisable(GL_BLEND);
} else {
glEnable(GL_BLEND);
@@ -421,62 +226,49 @@ bool fx_render_subtexture_with_matrix(struct fx_renderer *renderer,
glBlendFunc(GL_ONE, GL_ONE_MINUS_SRC_ALPHA);
glActiveTexture(GL_TEXTURE0);
- glBindTexture(texture_attrs.target, texture_attrs.tex);
+ glBindTexture(texture->target, texture->tex);
- glTexParameteri(texture_attrs.target, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
+ glTexParameteri(texture->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
- };
+ glUniform1f(shader->alpha, alpha);
+ glUniform2f(shader->size, box->width, box->height);
+ glUniform2f(shader->position, box->x, box->y);
+ glUniform1f(shader->radius, 0);
+
+ float tex_matrix[9];
+ wlr_matrix_identity(tex_matrix);
+ wlr_matrix_translate(tex_matrix, box->x / texture->wlr_texture.width,
+ box->y / texture->wlr_texture.height);
+ wlr_matrix_scale(tex_matrix, box->width / texture->wlr_texture.width,
+ box->height / texture->wlr_texture.height);
+ glUniformMatrix3fv(shader->tex_proj, 1, GL_FALSE, tex_matrix);
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);
+ glBindTexture(texture->target, 0);
+ pop_fx_debug(renderer);
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);
+static void fx_render_quad_with_matrix(struct wlr_renderer *wlr_renderer,
+ const float color[static 4], const float matrix[static 9]) {
+ struct fx_renderer *renderer = fx_get_renderer_in_context(wlr_renderer);
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);
+ push_fx_debug(renderer);
if (color[3] == 1.0) {
glDisable(GL_BLEND);
@@ -484,115 +276,505 @@ void fx_render_rect(struct fx_renderer *renderer, const struct wlr_box *box,
glEnable(GL_BLEND);
}
- struct quad_shader shader = renderer->shaders.quad;
- glUseProgram(shader.program);
+ glUseProgram(renderer->shaders.quad.program);
- glUniformMatrix3fv(shader.proj, 1, GL_FALSE, gl_matrix);
- glUniform4f(shader.color, color[0], color[1], color[2], color[3]);
+ glUniformMatrix3fv(renderer->shaders.quad.proj, 1, GL_FALSE, gl_matrix);
+ glUniform4f(renderer->shaders.quad.color, color[0], color[1], color[2], color[3]);
- glVertexAttribPointer(shader.pos_attrib, 2, GL_FLOAT, GL_FALSE,
+ glVertexAttribPointer(renderer->shaders.quad.pos_attrib, 2, GL_FLOAT, GL_FALSE,
0, verts);
- glEnableVertexAttribArray(shader.pos_attrib);
+ glEnableVertexAttribArray(renderer->shaders.quad.pos_attrib);
glDrawArrays(GL_TRIANGLE_STRIP, 0, 4);
- glDisableVertexAttribArray(shader.pos_attrib);
+ glDisableVertexAttribArray(renderer->shaders.quad.pos_attrib);
+
+ pop_fx_debug(renderer);
}
-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;
+static const uint32_t *fx_get_shm_texture_formats(
+ struct wlr_renderer *wlr_renderer, size_t *len) {
+ struct fx_renderer *renderer = fx_get_renderer(wlr_renderer);
+ return get_fx_shm_formats(renderer, len);
+}
+
+static const struct wlr_drm_format_set *fx_get_dmabuf_texture_formats(
+ struct wlr_renderer *wlr_renderer) {
+ struct fx_renderer *renderer = fx_get_renderer(wlr_renderer);
+ return wlr_egl_get_dmabuf_texture_formats(renderer->egl);
+}
+
+static const struct wlr_drm_format_set *fx_get_render_formats(
+ struct wlr_renderer *wlr_renderer) {
+ struct fx_renderer *renderer = fx_get_renderer(wlr_renderer);
+ return wlr_egl_get_dmabuf_render_formats(renderer->egl);
+}
+
+static uint32_t fx_preferred_read_format(
+ struct wlr_renderer *wlr_renderer) {
+ struct fx_renderer *renderer =
+ fx_get_renderer_in_context(wlr_renderer);
+
+ push_fx_debug(renderer);
+
+ GLint gl_format = -1, gl_type = -1, alpha_size = -1;
+ glGetIntegerv(GL_IMPLEMENTATION_COLOR_READ_FORMAT, &gl_format);
+ glGetIntegerv(GL_IMPLEMENTATION_COLOR_READ_TYPE, &gl_type);
+ glGetIntegerv(GL_ALPHA_BITS, &alpha_size);
+
+ pop_fx_debug(renderer);
+
+ const struct fx_pixel_format *fmt =
+ get_fx_format_from_gl(gl_format, gl_type, alpha_size > 0);
+ if (fmt != NULL) {
+ return fmt->drm_format;
}
- assert(box->width > 0 && box->height > 0);
- // TODO: just pass gl_matrix?
- float gl_matrix[9];
- wlr_matrix_multiply(gl_matrix, renderer->projection, matrix);
+ if (renderer->exts.EXT_read_format_bgra) {
+ return DRM_FORMAT_XRGB8888;
+ }
+ return DRM_FORMAT_XBGR8888;
+}
- // TODO: investigate why matrix is flipped prior to this cmd
- // wlr_matrix_multiply(gl_matrix, flip_180, gl_matrix);
+static bool fx_read_pixels(struct wlr_renderer *wlr_renderer,
+ uint32_t drm_format, uint32_t stride,
+ uint32_t width, uint32_t height, uint32_t src_x, uint32_t src_y,
+ uint32_t dst_x, uint32_t dst_y, void *data) {
+ struct fx_renderer *renderer =
+ fx_get_renderer_in_context(wlr_renderer);
- wlr_matrix_transpose(gl_matrix, gl_matrix);
+ const struct fx_pixel_format *fmt =
+ get_fx_format_from_drm(drm_format);
+ if (fmt == NULL || !is_fx_pixel_format_supported(renderer, fmt)) {
+ wlr_log(WLR_ERROR, "Cannot read pixels: unsupported pixel format 0x%"PRIX32, drm_format);
+ return false;
+ }
- glEnable(GL_BLEND);
+ if (fmt->gl_format == GL_BGRA_EXT && !renderer->exts.EXT_read_format_bgra) {
+ wlr_log(WLR_ERROR,
+ "Cannot read pixels: missing GL_EXT_read_format_bgra extension");
+ return false;
+ }
- struct stencil_mask_shader shader = renderer->shaders.stencil_mask;
+ const struct wlr_pixel_format_info *drm_fmt =
+ drm_get_pixel_format_info(fmt->drm_format);
+ assert(drm_fmt);
+ if (pixel_format_info_pixels_per_block(drm_fmt) != 1) {
+ wlr_log(WLR_ERROR, "Cannot read pixels: block formats are not supported");
+ return false;
+ }
- glUseProgram(shader.program);
+ push_fx_debug(renderer);
- glUniformMatrix3fv(shader.proj, 1, GL_FALSE, gl_matrix);
+ // Make sure any pending drawing is finished before we try to read it
+ glFinish();
- glUniform2f(shader.half_size, box->width * 0.5, box->height * 0.5);
- glUniform2f(shader.position, box->x, box->y);
- glUniform1f(shader.radius, corner_radius);
+ glGetError(); // Clear the error flag
- glVertexAttribPointer(shader.pos_attrib, 2, GL_FLOAT, GL_FALSE,
- 0, verts);
+ unsigned char *p = (unsigned char *)data + dst_y * stride;
+ glPixelStorei(GL_PACK_ALIGNMENT, 1);
+ uint32_t pack_stride = pixel_format_info_min_stride(drm_fmt, width);
+ if (pack_stride == stride && dst_x == 0) {
+ // Under these particular conditions, we can read the pixels with only
+ // one glReadPixels call
- glEnableVertexAttribArray(shader.pos_attrib);
+ glReadPixels(src_x, src_y, width, height, fmt->gl_format, fmt->gl_type, p);
+ } else {
+ // Unfortunately GLES2 doesn't support GL_PACK_ROW_LENGTH, so we have to read
+ // the lines out row by row
+ for (size_t i = 0; i < height; ++i) {
+ uint32_t y = src_y + i;
+ glReadPixels(src_x, y, width, 1, fmt->gl_format,
+ fmt->gl_type, p + i * stride + dst_x * drm_fmt->bytes_per_block);
+ }
+ }
- glDrawArrays(GL_TRIANGLE_STRIP, 0, 4);
+ pop_fx_debug(renderer);
+
+ return glGetError() == GL_NO_ERROR;
+}
+
+static int fx_get_drm_fd(struct wlr_renderer *wlr_renderer) {
+ struct fx_renderer *renderer =
+ fx_get_renderer(wlr_renderer);
+
+ if (renderer->drm_fd < 0) {
+ renderer->drm_fd = wlr_egl_dup_drm_fd(renderer->egl);
+ }
- glDisableVertexAttribArray(shader.pos_attrib);
+ return renderer->drm_fd;
}
-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) {
+static uint32_t fx_get_render_buffer_caps(struct wlr_renderer *wlr_renderer) {
+ return WLR_BUFFER_CAP_DMABUF;
+}
+
+static void fx_renderer_destroy(struct wlr_renderer *wlr_renderer) {
+ struct fx_renderer *renderer = fx_get_renderer(wlr_renderer);
+
+ wlr_egl_make_current(renderer->egl);
+
+ struct fx_framebuffer *fx_buffer, *fx_buffer_tmp;
+ wl_list_for_each_safe(fx_buffer, fx_buffer_tmp, &renderer->buffers, link) {
+ fx_framebuffer_destroy(fx_buffer);
+ }
+
+ struct fx_texture *tex, *tex_tmp;
+ wl_list_for_each_safe(tex, tex_tmp, &renderer->textures, link) {
+ fx_texture_destroy(tex);
+ }
+
+ push_fx_debug(renderer);
+ glDeleteProgram(renderer->shaders.quad.program);
+ glDeleteProgram(renderer->shaders.tex_rgba.program);
+ glDeleteProgram(renderer->shaders.tex_rgbx.program);
+ glDeleteProgram(renderer->shaders.tex_ext.program);
+ pop_fx_debug(renderer);
+
+ if (renderer->exts.KHR_debug) {
+ glDisable(GL_DEBUG_OUTPUT_KHR);
+ renderer->procs.glDebugMessageCallbackKHR(NULL, NULL);
+ }
+
+ wlr_egl_unset_current(renderer->egl);
+ wlr_egl_destroy(renderer->egl);
+
+ if (renderer->drm_fd >= 0) {
+ close(renderer->drm_fd);
+ }
+
+ free(renderer);
+}
+
+static struct wlr_render_pass *begin_buffer_pass(struct wlr_renderer *wlr_renderer,
+ struct wlr_buffer *wlr_buffer, const struct wlr_buffer_pass_options *options) {
+ struct fx_gles_render_pass *pass =
+ fx_renderer_begin_buffer_pass(wlr_renderer, wlr_buffer, options);
+ if (!pass) {
+ return NULL;
+ }
+ return &pass->base;
+}
+
+static struct wlr_render_timer *fx_render_timer_create(struct wlr_renderer *wlr_renderer) {
+ struct fx_renderer *renderer = fx_get_renderer(wlr_renderer);
+ if (!renderer->exts.EXT_disjoint_timer_query) {
+ wlr_log(WLR_ERROR, "can't create timer, EXT_disjoint_timer_query not available");
+ return NULL;
+ }
+
+ struct fx_render_timer *timer = calloc(1, sizeof(*timer));
+ if (!timer) {
+ return NULL;
+ }
+ timer->base.impl = &render_timer_impl;
+ timer->renderer = renderer;
+
+ struct wlr_egl_context prev_ctx;
+ wlr_egl_save_context(&prev_ctx);
+ wlr_egl_make_current(renderer->egl);
+ renderer->procs.glGenQueriesEXT(1, &timer->id);
+ wlr_egl_restore_context(&prev_ctx);
+
+ return &timer->base;
+}
+
+static int fx_get_render_time(struct wlr_render_timer *wlr_timer) {
+ struct fx_render_timer *timer = fx_get_render_timer(wlr_timer);
+ struct fx_renderer *renderer = timer->renderer;
+
+ struct wlr_egl_context prev_ctx;
+ wlr_egl_save_context(&prev_ctx);
+ wlr_egl_make_current(renderer->egl);
+
+ GLint64 disjoint;
+ renderer->procs.glGetInteger64vEXT(GL_GPU_DISJOINT_EXT, &disjoint);
+ if (disjoint) {
+ wlr_log(WLR_ERROR, "a disjoint operation occurred and the render timer is invalid");
+ wlr_egl_restore_context(&prev_ctx);
+ return -1;
+ }
+
+ GLint available;
+ renderer->procs.glGetQueryObjectivEXT(timer->id,
+ GL_QUERY_RESULT_AVAILABLE_EXT, &available);
+ if (!available) {
+ wlr_log(WLR_ERROR, "timer was read too early, gpu isn't done!");
+ wlr_egl_restore_context(&prev_ctx);
+ return -1;
+ }
+
+ GLuint64 gl_render_end;
+ renderer->procs.glGetQueryObjectui64vEXT(timer->id, GL_QUERY_RESULT_EXT,
+ &gl_render_end);
+
+ int64_t cpu_nsec_total = timespec_to_nsec(&timer->cpu_end) - timespec_to_nsec(&timer->cpu_start);
+
+ wlr_egl_restore_context(&prev_ctx);
+ return gl_render_end - timer->gl_cpu_end + cpu_nsec_total;
+}
+
+static void fx_render_timer_destroy(struct wlr_render_timer *wlr_timer) {
+ struct fx_render_timer *timer = wl_container_of(wlr_timer, timer, base);
+ struct fx_renderer *renderer = timer->renderer;
+
+ struct wlr_egl_context prev_ctx;
+ wlr_egl_save_context(&prev_ctx);
+ wlr_egl_make_current(renderer->egl);
+ renderer->procs.glDeleteQueriesEXT(1, &timer->id);
+ wlr_egl_restore_context(&prev_ctx);
+ free(timer);
+}
+
+static const struct wlr_renderer_impl renderer_impl = {
+ .destroy = fx_renderer_destroy,
+ .bind_buffer = fx_bind_main_buffer,
+ .begin = fx_renderer_begin,
+ .end = fx_renderer_end,
+ .clear = fx_renderer_clear,
+ .scissor = fx_renderer_scissor,
+ .render_subtexture_with_matrix = fx_render_subtexture_with_matrix,
+ .render_quad_with_matrix = fx_render_quad_with_matrix,
+ .get_shm_texture_formats = fx_get_shm_texture_formats,
+ .get_dmabuf_texture_formats = fx_get_dmabuf_texture_formats,
+ .get_render_formats = fx_get_render_formats,
+ .preferred_read_format = fx_preferred_read_format,
+ .read_pixels = fx_read_pixels,
+ .get_drm_fd = fx_get_drm_fd,
+ .get_render_buffer_caps = fx_get_render_buffer_caps,
+ .texture_from_buffer = fx_texture_from_buffer,
+ .begin_buffer_pass = begin_buffer_pass,
+ .render_timer_create = fx_render_timer_create,
+};
+
+static const struct wlr_render_timer_impl render_timer_impl = {
+ .get_duration_ns = fx_get_render_time,
+ .destroy = fx_render_timer_destroy,
+};
+
+void push_fx_debug_(struct fx_renderer *renderer,
+ const char *file, const char *func) {
+ if (!renderer->procs.glPushDebugGroupKHR) {
return;
}
- assert(box->width > 0 && box->height > 0);
- float *color = shadow_data->color;
- float blur_sigma = shadow_data->blur_sigma;
+ int len = snprintf(NULL, 0, "%s:%s", file, func) + 1;
+ char str[len];
+ snprintf(str, len, "%s:%s", file, func);
+ renderer->procs.glPushDebugGroupKHR(GL_DEBUG_SOURCE_APPLICATION_KHR, 1, -1, str);
+}
- float gl_matrix[9];
- wlr_matrix_multiply(gl_matrix, renderer->projection, matrix);
+void pop_fx_debug(struct fx_renderer *renderer) {
+ if (renderer->procs.glPopDebugGroupKHR) {
+ renderer->procs.glPopDebugGroupKHR();
+ }
+}
+
+static enum wlr_log_importance fx_log_importance_to_wlr(GLenum type) {
+ switch (type) {
+ case GL_DEBUG_TYPE_ERROR_KHR: return WLR_ERROR;
+ case GL_DEBUG_TYPE_DEPRECATED_BEHAVIOR_KHR: return WLR_DEBUG;
+ case GL_DEBUG_TYPE_UNDEFINED_BEHAVIOR_KHR: return WLR_ERROR;
+ case GL_DEBUG_TYPE_PORTABILITY_KHR: return WLR_DEBUG;
+ case GL_DEBUG_TYPE_PERFORMANCE_KHR: return WLR_DEBUG;
+ case GL_DEBUG_TYPE_OTHER_KHR: return WLR_DEBUG;
+ case GL_DEBUG_TYPE_MARKER_KHR: return WLR_DEBUG;
+ case GL_DEBUG_TYPE_PUSH_GROUP_KHR: return WLR_DEBUG;
+ case GL_DEBUG_TYPE_POP_GROUP_KHR: return WLR_DEBUG;
+ default: return WLR_DEBUG;
+ }
+}
- // TODO: investigate why matrix is flipped prior to this cmd
- // wlr_matrix_multiply(gl_matrix, flip_180, gl_matrix);
+static void fx_log(GLenum src, GLenum type, GLuint id, GLenum severity,
+ GLsizei len, const GLchar *msg, const void *user) {
+ _wlr_log(fx_log_importance_to_wlr(type), "[GLES2] %s", msg);
+}
- wlr_matrix_transpose(gl_matrix, gl_matrix);
+static struct wlr_renderer *renderer_autocreate(struct wlr_backend *backend, int drm_fd) {
+ bool own_drm_fd = false;
+ if (!open_preferred_drm_fd(backend, &drm_fd, &own_drm_fd)) {
+ wlr_log(WLR_ERROR, "Cannot create GLES2 renderer: no DRM FD available");
+ return NULL;
+ }
+
+ struct wlr_egl *egl = wlr_egl_create_with_drm_fd(drm_fd);
+ if (egl == NULL) {
+ wlr_log(WLR_ERROR, "Could not initialize EGL");
+ return NULL;
+ }
- // 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);
+ struct wlr_renderer *renderer = fx_renderer_create_egl(egl);
+ if (!renderer) {
+ wlr_log(WLR_ERROR, "Failed to create the FX renderer");
+ wlr_egl_destroy(egl);
+ return NULL;
+ }
- // 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);
+ if (own_drm_fd && drm_fd >= 0) {
+ close(drm_fd);
+ }
- glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
+ return renderer;
+}
- struct box_shadow_shader shader = renderer->shaders.box_shadow;
+struct wlr_renderer *fx_renderer_create_with_drm_fd(int drm_fd) {
+ assert(drm_fd >= 0);
- glUseProgram(shader.program);
+ return renderer_autocreate(NULL, drm_fd);
+}
- 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);
+struct wlr_renderer *fx_renderer_create(struct wlr_backend *backend) {
+ return renderer_autocreate(backend, -1);
+}
- glUniform2f(shader.size, box->width, box->height);
- glUniform2f(shader.position, box->x, box->y);
+struct wlr_renderer *fx_renderer_create_egl(struct wlr_egl *egl) {
+ if (!wlr_egl_make_current(egl)) {
+ return NULL;
+ }
- glVertexAttribPointer(shader.pos_attrib, 2, GL_FLOAT, GL_FALSE,
- 0, verts);
+ const char *exts_str = (const char *)glGetString(GL_EXTENSIONS);
+ if (exts_str == NULL) {
+ wlr_log(WLR_ERROR, "Failed to get GL_EXTENSIONS");
+ return NULL;
+ }
- glEnableVertexAttribArray(shader.pos_attrib);
+ struct fx_renderer *renderer = calloc(1, sizeof(struct fx_renderer));
+ if (renderer == NULL) {
+ return NULL;
+ }
+ wlr_renderer_init(&renderer->wlr_renderer, &renderer_impl);
- glDrawArrays(GL_TRIANGLE_STRIP, 0, 4);
+ wl_list_init(&renderer->buffers);
+ wl_list_init(&renderer->textures);
- glDisableVertexAttribArray(shader.pos_attrib);
+ renderer->egl = egl;
+ renderer->exts_str = exts_str;
+ renderer->drm_fd = -1;
- glBlendFunc(GL_ONE, GL_ONE_MINUS_SRC_ALPHA);
+ 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);
+
+ if (!renderer->egl->exts.EXT_image_dma_buf_import) {
+ wlr_log(WLR_ERROR, "EGL_EXT_image_dma_buf_import not supported");
+ free(renderer);
+ return NULL;
+ }
+ if (!check_gl_ext(exts_str, "GL_EXT_texture_format_BGRA8888")) {
+ wlr_log(WLR_ERROR, "BGRA8888 format not supported by GLES2");
+ free(renderer);
+ return NULL;
+ }
+ if (!check_gl_ext(exts_str, "GL_EXT_unpack_subimage")) {
+ wlr_log(WLR_ERROR, "GL_EXT_unpack_subimage not supported");
+ free(renderer);
+ return NULL;
+ }
+
+ renderer->exts.EXT_read_format_bgra =
+ check_gl_ext(exts_str, "GL_EXT_read_format_bgra");
+
+ renderer->exts.EXT_texture_type_2_10_10_10_REV =
+ check_gl_ext(exts_str, "GL_EXT_texture_type_2_10_10_10_REV");
+
+ renderer->exts.OES_texture_half_float_linear =
+ check_gl_ext(exts_str, "GL_OES_texture_half_float_linear");
- fx_renderer_stencil_mask_fini();
+ renderer->exts.EXT_texture_norm16 =
+ check_gl_ext(exts_str, "GL_EXT_texture_norm16");
+
+ if (check_gl_ext(exts_str, "GL_KHR_debug")) {
+ renderer->exts.KHR_debug = true;
+ load_gl_proc(&renderer->procs.glDebugMessageCallbackKHR,
+ "glDebugMessageCallbackKHR");
+ load_gl_proc(&renderer->procs.glDebugMessageControlKHR,
+ "glDebugMessageControlKHR");
+ }
+
+ // 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");
+ }
+
+ if (check_gl_ext(exts_str, "GL_KHR_robustness")) {
+ GLint notif_strategy = 0;
+ glGetIntegerv(GL_RESET_NOTIFICATION_STRATEGY_KHR, &notif_strategy);
+ switch (notif_strategy) {
+ case GL_LOSE_CONTEXT_ON_RESET_KHR:
+ wlr_log(WLR_DEBUG, "GPU reset notifications are enabled");
+ load_gl_proc(&renderer->procs.glGetGraphicsResetStatusKHR,
+ "glGetGraphicsResetStatusKHR");
+ break;
+ case GL_NO_RESET_NOTIFICATION_KHR:
+ wlr_log(WLR_DEBUG, "GPU reset notifications are disabled");
+ break;
+ }
+ }
+
+ if (check_gl_ext(exts_str, "GL_EXT_disjoint_timer_query")) {
+ renderer->exts.EXT_disjoint_timer_query = true;
+ load_gl_proc(&renderer->procs.glGenQueriesEXT, "glGenQueriesEXT");
+ load_gl_proc(&renderer->procs.glDeleteQueriesEXT, "glDeleteQueriesEXT");
+ load_gl_proc(&renderer->procs.glQueryCounterEXT, "glQueryCounterEXT");
+ load_gl_proc(&renderer->procs.glGetQueryObjectivEXT, "glGetQueryObjectivEXT");
+ load_gl_proc(&renderer->procs.glGetQueryObjectui64vEXT, "glGetQueryObjectui64vEXT");
+ load_gl_proc(&renderer->procs.glGetInteger64vEXT, "glGetInteger64vEXT");
+ }
+
+ if (renderer->exts.KHR_debug) {
+ glEnable(GL_DEBUG_OUTPUT_KHR);
+ glEnable(GL_DEBUG_OUTPUT_SYNCHRONOUS_KHR);
+ renderer->procs.glDebugMessageCallbackKHR(fx_log, NULL);
+
+ // Silence unwanted message types
+ renderer->procs.glDebugMessageControlKHR(GL_DONT_CARE,
+ GL_DEBUG_TYPE_POP_GROUP_KHR, GL_DONT_CARE, 0, NULL, GL_FALSE);
+ renderer->procs.glDebugMessageControlKHR(GL_DONT_CARE,
+ GL_DEBUG_TYPE_PUSH_GROUP_KHR, GL_DONT_CARE, 0, NULL, GL_FALSE);
+ }
+
+ push_fx_debug(renderer);
+
+ // Link all shaders
+ if (!link_shaders(renderer)) {
+ goto error;
+ }
+ pop_fx_debug(renderer);
+
+ wlr_log(WLR_INFO, "FX RENDERER: Shaders Initialized Successfully");
+
+ wlr_egl_unset_current(renderer->egl);
+
+ return &renderer->wlr_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);
+
+ pop_fx_debug(renderer);
+
+ if (renderer->exts.KHR_debug) {
+ glDisable(GL_DEBUG_OUTPUT_KHR);
+ renderer->procs.glDebugMessageCallbackKHR(NULL, NULL);
+ }
+
+ wlr_egl_unset_current(renderer->egl);
+
+ free(renderer);
+ return NULL;
}
diff --git a/render/fx_renderer/fx_stencilbuffer.c b/render/fx_renderer/fx_stencilbuffer.c
deleted file mode 100644
index 4f57216..0000000
--- a/render/fx_renderer/fx_stencilbuffer.c
+++ /dev/null
@@ -1,50 +0,0 @@
-#include <assert.h>
-#include <wlr/render/gles2.h>
-#include <wlr/util/log.h>
-
-#include "include/render/fx_renderer/fx_stencilbuffer.h"
-
-struct fx_stencilbuffer fx_stencilbuffer_create(void) {
- return (struct fx_stencilbuffer) {
- .rb = -1,
- .width = -1,
- .height = -1,
- };
-}
-
-void fx_stencilbuffer_init(struct fx_stencilbuffer *stencil_buffer, int width, int height) {
- bool first_alloc = false;
-
- if (stencil_buffer->rb == (uint32_t) -1) {
- glGenRenderbuffers(1, &stencil_buffer->rb);
- first_alloc = true;
- }
-
- if (first_alloc || stencil_buffer->width != width || stencil_buffer->height != height) {
- glBindRenderbuffer(GL_RENDERBUFFER, stencil_buffer->rb);
- glRenderbufferStorage(GL_RENDERBUFFER, GL_STENCIL_INDEX8, width, height);
- stencil_buffer->width = width;
- stencil_buffer->height = height;
-
- GLenum status = glCheckFramebufferStatus(GL_FRAMEBUFFER);
- if (status != GL_FRAMEBUFFER_COMPLETE) {
- wlr_log(WLR_ERROR,
- "Stencil buffer incomplete, couldn't create! (FB status: %i)",
- status);
- return;
- }
- }
-
- // Reattach the RenderBuffer to the FrameBuffer
- glFramebufferRenderbuffer(GL_FRAMEBUFFER, GL_STENCIL_ATTACHMENT,
- GL_RENDERBUFFER, stencil_buffer->rb);
-}
-
-void fx_stencilbuffer_release(struct fx_stencilbuffer *stencil_buffer) {
- if (stencil_buffer->rb != (uint32_t) -1 && stencil_buffer->rb) {
- glDeleteRenderbuffers(1, &stencil_buffer->rb);
- }
- stencil_buffer->rb = -1;
- stencil_buffer->width = -1;
- stencil_buffer->height = -1;
-}
diff --git a/render/fx_renderer/fx_texture.c b/render/fx_renderer/fx_texture.c
new file mode 100644
index 0000000..1952311
--- /dev/null
+++ b/render/fx_renderer/fx_texture.c
@@ -0,0 +1,368 @@
+#include <assert.h>
+#include <stdlib.h>
+#include <wlr/render/interface.h>
+#include <wlr/types/wlr_buffer.h>
+#include <wlr/util/log.h>
+#include <drm_fourcc.h>
+
+#include "render/fx_renderer/fx_renderer.h"
+#include "render/pixel_format.h"
+#include "render/egl.h"
+
+static const struct wlr_texture_impl texture_impl;
+
+bool wlr_texture_is_fx(struct wlr_texture *wlr_texture) {
+ return wlr_texture->impl == &texture_impl;
+}
+
+struct fx_texture *fx_get_texture(struct wlr_texture *wlr_texture) {
+ assert(wlr_texture_is_fx(wlr_texture));
+ struct fx_texture *texture = wl_container_of(wlr_texture, texture, wlr_texture);
+ return texture;
+}
+
+static bool fx_texture_update_from_buffer(struct wlr_texture *wlr_texture,
+ struct wlr_buffer *buffer, const pixman_region32_t *damage) {
+ struct fx_texture *texture = fx_get_texture(wlr_texture);
+
+ if (texture->target != GL_TEXTURE_2D || texture->image != EGL_NO_IMAGE_KHR) {
+ return false;
+ }
+
+ void *data;
+ uint32_t format;
+ size_t stride;
+ if (!wlr_buffer_begin_data_ptr_access(buffer,
+ WLR_BUFFER_DATA_PTR_ACCESS_READ, &data, &format, &stride)) {
+ return false;
+ }
+
+ if (format != texture->drm_format) {
+ wlr_buffer_end_data_ptr_access(buffer);
+ return false;
+ }
+
+ const struct fx_pixel_format *fmt =
+ get_fx_format_from_drm(texture->drm_format);
+ assert(fmt);
+
+ const struct wlr_pixel_format_info *drm_fmt =
+ drm_get_pixel_format_info(texture->drm_format);
+ assert(drm_fmt);
+ if (pixel_format_info_pixels_per_block(drm_fmt) != 1) {
+ wlr_buffer_end_data_ptr_access(buffer);
+ wlr_log(WLR_ERROR, "Cannot update texture: block formats are not supported");
+ return false;
+ }
+
+ if (!pixel_format_info_check_stride(drm_fmt, stride, buffer->width)) {
+ wlr_buffer_end_data_ptr_access(buffer);
+ return false;
+ }
+
+ struct wlr_egl_context prev_ctx;
+ wlr_egl_save_context(&prev_ctx);
+ wlr_egl_make_current(texture->fx_renderer->egl);
+
+ push_fx_debug(texture->fx_renderer);
+
+ glBindTexture(GL_TEXTURE_2D, texture->tex);
+
+ int rects_len = 0;
+ pixman_box32_t *rects = pixman_region32_rectangles(damage, &rects_len);
+
+ for (int i = 0; i < rects_len; i++) {
+ pixman_box32_t rect = rects[i];
+
+ glPixelStorei(GL_UNPACK_ROW_LENGTH_EXT, stride / drm_fmt->bytes_per_block);
+ glPixelStorei(GL_UNPACK_SKIP_PIXELS_EXT, rect.x1);
+ glPixelStorei(GL_UNPACK_SKIP_ROWS_EXT, rect.y1);
+
+ int width = rect.x2 - rect.x1;
+ int height = rect.y2 - rect.y1;
+ glTexSubImage2D(GL_TEXTURE_2D, 0, rect.x1, rect.y1, width, height,
+ fmt->gl_format, fmt->gl_type, data);
+ }
+
+ glPixelStorei(GL_UNPACK_ROW_LENGTH_EXT, 0);
+ glPixelStorei(GL_UNPACK_SKIP_PIXELS_EXT, 0);
+ glPixelStorei(GL_UNPACK_SKIP_ROWS_EXT, 0);
+
+ glBindTexture(GL_TEXTURE_2D, 0);
+
+ pop_fx_debug(texture->fx_renderer);
+
+ wlr_egl_restore_context(&prev_ctx);
+
+ wlr_buffer_end_data_ptr_access(buffer);
+
+ return true;
+}
+
+static bool fx_texture_invalidate(struct fx_texture *texture) {
+ if (texture->image == EGL_NO_IMAGE_KHR) {
+ return false;
+ }
+ if (texture->target == GL_TEXTURE_EXTERNAL_OES) {
+ // External changes are immediately made visible by the GL implementation
+ return true;
+ }
+
+ struct wlr_egl_context prev_ctx;
+ wlr_egl_save_context(&prev_ctx);
+ wlr_egl_make_current(texture->fx_renderer->egl);
+
+ push_fx_debug(texture->fx_renderer);
+
+ glBindTexture(texture->target, texture->tex);
+ texture->fx_renderer->procs.glEGLImageTargetTexture2DOES(texture->target,
+ texture->image);
+ glBindTexture(texture->target, 0);
+
+ pop_fx_debug(texture->fx_renderer);
+
+ wlr_egl_restore_context(&prev_ctx);
+
+ return true;
+}
+
+void fx_texture_destroy(struct fx_texture *texture) {
+ wl_list_remove(&texture->link);
+ if (texture->buffer != NULL) {
+ wlr_addon_finish(&texture->buffer_addon);
+ }
+
+ struct wlr_egl_context prev_ctx;
+ wlr_egl_save_context(&prev_ctx);
+ wlr_egl_make_current(texture->fx_renderer->egl);
+
+ glDeleteTextures(1, &texture->tex);
+ wlr_egl_destroy_image(texture->fx_renderer->egl, texture->image);
+
+ wlr_egl_restore_context(&prev_ctx);
+
+ free(texture);
+}
+
+static void fx_texture_unref(struct wlr_texture *wlr_texture) {
+ struct fx_texture *texture = fx_get_texture(wlr_texture);
+ if (texture->buffer != NULL) {
+ // Keep the texture around, in case the buffer is re-used later. We're
+ // still listening to the buffer's destroy event.
+ wlr_buffer_unlock(texture->buffer);
+ } else {
+ fx_texture_destroy(texture);
+ }
+}
+
+static const struct wlr_texture_impl texture_impl = {
+ .update_from_buffer = fx_texture_update_from_buffer,
+ .destroy = fx_texture_unref,
+};
+
+static struct fx_texture *fx_texture_create(
+ struct fx_renderer *renderer, uint32_t width, uint32_t height) {
+ struct fx_texture *texture = calloc(1, sizeof(struct fx_texture));
+ if (texture == NULL) {
+ wlr_log_errno(WLR_ERROR, "Allocation failed");
+ return NULL;
+ }
+ wlr_texture_init(&texture->wlr_texture, &renderer->wlr_renderer,
+ &texture_impl, width, height);
+ texture->fx_renderer = renderer;
+ wl_list_insert(&renderer->textures, &texture->link);
+ return texture;
+}
+
+static struct wlr_texture *fx_texture_from_pixels(
+ struct wlr_renderer *wlr_renderer,
+ uint32_t drm_format, uint32_t stride, uint32_t width,
+ uint32_t height, const void *data) {
+ struct fx_renderer *renderer = fx_get_renderer(wlr_renderer);
+
+ const struct fx_pixel_format *fmt = get_fx_format_from_drm(drm_format);
+ if (fmt == NULL) {
+ wlr_log(WLR_ERROR, "Unsupported pixel format 0x%"PRIX32, drm_format);
+ return NULL;
+ }
+
+ const struct wlr_pixel_format_info *drm_fmt =
+ drm_get_pixel_format_info(drm_format);
+ assert(drm_fmt);
+ if (pixel_format_info_pixels_per_block(drm_fmt) != 1) {
+ wlr_log(WLR_ERROR, "Cannot upload texture: block formats are not supported");
+ return NULL;
+ }
+
+ if (!pixel_format_info_check_stride(drm_fmt, stride, width)) {
+ return NULL;
+ }
+
+ struct fx_texture *texture =
+ fx_texture_create(renderer, width, height);
+ if (texture == NULL) {
+ return NULL;
+ }
+ texture->target = GL_TEXTURE_2D;
+ texture->has_alpha = fmt->has_alpha;
+ texture->drm_format = fmt->drm_format;
+
+ GLint internal_format = fmt->gl_internalformat;
+ if (!internal_format) {
+ internal_format = fmt->gl_format;
+ }
+
+ struct wlr_egl_context prev_ctx;
+ wlr_egl_save_context(&prev_ctx);
+ wlr_egl_make_current(renderer->egl);
+
+ push_fx_debug(renderer);
+
+ glGenTextures(1, &texture->tex);
+ glBindTexture(GL_TEXTURE_2D, texture->tex);
+
+ glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
+ glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
+ glPixelStorei(GL_UNPACK_ROW_LENGTH_EXT, stride / drm_fmt->bytes_per_block);
+ glTexImage2D(GL_TEXTURE_2D, 0, internal_format, width, height, 0,
+ fmt->gl_format, fmt->gl_type, data);
+ glPixelStorei(GL_UNPACK_ROW_LENGTH_EXT, 0);
+
+ glBindTexture(GL_TEXTURE_2D, 0);
+
+ pop_fx_debug(renderer);
+
+ wlr_egl_restore_context(&prev_ctx);
+
+ return &texture->wlr_texture;
+}
+
+static struct wlr_texture *fx_texture_from_dmabuf(
+ struct wlr_renderer *wlr_renderer,
+ struct wlr_dmabuf_attributes *attribs) {
+ struct fx_renderer *renderer = fx_get_renderer(wlr_renderer);
+
+ if (!renderer->procs.glEGLImageTargetTexture2DOES) {
+ return NULL;
+ }
+
+ struct fx_texture *texture =
+ fx_texture_create(renderer, attribs->width, attribs->height);
+ if (texture == NULL) {
+ return NULL;
+ }
+ texture->drm_format = DRM_FORMAT_INVALID; // texture can't be written anyways
+
+ const struct wlr_pixel_format_info *drm_fmt =
+ drm_get_pixel_format_info(attribs->format);
+ if (drm_fmt != NULL) {
+ texture->has_alpha = drm_fmt->has_alpha;
+ } else {
+ // We don't know, assume the texture has an alpha channel
+ texture->has_alpha = true;
+ }
+
+ struct wlr_egl_context prev_ctx;
+ wlr_egl_save_context(&prev_ctx);
+ wlr_egl_make_current(renderer->egl);
+
+ bool external_only;
+ texture->image =
+ wlr_egl_create_image_from_dmabuf(renderer->egl, attribs, &external_only);
+ if (texture->image == EGL_NO_IMAGE_KHR) {
+ wlr_log(WLR_ERROR, "Failed to create EGL image from DMA-BUF");
+ wlr_egl_restore_context(&prev_ctx);
+ wl_list_remove(&texture->link);
+ free(texture);
+ return NULL;
+ }
+
+ texture->target = external_only ? GL_TEXTURE_EXTERNAL_OES : GL_TEXTURE_2D;
+
+ push_fx_debug(renderer);
+
+ glGenTextures(1, &texture->tex);
+ glBindTexture(texture->target, texture->tex);
+ glTexParameteri(texture->target, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
+ glTexParameteri(texture->target, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
+ renderer->procs.glEGLImageTargetTexture2DOES(texture->target, texture->image);
+ glBindTexture(texture->target, 0);
+
+ pop_fx_debug(renderer);
+
+ wlr_egl_restore_context(&prev_ctx);
+
+ return &texture->wlr_texture;
+}
+
+static void texture_handle_buffer_destroy(struct wlr_addon *addon) {
+ struct fx_texture *texture =
+ wl_container_of(addon, texture, buffer_addon);
+ fx_texture_destroy(texture);
+}
+
+static const struct wlr_addon_interface texture_addon_impl = {
+ .name = "fx_texture",
+ .destroy = texture_handle_buffer_destroy,
+};
+
+static struct wlr_texture *fx_texture_from_dmabuf_buffer(
+ struct fx_renderer *renderer, struct wlr_buffer *buffer,
+ struct wlr_dmabuf_attributes *dmabuf) {
+ struct wlr_addon *addon =
+ wlr_addon_find(&buffer->addons, renderer, &texture_addon_impl);
+ if (addon != NULL) {
+ struct fx_texture *texture =
+ wl_container_of(addon, texture, buffer_addon);
+ if (!fx_texture_invalidate(texture)) {
+ wlr_log(WLR_ERROR, "Failed to invalidate texture");
+ return false;
+ }
+ wlr_buffer_lock(texture->buffer);
+ return &texture->wlr_texture;
+ }
+
+ struct wlr_texture *wlr_texture =
+ fx_texture_from_dmabuf(&renderer->wlr_renderer, dmabuf);
+ if (wlr_texture == NULL) {
+ return false;
+ }
+
+ struct fx_texture *texture = fx_get_texture(wlr_texture);
+ texture->buffer = wlr_buffer_lock(buffer);
+ wlr_addon_init(&texture->buffer_addon, &buffer->addons,
+ renderer, &texture_addon_impl);
+
+ return &texture->wlr_texture;
+}
+
+struct wlr_texture *fx_texture_from_buffer(struct wlr_renderer *wlr_renderer,
+ struct wlr_buffer *buffer) {
+ struct fx_renderer *renderer = fx_get_renderer(wlr_renderer);
+
+ void *data;
+ uint32_t format;
+ size_t stride;
+ struct wlr_dmabuf_attributes dmabuf;
+ if (wlr_buffer_get_dmabuf(buffer, &dmabuf)) {
+ return fx_texture_from_dmabuf_buffer(renderer, buffer, &dmabuf);
+ } else if (wlr_buffer_begin_data_ptr_access(buffer,
+ WLR_BUFFER_DATA_PTR_ACCESS_READ, &data, &format, &stride)) {
+ struct wlr_texture *tex = fx_texture_from_pixels(wlr_renderer,
+ format, stride, buffer->width, buffer->height, data);
+ wlr_buffer_end_data_ptr_access(buffer);
+ return tex;
+ } else {
+ return NULL;
+ }
+}
+
+void fx_texture_get_attribs(struct wlr_texture *wlr_texture,
+ struct fx_texture_attribs *attribs) {
+ struct fx_texture *texture = fx_get_texture(wlr_texture);
+ *attribs = (struct fx_texture_attribs){
+ .target = texture->target,
+ .tex = texture->tex,
+ .has_alpha = texture->has_alpha,
+ };
+}
diff --git a/render/fx_renderer/gles2/shaders/box_shadow.frag b/render/fx_renderer/gles2/shaders/box_shadow.frag
index c9b2b91..92d40fc 100644
--- a/render/fx_renderer/gles2/shaders/box_shadow.frag
+++ b/render/fx_renderer/gles2/shaders/box_shadow.frag
@@ -1,6 +1,11 @@
// Writeup: https://madebyevan.com/shaders/fast-rounded-rectangle-shadows/
+#ifdef GL_FRAGMENT_PRECISION_HIGH
+precision highp float;
+#else
precision mediump float;
+#endif
+
varying vec4 v_color;
varying vec2 v_texcoord;
diff --git a/render/fx_renderer/gles2/shaders/common.vert b/render/fx_renderer/gles2/shaders/common.vert
index 811e0f2..9e7b073 100644
--- a/render/fx_renderer/gles2/shaders/common.vert
+++ b/render/fx_renderer/gles2/shaders/common.vert
@@ -1,12 +1,13 @@
uniform mat3 proj;
uniform vec4 color;
+uniform mat3 tex_proj;
attribute vec2 pos;
-attribute vec2 texcoord;
varying vec4 v_color;
varying vec2 v_texcoord;
void main() {
- gl_Position = vec4(proj * vec3(pos, 1.0), 1.0);
- v_color = color;
- v_texcoord = texcoord;
+ vec3 pos3 = vec3(pos, 1.0);
+ gl_Position = vec4(pos3 * proj, 1.0);
+ v_color = color;
+ v_texcoord = (pos3 * tex_proj).xy;
}
diff --git a/render/fx_renderer/gles2/shaders/quad.frag b/render/fx_renderer/gles2/shaders/quad.frag
index 7c76327..97d3a31 100644
--- a/render/fx_renderer/gles2/shaders/quad.frag
+++ b/render/fx_renderer/gles2/shaders/quad.frag
@@ -1,7 +1,13 @@
+#ifdef GL_FRAGMENT_PRECISION_HIGH
+precision highp float;
+#else
precision mediump float;
+#endif
+
varying vec4 v_color;
varying vec2 v_texcoord;
+uniform vec4 color;
void main() {
- gl_FragColor = v_color;
+ gl_FragColor = color;
}
diff --git a/render/fx_renderer/gles2/shaders/stencil_mask.frag b/render/fx_renderer/gles2/shaders/stencil_mask.frag
index ee03307..523adc8 100644
--- a/render/fx_renderer/gles2/shaders/stencil_mask.frag
+++ b/render/fx_renderer/gles2/shaders/stencil_mask.frag
@@ -1,4 +1,9 @@
+#ifdef GL_FRAGMENT_PRECISION_HIGH
+precision highp float;
+#else
precision mediump float;
+#endif
+
varying vec2 v_texcoord;
uniform vec2 half_size;
diff --git a/render/fx_renderer/gles2/shaders/tex.frag b/render/fx_renderer/gles2/shaders/tex.frag
index bd3c596..8c14373 100644
--- a/render/fx_renderer/gles2/shaders/tex.frag
+++ b/render/fx_renderer/gles2/shaders/tex.frag
@@ -10,7 +10,11 @@
#extension GL_OES_EGL_image_external : require
#endif
+#ifdef GL_FRAGMENT_PRECISION_HIGH
+precision highp float;
+#else
precision mediump float;
+#endif
varying vec2 v_texcoord;
@@ -21,6 +25,7 @@ uniform sampler2D tex;
#endif
uniform float alpha;
+
uniform vec2 size;
uniform vec2 position;
uniform float radius;
diff --git a/render/fx_renderer/meson.build b/render/fx_renderer/meson.build
index 394caa3..d7160a8 100644
--- a/render/fx_renderer/meson.build
+++ b/render/fx_renderer/meson.build
@@ -7,7 +7,12 @@ endif
wlr_files += files(
'matrix.c',
- 'fx_stencilbuffer.c',
+ 'util.c',
+ 'shaders.c',
+ 'pixel_format.c',
+ 'fx_pass.c',
+ 'fx_framebuffer.c',
+ 'fx_texture.c',
'fx_renderer.c',
)
@@ -18,7 +23,6 @@ if 'gles2' in renderers or 'auto' in renderers
if egl.found() and gbm.found() and glesv2.found()
wlr_deps += [egl, gbm, glesv2]
- internal_features += { 'egl': true , 'gles2-renderer': true }
endif
subdir('gles2')
endif
diff --git a/render/fx_renderer/pixel_format.c b/render/fx_renderer/pixel_format.c
new file mode 100644
index 0000000..2693018
--- /dev/null
+++ b/render/fx_renderer/pixel_format.c
@@ -0,0 +1,175 @@
+#include <drm_fourcc.h>
+#include <GLES2/gl2.h>
+#include <GLES2/gl2ext.h>
+#include "render/fx_renderer/fx_renderer.h"
+
+/*
+ * The DRM formats are little endian while the GL formats are big endian,
+ * so DRM_FORMAT_ARGB8888 is actually compatible with GL_BGRA_EXT.
+ */
+static const struct fx_pixel_format formats[] = {
+ {
+ .drm_format = DRM_FORMAT_ARGB8888,
+ .gl_format = GL_BGRA_EXT,
+ .gl_type = GL_UNSIGNED_BYTE,
+ .has_alpha = true,
+ },
+ {
+ .drm_format = DRM_FORMAT_XRGB8888,
+ .gl_format = GL_BGRA_EXT,
+ .gl_type = GL_UNSIGNED_BYTE,
+ .has_alpha = false,
+ },
+ {
+ .drm_format = DRM_FORMAT_XBGR8888,
+ .gl_format = GL_RGBA,
+ .gl_type = GL_UNSIGNED_BYTE,
+ .has_alpha = false,
+ },
+ {
+ .drm_format = DRM_FORMAT_ABGR8888,
+ .gl_format = GL_RGBA,
+ .gl_type = GL_UNSIGNED_BYTE,
+ .has_alpha = true,
+ },
+ {
+ .drm_format = DRM_FORMAT_BGR888,
+ .gl_format = GL_RGB,
+ .gl_type = GL_UNSIGNED_BYTE,
+ .has_alpha = false,
+ },
+#if WLR_LITTLE_ENDIAN
+ {
+ .drm_format = DRM_FORMAT_RGBX4444,
+ .gl_format = GL_RGBA,
+ .gl_type = GL_UNSIGNED_SHORT_4_4_4_4,
+ .has_alpha = false,
+ },
+ {
+ .drm_format = DRM_FORMAT_RGBA4444,
+ .gl_format = GL_RGBA,
+ .gl_type = GL_UNSIGNED_SHORT_4_4_4_4,
+ .has_alpha = true,
+ },
+ {
+ .drm_format = DRM_FORMAT_RGBX5551,
+ .gl_format = GL_RGBA,
+ .gl_type = GL_UNSIGNED_SHORT_5_5_5_1,
+ .has_alpha = false,
+ },
+ {
+ .drm_format = DRM_FORMAT_RGBA5551,
+ .gl_format = GL_RGBA,
+ .gl_type = GL_UNSIGNED_SHORT_5_5_5_1,
+ .has_alpha = true,
+ },
+ {
+ .drm_format = DRM_FORMAT_RGB565,
+ .gl_format = GL_RGB,
+ .gl_type = GL_UNSIGNED_SHORT_5_6_5,
+ .has_alpha = false,
+ },
+ {
+ .drm_format = DRM_FORMAT_XBGR2101010,
+ .gl_format = GL_RGBA,
+ .gl_type = GL_UNSIGNED_INT_2_10_10_10_REV_EXT,
+ .has_alpha = false,
+ },
+ {
+ .drm_format = DRM_FORMAT_ABGR2101010,
+ .gl_format = GL_RGBA,
+ .gl_type = GL_UNSIGNED_INT_2_10_10_10_REV_EXT,
+ .has_alpha = true,
+ },
+ {
+ .drm_format = DRM_FORMAT_XBGR16161616F,
+ .gl_format = GL_RGBA,
+ .gl_type = GL_HALF_FLOAT_OES,
+ .has_alpha = false,
+ },
+ {
+ .drm_format = DRM_FORMAT_ABGR16161616F,
+ .gl_format = GL_RGBA,
+ .gl_type = GL_HALF_FLOAT_OES,
+ .has_alpha = true,
+ },
+ {
+ .drm_format = DRM_FORMAT_XBGR16161616,
+ .gl_internalformat = GL_RGBA16_EXT,
+ .gl_format = GL_RGBA,
+ .gl_type = GL_UNSIGNED_SHORT,
+ .has_alpha = false,
+ },
+ {
+ .drm_format = DRM_FORMAT_ABGR16161616,
+ .gl_internalformat = GL_RGBA16_EXT,
+ .gl_format = GL_RGBA,
+ .gl_type = GL_UNSIGNED_SHORT,
+ .has_alpha = true,
+ },
+#endif
+};
+
+// TODO: more pixel formats
+
+/*
+ * Return true if supported for texturing, even if other operations like
+ * reading aren't supported.
+ */
+bool is_fx_pixel_format_supported(const struct fx_renderer *renderer,
+ const struct fx_pixel_format *format) {
+ if (format->gl_type == GL_UNSIGNED_INT_2_10_10_10_REV_EXT
+ && !renderer->exts.EXT_texture_type_2_10_10_10_REV) {
+ return false;
+ }
+ if (format->gl_type == GL_HALF_FLOAT_OES
+ && !renderer->exts.OES_texture_half_float_linear) {
+ return false;
+ }
+ if (format->gl_type == GL_UNSIGNED_SHORT
+ && !renderer->exts.EXT_texture_norm16) {
+ return false;
+ }
+ /*
+ * Note that we don't need to check for GL_EXT_texture_format_BGRA8888
+ * here, since we've already checked if we have it at renderer creation
+ * time and bailed out if not. We do the check there because Wayland
+ * requires all compositors to support SHM buffers in that format.
+ */
+ return true;
+}
+
+const struct fx_pixel_format *get_fx_format_from_drm(uint32_t fmt) {
+ for (size_t i = 0; i < sizeof(formats) / sizeof(*formats); ++i) {
+ if (formats[i].drm_format == fmt) {
+ return &formats[i];
+ }
+ }
+ return NULL;
+}
+
+const struct fx_pixel_format *get_fx_format_from_gl(
+ GLint gl_format, GLint gl_type, bool alpha) {
+ for (size_t i = 0; i < sizeof(formats) / sizeof(*formats); ++i) {
+ if (formats[i].gl_format == gl_format &&
+ formats[i].gl_type == gl_type &&
+ formats[i].has_alpha == alpha) {
+ return &formats[i];
+ }
+ }
+ return NULL;
+}
+
+const uint32_t *get_fx_shm_formats(const struct fx_renderer *renderer,
+ size_t *len) {
+ static uint32_t shm_formats[sizeof(formats) / sizeof(formats[0])];
+ size_t j = 0;
+ for (size_t i = 0; i < sizeof(formats) / sizeof(formats[0]); i++) {
+ if (!is_fx_pixel_format_supported(renderer, &formats[i])) {
+ continue;
+ }
+ shm_formats[j++] = formats[i].drm_format;
+ }
+ *len = j;
+ return shm_formats;
+}
diff --git a/render/fx_renderer/shaders.c b/render/fx_renderer/shaders.c
new file mode 100644
index 0000000..9257ca3
--- /dev/null
+++ b/render/fx_renderer/shaders.c
@@ -0,0 +1,203 @@
+#include <EGL/egl.h>
+#include <stdio.h>
+#include <stdlib.h>
+#include <wlr/util/log.h>
+
+#include "render/fx_renderer/fx_renderer.h"
+#include "render/fx_renderer/shaders.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"
+
+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;
+}
+
+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;
+}
+
+
+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;
+}
+
+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;
+}
+
+// Shaders
+
+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_proj = glGetUniformLocation(prog, "tex_proj");
+ 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;
+}
+
+bool link_shaders(struct fx_renderer *renderer) {
+ // quad fragment shader
+ if (!link_quad_program(&renderer->shaders.quad)) {
+ wlr_log(WLR_ERROR, "Could not link quad shader");
+ return false;
+ }
+ // fragment shaders
+ if (!link_tex_program(&renderer->shaders.tex_rgba, SHADER_SOURCE_TEXTURE_RGBA)) {
+ wlr_log(WLR_ERROR, "Could not link tex_RGBA shader");
+ return false;
+ }
+ if (!link_tex_program(&renderer->shaders.tex_rgbx, SHADER_SOURCE_TEXTURE_RGBX)) {
+ wlr_log(WLR_ERROR, "Could not link tex_RGBX shader");
+ return false;
+ }
+ if (!link_tex_program(&renderer->shaders.tex_ext, SHADER_SOURCE_TEXTURE_EXTERNAL)) {
+ wlr_log(WLR_ERROR, "Could not link tex_EXTERNAL shader");
+ return false;
+ }
+
+ // stencil mask shader
+ if (!link_stencil_mask_program(&renderer->shaders.stencil_mask)) {
+ wlr_log(WLR_ERROR, "Could not link stencil mask shader");
+ return false;
+ }
+ // box shadow shader
+ if (!link_box_shadow_program(&renderer->shaders.box_shadow)) {
+ wlr_log(WLR_ERROR, "Could not link box shadow shader");
+ return false;
+ }
+
+ return true;
+}
diff --git a/render/fx_renderer/util.c b/render/fx_renderer/util.c
new file mode 100644
index 0000000..c262aab
--- /dev/null
+++ b/render/fx_renderer/util.c
@@ -0,0 +1,113 @@
+#define _POSIX_C_SOURCE 200809L
+#include <fcntl.h>
+#include <unistd.h>
+#include <wlr/util/log.h>
+#include <wlr/types/wlr_buffer.h>
+#include <xf86drm.h>
+
+#include "render/fx_renderer/util.h"
+
+static uint32_t backend_get_buffer_caps(struct wlr_backend *backend) {
+ if (!backend->impl->get_buffer_caps) {
+ return 0;
+ }
+
+ return backend->impl->get_buffer_caps(backend);
+}
+
+static int open_drm_render_node(void) {
+ uint32_t flags = 0;
+ int devices_len = drmGetDevices2(flags, NULL, 0);
+ if (devices_len < 0) {
+ wlr_log(WLR_ERROR, "drmGetDevices2 failed: %s", strerror(-devices_len));
+ return -1;
+ }
+ drmDevice **devices = calloc(devices_len, sizeof(*devices));
+ if (devices == NULL) {
+ wlr_log_errno(WLR_ERROR, "Allocation failed");
+ return -1;
+ }
+ devices_len = drmGetDevices2(flags, devices, devices_len);
+ if (devices_len < 0) {
+ free(devices);
+ wlr_log(WLR_ERROR, "drmGetDevices2 failed: %s", strerror(-devices_len));
+ return -1;
+ }
+
+ int fd = -1;
+ for (int i = 0; i < devices_len; i++) {
+ drmDevice *dev = devices[i];
+ if (dev->available_nodes & (1 << DRM_NODE_RENDER)) {
+ const char *name = dev->nodes[DRM_NODE_RENDER];
+ wlr_log(WLR_DEBUG, "Opening DRM render node '%s'", name);
+ fd = open(name, O_RDWR | O_CLOEXEC);
+ if (fd < 0) {
+ wlr_log_errno(WLR_ERROR, "Failed to open '%s'", name);
+ goto out;
+ }
+ break;
+ }
+ }
+ if (fd < 0) {
+ wlr_log(WLR_ERROR, "Failed to find any DRM render node");
+ }
+
+out:
+ for (int i = 0; i < devices_len; i++) {
+ drmFreeDevice(&devices[i]);
+ }
+ free(devices);
+
+ return fd;
+}
+
+bool open_preferred_drm_fd(struct wlr_backend *backend, int *drm_fd_ptr,
+ bool *own_drm_fd) {
+ if (*drm_fd_ptr >= 0) {
+ return true;
+ }
+
+ // Allow the user to override the render node
+ const char *render_name = getenv("WLR_RENDER_DRM_DEVICE");
+ if (render_name != NULL) {
+ wlr_log(WLR_INFO,
+ "Opening DRM render node '%s' from WLR_RENDER_DRM_DEVICE",
+ render_name);
+ int drm_fd = open(render_name, O_RDWR | O_CLOEXEC);
+ if (drm_fd < 0) {
+ wlr_log_errno(WLR_ERROR, "Failed to open '%s'", render_name);
+ return false;
+ }
+ if (drmGetNodeTypeFromFd(drm_fd) != DRM_NODE_RENDER) {
+ wlr_log(WLR_ERROR, "'%s' is not a DRM render node", render_name);
+ close(drm_fd);
+ return false;
+ }
+ *drm_fd_ptr = drm_fd;
+ *own_drm_fd = true;
+ return true;
+ }
+
+ // Prefer the backend's DRM node, if any
+ int backend_drm_fd = wlr_backend_get_drm_fd(backend);
+ if (backend_drm_fd >= 0) {
+ *drm_fd_ptr = backend_drm_fd;
+ *own_drm_fd = false;
+ return true;
+ }
+
+ // If the backend hasn't picked a DRM FD, but accepts DMA-BUFs, pick an
+ // arbitrary render node
+ uint32_t backend_caps = backend_get_buffer_caps(backend);
+ if (backend_caps & WLR_BUFFER_CAP_DMABUF) {
+ int drm_fd = open_drm_render_node();
+ if (drm_fd < 0) {
+ return false;
+ }
+ *drm_fd_ptr = drm_fd;
+ *own_drm_fd = true;
+ return true;
+ }
+
+ return false;
+}