2 * Copyright © 2009 Intel Corporation
4 * Permission is hereby granted, free of charge, to any person obtaining a
5 * copy of this software and associated documentation files (the "Software"),
6 * to deal in the Software without restriction, including without limitation
7 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
8 * and/or sell copies of the Software, and to permit persons to whom the
9 * Software is furnished to do so, subject to the following conditions:
11 * The above copyright notice and this permission notice (including the next
12 * paragraph) shall be included in all copies or substantial portions of the
15 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
18 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
20 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
21 * DEALINGS IN THE SOFTWARE.
25 #include "main/macros.h"
26 #include "compiler/glsl/glsl_parser_extras.h"
27 #include "glsl_types.h"
28 #include "util/hash_table.h"
31 mtx_t glsl_type::mutex = _MTX_INITIALIZER_NP;
32 hash_table *glsl_type::array_types = NULL;
33 hash_table *glsl_type::record_types = NULL;
34 hash_table *glsl_type::interface_types = NULL;
35 hash_table *glsl_type::subroutine_types = NULL;
36 void *glsl_type::mem_ctx = NULL;
39 glsl_type::init_ralloc_type_ctx(void)
41 if (glsl_type::mem_ctx == NULL) {
42 glsl_type::mem_ctx = ralloc_autofree_context();
43 assert(glsl_type::mem_ctx != NULL);
47 glsl_type::glsl_type(GLenum gl_type,
48 glsl_base_type base_type, unsigned vector_elements,
49 unsigned matrix_columns, const char *name) :
52 sampler_dimensionality(0), sampler_shadow(0), sampler_array(0),
53 sampler_type(0), interface_packing(0),
54 vector_elements(vector_elements), matrix_columns(matrix_columns),
57 mtx_lock(&glsl_type::mutex);
59 init_ralloc_type_ctx();
61 this->name = ralloc_strdup(this->mem_ctx, name);
63 mtx_unlock(&glsl_type::mutex);
65 /* Neither dimension is zero or both dimensions are zero.
67 assert((vector_elements == 0) == (matrix_columns == 0));
68 memset(& fields, 0, sizeof(fields));
71 glsl_type::glsl_type(GLenum gl_type, glsl_base_type base_type,
72 enum glsl_sampler_dim dim, bool shadow, bool array,
73 unsigned type, const char *name) :
76 sampler_dimensionality(dim), sampler_shadow(shadow),
77 sampler_array(array), sampler_type(type), interface_packing(0),
80 mtx_lock(&glsl_type::mutex);
82 init_ralloc_type_ctx();
84 this->name = ralloc_strdup(this->mem_ctx, name);
86 mtx_unlock(&glsl_type::mutex);
88 memset(& fields, 0, sizeof(fields));
90 if (base_type == GLSL_TYPE_SAMPLER) {
91 /* Samplers take no storage whatsoever. */
92 matrix_columns = vector_elements = 0;
94 matrix_columns = vector_elements = 1;
98 glsl_type::glsl_type(const glsl_struct_field *fields, unsigned num_fields,
101 base_type(GLSL_TYPE_STRUCT),
102 sampler_dimensionality(0), sampler_shadow(0), sampler_array(0),
103 sampler_type(0), interface_packing(0),
104 vector_elements(0), matrix_columns(0),
109 mtx_lock(&glsl_type::mutex);
111 init_ralloc_type_ctx();
112 assert(name != NULL);
113 this->name = ralloc_strdup(this->mem_ctx, name);
114 this->fields.structure = ralloc_array(this->mem_ctx,
115 glsl_struct_field, length);
117 for (i = 0; i < length; i++) {
118 this->fields.structure[i].type = fields[i].type;
119 this->fields.structure[i].name = ralloc_strdup(this->fields.structure,
121 this->fields.structure[i].location = fields[i].location;
122 this->fields.structure[i].interpolation = fields[i].interpolation;
123 this->fields.structure[i].centroid = fields[i].centroid;
124 this->fields.structure[i].sample = fields[i].sample;
125 this->fields.structure[i].matrix_layout = fields[i].matrix_layout;
126 this->fields.structure[i].patch = fields[i].patch;
127 this->fields.structure[i].image_read_only = fields[i].image_read_only;
128 this->fields.structure[i].image_write_only = fields[i].image_write_only;
129 this->fields.structure[i].image_coherent = fields[i].image_coherent;
130 this->fields.structure[i].image_volatile = fields[i].image_volatile;
131 this->fields.structure[i].image_restrict = fields[i].image_restrict;
132 this->fields.structure[i].precision = fields[i].precision;
135 mtx_unlock(&glsl_type::mutex);
138 glsl_type::glsl_type(const glsl_struct_field *fields, unsigned num_fields,
139 enum glsl_interface_packing packing, const char *name) :
141 base_type(GLSL_TYPE_INTERFACE),
142 sampler_dimensionality(0), sampler_shadow(0), sampler_array(0),
143 sampler_type(0), interface_packing((unsigned) packing),
144 vector_elements(0), matrix_columns(0),
149 mtx_lock(&glsl_type::mutex);
151 init_ralloc_type_ctx();
152 assert(name != NULL);
153 this->name = ralloc_strdup(this->mem_ctx, name);
154 this->fields.structure = ralloc_array(this->mem_ctx,
155 glsl_struct_field, length);
156 for (i = 0; i < length; i++) {
157 this->fields.structure[i].type = fields[i].type;
158 this->fields.structure[i].name = ralloc_strdup(this->fields.structure,
160 this->fields.structure[i].location = fields[i].location;
161 this->fields.structure[i].interpolation = fields[i].interpolation;
162 this->fields.structure[i].centroid = fields[i].centroid;
163 this->fields.structure[i].sample = fields[i].sample;
164 this->fields.structure[i].matrix_layout = fields[i].matrix_layout;
165 this->fields.structure[i].patch = fields[i].patch;
166 this->fields.structure[i].precision = fields[i].precision;
169 mtx_unlock(&glsl_type::mutex);
172 glsl_type::glsl_type(const char *subroutine_name) :
174 base_type(GLSL_TYPE_SUBROUTINE),
175 sampler_dimensionality(0), sampler_shadow(0), sampler_array(0),
176 sampler_type(0), interface_packing(0),
177 vector_elements(1), matrix_columns(1),
180 mtx_lock(&glsl_type::mutex);
182 init_ralloc_type_ctx();
183 assert(subroutine_name != NULL);
184 this->name = ralloc_strdup(this->mem_ctx, subroutine_name);
185 mtx_unlock(&glsl_type::mutex);
189 glsl_type::contains_sampler() const
191 if (this->is_array()) {
192 return this->fields.array->contains_sampler();
193 } else if (this->is_record()) {
194 for (unsigned int i = 0; i < this->length; i++) {
195 if (this->fields.structure[i].type->contains_sampler())
200 return this->is_sampler();
206 glsl_type::contains_integer() const
208 if (this->is_array()) {
209 return this->fields.array->contains_integer();
210 } else if (this->is_record()) {
211 for (unsigned int i = 0; i < this->length; i++) {
212 if (this->fields.structure[i].type->contains_integer())
217 return this->is_integer();
222 glsl_type::contains_double() const
224 if (this->is_array()) {
225 return this->fields.array->contains_double();
226 } else if (this->is_record()) {
227 for (unsigned int i = 0; i < this->length; i++) {
228 if (this->fields.structure[i].type->contains_double())
233 return this->is_double();
238 glsl_type::contains_opaque() const {
240 case GLSL_TYPE_SAMPLER:
241 case GLSL_TYPE_IMAGE:
242 case GLSL_TYPE_ATOMIC_UINT:
244 case GLSL_TYPE_ARRAY:
245 return fields.array->contains_opaque();
246 case GLSL_TYPE_STRUCT:
247 for (unsigned int i = 0; i < length; i++) {
248 if (fields.structure[i].type->contains_opaque())
258 glsl_type::contains_subroutine() const
260 if (this->is_array()) {
261 return this->fields.array->contains_subroutine();
262 } else if (this->is_record()) {
263 for (unsigned int i = 0; i < this->length; i++) {
264 if (this->fields.structure[i].type->contains_subroutine())
269 return this->is_subroutine();
274 glsl_type::sampler_index() const
276 const glsl_type *const t = (this->is_array()) ? this->fields.array : this;
278 assert(t->is_sampler());
280 switch (t->sampler_dimensionality) {
281 case GLSL_SAMPLER_DIM_1D:
282 return (t->sampler_array) ? TEXTURE_1D_ARRAY_INDEX : TEXTURE_1D_INDEX;
283 case GLSL_SAMPLER_DIM_2D:
284 return (t->sampler_array) ? TEXTURE_2D_ARRAY_INDEX : TEXTURE_2D_INDEX;
285 case GLSL_SAMPLER_DIM_3D:
286 return TEXTURE_3D_INDEX;
287 case GLSL_SAMPLER_DIM_CUBE:
288 return (t->sampler_array) ? TEXTURE_CUBE_ARRAY_INDEX : TEXTURE_CUBE_INDEX;
289 case GLSL_SAMPLER_DIM_RECT:
290 return TEXTURE_RECT_INDEX;
291 case GLSL_SAMPLER_DIM_BUF:
292 return TEXTURE_BUFFER_INDEX;
293 case GLSL_SAMPLER_DIM_EXTERNAL:
294 return TEXTURE_EXTERNAL_INDEX;
295 case GLSL_SAMPLER_DIM_MS:
296 return (t->sampler_array) ? TEXTURE_2D_MULTISAMPLE_ARRAY_INDEX : TEXTURE_2D_MULTISAMPLE_INDEX;
298 assert(!"Should not get here.");
299 return TEXTURE_BUFFER_INDEX;
304 glsl_type::contains_image() const
306 if (this->is_array()) {
307 return this->fields.array->contains_image();
308 } else if (this->is_record()) {
309 for (unsigned int i = 0; i < this->length; i++) {
310 if (this->fields.structure[i].type->contains_image())
315 return this->is_image();
319 const glsl_type *glsl_type::get_base_type() const
326 case GLSL_TYPE_FLOAT:
328 case GLSL_TYPE_DOUBLE:
338 const glsl_type *glsl_type::get_scalar_type() const
340 const glsl_type *type = this;
343 while (type->base_type == GLSL_TYPE_ARRAY)
344 type = type->fields.array;
346 /* Handle vectors and matrices */
347 switch (type->base_type) {
352 case GLSL_TYPE_FLOAT:
354 case GLSL_TYPE_DOUBLE:
359 /* Handle everything else */
366 _mesa_glsl_release_types(void)
368 /* Should only be called during atexit (either when unloading shared
369 * object, or if process terminates), so no mutex-locking should be
372 if (glsl_type::array_types != NULL) {
373 _mesa_hash_table_destroy(glsl_type::array_types, NULL);
374 glsl_type::array_types = NULL;
377 if (glsl_type::record_types != NULL) {
378 _mesa_hash_table_destroy(glsl_type::record_types, NULL);
379 glsl_type::record_types = NULL;
382 if (glsl_type::interface_types != NULL) {
383 _mesa_hash_table_destroy(glsl_type::interface_types, NULL);
384 glsl_type::interface_types = NULL;
389 glsl_type::glsl_type(const glsl_type *array, unsigned length) :
390 base_type(GLSL_TYPE_ARRAY),
391 sampler_dimensionality(0), sampler_shadow(0), sampler_array(0),
392 sampler_type(0), interface_packing(0),
393 vector_elements(0), matrix_columns(0),
394 length(length), name(NULL)
396 this->fields.array = array;
397 /* Inherit the gl type of the base. The GL type is used for
398 * uniform/statevar handling in Mesa and the arrayness of the type
399 * is represented by the size rather than the type.
401 this->gl_type = array->gl_type;
403 /* Allow a maximum of 10 characters for the array size. This is enough
404 * for 32-bits of ~0. The extra 3 are for the '[', ']', and terminating
407 const unsigned name_length = strlen(array->name) + 10 + 3;
409 mtx_lock(&glsl_type::mutex);
410 char *const n = (char *) ralloc_size(this->mem_ctx, name_length);
411 mtx_unlock(&glsl_type::mutex);
414 snprintf(n, name_length, "%s[]", array->name);
416 /* insert outermost dimensions in the correct spot
417 * otherwise the dimension order will be backwards
419 const char *pos = strchr(array->name, '[');
421 int idx = pos - array->name;
422 snprintf(n, idx+1, "%s", array->name);
423 snprintf(n + idx, name_length - idx, "[%u]%s",
424 length, array->name + idx);
426 snprintf(n, name_length, "%s[%u]", array->name, length);
435 glsl_type::vec(unsigned components)
437 if (components == 0 || components > 4)
440 static const glsl_type *const ts[] = {
441 float_type, vec2_type, vec3_type, vec4_type
443 return ts[components - 1];
447 glsl_type::dvec(unsigned components)
449 if (components == 0 || components > 4)
452 static const glsl_type *const ts[] = {
453 double_type, dvec2_type, dvec3_type, dvec4_type
455 return ts[components - 1];
459 glsl_type::ivec(unsigned components)
461 if (components == 0 || components > 4)
464 static const glsl_type *const ts[] = {
465 int_type, ivec2_type, ivec3_type, ivec4_type
467 return ts[components - 1];
472 glsl_type::uvec(unsigned components)
474 if (components == 0 || components > 4)
477 static const glsl_type *const ts[] = {
478 uint_type, uvec2_type, uvec3_type, uvec4_type
480 return ts[components - 1];
485 glsl_type::bvec(unsigned components)
487 if (components == 0 || components > 4)
490 static const glsl_type *const ts[] = {
491 bool_type, bvec2_type, bvec3_type, bvec4_type
493 return ts[components - 1];
498 glsl_type::get_instance(unsigned base_type, unsigned rows, unsigned columns)
500 if (base_type == GLSL_TYPE_VOID)
503 if ((rows < 1) || (rows > 4) || (columns < 1) || (columns > 4))
506 /* Treat GLSL vectors as Nx1 matrices.
514 case GLSL_TYPE_FLOAT:
516 case GLSL_TYPE_DOUBLE:
524 if ((base_type != GLSL_TYPE_FLOAT && base_type != GLSL_TYPE_DOUBLE) || (rows == 1))
527 /* GLSL matrix types are named mat{COLUMNS}x{ROWS}. Only the following
528 * combinations are valid:
536 #define IDX(c,r) (((c-1)*3) + (r-1))
538 if (base_type == GLSL_TYPE_DOUBLE) {
539 switch (IDX(columns, rows)) {
540 case IDX(2,2): return dmat2_type;
541 case IDX(2,3): return dmat2x3_type;
542 case IDX(2,4): return dmat2x4_type;
543 case IDX(3,2): return dmat3x2_type;
544 case IDX(3,3): return dmat3_type;
545 case IDX(3,4): return dmat3x4_type;
546 case IDX(4,2): return dmat4x2_type;
547 case IDX(4,3): return dmat4x3_type;
548 case IDX(4,4): return dmat4_type;
549 default: return error_type;
552 switch (IDX(columns, rows)) {
553 case IDX(2,2): return mat2_type;
554 case IDX(2,3): return mat2x3_type;
555 case IDX(2,4): return mat2x4_type;
556 case IDX(3,2): return mat3x2_type;
557 case IDX(3,3): return mat3_type;
558 case IDX(3,4): return mat3x4_type;
559 case IDX(4,2): return mat4x2_type;
560 case IDX(4,3): return mat4x3_type;
561 case IDX(4,4): return mat4_type;
562 default: return error_type;
567 assert(!"Should not get here.");
572 glsl_type::get_sampler_instance(enum glsl_sampler_dim dim,
578 case GLSL_TYPE_FLOAT:
580 case GLSL_SAMPLER_DIM_1D:
582 return (array ? sampler1DArrayShadow_type : sampler1DShadow_type);
584 return (array ? sampler1DArray_type : sampler1D_type);
585 case GLSL_SAMPLER_DIM_2D:
587 return (array ? sampler2DArrayShadow_type : sampler2DShadow_type);
589 return (array ? sampler2DArray_type : sampler2D_type);
590 case GLSL_SAMPLER_DIM_3D:
594 return sampler3D_type;
595 case GLSL_SAMPLER_DIM_CUBE:
597 return (array ? samplerCubeArrayShadow_type : samplerCubeShadow_type);
599 return (array ? samplerCubeArray_type : samplerCube_type);
600 case GLSL_SAMPLER_DIM_RECT:
604 return sampler2DRectShadow_type;
606 return sampler2DRect_type;
607 case GLSL_SAMPLER_DIM_BUF:
611 return samplerBuffer_type;
612 case GLSL_SAMPLER_DIM_MS:
615 return (array ? sampler2DMSArray_type : sampler2DMS_type);
616 case GLSL_SAMPLER_DIM_EXTERNAL:
620 return samplerExternalOES_type;
626 case GLSL_SAMPLER_DIM_1D:
627 return (array ? isampler1DArray_type : isampler1D_type);
628 case GLSL_SAMPLER_DIM_2D:
629 return (array ? isampler2DArray_type : isampler2D_type);
630 case GLSL_SAMPLER_DIM_3D:
633 return isampler3D_type;
634 case GLSL_SAMPLER_DIM_CUBE:
635 return (array ? isamplerCubeArray_type : isamplerCube_type);
636 case GLSL_SAMPLER_DIM_RECT:
639 return isampler2DRect_type;
640 case GLSL_SAMPLER_DIM_BUF:
643 return isamplerBuffer_type;
644 case GLSL_SAMPLER_DIM_MS:
645 return (array ? isampler2DMSArray_type : isampler2DMS_type);
646 case GLSL_SAMPLER_DIM_EXTERNAL:
653 case GLSL_SAMPLER_DIM_1D:
654 return (array ? usampler1DArray_type : usampler1D_type);
655 case GLSL_SAMPLER_DIM_2D:
656 return (array ? usampler2DArray_type : usampler2D_type);
657 case GLSL_SAMPLER_DIM_3D:
660 return usampler3D_type;
661 case GLSL_SAMPLER_DIM_CUBE:
662 return (array ? usamplerCubeArray_type : usamplerCube_type);
663 case GLSL_SAMPLER_DIM_RECT:
666 return usampler2DRect_type;
667 case GLSL_SAMPLER_DIM_BUF:
670 return usamplerBuffer_type;
671 case GLSL_SAMPLER_DIM_MS:
672 return (array ? usampler2DMSArray_type : usampler2DMS_type);
673 case GLSL_SAMPLER_DIM_EXTERNAL:
680 unreachable("switch statement above should be complete");
684 glsl_type::get_array_instance(const glsl_type *base, unsigned array_size)
686 /* Generate a name using the base type pointer in the key. This is
687 * done because the name of the base type may not be unique across
688 * shaders. For example, two shaders may have different record types
692 snprintf(key, sizeof(key), "%p[%u]", (void *) base, array_size);
694 mtx_lock(&glsl_type::mutex);
696 if (array_types == NULL) {
697 array_types = _mesa_hash_table_create(NULL, _mesa_key_hash_string,
698 _mesa_key_string_equal);
701 const struct hash_entry *entry = _mesa_hash_table_search(array_types, key);
703 mtx_unlock(&glsl_type::mutex);
704 const glsl_type *t = new glsl_type(base, array_size);
705 mtx_lock(&glsl_type::mutex);
707 entry = _mesa_hash_table_insert(array_types,
708 ralloc_strdup(mem_ctx, key),
712 assert(((glsl_type *) entry->data)->base_type == GLSL_TYPE_ARRAY);
713 assert(((glsl_type *) entry->data)->length == array_size);
714 assert(((glsl_type *) entry->data)->fields.array == base);
716 mtx_unlock(&glsl_type::mutex);
718 return (glsl_type *) entry->data;
723 glsl_type::record_compare(const glsl_type *b) const
725 if (this->length != b->length)
728 if (this->interface_packing != b->interface_packing)
731 /* From the GLSL 4.20 specification (Sec 4.2):
733 * "Structures must have the same name, sequence of type names, and
734 * type definitions, and field names to be considered the same type."
736 * GLSL ES behaves the same (Ver 1.00 Sec 4.2.4, Ver 3.00 Sec 4.2.5).
738 * Note that we cannot force type name check when comparing unnamed
739 * structure types, these have a unique name assigned during parsing.
741 if (!this->is_anonymous() && !b->is_anonymous())
742 if (strcmp(this->name, b->name) != 0)
745 for (unsigned i = 0; i < this->length; i++) {
746 if (this->fields.structure[i].type != b->fields.structure[i].type)
748 if (strcmp(this->fields.structure[i].name,
749 b->fields.structure[i].name) != 0)
751 if (this->fields.structure[i].matrix_layout
752 != b->fields.structure[i].matrix_layout)
754 if (this->fields.structure[i].location
755 != b->fields.structure[i].location)
757 if (this->fields.structure[i].interpolation
758 != b->fields.structure[i].interpolation)
760 if (this->fields.structure[i].centroid
761 != b->fields.structure[i].centroid)
763 if (this->fields.structure[i].sample
764 != b->fields.structure[i].sample)
766 if (this->fields.structure[i].patch
767 != b->fields.structure[i].patch)
769 if (this->fields.structure[i].image_read_only
770 != b->fields.structure[i].image_read_only)
772 if (this->fields.structure[i].image_write_only
773 != b->fields.structure[i].image_write_only)
775 if (this->fields.structure[i].image_coherent
776 != b->fields.structure[i].image_coherent)
778 if (this->fields.structure[i].image_volatile
779 != b->fields.structure[i].image_volatile)
781 if (this->fields.structure[i].image_restrict
782 != b->fields.structure[i].image_restrict)
784 if (this->fields.structure[i].precision
785 != b->fields.structure[i].precision)
794 glsl_type::record_key_compare(const void *a, const void *b)
796 const glsl_type *const key1 = (glsl_type *) a;
797 const glsl_type *const key2 = (glsl_type *) b;
799 return strcmp(key1->name, key2->name) == 0 && key1->record_compare(key2);
804 * Generate an integer hash value for a glsl_type structure type.
807 glsl_type::record_key_hash(const void *a)
809 const glsl_type *const key = (glsl_type *) a;
810 uintptr_t hash = key->length;
813 for (unsigned i = 0; i < key->length; i++) {
814 /* casting pointer to uintptr_t */
815 hash = (hash * 13 ) + (uintptr_t) key->fields.structure[i].type;
818 if (sizeof(hash) == 8)
819 retval = (hash & 0xffffffff) ^ ((uint64_t) hash >> 32);
828 glsl_type::get_record_instance(const glsl_struct_field *fields,
832 const glsl_type key(fields, num_fields, name);
834 mtx_lock(&glsl_type::mutex);
836 if (record_types == NULL) {
837 record_types = _mesa_hash_table_create(NULL, record_key_hash,
841 const struct hash_entry *entry = _mesa_hash_table_search(record_types,
844 mtx_unlock(&glsl_type::mutex);
845 const glsl_type *t = new glsl_type(fields, num_fields, name);
846 mtx_lock(&glsl_type::mutex);
848 entry = _mesa_hash_table_insert(record_types, t, (void *) t);
851 assert(((glsl_type *) entry->data)->base_type == GLSL_TYPE_STRUCT);
852 assert(((glsl_type *) entry->data)->length == num_fields);
853 assert(strcmp(((glsl_type *) entry->data)->name, name) == 0);
855 mtx_unlock(&glsl_type::mutex);
857 return (glsl_type *) entry->data;
862 glsl_type::get_interface_instance(const glsl_struct_field *fields,
864 enum glsl_interface_packing packing,
865 const char *block_name)
867 const glsl_type key(fields, num_fields, packing, block_name);
869 mtx_lock(&glsl_type::mutex);
871 if (interface_types == NULL) {
872 interface_types = _mesa_hash_table_create(NULL, record_key_hash,
876 const struct hash_entry *entry = _mesa_hash_table_search(interface_types,
879 mtx_unlock(&glsl_type::mutex);
880 const glsl_type *t = new glsl_type(fields, num_fields,
881 packing, block_name);
882 mtx_lock(&glsl_type::mutex);
884 entry = _mesa_hash_table_insert(interface_types, t, (void *) t);
887 assert(((glsl_type *) entry->data)->base_type == GLSL_TYPE_INTERFACE);
888 assert(((glsl_type *) entry->data)->length == num_fields);
889 assert(strcmp(((glsl_type *) entry->data)->name, block_name) == 0);
891 mtx_unlock(&glsl_type::mutex);
893 return (glsl_type *) entry->data;
897 glsl_type::get_subroutine_instance(const char *subroutine_name)
899 const glsl_type key(subroutine_name);
901 mtx_lock(&glsl_type::mutex);
903 if (subroutine_types == NULL) {
904 subroutine_types = _mesa_hash_table_create(NULL, record_key_hash,
908 const struct hash_entry *entry = _mesa_hash_table_search(subroutine_types,
911 mtx_unlock(&glsl_type::mutex);
912 const glsl_type *t = new glsl_type(subroutine_name);
913 mtx_lock(&glsl_type::mutex);
915 entry = _mesa_hash_table_insert(subroutine_types, t, (void *) t);
918 assert(((glsl_type *) entry->data)->base_type == GLSL_TYPE_SUBROUTINE);
919 assert(strcmp(((glsl_type *) entry->data)->name, subroutine_name) == 0);
921 mtx_unlock(&glsl_type::mutex);
923 return (glsl_type *) entry->data;
928 glsl_type::get_mul_type(const glsl_type *type_a, const glsl_type *type_b)
930 if (type_a == type_b) {
932 } else if (type_a->is_matrix() && type_b->is_matrix()) {
933 /* Matrix multiply. The columns of A must match the rows of B. Given
934 * the other previously tested constraints, this means the vector type
935 * of a row from A must be the same as the vector type of a column from
938 if (type_a->row_type() == type_b->column_type()) {
939 /* The resulting matrix has the number of columns of matrix B and
940 * the number of rows of matrix A. We get the row count of A by
941 * looking at the size of a vector that makes up a column. The
942 * transpose (size of a row) is done for B.
944 const glsl_type *const type =
945 get_instance(type_a->base_type,
946 type_a->column_type()->vector_elements,
947 type_b->row_type()->vector_elements);
948 assert(type != error_type);
952 } else if (type_a->is_matrix()) {
953 /* A is a matrix and B is a column vector. Columns of A must match
954 * rows of B. Given the other previously tested constraints, this
955 * means the vector type of a row from A must be the same as the
956 * vector the type of B.
958 if (type_a->row_type() == type_b) {
959 /* The resulting vector has a number of elements equal to
960 * the number of rows of matrix A. */
961 const glsl_type *const type =
962 get_instance(type_a->base_type,
963 type_a->column_type()->vector_elements,
965 assert(type != error_type);
970 assert(type_b->is_matrix());
972 /* A is a row vector and B is a matrix. Columns of A must match rows
973 * of B. Given the other previously tested constraints, this means
974 * the type of A must be the same as the vector type of a column from
977 if (type_a == type_b->column_type()) {
978 /* The resulting vector has a number of elements equal to
979 * the number of columns of matrix B. */
980 const glsl_type *const type =
981 get_instance(type_a->base_type,
982 type_b->row_type()->vector_elements,
984 assert(type != error_type);
995 glsl_type::field_type(const char *name) const
997 if (this->base_type != GLSL_TYPE_STRUCT
998 && this->base_type != GLSL_TYPE_INTERFACE)
1001 for (unsigned i = 0; i < this->length; i++) {
1002 if (strcmp(name, this->fields.structure[i].name) == 0)
1003 return this->fields.structure[i].type;
1011 glsl_type::field_index(const char *name) const
1013 if (this->base_type != GLSL_TYPE_STRUCT
1014 && this->base_type != GLSL_TYPE_INTERFACE)
1017 for (unsigned i = 0; i < this->length; i++) {
1018 if (strcmp(name, this->fields.structure[i].name) == 0)
1027 glsl_type::component_slots() const
1029 switch (this->base_type) {
1030 case GLSL_TYPE_UINT:
1032 case GLSL_TYPE_FLOAT:
1033 case GLSL_TYPE_BOOL:
1034 return this->components();
1036 case GLSL_TYPE_DOUBLE:
1037 return 2 * this->components();
1039 case GLSL_TYPE_STRUCT:
1040 case GLSL_TYPE_INTERFACE: {
1043 for (unsigned i = 0; i < this->length; i++)
1044 size += this->fields.structure[i].type->component_slots();
1049 case GLSL_TYPE_ARRAY:
1050 return this->length * this->fields.array->component_slots();
1052 case GLSL_TYPE_IMAGE:
1054 case GLSL_TYPE_SUBROUTINE:
1056 case GLSL_TYPE_SAMPLER:
1057 case GLSL_TYPE_ATOMIC_UINT:
1058 case GLSL_TYPE_VOID:
1059 case GLSL_TYPE_ERROR:
1067 glsl_type::record_location_offset(unsigned length) const
1069 unsigned offset = 0;
1070 const glsl_type *t = this->without_array();
1071 if (t->is_record()) {
1072 assert(length <= t->length);
1074 for (unsigned i = 0; i < length; i++) {
1075 const glsl_type *st = t->fields.structure[i].type;
1076 const glsl_type *wa = st->without_array();
1077 if (wa->is_record()) {
1078 unsigned r_offset = wa->record_location_offset(wa->length);
1079 offset += st->is_array() ?
1080 st->arrays_of_arrays_size() * r_offset : r_offset;
1081 } else if (st->is_array() && st->fields.array->is_array()) {
1082 unsigned outer_array_size = st->length;
1083 const glsl_type *base_type = st->fields.array;
1085 /* For arrays of arrays the outer arrays take up a uniform
1086 * slot for each element. The innermost array elements share a
1087 * single slot so we ignore the innermost array when calculating
1090 while (base_type->fields.array->is_array()) {
1091 outer_array_size = outer_array_size * base_type->length;
1092 base_type = base_type->fields.array;
1094 offset += outer_array_size;
1096 /* We dont worry about arrays here because unless the array
1097 * contains a structure or another array it only takes up a single
1108 glsl_type::uniform_locations() const
1112 switch (this->base_type) {
1113 case GLSL_TYPE_UINT:
1115 case GLSL_TYPE_FLOAT:
1116 case GLSL_TYPE_DOUBLE:
1117 case GLSL_TYPE_BOOL:
1118 case GLSL_TYPE_SAMPLER:
1119 case GLSL_TYPE_IMAGE:
1120 case GLSL_TYPE_SUBROUTINE:
1123 case GLSL_TYPE_STRUCT:
1124 case GLSL_TYPE_INTERFACE:
1125 for (unsigned i = 0; i < this->length; i++)
1126 size += this->fields.structure[i].type->uniform_locations();
1128 case GLSL_TYPE_ARRAY:
1129 return this->length * this->fields.array->uniform_locations();
1136 glsl_type::can_implicitly_convert_to(const glsl_type *desired,
1137 _mesa_glsl_parse_state *state) const
1139 if (this == desired)
1142 /* ESSL does not allow implicit conversions. If there is no state, we're
1143 * doing intra-stage function linking where these checks have already been
1146 if (state && state->es_shader)
1149 /* There is no conversion among matrix types. */
1150 if (this->matrix_columns > 1 || desired->matrix_columns > 1)
1153 /* Vector size must match. */
1154 if (this->vector_elements != desired->vector_elements)
1157 /* int and uint can be converted to float. */
1158 if (desired->is_float() && this->is_integer())
1161 /* With GLSL 4.0 / ARB_gpu_shader5, int can be converted to uint.
1162 * Note that state may be NULL here, when resolving function calls in the
1163 * linker. By this time, all the state-dependent checks have already
1164 * happened though, so allow anything that's allowed in any shader version. */
1165 if ((!state || state->is_version(400, 0) || state->ARB_gpu_shader5_enable) &&
1166 desired->base_type == GLSL_TYPE_UINT && this->base_type == GLSL_TYPE_INT)
1169 /* No implicit conversions from double. */
1170 if ((!state || state->has_double()) && this->is_double())
1173 /* Conversions from different types to double. */
1174 if ((!state || state->has_double()) && desired->is_double()) {
1175 if (this->is_float())
1177 if (this->is_integer())
1185 glsl_type::std140_base_alignment(bool row_major) const
1187 unsigned N = is_double() ? 8 : 4;
1189 /* (1) If the member is a scalar consuming <N> basic machine units, the
1190 * base alignment is <N>.
1192 * (2) If the member is a two- or four-component vector with components
1193 * consuming <N> basic machine units, the base alignment is 2<N> or
1194 * 4<N>, respectively.
1196 * (3) If the member is a three-component vector with components consuming
1197 * <N> basic machine units, the base alignment is 4<N>.
1199 if (this->is_scalar() || this->is_vector()) {
1200 switch (this->vector_elements) {
1211 /* (4) If the member is an array of scalars or vectors, the base alignment
1212 * and array stride are set to match the base alignment of a single
1213 * array element, according to rules (1), (2), and (3), and rounded up
1214 * to the base alignment of a vec4. The array may have padding at the
1215 * end; the base offset of the member following the array is rounded up
1216 * to the next multiple of the base alignment.
1218 * (6) If the member is an array of <S> column-major matrices with <C>
1219 * columns and <R> rows, the matrix is stored identically to a row of
1220 * <S>*<C> column vectors with <R> components each, according to rule
1223 * (8) If the member is an array of <S> row-major matrices with <C> columns
1224 * and <R> rows, the matrix is stored identically to a row of <S>*<R>
1225 * row vectors with <C> components each, according to rule (4).
1227 * (10) If the member is an array of <S> structures, the <S> elements of
1228 * the array are laid out in order, according to rule (9).
1230 if (this->is_array()) {
1231 if (this->fields.array->is_scalar() ||
1232 this->fields.array->is_vector() ||
1233 this->fields.array->is_matrix()) {
1234 return MAX2(this->fields.array->std140_base_alignment(row_major), 16);
1236 assert(this->fields.array->is_record() ||
1237 this->fields.array->is_array());
1238 return this->fields.array->std140_base_alignment(row_major);
1242 /* (5) If the member is a column-major matrix with <C> columns and
1243 * <R> rows, the matrix is stored identically to an array of
1244 * <C> column vectors with <R> components each, according to
1247 * (7) If the member is a row-major matrix with <C> columns and <R>
1248 * rows, the matrix is stored identically to an array of <R>
1249 * row vectors with <C> components each, according to rule (4).
1251 if (this->is_matrix()) {
1252 const struct glsl_type *vec_type, *array_type;
1253 int c = this->matrix_columns;
1254 int r = this->vector_elements;
1257 vec_type = get_instance(base_type, c, 1);
1258 array_type = glsl_type::get_array_instance(vec_type, r);
1260 vec_type = get_instance(base_type, r, 1);
1261 array_type = glsl_type::get_array_instance(vec_type, c);
1264 return array_type->std140_base_alignment(false);
1267 /* (9) If the member is a structure, the base alignment of the
1268 * structure is <N>, where <N> is the largest base alignment
1269 * value of any of its members, and rounded up to the base
1270 * alignment of a vec4. The individual members of this
1271 * sub-structure are then assigned offsets by applying this set
1272 * of rules recursively, where the base offset of the first
1273 * member of the sub-structure is equal to the aligned offset
1274 * of the structure. The structure may have padding at the end;
1275 * the base offset of the member following the sub-structure is
1276 * rounded up to the next multiple of the base alignment of the
1279 if (this->is_record()) {
1280 unsigned base_alignment = 16;
1281 for (unsigned i = 0; i < this->length; i++) {
1282 bool field_row_major = row_major;
1283 const enum glsl_matrix_layout matrix_layout =
1284 glsl_matrix_layout(this->fields.structure[i].matrix_layout);
1285 if (matrix_layout == GLSL_MATRIX_LAYOUT_ROW_MAJOR) {
1286 field_row_major = true;
1287 } else if (matrix_layout == GLSL_MATRIX_LAYOUT_COLUMN_MAJOR) {
1288 field_row_major = false;
1291 const struct glsl_type *field_type = this->fields.structure[i].type;
1292 base_alignment = MAX2(base_alignment,
1293 field_type->std140_base_alignment(field_row_major));
1295 return base_alignment;
1298 assert(!"not reached");
1303 glsl_type::std140_size(bool row_major) const
1305 unsigned N = is_double() ? 8 : 4;
1307 /* (1) If the member is a scalar consuming <N> basic machine units, the
1308 * base alignment is <N>.
1310 * (2) If the member is a two- or four-component vector with components
1311 * consuming <N> basic machine units, the base alignment is 2<N> or
1312 * 4<N>, respectively.
1314 * (3) If the member is a three-component vector with components consuming
1315 * <N> basic machine units, the base alignment is 4<N>.
1317 if (this->is_scalar() || this->is_vector()) {
1318 return this->vector_elements * N;
1321 /* (5) If the member is a column-major matrix with <C> columns and
1322 * <R> rows, the matrix is stored identically to an array of
1323 * <C> column vectors with <R> components each, according to
1326 * (6) If the member is an array of <S> column-major matrices with <C>
1327 * columns and <R> rows, the matrix is stored identically to a row of
1328 * <S>*<C> column vectors with <R> components each, according to rule
1331 * (7) If the member is a row-major matrix with <C> columns and <R>
1332 * rows, the matrix is stored identically to an array of <R>
1333 * row vectors with <C> components each, according to rule (4).
1335 * (8) If the member is an array of <S> row-major matrices with <C> columns
1336 * and <R> rows, the matrix is stored identically to a row of <S>*<R>
1337 * row vectors with <C> components each, according to rule (4).
1339 if (this->without_array()->is_matrix()) {
1340 const struct glsl_type *element_type;
1341 const struct glsl_type *vec_type;
1342 unsigned int array_len;
1344 if (this->is_array()) {
1345 element_type = this->without_array();
1346 array_len = this->arrays_of_arrays_size();
1348 element_type = this;
1353 vec_type = get_instance(element_type->base_type,
1354 element_type->matrix_columns, 1);
1356 array_len *= element_type->vector_elements;
1358 vec_type = get_instance(element_type->base_type,
1359 element_type->vector_elements, 1);
1360 array_len *= element_type->matrix_columns;
1362 const glsl_type *array_type = glsl_type::get_array_instance(vec_type,
1365 return array_type->std140_size(false);
1368 /* (4) If the member is an array of scalars or vectors, the base alignment
1369 * and array stride are set to match the base alignment of a single
1370 * array element, according to rules (1), (2), and (3), and rounded up
1371 * to the base alignment of a vec4. The array may have padding at the
1372 * end; the base offset of the member following the array is rounded up
1373 * to the next multiple of the base alignment.
1375 * (10) If the member is an array of <S> structures, the <S> elements of
1376 * the array are laid out in order, according to rule (9).
1378 if (this->is_array()) {
1379 if (this->without_array()->is_record()) {
1380 return this->arrays_of_arrays_size() *
1381 this->without_array()->std140_size(row_major);
1383 unsigned element_base_align =
1384 this->without_array()->std140_base_alignment(row_major);
1385 return this->arrays_of_arrays_size() * MAX2(element_base_align, 16);
1389 /* (9) If the member is a structure, the base alignment of the
1390 * structure is <N>, where <N> is the largest base alignment
1391 * value of any of its members, and rounded up to the base
1392 * alignment of a vec4. The individual members of this
1393 * sub-structure are then assigned offsets by applying this set
1394 * of rules recursively, where the base offset of the first
1395 * member of the sub-structure is equal to the aligned offset
1396 * of the structure. The structure may have padding at the end;
1397 * the base offset of the member following the sub-structure is
1398 * rounded up to the next multiple of the base alignment of the
1401 if (this->is_record() || this->is_interface()) {
1403 unsigned max_align = 0;
1405 for (unsigned i = 0; i < this->length; i++) {
1406 bool field_row_major = row_major;
1407 const enum glsl_matrix_layout matrix_layout =
1408 glsl_matrix_layout(this->fields.structure[i].matrix_layout);
1409 if (matrix_layout == GLSL_MATRIX_LAYOUT_ROW_MAJOR) {
1410 field_row_major = true;
1411 } else if (matrix_layout == GLSL_MATRIX_LAYOUT_COLUMN_MAJOR) {
1412 field_row_major = false;
1415 const struct glsl_type *field_type = this->fields.structure[i].type;
1416 unsigned align = field_type->std140_base_alignment(field_row_major);
1418 /* Ignore unsized arrays when calculating size */
1419 if (field_type->is_unsized_array())
1422 size = glsl_align(size, align);
1423 size += field_type->std140_size(field_row_major);
1425 max_align = MAX2(align, max_align);
1427 if (field_type->is_record() && (i + 1 < this->length))
1428 size = glsl_align(size, 16);
1430 size = glsl_align(size, MAX2(max_align, 16));
1434 assert(!"not reached");
1439 glsl_type::std430_base_alignment(bool row_major) const
1442 unsigned N = is_double() ? 8 : 4;
1444 /* (1) If the member is a scalar consuming <N> basic machine units, the
1445 * base alignment is <N>.
1447 * (2) If the member is a two- or four-component vector with components
1448 * consuming <N> basic machine units, the base alignment is 2<N> or
1449 * 4<N>, respectively.
1451 * (3) If the member is a three-component vector with components consuming
1452 * <N> basic machine units, the base alignment is 4<N>.
1454 if (this->is_scalar() || this->is_vector()) {
1455 switch (this->vector_elements) {
1466 /* OpenGL 4.30 spec, section 7.6.2.2 "Standard Uniform Block Layout":
1468 * "When using the std430 storage layout, shader storage blocks will be
1469 * laid out in buffer storage identically to uniform and shader storage
1470 * blocks using the std140 layout, except that the base alignment and
1471 * stride of arrays of scalars and vectors in rule 4 and of structures
1472 * in rule 9 are not rounded up a multiple of the base alignment of a vec4.
1475 /* (1) If the member is a scalar consuming <N> basic machine units, the
1476 * base alignment is <N>.
1478 * (2) If the member is a two- or four-component vector with components
1479 * consuming <N> basic machine units, the base alignment is 2<N> or
1480 * 4<N>, respectively.
1482 * (3) If the member is a three-component vector with components consuming
1483 * <N> basic machine units, the base alignment is 4<N>.
1485 if (this->is_array())
1486 return this->fields.array->std430_base_alignment(row_major);
1488 /* (5) If the member is a column-major matrix with <C> columns and
1489 * <R> rows, the matrix is stored identically to an array of
1490 * <C> column vectors with <R> components each, according to
1493 * (7) If the member is a row-major matrix with <C> columns and <R>
1494 * rows, the matrix is stored identically to an array of <R>
1495 * row vectors with <C> components each, according to rule (4).
1497 if (this->is_matrix()) {
1498 const struct glsl_type *vec_type, *array_type;
1499 int c = this->matrix_columns;
1500 int r = this->vector_elements;
1503 vec_type = get_instance(base_type, c, 1);
1504 array_type = glsl_type::get_array_instance(vec_type, r);
1506 vec_type = get_instance(base_type, r, 1);
1507 array_type = glsl_type::get_array_instance(vec_type, c);
1510 return array_type->std430_base_alignment(false);
1513 /* (9) If the member is a structure, the base alignment of the
1514 * structure is <N>, where <N> is the largest base alignment
1515 * value of any of its members, and rounded up to the base
1516 * alignment of a vec4. The individual members of this
1517 * sub-structure are then assigned offsets by applying this set
1518 * of rules recursively, where the base offset of the first
1519 * member of the sub-structure is equal to the aligned offset
1520 * of the structure. The structure may have padding at the end;
1521 * the base offset of the member following the sub-structure is
1522 * rounded up to the next multiple of the base alignment of the
1525 if (this->is_record()) {
1526 unsigned base_alignment = 0;
1527 for (unsigned i = 0; i < this->length; i++) {
1528 bool field_row_major = row_major;
1529 const enum glsl_matrix_layout matrix_layout =
1530 glsl_matrix_layout(this->fields.structure[i].matrix_layout);
1531 if (matrix_layout == GLSL_MATRIX_LAYOUT_ROW_MAJOR) {
1532 field_row_major = true;
1533 } else if (matrix_layout == GLSL_MATRIX_LAYOUT_COLUMN_MAJOR) {
1534 field_row_major = false;
1537 const struct glsl_type *field_type = this->fields.structure[i].type;
1538 base_alignment = MAX2(base_alignment,
1539 field_type->std430_base_alignment(field_row_major));
1541 assert(base_alignment > 0);
1542 return base_alignment;
1544 assert(!"not reached");
1549 glsl_type::std430_array_stride(bool row_major) const
1551 unsigned N = is_double() ? 8 : 4;
1553 /* Notice that the array stride of a vec3 is not 3 * N but 4 * N.
1554 * See OpenGL 4.30 spec, section 7.6.2.2 "Standard Uniform Block Layout"
1556 * (3) If the member is a three-component vector with components consuming
1557 * <N> basic machine units, the base alignment is 4<N>.
1559 if (this->is_vector() && this->vector_elements == 3)
1562 /* By default use std430_size(row_major) */
1563 return this->std430_size(row_major);
1567 glsl_type::std430_size(bool row_major) const
1569 unsigned N = is_double() ? 8 : 4;
1571 /* OpenGL 4.30 spec, section 7.6.2.2 "Standard Uniform Block Layout":
1573 * "When using the std430 storage layout, shader storage blocks will be
1574 * laid out in buffer storage identically to uniform and shader storage
1575 * blocks using the std140 layout, except that the base alignment and
1576 * stride of arrays of scalars and vectors in rule 4 and of structures
1577 * in rule 9 are not rounded up a multiple of the base alignment of a vec4.
1579 if (this->is_scalar() || this->is_vector())
1580 return this->vector_elements * N;
1582 if (this->without_array()->is_matrix()) {
1583 const struct glsl_type *element_type;
1584 const struct glsl_type *vec_type;
1585 unsigned int array_len;
1587 if (this->is_array()) {
1588 element_type = this->without_array();
1589 array_len = this->arrays_of_arrays_size();
1591 element_type = this;
1596 vec_type = get_instance(element_type->base_type,
1597 element_type->matrix_columns, 1);
1599 array_len *= element_type->vector_elements;
1601 vec_type = get_instance(element_type->base_type,
1602 element_type->vector_elements, 1);
1603 array_len *= element_type->matrix_columns;
1605 const glsl_type *array_type = glsl_type::get_array_instance(vec_type,
1608 return array_type->std430_size(false);
1611 if (this->is_array()) {
1612 if (this->without_array()->is_record())
1613 return this->arrays_of_arrays_size() *
1614 this->without_array()->std430_size(row_major);
1616 return this->arrays_of_arrays_size() *
1617 this->without_array()->std430_base_alignment(row_major);
1620 if (this->is_record() || this->is_interface()) {
1622 unsigned max_align = 0;
1624 for (unsigned i = 0; i < this->length; i++) {
1625 bool field_row_major = row_major;
1626 const enum glsl_matrix_layout matrix_layout =
1627 glsl_matrix_layout(this->fields.structure[i].matrix_layout);
1628 if (matrix_layout == GLSL_MATRIX_LAYOUT_ROW_MAJOR) {
1629 field_row_major = true;
1630 } else if (matrix_layout == GLSL_MATRIX_LAYOUT_COLUMN_MAJOR) {
1631 field_row_major = false;
1634 const struct glsl_type *field_type = this->fields.structure[i].type;
1635 unsigned align = field_type->std430_base_alignment(field_row_major);
1636 size = glsl_align(size, align);
1637 size += field_type->std430_size(field_row_major);
1639 max_align = MAX2(align, max_align);
1641 size = glsl_align(size, max_align);
1645 assert(!"not reached");
1650 glsl_type::count_attribute_slots(bool vertex_input_slots) const
1652 /* From page 31 (page 37 of the PDF) of the GLSL 1.50 spec:
1654 * "A scalar input counts the same amount against this limit as a vec4,
1655 * so applications may want to consider packing groups of four
1656 * unrelated float inputs together into a vector to better utilize the
1657 * capabilities of the underlying hardware. A matrix input will use up
1658 * multiple locations. The number of locations used will equal the
1659 * number of columns in the matrix."
1661 * The spec does not explicitly say how arrays are counted. However, it
1662 * should be safe to assume the total number of slots consumed by an array
1663 * is the number of entries in the array multiplied by the number of slots
1664 * consumed by a single element of the array.
1666 * The spec says nothing about how structs are counted, because vertex
1667 * attributes are not allowed to be (or contain) structs. However, Mesa
1668 * allows varying structs, the number of varying slots taken up by a
1669 * varying struct is simply equal to the sum of the number of slots taken
1670 * up by each element.
1672 * Doubles are counted different depending on whether they are vertex
1673 * inputs or everything else. Vertex inputs from ARB_vertex_attrib_64bit
1674 * take one location no matter what size they are, otherwise dvec3/4
1675 * take two locations.
1677 switch (this->base_type) {
1678 case GLSL_TYPE_UINT:
1680 case GLSL_TYPE_FLOAT:
1681 case GLSL_TYPE_BOOL:
1682 return this->matrix_columns;
1683 case GLSL_TYPE_DOUBLE:
1684 if (this->vector_elements > 2 && !vertex_input_slots)
1685 return this->matrix_columns * 2;
1687 return this->matrix_columns;
1688 case GLSL_TYPE_STRUCT:
1689 case GLSL_TYPE_INTERFACE: {
1692 for (unsigned i = 0; i < this->length; i++)
1693 size += this->fields.structure[i].type->count_attribute_slots(vertex_input_slots);
1698 case GLSL_TYPE_ARRAY:
1699 return this->length * this->fields.array->count_attribute_slots(vertex_input_slots);
1701 case GLSL_TYPE_SAMPLER:
1702 case GLSL_TYPE_IMAGE:
1703 case GLSL_TYPE_ATOMIC_UINT:
1704 case GLSL_TYPE_VOID:
1705 case GLSL_TYPE_SUBROUTINE:
1706 case GLSL_TYPE_ERROR:
1710 assert(!"Unexpected type in count_attribute_slots()");
1716 glsl_type::coordinate_components() const
1720 switch (sampler_dimensionality) {
1721 case GLSL_SAMPLER_DIM_1D:
1722 case GLSL_SAMPLER_DIM_BUF:
1725 case GLSL_SAMPLER_DIM_2D:
1726 case GLSL_SAMPLER_DIM_RECT:
1727 case GLSL_SAMPLER_DIM_MS:
1728 case GLSL_SAMPLER_DIM_EXTERNAL:
1731 case GLSL_SAMPLER_DIM_3D:
1732 case GLSL_SAMPLER_DIM_CUBE:
1736 assert(!"Should not get here.");
1741 /* Array textures need an additional component for the array index, except
1742 * for cubemap array images that behave like a 2D array of interleaved
1745 if (sampler_array &&
1746 !(base_type == GLSL_TYPE_IMAGE &&
1747 sampler_dimensionality == GLSL_SAMPLER_DIM_CUBE))
1754 * Declarations of type flyweights (glsl_type::_foo_type) and
1755 * convenience pointers (glsl_type::foo_type).
1758 #define DECL_TYPE(NAME, ...) \
1759 const glsl_type glsl_type::_##NAME##_type = glsl_type(__VA_ARGS__, #NAME); \
1760 const glsl_type *const glsl_type::NAME##_type = &glsl_type::_##NAME##_type;
1762 #define STRUCT_TYPE(NAME)
1764 #include "compiler/builtin_type_macros.h"