nir/spirv: Move CF emit code into vtn_cfg.c
[mesa.git] / src / glsl / link_uniform_initializers.cpp
index 5f57079d1b8ccaf422cf147f7865fedf1c32ffc0..cdc1d3ac7be96fe79e4ed9950bba155a12623065 100644 (file)
@@ -48,8 +48,8 @@ static unsigned
 get_uniform_block_index(const gl_shader_program *shProg,
                         const char *uniformBlockName)
 {
-   for (unsigned i = 0; i < shProg->NumUniformBlocks; i++) {
-      if (!strcmp(shProg->UniformBlocks[i].Name, uniformBlockName))
+   for (unsigned i = 0; i < shProg->NumBufferInterfaceBlocks; i++) {
+      if (!strcmp(shProg->BufferInterfaceBlocks[i].Name, uniformBlockName))
         return i;
    }
 
@@ -90,6 +90,7 @@ copy_constant_to_storage(union gl_constant_value *storage,
       case GLSL_TYPE_INTERFACE:
       case GLSL_TYPE_FUNCTION:
       case GLSL_TYPE_VOID:
+      case GLSL_TYPE_SUBROUTINE:
       case GLSL_TYPE_ERROR:
         /* All other types should have already been filtered by other
          * paths in the caller.
@@ -100,43 +101,70 @@ copy_constant_to_storage(union gl_constant_value *storage,
    }
 }
 
+/**
+ * Initialize an opaque uniform from the value of an explicit binding
+ * qualifier specified in the shader.  Atomic counters are different because
+ * they have no storage and should be handled elsewhere.
+ */
 void
-set_sampler_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);
+
+      if (storage == NULL) {
+         assert(storage != NULL);
+         return;
+      }
 
-   for (int sh = 0; sh < MESA_SHADER_STAGES; sh++) {
-      gl_shader *shader = prog->_LinkedShaders[sh];
+      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];
 
-      if (shader && storage->sampler[sh].active) {
-         for (unsigned i = 0; i < elements; i++) {
-            unsigned index = storage->sampler[sh].index + i;
+         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;
+               }
 
-            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;
+               }
+            }
          }
       }
-   }
 
-   storage->initialized = true;
+      storage->initialized = true;
+   }
 }
 
 void
@@ -151,11 +179,11 @@ set_block_binding(gl_shader_program *prog, const char *block_name, int binding)
 
       /* This is a field of a UBO.  val is the binding index. */
       for (int i = 0; i < MESA_SHADER_STAGES; i++) {
-         int stage_index = prog->UniformBlockStageIndex[i][block_index];
+         int stage_index = prog->InterfaceBlockStageIndex[i][block_index];
 
          if (stage_index != -1) {
             struct gl_shader *sh = prog->_LinkedShaders[i];
-            sh->UniformBlocks[stage_index].Binding = binding;
+            sh->BufferInterfaceBlocks[stage_index].Binding = binding;
          }
       }
 }
@@ -165,6 +193,7 @@ set_uniform_initializer(void *mem_ctx, gl_shader_program *prog,
                        const char *name, const glsl_type *type,
                         ir_constant *val, unsigned int boolean_true)
 {
+   const glsl_type *t_without_array = type->without_array();
    if (type->is_record()) {
       ir_constant *field_constant;
 
@@ -179,7 +208,8 @@ set_uniform_initializer(void *mem_ctx, gl_shader_program *prog,
         field_constant = (ir_constant *)field_constant->next;
       }
       return;
-   } else if (type->is_array() && type->fields.array->is_record()) {
+   } else if (t_without_array->is_record() ||
+              (type->is_array() && type->fields.array->is_array())) {
       const glsl_type *const element_type = type->fields.array;
 
       for (unsigned int i = 0; i < type->length; i++) {
@@ -229,8 +259,8 @@ set_uniform_initializer(void *mem_ctx, gl_shader_program *prog,
          for (int sh = 0; sh < MESA_SHADER_STAGES; sh++) {
             gl_shader *shader = prog->_LinkedShaders[sh];
 
-            if (shader && storage->sampler[sh].active) {
-               unsigned index = storage->sampler[sh].index;
+            if (shader && storage->opaque[sh].active) {
+               unsigned index = storage->opaque[sh].index;
 
                shader->SamplerUnits[index] = storage->storage[0].i;
             }
@@ -257,7 +287,8 @@ link_set_uniform_initializers(struct gl_shader_program *prog,
       foreach_in_list(ir_instruction, node, shader->ir) {
         ir_variable *const var = node->as_variable();
 
-        if (!var || var->data.mode != ir_var_uniform)
+        if (!var || (var->data.mode != ir_var_uniform &&
+            var->data.mode != ir_var_shader_storage))
            continue;
 
         if (!mem_ctx)
@@ -266,9 +297,12 @@ link_set_uniform_initializers(struct gl_shader_program *prog,
          if (var->data.explicit_binding) {
             const glsl_type *const type = var->type;
 
-            if (type->without_array()->is_sampler()) {
-               linker::set_sampler_binding(prog, var->name, var->data.binding);
-            } else if (var->is_in_uniform_block()) {
+            if (type->without_array()->is_sampler() ||
+                type->without_array()->is_image()) {
+               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();
 
                /* If the variable is an array and it is an interface instance,
@@ -281,7 +315,7 @@ link_set_uniform_initializers(struct gl_shader_program *prog,
                 *         float f[4];
                 *     };
                 *
-                * In this case "f" would pass is_in_uniform_block (above) and
+                * In this case "f" would pass is_in_buffer_block (above) and
                 * type->is_array(), but it will fail is_interface_instance().
                 */
                if (var->is_interface_instance() && var->type->is_array()) {
@@ -310,9 +344,9 @@ link_set_uniform_initializers(struct gl_shader_program *prog,
             } else {
                assert(!"Explicit binding not on a sampler, UBO or atomic.");
             }
-         } else if (var->constant_value) {
+         } else if (var->constant_initializer) {
             linker::set_uniform_initializer(mem_ctx, prog, var->name,
-                                            var->type, var->constant_value,
+                                            var->type, var->constant_initializer,
                                             boolean_true);
          }
       }