glsl: Move type_contains_sampler() into glsl_type for later reuse.
[mesa.git] / src / glsl / glsl_types.cpp
index 82eb470605689761b387e8c4f1a18963a7e5875f..a5e21bbb96c06ef319c1c12d6b220b790c71b8c9 100644 (file)
@@ -21,7 +21,7 @@
  * DEALINGS IN THE SOFTWARE.
  */
 
-#include <cstdio>
+#include <stdio.h>
 #include <stdlib.h>
 #include "main/core.h" /* for Elements */
 #include "glsl_symbol_table.h"
@@ -37,16 +37,16 @@ hash_table *glsl_type::record_types = NULL;
 void *glsl_type::mem_ctx = NULL;
 
 void
-glsl_type::init_talloc_type_ctx(void)
+glsl_type::init_ralloc_type_ctx(void)
 {
    if (glsl_type::mem_ctx == NULL) {
-      glsl_type::mem_ctx = talloc_autofree_context();
+      glsl_type::mem_ctx = ralloc_autofree_context();
       assert(glsl_type::mem_ctx != NULL);
    }
 }
 
 glsl_type::glsl_type(GLenum gl_type,
-                    unsigned base_type, unsigned vector_elements,
+                    glsl_base_type base_type, unsigned vector_elements,
                     unsigned matrix_columns, const char *name) :
    gl_type(gl_type),
    base_type(base_type),
@@ -55,8 +55,8 @@ glsl_type::glsl_type(GLenum gl_type,
    vector_elements(vector_elements), matrix_columns(matrix_columns),
    length(0)
 {
-   init_talloc_type_ctx();
-   this->name = talloc_strdup(this->mem_ctx, name);
+   init_ralloc_type_ctx();
+   this->name = ralloc_strdup(this->mem_ctx, name);
    /* Neither dimension is zero or both dimensions are zero.
     */
    assert((vector_elements == 0) == (matrix_columns == 0));
@@ -73,8 +73,8 @@ glsl_type::glsl_type(GLenum gl_type,
    vector_elements(0), matrix_columns(0),
    length(0)
 {
-   init_talloc_type_ctx();
-   this->name = talloc_strdup(this->mem_ctx, name);
+   init_ralloc_type_ctx();
+   this->name = ralloc_strdup(this->mem_ctx, name);
    memset(& fields, 0, sizeof(fields));
 }
 
@@ -88,13 +88,13 @@ glsl_type::glsl_type(const glsl_struct_field *fields, unsigned num_fields,
 {
    unsigned int i;
 
-   init_talloc_type_ctx();
-   this->name = talloc_strdup(this->mem_ctx, name);
-   this->fields.structure = talloc_array(this->mem_ctx,
+   init_ralloc_type_ctx();
+   this->name = ralloc_strdup(this->mem_ctx, name);
+   this->fields.structure = ralloc_array(this->mem_ctx,
                                         glsl_struct_field, length);
    for (i = 0; i < length; i++) {
       this->fields.structure[i].type = fields[i].type;
-      this->fields.structure[i].name = talloc_strdup(this->fields.structure,
+      this->fields.structure[i].name = ralloc_strdup(this->fields.structure,
                                                     fields[i].name);
    }
 }
@@ -111,6 +111,22 @@ add_types_to_symbol_table(glsl_symbol_table *symtab,
    }
 }
 
+bool
+glsl_type::contains_sampler() const
+{
+   if (this->is_array()) {
+      return this->fields.array->contains_sampler();
+   } else if (this->is_record()) {
+      for (unsigned int i = 0; i < this->length; i++) {
+        if (this->fields.structure[i].type->contains_sampler())
+           return true;
+      }
+      return false;
+   } else {
+      return this->is_sampler();
+   }
+}
+
 void
 glsl_type::generate_100ES_types(glsl_symbol_table *symtab)
 {
@@ -120,7 +136,7 @@ glsl_type::generate_100ES_types(glsl_symbol_table *symtab)
    add_types_to_symbol_table(symtab, builtin_structure_types,
                             Elements(builtin_structure_types),
                             false);
-   add_types_to_symbol_table(symtab, &void_type, 1, false);
+   add_types_to_symbol_table(symtab, void_type, 1, false);
 }
 
 void
@@ -131,6 +147,7 @@ glsl_type::generate_110_types(glsl_symbol_table *symtab)
    add_types_to_symbol_table(symtab, builtin_110_types,
                             Elements(builtin_110_types),
                             false);
+   add_types_to_symbol_table(symtab, &_sampler3D_type, 1, false);
    add_types_to_symbol_table(symtab, builtin_110_deprecated_structure_types,
                             Elements(builtin_110_deprecated_structure_types),
                             false);
@@ -178,6 +195,13 @@ glsl_type::generate_EXT_texture_array_types(glsl_symbol_table *symtab,
 }
 
 
+void
+glsl_type::generate_OES_texture_3D_types(glsl_symbol_table *symtab, bool warn)
+{
+   add_types_to_symbol_table(symtab, &_sampler3D_type, 1, warn);
+}
+
+
 void
 _mesa_glsl_initialize_types(struct _mesa_glsl_parse_state *state)
 {
@@ -204,6 +228,10 @@ _mesa_glsl_initialize_types(struct _mesa_glsl_parse_state *state)
       glsl_type::generate_ARB_texture_rectangle_types(state->symbols,
                                           state->ARB_texture_rectangle_warn);
    }
+   if (state->OES_texture_3D_enable && state->language_version == 100) {
+      glsl_type::generate_OES_texture_3D_types(state->symbols,
+                                              state->OES_texture_3D_warn);
+   }
 
    if (state->EXT_texture_array_enable && state->language_version < 130) {
       // These are already included in 130; don't create twice.
@@ -264,7 +292,7 @@ glsl_type::glsl_type(const glsl_type *array, unsigned length) :
     * NUL.
     */
    const unsigned name_length = strlen(array->name) + 10 + 3;
-   char *const n = (char *) talloc_size(this->mem_ctx, name_length);
+   char *const n = (char *) ralloc_size(this->mem_ctx, name_length);
 
    if (length == 0)
       snprintf(n, name_length, "%s[]", array->name);
@@ -279,7 +307,7 @@ const glsl_type *
 glsl_type::get_instance(unsigned base_type, unsigned rows, unsigned columns)
 {
    if (base_type == GLSL_TYPE_VOID)
-      return &void_type;
+      return void_type;
 
    if ((rows < 1) || (rows > 4) || (columns < 1) || (columns > 4))
       return error_type;
@@ -354,7 +382,7 @@ glsl_type::get_array_instance(const glsl_type *base, unsigned array_size)
    if (t == NULL) {
       t = new glsl_type(base, array_size);
 
-      hash_table_insert(array_types, (void *) t, talloc_strdup(mem_ctx, key));
+      hash_table_insert(array_types, (void *) t, ralloc_strdup(mem_ctx, key));
    }
 
    assert(t->base_type == GLSL_TYPE_ARRAY);