Debian Codegraph @

servers/rendering/renderer_rd/shaders/giprobe_write.glsl

raw
1 #[compute]
2
3 #version 450
4
5 #VERSION_DEFINES
6
7 layout(local_size_x = 64, local_size_y = 1, local_size_z = 1) in;
8
9 #define NO_CHILDREN 0xFFFFFFFF
10
11 struct CellChildren {
12 uint children[8];
13 };
14
15 layout(set = 0, binding = 1, std430) buffer CellChildrenBuffer {
16 CellChildren data[];
17 }
18 cell_children;
19
20 struct CellData {
21 uint position; // xyz 10 bits
22 uint albedo; //rgb albedo
23 uint emission; //rgb normalized with e as multiplier
24 uint normal; //RGB normal encoded
25 };
26
27 layout(set = 0, binding = 2, std430) buffer CellDataBuffer {
28 CellData data[];
29 }
30 cell_data;
31
32 #define LIGHT_TYPE_DIRECTIONAL 0
33 #define LIGHT_TYPE_OMNI 1
34 #define LIGHT_TYPE_SPOT 2
35
36 #ifdef MODE_COMPUTE_LIGHT
37
38 struct Light {
39 uint type;
40 float energy;
41 float radius;
42 float attenuation;
43
44 vec3 color;
45 float cos_spot_angle;
46
47 vec3 position;
48 float inv_spot_attenuation;
49
50 vec3 direction;
51 bool has_shadow;
52 };
53
54 layout(set = 0, binding = 3, std140) uniform Lights {
55 Light data[MAX_LIGHTS];
56 }
57 lights;
58
59 #endif
60
61 layout(push_constant, std430) uniform Params {
62 ivec3 limits;
63 uint stack_size;
64
65 float emission_scale;
66 float propagation;
67 float dynamic_range;
68
69 uint light_count;
70 uint cell_offset;
71 uint cell_count;
72 uint pad[2];
73 }
74 params;
75
76 layout(set = 0, binding = 4, std140) uniform Outputs {
77 vec4 data[];
78 }
79 output;
80
81 #ifdef MODE_COMPUTE_LIGHT
82
83 uint raymarch(float distance, float distance_adv, vec3 from, vec3 direction) {
84 uint result = NO_CHILDREN;
85
86 ivec3 size = ivec3(max(max(params.limits.x, params.limits.y), params.limits.z));
87
88 while (distance > -distance_adv) { //use this to avoid precision errors
89 uint cell = 0;
90
91 ivec3 pos = ivec3(from);
92
93 if (all(greaterThanEqual(pos, ivec3(0))) && all(lessThan(pos, size))) {
94 ivec3 ofs = ivec3(0);
95 ivec3 half_size = size / 2;
96
97 for (int i = 0; i < params.stack_size - 1; i++) {
98 bvec3 greater = greaterThanEqual(pos, ofs + half_size);
99
100 ofs += mix(ivec3(0), half_size, greater);
101
102 uint child = 0; //wonder if this can be done faster
103 if (greater.x) {
104 child |= 1;
105 }
106 if (greater.y) {
107 child |= 2;
108 }
109 if (greater.z) {
110 child |= 4;
111 }
112
113 cell = cell_children.data[cell].children[child];
114 if (cell == NO_CHILDREN) {
115 break;
116 }
117
118 half_size >>= ivec3(1);
119 }
120
121 if (cell != NO_CHILDREN) {
122 return cell; //found cell!
123 }
124 }
125
126 from += direction * distance_adv;
127 distance -= distance_adv;
128 }
129
130 return NO_CHILDREN;
131 }
132
133 bool compute_light_vector(uint light, uint cell, vec3 pos, out float attenuation, out vec3 light_pos) {
134 if (lights.data[light].type == LIGHT_TYPE_DIRECTIONAL) {
135 light_pos = pos - lights.data[light].direction * length(vec3(params.limits));
136 attenuation = 1.0;
137 } else {
138 light_pos = lights.data[light].position;
139 float distance = length(pos - light_pos);
140 if (distance >= lights.data[light].radius) {
141 return false;
142 }
143
144 attenuation = pow(clamp(1.0 - distance / lights.data[light].radius, 0.0001, 1.0), lights.data[light].attenuation);
145
146 if (lights.data[light].type == LIGHT_TYPE_SPOT) {
147 vec3 rel = normalize(pos - light_pos);
148 float cos_spot_angle = lights.data[light].cos_spot_angle;
149 float cos_angle = dot(rel, lights.data[light].direction);
150 if (cos_angle < cos_spot_angle) {
151 return false;
152 }
153
154 float scos = max(cos_angle, cos_spot_angle);
155 float spot_rim = max(0.0001, (1.0 - scos) / (1.0 - cos_spot_angle));
156 attenuation *= 1.0 - pow(spot_rim, lights.data[light].inv_spot_attenuation);
157 }
158 }
159
160 return true;
161 }
162
163 float get_normal_advance(vec3 p_normal) {
164 vec3 normal = p_normal;
165 vec3 unorm = abs(normal);
166
167 if ((unorm.x >= unorm.y) && (unorm.x >= unorm.z)) {
168 // x code
169 unorm = normal.x > 0.0 ? vec3(1.0, 0.0, 0.0) : vec3(-1.0, 0.0, 0.0);
170 } else if ((unorm.y > unorm.x) && (unorm.y >= unorm.z)) {
171 // y code
172 unorm = normal.y > 0.0 ? vec3(0.0, 1.0, 0.0) : vec3(0.0, -1.0, 0.0);
173 } else if ((unorm.z > unorm.x) && (unorm.z > unorm.y)) {
174 // z code
175 unorm = normal.z > 0.0 ? vec3(0.0, 0.0, 1.0) : vec3(0.0, 0.0, -1.0);
176 } else {
177 // oh-no we messed up code
178 // has to be
179 unorm = vec3(1.0, 0.0, 0.0);
180 }
181
182 return 1.0 / dot(normal, unorm);
183 }
184
185 #endif
186
187 void main() {
188 uint cell_index = gl_GlobalInvocationID.x;
189 if (cell_index >= params.cell_count) {
190 return;
191 }
192 cell_index += params.cell_offset;
193
194 uvec3 posu = uvec3(cell_data.data[cell_index].position & 0x7FF, (cell_data.data[cell_index].position >> 11) & 0x3FF, cell_data.data[cell_index].position >> 21);
195 vec4 albedo = unpackUnorm4x8(cell_data.data[cell_index].albedo);
196
197 #ifdef MODE_COMPUTE_LIGHT
198
199 vec3 pos = vec3(posu) + vec3(0.5);
200
201 vec3 emission = vec3(ivec3(cell_data.data[cell_index].emission & 0x3FF, (cell_data.data[cell_index].emission >> 10) & 0x7FF, cell_data.data[cell_index].emission >> 21)) * params.emission_scale;
202 vec4 normal = unpackSnorm4x8(cell_data.data[cell_index].normal);
203
204 vec3 accum = vec3(0.0);
205
206 for (uint i = 0; i < params.light_count; i++) {
207 float attenuation;
208 vec3 light_pos;
209
210 if (!compute_light_vector(i, cell_index, pos, attenuation, light_pos)) {
211 continue;
212 }
213
214 vec3 light_dir = pos - light_pos;
215 float distance = length(light_dir);
216 light_dir = normalize(light_dir);
217
218 if (length(normal.xyz) > 0.2 && dot(normal.xyz, light_dir) >= 0) {
219 continue; //not facing the light
220 }
221
222 if (lights.data[i].has_shadow) {
223 float distance_adv = get_normal_advance(light_dir);
224
225 distance += distance_adv - mod(distance, distance_adv); //make it reach the center of the box always
226
227 vec3 from = pos - light_dir * distance; //approximate
228 from -= sign(light_dir) * 0.45; //go near the edge towards the light direction to avoid self occlusion
229
230 uint result = raymarch(distance, distance_adv, from, light_dir);
231
232 if (result != cell_index) {
233 continue; //was occluded
234 }
235 }
236
237 vec3 light = lights.data[i].color * albedo.rgb * attenuation * lights.data[i].energy;
238
239 if (length(normal.xyz) > 0.2) {
240 accum += max(0.0, dot(normal.xyz, -light_dir)) * light + emission;
241 } else {
242 //all directions
243 accum += light + emission;
244 }
245 }
246
247 output.data[cell_index] = vec4(accum, 0.0);
248
249 #endif //MODE_COMPUTE_LIGHT
250
251 #ifdef MODE_UPDATE_MIPMAPS
252
253 {
254 vec3 light_accum = vec3(0.0);
255 float count = 0.0;
256 for (uint i = 0; i < 8; i++) {
257 uint child_index = cell_children.data[cell_index].children[i];
258 if (child_index == NO_CHILDREN) {
259 continue;
260 }
261 light_accum += output.data[child_index].rgb;
262
263 count += 1.0;
264 }
265
266 float divisor = mix(8.0, count, params.propagation);
267 output.data[cell_index] = vec4(light_accum / divisor, 0.0);
268 }
269 #endif
270
271 #ifdef MODE_WRITE_TEXTURE
272 {
273 }
274 #endif
275 }