Untitled
Loading...
Searching...
No Matches
probe_bake.comp
Go to the documentation of this file.
1
2#version 430 core
3#extension GL_ARB_bindless_texture : require
4
5layout (local_size_x = 8, local_size_y = 8, local_size_z = 8) in;
6
7
8struct Light
9{
10
11 vec3 pos;
12 uint entity_id;
13
14 vec3 dir;
15 float outer_cos;
16
17 vec3 color;
18 float cos_diff;
19
20 float intensity;
21 uint light_type;
22 float inner_angle;
23 float outer_angle;
24
25 float radius;
26 uint layer;
27 uint mesh;
28 uint tri;
29
30 float area;
31 uint bvh_index;
32 uint dynamic;
33 uint pad2;
34
35};
36
37layout(std430, binding = 0) buffer LightData {
38
39 Light lights[];
40
41};
42
43struct Material
44{
45
46 vec4 ambient;
47 vec4 diffuse;
48 vec4 specular;
49 vec4 emissive;
50
52 uint pad0;
53 uint pad1;
55 uint pad2;
56 uint pad3;
57
58 sampler2D albedo_tex;
59 sampler2D normal_tex;
60 sampler2D arm_tex;
61 sampler2D emissive_tex;
62
63 float metallic;
64 float roughness;
65 float ao_strength;
66 float normal_scale;
67 uint alpha_mode;
68 float alpha_cutoff;
69 uint double_sided;
71
72};
73
74layout(std430, binding = 2) buffer MaterialData {
75 Material materials[];
76};
77
78
79
80struct Vertex {
81
82 vec3 pos;
83 uint pad0;
84 vec3 normal;
85 uint pad1;
86 vec2 uv;
87 uint pad2;
88 uint pad3;
89
90};
91
92layout(std430, binding = 4) buffer VertexData {
93 Vertex vertices[];
94};
95
96struct Face {
97
98 uvec3 indices;
99 uint material_id;
100 vec3 normal;
101 uint mesh_id;
102 uint bvh_index;
103 uint tri_idx;
104 uint pad0;
105 uint pad1;
106 Vertex v0;
107 Vertex v1;
108 Vertex v2;
109
110};
111
112layout(std430, binding = 5) buffer TriData {
113 Face tris[];
114};
115
116struct MeshNode {
117
118 uint mesh_node_id;
119 uint mesh_id;
120
121 uint node_count;
122 uint node_offset;
123 uint vertex_offset;
124 uint vertex_count;
125 uint tri_offset;
126 uint tri_count;
127 uint bvh_index;
128 uint is_static;
130 uint pad2;
131
132 mat4 global_matrix;
134
135};
136
137layout(std430, binding = 6) buffer MeshData {
138 MeshNode mesh_nodes[];
139};
140
141struct AABB
142{
143
144 vec3 min;
145 uint pad0;
146 vec3 max;
147 uint pad1;
148 vec3 center;
149 uint pad2;
150 vec3 extents;
151 uint pad3;
152
153};
154
155
156struct BVHNode
157{
158
159 int left;
160 int right;
161 int parent;
162 int index;
163
164 int first_index;
165 int count;
166 int next;
167 float flux;
168
169 vec3 min;
170 float emissive_flux;
171 vec3 max;
172 uint pad2;
173 vec3 center;
174 uint pad3;
175 vec3 extents;
176 uint pad4;
177
178};
179
180layout(std430, binding = 7) buffer MeshBVHData {
181 BVHNode mesh_bvh_nodes[];
182};
183
184layout(std430, binding = 8) buffer SceneBVHData {
185 BVHNode scene_bvh_nodes[];
186};
187
188
189struct SHData
190{
192 uint pad0;
194};
195
196layout(std430, binding = 9) buffer ProbeData {
197 SHData probes[];
198};
199
200
201layout(std430, binding = 10) buffer LightBVHData {
202 BVHNode light_bvh_nodes[];
203};
204
205uniform samplerCube uSkybox;
206
208uniform int uLightCount;
210
213uniform int uIterations;
214
215uniform float uSpacing;
216
217
218#define MINIMUM_LIGHT_VISIBILITY_DIST 0.1
219
220struct RayHit
221{
222 vec3 pos;
223 float t;
225 uint mn_idx;
226 vec3 normal;
227 uint tri_idx;
228 float best_t;
229 float blocked_t;
230 bool hit;
231 bool occluded;
232 uint pad1;
233
234};
235
236
237
238uint state = 0;
239
240uint pcg_hash() {
241 state = state * 747796405u + 2891336453u;
242 uint word = ((state >> ((state >> 28u) + 4u)) ^ state) * 277803737u;
243 return (word >> 22u) ^ word;
244}
245
246
247uint wang_hash(uint seed) {
248 seed = (seed ^ 61u) ^ (seed >> 16u);
249 seed *= 9u;
250 seed = seed ^ (seed >> 4u);
251 seed *= 0x27d4eb2du;
252 seed = seed ^ (seed >> 15u);
253 return seed;
254}
255
256float rand() {
257 return float(pcg_hash()) / 4294967296.0;
258}
259
260void frisvad_basis(in vec3 n, out vec3 b1, out vec3 b2) {
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;
264 return;
265 }
266
267 float a = 1.0f / (1.0f + n.z);
268 float b = -n.x * n.y * a;
269
270 b1.x = 1.0f - n.x * n.x * a;
271 b1.y = b;
272 b1.z = -n.x;
273
274 b2.x = b;
275 b2.y = 1.0f - n.y * n.y * a;
276 b2.z = -n.y;
277}
278
280 float r1 = rand();
281 float r2 = rand();
282
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);
287 vec3 local_dir = vec3(x, y, z);
288
289 vec3 tangent;
290 vec3 bitangent;
291 if (abs(normal.z) > 0.999)
292 {
293
294 frisvad_basis(normal, tangent, bitangent);
295 }
296 else
297 {
298
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);
302 }
303
304 mat3 TBN = mat3(tangent, bitangent, normal);
305
306 return normalize(TBN * local_dir);
307}
309{
310
311 float phi = 3.14159265 * (3.0f - sqrt(5.0f));
312
313 float y = 1.0f - 2.0f*(i+0.5f)/N;
314
315 float r = sqrt(1-y*y);
316
317 float theta = phi*i;
318
319 return vec3(cos(theta) * r, y, sin(theta) * r);
320}
321
323
324bool ray_intersects_triangle(vec3 origin, vec3 dir, vec3 a, vec3 b, vec3 c, out RayHit out_hit)
325{
326
327
328 out_hit.hit = false;
329
330 vec3 edge1 = b-a;
331 vec3 edge2 = c-a;
332 vec3 h = cross(dir, edge2);
333 float dA = dot(edge1, h);
334
335 if (abs(dA) < 0.000001f) {
336 return false;
337 }
338
339 float f = 1.0f / dA;
340 vec3 s = origin - a;
341 float u = f * dot(s, h);
342 if (u < 0.0f || u > 1.0f) {
343 return false;
344 }
345
346 vec3 q = cross(s, edge1);
347 float v = f * dot(dir, q);
348 if (v < 0.0f || u + v > 1.0f) {
349 return false;
350 }
351
352 float t = f * dot(edge2, q);
353 if (t > 0.0f) {
354 out_hit.t = t;
355
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);
359 out_hit.hit = true;
360
361 return true;
362 }
363
364 return false;
365}
366
367
368
369
370bool slab_test(BVHNode n, vec3 pos, vec3 inv_dir) {
371 vec3 t1 = (n.min - pos) * inv_dir;
372 vec3 t2 = (n.max - pos) * inv_dir;
373
374 vec3 tmin = min(t1, t2);
375 vec3 tmax = max(t1, t2);
376
377 float t_near = max(max(tmin.x, tmin.y), tmin.z);
378 float t_far = min(min(tmax.x, tmax.y), tmax.z);
379
380 return t_far >= max(0.0f, t_near) && t_near < hit.t;
381}
382vec2 get_uvs(vec3 B, uint tri_idx)
383{
384
385 Face tri = tris[tri_idx];
386 Vertex v0 = tri.v0;
387 Vertex v1 = tri.v1;
388 Vertex v2 = tri.v2;
389
390 vec3 b = B;
391 vec2 hit_uv = v0.uv * b.x + v1.uv * b.y + v2.uv * b.z;
392 return hit_uv;
393}
395{
396
398 vec2 hit_uv = tri.v0.uv * b.x + tri.v1.uv * b.y + tri.v2.uv * b.z;
399 return hit_uv;
400
401}
403
404 vec4 albedo = texture(mat.albedo_tex, hit_uv) * mat.diffuse;
405
406 return albedo;
407}
409
410 vec3 emissive = texture(mat.emissive_tex, hit_uv).rgb * mat.emissive.rgb * mat.emissive_strength;
411
412 return emissive;
413}
414
415
416void traverse_mesh_bvh(uint node_offset, uint mn_id, vec3 pp, vec3 pm, vec3 original_dir, vec3 dir, vec3 md, uint mesh, uint tri)
417{
418
419 int ni = int(node_offset);
420 while (ni >= 0)
421 {
422 BVHNode n = mesh_bvh_nodes[ni];
423
424 if (!slab_test(n, pm, md))
425 {
426 ni = n.next;
427 continue;
428 }
429
430 if (n.left == -1 && n.right == -1)
431 {
432 for (int i = 0; i < n.count; i++)
433 {
434
435 uint tri_idx = tris[i + n.first_index].bvh_index;
436 Face f = tris[tri_idx];
437
438 vec3 a = f.v0.pos;
439 vec3 b = f.v1.pos;
440 vec3 c = f.v2.pos;
441
442 RayHit current_hit;
443 bool did_hit = ray_intersects_triangle(pm, dir, a, b, c, current_hit);
444 if (!did_hit) continue;
445
446
447 if (current_hit.t < hit.best_t)
448 {
449 hit.occluded = true;
450 hit.best_t = current_hit.t;
451 }
452 bool col = (current_hit.t < hit.t);
453
454 bool dot_test = dot(f.normal, dir) <= 0.0f;
455 vec2 uvs = get_hit_uvs(f, current_hit);
456 Material mat = materials[f.material_id];
457 vec4 albedo = get_hit_albedo(mat, uvs);
458 if (dot_test || albedo.a < 1.0f)
459 {
460 if (col)
461 {
462 float temp_best_t = hit.best_t;
463 float temp_blocked_t = hit.blocked_t;
464 hit = current_hit;
465 hit.occluded = true;
466 hit.best_t = temp_best_t;
467 hit.blocked_t = temp_blocked_t;
468 hit.mn_idx = mn_id;
469 hit.tri_idx = tri_idx;
470 hit.normal = f.normal;
471 hit.hit = true;
472
473 }
474 }
475 else
476 {
477 if (current_hit.t < hit.blocked_t)
478 {
479
480 hit.blocked_t = current_hit.t;
481 hit.hit = true;
482 }
483
484 hit.occluded = true;
485
486 }
487 }
488 ni = n.next;
489 }
490 else
491 {
492
493 ni = n.left;
494 }
495
496
497 }
498
499
500
501}
502
503
504void traverse_scene_bvh(vec3 pp, vec3 dir, vec3 inv_dir, uint light_type, uint mesh, uint tri)
505{
506 int ni = 0;
507 while (ni >= 0 )
508 {
509 BVHNode n = scene_bvh_nodes[ni];
510
511 if (!slab_test(n, pp, inv_dir))
512 {
513 ni = n.next;
514 continue;
515 }
516
517 if (n.left == -1 && n.right == -1)
518 {
519 for (int i = 0; i < n.count; i++)
520 {
521
522 MeshNode mn = mesh_nodes[mesh_nodes[i + n.first_index].bvh_index];
523 if (mn.is_static == 0) continue;
524 if (mn.mesh_id == -1) continue;
525
526 mat4 inverse_matrix = mn.inverse_global_matrix;
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;
530
531 traverse_mesh_bvh(mn.node_offset, mn.mesh_node_id, pp, pm, dir, md, inv_md, mesh, tri);
532 }
533 ni = n.next;
534
535 }
536 else
537 {
538 ni = n.left;
539 }
540 }
541
542}
543
545{
546
547 vec3 v0 = b - a;
548 vec3 v1 = c - a;
549 vec3 v2 = p - a;
550
551 vec3 tn = cross(v0, v1);
552 float ta = length(tn);
553
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);
558
559}
560
561float CalculateAttenuation(float distance, float radius, float intensity) {
562 float attenuation = intensity / (distance + 1.0);
563
564 float factor = distance / (radius * radius);
565 float window = clamp(1.0 - factor * factor * factor * factor, 0.0, 1.0);
566 window = window * window;
567
568 return attenuation * window;
569}
570
571
572vec3 solve_light_source(Light l, vec3 pos, vec3 normal, uint mesh, uint tri)
573{
574 vec3 L;
575 float d2;
576 float d;
577 float safe_d2;
578 // directional
579 uint light_type = l.light_type;
580 if (l.light_type == 1)
581 {
582 L = l.dir;
583 d2 = 1;
584 d = 1;
585
586 safe_d2 = 1;
587 }
588 else
589 {
590 L = (l.pos - pos);
591 d2 = dot(L, L);
592 if (d2 > l.radius * l.radius && l.light_type == 0) return vec3(0,0,0);
593
594
595 float min_radius = 0.05f;
596 safe_d2 = max(d2, min_radius * min_radius);
597 }
598
599 L = normalize(L);
600 vec3 inv_dir = 1.0f / L;
601
602 vec3 pp = pos + normal * 0.001f;
603
604 vec3 throughput = vec3(1.0);
605
606 bool hit_light = false;
607 int safety = 10;
608 int cur = 0;
609 while (!hit_light && cur < safety)
610 {
611 cur++;
612
613 hit.hit = false;
614 hit.t = 100000;
615 hit.best_t = 1000000;
616 if (l.light_type != 1)
617 {
618 hit.t = d;
619 hit.best_t = d;
620 hit.blocked_t = d;
621
622 }
623 hit.blocked_t = 1000000;
624 hit.tri_idx = 0;
625 hit.mn_idx = 0;
626 hit.occluded = false;
627
628 traverse_scene_bvh(pp, L, inv_dir, light_type, mesh, tri);
629
630 if (hit.hit && l.light_type == 1 || (l.light_type != 1 && hit.hit && hit.t < d))
631 {
632
633 Face hit_tri = tris[hit.tri_idx];
634 uint mat_id = hit_tri.material_id;
635 vec2 hit_uv = get_hit_uvs(hit_tri, hit);
636 Material mat = materials[mat_id];
637 vec4 albedo = get_hit_albedo(mat, hit_uv);
638 if (albedo.a < 1.0f)
639 {
640 throughput *= albedo.rgb;
641 MeshNode hit_mn = mesh_nodes[hit.mn_idx];
642 hit.normal = mat3(transpose(hit_mn.inverse_global_matrix)) * hit_tri.normal;
643 pp = hit.pos + L * 0.001f;
644 d = length(pp - l.pos);
645 }
646 else
647 {
648 break;
649 }
650
651 }
652 else
653 {
654 hit_light = true;
655 }
656
657
658 }
659
660
661 vec3 lighting = l.color * max(dot(normal, L), 0.0f) * throughput;
662 if (!hit_light) return vec3(0,0,0);
663
664 if (l.light_type == 1)
665 {
666 lighting *= l.intensity;
667 }
668 else if (l.light_type == 0)
669 {
670 lighting *= CalculateAttenuation(d2, l.radius, l.intensity);
671 }
672 else
673 {
674
675 float theta = dot(-L, l.dir);
676
677 float intensity = clamp((theta - l.outer_cos) * l.cos_diff, 0.0f, 1.0f);
678 lighting /= max(safe_d2, 0.001);
679 lighting *= intensity;
680 lighting *= l.intensity;
681
682 }
683
684 return lighting;
685
686}
687
689{
690 float r1 = rand();
691 float r2 = rand();
692
693 float sqrt_r1 = sqrt(r1);
694 return (1.0 - sqrt_r1) * a + (sqrt_r1 * (1.0 - r2)) * b + (sqrt_r1 * r2) * c;
695
696}
697
698vec3 process_area_light(in Light light, vec3 pos, vec3 normal, vec3 brdf, float p_select, bool skip_normal)
699{
700
701 MeshNode mn = mesh_nodes[light.mesh];
702 Face f = tris[mn.tri_offset + light.tri];
703
704 vec3 area_normal = mat3(transpose(mn.inverse_global_matrix)) * f.normal;
705 vec3 A = (mn.global_matrix * vec4(f.v0.pos, 1.0)).xyz;
706 vec3 B = (mn.global_matrix * vec4(f.v1.pos, 1.0)).xyz;
707 vec3 C = (mn.global_matrix * vec4(f.v2.pos, 1.0)).xyz;
708 vec3 random_pos = random_pos_on_triangle(A, B, C);
709 vec3 bary = compute_3d_barycentric(A, B, C, random_pos);
710 vec2 uvs = get_uvs(bary, mn.tri_offset + light.tri);
711 Material mat = materials[f.material_id];
712 vec3 emission = get_hit_emissive(mat, uvs);
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;
719 int cur = 0;
720 int safety = 10;
721 vec3 pp = pos + normal * 0.01f;
722 vec3 throughput = vec3(1.0);
723 while (!hit_light && cur < safety)
724 {
725 cur++;
726
727 hit.hit = false;
728 hit.t = dist;
729 hit.best_t = dist;
730 hit.blocked_t = 1000000;
731 hit.tri_idx = 0;
732 hit.mn_idx = 0;
733 hit.occluded = false;
734
735 traverse_scene_bvh(pp, L, inv_dir, 3, 0, 0);
736
737 if (hit.hit && hit.t < dist)
738 {
739
740 MeshNode hit_mn = mesh_nodes[hit.mn_idx];
741 Face hit_tri = tris[hit.tri_idx];
742 vec2 hit_uv = get_hit_uvs(hit_tri, hit);
743 uint mat_id = hit_tri.material_id;
744 Material hit_mat = materials[mat_id];
745 vec4 albedo = get_hit_albedo(hit_mat, hit_uv);
746 if (hit.blocked_t < hit.t)
747 {
748 return vec3(0,0,0);
749 }
750 if (albedo.a < 1.0f)
751 {
752 throughput *= albedo.rgb;
753 MeshNode hit_mn = mesh_nodes[hit.mn_idx];
754 hit.normal = mat3(transpose(hit_mn.inverse_global_matrix)) * hit_tri.normal;
755 pp = hit.pos + L * 0.001f;
756 diff = random_pos - pp;
757 dist = length(diff);
758 }
759 else
760 {
761 vec3 emissive = get_hit_emissive(hit_mat, hit_uv);
762 hit.normal = mat3(transpose(hit_mn.inverse_global_matrix)) * hit_tri.normal;
763 if (length(emissive) == 0 && f.material_id != mat_id)
764 {
765
766 return vec3(0,0,0);
767 }
768 else
769 {
770 area_normal = hit.normal;
771 hit_light = true;
772 }
773 }
774
775 }
776 else
777 {
778 hit_light = true;
779 }
780
781 }
782 float cos_light = dot(area_normal, -L);
783 if (cos_light <= 0.001)
784 {
785 return vec3(0,0,0);
786 }
787
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);
790 return color;
791}
792
793vec3 process_light(vec3 pos, vec3 normal, uint mesh, uint tri, vec3 brdf, Light light, float p_select)
794{
795
796
797 if (light.light_type == 0)
798 {
799
800 return solve_light_source(light, pos, normal, mesh, tri) * brdf / p_select;
801 }
802 else if (light.light_type == 1)
803 {
804 return vec3(0,0,0);
805
806 }
807 else if (light.light_type == 2)
808 {
809
810 return solve_light_source(light, pos, normal, mesh, tri) * brdf / p_select;
811 }
812 else
813 {
814 return process_area_light(light, pos, normal, brdf, p_select, false);
815
816 }
817 return vec3(0.0);
818
819}
820
821
822
823
825{
826 return clamp(pos, min, max);
827}
828
829bool pos_inside_aabb(vec3 pos, vec3 min, vec3 max)
830{
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;
834 return false;
835}
836
837vec3 traverse_light_bvh(vec3 pos, vec3 normal, uint mesh, uint tri, vec3 brdf)
838{
839
840 int ni = 0;
841 float pdf = 1.0f;
842
843
844 int dir_light_idx = -1;
845 for (int i = 0; i < uLightCount; i++)
846 {
847
848 if (lights[i].light_type == 1)
849 {
850 dir_light_idx = i;
851 break;
852 }
853
854 }
855
856
857
858 vec3 color = vec3(0.0);
859 while (ni >= 0)
860 {
861
862 BVHNode n = light_bvh_nodes[ni];
863
864
865 if (n.left == -1 && n.right == -1)
866 {
867 int random_light_idx = int(rand() * n.count);
868 if (random_light_idx >= n.count) random_light_idx = n.count-1;
869
870 uint index = lights[n.first_index + random_light_idx].bvh_index;
871 Light l = lights[index];
872
873 color = process_light(pos, normal, mesh, tri, brdf, l, pdf);
874
875 break;
876 }
877 else
878 {
879
880 BVHNode n0 = light_bvh_nodes[n.left];
881 BVHNode n1 = light_bvh_nodes[n.right];
882
883 vec3 closest_n0 = closest_point_on_aabb(pos, n0.min, n0.max);
884 vec3 closest_n1 = closest_point_on_aabb(pos, n1.min, n1.max);
885
886 vec3 diff_n0 = pos - closest_n0;
887 vec3 diff_n1 = pos - closest_n1;
888
889 float dist2_n0 = dot(diff_n0, diff_n0);
890 float dist2_n1 = dot(diff_n1, diff_n1);
891
892 dist2_n0 = max(dist2_n0, 0.001f);
893 dist2_n1 = max(dist2_n1, 0.001f);
894
895 float importance_n0 = n0.flux / dist2_n0;
896 float importance_n1 = n1.flux / dist2_n1;
897
898 float sum = importance_n0 + importance_n1;
899 if (sum <= 0.0f)
900 {
901 importance_n0 = 0.5f;
902 importance_n1 = 0.5f;
903 sum = 1.0f;
904 }
905 float norm_n0 = importance_n0 / sum;
906 float norm_n1 = importance_n1 / sum;
907
908 float p = rand();
909 if (p < norm_n0)
910 {
911 // enter left
912 pdf *= max(norm_n0, 0.0001f);
913 ni = n.left;
914 continue;
915 }
916 else
917 {
918 // enter right
919 pdf *= max(norm_n1, 0.0001f);
920 ni = n.right;
921 continue;
922 }
923 }
924 }
925 if (length(color) <= 0.0f && dir_light_idx != -1)
926 {
927
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;
936
937 float p = rand();
938 pdf = max(norm_dir, 0.0001f);
939
940 return solve_light_source(directional_light, pos, normal, mesh, tri) * brdf ;
941 }
942
943 return color;
944
945}
946
947
949{
950
951 int ni = 0;
952 float pdf = 1.0f;
953 while (ni >= 0)
954 {
955
956 BVHNode n = light_bvh_nodes[ni];
957
958
959 if (n.left == -1 && n.right == -1)
960 {
961 int random_light_idx = int(rand() * n.count);
962 if (random_light_idx >= n.count) random_light_idx = n.count-1;
963
964 uint index = lights[n.first_index + random_light_idx].bvh_index;
965 Light l = lights[index];
966 vec3 color = process_area_light(l, pos, normal, brdf, max(pdf, 0.00001f), true);
967
968 return color;
969 }
970 else
971 {
972
973 BVHNode n0 = light_bvh_nodes[n.left];
974 BVHNode n1 = light_bvh_nodes[n.right];
975
976 vec3 closest_n0 = closest_point_on_aabb(pos, n0.min, n0.max);
977 vec3 closest_n1 = closest_point_on_aabb(pos, n1.min, n1.max);
978
979 vec3 diff_n0 = pos - closest_n0;
980 vec3 diff_n1 = pos - closest_n1;
981
982 float dist2_n0 = dot(diff_n0, diff_n0);
983 float dist2_n1 = dot(diff_n1, diff_n1);
984
985 dist2_n0 = max(dist2_n0, 0.001f);
986 dist2_n1 = max(dist2_n1, 0.001f);
987
988 float importance_n0 = n0.emissive_flux / dist2_n0;
989 float importance_n1 = n1.emissive_flux / dist2_n1;
990
991 float sum = importance_n0 + importance_n1;
992 if (sum <= 0.0f)
993 {
994 importance_n0 = 0.5f;
995 importance_n1 = 0.5f;
996 sum = 1.0f;
997 }
998 float norm_n0 = importance_n0 / sum;
999 float norm_n1 = importance_n1 / sum;
1000
1001 float p = rand();
1002 if (p < norm_n0)
1003 {
1004 // enter left
1005 pdf *= max(norm_n0, 0.0001f);
1006 ni = n.left;
1007 continue;
1008 }
1009 else
1010 {
1011 // enter right
1012 pdf *= max(norm_n1, 0.0001f);
1013 ni = n.right;
1014 continue;
1015 }
1016 }
1017 }
1018
1019 return vec3(0.0);
1020
1021}
1022
1023
1024vec3 resolve_transparents(vec3 throughput, vec3 pos, vec3 dir, vec3 inv_dir)
1025{
1026 bool hit_surface = false;
1027 int safety = 10;
1028 int cur = 0;
1029 while (!hit_surface && cur < safety)
1030 {
1031 cur++;
1032
1033 hit.hit = false;
1034 hit.t = 100000;
1035 hit.best_t = 1000000;
1036 hit.blocked_t = 1000000;
1037 hit.tri_idx = 0;
1038 hit.mn_idx = 0;
1039 hit.occluded = false;
1040
1041 traverse_scene_bvh(pos, dir, inv_dir, 0, 0, 0);
1042
1043 if (hit.hit && hit.blocked_t >= hit.t)
1044 {
1045
1046 Face hit_tri = tris[hit.tri_idx];
1047 uint mat_id = hit_tri.material_id;
1048 vec2 hit_uv = get_hit_uvs(hit_tri, hit);
1049 Material mat = materials[mat_id];
1050 vec4 albedo = get_hit_albedo(mat, hit_uv);
1051 float r = rand();
1052 if (r < albedo.a)
1053 {
1054 break;
1055 }
1056 else
1057 {
1058 throughput *= albedo.rgb;
1059 MeshNode hit_mn = mesh_nodes[hit.mn_idx];
1060 hit.normal = mat3(transpose(hit_mn.inverse_global_matrix)) * hit_tri.normal;
1061 pos = hit.pos + dir * 0.01f;
1062 }
1063
1064 }
1065 else
1066 {
1067 hit_surface = true;
1068 }
1069 }
1070 return throughput;
1071}
1072
1073
1074
1076
1078{
1079 vec3 throughput = vec3(1.0);
1080 vec3 acc = vec3(0.0);
1081 vec3 current_pos;
1082 vec3 current_normal;
1083 uint current_mn_id;
1084 uint current_tri_idx;
1085
1086 vec3 brdf = vec3(0.5, 0.5, 0.5) * (1.0f / 3.14159265f);
1087
1089 dir = normalize(dir);
1090 acc += traverse_light_bvh_direct_emissives(pos, dir, brdf);
1091
1092 first_dir = dir;
1093
1094 vec3 inv_dir = 1.0/dir;
1095
1096 throughput = resolve_transparents(throughput, pos, dir, inv_dir);
1097
1098 bool skip_bounce = false;
1099
1100 if (!hit.hit)
1101 {
1102 if (!hit.occluded)
1103 {
1104
1105
1106 vec3 corrected = vec3(dir.x, dir.z, -dir.y);
1107 vec3 sky_light = texture(uSkybox, corrected).rgb;
1108 acc += throughput * sky_light;
1109 }
1110 skip_bounce = true;
1111 }
1112 else if (hit.blocked_t >= hit.t)
1113 {
1114 MeshNode hit_mn = mesh_nodes[hit.mn_idx];
1115 Face hit_tri = tris[hit.tri_idx];
1116 hit.normal = mat3(transpose(hit_mn.inverse_global_matrix)) * hit_tri.normal;
1117 hit.pos = (hit_mn.global_matrix * vec4(hit.pos, 1.0)).xyz;
1118
1119 vec3 direct_light = vec3(0.0);
1120 uint mat_id = hit_tri.material_id;
1121 vec2 hit_uv = get_hit_uvs(hit_tri, hit);
1122 Material mat = materials[mat_id];
1123 vec4 albedo = get_hit_albedo(mat, hit_uv);
1124 vec3 emissive = get_hit_emissive(mat, hit_uv);
1125 vec3 brdf = albedo.rgb * (1.0f / 3.14159265f);
1126
1127 current_pos = hit.pos;
1128 current_normal = hit.normal;
1129 current_mn_id = uint(hit.mn_idx);
1130 current_tri_idx = uint(hit.tri_idx);
1131
1132 direct_light += traverse_light_bvh(hit.pos, hit.normal, uint(hit.mn_idx), uint(hit.tri_idx), brdf);
1133
1134 float max_lum = 10.0;
1135 float lum = dot(direct_light, vec3(0.2126, 0.7152, 0.0722));
1136 if (lum > max_lum)
1137 {
1138 direct_light *= max_lum / lum;
1139 }
1140 acc += throughput * direct_light;
1141
1142 throughput *= albedo.rgb;
1143
1144 }
1145 else
1146 {
1147 skip_bounce = true;
1148 }
1149
1150
1151
1152 if (!skip_bounce)
1153 {
1154
1155 for (int i = 0; i < 1; i++)
1156 {
1157
1158 dir = get_bounce_direction(current_normal);
1159 vec3 inv_dir = 1.0/dir;
1160 float pdf = dot(dir, current_normal) / 3.14159265f;
1161
1162 vec3 pp = current_pos + current_normal * 0.001f;
1163
1164 throughput = resolve_transparents(throughput, pp, dir, inv_dir);
1165 if (!hit.hit)
1166 {
1167 if (!hit.occluded)
1168 {
1169
1170
1171 vec3 corrected = vec3(dir.x, dir.z, -dir.y);
1172 vec3 sky_light = texture(uSkybox, corrected).rgb;
1173 acc += throughput * sky_light;
1174
1175 }
1176 break;
1177 }
1178 else if (hit.blocked_t >= hit.t)
1179 {
1180
1181 MeshNode hit_mn = mesh_nodes[hit.mn_idx];
1182 Face hit_tri = tris[hit.tri_idx];
1183 hit.normal = mat3(transpose(hit_mn.inverse_global_matrix)) * hit_tri.normal;
1184 hit.pos = (hit_mn.global_matrix * vec4(hit.pos, 1.0)).xyz;
1185
1186 vec3 direct_light = vec3(0.0);
1187 uint mat_id = hit_tri.material_id;
1188 vec2 hit_uv = get_hit_uvs(hit_tri, hit);
1189 Material mat = materials[mat_id];
1190 vec4 albedo = get_hit_albedo(mat, hit_uv);
1191 vec3 emissive = get_hit_emissive(mat, hit_uv);
1192 vec3 brdf = albedo.rgb * (1.0f / 3.14159265f);
1193
1194 current_pos = hit.pos;
1195 current_normal = hit.normal;
1196 current_mn_id = uint(hit.mn_idx);
1197 current_tri_idx = uint(hit.tri_idx);
1198
1199 direct_light += traverse_light_bvh(hit.pos, hit.normal, uint(hit.mn_idx), uint(hit.tri_idx), brdf);
1200
1201 float max_lum = 10.0;
1202 float lum = dot(direct_light, vec3(0.2126, 0.7152, 0.0722));
1203 if (lum > max_lum)
1204 {
1205 direct_light *= max_lum / lum;
1206 }
1207 acc += throughput * direct_light;
1208
1209 throughput *= albedo.rgb;
1210
1211
1212 }
1213 else
1214 {
1215 break;
1216 }
1217
1218 }
1219 }
1220
1221
1222
1223 return acc;
1224
1225}
1226
1227
1228
1229void solve_SH(vec3 color, vec3 dir, int index, vec3 pos)
1230{
1231
1232
1233 vec3 p0 = color * 0.282095;
1234 vec3 p1 = color * 0.488603 * dir.y;
1235 vec3 p2 = color * 0.488603 * dir.z;
1236 vec3 p3 = color * 0.488603 * dir.x;
1237 vec3 p4 = color * 1.092548 * dir.x * dir.y;
1238 vec3 p5 = color * 1.092548 * dir.y * dir.z;
1239 vec3 p6 = color * 0.315392 * (3 * dir.z * dir.z - 1);
1240 vec3 p7 = color * 1.092548 * (dir.x * dir.z);
1241 vec3 p8 = color * 0.546274 * (dir.x * dir.x - dir.y * dir.y);
1242
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;
1252
1253}
1254
1255
1256
1257void main()
1258{
1259
1260 ivec3 coord = ivec3(gl_GlobalInvocationID.xyz);
1261 if (coord.x >= uProbeSize.x || coord.y >= uProbeSize.y || coord.z >= uProbeSize.z) return;
1262
1263 int index = coord.z * uProbeSize.y * uProbeSize.x + coord.y * uProbeSize.x + coord.x;
1264 vec3 pos = probes[index].pos;
1265
1266 uint seed = uint(coord.x) + uint(coord.y) * 1973u + uint(coord.z) * 9277u;
1267 state = wang_hash(seed ^ (uint(uCurrentIteration) * 2699u));
1268
1269 vec3 ray_color = trace_path(pos);
1270
1271 solve_SH(ray_color, first_dir, index, pos);
1272
1273
1274
1275}
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
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)
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()
void main()
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.
float intensity
The intensity of the Light.
sampler2D albedo_tex
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.
sampler2D emissive_tex
vec3 dir
Direction for lights that are not POINT_LIGHTs.
float outer_cos
float emissive_strength
uint skip_directional_light
float radius
The radius of the 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 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)
uniform int uLightCount
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)
SDL_Window * window
Definition main.c:519
float clamp(float a, float b, float c)
Definition mathf.h:379
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...
vec3 first_dir
uniform int uCurrentIteration
uniform vec3 uWorldMin
int index
The index of the node inside of the array.
int right
Right child.
int parent
Parent.
uniform float uSpacing
int left
Left child.
float emissive_flux
The only-emissive flux.
Nodes for the acceleration structures of BVHs.
vec4 coefficients[9]
@ B
Definition types.h:26
@ A
Definition types.h:26
@ C
Definition types.h:26
float x
Definition types.h:12
int y
Definition types.h:15
float y
Definition types.h:12
int z
Definition types.h:15
float z
Definition types.h:12
int x
Definition types.h:15
Definition types.h:14
Definition types.h:15
Definition types.h:20
Definition types.h:21
Definition types.h:11
Definition types.h:12
Definition types.h:13
uniform vec3 color
Definition wireframe.frag:6