From a75f9be2e8c63608ab64b9b93a10fdc25600501b Mon Sep 17 00:00:00 2001 From: Simon Ser Date: Wed, 26 Oct 2022 12:03:50 +0200 Subject: [PATCH] render/gles2: move shaders to individual files Instead of having a C file with strings for each shader, move each shader into its own file. Use a small POSIX shell script to convert the files into C strings (can't wait for C23 #embed...). The benefits from this are: - Improved readability and syntax highlighting. - Line numbers in shader compiler errors are easier to make sense of. - Consistency with the Vulkan renderer. - Shaders will become more complicated as we add color management features. --- meson.build | 2 +- render/gles2/meson.build | 3 +- render/gles2/renderer.c | 22 ++++---- render/gles2/shaders.c | 69 -------------------------- render/gles2/shaders/embed.sh | 11 ++++ render/gles2/shaders/meson.build | 23 +++++++++ render/gles2/shaders/quad.frag | 7 +++ render/gles2/shaders/quad.vert | 12 +++++ render/gles2/shaders/tex.vert | 9 ++++ render/gles2/shaders/tex_external.frag | 10 ++++ render/gles2/shaders/tex_rgba.frag | 8 +++ render/gles2/shaders/tex_rgbx.frag | 8 +++ 12 files changed, 102 insertions(+), 82 deletions(-) delete mode 100644 render/gles2/shaders.c create mode 100755 render/gles2/shaders/embed.sh create mode 100644 render/gles2/shaders/meson.build create mode 100644 render/gles2/shaders/quad.frag create mode 100644 render/gles2/shaders/quad.vert create mode 100644 render/gles2/shaders/tex.vert create mode 100644 render/gles2/shaders/tex_external.frag create mode 100644 render/gles2/shaders/tex_rgba.frag create mode 100644 render/gles2/shaders/tex_rgbx.frag diff --git a/meson.build b/meson.build index a180b43c..45eeabaa 100644 --- a/meson.build +++ b/meson.build @@ -3,7 +3,7 @@ project( 'c', version: '0.16.0-dev', license: 'MIT', - meson_version: '>=0.58.1', + meson_version: '>=0.59.0', default_options: [ 'c_std=c11', 'warning_level=2', diff --git a/render/gles2/meson.build b/render/gles2/meson.build index 504f0c11..c9226983 100644 --- a/render/gles2/meson.build +++ b/render/gles2/meson.build @@ -10,6 +10,7 @@ wlr_deps += glesv2 wlr_files += files( 'pixel_format.c', 'renderer.c', - 'shaders.c', 'texture.c', ) + +subdir('shaders') diff --git a/render/gles2/renderer.c b/render/gles2/renderer.c index aa1adabe..de80a55f 100644 --- a/render/gles2/renderer.c +++ b/render/gles2/renderer.c @@ -19,6 +19,13 @@ #include "render/pixel_format.h" #include "types/wlr_matrix.h" +#include "quad_vert_src.h" +#include "quad_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 @@ -667,13 +674,6 @@ static void load_gl_proc(void *proc_ptr, const char *name) { *(void **)proc_ptr = proc; } -extern const GLchar quad_vertex_src[]; -extern const GLchar quad_fragment_src[]; -extern const GLchar tex_vertex_src[]; -extern const GLchar tex_fragment_src_rgba[]; -extern const GLchar tex_fragment_src_rgbx[]; -extern const GLchar tex_fragment_src_external[]; - struct wlr_renderer *wlr_gles2_renderer_create_with_drm_fd(int drm_fd) { struct wlr_egl *egl = wlr_egl_create_with_drm_fd(drm_fd); if (egl == NULL) { @@ -786,7 +786,7 @@ struct wlr_renderer *wlr_gles2_renderer_create(struct wlr_egl *egl) { GLuint prog; renderer->shaders.quad.program = prog = - link_program(renderer, quad_vertex_src, quad_fragment_src); + link_program(renderer, quad_vert_src, quad_frag_src); if (!renderer->shaders.quad.program) { goto error; } @@ -795,7 +795,7 @@ struct wlr_renderer *wlr_gles2_renderer_create(struct wlr_egl *egl) { renderer->shaders.quad.pos_attrib = glGetAttribLocation(prog, "pos"); renderer->shaders.tex_rgba.program = prog = - link_program(renderer, tex_vertex_src, tex_fragment_src_rgba); + link_program(renderer, tex_vert_src, tex_rgba_frag_src); if (!renderer->shaders.tex_rgba.program) { goto error; } @@ -806,7 +806,7 @@ struct wlr_renderer *wlr_gles2_renderer_create(struct wlr_egl *egl) { renderer->shaders.tex_rgba.tex_attrib = glGetAttribLocation(prog, "texcoord"); renderer->shaders.tex_rgbx.program = prog = - link_program(renderer, tex_vertex_src, tex_fragment_src_rgbx); + link_program(renderer, tex_vert_src, tex_rgbx_frag_src); if (!renderer->shaders.tex_rgbx.program) { goto error; } @@ -818,7 +818,7 @@ struct wlr_renderer *wlr_gles2_renderer_create(struct wlr_egl *egl) { if (renderer->exts.OES_egl_image_external) { renderer->shaders.tex_ext.program = prog = - link_program(renderer, tex_vertex_src, tex_fragment_src_external); + link_program(renderer, tex_vert_src, tex_external_frag_src); if (!renderer->shaders.tex_ext.program) { goto error; } diff --git a/render/gles2/shaders.c b/render/gles2/shaders.c deleted file mode 100644 index 7898059e..00000000 --- a/render/gles2/shaders.c +++ /dev/null @@ -1,69 +0,0 @@ -#include -#include "render/gles2.h" - -// Colored quads -const GLchar quad_vertex_src[] = -"uniform mat3 proj;\n" -"uniform vec4 color;\n" -"attribute vec2 pos;\n" -"attribute vec2 texcoord;\n" -"varying vec4 v_color;\n" -"varying vec2 v_texcoord;\n" -"\n" -"void main() {\n" -" gl_Position = vec4(proj * vec3(pos, 1.0), 1.0);\n" -" v_color = color;\n" -" v_texcoord = texcoord;\n" -"}\n"; - -const GLchar quad_fragment_src[] = -"precision mediump float;\n" -"varying vec4 v_color;\n" -"varying vec2 v_texcoord;\n" -"\n" -"void main() {\n" -" gl_FragColor = v_color;\n" -"}\n"; - -// Textured quads -const GLchar tex_vertex_src[] = -"uniform mat3 proj;\n" -"attribute vec2 pos;\n" -"attribute vec2 texcoord;\n" -"varying vec2 v_texcoord;\n" -"\n" -"void main() {\n" -" gl_Position = vec4(proj * vec3(pos, 1.0), 1.0);\n" -" v_texcoord = texcoord;\n" -"}\n"; - -const GLchar tex_fragment_src_rgba[] = -"precision mediump float;\n" -"varying vec2 v_texcoord;\n" -"uniform sampler2D tex;\n" -"uniform float alpha;\n" -"\n" -"void main() {\n" -" gl_FragColor = texture2D(tex, v_texcoord) * alpha;\n" -"}\n"; - -const GLchar tex_fragment_src_rgbx[] = -"precision mediump float;\n" -"varying vec2 v_texcoord;\n" -"uniform sampler2D tex;\n" -"uniform float alpha;\n" -"\n" -"void main() {\n" -" gl_FragColor = vec4(texture2D(tex, v_texcoord).rgb, 1.0) * alpha;\n" -"}\n"; - -const GLchar tex_fragment_src_external[] = -"#extension GL_OES_EGL_image_external : require\n\n" -"precision mediump float;\n" -"varying vec2 v_texcoord;\n" -"uniform samplerExternalOES texture0;\n" -"uniform float alpha;\n" -"\n" -"void main() {\n" -" gl_FragColor = texture2D(texture0, v_texcoord) * alpha;\n" -"}\n"; diff --git a/render/gles2/shaders/embed.sh b/render/gles2/shaders/embed.sh new file mode 100755 index 00000000..acd7a119 --- /dev/null +++ b/render/gles2/shaders/embed.sh @@ -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 "};" diff --git a/render/gles2/shaders/meson.build b/render/gles2/shaders/meson.build new file mode 100644 index 00000000..626c2b14 --- /dev/null +++ b/render/gles2/shaders/meson.build @@ -0,0 +1,23 @@ +embed = find_program('./embed.sh', native: true) + +shaders = [ + 'quad.vert', + 'quad.frag', + 'tex.vert', + 'tex_rgba.frag', + 'tex_rgbx.frag', + 'tex_external.frag', +] + +foreach name : shaders + output = name.underscorify() + '_src.h' + var = name.underscorify() + '_src' + wlr_files += custom_target( + output, + command: [embed, var], + input: name, + output: output, + feed: true, + capture: true, + ) +endforeach diff --git a/render/gles2/shaders/quad.frag b/render/gles2/shaders/quad.frag new file mode 100644 index 00000000..08291046 --- /dev/null +++ b/render/gles2/shaders/quad.frag @@ -0,0 +1,7 @@ +precision mediump float; +varying vec4 v_color; +varying vec2 v_texcoord; + +void main() { + gl_FragColor = v_color; +} diff --git a/render/gles2/shaders/quad.vert b/render/gles2/shaders/quad.vert new file mode 100644 index 00000000..abcafd07 --- /dev/null +++ b/render/gles2/shaders/quad.vert @@ -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; +} diff --git a/render/gles2/shaders/tex.vert b/render/gles2/shaders/tex.vert new file mode 100644 index 00000000..9cebb458 --- /dev/null +++ b/render/gles2/shaders/tex.vert @@ -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; +} diff --git a/render/gles2/shaders/tex_external.frag b/render/gles2/shaders/tex_external.frag new file mode 100644 index 00000000..05eac503 --- /dev/null +++ b/render/gles2/shaders/tex_external.frag @@ -0,0 +1,10 @@ +#extension GL_OES_EGL_image_external : require + +precision mediump float; +varying vec2 v_texcoord; +uniform samplerExternalOES texture0; +uniform float alpha; + +void main() { + gl_FragColor = texture2D(texture0, v_texcoord) * alpha; +} diff --git a/render/gles2/shaders/tex_rgba.frag b/render/gles2/shaders/tex_rgba.frag new file mode 100644 index 00000000..c2e17a90 --- /dev/null +++ b/render/gles2/shaders/tex_rgba.frag @@ -0,0 +1,8 @@ +precision mediump float; +varying vec2 v_texcoord; +uniform sampler2D tex; +uniform float alpha; + +void main() { + gl_FragColor = texture2D(tex, v_texcoord) * alpha; +} diff --git a/render/gles2/shaders/tex_rgbx.frag b/render/gles2/shaders/tex_rgbx.frag new file mode 100644 index 00000000..42ddf92f --- /dev/null +++ b/render/gles2/shaders/tex_rgbx.frag @@ -0,0 +1,8 @@ +precision mediump float; +varying vec2 v_texcoord; +uniform sampler2D tex; +uniform float alpha; + +void main() { + gl_FragColor = vec4(texture2D(tex, v_texcoord).rgb, 1.0) * alpha; +}