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

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

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 .testfragment

xvar uniform smp-data : usamplerBuffer
    location = 2
xvar uniform smp-offset : usamplerBuffer
    location = 3

fn shader (uv)
    let m = (u32 (textureSize smp-data))
    let r = (u32 (textureSize smp-offset))
    let p = (uvec2 (uv * 256.0))
    let key = (pack-morton2x16 p)
    let h1 = ((texelFetch smp-offset (i32 (key % r))) . x)
    let h0 = ((texelFetch smp-data (i32 ((key + h1) % m))) . x)
    ? (h0 == key)
        vec4 1.0
        vec4 0.0

fn main ()
    let bmp =
        load-bitmap
            .. module-dir "/data/duangle_logo_edge.png"
            #.. module-dir "/data/duangle_logo.png"
            components = 1

    let samples =
        local
            Array u32
    let w h = (unpack bmp.size)
    for x y in (multirange w h)
        let v = (deref ('fetch bmp x y))
        if (v > 127)
            'append samples
                pack-morton2x16 (uvec2 x y)

    let H phi =
        perfect-hashmap samples
            verbose = true
    dump H phi
    let m = (countof H)
    let r = (countof phi)

    let test_texture =
        static 'copy
            'setup (glCreateTexture GL_TEXTURE_2D)
                bitmap = bmp

    let sparse_data =
        static 'copy (glCreateBuffer)
    glNamedBufferData sparse_data (i32 ((sizeof u32) * m)) (H @ 0) GL_STATIC_DRAW
    let sparse_data_tex =
        static 'copy (glCreateTexture GL_TEXTURE_BUFFER)
    glTextureBuffer sparse_data_tex GL_R32UI sparse_data
    let sparse_offsets =
        static 'copy (glCreateBuffer)
    glNamedBufferData sparse_offsets (i32 ((sizeof u32) * r)) (phi @ 0) GL_STATIC_DRAW
    let sparse_offsets_tex =
        static 'copy (glCreateTexture GL_TEXTURE_BUFFER)
    glTextureBuffer sparse_offsets_tex GL_R32UI sparse_offsets

    fn per-frame-setup ()
        glBindTextureUnit 0 sparse_data_tex
        glBindTextureUnit 1 sparse_offsets_tex
        glUniform smp-data 0
        glUniform smp-offset 1

    return
        per-frame-setup
        shader

render-fragment-shader main
    #debug = true
    size = (ivec2 768)