Add Anti-Aliased Corner Radius (#18)

This commit is contained in:
William McKinnon 2022-08-29 18:25:11 -04:00 committed by GitHub
parent 3b287a73b9
commit c1f4cf17db
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
5 changed files with 76 additions and 189 deletions

View file

@ -8,9 +8,12 @@ struct gles2_tex_shader {
GLint proj;
GLint tex;
GLint alpha;
GLint discardOpaque;
GLint pos_attrib;
GLint tex_attrib;
GLint width;
GLint height;
GLint position;
GLint radius;
};
struct fx_renderer {
@ -42,13 +45,14 @@ void fx_renderer_clear(const float color[static 4]);
void fx_renderer_scissor(struct wlr_box *box);
bool fx_render_subtexture_with_matrix(struct fx_renderer *renderer,
struct wlr_texture *wlr_texture, const struct wlr_fbox *box,
const float matrix[static 9], float alpha, int radius);
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 alpha, int radius);
bool fx_render_texture_with_matrix(struct fx_renderer *renderer,
struct wlr_texture *wlr_texture, const float matrix[static 9], float alpha, int radius);
bool fx_render_texture_with_matrix(struct fx_renderer *renderer, struct wlr_texture *wlr_texture,
const struct wlr_box *dst_box, const float matrix[static 9], float alpha, int radius);
void fx_render_rect(struct fx_renderer *renderer, const struct wlr_box *box, const float color[static 4], const float projection[static 9]);
void fx_render_rect(struct fx_renderer *renderer, const struct wlr_box *box,
const float color[static 4], const float projection[static 9]);
#endif

View file

@ -43,57 +43,19 @@ const GLchar tex_fragment_src_rgba[] =
"uniform sampler2D tex;\n"
"uniform float alpha;\n"
"\n"
"uniform vec2 topLeft;\n"
"uniform vec2 bottomRight;\n"
"uniform vec2 fullSize;\n"
"uniform float width;\n"
"uniform float height;\n"
"uniform vec2 position;\n"
"uniform float radius;\n"
"\n"
"uniform int discardOpaque;\n"
"\n"
"void main() {\n"
" vec4 pixColor = texture2D(tex, v_texcoord);\n"
"\n"
" if (discardOpaque == 1 && pixColor[3] * alpha == 1.0) {\n"
" discard;\n"
" return;\n"
" }\n"
"\n"
" vec2 pixCoord = fullSize * v_texcoord;\n"
"\n"
" if (pixCoord[0] < topLeft[0]) {\n"
" // we're close left\n"
" if (pixCoord[1] < topLeft[1]) {\n"
// top
" if (distance(topLeft, pixCoord) > radius) {\n"
" discard;\n"
" return;\n"
" }\n"
" } else if (pixCoord[1] > bottomRight[1]) {\n"
// bottom
" if (distance(vec2(topLeft[0], bottomRight[1]), pixCoord) > radius) {\n"
" discard;\n"
" return;\n"
" }\n"
" }\n"
" }\n"
" else if (pixCoord[0] > bottomRight[0]) {\n"
// we're close right
" if (pixCoord[1] < topLeft[1]) {\n"
// top
" if (distance(vec2(bottomRight[0], topLeft[1]), pixCoord) > radius) {\n"
" discard;\n"
" return;\n"
" }\n"
" } else if (pixCoord[1] > bottomRight[1]) {\n"
// bottom
" if (distance(bottomRight, pixCoord) > radius) {\n"
" discard;\n"
" return;\n"
" }\n"
" }\n"
" }\n"
"\n"
" gl_FragColor = pixColor * alpha;\n"
" gl_FragColor = texture2D(tex, v_texcoord) * alpha;\n"
" vec2 corner_distance = min(gl_FragCoord.xy - position, position + vec2(width, height) - gl_FragCoord.xy);\n"
" if (max(corner_distance.x, corner_distance.y) < radius) {\n"
" float d = radius - distance(corner_distance, vec2(radius, radius));\n"
" float smooth = smoothstep(-1.0f, 1.0f, d);\n"
" gl_FragColor = mix(vec4(0), gl_FragColor, smooth);\n"
" }\n"
"}\n";
const GLchar tex_fragment_src_rgbx[] =
@ -102,56 +64,19 @@ const GLchar tex_fragment_src_rgbx[] =
"uniform sampler2D tex;\n"
"uniform float alpha;\n"
"\n"
"uniform vec2 topLeft;\n"
"uniform vec2 bottomRight;\n"
"uniform vec2 fullSize;\n"
"uniform float width;\n"
"uniform float height;\n"
"uniform vec2 position;\n"
"uniform float radius;\n"
"\n"
"uniform int discardOpaque;\n"
"\n"
"void main() {\n"
"\n"
" if (discardOpaque == 1 && alpha == 1.0) {\n"
" discard;\n"
" return;\n"
" }\n"
"\n"
" vec2 pixCoord = fullSize * v_texcoord;\n"
"\n"
" if (pixCoord[0] < topLeft[0]) {\n"
// we're close left
" if (pixCoord[1] < topLeft[1]) {\n"
// top
" if (distance(topLeft, pixCoord) > radius) {\n"
" discard;\n"
" return;\n"
" }\n"
" } else if (pixCoord[1] > bottomRight[1]) {\n"
// bottom
" if (distance(vec2(topLeft[0], bottomRight[1]), pixCoord) > radius) {\n"
" discard;\n"
" return;\n"
" }\n"
" }\n"
" }\n"
" else if (pixCoord[0] > bottomRight[0]) {\n"
// we're close right
" if (pixCoord[1] < topLeft[1]) {\n"
// top
" if (distance(vec2(bottomRight[0], topLeft[1]), pixCoord) > radius) {\n"
" discard;\n"
" return;\n"
" }\n"
" } else if (pixCoord[1] > bottomRight[1]) {\n"
// bottom
" if (distance(bottomRight, pixCoord) > radius) {\n"
" discard;\n"
" return;\n"
" }\n"
" }\n"
" }\n"
"\n"
" gl_FragColor = vec4(texture2D(tex, v_texcoord).rgb, 1.0) * alpha;\n"
" vec2 corner_distance = min(gl_FragCoord.xy - position, position + vec2(width, height) - gl_FragCoord.xy);\n"
" if (max(corner_distance.x, corner_distance.y) < radius) {\n"
" float d = radius - distance(corner_distance, vec2(radius, radius));\n"
" float smooth = smoothstep(-1.0f, 1.0f, d);\n"
" gl_FragColor = mix(vec4(0), gl_FragColor, smooth);\n"
" }\n"
"}\n";
const GLchar tex_fragment_src_external[] =
@ -161,58 +86,19 @@ const GLchar tex_fragment_src_external[] =
"uniform samplerExternalOES texture0;\n"
"uniform float alpha;\n"
"\n"
"uniform vec2 topLeft;\n"
"uniform vec2 bottomRight;\n"
"uniform vec2 fullSize;\n"
"uniform float width;\n"
"uniform float height;\n"
"uniform vec2 position;\n"
"uniform float radius;\n"
"\n"
"uniform int discardOpaque;\n"
"\n"
"void main() {\n"
"\n"
" vec4 pixColor = texture2D(texture0, v_texcoord);\n"
"\n"
" if (discardOpaque == 1 && pixColor[3] * alpha == 1.0) {\n"
" discard;\n"
" return;\n"
" }\n"
"\n"
" vec2 pixCoord = fullSize * v_texcoord;\n"
"\n"
" if (pixCoord[0] < topLeft[0]) {\n"
// we're close left
" if (pixCoord[1] < topLeft[1]) {\n"
// top
" if (distance(topLeft, pixCoord) > radius) {\n"
" discard;\n"
" return;\n"
" }\n"
" } else if (pixCoord[1] > bottomRight[1]) {\n"
// bottom
" if (distance(vec2(topLeft[0], bottomRight[1]), pixCoord) > radius) {\n"
" discard;\n"
" return;\n"
" }\n"
" }\n"
" }\n"
" else if (pixCoord[0] > bottomRight[0]) {\n"
// we're close right
" if (pixCoord[1] < topLeft[1]) {\n"
// top
" if (distance(vec2(bottomRight[0], topLeft[1]), pixCoord) > radius) {\n"
" discard;\n"
" return;\n"
" }\n"
" } else if (pixCoord[1] > bottomRight[1]) {\n"
// bottom
" if (distance(bottomRight, pixCoord) > radius) {\n"
" discard;\n"
" return;\n"
" }\n"
" }\n"
" }\n"
"\n"
" gl_FragColor = pixColor * alpha;\n"
" gl_FragColor = texture2D(texture0, v_texcoord) * alpha;\n"
" vec2 corner_distance = min(gl_FragCoord.xy - position, position + vec2(width, height) - gl_FragCoord.xy);\n"
" if (max(corner_distance.x, corner_distance.y) < radius) {\n"
" float d = radius - distance(corner_distance, vec2(radius, radius));\n"
" float smooth = smoothstep(-1.0f, 1.0f, d);\n"
" gl_FragColor = mix(vec4(0), gl_FragColor, smooth);\n"
" }\n"
"}\n";
const GLchar frag_blur_1[] =
@ -259,4 +145,3 @@ const GLchar frag_blur_2[] =
"}\n";
#endif