From 78c685cf7dc4fb2a4e0fd279f203b52a34b2f8f8 Mon Sep 17 00:00:00 2001 From: David Thompson Date: Wed, 22 Mar 2017 22:21:00 -0400 Subject: render: sprite: Add fast path for non-rotated/scaled batched sprites. * chickadee/render/sprite.scm (sprite-batch-add!): Speed up rendering of non-rotated, non-scaled sprites by avoid matrix multiplication. --- chickadee/render/sprite.scm | 49 +++++++++++++++++++++++++++------------------ 1 file changed, 29 insertions(+), 20 deletions(-) diff --git a/chickadee/render/sprite.scm b/chickadee/render/sprite.scm index 2288edf..318a8a5 100644 --- a/chickadee/render/sprite.scm +++ b/chickadee/render/sprite.scm @@ -205,9 +205,9 @@ void main (void) { (define sprite-batch-add! (let ((tmp-matrix (make-null-matrix4)) (matrix (make-null-matrix4)) - (position (vec2 0 0)) - (world1 (vec2 0 0)) - (world2 (vec2 0 0)) + (position (vec2 0.0 0.0)) + (world1 (vec2 0.0 0.0)) + (world2 (vec2 0.0 0.0)) (offset-bv (make-u32vector 1))) (define (set-offset offset) (u32vector-set! offset-bv 0 offset)) @@ -235,6 +235,8 @@ void main (void) { (indices (vertex-buffer-data (sprite-batch-index-buffer batch))) (vertices (vertex-buffer-data (sprite-batch-position-buffer batch))) (texcoords (vertex-buffer-data (sprite-batch-texture-buffer batch))) + (rx (rect-x region)) + (ry (rect-y region)) (local-x1 0.0) (local-y1 0.0) (local-x2 (rect-width region)) @@ -243,23 +245,30 @@ void main (void) { (t1 (rect-bottom texture-region)) (s2 (rect-right texture-region)) (t2 (rect-top texture-region))) - (matrix4-identity! matrix) - (when rotation - (matrix4-rotate-z! tmp-matrix rotation) - (matrix4-mult! matrix matrix tmp-matrix)) - (when scale - (matrix4-scale! tmp-matrix scale) - (matrix4-mult! matrix matrix tmp-matrix)) - (set-vec2-x! position (rect-x region)) - (set-vec2-y! position (rect-y region)) - (matrix4-translate! tmp-matrix position) - (matrix4-mult! matrix matrix tmp-matrix) - (set-vec2-x! world1 local-x1) - (set-vec2-y! world1 local-y1) - (set-vec2-x! world2 local-x2) - (set-vec2-y! world2 local-y2) - (transform! matrix world1) - (transform! matrix world2) + (if (or rotation scale) + (begin + (matrix4-identity! matrix) + (when rotation + (matrix4-rotate-z! tmp-matrix rotation) + (matrix4-mult! matrix matrix tmp-matrix)) + (when scale + (matrix4-scale! tmp-matrix scale) + (matrix4-mult! matrix matrix tmp-matrix)) + (set-vec2-x! position rx) + (set-vec2-y! position ry) + (matrix4-translate! tmp-matrix position) + (matrix4-mult! matrix matrix tmp-matrix) + (set-vec2-x! world1 local-x1) + (set-vec2-y! world1 local-y1) + (set-vec2-x! world2 local-x2) + (set-vec2-y! world2 local-y2) + (transform! matrix world1) + (transform! matrix world2)) + (begin + (set-vec2-x! world1 (+ local-x1 rx)) + (set-vec2-y! world1 (+ local-y1 ry)) + (set-vec2-x! world2 (+ local-x2 rx)) + (set-vec2-y! world2 (+ local-y2 ry)))) (let ((world-x1 (vec2-x world1)) (world-y1 (vec2-y world1)) (world-x2 (vec2-x world2)) -- cgit v1.2.3