glsl: add support for initialising sampler AoA
authorTimothy Arceri <t_arceri@yahoo.com.au>
Wed, 29 Jul 2015 01:57:44 +0000 (11:57 +1000)
committerTimothy Arceri <t_arceri@yahoo.com.au>
Thu, 15 Oct 2015 09:35:40 +0000 (20:35 +1100)
Reviewed-by: Ian Romanick <ian.d.romanick@intel.com>
src/glsl/link_uniform_initializers.cpp

index f929acab1cefc8da60871ea7f326ba56a471b9fe..682a4eef13c5ef2fabeac7f7676106e0f66de387 100644 (file)
@@ -106,51 +106,64 @@ copy_constant_to_storage(union gl_constant_value *storage,
  * they have no storage and should be handled elsewhere.
  */
 void
-set_opaque_binding(gl_shader_program *prog, const char *name, int binding)
+set_opaque_binding(void *mem_ctx, gl_shader_program *prog,
+                   const glsl_type *type, const char *name, int *binding)
 {
-   struct gl_uniform_storage *const storage =
-      get_storage(prog->UniformStorage, prog->NumUniformStorage, name);
 
-   if (storage == NULL) {
-      assert(storage != NULL);
-      return;
-   }
+   if (type->is_array() && type->fields.array->is_array()) {
+      const glsl_type *const element_type = type->fields.array;
 
-   const unsigned elements = MAX2(storage->array_elements, 1);
+      for (unsigned int i = 0; i < type->length; i++) {
+        const char *element_name = ralloc_asprintf(mem_ctx, "%s[%d]", name, i);
 
-   /* Section 4.4.4 (Opaque-Uniform Layout Qualifiers) of the GLSL 4.20 spec
-    * says:
-    *
-    *     "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 = binding + i;
-   }
+        set_opaque_binding(mem_ctx, prog, element_type,
+                            element_name, binding);
+      }
+   } else {
+      struct gl_uniform_storage *const storage =
+         get_storage(prog->UniformStorage, prog->NumUniformStorage, name);
 
-   for (int sh = 0; sh < MESA_SHADER_STAGES; sh++) {
-      gl_shader *shader = prog->_LinkedShaders[sh];
+      if (storage == NULL) {
+         assert(storage != NULL);
+         return;
+      }
 
-      if (shader) {
-         if (storage->type->base_type == GLSL_TYPE_SAMPLER &&
-             storage->opaque[sh].active) {
-            for (unsigned i = 0; i < elements; i++) {
-               const unsigned index = storage->opaque[sh].index + i;
-               shader->SamplerUnits[index] = storage->storage[i].i;
-            }
+      const unsigned elements = MAX2(storage->array_elements, 1);
+
+      /* Section 4.4.4 (Opaque-Uniform Layout Qualifiers) of the GLSL 4.20 spec
+       * says:
+       *
+       *     "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 = (*binding)++;
+      }
+
+      for (int sh = 0; sh < MESA_SHADER_STAGES; sh++) {
+        gl_shader *shader = prog->_LinkedShaders[sh];
 
-         } else if (storage->type->base_type == GLSL_TYPE_IMAGE &&
+         if (shader) {
+            if (storage->type->base_type == GLSL_TYPE_SAMPLER &&
+                storage->opaque[sh].active) {
+               for (unsigned i = 0; i < elements; i++) {
+                  const unsigned index = storage->opaque[sh].index + i;
+                  shader->SamplerUnits[index] = storage->storage[i].i;
+               }
+
+            } else if (storage->type->base_type == GLSL_TYPE_IMAGE &&
                     storage->opaque[sh].active) {
-            for (unsigned i = 0; i < elements; i++) {
-               const unsigned index = storage->opaque[sh].index + i;
-               shader->ImageUnits[index] = storage->storage[i].i;
+               for (unsigned i = 0; i < elements; i++) {
+                  const unsigned index = storage->opaque[sh].index + i;
+                  shader->ImageUnits[index] = storage->storage[i].i;
+               }
             }
          }
       }
-   }
 
-   storage->initialized = true;
+      storage->initialized = true;
+   }
 }
 
 void
@@ -285,7 +298,9 @@ link_set_uniform_initializers(struct gl_shader_program *prog,
 
             if (type->without_array()->is_sampler() ||
                 type->without_array()->is_image()) {
-               linker::set_opaque_binding(prog, var->name, var->data.binding);
+               int binding = var->data.binding;
+               linker::set_opaque_binding(mem_ctx, prog, var->type,
+                                          var->name, &binding);
             } else if (var->is_in_buffer_block()) {
                const glsl_type *const iface_type = var->get_interface_type();