Skip to content

Commit 9b3d9e3

Browse files
authored
GLCORE: Performance optimizations (#385)
Remove use of length, repeated list traversals and combinations of apply+append.
1 parent f6dae46 commit 9b3d9e3

File tree

1 file changed

+122
-94
lines changed

1 file changed

+122
-94
lines changed

modules/ln_glcore/glcore.scm

Lines changed: 122 additions & 94 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
#|
22
LambdaNative - a cross-platform Scheme framework
3-
Copyright (c) 2009-2013, University of British Columbia
3+
Copyright (c) 2009-2020, University of British Columbia
44
All rights reserved.
55

66
Redistribution and use in source and binary forms, with or
@@ -131,44 +131,54 @@ SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
131131
)
132132

133133
(define (glCoreVertex2f x0 y0 . xtra)
134-
(let ((x (flo x0)) (y (flo y0))
135-
(tx (if (fx= (length xtra) 2) (flo (car xtra)) 0.5))
136-
(ty (if (fx= (length xtra) 2) (flo (cadr xtra)) 0.5)))
137-
(f32vector-set! glCore:varray (fx+ glCore:vindex 0) x)
138-
(f32vector-set! glCore:varray (fx+ glCore:vindex 1) y)
139-
(set! glCore:vindex (fx+ glCore:vindex 2))
140-
(f32vector-set! glCore:tarray (fx+ glCore:tindex 0) tx)
141-
(f32vector-set! glCore:tarray (fx+ glCore:tindex 1) ty)
142-
(set! glCore:tindex (fx+ glCore:tindex 2))
143-
(u8vector-set! glCore:carray (fx+ glCore:cindex 0) glCore:red)
144-
(u8vector-set! glCore:carray (fx+ glCore:cindex 1) glCore:green)
145-
(u8vector-set! glCore:carray (fx+ glCore:cindex 2) glCore:blue)
146-
(u8vector-set! glCore:carray (fx+ glCore:cindex 3) glCore:alpha)
147-
(set! glCore:cindex (fx+ glCore:cindex 4))
148-
(set! glCore:use3D #f)
149-
))
134+
(let* ((txx (pair? xtra))
135+
(tx (if txx (flo (car xtra)) 0.5))
136+
(ty (cond
137+
((not txx) 0.5)
138+
((let ((r (cdr xtra)))
139+
(and (pair? r) (car r))))
140+
(else 0.5))))
141+
(let ((x (flo x0)) (y (flo y0)))
142+
(f32vector-set! glCore:varray (fx+ glCore:vindex 0) x)
143+
(f32vector-set! glCore:varray (fx+ glCore:vindex 1) y)
144+
(set! glCore:vindex (fx+ glCore:vindex 2))
145+
(f32vector-set! glCore:tarray (fx+ glCore:tindex 0) tx)
146+
(f32vector-set! glCore:tarray (fx+ glCore:tindex 1) ty)
147+
(set! glCore:tindex (fx+ glCore:tindex 2))
148+
(u8vector-set! glCore:carray (fx+ glCore:cindex 0) glCore:red)
149+
(u8vector-set! glCore:carray (fx+ glCore:cindex 1) glCore:green)
150+
(u8vector-set! glCore:carray (fx+ glCore:cindex 2) glCore:blue)
151+
(u8vector-set! glCore:carray (fx+ glCore:cindex 3) glCore:alpha)
152+
(set! glCore:cindex (fx+ glCore:cindex 4))
153+
(set! glCore:use3D #f)
154+
)))
150155

151156
;; ------------------------------------------
152157
;; 3D rendering
153158

154159
(define (glCoreVertex3f x0 y0 z0 . xtra)
155-
(let ((x (flo x0)) (y (flo y0)) (z (flo z0))
156-
(tx (if (fx= (length xtra) 2) (flo (car xtra)) 0.5))
157-
(ty (if (fx= (length xtra) 2) (flo (cadr xtra)) 0.5)))
158-
(f32vector-set! glCore:varray3D (fx+ glCore:vindex 0) x)
159-
(f32vector-set! glCore:varray3D (fx+ glCore:vindex 1) y)
160-
(f32vector-set! glCore:varray3D (fx+ glCore:vindex 2) z)
161-
(set! glCore:vindex (fx+ glCore:vindex 3))
162-
(f32vector-set! glCore:tarray (fx+ glCore:tindex 0) tx)
163-
(f32vector-set! glCore:tarray (fx+ glCore:tindex 1) ty)
164-
(set! glCore:tindex (fx+ glCore:tindex 2))
165-
(u8vector-set! glCore:carray (fx+ glCore:cindex 0) glCore:red)
166-
(u8vector-set! glCore:carray (fx+ glCore:cindex 1) glCore:green)
167-
(u8vector-set! glCore:carray (fx+ glCore:cindex 2) glCore:blue)
168-
(u8vector-set! glCore:carray (fx+ glCore:cindex 3) glCore:alpha)
169-
(set! glCore:cindex (fx+ glCore:cindex 4))
170-
(set! glCore:use3D #t)
171-
))
160+
(let* ((txx (pair? xtra))
161+
(tx (if txx (flo (car xtra)) 0.5))
162+
(ty (cond
163+
((not txx) 0.5)
164+
((let ((r (cdr xtra)))
165+
(and (pair? r) (car r))))
166+
(else 0.5))))
167+
(let ((x (flo x0)) (y (flo y0)) (z (flo z0)))
168+
(f32vector-set! glCore:varray3D (fx+ glCore:vindex 0) x)
169+
(f32vector-set! glCore:varray3D (fx+ glCore:vindex 1) y)
170+
(f32vector-set! glCore:varray3D (fx+ glCore:vindex 2) z)
171+
(set! glCore:vindex (fx+ glCore:vindex 3))
172+
(f32vector-set! glCore:tarray (fx+ glCore:tindex 0) tx)
173+
(f32vector-set! glCore:tarray (fx+ glCore:tindex 1) ty)
174+
(set! glCore:tindex (fx+ glCore:tindex 2))
175+
(u8vector-set! glCore:carray (fx+ glCore:cindex 0) glCore:red)
176+
(u8vector-set! glCore:carray (fx+ glCore:cindex 1) glCore:green)
177+
(u8vector-set! glCore:carray (fx+ glCore:cindex 2) glCore:blue)
178+
(u8vector-set! glCore:carray (fx+ glCore:cindex 3) glCore:alpha)
179+
(set! glCore:cindex (fx+ glCore:cindex 4))
180+
(set! glCore:use3D #t)
181+
)))
172182

173183
;; ----------------------------------
174184
;; textures
@@ -180,18 +190,21 @@ SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
180190

181191
(define (glCoreTextureCreate w h data . aux)
182192
(glcore:log 5 "glCoreTextureCreate")
183-
(let ((idx glCore:tidx)
184-
(pixeltype (cond
185-
((fx= (u8vector-length data) (* w h)) GL_ALPHA)
186-
((fx= (u8vector-length data) (* 3 w h)) GL_RGB)
187-
((fx= (u8vector-length data) (* 4 w h)) GL_RGBA)
188-
(else (log-error "glCoreTextureCreate: Invalid data range") #f)))
189-
(interpolation (if (>= (length aux) 1) (car aux) GL_LINEAR))
190-
(wrap (if (>= (length aux) 2) (cadr aux) GL_CLAMP)))
191-
(table-set! glCore:textures idx (##still-copy
192-
(vector #f (u32vector 0) w h (##still-copy data) pixeltype interpolation wrap)))
193-
(set! glCore:tidx (fx+ glCore:tidx 1))
194-
idx))
193+
(let* ((o1x (pair? aux))
194+
(o2 (and o1x (cdr aux))))
195+
(let ((idx glCore:tidx)
196+
(pixeltype
197+
(cond
198+
((fx= (u8vector-length data) (* w h)) GL_ALPHA)
199+
((fx= (u8vector-length data) (* 3 w h)) GL_RGB)
200+
((fx= (u8vector-length data) (* 4 w h)) GL_RGBA)
201+
(else (log-error "glCoreTextureCreate: Invalid data range") #f)))
202+
(interpolation (if o1x (car aux) GL_LINEAR))
203+
(wrap (if (pair? o2) (car o2) GL_CLAMP)))
204+
(table-set! glCore:textures idx
205+
(##still-copy (vector #f (u32vector 0) w h (##still-copy data) pixeltype interpolation wrap)))
206+
(set! glCore:tidx (fx+ glCore:tidx 1))
207+
idx)))
195208

196209
;; return texture width
197210
(define (glCoreTextureWidth t)
@@ -226,19 +239,24 @@ SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
226239
;; (glCoreClipPush x1 y1 x2 y2)
227240
(define (glCoreClipPush . coords)
228241
(let* ((oldlist glcore:cliplist)
229-
(newcoords (if (fx= (length coords) 4) (map flo
230-
(list (min (car coords) (caddr coords)) (min (cadr coords) (cadddr coords))
231-
(max (car coords) (caddr coords)) (max (cadr coords) (cadddr coords)))) #f))
232-
(newlist (if newcoords (append (list newcoords) oldlist)
233-
(if (null? oldlist) oldlist (cdr oldlist)))))
234-
(if (not (null? newlist)) (begin
235-
(set! glcore:clipx1 (car (car newlist)))
236-
(set! glcore:clipy1 (cadr (car newlist)))
237-
(set! glcore:clipx2 (caddr (car newlist)))
238-
(set! glcore:clipy2 (cadddr (car newlist)))
239-
))
240-
(set! glcore:cliplist newlist)
241-
))
242+
(newcoords
243+
(if (fx= (length coords) 4)
244+
(map flo
245+
(list (min (car coords) (caddr coords))
246+
(min (cadr coords) (cadddr coords))
247+
(max (car coords) (caddr coords))
248+
(max (cadr coords) (cadddr coords))))
249+
#f))
250+
(newlist (if newcoords
251+
(append (list newcoords) oldlist)
252+
(if (null? oldlist) oldlist (cdr oldlist)))))
253+
(if (not (null? newlist))
254+
(begin
255+
(set! glcore:clipx1 (car (car newlist)))
256+
(set! glcore:clipy1 (cadr (car newlist)))
257+
(set! glcore:clipx2 (caddr (car newlist)))
258+
(set! glcore:clipy2 (cadddr (car newlist)))))
259+
(set! glcore:cliplist newlist)))
242260

243261
(define glCoreClipPop glCoreClipPush)
244262

@@ -252,13 +270,20 @@ SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
252270
(if entry
253271
(let ((w (flo (if (fx= (fix w0) 0) (vector-ref entry 2) w0)))
254272
(h (flo (if (fx= (fix h0) 0) (vector-ref entry 3) h0))))
255-
(if (null? glcore:cliplist)
256-
(apply glCore:TextureDrawUnClipped (append (list (flo x) (flo y) w h t (flo x1) (flo y1) (flo x2) (flo y2) (flo r))
257-
(if (null? colors) '() (car colors))))
258-
(apply glCore:TextureDrawClipped (append (list (flo x) (flo y) w h t (flo x1) (flo y1) (flo x2) (flo y2) (flo r))
259-
(if (null? colors) '() (car colors)))))
260-
) (log-error "glCoreTextureDraw: unbound index " t))
261-
))
273+
(if (null? glcore:cliplist)
274+
(if (pair? colors)
275+
(glCore:TextureDrawUnClipped
276+
(flo x) (flo y) w h t (flo x1) (flo y1) (flo x2) (flo y2) (flo r)
277+
(car colors))
278+
(glCore:TextureDrawUnClipped
279+
(flo x) (flo y) w h t (flo x1) (flo y1) (flo x2) (flo y2) (flo r)))
280+
(if (pair? colors)
281+
(glCore:TextureDrawClipped
282+
(flo x) (flo y) w h t (flo x1) (flo y1) (flo x2) (flo y2) (flo r)
283+
(car colors))
284+
(glCore:TextureDrawClipped
285+
(flo x) (flo y) w h t (flo x1) (flo y1) (flo x2) (flo y2) (flo r)))))
286+
(log-error "glCoreTextureDraw: unbound index " t))))
262287

263288
(define (glCore:TextureDrawUnClipped x y w h t @x1 @y1 @x2 @y2 r . colors)
264289
(glcore:log 5 "glCoreTextureDrawUnclipped enter")
@@ -309,22 +334,24 @@ SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
309334
(glRotatef r 0. 0. 1.)
310335
(_glCoreTextureBind t)
311336
(glCoreBegin GL_TRIANGLE_STRIP)
312-
(if (null? colors) (begin
313-
(glCoreVertex2f (fl- cw2) ch2 c@x1 c@y2)
314-
(glCoreVertex2f cw2 ch2 c@x2 c@y2)
315-
(glCoreVertex2f (fl- cw2) (fl- ch2) c@x1 c@y1)
316-
(glCoreVertex2f cw2 (fl- ch2) c@x2 c@y1)
317-
) (begin
318-
;; TODO: color interpolation here!
319-
(glCoreColor (car colors))
320-
(glCoreVertex2f (fl- cw2) ch2 c@x1 c@y2)
321-
(glCoreColor (cadr colors))
322-
(glCoreVertex2f cw2 ch2 c@x2 c@y2)
323-
(glCoreColor (caddr colors))
324-
(glCoreVertex2f (fl- cw2) (fl- ch2) c@x1 c@y1)
325-
(glCoreColor (cadddr colors))
326-
(glCoreVertex2f cw2 (fl- ch2) c@x2 c@y1)
327-
))
337+
(if (null? colors)
338+
(begin
339+
(glCoreVertex2f (fl- cw2) ch2 c@x1 c@y2)
340+
(glCoreVertex2f cw2 ch2 c@x2 c@y2)
341+
(glCoreVertex2f (fl- cw2) (fl- ch2) c@x1 c@y1)
342+
(glCoreVertex2f cw2 (fl- ch2) c@x2 c@y1)
343+
)
344+
(let ((colors (list->vector colors)))
345+
;; TODO: color interpolation here!
346+
(glCoreColor (vector-ref colors 0))
347+
(glCoreVertex2f (fl- cw2) ch2 c@x1 c@y2)
348+
(glCoreColor (vector-ref colors 1))
349+
(glCoreVertex2f cw2 ch2 c@x2 c@y2)
350+
(glCoreColor (vector-ref colors 2))
351+
(glCoreVertex2f (fl- cw2) (fl- ch2) c@x1 c@y1)
352+
(glCoreColor (vector-ref colors 3))
353+
(glCoreVertex2f cw2 (fl- ch2) c@x2 c@y1)
354+
))
328355
(glCoreEnd)
329356
(glPopMatrix)
330357
)))
@@ -338,24 +365,25 @@ SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
338365
(glcore:log 5 "glCoreTexturePolygonDraw")
339366
(let ((entry (table-ref glCore:textures t #f)))
340367
(if entry
341-
(let* ((cx (flo _cx)) (cy (flo _cy))
342-
(r (flo _r)))
368+
(let* ((cx (flo _cx)) (cy (flo _cy)) (r (flo _r)))
343369
(glPushMatrix)
344370
(glTranslatef cx cy 0.)
345371
(glRotatef r 0. 0. 1.)
346372
(_glCoreTextureBind t)
347373
(glCoreBegin GL_TRIANGLE_STRIP)
348-
(for-each (lambda (p)
349-
(let* ((x (fl- (car p) cx))
350-
(y (fl- (cadr p) cy))
351-
(tx (caddr p))
352-
(ty (cadddr p)))
374+
(for-each
375+
(lambda (p)
376+
;; TBD: should accept vectoralikes as point
377+
(let* ((p (list->vector p))
378+
(x (fl- (vector-ref p 0) cx))
379+
(y (fl- (vector-ref p 1) cy))
380+
(tx (vector-ref p 2))
381+
(ty (vector-ref p 3)))
353382
(glCoreVertex2f x y tx ty)))
354-
points)
355-
(glCoreEnd)
356-
(glPopMatrix)
357-
) (log-error "glCoreTexturePolygonDraw: unbound index " t))
358-
))
383+
points)
384+
(glCoreEnd)
385+
(glPopMatrix))
386+
(log-error "glCoreTexturePolygonDraw: unbound index " t))))
359387

360388
;; update texture data (for dynamic textures)
361389
;; to use this, first modify data returned with glCoreTextureData..

0 commit comments

Comments
 (0)