rev: 3827f53bae88e48a594610914ba74b9512ead6dd tukan/testing/test_rt.sc -rw-r--r-- 4.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
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
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.GBuffer
using import ..tukan.projection
using import ..tukan.color
using import ..tukan.brdf
using import .testfragment

let sqrt3 = (sqrt 3.0)
let THREADSIZE = 256

let NEAR = 0.1

let TU_GBUFFER = 0

# 32 64 128 256 512 1024
let VOLUME_CASCADE_LEVELS = 6
let VOLUME_LEVEL_SIZE = 32

run-stage;

uniform smp-screen : GBuffer.SamplerType
    binding = TU_GBUFFER

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

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 ()
    global gbuffer = (GBuffer)

    global world-volume = (GL.CreateTexture GL.TEXTURE_3D)
    'setup world-volume
        size = (ivec3 512)
        format = GL.R8
        pyramid = true

    global volume-cascade =
        arrayof GL.texture
            va-map
                inline (i)
                    let tex = (GL.CreateTexture GL.TEXTURE_3D)
                    'setup tex
                        size = (ivec3 VOLUME_LEVEL_SIZE)
                        format = GL.RG32UI
                    tex
                va-range VOLUME_LEVEL_SIZE

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

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

        'bind-for-fetch gbuffer TU_GBUFFER

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

            GL.Disable GL.DEPTH_TEST
            GL.Disable GL.CULL_FACE

    _ 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;