git.lucas.co / cce-compositor
Wayland compositor (wlroots)
git clone https://git.lucas.co/cce-compositor.git

scenefx/render/fx_renderer/shaders/blur_effects.frag (1.3K)

 1 #ifdef GL_FRAGMENT_PRECISION_HIGH
 2 precision highp float;
 3 #else
 4 precision mediump float;
 5 #endif
 6 
 7 varying vec2 v_texcoord;
 8 uniform sampler2D tex;
 9 
10 uniform float brightness;
11 uniform float contrast;
12 uniform float saturation;
13 uniform float noise;
14 
15 mat4 brightnessMatrix() {
16 	float b = brightness - 1.0;
17 	return mat4(1, 0, 0, 0,
18 				0, 1, 0, 0,
19 				0, 0, 1, 0,
20 				b, b, b, 1);
21 }
22 
23 mat4 contrastMatrix() {
24 	float t = (1.0 - contrast) / 2.0;
25 	return mat4(contrast, 0, 0, 0,
26 				0, contrast, 0, 0,
27 				0, 0, contrast, 0,
28 				t, t, t, 1);
29 }
30 
31 mat4 saturationMatrix() {
32 	vec3 luminance = vec3(0.3086, 0.6094, 0.0820) * (1.0 - saturation);
33 	vec3 red = vec3(luminance.x);
34 	red.x += saturation;
35 	vec3 green = vec3(luminance.y);
36 	green.y += saturation;
37 	vec3 blue = vec3(luminance.z);
38 	blue.z += saturation;
39 	return mat4(red, 0,
40 				green, 0,
41 				blue, 0,
42 				0, 0, 0, 1);
43 }
44 
45 float noiseAmount(vec2 p) {
46 	vec3 p3 = fract(vec3(p.xyx) * 1689.1984);
47 	p3 += dot(p3, p3.yzx + 33.33);
48 	float hash = fract((p3.x + p3.y) * p3.z);
49 	return (mod(hash, 1.0) - 0.5) * noise;
50 }
51 
52 void main() {
53 	vec4 color = texture2D(tex, v_texcoord);
54 	// Do *not* transpose the combined matrix when multiplying
55 	color = brightnessMatrix() * contrastMatrix() * saturationMatrix() * color;
56 	color.xyz += noiseAmount(v_texcoord);
57 	gl_FragColor = color;
58 }