Skip to content

Commit c1962d2

Browse files
authored
Merge pull request #60 from loqusion/fix/update-glsl-version-3.0-latest
Update shaders to use GLES version 3.0 (latest)
2 parents 93da215 + 25ab126 commit c1962d2

File tree

5 files changed

+82
-66
lines changed

5 files changed

+82
-66
lines changed

shaders/blue-light-filter.glsl.mustache

Lines changed: 22 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,12 @@
66
* Source: https://github.com/hyprwm/Hyprland/issues/1140#issuecomment-1335128437
77
*/
88

9+
#version 300 es
910
precision highp float;
10-
varying vec2 v_texcoord;
11+
12+
in vec2 v_texcoord;
1113
uniform sampler2D tex;
14+
out vec4 fragColor;
1215

1316
/**
1417
* Color temperature in Kelvin.
@@ -34,32 +37,33 @@ const float LuminancePreservationFactor = 1.0;
3437
// valid from 1000 to 40000 K (and additionally 0 for pure full white)
3538
vec3 colorTemperatureToRGB(const in float temperature) {
3639
// values from: http://blenderartists.org/forum/showthread.php?270332-OSL-Goodness&p=2268693&viewfull=1#post2268693
37-
mat3 m = (temperature <= 6500.0) ? mat3(vec3(0.0, -2902.1955373783176, -8257.7997278925690),
38-
vec3(0.0, 1669.5803561666639, 2575.2827530017594),
39-
vec3(1.0, 1.3302673723350029, 1.8993753891711275))
40-
: mat3(vec3(1745.0425298314172, 1216.6168361476490, -8257.7997278925690),
41-
vec3(-2666.3474220535695, -2173.1012343082230, 2575.2827530017594),
42-
vec3(0.55995389139931482, 0.70381203140554553, 1.8993753891711275));
43-
return mix(clamp(vec3(m[0] / (vec3(clamp(temperature, 1000.0, 40000.0)) + m[1]) + m[2]), vec3(0.0), vec3(1.0)),
44-
vec3(1.0), smoothstep(1000.0, 0.0, temperature));
40+
mat3 m = (temperature <= 6500.0)
41+
? mat3(vec3(0.0, -2902.1955373783176, -8257.7997278925690),
42+
vec3(0.0, 1669.5803561666639, 2575.2827530017594),
43+
vec3(1.0, 1.3302673723350029, 1.8993753891711275))
44+
: mat3(vec3(1745.0425298314172, 1216.6168361476490, -8257.7997278925690),
45+
vec3(-2666.3474220535695, -2173.1012343082230, 2575.2827530017594),
46+
vec3(0.55995389139931482, 0.70381203140554553, 1.8993753891711275));
47+
48+
return mix(
49+
clamp(m[0] / (vec3(clamp(temperature, 1000.0, 40000.0)) + m[1]) + m[2], 0.0, 1.0),
50+
vec3(1.0),
51+
smoothstep(1000.0, 0.0, temperature)
52+
);
4553
}
4654

4755
void main() {
48-
vec4 pixColor = texture2D(tex, v_texcoord);
49-
50-
// RGB
51-
vec3 color = vec3(pixColor[0], pixColor[1], pixColor[2]);
56+
vec4 pixColor = texture(tex, v_texcoord);
57+
vec3 color = pixColor.rgb;
5258

5359
#ifdef WithQuickAndDirtyLuminancePreservation
54-
color *= mix(1.0, dot(color, vec3(0.2126, 0.7152, 0.0722)) / max(dot(color, vec3(0.2126, 0.7152, 0.0722)), 1e-5),
55-
LuminancePreservationFactor);
60+
float lum = dot(color, vec3(0.2126, 0.7152, 0.0722));
61+
color *= mix(1.0, lum / max(lum, 1e-5), LuminancePreservationFactor);
5662
#endif
5763

5864
color = mix(color, color * colorTemperatureToRGB(Temperature), Strength);
5965

60-
vec4 outCol = vec4(color, pixColor[3]);
61-
62-
gl_FragColor = outCol;
66+
fragColor = vec4(color, pixColor.a);
6367
}
6468

6569
// vim: ft=glsl

shaders/color-filter.glsl.mustache

Lines changed: 31 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -7,9 +7,12 @@
77
* Source: https://godotshaders.com/shader/colorblindness-correction-shader/
88
*/
99

10+
#version 300 es
1011
precision highp float;
11-
varying vec2 v_texcoord;
12+
13+
in vec2 v_texcoord;
1214
uniform sampler2D tex;
15+
out vec4 fragColor;
1316

