diff options
-rw-r--r-- | chickadee/graphics/buffer.scm | 156 |
1 files changed, 78 insertions, 78 deletions
diff --git a/chickadee/graphics/buffer.scm b/chickadee/graphics/buffer.scm index 369ec7b..5ee12da 100644 --- a/chickadee/graphics/buffer.scm +++ b/chickadee/graphics/buffer.scm @@ -275,84 +275,6 @@ resized." ;;; -;;; Dynamic Buffers -;;; - -;; A layer on top of vertex buffers to handle buffer streaming with -;; dynamic buffer expansion. -(define-record-type <dynamic-buffer> - (%make-dynamic-buffer buffer capacity count) - dynamic-buffer? - (buffer dynamic-buffer->buffer) - (data dynamic-buffer-data set-dynamic-buffer-data!) - (capacity dynamic-buffer-capacity set-dynamic-buffer-capacity!) - (count dynamic-buffer-count set-dynamic-buffer-count!)) - -(define* (make-dynamic-buffer #:key name capacity stride usage (target 'vertex)) - (let* ((buffer (make-buffer #f - #:name name - #:length (* capacity stride) - #:stride stride - #:usage usage - #:target target))) - (%make-dynamic-buffer buffer capacity 0))) - -(define-inlinable (dynamic-buffer-bounds-check dbuffer i) - (unless (< i (dynamic-buffer-count dbuffer)) - (error "index out of bounds" i))) - -(define (expand-dynamic-buffer dbuffer) - (let ((new-capacity (inexact->exact - (round (* (dynamic-buffer-capacity dbuffer) 1.5)))) - (buffer (dynamic-buffer->buffer dbuffer))) - (resize-buffer! buffer (* new-capacity (buffer-stride buffer))) - (set-dynamic-buffer-capacity! dbuffer new-capacity) - (set-dynamic-buffer-data! dbuffer (buffer-data buffer)))) - -(define-inlinable (dynamic-buffer-next! dbuffer n) - (let ((count (dynamic-buffer-count dbuffer))) - (let resize () - (let ((capacity (dynamic-buffer-capacity dbuffer))) - (when (> (+ count n) capacity) - (expand-dynamic-buffer dbuffer) - (resize)))) - (set-dynamic-buffer-count! dbuffer (+ count n)) - count)) - -(define (dynamic-buffer-clear! dbuffer) - (set-dynamic-buffer-count! dbuffer 0)) - -(define (dynamic-buffer-map! dbuffer) - (let ((buffer (dynamic-buffer->buffer dbuffer))) - (dynamic-buffer-clear! dbuffer) - (map-buffer! buffer 'write-only) - ;; Stashing the bytevector here turns out to be a *huge* performance - ;; booster. Probably because it's avoiding another layer of record - ;; type checks and stuff? I dunno. - (set-dynamic-buffer-data! dbuffer (buffer-data buffer)))) - -(define (dynamic-buffer-unmap! dbuffer) - (unmap-buffer! (dynamic-buffer->buffer dbuffer)) - (set-dynamic-buffer-data! dbuffer #f)) - -(define (dynamic-buffer-import! dbuffer bv start end) - (let ((stride (buffer-stride (dynamic-buffer->buffer dbuffer))) - (copy-count (- end start))) - (let resize () - (let ((capacity (dynamic-buffer-capacity dbuffer))) - (when (< capacity copy-count) - (begin - (expand-dynamic-buffer dbuffer) - (resize))))) - (bytevector-copy! bv - (* start stride) - (dynamic-buffer-data dbuffer) - 0 - (* copy-count stride)) - (set-dynamic-buffer-count! dbuffer copy-count))) - - -;;; ;;; Vertex Attributes ;;; @@ -618,6 +540,84 @@ argument may be overridden. The following values are supported: ;;; +;;; Dynamic Buffers +;;; + +;; A layer on top of vertex buffers to handle buffer streaming with +;; dynamic buffer expansion. +(define-record-type <dynamic-buffer> + (%make-dynamic-buffer buffer capacity count) + dynamic-buffer? + (buffer dynamic-buffer->buffer) + (data dynamic-buffer-data set-dynamic-buffer-data!) + (capacity dynamic-buffer-capacity set-dynamic-buffer-capacity!) + (count dynamic-buffer-count set-dynamic-buffer-count!)) + +(define* (make-dynamic-buffer #:key name capacity stride usage (target 'vertex)) + (let* ((buffer (make-buffer #f + #:name name + #:length (* capacity stride) + #:stride stride + #:usage usage + #:target target))) + (%make-dynamic-buffer buffer capacity 0))) + +(define-inlinable (dynamic-buffer-bounds-check dbuffer i) + (unless (< i (dynamic-buffer-count dbuffer)) + (error "index out of bounds" i))) + +(define (expand-dynamic-buffer dbuffer) + (let ((new-capacity (inexact->exact + (round (* (dynamic-buffer-capacity dbuffer) 1.5)))) + (buffer (dynamic-buffer->buffer dbuffer))) + (resize-buffer! buffer (* new-capacity (buffer-stride buffer))) + (set-dynamic-buffer-capacity! dbuffer new-capacity) + (set-dynamic-buffer-data! dbuffer (buffer-data buffer)))) + +(define-inlinable (dynamic-buffer-next! dbuffer n) + (let ((count (dynamic-buffer-count dbuffer))) + (let resize () + (let ((capacity (dynamic-buffer-capacity dbuffer))) + (when (> (+ count n) capacity) + (expand-dynamic-buffer dbuffer) + (resize)))) + (set-dynamic-buffer-count! dbuffer (+ count n)) + count)) + +(define (dynamic-buffer-clear! dbuffer) + (set-dynamic-buffer-count! dbuffer 0)) + +(define (dynamic-buffer-map! dbuffer) + (let ((buffer (dynamic-buffer->buffer dbuffer))) + (dynamic-buffer-clear! dbuffer) + (map-buffer! buffer 'write-only) + ;; Stashing the bytevector here turns out to be a *huge* performance + ;; booster. Probably because it's avoiding another layer of record + ;; type checks and stuff? I dunno. + (set-dynamic-buffer-data! dbuffer (buffer-data buffer)))) + +(define (dynamic-buffer-unmap! dbuffer) + (unmap-buffer! (dynamic-buffer->buffer dbuffer)) + (set-dynamic-buffer-data! dbuffer #f)) + +(define (dynamic-buffer-import! dbuffer bv start end) + (let ((stride (buffer-stride (dynamic-buffer->buffer dbuffer))) + (copy-count (- end start))) + (let resize () + (let ((capacity (dynamic-buffer-capacity dbuffer))) + (when (< capacity copy-count) + (begin + (expand-dynamic-buffer dbuffer) + (resize))))) + (bytevector-copy! bv + (* start stride) + (dynamic-buffer-data dbuffer) + 0 + (* copy-count stride)) + (set-dynamic-buffer-count! dbuffer copy-count))) + + +;;; ;;; Geometry Builder ;;; |