Untitled
Loading...
Searching...
No Matches
audio_probe_bake.comp
Go to the documentation of this file.
1#version 430 core
2
3#extension GL_ARB_bindless_texture : require
4layout (local_size_x = 16, local_size_y = 16) in;
5
6
7#define SAMPLE_RATE 48000
8#define SPEED_OF_SOUND 343.0f
9#define MAX_SAMPLES (SAMPLE_RATE * 1)
10#define NUM_RAYS 100000
11
42
43layout(std430, binding = 2) buffer MaterialData {
44 Material materials[];
45};
46
47
48
49struct Vertex {
50
52 uint pad0;
54 uint pad1;
56 uint pad2;
57 uint pad3;
58
59};
60
61layout(std430, binding = 4) buffer VertexData {
62 Vertex vertices[];
63};
64
80
81layout(std430, binding = 5) buffer TriData {
82 Face tris[];
83};
84
105
106layout(std430, binding = 6) buffer MeshData {
107 MeshNode mesh_nodes[];
108};
109
110struct AABB
111{
112
114 uint pad0;
116 uint pad1;
118 uint pad2;
120 uint pad3;
121
122};
123
124
126{
127
128 int left;
129 int right;
131 int index;
132
134 int count;
135 int next;
136 uint pad0;
137
139 uint pad1;
141 uint pad2;
143 uint pad3;
145 uint pad4;
146
147};
148
149layout(std430, binding = 7) buffer MeshBVHData {
150 BVHNode mesh_bvh_nodes[];
151};
152
153layout(std430, binding = 8) buffer SceneBVHData {
154 BVHNode scene_bvh_nodes[];
155};
157{
158
160 uint pad0;
161
162};
163
164layout(std430, binding = 9) buffer AProbeData {
165 AudioProbe aprobes[];
166};
167
169{
173 uint valid;
174 float energy[6];
175 uint pad0;
176 uint pad1;
177 float abs_energy[6];
178 uint pad4;
179 uint pad5;
180
181};
182
183
184layout(std430, binding = 10) buffer BounceData {
186};
187
188uint state = 0;
189
190uint pcg_hash() {
191 state = state * 747796405u + 2891336453u;
192 uint word = ((state >> ((state >> 28u) + 4u)) ^ state) * 277803737u;
193 return (word >> 22u) ^ word;
194}
195
196
197uint wang_hash(uint seed) {
198 seed = (seed ^ 61u) ^ (seed >> 16u);
199 seed *= 9u;
200 seed = seed ^ (seed >> 4u);
201 seed *= 0x27d4eb2du;
202 seed = seed ^ (seed >> 15u);
203 return seed;
204}
205
206float rand() {
207 return float(pcg_hash()) / 4294967296.0;
208}
209void frisvad_basis(in vec3 n, out vec3 b1, out vec3 b2) {
210 if (n.z < -0.99998796f) {
211 b1.x = 0.0f; b1.y = -1.0f; b1.z = 0.0f;
212 b2.x = -1.0f; b2.y = 0.0f; b2.z = 0.0f;
213 return;
214 }
215
216 float a = 1.0f / (1.0f + n.z);
217 float b = -n.x * n.y * a;
218
219 b1.x = 1.0f - n.x * n.x * a;
220 b1.y = b;
221 b1.z = -n.x;
222
223 b2.x = b;
224 b2.y = 1.0f - n.y * n.y * a;
225 b2.z = -n.y;
226}
227
229 float r1 = rand();
230 float r2 = rand();
231
232 float phi = 2.0 * 3.14159265 * r1;
233 float x = cos(phi) * sqrt(r2);
234 float y = sin(phi) * sqrt(r2);
235 float z = sqrt(1.0 - r2);
236 vec3 local_dir = vec3(x, y, z);
237
238 vec3 tangent;
239 vec3 bitangent;
240 if (abs(normal.z) > 0.999)
241 {
242
243 frisvad_basis(normal, tangent, bitangent);
244 }
245 else
246 {
247
248 vec3 up = abs(normal.z) < 0.999 ? vec3(0.0, 0.0, 1.0) : vec3(1.0, 0.0, 0.0);
249 tangent = normalize(cross(up, normal));
250 bitangent = cross(normal, tangent);
251 }
252
253 mat3 TBN = mat3(tangent, bitangent, normal);
254
255 return normalize(TBN * local_dir);
256}
257
259{
260
261 float phi = 3.14159265 * (3.0f - sqrt(5.0f));
262
263 float y = 1.0f - 2.0f*(i+0.5f)/N;
264
265 float r = sqrt(1-y*y);
266
267 float theta = phi*i;
268
269 return vec3(cos(theta) * r, y, sin(theta) * r);
270}
271struct RayHit
272{
274 float t;
276 uint mn_idx;
279 float best_t;
281 bool hit;
283 uint pad1;
284
285};
286
288
290{
291
292 RayHit out_hit;
293
294 out_hit.hit = false;
295
296 vec3 edge1 = b-a;
297 vec3 edge2 = c-a;
298 vec3 h = cross(dir, edge2);
299 float dA = dot(edge1, h);
300
301 if (abs(dA) < 0.000001f) {
302 return out_hit;
303 }
304
305 float f = 1.0f / dA;
306 vec3 s = origin - a;
307 float u = f * dot(s, h);
308 if (u < 0.0f || u > 1.0f) {
309 return out_hit;
310 }
311
312 vec3 q = cross(s, edge1);
313 float v = f * dot(dir, q);
314 if (v < 0.0f || u + v > 1.0f) {
315 return out_hit;
316 }
317
318 float t = f * dot(edge2, q);
319 if (t > 0.0) {
320 out_hit.t = t;
321
322 float w = 1.0f - u - v;
323 out_hit.pos = a * w + b * u + c * v;
324 out_hit.barycentrics = vec3(w, u, v);
325 out_hit.hit = true;
326
327 return out_hit;
328 }
329
330 return out_hit;
331}
332
333bool slab_test(BVHNode n, vec3 pos, vec3 inv_dir) {
334 vec3 t1 = (n.min - pos) * inv_dir;
335 vec3 t2 = (n.max - pos) * inv_dir;
336
337 vec3 tmin = min(t1, t2);
338 vec3 tmax = max(t1, t2);
339
340 float t_near = max(max(tmin.x, tmin.y), tmin.z);
341 float t_far = min(min(tmax.x, tmax.y), tmax.z);
342 float closest_valid_t = min(hit.t, hit.blocked_t);
343 return (t_far >= max(0.0f, t_near) && t_near < closest_valid_t);
344}
345
346void traverse_mesh_bvh(uint node_offset, uint mn_id, vec3 pp, vec3 pm, vec3 original_dir, vec3 dir, vec3 md, uint mesh, uint tri)
347{
348 int ni = int(node_offset);
349
350 while (ni >= 0)
351 {
352 BVHNode n = mesh_bvh_nodes[ni];
353
354
355 if (!slab_test(n, pm, md))
356 {
357 ni = n.next;
358 continue;
359 }
360
361 if (n.left == -1 && n.right == -1)
362 {
363 for (int i = 0; i < n.count; i++)
364 {
365
366 uint tri_idx = tris[i + n.first_index].bvh_index;
367 Face f = tris[tri_idx];
368
369 vec3 a = f.v0.pos;
370 vec3 b = f.v1.pos;
371 vec3 c = f.v2.pos;
372
373 RayHit current_hit = ray_intersects_triangle(pm, dir, a, b, c);
374 if (!current_hit.hit) continue;
375
376
377 bool col = (current_hit.t < hit.t);
378
379 bool dot_test = dot(f.normal, dir) <= 0.0f;
380 if (dot_test)
381 {
382
383 if (col)
384 {
385
386 float temp_best_t = hit.best_t;
387 float temp_blocked_t = hit.blocked_t;
388 hit = current_hit;
389 hit.occluded = true;
390 hit.best_t = temp_best_t;
391 hit.blocked_t = temp_blocked_t;
392 hit.mn_idx = mn_id;
393 hit.tri_idx = tri_idx;
394 hit.normal = f.normal;
395 hit.hit = true;
396 }
397 }
398 }
399 ni = n.next;
400 }
401 else
402 {
403
404 ni = n.left;
405 }
406 }
407}
408
409
410void traverse_scene_bvh(vec3 pp, vec3 dir, vec3 inv_dir, uint light_type, uint mesh, uint tri)
411{
412 int ni = 0;
413 while (ni >= 0)
414 {
415 BVHNode n = scene_bvh_nodes[ni];
416
417 if (!slab_test(n, pp, inv_dir))
418 {
419 ni = n.next;
420 continue;
421 }
422
423 if (n.left == -1 && n.right == -1)
424 {
425 for (int i = 0; i < n.count; i++)
426 {
427
428 MeshNode mn = mesh_nodes[mesh_nodes[i + n.first_index].bvh_index];
429 if (mn.is_static == 0) continue;
430 if (mn.mesh_id == -1) continue;
431
432
433 mat4 inverse_matrix = mn.inverse_global_matrix;
434 vec3 pm = (inverse_matrix * vec4(pp, 1.0)).xyz;
435 vec3 md = (inverse_matrix * vec4(dir, 0.0)).xyz;
436 vec3 inv_md = 1.0f / md;
437
438 traverse_mesh_bvh(mn.node_offset, mn.mesh_node_id, pp, pm, dir, md, inv_md, mesh, tri);
439 }
440 ni = n.next;
441
442 }
443 else
444 {
445 ni = n.left;
446 }
447 }
448
449}
450
451uniform uint uProbeSize;
452uniform uint uRayOffset;
453uniform uint uIterations;
455uniform uint uBounces;
456uniform uint uBatchSize;
457uniform uint uProbeOffset;
458
459
460
461
462const int AUDIO_NUM_BANDS = 6;
463
464
465void main()
466{
467
468 uvec2 id = gl_GlobalInvocationID.xy;
469 if (id.x >= uBatchSize || id.y >= uProbeSize) return;
470
471 uint seed = uint(id.x) + uint(id.y) * 1973u;
472 state = wang_hash(seed ^ (uint(uRayOffset + id.x) * 2699u));
473
474
475 uint idx = uProbeOffset + id.y;
476 AudioProbe probe = aprobes[idx];
477 uint ray_index = id.x + uRayOffset;
478
479 vec3 pos = probe.pos;
480 vec3 dir = normalize(generate_sphere_dir(int(ray_index), int(uIterations)));
481 vec3 inv_dir = 1.0f/dir;
482 float dist = 0.0f;
483 float energy[6] = {1.0f, 1.0f, 1.0f, 1.0f, 1.0f, 1.0f};
484 float abs_energy[6] = {1.0f, 1.0f, 1.0f, 1.0f, 1.0f, 1.0f};
485 uint offset = id.y * uMaxSamplesPerProbe + ray_index * uBounces;
486 RayHit temp_hit;
487
488 for (int i = 0; i < uBounces; i++)
489 {
490
491 hit.hit = false;
492 hit.t = 100000;
493 hit.best_t = 1000000;
494 hit.blocked_t = 1000000;
495 hit.tri_idx = 0;
496 hit.mn_idx = 0;
497 hit.occluded = false;
498 uint sample_idx = offset + i;
499 traverse_scene_bvh(pos, dir, inv_dir, 0, 0, 0);
500 if (hit.hit)
501 {
502
503 float specular = 0.9f;
504 float absorption = 0.1f;
505 dist += hit.t;
506 MeshNode temp_mn = mesh_nodes[hit.mn_idx];
507 Face tri = tris[hit.tri_idx];
508 hit.normal = mat3(transpose(temp_mn.inverse_global_matrix)) * tri.normal;
509 hit.pos = (temp_mn.global_matrix * vec4(hit.pos, 1.0)).xyz;
510 Material mat = materials[tri.material_id];
511 temp_hit = hit;
512
513
514
515 vec3 hit_to_probe = probe.pos - temp_hit.pos;
516 float dist_to_probe = length(hit_to_probe);
517 vec3 arrival_dir = hit_to_probe / dist_to_probe;
518
519
520 const float AIR_ABSORPTION_PER_METER = 0.05f;
521
522 float total_dist = dist + dist_to_probe;
523
524 float high_freq_damping = exp(-AIR_ABSORPTION_PER_METER * total_dist);
525
526 float attenuation = 1.0f / max(1.0f, dist_to_probe * dist_to_probe);
527
528
529 samples[sample_idx].pos = temp_hit.pos;
530 samples[sample_idx].arrival_dir = arrival_dir;
531 samples[sample_idx].time_of_flight = (dist + dist_to_probe) / SPEED_OF_SOUND;
532
533 samples[sample_idx].valid = 1;
534
535
536 pos = temp_hit.pos + temp_hit.normal * 0.001f;
537
538 hit.hit = false;
539 hit.t = 100000;
540 hit.best_t = 1000000;
541 hit.blocked_t = 1000000;
542 hit.tri_idx = 0;
543 hit.mn_idx = 0;
544 hit.occluded = false;
545 traverse_scene_bvh(pos, arrival_dir, 1.0f/arrival_dir, 0, 0, 0);
546
547 if (hit.hit)
548 {
549 if (hit.t < dist_to_probe)
550 {
551 samples[sample_idx].valid = 0;
552 }
553 }
554
555 float avg_scattering = 0.0f;
556 for (int i = 0; i < AUDIO_NUM_BANDS; i++)
557 {
558 avg_scattering += mat.scattering_coefficients[i];
559 }
560 avg_scattering /= AUDIO_NUM_BANDS;
561
562 bool diffuse = (rand() < avg_scattering);
563 if (diffuse)
564 {
565 dir = get_bounce_direction(temp_hit.normal);
566 }
567 else
568 {
569 dir = reflect(dir, temp_hit.normal);
570 }
571
572 float current_energy = 0.0f;
573 for (int b = 0; b < 6; b++) {
574
575 float S = mat.scattering_coefficients[b];
576
577 if (diffuse)
578 {
579 energy[b] *= (S / max(0.001f, avg_scattering));
580 }
581 else
582 {
583 energy[b] *= ((1.0f - S) / max(0.001f, 1.0f - avg_scattering));
584 }
585
586 energy[b] *= (1.0f - mat.absorption_coefficients[b]);
587 abs_energy[b] *= (1.0f - mat.absorption_coefficients[b]);
588
589 samples[sample_idx].energy[b] = energy[b] * attenuation;
590 samples[sample_idx].abs_energy[b] = abs_energy[b];
591 current_energy += energy[b] * energy[b];
592 }
593
594 energy[3] *= min(high_freq_damping * 1.5f, 1.0f);
595 energy[4] *= min(high_freq_damping * 1.1f, 1.0f);
596 energy[5] *= high_freq_damping;
597
598
599 inv_dir = 1.0f/dir;
600
601
602 if (sqrt(current_energy) < 0.0001f)
603 {
604 break;
605 }
606
607 }
608 else
609 {
610 break;
611 }
612
613
614
615 }
616
617
618
619
620}
RayHit ray_intersects_triangle(vec3 origin, vec3 dir, vec3 a, vec3 b, vec3 c)
uniform uint uMaxSamplesPerProbe
vec3 generate_sphere_dir(int i, int N)
layout(local_size_x=16, local_size_y=16) in
uniform uint uProbeSize
void traverse_scene_bvh(vec3 pp, vec3 dir, vec3 inv_dir, uint light_type, uint mesh, uint tri)
uint state
bool slab_test(BVHNode n, vec3 pos, vec3 inv_dir)
vec3 get_bounce_direction(vec3 normal)
uint wang_hash(uint seed)
uniform uint uBounces
void traverse_mesh_bvh(uint node_offset, uint mn_id, vec3 pp, vec3 pm, vec3 original_dir, vec3 dir, vec3 md, uint mesh, uint tri)
uint pcg_hash()
uniform uint uIterations
RayHit hit
void frisvad_basis(in vec3 n, out vec3 b1, out vec3 b2)
float rand()
uniform uint uRayOffset
uniform uint uProbeOffset
#define SPEED_OF_SOUND
void main()
uniform uint uBatchSize
in mat3 TBN
uniform uint n
The GPU side SSBO structure.
The GPU side structure for the SSBO of Meshs.
The GPU side structure for the SSBO of MeshFaces.
The GPU side structure for the SSBO of Points.
vec3 pos
The world position of this probe.
#define AUDIO_NUM_BANDS
The amount of bands to use for audio probe baking.
Baked IR samples loaded in from binary into APVs from ProbeVolumes.
sampler2D albedo_tex
float normal_scale
Unused - Used to determine how much of the texture normal to use.
sampler2D emissive_tex
float emissive_strength
uint skip_directional_light
sampler2D arm_tex
sampler2D normal_tex
float alpha_cutoff
Used to discard transparent Materials.
float absorption_coefficients[6]
How much audio is absorbed when hitting this Material.
float scattering_coefficients[6]
How much audio specularly reflects when hitting this Material.
The Materials parsed in from scene files that determine rendering pipeline setup, uploads,...
MeshNodes that describe the position of Meshes, GLTF nodes for GLTFAnimations, or hold references to ...
const int samples
int next
The pointer to the next node (either right child or parent's right child) for recursion-less traversa...
int index
The index of the node inside of the array.
int right
Right child.
int parent
Parent.
int left
Left child.
Nodes for the acceleration structures of BVHs.
float x
Definition types.h:12
float y
Definition types.h:12
float z
Definition types.h:12
Definition types.h:20
Definition types.h:21
Definition types.h:17
Definition types.h:18
Definition types.h:11
Definition types.h:12
Definition types.h:13