render/gles2: unify fragment shader

Instead of having 3 different fragment shaders for textures and 1
more for quads, unify them all and compile different variants via
a SOURCE constant.

In the future, we will have more variant dimensions: pre-multiplied
alpha on/off, color transformation, etc. It will become inpractical
to have one file per combination.

Weston has a similar approach:
https://gitlab.freedesktop.org/wayland/weston/-/blob/main/libweston/renderer-gl/fragment.glsl
This commit is contained in:
Simon Ser 2022-10-26 21:22:47 +02:00
parent 32d00984e1
commit 25d8151870
8 changed files with 91 additions and 77 deletions

View file

@ -0,0 +1,45 @@
/* enum wlr_gles2_shader_source */
#define SOURCE_SINGLE_COLOR 1
#define SOURCE_TEXTURE_RGBA 2
#define SOURCE_TEXTURE_RGBX 3
#define SOURCE_TEXTURE_EXTERNAL 4
#if !defined(SOURCE)
#error "Missing shader preamble"
#endif
#if SOURCE == SOURCE_TEXTURE_EXTERNAL
#extension GL_OES_EGL_image_external : require
#endif
precision mediump float;
varying vec2 v_texcoord;
#if SOURCE == SOURCE_TEXTURE_EXTERNAL
uniform samplerExternalOES tex;
#elif SOURCE == SOURCE_TEXTURE_RGBA || SOURCE == SOURCE_TEXTURE_RGBX
uniform sampler2D tex;
#elif SOURCE == SOURCE_SINGLE_COLOR
uniform vec4 color;
#endif
#if SOURCE != SOURCE_SINGLE_COLOR
uniform float alpha;
#else
const float alpha = 1.0;
#endif
vec4 sample_texture() {
#if SOURCE == SOURCE_TEXTURE_RGBA || SOURCE == SOURCE_TEXTURE_EXTERNAL
return texture2D(tex, v_texcoord);
#elif SOURCE == SOURCE_TEXTURE_RGBX
return vec4(texture2D(tex, v_texcoord).rgb, 1.0);
#elif SOURCE == SOURCE_SINGLE_COLOR
return color;
#endif
}
void main() {
gl_FragColor = sample_texture() * alpha;
}

View file

@ -2,10 +2,7 @@ embed = find_program('./embed.sh', native: true)
shaders = [
'common.vert',
'quad.frag',
'tex_rgba.frag',
'tex_rgbx.frag',
'tex_external.frag',
'common.frag',
]
foreach name : shaders

View file

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

View file

@ -1,10 +0,0 @@
#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;
}

View file

@ -1,8 +0,0 @@
precision mediump float;
varying vec2 v_texcoord;
uniform sampler2D tex;
uniform float alpha;
void main() {
gl_FragColor = texture2D(tex, v_texcoord) * alpha;
}

View file

@ -1,8 +0,0 @@
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;
}