3#extension GL_ARB_bindless_texture : require
4layout (local_size_x = 16, local_size_y = 16) in;
7#define SAMPLE_RATE 48000
8#define SPEED_OF_SOUND 343.0f
9#define MAX_SAMPLES (SAMPLE_RATE * 1)
10#define NUM_RAYS 100000
149layout(std430, binding = 7) buffer MeshBVHData {
153layout(std430, binding = 8) buffer SceneBVHData {
164layout(std430, binding = 9) buffer AProbeData {
184layout(std430, binding = 10) buffer BounceData {
193 return (word >> 22u) ^ word;
198 seed = (seed ^ 61u) ^ (seed >> 16u);
200 seed = seed ^ (seed >> 4u);
202 seed = seed ^ (seed >> 15u);
207 return float(
pcg_hash()) / 4294967296.0;
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;
216 float a = 1.0f / (1.0f +
n.z);
217 float b = -
n.x *
n.y * a;
219 b1.x = 1.0f -
n.x *
n.x * a;
224 b2.y = 1.0f -
n.y *
n.y * a;
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);
240 if (abs(normal.
z) > 0.999)
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);
255 return normalize(
TBN * local_dir);
261 float phi = 3.14159265 * (3.0f - sqrt(5.0f));
263 float y = 1.0f - 2.0f*(i+0.5f)/N;
265 float r = sqrt(1-y*y);
269 return vec3(cos(theta) * r, y, sin(theta) * r);
298 vec3 h = cross(dir, edge2);
299 float dA = dot(edge1, h);
301 if (abs(dA) < 0.000001f) {
307 float u = f * dot(s, h);
308 if (u < 0.0f || u > 1.0f) {
312 vec3 q = cross(s, edge1);
313 float v = f * dot(dir, q);
314 if (v < 0.0f || u + v > 1.0f) {
318 float t = f * dot(edge2, q);
322 float w = 1.0f - u - v;
323 out_hit.
pos = a * w + b * u + c * v;
334 vec3 t1 = (
n.min - pos) * inv_dir;
335 vec3 t2 = (
n.max - pos) * inv_dir;
337 vec3 tmin = min(t1, t2);
338 vec3 tmax = max(t1, t2);
340 float t_near = max(max(tmin.
x, tmin.
y), tmin.
z);
341 float t_far = min(min(tmax.
x, tmax.
y), tmax.
z);
343 return (t_far >= max(0.0f, t_near) && t_near < closest_valid_t);
348 int ni = int(node_offset);
361 if (
n.left == -1 &&
n.right == -1)
363 for (
int i = 0; i <
n.count; i++)
366 uint tri_idx = tris[i +
n.first_index].bvh_index;
367 Face f = tris[tri_idx];
374 if (!current_hit.
hit)
continue;
377 bool col = (current_hit.
t <
hit.
t);
379 bool dot_test = dot(f.
normal, dir) <= 0.0f;
423 if (
n.left == -1 &&
n.right == -1)
425 for (
int i = 0; i <
n.count; i++)
428 MeshNode mn = mesh_nodes[mesh_nodes[i +
n.first_index].bvh_index];
430 if (mn.
mesh_id == -1)
continue;
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;
468 uvec2 id = gl_GlobalInvocationID.xy;
471 uint seed = uint(
id.x) + uint(
id.y) * 1973u;
481 vec3 inv_dir = 1.0f/dir;
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};
498 uint sample_idx = offset + i;
503 float specular = 0.9f;
504 float absorption = 0.1f;
516 float dist_to_probe = length(hit_to_probe);
517 vec3 arrival_dir = hit_to_probe / dist_to_probe;
520 const float AIR_ABSORPTION_PER_METER = 0.05f;
522 float total_dist = dist + dist_to_probe;
524 float high_freq_damping = exp(-AIR_ABSORPTION_PER_METER * total_dist);
526 float attenuation = 1.0f / max(1.0f, dist_to_probe * dist_to_probe);
530 samples[sample_idx].arrival_dir = arrival_dir;
536 pos = temp_hit.
pos + temp_hit.
normal * 0.001f;
549 if (
hit.
t < dist_to_probe)
555 float avg_scattering = 0.0f;
562 bool diffuse = (
rand() < avg_scattering);
569 dir = reflect(dir, temp_hit.
normal);
572 float current_energy = 0.0f;
573 for (
int b = 0; b < 6; b++) {
579 energy[b] *= (S / max(0.001f, avg_scattering));
583 energy[b] *= ((1.0f - S) / max(0.001f, 1.0f - avg_scattering));
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];
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;
602 if (sqrt(current_energy) < 0.0001f)
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
void traverse_scene_bvh(vec3 pp, vec3 dir, vec3 inv_dir, uint light_type, uint mesh, uint tri)
bool slab_test(BVHNode n, vec3 pos, vec3 inv_dir)
vec3 get_bounce_direction(vec3 normal)
uint wang_hash(uint seed)
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)
void frisvad_basis(in vec3 n, out vec3 b1, out vec3 b2)
uniform uint uProbeOffset
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.
float normal_scale
Unused - Used to determine how much of the texture normal to use.
mat4 inverse_global_matrix
uint skip_directional_light
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 ...
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.
Nodes for the acceleration structures of BVHs.