render: texture: Support using 32 texture units.
[chickadee.git] / chickadee / render / shader.scm
index ddf2098..b5f9b7d 100644 (file)
@@ -42,6 +42,7 @@
             shader-uniforms
             uniform?
             uniform-name
+            uniform-type
             uniform-value
             uniform-default-value
             set-uniform-value!
     ;; ('int-vec2 (vector2 0 0))
     ;; ('int-vec3 (vector3 0 0 0))
     ;; ('int-vec4 (vector4 0 0 0 0))
-    ('sampler-2d 0)
     ('mat4 %default-mat4)))
 
 (define (uniform-setter-for-type type)
 (define (extract-uniforms id)
   (let ((total (uniform-count id))
         (table (make-hash-table)))
-    (let loop ((i 0))
+    (let loop ((i 0)
+               (texture-unit 0))
       (unless (= i total)
         (let ((length-bv (make-u32vector 1))
               (size-bv (make-u32vector 1))
                  (location (gl-get-uniform-location id name))
                  (size (u32vector-ref size-bv 0))
                  (type (gl-type->symbol (u32vector-ref type-bv 0)))
-                 (default (default-uniform-value type))
+                 (sampler? (eq? type 'sampler-2d))
+                 (default (if sampler?
+                              texture-unit
+                              (default-uniform-value type)))
                  (setter (uniform-setter-for-type type)))
             ;; TODO: Handle uniform arrays.
             (unless (= size 1)
               (error "unsupported uniform size" name size))
 
-            (unless (eq? type 'sampler-2d)
-              (hash-set! table
-                         name
-                         (make-uniform name location type default setter)))))
-        (loop (1+ i))))
+            (hash-set! table
+                       name
+                       (make-uniform name location type default setter))
+            (loop (1+ i)
+                  (if sampler?
+                      (1+ texture-unit)
+                      texture-unit))))))
     table))
 
 (define (attribute-count id)