From 4ab013cfe28f815fe4dc28a317edb8ab11306a76 Mon Sep 17 00:00:00 2001 From: David Thompson Date: Sat, 2 Jan 2016 20:51:06 -0500 Subject: render: sprite-batch: Unbind VAO *before* mapping buffers. * sly/render/sprite-batch.scm (sprite-batch-flush): Don't worry about VAOs here. (with-sprite-batch): Unbind VAO before mapping vertex buffers. --- sly/render/sprite-batch.scm | 56 +++++++++++++++++++++++---------------------- 1 file changed, 29 insertions(+), 27 deletions(-) diff --git a/sly/render/sprite-batch.scm b/sly/render/sprite-batch.scm index bd008cc..7caef67 100644 --- a/sly/render/sprite-batch.scm +++ b/sly/render/sprite-batch.scm @@ -223,37 +223,39 @@ (define (sprite-batch-flush! batch context) "Render the contents of BATCH and clear the cache." (unless (zero? (sprite-batch-size batch)) - (graphics-mesh-excursion context + (graphics-texture-excursion context (lambda (context) - (set-graphics-mesh! context null-mesh) - (graphics-texture-excursion context + (set-graphics-texture! context (sprite-batch-texture batch)) + (graphics-model-view-excursion context (lambda (context) - (set-graphics-texture! context (sprite-batch-texture batch)) - (graphics-model-view-excursion context + (graphics-model-view-mul! context + (graphics-projection-transform context)) + (graphics-uniform-excursion context + `((mvp ,(graphics-model-view-transform context)) + (texture? ,(not (texture-null? (graphics-texture context))))) (lambda (context) - (graphics-model-view-mul! context - (graphics-projection-transform context)) - (graphics-uniform-excursion context - `((mvp ,(graphics-model-view-transform context)) - (texture? ,(not (texture-null? (graphics-texture context))))) - (lambda (context) - (unmap-vertex-buffer! (sprite-batch-index-buffer batch)) - (unmap-vertex-buffer! (sprite-batch-position-buffer batch)) - (unmap-vertex-buffer! (sprite-batch-texture-buffer batch)) + (unmap-vertex-buffer! (sprite-batch-index-buffer batch)) + (unmap-vertex-buffer! (sprite-batch-position-buffer batch)) + (unmap-vertex-buffer! (sprite-batch-texture-buffer batch)) - (glBindVertexArray (sprite-batch-vao batch)) - (glDrawElements (begin-mode triangles) - ;; 6 indices per sprite. - (* (sprite-batch-size batch) 6) - (data-type unsigned-int) - %null-pointer) - (glBindVertexArray 0) + (glBindVertexArray (sprite-batch-vao batch)) + (glDrawElements (begin-mode triangles) + ;; 6 indices per sprite. + (* (sprite-batch-size batch) 6) + (data-type unsigned-int) + %null-pointer) + (glBindVertexArray 0) - (sprite-batch-reset! batch))))))))))) + (sprite-batch-reset! batch))))))))) (define-syntax-rule (with-sprite-batch batch context body ...) - (begin - (sprite-batch-reset! batch) - (sprite-batch-begin! batch) - body ... - (sprite-batch-flush! batch context))) + ;; IMPORTANT! We need to make sure that the current VAO is unbound + ;; before we start mapping/unmapping vertex buffers. Not doing this + ;; created a nasty bug that took me a long time to find. + (graphics-mesh-excursion context + (lambda (context) + (set-graphics-mesh! context null-mesh) + (sprite-batch-reset! batch) + (sprite-batch-begin! batch) + body ... + (sprite-batch-flush! batch context)))) -- cgit v1.2.3