-
Notifications
You must be signed in to change notification settings - Fork 19
/
Copy pathrender.lisp
387 lines (347 loc) · 15.4 KB
/
render.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
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
(in-package :ulubis)
(defclass texture-gl ()
((width :accessor width :initarg :width :initform 0)
(height :accessor height :initarg :height :initform 0)
(cepl-texture :accessor cepl-texture :initarg :cepl-texture :initform 0)))
(defun create-texture (surface)
(with-slots (buffer) surface
(when (and buffer (not (pointer-eq buffer (null-pointer))))
(if (and (egl-supported? (backend *compositor*)) (egl-surface? (backend *compositor*) buffer))
(create-texture-egl surface)
(create-texture-shm surface)))))
(defun create-texture-egl (surface)
(multiple-value-bind (width height) (egl-get-dimensions (backend *compositor*) (buffer surface))
(setf (width surface) width)
(setf (height surface) height)
(when (and (texture surface) (cepl-texture (texture surface)))
(cepl:free (cepl-texture (texture surface))))
(setf (texture surface) (make-instance 'texture-gl :width width
:height height
:cepl-texture
(egl-texture-from-image (backend *compositor*) (buffer surface) width height)))
(wl-buffer-send-release (buffer surface))
(setf (buffer surface) (null-pointer))))
(defun create-texture-shm (surface)
(let* ((buffer (buffer surface))
(shm-buffer (wl-shm-buffer-get buffer))
(width (wl-shm-buffer-get-width shm-buffer))
(height (wl-shm-buffer-get-height shm-buffer))
(stride (wl-shm-buffer-get-stride shm-buffer))
(array (cepl:make-c-array-from-pointer
(list width height)
:uint8-vec4
(wl-shm-buffer-get-data shm-buffer))))
(setf (width surface) width)
(setf (height surface) height)
(when (and (texture surface) (cepl-texture (texture surface)))
(cepl:free (cepl-texture (texture surface))))
(setf (texture surface)
(make-instance 'texture-gl
:width width
:height height
:cepl-texture
(cepl:make-texture
array
:element-type :rgba8)))
;; Copy pixels from shared-memory buffer to SDL texture
(wl-buffer-send-release buffer)
(setf (buffer surface) (null-pointer))))
(defmacro with-blending-on-fbo (blending-params fbo &body body)
(let ((b-params (gensym "blending-params")))
`(let* ((,b-params ,blending-params))
(cepl.blending::%with-blending ,fbo nil ,b-params
,@body))))
(defmacro with-screen ((vertex-stream) &body body)
(let ((result (gensym "result"))
(array (gensym "array")))
`(let* ((,array (cepl:make-gpu-array (list (list (rtg-math:v! -1 -1 0)
(rtg-math:v! 0 0))
(list (rtg-math:v! -1 1 0)
(rtg-math:v! 0 1))
(list (rtg-math:v! 1 1 0)
(rtg-math:v! 1 1))
(list (rtg-math:v! 1 1 0)
(rtg-math:v! 1 1))
(list (rtg-math:v! 1 -1 0)
(rtg-math:v! 1 0))
(list (rtg-math:v! -1 -1 0)
(rtg-math:v! 0 0)))
:dimensions 6 :element-type 'cepl:g-pt))
(,vertex-stream (cepl:make-buffer-stream ,array)))
(let ((,result (progn ,@body)))
(cepl:free ,vertex-stream)
(cepl:free ,array)
,result))))
(defmacro with-quarter ((vertex-stream) &body body)
(let ((result (gensym "result"))
(array (gensym "array")))
`(let* ((,array (cepl:make-gpu-array (list (list (rtg-math:v! -1 -1 0)
(rtg-math:v! 0 0))
(list (rtg-math:v! -1 0 0)
(rtg-math:v! 0 1))
(list (rtg-math:v! 0 0 0)
(rtg-math:v! 1 1))
(list (rtg-math:v! 0 0 0)
(rtg-math:v! 1 1))
(list (rtg-math:v! 0 -1 0)
(rtg-math:v! 1 0))
(list (rtg-math:v! -1 -1 0)
(rtg-math:v! 0 0)))
:dimensions 6 :element-type 'cepl:g-pt))
(,vertex-stream (cepl:make-buffer-stream ,array)))
(let ((,result (progn ,@body)))
(cepl:free ,vertex-stream)
(cepl:free ,array)
,result))))
(defmacro with-rect ((vertex-stream width height) &body body)
(let ((vert-list (gensym "vert-list"))
(array (gensym "array"))
(result (gensym "result")))
`(let* ((,vert-list (list (list (rtg-math:v! 0 0 0)
(rtg-math:v! 0 0))
(list (rtg-math:v! ,width 0 0)
(rtg-math:v! 1 0))
(list (rtg-math:v! ,width ,height 0)
(rtg-math:v! 1 1))
(list (rtg-math:v! ,width ,height 0)
(rtg-math:v! 1 1))
(list (rtg-math:v! 0 ,height 0)
(rtg-math:v! 0 1))
(list (rtg-math:v! 0 0 0)
(rtg-math:v! 0 0))))
(,array (cepl:make-gpu-array ,vert-list :dimensions 6 :element-type 'cepl:g-pt))
(,vertex-stream (cepl:make-buffer-stream ,array)))
(let ((,result (progn ,@body)))
(cepl:free ,vertex-stream)
(cepl:free ,array)
,result))))
(defun delete-effect (effect)
(cepl:free (fbo effect)))
(defun get-or-make-fbo (surface effect)
(with-slots (width height) surface
(if (and (fbo effect) (= width (width effect)) (= height (height effect)))
(fbo effect)
(progn
;;(format t "Making new framebuffer (dimensions: ~Ax~A) ~%" width height)
(when (fbo effect)
(cepl:free (fbo effect)))
(setf (width effect) width)
(setf (height effect) height)
(let ((fbo (cepl:make-fbo `(0 :dimensions (,width ,height)))))
(setf (cepl:blending-params fbo) (cepl:make-blending-params :source-alpha :one))
(setf (fbo effect) fbo)
(setf (fbo-attachment effect) (cepl:attachment-tex fbo 0))
(setf (fbo-sample effect) (cepl:sample (fbo-attachment effect)))
fbo)))))
(defclass effect ()
((fbo :accessor fbo :initarg :fbo :initform nil)
(fbo-attachment :accessor fbo-attachment :initarg :fbo-attachment :initform nil)
(fbo-sample :accessor fbo-sample :initarg :fbo-sample :initform nil)
(blending-parameters :accessor blending-parameters :initarg :blending-parameters :initform nil)
(width :accessor width :initarg :width :initform nil)
(height :accessor height :initarg :height :initform nil)
(pipeline :accessor pipeline :initarg :pipeline :initform nil)))
#|
(defmethod add-effect ((surface surface) pipeline)
(with-slots (width height effects) surface
(let* ((fbo (cepl:make-fbo `(0 :dimensions (,width ,height))))
(fbo-attachment (cepl:attachment-tex fbo 0)))
(setf (cepl:blending-params fbo) (cepl:make-blending-params :destination-alpha :one :source-alpha :one))
(setf effects (cons (make-instance 'effect :width width :height height :pipeline pipeline :fbo fbo :fbo-attachment fbo-attachment :fbo-sample (cepl:sample fbo-attachment) :blending-parameters (cepl:make-blending-params)) effects)))))
|#
#|
(defmethod add-effect ((surface ulubis-surface) pipeline)
(with-slots (width height effects) surface
(push (make-instance 'effect :width width :height height :pipeline pipeline) effects)))
|#
(defmacro map-g-default/fbo (fbo pipeline vertex-stream &rest uniforms)
`(if ,fbo
(cepl:map-g-into ,fbo ,pipeline ,vertex-stream ,@uniforms)
(cepl:map-g ,pipeline ,vertex-stream ,@uniforms)))
;; Each screen to have its own framebuffer?
;; Let's
(defgeneric texture-of (surface)
(:documentation "Given a surface will return a texture sampler of either the underlying texture or a FBO which has been used to apply effects to the surface"))
(defmethod texture-of ((surface isurface))
;;(describe surface)
(with-slots (effects wl-surface) surface
;;(format t "Effects ~A~%" effects)
(with-slots (width height texture) wl-surface
(with-screen (ys)
(let ((tex (cepl-texture texture)))
(if effects
(let (dest-fbo)
(loop :for effect :in (reverse effects)
:for i :from 0 :to (- (length effects) 1)
:do (progn
(setf dest-fbo (get-or-make-fbo surface effect))
(let ((sampler (if (> i 0)
(cepl:sample (fbo-attachment (nth (- i 1) effects)))
(cepl:sample tex))))
(gl:viewport 0 0 width height)
(gl:disable :blend) ;; We just want to copy into a blank FBO
(clear dest-fbo) ;This makes shadows disappear
(cepl:map-g-into dest-fbo (pipeline effect) ys :texture sampler)
(gl:enable :blend)))
:finally (return-from texture-of (fbo-sample effect))))
(cepl:sample tex)))))))
#|
(defmethod texture-of :after ((surface isurface)); map-pipleine &optional final-fbo)
;;(format t "FRAME CALLBACK~%")
(with-slots (wl-surface) surface
(with-slots (frame-callback) wl-surface
(when frame-callback
(wl-callback-send-done (->resource frame-callback) (get-milliseconds))
(wl-resource-destroy (->resource frame-callback))
(remove-resource frame-callback)
(setf frame-callback nil)))))
|#
(defmacro with-surface ((vertex-stream tex mode surface &key (fbo nil) (z 0) (scale 1.0)) &body body)
(let ((x (gensym "x"))
(y (gensym "y"))
(texture (gensym "texture"))
(width (gensym "width"))
(height (gensym "height"))
(array (gensym "array")))
`(let* ((,x (x ,surface))
(,y (y ,surface))
(,texture (texture (wl-surface ,surface)))
(,width (width (wl-surface ,texture)))
(,height (height (wl-surface ,texture)))
(,array (cepl:make-gpu-array (list (list (rtg-math:v! 0 0 ,z)
(rtg-math:v! 0 0))
(list (rtg-math:v! ,width 0 ,z)
(rtg-math:v! 1 0))
(list (rtg-math:v! ,width ,height ,z)
(rtg-math:v! 1 1))
(list (rtg-math:v! ,width ,height ,z)
(rtg-math:v! 1 1))
(list (rtg-math:v! 0 ,height ,z)
(rtg-math:v! 0 1))
(list (rtg-math:v! 0 0 ,z)
(rtg-math:v! 0 0)))
:dimensions 6 :element-type 'cepl:g-pt))
(,vertex-stream (cepl:make-buffer-stream ,array))
(,tex (cepl-texture ,texture)))
(cepl:with-blending (blending-parameters ,mode)
,@body)
(cepl:free ,vertex-stream)
(cepl:free ,array)
(when (frame-callback ,surface)
(wl-callback-send-done (->resource (frame-callback ,surface)) (get-milliseconds))
(wl-resource-destroy (->resource (frame-callback ,surface)))
(setf (frame-callback ,surface) nil)))))
(defun ortho (left right bottom top near far)
(let ((m (m4:identity)))
(setf (m4:melm m 0 0) (/ 2.0 (- right left)))
(setf (m4:melm m 1 1) (/ 2.0 (- top bottom)))
(setf (m4:melm m 2 2) (/ -2.0 (- far near)))
(setf (m4:melm m 0 3) (coerce (- (/ (+ right left) (- right left))) 'float))
(setf (m4:melm m 1 3) (coerce (- (/ (+ top bottom) (- top bottom))) 'float))
(setf (m4:melm m 2 3) (coerce (- (/ (+ far near) (- far near))) 'float))
m))
(cepl:defun-g passthrough-vert ((vert cepl:g-pt))
(values (rtg-math:v! (cepl:pos vert) 1) (cepl:tex vert)))
(cepl:defun-g passthrough-frag ((tex-coord :vec2) &uniform (texture :sampler-2d))
(cepl:texture texture tex-coord))
(cepl:defpipeline-g passthrough-shader ()
(passthrough-vert cepl:g-pt) (passthrough-frag :vec2))
(cepl:defun-g default-vertex-shader ((vert cepl:g-pt) &uniform (ortho :mat4) (surface-scale :mat4) (surface-translate :mat4))
(values (* ortho surface-translate surface-scale (rtg-math:v! (cepl:pos vert) 1))
(:smooth (cepl:tex vert))))
#|
Wayland surfaces come in in BGR (I believe) so we swap to RGB here
|#
(cepl:defun-g default-fragment-shader ((tex-coord :vec2) &uniform (texture :sampler-2d) (alpha :float))
(rtg-math:v! (rtg-math:s~ (cepl:texture texture tex-coord) :z)
(rtg-math:s~ (cepl:texture texture tex-coord) :y)
(rtg-math:s~ (cepl:texture texture tex-coord) :x)
(* alpha (rtg-math:s~ (cepl:texture texture tex-coord) :w))))
(cepl:defun-g default-rgb-frag ((tex-coord :vec2) &uniform (texture :sampler-2d) (alpha :float))
(rtg-math:v! (rtg-math:s~ (cepl:texture texture tex-coord) :x)
(rtg-math:s~ (cepl:texture texture tex-coord) :y)
(rtg-math:s~ (cepl:texture texture tex-coord) :z)
(* alpha (rtg-math:s~ (cepl:texture texture tex-coord) :w))))
(cepl:defun-g ulubis-cursor-vertex-shader ((vert cepl:g-pt) &uniform (ortho :mat4) (origin :mat4) (origin-inverse :mat4) (surface-scale :mat4) (surface-translate :mat4))
(values (* ortho surface-translate origin-inverse surface-scale origin (rtg-math:v! (cepl:pos vert) 1))
(:smooth (cepl:tex vert))))
(cepl:defpipeline-g ulubis-cursor-pipeline ()
(ulubis-cursor-vertex-shader cepl:g-pt) (default-fragment-shader :vec2))
(defmethod draw-cursor ((surface isurface) fbo x y ortho)
;;(format t "DRAW-CURSOR~%")
;;(describe surface)
(when (texture (wl-surface surface))
(with-rect (vertex-stream (width (wl-surface surface)) (height (wl-surface surface)))
(let ((texture (texture-of surface)))
(gl:viewport 0 0 (screen-width *compositor*) (screen-height *compositor*))
(map-g-default/fbo fbo #'ulubis-cursor-pipeline vertex-stream
:ortho ortho
:origin (m4:translation (rtg-math:v! (- (origin-x surface)) (- (origin-y surface)) 0))
:origin-inverse (m4:translation (rtg-math:v! (origin-x surface) (origin-y surface) 0))
:surface-scale (m4:scale (rtg-math:v! (scale-x surface) (scale-y surface) 1.0))
:surface-translate (m4:translation (rtg-math:v! (- x (x surface)) (- y (y surface)) 0.0))
:texture texture
:alpha (opacity surface))))))
(cepl:defun-g cursor-vertex-shader ((vert cepl:g-pt) &uniform (ortho :mat4))
(values (* ortho (rtg-math:v! (cepl:pos vert) 1))
(cepl:tex vert)))
(cepl:defpipeline-g cursor-pipeline ()
(cursor-vertex-shader cepl:g-pt) (passthrough-frag :vec2))
(defparameter *default-cursor* nil)
(defun init-vector-cursor ()
(unless *default-cursor*
(setf *default-cursor* (make-instance 'cairo-surface
:allow-gl t
:width 64
:height 64))
(setf (draw-func *default-cursor*)
(lambda ()
(cl-cairo2:translate 32 32)
(cl-cairo2:set-source-rgba 1 1 1 0)
(cl-cairo2:paint)
(cl-cairo2:move-to 0 0)
(cl-cairo2:line-to 0 15)
(cl-cairo2:line-to 4 13)
(cl-cairo2:line-to 6 20)
(cl-cairo2:line-to 8 19)
(cl-cairo2:line-to 6 12)
(cl-cairo2:line-to 9 12)
(cl-cairo2:close-path)
(cl-cairo2:set-source-rgba 0 0 0 1)
(cl-cairo2:stroke-preserve)
(cl-cairo2:set-source-rgba 1 1 1 1)
(cl-cairo2:fill-path)
))))
(defun init-image-cursor ()
(unless *default-cursor*
(setf *default-cursor* (make-instance 'cairo-surface
:allow-gl t
:filename "assets/cursor.png"))))
(defun make-g-pt-quad (top bottom left right)
`((,(rtg-math:v! left top 0) ,(rtg-math:v! 0 0))
(,(rtg-math:v! left bottom 0) ,(rtg-math:v! 0 1))
(,(rtg-math:v! right top 0) ,(rtg-math:v! 1 0))
(,(rtg-math:v! right bottom 0) ,(rtg-math:v! 1 1))
(,(rtg-math:v! right top 0) ,(rtg-math:v! 1 0))
(,(rtg-math:v! left bottom 0) ,(rtg-math:v! 0 1))))
(defmethod draw-cursor ((cursor (eql nil)) fbo x y ortho)
(unless *default-cursor*
(init-image-cursor)
(cairo-surface-redraw *default-cursor*))
(let* ((halfw (/ (width *default-cursor*) 2))
(halfh (/ (height *default-cursor*) 2))
(array (cepl:make-gpu-array (make-g-pt-quad (- y halfh)
(+ y halfh)
(- x halfw)
(+ x halfw))
:element-type 'cepl:g-pt))
(vertex-stream (cepl:make-buffer-stream array)))
(map-g-default/fbo fbo #'cursor-pipeline vertex-stream
:ortho ortho
:texture (texture-of *default-cursor*))
(cepl:free vertex-stream)
(cepl:free array)
(setf (render-needed *compositor*) t)))
#|
(defgeneric render-surface (surface mode))
|#