rev: 3827f53bae88e48a594610914ba74b9512ead6dd tukan/testing/test_splat.sc -rw-r--r-- 27.0 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
#
    voxelization + rasterization

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

using import glm
using import glsl
using import Array
using import Box
using import struct
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.normal
using import ..tukan.projection
using import ..tukan.derivative
using import .testfragment

# reserve 10MB for each voxel buffer
    at 4 bytes per voxel
let MAX_VOXELS = ((10 * (1 << 20)) // 4)
let POINTLIMIT = 120000

let BINDING_BUF_CELLS_IN = 1
let BINDING_BUF_CELLS_OUT = 2
let BINDING_BUF_DRAW_VOXELS_CMD = 3
let BINDING_BUF_DISPATCH_CMD = 4

let IMAGE_TARGET_RGBA32F = 1
let IMAGE_TARGET2_RGBA32F = 2

let UNIFORM_LEVEL = 1
let UNIFORM_PROGRAM = 2
let UNIFORM_SCREEN_SAMPLER = 3
let UNIFORM_SCREEN_SAMPLER2 = 4

let ProgramVoxelizeInit = 0
let ProgramVoxelize = 1

let LEVELS = 8

let SEARCH_R = 5
let SEARCH_Rf = (SEARCH_R as f32)

let sqrt3 = (sqrt 3.0)

run-stage;

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

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

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

uniform u-program : i32
    location = UNIFORM_PROGRAM

uniform u-level : i32
    location = UNIFORM_LEVEL

uniform smp-screen : sampler2D
    location = UNIFORM_SCREEN_SAMPLER
uniform smp-screen2 : sampler2D
    location = UNIFORM_SCREEN_SAMPLER2

struct DrawElementsIndirectCommand plain
    count : u32 = 0
    instanceCount : u32 = 0
    firstIndex : u32 = 0
    baseVertex : u32 = 0
    baseInstance : u32 = 0

buffer buf-draw-voxels-cmd : DrawElementsIndirectCommand
    binding = BINDING_BUF_DRAW_VOXELS_CMD

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

    sdSmoothAnd
        sdSmoothOr
            sdTorus p1.xzy (vec2 0.5 0.05)
            sdTorus p2 (vec2 0.4 0.1)
            0.2
        -
            (abs (sdSphere p 0.4)) - 0.03
        0.03

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
        sdBox p1 (vec3 0.33)
        sdBox p2 (vec3 0.33)
        0.2

fn two-boxes-merge (p)
    let d =
        vec3 0.3
    let sz =
        vec3 0.5
    'sdSmoothAnd
        'sdSmoothOr
            sdmDist
                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)
                    0.4
            sdMaterial
                vec4 0.5 0.3 1.0 1.0
        0.1

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

fn matmapf (p)
    p := p.yzx * 2.0
    (two-boxes-merge p) * 0.5

# 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 subdivide-cell (key)
    let level = ((deref u-level) as u32)
    let r = (/ (f32 (1:u32 << level)))
    let d = (2.0 * r)
    let rlimit = (sqrt3 * 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

    local cells : (array u32 8)
    local written = 0:u32
    inline test-cell (i ofs final)
        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-cell2 (i ofs final)
        key := key | i
        pos := coord + ofs
        let dist = (mapf pos)
        let hit = ((abs dist) < rlimit)
        if hit
            n :=
                sdNormalFast mapf coord r
            n := dist * n
            if ((max (abs n.x) (abs n.y) (abs n.z)) < r)
                cells @ (deref written) = key
                written += 1:u32

    #if (level == 8:u32)
        #do
            test-cell2 0:u32 (vec3 0 0 0)
            test-cell2 3:u32 (vec3 d d 0)
            test-cell2 5:u32 (vec3 d 0 d)
            test-cell2 6:u32 (vec3 0 d d)
        do
            test-cell2 0:u32 (vec3 0 0 0)
            test-cell2 1:u32 (vec3 d 0 0)
            test-cell2 2:u32 (vec3 0 d 0)
            test-cell2 3:u32 (vec3 d d 0)
            test-cell2 4:u32 (vec3 0 0 d)
            test-cell2 5:u32 (vec3 d 0 d)
            test-cell2 6:u32 (vec3 0 d d)
            test-cell2 7:u32 (vec3 d d d)
    do  #else
        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.count (deref written))
        for i in (range (deref written))
            buf-cells-out.entries @ (id + i) = (cells @ i)

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.count)
        subdivide-cell (deref (buf-cells-in.entries @ index))

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

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

