mirror of
https://gitlab.freedesktop.org/wlroots/wlroots.git
synced 2026-04-20 06:47:19 -04:00
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:
parent
32d00984e1
commit
25d8151870
8 changed files with 91 additions and 77 deletions
45
render/gles2/shaders/common.frag
Normal file
45
render/gles2/shaders/common.frag
Normal 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;
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue