rev: 3827f53bae88e48a594610914ba74b9512ead6dd tukan/testing/test_tmt4.sc -rw-r--r-- 47.7 KiB View raw Log this file
3827f53bae88 — Leonard Ritter * more work on module system 3 months ago
                                                                                
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
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
#
    biquadratic interpolation
    with raymarching

#
    tetrahedral marching triangles

    step 1: identify by mask for each tet if its relevant vertices are partially set
    mask has 8 bits, 3 tets per bitarray = 2 x 24 bits
    all bits set or all bits not set = ignore, otherwise generate surface

    one tet is handled per thread

import ..tukan.voxel
let voxel = tukan.voxel

using import glm
using import glsl
using import Array
using import Box
using import struct
using import enum
using import ..tukan.gl
using import ..tukan.bitmap
using import ..tukan.packing
using import ..tukan.random
using import ..tukan.color
using import ..tukan.perfect_hash
using import ..tukan.raytrace
using import ..tukan.sdf
using import ..tukan.sdl
using import ..tukan.rotation
using import ..tukan.brdf
using import ..tukan.mesh
using import ..tukan.normal
using import ..tukan.projection
using import ..tukan.derivative
using import ..tukan.isosurface
using import ..tukan.hash
using import ..tukan.noise
using import ..tukan.GBuffer
using import ..tukan.File
using import ..tukan.imgui
using import ..tukan.GUI
using import .testfragment

let USE_PACKING =
    true
    #false

# regress to trilinear filtering
let USE_TRILINEAR_FILTER =
    #true
    false

let USE_WEIGHT_BLUR =
    true
    #false

let USE_BACKFACE_CULLING =
    true
    #false

let NEAR = 0.01

let MAX_VOXELS = (20 * (1 << 20))

# pack quantization
let PACKING_Q = 1.0

let BINDING_BUF_CELLS_IN = 1
let BINDING_BUF_CELLS_IN_INFO = 2
let BINDING_BUF_CELLS_OUT = 3
let BINDING_BUF_CELLS_OUT_INFO = 4
let BINDING_BUF_DRAW_CMD = 3
let BINDING_BUF_COMPUTE_CMD = 4
let BINDING_BUF_VERTEX_ATTR1_IN = 5
let BINDING_BUF_VERTEX_ATTR1_OUT = 6

let TU_GBUFFER = 0

let IMAGE_TARGET_RGBA32F = 1

let UNIFORM_LEVEL = 1
let UNIFORM_PROGRAM = 2
let UNIFORM_DIVISOR = 4

let ProgramVoxelizeInit = 0
let ProgramVoxelize = 1
let ProgramSimplify = 2

let sqrt3 = (sqrt 3.0)
let THREADSIZE = 256

let corner-vectors =
    arrayof vec3
        vec3 0 0 0
        vec3 1 0 0
        vec3 0 1 0
        vec3 1 1 0
        vec3 0 0 1
        vec3 1 0 1
        vec3 0 1 1
        vec3 1 1 1

run-stage;

struct CellValsInfo plain
    count : u32

struct CellVals plain
    # each entry holds a key
    entries : (array u32)

buffer buf-cells-in : CellVals
    binding = BINDING_BUF_CELLS_IN
    \ readonly coherent restrict

buffer buf-cells-in-info : CellValsInfo
    binding = BINDING_BUF_CELLS_IN_INFO
    \ readonly coherent restrict

buffer buf-cells-out : CellVals
    binding = BINDING_BUF_CELLS_OUT
    \ writeonly coherent restrict

buffer buf-cells-out-info : CellValsInfo
    binding = BINDING_BUF_CELLS_OUT_INFO
    \ coherent restrict

let CubeData =
    static-if USE_PACKING
        static-if USE_TRILINEAR_FILTER
            struct CubeData plain
                v0 : uvec4
        else
            struct CubeData plain
                v0 : uvec4
                v1 : uvec4
    else
        static-if USE_TRILINEAR_FILTER
            struct CubeData plain
                key : u32
                v : (array f32 8)
        else
            struct CubeData plain
                key : u32
                v : (array f32 27)

struct VertexAttr1 plain
    # cell key + simplex index, 4x u8 weights
    entries : (array CubeData)

buffer buf-vertex-attr1-in : VertexAttr1
    binding = BINDING_BUF_VERTEX_ATTR1_IN
    \ readonly coherent restrict

buffer buf-vertex-attr1-out : VertexAttr1
    binding = BINDING_BUF_VERTEX_ATTR1_OUT
    \ writeonly coherent restrict

uniform u-program : i32
    location = UNIFORM_PROGRAM

uniform u-level : i32
    location = UNIFORM_LEVEL

uniform smp-screen : GBuffer.SamplerType
    binding = TU_GBUFFER

struct DispatchIndirectCommandI plain
    command : DispatchIndirectCommand

struct DrawArraysIndirectCommandI plain
    command : DrawArraysIndirectCommand

buffer buf-compute-cmd : DispatchIndirectCommandI
    binding = BINDING_BUF_COMPUTE_CMD
    \ writeonly coherent restrict

buffer buf-draw-cmd : DrawArraysIndirectCommandI
    binding = BINDING_BUF_DRAW_CMD
    \ writeonly coherent restrict

uniform u-divisor : i32
    location = UNIFORM_DIVISOR

fn setup-compute-command ()
    local_size 1 1 1
    let divisor = ((deref u-divisor) as u32)
    buf-compute-cmd.command =
        DispatchIndirectCommand
            ((deref buf-cells-in-info.count) + divisor - 1:u32) // divisor
            1
            1
    ;

fn setup-draw-arrays-command ()
    local_size 1 1 1
    buf-draw-cmd.command =
        DrawArraysIndirectCommand
            deref buf-cells-in-info.count
            1
            0
            0
    ;

fn simple-sphere (p)
    (length p) - 0.5

fn nine-spheres (p)
    let x = (deref shglobals.time)
    r := (mix 0.1 0.2 ((sin x) * 0.5 + 0.5))
    d := (sqrt 0.5) * 0.4
    sdSmoothAnd
        sdSmoothOr
            (length p) - 0.5
            min
                (length (p - (vec3 d d d))) - r
                (length (p - (vec3 -d d d))) - r
                (length (p - (vec3 d -d d))) - r
                (length (p - (vec3 -d -d d))) - r
            0.1
        * -1.0
            min
                (length (p - (vec3 d d -d))) - r
                (length (p - (vec3 -d d -d))) - r
                (length (p - (vec3 d -d -d))) - r
                (length (p - (vec3 -d -d -d))) - r
        0.1

fn twoballs (p)
    let x = (deref shglobals.time)
    x := ((sin x) * 0.5 + 0.5) * 0.5
    sdSmoothOr
        (length (p - (vec3 0.0 0 x))) - 0.25
        (length (p - (vec3 0.0 0 -x))) - 0.25
        0.2

