i965/vs: rename vec4_generator::generate_vs_instruction.
[mesa.git] / src / glsl / glsl_types.cpp
index f2dd44acb6227bda389f081f7ad14bea1cdc138e..df9c5d36f0b0b1dea091d4a15644e7f2cb6c00f7 100644 (file)
@@ -34,6 +34,7 @@ extern "C" {
 
 hash_table *glsl_type::array_types = NULL;
 hash_table *glsl_type::record_types = NULL;
+hash_table *glsl_type::interface_types = NULL;
 void *glsl_type::mem_ctx = NULL;
 
 void
@@ -51,11 +52,12 @@ glsl_type::glsl_type(GLenum gl_type,
    gl_type(gl_type),
    base_type(base_type),
    sampler_dimensionality(0), sampler_shadow(0), sampler_array(0),
-   sampler_type(0),
+   sampler_type(0), interface_packing(0),
    vector_elements(vector_elements), matrix_columns(matrix_columns),
    length(0)
 {
    init_ralloc_type_ctx();
+   assert(name != NULL);
    this->name = ralloc_strdup(this->mem_ctx, name);
    /* Neither dimension is zero or both dimensions are zero.
     */
@@ -69,26 +71,53 @@ glsl_type::glsl_type(GLenum gl_type,
    gl_type(gl_type),
    base_type(GLSL_TYPE_SAMPLER),
    sampler_dimensionality(dim), sampler_shadow(shadow),
-   sampler_array(array), sampler_type(type),
+   sampler_array(array), sampler_type(type), interface_packing(0),
    vector_elements(0), matrix_columns(0),
    length(0)
 {
    init_ralloc_type_ctx();
+   assert(name != NULL);
    this->name = ralloc_strdup(this->mem_ctx, name);
    memset(& fields, 0, sizeof(fields));
 }
 
 glsl_type::glsl_type(const glsl_struct_field *fields, unsigned num_fields,
                     const char *name) :
+   gl_type(0),
    base_type(GLSL_TYPE_STRUCT),
    sampler_dimensionality(0), sampler_shadow(0), sampler_array(0),
-   sampler_type(0),
+   sampler_type(0), interface_packing(0),
    vector_elements(0), matrix_columns(0),
    length(num_fields)
 {
    unsigned int i;
 
    init_ralloc_type_ctx();
+   assert(name != NULL);
+   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 = ralloc_strdup(this->fields.structure,
+                                                    fields[i].name);
+      this->fields.structure[i].row_major = fields[i].row_major;
+   }
+}
+
+glsl_type::glsl_type(const glsl_struct_field *fields, unsigned num_fields,
+                    enum glsl_interface_packing packing, const char *name) :
+   gl_type(0),
+   base_type(GLSL_TYPE_INTERFACE),
+   sampler_dimensionality(0), sampler_shadow(0), sampler_array(0),
+   sampler_type(0), interface_packing((unsigned) packing),
+   vector_elements(0), matrix_columns(0),
+   length(num_fields)
+{
+   unsigned int i;
+
+   init_ralloc_type_ctx();
+   assert(name != NULL);
    this->name = ralloc_strdup(this->mem_ctx, name);
    this->fields.structure = ralloc_array(this->mem_ctx,
                                         glsl_struct_field, length);
@@ -133,6 +162,24 @@ glsl_type::contains_sampler() const
    }
 }
 
+
+bool
+glsl_type::contains_integer() const
+{
+   if (this->is_array()) {
+      return this->fields.array->contains_integer();
+   } else if (this->is_record()) {
+      for (unsigned int i = 0; i < this->length; i++) {
+        if (this->fields.structure[i].type->contains_integer())
+           return true;
+      }
+      return false;
+   } else {
+      return this->is_integer();
+   }
+}
+
+
 gl_texture_index
 glsl_type::sampler_index() const
 {
@@ -155,6 +202,8 @@ glsl_type::sampler_index() const
       return TEXTURE_BUFFER_INDEX;
    case GLSL_SAMPLER_DIM_EXTERNAL:
       return TEXTURE_EXTERNAL_INDEX;
+   case GLSL_SAMPLER_DIM_MS:
+      return (t->sampler_array) ? TEXTURE_2D_MULTISAMPLE_ARRAY_INDEX : TEXTURE_2D_MULTISAMPLE_INDEX;
    default:
       assert(!"Should not get here.");
       return TEXTURE_BUFFER_INDEX;
@@ -246,6 +295,14 @@ glsl_type::generate_140_types(glsl_symbol_table *symtab)
 }
 
 
+void
+glsl_type::generate_150_types(glsl_symbol_table *symtab)
+{
+   generate_140_types(symtab);
+   generate_ARB_texture_multisample_types(symtab, false);
+}
+
+
 void
 glsl_type::generate_ARB_texture_rectangle_types(glsl_symbol_table *symtab,
                                                bool warn)
@@ -301,6 +358,16 @@ glsl_type::generate_ARB_texture_cube_map_array_types(glsl_symbol_table *symtab,
                             warn, skip_1d);
 }
 
+void
+glsl_type::generate_ARB_texture_multisample_types(glsl_symbol_table *symtab,
+                                                  bool warn)
+{
+   bool skip_1d = false;
+   add_types_to_symbol_table(symtab, builtin_ARB_texture_multisample_types,
+                             Elements(builtin_ARB_texture_multisample_types),
+                             warn, skip_1d);
+}
+
 void
 _mesa_glsl_initialize_types(struct _mesa_glsl_parse_state *state)
 {
@@ -332,6 +399,9 @@ _mesa_glsl_initialize_types(struct _mesa_glsl_parse_state *state)
       case 140:
          glsl_type::generate_140_types(state->symbols);
          break;
+      case 150:
+         glsl_type::generate_150_types(state->symbols);
+         break;
       default:
          assert(!"Unexpected language version");
          break;
@@ -369,6 +439,11 @@ _mesa_glsl_initialize_types(struct _mesa_glsl_parse_state *state)
       glsl_type::generate_ARB_texture_cube_map_array_types(state->symbols,
                                       state->ARB_texture_cube_map_array_warn);
    }
+
+   if (state->ARB_texture_multisample_enable) {
+      glsl_type::generate_ARB_texture_multisample_types(state->symbols,
+         state->ARB_texture_multisample_warn);
+   }
 }
 
 
