summaryrefslogtreecommitdiff
path: root/chickadee/graphics.scm
blob: 67abacbb0e348582c034cecdac4852c005f2f3c5 (plain)
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
;;; Chickadee Game Toolkit
;;; Copyright © 2016 David Thompson <davet@gnu.org>
;;;
;;; Chickadee is free software: you can redistribute it and/or modify
;;; it under the terms of the GNU General Public License as published
;;; by the Free Software Foundation, either version 3 of the License,
;;; or (at your option) any later version.
;;;
;;; Chickadee is distributed in the hope that it will be useful, but
;;; WITHOUT ANY WARRANTY; without even the implied warranty of
;;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
;;; General Public License for more details.
;;;
;;; You should have received a copy of the GNU General Public License
;;; along with this program.  If not, see
;;; <http://www.gnu.org/licenses/>.

;;; Commentary:
;;
;; High-level rendering API.
;;
;;; Code:

(define-module (chickadee graphics)
  #:use-module (chickadee math matrix)
  #:use-module (chickadee graphics blend)
  #:use-module (chickadee graphics buffer)
  #:use-module (chickadee graphics color)
  #:use-module (chickadee graphics framebuffer)
  #:use-module (chickadee graphics gpu)
  #:use-module (chickadee graphics polygon)
  #:use-module (chickadee graphics shader)
  #:use-module (chickadee graphics texture)
  #:use-module (chickadee graphics viewport)
  #:use-module (srfi srfi-9)
  #:export (current-viewport
            current-framebuffer
            current-blend-mode
            current-polygon-mode
            current-cull-face-mode
            current-depth-test
            current-stencil-test
            current-texture
            current-projection
            current-multisample
            current-color-mask
            with-viewport
            with-framebuffer
            with-blend-mode
            with-polygon-mode
            with-cull-face-mode
            with-depth-test
            with-stencil-test
            with-texture
            with-projection
            with-multisample
            with-color-mask
            clear-screen
            gpu-apply
            gpu-apply*
            gpu-apply/instanced*
            gpu-apply/instanced))

(define-record-type <render-context>
  (make-render-context viewport framebuffer blend-mode polygon-mode
                       cull-face-mode depth-test stencil-test projection
                       multisample? color-mask textures)
  render-context?
  (viewport render-context-viewport set-render-context-viewport!)
  (framebuffer render-context-framebuffer set-render-context-framebuffer!)
  (blend-mode render-context-blend-mode set-render-context-blend-mode!)
  (polygon-mode render-context-polygon-mode set-render-context-polygon-mode!)
  (cull-face-mode render-context-cull-face-mode set-render-context-cull-face-mode!)
  (depth-test render-context-depth-test set-render-context-depth-test!)
  (stencil-test render-context-stencil-test set-render-context-stencil-test!)
  (projection render-context-projection set-render-context-projection!)
  (multisample? render-context-multisample? set-render-context-multisample!)
  (color-mask render-context-color-mask set-render-context-color-mask!)
  (textures render-context-textures))

(define render-context
  (make-render-context null-viewport
                       null-framebuffer
                       'replace
                       fill-polygon-mode
                       back-cull-face-mode
                       #f
                       #f
                       (make-identity-matrix4)
                       #f
                       default-color-mask
                       (make-vector 32 null-texture)))

(define (current-viewport)
  (render-context-viewport render-context))

(define (current-framebuffer)
  (render-context-framebuffer render-context))

(define (current-blend-mode)
  (render-context-blend-mode render-context))

(define (current-polygon-mode)
  (render-context-polygon-mode render-context))

(define (current-cull-face-mode)
  (render-context-cull-face-mode render-context))

(define (current-depth-test)
  (render-context-depth-test render-context))

(define (current-stencil-test)
  (render-context-stencil-test render-context))

(define (current-texture i)
  (vector-ref (render-context-textures render-context) i))

(define (current-projection)
  (render-context-projection render-context))

(define (current-multisample)
  (render-context-multisample? render-context))

(define (current-color-mask)
  (render-context-color-mask render-context))

(define-syntax-rule (with (getter setter value) body ...)
  (let ((prev (getter render-context)))
    (setter render-context value)
    body ...
    (setter render-context prev)))

(define-syntax-rule (with-viewport viewport body ...)
  (with (render-context-viewport set-render-context-viewport! viewport)
        body ...))

(define (clear-screen)
  (let ((viewport (current-viewport)))
    (set-gpu-framebuffer! (current-gpu) (current-framebuffer))
    (set-gpu-viewport! (current-gpu) viewport)
    (clear-viewport viewport)))