fn doubletori (p)
    let x = (deref shglobals.time)
    let xz =
        anglevector-rotate
            anglevector -x
            p.xz
    let p1 =
        vec3
            xz.x
            p.y
            xz.y
    let p2 =
        vec3
            anglevector-rotate
                anglevector x
                p.xy
            p.z

    'sdSmoothOr
        sdmDist
            sdTorus p1.xzy (vec2 0.5 0.05)
            sdMaterial
                vec4 1.0 0.5 0.3 1.0
        sdmDist
            sdTorus p2 (vec2 0.4 0.1)
            sdMaterial
                vec4 0.3 0.5 1.0 1.0
                roughness = 0.1
        0.2

fn tentacles (p)
    p := (vec3 p.x (p.y - 0.2) p.z)
    let x = (deref shglobals.time)
    r := (mix -1.0 1.0 ((sin x) * 0.5 + 0.5))

    local d =
        sdmDist inf
            sdMaterial (vec4 0.0)
    for a in (range 6)
        a := a as f32
        q1 := (anglevector (radians (360.0 * a / 6.0))) * 1.0
        q2 := (anglevector (radians (360.0 * (a + 2.0 * r) / 6.0))) * 0.2
        b :=
            sdBezier
                vec3 0 -0.8 0
                vec3 q1.x -0.6 q1.y
                vec3 q2.x 0.4 q2.y
                p
        bbby := b.y * b.y * b.y
        throb := (sin (2.0 * pi * (bbby - x * 0.1 + a * 0.3) * 12.0))
        armlen := b.y

        b := b.x - 0.09 * (exp -b.y) + throb * 0.01 * (1.0 - bbby)

        #d = (min d b)
        veinmatmix :=
            clamp (armlen - throb * 0.2) 0.0 1.0
        d =
            'sdSmoothOrM (deref d)
                sdmDist (b / 1.1)
                    sdMaterial
                        mix
                            vec4 0.3 0.5 1.0 1.0
                            vec4 1.0 0.3 0.3 1.0
                            veinmatmix
                        roughness =
                            mix 1.0 0.1 veinmatmix
                sdMaterial
                    vec4 2.0 1.8 0.5 1.0
                    roughness = 1.0
                0.15
                4.0
    deref d

fn two-boxes (p)
    let x = (deref shglobals.time)
    let p1 =
        vec3
            anglevector-rotate
                anglevector -x
                p.xy
            p.z
    let p2 =
        vec3
            p.x
            anglevector-rotate
                anglevector (x * 0.917)
                p.yz
    'sdSmoothOr
        sdmDist
            sdBox p1 (vec3 0.33)
            sdMaterial
                vec4 1.0 0.5 0.3 1.0
        sdmDist
            sdBox p2 (vec3 0.33)
            sdMaterial
                vec4 0.3 0.5 1.0 1.0
                roughness = 0.1
        0.2

fn two-boxes-merge (p)
    let d =
        vec3 0.3
    let sz =
        vec3 0.5
    let x = (deref shglobals.time)
    r := (mix 1.0 0.2 ((sin x) * 0.5 + 0.5))
    'sdSmoothAnd
        'sdSmoothOr
            sdmDist
                #sdSphere
                    p - (vec3 0.01)
                    1.0
                sdUberprim
                    p - (vec3 0.01)
                    vec4 1.0 1.0 0.05 0.05
                    vec3 0.5 0.05 0.0
                sdMaterial
                    vec4 1.0 0.9 0.3 1.0
                    metallic = 1.0
            'sdSmoothOr
                sdmDist
                    sdBox (p - d) sz
                    sdMaterial
                        vec4 1.0 0.5 0.3 1.0
                sdmDist
                    sdBox (p + d) sz
                    sdMaterial
                        vec4 0.3 0.5 1.0 1.0
                        roughness = 0.2
                0.5
            0.05
        sdmDist
            -
                sdSphere
                    p - (vec3 0.0 0.0 -0.5)
                    r
            sdMaterial
                vec4 0.5 0.3 1.0 1.0
        0.1

fn two-boxes-merge-deluxe (p)
    let d =
        vec3 0.3
    let sz =
        vec3 0.5
    let x = (deref shglobals.time)
    let sp1 =
        p - (vec3 0.0 0.0 -0.5)
    cwave := (sin (x * 10.0 + 2.0 * pi * 10.0 * (length sp1)))
    wave := cwave * 0.5 + 0.5
    r := (mix 1.0 0.2 ((sin x) * 0.5 + 0.5))
    'sdSmoothOr
        'sdSmoothAnd
            'sdSmoothOr
                sdmDist
                    #sdSphere
                        p - (vec3 0.01)
                        1.0
                    sdUberprim
                        p - (vec3 0.01)
                        vec4 1.0 1.0 0.05 0.05
                        vec3 0.5 0.05 0.0
                    sdMaterial
                        vec4 1.0 0.9 0.3 1.0
                        metallic = 1.0
                'sdSmoothOr
                    sdmDist
                        ((sdBox (p - d) (sz - 0.1)) - 0.1 + cwave * 0.01) / 1.3
                        sdMaterial
                            mix
                                vec4 1.0 0.5 0.3 1.0
                                (vec4 1.0 0.5 0.3 1.0) * 0.3
                                wave
                    sdmDist
                        sdBox (p + d) sz
                        sdMaterial
                            vec4 0.3 0.5 1.0 1.0
                            roughness = 0.2
                    0.5
                0.05
            sdmDist
                - (sdSphere sp1 r)
                sdMaterial
                    vec4 0.5 0.3 1.0 1.0
            0.1
        sdmDist
            sdEllipsoid
                p - (vec3 0.2 0.0 0.0)
                vec3 0.3 0.4 1.0
            sdMaterial
                vec4 0.3 1.0 0.5 1.0
        0.1

fn one-box (p)
    sdBox p (vec3 0.33)

fn noiseroid (p)
    np := (vec3 (p.x + shglobals.time * 0.05) p.yz)
    d := ((triquad-noise3 (np * 48.0)) * 2.0 - 1.0) / 48.0

    d := d + 0.0025
    #d := (abs (d - 0.02)) - 0.015

    'sdSmoothAnd
        sdmDist d
            sdMaterial
                vec4 0.2
                roughness = 1.0
        sdmDist
            sdTorus p (vec2 0.5 0.2)
            sdMaterial
                vec4 1.0
                roughness = 0.1
        0.01

fn matmapf (p)
    #do
        p := p.yzx * 2.0
        (two-boxes-merge p) * 0.5
    #doubletori p
    #two-boxes p
    #tentacles p
    noiseroid p

# single sphere:
    256^3: 89240 cells (best: 0.3ms)
    1024^3: 1427240 cells (best: 3.5ms)
# min: 104567 at 256^3
fn mapf (p)
    #let x = ((radians (deref shglobals.time)) * 10.0)
    #let xz =
        anglevector-rotate
            anglevector -x
            p.xz
    #let p =
        vec3
            xz.x
            p.y
            xz.y
    #simple-sphere p
    #twoballs p
    #doubletori p
    #nine-spheres p
    #two-boxes p
    #one-box p
    (matmapf p) as f32

