Clean up glsl code

This commit is contained in:
Emil Ernerfeldt 2021-11-13 13:09:08 +01:00
parent b5cb2b2c0d
commit 105cb57050
13 changed files with 219 additions and 219 deletions

View file

@ -16,7 +16,13 @@ varying vec4 v_rgba;
varying vec2 v_tc; varying vec2 v_tc;
#endif #endif
#ifndef SRGB_SUPPORTED #ifdef SRGB_SUPPORTED
void main() {
// The texture sampler is sRGB aware, and OpenGL already expects linear rgba output
// so no need for any sRGB conversions here:
gl_FragColor = v_rgba * texture2D(u_sampler, v_tc);
}
#else
// 0-255 sRGB from 0-1 linear // 0-255 sRGB from 0-1 linear
vec3 srgb_from_linear(vec3 rgb) { vec3 srgb_from_linear(vec3 rgb) {
bvec3 cutoff = lessThan(rgb, vec3(0.0031308)); bvec3 cutoff = lessThan(rgb, vec3(0.0031308));
@ -28,6 +34,7 @@ vec3 srgb_from_linear(vec3 rgb) {
vec4 srgba_from_linear(vec4 rgba) { vec4 srgba_from_linear(vec4 rgba) {
return vec4(srgb_from_linear(rgba.rgb), 255.0 * rgba.a); return vec4(srgb_from_linear(rgba.rgb), 255.0 * rgba.a);
} }
// 0-1 linear from 0-255 sRGB // 0-1 linear from 0-255 sRGB
vec3 linear_from_srgb(vec3 srgb) { vec3 linear_from_srgb(vec3 srgb) {
bvec3 cutoff = lessThan(srgb, vec3(10.31475)); bvec3 cutoff = lessThan(srgb, vec3(10.31475));
@ -41,12 +48,12 @@ vec4 linear_from_srgba(vec4 srgba) {
} }
void main() { void main() {
// We must decode the colors, since WebGL doesn't come with sRGBA textures: // We must decode the colors, since WebGL1 doesn't come with sRGBA textures:
vec4 texture_rgba = linear_from_srgba(texture2D(u_sampler, v_tc) * 255.0); vec4 texture_rgba = linear_from_srgba(texture2D(u_sampler, v_tc) * 255.0);
/// Multiply vertex color with texture color (in linear space). /// Multiply vertex color with texture color (in linear space).
gl_FragColor = v_rgba * texture_rgba; gl_FragColor = v_rgba * texture_rgba;
// WebGL doesn't support linear blending in the framebuffer, // WebGL1 doesn't support linear blending in the framebuffer,
// so we do a hack here where we change the premultiplied alpha // so we do a hack here where we change the premultiplied alpha
// to do the multiplication in gamma space instead: // to do the multiplication in gamma space instead:
@ -65,13 +72,5 @@ void main() {
if (gl_FragColor.a > 0.0) { if (gl_FragColor.a > 0.0) {
gl_FragColor.rgb *= gl_FragColor.a; gl_FragColor.rgb *= gl_FragColor.a;
} }
}
#else
void main() {
// The texture sampler is sRGB aware, and OpenGL already expects linear rgba output
// so no need for any sRGB conversions here:
gl_FragColor = v_rgba * texture2D(u_sampler, v_tc);
} }
#endif #endif

View file

@ -11,6 +11,7 @@
#ifdef GL_ES #ifdef GL_ES
precision mediump float; precision mediump float;
#endif #endif
uniform vec2 u_screen_size; uniform vec2 u_screen_size;
I vec2 a_pos; I vec2 a_pos;
I vec4 a_srgba; // 0-255 sRGB I vec4 a_srgba; // 0-255 sRGB
@ -36,7 +37,7 @@ void main() {
1.0 - 2.0 * a_pos.y / u_screen_size.y, 1.0 - 2.0 * a_pos.y / u_screen_size.y,
0.0, 0.0,
1.0); 1.0);
// egui encodes vertex colors in gamma spaces, so we must decode the colors here: // egui encodes vertex colors in gamma space, so we must decode the colors here:
v_rgba = linear_from_srgba(a_srgba); v_rgba = linear_from_srgba(a_srgba);
v_tc = a_tc; v_tc = a_tc;
} }