(define-syntax-rule (with-framebuffer framebuffer body ...)
  (with (render-context-framebuffer set-render-context-framebuffer! framebuffer)
        ;; As a convenience, initialize the viewport and projection
        ;; matrix as well so that the user doesn't have to explicitly
        ;; make a viewport and/or projection matrix unless they
        ;; actually want to do fancy viewport manipulations.
        (with-viewport (framebuffer-viewport framebuffer)
          (with-projection (framebuffer-projection framebuffer)
            body ...))))

(define-syntax-rule (with-blend-mode blend-mode body ...)
  (with (render-context-blend-mode set-render-context-blend-mode! blend-mode)
        body ...))

(define-syntax-rule (with-polygon-mode polygon-mode body ...)
  (with (render-context-polygon-mode set-render-context-polygon-mode! polygon-mode)
        body ...))

(define-syntax-rule (with-cull-face-mode cull-face-mode body ...)
  (with (render-context-cull-face-mode set-render-context-cull-face-mode! cull-face-mode)
        body ...))

(define-syntax-rule (with-depth-test depth-test body ...)
  (with (render-context-depth-test set-render-context-depth-test! depth-test)
        body ...))

(define-syntax-rule (with-stencil-test stencil-test body ...)
  (with (render-context-stencil-test set-render-context-stencil-test! stencil-test)
        body ...))

(define-syntax-rule (with-texture n texture body ...)
  (let* ((textures (render-context-textures render-context))
         (prev (vector-ref textures n)))
    (dynamic-wind
      (lambda () (vector-set! textures n texture))
      (lambda () body ...)
      (lambda () (vector-set! textures n prev)))))

(define-syntax-rule (with-projection matrix body ...)
  (with (render-context-projection set-render-context-projection! matrix)
        body ...))

(define-syntax-rule (with-multisample multisample? body ...)
  (with (render-context-multisample? set-render-context-multisample! multisample?)
        body ...))

(define-syntax-rule (with-color-mask color-mask body ...)
  (with (render-context-color-mask set-render-context-color-mask! color-mask)
        body ...))

(define (keyword->string kw)
  (symbol->string (keyword->symbol kw)))

(define-syntax uniform-apply
  (lambda (x)
    (syntax-case x ()
      ((_ shader ()) (datum->syntax x #t))
      ((_ shader (name value . rest))
       (with-syntax ((sname (datum->syntax x (keyword->symbol
                                              (syntax->datum #'name)))))
         #'(begin
             (shader-uniform-set! shader 'sname value)
             (uniform-apply shader rest)))))))

(define-syntax-rule (gpu-prepare shader vertex-array uniforms)
  (let ((gpu (current-gpu)))
    ;; It's important that the framebuffer is set before setting the
    ;; viewport because applying a new viewport will clear the current
    ;; framebuffer.
    (set-gpu-framebuffer! gpu (current-framebuffer))
    (set-gpu-viewport! gpu (current-viewport))
    (set-gpu-blend-mode! gpu (current-blend-mode))
    (set-gpu-polygon-mode! gpu (current-polygon-mode))
    (set-gpu-cull-face-mode! gpu (current-cull-face-mode))
    (set-gpu-depth-test! gpu (current-depth-test))
    (set-gpu-stencil-test! gpu (current-stencil-test))
    (set-gpu-multisample! gpu (current-multisample))
    (set-gpu-color-mask! gpu (current-color-mask))
    (set-gpu-shader! gpu shader)
    (let loop ((i 0))
      (when (< i 32)
        (set-gpu-texture! gpu i (current-texture i))
        (loop (1+ i))))
    (uniform-apply shader uniforms)
    ;; Sampler2D values aren't explicitly passed as uniform values via
    ;; gpu-apply, so we have to bind them to the proper texture units
    ;; behind the scenes.
    (shader-uniform-for-each
     (lambda (uniform)
       (when (eq? (uniform-type uniform) sampler-2d)
         (set-uniform-value! shader uniform (uniform-value uniform))))
     shader)))

(define-syntax-rule (gpu-apply* shader vertex-array offset count . uniforms)
  (begin
    (gpu-prepare shader vertex-array uniforms)
    (render-vertices vertex-array #:count count #:offset offset)))

(define-syntax-rule (gpu-apply shader vertex-array uniforms ...)
  (gpu-apply* shader vertex-array 0 #f uniforms ...))

(define-syntax-rule (gpu-apply/instanced* shader vertex-array offset count instances .
                                          uniforms)
  (begin
    (gpu-prepare shader vertex-array uniforms)
    (render-vertices/instanced vertex-array instances #:count count #:offset offset)))

(define-syntax-rule (gpu-apply/instanced shader vertex-array instances
                                         uniforms ...)
  (gpu-apply/instanced* shader vertex-array 0 #f instances uniforms ...))