fn normalmapf (p r)
    - (sdNormalFast mapf p r)

fn subdivide-cell (key)
    let level = ((deref u-level) as u32)
    let r = (/ (f32 (1:u32 << level)))
    let d = (2.0 * r)
    #let rlimit =
        if (level == 8:u32) r
        else (sqrt3 * r)

    key := (key << 3:u32)

    ucoord := (unpack-morton3x10 key)
    coord := (vec3 ucoord) * d - 1.0 + r

    global cells : (array u32 8)
    global written = 0:u32

    fn test-cell (i ofs key coord r)
        let rlimit = (sqrt3 * r)
        key := key | i
        pos := coord + ofs
        let dist = (mapf pos)
        let hit = ((abs dist) < rlimit)
        if hit
            cells @ (deref written) = key
            written += 1:u32

    inline test-cell (i ofs)
        test-cell i ofs key coord r

    test-cell 0:u32 (vec3 0 0 0)
    test-cell 1:u32 (vec3 d 0 0)
    test-cell 2:u32 (vec3 0 d 0)
    test-cell 3:u32 (vec3 d d 0)
    test-cell 4:u32 (vec3 0 0 d)
    test-cell 5:u32 (vec3 d 0 d)
    test-cell 6:u32 (vec3 0 d d)
    test-cell 7:u32 (vec3 d d d)

    # commit
    if (written != 0:u32)
        let id = (atomicAdd buf-cells-out-info.count (deref written))
        for i in (range (deref written))
            buf-cells-out.entries @ (id + i) = (cells @ i)

fn simplify-cell (key)
    let level = (((deref u-level) as u32) - 1)
    let r = (/ (f32 (1:u32 << level)))
    let d = (2.0 * r)

    ucoord := (unpack-morton3x10 key)
    coord := (vec3 ucoord) * d - 1.0 + r

    let L =
        static-if USE_TRILINEAR_FILTER r
        elseif USE_WEIGHT_BLUR d
        else r

    local mind = inf
    local maxd = -inf

    inline check-sample (s)
        mind = (min mind s)
        maxd = (max maxd s)
        s

    inline cube-empty? ()
        (mind >= 0.0) | (maxd < 0.0)

    inline get (x y z)
        ofs := ((vec3 x y z) * L)
        (mapf (coord + ofs)) / (d * PACKING_Q)

    inline get8 (x y z)
        ofs := ((vec3 x y z) * r)
        (mapf (coord + ofs)) / (d * PACKING_Q)

    inline alloc-data ()
        let id = (atomicAdd buf-cells-out-info.count 1:u32)
        buf-vertex-attr1-out.entries @ id

    static-if USE_TRILINEAR_FILTER
        inline get8 (x y z)
            check-sample (get8 x y z)

        let
            p000 = (get8 -1 -1 -1)
            p001 = (get8  1 -1 -1)
            p010 = (get8 -1  1 -1)
            p011 = (get8  1  1 -1)
            p100 = (get8 -1 -1  1)
            p101 = (get8  1 -1  1)
            p110 = (get8 -1  1  1)
            p111 = (get8  1  1  1)

        if (cube-empty?)
            return;
        let data = (alloc-data)

        static-if USE_PACKING
            data.v0 =
                uvec4
                    key
                    packSnorm4x8
                        vec4
                            p000
                            p001
                            p010
                            p011
                    packSnorm4x8
                        vec4
                            p100
                            p101
                            p110
                            p111
                    0
        else
            data.key = key
            data.v @ 0 = p000
            data.v @ 1 = p001
            data.v @ 2 = p010
            data.v @ 3 = p011
            data.v @ 4 = p100
            data.v @ 5 = p101
            data.v @ 6 = p110
            data.v @ 7 = p111

    else
        let
            p000 = (get -1 -1 -1)
            p001 = (get  0 -1 -1)
            p002 = (get  1 -1 -1)
            p010 = (get -1  0 -1)
            p011 = (get  0  0 -1)
            p012 = (get  1  0 -1)
            p020 = (get -1  1 -1)
            p021 = (get  0  1 -1)
            p022 = (get  1  1 -1)

            p100 = (get -1 -1  0)
            p101 = (get  0 -1  0)
            p102 = (get  1 -1  0)
            p110 = (get -1  0  0)
            p111 = (get  0  0  0)
            p112 = (get  1  0  0)
            p120 = (get -1  1  0)
            p121 = (get  0  1  0)
            p122 = (get  1  1  0)

            p200 = (get -1 -1  1)
            p201 = (get  0 -1  1)
            p202 = (get  1 -1  1)
            p210 = (get -1  0  1)
            p211 = (get  0  0  1)
            p212 = (get  1  0  1)
            p220 = (get -1  1  1)
            p221 = (get  0  1  1)
            p222 = (get  1  1  1)

        let samples... =
            static-if USE_WEIGHT_BLUR
                inline average (...)
                    let count = (va-countof ...)
                    let s =
                        static-if (count == 1) ...
                        else
                            * (/ (count as f32))
                                + ...
                    check-sample s

                _
                    average p000 p001 p010 p011 p100 p101 p110 p111
                    average p001 p011 p101 p111
                    average p001 p002 p011 p012 p101 p102 p111 p112
                    average p010 p011 p110 p111
                    average p011 p111
                    average p011 p012 p111 p112
                    average p010 p011 p020 p021 p110 p111 p120 p121
                    average p011 p021 p111 p121
                    average p011 p012 p021 p022 p111 p112 p121 p122
                    average p100 p101 p110 p111
                    average p101 p111
                    average p101 p102 p111 p112
                    average p110 p111
                    average p111
                    average p111 p112
                    average p110 p111 p120 p121
                    average p111 p121
                    average p111 p112 p121 p122
                    average p100 p101 p110 p111 p200 p201 p210 p211
                    average p101 p111 p201 p211
                    average p101 p102 p111 p112 p201 p202 p211 p212
                    average p110 p111 p210 p211
                    average p111 p211
                    average p111 p112 p211 p212
                    average p110 p111 p120 p121 p210 p211 p220 p221
                    average p111 p121 p211 p221
                    average p111 p112 p121 p122 p211 p212 p221 p222
            else
                _
                    check-sample p000
                    check-sample p001
                    check-sample p002
                    check-sample p010
                    check-sample p011
                    check-sample p012
                    check-sample p020
                    check-sample p021
                    check-sample p022

                    check-sample p100
                    check-sample p101
                    check-sample p102
                    check-sample p110
                    check-sample p111
                    check-sample p112
                    check-sample p120
                    check-sample p121
                    check-sample p122

                    check-sample p200
                    check-sample p201
                    check-sample p202
                    check-sample p210
                    check-sample p211
                    check-sample p212
                    check-sample p220
                    check-sample p221
                    check-sample p222

        if (cube-empty?)
            return;
        let data = (alloc-data)

        static-if USE_PACKING
            let s0 s1 s2 s3 s4 s5 s6 s7 s8 \
                s9 s10 s11 s12 s13 s14 s15 s16 s17 \
                s18 s19 s20 s21 s22 s23 s24 s25 s26 = samples...
            data.v0 =
                uvec4
                    key
                    packSnorm4x8
                        vec4 s0 s1 s2 s3
                    packSnorm4x8
                        vec4 s4 s5 s6 s7
                    packSnorm4x8
                        vec4 s8 s9 s10 s11
            data.v1 =
                uvec4
                    packSnorm4x8
                        vec4 s12 s13 s14 s15
                    packSnorm4x8
                        vec4 s16 s17 s18 s19
                    packSnorm4x8
                        vec4 s20 s21 s22 s23
                    packSnorm4x8
                        vec4 s24 s25 s26 0

        else
            data.key = key
            va-lifold none
                inline (i key value)
                    data.v @ i = value
                    none
                samples...

    ;