inout normal : vec3
inout radius : f32
inout depthval : f32
inout albedo : vec4
inout matdata : vec4
fn rasterize-vert ()
    let index = ((deref gl_VertexID) as u32)
    if (index < buf-cells-in.count)
        let key = (deref (buf-cells-in.entries @ index))

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

        # project to surface
        n :=
            sdNormalFast mapf coord r
        let dist = (mapf coord)
        coord := coord - n * dist

        n :=
            sdNormalFast mapf coord r
        n := -n

        let q = (matmapf coord)

        #embed
            let vid = (deref gl_VertexID)
            vx :=
                vec3
                    (0x287a >> vid) & 1
                    (0x02af >> vid) & 1
                    (0x31e3 >> vid) & 1

            coord := coord + vx * d

        # rotate it a little
        embed
            let a = ((deref shglobals.time) * 0.2)
            let c s = (cos a) (sin a)

            n :=
                vec3
                    c * n.x - s * n.z
                    n.y
                    s * n.x + c * n.z

            coord :=
                vec3
                    c * coord.x - s * coord.z
                    coord.y
                    s * coord.x + c * coord.z

        coord :=
            coord + (vec3 0 0 1)

        let dz = (dot n (normalize coord))
        if (dz > 0.0)
            let proj =
                calc-projection;

            let pcoord =
                'project proj
                    vec4 coord 1.0

            let nzz =
                (abs n.z) - 1.0

            #n :=
                normalize
                    vec3
                        n.xy
                        dz

            normal.out = n
            radius.out =
                #3.0
                #max 1.0
                    (r * (sqrt 3.0) * (f32 shglobals.size.y)) / coord.z
                (r * (sqrt 3.0) * (f32 shglobals.size.y)) / coord.z
                #(r * 0.5 * (f32 shglobals.size.y)) / coord.z
            depthval.out = coord.z
            albedo.out = q.material.albedo
            matdata.out =
                vec4
                    q.material.roughness
                    q.material.metallic
                    \ 0 0
            #radius.out = ((r * (f32 shglobals.size.y) * 0.5) / pcoord.w)
            gl_Position = pcoord
            return;

    radius.out = 0.0
    gl_Position = (vec4 0 0 0 inf)
    ;

fn pack-surfel-data (normal radius pos depth color matdata)
    let normal =
        bitcast (packSnorm2x16 (pack_normal_snorm normal)) f32
    let color =
        bitcast (packUnorm4x8 color) f32
    let matdata =
        bitcast (packUnorm4x8 matdata) f32
    _
        vec4 normal matdata 0.0 radius
        vec4 pos color depth

fn unpack-surfel-data (frag1 frag2)
    let normal =
        unpack_normal_snorm (unpackSnorm2x16 (bitcast frag1.x u32))
    let color =
        unpackUnorm4x8 (bitcast frag2.z u32)
    let matdata =
        unpackUnorm4x8 (bitcast frag1.y u32)
    _
        normal
        frag1.w
        frag2.xy
        frag2.w
        color
        matdata

fn unpack-surfel-radius-depth (frag1 frag2)
    _ frag1.w frag2.w

out out_Color : vec4
    binding = 0
out out_Color2 : vec4
    binding = 1
fn rasterize-frag ()
    let v1 v2 =
        pack-surfel-data
            deref normal.in
            deref radius.in
            ((deref gl_FragCoord) . xy) / (vec2 (deref shglobals.size))
            deref depthval.in
            deref albedo.in
            deref matdata.in
    out_Color = v1
    out_Color2 = v2

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

uniform img-target2-rgba32f : (image2D rgba32f)
    binding = IMAGE_TARGET2_RGBA32F
    \ writeonly
    #\ coherent writeonly restrict

uniform u-level : i32
    location = UNIFORM_LEVEL

