render: texture: Support using 32 texture units.
[chickadee.git] / chickadee / render / texture.scm
index 20f58a6..65e2ba5 100644 (file)
@@ -45,7 +45,8 @@
             texture-wrap-s
             texture-wrap-t
             null-texture
-            *texture-state*
+            texture-set!
+            texture-ref
 
             texture-atlas
             list->texture-atlas
 (define-method (gpu-finalize (texture <<texture>>))
   (free-texture texture))
 
-(define (apply-texture texture)
-  (gl-enable (enable-cap texture-2d))
-  (gl-bind-texture (texture-target texture-2d)
-                   (texture-id texture)))
+(define (make-apply-texture n)
+  (let ((texture-unit (+ (version-1-3 texture0) n)))
+    (lambda (texture)
+      (set-gl-active-texture texture-unit)
+      (gl-bind-texture (texture-target texture-2d)
+                       (texture-id texture)))))
 
-(define *texture-state* (make-gpu-state apply-texture null-texture))
+(define *texture-states*
+  (let ((states (make-vector 32)))
+    (let loop ((i 0))
+      (if (< i 32)
+          (begin
+            (vector-set! states i (make-gpu-state (make-apply-texture i)
+                                                  null-texture))
+            (loop (1+ i)))
+          states))))
+
+(define (texture-ref! n)
+  (gpu-state-ref (vector-ref *texture-states* n)))
+
+(define (texture-set! n texture)
+  (gpu-state-set! (vector-ref *texture-states* n) texture))
 
 (define* (make-texture pixels width height #:key
                        (min-filter 'linear)
@@ -127,7 +144,7 @@ clamp-to-edge.  FORMAT specifies the pixel format.  Currently only
   (let ((texture (gpu-guard
                   (%make-texture (gl-generate-texture) width height
                                  min-filter mag-filter wrap-s wrap-t))))
-    (gpu-state-set! *texture-state* texture)
+    (texture-set! 0 texture)
     (gl-texture-parameter (texture-target texture-2d)
                           (texture-parameter-name texture-min-filter)
                           (match min-filter