-
Notifications
You must be signed in to change notification settings - Fork 2
/
everything.svo
1440 lines (1353 loc) · 76.6 KB
/
everything.svo
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
bool rayBoxIntersectTime (vec3 l1,vec3 invm,vec3 bmin,vec3 bmax, out float tMin, out float tMax)
{
vec3 bmin_l1 = (bmin - l1)*invm;
vec3 bmax_l1 = (bmax - l1)*invm;
vec3 minVec = min (bmin_l1, bmax_l1);
vec3 maxVec = max (bmin_l1, bmax_l1);
float tmin = max(max(minVec.x, minVec.y), minVec.z);
float tmax = min(min(maxVec.x, maxVec.y), maxVec.z);
bool retVal = ((tmax >= tmin) && (tmin < 1.0) && (tmax > 0.0));
tMin = tmin;
tMax = tmax;
return retVal;
}
uint countSetBits(uint n)
{
uint count = 0u;
while (n != 0u) {
count += (n & 1u);
n >>= 1u;
}
return count;
}
uint countSetBitsBefore(uint n, uint comp)
{
uint beforeMask = comp ^ (comp - 1u); // See: https://realtimecollisiondetection.net/blog/?p=78
n &= (~beforeMask);
uint count = 0u;
while (n != 0u) {
count += (n & 1u);
n >>= 1u;
}
return count;
}
const vec3 grid0Min = vec3 (-5.00, -5.00, -5.00);
const vec3 grid0Max = vec3 (5.00, 5.00, 5.00);
const vec3 grid0Range = grid0Max - grid0Min;
uint svoObject0[480] = uint[](128u,2210513095u,3479298144u,1206382640u,2955944416u,2948644703u,3759157328u,4287565583u,3759675512u,2139587772u,802340674u,3188982804u,141045714u,2138838076u,868254667u,3286638110u,504914401u,2400088239u,4111435140u,25231882u,717225475u,1058011896u,3229678206u,2130212960u,2131742716u,1009857343u,2275802654u,3097205408u,547535824u,2192417534u,3288236371u,3896412222u,505289727u,4279177016u,4043288476u,2021159043u,2386045908u,708321408u,3052425285u,4261731647u,2600436334u,1905893279u,4231802947u,3476757529u,3877378112u,1105215936u,34434u,4289098691u,3288284978u,4058111969u,1368453360u,4030781552u,2018533408u,540094515u,863606808u,301587464u,2349928471u,3958414079u,1086318151u,2701128451u,61471098u,2156002367u,62979548u,3180696977u,2123695444u,358083563u,1106362448u,272611232u,1587628053u,2095138053u,3746421117u,802705685u,17108991u,944109840u,2236852288u,1090600768u,3208819995u,1789041304u,2577271807u,4244787370u,4078985120u,265989392u,626174633u,277056386u,2952430954u,532654112u,573780048u,4286382072u,404256888u,2308730983u,4027342703u,3436158974u,2146558435u,2551187007u,867382067u,865704345u,2576993484u,3704409341u,3258775938u,3176781832u,2297956543u,3808397308u,71073779u,4062383231u,4001299939u,3858923261u,4294504910u,2281914348u,3370546118u,1063543298u,537132546u,4261943487u,4271477196u,4224646856u,3292938240u,272650234u,4016187103u,4133678343u,4080218316u,3476239359u,3120287513u,2441110503u,4294757583u,4090285104u,553641598u,4193511022u,2294243840u,2818752010u,1848374463u,3254062863u,2127319104u,2682678906u,2545967183u,3435708155u,4231000385u,432095607u,285339157u,284091511u,1148523596u,1145953754u,626850341u,3632174440u,359142741u,1118551722u,2863275440u,1521198420u,1252047863u,717509418u,3288667447u,1947428677u,3120952896u,1094996991u,1600391359u,1428034056u,16427093u,542637738u,1119939523u,3764511729u,4050662903u,4002366143u,130351256u,410541068u,1010581182u,515774602u,737923627u,3004883152u,1509530008u,2144597893u,4231843327u,3263414787u,1106579704u,410548344u,2084322364u,809377310u,403708943u,235416312u,1731735602u,3482858001u,3677724u,166411u,4256956382u,4232957900u,3451073271u,1986419065u,4052775731u,858829601u,2441190596u,1288490207u,4268161167u,1449650319u,1009265720u,1162105604u,4116665695u,4114841579u,3926179344u,2856591392u,4126637728u,938539692u,67170282u,357892821u,39127072u,1874417153u,2105410472u,717184855u,1470217045u,1428347733u,571195218u,3158670264u,1079326545u,1072002912u,1604612128u,680036493u,3712343713u,1440621329u,286263846u,1713861618u,4144182067u,4169136513u,2174327168u,4026721826u,601629663u,4084412932u,4208664577u,85018111u,2945462233u,4110471175u,4286675613u,3793592382u,2151743801u,2155977084u,4169173120u,717083454u,3154037963u,3003960254u,1637456281u,2670775436u,3436157319u,3060164556u,4088607539u,3996247560u,434274295u,717079091u,1787128459u,4164421880u,257162247u,3278619619u,3275833600u,1052572127u,3781082648u,2693315316u,341137392u,804388289u,3519082624u,262318170u,99517628u,787753794u,2906981461u,83750360u,653103197u,2554865794u,2750130721u,3876522510u,4117868073u,2155911579u,2880295434u,2916137919u,2831494024u,3577881349u,871633902u,1738041274u,1731343139u,848857241u,2564676733u,3808693497u,2974916321u,7540787u,858995097u,2417596816u,2155881986u,645005824u,359751649u,823902198u,1639497159u,4292310531u,2818932589u,830078335u,4294246389u,2635153856u,44739310u,552586956u,3186548104u,3860437401u,2582842572u,3436181894u,1718085244u,3274912563u,1047109219u,3059754667u,2113579688u,4146775159u,684012417u,3185398955u,1420475220u,1414874165u,1363219957u,1079339767u,2102744389u,1413815632u,366826968u,357914485u,1467193481u,2592717738u,2732401341u,179015024u,2411672478u,568205431u,1362022673u,785458779u,1717003460u,1103939548u,3809811491u,1440483938u,1646683955u,926936271u,2579996262u,1741474824u,4002152450u,570564978u,863502128u,3493413888u,2855251616u,2050484286u,536879654u,1648291565u,3366878410u,2925648319u,3882300403u,858915737u,2583200137u,3426431964u,4243502958u,2724621994u,3903335624u,2343400104u,1463636000u,2157835338u,2847797764u,1512267499u,1587762346u,335902077u,1410509800u,3755666049u,706029661u,2170508670u,33166762u,948444056u,2595470683u,3041042603u,2297890824u,4151451042u,998733074u,3977315069u,306673526u,1145324621u,4248974685u,3405070562u,40265331u,785182542u,4186334822u,1727842172u,3471343616u,499u,1357113102u,284424167u,3884189752u,4056380211u,590590777u,2156501401u,2631716047u,993058526u,2107101574u,3707808442u,4203227235u,3812451259u,3256942850u,2677709668u,163029666u,4196412085u,4155116048u,2868572290u,2947481239u,3356977160u,747284398u,2726734856u,2864641032u,547417086u,2776108975u,2852240829u,1153389611u,3716223248u,270268584u,2943355891u,4228260522u,5584208u,272098427u,2272765032u,462565435u,3287848969u,4194171908u,1308099677u,4087566845u,290458114u,118698865u,59898265u,2172700672u,11u,2290648947u,3208630076u,3234740275u,2678056550u,1712855483u,860142112u,3450012993u,1138225127u,337383764u,3791279575u,3244245119u,3059112932u,67235856u,2941298647u,2231728215u,4161258114u,3286278176u,2431680288u,4289712893u,41780485u,8213547u,3492795408u,635461281u,4272287920u,2968533048u,947420220u,1072201707u,4095726655u,1363230741u,218369513u,2112387719u,2919545868u,3158299634u,1040326207u,4192304903u,125730528u,4034981951u,1078985600u,2180641853u,1069596736u,4229930976u,1616934975u,941621248u,0u,2282245244u,4039911032u,2036729363u,187826218u,3170083338u,418434846u,1045493886u,2123921535u,3912681479u,2865489930u,36173420u,2302931796u,3205692963u,4193787455u,2575403127u,4160876624u,4030787824u,2021154816u,0u,0u);
uint readBitsSVO0 (uint bitLoc, uint numBits) {
uint wordLoc = bitLoc / 32u;
uint leftToRead = (32u - (bitLoc % 32u));
if (numBits <= leftToRead) {
uint shiftToMask = leftToRead - numBits;
uint masker = 0xFFFFFFFFu;
masker >>= uint(32u - numBits);
masker <<= shiftToMask;
uint value = (svoObject0[wordLoc] & masker);
value >>= shiftToMask;
return value;
} else {
uint bottomBits = numBits - leftToRead;
uint masker = 0xFFFFFFFFu;
masker >>= uint(32u - leftToRead);
uint topNum = (svoObject0[wordLoc] & masker);
uint bottomMasker = 0xFFFFFFFFu;
uint bottomShifter = uint(32u - bottomBits);
bottomMasker <<= bottomShifter;
uint value = (svoObject0[wordLoc + 1u] & bottomMasker);
uint bottomNum = (value >> bottomShifter);
return ((topNum << bottomBits) | bottomNum);
}
}
bool readLeafSVO0 (vec3 samplePos, vec3 sampleDir, out vec3 skipPos) {
skipPos = vec3 (10000.0);
if ( any(lessThan(samplePos, grid0Min)) || any(greaterThan(samplePos, grid0Max)) ) return false;
uvec3 topBrickPos = uvec3 (samplePos - grid0Min);
uint topBrickId = topBrickPos.z + topBrickPos.y * uint(grid0Range.x) + topBrickPos.x * uint(grid0Range.y) * uint(grid0Range.z);
uint streamReadPos = 0u;
for (int i = 0; i < int(topBrickId); i++) {
uint isOcc = readBitsSVO0 (streamReadPos, 1u);
streamReadPos += 1u;
if (isOcc == 1u) {
uint countMidBricks = countSetBits (readBitsSVO0 (streamReadPos, 8u));
streamReadPos += (8u + countMidBricks * 8u);
}
}
uint topBrick = readBitsSVO0 (streamReadPos, 1u);
if (topBrick == 0u) {
vec3 topBrickMin = grid0Min + vec3 (topBrickPos);
vec3 topBrickMax = topBrickMin + vec3 (1.0);
vec3 p1 = samplePos;
vec3 p2 = p1 + sampleDir * 2.0;
vec3 m = p2 - p1;
float tMin, tMax;
rayBoxIntersectTime (p1, vec3(1.0)/m, topBrickMin, topBrickMax, tMin, tMax);
skipPos = p1 + m * (tMax + 0.01);
return false;
}
streamReadPos += 1u;
uint midBricks = readBitsSVO0 (streamReadPos, 8u);
streamReadPos += 8u;
vec3 topBrickMinCorner = grid0Min + vec3 (topBrickPos);
vec3 sampleRelativeToTopBrick = fract (samplePos);
uint checkMidBrickBit = 0x80u;
vec3 sampleRelativeToMidBrick = sampleRelativeToTopBrick;
vec3 midBrickPos = vec3 (0.0);
if ( sampleRelativeToTopBrick.x > 0.5 ) {
sampleRelativeToMidBrick.x -= 0.5;
midBrickPos.x = 0.5;
checkMidBrickBit >>= 4u;
}
if ( sampleRelativeToTopBrick.y > 0.5 ) {
sampleRelativeToMidBrick.y -= 0.5;
midBrickPos.y = 0.5;
checkMidBrickBit >>= 2u;
}
if ( sampleRelativeToTopBrick.z > 0.5 ) {
sampleRelativeToMidBrick.z -= 0.5;
midBrickPos.z = 0.5;
checkMidBrickBit >>= 1u;
}
if ( (midBricks & checkMidBrickBit) == 0u ) {
vec3 midBrickMin = grid0Min + vec3 (topBrickPos) + midBrickPos;
vec3 midBrickMax = midBrickMin + vec3 (0.5);
vec3 p1 = samplePos;
vec3 p2 = p1 + sampleDir * 2.0;
vec3 m = p2 - p1;
float tMin, tMax;
rayBoxIntersectTime (p1, vec3(1.0)/m, midBrickMin, midBrickMax, tMin, tMax);
skipPos = p1 + m * (tMax + 0.01);
return false;
}
uint skipMidBricks = countSetBitsBefore (midBricks, checkMidBrickBit);
streamReadPos += (8u * skipMidBricks);
uint finalMidBrick = readBitsSVO0 (streamReadPos, 8u);
uint checkVoxelBrickBit = 0x80u;
if ( sampleRelativeToMidBrick.x > 0.25 ) {
checkVoxelBrickBit >>= 4u;
}
if ( sampleRelativeToMidBrick.y > 0.25 ) {
checkVoxelBrickBit >>= 2u;
}
if ( sampleRelativeToMidBrick.z > 0.25 ) {
checkVoxelBrickBit >>= 1u;
}
if ( (checkVoxelBrickBit & finalMidBrick) != 0u ) return true;
skipPos = samplePos + sampleDir * 0.25;
return false;
}
bool traceRaySVO0(vec3 p1, vec3 p2, out vec3 hitPos) {
vec3 m = p2 - p1;
float hitMin, hitMax;
if ( !rayBoxIntersectTime (p1, vec3(1.0)/m, grid0Min, grid0Max, hitMin, hitMax) ) {
hitPos = vec3 (-1.0);
return false;
}
hitMin += 0.00001;
hitMax -= 0.00001;
vec3 curPos = p1 + hitMin * m;
vec3 curDir = normalize (m);
vec3 skipPos = vec3 (0.0);
for (int i = 0; i != 100; i++) {
if (readLeafSVO0 (curPos, curDir, skipPos)) {
hitPos = curPos;
return true;
}
if ( skipPos == vec3(10000.0) ) break;
curPos = skipPos;
}
return false;
}
void decodeSVO0( inout vec4 fragColor, vec2 fragCoord ) {
uvec2 pixCoord = uvec2 (fragCoord - vec2 (0.5));
if ( iFrame % 60 == 0 && all (lessThan (pixCoord,uvec2 (uint(grid0Range.x), uint(grid0Range.y * grid0Range.z)/2u))) ) {
uint writeR = 0u, writeG = 0u, writeB = 0u, writeA = 0u;
vec3 baseCoord;
baseCoord.x = float (pixCoord.x % uint(grid0Range.x)) - float(uint(grid0Range.x)/2u);
baseCoord.y = float (pixCoord.y % uint(grid0Range.y)) - float(uint(grid0Range.y)/2u);
baseCoord.z = float (pixCoord.y / uint(grid0Range.y)) * 2.0 - float(uint(grid0Range.z)/2u);
for (uint zone = 0u; zone != 2u; zone++) {
for (uint k = 0u;k != 4u; k++) {
for (uint j = 0u;j != 4u; j++) {
for (uint i = 0u;i != 4u; i++) {
bool occupancy = readLeafSVO0 (baseCoord + vec3 (i, j, k) * 0.25 + vec3 (0.001, -0.001, float(zone) + 0.001));
if (!occupancy) continue;
uint write = (1u << i);
write <<= (j * 4u);
if ( zone == 0u ) {
if ( k < 2u ) {
write <<= (k * 16u);
writeR |= write;
} else {
write <<= ((k - 2u) * 16u);
writeG |= write;
}
} else {
if ( k < 2u ) {
write <<= (k * 16u);
writeB |= write;
} else {
write <<= ((k - 2u) * 16u);
writeA |= write;
}
}
}
}
}
}
fragColor = vec4 (uintBitsToFloat(writeR), uintBitsToFloat(writeG), uintBitsToFloat(writeB), uintBitsToFloat(writeA));
}
else {
fragColor = texelFetch (iChannel0, ivec2(fragCoord - vec2 (0.5)), 0);
}
}
bool occupancyReadGrid0 (vec3 samplePos) {
if ( any(lessThan(samplePos, grid0Min)) || any(greaterThan(samplePos, grid0Max)) ) return false;
vec3 samplePosRel = samplePos - grid0Min;
uvec3 fetchPos = uvec3 (samplePosRel);
ivec2 sampleCoord = ivec2 (fetchPos.x, fetchPos.y + uint (fetchPos.z/2u) * uint(grid0Range.y));
vec4 fetchTexel = texelFetch (iChannel0, sampleCoord, 0);
uvec4 fetchTexelRGBA = uvec4 (floatBitsToUint(fetchTexel.x), floatBitsToUint(fetchTexel.y), floatBitsToUint(fetchTexel.z), floatBitsToUint(fetchTexel.a));
uvec3 checkBits = uvec3 (fract (samplePosRel) * 4.0);
uint shiftBits = 0u;
if (checkBits.z < 2u)
shiftBits = checkBits.x + checkBits.y * 4u + checkBits.z * 16u;
else
shiftBits = checkBits.x + checkBits.y * 4u + (checkBits.z - 2u) * 16u;
uint readUint;
if ((fetchPos.z % 2u) == 0u)
if (checkBits.z < 2u)
readUint = fetchTexelRGBA.x;
else
readUint = fetchTexelRGBA.y;
else
if (checkBits.z < 2u)
readUint = fetchTexelRGBA.z;
else
readUint = fetchTexelRGBA.a;
uint maskBits = (1u << shiftBits);
if ( (readUint & maskBits) == 0u ) return false;
return true;
}
void decodeAndJFA_SVO0( out vec4 fragColor, in vec2 fragCoord )
{
uvec2 pixCoord = uvec2 (fragCoord - vec2 (0.5));
const uint tilingSide = uint (floor (sqrt(grid0Range.z))) + 1u;
int log2N = int(ceil(log2(max (max(grid0Range.x * 4.0, grid0Range.y * 4.0), grid0Range.z * 4.0))));
float offsetPower = float (log2N - (iFrame-1) - 1);
bool isInJFAResolution = all (lessThan (pixCoord,uvec2 (uint(grid0Range.x), uint(grid0Range.y)) * 4u * tilingSide));
float zRange = ceil (grid0Range.z * 4.0 * 0.0625);
bool isInJFACompactionResolution = all (lessThan (pixCoord, uvec2 (uint(grid0Range.x * 4.0), uint(grid0Range.y * 4.0 * zRange))));
if ( iFrame == 0 && isInJFAResolution ) {
vec3 baseCoord;
baseCoord.x = float (pixCoord.x % uint(grid0Range.x * 4.0)) - float(uint(grid0Range.x * 4.0)/2u);
baseCoord.y = float (pixCoord.y % uint(grid0Range.y * 4.0)) - float(uint(grid0Range.y * 4.0)/2u);
baseCoord.z = (float (pixCoord.y / uint(grid0Range.y * 4.0)) * float(tilingSide) + float (pixCoord.x / uint(grid0Range.x * 4.0))) * 4.0 - float(uint(grid0Range.z * 4.0)/2u);
baseCoord *= 0.25;
if ( baseCoord.z >= grid0Max.z )
{
fragColor = vec4 (uintBitsToFloat(0u), uintBitsToFloat(0u), uintBitsToFloat(0u), uintBitsToFloat(0u));
return ;
}
uint writeR = 0u, writeG = 0u, writeB = 0u, writeA = 0u;
vec3 pos1 = baseCoord + vec3 (0, 0, 0);
vec3 pos2 = baseCoord + vec3 (0, 0, 0.25);
vec3 pos3 = baseCoord + vec3 (0, 0, 0.5);
vec3 pos4 = baseCoord + vec3 (0, 0, 0.75);
bool occupancy1 = readLeafSVO0 (pos1 + vec3 (0.001, -0.001, 0.001));
bool occupancy2 = readLeafSVO0 (pos2 + vec3 (0.001, -0.001, 0.001));
bool occupancy3 = readLeafSVO0 (pos3 + vec3 (0.001, -0.001, 0.001));
bool occupancy4 = readLeafSVO0 (pos4 + vec3 (0.001, -0.001, 0.001));
if ( occupancy1 )
{
pos1 *= 4.0;
pos1 += vec3(uint(grid0Range * 4.0)/2u);
writeR = 0xFF000000u;
writeR |= (uint(pos1.x) << 16);
writeR |= (uint(pos1.y) << 8);
writeR |= uint(pos1.z);
}
if ( occupancy2 )
{
pos2 *= 4.0;
pos2 += vec3(uint(grid0Range * 4.0)/2u);
writeG = 0xFF000000u;
writeG |= (uint(pos2.x) << 16);
writeG |= (uint(pos2.y) << 8);
writeG |= uint(pos2.z);
}
if ( occupancy3 )
{
pos3 *= 4.0;
pos3 += vec3(uint(grid0Range * 4.0)/2u);
writeB = 0xFF000000u;
writeB |= (uint(pos3.x) << 16);
writeB |= (uint(pos3.y) << 8);
writeB |= uint(pos3.z);
}
if ( occupancy4 )
{
pos4 *= 4.0;
pos4 += vec3(uint(grid0Range * 4.0)/2u);
writeA = 0xFF000000u;
writeA |= (uint(pos4.x) << 16);
writeA |= (uint(pos4.y) << 8);
writeA |= uint(pos4.z);
}
fragColor = vec4 (uintBitsToFloat(writeR), uintBitsToFloat(writeG), uintBitsToFloat(writeB), uintBitsToFloat(writeA));
} else if ( iFrame > 0 && offsetPower >= 0.0 && isInJFAResolution ) {
vec3 baseCoord;
baseCoord.x = float (pixCoord.x % uint(grid0Range.x * 4.0)) - float(uint(grid0Range.x * 4.0)/2u);
baseCoord.y = float (pixCoord.y % uint(grid0Range.y * 4.0)) - float(uint(grid0Range.y * 4.0)/2u);
baseCoord.z = (float (pixCoord.y / uint(grid0Range.y * 4.0)) * float(tilingSide) + float (pixCoord.x / uint(grid0Range.x * 4.0))) * 4.0 - float(uint(grid0Range.z * 4.0)/2u);
baseCoord *= 0.25;
if ( baseCoord.z >= grid0Max.z )
{
fragColor = texelFetch (iChannel0, ivec2(fragCoord - vec2 (0.5)), 0);
return ;
}
vec4 fragmentRead = texelFetch (iChannel0, ivec2(fragCoord - vec2 (0.5)), 0);
uint baseR = floatBitsToUint(fragmentRead.x);
uint baseG = floatBitsToUint(fragmentRead.y);
uint baseB = floatBitsToUint(fragmentRead.z);
uint baseA = floatBitsToUint(fragmentRead.a);
float offsetAmt = pow (2.0, offsetPower);
for (int zBrick = 0; zBrick != 4; zBrick++)
{
vec3 curCoord = baseCoord + vec3 (0, 0, float(zBrick) * 0.25);
float minDist = 100000.0;
uvec3 minCoord = uvec3 (0);
bool foundMinCoord = false;
for (int i = -1; i != 2; i++)
for (int j = -1; j != 2; j++)
for (int k = -1; k != 2; k++)
{
vec3 sampleLoc = curCoord + vec3(i, j, k) * 0.25 * offsetAmt;
if ( sampleLoc.x < grid0Min.x || sampleLoc.x >= grid0Max.x ) continue;
if ( sampleLoc.y < grid0Min.y || sampleLoc.y >= grid0Max.y ) continue;
if ( sampleLoc.z < grid0Min.z || sampleLoc.z >= grid0Max.z ) continue;
float diffZ = sampleLoc.z - grid0Min.z;
float fractionalZ = fract(diffZ);
uint topLevelZ = uint(diffZ);
uint xBase = (topLevelZ % tilingSide) * 4u * uint(grid0Range.x);
uint yBase = (topLevelZ / tilingSide) * 4u * uint(grid0Range.y);
xBase += uint ((sampleLoc.x - grid0Min.x) * 4.0);
yBase += uint ((sampleLoc.y - grid0Min.y) * 4.0);
vec4 readFrag = texelFetch (iChannel0, ivec2(xBase, yBase), 0);
uint readUint;
if ( fractionalZ == 0.0 ) readUint = floatBitsToUint (readFrag.x);
else if ( fractionalZ == 0.25 ) readUint = floatBitsToUint (readFrag.y);
else if ( fractionalZ == 0.5 ) readUint = floatBitsToUint (readFrag.z);
else readUint = floatBitsToUint (readFrag.a);
uint readR = ((readUint & 0x00FF0000u) >> 16);
uint readG = ((readUint & 0x0000FF00u) >> 8);
uint readB = (readUint & 0x000000FFu);
vec3 sampledCoord = (vec3 (readR, readG, readB) - vec3(uint(grid0Range * 4.0)/2u)) * 0.25;
float sampledDist = length (sampledCoord - curCoord);
if ( readUint != 0u && sampledDist < minDist)
{
minDist = sampledDist;
minCoord = uvec3 (readR, readG, readB);
foundMinCoord = true;
}
}
if ( !foundMinCoord ) continue;
if ( zBrick == 0 )
{
baseR = 0xFF000000u;
baseR |= (minCoord.x << 16);
baseR |= (minCoord.y << 8);
baseR |= minCoord.z;
}
else if ( zBrick == 1 )
{
baseG = 0xFF000000u;
baseG |= (minCoord.x << 16);
baseG |= (minCoord.y << 8);
baseG |= minCoord.z;
}
else if ( zBrick == 2 )
{
baseB = 0xFF000000u;
baseB |= (minCoord.x << 16);
baseB |= (minCoord.y << 8);
baseB |= minCoord.z;
}
else
{
baseA = 0xFF000000u;
baseA |= (minCoord.x << 16);
baseA |= (minCoord.y << 8);
baseA |= minCoord.z;
}
}
fragColor = vec4 (uintBitsToFloat(baseR), uintBitsToFloat(baseG), uintBitsToFloat(baseB), uintBitsToFloat(baseA));
} else if ( iFrame > 0 && offsetPower == -1.0 ) {
if ( !isInJFACompactionResolution )
{
fragColor = texelFetch (iChannel0, ivec2(fragCoord - vec2 (0.5)), 0);
return ;
}
vec3 baseCoord;
baseCoord.x = float (pixCoord.x % uint(grid0Range.x * 4.0)) - float(uint(grid0Range.x * 4.0)/2u);
baseCoord.y = float (pixCoord.y % uint(grid0Range.y * 4.0)) - float(uint(grid0Range.y * 4.0)/2u);
baseCoord.z = float (pixCoord.y / uint(grid0Range.y * 4.0)) * 16.0 - float(uint(grid0Range.z * 4.0)/2u);
baseCoord *= 0.25;
uint writeR = 0u, writeG = 0u, writeB = 0u, writeA = 0u;
for (int i = 0; i != 16; i++)
{
vec3 sampleLoc = baseCoord + vec3 (0, 0, float(i) * 0.25);
if ( sampleLoc.x < grid0Min.x || sampleLoc.x >= grid0Max.x ) continue;
if ( sampleLoc.y < grid0Min.y || sampleLoc.y >= grid0Max.y ) continue;
if ( sampleLoc.z < grid0Min.z || sampleLoc.z >= grid0Max.z ) continue;
float diffZ = sampleLoc.z - grid0Min.z;
float fractionalZ = fract(diffZ);
uint topLevelZ = uint(diffZ);
uint xBase = (topLevelZ % tilingSide) * 4u * uint(grid0Range.x);
uint yBase = (topLevelZ / tilingSide) * 4u * uint(grid0Range.y);
xBase += uint ((sampleLoc.x - grid0Min.x) * 4.0);
yBase += uint ((sampleLoc.y - grid0Min.y) * 4.0);
vec4 readFrag = texelFetch (iChannel0, ivec2(xBase, yBase), 0);
uint readUint;
if ( fractionalZ == 0.0 ) readUint = floatBitsToUint (readFrag.x);
else if ( fractionalZ == 0.25 ) readUint = floatBitsToUint (readFrag.y);
else if ( fractionalZ == 0.5 ) readUint = floatBitsToUint (readFrag.z);
else readUint = floatBitsToUint (readFrag.a);
uint readR = ((readUint & 0x00FF0000u) >> 16);
uint readG = ((readUint & 0x0000FF00u) >> 8);
uint readB = (readUint & 0x000000FFu);
vec3 sampledCoord = (vec3 (readR, readG, readB) - vec3(uint(grid0Range * 4.0)/2u)) * 0.25;
float sampledDist = clamp (floor (length (sampledCoord - sampleLoc) * 10.0), 0.0, 255.0);
int channelSelect = i / 4;
int shiftAmount = 24 - ((i % 4) * 8);
switch (channelSelect)
{
case 0:
writeR |= (uint(sampledDist) << shiftAmount);
break;
case 1:
writeG |= (uint(sampledDist) << shiftAmount);
break;
case 2:
writeB |= (uint(sampledDist) << shiftAmount);
break;
case 3:
writeA |= (uint(sampledDist) << shiftAmount);
break;
}
}
fragColor = vec4 (uintBitsToFloat(writeR), uintBitsToFloat(writeG), uintBitsToFloat(writeB), uintBitsToFloat(writeA));
} else
fragColor = texelFetch (iChannel0, ivec2(fragCoord - vec2 (0.5)), 0);
}
float sdfGrid0(vec3 p)
{
p = floor (p * 4.0) * 0.25;
if ( clamp (p, grid0Min, grid0Max - vec3 (0.25)) != p ) return 0.25;
float pzBlock = (p.z - grid0Min.z) * 4.0;
uint diffZ = uint (pzBlock * 0.0625);
uint xBase = uint ((p.x - grid0Min.x) * 4.0);
uint yBase = uint ((p.y - grid0Min.y) * 4.0);
yBase += diffZ * uint (grid0Range.y * 4.0);
uint pickChannelBits = uint (pzBlock) % 16u;
uint pickedUint;
vec4 readTexel = texelFetch (iChannel0, ivec2(xBase, yBase), 0);
switch (pickChannelBits / 4u)
{
case 0u:
pickedUint = floatBitsToUint (readTexel.x);
break;
case 1u:
pickedUint = floatBitsToUint (readTexel.y);
break;
case 2u:
pickedUint = floatBitsToUint (readTexel.z);
break;
case 3u:
pickedUint = floatBitsToUint (readTexel.a);
break;
}
uint fetchShift = (24u - ((pickChannelBits % 4u) * 8u));
uint readDistance = ((pickedUint & (0x000000FFu << fetchShift)) >> fetchShift);
return float(readDistance) * 0.1;
}
const vec3 grid1Min = vec3 (-5.00, -5.00, -5.00);
const vec3 grid1Max = vec3 (5.00, 5.00, 5.00);
const vec3 grid1Range = grid1Max - grid1Min;
uint svoObject1[592] = uint[](559947905u,130u,46269312u,514u,536913538u,2184161023u,3622749043u,4147064396u,4293156985u,3790071392u,1651699907u,872103072u,1078026339u,3887358020u,1128162041u,4268123168u,537552954u,1738155708u,741593847u,1070472734u,533601546u,34078727u,3696020863u,3435217855u,4268124129u,3764412824u,431961079u,255014664u,3231647631u,2680045679u,9421823u,803594597u,1618485168u,816871512u,142111045u,72090087u,3865470951u,2275311235u,1203885758u,3091078942u,505290511u,256839439u,2850578305u,2055373886u,3158228000u,1090609643u,1577713279u,2701099229u,2852786055u,2240251783u,3250832323u,3287015775u,3927875747u,179210760u,539454485u,742320490u,2212619744u,2678583536u,4089909235u,3347384179u,3099918671u,72348164u,1340573191u,2551735777u,4089676300u,1014219681u,1354825761u,2172752000u,2148855824u,1607523583u,4294926950u,3867049598u,2684302130u,3973005116u,3436183449u,3093720967u,4025457130u,4293983266u,2148018184u,805045324u,326958287u,1019999844u,1640872517u,2576956195u,51166268u,2576064246u,1724418184u,2165252088u,202174374u,1436711000u,128450449u,3006222284u,1334034329u,2122127391u,2644021224u,22464u,3615870869u,1748467833u,2575820798u,3279865870u,3436078694u,1717989171u,858995507u,2579003289u,872364028u,3502273487u,201911934u,2117625831u,2146386109u,179750935u,3893712383u,2863316241u,67113052u,1409563596u,2122219105u,2166258174u,4013945029u,3926689106u,3977466495u,2021621334u,2173250306u,45943586u,1644175487u,1997581936u,2339600512u,2829189768u,3951733437u,1138987248u,275792123u,4206366032u,4285725702u,572653610u,2694884031u,1535815587u,8705089u,3291764692u,3943765237u,1342842926u,3539979567u,1278228416u,3286755302u,471359713u,4024929288u,283567880u,46050974u,804831041u,4244354034u,837873910u,429496440u,2063027404u,3469659377u,2143745606u,4110828559u,1090848015u,4278225794u,4248813684u,1965510609u,71297433u,2576975390u,135798780u,3235695823u,511462605u,3495325652u,1204830535u,3556466879u,1234639321u,2603748569u,999504281u,2576980381u,3703955248u,1576739793u,3707769168u,665610u,2284321723u,862194491u,854301081u,2577375180u,3435973708u,3865503718u,1718827827u,858995609u,2540214679u,4091404275u,3285979088u,2207244192u,1606320834u,2715632831u,3227104005u,3573644945u,1560807484u,1549565055u,3824192436u,4191287680u,2688614713u,3901979492u,12319539u,617479971u,159911945u,2227981516u,3318676486u,4260889597u,2650543998u,3363889280u,136340981u,1363385407u,1816132987u,4190101902u,505019632u,3232768012u,134390307u,3458330599u,235409652u,3889163655u,3884322936u,2021405932u,335020275u,2865048571u,3100066627u,2210472425u,4265733119u,2698683736u,168427648u,2162017473u,2131509235u,4125052956u,3776047208u,578299901u,1357383035u,3363929728u,547365567u,4118676986u,250673840u,4166547832u,1410073614u,67492609u,3940177759u,2148016175u,4048997443u,3942509562u,101040571u,3782930239u,808791871u,267558815u,2644349408u,1795146106u,3118634976u,1040712128u,1149805058u,2801778361u,4089589754u,1008765072u,2357230604u,205274366u,637933681u,3860758076u,4035120910u,1048475233u,2173075071u,1865886736u,3274689503u,4019722273u,3823312863u,3435934147u,4077878997u,1861709888u,487394042u,4286083136u,1099407160u,1949449266u,404232461u,223285167u,2566374279u,2277407628u,4221767592u,939874293u,339826516u,1097161744u,1162625345u,2110403037u,3478920513u,1157911893u,4184692743u,3914361864u,1009266182u,236863351u,242020517u,1359931460u,4280641u,2069922273u,2518644255u,230622463u,2141166712u,2019591046u,943310379u,2618310728u,4219502224u,578813684u,3151151744u,3284901726u,272753615u,505219214u,142640258u,2192172014u,436009200u,2062569230u,4260894842u,4018415696u,1210581536u,2711621872u,2826856572u,1010580536u,1073081333u,1793197594u,3172600330u,11012128u,1611147784u,104466435u,249867258u,42796546u,2148432557u,4221539940u,1174644737u,50398081u,21056593u,1776329854u,1979674867u,859733052u,1347788400u,2291663487u,3290184880u,3284321219u,4283305496u,2053235163u,51317248u,1141128718u,519889407u,4042322112u,2160131855u,249067640u,1749516551u,3278553407u,3222603031u,1601992768u,1082623879u,473940938u,1010736649u,3789592545u,4061133325u,3936355670u,4293477270u,491658781u,202247559u,2280489479u,4269894293u,1648113281u,1432047022u,3134722169u,270959118u,10137434u,3854491729u,1092075839u,1769881737u,2753263199u,318967608u,3809427358u,2019725214u,1981817384u,136474160u,870199824u,268435600u,1342218112u,2231730442u,3745566404u,4007682815u,2398019871u,3279154956u,214749153u,3791619974u,1718090812u,737276727u,4027589630u,1650948199u,4168581217u,4281545591u,4008697652u,4258598976u,361046440u,2611486705u,1919992030u,1007471535u,2214037445u,2212528608u,568455664u,4042321923u,4202190840u,3349171136u,3162307851u,3781255649u,1163138067u,3949715957u,3786925552u,806416624u,4172837873u,2267575219u,2736650018u,3114377607u,3999305983u,418435304u,1350058245u,1594156934u,2279084133u,4168938481u,3271765052u,903995650u,515775728u,2694882352u,338178022u,238838185u,3768844275u,51150653u,1043592185u,4286645368u,2021150741u,1461379358u,1852076099u,4265938961u,62527926u,160003721u,2720049834u,89566070u,2859877476u,1149811643u,3937051178u,2717967910u,1822842363u,2356405004u,3719508377u,2576416768u,1016705u,2257042639u,1055998918u,4267891992u,577216261u,3435646015u,3975046767u,2733008507u,926545651u,994023899u,2172680601u,2681467183u,50121653u,219644165u,1384219101u,3173248832u,4031809502u,133701571u,3233861566u,601890912u,2141167600u,2418029371u,34078523u,2552756206u,511696239u,4013703119u,1048278372u,2672104512u,1429175632u,903897438u,504241680u,4279177022u,2063087663u,2399305095u,2214199979u,2711743361u,4144957614u,1357926640u,4287135736u,2079854712u,1343576701u,4043659391u,410548292u,809251354u,464953258u,2864390306u,3090852095u,4294884414u,4215028784u,540909169u,1369313285u,1433763159u,3612426560u,346687544u,40548715u,4271291934u,2684227608u,142693727u,3958756801u,1074112682u,2161322u,4210536096u,1429173524u,669507653u,2690258928u,850656248u,144437275u,4293265058u,2852257806u,570878471u,2968500995u,4060283280u,62u,35529246u,2682059979u,4038135565u,259941984u,377389033u,2134848307u,801913919u,2677643751u,3860733903u,3436950320u,265015568u,1574439426u,117654483u,2581199489u,3925816066u,1085882146u,96114712u,407718972u,1124103518u,4049556800u,2155929553u,1094960506u,530595448u,4192321926u,102694128u,3766552447u,411583488u,2550691964u,203177059u,4294836283u,4123013071u,3659067627u,252645144u,2139602544u,1143087166u,505167361u,4269338848u,4294269071u,3132080255u,2273543998u,3095561158u,941625223u,2172618159u,4230480940u,536101205u,3422437344u,4125223441u,1308983424u,58753505u,4106059472u,3222136688u,1074234630u,0u,4459558u,101061121u,2031887u,84811655u,2273605759u,3284320575u,3393257480u,2156380610u,1050427360u,505290271u,4028625018u,2147737633u,3823193072u,252645182u,4161781108u,1080041473u,1884295408u,4168645240u,1886928672u,536902996u,970197636u,473398248u,2504408564u,1075072469u,267984894u,3829919706u,106954752u,0u);
uint readBitsSVO1 (uint bitLoc, uint numBits) {
uint wordLoc = bitLoc / 32u;
uint leftToRead = (32u - (bitLoc % 32u));
if (numBits <= leftToRead) {
uint shiftToMask = leftToRead - numBits;
uint masker = 0xFFFFFFFFu;
masker >>= uint(32u - numBits);
masker <<= shiftToMask;
uint value = (svoObject1[wordLoc] & masker);
value >>= shiftToMask;
return value;
} else {
uint bottomBits = numBits - leftToRead;
uint masker = 0xFFFFFFFFu;
masker >>= uint(32u - leftToRead);
uint topNum = (svoObject1[wordLoc] & masker);
uint bottomMasker = 0xFFFFFFFFu;
uint bottomShifter = uint(32u - bottomBits);
bottomMasker <<= bottomShifter;
uint value = (svoObject1[wordLoc + 1u] & bottomMasker);
uint bottomNum = (value >> bottomShifter);
return ((topNum << bottomBits) | bottomNum);
}
}
bool readLeafSVO1 (vec3 samplePos, vec3 sampleDir, out vec3 skipPos) {
skipPos = vec3 (10000.0);
if ( any(lessThan(samplePos, grid1Min)) || any(greaterThan(samplePos, grid1Max)) ) return false;
uvec3 topBrickPos = uvec3 (samplePos - grid1Min);
uint topBrickId = topBrickPos.z + topBrickPos.y * uint(grid0Range.x) + topBrickPos.x * uint(grid0Range.y) * uint(grid0Range.z);
uint streamReadPos = 0u;
for (int i = 0; i < int(topBrickId); i++) {
uint isOcc = readBitsSVO1 (streamReadPos, 1u);
streamReadPos += 1u;
if (isOcc == 1u) {
uint countMidBricks = countSetBits (readBitsSVO1 (streamReadPos, 8u));
streamReadPos += (8u + countMidBricks * 8u);
}
}
uint topBrick = readBitsSVO1 (streamReadPos, 1u);
if (topBrick == 0u) {
vec3 topBrickMin = grid1Min + vec3 (topBrickPos);
vec3 topBrickMax = topBrickMin + vec3 (1.0);
vec3 p1 = samplePos;
vec3 p2 = p1 + sampleDir * 2.0;
vec3 m = p2 - p1;
float tMin, tMax;
rayBoxIntersectTime (p1, vec3(1.0)/m, topBrickMin, topBrickMax, tMin, tMax);
skipPos = p1 + m * (tMax + 0.01);
return false;
}
streamReadPos += 1u;
uint midBricks = readBitsSVO1 (streamReadPos, 8u);
streamReadPos += 8u;
vec3 topBrickMinCorner = grid1Min + vec3 (topBrickPos);
vec3 sampleRelativeToTopBrick = fract (samplePos);
uint checkMidBrickBit = 0x80u;
vec3 sampleRelativeToMidBrick = sampleRelativeToTopBrick;
vec3 midBrickPos = vec3 (0.0);
if ( sampleRelativeToTopBrick.x > 0.5 ) {
sampleRelativeToMidBrick.x -= 0.5;
midBrickPos.x = 0.5;
checkMidBrickBit >>= 4u;
}
if ( sampleRelativeToTopBrick.y > 0.5 ) {
sampleRelativeToMidBrick.y -= 0.5;
midBrickPos.y = 0.5;
checkMidBrickBit >>= 2u;
}
if ( sampleRelativeToTopBrick.z > 0.5 ) {
sampleRelativeToMidBrick.z -= 0.5;
midBrickPos.z = 0.5;
checkMidBrickBit >>= 1u;
}
if ( (midBricks & checkMidBrickBit) == 0u ) {
vec3 midBrickMin = grid1Min + vec3 (topBrickPos) + midBrickPos;
vec3 midBrickMax = midBrickMin + vec3 (0.5);
vec3 p1 = samplePos;
vec3 p2 = p1 + sampleDir * 2.0;
vec3 m = p2 - p1;
float tMin, tMax;
rayBoxIntersectTime (p1, vec3(1.0)/m, midBrickMin, midBrickMax, tMin, tMax);
skipPos = p1 + m * (tMax + 0.01);
return false;
}
uint skipMidBricks = countSetBitsBefore (midBricks, checkMidBrickBit);
streamReadPos += (8u * skipMidBricks);
uint finalMidBrick = readBitsSVO1 (streamReadPos, 8u);
uint checkVoxelBrickBit = 0x80u;
if ( sampleRelativeToMidBrick.x > 0.25 ) {
checkVoxelBrickBit >>= 4u;
}
if ( sampleRelativeToMidBrick.y > 0.25 ) {
checkVoxelBrickBit >>= 2u;
}
if ( sampleRelativeToMidBrick.z > 0.25 ) {
checkVoxelBrickBit >>= 1u;
}
if ( (checkVoxelBrickBit & finalMidBrick) != 0u ) return true;
skipPos = samplePos + sampleDir * 0.25;
return false;
}
bool traceRaySVO1(vec3 p1, vec3 p2, out vec3 hitPos) {
vec3 m = p2 - p1;
float hitMin, hitMax;
if ( !rayBoxIntersectTime (p1, vec3(1.0)/m, grid1Min, grid1Max, hitMin, hitMax) ) {
hitPos = vec3 (-1.0);
return false;
}
hitMin += 0.00001;
hitMax -= 0.00001;
vec3 curPos = p1 + hitMin * m;
vec3 curDir = normalize (m);
vec3 skipPos = vec3 (0.0);
for (int i = 0; i != 100; i++) {
if (readLeafSVO1 (curPos, curDir, skipPos)) {
hitPos = curPos;
return true;
}
if ( skipPos == vec3(10000.0) ) break;
curPos = skipPos;
}
return false;
}
void decodeSVO1( inout vec4 fragColor, vec2 fragCoord ) {
uvec2 pixCoord = uvec2 (fragCoord - vec2 (0.5));
if ( iFrame % 60 == 0 && all (lessThan (pixCoord,uvec2 (uint(grid1Range.x), uint(grid1Range.y * grid1Range.z)/2u))) ) {
uint writeR = 0u, writeG = 0u, writeB = 0u, writeA = 0u;
vec3 baseCoord;
baseCoord.x = float (pixCoord.x % uint(grid1Range.x)) - float(uint(grid1Range.x)/2u);
baseCoord.y = float (pixCoord.y % uint(grid1Range.y)) - float(uint(grid1Range.y)/2u);
baseCoord.z = float (pixCoord.y / uint(grid1Range.y)) * 2.0 - float(uint(grid1Range.z)/2u);
for (uint zone = 0u; zone != 2u; zone++) {
for (uint k = 0u;k != 4u; k++) {
for (uint j = 0u;j != 4u; j++) {
for (uint i = 0u;i != 4u; i++) {
bool occupancy = readLeafSVO1 (baseCoord + vec3 (i, j, k) * 0.25 + vec3 (0.001, -0.001, float(zone) + 0.001));
if (!occupancy) continue;
uint write = (1u << i);
write <<= (j * 4u);
if ( zone == 0u ) {
if ( k < 2u ) {
write <<= (k * 16u);
writeR |= write;
} else {
write <<= ((k - 2u) * 16u);
writeG |= write;
}
} else {
if ( k < 2u ) {
write <<= (k * 16u);
writeB |= write;
} else {
write <<= ((k - 2u) * 16u);
writeA |= write;
}
}
}
}
}
}
fragColor = vec4 (uintBitsToFloat(writeR), uintBitsToFloat(writeG), uintBitsToFloat(writeB), uintBitsToFloat(writeA));
}
else {
fragColor = texelFetch (iChannel1, ivec2(fragCoord - vec2 (0.5)), 0);
}
}
bool occupancyReadGrid1 (vec3 samplePos) {
if ( any(lessThan(samplePos, grid1Min)) || any(greaterThan(samplePos, grid1Max)) ) return false;
vec3 samplePosRel = samplePos - grid1Min;
uvec3 fetchPos = uvec3 (samplePosRel);
ivec2 sampleCoord = ivec2 (fetchPos.x, fetchPos.y + uint (fetchPos.z/2u) * uint(grid1Range.y));
vec4 fetchTexel = texelFetch (iChannel1, sampleCoord, 0);
uvec4 fetchTexelRGBA = uvec4 (floatBitsToUint(fetchTexel.x), floatBitsToUint(fetchTexel.y), floatBitsToUint(fetchTexel.z), floatBitsToUint(fetchTexel.a));
uvec3 checkBits = uvec3 (fract (samplePosRel) * 4.0);
uint shiftBits = 0u;
if (checkBits.z < 2u)
shiftBits = checkBits.x + checkBits.y * 4u + checkBits.z * 16u;
else
shiftBits = checkBits.x + checkBits.y * 4u + (checkBits.z - 2u) * 16u;
uint readUint;
if ((fetchPos.z % 2u) == 0u)
if (checkBits.z < 2u)
readUint = fetchTexelRGBA.x;
else
readUint = fetchTexelRGBA.y;
else
if (checkBits.z < 2u)
readUint = fetchTexelRGBA.z;
else
readUint = fetchTexelRGBA.a;
uint maskBits = (1u << shiftBits);
if ( (readUint & maskBits) == 0u ) return false;
return true;
}
void decodeAndJFA_SVO1( out vec4 fragColor, in vec2 fragCoord )
{
uvec2 pixCoord = uvec2 (fragCoord - vec2 (0.5));
const uint tilingSide = uint (floor (sqrt(grid1Range.z))) + 1u;
int log2N = int(ceil(log2(max (max(grid1Range.x * 4.0, grid1Range.y * 4.0), grid1Range.z * 4.0))));
float offsetPower = float (log2N - (iFrame-1) - 1);
bool isInJFAResolution = all (lessThan (pixCoord,uvec2 (uint(grid1Range.x), uint(grid1Range.y)) * 4u * tilingSide));
float zRange = ceil (grid1Range.z * 4.0 * 0.0625);
bool isInJFACompactionResolution = all (lessThan (pixCoord, uvec2 (uint(grid1Range.x * 4.0), uint(grid1Range.y * 4.0 * zRange))));
if ( iFrame == 0 && isInJFAResolution ) {
vec3 baseCoord;
baseCoord.x = float (pixCoord.x % uint(grid1Range.x * 4.0)) - float(uint(grid1Range.x * 4.0)/2u);
baseCoord.y = float (pixCoord.y % uint(grid1Range.y * 4.0)) - float(uint(grid1Range.y * 4.0)/2u);
baseCoord.z = (float (pixCoord.y / uint(grid1Range.y * 4.0)) * float(tilingSide) + float (pixCoord.x / uint(grid1Range.x * 4.0))) * 4.0 - float(uint(grid1Range.z * 4.0)/2u);
baseCoord *= 0.25;
if ( baseCoord.z >= grid1Max.z )
{
fragColor = vec4 (uintBitsToFloat(0u), uintBitsToFloat(0u), uintBitsToFloat(0u), uintBitsToFloat(0u));
return ;
}
uint writeR = 0u, writeG = 0u, writeB = 0u, writeA = 0u;
vec3 pos1 = baseCoord + vec3 (0, 0, 0);
vec3 pos2 = baseCoord + vec3 (0, 0, 0.25);
vec3 pos3 = baseCoord + vec3 (0, 0, 0.5);
vec3 pos4 = baseCoord + vec3 (0, 0, 0.75);
bool occupancy1 = readLeafSVO1 (pos1 + vec3 (0.001, -0.001, 0.001));
bool occupancy2 = readLeafSVO1 (pos2 + vec3 (0.001, -0.001, 0.001));
bool occupancy3 = readLeafSVO1 (pos3 + vec3 (0.001, -0.001, 0.001));
bool occupancy4 = readLeafSVO1 (pos4 + vec3 (0.001, -0.001, 0.001));
if ( occupancy1 )
{
pos1 *= 4.0;
pos1 += vec3(uint(grid1Range * 4.0)/2u);
writeR = 0xFF000000u;
writeR |= (uint(pos1.x) << 16);
writeR |= (uint(pos1.y) << 8);
writeR |= uint(pos1.z);
}
if ( occupancy2 )
{
pos2 *= 4.0;
pos2 += vec3(uint(grid1Range * 4.0)/2u);
writeG = 0xFF000000u;
writeG |= (uint(pos2.x) << 16);
writeG |= (uint(pos2.y) << 8);
writeG |= uint(pos2.z);
}
if ( occupancy3 )
{
pos3 *= 4.0;
pos3 += vec3(uint(grid1Range * 4.0)/2u);
writeB = 0xFF000000u;
writeB |= (uint(pos3.x) << 16);
writeB |= (uint(pos3.y) << 8);
writeB |= uint(pos3.z);
}
if ( occupancy4 )
{
pos4 *= 4.0;
pos4 += vec3(uint(grid1Range * 4.0)/2u);
writeA = 0xFF000000u;
writeA |= (uint(pos4.x) << 16);
writeA |= (uint(pos4.y) << 8);
writeA |= uint(pos4.z);
}
fragColor = vec4 (uintBitsToFloat(writeR), uintBitsToFloat(writeG), uintBitsToFloat(writeB), uintBitsToFloat(writeA));
} else if ( iFrame > 0 && offsetPower >= 0.0 && isInJFAResolution ) {
vec3 baseCoord;
baseCoord.x = float (pixCoord.x % uint(grid1Range.x * 4.0)) - float(uint(grid0Range.x * 4.0)/2u);
baseCoord.y = float (pixCoord.y % uint(grid1Range.y * 4.0)) - float(uint(grid0Range.y * 4.0)/2u);
baseCoord.z = (float (pixCoord.y / uint(grid1Range.y * 4.0)) * float(tilingSide) + float (pixCoord.x / uint(grid1Range.x * 4.0))) * 4.0 - float(uint(grid1Range.z * 4.0)/2u);
baseCoord *= 0.25;
if ( baseCoord.z >= grid1Max.z )
{
fragColor = texelFetch (iChannel1, ivec2(fragCoord - vec2 (0.5)), 0);
return ;
}
vec4 fragmentRead = texelFetch (iChannel1, ivec2(fragCoord - vec2 (0.5)), 0);
uint baseR = floatBitsToUint(fragmentRead.x);
uint baseG = floatBitsToUint(fragmentRead.y);
uint baseB = floatBitsToUint(fragmentRead.z);
uint baseA = floatBitsToUint(fragmentRead.a);
float offsetAmt = pow (2.0, offsetPower);
for (int zBrick = 0; zBrick != 4; zBrick++)
{
vec3 curCoord = baseCoord + vec3 (0, 0, float(zBrick) * 0.25);
float minDist = 100000.0;
uvec3 minCoord = uvec3 (0);
bool foundMinCoord = false;
for (int i = -1; i != 2; i++)
for (int j = -1; j != 2; j++)
for (int k = -1; k != 2; k++)
{
vec3 sampleLoc = curCoord + vec3(i, j, k) * 0.25 * offsetAmt;
if ( sampleLoc.x < grid1Min.x || sampleLoc.x >= grid1Max.x ) continue;
if ( sampleLoc.y < grid1Min.y || sampleLoc.y >= grid1Max.y ) continue;
if ( sampleLoc.z < grid1Min.z || sampleLoc.z >= grid1Max.z ) continue;
float diffZ = sampleLoc.z - grid1Min.z;
float fractionalZ = fract(diffZ);
uint topLevelZ = uint(diffZ);
uint xBase = (topLevelZ % tilingSide) * 4u * uint(grid1Range.x);
uint yBase = (topLevelZ / tilingSide) * 4u * uint(grid1Range.y);
xBase += uint ((sampleLoc.x - grid1Min.x) * 4.0);
yBase += uint ((sampleLoc.y - grid1Min.y) * 4.0);
vec4 readFrag = texelFetch (iChannel1, ivec2(xBase, yBase), 0);
uint readUint;
if ( fractionalZ == 0.0 ) readUint = floatBitsToUint (readFrag.x);
else if ( fractionalZ == 0.25 ) readUint = floatBitsToUint (readFrag.y);
else if ( fractionalZ == 0.5 ) readUint = floatBitsToUint (readFrag.z);
else readUint = floatBitsToUint (readFrag.a);
uint readR = ((readUint & 0x00FF0000u) >> 16);
uint readG = ((readUint & 0x0000FF00u) >> 8);
uint readB = (readUint & 0x000000FFu);
vec3 sampledCoord = (vec3 (readR, readG, readB) - vec3(uint(grid1Range * 4.0)/2u)) * 0.25;
float sampledDist = length (sampledCoord - curCoord);
if ( readUint != 0u && sampledDist < minDist)
{
minDist = sampledDist;
minCoord = uvec3 (readR, readG, readB);
foundMinCoord = true;
}
}
if ( !foundMinCoord ) continue;
if ( zBrick == 0 )
{
baseR = 0xFF000000u;
baseR |= (minCoord.x << 16);
baseR |= (minCoord.y << 8);
baseR |= minCoord.z;
}
else if ( zBrick == 1 )
{
baseG = 0xFF000000u;
baseG |= (minCoord.x << 16);
baseG |= (minCoord.y << 8);
baseG |= minCoord.z;
}
else if ( zBrick == 2 )
{
baseB = 0xFF000000u;
baseB |= (minCoord.x << 16);
baseB |= (minCoord.y << 8);
baseB |= minCoord.z;
}
else
{
baseA = 0xFF000000u;
baseA |= (minCoord.x << 16);
baseA |= (minCoord.y << 8);
baseA |= minCoord.z;
}
}
fragColor = vec4 (uintBitsToFloat(baseR), uintBitsToFloat(baseG), uintBitsToFloat(baseB), uintBitsToFloat(baseA));
} else if ( iFrame > 0 && offsetPower == -1.0 ) {
if ( !isInJFACompactionResolution )
{
fragColor = texelFetch (iChannel1, ivec2(fragCoord - vec2 (0.5)), 0);
return ;
}
vec3 baseCoord;
baseCoord.x = float (pixCoord.x % uint(grid1Range.x * 4.0)) - float(uint(grid1Range.x * 4.0)/2u);
baseCoord.y = float (pixCoord.y % uint(grid1Range.y * 4.0)) - float(uint(grid1Range.y * 4.0)/2u);
baseCoord.z = float (pixCoord.y / uint(grid1Range.y * 4.0)) * 16.0 - float(uint(grid1Range.z * 4.0)/2u);
baseCoord *= 0.25;
uint writeR = 0u, writeG = 0u, writeB = 0u, writeA = 0u;
for (int i = 0; i != 16; i++)
{
vec3 sampleLoc = baseCoord + vec3 (0, 0, float(i) * 0.25);
if ( sampleLoc.x < grid1Min.x || sampleLoc.x >= grid1Max.x ) continue;
if ( sampleLoc.y < grid1Min.y || sampleLoc.y >= grid1Max.y ) continue;
if ( sampleLoc.z < grid1Min.z || sampleLoc.z >= grid1Max.z ) continue;
float diffZ = sampleLoc.z - grid1Min.z;
float fractionalZ = fract(diffZ);
uint topLevelZ = uint(diffZ);
uint xBase = (topLevelZ % tilingSide) * 4u * uint(grid1Range.x);
uint yBase = (topLevelZ / tilingSide) * 4u * uint(grid1Range.y);
xBase += uint ((sampleLoc.x - grid1Min.x) * 4.0);
yBase += uint ((sampleLoc.y - grid1Min.y) * 4.0);
vec4 readFrag = texelFetch (iChannel1, ivec2(xBase, yBase), 0);
uint readUint;
if ( fractionalZ == 0.0 ) readUint = floatBitsToUint (readFrag.x);
else if ( fractionalZ == 0.25 ) readUint = floatBitsToUint (readFrag.y);
else if ( fractionalZ == 0.5 ) readUint = floatBitsToUint (readFrag.z);
else readUint = floatBitsToUint (readFrag.a);
uint readR = ((readUint & 0x00FF0000u) >> 16);
uint readG = ((readUint & 0x0000FF00u) >> 8);
uint readB = (readUint & 0x000000FFu);
vec3 sampledCoord = (vec3 (readR, readG, readB) - vec3(uint(grid1Range * 4.0)/2u)) * 0.25;
float sampledDist = clamp (floor (length (sampledCoord - sampleLoc) * 10.0), 0.0, 255.0);
int channelSelect = i / 4;
int shiftAmount = 24 - ((i % 4) * 8);
switch (channelSelect)
{
case 0:
writeR |= (uint(sampledDist) << shiftAmount);
break;
case 1:
writeG |= (uint(sampledDist) << shiftAmount);
break;
case 2:
writeB |= (uint(sampledDist) << shiftAmount);
break;
case 3:
writeA |= (uint(sampledDist) << shiftAmount);
break;
}
}
fragColor = vec4 (uintBitsToFloat(writeR), uintBitsToFloat(writeG), uintBitsToFloat(writeB), uintBitsToFloat(writeA));
} else
fragColor = texelFetch (iChannel1, ivec2(fragCoord - vec2 (0.5)), 0);
}
float sdfGrid1(vec3 p)
{
p = floor (p * 4.0) * 0.25;
if ( clamp (p, grid1Min, grid1Max - vec3 (0.25)) != p ) return 0.25;
float pzBlock = (p.z - grid1Min.z) * 4.0;
uint diffZ = uint (pzBlock * 0.0625);
uint xBase = uint ((p.x - grid1Min.x) * 4.0);
uint yBase = uint ((p.y - grid1Min.y) * 4.0);
yBase += diffZ * uint (grid1Range.y * 4.0);
uint pickChannelBits = uint (pzBlock) % 16u;
uint pickedUint;
vec4 readTexel = texelFetch (iChannel1, ivec2(xBase, yBase), 0);
switch (pickChannelBits / 4u)
{
case 0u:
pickedUint = floatBitsToUint (readTexel.x);
break;
case 1u:
pickedUint = floatBitsToUint (readTexel.y);
break;
case 2u:
pickedUint = floatBitsToUint (readTexel.z);
break;
case 3u:
pickedUint = floatBitsToUint (readTexel.a);
break;
}
uint fetchShift = (24u - ((pickChannelBits % 4u) * 8u));
uint readDistance = ((pickedUint & (0x000000FFu << fetchShift)) >> fetchShift);
return float(readDistance) * 0.1;
}
const vec3 grid2Min = vec3 (-5.00, -5.00, -5.00);
const vec3 grid2Max = vec3 (5.00, 5.00, 5.00);
const vec3 grid2Range = grid2Max - grid2Min;
uint svoObject2[406] = uint[](140517500u,872168448u,0u,131619u,53674755u,4261815835u,3193971175u,3791598371u,4042534961u,4090494731u,2684352639u,1711570767u,3405523952u,924066117u,1975578498u,663121657u,2724626430u,4233327587u,3288326141u,4261011714u,1719239068u,3430467388u,3896524816u,2361393382u,545254143u,4294967171u,859550515u,938866161u,530668769u,1074819313u,2146990200u,2147055624u,180108799u,3287531039u,3995016208u,34118415u,3482259215u,2231732096u,1090724097u,8742u,571683635u,855849369u,2568616444u,217843729u,17805197u,2141745799u,2369586110u,716992487u,2716594078u,1726459658u,150319087u,135069826u,96584131u,60422774u,2716453943u,4009543876u,1145045376u,3221228167u,2852648895u,993192259u,3621765017u,4226523032u,348105258u,145721345u,4262849143u,2153090799u,2867505834u,2884272770u,2820669610u,2864351272u,3941245482u,3955372014u,4273012783u,3954524202u,3131746986u,3145861922u,9416609u,1535475690u,2754087592u,169878063u,2139081778u,3967882047u,4024917528u,283352989u,178915328u,17830297u,4159060967u,1341599424u,3237166881u,3764414433u,4028670077u,71128117u,1412915527u,1718968294u,2000638782u,3812035704u,1470496647u,3156749372u,140517484u,826228253u,2652770568u,134217796u,71174686u,505289226u,134225665u,253173503u,2281669375u,2147993554u,4257467459u,3841802726u,1077936802u,2729091567u,4010543152u,4159638758u,2188306393u,2800257198u,3934298794u,537063050u,3085740718u,2785414714u,2935533535u,1881333792u,2725249008u,1290809597u,3121596431u,2927392888u,2055374396u,603204564u,672485392u,1434451936u,4227323399u,1141310451u,4088002942u,2156002619u,49613812u,3218375937u,1426589709u,1565875540u,232739157u,1681948028u,735849428u,337728853u,1107823439u,72350479u,1307568121u,1717047407u,4194094092u,863239372u,1046875705u,4192601662u,4268794049u,259946728u,1077985152u,2291630251u,2868888235u,3953229953u,2428820728u,1719563296u,536870912u,71568588u,322117491u,859806081u,109042049u,2627502157u,3756138239u,4193229262u,84869119u,3423254666u,4223143767u,3015188995u,2719127689u,2576465405u,1412092135u,2860778500u,1078459402u,4235186347u,301757776u,720371415u,1107699205u,1433910133u,3587523585u,1158681677u,1432178965u,3642161674u,34209537u,50597175u,4258249727u,25276414u,2122193279u,14540798u,1060832085u,3958287018u,1090600809u,4282425207u,1754755887u,185608449u,2440122385u,3883390295u,74925071u,2729441799u,3794252623u,3148084207u,2680153958u,1720261119u,4080206860u,822267084u,4145675140u,161080947u,864172962u,1081584u,142084112u,2018509328u,402734899u,922951550u,4171210649u,2676091486u,3700169981u,3367898654u,521673227u,134265528u,2289535146u,3803949696u,2954370992u,3735308973u,3581018176u,1144908465u,3722583726u,4124092576u,2239688564u,380114924u,604378453u,1977048412u,1162101085u,1564738781u,1171591551u,2422024706u,681572221u,4291031811u,202182654u,2130188321u,2173142975u,3757030448u,567791550u,353637429u,1252704095u,1157698555u,4261710207u,3791650607u,571411503u,528555921u,2003915071u,3980883015u,1353097218u,4289098675u,926874275u,1067427432u,404398015u,3274783756u,3287547745u,2577285627u,4147362339u,838862880u,2105293843u,4293989171u,4240439487u,4120484343u,4294770677u,2328881749u,2718005015u,33453847u,3942416248u,2021147771u,4249009092u,1788698624u,2158690177u,2009595807u,3359260350u,2130158918u,1071155374u,3760890538u,2863316623u,3993634830u,2919754374u,3894847498u,2827659274u,2720137894u,2930441903u,1392422402u,242154993u,2835016362u,39870951u,4160008167u,2411737136u,4278691719u,2817540184u,1816051336u,22040661u,4160194309u,142145783u,1673739516u,3271768544u,2174352353u,2852109531u,2689466127u,3121152256u,1414861892u,905278814u,2690557296u,539506808u,142441853u,2105442686u,2202091840u,520949583u,1041230329u,2122710535u,3993435146u,4244569343u,51543590u,1853784057u,3254650366u,4271962240u,2160082959u,2290321967u,4221545470u,2209907330u,2172764352u,3235931744u,1612710114u,578694911u,2147193139u,1069535295u,3650879423u,4294187034u,185540631u,387389671u,264206456u,1613334407u,2273541633u,4228556938u,2827126783u,3156346885u,3284386755u,4228279871u,1440858433u,2162819744u,2863378186u,737542128u,4244440829u,2192570488u,1210581119u,1062195248u,271597315u,4292349976u,403177472u,161001331u,4268019507u,859012709u,4131472388u,68417194u,1141504950u,2579183411u,923090942u,4247507071u,1349862400u,1882206264u,403701760u,117507008u,0u,307u,20123955u,2583422873u,2571941868u,2348843691u,2281609214u,2172649406u,3082034750u,3768315647u,2690624744u,252444704u,2694970608u,1560257284u,4241389168u,1203519552u,272693360u,947414048u,536870912u,0u,1245443u,151093259u,3146255856u,2156896063u,3207643591u,1044365211u,1996482112u,3208644092u,348961863u,3742626814u,4292998467u,2652798717u,4261483383u,3439262968u,4131112000u,2118123972u,1157480694u,2147360864u,1616917248u);
uint readBitsSVO2 (uint bitLoc, uint numBits) {
uint wordLoc = bitLoc / 32u;
uint leftToRead = (32u - (bitLoc % 32u));
if (numBits <= leftToRead) {
uint shiftToMask = leftToRead - numBits;
uint masker = 0xFFFFFFFFu;
masker >>= uint(32u - numBits);
masker <<= shiftToMask;
uint value = (svoObject2[wordLoc] & masker);
value >>= shiftToMask;
return value;
} else {
uint bottomBits = numBits - leftToRead;
uint masker = 0xFFFFFFFFu;
masker >>= uint(32u - leftToRead);
uint topNum = (svoObject2[wordLoc] & masker);
uint bottomMasker = 0xFFFFFFFFu;
uint bottomShifter = uint(32u - bottomBits);
bottomMasker <<= bottomShifter;
uint value = (svoObject2[wordLoc + 1u] & bottomMasker);
uint bottomNum = (value >> bottomShifter);
return ((topNum << bottomBits) | bottomNum);
}