rev: 3827f53bae88e48a594610914ba74b9512ead6dd tukan/testing/testfragment.sc -rw-r--r-- 8.8 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
using import glm
using import glsl
using import struct

using import ..tukan.GLMain
using import ..tukan.Screen
using import ..tukan.GUI

using import ..tukan.gl
using import ..tukan.sdl
using import ..tukan.rotation

let BUFFER_COUNT = 3

struct ShaderGlobals plain
    view : mat4
    view-inverse : mat4
    size : ivec2
    aspect : f32
    time : f32
    frame : i32

uniform shglobals : ShaderGlobals
    binding = 64

inout uv : vec2 (location = 0)
out out_Color : vec4

global g_frame = 0

inline render-fragment-shader (func opts...)
    let size = (va-option size opts... (ivec2 960 540))
    let debug = (va-option debug opts... false)

    let glmain =
        GLMain
            title = "Test Fragment"
            width = size.x
            height = size.y
            resizable = true

    print "t: toggle timer display"
    print "o: toggle orbit mode"
    print ".: toggle time"
    print "escape: toggle mouse capture"
    print "wsadrf: move"

    let screen = (Screen)

    let frame-setup shader-func = (func)

    global timer_accum = 0.0
    global timer_accum_count = 0
    global enable_timer_display = true
    global advance_time = true
    global g_time = 0.0
    global orbit_mode = false
    global cam_pitch = 0
    global cam_yaw = 0
    global world_scale = 1.0 #100.0
    global cam_origin = (vec3 0 0 -world_scale)
    global capturing = false
    global timers =
        arrayof GL.Query
            GL.QueryTimeElapsed;
            GL.QueryTimeElapsed;
            GL.QueryTimeElapsed;

    global globals-data = (GL.Buffer)
    setup-ubo (deref globals-data) shglobals BUFFER_COUNT

    fn test-vertex ()
        uv.out =
            ((Screen.set-vertex-position) * 0.5) + 0.5
        return;

    fn test-fragment ()
        out_Color = (shader-func (deref uv.in))
        return;

    fn capture-mouse (enabled)
        if (enabled == capturing)
            return;
        capturing = enabled
        if enabled
            SDL_CaptureMouse SDL_TRUE
            SDL_SetRelativeMouseMode SDL_TRUE
        else
            SDL_CaptureMouse SDL_FALSE
            SDL_SetRelativeMouseMode SDL_FALSE
        ;

    capture-mouse (deref capturing)

    fn compute-cam-versor ()
        pitch-versor := (versor (vec3 1 0 0) (angletof (deref cam_pitch)))
        yaw-versor := (versor (vec3 0 1 0) (angletof (deref cam_yaw)))

        versor-rotate yaw-versor pitch-versor


    global pg-test = (GL.Program)

    call
        attach-shaders (deref pg-test)
            vertex = test-vertex
            fragment = test-fragment
            debug = debug

    inline render-view (size)
        let buffer-index = (g_frame % BUFFER_COUNT)

        let timer = (timers @ buffer-index)
        if (g_frame >= BUFFER_COUNT)
            local timer_value = 0:u32
            GL.GetQueryObjectuiv timer GL.QUERY_RESULT_NO_WAIT &timer_value
            if (timer_value != 0:u32)
                timer_accum += ((timer_value as f32) / 1000000.0)
                timer_accum_count += 1
                if (timer_accum_count == 60)
                    let ms = (timer_accum / (timer_accum_count as f32))
                    if enable_timer_display
                        let perc = (ms * 100.0 / (1000.0 / 60.0))
                        print ms "ms," (1000.0 / ms) "fps," perc "% of frame"
                    timer_accum = 0.0
                    timer_accum_count = 0
            else
                print "GL timer stalled"
        GL.BeginQuery GL.TIME_ELAPSED timer

        GL.BindFramebuffer GL.FRAMEBUFFER 0
        GL.Viewport 0 0 (i32 size.x) (i32 size.y)
        #GL.ClearColor 0 0 1 1
        #GL.Clear
            |
                GL.COLOR_BUFFER_BIT
                GL.DEPTH_BUFFER_BIT
                GL.STENCIL_BUFFER_BIT

        GL.UseProgram pg-test

        let m m-inv =
            if orbit_mode
                let a = ((deref g_time) * 0.2)
                let c s = (cos a) (sin a)
                let rot-matrix =
                    mat4
                        \  c  0 -s  0
                        \  0  1  0  0
                        \  s  0  c  0
                        \  0  0  0  1
                let inv-rot-matrix = (transpose rot-matrix)
                let translate-matrix =
                    mat4
                        \  1  0  0  0
                        \  0  1  0  0
                        \  0  0  1  1
                        \  0  0  0  1
                let inv-translate-matrix =
                    mat4
                        \  1  0  0  0
                        \  0  1  0  0
                        \  0  0  1  -1
                        \  0  0  0  1
                _
                    rot-matrix * translate-matrix
                    inv-translate-matrix * inv-rot-matrix
            else
                cam-versor := (compute-cam-versor)

                forward :=
                    versor-rotate cam-versor
                        vec3 0 0 1
                right :=
                    versor-rotate cam-versor
                        vec3 1 0 0
                up :=
                    versor-rotate cam-versor
                        vec3 0 1 0

                local numkeys = 0
                state := (SDL_GetKeyboardState &numkeys)
                let SPEED =
                    if (state @ SDL_SCANCODE_LSHIFT) (20.0 / 60.0)
                    else (5.0 / 60.0)
                if (state @ SDL_SCANCODE_W)
                    cam_origin += forward * SPEED
                if (state @ SDL_SCANCODE_S)
                    cam_origin -= forward * SPEED
                if (state @ SDL_SCANCODE_D)
                    cam_origin += right * SPEED
                if (state @ SDL_SCANCODE_A)
                    cam_origin -= right * SPEED
                if (state @ SDL_SCANCODE_R)
                    cam_origin += up * SPEED
                if (state @ SDL_SCANCODE_F)
                    cam_origin -= up * SPEED

                rot-matrix := (rotation cam-versor)
                let inv-rot-matrix = (transpose rot-matrix)

                scale-matrix :=
                    mat4 (mat3 world_scale)
                inv-scale-matrix :=
                    mat4 (mat3 (/ world_scale))

                let translate-matrix =
                    mat4
                        \  1  0  0  -cam_origin.x
                        \  0  1  0  -cam_origin.y
                        \  0  0  1  -cam_origin.z
                        \  0  0  0  1
                let inv-translate-matrix =
                    mat4
                        \  1  0  0  cam_origin.x
                        \  0  1  0  cam_origin.y
                        \  0  0  1  cam_origin.z
                        \  0  0  0  1
                _
                    scale-matrix * (transpose (translate-matrix * (mat4 rot-matrix)))
                    inv-scale-matrix * (transpose ((mat4 inv-rot-matrix) * inv-translate-matrix))


        local data =
            ShaderGlobals
                view = m
                view-inverse = m-inv
                aspect = (size.x / size.y)
                time = g_time
                frame = (deref g_frame)
                size = size
        bind-ubo (deref globals-data) shglobals &data buffer-index

        frame-setup size pg-test (deref g_frame)

        g_frame = g_frame + 1
        if advance_time
            g_time += (/ 60.0)
        GL.UseProgram pg-test
        'draw screen
        GL.UseProgram 0
        GL.EndQuery GL.TIME_ELAPSED

    @@ 'on GLMain.on-event
    inline (event glmain)
        switch (event.type as (typeof SDL_KEYDOWN))
        case SDL_MOUSEMOTION
            if capturing
                cam_yaw += (ftoangle ((event.motion.xrel as f32) * pi * 0.0005))
                cam_pitch += (ftoangle ((event.motion.yrel as f32) * pi * 0.0005))
                cam_pitch = (clamp cam_pitch (ftoangle (pi * -0.5)) (ftoangle (pi * 0.5)))

        case SDL_KEYDOWN
            let sym = (deref event.key.keysym.sym)
            switch (bitcast sym (typeof SDLK_f))
            case SDLK_o
                if (not capturing)
                    orbit_mode = (not orbit_mode)
                    print "orbit mode"
                        if orbit_mode "enabled"
                        else "disabled"
            case SDLK_t
                enable_timer_display = (not enable_timer_display)
                print "timer display"
                    if enable_timer_display "enabled"
                    else "disabled"
            case SDLK_PERIOD
                advance_time = (not advance_time)
                print
                    if advance_time "playing"
                    else "stopped"
            case SDLK_ESCAPE
                if (not orbit_mode)
                    capture-mouse (not capturing)
            default
                ;;
        default
            ;;


    @@ 'on GLMain.on-draw
    inline (time size glmain)
        render-view size
    let gui = (GUI glmain)

    'run glmain

do
    let render-fragment-shader shglobals

    locals;