From 3a4bc446881193c5404ce5890f20bec8bb1fe505 Mon Sep 17 00:00:00 2001 From: Alexander Orzechowski Date: Mon, 27 Jan 2025 13:43:51 -0500 Subject: [PATCH 1/8] util/matrix: Rotate around (.5, .5) with wlr_matrix_transform Before we would rotate around 0,0 which means that the content needed to be scaled to -1, 1 to be transformed properly when we're usually working in the 0, 1 range. Switch it around. These matrices were precomputed by simply adding a -.5 -.5 translate and back again with old transform in between. --- render/gles2/pass.c | 2 -- util/matrix.c | 18 ++++++++---------- 2 files changed, 8 insertions(+), 12 deletions(-) diff --git a/render/gles2/pass.c b/render/gles2/pass.c index b10ac047d..f27a6a755 100644 --- a/render/gles2/pass.c +++ b/render/gles2/pass.c @@ -139,7 +139,6 @@ static void set_tex_matrix(GLint loc, enum wl_output_transform trans, 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 @@ -148,7 +147,6 @@ static void set_tex_matrix(GLint loc, enum wl_output_transform trans, } else { wlr_matrix_transform(tex_matrix, trans); } - wlr_matrix_translate(tex_matrix, -.5, -.5); glUniformMatrix3fv(loc, 1, GL_FALSE, tex_matrix); } diff --git a/util/matrix.c b/util/matrix.c index 86b434c64..7fdd1fae0 100644 --- a/util/matrix.c +++ b/util/matrix.c @@ -57,21 +57,21 @@ static const float transforms[][9] = { }, [WL_OUTPUT_TRANSFORM_90] = { 0.0f, 1.0f, 0.0f, - -1.0f, 0.0f, 0.0f, + -1.0f, 0.0f, 1.0f, 0.0f, 0.0f, 1.0f, }, [WL_OUTPUT_TRANSFORM_180] = { - -1.0f, 0.0f, 0.0f, - 0.0f, -1.0f, 0.0f, + -1.0f, 0.0f, 1.0f, + 0.0f, -1.0f, 1.0f, 0.0f, 0.0f, 1.0f, }, [WL_OUTPUT_TRANSFORM_270] = { - 0.0f, -1.0f, 0.0f, + 0.0f, -1.0f, 1.0f, 1.0f, 0.0f, 0.0f, 0.0f, 0.0f, 1.0f, }, [WL_OUTPUT_TRANSFORM_FLIPPED] = { - -1.0f, 0.0f, 0.0f, + -1.0f, 0.0f, 1.0f, 0.0f, 1.0f, 0.0f, 0.0f, 0.0f, 1.0f, }, @@ -82,12 +82,12 @@ static const float transforms[][9] = { }, [WL_OUTPUT_TRANSFORM_FLIPPED_180] = { 1.0f, 0.0f, 0.0f, - 0.0f, -1.0f, 0.0f, + 0.0f, -1.0f, 1.0f, 0.0f, 0.0f, 1.0f, }, [WL_OUTPUT_TRANSFORM_FLIPPED_270] = { - 0.0f, -1.0f, 0.0f, - -1.0f, 0.0f, 0.0f, + 0.0f, -1.0f, 1.0f, + -1.0f, 0.0f, 1.0f, 0.0f, 0.0f, 1.0f, }, }; @@ -132,9 +132,7 @@ void wlr_matrix_project_box(float mat[static 9], const struct wlr_box *box, wlr_matrix_scale(mat, width, height); if (transform != WL_OUTPUT_TRANSFORM_NORMAL) { - wlr_matrix_translate(mat, 0.5, 0.5); wlr_matrix_transform(mat, transform); - wlr_matrix_translate(mat, -0.5, -0.5); } wlr_matrix_multiply(mat, projection, mat); From b6fec1b101d0d02e05af66eca1cfab590f29080a Mon Sep 17 00:00:00 2001 From: Alexander Orzechowski Date: Sat, 22 Feb 2025 17:06:46 -0500 Subject: [PATCH 2/8] util/matrix: Simplify wlr_matrix_project_box Remove the transform and projection arguments. --- include/util/matrix.h | 10 +++++----- render/vulkan/pass.c | 11 +++++------ util/matrix.c | 9 +-------- 3 files changed, 11 insertions(+), 19 deletions(-) diff --git a/include/util/matrix.h b/include/util/matrix.h index 059a3bb1f..e6197cecc 100644 --- a/include/util/matrix.h +++ b/include/util/matrix.h @@ -24,11 +24,11 @@ void wlr_matrix_transform(float mat[static 9], enum wl_output_transform transform); /** Shortcut for the various matrix operations involved in projecting the - * specified wlr_box onto a given orthographic projection with a given - * rotation. The result is written to mat, which can be applied to each - * coordinate of the box to get a new coordinate from [-1,1]. */ -void wlr_matrix_project_box(float mat[static 9], const struct wlr_box *box, - enum wl_output_transform transform, const float projection[static 9]); + * specified wlr_box onto a given orthographic projection. The result is + * written to mat, which can be applied to each coordinate of the box to get a + * new coordinate from [-1,1]. + */ +void wlr_matrix_project_box(float mat[static 9], const struct wlr_box *box); /** * Writes a 2D orthographic projection matrix to mat of (width, height) with a diff --git a/render/vulkan/pass.c b/render/vulkan/pass.c index 032fcc7b2..92c771f06 100644 --- a/render/vulkan/pass.c +++ b/render/vulkan/pass.c @@ -618,9 +618,8 @@ static void render_pass_add_rect(struct wlr_render_pass *wlr_pass, switch (options->blend_mode) { case WLR_RENDER_BLEND_MODE_PREMULTIPLIED:; - float proj[9], matrix[9]; - wlr_matrix_identity(proj); - wlr_matrix_project_box(matrix, &box, WL_OUTPUT_TRANSFORM_NORMAL, proj); + float matrix[9]; + wlr_matrix_project_box(matrix, &box); wlr_matrix_multiply(matrix, pass->projection, matrix); struct wlr_vk_render_format_setup *setup = pass->srgb_pathway ? @@ -709,9 +708,9 @@ static void render_pass_add_texture(struct wlr_render_pass *wlr_pass, wlr_render_texture_options_get_dst_box(options, &dst_box); float alpha = wlr_render_texture_options_get_alpha(options); - float proj[9], matrix[9]; - wlr_matrix_identity(proj); - wlr_matrix_project_box(matrix, &dst_box, options->transform, proj); + float matrix[9]; + wlr_matrix_project_box(matrix, &dst_box); + wlr_matrix_transform(matrix, options->transform); wlr_matrix_multiply(matrix, pass->projection, matrix); struct wlr_vk_vert_pcr_data vert_pcr_data = { diff --git a/util/matrix.c b/util/matrix.c index 7fdd1fae0..d049b6ce3 100644 --- a/util/matrix.c +++ b/util/matrix.c @@ -119,8 +119,7 @@ void matrix_projection(float mat[static 9], int width, int height, mat[8] = 1.0f; } -void wlr_matrix_project_box(float mat[static 9], const struct wlr_box *box, - enum wl_output_transform transform, const float projection[static 9]) { +void wlr_matrix_project_box(float mat[static 9], const struct wlr_box *box) { int x = box->x; int y = box->y; int width = box->width; @@ -130,10 +129,4 @@ void wlr_matrix_project_box(float mat[static 9], const struct wlr_box *box, wlr_matrix_translate(mat, x, y); wlr_matrix_scale(mat, width, height); - - if (transform != WL_OUTPUT_TRANSFORM_NORMAL) { - wlr_matrix_transform(mat, transform); - } - - wlr_matrix_multiply(mat, projection, mat); } From 51c07a662a883352c888243ecada6a0c70b84919 Mon Sep 17 00:00:00 2001 From: Alexander Orzechowski Date: Mon, 27 Jan 2025 13:48:07 -0500 Subject: [PATCH 3/8] util/matrix: Simplify matrix_projection The transform argument is always passed WL_OUTPUT_TRANSFORM_FLIPPED_180 --- include/util/matrix.h | 6 ++---- render/gles2/pass.c | 3 +-- render/vulkan/pass.c | 4 +--- util/matrix.c | 14 +++----------- 4 files changed, 7 insertions(+), 20 deletions(-) diff --git a/include/util/matrix.h b/include/util/matrix.h index e6197cecc..5c9db1634 100644 --- a/include/util/matrix.h +++ b/include/util/matrix.h @@ -31,12 +31,10 @@ void wlr_matrix_transform(float mat[static 9], void wlr_matrix_project_box(float mat[static 9], const struct wlr_box *box); /** - * Writes a 2D orthographic projection matrix to mat of (width, height) with a - * specified wl_output_transform. + * Writes a 2D orthographic projection matrix to mat of (width, height). * * Equivalent to glOrtho(0, width, 0, height, 1, -1) with the transform applied. */ -void matrix_projection(float mat[static 9], int width, int height, - enum wl_output_transform transform); +void matrix_projection(float mat[static 9], int width, int height); #endif diff --git a/render/gles2/pass.c b/render/gles2/pass.c index f27a6a755..a1a593fd9 100644 --- a/render/gles2/pass.c +++ b/render/gles2/pass.c @@ -327,8 +327,7 @@ struct wlr_gles2_render_pass *begin_gles2_buffer_pass(struct wlr_gles2_buffer *b pass->signal_point = signal_point; } - matrix_projection(pass->projection_matrix, wlr_buffer->width, wlr_buffer->height, - WL_OUTPUT_TRANSFORM_FLIPPED_180); + matrix_projection(pass->projection_matrix, wlr_buffer->width, wlr_buffer->height); push_gles2_debug(renderer); glBindFramebuffer(GL_FRAMEBUFFER, fbo); diff --git a/render/vulkan/pass.c b/render/vulkan/pass.c index 92c771f06..104fbda3c 100644 --- a/render/vulkan/pass.c +++ b/render/vulkan/pass.c @@ -1131,9 +1131,7 @@ struct wlr_vk_render_pass *vulkan_begin_render_pass(struct wlr_vk_renderer *rend .maxDepth = 1, }); - // matrix_projection() assumes a GL coordinate system so we need - // to pass WL_OUTPUT_TRANSFORM_FLIPPED_180 to adjust it for vulkan. - matrix_projection(pass->projection, width, height, WL_OUTPUT_TRANSFORM_FLIPPED_180); + matrix_projection(pass->projection, width, height); wlr_buffer_lock(buffer->wlr_buffer); pass->render_buffer = buffer; diff --git a/util/matrix.c b/util/matrix.c index d049b6ce3..d111926df 100644 --- a/util/matrix.c +++ b/util/matrix.c @@ -97,19 +97,11 @@ void wlr_matrix_transform(float mat[static 9], wlr_matrix_multiply(mat, mat, transforms[transform]); } -void matrix_projection(float mat[static 9], int width, int height, - enum wl_output_transform transform) { +void matrix_projection(float mat[static 9], int width, int height) { memset(mat, 0, sizeof(*mat) * 9); - const float *t = transforms[transform]; - float x = 2.0f / width; - float y = 2.0f / height; - - // Rotation + reflection - mat[0] = x * t[0]; - mat[1] = x * t[1]; - mat[3] = y * -t[3]; - mat[4] = y * -t[4]; + mat[0] = 2.0f / width; + mat[4] = 2.0f / height; // Translation mat[2] = -copysign(1.0f, mat[0] + mat[1]); From 26d24d2229c15762e7f6814ecec6a38c2d8b8742 Mon Sep 17 00:00:00 2001 From: Alexander Orzechowski Date: Mon, 27 Jan 2025 13:53:58 -0500 Subject: [PATCH 4/8] render/gles2: Use wlr_matrix_project_box more --- render/gles2/pass.c | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/render/gles2/pass.c b/render/gles2/pass.c index a1a593fd9..f8ece60df 100644 --- a/render/gles2/pass.c +++ b/render/gles2/pass.c @@ -126,9 +126,7 @@ static void render(const struct wlr_box *box, const pixman_region32_t *clip, GLi 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_project_box(gl_matrix, box); wlr_matrix_multiply(gl_matrix, proj, gl_matrix); glUniformMatrix3fv(loc, 1, GL_FALSE, gl_matrix); } From 088b7c98c56ef5b559039aff8cafea468cce5e1b Mon Sep 17 00:00:00 2001 From: Alexander Orzechowski Date: Mon, 27 Jan 2025 14:26:47 -0500 Subject: [PATCH 5/8] util/matrix: Introduce wlr_matrix_project_fbox Same as wlr_matrix_project_box but takes an fbox And use it with gles2 --- include/util/matrix.h | 3 +++ render/gles2/pass.c | 4 +--- util/matrix.c | 23 ++++++++++++++--------- 3 files changed, 18 insertions(+), 12 deletions(-) diff --git a/include/util/matrix.h b/include/util/matrix.h index 5c9db1634..7ad326823 100644 --- a/include/util/matrix.h +++ b/include/util/matrix.h @@ -4,6 +4,7 @@ #include struct wlr_box; +struct wlr_fbox; /** Writes the identity matrix into mat */ void wlr_matrix_identity(float mat[static 9]); @@ -30,6 +31,8 @@ void wlr_matrix_transform(float mat[static 9], */ void wlr_matrix_project_box(float mat[static 9], const struct wlr_box *box); +void wlr_matrix_project_fbox(float mat[static 9], const struct wlr_fbox *box); + /** * Writes a 2D orthographic projection matrix to mat of (width, height). * diff --git a/render/gles2/pass.c b/render/gles2/pass.c index f8ece60df..0b5c1ab0d 100644 --- a/render/gles2/pass.c +++ b/render/gles2/pass.c @@ -134,9 +134,7 @@ static void set_proj_matrix(GLint loc, float proj[9], const struct wlr_box *box) 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_project_fbox(tex_matrix, box); // since textures have a different origin point we have to transform // differently if we are rotating diff --git a/util/matrix.c b/util/matrix.c index d111926df..d93eacdd8 100644 --- a/util/matrix.c +++ b/util/matrix.c @@ -111,14 +111,19 @@ void matrix_projection(float mat[static 9], int width, int height) { mat[8] = 1.0f; } -void wlr_matrix_project_box(float mat[static 9], const struct wlr_box *box) { - int x = box->x; - int y = box->y; - int width = box->width; - int height = box->height; - +void wlr_matrix_project_fbox(float mat[static 9], const struct wlr_fbox *box) { wlr_matrix_identity(mat); - wlr_matrix_translate(mat, x, y); - - wlr_matrix_scale(mat, width, height); + wlr_matrix_translate(mat, box->x, box->y); + wlr_matrix_scale(mat, box->width, box->height); +} + +void wlr_matrix_project_box(float mat[static 9], const struct wlr_box *box) { + struct wlr_fbox fbox = { + .x = box->x, + .y = box->y, + .width = box->width, + .height = box->height, + }; + + wlr_matrix_project_fbox(mat, &fbox); } From 99b6084fcdccfeca5e5f0f808d5d123400e5693e Mon Sep 17 00:00:00 2001 From: Alexander Orzechowski Date: Mon, 27 Jan 2025 14:01:31 -0500 Subject: [PATCH 6/8] util/matrix: Inline wlr_matrix_{identity, scale, translate} --- include/util/matrix.h | 9 --------- util/matrix.c | 41 +++++++++++------------------------------ 2 files changed, 11 insertions(+), 39 deletions(-) diff --git a/include/util/matrix.h b/include/util/matrix.h index 7ad326823..9b219ff61 100644 --- a/include/util/matrix.h +++ b/include/util/matrix.h @@ -6,19 +6,10 @@ struct wlr_box; struct wlr_fbox; -/** Writes the identity matrix into mat */ -void wlr_matrix_identity(float mat[static 9]); - /** mat ← a × b */ void wlr_matrix_multiply(float mat[static 9], const float a[static 9], const float b[static 9]); -/** Writes a 2D translation matrix to mat of magnitude (x, y) */ -void wlr_matrix_translate(float mat[static 9], float x, float y); - -/** Writes a 2D scale matrix to mat of magnitude (x, y) */ -void wlr_matrix_scale(float mat[static 9], float x, float y); - /** Writes a transformation matrix which applies the specified * wl_output_transform to mat */ void wlr_matrix_transform(float mat[static 9], diff --git a/util/matrix.c b/util/matrix.c index d93eacdd8..20612182a 100644 --- a/util/matrix.c +++ b/util/matrix.c @@ -3,15 +3,6 @@ #include #include "util/matrix.h" -void wlr_matrix_identity(float mat[static 9]) { - static const float identity[9] = { - 1.0f, 0.0f, 0.0f, - 0.0f, 1.0f, 0.0f, - 0.0f, 0.0f, 1.0f, - }; - memcpy(mat, identity, sizeof(identity)); -} - void wlr_matrix_multiply(float mat[static 9], const float a[static 9], const float b[static 9]) { float product[9]; @@ -31,24 +22,6 @@ void wlr_matrix_multiply(float mat[static 9], const float a[static 9], memcpy(mat, product, sizeof(product)); } -void wlr_matrix_translate(float mat[static 9], float x, float y) { - float translate[9] = { - 1.0f, 0.0f, x, - 0.0f, 1.0f, y, - 0.0f, 0.0f, 1.0f, - }; - wlr_matrix_multiply(mat, mat, translate); -} - -void wlr_matrix_scale(float mat[static 9], float x, float y) { - float scale[9] = { - x, 0.0f, 0.0f, - 0.0f, y, 0.0f, - 0.0f, 0.0f, 1.0f, - }; - wlr_matrix_multiply(mat, mat, scale); -} - static const float transforms[][9] = { [WL_OUTPUT_TRANSFORM_NORMAL] = { 1.0f, 0.0f, 0.0f, @@ -112,9 +85,17 @@ void matrix_projection(float mat[static 9], int width, int height) { } void wlr_matrix_project_fbox(float mat[static 9], const struct wlr_fbox *box) { - wlr_matrix_identity(mat); - wlr_matrix_translate(mat, box->x, box->y); - wlr_matrix_scale(mat, box->width, box->height); + mat[0] = box->width; + mat[1] = 0.0f; + mat[2] = box->x; + + mat[3] = 0.0f; + mat[4] = box->height; + mat[5] = box->y; + + mat[6] = 0.0f; + mat[7] = 0.0f; + mat[8] = 1.0f; } void wlr_matrix_project_box(float mat[static 9], const struct wlr_box *box) { From 7775f55e3abe2b20369f1c77abc4df832ceea906 Mon Sep 17 00:00:00 2001 From: Alexander Orzechowski Date: Mon, 27 Jan 2025 14:30:13 -0500 Subject: [PATCH 7/8] util/matrix: Rework matrix_projection to compute transform on-demand --- include/render/gles2.h | 1 - include/render/vulkan.h | 1 - render/gles2/pass.c | 10 ++++------ render/vulkan/pass.c | 10 ++++++---- util/matrix.c | 19 +++++++++---------- 5 files changed, 19 insertions(+), 22 deletions(-) diff --git a/include/render/gles2.h b/include/render/gles2.h index 0f24ae38e..f36b173e2 100644 --- a/include/render/gles2.h +++ b/include/render/gles2.h @@ -135,7 +135,6 @@ struct wlr_gles2_texture { struct wlr_gles2_render_pass { struct wlr_render_pass base; struct wlr_gles2_buffer *buffer; - float projection_matrix[9]; struct wlr_egl_context prev_ctx; struct wlr_gles2_render_timer *timer; struct wlr_drm_syncobj_timeline *signal_timeline; diff --git a/include/render/vulkan.h b/include/render/vulkan.h index abeb11cc5..83a99a8dd 100644 --- a/include/render/vulkan.h +++ b/include/render/vulkan.h @@ -384,7 +384,6 @@ struct wlr_vk_render_pass { struct wlr_vk_command_buffer *command_buffer; struct rect_union updated_region; VkPipeline bound_pipeline; - float projection[9]; bool failed; bool srgb_pathway; // if false, rendering via intermediate blending buffer struct wlr_color_transform *color_transform; diff --git a/render/gles2/pass.c b/render/gles2/pass.c index 0b5c1ab0d..7be09b1a6 100644 --- a/render/gles2/pass.c +++ b/render/gles2/pass.c @@ -124,10 +124,10 @@ static void render(const struct wlr_box *box, const pixman_region32_t *clip, GLi pixman_region32_fini(®ion); } -static void set_proj_matrix(GLint loc, float proj[9], const struct wlr_box *box) { +static void set_proj_matrix(GLint loc, struct wlr_gles2_buffer *buffer, const struct wlr_box *box) { float gl_matrix[9]; wlr_matrix_project_box(gl_matrix, box); - wlr_matrix_multiply(gl_matrix, proj, gl_matrix); + matrix_projection(gl_matrix, buffer->buffer->width, buffer->buffer->height); glUniformMatrix3fv(loc, 1, GL_FALSE, gl_matrix); } @@ -238,7 +238,7 @@ static void render_pass_add_texture(struct wlr_render_pass *wlr_pass, glUniform1i(shader->tex, 0); glUniform1f(shader->alpha, alpha); - set_proj_matrix(shader->proj, pass->projection_matrix, &dst_box); + set_proj_matrix(shader->proj, pass->buffer, &dst_box); set_tex_matrix(shader->tex_proj, options->transform, &src_fbox); render(&dst_box, options->clip, shader->pos_attrib); @@ -261,7 +261,7 @@ static void render_pass_add_rect(struct wlr_render_pass *wlr_pass, glUseProgram(renderer->shaders.quad.program); - set_proj_matrix(renderer->shaders.quad.proj, pass->projection_matrix, &box); + set_proj_matrix(renderer->shaders.quad.proj, pass->buffer, &box); glUniform4f(renderer->shaders.quad.color, color->r, color->g, color->b, color->a); render(&box, options->clip, renderer->shaders.quad.pos_attrib); @@ -323,8 +323,6 @@ struct wlr_gles2_render_pass *begin_gles2_buffer_pass(struct wlr_gles2_buffer *b pass->signal_point = signal_point; } - matrix_projection(pass->projection_matrix, wlr_buffer->width, wlr_buffer->height); - push_gles2_debug(renderer); glBindFramebuffer(GL_FRAMEBUFFER, fbo); diff --git a/render/vulkan/pass.c b/render/vulkan/pass.c index 104fbda3c..0e50e25cd 100644 --- a/render/vulkan/pass.c +++ b/render/vulkan/pass.c @@ -620,7 +620,9 @@ static void render_pass_add_rect(struct wlr_render_pass *wlr_pass, case WLR_RENDER_BLEND_MODE_PREMULTIPLIED:; float matrix[9]; wlr_matrix_project_box(matrix, &box); - wlr_matrix_multiply(matrix, pass->projection, matrix); + matrix_projection(matrix, + pass->render_buffer->wlr_buffer->width, + pass->render_buffer->wlr_buffer->height); struct wlr_vk_render_format_setup *setup = pass->srgb_pathway ? pass->render_buffer->srgb.render_setup : @@ -711,7 +713,9 @@ static void render_pass_add_texture(struct wlr_render_pass *wlr_pass, float matrix[9]; wlr_matrix_project_box(matrix, &dst_box); wlr_matrix_transform(matrix, options->transform); - wlr_matrix_multiply(matrix, pass->projection, matrix); + matrix_projection(matrix, + pass->render_buffer->wlr_buffer->width, + pass->render_buffer->wlr_buffer->height); struct wlr_vk_vert_pcr_data vert_pcr_data = { .uv_off = { @@ -1131,8 +1135,6 @@ struct wlr_vk_render_pass *vulkan_begin_render_pass(struct wlr_vk_renderer *rend .maxDepth = 1, }); - matrix_projection(pass->projection, width, height); - wlr_buffer_lock(buffer->wlr_buffer); pass->render_buffer = buffer; pass->command_buffer = cb; diff --git a/util/matrix.c b/util/matrix.c index 20612182a..ce4f29fa1 100644 --- a/util/matrix.c +++ b/util/matrix.c @@ -71,17 +71,16 @@ void wlr_matrix_transform(float mat[static 9], } void matrix_projection(float mat[static 9], int width, int height) { - memset(mat, 0, sizeof(*mat) * 9); + struct wlr_fbox fbox = { + .x = -1.0f, + .y = -1.0f, + .width = 2.0f / width, + .height = 2.0f / height, + }; - mat[0] = 2.0f / width; - mat[4] = 2.0f / height; - - // Translation - mat[2] = -copysign(1.0f, mat[0] + mat[1]); - mat[5] = -copysign(1.0f, mat[3] + mat[4]); - - // Identity - mat[8] = 1.0f; + float trans[9]; + wlr_matrix_project_fbox(trans, &fbox); + wlr_matrix_multiply(mat, trans, mat); } void wlr_matrix_project_fbox(float mat[static 9], const struct wlr_fbox *box) { From 97b01d9b9b622bc1ea719225f20553b30e4a36be Mon Sep 17 00:00:00 2001 From: Alexander Orzechowski Date: Mon, 27 Jan 2025 14:26:40 -0500 Subject: [PATCH 8/8] util/matrix: Drop wlr_ prefix --- include/util/matrix.h | 9 ++++----- render/gles2/pass.c | 8 ++++---- render/vulkan/pass.c | 6 +++--- util/matrix.c | 16 ++++++++-------- 4 files changed, 19 insertions(+), 20 deletions(-) diff --git a/include/util/matrix.h b/include/util/matrix.h index 9b219ff61..ab1e05f71 100644 --- a/include/util/matrix.h +++ b/include/util/matrix.h @@ -7,22 +7,21 @@ struct wlr_box; struct wlr_fbox; /** mat ← a × b */ -void wlr_matrix_multiply(float mat[static 9], const float a[static 9], +void matrix_multiply(float mat[static 9], const float a[static 9], const float b[static 9]); /** Writes a transformation matrix which applies the specified * wl_output_transform to mat */ -void wlr_matrix_transform(float mat[static 9], - enum wl_output_transform transform); +void matrix_transform(float mat[static 9], enum wl_output_transform transform); /** Shortcut for the various matrix operations involved in projecting the * specified wlr_box onto a given orthographic projection. The result is * written to mat, which can be applied to each coordinate of the box to get a * new coordinate from [-1,1]. */ -void wlr_matrix_project_box(float mat[static 9], const struct wlr_box *box); +void matrix_project_box(float mat[static 9], const struct wlr_box *box); -void wlr_matrix_project_fbox(float mat[static 9], const struct wlr_fbox *box); +void matrix_project_fbox(float mat[static 9], const struct wlr_fbox *box); /** * Writes a 2D orthographic projection matrix to mat of (width, height). diff --git a/render/gles2/pass.c b/render/gles2/pass.c index 7be09b1a6..d634ebfff 100644 --- a/render/gles2/pass.c +++ b/render/gles2/pass.c @@ -126,7 +126,7 @@ static void render(const struct wlr_box *box, const pixman_region32_t *clip, GLi static void set_proj_matrix(GLint loc, struct wlr_gles2_buffer *buffer, const struct wlr_box *box) { float gl_matrix[9]; - wlr_matrix_project_box(gl_matrix, box); + matrix_project_box(gl_matrix, box); matrix_projection(gl_matrix, buffer->buffer->width, buffer->buffer->height); glUniformMatrix3fv(loc, 1, GL_FALSE, gl_matrix); } @@ -134,14 +134,14 @@ static void set_proj_matrix(GLint loc, struct wlr_gles2_buffer *buffer, const st static void set_tex_matrix(GLint loc, enum wl_output_transform trans, const struct wlr_fbox *box) { float tex_matrix[9]; - wlr_matrix_project_fbox(tex_matrix, box); + matrix_project_fbox(tex_matrix, box); // 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)); + matrix_transform(tex_matrix, wlr_output_transform_invert(trans)); } else { - wlr_matrix_transform(tex_matrix, trans); + matrix_transform(tex_matrix, trans); } glUniformMatrix3fv(loc, 1, GL_FALSE, tex_matrix); diff --git a/render/vulkan/pass.c b/render/vulkan/pass.c index 0e50e25cd..c263c604a 100644 --- a/render/vulkan/pass.c +++ b/render/vulkan/pass.c @@ -619,7 +619,7 @@ static void render_pass_add_rect(struct wlr_render_pass *wlr_pass, switch (options->blend_mode) { case WLR_RENDER_BLEND_MODE_PREMULTIPLIED:; float matrix[9]; - wlr_matrix_project_box(matrix, &box); + matrix_project_box(matrix, &box); matrix_projection(matrix, pass->render_buffer->wlr_buffer->width, pass->render_buffer->wlr_buffer->height); @@ -711,8 +711,8 @@ static void render_pass_add_texture(struct wlr_render_pass *wlr_pass, float alpha = wlr_render_texture_options_get_alpha(options); float matrix[9]; - wlr_matrix_project_box(matrix, &dst_box); - wlr_matrix_transform(matrix, options->transform); + matrix_project_box(matrix, &dst_box); + matrix_transform(matrix, options->transform); matrix_projection(matrix, pass->render_buffer->wlr_buffer->width, pass->render_buffer->wlr_buffer->height); diff --git a/util/matrix.c b/util/matrix.c index ce4f29fa1..3a7b95c6b 100644 --- a/util/matrix.c +++ b/util/matrix.c @@ -3,7 +3,7 @@ #include #include "util/matrix.h" -void wlr_matrix_multiply(float mat[static 9], const float a[static 9], +void matrix_multiply(float mat[static 9], const float a[static 9], const float b[static 9]) { float product[9]; @@ -65,9 +65,9 @@ static const float transforms[][9] = { }, }; -void wlr_matrix_transform(float mat[static 9], +void matrix_transform(float mat[static 9], enum wl_output_transform transform) { - wlr_matrix_multiply(mat, mat, transforms[transform]); + matrix_multiply(mat, mat, transforms[transform]); } void matrix_projection(float mat[static 9], int width, int height) { @@ -79,11 +79,11 @@ void matrix_projection(float mat[static 9], int width, int height) { }; float trans[9]; - wlr_matrix_project_fbox(trans, &fbox); - wlr_matrix_multiply(mat, trans, mat); + matrix_project_fbox(trans, &fbox); + matrix_multiply(mat, trans, mat); } -void wlr_matrix_project_fbox(float mat[static 9], const struct wlr_fbox *box) { +void matrix_project_fbox(float mat[static 9], const struct wlr_fbox *box) { mat[0] = box->width; mat[1] = 0.0f; mat[2] = box->x; @@ -97,7 +97,7 @@ void wlr_matrix_project_fbox(float mat[static 9], const struct wlr_fbox *box) { mat[8] = 1.0f; } -void wlr_matrix_project_box(float mat[static 9], const struct wlr_box *box) { +void matrix_project_box(float mat[static 9], const struct wlr_box *box) { struct wlr_fbox fbox = { .x = box->x, .y = box->y, @@ -105,5 +105,5 @@ void wlr_matrix_project_box(float mat[static 9], const struct wlr_box *box) { .height = box->height, }; - wlr_matrix_project_fbox(mat, &fbox); + matrix_project_fbox(mat, &fbox); }