1417
/**
1518
* Strength of filter.
@@ -36,40 +39,47 @@ const int BLUEYELLOW = TRITANOPIA;
3639
const int Type = {{#nc}}{{type}} ? PROTANOPIA{{/nc}};
3740

3841
void main() {
39-
vec4 pixColor = texture2D(tex, v_texcoord);
42+
vec4 pixColor = texture(tex, v_texcoord);
4043

41-
float L = (17.8824 * pixColor.r) + (43.5161 * pixColor.g) + (4.11935 * pixColor.b);
42-
float M = (3.45565 * pixColor.r) + (27.1554 * pixColor.g) + (3.86714 * pixColor.b);
43-
float S = (0.0299566 * pixColor.r) + (0.184309 * pixColor.g) + (1.46709 * pixColor.b);
44+
// Convert to LMS color space
45+
float L = 17.8824 * pixColor.r + 43.5161 * pixColor.g + 4.11935 * pixColor.b;
46+
float M = 3.45565 * pixColor.r + 27.1554 * pixColor.g + 3.86714 * pixColor.b;
47+
float S = 0.0299566 * pixColor.r + 0.184309 * pixColor.g + 1.46709 * pixColor.b;
4448

4549
float l, m, s;
50+
4651
if (Type == PROTANOPIA) {
47-
l = 0.0 * L + 2.02344 * M + -2.52581 * S;
48-
m = 0.0 * L + 1.0 * M + 0.0 * S;
49-
s = 0.0 * L + 0.0 * M + 1.0 * S;
52+
l = 2.02344 * M - 2.52581 * S;
53+
m = M;
54+
s = S;
5055
} else if (Type == DEUTERANOPIA) {
51-
l = 1.0 * L + 0.0 * M + 0.0 * S;
52-
m = 0.494207 * L + 0.0 * M + 1.24827 * S;
53-
s = 0.0 * L + 0.0 * M + 1.0 * S;
56+
l = L;
57+
m = 0.494207 * L + 1.24827 * S;
58+
s = S;
5459
} else if (Type == TRITANOPIA) {
55-
l = 1.0 * L + 0.0 * M + 0.0 * S;
56-
m = 0.0 * L + 1.0 * M + 0.0 * S;
57-
s = -0.395913 * L + 0.801109 * M + 0.0 * S;
60+
l = L;
61+
m = M;
62+
s = -0.395913 * L + 0.801109 * M;
5863
}
5964

65+
// Convert back to RGB
6066
vec4 error;
61-
error.r = (0.0809444479 * l) + (-0.130504409 * m) + (0.116721066 * s);
62-
error.g = (-0.0102485335 * l) + (0.0540193266 * m) + (-0.113614708 * s);
63-
error.b = (-0.000365296938 * l) + (-0.00412161469 * m) + (0.693511405 * s);
67+
error.r = 0.0809444479 * l + -0.130504409 * m + 0.116721066 * s;
68+
error.g = -0.0102485335 * l + 0.0540193266 * m - 0.113614708 * s;
69+
error.b = -0.000365296938 * l - 0.00412161469 * m + 0.693511405 * s;
6470
error.a = 1.0;
71+
6572
vec4 diff = pixColor - error;
73+
74+
// Apply correction
6675
vec4 correction;
6776
correction.r = 0.0;
68-
correction.g = (diff.r * 0.7) + (diff.g * 1.0);
69-
correction.b = (diff.r * 0.7) + (diff.b * 1.0);
70-
correction = mix(pixColor, pixColor + correction, Strength);
77+
correction.g = diff.r * 0.7 + diff.g;
78+
correction.b = diff.r * 0.7 + diff.b;
79+
80+
vec4 result = mix(pixColor, pixColor + correction, Strength);
7181

72-
gl_FragColor = vec4(correction);
82+
fragColor = vec4(result.rgb, pixColor.a);
7383
}
7484

7585
// vim: ft=glsl

shaders/grayscale.glsl.mustache

Lines changed: 11 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,12 @@
22
* Grayscale
33
*/
44

5+
#version 300 es
56
precision highp float;
6-
varying vec2 v_texcoord;
7+
8+
in vec2 v_texcoord;
79
uniform sampler2D tex;
10+
out vec4 fragColor;
811

912
// Enum for type of grayscale conversion
1013
const int LUMINOSITY = 0;
@@ -23,14 +26,14 @@ const int HDR = 2;
2326

