rev: 3827f53bae88e48a594610914ba74b9512ead6dd tukan/testing/test_voxel_loop.sc -rw-r--r-- 3.1 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
#
    1D version of

    Perfect Spatial Hashing (Lefebvre & Hoppe)
    http://hhoppe.com/perfecthash.pdf

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

using import glm
using import glsl
using import Array
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 .testfragment

#fn cast-ray (ro rd)
    let t0 t1 hit n0 n1 =
        iCube ro rd
    if (not hit)
        return
            unconst-all false t0 0

    let m = (u32 (textureSize smp-data))
    let r = (u32 (textureSize smp-offset))

    fn test-cell (key)
        """"key : u32
        let h1 = ((texelFetch smp-offset (i32 (key % r))) . x)
        let h0 = ((texelFetch smp-data (i32 ((key + h1) % m))) . x)
        return (h0 == key)

    # move cube from -0.5 .. 0.5 to 0 .. 1
    let ro = (ro + 0.5)
    let stp = (sign rd)
    let rdinv = (/ (abs rd))

    label merge (args...)
        return
            unconst-all args...
    label success (t i)
        merge (unconst true) t i
    label fail (t i)
        merge (unconst false) t i

    loop (i t level code) = 0 t0 0 0:u32
    let levelbit = (1:u32 << (voxel.level-bit (level as u32)))
    let iscale = (1 << level)
    let scale = (f32 iscale)
    let key =
        code | levelbit
    if (i < maxsteps)
        if (test-cell key)
            # crossing
            if (level == LEVELS)
                # hit
                success t i
            else
                # descend
                let pos = (ro + rd * t)
                let c = ((unpack-morton3x10 code) * 2:u32 + 1:u32)
                let p = (uvec3 (2.0 * scale * pos))
                let s =
                    ? (p < c)
                        uvec3 0
                        uvec3 1
                let code =
                    voxel.leaf-key code
                        | s.x (s.y << 1:u32) (s.z << 2:u32)
                repeat (i + 1) t (level + 1) code
        else
            # empty
            if (level == 0)
                # we are done
            elseif (test-cell (voxel.parent-key key))
                # parent is crossing, find neighboring cell and step forward

                let cc = (ivec3 (unpack-morton3x10 code))
                let c = ((vec3 cc) + 0.5)
                let t0 t1 hit n0 n1 =
                    iCube (ro * scale - c) (rd * scale)
                let d = (ivec3 n1)
                let cc = (cc + d)
                let t = t1

                let code =
                    pack-morton3x10 (uvec3 cc)
                if (code >= levelbit)
                    # we are done
                else
                    repeat (i + 1) t level code
            else
                # parent is empty
                repeat (i + 1) t (level - 1) (voxel.parent-key code)
    fail t i

fn main ()
    let loop (i) = (unconst 0)
    if (icmp<s i 16)
        loop (add i 1)
    return;

compile-glsl 'fragment
    typify main
    'dump-module
    'dump-disassembly
    'no-debug-info