4#extension GL_ARB_bindless_texture : require
6layout (local_size_x = 16, local_size_y = 16, local_size_z = 1) in;
7layout(rgba32f, binding = 0) uniform image2D lightmapTex;
8layout(rgba32f, binding = 1) uniform image2D directionalTex;
39layout(std430, binding = 0) buffer LightData {
181layout(std430, binding = 7) buffer MeshBVHData {
185layout(std430, binding = 8) buffer SceneBVHData {
238layout(std430, binding = 10) buffer LightBVHData {
256#define MINIMUM_LIGHT_VISIBILITY_DIST 0.1
280 state = prev * 747796405u + 2891336453u;
281 uint word = ((prev >> ((prev >> 28u) + 4u)) ^ prev) * 277803737u;
282 return (word >> 22u) ^ word;
286 seed = (seed ^ 61u) ^ (seed >> 16u);
288 seed = seed ^ (seed >> 4u);
290 seed = seed ^ (seed >> 15u);
294 return float(
pcg_hash()) / 4294967296.0;
321 if (
n.z < -0.99998796f) {
322 b1.x = 0.0f; b1.y = -1.0f; b1.z = 0.0f;
323 b2.x = -1.0f; b2.y = 0.0f; b2.z = 0.0f;
327 float a = 1.0f / (1.0f +
n.z);
328 float b = -
n.x *
n.y * a;
330 b1.x = 1.0f -
n.x *
n.x * a;
335 b2.y = 1.0f -
n.y *
n.y * a;
343 float phi = 2.0 * 3.14159265 * r1;
344 float x = cos(phi) * sqrt(r2);
345 float y = sin(phi) * sqrt(r2);
346 float z = sqrt(1.0 - r2);
354 if (abs(normal.
z) > 0.999)
362 vec3 up = abs(normal.
z) < 0.999 ?
vec3(0.0, 0.0, 1.0) :
vec3(1.0, 0.0, 0.0);
363 tangent = normalize(cross(up, normal));
364 bitangent = cross(normal, tangent);
369 return normalize(
TBN * local_dir);
410 vec3 h = cross(dir, edge2);
411 float dA = dot(edge1, h);
413 if (abs(dA) < 0.000001f) {
419 float u = f * dot(s, h);
420 if (u < 0.0f || u > 1.0f) {
424 vec3 q = cross(s, edge1);
425 float v = f * dot(dir, q);
426 if (v < 0.0f || u + v > 1.0f) {
430 float t = f * dot(edge2, q);
434 float w = 1.0f - u - v;
435 out_hit.pos = a * w + b * u + c * v;
436 out_hit.barycentrics =
vec3(w, u, v);
446 vec3 t1 = (
n.min - pos) * inv_dir;
447 vec3 t2 = (
n.max - pos) * inv_dir;
449 vec3 tmin = min(t1, t2);
450 vec3 tmax = max(t1, t2);
452 float t_near = max(max(tmin.
x, tmin.
y), tmin.
z);
453 float t_far = min(min(tmax.
x, tmax.
y), tmax.
z);
455 return t_far >= max(0.0f, t_near) && t_near <
hit.
t;
474 vec2 hit_uv = tri.v0.uv * b.
x + tri.v1.uv * b.
y + tri.v2.uv * b.
z;
481 vec4 albedo = texture(mat.albedo_tex, hit_uv) * mat.diffuse;
488 float metallic = texture(mat.arm_tex, hit_uv).b * mat.metallic;
495 vec3 emissive = texture(mat.emissive_tex, hit_uv).rgb * mat.emissive.rgb * mat.emissive_strength;
501 int ni = int(node_offset);
514 if (
n.left == -1 &&
n.right == -1)
516 for (
int i = 0; i <
n.count; i++)
520 uint tri_idx = i +
n.first_index;
521 Face f = tris[tri_idx];
533 if (!did_hit)
continue;
542 bool col = (current_hit.
t <
hit.
t);
544 bool dot_test = dot(f.
normal, dir) <= 0.0f;
549 if (dot_test || albedo.a < 1.0f)
606 if (
n.left == -1 &&
n.right == -1)
608 for (
int i = 0; i <
n.count; i++)
612 MeshNode mn = mesh_nodes[i +
n.first_index];
614 if (mn.
mesh_id == -1)
continue;
620 vec3 pm = (inverse_matrix *
vec4(pp, 1.0)).xyz;
621 vec3 md = (inverse_matrix *
vec4(dir, 0.0)).xyz;
624 vec3 inv_md = 1.0f / md;
651 vec3 tn = cross(v0, v1);
652 float ta = length(tn);
654 float wwC = length(cross(v0, v2)) / ta;
655 float wwB = length(cross(v2, v1)) / ta;
656 float wwA = 1.0f - wwB - wwC;
657 return vec3(wwA, wwB, wwC);
663 Face tri = tris[tri_idx];
677 float attenuation = intensity / (distance + 1.0);
679 float factor = distance / (radius * radius);
680 float window =
clamp(1.0 - factor * factor * factor * factor, 0.0, 1.0);
683 return attenuation *
window;
693 uint light_type = l.light_type;
694 if (l.light_type == 1)
706 if (d2 > l.radius * l.radius && l.light_type == 0)
return vec3(0,0,0);
709 float min_radius = 0.05f;
710 safe_d2 = max(d2, min_radius * min_radius);
715 vec3 inv_dir = 1.0f / L;
716 vec3 pp = pos + normal * 0.001f;
720 bool hit_light =
false;
723 while (!hit_light && cur < safety)
730 if (l.light_type != 1)
744 if (
hit.
hit && l.light_type == 1 || (l.light_type != 1 &&
hit.
hit &&
hit.
t < d))
754 throughput *= albedo.rgb;
757 pp =
hit.
pos + L * 0.001f;
758 d = length(pp - l.pos);
774 vec3 lighting = l.color * max(dot(normal, L), 0.0f) * throughput;
776 if (!hit_light)
return vec3(0,0,0);
778 if (l.light_type == 1)
780 lighting *= l.intensity;
782 else if (l.light_type == 0)
789 float theta = dot(-L, l.dir);
791 float intensity =
clamp((theta - l.outer_cos) * l.cos_diff, 0.0f, 1.0f);
792 lighting /= max(safe_d2, 0.001);
793 lighting *= intensity;
794 lighting *= l.intensity;
838 float sqrt_r1 = sqrt(r1);
839 return (1.0 - sqrt_r1) * a + (sqrt_r1 * (1.0 - r2)) * b + (sqrt_r1 * r2) * c;
847 MeshNode mn = mesh_nodes[light.mesh];
860 vec3 diff = random_pos - pos;
861 float dist = length(diff);
862 float d2 = (dist * dist);
863 vec3 L = diff / dist;
864 vec3 inv_dir = 1.0f / L;
865 bool hit_light =
false;
868 vec3 pp = pos + normal * 0.01f;
871 while (!hit_light && cur < safety)
895 Material hit_mat = materials[mat_id];
905 throughput *= albedo.rgb;
910 pp =
hit.
pos + L * 0.001f;
911 diff = random_pos - pp;
919 if (length(emissive) == 0 && f.
material_id != mat_id)
953 float cos_light = dot(area_normal, -L);
954 if (cos_light <= 0.001)
959 float cos_surface = max(0.0, dot(normal, L));
960 vec3 color = (emission * brdf * throughput * cos_light * cos_surface * light.area) / (d2 * p_select);
1004 return clamp(pos, min, max);
1009 if (pos.
x >= min.
x && pos.
x <= max.
x &&
1010 pos.
y >= min.
y && pos.
y <= max.
y &&
1011 pos.
z >= min.
z && pos.
z <= max.
z)
return true;
1022 int dir_light_idx = -1;
1026 if (lights[i].light_type == 1)
1072 if (
n.left == -1 &&
n.right == -1)
1074 bool processed =
false;
1099 int random_light_idx = int(
rand() *
n.count);
1100 if (random_light_idx >=
n.count) random_light_idx =
n.count-1;
1102 uint index =
n.first_index + random_light_idx;
1103 Light l = lights[index];
1116 BVHNode n0 = light_bvh_nodes[
n.left];
1117 BVHNode n1 = light_bvh_nodes[
n.right];
1122 vec3 diff_n0 = pos - closest_n0;
1123 vec3 diff_n1 = pos - closest_n1;
1125 float dist2_n0 = dot(diff_n0, diff_n0);
1126 float dist2_n1 = dot(diff_n1, diff_n1);
1128 dist2_n0 = max(dist2_n0, 0.001f);
1129 dist2_n1 = max(dist2_n1, 0.001f);
1131 float importance_n0 = n0.
flux / dist2_n0;
1132 float importance_n1 = n1.
flux / dist2_n1;
1134 float sum = importance_n0 + importance_n1;
1137 importance_n0 = 0.5f;
1138 importance_n1 = 0.5f;
1141 float norm_n0 = importance_n0 / sum;
1142 float norm_n1 = importance_n1 / sum;
1148 pdf *= max(norm_n0, 0.0001f);
1155 pdf *= max(norm_n1, 0.0001f);
1161 if (length(
color) <= 0.0f && dir_light_idx != -1)
1164 Light directional_light = lights[dir_light_idx];
1165 float lum = dot(directional_light.
color,
vec3(0.2126f, 0.7152f, 0.0722f));
1167 float flux_dir = lum * directional_light.
intensity;
1168 BVHNode n_root = light_bvh_nodes[0];
1169 float importance_n_root = n_root.
flux;
1170 float sum_imp = flux_dir + importance_n_root;
1171 float norm_dir = flux_dir / sum_imp;
1172 float norm_root = importance_n_root / sum_imp;
1178 pdf = max(norm_dir, 0.0001f);
1202 if (
n.left == -1 &&
n.right == -1)
1204 int random_light_idx = int(
rand() *
n.count);
1205 if (random_light_idx >=
n.count) random_light_idx =
n.count-1;
1207 uint index =
n.first_index + random_light_idx;
1208 Light l = lights[index];
1226 BVHNode n0 = light_bvh_nodes[
n.left];
1227 BVHNode n1 = light_bvh_nodes[
n.right];
1232 vec3 diff_n0 = pos - closest_n0;
1233 vec3 diff_n1 = pos - closest_n1;
1235 float dist2_n0 = dot(diff_n0, diff_n0);
1236 float dist2_n1 = dot(diff_n1, diff_n1);
1238 dist2_n0 = max(dist2_n0, 0.001f);
1239 dist2_n1 = max(dist2_n1, 0.001f);
1244 float sum = importance_n0 + importance_n1;
1247 importance_n0 = 0.5f;
1248 importance_n1 = 0.5f;
1251 float norm_n0 = importance_n0 / sum;
1252 float norm_n1 = importance_n1 / sum;
1258 pdf *= max(norm_n0, 0.0001f);
1265 pdf *= max(norm_n1, 0.0001f);
1279 bool hit_surface =
false;
1282 while (!hit_surface && cur < safety)
1313 throughput *= albedo.rgb;
1318 pos =
hit.
pos + dir * 0.01f;
1348 vec3 current_pos = pos;
1349 vec3 current_normal = normal;
1350 uint current_mn_id = uint(ids.
y);
1351 uint current_tri_idx = uint(ids.
z);
1354 MeshNode mn = mesh_nodes[current_mn_id];
1355 uint tri_idx = mn.
tri_offset + current_tri_idx;
1356 Face tri = tris[tri_idx];
1362 uint mat_id = tris[tri_idx].material_id;
1365 vec3 brdf = albedo.rgb * (1.0f / 3.14159265f);
1386 vec3 inv_dir = 1.0f / dir;
1387 vec3 pp = current_pos + current_normal * 0.01f;
1401 bool skip_bounce =
false;
1411 acc += throughput * sky_light;
1437 vec3 brdf = albedo.rgb * (1.0f / 3.14159265f);
1445 float max_lum = 10.0;
1446 float lum = dot(direct_light,
vec3(0.2126, 0.7152, 0.0722));
1449 direct_light *= max_lum / lum;
1451 acc += throughput * direct_light;
1452 throughput *= albedo.rgb;
1474 for (
int i = 0; i < 1; i++)
1477 vec3 inv_dir = 1.0f / dir;
1478 vec3 pp = current_pos + current_normal * 0.001f;
1498 acc += throughput * sky_light ;
1528 vec3 brdf = albedo.rgb * (1.0f / 3.14159265f);
1538 float max_lum = 10.0;
1539 float lum = dot(direct_light,
vec3(0.2126, 0.7152, 0.0722));
1542 direct_light *= max_lum / lum;
1545 acc += throughput * direct_light;
1546 throughput *= albedo.rgb;
1570 vec3 inv_dir = 1.0f / dir;
1610 uint seed = uint(coord.
x) * 1973u + uint(coord.
y) * 9277u;
1625 vec3 pos = pos_tex.xyz;
1627 vec3 normal = normal_tex.xyz;
1639 float sample_luminance = dot(ray_color,
vec3(0.2126, 0.7152, 0.0722));
1642 vec4 old_data = imageLoad(lightmapTex, coord);
1643 vec3 old_color = old_data.rgb;
1644 float current_sample_count = old_data.a;
1646 float new_sample_count = current_sample_count + 1.0;
1648 vec3 new_color = old_color + ray_color;
1651 vec4 old_dir_data = imageLoad(directionalTex, coord);
1654 vec3 old_dir_sum = old_dir_data.rgb;
1655 vec3 new_dir_sum = old_dir_sum + ray_dir_sample;
1657 float new_luminance = old_dir_data.a + sample_luminance;
1660 imageStore(directionalTex, coord,
vec4(new_dir_sum, new_luminance));
RayHit ray_intersects_triangle(vec3 origin, vec3 dir, vec3 a, vec3 b, vec3 c)
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 int uLightmapHeight
uniform int uLightmapWidth
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.
uint32_t layer
Which layer a DIRECTIONAL_LIGHT corresponds to in the SHADOW_MAP_FBO.
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
float inner_angle
Starting angle for SPOT_LIGHTs.
int tri
The MeshFace this corresponds to.
float outer_angle
Outer angle for SPOT_LIGHTs.
uint skip_directional_light
float radius
The radius of the Light.
int mesh
The MeshNode this corresponds to.
AABB area
The physical area of AREA_LIGHTs.
uint32_t entity_id
Entity ID into ENTITIES.
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)
uniform sampler2D uLightmapPosTex
uniform float uAOThreshold
uniform int uAOIterations
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 sampler2D uLightmapNormalTex
float get_hit_metallic(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)
uniform isampler2D uLightmapIDTex
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)
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...
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.