Untitled
Loading...
Searching...
No Matches
lightmap_bounce.comp
Go to the documentation of this file.
1
2
3#version 430 core
4#extension GL_ARB_bindless_texture : require
5
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;
9
10struct Light
11{
12
13 vec3 pos;
15
16 vec3 dir;
17 float outer_cos;
18
20 float cos_diff;
21
22 float intensity;
24 float inner_angle;
25 float outer_angle;
26
27 float radius;
28 uint layer;
29 uint mesh;
30 uint tri;
31
32 float area;
34 uint dynamic;
35 uint pad2;
36
37};
38
39layout(std430, binding = 0) buffer LightData {
40
41 Light lights[];
42
43};
44
45struct Material {
46
47 vec4 ambient;
48 vec4 diffuse;
49 vec4 specular;
50 vec4 emissive;
51
53 uint pad0;
54 uint pad1;
56 uint pad2;
57 uint pad3;
58
59 sampler2D albedo_tex;
60 sampler2D normal_tex;
61 sampler2D arm_tex;
62 sampler2D emissive_tex;
63
64 float metallic;
65 float roughness;
66 float ao_strength;
67 float normal_scale;
68 uint alpha_mode;
69 float alpha_cutoff;
70 uint double_sided;
72
73};
74
75layout(std430, binding = 2) buffer MaterialData {
76 Material materials[];
77};
78
79
80
81struct Vertex {
82
83 vec3 pos;
84 uint pad0;
85 vec3 normal;
86 uint pad1;
87 vec2 uv;
88 uint pad2;
89 uint pad3;
90
91};
92
93layout(std430, binding = 4) buffer VertexData {
94 Vertex vertices[];
95};
96
97struct Face {
98
99 uvec3 indices;
100 uint material_id;
101 vec3 normal;
102 uint mesh_id;
103 uint bvh_index;
104 uint tri_idx;
105 uint pad0;
106 uint pad1;
107 Vertex v0;
108 Vertex v1;
109 Vertex v2;
110
111};
112
113layout(std430, binding = 5) buffer TriData {
114 Face tris[];
115};
116
117struct MeshNode {
118
119 uint mesh_node_id;
120 uint mesh_id;
121
122 uint node_count;
123 uint node_offset;
124 uint vertex_offset;
125 uint vertex_count;
126 uint tri_offset;
127 uint tri_count;
128 uint bvh_index;
129 uint is_static;
131 uint pad2;
132
133 mat4 global_matrix;
135
136};
137
138layout(std430, binding = 6) buffer MeshData {
139 MeshNode mesh_nodes[];
140};
141
142struct AABB
143{
144
145 vec3 min;
146 uint pad0;
147 vec3 max;
148 uint pad1;
149 vec3 center;
150 uint pad2;
151 vec3 extents;
152 uint pad3;
153
154};
155
156
157struct BVHNode
158{
159
160 int left;
161 int right;
162 int parent;
163 int index;
164
165 int first_index;
166 int count;
167 int next;
168 float flux;
169
170 vec3 min;
171 float emissive_flux;
172 vec3 max;
173 uint pad2;
174 vec3 center;
175 uint pad3;
176 vec3 extents;
177 uint pad4;
178
179};
180
181layout(std430, binding = 7) buffer MeshBVHData {
182 BVHNode mesh_bvh_nodes[];
183};
184
185layout(std430, binding = 8) buffer SceneBVHData {
186 BVHNode scene_bvh_nodes[];
187};
188// #define DIRECTIONAL_LIGHTS 4
189// #define POINT_LIGHTS 2048
190// #define EMISSIVE_TRIS 100000
191
192// struct EmissiveTriangle
193// {
194
195// vec3 A;
196// uint pad0;
197// vec3 B;
198// uint pad1;
199// vec3 C;
200// uint pad2;
201// vec3 normal;
202// uint pad3;
203// vec3 emissive;
204// uint pad4;
205// float area;
206// float strength;
207// float cdf;
208// uint pad5;
209
210// };
211// struct LMDebug
212// {
213// vec3 pos;
214// uint pad0;
215// vec3 dir;
216// uint pad1;
217// vec3 origin;
218// uint pad2;
219// };
220// #define LMDEBUG_SIZE 4096
221
222// layout(std430, binding = 10) buffer LightEmitterData {
223
224// int dir_count;
225// int point_count;
226// int tri_count;
227// uint sample_count;
228// float dir_w;
229// float point_w;
230// float tri_w;
231// float total_power;
232// Light directional_lights[DIRECTIONAL_LIGHTS];
233// Light point_lights[POINT_LIGHTS];
234// LMDebug LMDEBUG[LMDEBUG_SIZE];
235// EmissiveTriangle ems[];
236
237// };
238layout(std430, binding = 10) buffer LightBVHData {
239 BVHNode light_bvh_nodes[];
240};
241
242
243uniform sampler2D uLightmapPosTex;
244uniform sampler2D uLightmapNormalTex;
245uniform isampler2D uLightmapIDTex;
246uniform samplerCube uSkybox;
247
249uniform int uLightCount;
250uniform int uIteration;
251uniform int uLightmapWidth;
252uniform int uLightmapHeight;
253
254// vec4 color = texture(materials[mat_id].diffuse_tex, uv);
255
256#define MINIMUM_LIGHT_VISIBILITY_DIST 0.1
257
258struct RayHit
259{
260 vec3 pos;
261 float t;
263 uint mn_idx;
264 vec3 normal;
265 uint tri_idx;
266 float best_t;
267 float blocked_t;
268 bool hit;
269 bool occluded;
270 uint pad1;
271
272};
273
274
275
276uint state = 0;
277
278uint pcg_hash() {
279 uint prev = state;
280 state = prev * 747796405u + 2891336453u;
281 uint word = ((prev >> ((prev >> 28u) + 4u)) ^ prev) * 277803737u;
282 return (word >> 22u) ^ word;
283}
284
285uint wang_hash(uint seed) {
286 seed = (seed ^ 61u) ^ (seed >> 16u);
287 seed *= 9u;
288 seed = seed ^ (seed >> 4u);
289 seed *= 0x27d4eb2du;
290 seed = seed ^ (seed >> 15u);
291 return seed;
292}
293float rand() {
294 return float(pcg_hash()) / 4294967296.0;
295}
296
297
298
299
300// vec3 generate_bounce(uint i, uint N) {
301
302
303
304// int grid = 8;
305
306// int ix = i % grid;
307// int iy = i / grid;
308
309// float u = (ix + rand() / grid;
310// float v = (iy + rand() / grid;
311// float r = sqrt(u);
312// float theta = 2.0f * PI * v;
313
314// float x = r * cos(theta);
315// float y = r * sin(theta);
316// float z = sqrt(max(0, 1.0f - x*x - y*y));
317// return (vec3){x,y,z};
318// }
319
320void frisvad_basis(in vec3 n, out vec3 b1, out vec3 b2) {
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;
324 return;
325 }
326
327 float a = 1.0f / (1.0f + n.z);
328 float b = -n.x * n.y * a;
329
330 b1.x = 1.0f - n.x * n.x * a;
331 b1.y = b;
332 b1.z = -n.x;
333
334 b2.x = b;
335 b2.y = 1.0f - n.y * n.y * a;
336 b2.z = -n.y;
337}
338
340 float r1 = rand();
341 float r2 = rand();
342
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);
347 vec3 local_dir = vec3(x, y, z);
348
349 // vec3 up = abs(normal.z) < 0.999 ? vec3(0.0, 0.0, 1.0) : vec3(1.0, 0.0, 0.0);
350 // vec3 tangent = normalize(cross(up, normal));
351 // vec3 bitangent = cross(normal, tangent);
352 vec3 tangent;
353 vec3 bitangent;
354 if (abs(normal.z) > 0.999)
355 {
356
357 frisvad_basis(normal, tangent, bitangent);
358 }
359 else
360 {
361
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);
365 }
366
367 mat3 TBN = mat3(tangent, bitangent, normal);
368
369 return normalize(TBN * local_dir);
370}
371
372
373// bool slab_test(BVHNode n, vec3 pos, vec3 inv_dir)
374// {
375
376// float tx1 = (n.min.x - pos.x) * inv_dir.x;
377// float tx2 = (n.max.x - pos.x) * inv_dir.x;
378// float ty1 = (n.min.y - pos.y) * inv_dir.y;
379// float ty2 = (n.max.y - pos.y) * inv_dir.y;
380// float tz1 = (n.min.z - pos.z) * inv_dir.z;
381// float tz2 = (n.max.z - pos.z) * inv_dir.z;
382// float txmin = min(tx1, tx2);
383// float txmax = max(tx1, tx2);
384// float tymin = min(ty1, ty2);
385// float tymax = max(ty1, ty2);
386// float tzmin = min(tz1, tz2);
387// float tzmax = max(tz1, tz2);
388
389// float t_enter = max(txmin, max(tymin, tzmin));
390
391// float t_exit = min(txmax, min(tymax, tzmax));
392
393// if (t_enter > t_exit) return false;
394// if (t_exit < 0) return false;
395
396// return true;
397
398
399// }
400
402
403bool ray_intersects_triangle(vec3 origin, vec3 dir, vec3 a, vec3 b, vec3 c, out RayHit out_hit)
404{
405
406 out_hit.hit = false;
407
408 vec3 edge1 = b-a;
409 vec3 edge2 = c-a;
410 vec3 h = cross(dir, edge2);
411 float dA = dot(edge1, h);
412
413 if (abs(dA) < 0.000001f) {
414 return false;
415 }
416
417 float f = 1.0f / dA;
418 vec3 s = origin - a;
419 float u = f * dot(s, h);
420 if (u < 0.0f || u > 1.0f) {
421 return false;
422 }
423
424 vec3 q = cross(s, edge1);
425 float v = f * dot(dir, q);
426 if (v < 0.0f || u + v > 1.0f) {
427 return false;
428 }
429
430 float t = f * dot(edge2, q);
431 if (t > 0.0) {
432 out_hit.t = t;
433
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);
437 out_hit.hit = true;
438
439 return true;
440 }
441
442 return false;
443}
444
445bool slab_test(in BVHNode n, vec3 pos, vec3 inv_dir) {
446 vec3 t1 = (n.min - pos) * inv_dir;
447 vec3 t2 = (n.max - pos) * inv_dir;
448
449 vec3 tmin = min(t1, t2);
450 vec3 tmax = max(t1, t2);
451
452 float t_near = max(max(tmin.x, tmin.y), tmin.z);
453 float t_far = min(min(tmax.x, tmax.y), tmax.z);
454
455 return t_far >= max(0.0f, t_near) && t_near < hit.t;
456
457
458 // float closest_valid_t = min(hit.t, hit.blocked_t);
459 // return (t_far >= max(0.0f, t_near) && t_near < closest_valid_t);
460}
461
463{
464
465 // Face tri = tris[hit.tri_idx];
466 // Vertex v0 = vertices[tri.indices.x];
467 // Vertex v1 = vertices[tri.indices.y];
468 // Vertex v2 = vertices[tri.indices.z];
469 // Vertex v0 = tri.v0;
470 // Vertex v1 = tri.v1;
471 // Vertex v2 = tri.v2;
472
474 vec2 hit_uv = tri.v0.uv * b.x + tri.v1.uv * b.y + tri.v2.uv * b.z;
475 return hit_uv;
476
477}
478
480
481 vec4 albedo = texture(mat.albedo_tex, hit_uv) * mat.diffuse;
482 // vec4 albedo = mat.diffuse;
483
484 return albedo;
485}
486float get_hit_metallic(in Material mat, vec2 hit_uv) {
487
488 float metallic = texture(mat.arm_tex, hit_uv).b * mat.metallic;
489 // vec4 albedo = mat.diffuse;
490
491 return metallic;
492}
494
495 vec3 emissive = texture(mat.emissive_tex, hit_uv).rgb * mat.emissive.rgb * mat.emissive_strength;
496
497 return emissive;
498}
499void traverse_mesh_bvh(uint node_offset, uint mn_id, vec3 pp, vec3 pm, vec3 original_dir, vec3 dir, vec3 md, uint mesh, uint tri)
500{
501 int ni = int(node_offset);
502
503 while (ni >= 0)
504 {
505 BVHNode n = mesh_bvh_nodes[ni];
506
507
508 if (!slab_test(n, pm, md))
509 {
510 ni = n.next;
511 continue;
512 }
513
514 if (n.left == -1 && n.right == -1)
515 {
516 for (int i = 0; i < n.count; i++)
517 {
518
519 // uint tri_idx = tris[i + n.first_index].bvh_index;
520 uint tri_idx = i + n.first_index;
521 Face f = tris[tri_idx];
522 // if (tri == f.tri_idx && mesh == mn_id) continue;
523
524 // vec3 a = vertices[f.indices.x].pos;
525 // vec3 b = vertices[f.indices.y].pos;
526 // vec3 c = vertices[f.indices.z].pos;
527 vec3 a = f.v0.pos;
528 vec3 b = f.v1.pos;
529 vec3 c = f.v2.pos;
530
531 RayHit current_hit;
532 bool did_hit = ray_intersects_triangle(pm, dir, a, b, c, current_hit);
533 if (!did_hit) continue;
534
535
536
537 if (current_hit.t < hit.best_t)
538 {
539 hit.occluded = true;
540 hit.best_t = current_hit.t;
541 }
542 bool col = (current_hit.t < hit.t);
543
544 bool dot_test = dot(f.normal, dir) <= 0.0f;
545 vec2 uvs = get_hit_uvs(f, current_hit);
546 Material mat = materials[f.material_id];
547 vec4 albedo = get_hit_albedo(mat, uvs);
548
549 if (dot_test || albedo.a < 1.0f)
550 // if (dot_test)
551 {
552
553 if (col)
554 {
555
556 float temp_best_t = hit.best_t;
557 float temp_blocked_t = hit.blocked_t;
558 hit = current_hit;
559 hit.occluded = true;
560 hit.best_t = temp_best_t;
561 hit.blocked_t = temp_blocked_t;
562 hit.mn_idx = mn_id;
563 hit.tri_idx = tri_idx;
564 hit.normal = f.normal;
565 hit.hit = true;
566 }
567 }
568 else
569 {
570
571
572 if (current_hit.t < hit.blocked_t)
573 {
574
575 hit.blocked_t = current_hit.t;
576 hit.hit = true;
577 }
578
579 hit.occluded = true;
580 }
581 }
582 ni = n.next;
583 }
584 else
585 {
586
587 ni = n.left;
588 }
589 }
590}
591
592
593void traverse_scene_bvh(vec3 pp, vec3 dir, vec3 inv_dir, uint light_type, uint mesh, uint tri)
594{
595 int ni = 0;
596 while (ni >= 0)
597 {
598 BVHNode n = scene_bvh_nodes[ni];
599
600 if (!slab_test(n, pp, inv_dir))
601 {
602 ni = n.next;
603 continue;
604 }
605
606 if (n.left == -1 && n.right == -1)
607 {
608 for (int i = 0; i < n.count; i++)
609 {
610
611 // MeshNode mn = mesh_nodes[mesh_nodes[i + n.first_index].bvh_index];
612 MeshNode mn = mesh_nodes[i + n.first_index];
613 if (mn.is_static == 0) continue;
614 if (mn.mesh_id == -1) continue;
615 // if (mesh_nodes[i + n.first_index].bvh_index == mesh) continue;
616 // if (mn.skip_directional_light == 1 && light_type == 1) continue;
617
618
619 mat4 inverse_matrix = mn.inverse_global_matrix;
620 vec3 pm = (inverse_matrix * vec4(pp, 1.0)).xyz;
621 vec3 md = (inverse_matrix * vec4(dir, 0.0)).xyz;
622
623 // vec3 md = (mat3(transpose(inverse_matrix)) * dir).xyz;
624 vec3 inv_md = 1.0f / md;
625
626 // const float EPSILON = 0.0000001f;
627 // inv_md.x = abs(md.x) < EPSILON ? (md.x < 0.0f ? -0.0001f : 0.0001f) : 1.0f / md.x;
628 // inv_md.y = abs(md.y) < EPSILON ? (md.y < 0.0f ? -0.0001f : 0.0001f) : 1.0f / md.y;
629 // inv_md.z = abs(md.z) < EPSILON ? (md.z < 0.0f ? -0.0001f : 0.0001f) : 1.0f / md.z;
630 traverse_mesh_bvh(mn.node_offset, mn.mesh_node_id, pp, pm, dir, md, inv_md, mesh, tri);
631 // traverse_mesh_bvh(mn, pp, pm, dir, inv_md, mesh, tri);
632 }
633 ni = n.next;
634
635 }
636 else
637 {
638 ni = n.left;
639 }
640 }
641
642}
643
645{
646
647 vec3 v0 = b - a;
648 vec3 v1 = c - a;
649 vec3 v2 = p - a;
650
651 vec3 tn = cross(v0, v1);
652 float ta = length(tn);
653
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);
658
659}
660vec2 get_uvs(vec3 B, uint tri_idx)
661{
662
663 Face tri = tris[tri_idx];
664 // Vertex v0 = vertices[tri.indices.x];
665 // Vertex v1 = vertices[tri.indices.y];
666 // Vertex v2 = vertices[tri.indices.z];
667 Vertex v0 = tri.v0;
668 Vertex v1 = tri.v1;
669 Vertex v2 = tri.v2;
670
671 vec3 b = B;
672 vec2 hit_uv = v0.uv * b.x + v1.uv * b.y + v2.uv * b.z;
673 return hit_uv;
674}
675
676float CalculateAttenuation(float distance, float radius, float intensity) {
677 float attenuation = intensity / (distance + 1.0);
678
679 float factor = distance / (radius * radius);
680 float window = clamp(1.0 - factor * factor * factor * factor, 0.0, 1.0);
681 window = window * window;
682
683 return attenuation * window;
684}
685
686vec3 solve_light_source(in Light l, vec3 pos, vec3 normal, uint mesh, uint tri)
687{
688 vec3 L;
689 float d2;
690 float d;
691 float safe_d2;
692 // directional
693 uint light_type = l.light_type;
694 if (l.light_type == 1)
695 {
696 L = l.dir;
697 d2 = 1;
698 d = 1;
699
700 safe_d2 = 1;
701 }
702 else
703 {
704 L = (l.pos - pos);
705 d2 = dot(L, L);
706 if (d2 > l.radius * l.radius && l.light_type == 0) return vec3(0,0,0);
707
708
709 float min_radius = 0.05f;
710 safe_d2 = max(d2, min_radius * min_radius);
711 d = sqrt(safe_d2);
712 }
713
714 L = normalize(L);
715 vec3 inv_dir = 1.0f / L;
716 vec3 pp = pos + normal * 0.001f;
717
718 vec3 throughput = vec3(1.0);
719
720 bool hit_light = false;
721 int safety = 10;
722 int cur = 0;
723 while (!hit_light && cur < safety)
724 {
725 cur++;
726
727 hit.hit = false;
728 hit.t = 100000;
729 hit.best_t = 1000000;
730 if (l.light_type != 1)
731 {
732 hit.t = d;
733 hit.best_t = d;
734 hit.blocked_t = d;
735
736 }
737 hit.blocked_t = 1000000;
738 hit.tri_idx = 0;
739 hit.mn_idx = 0;
740 hit.occluded = false;
741
742 traverse_scene_bvh(pp, L, inv_dir, light_type, mesh, tri);
743
744 if (hit.hit && l.light_type == 1 || (l.light_type != 1 && hit.hit && hit.t < d))
745 {
746
747 Face hit_tri = tris[hit.tri_idx];
748 uint mat_id = hit_tri.material_id;
749 vec2 hit_uv = get_hit_uvs(hit_tri, hit);
750 Material mat = materials[mat_id];
751 vec4 albedo = get_hit_albedo(mat, hit_uv);
752 if (albedo.a < 1.0f)
753 {
754 throughput *= albedo.rgb;
755 MeshNode hit_mn = mesh_nodes[hit.mn_idx];
756 hit.normal = mat3(transpose(hit_mn.inverse_global_matrix)) * hit_tri.normal;
757 pp = hit.pos + L * 0.001f;
758 d = length(pp - l.pos);
759 }
760 else
761 {
762 break;
763 }
764
765 }
766 else
767 {
768 hit_light = true;
769 }
770
771
772 }
773
774 vec3 lighting = l.color * max(dot(normal, L), 0.0f) * throughput;
775 // vec3 lighting = l.color * throughput;
776 if (!hit_light) return vec3(0,0,0);
777
778 if (l.light_type == 1)
779 {
780 lighting *= l.intensity;
781 }
782 else if (l.light_type == 0)
783 {
784 lighting *= CalculateAttenuation(d2, l.radius, l.intensity);
785 }
786 else
787 {
788
789 float theta = dot(-L, l.dir);
790
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;
795
796 }
797
798
799 return lighting;
800
801}
802
803
804// vec3 resolve_light_direct(in Light l, vec3 lighting, vec3 normal, vec3 L, vec3 albedo, float metallic)
805// {
806
807
808// if (l.light_type == 1)
809// {
810// lighting *= l.intensity;
811// }
812// else if (l.light_type == 0)
813// {
814// lighting *= CalculateAttenuation(d2, l.radius, l.intensity);
815// }
816// else
817// {
818
819// float theta = dot(-L, l.dir);
820
821// float intensity = clamp((theta - l.outer_cos) * l.cos_diff, 0.0f, 1.0f);
822// lighting /= max(safe_d2, 0.001);
823// lighting *= intensity;
824// lighting *= l.intensity;
825
826// }
827// vec3 C_diff = albedo * (1 - metallic);
828// lighting *= C_diff;
829
830// }
831
832
834{
835 float r1 = rand();
836 float r2 = rand();
837
838 float sqrt_r1 = sqrt(r1);
839 return (1.0 - sqrt_r1) * a + (sqrt_r1 * (1.0 - r2)) * b + (sqrt_r1 * r2) * c;
840
841}
842
843
844vec3 process_area_light(in Light light, vec3 pos, vec3 normal, vec3 brdf, float p_select)
845{
846
847 MeshNode mn = mesh_nodes[light.mesh];
848 Face f = tris[mn.tri_offset + light.tri];
849
850 vec3 area_normal = mat3(transpose(mn.inverse_global_matrix)) * f.normal;
851 vec3 A = (mn.global_matrix * vec4(f.v0.pos, 1.0)).xyz;
852 vec3 B = (mn.global_matrix * vec4(f.v1.pos, 1.0)).xyz;
853 vec3 C = (mn.global_matrix * vec4(f.v2.pos, 1.0)).xyz;
854 vec3 random_pos = random_pos_on_triangle(A, B, C);
855 vec3 bary = compute_3d_barycentric(A, B, C, random_pos);
856 vec2 uvs = get_uvs(bary, mn.tri_offset + light.tri);
857 Material mat = materials[f.material_id];
858 vec3 emission = get_hit_emissive(mat, uvs);
859 // pos += normal * 0.01f;
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;
866 int cur = 0;
867 int safety = 10;
868 vec3 pp = pos + normal * 0.01f;
869 vec3 throughput = vec3(1.0);
870 // return vec3(A / 10);
871 while (!hit_light && cur < safety)
872 {
873 cur++;
874
875 hit.hit = false;
876 hit.t = dist;
877 hit.best_t = dist;
878 // hit.t = 100000;
879 // hit.best_t = 1000000;
880 hit.blocked_t = 1000000;
881 hit.tri_idx = 0;
882 hit.mn_idx = 0;
883 hit.occluded = false;
884
885 traverse_scene_bvh(pp, L, inv_dir, 3, 0, 0);
886
887 // if (hit.hit && (hit.blocked_t < hit.t || hit.t < dist))
888 if (hit.hit && hit.t < dist)
889 {
890
891 MeshNode hit_mn = mesh_nodes[hit.mn_idx];
892 Face hit_tri = tris[hit.tri_idx];
893 vec2 hit_uv = get_hit_uvs(hit_tri, hit);
894 uint mat_id = hit_tri.material_id;
895 Material hit_mat = materials[mat_id];
896 vec4 albedo = get_hit_albedo(hit_mat, hit_uv);
897 // return vec3(mat_id/10.0f);
898 if (hit.blocked_t < hit.t)
899 {
900 return vec3(0,0,0);
901 }
902 if (albedo.a < 1.0f)
903 {
904 // throughput *= 1 - albedo.a * (vec3(1.0) - albedo.rgb);
905 throughput *= albedo.rgb;
906 // return vec3(1,1,1);
907 // throughput *= vec3(1,1,1);
908 MeshNode hit_mn = mesh_nodes[hit.mn_idx];
909 hit.normal = mat3(transpose(hit_mn.inverse_global_matrix)) * hit_tri.normal;
910 pp = hit.pos + L * 0.001f;
911 diff = random_pos - pp;
912 dist = length(diff);
913 // return vec3(dist);
914 }
915 else
916 {
917 vec3 emissive = get_hit_emissive(hit_mat, hit_uv);
918 hit.normal = mat3(transpose(hit_mn.inverse_global_matrix)) * hit_tri.normal;
919 if (length(emissive) == 0 && f.material_id != mat_id)
920 {
921
922 return vec3(0,0,0);
923 // return hit.normal * 0.5 + 0.5;
924 // return albedo.rgb;
925 // return pos;
926 }
927 else
928 {
929 area_normal = hit.normal;
930 // return vec3(1,0,0);
931 hit_light = true;
932 }
933 }
934
935 }
936 else
937 {
938 // return vec3(1,0,0);
939 hit_light = true;
940 }
941
942 }
943 // hit.hit = false;
944 // // hit.t = 100000;
945 // // hit.best_t = 1000000;
946 // hit.blocked_t = 1000000;
947 // hit.tri_idx = 0;
948 // hit.mn_idx = 0;
949 // hit.occluded = false;
950 // traverse_scene_bvh(pos + normal * 0.001f, L, inv_dir, 0, 0, 0);
951
952 // return vec3(1,0,0);
953 float cos_light = dot(area_normal, -L);
954 if (cos_light <= 0.001)
955 {
956 return vec3(0,0,0);
957 }
958
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);
961 // vec3 color = (emission * brdf) / (d2);
962 // color *= CalculateAttenuation(d2, light.radius, 20);
963 // vec3 color = (emission * brdf * cos_light * cos_surface) / (d2 * p_select) * throughput;
964 // vec3 color = (emission * brdf * cos_light * cos_surface) / (d2 * p_select) * throughput;
965 // vec3 color = (emission * brdf * cos_light * cos_surface) / (d2);
966 // vec3 color = (light.color * brdf * cos_light * cos_surface * light.area) / (d2 * max(p_select, 0.0001f));
967 return color;
968}
969
970vec3 process_light(vec3 pos, vec3 normal, uint mesh, uint tri, vec3 brdf, Light light, float p_select)
971{
972
973
974 if (light.light_type == 0)
975 {
976
977 return solve_light_source(light, pos, normal, mesh, tri) * brdf / p_select;
978 // return vec3(0,0,1);
979 }
980 else if (light.light_type == 1)
981 {
982 // return solve_light_source(light, pos, normal, mesh, tri) * brdf / p_select;
983 return vec3(0,0,0);
984
985 }
986 else if (light.light_type == 2)
987 {
988
989 return solve_light_source(light, pos, normal, mesh, tri) * brdf / p_select;
990 }
991 else
992 {
993 return process_area_light(light, pos, normal, brdf, p_select);
994 // return vec3(1.0);
995
996 }
997 return vec3(0.0);
998
999}
1000
1001
1003{
1004 return clamp(pos, min, max);
1005}
1006
1007bool pos_inside_aabb(vec3 pos, vec3 min, vec3 max)
1008{
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;
1012 return false;
1013}
1014
1015vec3 traverse_light_bvh(vec3 pos, vec3 normal, uint mesh, uint tri, vec3 brdf)
1016{
1017
1018 int ni = 0;
1019 float pdf = 1.0f;
1020
1021
1022 int dir_light_idx = -1;
1023 for (int i = 0; i < uLightCount; i++)
1024 {
1025
1026 if (lights[i].light_type == 1)
1027 {
1028 dir_light_idx = i;
1029 break;
1030 }
1031
1032 }
1033
1034 // if (dir_light_idx != -1)
1035 // {
1036
1037 // Light directional_light = lights[dir_light_idx];
1038 // float lum = dot(directional_light.color, vec3(0.2126f, 0.7152f, 0.0722f));
1039 // // float lum = dot(vec3(0.5,0.5,0.5), vec3(0.2126f, 0.7152f, 0.0722f));
1040 // float flux_dir = lum * directional_light.intensity;
1041 // BVHNode n_root = light_bvh_nodes[0];
1042 // float importance_n_root = n_root.flux;
1043 // float sum_imp = flux_dir + importance_n_root;
1044 // float norm_dir = flux_dir / sum_imp;
1045 // float norm_root = importance_n_root / sum_imp;
1046
1047 // float p = rand();
1048 // if (p < norm_dir)
1049 // {
1050 // // enter left
1051 // pdf *= max(norm_dir, 0.0001f);
1052
1053 // return solve_light_source(directional_light, pos, normal, mesh, tri) * brdf / pdf;
1054 // // return vec3(1,0,0);
1055 // }
1056 // }
1057
1058
1059
1060 vec3 color = vec3(0.0);
1061 while (ni >= 0)
1062 {
1063
1064 BVHNode n = light_bvh_nodes[ni];
1065
1066 // if (!pos_inside_aabb(pos, n.min, n.max))
1067 // {
1068 // ni = n.next;
1069 // continue;
1070 // }
1071
1072 if (n.left == -1 && n.right == -1)
1073 {
1074 bool processed = false;
1075 // for (int i = 0; i < n.count; i++)
1076 // {
1077 // uint index = lights[n.first_index + i].bvh_index;
1078 // Light l = lights[index];
1079 // if (l.light_type != 3 && l.light_type != 1)
1080 // {
1081
1082 // color = process_light(pos, normal, mesh, tri, brdf, l, pdf);
1083 // processed = true;
1084 // break;
1085 // }
1086 // }
1087 // if (!processed)
1088 // {
1089 // bool dir = true;
1090 // uint index = 0;
1091 // uint safety = 10;
1092 // uint cur = 0;
1093 // while (dir && cur < safety)
1094 // {
1095
1096 // cur++;
1097 // if (lights[index].light_type != 1) dir = false;
1098 // }
1099 int random_light_idx = int(rand() * n.count);
1100 if (random_light_idx >= n.count) random_light_idx = n.count-1;
1101 // uint index = lights[n.first_index + random_light_idx].bvh_index;
1102 uint index = n.first_index + random_light_idx;
1103 Light l = lights[index];
1104
1105 color = process_light(pos, normal, mesh, tri, brdf, l, pdf);
1106 // }
1107
1108 // vec3 color = process_area_light(light, pos, normal, brdf, pdf);
1109
1110 break;
1111 // return color;
1112 }
1113 else
1114 {
1115
1116 BVHNode n0 = light_bvh_nodes[n.left];
1117 BVHNode n1 = light_bvh_nodes[n.right];
1118
1119 vec3 closest_n0 = closest_point_on_aabb(pos, n0.min, n0.max);
1120 vec3 closest_n1 = closest_point_on_aabb(pos, n1.min, n1.max);
1121
1122 vec3 diff_n0 = pos - closest_n0;
1123 vec3 diff_n1 = pos - closest_n1;
1124
1125 float dist2_n0 = dot(diff_n0, diff_n0);
1126 float dist2_n1 = dot(diff_n1, diff_n1);
1127
1128 dist2_n0 = max(dist2_n0, 0.001f);
1129 dist2_n1 = max(dist2_n1, 0.001f);
1130
1131 float importance_n0 = n0.flux / dist2_n0;
1132 float importance_n1 = n1.flux / dist2_n1;
1133
1134 float sum = importance_n0 + importance_n1;
1135 if (sum <= 0.0f)
1136 {
1137 importance_n0 = 0.5f;
1138 importance_n1 = 0.5f;
1139 sum = 1.0f;
1140 }
1141 float norm_n0 = importance_n0 / sum;
1142 float norm_n1 = importance_n1 / sum;
1143
1144 float p = rand();
1145 if (p < norm_n0)
1146 {
1147 // enter left
1148 pdf *= max(norm_n0, 0.0001f);
1149 ni = n.left;
1150 continue;
1151 }
1152 else
1153 {
1154 // enter right
1155 pdf *= max(norm_n1, 0.0001f);
1156 ni = n.right;
1157 continue;
1158 }
1159 }
1160 }
1161 if (length(color) <= 0.0f && dir_light_idx != -1)
1162 {
1163
1164 Light directional_light = lights[dir_light_idx];
1165 float lum = dot(directional_light.color, vec3(0.2126f, 0.7152f, 0.0722f));
1166 // float lum = dot(vec3(0.5,0.5,0.5), 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;
1173
1174 float p = rand();
1175 // if (p < norm_dir)
1176 // {
1177 // enter left
1178 pdf = max(norm_dir, 0.0001f);
1179
1180 // return solve_light_source(directional_light, pos, normal, mesh, tri) * brdf / pdf;
1181 return solve_light_source(directional_light, pos, normal, mesh, tri) * brdf ;
1182 // return vec3(1,0,0);
1183 // }
1184 }
1185
1186 return color;
1187
1188}
1189
1190
1192{
1193
1194 int ni = 0;
1195 float pdf = 1.0f;
1196 while (ni >= 0)
1197 {
1198
1199 BVHNode n = light_bvh_nodes[ni];
1200
1201
1202 if (n.left == -1 && n.right == -1)
1203 {
1204 int random_light_idx = int(rand() * n.count);
1205 if (random_light_idx >= n.count) random_light_idx = n.count-1;
1206 // uint index = lights[n.first_index + random_light_idx].bvh_index;
1207 uint index = n.first_index + random_light_idx;
1208 Light l = lights[index];
1209
1210 if (l.light_type == 3)
1211 {
1212
1213 vec3 color = process_area_light(l, pos, normal, brdf, pdf);
1214 // vec3 color = process_light(pos, normal, mesh, tri, brdf, lights[n.first_index + random_light_idx], pdf);
1215
1216 return color;
1217 }
1218 else
1219 {
1220 return vec3(0,0,0);
1221 }
1222 }
1223 else
1224 {
1225
1226 BVHNode n0 = light_bvh_nodes[n.left];
1227 BVHNode n1 = light_bvh_nodes[n.right];
1228
1229 vec3 closest_n0 = closest_point_on_aabb(pos, n0.min, n0.max);
1230 vec3 closest_n1 = closest_point_on_aabb(pos, n1.min, n1.max);
1231
1232 vec3 diff_n0 = pos - closest_n0;
1233 vec3 diff_n1 = pos - closest_n1;
1234
1235 float dist2_n0 = dot(diff_n0, diff_n0);
1236 float dist2_n1 = dot(diff_n1, diff_n1);
1237
1238 dist2_n0 = max(dist2_n0, 0.001f);
1239 dist2_n1 = max(dist2_n1, 0.001f);
1240
1241 float importance_n0 = n0.emissive_flux / dist2_n0;
1242 float importance_n1 = n1.emissive_flux / dist2_n1;
1243
1244 float sum = importance_n0 + importance_n1;
1245 if (sum <= 0.0f)
1246 {
1247 importance_n0 = 0.5f;
1248 importance_n1 = 0.5f;
1249 sum = 1.0f;
1250 }
1251 float norm_n0 = importance_n0 / sum;
1252 float norm_n1 = importance_n1 / sum;
1253
1254 float p = rand();
1255 if (p < norm_n0)
1256 {
1257 // enter left
1258 pdf *= max(norm_n0, 0.0001f);
1259 ni = n.left;
1260 continue;
1261 }
1262 else
1263 {
1264 // enter right
1265 pdf *= max(norm_n1, 0.0001f);
1266 ni = n.right;
1267 continue;
1268 }
1269 }
1270 }
1271
1272 return vec3(0.0);
1273
1274}
1275
1276
1277vec3 resolve_transparents(vec3 throughput, vec3 pos, vec3 dir, vec3 inv_dir)
1278{
1279 bool hit_surface = false;
1280 int safety = 10;
1281 int cur = 0;
1282 while (!hit_surface && cur < safety)
1283 {
1284 cur++;
1285
1286 hit.hit = false;
1287 hit.t = 100000;
1288 hit.best_t = 1000000;
1289 hit.blocked_t = 1000000;
1290 hit.tri_idx = 0;
1291 hit.mn_idx = 0;
1292 hit.occluded = false;
1293
1294 traverse_scene_bvh(pos, dir, inv_dir, 0, 0, 0);
1295
1296 if (hit.hit && hit.blocked_t >= hit.t)
1297 {
1298
1299 Face hit_tri = tris[hit.tri_idx];
1300 uint mat_id = hit_tri.material_id;
1301 vec2 hit_uv = get_hit_uvs(hit_tri, hit);
1302 Material mat = materials[mat_id];
1303 vec4 albedo = get_hit_albedo(mat, hit_uv);
1304 float r = rand();
1305 // if (r < albedo.a)
1306 if (r < albedo.a)
1307 {
1308 break;
1309 }
1310 else
1311 {
1312 // throughput *= 1 - albedo.a * (vec3(1.0) - albedo.rgb);
1313 throughput *= albedo.rgb;
1314 // throughput = albedo.rgb;
1315 // throughput *= vec3(0,0,1);
1316 MeshNode hit_mn = mesh_nodes[hit.mn_idx];
1317 hit.normal = mat3(transpose(hit_mn.inverse_global_matrix)) * hit_tri.normal;
1318 pos = hit.pos + dir * 0.01f;
1319 }
1320
1321 }
1322 else
1323 {
1324 hit_surface = true;
1325 }
1326 }
1327 return throughput;
1328}
1329
1330
1331
1332
1333
1334
1336
1337
1339
1340uniform int uAOIterations;
1341uniform float uAOThreshold;
1342vec3 trace_path(vec3 pos, vec3 normal, ivec3 ids)
1343{
1344
1345 ao_hits = 0;
1346 vec3 throughput = vec3(1.0);
1347 vec3 acc = vec3(0.0);
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);
1352
1353
1354 MeshNode mn = mesh_nodes[current_mn_id];
1355 uint tri_idx = mn.tri_offset + current_tri_idx;
1356 Face tri = tris[tri_idx];
1357 vec3 A = (mn.global_matrix * vec4(tri.v0.pos, 1.0)).xyz;
1358 vec3 B = (mn.global_matrix * vec4(tri.v1.pos, 1.0)).xyz;
1359 vec3 C = (mn.global_matrix * vec4(tri.v2.pos, 1.0)).xyz;
1360 vec3 bary = compute_3d_barycentric(A, B, C, pos);
1361 vec2 hit_uv = get_uvs(bary, tri_idx);
1362 uint mat_id = tris[tri_idx].material_id;
1363 Material mat = materials[mat_id];
1364 vec4 albedo = get_hit_albedo(mat, hit_uv);
1365 vec3 brdf = albedo.rgb * (1.0f / 3.14159265f);
1366 // vec3 brdf = vec3(1,1,1) * (1.0f / 3.14159265f);
1367 // acc += pick_emissive_tri(pos, normal, current_mn_id, current_tri_idx, brdf);
1368 // acc += pick_emissive_tri(pos, normal, current_mn_id, current_tri_idx, brdf);
1369 // if (hit.hit)
1370 // {
1371 // vec2 hit_uv = get_hit_uvs(hit);
1372 // uint mat_id = tris[hit.tri_idx].material_id;
1373 // Material mat = materials[mat_id];
1374 // vec3 emissive = get_hit_emissive(mat, hit_uv);
1375 // acc += emissive * brdf;
1376 // }
1377
1378 // return brdf;
1379
1380 // acc += evaluate_direct_light(pos, normal, mat, hit_uv);
1381 // return acc;
1383 // first_bounce_dir = current_normal;
1384 vec3 dir = first_bounce_dir;
1385
1386 vec3 inv_dir = 1.0f / dir;
1387 vec3 pp = current_pos + current_normal * 0.01f;
1388 acc += traverse_light_bvh_direct_emissives(pp, normal, brdf);
1389 // return pp;
1390 // vec3 pp = current_pos;
1391 throughput = resolve_transparents(throughput, pp, dir, inv_dir);
1392 // return throughput;
1393 // hit.hit = false;
1394 // hit.t = 100000;
1395 // hit.best_t = 1000000;
1396 // hit.blocked_t = 1000000;
1397 // hit.tri_idx = 0;
1398 // hit.mn_idx = 0;
1399 // hit.occluded = false;
1400 // traverse_scene_bvh(pp, dir, inv_dir, 0, current_mn_id, current_tri_idx);
1401 bool skip_bounce = false;
1402 if (!hit.hit)
1403 {
1404 if (!hit.occluded)
1405 {
1406
1407
1408 vec3 corrected = vec3(dir.x, dir.z, -dir.y);
1409 vec3 sky_light = texture(uSkybox, corrected).rgb;
1410 // vec3 sky_light = vec3(0,0,0.05);
1411 acc += throughput * sky_light;
1412
1413 }
1414 skip_bounce = true;
1415 }
1416 else if (hit.blocked_t>= hit.t)
1417 // else
1418 {
1419
1420 // RayHit temp_hit = hit;
1421 // MeshNode temp_mn = mesh_nodes[temp_hit.mn_idx];
1422 // temp_hit.normal = mat3(transpose(temp_mn.inverse_global_matrix)) * tris[temp_hit.tri_idx].normal;
1423 // temp_hit.pos = (temp_mn.global_matrix * vec4(hit.pos, 1.0)).xyz;
1424 // uint temp_tri_idx = tris[temp_hit.tri_idx].tri_idx;
1425 MeshNode hit_mn = mesh_nodes[hit.mn_idx];
1426 Face hit_tri = tris[hit.tri_idx];
1427 hit.normal = mat3(transpose(hit_mn.inverse_global_matrix)) * hit_tri.normal;
1428 hit.pos = (hit_mn.global_matrix * vec4(hit.pos, 1.0)).xyz;
1429
1430 vec3 direct_light = vec3(0.0);
1431
1432 uint mat_id = hit_tri.material_id;
1433 vec2 hit_uv = get_hit_uvs(hit_tri, hit);
1434 Material mat = materials[mat_id];
1435 vec4 albedo = get_hit_albedo(mat, hit_uv);
1436 vec3 emissive = get_hit_emissive(mat, hit_uv);
1437 vec3 brdf = albedo.rgb * (1.0f / 3.14159265f);
1438 // vec3 brdf = vec3(1,1,1) * (1.0f / 3.14159265f);
1439 current_pos = hit.pos;
1440 current_normal = hit.normal;
1441 current_mn_id = uint(hit.mn_idx);
1442 current_tri_idx = uint(hit.tri_idx);
1443
1444 direct_light += traverse_light_bvh(hit.pos, hit.normal, uint(hit.mn_idx), uint(hit.tri_idx), brdf);
1445 float max_lum = 10.0;
1446 float lum = dot(direct_light, vec3(0.2126, 0.7152, 0.0722));
1447 if (lum > max_lum)
1448 {
1449 direct_light *= max_lum / lum;
1450 }
1451 acc += throughput * direct_light;
1452 throughput *= albedo.rgb;
1453
1454
1455 // float p = max(throughput.r, max(throughput.g, throughput.b));
1456 // if (rand() > p) skip_bounce = true;
1457 // if (p != 0)
1458 // {
1459 // throughput /= p;
1460 // }
1461
1462 }
1463 else
1464 {
1465 skip_bounce = true;
1466 }
1467
1468
1469 // return acc;
1470
1471 if (!skip_bounce)
1472 {
1473
1474 for (int i = 0; i < 1; i++)
1475 {
1476 dir = get_bounce_direction(current_normal);
1477 vec3 inv_dir = 1.0f / dir;
1478 vec3 pp = current_pos + current_normal * 0.001f;
1479 // hit.hit = false;
1480 // hit.t = 100000;
1481 // hit.best_t = 1000000;
1482 // hit.blocked_t = 1000000;
1483 // hit.tri_idx = 0;
1484 // hit.mn_idx = 0;
1485 // hit.occluded = false;
1486 // traverse_scene_bvh(pp, dir, inv_dir, 0, current_mn_id, current_tri_idx);
1487 throughput = resolve_transparents(throughput, pp, dir, inv_dir);
1488
1489 if (!hit.hit)
1490 {
1491 if (!hit.occluded)
1492 {
1493
1494 vec3 corrected = vec3(dir.x, dir.z, -dir.y);
1495
1496 vec3 sky_light = texture(uSkybox, corrected).rgb;
1497 // vec3 sky_light = vec3(0,0,0.05);
1498 acc += throughput * sky_light ;
1499 // acc += throughput * sky_light * 0.075f;
1500 }
1501 break;
1502 }
1503 else if (hit.blocked_t >= hit.t)
1504 // else
1505 {
1506 MeshNode hit_mn = mesh_nodes[hit.mn_idx];
1507 Face hit_tri = tris[hit.tri_idx];
1508 hit.normal = mat3(transpose(hit_mn.inverse_global_matrix)) * hit_tri.normal;
1509 hit.pos = (hit_mn.global_matrix * vec4(hit.pos, 1.0)).xyz;
1510
1511 vec3 direct_light = vec3(0.0);
1512 // int random_light_idx = int(rand() * uLightCount);
1513 // if (random_light_idx >= uLightCount) random_light_idx = uLightCount-1;
1514 // float light_pdf = 1.0f / uLightCount;
1515 // for (int i = 0; i < uLightCount; i++)
1516 // {
1517 // direct_light += solve_light_source(lights[i], temp_hit.pos, temp_hit.normal, uint(temp_hit.mn_idx), temp_tri_idx);
1518 // direct_light += solve_light_source(lights[random_light_idx],temp_hit.pos, temp_hit.normal, uint(temp_hit.mn_idx), temp_tri_idx);
1519 // direct_light += pick_random_light(temp_hit.pos, temp_hit.normal, uint(temp_hit.mn_idx), temp_tri_idx);
1520 // }
1521
1522 // direct_light *= 1.0f / light_pdf;
1523 uint mat_id = hit_tri.material_id;
1524 vec2 hit_uv = get_hit_uvs(hit_tri, hit);
1525 Material mat = materials[mat_id];
1526 vec4 albedo = get_hit_albedo(mat, hit_uv);
1527 vec3 emissive = get_hit_emissive(mat, hit_uv);
1528 vec3 brdf = albedo.rgb * (1.0f / 3.14159265f);
1529 // vec3 brdf = vec3(1,1,1) * (1.0f / 3.14159265f);
1530 current_pos = hit.pos;
1531 current_normal = hit.normal;
1532 current_mn_id = uint(hit.mn_idx);
1533 current_tri_idx = uint(hit.tri_idx);
1534
1535 // direct_light += pick_random_light(hit.pos, hit.normal, uint(hit.mn_idx), uint(hit.tri_idx), brdf);
1536 direct_light += traverse_light_bvh(hit.pos, hit.normal, uint(hit.mn_idx), uint(hit.tri_idx), brdf);
1537
1538 float max_lum = 10.0;
1539 float lum = dot(direct_light, vec3(0.2126, 0.7152, 0.0722));
1540 if (lum > max_lum)
1541 {
1542 direct_light *= max_lum / lum;
1543 }
1544 // acc += throughput * emissive;
1545 acc += throughput * direct_light;
1546 throughput *= albedo.rgb;
1547
1548
1549 // float p = max(throughput.r, max(throughput.g, throughput.b));
1550 // if (rand() > p) break;
1551 // if (p != 0)
1552 // {
1553 // throughput /= p;
1554 // }
1555
1556 }
1557 else
1558 {
1559 break;
1560 }
1561
1562 }
1563 }
1564
1565
1566
1567 for (int i = 0; i < uAOIterations; i++)
1568 {
1569 dir = get_bounce_direction(normal);
1570 vec3 inv_dir = 1.0f / dir;
1571 // const float EPSILON = 0.0000001f;
1572 // inv_dir.x = abs(dir.x) < EPSILON ? (dir.x < 0.0f ? -1000000.0f : 1000000.0f) : 1.0f / dir.x;
1573 // inv_dir.y = abs(dir.y) < EPSILON ? (dir.y < 0.0f ? -1000000.0f : 1000000.0f) : 1.0f / dir.y;
1574 // inv_dir.z = abs(dir.z) < EPSILON ? (dir.z < 0.0f ? -1000000.0f : 1000000.0f) : 1.0f / dir.z;
1575
1576 hit.hit = false;
1577 hit.t = 1.0 / 0.0;
1578 hit.blocked_t = 1.0 / 0.0;
1579 hit.best_t = 1.0 / 0.0;
1580 hit.tri_idx = 0;
1581 hit.mn_idx = 0;
1582 hit.occluded = false;
1583
1584 traverse_scene_bvh(pp, dir, inv_dir, 0, uint(ids.y), uint(ids.z));
1585 if (hit.hit || hit.occluded)
1586 {
1587 if (hit.best_t < uAOThreshold) ao_hits++;
1588 }
1589 }
1590
1591
1592 return acc;
1593
1594}
1595
1596
1597// implement a texel list per tri
1598void main()
1599{
1600
1601 ivec2 coord = ivec2(gl_GlobalInvocationID.xy) + uTileOffset;
1602 if (coord.x >= uLightmapWidth || coord.y >= uLightmapHeight) return;
1603 ivec3 ids = texelFetch(uLightmapIDTex, coord, 0).xyz;
1604 if (ids.y < 0)
1605 {
1606 return;
1607 }
1608
1609
1610 uint seed = uint(coord.x) * 1973u + uint(coord.y) * 9277u;
1611 // uint seed = uint(coord.y) * uint(uLightmapWidth) + uint(coord.x);
1612 state = wang_hash(seed ^ uint(uIteration));
1613 // state = coord.x * 1973u + coord.y * 9277u + uIteration * 2699u;
1614 // pcg_hash();
1615 // vec3 r0 = vec3(0.0);
1616 // for (int j = 0; j <20; j++)
1617 // {
1618 // r0 += vec3(rand(), rand(), rand());
1619 // }
1620 // r0 /= 20;
1621
1622
1623
1624 vec4 pos_tex = texelFetch(uLightmapPosTex, coord, 0);
1625 vec3 pos = pos_tex.xyz;
1626 vec4 normal_tex = texelFetch(uLightmapNormalTex, coord, 0);
1627 vec3 normal = normal_tex.xyz;
1628 vec2 uv = vec2(pos_tex.w, normal_tex.w);
1629
1630 vec3 ray_color = trace_path(pos, normal, ids);
1631 // vec3 ray_color = pick_random_light(pos, normal, int(ids.y), int(ids.z));
1632 // vec3 ray_color = r0;
1633 // ray_color = vec3(float(ids.z) / 200);
1634 // ray_color = (normal * 0.5 + 0.5);
1635 // ray_color = (first_bounce_dir * 0.5 + 0.5);
1636 // ray_color = vec3(rand(), rand(), rand());
1637
1638
1639 float sample_luminance = dot(ray_color, vec3(0.2126, 0.7152, 0.0722));
1640
1641
1642 vec4 old_data = imageLoad(lightmapTex, coord);
1643 vec3 old_color = old_data.rgb;
1644 float current_sample_count = old_data.a;
1645
1646 float new_sample_count = current_sample_count + 1.0;
1647 // vec3 new_color = normal;
1648 vec3 new_color = old_color + ray_color;
1649 // vec3 new_color = vec3(1,0,0);
1650
1651 vec4 old_dir_data = imageLoad(directionalTex, coord);
1652
1653 vec3 ray_dir_sample = first_bounce_dir * sample_luminance;
1654 vec3 old_dir_sum = old_dir_data.rgb;
1655 vec3 new_dir_sum = old_dir_sum + ray_dir_sample;
1656
1657 float new_luminance = old_dir_data.a + sample_luminance;
1658
1659 imageStore(lightmapTex, coord, vec4(new_color, old_data.a + 1 - float(ao_hits) / float(uAOIterations)));
1660 imageStore(directionalTex, coord, vec4(new_dir_sum, new_luminance));
1661 // imageStore(lightmapTex, coord, vec4(texture(uSkybox, vec3(0,1,0)).rgb, new_sample_count));
1662
1663
1664}
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)
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()
RayHit hit
void frisvad_basis(in vec3 n, out vec3 b1, out vec3 b2)
float rand()
void main()
in mat3 TBN
uniform int uLightmapHeight
uniform int uLightmapWidth
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.
in vec2 uv
float intensity
The intensity of the Light.
sampler2D albedo_tex
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.
sampler2D emissive_tex
vec3 dir
Direction for lights that are not POINT_LIGHTs.
float inner_angle
Starting angle for SPOT_LIGHTs.
int tri
The MeshFace this corresponds to.
float outer_angle
Outer angle for SPOT_LIGHTs.
float outer_cos
float emissive_strength
uint skip_directional_light
float radius
The radius of the Light.
int mesh
The MeshNode this corresponds to.
sampler2D arm_tex
sampler2D normal_tex
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 first_bounce_dir
vec3 trace_path(vec3 pos, vec3 normal, ivec3 ids)
int ao_hits
vec3 traverse_light_bvh(vec3 pos, vec3 normal, uint mesh, uint tri, vec3 brdf)
uniform int uIteration
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 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)
SDL_Window * window
Definition main.c:519
float clamp(float a, float b, float c)
Definition mathf.h:379
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.
int right
Right child.
int parent
Parent.
int left
Left child.
float emissive_flux
The only-emissive flux.
Nodes for the acceleration structures of BVHs.
float w
Definition types.h:13
@ 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 y
Definition types.h:14
int x
Definition types.h:14
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