rev: 3827f53bae88e48a594610914ba74b9512ead6dd tukan/testing/test_voxel_rt.sc -rw-r--r-- 11.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
#
    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 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 .testfragment

let LEVELS = 8
let IN = (1 << LEVELS)
let maxsteps = (IN * 17 // 10)
let N = (IN as f32)

run-stage;

fn torus-sphere-merge (p)
    'sdSmoothOr
        sdmDist
            sdUberprim
                p - (vec3 0.01)
                vec4 1.0 1.0 0.25 0.25
                vec3 1.0 0.25 0.0
            sdMaterial
                vec4 1.0 0.5 0.3 1.0
        sdmDist
            sdSphere
                p - (vec3 0.5 0.0 0.0)
                0.4
            sdMaterial
                vec4 0.3 0.5 1.0 1.0
        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 map (p)
    let p = p.yzx
    #torus-sphere-merge p
    two-boxes-merge p
    #sdOr
        sdBox
            p -
                vec3 1.0 -0.5 1.0
            vec3 0.5
        sdUberprim p
            vec4 1.0 1.0 0.25 0.25
            vec3 1.0 0.25 0.0
        sdPlane p
            vec4 0.0 1.0 0.0 1.0

let BINDING_BUF_MATS = 1
let BINDING_BUF_NORMS = 2
struct MatVals plain
    entries : (array sdMaterial)
struct NormVals plain
    entries : (array u32)
buffer buf-mats : MatVals
    binding = BINDING_BUF_MATS
    \ readonly
buffer buf-norms : NormVals
    binding = BINDING_BUF_NORMS
    \ readonly
uniform smp-data : usamplerBuffer
    location = 2
uniform smp-offset : usamplerBuffer
    location = 3

fn get-key-offset (key)
    """"key : u32
    let m = (u32 (textureSize smp-data))
    let r = (u32 (textureSize smp-offset))

    let h1 = ((texelFetch smp-offset (i32 (key % r))) . x)
    return (i32 ((key + h1) % m))

fn test-cell (key)
    """"key : u32
    let h0 = ((texelFetch smp-data (get-key-offset key)) . x)
    return (h0 == key)

inline get-mat-norm (key)
    """"key : u32
    let ofs = (get-key-offset key)
    _
        buf-mats.entries @ ofs
        buf-norms.entries @ ofs

fn cast-ray (ro rd aperture)
    let t0 t1 hit n0 n1 =
        iCube ro rd
    if (not hit)
        return -1:u32 t0 0 -1:u32

    let t0 =
        max 0.0 t0

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

    local hit = -1:u32
    local t = t0
    local level = 0
    local code = 0:u32
    local steps = 0
    local hitcode = -1:u32
    local quit = false
    loop (i = 0)
        if ((i >= maxsteps) | ((deref quit) == true))
            break;
        let distlevel =
            (log2 (max 1.0 (aperture * (deref t)))) as i32
        let maxlevel =
            LEVELS - (min LEVELS distlevel)

        steps = i
        let levelbit = (1:u32 << (voxel.level-bit ((deref level) as u32)))
        let iscale = (1 << level)
        let scale = (f32 iscale)
        let key =
            code | levelbit
        if (test-cell key)
            # crossing
            if (level >= maxlevel)
                # hit
                hit = key
                hitcode = code
                quit = true
            else
                # descend
                let pos = (ro + rd * (deref 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
                code =
                    voxel.leaf-key code
                        | s.x (s.y << 1:u32) (s.z << 2:u32)
                level += 1
        else
            # empty
            if (level == 0)
                # we are done
                quit = true
            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)

                t = t1
                code =
                    pack-morton3x10 (uvec3 cc)
                if (code >= levelbit)
                    # we are done
                    quit = true
            else
                # parent is empty
                level -= 1
                code =
                    voxel.parent-key code
        i + 1
    return (deref hit) (deref t) (deref steps) (deref hitcode)

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

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

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

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

fn shader (uv)
    let t = (deref shglobals.time)
    let uv =
        (uv * 2.0 - 1.0) * (vec2 shglobals.aspect 1)
    let yaw =
        versor (vec3 0 1 0)
            * 0.2 t
            #radians 90.0
    let l =
        normalize
            vec3 -0.5 1.0 -0.25
    let l =
        versor-rotate yaw l
    let pitch =
        versor-rotate yaw
            versor (vec3 1 0 0)
                0.3 #(mix 0.3 1.0 ((sin t) * 0.5 + 0.5))
    #let ro = (vec3 0)
    #let rd =
        vec3 uv 1
    rd :=
        transform-invert-dist
            #normalize (vec3 uv 1.0)
            vec3 uv 1
    ro :=
        transform-invert-pos (vec3 0)
    #let rd =
        normalize rd
    let dv =
        (vec3 0 1 -3) * 0.3 # (0.1 + ((sin (t * 0.2)) * 0.5 + 0.5) * 8.0)
    #let ro =
        versor-rotate yaw (ro + dv)
    #let rd =
        versor-rotate pitch rd
    let fail-color = (vec4 0.0)
    fn hue (i)
        vec4
            magma ((f32 i) / 50.0)
            1.0
    let hit t i code = (cast-ray ro rd 0.5)
    if (hit == -1:u32)
        return
            hue i
    else
        #let pos =
            #ro + rd * t
        let mat norm =
            get-mat-norm hit
        let norm = (deref norm)
        let norm =
            uvec2
                norm & 65535:u32
                (norm >> 16:u32) & 65535:u32
        let norm =
            decode-normal
                (vec2 norm) / 65535.0

        let n = l
        #let so = (ro + rd * t + norm * (1.0 / N))

        let coord = (unpack-morton3x10 code)
        let so = (((vec3 coord) / N) - 0.5 + norm * (2.0 / N))

        #let shadow-hit = (cast-ray so n 0.0)
        let shadow =
            #? (shadow-hit == -1:u32) 1.0 0.0
            1.0

        let lambert =
            (max 0.0 (dot norm l)) * shadow

        let albedo =
            mat.albedo.xyz * 1.0 #hp
        let ambient =
            ((dot norm (vec3 0.0 1.0 0.0)) * 0.5 + 0.5) * 0.1
        let exposure = 2.0

        let color =
            linear->sRGB
                tonemap
                    * exposure
                        +
                            ambient * albedo * (1.0 - mat.metallic)
                            * lambert
                                BRDF albedo
                                    deref mat.roughness
                                    deref mat.metallic
                                    \ l (- rd) norm
        return
            +
                vec4 color 1.0
                0.0 #(f32 i) / 200.0
                #vec4 ro 1.0

inline main ()
    let ms = (SDL_GetTicks)
    let voxels mats norms steps =
        voxel.voxelize map
            levels = LEVELS
    print
        countof voxels
        "voxels in"
        (SDL_GetTicks) - ms
        "ms and"
        steps
        "steps"
    let voxel-indices voxel-offsets =
        perfect-hashmap voxels
            verbose = true
            search-steps = 0
            sigma = (/ (2.0 * 2.0))

    let voxel-keys = (Box.new (GrowingArray u32))
    let voxel-mats = (Box.new (GrowingArray sdMaterial))
    let voxel-norms = (Box.new (GrowingArray u32))

    for idx in voxel-indices
        if (idx < 0)
            'append voxel-keys -1:u32
            'append voxel-mats (sdMaterial)
            'append voxel-norms 0:u32
        else
            'append voxel-keys
                deref (voxels @ idx)
            'append voxel-mats (mats @ idx)
            'append voxel-norms (norms @ idx)

    assert ((countof voxel-keys) == (countof voxel-mats))

    global sparse_mat = (GL.CreateBuffer)
    let sparse_mat_sz =
        (sizeof sdMaterial) * (countof voxel-mats)
    GL.NamedBufferData sparse_mat (i32 sparse_mat_sz) (& (voxel-mats @ 0)) GL.STATIC_DRAW
    GL.BindBufferRange GL.SHADER_STORAGE_BUFFER BINDING_BUF_MATS sparse_mat 0:i64 (i64 sparse_mat_sz)

    global sparse_norm = (GL.CreateBuffer)
    let sparse_norm_sz =
        (sizeof u32) * (countof voxel-norms)
    GL.NamedBufferData sparse_norm (i32 sparse_norm_sz) (& (voxel-norms @ 0)) GL.STATIC_DRAW
    GL.BindBufferRange GL.SHADER_STORAGE_BUFFER BINDING_BUF_NORMS sparse_norm 0:i64 (i64 sparse_norm_sz)

    global sparse_data = (GL.CreateBuffer)
    GL.NamedBufferData sparse_data (i32 ((sizeof u32) * (countof voxel-keys))) (& (voxel-keys @ 0)) GL.STATIC_DRAW
    global sparse_data_tex = (GL.CreateTexture GL.TEXTURE_BUFFER)
    GL.TextureBuffer sparse_data_tex GL.R32UI sparse_data

    global sparse_offsets = (GL.CreateBuffer)
    GL.NamedBufferData sparse_offsets (i32 ((sizeof u32) * (countof voxel-offsets))) (& (voxel-offsets @ 0)) GL.STATIC_DRAW
    global sparse_offsets_tex = (GL.CreateTexture GL.TEXTURE_BUFFER)
    GL.TextureBuffer sparse_offsets_tex GL.R32UI sparse_offsets

    fn per-frame-setup (size pg-test frame)
        GL.BindTextureUnit 0 sparse_data_tex
        GL.BindTextureUnit 1 sparse_offsets_tex
        GL.Uniform smp-data 0
        GL.Uniform smp-offset 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;