fn reduce-2x2 ()
    local_size 8 8 1
    let pos = (ivec2 ((deref gl_GlobalInvocationID) . xy))
    let level = (deref u-level)
    let size = (deref shglobals.size)
    radiusf := (/ ((min size.x size.y) as f32))
    let divisor = (1 << level)
    let realsize = ((size + (divisor - 1)) // divisor)
    diff := realsize - pos
    if ((min diff.x diff.y) > 0)
        let level0 = (level - 1)
        let fullsize0 = (2048 >> level0)
        let s = (/ (vec2 fullsize0))
        let o = (pos * 2)

        local candidates : (array vec4 4)
        local candidates2 : (array vec4 4)
        local count = 0
        local best_depth = inf
        local radii = 0.0

        inline read-offset (v)
            uv := o + v
            src1 := (texelFetch smp-screen uv level0)
            src2 := (texelFetch smp-screen2 uv level0)
            let radius depth = (unpack-surfel-radius-depth src1 src2)
            if (radius != 0.0)
                let target-level =
                    ((ceil (log2 (radius / SEARCH_Rf))) + 0.5) as i32
                if (level0 <= target-level)
                    candidates @ count = src1
                    candidates2 @ count = src2
                    count += 1
                    radii += radius
                    best_depth = (min (deref best_depth) depth)

        read-offset (ivec2 0 0)
        read-offset (ivec2 1 0)
        read-offset (ivec2 0 1)
        read-offset (ivec2 1 1)

        local w = 0.0
        local sum_normal = (vec3 0.0)
        local sum_pos = (vec2 0.0)
        local sum_depth = 0.0
        local sum_radius = 0.0
        local sum_color = (vec4 0.0)
        local sum_matdata = (vec4 0.0)

        let max_depth = (best_depth + radii * radiusf)

        for i in (range (deref count))
            let src1 = (deref (candidates @ i))
            let src2 = (deref (candidates2 @ i))
            let normal radius pos depth color matdata = (unpack-surfel-data src1 src2)
            if (depth < max_depth)
                w += 1.0
                sum_normal += normal
                sum_pos += pos
                sum_depth += depth
                sum_radius += radius
                sum_color += color
                sum_matdata += matdata
                break;

        let data1 data2 =
            if (w == 0.0)
                _
                    vec4 0.0
                    vec4 0.0
            else
                let inv_w = (/ w)
                let normal radius uv depth color matdata =
                    normalize
                        sum_normal * inv_w
                    sum_radius * inv_w
                    sum_pos * inv_w
                    sum_depth * inv_w
                    sum_color * inv_w
                    sum_matdata * inv_w
                pack-surfel-data normal radius uv depth color matdata

        imageStore img-target-rgba32f pos data1
        imageStore img-target2-rgba32f pos data2
    ;

fn reconstruct-surface (uv)
    #let t = (deref shglobals.time)
    let size =
        vec2 (deref shglobals.size)
    radiusf := (/ (min size.x size.y))
    let uv2 = uv
    let uv = (ivec2 ((deref gl_FragCoord) . xy + 0.5))

    #let proj =
        calc-projection;

    #let pcoord =
        'project proj
            vec4 (vec3 coord) 1.0

    # find closest depth

    local best_depth = inf
    local radii = 0.0

    local w = 0.0
    local sum_normal = (vec3 0.0)
    local sum_pos = (vec2 0.0)
    local sum_depth = 0.0
    local sum_radius = 0.0
    local sum_color = (vec4 0.0)
    local sum_matdata = (vec4 0.0)

    let U0 = (SEARCH_R // -2)
    let U1 = (U0 + SEARCH_R)
    let urange = (range U0 U1)

    let MAXLEVEL = 4
    for i in (range 0 (MAXLEVEL + 1))
        let i =
            MAXLEVEL - i
        loduv :=
            ivec2
                uv.x >> i
                uv.y >> i
        for j in urange
            for k in urange
                let col1 =
                    texelFetch smp-screen (loduv + (ivec2 j k)) i
                let col2 =
                    texelFetch smp-screen2 (loduv + (ivec2 j k)) i
                let normal radius pos0 depth color matdata = (unpack-surfel-data col1 col2)
                if (radius == 0.0)
                    continue;
                let proj =
                    calc-projection;
                let coord =
                    vec3 ((pos0 * 2.0 - 1.0) * depth / proj.aspect) depth
                let dz = (abs (dot normal (normalize coord)))

                #let target-level =
                    ((ceil (log2 (radius / SEARCH_Rf))) + 0.5) as i32
                #if (i == target-level)
                    continue;
                if (depth >= (best_depth + ((radii / w) * radiusf)))
                    continue;
                let pos = (pos0 * size)
                pos := (deref gl_FragCoord.xy) - pos
                u := (normalize normal.xy)
                v := ((vec2 -u.y u.x) * (/ radius))
                u := u * (/ (radius * dz))
                l := (length (vec2 (dot v pos) (dot u pos)))
                if (l <= 1.0)
                    best_depth =
                        min (deref best_depth) depth
                    let wi = (1.0 - l)
                    w += wi
                    radii += radius * wi
                    sum_normal += normal * wi
                    sum_pos += pos0 * wi
                    sum_depth += depth * wi
                    sum_radius += radius * wi
                    sum_color += color * wi
                    sum_matdata += matdata * wi

    if (w == 0.0)
        return
            vec4 0.1 0.1 0.2 1.0
    let inv_w = (/ w)
    let normal radius uv depth color matdata =
        normalize sum_normal
        sum_radius * inv_w
        sum_pos * inv_w
        sum_depth * inv_w
        sum_color * inv_w
        sum_matdata * inv_w
    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
    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

    let roughness = matdata.x
    let metallic = matdata.y
    let color =
        linear->sRGB
            tonemap
                * exposure
                    +
                        ambient * albedo * (1.0 - metallic)
                        * lambert
                            BRDF albedo
                                roughness
                                metallic
                                \ l rd normal

    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 lod = 0

    loduv :=
        ivec2
            uv.x >> lod
            uv.y >> lod

    let col1 =
        texelFetch smp-screen loduv lod
    let col2 =
        texelFetch smp-screen2 loduv lod
    let normal radius uv depth = (unpack-surfel-data col1 col2)
    if (uv.x == 0.0)
        return
            vec4 0 0 0 1

    return
        vec4
            #\ uv 0.0
            normal * 0.5 + 0.5
            #normhue depth
            #normhue (radius / 16.0)
            1.0

fn shader (uv)
    reconstruct-surface 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_BUFFERS = 5

    global cell_buffers =
        arrayof GL.uint
            GL.CreateBuffer;
            GL.CreateBuffer;
            GL.CreateBuffer;
            GL.CreateBuffer;
            GL.CreateBuffer;
    let cell_buffer_sz = ((sizeof u32) * (1 + MAX_VOXELS))
    for i in (range (NUM_BUFFERS as u32))
        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 draw_voxels_cmd = (GL.CreateBuffer)
    #setup-ssbo draw_voxels_cmd buf-draw-voxels-cmd
    #let draw_voxels_cmd_sz = (sizeof DrawElementsIndirectCommand)
    #GL.NamedBufferData draw_voxels_cmd (i32 draw_voxels_cmd_sz) null GL.STREAM_DRAW
    #GL.BindBufferRange GL.SHADER_STORAGE_BUFFER BINDING_BUF_DRAW_VOXELS_CMD draw_voxels_cmd 0:i64 (i64 draw_voxels_cmd_sz)

    #global tx_position_normal = (GL.CreateTexture GL.TEXTURE_BUFFER)
    #GL.TextureBuffer tx_position_normal GL.RGBA32UI position_normal

    global fb-scene-color = (GL.CreateTexture GL.TEXTURE_2D)
    'setup fb-scene-color
        size = (ivec2 2048 2048)
        format = GL.RGBA32F
        pyramid = true
    global fb-scene-color2 = (GL.CreateTexture GL.TEXTURE_2D)
    'setup fb-scene-color2
        size = (ivec2 2048 2048)
        format = GL.RGBA32F
        pyramid = true
    for i in (range 12)
        let h = (2048 >> i)
        GL.ClearTexSubImage fb-scene-color i 0 0 0 h h 1 GL.RGBA GL.FLOAT null

    global rb-scene-depth = (GL.CreateRenderbuffer)
    setup-renderbuffer rb-scene-depth 2048 2048
        format = GL.DEPTH_COMPONENT
    global fb-scene = (GL.CreateFramebuffer)
    setup-framebuffer fb-scene
        color = fb-scene-color
        color = fb-scene-color2
        rb-depth = rb-scene-depth

    global vao-empty = (GL.CreateVertexArray)

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

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

    let pg-reduce-2x2 = (GL.CreateProgram)
    call
        attach-shaders (deref pg-reduce-2x2)
            compute = reduce-2x2
            #debug = true

    inline per-frame-setup (size)

        GL.BindTextureUnit 0 fb-scene-color
        GL.BindTextureUnit 1 fb-scene-color2
        GL.Uniform smp-screen 0
        GL.Uniform smp-screen2 1

        for i in (range NUM_BUFFERS)
            let buf = (cell_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 CellVals))
            ptr.count = 0:u32
            GL.UnmapNamedBuffer buf

        #local cmd = (DrawElementsIndirectCommand)
        #bind-ssbo draw_voxels_cmd buf-draw-voxels-cmd &cmd
        #GL.NamedBufferSubData draw_voxels_cmd 0 draw_voxels_cmd_sz &cmd

        inline bind-buffers (i0 i1)
            GL.BindBufferRange GL.SHADER_STORAGE_BUFFER
                BINDING_BUF_CELLS_IN
                cell_buffers @ i0
                \ 0:i64 (i64 cell_buffer_sz)
            GL.BindBufferRange GL.SHADER_STORAGE_BUFFER
                BINDING_BUF_CELLS_OUT
                cell_buffers @ i1
                \ 0:i64 (i64 cell_buffer_sz)

        GL.UseProgram pg-supershader

        do
            GL.Uniform u-program ProgramVoxelizeInit
            GL.Uniform u-level 6
            bind-buffers 0 1
            GL.DispatchCompute ((8 ** 3) as u32) 1 1
            GL.MemoryBarrier GL.SHADER_STORAGE_BARRIER_BIT
            #GL.MemoryBarrier (GL.ALL_BARRIER_BITS as u32)

        #do
            GL.UseProgram pg-voxelize
            GL.Uniform u-level 7
            bind-buffers 1 2
            GL.DispatchCompute 84 1 1
            GL.MemoryBarrier GL.SHADER_STORAGE_BARRIER_BIT
            #GL.MemoryBarrier (GL.ALL_BARRIER_BITS as u32)

        GL.Uniform u-program ProgramVoxelize

        #do
            GL.Uniform u-level 6
            bind-buffers 1 2
            #GL.DispatchCompute 21 1 1
            #GL.DispatchCompute 346 1 1
            GL.DispatchCompute 594 1 1
            GL.MemoryBarrier GL.SHADER_STORAGE_BARRIER_BIT
            #GL.MemoryBarrier (GL.ALL_BARRIER_BITS as u32)

        do
            GL.Uniform u-level 7
            bind-buffers 1 2
            #GL.DispatchCompute 84 1 1
            #GL.DispatchCompute 1395 1 1
            GL.DispatchCompute ((POINTLIMIT // 15) // 64) 1 1
            GL.MemoryBarrier GL.SHADER_STORAGE_BARRIER_BIT
            #GL.MemoryBarrier (GL.ALL_BARRIER_BITS as u32)

        do
            GL.Uniform u-level 8
            bind-buffers 2 3
            GL.DispatchCompute ((POINTLIMIT // 3) // 64) 1 1
            GL.MemoryBarrier GL.SHADER_STORAGE_BARRIER_BIT
            #GL.MemoryBarrier (GL.ALL_BARRIER_BITS as u32)

        #do
            for i in (range NUM_BUFFERS)
                let buf = (cell_buffers @ i)
                let ptr =
                    GL.MapNamedBufferRange buf 0 (sizeof u32)
                        GL.MAP_READ_BIT
                let ptr = (bitcast ptr (pointer CellVals))
                print i "=" ptr.count "/" ((ptr.count + 63:u32) // 64:u32)
                GL.UnmapNamedBuffer buf

        do
            GL.BindFramebuffer GL.FRAMEBUFFER fb-scene
            GL.Viewport 0 0 (i32 size.x) (i32 size.y)
            GL.ClearColor 0 0 0 1
            GL.DepthFunc GL.GREATER
            GL.ClearDepth 0
            GL.DepthRangef -1 1
            GL.Enable GL.DEPTH_TEST
            GL.Clear
                |
                    GL.COLOR_BUFFER_BIT
                    GL.DEPTH_BUFFER_BIT
                    GL.STENCIL_BUFFER_BIT

            GL.UseProgram pg-rasterize
            bind-buffers 3 0
            GL.Uniform u-level 8
            GL.BindVertexArray vao-empty
            GL.DrawArrays GL.POINTS 0 POINTLIMIT

            GL.Disable GL.DEPTH_TEST
            GL.BindFramebuffer GL.FRAMEBUFFER 0

        do
            GL.UseProgram pg-reduce-2x2
            GL.BindTextureUnit 0 fb-scene-color
            GL.Uniform smp-screen2 1
            for i in (range 1 12)
                let sz = ((2048 >> i) as u32)
                GL.Uniform u-level i
                GL.BindImageTexture IMAGE_TARGET_RGBA32F \
                    fb-scene-color i false 0 GL.WRITE_ONLY GL.RGBA32F
                GL.BindImageTexture IMAGE_TARGET2_RGBA32F \
                    fb-scene-color2 i false 0 GL.WRITE_ONLY GL.RGBA32F
                GL.DispatchCompute (sz // 8) (sz // 8) 1

    _ per-frame-setup shader

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


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

    f;