@@ -405,6 +480,8 @@ const glsl_type *glsl_type::get_scalar_type() const
       return int_type;
    case GLSL_TYPE_FLOAT:
       return float_type;
+   case GLSL_TYPE_BOOL:
+      return bool_type;
    default:
       /* Handle everything else */
       return type;
@@ -430,7 +507,7 @@ _mesa_glsl_release_types(void)
 glsl_type::glsl_type(const glsl_type *array, unsigned length) :
    base_type(GLSL_TYPE_ARRAY),
    sampler_dimensionality(0), sampler_shadow(0), sampler_array(0),
-   sampler_type(0),
+   sampler_type(0), interface_packing(0),
    vector_elements(0), matrix_columns(0),
    name(NULL), length(length)
 {
@@ -562,6 +639,9 @@ glsl_type::record_key_compare(const void *a, const void *b)
    if (key1->length != key2->length)
       return 1;
 
+   if (key1->interface_packing != key2->interface_packing)
+      return 1;
+
    for (unsigned i = 0; i < key1->length; i++) {
       if (key1->fields.structure[i].type != key2->fields.structure[i].type)
         return 1;
@@ -624,10 +704,38 @@ glsl_type::get_record_instance(const glsl_struct_field *fields,
 }
 
 
+const glsl_type *
+glsl_type::get_interface_instance(const glsl_struct_field *fields,
+                                 unsigned num_fields,
+                                 enum glsl_interface_packing packing,
+                                 const char *name)
+{
+   const glsl_type key(fields, num_fields, packing, name);
+
+   if (interface_types == NULL) {
+      interface_types = hash_table_ctor(64, record_key_hash, record_key_compare);
+   }
+
+   const glsl_type *t = (glsl_type *) hash_table_find(interface_types, & key);
+   if (t == NULL) {
+      t = new glsl_type(fields, num_fields, packing, name);
+
+      hash_table_insert(interface_types, (void *) t, t);
+   }
+
+   assert(t->base_type == GLSL_TYPE_INTERFACE);
+   assert(t->length == num_fields);
+   assert(strcmp(t->name, name) == 0);
+
+   return t;
+}
+
+
 const glsl_type *
 glsl_type::field_type(const char *name) const
 {
-   if (this->base_type != GLSL_TYPE_STRUCT)
+   if (this->base_type != GLSL_TYPE_STRUCT
+       && this->base_type != GLSL_TYPE_INTERFACE)
       return error_type;
 
    for (unsigned i = 0; i < this->length; i++) {
@@ -642,7 +750,8 @@ glsl_type::field_type(const char *name) const
 int
 glsl_type::field_index(const char *name) const
 {
-   if (this->base_type != GLSL_TYPE_STRUCT)
+   if (this->base_type != GLSL_TYPE_STRUCT
+       && this->base_type != GLSL_TYPE_INTERFACE)
       return -1;
 
    for (unsigned i = 0; i < this->length; i++) {
@@ -664,7 +773,8 @@ glsl_type::component_slots() const
    case GLSL_TYPE_BOOL:
       return this->components();
 
-   case GLSL_TYPE_STRUCT: {
+   case GLSL_TYPE_STRUCT:
+   case GLSL_TYPE_INTERFACE: {
       unsigned size = 0;
 
       for (unsigned i = 0; i < this->length; i++)
@@ -807,12 +917,6 @@ glsl_type::std140_base_alignment(bool row_major) const
    return -1;
 }
 
-static unsigned
-align(unsigned val, unsigned align)
-{
-   return (val + align - 1) / align * align;
-}
-
 unsigned
 glsl_type::std140_size(bool row_major) const
 {
@@ -914,11 +1018,11 @@ glsl_type::std140_size(bool row_major) const
       for (unsigned i = 0; i < this->length; i++) {
         const struct glsl_type *field_type = this->fields.structure[i].type;
         unsigned align = field_type->std140_base_alignment(row_major);
-        size = (size + align - 1) / align * align;
+        size = glsl_align(size, align);
         size += field_type->std140_size(row_major);
       }
-      size = align(size,
-                  this->fields.structure[0].type->std140_base_alignment(row_major));
+      size = glsl_align(size,
+                       this->fields.structure[0].type->std140_base_alignment(row_major));
       return size;
    }