3#extension GL_ARB_bindless_texture : require
5layout (local_size_x = 8, local_size_y = 8, local_size_z = 8) in;
37layout(std430, binding = 0) buffer LightData {
180layout(std430, binding = 7) buffer MeshBVHData {
184layout(std430, binding = 8) buffer SceneBVHData {
196layout(std430, binding = 9) buffer ProbeData {
201layout(std430, binding = 10) buffer LightBVHData {
218#define MINIMUM_LIGHT_VISIBILITY_DIST 0.1
243 return (word >> 22u) ^ word;
248 seed = (seed ^ 61u) ^ (seed >> 16u);
250 seed = seed ^ (seed >> 4u);
252 seed = seed ^ (seed >> 15u);
257 return float(
pcg_hash()) / 4294967296.0;
261 if (
n.z < -0.99998796f) {
262 b1.x = 0.0f; b1.y = -1.0f; b1.z = 0.0f;
263 b2.x = -1.0f; b2.y = 0.0f; b2.z = 0.0f;
267 float a = 1.0f / (1.0f +
n.z);
268 float b = -
n.x *
n.y * a;
270 b1.x = 1.0f -
n.x *
n.x * a;
275 b2.y = 1.0f -
n.y *
n.y * a;
283 float phi = 2.0 * 3.14159265 * r1;
284 float x = cos(phi) * sqrt(r2);
285 float y = sin(phi) * sqrt(r2);
286 float z = sqrt(1.0 - r2);
291 if (abs(normal.
z) > 0.999)
299 vec3 up = abs(normal.
z) < 0.999 ?
vec3(0.0, 0.0, 1.0) :
vec3(1.0, 0.0, 0.0);
300 tangent = normalize(cross(up, normal));
301 bitangent = cross(normal, tangent);
306 return normalize(
TBN * local_dir);
311 float phi = 3.14159265 * (3.0f - sqrt(5.0f));
313 float y = 1.0f - 2.0f*(i+0.5f)/N;
315 float r = sqrt(1-y*y);
319 return vec3(cos(theta) * r, y, sin(theta) * r);
332 vec3 h = cross(dir, edge2);
333 float dA = dot(edge1, h);
335 if (abs(dA) < 0.000001f) {
341 float u = f * dot(s, h);
342 if (u < 0.0f || u > 1.0f) {
346 vec3 q = cross(s, edge1);
347 float v = f * dot(dir, q);
348 if (v < 0.0f || u + v > 1.0f) {
352 float t = f * dot(edge2, q);
356 float w = 1.0f - u - v;
357 out_hit.pos = a * w + b * u + c * v;
358 out_hit.barycentrics =
vec3(w, u, v);
371 vec3 t1 = (
n.min - pos) * inv_dir;
372 vec3 t2 = (
n.max - pos) * inv_dir;
374 vec3 tmin = min(t1, t2);
375 vec3 tmax = max(t1, t2);
377 float t_near = max(max(tmin.
x, tmin.
y), tmin.
z);
378 float t_far = min(min(tmax.
x, tmax.
y), tmax.
z);
380 return t_far >= max(0.0f, t_near) && t_near <
hit.
t;
385 Face tri = tris[tri_idx];
398 vec2 hit_uv = tri.v0.uv * b.
x + tri.v1.uv * b.
y + tri.v2.uv * b.
z;
404 vec4 albedo = texture(mat.albedo_tex, hit_uv) * mat.diffuse;
410 vec3 emissive = texture(mat.emissive_tex, hit_uv).rgb * mat.emissive.rgb * mat.emissive_strength;
419 int ni = int(node_offset);
430 if (
n.left == -1 &&
n.right == -1)
432 for (
int i = 0; i <
n.count; i++)
435 uint tri_idx = tris[i +
n.first_index].bvh_index;
436 Face f = tris[tri_idx];
444 if (!did_hit)
continue;
452 bool col = (current_hit.
t <
hit.
t);
454 bool dot_test = dot(f.
normal, dir) <= 0.0f;
458 if (dot_test || albedo.a < 1.0f)
517 if (
n.left == -1 &&
n.right == -1)
519 for (
int i = 0; i <
n.count; i++)
522 MeshNode mn = mesh_nodes[mesh_nodes[i +
n.first_index].bvh_index];
524 if (mn.
mesh_id == -1)
continue;
527 vec3 pm = (inverse_matrix *
vec4(pp, 1.0)).xyz;
528 vec3 md = (inverse_matrix *
vec4(dir, 0.0)).xyz;
529 vec3 inv_md = 1.0f / md;
551 vec3 tn = cross(v0, v1);
552 float ta = length(tn);
554 float wwC = length(cross(v0, v2)) / ta;
555 float wwB = length(cross(v2, v1)) / ta;
556 float wwA = 1.0f - wwB - wwC;
557 return vec3(wwA, wwB, wwC);
562 float attenuation = intensity / (distance + 1.0);
564 float factor = distance / (radius * radius);
565 float window =
clamp(1.0 - factor * factor * factor * factor, 0.0, 1.0);
568 return attenuation *
window;
595 float min_radius = 0.05f;
596 safe_d2 = max(d2, min_radius * min_radius);
600 vec3 inv_dir = 1.0f / L;
602 vec3 pp = pos + normal * 0.001f;
606 bool hit_light =
false;
609 while (!hit_light && cur < safety)
640 throughput *= albedo.rgb;
643 pp =
hit.
pos + L * 0.001f;
644 d = length(pp - l.
pos);
661 vec3 lighting = l.
color * max(dot(normal, L), 0.0f) * throughput;
662 if (!hit_light)
return vec3(0,0,0);
675 float theta = dot(-L, l.
dir);
678 lighting /= max(safe_d2, 0.001);
679 lighting *= intensity;
693 float sqrt_r1 = sqrt(r1);
694 return (1.0 - sqrt_r1) * a + (sqrt_r1 * (1.0 - r2)) * b + (sqrt_r1 * r2) * c;
701 MeshNode mn = mesh_nodes[light.mesh];
713 vec3 diff = random_pos - pos;
714 float dist = length(diff);
715 float d2 = (dist * dist);
716 vec3 L = diff / dist;
717 vec3 inv_dir = 1.0f / L;
718 bool hit_light =
false;
721 vec3 pp = pos + normal * 0.01f;
723 while (!hit_light && cur < safety)
744 Material hit_mat = materials[mat_id];
752 throughput *= albedo.rgb;
755 pp =
hit.
pos + L * 0.001f;
756 diff = random_pos - pp;
763 if (length(emissive) == 0 && f.
material_id != mat_id)
782 float cos_light = dot(area_normal, -L);
783 if (cos_light <= 0.001)
788 float cos_surface = max(0.0, dot(normal, L));
789 vec3 color = (emission * brdf * throughput * cos_light * cos_surface * light.area) / (d2 * p_select);
826 return clamp(pos, min, max);
831 if (pos.
x >= min.
x && pos.
x <= max.
x &&
832 pos.
y >= min.
y && pos.
y <= max.
y &&
833 pos.
z >= min.
z && pos.
z <= max.
z)
return true;
844 int dir_light_idx = -1;
848 if (lights[i].light_type == 1)
865 if (
n.left == -1 &&
n.right == -1)
867 int random_light_idx = int(
rand() *
n.count);
868 if (random_light_idx >=
n.count) random_light_idx =
n.count-1;
870 uint index = lights[
n.first_index + random_light_idx].bvh_index;
871 Light l = lights[index];
880 BVHNode n0 = light_bvh_nodes[
n.left];
881 BVHNode n1 = light_bvh_nodes[
n.right];
886 vec3 diff_n0 = pos - closest_n0;
887 vec3 diff_n1 = pos - closest_n1;
889 float dist2_n0 = dot(diff_n0, diff_n0);
890 float dist2_n1 = dot(diff_n1, diff_n1);
892 dist2_n0 = max(dist2_n0, 0.001f);
893 dist2_n1 = max(dist2_n1, 0.001f);
895 float importance_n0 = n0.
flux / dist2_n0;
896 float importance_n1 = n1.
flux / dist2_n1;
898 float sum = importance_n0 + importance_n1;
901 importance_n0 = 0.5f;
902 importance_n1 = 0.5f;
905 float norm_n0 = importance_n0 / sum;
906 float norm_n1 = importance_n1 / sum;
912 pdf *= max(norm_n0, 0.0001f);
919 pdf *= max(norm_n1, 0.0001f);
925 if (length(
color) <= 0.0f && dir_light_idx != -1)
928 Light directional_light = lights[dir_light_idx];
929 float lum = dot(directional_light.
color,
vec3(0.2126f, 0.7152f, 0.0722f));
930 float flux_dir = lum * directional_light.
intensity;
931 BVHNode n_root = light_bvh_nodes[0];
932 float importance_n_root = n_root.
flux;
933 float sum_imp = flux_dir + importance_n_root;
934 float norm_dir = flux_dir / sum_imp;
935 float norm_root = importance_n_root / sum_imp;
938 pdf = max(norm_dir, 0.0001f);
959 if (
n.left == -1 &&
n.right == -1)
961 int random_light_idx = int(
rand() *
n.count);
962 if (random_light_idx >=
n.count) random_light_idx =
n.count-1;
964 uint index = lights[
n.first_index + random_light_idx].bvh_index;
965 Light l = lights[index];
973 BVHNode n0 = light_bvh_nodes[
n.left];
974 BVHNode n1 = light_bvh_nodes[
n.right];
979 vec3 diff_n0 = pos - closest_n0;
980 vec3 diff_n1 = pos - closest_n1;
982 float dist2_n0 = dot(diff_n0, diff_n0);
983 float dist2_n1 = dot(diff_n1, diff_n1);
985 dist2_n0 = max(dist2_n0, 0.001f);
986 dist2_n1 = max(dist2_n1, 0.001f);
991 float sum = importance_n0 + importance_n1;
994 importance_n0 = 0.5f;
995 importance_n1 = 0.5f;
998 float norm_n0 = importance_n0 / sum;
999 float norm_n1 = importance_n1 / sum;
1005 pdf *= max(norm_n0, 0.0001f);
1012 pdf *= max(norm_n1, 0.0001f);
1026 bool hit_surface =
false;
1029 while (!hit_surface && cur < safety)
1058 throughput *= albedo.rgb;
1061 pos =
hit.
pos + dir * 0.01f;
1082 vec3 current_normal;
1084 uint current_tri_idx;
1086 vec3 brdf =
vec3(0.5, 0.5, 0.5) * (1.0f / 3.14159265f);
1089 dir = normalize(dir);
1094 vec3 inv_dir = 1.0/dir;
1098 bool skip_bounce =
false;
1108 acc += throughput * sky_light;
1125 vec3 brdf = albedo.rgb * (1.0f / 3.14159265f);
1134 float max_lum = 10.0;
1135 float lum = dot(direct_light,
vec3(0.2126, 0.7152, 0.0722));
1138 direct_light *= max_lum / lum;
1140 acc += throughput * direct_light;
1142 throughput *= albedo.rgb;
1155 for (
int i = 0; i < 1; i++)
1159 vec3 inv_dir = 1.0/dir;
1160 float pdf = dot(dir, current_normal) / 3.14159265f;
1162 vec3 pp = current_pos + current_normal * 0.001f;
1173 acc += throughput * sky_light;
1192 vec3 brdf = albedo.rgb * (1.0f / 3.14159265f);
1201 float max_lum = 10.0;
1202 float lum = dot(direct_light,
vec3(0.2126, 0.7152, 0.0722));
1205 direct_light *= max_lum / lum;
1207 acc += throughput * direct_light;
1209 throughput *= albedo.rgb;
1239 vec3 p6 =
color * 0.315392 * (3 * dir.
z * dir.
z - 1);
1241 vec3 p8 =
color * 0.546274 * (dir.
x * dir.
x - dir.
y * dir.
y);
1243 probes[index].coefficients[0].xyz += p0;
1244 probes[index].coefficients[1].xyz += p1;
1245 probes[index].coefficients[2].xyz += p2;
1246 probes[index].coefficients[3].xyz += p3;
1247 probes[index].coefficients[4].xyz += p4;
1248 probes[index].coefficients[5].xyz += p5;
1249 probes[index].coefficients[6].xyz += p6;
1250 probes[index].coefficients[7].xyz += p7;
1251 probes[index].coefficients[8].xyz += p8;
1260 ivec3 coord =
ivec3(gl_GlobalInvocationID.xyz);
1264 vec3 pos = probes[index].pos;
1266 uint seed = uint(coord.
x) + uint(coord.
y) * 1973u + uint(coord.
z) * 9277u;
RayHit ray_intersects_triangle(vec3 origin, vec3 dir, vec3 a, vec3 b, vec3 c)
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)
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.
float intensity
The intensity of the Light.
vec3 pos
Position of light.
LightType light_type
The type of Light.
float normal_scale
Unused - Used to determine how much of the texture normal to use.
vec3 dir
Direction for lights that are not POINT_LIGHTs.
mat4 inverse_global_matrix
uint skip_directional_light
float radius
The radius of the 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 CPU side structure for Lights.
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 ...
vec2 get_hit_uvs(in Face tri, in RayHit hit)
vec3 trace_path(vec3 pos, vec3 normal, ivec3 ids)
vec3 traverse_light_bvh(vec3 pos, vec3 normal, uint mesh, uint tri, vec3 brdf)
vec3 random_pos_on_triangle(vec3 a, vec3 b, vec3 c)
vec3 get_hit_emissive(in Material mat, vec2 hit_uv)
uniform samplerCube uSkybox
vec4 get_hit_albedo(in Material mat, vec2 hit_uv)
vec2 get_uvs(vec3 B, uint tri_idx)
vec3 process_area_light(in Light light, vec3 pos, vec3 normal, vec3 brdf, float p_select)
vec3 traverse_light_bvh_direct_emissives(vec3 pos, vec3 normal, vec3 brdf)
vec3 process_light(vec3 pos, vec3 normal, uint mesh, uint tri, vec3 brdf, Light light, float p_select)
vec3 closest_point_on_aabb(vec3 pos, vec3 min, vec3 max)
vec3 resolve_transparents(vec3 throughput, vec3 pos, vec3 dir, vec3 inv_dir)
float CalculateAttenuation(float distance, float radius, float intensity)
vec3 solve_light_source(in Light l, vec3 pos, vec3 normal, uint mesh, uint tri)
bool pos_inside_aabb(vec3 pos, vec3 min, vec3 max)
uniform ivec2 uTileOffset
vec3 compute_3d_barycentric(vec3 a, vec3 b, vec3 c, vec3 p)
float clamp(float a, float b, float c)
void solve_SH(vec3 color, vec3 dir, int index, vec3 pos)
float flux
The total light flux of this node.
int next
The pointer to the next node (either right child or parent's right child) for recursion-less traversa...
uniform int uCurrentIteration
int index
The index of the node inside of the array.
float emissive_flux
The only-emissive flux.
Nodes for the acceleration structures of BVHs.