summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--chickadee/render/sprite.scm67
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)