fn voxelize-init ()
    let index = (deref gl_GlobalInvocationID.x)
    subdivide-cell index

fn voxelize ()
    let index = (deref gl_GlobalInvocationID.x)
    if (index < buf-cells-in-info.count)
        subdivide-cell (deref (buf-cells-in.entries @ index))

fn simplify ()
    local_size THREADSIZE 1 1
    let index = (deref gl_GlobalInvocationID.x)
    if (index < buf-cells-in-info.count)
        simplify-cell (deref (buf-cells-in.entries @ index))

fn supershader ()
    local_size THREADSIZE 1 1
    let mode = (deref u-program)
    switch mode
    case ProgramVoxelizeInit
        voxelize-init;
    case ProgramVoxelize
        voxelize;
    #case ProgramSimplify
        simplify;
    default
        ;;

fn calc-projection ()
    let aspect = (vec2 (/ (deref shglobals.aspect)) 1.0)
    'ifp-perspective ProjectionSetup aspect NEAR

inout-geometry geom_data : CubeData
fn rasterize-vert ()
    let index = ((deref gl_VertexID) as u32)
    let data = (deref (buf-vertex-attr1-in.entries @ index))
    geom_data.out = data
    ;

inline transform-dist (p)
    (mat3 shglobals.view) * p

inline transform-invert-dist (p)
    (mat3 shglobals.view-inverse) * p

inline transform-pos (p)
    v := (deref shglobals.view) * (vec4 p 1)
    v.xyz

inline unpack-cube-data8 (v0 v1)
    local W :
        array (array vec2 2) 2

    static-if USE_PACKING
        static-if USE_TRILINEAR_FILTER
            tmp := (unpackSnorm4x8 (deref v0.y))
            W @ 0 @ 0 @ 0 = tmp.x
            W @ 0 @ 0 @ 1 = tmp.y
            W @ 0 @ 1 @ 0 = tmp.z
            W @ 0 @ 1 @ 1 = tmp.w
            tmp := (unpackSnorm4x8 (deref v0.z))
            W @ 1 @ 0 @ 0 = tmp.x
            W @ 1 @ 0 @ 1 = tmp.y
            W @ 1 @ 1 @ 0 = tmp.z
            W @ 1 @ 1 @ 1 = tmp.w
        else
            tmp := (unpackSnorm4x8 (deref v0.y))
            W @ 0 @ 0 @ 0 = tmp.x
            W @ 0 @ 0 @ 1 = tmp.z
            tmp := (unpackSnorm4x8 (deref v0.z))
            W @ 0 @ 1 @ 0 = tmp.z
            tmp := (unpackSnorm4x8 (deref v0.w))
            W @ 0 @ 1 @ 1 = tmp.x
            tmp := (unpackSnorm4x8 (deref v1.y))
            W @ 1 @ 0 @ 0 = tmp.z
            tmp := (unpackSnorm4x8 (deref v1.z))
            W @ 1 @ 0 @ 1 = tmp.x
            tmp := (unpackSnorm4x8 (deref v1.w))
            W @ 1 @ 1 @ 0 = tmp.x
            W @ 1 @ 1 @ 1 = tmp.z
    else
        let v = v0
        static-if USE_TRILINEAR_FILTER
            W @ 0 @ 0 @ 0 = v @ 0
            W @ 0 @ 0 @ 1 = v @ 1
            W @ 0 @ 1 @ 0 = v @ 2
            W @ 0 @ 1 @ 1 = v @ 3
            W @ 1 @ 0 @ 0 = v @ 4
            W @ 1 @ 0 @ 1 = v @ 5
            W @ 1 @ 1 @ 0 = v @ 6
            W @ 1 @ 1 @ 1 = v @ 7
        else
            W @ 0 @ 0 @ 0 = v @ 0
            W @ 0 @ 0 @ 1 = v @ 2
            W @ 0 @ 1 @ 0 = v @ 6
            W @ 0 @ 1 @ 1 = v @ 8
            W @ 1 @ 0 @ 0 = v @ 18
            W @ 1 @ 0 @ 1 = v @ 20
            W @ 1 @ 1 @ 0 = v @ 24
            W @ 1 @ 1 @ 1 = v @ 26
    W

