diff options
author | David Thompson <dthompson2@worcester.edu> | 2018-12-13 17:18:16 -0500 |
---|---|---|
committer | David Thompson <dthompson2@worcester.edu> | 2018-12-13 17:25:31 -0500 |
commit | f6e11d5e73a0f61a59f50b49817779f9fb520e15 (patch) | |
tree | ce69a559df35f8ef924aed7013e84f4d260c815d | |
parent | 61f4554f47e342bb9eb1f2754586e55fd458bec7 (diff) |
render: sprite: batch: Combine vertices and texcoords into one buffer.
Not sure how much of a gain this is, but it's one less buffer to map
each frame.
* chickadee/render/sprite.scm (init-sprite-batch): Create combined
vertex/texcoord buffer.
(double-sprite-batch-size!): Map and copy only a single buffer.
(sprite-batch-begin!): Map just one buffer.
(sprite-batch-add!): Use new packed buffer layout.
-rw-r--r-- | chickadee/render/sprite.scm | 67 |
1 files changed, 36 insertions, 31 deletions
diff --git a/chickadee/render/sprite.scm b/chickadee/render/sprite.scm index ccb731c..8ccd32b 100644 --- a/chickadee/render/sprite.scm +++ b/chickadee/render/sprite.scm @@ -148,10 +148,23 @@ void main (void) { 'unsigned-int (* capacity 6) #:target 'index)) - (pos (make-streaming-typed-buffer 'vec2 'float (* capacity 4) - #:name "batched-sprite-vertices")) - (tex (make-streaming-typed-buffer 'vec2 'float (* capacity 4) - #:name "batched-sprite-vertices")) + (stride 16) ; 4 f32s, 2 for vertex, 2 for texcoord + (buffer (make-buffer #f + #:name "sprite batch buffer" + #:length (* capacity stride 4) + #:stride stride + #:usage 'stream)) + (pos (make-typed-buffer #:name "sprite batches vertices" + #:buffer buffer + #:type 'vec2 + #:component-type 'float + #:length (* capacity 4))) + (tex (make-typed-buffer #:name "batched-sprite-vertices" + #:buffer buffer + #:type 'vec2 + #:component-type 'float + #:length (* capacity 4) + #:offset (/ stride 2))) (va (make-vertex-array #:indices index #:attributes `((0 . ,pos) (1 . ,tex))))) (set-sprite-batch-capacity! batch capacity) @@ -172,25 +185,20 @@ void main (void) { (define (double-sprite-batch-size! batch) (let* ((old-index (sprite-batch-index-buffer batch)) (old-verts (sprite-batch-position-buffer batch)) - (old-tex (sprite-batch-texture-buffer batch)) (old-index-data (typed-buffer-data old-index)) - (old-verts-data (typed-buffer-data old-verts)) - (old-tex-data (typed-buffer-data old-tex))) + (old-vertex-data (typed-buffer-data old-verts))) (unmap-typed-buffer! old-index) (unmap-typed-buffer! old-verts) - (unmap-typed-buffer! old-tex) (init-sprite-batch batch (* (sprite-batch-capacity batch) 2)) (sprite-batch-begin! batch) (let ((new-index (sprite-batch-index-buffer batch)) - (new-verts (sprite-batch-position-buffer batch)) - (new-tex (sprite-batch-texture-buffer batch))) + (new-verts (sprite-batch-position-buffer batch))) (define (copy from to) (bytevector-copy! from 0 (typed-buffer-data to) 0 (bytevector-length from))) (copy old-index-data new-index) - (copy old-verts-data new-verts) - (copy old-tex-data new-tex)))) + (copy old-vertex-data new-verts)))) (define (sprite-batch-reset! batch) "Reset BATCH to size 0." @@ -201,8 +209,7 @@ void main (void) { (define (sprite-batch-begin! batch) (map-typed-buffer! (sprite-batch-index-buffer batch)) - (map-typed-buffer! (sprite-batch-position-buffer batch)) - (map-typed-buffer! (sprite-batch-texture-buffer batch))) + (map-typed-buffer! (sprite-batch-position-buffer batch))) (define (sprite-batch-flush! batch) "Render the contents of BATCH and clear the cache." @@ -211,7 +218,6 @@ void main (void) { (with-texture 0 (sprite-batch-texture batch) (unmap-typed-buffer! (sprite-batch-index-buffer batch)) (unmap-typed-buffer! (sprite-batch-position-buffer batch)) - (unmap-typed-buffer! (sprite-batch-texture-buffer batch)) (gpu-apply* (sprite-batch-shader batch) (sprite-batch-vertex-array batch) (* (sprite-batch-size batch) 6) @@ -274,33 +280,32 @@ void main (void) { (u32vector-set! indices (+ (offset) 4) (+ index-vertex-offset 2)) (u32vector-set! indices (+ (offset) 5) (+ index-vertex-offset 1))) ;; Add vertices. - (set-offset (* size 8)) ;; 4 vertices, 2 floats per vertex + (set-offset (* size 16)) ;; Bottom-left (f32vector-set! vertices (offset) (vec2-x world1)) (f32vector-set! vertices (+ (offset) 1) (vec2-y world1)) ;; Bottom-right - (f32vector-set! vertices (+ (offset) 2) (vec2-x world2)) - (f32vector-set! vertices (+ (offset) 3) (vec2-y world2)) + (f32vector-set! vertices (+ (offset) 4) (vec2-x world2)) + (f32vector-set! vertices (+ (offset) 5) (vec2-y world2)) ;; Top-right - (f32vector-set! vertices (+ (offset) 4) (vec2-x world3)) - (f32vector-set! vertices (+ (offset) 5) (vec2-y world3)) + (f32vector-set! vertices (+ (offset) 8) (vec2-x world3)) + (f32vector-set! vertices (+ (offset) 9) (vec2-y world3)) ;; Top-left - (f32vector-set! vertices (+ (offset) 6) (vec2-x world4)) - (f32vector-set! vertices (+ (offset) 7) (vec2-y world4)) + (f32vector-set! vertices (+ (offset) 12) (vec2-x world4)) + (f32vector-set! vertices (+ (offset) 13) (vec2-y world4)) ;; Add texture coordinates. - (set-offset (* size 8)) ;; Bottom-left - (f32vector-set! texcoords (offset) s1) - (f32vector-set! texcoords (+ (offset) 1) t2) - ;; Bottom-right - (f32vector-set! texcoords (+ (offset) 2) s2) + (f32vector-set! texcoords (+ (offset) 2) s1) (f32vector-set! texcoords (+ (offset) 3) t2) + ;; Bottom-right + (f32vector-set! texcoords (+ (offset) 6) s2) + (f32vector-set! texcoords (+ (offset) 7) t2) ;; Top-right - (f32vector-set! texcoords (+ (offset) 4) s2) - (f32vector-set! texcoords (+ (offset) 5) t1) + (f32vector-set! texcoords (+ (offset) 10) s2) + (f32vector-set! texcoords (+ (offset) 11) t1) ;; Top-left - (f32vector-set! texcoords (+ (offset) 6) s1) - (f32vector-set! texcoords (+ (offset) 7) t1) + (f32vector-set! texcoords (+ (offset) 14) s1) + (f32vector-set! texcoords (+ (offset) 15) t1) (set-sprite-batch-size! batch (1+ size))))))) (define *batch?* #f) |