feat: add round borders (#22)

Co-authored-by: Erik Reider <35975961+ErikReider@users.noreply.github.com>
This commit is contained in:
William McKinnon 2022-10-09 11:47:34 -04:00 committed by GitHub
parent 22a6aba9f0
commit 90a80e7041
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
5 changed files with 301 additions and 18 deletions

View file

@ -2,6 +2,9 @@
#define _SWAY_OPENGL_H
#include <GLES2/gl2.h>
#include <stdbool.h>
enum corner_location {NONE, TOP_LEFT, TOP_RIGHT, BOTTOM_LEFT, BOTTOM_RIGHT};
struct gles2_tex_shader {
GLuint program;
@ -29,6 +32,21 @@ struct fx_renderer {
GLint color;
GLint pos_attrib;
} quad;
struct {
GLuint program;
GLint proj;
GLint color;
GLint pos_attrib;
GLint is_top_left;
GLint is_top_right;
GLint is_bottom_left;
GLint is_bottom_right;
GLint width;
GLint height;
GLint position;
GLint radius;
GLint thickness;
} corner;
struct gles2_tex_shader tex_rgba;
struct gles2_tex_shader tex_rgbx;
struct gles2_tex_shader tex_ext;
@ -55,4 +73,8 @@ bool fx_render_texture_with_matrix(struct fx_renderer *renderer, struct wlr_text
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_border_corner(struct fx_renderer *renderer, const struct wlr_box *box,
const float color[static 4], const float projection[static 9],
enum corner_location corner_location, int radius, int border_thickness);
#endif

View file

@ -1,6 +1,8 @@
#ifndef _SWAY_SHADERS_H
#define _SWAY_SHADERS_H
#include <GLES2/gl2.h>
// Colored quads
const GLchar quad_vertex_src[] =
"uniform mat3 proj;\n"
@ -53,7 +55,7 @@ const GLchar tex_fragment_src_rgba[] =
" 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"
" float smooth = smoothstep(-1.0f, 0.5f, d);\n"
" gl_FragColor = mix(vec4(0), gl_FragColor, smooth);\n"
" }\n"
"}\n";
@ -74,7 +76,7 @@ const GLchar tex_fragment_src_rgbx[] =
" 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"
" float smooth = smoothstep(-1.0f, 0.5f, d);\n"
" gl_FragColor = mix(vec4(0), gl_FragColor, smooth);\n"
" }\n"
"}\n";
@ -96,9 +98,65 @@ const GLchar tex_fragment_src_external[] =
" 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"
" float smooth = smoothstep(-1.0f, 0.5f, d);\n"
" gl_FragColor = mix(vec4(0), gl_FragColor, smooth);\n"
" }\n"
"}\n";
const GLchar corner_fragment_src[] =
"precision mediump float;\n"
"varying vec4 v_color;\n"
"varying vec2 v_texcoord;\n"
"\n"
"uniform bool is_top_left;\n"
"uniform bool is_top_right;\n"
"uniform bool is_bottom_left;\n"
"uniform bool is_bottom_right;\n"
"\n"
"uniform float width;\n"
"uniform float height;\n"
"uniform vec2 position;\n"
"uniform float radius;\n"
"uniform float thickness;\n"
"\n"
"float roundedBoxSDF(vec2 center, vec2 size, float radius) {\n"
" return length(max(abs(center) - size + radius, 0.0)) - radius;\n"
"}\n"
"\n"
"void main() {\n"
" gl_FragColor = v_color;\n"
" vec2 size = vec2(width, height);\n"
" vec2 pos = vec2(position.x - (width + thickness) * 0.5, position.y -\n"
" (width + thickness) * 0.5);\n"
" vec2 rel_pos = gl_FragCoord.xy - pos - size - thickness * 0.5;\n"
"\n"
" float distance = roundedBoxSDF(\n"
" rel_pos,\n" // Center
" (size - thickness) * 0.5,\n" // Size
" radius + thickness * 0.5\n" // Radius
" );\n"
"\n"
" float smoothedAlphaOuter = 1.0 - smoothstep(-1.0, 1.0, distance - thickness * 0.5);\n"
// Creates a inner circle that isn't as anti-aliased as the outer ring
" float smoothedAlphaInner = 1.0 - smoothstep(-1.0, 0.5, distance + thickness * 0.5);\n"
" gl_FragColor = mix(vec4(0), gl_FragColor, smoothedAlphaOuter - smoothedAlphaInner);\n"
"\n"
// top left
" if (is_top_left && (rel_pos.y > 0.0 || rel_pos.x > 0.0)) {\n"
" discard;\n"
" }\n"
// top right
" else if (is_top_right && (rel_pos.y > 0.0 || rel_pos.x < 0.0)) {\n"
" discard;\n"
" }\n"
// bottom left
" else if (is_bottom_left && (rel_pos.y < 0.0 || rel_pos.x > 0.0)) {\n"
" discard;\n"
" }\n"
// bottom right
" else if (is_bottom_right && (rel_pos.y < 0.0 || rel_pos.x < 0.0)) {\n"
" discard;\n"
" }\n"
"}\n";
#endif