From 5a49f2ae142fdec8a23572d3b92b8aaddb99b38a Mon Sep 17 00:00:00 2001 From: Loukas Agorgianitis Date: Mon, 8 Dec 2025 11:47:15 +0200 Subject: [PATCH 1/2] render/pass: accept fractional destination coordinates Signed-off-by: Loukas Agorgianitis --- examples/output-layout.c | 7 ++++++- examples/tablet.c | 2 +- include/util/matrix.h | 3 ++- include/wlr/render/interface.h | 4 ++-- include/wlr/render/pass.h | 4 ++-- render/gles2/pass.c | 24 ++++++++++++------------ render/pass.c | 10 +++++----- render/pixman/pass.c | 14 +++++++------- render/vulkan/pass.c | 20 ++++++++++++++++---- types/output/cursor.c | 13 ++++++++++--- types/scene/wlr_scene.c | 13 ++++++++++--- types/wlr_screencopy_v1.c | 2 +- util/matrix.c | 10 +++++----- 13 files changed, 79 insertions(+), 47 deletions(-) diff --git a/examples/output-layout.c b/examples/output-layout.c index c481ddf80..715f7af8d 100644 --- a/examples/output-layout.c +++ b/examples/output-layout.c @@ -136,7 +136,12 @@ static void output_frame_notify(struct wl_listener *listener, void *data) { wlr_render_pass_add_texture(pass, &(struct wlr_render_texture_options){ .texture = sample->cat_texture, - .dst_box = box, + .dst_box = (struct wlr_fbox){ + .x = box.x, + .y = box.y, + .width = box.width, + .height = box.height, + }, }); } diff --git a/examples/tablet.c b/examples/tablet.c index 78666fff7..b48eaa157 100644 --- a/examples/tablet.c +++ b/examples/tablet.c @@ -123,7 +123,7 @@ static void output_frame_notify(struct wl_listener *listener, void *data) { }); if (sample->proximity) { - struct wlr_box box = { + struct wlr_fbox box = { .x = (sample->x * pad_width) - 8 * (sample->pressure + 1) + left, .y = (sample->y * pad_height) - 8 * (sample->pressure + 1) + top, .width = 16 * (sample->pressure + 1), diff --git a/include/util/matrix.h b/include/util/matrix.h index c45c99a06..ff1905608 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]); @@ -27,7 +28,7 @@ void wlr_matrix_transform(float mat[static 9], * 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, +void wlr_matrix_project_box(float mat[static 9], const struct wlr_fbox *box, enum wl_output_transform transform, const float projection[static 9]); /** diff --git a/include/wlr/render/interface.h b/include/wlr/render/interface.h index 89f6de970..0b2452197 100644 --- a/include/wlr/render/interface.h +++ b/include/wlr/render/interface.h @@ -76,10 +76,10 @@ struct wlr_render_timer_impl { void wlr_render_texture_options_get_src_box(const struct wlr_render_texture_options *options, struct wlr_fbox *box); void wlr_render_texture_options_get_dst_box(const struct wlr_render_texture_options *options, - struct wlr_box *box); + struct wlr_fbox *box); float wlr_render_texture_options_get_alpha(const struct wlr_render_texture_options *options); void wlr_render_rect_options_get_box(const struct wlr_render_rect_options *options, - const struct wlr_buffer *buffer, struct wlr_box *box); + const struct wlr_buffer *buffer, struct wlr_fbox *box); void wlr_texture_read_pixels_options_get_src_box( const struct wlr_texture_read_pixels_options *options, diff --git a/include/wlr/render/pass.h b/include/wlr/render/pass.h index 1ef888f46..23bea6eeb 100644 --- a/include/wlr/render/pass.h +++ b/include/wlr/render/pass.h @@ -90,7 +90,7 @@ struct wlr_render_texture_options { /* Source coordinates, leave empty to render the whole texture */ struct wlr_fbox src_box; /* Destination coordinates, width/height default to the texture size */ - struct wlr_box dst_box; + struct wlr_fbox dst_box; /* Opacity between 0 (transparent) and 1 (opaque), leave NULL for opaque */ const float *alpha; /* Clip region, leave NULL to disable clipping */ @@ -140,7 +140,7 @@ struct wlr_render_color { struct wlr_render_rect_options { /* Rectangle coordinates */ - struct wlr_box box; + struct wlr_fbox box; /* Source color */ struct wlr_render_color color; /* Clip region, leave NULL to disable clipping */ diff --git a/render/gles2/pass.c b/render/gles2/pass.c index b10ac047d..d79f404c6 100644 --- a/render/gles2/pass.c +++ b/render/gles2/pass.c @@ -75,9 +75,9 @@ out: return ok; } -static void render(const struct wlr_box *box, const pixman_region32_t *clip, GLint attrib) { +static void render(const struct wlr_fbox *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); + pixman_region32_init_rect(®ion, round(box->x), round(box->y), round(box->width), round(box->height)); if (clip) { pixman_region32_intersect(®ion, ®ion, clip); @@ -124,7 +124,7 @@ 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, float proj[9], const struct wlr_fbox *box) { float gl_matrix[9]; wlr_matrix_identity(gl_matrix); wlr_matrix_translate(gl_matrix, box->x, box->y); @@ -190,16 +190,16 @@ static void render_pass_add_texture(struct wlr_render_pass *wlr_pass, abort(); } - struct wlr_box dst_box; - struct wlr_fbox src_fbox; - wlr_render_texture_options_get_src_box(options, &src_fbox); + struct wlr_fbox dst_box; + struct wlr_fbox src_box; + wlr_render_texture_options_get_src_box(options, &src_box); 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; + src_box.x /= options->texture->width; + src_box.y /= options->texture->height; + src_box.width /= options->texture->width; + src_box.height /= options->texture->height; push_gles2_debug(renderer); @@ -245,7 +245,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_tex_matrix(shader->tex_proj, options->transform, &src_fbox); + set_tex_matrix(shader->tex_proj, options->transform, &src_box); render(&dst_box, options->clip, shader->pos_attrib); @@ -259,7 +259,7 @@ static void render_pass_add_rect(struct wlr_render_pass *wlr_pass, struct wlr_gles2_renderer *renderer = pass->buffer->renderer; const struct wlr_render_color *color = &options->color; - struct wlr_box box; + struct wlr_fbox box; wlr_render_rect_options_get_box(options, pass->buffer->buffer, &box); push_gles2_debug(renderer); diff --git a/render/pass.c b/render/pass.c index 23bdf96dd..0c28f9001 100644 --- a/render/pass.c +++ b/render/pass.c @@ -46,9 +46,9 @@ void wlr_render_texture_options_get_src_box(const struct wlr_render_texture_opti } void wlr_render_texture_options_get_dst_box(const struct wlr_render_texture_options *options, - struct wlr_box *box) { + struct wlr_fbox *box) { *box = options->dst_box; - if (wlr_box_empty(box)) { + if (wlr_fbox_empty(box)) { box->width = options->texture->width; box->height = options->texture->height; } @@ -62,9 +62,9 @@ float wlr_render_texture_options_get_alpha(const struct wlr_render_texture_optio } void wlr_render_rect_options_get_box(const struct wlr_render_rect_options *options, - const struct wlr_buffer *buffer, struct wlr_box *box) { - if (wlr_box_empty(&options->box)) { - *box = (struct wlr_box){ + const struct wlr_buffer *buffer, struct wlr_fbox *box) { + if (wlr_fbox_empty(&options->box)) { + *box = (struct wlr_fbox){ .width = buffer->width, .height = buffer->height, }; diff --git a/render/pixman/pass.c b/render/pixman/pass.c index 4ae742cab..e2109b9eb 100644 --- a/render/pixman/pass.c +++ b/render/pixman/pass.c @@ -59,7 +59,7 @@ static void render_pass_add_texture(struct wlr_render_pass *wlr_pass, .height = roundf(src_fbox.height), }; - struct wlr_box dst_box; + struct wlr_fbox dst_box; wlr_render_texture_options_get_dst_box(options, &dst_box); pixman_image_t *mask = NULL; @@ -125,8 +125,8 @@ static void render_pass_add_texture(struct wlr_render_pass *wlr_pass, // it depends on the whether the rotation swapped width and height, which is why // we use src_box_transformed instead of src_box. pixman_transform_scale(&transform, NULL, - pixman_double_to_fixed(src_box_transformed.width / (double)dst_box.width), - pixman_double_to_fixed(src_box_transformed.height / (double)dst_box.height)); + pixman_double_to_fixed(src_box_transformed.width / dst_box.width), + pixman_double_to_fixed(src_box_transformed.height / dst_box.height)); // pixman rotates about the origin which again leaves everything outside of the // viewport. Translate the result so that its new top-left corner is back at the @@ -174,8 +174,8 @@ static void render_pass_add_texture(struct wlr_render_pass *wlr_pass, pixman_image_composite32(op, texture->image, mask, buffer->image, 0, 0, // source x,y 0, 0, // mask x,y - dst_box.x, dst_box.y, // dest x,y - dst_box.width, dst_box.height // composite width,height + round(dst_box.x), round(dst_box.y), // dest x,y + round(dst_box.width), round(dst_box.height) // composite width,height ); pixman_image_set_transform(texture->image, NULL); @@ -183,7 +183,7 @@ static void render_pass_add_texture(struct wlr_render_pass *wlr_pass, // No transforms or crop needed, just a straight blit from the source pixman_image_set_transform(texture->image, NULL); pixman_image_composite32(op, texture->image, mask, buffer->image, - src_box.x, src_box.y, 0, 0, dst_box.x, dst_box.y, + src_box.x, src_box.y, 0, 0, round(dst_box.x), round(dst_box.y), src_box.width, src_box.height); } @@ -202,7 +202,7 @@ static void render_pass_add_rect(struct wlr_render_pass *wlr_pass, const struct wlr_render_rect_options *options) { struct wlr_pixman_render_pass *pass = get_render_pass(wlr_pass); struct wlr_pixman_buffer *buffer = pass->buffer; - struct wlr_box box; + struct wlr_fbox box; wlr_render_rect_options_get_box(options, pass->buffer->buffer, &box); pixman_op_t op = get_pixman_blending(options->color.a == 1 ? diff --git a/render/vulkan/pass.c b/render/vulkan/pass.c index db7a659d6..4fd06aab2 100644 --- a/render/vulkan/pass.c +++ b/render/vulkan/pass.c @@ -686,14 +686,20 @@ static void render_pass_add_rect(struct wlr_render_pass *wlr_pass, .width = clip_rects[i].x2 - clip_rects[i].x1, .height = clip_rects[i].y2 - clip_rects[i].y1, }; + struct wlr_box rect_box = { + .x = round(options->box.x), + .y = round(options->box.y), + .width = round(options->box.width), + .height = round(options->box.height), + }; struct wlr_box intersection; - if (!wlr_box_intersection(&intersection, &options->box, &clip_box)) { + if (!wlr_box_intersection(&intersection, &rect_box, &clip_box)) { continue; } render_pass_mark_box_updated(pass, &intersection); } - struct wlr_box box; + struct wlr_fbox box; wlr_render_rect_options_get_box(options, pass->render_buffer->wlr_buffer, &box); switch (options->blend_mode) { @@ -782,7 +788,7 @@ static void render_pass_add_texture(struct wlr_render_pass *wlr_pass, struct wlr_fbox src_box; wlr_render_texture_options_get_src_box(options, &src_box); - struct wlr_box dst_box; + struct wlr_fbox dst_box; wlr_render_texture_options_get_dst_box(options, &dst_box); float alpha = wlr_render_texture_options_get_alpha(options); @@ -926,8 +932,14 @@ static void render_pass_add_texture(struct wlr_render_pass *wlr_pass, .width = clip_rects[i].x2 - clip_rects[i].x1, .height = clip_rects[i].y2 - clip_rects[i].y1, }; + struct wlr_box texture_box = { + .x = round(dst_box.x), + .y = round(dst_box.y), + .width = round(dst_box.width), + .height = round(dst_box.height), + }; struct wlr_box intersection; - if (!wlr_box_intersection(&intersection, &dst_box, &clip_box)) { + if (!wlr_box_intersection(&intersection, &texture_box, &clip_box)) { continue; } render_pass_mark_box_updated(pass, &intersection); diff --git a/types/output/cursor.c b/types/output/cursor.c index 8a7f9e92a..679b8e78c 100644 --- a/types/output/cursor.c +++ b/types/output/cursor.c @@ -122,10 +122,17 @@ void wlr_output_add_software_cursors_to_render_pass(struct wlr_output *output, continue; } + struct wlr_fbox dst_box = (struct wlr_fbox){ + .x = box.x, + .y = box.y, + .width = box.width, + .height = box.height, + }; + wlr_render_pass_add_texture(render_pass, &(struct wlr_render_texture_options) { .texture = texture, .src_box = cursor->src_box, - .dst_box = box, + .dst_box = dst_box, .clip = &cursor_damage, .transform = output->transform, }); @@ -249,11 +256,11 @@ static struct wlr_buffer *render_cursor_buffer(struct wlr_output_cursor *cursor) return NULL; } - struct wlr_box dst_box = { + struct wlr_fbox dst_box = { .width = cursor->width, .height = cursor->height, }; - wlr_box_transform(&dst_box, &dst_box, wlr_output_transform_invert(output->transform), + wlr_fbox_transform(&dst_box, &dst_box, wlr_output_transform_invert(output->transform), buffer->width, buffer->height); struct wlr_buffer_pass_options options = { diff --git a/types/scene/wlr_scene.c b/types/scene/wlr_scene.c index e7d628d01..e71e102e4 100644 --- a/types/scene/wlr_scene.c +++ b/types/scene/wlr_scene.c @@ -1452,12 +1452,19 @@ static void scene_entry_render(struct render_list_entry *entry, const struct ren int x = entry->x - data->logical.x; int y = entry->y - data->logical.y; - struct wlr_box dst_box = { + struct wlr_box box = { .x = x, .y = y, }; - scene_node_get_size(node, &dst_box.width, &dst_box.height); - transform_output_box(&dst_box, data); + scene_node_get_size(node, &box.width, &box.height); + transform_output_box(&box, data); + + struct wlr_fbox dst_box = (struct wlr_fbox){ + .x = box.x, + .y = box.y, + .width = box.width, + .height = box.height, + }; pixman_region32_t opaque; pixman_region32_init(&opaque); diff --git a/types/wlr_screencopy_v1.c b/types/wlr_screencopy_v1.c index 58e8dc8ba..8e8900531 100644 --- a/types/wlr_screencopy_v1.c +++ b/types/wlr_screencopy_v1.c @@ -256,7 +256,7 @@ static bool frame_dma_copy(struct wlr_screencopy_frame_v1 *frame, wlr_render_pass_add_texture(pass, &(struct wlr_render_texture_options) { .texture = src_tex, .blend_mode = WLR_RENDER_BLEND_MODE_NONE, - .dst_box = (struct wlr_box){ + .dst_box = (struct wlr_fbox){ .width = dst_buffer->width, .height = dst_buffer->height, }, diff --git a/util/matrix.c b/util/matrix.c index 640787c95..9d252a640 100644 --- a/util/matrix.c +++ b/util/matrix.c @@ -120,12 +120,12 @@ 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, +void wlr_matrix_project_box(float mat[static 9], const struct wlr_fbox *box, enum wl_output_transform transform, const float projection[static 9]) { - int x = box->x; - int y = box->y; - int width = box->width; - int height = box->height; + float x = box->x; + float y = box->y; + float width = box->width; + float height = box->height; wlr_matrix_identity(mat); wlr_matrix_translate(mat, x, y); From e92d8a7a53bbe3ada1ba415dfe5ae72b6913793f Mon Sep 17 00:00:00 2001 From: Loukas Agorgianitis Date: Thu, 8 Jan 2026 22:40:20 +0200 Subject: [PATCH 2/2] render/pass: implement antialiasing for fractional coordinates Signed-off-by: Loukas Agorgianitis --- include/render/gles2.h | 2 + include/render/vulkan.h | 7 +++ render/gles2/pass.c | 21 +++++++- render/gles2/renderer.c | 4 ++ render/gles2/shaders/quad.frag | 14 +++++- render/gles2/shaders/tex_external.frag | 14 +++++- render/gles2/shaders/tex_rgba.frag | 14 +++++- render/gles2/shaders/tex_rgbx.frag | 14 +++++- render/vulkan/pass.c | 69 +++++++++++++++++++++----- render/vulkan/shaders/quad.frag | 16 +++++- render/vulkan/shaders/texture.frag | 15 +++++- 11 files changed, 170 insertions(+), 20 deletions(-) diff --git a/include/render/gles2.h b/include/render/gles2.h index 6b852dcb7..c8faeb87a 100644 --- a/include/render/gles2.h +++ b/include/render/gles2.h @@ -35,6 +35,7 @@ struct wlr_gles2_tex_shader { GLint tex; GLint alpha; GLint pos_attrib; + GLint dst_bounds; }; struct wlr_gles2_renderer { @@ -79,6 +80,7 @@ struct wlr_gles2_renderer { GLint proj; GLint color; GLint pos_attrib; + GLint dst_bounds; } quad; struct wlr_gles2_tex_shader tex_rgba; struct wlr_gles2_tex_shader tex_rgbx; diff --git a/include/render/vulkan.h b/include/render/vulkan.h index ac0af65fb..e684e2c21 100644 --- a/include/render/vulkan.h +++ b/include/render/vulkan.h @@ -368,6 +368,13 @@ struct wlr_vk_frag_texture_pcr_data { float matrix[4][4]; // only a 3x3 subset is used float alpha; float luminance_multiplier; + float _pad[2]; // padding for vec4 alignment + float dst_bounds[4]; // x, y, x+width, y+height +}; + +struct wlr_vk_frag_quad_pcr_data { + float color[4]; + float dst_bounds[4]; // x, y, x+width, y+height }; struct wlr_vk_frag_output_pcr_data { diff --git a/render/gles2/pass.c b/render/gles2/pass.c index d79f404c6..51b681f59 100644 --- a/render/gles2/pass.c +++ b/render/gles2/pass.c @@ -1,6 +1,8 @@ #include #include +#include #include +#include #include #include #include @@ -77,7 +79,12 @@ out: static void render(const struct wlr_fbox *box, const pixman_region32_t *clip, GLint attrib) { pixman_region32_t region; - pixman_region32_init_rect(®ion, round(box->x), round(box->y), round(box->width), round(box->height)); + // Expand region to include edge fragments for AA + pixman_region32_init_rect(®ion, + floor(box->x), + floor(box->y), + ceil(box->x + box->width) - floor(box->x), + ceil(box->y + box->height) - floor(box->y)); if (clip) { pixman_region32_intersect(®ion, ®ion, clip); @@ -247,6 +254,12 @@ static void render_pass_add_texture(struct wlr_render_pass *wlr_pass, set_proj_matrix(shader->proj, pass->projection_matrix, &dst_box); set_tex_matrix(shader->tex_proj, options->transform, &src_box); + glUniform4f(shader->dst_bounds, + (float)dst_box.x, + (float)dst_box.y, + (float)(dst_box.x + dst_box.width), + (float)(dst_box.y + dst_box.height)); + render(&dst_box, options->clip, shader->pos_attrib); glBindTexture(texture->target, 0); @@ -270,6 +283,12 @@ static void render_pass_add_rect(struct wlr_render_pass *wlr_pass, set_proj_matrix(renderer->shaders.quad.proj, pass->projection_matrix, &box); glUniform4f(renderer->shaders.quad.color, color->r, color->g, color->b, color->a); + glUniform4f(renderer->shaders.quad.dst_bounds, + (float)box.x, + (float)box.y, + (float)(box.x + box.width), + (float)(box.y + box.height)); + render(&box, options->clip, renderer->shaders.quad.pos_attrib); pop_gles2_debug(renderer); diff --git a/render/gles2/renderer.c b/render/gles2/renderer.c index e362daee8..de16ea10d 100644 --- a/render/gles2/renderer.c +++ b/render/gles2/renderer.c @@ -641,6 +641,7 @@ struct wlr_renderer *wlr_gles2_renderer_create(struct wlr_egl *egl) { renderer->shaders.quad.proj = glGetUniformLocation(prog, "proj"); renderer->shaders.quad.color = glGetUniformLocation(prog, "color"); renderer->shaders.quad.pos_attrib = glGetAttribLocation(prog, "pos"); + renderer->shaders.quad.dst_bounds = glGetUniformLocation(prog, "dst_bounds"); renderer->shaders.tex_rgba.program = prog = link_program(renderer, common_vert_src, tex_rgba_frag_src); @@ -652,6 +653,7 @@ struct wlr_renderer *wlr_gles2_renderer_create(struct wlr_egl *egl) { renderer->shaders.tex_rgba.tex = glGetUniformLocation(prog, "tex"); renderer->shaders.tex_rgba.alpha = glGetUniformLocation(prog, "alpha"); renderer->shaders.tex_rgba.pos_attrib = glGetAttribLocation(prog, "pos"); + renderer->shaders.tex_rgba.dst_bounds = glGetUniformLocation(prog, "dst_bounds"); renderer->shaders.tex_rgbx.program = prog = link_program(renderer, common_vert_src, tex_rgbx_frag_src); @@ -663,6 +665,7 @@ struct wlr_renderer *wlr_gles2_renderer_create(struct wlr_egl *egl) { renderer->shaders.tex_rgbx.tex = glGetUniformLocation(prog, "tex"); renderer->shaders.tex_rgbx.alpha = glGetUniformLocation(prog, "alpha"); renderer->shaders.tex_rgbx.pos_attrib = glGetAttribLocation(prog, "pos"); + renderer->shaders.tex_rgbx.dst_bounds = glGetUniformLocation(prog, "dst_bounds"); if (renderer->exts.OES_egl_image_external) { renderer->shaders.tex_ext.program = prog = @@ -675,6 +678,7 @@ struct wlr_renderer *wlr_gles2_renderer_create(struct wlr_egl *egl) { renderer->shaders.tex_ext.tex = glGetUniformLocation(prog, "tex"); renderer->shaders.tex_ext.alpha = glGetUniformLocation(prog, "alpha"); renderer->shaders.tex_ext.pos_attrib = glGetAttribLocation(prog, "pos"); + renderer->shaders.tex_ext.dst_bounds = glGetUniformLocation(prog, "dst_bounds"); } pop_gles2_debug(renderer); diff --git a/render/gles2/shaders/quad.frag b/render/gles2/shaders/quad.frag index 97d3a31c1..dc240b764 100644 --- a/render/gles2/shaders/quad.frag +++ b/render/gles2/shaders/quad.frag @@ -7,7 +7,19 @@ precision mediump float; varying vec4 v_color; varying vec2 v_texcoord; uniform vec4 color; +uniform vec4 dst_bounds; // x, y, x+width, y+height in output coordinates + +float compute_coverage() { + vec4 d = vec4( + gl_FragCoord.x - dst_bounds.x, + gl_FragCoord.y - dst_bounds.y, + dst_bounds.z - gl_FragCoord.x, + dst_bounds.w - gl_FragCoord.y + ); + vec4 cov = clamp(d + 0.5, 0.0, 1.0); + return min(min(cov.x, cov.y), min(cov.z, cov.w)); +} void main() { - gl_FragColor = color; + gl_FragColor = color * compute_coverage(); } diff --git a/render/gles2/shaders/tex_external.frag b/render/gles2/shaders/tex_external.frag index 73909fe03..4c6033b4d 100644 --- a/render/gles2/shaders/tex_external.frag +++ b/render/gles2/shaders/tex_external.frag @@ -9,7 +9,19 @@ precision mediump float; varying vec2 v_texcoord; uniform samplerExternalOES texture0; uniform float alpha; +uniform vec4 dst_bounds; // x, y, x+width, y+height in output coordinates + +float compute_coverage() { + vec4 d = vec4( + gl_FragCoord.x - dst_bounds.x, + gl_FragCoord.y - dst_bounds.y, + dst_bounds.z - gl_FragCoord.x, + dst_bounds.w - gl_FragCoord.y + ); + vec4 cov = clamp(d + 0.5, 0.0, 1.0); + return min(min(cov.x, cov.y), min(cov.z, cov.w)); +} void main() { - gl_FragColor = texture2D(texture0, v_texcoord) * alpha; + gl_FragColor = texture2D(texture0, v_texcoord) * alpha * compute_coverage(); } diff --git a/render/gles2/shaders/tex_rgba.frag b/render/gles2/shaders/tex_rgba.frag index c0a0dea61..f7e5afe95 100644 --- a/render/gles2/shaders/tex_rgba.frag +++ b/render/gles2/shaders/tex_rgba.frag @@ -7,7 +7,19 @@ precision mediump float; varying vec2 v_texcoord; uniform sampler2D tex; uniform float alpha; +uniform vec4 dst_bounds; // x, y, x+width, y+height in output coordinates + +float compute_coverage() { + vec4 d = vec4( + gl_FragCoord.x - dst_bounds.x, + gl_FragCoord.y - dst_bounds.y, + dst_bounds.z - gl_FragCoord.x, + dst_bounds.w - gl_FragCoord.y + ); + vec4 cov = clamp(d + 0.5, 0.0, 1.0); + return min(min(cov.x, cov.y), min(cov.z, cov.w)); +} void main() { - gl_FragColor = texture2D(tex, v_texcoord) * alpha; + gl_FragColor = texture2D(tex, v_texcoord) * alpha * compute_coverage(); } diff --git a/render/gles2/shaders/tex_rgbx.frag b/render/gles2/shaders/tex_rgbx.frag index ae40ad53e..230eb7c4b 100644 --- a/render/gles2/shaders/tex_rgbx.frag +++ b/render/gles2/shaders/tex_rgbx.frag @@ -7,7 +7,19 @@ precision mediump float; varying vec2 v_texcoord; uniform sampler2D tex; uniform float alpha; +uniform vec4 dst_bounds; // x, y, x+width, y+height in output coordinates + +float compute_coverage() { + vec4 d = vec4( + gl_FragCoord.x - dst_bounds.x, + gl_FragCoord.y - dst_bounds.y, + dst_bounds.z - gl_FragCoord.x, + dst_bounds.w - gl_FragCoord.y + ); + vec4 cov = clamp(d + 0.5, 0.0, 1.0); + return min(min(cov.x, cov.y), min(cov.z, cov.w)); +} void main() { - gl_FragColor = vec4(texture2D(tex, v_texcoord).rgb, 1.0) * alpha; + gl_FragColor = vec4(texture2D(tex, v_texcoord).rgb, 1.0) * alpha * compute_coverage(); } diff --git a/render/vulkan/pass.c b/render/vulkan/pass.c index 4fd06aab2..52fd0b913 100644 --- a/render/vulkan/pass.c +++ b/render/vulkan/pass.c @@ -1,5 +1,7 @@ #include #include +#include +#include #include #include #include @@ -704,9 +706,17 @@ static void render_pass_add_rect(struct wlr_render_pass *wlr_pass, switch (options->blend_mode) { case WLR_RENDER_BLEND_MODE_PREMULTIPLIED:; + // Expand the rendered region to include edge pixels for AA + struct wlr_fbox render_box = { + .x = floor(box.x), + .y = floor(box.y), + .width = ceil(box.x + box.width) - floor(box.x), + .height = ceil(box.y + box.height) - floor(box.y), + }; + float proj[9], matrix[9]; wlr_matrix_identity(proj); - wlr_matrix_project_box(matrix, &box, WL_OUTPUT_TRANSFORM_NORMAL, proj); + wlr_matrix_project_box(matrix, &render_box, WL_OUTPUT_TRANSFORM_NORMAL, proj); wlr_matrix_multiply(matrix, pass->projection, matrix); struct wlr_vk_pipeline *pipe = setup_get_or_create_pipeline( @@ -726,12 +736,22 @@ static void render_pass_add_rect(struct wlr_render_pass *wlr_pass, }; encode_proj_matrix(matrix, vert_pcr_data.mat4); + struct wlr_vk_frag_quad_pcr_data frag_pcr_data = { + .color = { linear_color[0], linear_color[1], linear_color[2], linear_color[3] }, + .dst_bounds = { + (float)box.x, + (float)box.y, + (float)(box.x + box.width), + (float)(box.y + box.height), + }, + }; + bind_pipeline(pass, pipe->vk); vkCmdPushConstants(cb, pipe->layout->vk, VK_SHADER_STAGE_VERTEX_BIT, 0, sizeof(vert_pcr_data), &vert_pcr_data); vkCmdPushConstants(cb, pipe->layout->vk, - VK_SHADER_STAGE_FRAGMENT_BIT, sizeof(vert_pcr_data), sizeof(float) * 4, - linear_color); + VK_SHADER_STAGE_FRAGMENT_BIT, sizeof(vert_pcr_data), + sizeof(frag_pcr_data), &frag_pcr_data); for (int i = 0; i < clip_rects_len; i++) { VkRect2D rect; @@ -792,20 +812,39 @@ 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); + // Expand the rendered region to include edge pixels for AA + struct wlr_fbox render_box = { + .x = floor(dst_box.x), + .y = floor(dst_box.y), + .width = ceil(dst_box.x + dst_box.width) - floor(dst_box.x), + .height = ceil(dst_box.y + dst_box.height) - floor(dst_box.y), + }; + float proj[9], matrix[9]; wlr_matrix_identity(proj); - wlr_matrix_project_box(matrix, &dst_box, options->transform, proj); + wlr_matrix_project_box(matrix, &render_box, options->transform, proj); wlr_matrix_multiply(matrix, pass->projection, matrix); + // Base UV coordinates for sampling src_box from texture + float uv_off_x = src_box.x / options->texture->width; + float uv_off_y = src_box.y / options->texture->height; + float uv_size_x = src_box.width / options->texture->width; + float uv_size_y = src_box.height / options->texture->height; + + // Adjust UV offset and size to compensate for expanded render region + float x_expand = (render_box.x - dst_box.x) / dst_box.width; + float y_expand = (render_box.y - dst_box.y) / dst_box.height; + float x_scale = render_box.width / dst_box.width; + float y_scale = render_box.height / dst_box.height; + + uv_off_x += x_expand * uv_size_x; + uv_off_y += y_expand * uv_size_y; + uv_size_x *= x_scale; + uv_size_y *= y_scale; + struct wlr_vk_vert_pcr_data vert_pcr_data = { - .uv_off = { - src_box.x / options->texture->width, - src_box.y / options->texture->height, - }, - .uv_size = { - src_box.width / options->texture->width, - src_box.height / options->texture->height, - }, + .uv_off = { uv_off_x, uv_off_y }, + .uv_size = { uv_size_x, uv_size_y }, }; encode_proj_matrix(matrix, vert_pcr_data.mat4); @@ -901,6 +940,12 @@ static void render_pass_add_texture(struct wlr_render_pass *wlr_pass, struct wlr_vk_frag_texture_pcr_data frag_pcr_data = { .alpha = alpha, .luminance_multiplier = luminance_multiplier, + .dst_bounds = { + (float)dst_box.x, + (float)dst_box.y, + (float)(dst_box.x + dst_box.width), + (float)(dst_box.y + dst_box.height), + }, }; encode_color_matrix(color_matrix, frag_pcr_data.matrix); diff --git a/render/vulkan/shaders/quad.frag b/render/vulkan/shaders/quad.frag index affd1f116..29d0c4233 100644 --- a/render/vulkan/shaders/quad.frag +++ b/render/vulkan/shaders/quad.frag @@ -3,8 +3,20 @@ layout(location = 0) out vec4 out_color; layout(push_constant) uniform UBO { layout(offset = 80) vec4 color; + vec4 dst_bounds; // x, y, x+width, y+height in output coordinates } data; -void main() { - out_color = data.color; +float compute_coverage() { + vec4 d = vec4( + gl_FragCoord.x - data.dst_bounds.x, + gl_FragCoord.y - data.dst_bounds.y, + data.dst_bounds.z - gl_FragCoord.x, + data.dst_bounds.w - gl_FragCoord.y + ); + vec4 cov = clamp(d + 0.5, 0.0, 1.0); + return min(min(cov.x, cov.y), min(cov.z, cov.w)); +} + +void main() { + out_color = data.color * compute_coverage(); } diff --git a/render/vulkan/shaders/texture.frag b/render/vulkan/shaders/texture.frag index 3ec974235..52f13c32a 100644 --- a/render/vulkan/shaders/texture.frag +++ b/render/vulkan/shaders/texture.frag @@ -10,6 +10,8 @@ layout(push_constant, row_major) uniform UBO { layout(offset = 80) mat4 matrix; float alpha; float luminance_multiplier; + vec2 _pad; // padding for vec4 alignment + vec4 dst_bounds; // x, y, x+width, y+height in output coordinates } data; layout (constant_id = 0) const int TEXTURE_TRANSFORM = 0; @@ -57,6 +59,17 @@ vec3 bt1886_color_to_linear(vec3 color) { return (L - Lmin) / (Lmax - Lmin); } +float compute_coverage() { + vec4 d = vec4( + gl_FragCoord.x - data.dst_bounds.x, + gl_FragCoord.y - data.dst_bounds.y, + data.dst_bounds.z - gl_FragCoord.x, + data.dst_bounds.w - gl_FragCoord.y + ); + vec4 cov = clamp(d + 0.5, 0.0, 1.0); + return min(min(cov.x, cov.y), min(cov.z, cov.w)); +} + void main() { vec4 in_color = textureLod(tex, uv, 0); @@ -89,5 +102,5 @@ void main() { // Back to pre-multiplied alpha out_color = vec4(rgb * alpha, alpha); - out_color *= data.alpha; + out_color *= data.alpha * compute_coverage(); }