Debian Codegraph @

servers/rendering/renderer_rd/shaders/effects/octmap_roughness_raster.glsl

raw
1 /* clang-format off */
2 #[vertex]
3
4 #version 450
5
6 #VERSION_DEFINES
7
8 #include "octmap_roughness_inc.glsl"
9
10 layout(location = 0) out vec2 uv_interp;
11 /* clang-format on */
12
13 void main() {
14 // old code, ARM driver bug on Mali-GXXx GPUs and Vulkan API 1.3.xxx
15 // https://github.com/godotengine/godot/pull/92817#issuecomment-2168625982
16 //vec2 base_arr[3] = vec2[](vec2(-1.0, -1.0), vec2(-1.0, 3.0), vec2(3.0, -1.0));
17 //gl_Position = vec4(base_arr[gl_VertexIndex], 0.0, 1.0);
18 //uv_interp = clamp(gl_Position.xy, vec2(0.0, 0.0), vec2(1.0, 1.0)) * 2.0; // saturate(x) * 2.0
19
20 vec2 vertex_base;
21 if (gl_VertexIndex == 0) {
22 vertex_base = vec2(-1.0, -1.0);
23 } else if (gl_VertexIndex == 1) {
24 vertex_base = vec2(-1.0, 3.0);
25 } else {
26 vertex_base = vec2(3.0, -1.0);
27 }
28 gl_Position = vec4(vertex_base, 0.0, 1.0);
29 uv_interp = clamp(vertex_base, vec2(0.0, 0.0), vec2(1.0, 1.0)) * 2.0; // saturate(x) * 2.0
30 }
31
32 /* clang-format off */
33 #[fragment]
34
35 #version 450
36
37 #VERSION_DEFINES
38
39 #include "../oct_inc.glsl"
40 #include "octmap_roughness_inc.glsl"
41
42 layout(location = 0) in vec2 uv_interp;
43
44 layout(set = 0, binding = 0) uniform sampler2D source_oct;
45
46 layout(location = 0) out vec4 frag_color;
47 /* clang-format on */
48
49 void main() {
50 if (params.use_direct_write) {
51 frag_color = vec4(texture(source_oct, uv_interp).rgb, 1.0);
52 } else {
53 vec3 N = oct_to_vec3_with_border(uv_interp, params.border_size.y);
54 vec4 sum = vec4(0.0, 0.0, 0.0, 0.0);
55 float solid_angle_texel = 4.0 * M_PI / (params.dest_size * params.dest_size);
56 float roughness2 = params.roughness * params.roughness;
57 float roughness4 = roughness2 * roughness2;
58
59 vec3 UpVector = abs(N.y) < 0.999 ? vec3(0.0, 1.0, 0.0) : vec3(0.0, 0.0, 1.0);
60 mat3 T;
61 T[0] = normalize(cross(UpVector, N));
62 T[1] = cross(N, T[0]);
63 T[2] = N;
64
65 for (uint sampleNum = 0u; sampleNum < params.sample_count; sampleNum++) {
66 vec2 xi = Hammersley(sampleNum, params.sample_count);
67
68 vec3 H = T * ImportanceSampleGGX(xi, roughness4);
69 float NdotH = dot(N, H);
70 vec3 L = (2.0 * NdotH * H - N);
71
72 float ndotl = clamp(dot(N, L), 0.0, 1.0);
73
74 if (ndotl > 0.0) {
75 float D = DistributionGGX(NdotH, roughness4);
76 float pdf = D * NdotH / (4.0 * NdotH) + 0.0001;
77
78 float solid_angle_sample = 1.0 / (float(params.sample_count) * pdf + 0.0001);
79
80 float mipLevel = params.roughness == 0.0 ? 0.0 : 0.5 * log2(solid_angle_sample / solid_angle_texel);
81
82 vec2 sample_uv = vec3_to_oct_with_border(L, params.border_size);
83 sum.rgb += textureLod(source_oct, sample_uv, mipLevel).rgb * ndotl;
84 sum.a += ndotl;
85 }
86 }
87
88 frag_color = vec4(sum.rgb / sum.a, 1.0);
89 }
90 }