Debian Codegraph @

servers/rendering/renderer_rd/shaders/environment/sdfgi_debug_probes.glsl

raw
1 #[vertex]
2
3 #version 450
4
5 #ifdef USE_MULTIVIEW
6 #extension GL_EXT_multiview : enable
7 #define ViewIndex gl_ViewIndex
8 #else // USE_MULTIVIEW
9 #define ViewIndex 0
10 #endif // !USE_MULTIVIEW
11
12 #VERSION_DEFINES
13
14 #define MAX_CASCADES 8
15 #define MAX_VIEWS 2
16
17 layout(push_constant, std430) uniform Params {
18 uint band_power;
19 uint sections_in_band;
20 uint band_mask;
21 float section_arc;
22
23 vec3 grid_size;
24 uint cascade;
25
26 uint pad;
27 float y_mult;
28 uint probe_debug_index;
29 int probe_axis_size;
30 }
31 params;
32
33 // https://in4k.untergrund.net/html_articles/hugi_27_-_coding_corner_polaris_sphere_tessellation_101.htm
34
35 vec3 get_sphere_vertex(uint p_vertex_id) {
36 float x_angle = float(p_vertex_id & 1u) + (p_vertex_id >> params.band_power);
37
38 float y_angle =
39 float((p_vertex_id & params.band_mask) >> 1) + ((p_vertex_id >> params.band_power) * params.sections_in_band);
40
41 x_angle *= params.section_arc * 0.5f; // remember - 180AA x rot not 360
42 y_angle *= -params.section_arc;
43
44 vec3 point = vec3(sin(x_angle) * sin(y_angle), cos(x_angle), sin(x_angle) * cos(y_angle));
45
46 return point;
47 }
48
49 #ifdef MODE_PROBES
50
51 layout(location = 0) out vec3 normal_interp;
52 layout(location = 1) out flat uint probe_index;
53
54 #endif
55
56 #ifdef MODE_VISIBILITY
57
58 layout(location = 0) out float visibility;
59
60 #endif
61
62 struct CascadeData {
63 vec3 offset; //offset of (0,0,0) in world coordinates
64 float to_cell; // 1/bounds * grid_size
65 ivec3 probe_world_offset;
66 uint pad;
67 vec4 pad2;
68 };
69
70 layout(set = 0, binding = 1, std140) uniform Cascades {
71 CascadeData data[MAX_CASCADES];
72 }
73 cascades;
74
75 layout(set = 0, binding = 4) uniform texture3D occlusion_texture;
76 layout(set = 0, binding = 3) uniform sampler linear_sampler;
77
78 layout(set = 0, binding = 5, std140) uniform SceneData {
79 mat4 projection[MAX_VIEWS];
80 }
81 scene_data;
82
83 void main() {
84 #ifdef MODE_PROBES
85 probe_index = gl_InstanceIndex;
86
87 normal_interp = get_sphere_vertex(gl_VertexIndex);
88
89 vec3 vertex = normal_interp * 0.2;
90
91 float probe_cell_size = float(params.grid_size / float(params.probe_axis_size - 1)) / cascades.data[params.cascade].to_cell;
92
93 ivec3 probe_cell;
94 probe_cell.x = int(probe_index % params.probe_axis_size);
95 probe_cell.y = int(probe_index / (params.probe_axis_size * params.probe_axis_size));
96 probe_cell.z = int((probe_index / params.probe_axis_size) % params.probe_axis_size);
97
98 vertex += (cascades.data[params.cascade].offset + vec3(probe_cell) * probe_cell_size) / vec3(1.0, params.y_mult, 1.0);
99
100 gl_Position = scene_data.projection[ViewIndex] * vec4(vertex, 1.0);
101 #endif
102
103 #ifdef MODE_VISIBILITY
104
105 int probe_index = int(params.probe_debug_index);
106
107 vec3 vertex = get_sphere_vertex(gl_VertexIndex) * 0.01;
108
109 float probe_cell_size = float(params.grid_size / float(params.probe_axis_size - 1)) / cascades.data[params.cascade].to_cell;
110
111 ivec3 probe_cell;
112 probe_cell.x = int(probe_index % params.probe_axis_size);
113 probe_cell.y = int((probe_index % (params.probe_axis_size * params.probe_axis_size)) / params.probe_axis_size);
114 probe_cell.z = int(probe_index / (params.probe_axis_size * params.probe_axis_size));
115
116 vertex += (cascades.data[params.cascade].offset + vec3(probe_cell) * probe_cell_size) / vec3(1.0, params.y_mult, 1.0);
117
118 int probe_voxels = int(params.grid_size.x) / int(params.probe_axis_size - 1);
119 int occluder_index = int(gl_InstanceIndex);
120
121 int diameter = probe_voxels * 2;
122 ivec3 occluder_pos;
123 occluder_pos.x = int(occluder_index % diameter);
124 occluder_pos.y = int(occluder_index / (diameter * diameter));
125 occluder_pos.z = int((occluder_index / diameter) % diameter);
126
127 float cell_size = 1.0 / cascades.data[params.cascade].to_cell;
128
129 ivec3 occluder_offset = occluder_pos - ivec3(diameter / 2);
130 vertex += ((vec3(occluder_offset) + vec3(0.5)) * cell_size) / vec3(1.0, params.y_mult, 1.0);
131
132 ivec3 global_cell = probe_cell + cascades.data[params.cascade].probe_world_offset;
133 uint occlusion_layer = 0;
134 if ((global_cell.x & 1) != 0) {
135 occlusion_layer |= 1;
136 }
137 if ((global_cell.y & 1) != 0) {
138 occlusion_layer |= 2;
139 }
140 if ((global_cell.z & 1) != 0) {
141 occlusion_layer |= 4;
142 }
143 ivec3 tex_pos = probe_cell * probe_voxels + occluder_offset;
144
145 const vec4 layer_axis[4] = vec4[](
146 vec4(1, 0, 0, 0),
147 vec4(0, 1, 0, 0),
148 vec4(0, 0, 1, 0),
149 vec4(0, 0, 0, 1));
150
151 tex_pos.z += int(params.cascade) * int(params.grid_size);
152 if (occlusion_layer >= 4) {
153 tex_pos.x += int(params.grid_size.x);
154 occlusion_layer &= 3;
155 }
156
157 visibility = dot(texelFetch(sampler3D(occlusion_texture, linear_sampler), tex_pos, 0), layer_axis[occlusion_layer]);
158
159 gl_Position = scene_data.projection[ViewIndex] * vec4(vertex, 1.0);
160
161 #endif
162 }
163
164 #[fragment]
165
166 #version 450
167
168 #VERSION_DEFINES
169
170 #define MAX_VIEWS 2
171
172 layout(location = 0) out vec4 frag_color;
173
174 layout(set = 0, binding = 2) uniform texture2DArray lightprobe_texture;
175 layout(set = 0, binding = 3) uniform sampler linear_sampler;
176
177 layout(push_constant, std430) uniform Params {
178 uint band_power;
179 uint sections_in_band;
180 uint band_mask;
181 float section_arc;
182
183 vec3 grid_size;
184 uint cascade;
185
186 uint pad;
187 float y_mult;
188 uint probe_debug_index;
189 int probe_axis_size;
190 }
191 params;
192
193 #ifdef MODE_PROBES
194
195 layout(location = 0) in vec3 normal_interp;
196 layout(location = 1) in flat uint probe_index;
197
198 #endif
199
200 #ifdef MODE_VISIBILITY
201 layout(location = 0) in float visibility;
202 #endif
203
204 vec2 octahedron_wrap(vec2 v) {
205 vec2 signVal;
206 signVal.x = v.x >= 0.0 ? 1.0 : -1.0;
207 signVal.y = v.y >= 0.0 ? 1.0 : -1.0;
208 return (1.0 - abs(v.yx)) * signVal;
209 }
210
211 vec2 octahedron_encode(vec3 n) {
212 // https://twitter.com/Stubbesaurus/status/937994790553227264
213 n /= (abs(n.x) + abs(n.y) + abs(n.z));
214 n.xy = n.z >= 0.0 ? n.xy : octahedron_wrap(n.xy);
215 n.xy = n.xy * 0.5 + 0.5;
216 return n.xy;
217 }
218
219 void main() {
220 #ifdef MODE_PROBES
221
222 ivec3 tex_pos;
223 tex_pos.x = int(probe_index) % params.probe_axis_size; //x
224 tex_pos.y = int(probe_index) / (params.probe_axis_size * params.probe_axis_size);
225 tex_pos.x += params.probe_axis_size * ((int(probe_index) / params.probe_axis_size) % params.probe_axis_size); //z
226 tex_pos.z = int(params.cascade);
227
228 vec3 tex_pos_ofs = vec3(octahedron_encode(normal_interp) * float(OCT_SIZE), 0.0);
229 vec3 tex_posf = vec3(vec2(tex_pos.xy * (OCT_SIZE + 2) + ivec2(1)), float(tex_pos.z)) + tex_pos_ofs;
230
231 tex_posf.xy /= vec2(ivec2(params.probe_axis_size * params.probe_axis_size * (OCT_SIZE + 2), params.probe_axis_size * (OCT_SIZE + 2)));
232
233 vec4 indirect_light = textureLod(sampler2DArray(lightprobe_texture, linear_sampler), tex_posf, 0.0);
234
235 frag_color = indirect_light;
236
237 #endif
238
239 #ifdef MODE_VISIBILITY
240
241 frag_color = vec4(vec3(1, visibility, visibility), 1.0);
242 #endif
243 }