inline unpack-cube-data (v0 v1)
    local W :
        static-if USE_TRILINEAR_FILTER
            array (array vec2 2) 2
        else
            array (array vec3 3) 3

    static-if USE_PACKING
        static-if USE_TRILINEAR_FILTER
            tmp := (unpackSnorm4x8 (deref v0.y))
            W @ 0 @ 0 @ 0 = tmp.x
            W @ 0 @ 0 @ 1 = tmp.y
            W @ 0 @ 1 @ 0 = tmp.z
            W @ 0 @ 1 @ 1 = tmp.w
            tmp := (unpackSnorm4x8 (deref v0.z))
            W @ 1 @ 0 @ 0 = tmp.x
            W @ 1 @ 0 @ 1 = tmp.y
            W @ 1 @ 1 @ 0 = tmp.z
            W @ 1 @ 1 @ 1 = tmp.w
        else
            tmp := (unpackSnorm4x8 (deref v0.y))
            W @ 0 @ 0 @ 0 = tmp.x
            W @ 0 @ 0 @ 1 = tmp.y
            W @ 0 @ 0 @ 2 = tmp.z
            W @ 0 @ 1 @ 0 = tmp.w
            tmp := (unpackSnorm4x8 (deref v0.z))
            W @ 0 @ 1 @ 1 = tmp.x
            W @ 0 @ 1 @ 2 = tmp.y
            W @ 0 @ 2 @ 0 = tmp.z
            W @ 0 @ 2 @ 1 = tmp.w
            tmp := (unpackSnorm4x8 (deref v0.w))
            W @ 0 @ 2 @ 2 = tmp.x
            W @ 1 @ 0 @ 0 = tmp.y
            W @ 1 @ 0 @ 1 = tmp.z
            W @ 1 @ 0 @ 2 = tmp.w
            tmp := (unpackSnorm4x8 (deref v1.x))
            W @ 1 @ 1 @ 0 = tmp.x
            W @ 1 @ 1 @ 1 = tmp.y
            W @ 1 @ 1 @ 2 = tmp.z
            W @ 1 @ 2 @ 0 = tmp.w
            tmp := (unpackSnorm4x8 (deref v1.y))
            W @ 1 @ 2 @ 1 = tmp.x
            W @ 1 @ 2 @ 2 = tmp.y
            W @ 2 @ 0 @ 0 = tmp.z
            W @ 2 @ 0 @ 1 = tmp.w
            tmp := (unpackSnorm4x8 (deref v1.z))
            W @ 2 @ 0 @ 2 = tmp.x
            W @ 2 @ 1 @ 0 = tmp.y
            W @ 2 @ 1 @ 1 = tmp.z
            W @ 2 @ 1 @ 2 = tmp.w
            tmp := (unpackSnorm4x8 (deref v1.w))
            W @ 2 @ 2 @ 0 = tmp.x
            W @ 2 @ 2 @ 1 = tmp.y
            W @ 2 @ 2 @ 2 = tmp.z
    else
        let v = v0
        static-if USE_TRILINEAR_FILTER
            W @ 0 @ 0 @ 0 = v @ 0
            W @ 0 @ 0 @ 1 = v @ 1
            W @ 0 @ 1 @ 0 = v @ 2
            W @ 0 @ 1 @ 1 = v @ 3
            W @ 1 @ 0 @ 0 = v @ 4
            W @ 1 @ 0 @ 1 = v @ 5
            W @ 1 @ 1 @ 0 = v @ 6
            W @ 1 @ 1 @ 1 = v @ 7
        else
            W @ 0 @ 0 @ 0 = v @ 0
            W @ 0 @ 0 @ 1 = v @ 1
            W @ 0 @ 0 @ 2 = v @ 2
            W @ 0 @ 1 @ 0 = v @ 3
            W @ 0 @ 1 @ 1 = v @ 4
            W @ 0 @ 1 @ 2 = v @ 5
            W @ 0 @ 2 @ 0 = v @ 6
            W @ 0 @ 2 @ 1 = v @ 7
            W @ 0 @ 2 @ 2 = v @ 8
            W @ 1 @ 0 @ 0 = v @ 9
            W @ 1 @ 0 @ 1 = v @ 10
            W @ 1 @ 0 @ 2 = v @ 11
            W @ 1 @ 1 @ 0 = v @ 12
            W @ 1 @ 1 @ 1 = v @ 13
            W @ 1 @ 1 @ 2 = v @ 14
            W @ 1 @ 2 @ 0 = v @ 15
            W @ 1 @ 2 @ 1 = v @ 16
            W @ 1 @ 2 @ 2 = v @ 17
            W @ 2 @ 0 @ 0 = v @ 18
            W @ 2 @ 0 @ 1 = v @ 19
            W @ 2 @ 0 @ 2 = v @ 20
            W @ 2 @ 1 @ 0 = v @ 21
            W @ 2 @ 1 @ 1 = v @ 22
            W @ 2 @ 1 @ 2 = v @ 23
            W @ 2 @ 2 @ 0 = v @ 24
            W @ 2 @ 2 @ 1 = v @ 25
            W @ 2 @ 2 @ 2 = v @ 26
    W

fn _interpolate-biquad (W o)
    fn b2 (x)
        rx := (1.0 - x)
        vec3
            rx * rx
            2.0 * rx * x
            x * x
    #fn b2 (x)
        vec3
            max 0.0 (1.0 - 2.0 * x)
            1.0 - (abs (1.0 - 2.0 * x))
            max 0.0 (2.0 * x - 1.0)
    # don't clamp to allow normal sampling beyond range
    #o := (clamp o (vec3 0.0) (vec3 1.0))

    u := (b2 o.x)
    v := (b2 o.y)
    w := (b2 o.z)

    x0 :=
        vec3
            dot (W @ 0 @ 0) u
            dot (W @ 0 @ 1) u
            dot (W @ 0 @ 2) u
    x1 :=
        vec3
            dot (W @ 1 @ 0) u
            dot (W @ 1 @ 1) u
            dot (W @ 1 @ 2) u
    x2 :=
        vec3
            dot (W @ 2 @ 0) u
            dot (W @ 2 @ 1) u
            dot (W @ 2 @ 2) u

    y :=
        vec3
            dot x0 v
            dot x1 v
            dot x2 v

    dot y w

fn _interpolate-biquad-normal (W o)
    fn b2 (x)
        rx := (1.0 - x)
        vec3
            rx * rx
            2.0 * rx * x
            x * x

    fn b2d (x)
        2x := 2.0 * x
        vec3
            2x - 2.0
            2.0 - 4.0 * x
            2x

    u2 := (b2 o.x)
    v2 := (b2 o.y)
    w2 := (b2 o.z)

    u1 := (b2d o.x)
    v1 := (b2d o.y)
    w1 := (b2d o.z)

    inline compute-field (u v w)
        x0 :=
            vec3
                dot (W @ 0 @ 0) u
                dot (W @ 0 @ 1) u
                dot (W @ 0 @ 2) u
        x1 :=
            vec3
                dot (W @ 1 @ 0) u
                dot (W @ 1 @ 1) u
                dot (W @ 1 @ 2) u
        x2 :=
            vec3
                dot (W @ 2 @ 0) u
                dot (W @ 2 @ 1) u
                dot (W @ 2 @ 2) u

        y :=
            vec3
                dot x0 v
                dot x1 v
                dot x2 v

        - (dot y w)

    vec3
        compute-field u1 v2 w2
        compute-field u2 v1 w2
        compute-field u2 v2 w1

