[Mesa-dev] [PATCH 08/11] glsl: Handle the binding qualifier for arrays of samplers correctly.

Kenneth Graunke kenneth at whitecape.org
Wed Jul 17 18:24:08 PDT 2013


Normally, uniform array variables are initialized by array literals.
That is, val->type->array_elements >= storage->array_elements.

However, samplers are different.  Consider a declaration such as:

   layout(binding = 5) uniform sampler2D[3];

The initializer value is a single integer (5), while the storage has 3
array elements.  The proper behavior here is to increment one for each
element; they should be initialized to 5, 6, and 7.

This patch introduces a new case for sampler types which handles both
arrays of samplers and single samplers correctly.  It removes the old
code for single samplers, since it's now dead code.

Signed-off-by: Kenneth Graunke <kenneth at whitecape.org>
---
 src/glsl/link_uniform_initializers.cpp | 37 ++++++++++++++++++++++------------
 1 file changed, 24 insertions(+), 13 deletions(-)

diff --git a/src/glsl/link_uniform_initializers.cpp b/src/glsl/link_uniform_initializers.cpp
index 3d8b181..f42b06b 100644
--- a/src/glsl/link_uniform_initializers.cpp
+++ b/src/glsl/link_uniform_initializers.cpp
@@ -121,7 +121,30 @@ set_uniform_initializer(void *mem_ctx, gl_shader_program *prog,
       return;
    }
 
-   if (val->type->is_array()) {
+   if (storage->type->is_sampler()) {
+      unsigned elements = MAX2(storage->array_elements, 1);
+
+      /* From section 4.4.4 of the GLSL 4.20 specification:
+       * "If the binding identifier is used with an array, the first element
+       *  of the array takes the specified unit and each subsequent element
+       *  takes the next consecutive unit."
+       */
+      for (unsigned int i = 0; i < elements; i++) {
+         storage->storage[i].i = val->value.i[0] + i;
+      }
+
+      for (int sh = 0; sh < MESA_SHADER_TYPES; sh++) {
+         gl_shader *shader = prog->_LinkedShaders[sh];
+
+         if (shader && storage->sampler[sh].active) {
+            for (unsigned i = 0; i < elements; i++) {
+               unsigned index = storage->sampler[sh].index + i;
+
+               shader->SamplerUnits[index] = storage->storage[i].i;
+            }
+         }
+      }
+   } else if (val->type->is_array()) {
       const enum glsl_base_type base_type =
 	 val->array_elements[0]->type->base_type;
       const unsigned int elements = val->array_elements[0]->type->components();
@@ -142,18 +165,6 @@ set_uniform_initializer(void *mem_ctx, gl_shader_program *prog,
 			       val,
 			       val->type->base_type,
 			       val->type->components());
-
-      if (storage->type->is_sampler()) {
-         for (int sh = 0; sh < MESA_SHADER_TYPES; sh++) {
-            gl_shader *shader = prog->_LinkedShaders[sh];
-
-            if (shader && storage->sampler[sh].active) {
-               unsigned index = storage->sampler[sh].index;
-
-               shader->SamplerUnits[index] = storage->storage[0].i;
-            }
-         }
-      }
    }
 
    storage->initialized = true;
-- 
1.8.3.2



More information about the mesa-dev mailing list