2427
/**
2528
* Formula used to calculate relative luminance.
26-
* (Only applies to type = "luminosity".)
29+
* (Only applies when type = "luminosity".)
2730
*/
2831
const int LuminosityType = {{#nc}}{{luminosity_type}} ? HDR{{/nc}};
2932

3033
void main() {
31-
vec4 pixColor = texture2D(tex, v_texcoord);
34+
vec4 pixColor = texture(tex, v_texcoord);
35+
float gray = 0.0;
3236

33-
float gray;
3437
if (Type == LUMINOSITY) {
3538
// https://en.wikipedia.org/wiki/Grayscale#Luma_coding_in_video_systems
3639
if (LuminosityType == PAL) {
@@ -41,15 +44,14 @@ void main() {
4144
gray = dot(pixColor.rgb, vec3(0.2627, 0.6780, 0.0593));
4245
}
4346
} else if (Type == LIGHTNESS) {
44-
float maxPixColor = max(pixColor.r, max(pixColor.g, pixColor.b));
45-
float minPixColor = min(pixColor.r, min(pixColor.g, pixColor.b));
46-
gray = (maxPixColor + minPixColor) / 2.0;
47+
float maxColor = max(pixColor.r, max(pixColor.g, pixColor.b));
48+
float minColor = min(pixColor.r, min(pixColor.g, pixColor.b));
49+
gray = (maxColor + minColor) / 2.0;
4750
} else if (Type == AVERAGE) {
4851
gray = (pixColor.r + pixColor.g + pixColor.b) / 3.0;
4952
}
50-
vec3 grayscale = vec3(gray);
5153

52-
gl_FragColor = vec4(grayscale, pixColor.a);
54+
fragColor = vec4(vec3(gray), pixColor.a);
5355
}
5456

5557
// vim: ft=glsl

shaders/invert-colors.glsl

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,11 @@
1+
#version 300 es
12
precision highp float;
2-
varying vec2 v_texcoord;
3+
4+
in vec2 v_texcoord;
35
uniform sampler2D tex;
6+
out vec4 fragColor;
47

58
void main() {
6-
vec4 pixColor = texture2D(tex, v_texcoord);
7-
gl_FragColor = vec4(1.0 - pixColor.r, 1.0 - pixColor.g, 1.0 - pixColor.b, pixColor.a);
9+
vec4 pixColor = texture(tex, v_texcoord);
10+
fragColor = vec4(1.0 - pixColor.rgb, pixColor.a);
811
}

shaders/vibrance.glsl.mustache

Lines changed: 12 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -7,9 +7,12 @@
77
* Source: https://github.com/hyprwm/Hyprland/issues/1140#issuecomment-1614863627
88
*/
99

10+
#version 300 es
1011
precision highp float;
11-
varying vec2 v_texcoord;
12+
13+
in vec2 v_texcoord;
1214
uniform sampler2D tex;
15+
out vec4 fragColor;
1316

1417
// see https://github.com/CeeJayDK/SweetFX/blob/a792aee788c6203385a858ebdea82a77f81c67f0/Shaders/Vibrance.fx#L20-L30
1518

@@ -37,26 +40,20 @@ const float Strength = float({{#nc}}{{strength}} ? 0.15{{/nc}});
3740
const vec3 VIB_coeffVibrance = Balance * -Strength;
3841

3942
void main() {
40-
vec4 pixColor = texture2D(tex, v_texcoord);
41-
vec3 color = vec3(pixColor[0], pixColor[1], pixColor[2]);
42-
43-
// vec3 VIB_coefLuma = vec3(0.333333, 0.333334, 0.333333); // was for `if VIB_LUMA == 1`
44-
vec3 VIB_coefLuma = vec3(0.212656, 0.715158, 0.072186); // try both and see which one looks nicer.
43+
vec4 pixColor = texture(tex, v_texcoord);
44+
vec3 color = pixColor.rgb;
4545

46+
vec3 VIB_coefLuma = vec3(0.212656, 0.715158, 0.072186);
4647
float luma = dot(VIB_coefLuma, color);
4748

48-
float max_color = max(color[0], max(color[1], color[2]));
49-
float min_color = min(color[0], min(color[1], color[2]));
50-
49+
float max_color = max(color.r, max(color.g, color.b));
50+
float min_color = min(color.r, min(color.g, color.b));
5151
float color_saturation = max_color - min_color;
5252

53-
vec3 p_col = vec3(vec3(vec3(vec3(sign(VIB_coeffVibrance) * color_saturation) - 1.0) * VIB_coeffVibrance) + 1.0);
54-
55-
pixColor[0] = mix(luma, color[0], p_col[0]);
56-
pixColor[1] = mix(luma, color[1], p_col[1]);
57-
pixColor[2] = mix(luma, color[2], p_col[2]);
53+
vec3 p_col = (sign(VIB_coeffVibrance) * color_saturation - 1.0) * VIB_coeffVibrance + 1.0;
5854

59-
gl_FragColor = pixColor;
55+
vec3 adjustedColor = mix(vec3(luma), color, p_col);
56+
fragColor = vec4(adjustedColor, pixColor.a);
6057
}
6158

6259
// vim: ft=glsl

0 commit comments

Comments
 (0)