inout depthval : f32
inout ray_origin : vec3
inout ray_dir : vec3
inout debug_color : vec3
inout cube_data_key : u32 flat
inout cube_data_v : vec4 flat
inout cube_data_v0 : uvec4 flat
inout cube_data_v1 : uvec4 flat
fn rasterize-geom ()
    input_primitive points
    output_primitive triangle_strip 14

    let data = (geom_data.in @ 0)

    let key =
        static-if USE_PACKING (deref data.v0.x)
        else (deref data.key)

    let level = (((deref u-level) as u32) - 1)
    let r = (/ (f32 (1:u32 << level)))
    let d = (2.0 * r)
    ucoord := (unpack-morton3x10 key)
    coord := (vec3 ucoord) * d - 1.0

    static-if USE_BACKFACE_CULLING
        let W =
            unpack-cube-data data.v0 data.v1

        local dist = -inf
        inline check-sample-normal (X Y Z)
            o := (vec3 X Y Z) * 0.5
            p := coord + o * d
            p := (transform-pos p)
            n := (_interpolate-biquad-normal W o)
            n := (transform-dist n)
            dist = (max dist (dot p n))
            ;

        #do
            check-sample-normal 0 1 1
            check-sample-normal 2 1 1
            check-sample-normal 1 0 1
            check-sample-normal 1 2 1
            check-sample-normal 1 1 0
            check-sample-normal 1 1 2

        #check-sample-normal 1 1 1

        #do
            check-sample-normal 0 0 0
            check-sample-normal 2 2 0
            check-sample-normal 2 0 2
            check-sample-normal 0 2 2

        va-map
            inline (Z)
                va-map
                    inline (Y)
                        va-map
                            inline (X)
                                check-sample-normal X Y Z
                                ;
                            \ 0 2
                    \ 0 2
            \ 0 2

        if (dist < 0.0)
            return;

        #debug_color.out = (normhue dist)

    static-if USE_PACKING
        cube_data_v0.out = data.v0
        static-if USE_TRILINEAR_FILTER
        else
            cube_data_v1.out = data.v1
    else
        cube_data_key.out = data.key
        cube_data_v.out = data.v

    inline write-vertex (i)
        let p = (cube-vertex i)
        p := (vec3 p.x p.y (1 - p.z))
        ray_origin.out = p

        coord := coord + (vec3 p) * d
        coord := (transform-pos coord)

        ray_dir.out =
            transform-invert-dist coord

        let proj =
            calc-projection;

        let pcoord =
            'project proj
                vec4 coord 1.0

        depthval.out = coord.z
        gl_Position = pcoord
        EmitVertex;

    for i in (range 14)
        write-vertex i
    EndPrimitive;
    ;

