refactor: move shaders to individual files

This commit is contained in:
Will McKinnon 2022-11-10 01:32:28 -05:00
parent 79dfbb9f44
commit 26271ef865
12 changed files with 171 additions and 148 deletions

View file

@ -14,10 +14,18 @@
#include <wlr/util/box.h>
#include "log.h"
#include "sway/desktop/fx_renderer.h"
#include "sway/desktop/shaders.h"
#include "sway/output.h"
#include "sway/server.h"
// shaders
#include "quad_vert_src.h"
#include "quad_frag_src.h"
#include "corner_frag_src.h"
#include "tex_vert_src.h"
#include "tex_rgba_frag_src.h"
#include "tex_rgbx_frag_src.h"
#include "tex_external_frag_src.h"
static const GLfloat verts[] = {
1, 0, // top right
0, 0, // top left
@ -125,7 +133,7 @@ struct fx_renderer *fx_renderer_create(struct wlr_egl *egl) {
// init shaders
GLuint prog;
prog = link_program(quad_vertex_src, quad_fragment_src);
prog = link_program(quad_vert_src, quad_frag_src);
renderer->shaders.quad.program = prog;
if (!renderer->shaders.quad.program) {
goto error;
@ -135,7 +143,7 @@ struct fx_renderer *fx_renderer_create(struct wlr_egl *egl) {
renderer->shaders.quad.pos_attrib = glGetAttribLocation(prog, "pos");
// Border corners
prog = link_program(quad_vertex_src, corner_fragment_src);
prog = link_program(quad_vert_src, corner_frag_src);
renderer->shaders.corner.program = prog;
if (!renderer->shaders.corner.program) {
goto error;
@ -153,15 +161,15 @@ struct fx_renderer *fx_renderer_create(struct wlr_egl *egl) {
renderer->shaders.corner.half_thickness = glGetUniformLocation(prog, "half_thickness");
// fragment shaders
prog = link_program(tex_vertex_src, tex_fragment_src_rgba);
prog = link_program(tex_vert_src, tex_rgba_frag_src);
if (!init_frag_shader(&renderer->shaders.tex_rgba, prog)) {
goto error;
}
prog = link_program(tex_vertex_src, tex_fragment_src_rgbx);
prog = link_program(tex_vert_src, tex_rgbx_frag_src);
if (!init_frag_shader(&renderer->shaders.tex_rgbx, prog)) {
goto error;
}
prog = link_program(tex_vertex_src, tex_fragment_src_external);
prog = link_program(tex_vert_src, tex_external_frag_src);
if (!init_frag_shader(&renderer->shaders.tex_ext, prog)) {
goto error;
}

View file

@ -0,0 +1,36 @@
precision mediump float;
varying vec4 v_color;
varying vec2 v_texcoord;
uniform bool is_top_left;
uniform bool is_top_right;
uniform bool is_bottom_left;
uniform bool is_bottom_right;
uniform vec2 position;
uniform float radius;
uniform vec2 half_size;
uniform float half_thickness;
float roundedBoxSDF(vec2 center, vec2 size, float radius) {
return length(max(abs(center) - size + radius, 0.0)) - radius;
}
void main() {
vec2 center = gl_FragCoord.xy - position - half_size;
float distance = roundedBoxSDF(center, half_size - half_thickness, radius + half_thickness);
float smoothedAlphaOuter = 1.0 - smoothstep(-1.0, 1.0, distance - half_thickness);
// Create an inner circle that isn't as anti-aliased as the outer ring
float smoothedAlphaInner = 1.0 - smoothstep(-1.0, 0.5, distance + half_thickness);
gl_FragColor = mix(vec4(0), v_color, smoothedAlphaOuter - smoothedAlphaInner);
if (is_top_left && (center.y > 0.0 || center.x > 0.0)) {
discard;
} else if (is_top_right && (center.y > 0.0 || center.x < 0.0)) {
discard;
} else if (is_bottom_left && (center.y < 0.0 || center.x > 0.0)) {
discard;
} else if (is_bottom_right && (center.y < 0.0 || center.x < 0.0)) {
discard;
}
}

View file

@ -0,0 +1,11 @@
#!/bin/sh -eu
var=${1:-data}
hex="$(od -A n -t x1 -v)"
echo "static const char $var[] = {"
for byte in $hex; do
echo " 0x$byte,"
done
echo " 0x00,"
echo "};"

View file

@ -0,0 +1,24 @@
embed = find_program('./embed.sh', native: true)
shaders = [
'quad.vert',
'quad.frag',
'tex.vert',
'corner.frag',
'tex_rgba.frag',
'tex_rgbx.frag',
'tex_external.frag',
]
foreach name : shaders
output = name.underscorify() + '_src.h'
var = name.underscorify() + '_src'
sway_sources += custom_target(
output,
command: [embed, var],
input: name,
output: output,
feed: true,
capture: true,
)
endforeach

View file

@ -0,0 +1,7 @@
precision mediump float;
varying vec4 v_color;
varying vec2 v_texcoord;
void main() {
gl_FragColor = v_color;
}

View file

@ -0,0 +1,12 @@
uniform mat3 proj;
uniform vec4 color;
attribute vec2 pos;
attribute vec2 texcoord;
varying vec4 v_color;
varying vec2 v_texcoord;
void main() {
gl_Position = vec4(proj * vec3(pos, 1.0), 1.0);
v_color = color;
v_texcoord = texcoord;
}

View file

@ -0,0 +1,9 @@
uniform mat3 proj;
attribute vec2 pos;
attribute vec2 texcoord;
varying vec2 v_texcoord;
void main() {
gl_Position = vec4(proj * vec3(pos, 1.0), 1.0);
v_texcoord = texcoord;
}

View file

@ -0,0 +1,20 @@
#extension GL_OES_EGL_image_external : require
precision mediump float;
varying vec2 v_texcoord;
uniform samplerExternalOES texture0;
uniform float alpha;
uniform vec2 size;
uniform vec2 position;
uniform float radius;
void main() {
gl_FragColor = texture2D(texture0, v_texcoord) * alpha;
vec2 corner_distance = min(gl_FragCoord.xy - position, position + size - gl_FragCoord.xy);
if (max(corner_distance.x, corner_distance.y) < radius) {
float d = radius - distance(corner_distance, vec2(radius));
float smooth = smoothstep(-1.0f, 0.5f, d);
gl_FragColor = mix(vec4(0), gl_FragColor, smooth);
}
}

View file

@ -0,0 +1,18 @@
precision mediump float;
varying vec2 v_texcoord;
uniform sampler2D tex;
uniform float alpha;
uniform vec2 size;
uniform vec2 position;
uniform float radius;
void main() {
gl_FragColor = texture2D(tex, v_texcoord) * alpha;
vec2 corner_distance = min(gl_FragCoord.xy - position, size + position - gl_FragCoord.xy);
if (max(corner_distance.x, corner_distance.y) < radius) {
float d = radius - distance(corner_distance, vec2(radius));
float smooth = smoothstep(-1.0f, 0.5f, d);
gl_FragColor = mix(vec4(0), gl_FragColor, smooth);
}
}

View file

@ -0,0 +1,18 @@
precision mediump float;
varying vec2 v_texcoord;
uniform sampler2D tex;
uniform float alpha;
uniform vec2 size;
uniform vec2 position;
uniform float radius;
void main() {
gl_FragColor = vec4(texture2D(tex, v_texcoord).rgb, 1.0) * alpha;
vec2 corner_distance = min(gl_FragCoord.xy - position, position + size - gl_FragCoord.xy);
if (max(corner_distance.x, corner_distance.y) < radius) {
float d = radius - distance(corner_distance, vec2(radius));
float smooth = smoothstep(-1.0f, 0.5f, d);
gl_FragColor = mix(vec4(0), gl_FragColor, smooth);
}
}

View file

@ -206,6 +206,8 @@ sway_sources = files(
'tree/output.c',
)
subdir('desktop/shaders')
sway_deps = [
cairo,
drm,