render: sprite: Use a single buffer for unbatched sprite mesh.
authorDavid Thompson <dthompson2@worcester.edu>
Thu, 13 Dec 2018 22:32:07 +0000 (17:32 -0500)
committerDavid Thompson <dthompson2@worcester.edu>
Thu, 13 Dec 2018 22:32:07 +0000 (17:32 -0500)
* chickadee/render/sprite.scm (draw-sprite-unbatched): Combine
vertices and texcoords into a single packed buffer.

chickadee/render/sprite.scm

index 8ccd32b..c9a777f 100644 (file)
@@ -61,59 +61,69 @@ void main (void) {
 ")))
 
 (define draw-sprite-unbatched
-  (let* ((position-buffer
+  (let* ((stride 16)            ; 4 f32s, 2 for vertex, 2 for texcoord
+         (buffer (delay
+                   (make-buffer #f
+                                #:name "unbatched sprite buffer"
+                                #:length (* stride 4)
+                                #:stride stride
+                                #:usage 'stream)))
+         (pos (delay
+                (make-typed-buffer #:name "unbatched sprite vertices"
+                                   #:buffer (force buffer)
+                                   #:type 'vec2
+                                   #:component-type 'float
+                                   #:length 4)))
+         (tex (delay
+                (make-typed-buffer #:name "unbatched sprite texcoords"
+                                   #:buffer (force buffer)
+                                   #:type 'vec2
+                                   #:component-type 'float
+                                   #:length 4
+                                   #:offset 8)))
+         (indices
           (delay
-            (make-streaming-typed-buffer 'vec2 'float 4
-                                         #:name "unbatched-sprite-vertices")))
-         (texcoord-buffer
-          (delay
-            (make-streaming-typed-buffer 'vec2 'float 4
-                                         #:name "unbatched-sprite-texcoords")))
-         (index-buffer
-          (delay
-            (make-typed-buffer #:name "unbatched-sprite-indices"
+            (make-typed-buffer #:name "unbatched sprite indices"
                                #:type 'scalar
                                #:component-type 'unsigned-int
                                #:buffer (make-buffer (u32vector 0 3 2 0 2 1)
                                                      #:target 'index))))
          (vertex-array
           (delay
-            (make-vertex-array #:indices (force index-buffer)
+            (make-vertex-array #:indices (force indices)
                                #:attributes
-                               `((0 . ,(force position-buffer))
-                                 (1 . ,(force texcoord-buffer))))))
+                               `((0 . ,(force pos))
+                                 (1 . ,(force tex))))))
          (mvp (make-null-matrix4)))
     (lambda (texture region world-matrix blend-mode shader texture-region)
-      (with-mapped-typed-buffer (force position-buffer)
+      (with-mapped-typed-buffer (force pos)
         (let* ((x1 (rect-x region))
                (y1 (rect-y region))
                (x2 (+ x1 (rect-width region)))
                (y2 (+ y1 (rect-height region)))
-               (bv (typed-buffer-data (force position-buffer))))
+               (s1 (rect-x texture-region))
+               (t1 (rect-y texture-region))
+               (s2 (+ (rect-x texture-region) (rect-width texture-region)))
+               (t2 (+ (rect-y texture-region) (rect-height texture-region)))
+               (bv (typed-buffer-data (force pos))))
+          ;; Texture origin is at the top-left, so we need to flip the Y
+          ;; coordinate relative to the vertices.
           (f32vector-set! bv 0 x1)
           (f32vector-set! bv 1 y1)
-          (f32vector-set! bv 2 x2)
-          (f32vector-set! bv 3 y1)
-          (f32vector-set! bv 4 x2)
-          (f32vector-set! bv 5 y2)
-          (f32vector-set! bv 6 x1)
-          (f32vector-set! bv 7 y2)))
-      (with-mapped-typed-buffer (force texcoord-buffer)
-        ;; Texture origin is at the top-left, so we need to flip the Y
-        ;; coordinate relative to the vertices.
-        (let ((s1 (rect-x texture-region))
-              (t1 (rect-y texture-region))
-              (s2 (+ (rect-x texture-region) (rect-width texture-region)))
-              (t2 (+ (rect-y texture-region) (rect-height texture-region)))
-              (bv (typed-buffer-data (force texcoord-buffer))))
-          (f32vector-set! bv 0 s1)
-          (f32vector-set! bv 1 t2)
-          (f32vector-set! bv 2 s2)
+          (f32vector-set! bv 2 s1)
           (f32vector-set! bv 3 t2)
-          (f32vector-set! bv 4 s2)
-          (f32vector-set! bv 5 t1)
-          (f32vector-set! bv 6 s1)
-          (f32vector-set! bv 7 t1)))
+          (f32vector-set! bv 4 x2)
+          (f32vector-set! bv 5 y1)
+          (f32vector-set! bv 6 s2)
+          (f32vector-set! bv 7 t2)
+          (f32vector-set! bv 8 x2)
+          (f32vector-set! bv 9 y2)
+          (f32vector-set! bv 10 s2)
+          (f32vector-set! bv 11 t1)
+          (f32vector-set! bv 12 x1)
+          (f32vector-set! bv 13 y2)
+          (f32vector-set! bv 14 s1)
+          (f32vector-set! bv 15 t1)))
       (with-blend-mode blend-mode
         (with-texture 0 texture
           (gpu-apply shader (force vertex-array)