fn rasterize-frag ()
    fragment_depth depth_less
    let level = (((deref u-level) as u32) - 1)
    let r = (/ (f32 (1:u32 << level)))
    let d = (2.0 * r)
    let key =
        static-if USE_PACKING (deref cube_data_v0.in.x)
        else (deref cube_data_key.in)
    ucoord := (unpack-morton3x10 key)
    coord := (vec3 ucoord) * d - 1.0

    ro := (deref ray_origin.in)
    rd := (normalize (deref ray_dir.in))

    far := (iCubeFar (ro - 0.5) rd)

    let W =
        unpack-cube-data cube_data_v0.in cube_data_v1.in

    fn _interpolate-bilin (W o)
        fn _weight1 (p)
            vec2 (p @ 0) (p @ 1)
        inline weight (p)
            _weight1 (deref p)

        fn b1 (x)
            rx := (1.0 - x)
            vec2 rx x
        # don't clamp to allow normal sampling beyond range
        #o := (clamp o (vec3 0.0) (vec3 1.0))

        u := (b1 o.x)
        v := (b1 o.y)
        w := (b1 o.z)

        x0 :=
            vec2
                dot (weight (W @ 0 @ 0)) u
                dot (weight (W @ 0 @ 1)) u
        x1 :=
            vec2
                dot (weight (W @ 1 @ 0)) u
                dot (weight (W @ 1 @ 1)) u

        y :=
            vec2
                dot (weight x0) v
                dot (weight x1) v

        dot (weight y) w

    fn _interpolate-bilin-normal (W o)
        fn _weight1 (p)
            vec2 (p @ 0) (p @ 1)
        inline weight (p)
            _weight1 (deref p)

        fn b1 (x)
            rx := (1.0 - x)
            vec2 rx x

        fn b1d (x)
            vec2 -1.0 1.0

        # don't clamp to allow normal sampling beyond range
        #o := (clamp o (vec3 0.0) (vec3 1.0))

        u1 := (b1 o.x)
        v1 := (b1 o.y)
        w1 := (b1 o.z)

        u0 := (b1d o.x)
        v0 := (b1d o.y)
        w0 := (b1d o.z)

        inline compute-field (u v w)
            x0 :=
                vec2
                    dot (weight (W @ 0 @ 0)) u
                    dot (weight (W @ 0 @ 1)) u
            x1 :=
                vec2
                    dot (weight (W @ 1 @ 0)) u
                    dot (weight (W @ 1 @ 1)) u

            y :=
                vec2
                    dot (weight x0) v
                    dot (weight x1) v

            - (dot (weight y) w)

        vec3
            compute-field u0 v1 w1
            compute-field u1 v0 w1
            compute-field u1 v1 w0

    inline interpolate (o)
        #o := (clamp o (vec3 0) (vec3 1))
        static-if USE_TRILINEAR_FILTER
            _interpolate-bilin (deref W) o
        else
            _interpolate-biquad (deref W) o

    inline interpolate-normal (o)
        static-if USE_TRILINEAR_FILTER
            _interpolate-bilin-normal (deref W) o
        else
            _interpolate-biquad-normal (deref W) o

        # reconstruct 1D quadratic coefficients from three samples

    # option A: sample from enter, middle and exit

    embed
        c0 := (interpolate ro)
        #if (c0 < 0.0)
            discard;

        c1 := (interpolate (ro + rd * (far * 0.5)))
        c2 := (interpolate (ro + rd * far))

        2A := 4.0 * (c2 + c0 - 2.0 * c1)
        NB := -4.0 * c1 + 3.0 * c0 + c2
        2C := 2.0 * c0

        let k =
            do
                # from
                    Numerically Stable Method for Solving Quadratic Equations
                    https://people.csail.mit.edu/bkph/articles/Quadratics.pdf
                k := NB * NB - 2A * 2C
                if (k < 0.0)
                    discard;
                k := (sqrt k)
                let pred = (NB > 0.0)
                let q0 q1 =
                    /
                        ? pred 2C (NB - k)
                        ? pred (NB + k) 2A
                    /
                        ? pred (NB + k) 2C
                        ? pred 2A (NB - k)
                ? (q0 < 0.0) q1 q0

                #q1 := (? (q1 > q0) q1 inf)

        if ((k < -0.01) | (k > 1.01))
            discard;
        t := k * far

        p := ro + rd * t
        #n :=
            - (sdNormalFast interpolate p 0.01)

        coord := coord + p * d
        n :=
            #normalmapf coord (r / 4.0)
            #- (sdNormalFast interpolate p 0.01)
            interpolate-normal p
        let dist = (matmapf coord)
        let material =
            dist.material

        p := (transform-pos coord)
        #n := (transform-dist n)

        gl_FragDepth = (NEAR / p.z)

        #if (p.z < 1.0)
            discard;

    #embed
        h0 := (ro * -2.0 - 1.0)
        n := #(vec3 0)
            - ((sign rd) * (step h0.yzx h0) * (step h0.zxy h0))
        p := (vec3 0 0 (/ (deref gl_FragCoord.z)))

        #if ((/ gl_FragCoord.z) < 1.8)
            discard;

    let data =
        GBuffer.Data
            normal = n
            color =
                do
                    #vec4
                        rd * 0.5 + 0.5
                        1
                    material.albedo.rgb
                    #vec4 (rd * 0.5 + 0.5) 1
                    #vec4 ((deref p) * 0.5 + 0.5) 1
                    #vec4 (deref debug_color.in) 1
                    #vec4 (normhue (count / RAYMARCH_STEPS)) 1
                    #vec4 (normhue (t / (max 0.01 far))) 1
                    #vec4 1
            depth = p.z
            roughness = material.roughness
            metallic = material.metallic

    GBuffer.fragment = ('pack data)

#uniform img-target-rgba32f : (image2D rgba32f)
    binding = IMAGE_TARGET_RGBA32F
    \ coherent writeonly restrict

uniform u-level : i32
    location = UNIFORM_LEVEL

fn mixdown (uv)
    #let t = (deref shglobals.time)
    let size =
        vec2 (deref shglobals.size)
    let uv2 =
        (uv * 2.0 - 1.0) * (vec2 shglobals.aspect 1)
    let uv = (ivec2 ((deref gl_FragCoord) . xy + 0.5))

    let col =
        texelFetch smp-screen uv 0
    let data = (GBuffer.Data.unpack col)
    let depth = data.depth
    if (depth == 0.0)
        return
            vec4 0.1 0.1 0.2 1
    let proj =
        calc-projection;
    let coord =
        vec3 ((uv2 * 2.0 - 1.0) * depth / proj.aspect) depth
    let rd = (normalize coord)

    let l =
        normalize
            vec3 0.5 -1.0 0.25
    normal := data.normal
    color := data.color
    let lambert =
        max 0.0 (dot normal l)

    let albedo = color.rgb
    let ambient =
        ((dot normal (vec3 0.0 -1.0 0.0)) * 0.5 + 0.5) * 0.1
    let exposure = 2.0

    glow :=
        max 0.0 ((max color.r color.g color.b) - 1.0)

    roughness := data.roughness
    metallic := data.metallic
    let color =
        linear->sRGB
            tonemap
                * exposure
                    mix
                        +
                            ambient * albedo * (1.0 - metallic)
                            * lambert
                                BRDF albedo
                                    roughness
                                    metallic
                                    \ l rd normal
                        albedo
                        glow

    return
        vec4 color 1.0
        #vec4
            #\ uv 0.0
            #normal * 0.5 + 0.5
            vec3
                (dot normal (normalize (vec3 0 -1 0))) * 0.5 + 0.5
            #normhue depth
            #normhue (radius / 16.0)
            #normhue (w / 8.0)
            1.0

fn visualize-buffer (uv)
    #let t = (deref shglobals.time)
    let size =
        vec2 (deref shglobals.size)
    let uv2 =
        (uv * 2.0 - 1.0) * (vec2 shglobals.aspect 1)
    let uv = (ivec2 ((deref gl_FragCoord) . xy + 0.5))

    let col =
        texelFetch smp-screen uv 0
    let data = (GBuffer.Data.unpack col)
    if (data.depth == 0.0)
        return
            vec4 0 0 0 1

    return
        vec4
            data.normal * -0.5 + 0.5
            #normhue ((depth * 20.0) % 1.0)
            #normhue (radius / 16.0)
            #color.xyz
            #*
                0.5 + 0.5 * ((depth * 100.0) % 1.0)
                color.xyz
            1.0

fn shader (uv)
    mixdown uv
    #visualize-buffer uv

inline main ()

    # pass overview:
        in: compute 4 * 4 * 4
        per thread:
            traverse 1/64th of octree
        for each leaf found:
            increase atomic counter in indirect draw call argument to obtain index
            append position + normal as vec4 (xyz, packed normal)
            append color as i32
                gamma compressed RGB10 values
                values >= 256 are emissive
                2 bits left for ?
            append material props as i32
                * roughness
                * metallic
                * film thickness
                * waxiness?
                * hue rotation?
                * UV activeness?
                * snowy glitter?

        in: indirect draw call argument
        out: rasterized voxel cubes

    let NUM_CMD_BUFFERS = 1

    global compute_cmd_buffers = (GL.CreateBuffer)
    setup-ssbo compute_cmd_buffers buf-compute-cmd NUM_CMD_BUFFERS

    global draw_cmd_buffers = (GL.CreateBuffer)
    setup-ssbo draw_cmd_buffers buf-draw-cmd NUM_CMD_BUFFERS

    let NUM_BUFFERS = 2

    global cell_info_buffers =
        arrayof GL.uint
            GL.CreateBuffer;
            GL.CreateBuffer;

    global cell_buffers =
        arrayof GL.uint
            GL.CreateBuffer;
            GL.CreateBuffer;
    let cell_info_buffer_sz = (sizeof u32)
    let cell_buffer_sz = ((sizeof u32) * MAX_VOXELS)
    for i in (range (NUM_BUFFERS as u32))
        let buf = (cell_info_buffers @ i)
        GL.NamedBufferData buf (i32 cell_info_buffer_sz) null GL.STREAM_COPY
        GL.BindBufferRange GL.SHADER_STORAGE_BUFFER (BINDING_BUF_CELLS_IN_INFO + i)
            \ buf 0:i64 (i64 cell_info_buffer_sz)
        let buf = (cell_buffers @ i)
        GL.NamedBufferData buf (i32 cell_buffer_sz) null GL.STREAM_COPY
        GL.BindBufferRange GL.SHADER_STORAGE_BUFFER (BINDING_BUF_CELLS_IN + i)
            \ buf 0:i64 (i64 cell_buffer_sz)

    global vertex_buffer = (GL.CreateBuffer)
    let vertex_buffer_sz = ((sizeof CubeData) * MAX_VOXELS)
    GL.NamedBufferData vertex_buffer (i32 vertex_buffer_sz) null GL.STREAM_COPY

    global gbuffer = (GBuffer)

    global vao-empty = (GL.CreateVertexArray)

    global pg-rasterize = (GL.CreateProgram)
    call
        attach-shaders (deref pg-rasterize)
            vertex = rasterize-vert
            geometry = rasterize-geom
            fragment = rasterize-frag
            #debug = true

    global pg-supershader = (GL.CreateProgram)
    call
        attach-shaders (deref pg-supershader)
            compute = supershader

    global pg-simplify = (GL.CreateProgram)
    call
        attach-shaders (deref pg-simplify)
            compute = simplify
            #debug = true

    global pg-setup-compute = (GL.CreateProgram)
    call
        attach-shaders (deref pg-setup-compute)
            compute = setup-compute-command
            #debug = true

    global pg-setup-draw = (GL.CreateProgram)
    call
        attach-shaders (deref pg-setup-draw)
            compute = setup-draw-arrays-command
            #debug = true

    @@ 'on GUI.on-frame
    inline (time size)
        Button "Test"
        

    inline per-frame-setup (size pg-test frame)

        'bind-for-fetch gbuffer TU_GBUFFER

        inline bind-read-buffer (i)
            GL.BindBufferRange GL.SHADER_STORAGE_BUFFER
                BINDING_BUF_CELLS_IN
                cell_buffers @ i
                \ 0:i64 (i64 cell_buffer_sz)
            GL.BindBufferRange GL.SHADER_STORAGE_BUFFER
                BINDING_BUF_CELLS_IN_INFO
                cell_info_buffers @ i
                \ 0:i64 (i64 cell_info_buffer_sz)

        inline clear-buffer-count (i)
            let buf = (cell_info_buffers @ i)
            let ptr =
                GL.MapNamedBufferRange buf 0 (sizeof u32)
                    | GL.MAP_WRITE_BIT
                        GL.MAP_INVALIDATE_BUFFER_BIT
                        #GL.MAP_UNSYNCHRONIZED_BIT
            let ptr = (bitcast ptr (mutable pointer CellValsInfo))
            ptr.count = 0:u32
            GL.UnmapNamedBuffer buf

        inline bind-write-buffer (i)
            clear-buffer-count i
            GL.BindBufferRange GL.SHADER_STORAGE_BUFFER
                BINDING_BUF_CELLS_OUT
                cell_buffers @ i
                \ 0:i64 (i64 cell_buffer_sz)
            GL.BindBufferRange GL.SHADER_STORAGE_BUFFER
                BINDING_BUF_CELLS_OUT_INFO
                cell_info_buffers @ i
                \ 0:i64 (i64 cell_info_buffer_sz)

        GL.UseProgram pg-supershader
        GL.BindBuffer GL.DISPATCH_INDIRECT_BUFFER compute_cmd_buffers

        local idx = 0
        local level = 0
        inline print-in-count (i)
            let buf = (cell_info_buffers @ i)
            let ptr =
                GL.MapNamedBufferRange buf 0 (sizeof u32)
                    GL.MAP_READ_BIT
            let ptr = (bitcast ptr (pointer CellValsInfo))
            print ptr.count
            GL.UnmapNamedBuffer buf

        inline init-voxels (l)
            level = l
            let i0 = (deref idx)
            idx = ((idx + 1) & 1)
            let i1 = (deref idx)
            let level = (deref level)
            GL.Uniform u-program ProgramVoxelizeInit
            GL.Uniform u-level level
            bind-read-buffer i0
            bind-write-buffer i1
            GL.DispatchCompute ((((1 << level) ** 3) // THREADSIZE) as u32) 1 1
            GL.MemoryBarrier GL.SHADER_STORAGE_BARRIER_BIT

        inline refine-voxels ()
            let i0 = (deref idx)
            idx = ((idx + 1) & 1)
            let i1 = (deref idx)
            level = level + 1
            let level = (deref level)
            GL.UseProgram pg-setup-compute
            GL.Uniform u-divisor THREADSIZE
            bind-read-buffer i0
            bind-ssbo compute_cmd_buffers buf-compute-cmd none 0
            GL.DispatchCompute 1 1 1
            GL.MemoryBarrier GL.COMMAND_BARRIER_BIT

            GL.UseProgram pg-supershader
            GL.Uniform u-program ProgramVoxelize
            GL.Uniform u-level level
            bind-read-buffer i0
            bind-write-buffer i1
            GL.DispatchComputeIndirect 0
            GL.MemoryBarrier GL.SHADER_STORAGE_BARRIER_BIT

        inline finalize-voxels ()
            let i0 = (deref idx)
            idx = ((idx + 1) & 1)
            let i1 = (deref idx)
            level = level + 1
            let level = (deref level)
            GL.UseProgram pg-setup-compute
            GL.Uniform u-divisor THREADSIZE
            #GL.Uniform u-divisor THREADSIZE
            bind-read-buffer i0
            bind-ssbo compute_cmd_buffers buf-compute-cmd none 0
            GL.DispatchCompute 1 1 1
            GL.MemoryBarrier GL.COMMAND_BARRIER_BIT

            GL.UseProgram pg-simplify
            #GL.Uniform u-program ProgramSimplify
            GL.Uniform u-level level
            bind-read-buffer i0
            bind-write-buffer i1
            GL.BindBufferRange GL.SHADER_STORAGE_BUFFER BINDING_BUF_VERTEX_ATTR1_OUT
                    \ vertex_buffer 0:i64 (i64 vertex_buffer_sz)
            GL.DispatchComputeIndirect 0
            GL.MemoryBarrier GL.SHADER_STORAGE_BARRIER_BIT

        inline draw-surface ()
            #print-in-count (deref idx)
            GL.MemoryBarrier (GL.ALL_BARRIER_BITS as u32)
            GL.BindBuffer GL.DRAW_INDIRECT_BUFFER draw_cmd_buffers

            GL.UseProgram pg-setup-draw
            bind-read-buffer (deref idx)
            bind-ssbo draw_cmd_buffers buf-draw-cmd none 0
            GL.DispatchCompute 1 1 1
            GL.MemoryBarrier GL.COMMAND_BARRIER_BIT

            do
                'render-scope gbuffer size
                GL.Enable GL.CULL_FACE
                GL.CullFace GL.BACK

                GL.UseProgram pg-rasterize
                bind-read-buffer (deref idx)
                GL.BindBufferRange GL.SHADER_STORAGE_BUFFER BINDING_BUF_VERTEX_ATTR1_IN
                        \ vertex_buffer 0:i64 (i64 vertex_buffer_sz)
                GL.Uniform u-level (deref level)
                GL.BindVertexArray vao-empty
                GL.DrawArraysIndirect GL.POINTS null

                GL.Disable GL.BLEND
                GL.Disable GL.CULL_FACE
                # auto-cleanup here

        #if true
            # dump to disk
            init-voxels 5
            refine-voxels; # 6
            refine-voxels; # 7
            refine-voxels; # 8
            refine-voxels; # 9
            refine-voxels; # 10
            let buf = (cell_info_buffers @ idx)
            let ptr = (GL.MapNamedBufferRange buf 0 (sizeof u32) GL.MAP_READ_BIT)
            let ptr = (bitcast ptr (pointer CellValsInfo))
            local count = (deref ptr.count)
            GL.UnmapNamedBuffer buf
            print count

            let buf = (cell_buffers @ idx)
            let ptr = (GL.MapNamedBufferRange buf 0 cell_buffer_sz GL.MAP_READ_BIT)
            let ptr = (bitcast ptr (pointer u32))
            let f =
                try (File.open "noisetorus.voxels" "wb")
                except (err)
                    error "couldn't open file"
            'write f ptr ((sizeof u32) * (deref count))
            drop f
            GL.UnmapNamedBuffer buf

            exit 0

        if true #((frame % 240) == 0)
            init-voxels 5
            refine-voxels;
            refine-voxels;
            #refine-voxels;
            finalize-voxels;
        else
            idx = 0
            level = 8
        draw-surface;

    _ per-frame-setup shader

fn program ()
    render-fragment-shader main
        #debug = true
        #size = (ivec2 512)
        size = (ivec2 960 540)


static-if true
    program;
else
    define f
        compile
            typify program
            'O3

    f;