Add rounded corners (#9)

This commit is contained in:
William McKinnon 2022-08-21 17:12:54 -04:00 committed by GitHub
parent c5d08f6085
commit 5c0086b944
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
9 changed files with 454 additions and 244 deletions

View file

@ -4,6 +4,8 @@
- https://github.com/vaxerski/Hyprland/blob/main/src/render/OpenGL.cpp
*/
// TODO: add push / pop_gles2_debug(renderer)?
#define _POSIX_C_SOURCE 200809L
#include <assert.h>
#include <GLES2/gl2.h>
@ -64,8 +66,57 @@ 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 radius;\n"
"\n"
"uniform int discardOpaque;\n"
"\n"
"void main() {\n"
" gl_FragColor = texture2D(tex, v_texcoord) * alpha;\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"
"}\n";
const GLchar tex_fragment_src_rgbx[] =
@ -74,7 +125,55 @@ 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 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"
"}\n";
@ -85,8 +184,58 @@ 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 radius;\n"
"\n"
"uniform int discardOpaque;\n"
"\n"
"void main() {\n"
" gl_FragColor = texture2D(texture0, v_texcoord) * alpha;\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"
"}\n";
/************************
@ -207,6 +356,7 @@ struct fx_renderer *fx_renderer_create(struct wlr_egl *egl) {
renderer->shaders.tex_rgba.alpha = glGetUniformLocation(prog, "alpha");
renderer->shaders.tex_rgba.pos_attrib = glGetAttribLocation(prog, "pos");
renderer->shaders.tex_rgba.tex_attrib = glGetAttribLocation(prog, "texcoord");
renderer->shaders.tex_rgba.discardOpaque = glGetUniformLocation(prog, "discardOpaque");
prog = link_program(tex_vertex_src, tex_fragment_src_rgbx);
renderer->shaders.tex_rgbx.program = prog;
@ -218,6 +368,7 @@ struct fx_renderer *fx_renderer_create(struct wlr_egl *egl) {
renderer->shaders.tex_rgbx.alpha = glGetUniformLocation(prog, "alpha");
renderer->shaders.tex_rgbx.pos_attrib = glGetAttribLocation(prog, "pos");
renderer->shaders.tex_rgbx.tex_attrib = glGetAttribLocation(prog, "texcoord");
renderer->shaders.tex_rgbx.discardOpaque = glGetUniformLocation(prog, "discardOpaque");
prog = link_program(tex_vertex_src, tex_fragment_src_external);
renderer->shaders.tex_ext.program = prog;
@ -229,8 +380,8 @@ struct fx_renderer *fx_renderer_create(struct wlr_egl *egl) {
renderer->shaders.tex_ext.alpha = glGetUniformLocation(prog, "alpha");
renderer->shaders.tex_ext.pos_attrib = glGetAttribLocation(prog, "pos");
renderer->shaders.tex_ext.tex_attrib = glGetAttribLocation(prog, "texcoord");
renderer->shaders.tex_ext.discardOpaque = glGetUniformLocation(prog, "discardOpaque");
// TODO: if remove renderer->egl, replace below with r->egl
wlr_egl_unset_current(renderer->egl);
sway_log(SWAY_INFO, "GLES2 RENDERER: Shaders Initialized Successfully");
@ -262,7 +413,7 @@ void fx_renderer_begin(struct fx_renderer *renderer, uint32_t width, uint32_t he
}
void fx_renderer_end() {
// TODO
}
void fx_renderer_clear(const float color[static 4]) {
@ -285,7 +436,7 @@ 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) {
const float matrix[static 9], float alpha, int radius) {
assert(wlr_texture_is_gles2(wlr_texture));
struct wlr_gles2_texture_attribs texture_attrs;
@ -341,6 +492,7 @@ bool fx_render_subtexture_with_matrix(struct fx_renderer *renderer,
glUniformMatrix3fv(shader->proj, 1, GL_FALSE, gl_matrix);
glUniform1i(shader->tex, 0);
glUniform1f(shader->alpha, alpha);
glUniform1i(shader->discardOpaque, 0); // TODO
const GLfloat x1 = box->x / wlr_texture->width;
const GLfloat y1 = box->y / wlr_texture->height;
@ -353,6 +505,23 @@ bool fx_render_subtexture_with_matrix(struct fx_renderer *renderer,
x1, y2, // bottom left
};
glUniform2f(
glGetUniformLocation(shader->program, "topLeft"),
radius,
radius
);
glUniform2f(
glGetUniformLocation(shader->program, "bottomRight"),
wlr_texture->width - radius,
wlr_texture->height - radius
);
glUniform2f(
glGetUniformLocation(shader->program, "fullSize"),
wlr_texture->width,
wlr_texture->height
);
glUniform1f(glGetUniformLocation(shader->program, "radius"), radius);
glVertexAttribPointer(shader->pos_attrib, 2, GL_FLOAT, GL_FALSE, 0, verts);
glVertexAttribPointer(shader->tex_attrib, 2, GL_FLOAT, GL_FALSE, 0, texcoord);
@ -370,14 +539,14 @@ bool fx_render_subtexture_with_matrix(struct fx_renderer *renderer,
}
bool fx_render_texture_with_matrix(struct fx_renderer *renderer,
struct wlr_texture *wlr_texture, const float matrix[static 9], float alpha) {
struct wlr_texture *wlr_texture, const float matrix[static 9], float alpha, int radius) {
struct wlr_fbox box = {
.x = 0,
.y = 0,
.width = wlr_texture->width,
.height = wlr_texture->height,
};
return fx_render_subtexture_with_matrix(renderer, wlr_texture, &box, matrix, alpha);
return fx_render_subtexture_with_matrix(renderer, wlr_texture, &box, matrix, alpha, radius);
}
void fx_render_rect(struct fx_renderer *renderer, const struct wlr_box *box, const float color[static 4], const float projection[static 9]) {