forked from Shirakumo/trial
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtype-info.lisp
293 lines (273 loc) · 12.8 KB
/
type-info.lisp
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
#|
This file is a part of trial
(c) 2016 Shirakumo http://tymoon.eu (shinmera@tymoon.eu)
Author: Nicolas Hafner <shinmera@tymoon.eu>
|#
(in-package #:org.shirakumo.fraf.trial)
(defun check-integer-size (thing size &optional unsigned)
(declare (type (unsigned-byte 8) size))
(declare (optimize speed))
(if unsigned
(unless (<= 0 thing (expt 2 size))
(error "~a does not fit within [0,2^~a]." thing size))
(let ((size (1- size)))
(unless (<= (- (expt 2 size)) thing (1- (expt 2 size)))
(error "~a does not fit within [-2^~a,2^~:*~a-1]." thing size)))))
(define-constant-fold-function cl-type->gl-type (type)
(cond ((eql type 'fixnum) :int)
((subtypep type '(signed-byte 8)) :char)
((subtypep type '(unsigned-byte 32)) :uint)
((subtypep type '(signed-byte 32)) :int)
((subtypep type '(unsigned-byte 64)) :ulong)
((subtypep type '(signed-byte 64)) :long)
((subtypep type 'single-float) :float)
((subtypep type 'double-float) :double)
((eql type 'vec2) :vec2)
((eql type 'vec3) :vec3)
((eql type 'vec4) :vec4)
((eql type 'mat2) :mat2)
((eql type 'mat3) :mat3)
((eql type 'mat4) :mat4)
(T (error "Don't know how to convert ~s to a GL type." type))))
(define-constant-fold-function gl-type->cl-type (type)
(ecase type
((:boolean :bool :ubyte :unsigned-byte :unsigned-char) '(unsigned-byte 8))
((:byte :char) '(signed-byte 8))
((:ushort :unsigned-short) '(unsigned-byte 16))
(:short '(signed-byte 16))
((:uint :unsigend-int) '(unsigned-byte 32))
((:int :fixed :sizei :enum :bitfield) '(signed-byte 32))
((:uint64 :ulong :unsigned-long) '(unsigned-byte 64))
((:int64 :long) '(signed-byte 64))
((:half :half-float) 'short-float)
((:float :clampf) 'single-float)
((:double :clampd) 'double-float)
(:vec2 'vec2)
(:vec3 'vec3)
(:vec4 'vec4)
(:mat2 'mat2)
(:mat3 'mat3)
(:mat4 'mat4)))
(defun gl-coerce (thing type)
(declare (optimize speed))
(ecase type
((:double :double-float)
(float thing 0.0d0))
((:float :single-float)
(float thing 0.0f0))
((:int)
#-elide-coercion-size-checks
(check-integer-size thing 32)
(values (round thing)))
((:uint :unsigned-int)
#-elide-coercion-size-checks
(check-integer-size thing 32 T)
(values (round thing)))
((:char :byte)
#-elide-coercion-size-checks
(check-integer-size thing 8)
(values (round thing)))
((:uchar :unsigned-char :unsigned-byte)
#-elide-coercion-size-checks
(check-integer-size thing 8 T)
(values (round thing)))))
(define-compiler-macro gl-coerce (&whole whole &environment env thing type)
(if (constantp type env)
`(funcall (load-time-value
(ecase ,type
((:double :double-float)
(lambda (thing) (float thing 0.0d0)))
((:float :single-float)
(lambda (thing) (float thing 0.0f0)))
((:int)
(lambda (thing)
#-elide-coercion-size-checks
(check-integer-size thing 32)
(values (round thing))))
((:uint :unsigned-int)
(lambda (thing)
#-elide-coercion-size-checks
(check-integer-size thing 32 T)
(values (round thing))))
((:char :byte)
(lambda (thing)
#-elide-coercion-size-checks
(check-integer-size thing 8)
(values (round thing))))
((:uchar :unsigned-char :unsigned-byte)
(lambda (thing)
#-elide-coercion-size-checks
(check-integer-size thing 8 T)
(values (round thing))))))
,thing)
whole))
(declaim (ftype (function (keyword) (unsigned-byte 8)) gl-type-size))
(define-constant-fold-function gl-type-size (type)
(ecase type
(:boolean 1)
((:ubyte :unsigned-byte :byte :char) 1)
((:ushort :unsigned-short :short) 2)
((:uint :unsigned-int :int) 4)
(:fixed 4)
((:ulong :unsigned-long :uint64 :int64) 8)
(:sizei 4)
(:enum 4)
((:intptr :sizeiptr :sync) #+x86 4 #+x86-64 8)
(:bitfield 4)
((:half :half-float) 2)
((:float :clampf) 4)
((:double :clampd) 8)
(:vec2 (* 2 4))
(:vec3 (* 3 4))
(:vec4 (* 4 4))
(:mat2 (* 2 2 4))
(:mat3 (* 3 3 4))
(:mat4 (* 4 44))))
(defgeneric buffer-field-base (type standard)
(:method ((type symbol) standard)
(buffer-field-base (find-class type) standard)))
(defgeneric buffer-field-size (type standard base)
(:method ((type symbol) standard base)
(buffer-field-size (find-class type) standard base)))
(defgeneric buffer-field-stride (type standard)
(:method ((type symbol) standard)
(buffer-field-stride (find-class type) standard)))
(defmethod buffer-field-base ((type (eql :int)) (standard (eql :std140))) 4)
(defmethod buffer-field-base ((type (eql :bool)) (standard (eql :std140))) 4)
(defmethod buffer-field-base ((type (eql :float)) (standard (eql :std140))) 4)
(defmethod buffer-field-base ((type (eql :vec2)) (standard (eql :std140))) 8)
(defmethod buffer-field-base ((type (eql :vec3)) (standard (eql :std140))) 16)
(defmethod buffer-field-base ((type (eql :vec4)) (standard (eql :std140))) 16)
(defmethod buffer-field-base ((type (eql :mat2)) (standard (eql :std140))) (buffer-field-base :vec4 standard))
(defmethod buffer-field-base ((type (eql :mat3)) (standard (eql :std140))) (buffer-field-base :vec4 standard))
(defmethod buffer-field-base ((type (eql :mat4)) (standard (eql :std140))) (buffer-field-base :vec4 standard))
(defmethod buffer-field-base ((type cons) (standard (eql :std140))) (buffer-field-base :vec4 standard))
(defmethod buffer-field-size ((type (eql :int)) (standard (eql :std140)) base) 4)
(defmethod buffer-field-size ((type (eql :bool)) (standard (eql :std140)) base) 4)
(defmethod buffer-field-size ((type (eql :float)) (standard (eql :std140)) base) 4)
(defmethod buffer-field-size ((type (eql :vec2)) (standard (eql :std140)) base) 8)
(defmethod buffer-field-size ((type (eql :vec3)) (standard (eql :std140)) base) 12)
(defmethod buffer-field-size ((type (eql :vec4)) (standard (eql :std140)) base) 16)
(defmethod buffer-field-size ((type (eql :mat2)) (standard (eql :std140)) base) (buffer-field-size '(:array :vec4 2) standard base))
(defmethod buffer-field-size ((type (eql :mat3)) (standard (eql :std140)) base) (buffer-field-size '(:array :vec4 3) standard base))
(defmethod buffer-field-size ((type (eql :mat4)) (standard (eql :std140)) base) (buffer-field-size '(:array :vec4 4) standard base))
(defmethod buffer-field-size ((type cons) (standard (eql :std140)) base)
(ecase (first type)
(:struct (buffer-field-size (second type) standard base))
(:array (destructuring-bind (identifier type count) type
(declare (ignore identifier))
(if (listp type)
(* count (buffer-field-size type standard base))
(round-to
(buffer-field-base :vec4 standard)
(* count (round-to (buffer-field-base :vec4 standard)
(buffer-field-base type standard)))))))))
(defmethod buffer-field-stride ((type (eql :int)) (standard (eql :std140))) 16)
(defmethod buffer-field-stride ((type (eql :bool)) (standard (eql :std140))) 16)
(defmethod buffer-field-stride ((type (eql :float)) (standard (eql :std140))) 16)
(defmethod buffer-field-stride ((type (eql :vec2)) (standard (eql :std140))) 16)
(defmethod buffer-field-stride ((type (eql :vec3)) (standard (eql :std140))) 16)
(defmethod buffer-field-stride ((type (eql :vec4)) (standard (eql :std140))) 16)
(defmethod buffer-field-stride ((type (eql :mat2)) (standard (eql :std140))) (* 2 (buffer-field-stride :vec4)))
(defmethod buffer-field-stride ((type (eql :mat3)) (standard (eql :std140))) (* 3 (buffer-field-stride :vec4)))
(defmethod buffer-field-stride ((type (eql :mat4)) (standard (eql :std140))) (* 4 (buffer-field-stride :vec4)))
(defmethod buffer-field-stride ((type cons) (standard (eql :std140)))
(ecase (first type)
(:struct (buffer-field-size (second type) standard 0))))
(defmethod buffer-field-base ((type (eql :int)) (standard (eql :vertex-buffer))) 1)
(defmethod buffer-field-base ((type (eql :bool)) (standard (eql :vertex-buffer))) 1)
(defmethod buffer-field-base ((type (eql :float)) (standard (eql :vertex-buffer))) 1)
(defmethod buffer-field-base ((type (eql :vec2)) (standard (eql :vertex-buffer))) 1)
(defmethod buffer-field-base ((type (eql :vec3)) (standard (eql :vertex-buffer))) 1)
(defmethod buffer-field-base ((type (eql :vec4)) (standard (eql :vertex-buffer))) 1)
(defmethod buffer-field-base ((type (eql :mat2)) (standard (eql :vertex-buffer))) 1)
(defmethod buffer-field-base ((type (eql :mat3)) (standard (eql :vertex-buffer))) 1)
(defmethod buffer-field-base ((type (eql :mat4)) (standard (eql :vertex-buffer))) 1)
(defmethod buffer-field-base ((type cons) (standard (eql :vertex-buffer)))
(ecase (first type)
(:struct (buffer-field-base (second type) standard))))
(defmethod buffer-field-size ((type (eql :int)) (standard (eql :vertex-buffer)) base) 4)
(defmethod buffer-field-size ((type (eql :bool)) (standard (eql :vertex-buffer)) base) 4)
(defmethod buffer-field-size ((type (eql :float)) (standard (eql :vertex-buffer)) base) 4)
(defmethod buffer-field-size ((type (eql :vec2)) (standard (eql :vertex-buffer)) base) 8)
(defmethod buffer-field-size ((type (eql :vec3)) (standard (eql :vertex-buffer)) base) 12)
(defmethod buffer-field-size ((type (eql :vec4)) (standard (eql :vertex-buffer)) base) 16)
(defmethod buffer-field-size ((type (eql :mat2)) (standard (eql :vertex-buffer)) base) (* 2 2 4))
(defmethod buffer-field-size ((type (eql :mat3)) (standard (eql :vertex-buffer)) base) (* 3 3 4))
(defmethod buffer-field-size ((type (eql :mat4)) (standard (eql :vertex-buffer)) base) (* 4 4 4))
(defmethod buffer-field-size ((type cons) (standard (eql :vertex-buffer)) base)
(ecase (first type)
(:struct (buffer-field-size (second type) standard base))))
(defmethod buffer-field-stride (type (standard (eql :vertex-buffer))) (buffer-field-size type standard 0))
(defmethod buffer-field-stride ((type cons) (standard (eql :std140)))
(ecase (first type)
(:struct (buffer-field-size (second type) standard 0))))
(defun gl-memref-std140 (ptr type)
(flet ((%ref (i)
(cffi:mem-aref ptr :float i)))
(declare (inline %ref))
(ecase type
(:int (cffi:mem-ref ptr :int))
(:uint (cffi:mem-ref ptr :uint))
(:float (cffi:mem-ref ptr :float))
(:double (cffi:mem-ref ptr :double))
(:vec2 (vec2 (%ref 0) (%ref 1)))
(:vec3 (vec3 (%ref 0) (%ref 1) (%ref 2)))
(:vec4 (vec4 (%ref 0) (%ref 1) (%ref 2) (%ref 3)))
(:mat2 (mat2 (list (%ref 0) (%ref 1)
(%ref 4) (%ref 5))))
(:mat3 (mat3 (list (%ref 0) (%ref 1) (%ref 2)
(%ref 4) (%ref 5) (%ref 6)
(%ref 8) (%ref 9) (%ref 10))))
;; FIXME: could copy raw array with memcpy.
(:mat4 (mat4 (list (%ref 0) (%ref 1) (%ref 2) (%ref 3)
(%ref 4) (%ref 5) (%ref 6) (%ref 7)
(%ref 8) (%ref 9) (%ref 10) (%ref 11)))))))
(defun gl-memref (ptr type &key (layout :std140))
(ecase layout
(:std140 (gl-memref-std140 ptr type))
;; TODO: (:std430)
))
(defun (setf gl-memref-std140) (value ptr type)
(flet (((setf %ref) (value i)
(setf (cffi:mem-aref ptr :float i) value)))
(declare (inline (setf %ref)))
(ecase type
(:int
(setf (cffi:mem-ref ptr :int) value))
(:uint
(setf (cffi:mem-ref ptr :uint) value))
(:float
(setf (cffi:mem-ref ptr :float) value))
(:double
(setf (cffi:mem-ref ptr :double) value))
(:vec2
(setf (%ref 0) (vx2 value))
(setf (%ref 1) (vy2 value)))
(:vec3
(setf (%ref 0) (vx3 value))
(setf (%ref 1) (vy3 value))
(setf (%ref 2) (vz3 value)))
(:vec4
(setf (%ref 0) (vx4 value))
(setf (%ref 1) (vy4 value))
(setf (%ref 2) (vz4 value))
(setf (%ref 3) (vw4 value)))
(:mat2
(loop with idx = #(0 1 4 5)
for i from 0 below 4
do (setf (%ref (aref idx i)) (miref2 value i))))
(:mat3
(loop with idx = #(0 1 2 4 5 6 8 9 10)
for i from 0 below 9
do (setf (%ref (aref idx i)) (miref3 value i))))
(:mat4
;; FIXME: could copy raw array with memcpy.
(loop for i from 0 below 16
do (setf (%ref i) (miref4 value i))))))
value)
(defun (setf gl-memref) (value ptr type &key (layout :std140))
(ecase layout
(:std140 (setf (gl-memref-std140 ptr type) value))
;; TODO: (:std430)
))