summaryrefslogtreecommitdiff
path: root/chickadee/render.scm
blob: efb50047b5670437552f2d6113e598aa310dd023 (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
;;; 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 render)
  #:use-module (srfi srfi-88)
  #:use-module (chickadee math matrix)
  #:use-module (chickadee render gpu)
  #:use-module (chickadee render blend)
  #:use-module (chickadee render shader)
  #:use-module (chickadee render texture)
  #:use-module (chickadee render vertex-buffer)
  #:use-module (chickadee render viewport)
  #:export (current-viewport
            current-blend-mode
            current-depth-test
            current-texture
            current-projection
            with-viewport
            with-blend-mode
            with-depth-test
            with-texture
            with-projection
            gpu-apply
            gpu-apply*))

(define *current-viewport* null-viewport)
(define *current-blend-mode* 'replace)
(define *current-depth-test* #f)
(define *current-texture* null-texture)
(define *current-projection* (make-identity-matrix4))

(define (current-viewport)
  *current-viewport*)

(define (current-blend-mode)
  *current-blend-mode*)

(define (current-depth-test)
  *current-depth-test*)

(define (current-texture)
  *current-texture*)

(define (current-projection)
  *current-projection*)

(define-syntax-rule (with (name value) body ...)
  (let ((prev name))
    (dynamic-wind
      (lambda () (set! name value))
      (lambda () body ...)
      (lambda () (set! name prev)))))

(define-syntax-rule (with-viewport viewport body ...)
  (with (*current-viewport* viewport) body ...))

(define-syntax-rule (with-blend-mode blend-mode body ...)
  (with (*current-blend-mode* blend-mode) body ...))

(define-syntax-rule (with-depth-test depth-test body ...)
  (with (*current-depth-test* depth-test) body ...))

(define-syntax-rule (with-texture texture body ...)
  (with (*current-texture* texture) body ...))

(define-syntax-rule (with-shader shader body ...)
  (with (*current-shader* shader)
        (initialize-uniforms)
        body ...))

(define-syntax-rule (with-vertex-array vertex-array body ...)
  (with (*current-vertex-array* vertex-array) body ...))

(define-syntax-rule (with-projection matrix body ...)
  (with (*current-projection* matrix) body ...))

;; (define (initialize-uniforms)
;;   (hash-for-each (lambda (name uniform)
;;                    (unless (hash-get-handle *current-uniforms* name)
;;                      (hash-set! *current-uniforms* name
;;                                 (uniform-default-value uniform))))
;;                  (shader-uniforms *current-shader*)))

;; (define-syntax uniform-let
;;   (syntax-rules ()
;;     ((_ () body ...) (begin body ...))
;;     ((_ ((name value) . rest) body ...)
;;      (let ((uniform (shader-uniform (current-shader) name))
;;            (prev (hash-ref *current-uniforms* name)))
;;        (if uniform
;;            (dynamic-wind
;;              (lambda ()
;;                (hash-set! *current-uniforms* name value))
;;              (lambda ()
;;                (uniform-let rest body ...))
;;              (lambda ()
;;                (hash-set! *current-uniforms* name prev)))
;;            (error "no such uniform: " name))))))

;; (define (uniform-ref name)
;;   (uniform-value (shader-uniform (current-shader) name)))

(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->string
                                              (syntax->datum #'name)))))
         #'(begin
             (set-uniform-value! (shader-uniform shader sname) value)
             (uniform-apply shader rest)))))))

(define-syntax-rule (gpu-apply* shader vertex-array count . uniforms)
  (begin
    (gpu-state-set! *viewport-state* (current-viewport))
    (gpu-state-set! *blend-mode-state* (current-blend-mode))
    (gpu-state-set! *depth-test-state* (current-depth-test))
    (gpu-state-set! *texture-state* (current-texture))
    (gpu-state-set! *shader-state* shader)
    (gpu-state-set! *vertex-array-state* vertex-array)
    (uniform-apply shader uniforms)
    (render-vertices count)))

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