From 51c7078b9ec413ebd8316501f01ccf769a090f64 Mon Sep 17 00:00:00 2001 From: Erik Reider <35975961+ErikReider@users.noreply.github.com> Date: Sun, 31 Dec 2023 00:32:39 +0100 Subject: Converted fx_renderer to impl wlr_renderer Makes the fx_renderer the default renderer for everything, no wlr_gles2 rendering. This includes wlr_render_pass (fx_render_pass in our case) --- include/render/fx_renderer/fx_renderer.h | 230 +++-- include/render/fx_renderer/shaders.h | 69 ++ include/render/fx_renderer/util.h | 11 + include/render/pass.h | 52 ++ include/render/pixel_format.h | 4 +- render/fx_renderer/fx_framebuffer.c | 140 ++- render/fx_renderer/fx_pass.c | 375 ++++++++ render/fx_renderer/fx_renderer.c | 994 +++++++++++++-------- render/fx_renderer/fx_texture.c | 91 +- render/fx_renderer/gles2/shaders/box_shadow.frag | 5 + render/fx_renderer/gles2/shaders/common.vert | 9 +- render/fx_renderer/gles2/shaders/quad.frag | 8 +- render/fx_renderer/gles2/shaders/stencil_mask.frag | 5 + render/fx_renderer/gles2/shaders/tex.frag | 5 + render/fx_renderer/meson.build | 4 + render/fx_renderer/pixel_format.c | 175 ++++ render/fx_renderer/shaders.c | 204 +++++ render/fx_renderer/util.c | 113 +++ render/pixel_format.c | 2 +- tinywl/tinywl.c | 4 +- types/scene/wlr_scene.c | 511 +---------- 21 files changed, 1946 insertions(+), 1065 deletions(-) create mode 100644 include/render/fx_renderer/shaders.h create mode 100644 include/render/fx_renderer/util.h create mode 100644 include/render/pass.h create mode 100644 render/fx_renderer/fx_pass.c create mode 100644 render/fx_renderer/pixel_format.c create mode 100644 render/fx_renderer/shaders.c create mode 100644 render/fx_renderer/util.c diff --git a/include/render/fx_renderer/fx_renderer.h b/include/render/fx_renderer/fx_renderer.h index ffd31da..6483f12 100644 --- a/include/render/fx_renderer/fx_renderer.h +++ b/include/render/fx_renderer/fx_renderer.h @@ -4,74 +4,63 @@ #include #include #include +#include #include -#include +#include #include #include #include + #include "render/fx_renderer/fx_stencilbuffer.h" +#include "render/fx_renderer/shaders.h" +#include "render/pass.h" #include "types/fx/shadow_data.h" -enum fx_tex_shader_source { - SHADER_SOURCE_TEXTURE_RGBA = 1, - SHADER_SOURCE_TEXTURE_RGBX = 2, - SHADER_SOURCE_TEXTURE_EXTERNAL = 3, -}; - -struct quad_shader { - GLuint program; - GLint proj; - GLint color; - GLint pos_attrib; +struct fx_pixel_format { + uint32_t drm_format; + // optional field, if empty then internalformat = format + GLint gl_internalformat; + GLint gl_format, gl_type; + bool has_alpha; }; -struct tex_shader { - GLuint program; - GLint proj; - GLint tex; - GLint alpha; - GLint pos_attrib; - GLint tex_attrib; - GLint size; - GLint position; - GLint radius; -}; +bool is_fx_pixel_format_supported(const struct fx_renderer *renderer, + const struct fx_pixel_format *format); +const struct fx_pixel_format *get_fx_format_from_drm(uint32_t fmt); +const struct fx_pixel_format *get_fx_format_from_gl( + GLint gl_format, GLint gl_type, bool alpha); +const uint32_t *get_fx_shm_formats(const struct fx_renderer *renderer, + size_t *len); -struct stencil_mask_shader { - GLuint program; - GLint proj; - GLint color; - GLint pos_attrib; - GLint half_size; - GLint position; - GLint radius; -}; - -struct box_shadow_shader { - GLuint program; - GLint proj; - GLint color; - GLint pos_attrib; - GLint position; - GLint size; - GLint blur_sigma; - GLint corner_radius; -}; +/// +/// fx_framebuffer +/// struct fx_framebuffer { - bool initialized; - - GLuint fbo; - GLuint rbo; - - struct wlr_buffer *wlr_buffer; + struct wlr_buffer *buffer; struct fx_renderer *renderer; struct wl_list link; // fx_renderer.buffers - struct wlr_addon addon; EGLImageKHR image; + GLuint rbo; + GLuint fbo; + + struct wlr_addon addon; }; +struct fx_framebuffer *fx_framebuffer_get_or_create(struct fx_renderer *renderer, + struct wlr_buffer *wlr_buffer); + +void fx_framebuffer_bind(struct fx_framebuffer *buffer); + +void fx_framebuffer_bind_wlr_fbo(struct fx_renderer *renderer); + +void fx_framebuffer_destroy(struct fx_framebuffer *buffer); + +/// +/// fx_texture +/// + struct fx_texture { struct wlr_texture wlr_texture; struct fx_renderer *fx_renderer; @@ -94,34 +83,62 @@ struct fx_texture { struct wlr_addon buffer_addon; }; -struct fx_renderer { - float projection[9]; +struct fx_texture_attribs { + GLenum target; /* either GL_TEXTURE_2D or GL_TEXTURE_EXTERNAL_OES */ + GLuint tex; - int viewport_width, viewport_height; + bool has_alpha; +}; - struct wlr_output *wlr_output; +struct fx_texture *fx_get_texture(struct wlr_texture *wlr_texture); - struct wlr_egl *egl; +struct wlr_texture *fx_texture_from_buffer(struct wlr_renderer *wlr_renderer, + struct wlr_buffer *buffer); - struct fx_stencilbuffer stencil_buffer; +void fx_texture_destroy(struct fx_texture *texture); - struct wl_list textures; // fx_texture.link - struct wl_list buffers; // fx_framebuffer.link +bool wlr_texture_is_fx(struct wlr_texture *wlr_texture); - // The FBO and texture used by wlroots - GLuint wlr_main_buffer_fbo; - struct wlr_gles2_texture_attribs wlr_main_texture_attribs; +void fx_texture_get_attribs(struct wlr_texture *texture, + struct fx_texture_attribs *attribs); - struct wlr_addon addon; +/// +/// fx_renderer +/// +struct fx_renderer { + struct wlr_renderer wlr_renderer; + + float projection[9]; + struct wlr_egl *egl; + int drm_fd; + + const char *exts_str; struct { + bool EXT_read_format_bgra; + bool KHR_debug; bool OES_egl_image_external; bool OES_egl_image; + bool EXT_texture_type_2_10_10_10_REV; + bool OES_texture_half_float_linear; + bool EXT_texture_norm16; + bool EXT_disjoint_timer_query; } exts; struct { PFNGLEGLIMAGETARGETTEXTURE2DOESPROC glEGLImageTargetTexture2DOES; + PFNGLDEBUGMESSAGECALLBACKKHRPROC glDebugMessageCallbackKHR; + PFNGLDEBUGMESSAGECONTROLKHRPROC glDebugMessageControlKHR; + PFNGLPOPDEBUGGROUPKHRPROC glPopDebugGroupKHR; + PFNGLPUSHDEBUGGROUPKHRPROC glPushDebugGroupKHR; PFNGLEGLIMAGETARGETRENDERBUFFERSTORAGEOESPROC glEGLImageTargetRenderbufferStorageOES; + PFNGLGETGRAPHICSRESETSTATUSKHRPROC glGetGraphicsResetStatusKHR; + PFNGLGENQUERIESEXTPROC glGenQueriesEXT; + PFNGLDELETEQUERIESEXTPROC glDeleteQueriesEXT; + PFNGLQUERYCOUNTEREXTPROC glQueryCounterEXT; + PFNGLGETQUERYOBJECTIVEXTPROC glGetQueryObjectivEXT; + PFNGLGETQUERYOBJECTUI64VEXTPROC glGetQueryObjectui64vEXT; + PFNGLGETINTEGER64VEXTPROC glGetInteger64vEXT; } procs; struct { @@ -132,60 +149,35 @@ struct fx_renderer { struct box_shadow_shader box_shadow; struct stencil_mask_shader stencil_mask; } shaders; -}; - -/// -/// fx_framebuffer -/// - -struct fx_framebuffer fx_framebuffer_create(void); - -void fx_framebuffer_bind(struct fx_framebuffer *buffer); - -void fx_framebuffer_bind_wlr_fbo(struct fx_renderer *renderer); - -void fx_framebuffer_update(struct fx_renderer *fx_renderer, struct fx_framebuffer *fx_buffer, - int width, int height); - -void fx_framebuffer_add_stencil_buffer(struct fx_framebuffer *buffer, int width, int height); - -void fx_framebuffer_release(struct fx_framebuffer *buffer); - -/// -/// fx_texture -/// - -struct fx_texture *fx_get_texture(struct wlr_texture *wlr_texture); - -struct fx_texture *fx_texture_from_buffer(struct fx_renderer *fx_renderer, - struct wlr_buffer *buffer); - -void fx_texture_destroy(struct fx_texture *texture); - -bool wlr_texture_is_fx(struct wlr_texture *wlr_texture); - -void wlr_gles2_texture_get_fx_attribs(struct fx_texture *texture, - struct wlr_gles2_texture_attribs *attribs); -/// -/// fx_renderer -/// + struct wl_list buffers; // fx_framebuffer.link + struct wl_list textures; // fx_texture.link -void fx_renderer_init_addon(struct wlr_egl *egl, struct wlr_output *output, - struct wlr_addon_set *addons, const void * owner); + struct fx_stencilbuffer stencil_buffer; -struct fx_renderer *fx_renderer_addon_find(struct wlr_addon_set *addons, - const void * owner); + struct fx_framebuffer *current_buffer; + uint32_t viewport_width, viewport_height; +}; -struct fx_renderer *fx_renderer_create(struct wlr_egl *egl, struct wlr_output *output); +bool wlr_renderer_is_fx(struct wlr_renderer *wlr_renderer); -void fx_renderer_begin(struct fx_renderer *renderer, int width, int height); +struct fx_renderer *fx_get_renderer( + struct wlr_renderer *wlr_renderer); +struct fx_render_timer *fx_get_render_timer( + struct wlr_render_timer *timer); +struct fx_texture *fx_get_texture( + struct wlr_texture *wlr_texture); -void fx_renderer_end(struct fx_renderer *renderer); +struct wlr_renderer *fx_renderer_create_with_drm_fd(int drm_fd); +struct wlr_renderer *fx_renderer_create(struct wlr_backend *backend); +struct wlr_renderer *fx_renderer_create_egl(struct wlr_egl *egl); -void fx_renderer_clear(const float color[static 4]); +struct wlr_egl *wlr_fx_renderer_get_egl(struct wlr_renderer *renderer); -void fx_renderer_scissor(struct wlr_box *box); +void push_fx_debug_(struct fx_renderer *renderer, + const char *file, const char *func); +#define push_fx_debug(renderer) push_fx_debug_(renderer, _WLR_FILENAME, __func__) +void pop_fx_debug(struct fx_renderer *renderer); // Initialize the stenciling work void fx_renderer_stencil_mask_init(void); @@ -200,17 +192,19 @@ void fx_renderer_stencil_enable(void); void fx_renderer_stencil_disable(void); -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); +/// +/// Render Timer +/// -void fx_render_rect(struct fx_renderer *renderer, const struct wlr_box *box, - const float color[static 4], const float projection[static 9]); +struct fx_render_timer { + struct wlr_render_timer base; + struct fx_renderer *renderer; + struct timespec cpu_start; + struct timespec cpu_end; + GLuint id; + GLint64 gl_cpu_end; +}; -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); +bool wlr_render_timer_is_fx(struct wlr_render_timer *timer); #endif diff --git a/include/render/fx_renderer/shaders.h b/include/render/fx_renderer/shaders.h new file mode 100644 index 0000000..584b18e --- /dev/null +++ b/include/render/fx_renderer/shaders.h @@ -0,0 +1,69 @@ +#ifndef _FX_SHADERS_H +#define _FX_SHADERS_H + +#include +#include + +struct fx_renderer; + +GLuint compile_shader(GLuint type, const GLchar *src); + +GLuint link_program(const GLchar *frag_src); + +bool check_gl_ext(const char *exts, const char *ext); + +void load_gl_proc(void *proc_ptr, const char *name); + +enum fx_tex_shader_source { + SHADER_SOURCE_TEXTURE_RGBA = 1, + SHADER_SOURCE_TEXTURE_RGBX = 2, + SHADER_SOURCE_TEXTURE_EXTERNAL = 3, +}; + +struct quad_shader { + GLuint program; + GLint proj; + GLint color; + GLint pos_attrib; +}; + +struct tex_shader { + GLuint program; + GLint proj; + GLint tex_proj; + GLint tex; + GLint alpha; + GLint pos_attrib; + GLint size; + GLint position; + GLint radius; +}; + +struct stencil_mask_shader { + GLuint program; + GLint proj; + GLint tex_proj; + GLint tex; + GLint pos_attrib; + GLint half_size; + GLint position; + GLint color; + GLint radius; +}; + +struct box_shadow_shader { + GLuint program; + GLint proj; + GLint tex_proj; + GLint tex; + GLint pos_attrib; + GLint position; + GLint size; + GLint color; + GLint blur_sigma; + GLint corner_radius; +}; + +bool link_shaders(struct fx_renderer *renderer); + +#endif diff --git a/include/render/fx_renderer/util.h b/include/render/fx_renderer/util.h new file mode 100644 index 0000000..c0afc69 --- /dev/null +++ b/include/render/fx_renderer/util.h @@ -0,0 +1,11 @@ +#ifndef _FX_UTIL_H +#define _FX_UTIL_H + +#include +#include +#include + +bool open_preferred_drm_fd(struct wlr_backend *backend, int *drm_fd_ptr, + bool *own_drm_fd); + +#endif diff --git a/include/render/pass.h b/include/render/pass.h new file mode 100644 index 0000000..9551f81 --- /dev/null +++ b/include/render/pass.h @@ -0,0 +1,52 @@ +#ifndef FX_RENDER_PASS_H +#define FX_RENDER_PASS_H + +#include +#include +#include +#include + +struct fx_gles_render_pass { + struct wlr_render_pass base; + struct fx_framebuffer *buffer; + float projection_matrix[9]; + struct fx_render_timer *timer; +}; + +/** + * Begin a new render pass with the supplied destination buffer. + * + * Callers must call wlr_render_pass_submit() once they are done with the + * render pass. + */ +struct fx_gles_render_pass *fx_renderer_begin_buffer_pass(struct wlr_renderer *renderer, + struct wlr_buffer *buffer, const struct wlr_buffer_pass_options *options); + +struct fx_render_texture_options { + struct wlr_render_texture_options base; + int corner_radius; +}; + +struct fx_render_texture_options fx_render_texture_options_default( + const struct wlr_render_texture_options *base); + +struct fx_render_rect_options { + struct wlr_render_rect_options base; +}; + +struct fx_render_rect_options fx_render_rect_options_default( + const struct wlr_render_rect_options *base); + +/** + * Render a fx texture. + */ +void fx_render_pass_add_texture(struct fx_gles_render_pass *render_pass, + const struct fx_render_texture_options *options); + +/** + * Render a rectangle. + */ +void fx_render_pass_add_rect(struct fx_gles_render_pass *render_pass, + const struct fx_render_rect_options *options); + +#endif diff --git a/include/render/pixel_format.h b/include/render/pixel_format.h index a024ff9..6ca11f3 100644 --- a/include/render/pixel_format.h +++ b/include/render/pixel_format.h @@ -1,5 +1,5 @@ -#ifndef RENDER_PIXEL_FORMAT_H -#define RENDER_PIXEL_FORMAT_H +#ifndef FX_RENDER_PIXEL_FORMAT_H +#define FX_RENDER_PIXEL_FORMAT_H #include diff --git a/render/fx_renderer/fx_framebuffer.c b/render/fx_renderer/fx_framebuffer.c index 4de6439..3e52f49 100644 --- a/render/fx_renderer/fx_framebuffer.c +++ b/render/fx_renderer/fx_framebuffer.c @@ -12,7 +12,7 @@ static void handle_buffer_destroy(struct wlr_addon *addon) { struct fx_framebuffer *buffer = wl_container_of(addon, buffer, addon); - fx_framebuffer_release(buffer); + fx_framebuffer_destroy(buffer); } static const struct wlr_addon_interface buffer_addon_impl = { @@ -20,101 +20,83 @@ static const struct wlr_addon_interface buffer_addon_impl = { .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 fx_framebuffer_create(void) { - return (struct fx_framebuffer) { - .initialized = false, - .fbo = -1, - .rbo = -1, - .wlr_buffer = NULL, - .image = NULL, - }; -} - -void fx_framebuffer_bind(struct fx_framebuffer *fx_buffer) { - glBindFramebuffer(GL_FRAMEBUFFER, fx_buffer->fbo); -} + 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; -void fx_framebuffer_bind_wlr_fbo(struct fx_renderer *renderer) { - glBindFramebuffer(GL_FRAMEBUFFER, renderer->wlr_main_buffer_fbo); -} + struct wlr_dmabuf_attributes dmabuf = {0}; + if (!wlr_buffer_get_dmabuf(wlr_buffer, &dmabuf)) { + goto error_buffer; + } -void fx_framebuffer_update(struct fx_renderer *fx_renderer, struct fx_framebuffer *fx_buffer, - int width, int height) { - struct wlr_output *output = fx_renderer->wlr_output; + 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; + } - fx_buffer->renderer = fx_renderer; + push_fx_debug(renderer); - bool first_alloc = false; + glGenRenderbuffers(1, &buffer->rbo); + glBindRenderbuffer(GL_RENDERBUFFER, buffer->rbo); + renderer->procs.glEGLImageTargetRenderbufferStorageOES(GL_RENDERBUFFER, + buffer->image); + glBindRenderbuffer(GL_RENDERBUFFER, 0); - if (!fx_buffer->wlr_buffer || - fx_buffer->wlr_buffer->width != width || - fx_buffer->wlr_buffer->height != height) { - wlr_buffer_drop(fx_buffer->wlr_buffer); - fx_buffer->wlr_buffer = wlr_allocator_create_buffer(output->allocator, - width, height, &output->swapchain->format); - first_alloc = true; - } + 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); + glBindFramebuffer(GL_FRAMEBUFFER, 0); - if (fx_buffer->fbo == (uint32_t) -1 || first_alloc) { - glGenFramebuffers(1, &fx_buffer->fbo); - first_alloc = true; - } + pop_fx_debug(renderer); - if (fx_buffer->rbo == (uint32_t) -1 || first_alloc) { - struct wlr_dmabuf_attributes dmabuf = {0}; - if (!wlr_buffer_get_dmabuf(fx_buffer->wlr_buffer, &dmabuf)) { - goto error_buffer; - } - - bool external_only; - fx_buffer->image = wlr_egl_create_image_from_dmabuf(fx_renderer->egl, - &dmabuf, &external_only); - if (fx_buffer->image == EGL_NO_IMAGE_KHR) { - goto error_buffer; - } - - glGenRenderbuffers(1, &fx_buffer->rbo); - glBindRenderbuffer(GL_RENDERBUFFER, fx_buffer->rbo); - fx_renderer->procs.glEGLImageTargetRenderbufferStorageOES(GL_RENDERBUFFER, - fx_buffer->image); - glBindRenderbuffer(GL_RENDERBUFFER, 0); - - glBindFramebuffer(GL_FRAMEBUFFER, fx_buffer->fbo); - glFramebufferRenderbuffer(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, - GL_RENDERBUFFER, fx_buffer->rbo); - GLenum fb_status = glCheckFramebufferStatus(GL_FRAMEBUFFER); - glBindFramebuffer(GL_FRAMEBUFFER, 0); - - if (fb_status != GL_FRAMEBUFFER_COMPLETE) { - wlr_log(WLR_ERROR, "Failed to create FBO"); - goto error_image; - } + if (fb_status != GL_FRAMEBUFFER_COMPLETE) { + wlr_log(WLR_ERROR, "Failed to create FBO"); + goto error_image; } - if (!fx_buffer->initialized) { - fx_buffer->initialized = true; + wlr_addon_init(&buffer->addon, &wlr_buffer->addons, renderer, + &buffer_addon_impl); - wlr_addon_init(&fx_buffer->addon, &fx_buffer->wlr_buffer->addons, fx_renderer, - &buffer_addon_impl); + wl_list_insert(&renderer->buffers, &buffer->link); - wl_list_insert(&fx_renderer->buffers, &fx_buffer->link); - } + wlr_log(WLR_DEBUG, "Created GL FBO for buffer %dx%d", + wlr_buffer->width, wlr_buffer->height); - if (first_alloc) { - wlr_log(WLR_DEBUG, "Created GL FBO for buffer %dx%d", - fx_buffer->wlr_buffer->width, fx_buffer->wlr_buffer->height); - } + return buffer; - return; error_image: - wlr_egl_destroy_image(fx_renderer->egl, fx_buffer->image); + wlr_egl_destroy_image(renderer->egl, buffer->image); error_buffer: - wlr_log(WLR_ERROR, "Could not create FX buffer! Aborting..."); - abort(); + 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_release(struct fx_framebuffer *fx_buffer) { +void fx_framebuffer_destroy(struct fx_framebuffer *fx_buffer) { // Release the framebuffer wl_list_remove(&fx_buffer->link); wlr_addon_finish(&fx_buffer->addon); @@ -132,5 +114,5 @@ void fx_framebuffer_release(struct fx_framebuffer *fx_buffer) { wlr_egl_restore_context(&prev_ctx); - fx_buffer->initialized = false; + 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..6a1570c --- /dev/null +++ b/render/fx_renderer/fx_pass.c @@ -0,0 +1,375 @@ +#define _POSIX_C_SOURCE 199309L +#include +#include +#include +#include +#include +#include +#include + +#include "render/egl.h" +#include "render/fx_renderer/fx_renderer.h" +#include "render/fx_renderer/matrix.h" +#include "render/pass.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, + }; + 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 = {}; + 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(®ion, box->x, box->y, box->width, box->height); + + if (clip) { + pixman_region32_intersect(®ion, ®ion, clip); + } + + int rects_len; + const pixman_box32_t *rects = pixman_region32_rectangles(®ion, &rects_len); + if (rects_len == 0) { + pixman_region32_fini(®ion); + 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(®ion); +} + +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; + } +} + +// 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); + + 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, dst_box.width, dst_box.height); + glUniform2f(shader->position, dst_box.x, dst_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); +} + +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 ""; + } +} + +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 7bc0be1..9d3b763 100644 --- a/render/fx_renderer/fx_renderer.c +++ b/render/fx_renderer/fx_renderer.c @@ -3,28 +3,29 @@ https://gitlab.freedesktop.org/wlroots/wlroots/-/tree/master/render/gles2 */ +#define _POSIX_C_SOURCE 199309L #include +#include #include #include #include +#include +#include #include #include #include -#include +#include #include #include #include +#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 @@ -33,319 +34,110 @@ 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"); - - return true; +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; } -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; +bool wlr_render_timer_is_fx(struct wlr_render_timer *timer) { + return timer->impl == &render_timer_impl; } -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; +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; } -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"); +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); - return true; -} + if (renderer->current_buffer != NULL) { + assert(wlr_egl_is_current(renderer->egl)); -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); + glFlush(); + glBindFramebuffer(GL_FRAMEBUFFER, 0); + 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; + wlr_buffer_unlock(renderer->current_buffer->buffer); + renderer->current_buffer = NULL; } - 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(); + if (wlr_buffer == NULL) { + wlr_egl_unset_current(renderer->egl); + return true; } - *(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); - } + wlr_egl_make_current(renderer->egl); - struct fx_texture *tex, *tex_tmp; - wl_list_for_each_safe(tex, tex_tmp, &renderer->textures, link) { - fx_texture_destroy(tex); + struct fx_framebuffer *buffer = fx_framebuffer_get_or_create(renderer, wlr_buffer); + if (buffer == NULL) { + return false; } - fx_stencilbuffer_release(&renderer->stencil_buffer); + wlr_buffer_lock(wlr_buffer); + renderer->current_buffer = buffer; - free(renderer); -} -static const struct wlr_addon_interface fx_renderer_addon_impl = { - .name = "fx_renderer", - .destroy = fx_renderer_handle_destroy, -}; + push_fx_debug(renderer); + glBindFramebuffer(GL_FRAMEBUFFER, renderer->current_buffer->fbo); + pop_fx_debug(renderer); -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); + return true; } -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; +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 ""; } - 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; +static bool fx_renderer_begin(struct wlr_renderer *wlr_renderer, uint32_t width, + uint32_t height) { + struct fx_renderer *renderer = + fx_get_renderer_in_context(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); + push_fx_debug(renderer); - 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"); + 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; + } } - // 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); @@ -357,24 +149,45 @@ void fx_renderer_begin(struct fx_renderer *renderer, int width, int height) { WL_OUTPUT_TRANSFORM_FLIPPED_180); glBlendFunc(GL_ONE, GL_ONE_MINUS_SRC_ALPHA); + + // XXX: maybe we should save output projection and remove some of the need + // for users to sling matricies themselves + + pop_fx_debug(renderer); + + return true; } -void fx_renderer_end(struct fx_renderer *renderer) { +static void fx_renderer_end(struct wlr_renderer *wlr_renderer) { + fx_get_renderer_in_context(wlr_renderer); + // no-op } -void fx_renderer_clear(const float color[static 4]) { +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); + + 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) { @@ -414,40 +227,29 @@ 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(); - } +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"); @@ -457,12 +259,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); @@ -471,62 +270,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); @@ -534,20 +320,21 @@ 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, @@ -589,7 +376,7 @@ static void fx_render_stencil_mask(struct fx_renderer *renderer, glDisableVertexAttribArray(shader.pos_attrib); } -void fx_render_box_shadow(struct fx_renderer *renderer, +static 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) { @@ -646,3 +433,494 @@ void fx_render_box_shadow(struct fx_renderer *renderer, fx_renderer_stencil_mask_fini(); } + +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; + } + + if (renderer->exts.EXT_read_format_bgra) { + return DRM_FORMAT_XRGB8888; + } + return DRM_FORMAT_XBGR8888; +} + +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); + + 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; + } + + 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; + } + + 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; + } + + push_fx_debug(renderer); + + // Make sure any pending drawing is finished before we try to read it + glFinish(); + + glGetError(); // Clear the error flag + + 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 + + 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); + } + } + + 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); + } + + return renderer->drm_fd; +} + +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); + } + + fx_stencilbuffer_release(&renderer->stencil_buffer); + + 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; + } + + 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); +} + +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; + } +} + +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); +} + +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; + } + + 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; + } + + if (own_drm_fd && drm_fd >= 0) { + close(drm_fd); + } + + return renderer; +} + +struct wlr_renderer *fx_renderer_create_with_drm_fd(int drm_fd) { + assert(drm_fd >= 0); + + return renderer_autocreate(NULL, drm_fd); +} + +struct wlr_renderer *fx_renderer_create(struct wlr_backend *backend) { + return renderer_autocreate(backend, -1); +} + +struct wlr_renderer *fx_renderer_create_egl(struct wlr_egl *egl) { + if (!wlr_egl_make_current(egl)) { + return NULL; + } + + const char *exts_str = (const char *)glGetString(GL_EXTENSIONS); + if (exts_str == NULL) { + wlr_log(WLR_ERROR, "Failed to get GL_EXTENSIONS"); + return NULL; + } + + struct fx_renderer *renderer = calloc(1, sizeof(struct fx_renderer)); + if (renderer == NULL) { + return NULL; + } + wlr_renderer_init(&renderer->wlr_renderer, &renderer_impl); + + wl_list_init(&renderer->buffers); + wl_list_init(&renderer->textures); + + renderer->egl = egl; + renderer->exts_str = exts_str; + renderer->drm_fd = -1; + + // Create the stencil buffer + renderer->stencil_buffer = fx_stencilbuffer_create(); + + 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"); + + 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, ¬if_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_texture.c b/render/fx_renderer/fx_texture.c index a8977fa..1952311 100644 --- a/render/fx_renderer/fx_texture.c +++ b/render/fx_renderer/fx_texture.c @@ -1,7 +1,6 @@ #include #include #include -#include #include #include #include @@ -18,7 +17,8 @@ bool wlr_texture_is_fx(struct wlr_texture *wlr_texture) { struct fx_texture *fx_get_texture(struct wlr_texture *wlr_texture) { assert(wlr_texture_is_fx(wlr_texture)); - return (struct fx_texture *) 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, @@ -42,6 +42,10 @@ static bool fx_texture_update_from_buffer(struct wlr_texture *wlr_texture, 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); @@ -60,6 +64,8 @@ static bool fx_texture_update_from_buffer(struct wlr_texture *wlr_texture, 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; @@ -75,7 +81,7 @@ static bool fx_texture_update_from_buffer(struct wlr_texture *wlr_texture, int width = rect.x2 - rect.x1; int height = rect.y2 - rect.y1; glTexSubImage2D(GL_TEXTURE_2D, 0, rect.x1, rect.y1, width, height, - GL_RGBA, GL_UNSIGNED_BYTE, data); + fmt->gl_format, fmt->gl_type, data); } glPixelStorei(GL_UNPACK_ROW_LENGTH_EXT, 0); @@ -84,6 +90,8 @@ static bool fx_texture_update_from_buffer(struct wlr_texture *wlr_texture, glBindTexture(GL_TEXTURE_2D, 0); + pop_fx_debug(texture->fx_renderer); + wlr_egl_restore_context(&prev_ctx); wlr_buffer_end_data_ptr_access(buffer); @@ -104,11 +112,15 @@ static bool fx_texture_invalidate(struct fx_texture *texture) { 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; @@ -155,21 +167,28 @@ static struct fx_texture *fx_texture_create( wlr_log_errno(WLR_ERROR, "Allocation failed"); return NULL; } - wlr_texture_init(&texture->wlr_texture, renderer->wlr_output->renderer, + 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 fx_texture *fx_texture_from_pixels( - struct fx_renderer *renderer, +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; @@ -185,35 +204,43 @@ static struct fx_texture *fx_texture_from_pixels( return NULL; } texture->target = GL_TEXTURE_2D; - texture->has_alpha = false; - texture->drm_format = DRM_FORMAT_XBGR8888; + 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); - glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR); - glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR); glPixelStorei(GL_UNPACK_ROW_LENGTH_EXT, stride / drm_fmt->bytes_per_block); - glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, width, height, 0, - GL_RGBA, GL_UNSIGNED_BYTE, data); + 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; + return &texture->wlr_texture; } static struct wlr_texture *fx_texture_from_dmabuf( - struct fx_renderer *renderer, + 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; @@ -252,15 +279,17 @@ static struct wlr_texture *fx_texture_from_dmabuf( 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); - glTexParameteri(texture->target, GL_TEXTURE_MAG_FILTER, GL_LINEAR); - glTexParameteri(texture->target, GL_TEXTURE_MIN_FILTER, GL_LINEAR); 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; @@ -277,7 +306,7 @@ static const struct wlr_addon_interface texture_addon_impl = { .destroy = texture_handle_buffer_destroy, }; -static struct fx_texture *fx_texture_from_dmabuf_buffer( +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 = @@ -290,11 +319,11 @@ static struct fx_texture *fx_texture_from_dmabuf_buffer( return false; } wlr_buffer_lock(texture->buffer); - return texture; + return &texture->wlr_texture; } struct wlr_texture *wlr_texture = - fx_texture_from_dmabuf(renderer, dmabuf); + fx_texture_from_dmabuf(&renderer->wlr_renderer, dmabuf); if (wlr_texture == NULL) { return false; } @@ -304,11 +333,13 @@ static struct fx_texture *fx_texture_from_dmabuf_buffer( wlr_addon_init(&texture->buffer_addon, &buffer->addons, renderer, &texture_addon_impl); - return texture; + return &texture->wlr_texture; } -struct fx_texture *fx_texture_from_buffer(struct fx_renderer *renderer, +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; @@ -317,7 +348,7 @@ struct fx_texture *fx_texture_from_buffer(struct fx_renderer *renderer, 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 fx_texture *tex = fx_texture_from_pixels(renderer, + 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; @@ -326,10 +357,12 @@ struct fx_texture *fx_texture_from_buffer(struct fx_renderer *renderer, } } -void wlr_gles2_texture_get_fx_attribs(struct fx_texture *texture, - struct wlr_gles2_texture_attribs *attribs) { - memset(attribs, 0, sizeof(*attribs)); - attribs->target = texture->target; - attribs->tex = texture->tex; - attribs->has_alpha = texture->has_alpha; +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 f6d44ae..ef5521f 100644 --- a/render/fx_renderer/meson.build +++ b/render/fx_renderer/meson.build @@ -7,6 +7,10 @@ endif wlr_files += files( 'matrix.c', + 'util.c', + 'shaders.c', + 'pixel_format.c', + 'fx_pass.c', 'fx_framebuffer.c', 'fx_stencilbuffer.c', 'fx_texture.c', 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 +#include +#include +#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..6196d19 --- /dev/null +++ b/render/fx_renderer/shaders.c @@ -0,0 +1,204 @@ +#include +#include +#include +#include + +#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->tex_proj = glGetUniformLocation(prog, "tex_proj"); + 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->tex_proj = glGetUniformLocation(prog, "tex_proj"); + 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; + } + + // TODO: Fix the shader compilation errors + // // stencil mask shader + // if (!link_stencil_mask_program(&renderer->shaders.stencil_mask)) { + // return false; + // } + // // box shadow shader + // if (!link_box_shadow_program(&renderer->shaders.box_shadow)) { + // 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 +#include +#include +#include +#include + +#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; +} diff --git a/render/pixel_format.c b/render/pixel_format.c index e303799..b81f561 100644 --- a/render/pixel_format.c +++ b/render/pixel_format.c @@ -1,6 +1,6 @@ +#include #include #include - #include "render/pixel_format.h" static const struct wlr_pixel_format_info pixel_format_info[] = { diff --git a/tinywl/tinywl.c b/tinywl/tinywl.c index adc7b38..8b6df63 100644 --- a/tinywl/tinywl.c +++ b/tinywl/tinywl.c @@ -29,6 +29,8 @@ #include "types/wlr_scene.h" +#include "render/fx_renderer/fx_renderer.h" + /* For brevity's sake, struct members are annotated where they are used. */ enum tinywl_cursor_mode { TINYWL_CURSOR_PASSTHROUGH, @@ -919,7 +921,7 @@ int main(int argc, char *argv[]) { * can also specify a renderer using the WLR_RENDERER env var. * The renderer is responsible for defining the various pixel formats it * supports for shared memory, this configures that for clients. */ - server.renderer = wlr_renderer_autocreate(server.backend); + server.renderer = fx_renderer_create(server.backend); if (server.renderer == NULL) { wlr_log(WLR_ERROR, "failed to create wlr_renderer"); return 1; diff --git a/types/scene/wlr_scene.c b/types/scene/wlr_scene.c index 45b1a18..343a901 100644 --- a/types/scene/wlr_scene.c +++ b/types/scene/wlr_scene.c @@ -15,7 +15,7 @@ #include #include -#include "render/fx_renderer/fx_renderer.h" +#include "render/pass.h" #include "types/fx/shadow_data.h" #include "types/wlr_buffer.h" #include "types/wlr_output.h" @@ -306,7 +306,7 @@ struct render_data { struct wlr_scene_output *output; - struct wlr_render_pass *render_pass; + struct fx_gles_render_pass *render_pass; pixman_region32_t damage; }; @@ -1148,211 +1148,6 @@ struct wlr_scene_node *wlr_scene_node_at(struct wlr_scene_node *node, return NULL; } -// static void scissor_output(struct wlr_output *output, pixman_box32_t *rect) { -// struct wlr_box box = { -// .x = rect->x1, -// .y = rect->y1, -// .width = rect->x2 - rect->x1, -// .height = rect->y2 - rect->y1, -// }; -// -// int ow, oh; -// wlr_output_transformed_resolution(output, &ow, &oh); -// -// enum wl_output_transform transform = -// wlr_output_transform_invert(output->transform); -// wlr_box_transform(&box, &box, transform, ow, oh); -// -// fx_renderer_scissor(&box); -// } -// -// static void render_rect(struct fx_renderer *fx_renderer, struct wlr_output *output, -// pixman_region32_t *damage, const float color[static 4], -// const struct wlr_box *box, const float matrix[static 9]) { -// assert(fx_renderer); -// -// int nrects; -// pixman_box32_t *rects = pixman_region32_rectangles(damage, &nrects); -// for (int i = 0; i < nrects; ++i) { -// scissor_output(output, &rects[i]); -// fx_render_rect(fx_renderer, box, color, matrix); -// } -// } -// -// static void render_texture(struct fx_renderer *fx_renderer, struct wlr_output *output, -// pixman_region32_t *damage, struct wlr_texture *texture, -// const struct wlr_fbox *src_box, const struct wlr_box *dst_box, -// const float matrix[static 9], float opacity, int corner_radius) { -// assert(fx_renderer); -// -// struct wlr_fbox default_src_box = {0}; -// if (wlr_fbox_empty(src_box)) { -// default_src_box.width = texture->width; -// default_src_box.height = texture->height; -// src_box = &default_src_box; -// } -// -// // ensure the box is updated as per the output orientation -// struct wlr_box transformed_box; -// int width, height; -// wlr_output_transformed_resolution(output, &width, &height); -// wlr_box_transform(&transformed_box, dst_box, -// wlr_output_transform_invert(output->transform), width, height); -// -// int nrects; -// pixman_box32_t *rects = pixman_region32_rectangles(damage, &nrects); -// for (int i = 0; i < nrects; ++i) { -// scissor_output(output, &rects[i]); -// -// fx_render_subtexture_with_matrix(fx_renderer, texture, src_box, -// &transformed_box, matrix, opacity, corner_radius); -// } -// } -// -// static void render_box_shadow(struct fx_renderer *fx_renderer, -// struct wlr_output *output, pixman_region32_t *surface_damage, -// const struct wlr_box *surface_box, int corner_radius, -// struct shadow_data *shadow_data) { -// // don't damage area behind window since we dont render it anyway -// 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_intersect(&inner_region, &inner_region, surface_damage); -// -// pixman_region32_t damage; -// pixman_region32_init(&damage); -// pixman_region32_subtract(&damage, surface_damage, &inner_region); -// if (!pixman_region32_not_empty(&damage)) { -// goto damage_finish; -// } -// -// struct wlr_box shadow_box = { -// .x = surface_box->x - shadow_data->blur_sigma, -// .y = surface_box->y - shadow_data->blur_sigma, -// .width = surface_box->width + 2 * shadow_data->blur_sigma, -// .height = surface_box->height + 2 * shadow_data->blur_sigma, -// }; -// float matrix[9]; -// wlr_matrix_project_box(matrix, &shadow_box, WL_OUTPUT_TRANSFORM_NORMAL, 0, -// output->transform_matrix); -// -// // ensure the box is updated as per the output orientation -// struct wlr_box transformed_box; -// int width, height; -// wlr_output_transformed_resolution(output, &width, &height); -// wlr_box_transform(&transformed_box, &shadow_box, -// wlr_output_transform_invert(output->transform), width, height); -// -// int nrects; -// pixman_box32_t *rects = pixman_region32_rectangles(&damage, &nrects); -// for (int i = 0; i < nrects; ++i) { -// scissor_output(output, &rects[i]); -// fx_render_box_shadow(fx_renderer, &transformed_box, surface_box, matrix, -// corner_radius, shadow_data); -// } -// -// damage_finish: -// pixman_region32_fini(&damage); -// pixman_region32_fini(&inner_region); -// } -// -// static void scene_node_render(struct fx_renderer *fx_renderer, struct wlr_scene_node *node, -// struct wlr_scene_output *scene_output, pixman_region32_t *damage) { -// int x, y; -// wlr_scene_node_coords(node, &x, &y); -// x -= scene_output->x; -// y -= scene_output->y; -// -// struct wlr_output *output = scene_output->output; -// -// pixman_region32_t render_region; -// pixman_region32_init(&render_region); -// pixman_region32_copy(&render_region, &node->visible); -// pixman_region32_translate(&render_region, -scene_output->x, -scene_output->y); -// scale_output_damage(&render_region, output->scale); -// pixman_region32_intersect(&render_region, &render_region, damage); -// if (!pixman_region32_not_empty(&render_region)) { -// pixman_region32_fini(&render_region); -// return; -// } -// -// struct wlr_box dst_box = { -// .x = x, -// .y = y, -// }; -// scene_node_get_size(node, &dst_box.width, &dst_box.height); -// scale_box(&dst_box, output->scale); -// -// struct wlr_texture *texture; -// float matrix[9]; -// enum wl_output_transform transform; -// switch (node->type) { -// case WLR_SCENE_NODE_TREE: -// assert(false); -// break; -// case WLR_SCENE_NODE_RECT:; -// struct wlr_scene_rect *scene_rect = scene_rect_from_node(node); -// -// render_rect(fx_renderer, output, &render_region, scene_rect->color, &dst_box, -// output->transform_matrix); -// break; -// case WLR_SCENE_NODE_BUFFER:; -// struct wlr_scene_buffer *scene_buffer = wlr_scene_buffer_from_node(node); -// assert(scene_buffer->buffer); -// -// struct wlr_renderer *renderer = output->renderer; -// texture = scene_buffer_get_texture(scene_buffer, renderer); -// -// transform = wlr_output_transform_invert(scene_buffer->transform); -// wlr_matrix_project_box(matrix, &dst_box, transform, 0.0, -// output->transform_matrix); -// -// // Some surfaces (mostly GTK 4) decorate their windows with shadows -// // which extends the node size past the actual window size. This gets -// // the actual surface geometry, mostly ignoring CSD decorations -// // but only if we need to. -// if (scene_buffer->corner_radius != 0 || -// scene_buffer_has_shadow(&scene_buffer->shadow_data)) { -// struct wlr_scene_surface *scene_surface = NULL; -// if ((scene_surface = wlr_scene_surface_from_buffer(scene_buffer)) && -// wlr_surface_is_xdg_surface(scene_surface->surface)) { -// struct wlr_xdg_surface *xdg_surface = -// wlr_xdg_surface_from_wlr_surface(scene_surface->surface); -// -// struct wlr_box geometry; -// wlr_xdg_surface_get_geometry(xdg_surface, &geometry); -// dst_box.width = fmin(dst_box.width, geometry.width); -// dst_box.height = fmin(dst_box.height, geometry.height); -// dst_box.x = fmax(dst_box.x, geometry.x + x); -// dst_box.y = fmax(dst_box.y, geometry.y + y); -// } -// } -// -// // Shadow -// if (scene_buffer_has_shadow(&scene_buffer->shadow_data)) { -// // TODO: Compensate for SSD borders here -// render_box_shadow(fx_renderer, output, &render_region, &dst_box, -// scene_buffer->corner_radius, &scene_buffer->shadow_data); -// } -// -// // Clip the damage to the dst_box before rendering the texture -// pixman_region32_intersect_rect(&render_region, &render_region, -// dst_box.x, dst_box.y, dst_box.width, dst_box.height); -// -// render_texture(fx_renderer, output, &render_region, texture, &scene_buffer->src_box, -// &dst_box, matrix, scene_buffer->opacity, scene_buffer->corner_radius); -// -// wl_signal_emit_mutable(&scene_buffer->events.output_present, scene_output); -// break; -// } -// -// pixman_region32_fini(&render_region); -// } - struct render_list_entry { struct wlr_scene_node *node; bool sent_dmabuf_feedback; @@ -1396,16 +1191,19 @@ static void scene_entry_render(struct render_list_entry *entry, const struct ren case WLR_SCENE_NODE_RECT:; struct wlr_scene_rect *scene_rect = wlr_scene_rect_from_node(node); - wlr_render_pass_add_rect(data->render_pass, &(struct wlr_render_rect_options){ - .box = dst_box, - .color = { - .r = scene_rect->color[0], - .g = scene_rect->color[1], - .b = scene_rect->color[2], - .a = scene_rect->color[3], + struct fx_render_rect_options rect_options = { + .base = { + .box = dst_box, + .color = { + .r = scene_rect->color[0], + .g = scene_rect->color[1], + .b = scene_rect->color[2], + .a = scene_rect->color[3], + }, + .clip = &render_region, }, - .clip = &render_region, - }); + }; + fx_render_pass_add_rect(data->render_pass, &rect_options); break; case WLR_SCENE_NODE_BUFFER:; struct wlr_scene_buffer *scene_buffer = wlr_scene_buffer_from_node(node); @@ -1421,17 +1219,21 @@ static void scene_entry_render(struct render_list_entry *entry, const struct ren wlr_output_transform_invert(scene_buffer->transform); transform = wlr_output_transform_compose(transform, data->transform); - wlr_render_pass_add_texture(data->render_pass, &(struct wlr_render_texture_options) { - .texture = texture, - .src_box = scene_buffer->src_box, - .dst_box = dst_box, - .transform = transform, - .clip = &render_region, - .alpha = &scene_buffer->opacity, - .filter_mode = scene_buffer->filter_mode, - .blend_mode = pixman_region32_not_empty(&opaque) ? - WLR_RENDER_BLEND_MODE_PREMULTIPLIED : WLR_RENDER_BLEND_MODE_NONE, - }); + struct fx_render_texture_options tex_options = { + .base = (struct wlr_render_texture_options){ + .texture = texture, + .src_box = scene_buffer->src_box, + .dst_box = dst_box, + .transform = transform, + .clip = &render_region, + .alpha = &scene_buffer->opacity, + .filter_mode = scene_buffer->filter_mode, + .blend_mode = pixman_region32_not_empty(&opaque) ? + WLR_RENDER_BLEND_MODE_PREMULTIPLIED : WLR_RENDER_BLEND_MODE_NONE, + }, + .corner_radius = scene_buffer->corner_radius, + }; + fx_render_pass_add_texture(data->render_pass, &tex_options); struct wlr_scene_output_sample_event sample_event = { .output = data->output, @@ -1557,10 +1359,6 @@ struct wlr_scene_output *wlr_scene_output_create(struct wlr_scene *scene, scene_output->scene = scene; wlr_addon_init(&scene_output->addon, &output->addons, scene, &output_addon_impl); - // Init FX Renderer - struct wlr_egl *egl = wlr_gles2_renderer_get_egl(output->renderer); - fx_renderer_init_addon(egl, output, &output->addons, scene); - wlr_damage_ring_init(&scene_output->damage_ring); wl_list_init(&scene_output->damage_highlight_regions); @@ -1618,10 +1416,6 @@ void wlr_scene_output_destroy(struct wlr_scene_output *scene_output) { highlight_region_destroy(damage); } - struct fx_renderer *fx_renderer = - fx_renderer_addon_find(&scene_output->output->addons, scene_output->scene); - wlr_addon_finish(&fx_renderer->addon); - wlr_addon_finish(&scene_output->addon); wlr_damage_ring_finish(&scene_output->damage_ring); wl_list_remove(&scene_output->link); @@ -1859,235 +1653,6 @@ static bool scene_entry_try_direct_scanout(struct render_list_entry *entry, return true; } -// bool wlr_scene_output_commit(struct wlr_scene_output *scene_output) { -// struct wlr_output *output = scene_output->output; -// enum wlr_scene_debug_damage_option debug_damage = -// scene_output->scene->debug_damage_option; -// -// // Find the fx_renderer addon -// struct fx_renderer *renderer = -// fx_renderer_addon_find(&output->addons, scene_output->scene); -// assert(renderer != NULL); -// -// struct render_list_constructor_data list_con = { -// .box = { .x = scene_output->x, .y = scene_output->y }, -// .render_list = &scene_output->render_list, -// .calculate_visibility = scene_output->scene->calculate_visibility, -// }; -// wlr_output_effective_resolution(output, -// &list_con.box.width, &list_con.box.height); -// -// list_con.render_list->size = 0; -// scene_nodes_in_box(&scene_output->scene->tree.node, &list_con.box, -// construct_render_list_iterator, &list_con); -// array_realloc(list_con.render_list, list_con.render_list->size); -// -// int list_len = list_con.render_list->size / sizeof(struct wlr_scene_node *); -// struct wlr_scene_node **list_data = list_con.render_list->data; -// -// // if there is only one thing to render let's see if that thing can be -// // directly scanned out -// bool scanout = false; -// if (list_len == 1) { -// struct wlr_scene_node *node = list_data[0]; -// scanout = scene_node_try_direct_scanout(node, scene_output, &list_con.box); -// } -// -// if (scene_output->prev_scanout != scanout) { -// scene_output->prev_scanout = scanout; -// wlr_log(WLR_DEBUG, "Direct scan-out %s", -// scanout ? "enabled" : "disabled"); -// // When exiting direct scan-out, damage everything -// wlr_damage_ring_add_whole(&scene_output->damage_ring); -// } -// -// if (scanout) { -// struct wlr_scene_node *node = list_data[0]; -// -// assert(node->type == WLR_SCENE_NODE_BUFFER); -// struct wlr_scene_buffer *buffer = wlr_scene_buffer_from_node(node); -// wl_signal_emit_mutable(&buffer->events.output_present, scene_output); -// return true; -// } -// -// if (debug_damage == WLR_SCENE_DEBUG_DAMAGE_RERENDER) { -// wlr_damage_ring_add_whole(&scene_output->damage_ring); -// } -// -// struct timespec now; -// if (debug_damage == WLR_SCENE_DEBUG_DAMAGE_HIGHLIGHT) { -// struct wl_list *regions = &scene_output->damage_highlight_regions; -// clock_gettime(CLOCK_MONOTONIC, &now); -// -// // add the current frame's damage if there is damage -// if (pixman_region32_not_empty(&scene_output->damage_ring.current)) { -// struct highlight_region *current_damage = -// calloc(1, sizeof(*current_damage)); -// if (current_damage) { -// pixman_region32_init(¤t_damage->region); -// pixman_region32_copy(¤t_damage->region, -// &scene_output->damage_ring.current); -// current_damage->when = now; -// wl_list_insert(regions, ¤t_damage->link); -// } -// } -// -// pixman_region32_t acc_damage; -// pixman_region32_init(&acc_damage); -// struct highlight_region *damage, *tmp_damage; -// wl_list_for_each_safe(damage, tmp_damage, regions, link) { -// // remove overlaping damage regions -// pixman_region32_subtract(&damage->region, &damage->region, &acc_damage); -// pixman_region32_union(&acc_damage, &acc_damage, &damage->region); -// -// // if this damage is too old or has nothing in it, get rid of it -// struct timespec time_diff; -// timespec_sub(&time_diff, &now, &damage->when); -// if (timespec_to_msec(&time_diff) >= HIGHLIGHT_DAMAGE_FADEOUT_TIME || -// !pixman_region32_not_empty(&damage->region)) { -// highlight_region_destroy(damage); -// } -// } -// -// wlr_damage_ring_add(&scene_output->damage_ring, &acc_damage); -// pixman_region32_fini(&acc_damage); -// } -// -// int buffer_age; -// if (!wlr_output_attach_render(output, &buffer_age)) { -// return false; -// } -// -// pixman_region32_t damage; -// pixman_region32_init(&damage); -// wlr_damage_ring_get_buffer_damage(&scene_output->damage_ring, -// buffer_age, &damage); -// if (!output->needs_frame && !pixman_region32_not_empty( -// &scene_output->damage_ring.current)) { -// pixman_region32_fini(&damage); -// wlr_output_rollback(output); -// return true; -// } -// -// fx_renderer_begin(renderer, output->width, output->height); -// -// pixman_region32_t background; -// pixman_region32_init(&background); -// pixman_region32_copy(&background, &damage); -// -// // Cull areas of the background that are occluded by opaque regions of -// // scene nodes above. Those scene nodes will just render atop having us -// // never see the background. -// if (scene_output->scene->calculate_visibility) { -// float output_scale = scene_output->output->scale; -// -// for (int i = list_len - 1; i >= 0; i--) { -// struct wlr_scene_node *node = list_data[i]; -// int x, y; -// wlr_scene_node_coords(node, &x, &y); -// -// // We must only cull opaque regions that are visible by the node. -// // The node's visibility will have the knowledge of a black rect -// // that may have been omitted from the render list via the black -// // rect optimization. In order to ensure we don't cull background -// // rendering in that black rect region, consider the node's visibility. -// pixman_region32_t opaque; -// pixman_region32_init(&opaque); -// scene_node_opaque_region(node, x, y, &opaque); -// pixman_region32_intersect(&opaque, &opaque, &node->visible); -// -// pixman_region32_translate(&opaque, -scene_output->x, -scene_output->y); -// wlr_region_scale(&opaque, &opaque, output_scale); -// pixman_region32_subtract(&background, &background, &opaque); -// pixman_region32_fini(&opaque); -// } -// -// if (floor(output_scale) != output_scale) { -// wlr_region_expand(&background, &background, 1); -// -// // reintersect with the damage because we never want to render -// // outside of the damage region -// pixman_region32_intersect(&background, &background, &damage); -// } -// } -// -// int nrects; -// pixman_box32_t *rects = pixman_region32_rectangles(&background, &nrects); -// for (int i = 0; i < nrects; ++i) { -// scissor_output(output, &rects[i]); -// fx_renderer_clear((float[4]){ 0.0, 0.0, 0.0, 1.0 }); -// } -// pixman_region32_fini(&background); -// -// for (int i = list_len - 1; i >= 0; i--) { -// struct wlr_scene_node *node = list_data[i]; -// scene_node_render(renderer, node, scene_output, &damage); -// } -// -// fx_renderer_scissor(NULL); -// -// if (debug_damage == WLR_SCENE_DEBUG_DAMAGE_HIGHLIGHT) { -// struct highlight_region *damage; -// wl_list_for_each(damage, &scene_output->damage_highlight_regions, link) { -// struct timespec time_diff; -// timespec_sub(&time_diff, &now, &damage->when); -// int64_t time_diff_ms = timespec_to_msec(&time_diff); -// float alpha = 1.0 - (double)time_diff_ms / HIGHLIGHT_DAMAGE_FADEOUT_TIME; -// -// int nrects; -// pixman_box32_t *rects = pixman_region32_rectangles(&damage->region, &nrects); -// for (int i = 0; i < nrects; ++i) { -// struct wlr_box box = { -// .x = rects[i].x1, -// .y = rects[i].y1, -// .width = rects[i].x2 - rects[i].x1, -// .height = rects[i].y2 - rects[i].y1, -// }; -// -// float color[4] = { alpha * .5, 0.0, 0.0, alpha * .5 }; -// fx_render_rect(renderer, &box, color, output->transform_matrix); -// } -// } -// } -// -// fx_renderer_scissor(NULL); -// fx_renderer_end(renderer); -// -// // Draw the software cursors -// wlr_renderer_begin(output->renderer, output->width, output->height); -// wlr_output_render_software_cursors(output, &damage); -// wlr_renderer_end(output->renderer); -// -// pixman_region32_fini(&damage); -// -// int tr_width, tr_height; -// wlr_output_transformed_resolution(output, &tr_width, &tr_height); -// -// enum wl_output_transform transform = -// wlr_output_transform_invert(output->transform); -// -// pixman_region32_t frame_damage; -// pixman_region32_init(&frame_damage); -// wlr_region_transform(&frame_damage, -// &scene_output->damage_ring.current, -// transform, tr_width, tr_height); -// wlr_output_set_damage(output, &frame_damage); -// pixman_region32_fini(&frame_damage); -// -// bool success = wlr_output_commit(output); -// -// if (success) { -// wlr_damage_ring_rotate(&scene_output->damage_ring); -// } -// -// if (debug_damage == WLR_SCENE_DEBUG_DAMAGE_HIGHLIGHT && -// !wl_list_empty(&scene_output->damage_highlight_regions)) { -// wlr_output_schedule_frame(scene_output->output); -// } -// -// return success; -// } - bool wlr_scene_output_commit(struct wlr_scene_output *scene_output, const struct wlr_scene_output_state_options *options) { if (!scene_output->output->needs_frame && !pixman_region32_not_empty( @@ -2273,10 +1838,12 @@ bool wlr_scene_output_build_state(struct wlr_scene_output *scene_output, timer->pre_render_duration = timespec_to_nsec(&duration); } - struct wlr_render_pass *render_pass = wlr_renderer_begin_buffer_pass(output->renderer, buffer, - &(struct wlr_buffer_pass_options){ - .timer = timer ? timer->render_timer : NULL, - }); + struct fx_gles_render_pass *render_pass = + fx_renderer_begin_buffer_pass(output->renderer, buffer, + &(struct wlr_buffer_pass_options){ + .timer = timer ? timer->render_timer : NULL, + } + ); if (render_pass == NULL) { wlr_buffer_unlock(buffer); return false; @@ -2324,7 +1891,7 @@ bool wlr_scene_output_build_state(struct wlr_scene_output *scene_output, } transform_output_damage(&background, &render_data); - wlr_render_pass_add_rect(render_pass, &(struct wlr_render_rect_options){ + wlr_render_pass_add_rect(&render_pass->base, &(struct wlr_render_rect_options){ .box = { .width = buffer->width, .height = buffer->height }, .color = { .r = 0, .g = 0, .b = 0, .a = 1 }, .clip = &background, @@ -2357,7 +1924,7 @@ bool wlr_scene_output_build_state(struct wlr_scene_output *scene_output, int64_t time_diff_ms = timespec_to_msec(&time_diff); float alpha = 1.0 - (double)time_diff_ms / HIGHLIGHT_DAMAGE_FADEOUT_TIME; - wlr_render_pass_add_rect(render_pass, &(struct wlr_render_rect_options){ + wlr_render_pass_add_rect(&render_pass->base, &(struct wlr_render_rect_options){ .box = { .width = buffer->width, .height = buffer->height }, .color = { .r = alpha * 0.5, .g = 0, .b = 0, .a = alpha * 0.5 }, .clip = &damage->region, @@ -2365,11 +1932,11 @@ bool wlr_scene_output_build_state(struct wlr_scene_output *scene_output, } } - wlr_output_add_software_cursors_to_render_pass(output, render_pass, &render_data.damage); + wlr_output_add_software_cursors_to_render_pass(output, &render_pass->base, &render_data.damage); pixman_region32_fini(&render_data.damage); - if (!wlr_render_pass_submit(render_pass)) { + if (!wlr_render_pass_submit(&render_pass->base)) { wlr_buffer_unlock(buffer); return false; } -- cgit v1.2.3