render: buffer: Don't attempt to map a buffer that is already mapped.
authorDavid Thompson <dthompson2@worcester.edu>
Thu, 9 May 2019 20:27:44 +0000 (16:27 -0400)
committerDavid Thompson <dthompson2@worcester.edu>
Thu, 9 May 2019 20:27:44 +0000 (16:27 -0400)
* chickadee/render/buffer (map-buffer!): Do nothing if buffer is
  already mapped to avoid throwing an error.

chickadee/render/buffer.scm

index 5285be3..04598df 100644 (file)
@@ -207,16 +207,17 @@ for streaming data to the GPU every frame."
 the vertex buffer to be updated with new vertex data.  The
 'unmap-buffer!' procedure must be called to submit the new
 vertex buffer data back to the GPU."
-  (let ((target (buffer-target-gl buffer))
-        (length (buffer-length buffer))
-        (usage (buffer-usage-gl buffer)))
-    (gpu-state-set! *buffer-state* buffer)
-    (when (eq? usage 'stream)
-      ;; Orphan the buffer to avoid implicit synchronization.
-      ;; See: https://www.opengl.org/wiki/Buffer_Object_Streaming#Buffer_re-specification
-      (gl-buffer-data target length %null-pointer usage))
-    (let ((ptr (gl-map-buffer target (version-1-5 read-write))))
-      (set-buffer-data! buffer (pointer->bytevector ptr length)))))
+  (unless (buffer-mapped? buffer) ;; Don't map a buffer that is already mapped!
+    (let ((target (buffer-target-gl buffer))
+          (length (buffer-length buffer))
+          (usage (buffer-usage-gl buffer)))
+      (gpu-state-set! *buffer-state* buffer)
+      (when (eq? usage 'stream)
+        ;; Orphan the buffer to avoid implicit synchronization.
+        ;; See: https://www.opengl.org/wiki/Buffer_Object_Streaming#Buffer_re-specification
+        (gl-buffer-data target length %null-pointer usage))
+      (let ((ptr (gl-map-buffer target (version-1-5 read-write))))
+        (set-buffer-data! buffer (pointer->bytevector ptr length))))))
 
 (define (unmap-buffer! buffer)
   "Return the mapped vertex buffer data for BUFFER to the GPU."