Debian Codegraph @

drivers/gles3/shaders/effects/cubemap_filter.glsl

raw
1 /* clang-format off */
2 #[modes]
3
4 mode_default =
5 mode_copy = #define MODE_DIRECT_WRITE
6
7 #[specializations]
8
9 #[vertex]
10
11 layout(location = 0) in highp vec2 vertex_attrib;
12 /* clang-format on */
13
14 out highp vec2 uv_interp;
15
16 void main() {
17 uv_interp = vertex_attrib;
18 gl_Position = vec4(uv_interp, 0.0, 1.0);
19 }
20
21 /* clang-format off */
22 #[fragment]
23
24
25 #define M_PI 3.14159265359
26
27 uniform samplerCube source_cube; //texunit:0
28
29 /* clang-format on */
30
31 uniform int face_id;
32
33 #ifndef MODE_DIRECT_WRITE
34 uniform uint sample_count;
35 uniform vec4 sample_directions_mip[MAX_SAMPLE_COUNT];
36 uniform float weight;
37 #endif
38
39 in highp vec2 uv_interp;
40
41 layout(location = 0) out vec4 frag_color;
42
43 #define M_PI 3.14159265359
44
45 // Don't include tonemap_inc.glsl because all we want is these functions, we don't want the uniforms
46 vec3 linear_to_srgb(vec3 color) {
47 return max(vec3(1.055) * pow(color, vec3(0.416666667)) - vec3(0.055), vec3(0.0));
48 }
49
50 vec3 srgb_to_linear(vec3 color) {
51 return color * (color * (color * 0.305306011 + 0.682171111) + 0.012522878);
52 }
53
54 vec3 texelCoordToVec(vec2 uv, int faceID) {
55 mat3 faceUvVectors[6];
56
57 // -x
58 faceUvVectors[1][0] = vec3(0.0, 0.0, 1.0); // u -> +z
59 faceUvVectors[1][1] = vec3(0.0, -1.0, 0.0); // v -> -y
60 faceUvVectors[1][2] = vec3(-1.0, 0.0, 0.0); // -x face
61
62 // +x
63 faceUvVectors[0][0] = vec3(0.0, 0.0, -1.0); // u -> -z
64 faceUvVectors[0][1] = vec3(0.0, -1.0, 0.0); // v -> -y
65 faceUvVectors[0][2] = vec3(1.0, 0.0, 0.0); // +x face
66
67 // -y
68 faceUvVectors[3][0] = vec3(1.0, 0.0, 0.0); // u -> +x
69 faceUvVectors[3][1] = vec3(0.0, 0.0, -1.0); // v -> -z
70 faceUvVectors[3][2] = vec3(0.0, -1.0, 0.0); // -y face
71
72 // +y
73 faceUvVectors[2][0] = vec3(1.0, 0.0, 0.0); // u -> +x
74 faceUvVectors[2][1] = vec3(0.0, 0.0, 1.0); // v -> +z
75 faceUvVectors[2][2] = vec3(0.0, 1.0, 0.0); // +y face
76
77 // -z
78 faceUvVectors[5][0] = vec3(-1.0, 0.0, 0.0); // u -> -x
79 faceUvVectors[5][1] = vec3(0.0, -1.0, 0.0); // v -> -y
80 faceUvVectors[5][2] = vec3(0.0, 0.0, -1.0); // -z face
81
82 // +z
83 faceUvVectors[4][0] = vec3(1.0, 0.0, 0.0); // u -> +x
84 faceUvVectors[4][1] = vec3(0.0, -1.0, 0.0); // v -> -y
85 faceUvVectors[4][2] = vec3(0.0, 0.0, 1.0); // +z face
86
87 // out = u * s_faceUv[0] + v * s_faceUv[1] + s_faceUv[2].
88 vec3 result = (faceUvVectors[faceID][0] * uv.x) + (faceUvVectors[faceID][1] * uv.y) + faceUvVectors[faceID][2];
89 return normalize(result);
90 }
91
92 void main() {
93 vec3 color = vec3(0.0);
94 vec2 uv = uv_interp;
95 vec3 N = texelCoordToVec(uv, face_id);
96
97 #ifdef MODE_DIRECT_WRITE
98 frag_color = vec4(textureLod(source_cube, N, 0.0).rgb, 1.0);
99 #else
100
101 vec4 sum = vec4(0.0);
102 vec3 UpVector = abs(N.z) < 0.999 ? vec3(0.0, 0.0, 1.0) : vec3(1.0, 0.0, 0.0);
103 mat3 T;
104 T[0] = normalize(cross(UpVector, N));
105 T[1] = cross(N, T[0]);
106 T[2] = N;
107
108 for (uint sample_num = 0u; sample_num < sample_count; sample_num++) {
109 vec4 sample_direction_mip = sample_directions_mip[sample_num];
110 vec3 L = T * sample_direction_mip.xyz;
111 vec3 val = textureLod(source_cube, L, sample_direction_mip.w).rgb;
112 // Mix using linear
113 val = srgb_to_linear(val);
114 sum.rgb += val * sample_direction_mip.z;
115 }
116
117 sum /= weight;
118
119 sum.rgb = linear_to_srgb(sum.rgb);
120 frag_color = vec4(sum.rgb, 1.0);
121 #endif
122 }