glsl: Assert that interfaces, like structures, are not seen as leaf types
[mesa.git] / src / glsl / link_uniforms.cpp
index 07d9c18dea0545e7506096cc8ffb57e043bf387f..d7116009ecd3c0734734c16f3408c3f2fcfc3c97 100644 (file)
@@ -57,24 +57,50 @@ values_for_type(const glsl_type *type)
    }
 }
 
+void
+uniform_field_visitor::process(const glsl_type *type, const char *name)
+{
+   assert(type->is_record()
+          || (type->is_array() && type->fields.array->is_record())
+          || type->is_interface()
+          || (type->is_array() && type->fields.array->is_interface()));
+
+   char *name_copy = ralloc_strdup(NULL, name);
+   recursion(type, &name_copy, strlen(name), false);
+   ralloc_free(name_copy);
+}
+
 void
 uniform_field_visitor::process(ir_variable *var)
 {
    const glsl_type *t = var->type;
 
+   /* false is always passed for the row_major parameter to the other
+    * processing functions because no information is available to do
+    * otherwise.  See the warning in linker.h.
+    */
+
    /* Only strdup the name if we actually will need to modify it. */
    if (t->is_record() || (t->is_array() && t->fields.array->is_record())) {
       char *name = ralloc_strdup(NULL, var->name);
-      recursion(var->type, &name, strlen(name));
+      recursion(var->type, &name, strlen(name), false);
+      ralloc_free(name);
+   } else if (t->is_interface()) {
+      char *name = ralloc_strdup(NULL, var->type->name);
+      recursion(var->type, &name, strlen(name), false);
+      ralloc_free(name);
+   } else if (t->is_array() && t->fields.array->is_interface()) {
+      char *name = ralloc_strdup(NULL, var->type->fields.array->name);
+      recursion(var->type, &name, strlen(name), false);
       ralloc_free(name);
    } else {
-      this->visit_field(t, var->name);
+      this->visit_field(t, var->name, false);
    }
 }
 
 void
 uniform_field_visitor::recursion(const glsl_type *t, char **name,
-                                size_t name_length)
+                                 size_t name_length, bool row_major)
 {
    /* Records need to have each field processed individually.
     *
@@ -82,30 +108,47 @@ uniform_field_visitor::recursion(const glsl_type *t, char **name,
     * individually, then each field of the resulting array elements processed
     * individually.
     */
-   if (t->is_record()) {
+   if (t->is_record() || t->is_interface()) {
       for (unsigned i = 0; i < t->length; i++) {
         const char *field = t->fields.structure[i].name;
         size_t new_length = name_length;
 
-        /* Append '.field' to the current uniform name. */
-        ralloc_asprintf_rewrite_tail(name, &new_length, ".%s", field);
+         if (t->fields.structure[i].type->is_record())
+            this->visit_field(&t->fields.structure[i]);
+
+         /* Append '.field' to the current uniform name. */
+         if (name_length == 0) {
+            ralloc_asprintf_rewrite_tail(name, &new_length, "%s", field);
+         } else {
+            ralloc_asprintf_rewrite_tail(name, &new_length, ".%s", field);
+         }
 
-        recursion(t->fields.structure[i].type, name, new_length);
+         recursion(t->fields.structure[i].type, name, new_length,
+                   t->fields.structure[i].row_major);
       }
-   } else if (t->is_array() && t->fields.array->is_record()) {
+   } else if (t->is_array() && (t->fields.array->is_record()
+                                || t->fields.array->is_interface())) {
       for (unsigned i = 0; i < t->length; i++) {
         size_t new_length = name_length;
 
         /* Append the subscript to the current uniform name */
         ralloc_asprintf_rewrite_tail(name, &new_length, "[%u]", i);
 
-        recursion(t->fields.array, name, new_length);
+         recursion(t->fields.array, name, new_length,
+                   t->fields.structure[i].row_major);
       }
    } else {
-      this->visit_field(t, *name);
+      this->visit_field(t, *name, row_major);
    }
 }
 
+void
+uniform_field_visitor::visit_field(const glsl_struct_field *field)
+{
+   (void) field;
+   /* empty */
+}
+
 /**
  * Class to help calculate the storage requirements for a set of uniforms
  *
@@ -131,6 +174,15 @@ public:
       this->num_shader_uniform_components = 0;
    }
 
+   void process(ir_variable *var)
+   {
+      if (var->is_interface_instance())
+         uniform_field_visitor::process(var->interface_type,
+                                        var->interface_type->name);
+      else
+         uniform_field_visitor::process(var);
+   }
+
    /**
     * Total number of active uniforms counted
     */
@@ -152,10 +204,15 @@ public:
    unsigned num_shader_uniform_components;
 
 private:
-   virtual void visit_field(const glsl_type *type, const char *name)
+   virtual void visit_field(const glsl_type *type, const char *name,
+                            bool row_major)
    {
       assert(!type->is_record());
       assert(!(type->is_array() && type->fields.array->is_record()));
+      assert(!type->is_interface());
+      assert(!(type->is_array() && type->fields.array->is_interface()));
+
+      (void) row_major;
 
       /* Count the number of samplers regardless of whether the uniform is
        * already in the hash table.  The hash table prevents adding the same
@@ -228,7 +285,7 @@ public:
                        ir_variable *var)
    {
       ubo_var = NULL;
-      if (var->uniform_block != -1) {
+      if (var->is_in_uniform_block()) {
         struct gl_uniform_block *block =
            &shader->UniformBlocks[var->uniform_block];
 
@@ -256,10 +313,15 @@ public:
    int ubo_byte_offset;
 
 private:
-   virtual void visit_field(const glsl_type *type, const char *name)
+   virtual void visit_field(const glsl_type *type, const char *name,
+                            bool row_major)
    {
       assert(!type->is_record());
       assert(!(type->is_array() && type->fields.array->is_record()));
+      assert(!type->is_interface());
+      assert(!(type->is_array() && type->fields.array->is_interface()));
+
+      (void) row_major;
 
       unsigned id;
       bool found = this->map->get(id, name);
@@ -399,26 +461,10 @@ link_cross_validate_uniform_block(void *mem_ctx,
 {
    for (unsigned int i = 0; i < *num_linked_blocks; i++) {
       struct gl_uniform_block *old_block = &(*linked_blocks)[i];
-      if (strcmp(old_block->Name, new_block->Name) == 0) {
-        if (old_block->NumUniforms != new_block->NumUniforms) {
-           return -1;
-        }
-
-        for (unsigned j = 0; j < old_block->NumUniforms; j++) {
-           if (strcmp(old_block->Uniforms[j].Name,
-                      new_block->Uniforms[j].Name) != 0)
-              return -1;
-
-           if (old_block->Uniforms[j].Offset !=
-               new_block->Uniforms[j].Offset)
-              return -1;
 
-           if (old_block->Uniforms[j].RowMajor !=
-               new_block->Uniforms[j].RowMajor)
-              return -1;
-        }
-        return i;
-      }
+      if (strcmp(old_block->Name, new_block->Name) == 0)
+        return link_uniform_blocks_are_compatible(old_block, new_block)
+           ? i : -1;
    }
 
    *linked_blocks = reralloc(mem_ctx, *linked_blocks,
@@ -458,7 +504,7 @@ link_update_uniform_buffer_variables(struct gl_shader *shader)
    foreach_list(node, shader->ir) {
       ir_variable *const var = ((ir_instruction *) node)->as_variable();
 
-      if ((var == NULL) || (var->uniform_block == -1))
+      if ((var == NULL) || !var->is_in_uniform_block())
         continue;
 
       assert(var->mode == ir_var_uniform);