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::hash_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::function_types = NULL;
36 hash_table *glsl_type::subroutine_types = NULL;
38 glsl_type::glsl_type(GLenum gl_type,
39 glsl_base_type base_type, unsigned vector_elements,
40 unsigned matrix_columns, const char *name) :
42 base_type(base_type), sampled_type(GLSL_TYPE_VOID),
43 sampler_dimensionality(0), sampler_shadow(0), sampler_array(0),
44 interface_packing(0), interface_row_major(0),
45 vector_elements(vector_elements), matrix_columns(matrix_columns),
48 /* Values of these types must fit in the two bits of
49 * glsl_type::sampled_type.
51 STATIC_ASSERT((unsigned(GLSL_TYPE_UINT) & 3) == unsigned(GLSL_TYPE_UINT));
52 STATIC_ASSERT((unsigned(GLSL_TYPE_INT) & 3) == unsigned(GLSL_TYPE_INT));
53 STATIC_ASSERT((unsigned(GLSL_TYPE_FLOAT) & 3) == unsigned(GLSL_TYPE_FLOAT));
55 ASSERT_BITFIELD_SIZE(glsl_type, base_type, GLSL_TYPE_ERROR);
56 ASSERT_BITFIELD_SIZE(glsl_type, sampled_type, GLSL_TYPE_ERROR);
57 ASSERT_BITFIELD_SIZE(glsl_type, sampler_dimensionality,
58 GLSL_SAMPLER_DIM_SUBPASS_MS);
60 this->mem_ctx = ralloc_context(NULL);
61 assert(this->mem_ctx != NULL);
64 this->name = ralloc_strdup(this->mem_ctx, name);
66 /* Neither dimension is zero or both dimensions are zero.
68 assert((vector_elements == 0) == (matrix_columns == 0));
69 memset(& fields, 0, sizeof(fields));
72 glsl_type::glsl_type(GLenum gl_type, glsl_base_type base_type,
73 enum glsl_sampler_dim dim, bool shadow, bool array,
74 glsl_base_type type, const char *name) :
76 base_type(base_type), sampled_type(type),
77 sampler_dimensionality(dim), sampler_shadow(shadow),
78 sampler_array(array), interface_packing(0),
79 interface_row_major(0), length(0)
81 this->mem_ctx = ralloc_context(NULL);
82 assert(this->mem_ctx != NULL);
85 this->name = ralloc_strdup(this->mem_ctx, name);
87 memset(& fields, 0, sizeof(fields));
89 matrix_columns = vector_elements = 1;
92 glsl_type::glsl_type(const glsl_struct_field *fields, unsigned num_fields,
95 base_type(GLSL_TYPE_STRUCT), sampled_type(GLSL_TYPE_VOID),
96 sampler_dimensionality(0), sampler_shadow(0), sampler_array(0),
97 interface_packing(0), interface_row_major(0),
98 vector_elements(0), matrix_columns(0),
103 this->mem_ctx = ralloc_context(NULL);
104 assert(this->mem_ctx != NULL);
106 assert(name != NULL);
107 this->name = ralloc_strdup(this->mem_ctx, name);
108 /* Zero-fill to prevent spurious Valgrind errors when serializing NIR
109 * due to uninitialized unused bits in bit fields. */
110 this->fields.structure = rzalloc_array(this->mem_ctx,
111 glsl_struct_field, length);
113 for (i = 0; i < length; i++) {
114 this->fields.structure[i] = fields[i];
115 this->fields.structure[i].name = ralloc_strdup(this->fields.structure,
120 glsl_type::glsl_type(const glsl_struct_field *fields, unsigned num_fields,
121 enum glsl_interface_packing packing,
122 bool row_major, const char *name) :
124 base_type(GLSL_TYPE_INTERFACE), sampled_type(GLSL_TYPE_VOID),
125 sampler_dimensionality(0), sampler_shadow(0), sampler_array(0),
126 interface_packing((unsigned) packing),
127 interface_row_major((unsigned) row_major),
128 vector_elements(0), matrix_columns(0),
133 this->mem_ctx = ralloc_context(NULL);
134 assert(this->mem_ctx != NULL);
136 assert(name != NULL);
137 this->name = ralloc_strdup(this->mem_ctx, name);
138 this->fields.structure = rzalloc_array(this->mem_ctx,
139 glsl_struct_field, length);
140 for (i = 0; i < length; i++) {
141 this->fields.structure[i] = fields[i];
142 this->fields.structure[i].name = ralloc_strdup(this->fields.structure,
147 glsl_type::glsl_type(const glsl_type *return_type,
148 const glsl_function_param *params, unsigned num_params) :
150 base_type(GLSL_TYPE_FUNCTION), sampled_type(GLSL_TYPE_VOID),
151 sampler_dimensionality(0), sampler_shadow(0), sampler_array(0),
152 interface_packing(0), interface_row_major(0),
153 vector_elements(0), matrix_columns(0),
158 this->mem_ctx = ralloc_context(NULL);
159 assert(this->mem_ctx != NULL);
161 this->fields.parameters = rzalloc_array(this->mem_ctx,
162 glsl_function_param, num_params + 1);
164 /* We store the return type as the first parameter */
165 this->fields.parameters[0].type = return_type;
166 this->fields.parameters[0].in = false;
167 this->fields.parameters[0].out = true;
169 /* We store the i'th parameter in slot i+1 */
170 for (i = 0; i < length; i++) {
171 this->fields.parameters[i + 1].type = params[i].type;
172 this->fields.parameters[i + 1].in = params[i].in;
173 this->fields.parameters[i + 1].out = params[i].out;
177 glsl_type::glsl_type(const char *subroutine_name) :
179 base_type(GLSL_TYPE_SUBROUTINE), sampled_type(GLSL_TYPE_VOID),
180 sampler_dimensionality(0), sampler_shadow(0), sampler_array(0),
181 interface_packing(0), interface_row_major(0),
182 vector_elements(1), matrix_columns(1),
185 this->mem_ctx = ralloc_context(NULL);
186 assert(this->mem_ctx != NULL);
188 assert(subroutine_name != NULL);
189 this->name = ralloc_strdup(this->mem_ctx, subroutine_name);
192 glsl_type::~glsl_type()
194 ralloc_free(this->mem_ctx);
198 glsl_type::contains_sampler() const
200 if (this->is_array()) {
201 return this->fields.array->contains_sampler();
202 } else if (this->is_record() || this->is_interface()) {
203 for (unsigned int i = 0; i < this->length; i++) {
204 if (this->fields.structure[i].type->contains_sampler())
209 return this->is_sampler();
214 glsl_type::contains_array() const
216 if (this->is_record() || this->is_interface()) {
217 for (unsigned int i = 0; i < this->length; i++) {
218 if (this->fields.structure[i].type->contains_array())
223 return this->is_array();
228 glsl_type::contains_integer() const
230 if (this->is_array()) {
231 return this->fields.array->contains_integer();
232 } else if (this->is_record() || this->is_interface()) {
233 for (unsigned int i = 0; i < this->length; i++) {
234 if (this->fields.structure[i].type->contains_integer())
239 return this->is_integer();
244 glsl_type::contains_double() const
246 if (this->is_array()) {
247 return this->fields.array->contains_double();
248 } else if (this->is_record() || this->is_interface()) {
249 for (unsigned int i = 0; i < this->length; i++) {
250 if (this->fields.structure[i].type->contains_double())
255 return this->is_double();
260 glsl_type::contains_opaque() const {
262 case GLSL_TYPE_SAMPLER:
263 case GLSL_TYPE_IMAGE:
264 case GLSL_TYPE_ATOMIC_UINT:
266 case GLSL_TYPE_ARRAY:
267 return fields.array->contains_opaque();
268 case GLSL_TYPE_STRUCT:
269 case GLSL_TYPE_INTERFACE:
270 for (unsigned int i = 0; i < length; i++) {
271 if (fields.structure[i].type->contains_opaque())
281 glsl_type::contains_subroutine() const
283 if (this->is_array()) {
284 return this->fields.array->contains_subroutine();
285 } else if (this->is_record() || this->is_interface()) {
286 for (unsigned int i = 0; i < this->length; i++) {
287 if (this->fields.structure[i].type->contains_subroutine())
292 return this->is_subroutine();
297 glsl_type::sampler_index() const
299 const glsl_type *const t = (this->is_array()) ? this->fields.array : this;
301 assert(t->is_sampler() || t->is_image());
303 switch (t->sampler_dimensionality) {
304 case GLSL_SAMPLER_DIM_1D:
305 return (t->sampler_array) ? TEXTURE_1D_ARRAY_INDEX : TEXTURE_1D_INDEX;
306 case GLSL_SAMPLER_DIM_2D:
307 return (t->sampler_array) ? TEXTURE_2D_ARRAY_INDEX : TEXTURE_2D_INDEX;
308 case GLSL_SAMPLER_DIM_3D:
309 return TEXTURE_3D_INDEX;
310 case GLSL_SAMPLER_DIM_CUBE:
311 return (t->sampler_array) ? TEXTURE_CUBE_ARRAY_INDEX : TEXTURE_CUBE_INDEX;
312 case GLSL_SAMPLER_DIM_RECT:
313 return TEXTURE_RECT_INDEX;
314 case GLSL_SAMPLER_DIM_BUF:
315 return TEXTURE_BUFFER_INDEX;
316 case GLSL_SAMPLER_DIM_EXTERNAL:
317 return TEXTURE_EXTERNAL_INDEX;
318 case GLSL_SAMPLER_DIM_MS:
319 return (t->sampler_array) ? TEXTURE_2D_MULTISAMPLE_ARRAY_INDEX : TEXTURE_2D_MULTISAMPLE_INDEX;
321 assert(!"Should not get here.");
322 return TEXTURE_BUFFER_INDEX;
327 glsl_type::contains_image() const
329 if (this->is_array()) {
330 return this->fields.array->contains_image();
331 } else if (this->is_record() || this->is_interface()) {
332 for (unsigned int i = 0; i < this->length; i++) {
333 if (this->fields.structure[i].type->contains_image())
338 return this->is_image();
342 const glsl_type *glsl_type::get_base_type() const
347 case GLSL_TYPE_UINT16:
348 return uint16_t_type;
349 case GLSL_TYPE_UINT8:
353 case GLSL_TYPE_INT16:
357 case GLSL_TYPE_FLOAT:
359 case GLSL_TYPE_FLOAT16:
360 return float16_t_type;
361 case GLSL_TYPE_DOUBLE:
365 case GLSL_TYPE_UINT64:
366 return uint64_t_type;
367 case GLSL_TYPE_INT64:
375 const glsl_type *glsl_type::get_scalar_type() const
377 const glsl_type *type = this;
380 while (type->base_type == GLSL_TYPE_ARRAY)
381 type = type->fields.array;
383 const glsl_type *scalar_type = type->get_base_type();
384 if (scalar_type == error_type)
392 hash_free_type_function(struct hash_entry *entry)
394 glsl_type *type = (glsl_type *) entry->data;
396 if (type->is_array())
397 free((void*)entry->key);
403 _mesa_glsl_release_types(void)
405 /* Should only be called during atexit (either when unloading shared
406 * object, or if process terminates), so no mutex-locking should be
409 if (glsl_type::array_types != NULL) {
410 _mesa_hash_table_destroy(glsl_type::array_types, hash_free_type_function);
411 glsl_type::array_types = NULL;
414 if (glsl_type::record_types != NULL) {
415 _mesa_hash_table_destroy(glsl_type::record_types, hash_free_type_function);
416 glsl_type::record_types = NULL;
419 if (glsl_type::interface_types != NULL) {
420 _mesa_hash_table_destroy(glsl_type::interface_types, hash_free_type_function);
421 glsl_type::interface_types = NULL;
424 if (glsl_type::function_types != NULL) {
425 _mesa_hash_table_destroy(glsl_type::function_types, hash_free_type_function);
426 glsl_type::function_types = NULL;
429 if (glsl_type::subroutine_types != NULL) {
430 _mesa_hash_table_destroy(glsl_type::subroutine_types, hash_free_type_function);
431 glsl_type::subroutine_types = NULL;
436 glsl_type::glsl_type(const glsl_type *array, unsigned length) :
437 base_type(GLSL_TYPE_ARRAY), sampled_type(GLSL_TYPE_VOID),
438 sampler_dimensionality(0), sampler_shadow(0), sampler_array(0),
439 interface_packing(0), interface_row_major(0),
440 vector_elements(0), matrix_columns(0),
441 length(length), name(NULL)
443 this->fields.array = array;
444 /* Inherit the gl type of the base. The GL type is used for
445 * uniform/statevar handling in Mesa and the arrayness of the type
446 * is represented by the size rather than the type.
448 this->gl_type = array->gl_type;
450 /* Allow a maximum of 10 characters for the array size. This is enough
451 * for 32-bits of ~0. The extra 3 are for the '[', ']', and terminating
454 const unsigned name_length = strlen(array->name) + 10 + 3;
456 this->mem_ctx = ralloc_context(NULL);
457 assert(this->mem_ctx != NULL);
459 char *const n = (char *) ralloc_size(this->mem_ctx, name_length);
462 snprintf(n, name_length, "%s[]", array->name);
464 /* insert outermost dimensions in the correct spot
465 * otherwise the dimension order will be backwards
467 const char *pos = strchr(array->name, '[');
469 int idx = pos - array->name;
470 snprintf(n, idx+1, "%s", array->name);
471 snprintf(n + idx, name_length - idx, "[%u]%s",
472 length, array->name + idx);
474 snprintf(n, name_length, "%s[%u]", array->name, length);
482 glsl_type::vec(unsigned components, const glsl_type *const ts[])
484 unsigned n = components;
488 else if (components == 16)
497 #define VECN(components, sname, vname) \
499 glsl_type:: vname (unsigned components) \
501 static const glsl_type *const ts[] = { \
502 sname ## _type, vname ## 2_type, \
503 vname ## 3_type, vname ## 4_type, \
504 vname ## 8_type, vname ## 16_type, \
506 return glsl_type::vec(components, ts); \
509 VECN(components, float, vec)
510 VECN(components, float16_t, f16vec)
511 VECN(components, double, dvec)
512 VECN(components, int, ivec)
513 VECN(components, uint, uvec)
514 VECN(components, bool, bvec)
515 VECN(components, int64_t, i64vec)
516 VECN(components, uint64_t, u64vec)
517 VECN(components, int16_t, i16vec)
518 VECN(components, uint16_t, u16vec)
519 VECN(components, int8_t, i8vec)
520 VECN(components, uint8_t, u8vec)
523 glsl_type::get_instance(unsigned base_type, unsigned rows, unsigned columns)
525 if (base_type == GLSL_TYPE_VOID)
528 /* Treat GLSL vectors as Nx1 matrices.
536 case GLSL_TYPE_FLOAT:
538 case GLSL_TYPE_FLOAT16:
540 case GLSL_TYPE_DOUBLE:
544 case GLSL_TYPE_UINT64:
546 case GLSL_TYPE_INT64:
548 case GLSL_TYPE_UINT16:
550 case GLSL_TYPE_INT16:
552 case GLSL_TYPE_UINT8:
560 if ((base_type != GLSL_TYPE_FLOAT &&
561 base_type != GLSL_TYPE_DOUBLE &&
562 base_type != GLSL_TYPE_FLOAT16) || (rows == 1))
565 /* GLSL matrix types are named mat{COLUMNS}x{ROWS}. Only the following
566 * combinations are valid:
574 #define IDX(c,r) (((c-1)*3) + (r-1))
577 case GLSL_TYPE_DOUBLE: {
578 switch (IDX(columns, rows)) {
579 case IDX(2,2): return dmat2_type;
580 case IDX(2,3): return dmat2x3_type;
581 case IDX(2,4): return dmat2x4_type;
582 case IDX(3,2): return dmat3x2_type;
583 case IDX(3,3): return dmat3_type;
584 case IDX(3,4): return dmat3x4_type;
585 case IDX(4,2): return dmat4x2_type;
586 case IDX(4,3): return dmat4x3_type;
587 case IDX(4,4): return dmat4_type;
588 default: return error_type;
591 case GLSL_TYPE_FLOAT: {
592 switch (IDX(columns, rows)) {
593 case IDX(2,2): return mat2_type;
594 case IDX(2,3): return mat2x3_type;
595 case IDX(2,4): return mat2x4_type;
596 case IDX(3,2): return mat3x2_type;
597 case IDX(3,3): return mat3_type;
598 case IDX(3,4): return mat3x4_type;
599 case IDX(4,2): return mat4x2_type;
600 case IDX(4,3): return mat4x3_type;
601 case IDX(4,4): return mat4_type;
602 default: return error_type;
605 case GLSL_TYPE_FLOAT16: {
606 switch (IDX(columns, rows)) {
607 case IDX(2,2): return f16mat2_type;
608 case IDX(2,3): return f16mat2x3_type;
609 case IDX(2,4): return f16mat2x4_type;
610 case IDX(3,2): return f16mat3x2_type;
611 case IDX(3,3): return f16mat3_type;
612 case IDX(3,4): return f16mat3x4_type;
613 case IDX(4,2): return f16mat4x2_type;
614 case IDX(4,3): return f16mat4x3_type;
615 case IDX(4,4): return f16mat4_type;
616 default: return error_type;
619 default: return error_type;
623 assert(!"Should not get here.");
628 glsl_type::get_sampler_instance(enum glsl_sampler_dim dim,
634 case GLSL_TYPE_FLOAT:
636 case GLSL_SAMPLER_DIM_1D:
638 return (array ? sampler1DArrayShadow_type : sampler1DShadow_type);
640 return (array ? sampler1DArray_type : sampler1D_type);
641 case GLSL_SAMPLER_DIM_2D:
643 return (array ? sampler2DArrayShadow_type : sampler2DShadow_type);
645 return (array ? sampler2DArray_type : sampler2D_type);
646 case GLSL_SAMPLER_DIM_3D:
650 return sampler3D_type;
651 case GLSL_SAMPLER_DIM_CUBE:
653 return (array ? samplerCubeArrayShadow_type : samplerCubeShadow_type);
655 return (array ? samplerCubeArray_type : samplerCube_type);
656 case GLSL_SAMPLER_DIM_RECT:
660 return sampler2DRectShadow_type;
662 return sampler2DRect_type;
663 case GLSL_SAMPLER_DIM_BUF:
667 return samplerBuffer_type;
668 case GLSL_SAMPLER_DIM_MS:
671 return (array ? sampler2DMSArray_type : sampler2DMS_type);
672 case GLSL_SAMPLER_DIM_EXTERNAL:
676 return samplerExternalOES_type;
677 case GLSL_SAMPLER_DIM_SUBPASS:
678 case GLSL_SAMPLER_DIM_SUBPASS_MS:
685 case GLSL_SAMPLER_DIM_1D:
686 return (array ? isampler1DArray_type : isampler1D_type);
687 case GLSL_SAMPLER_DIM_2D:
688 return (array ? isampler2DArray_type : isampler2D_type);
689 case GLSL_SAMPLER_DIM_3D:
692 return isampler3D_type;
693 case GLSL_SAMPLER_DIM_CUBE:
694 return (array ? isamplerCubeArray_type : isamplerCube_type);
695 case GLSL_SAMPLER_DIM_RECT:
698 return isampler2DRect_type;
699 case GLSL_SAMPLER_DIM_BUF:
702 return isamplerBuffer_type;
703 case GLSL_SAMPLER_DIM_MS:
704 return (array ? isampler2DMSArray_type : isampler2DMS_type);
705 case GLSL_SAMPLER_DIM_EXTERNAL:
707 case GLSL_SAMPLER_DIM_SUBPASS:
708 case GLSL_SAMPLER_DIM_SUBPASS_MS:
715 case GLSL_SAMPLER_DIM_1D:
716 return (array ? usampler1DArray_type : usampler1D_type);
717 case GLSL_SAMPLER_DIM_2D:
718 return (array ? usampler2DArray_type : usampler2D_type);
719 case GLSL_SAMPLER_DIM_3D:
722 return usampler3D_type;
723 case GLSL_SAMPLER_DIM_CUBE:
724 return (array ? usamplerCubeArray_type : usamplerCube_type);
725 case GLSL_SAMPLER_DIM_RECT:
728 return usampler2DRect_type;
729 case GLSL_SAMPLER_DIM_BUF:
732 return usamplerBuffer_type;
733 case GLSL_SAMPLER_DIM_MS:
734 return (array ? usampler2DMSArray_type : usampler2DMS_type);
735 case GLSL_SAMPLER_DIM_EXTERNAL:
737 case GLSL_SAMPLER_DIM_SUBPASS:
738 case GLSL_SAMPLER_DIM_SUBPASS_MS:
745 unreachable("switch statement above should be complete");
749 glsl_type::get_image_instance(enum glsl_sampler_dim dim,
750 bool array, glsl_base_type type)
753 case GLSL_TYPE_FLOAT:
755 case GLSL_SAMPLER_DIM_1D:
756 return (array ? image1DArray_type : image1D_type);
757 case GLSL_SAMPLER_DIM_2D:
758 return (array ? image2DArray_type : image2D_type);
759 case GLSL_SAMPLER_DIM_3D:
761 case GLSL_SAMPLER_DIM_CUBE:
762 return (array ? imageCubeArray_type : imageCube_type);
763 case GLSL_SAMPLER_DIM_RECT:
767 return image2DRect_type;
768 case GLSL_SAMPLER_DIM_BUF:
772 return imageBuffer_type;
773 case GLSL_SAMPLER_DIM_MS:
774 return (array ? image2DMSArray_type : image2DMS_type);
775 case GLSL_SAMPLER_DIM_SUBPASS:
776 return subpassInput_type;
777 case GLSL_SAMPLER_DIM_SUBPASS_MS:
778 return subpassInputMS_type;
779 case GLSL_SAMPLER_DIM_EXTERNAL:
784 case GLSL_SAMPLER_DIM_1D:
785 return (array ? iimage1DArray_type : iimage1D_type);
786 case GLSL_SAMPLER_DIM_2D:
787 return (array ? iimage2DArray_type : iimage2D_type);
788 case GLSL_SAMPLER_DIM_3D:
791 return iimage3D_type;
792 case GLSL_SAMPLER_DIM_CUBE:
793 return (array ? iimageCubeArray_type : iimageCube_type);
794 case GLSL_SAMPLER_DIM_RECT:
797 return iimage2DRect_type;
798 case GLSL_SAMPLER_DIM_BUF:
801 return iimageBuffer_type;
802 case GLSL_SAMPLER_DIM_MS:
803 return (array ? iimage2DMSArray_type : iimage2DMS_type);
804 case GLSL_SAMPLER_DIM_SUBPASS:
805 return isubpassInput_type;
806 case GLSL_SAMPLER_DIM_SUBPASS_MS:
807 return isubpassInputMS_type;
808 case GLSL_SAMPLER_DIM_EXTERNAL:
813 case GLSL_SAMPLER_DIM_1D:
814 return (array ? uimage1DArray_type : uimage1D_type);
815 case GLSL_SAMPLER_DIM_2D:
816 return (array ? uimage2DArray_type : uimage2D_type);
817 case GLSL_SAMPLER_DIM_3D:
820 return uimage3D_type;
821 case GLSL_SAMPLER_DIM_CUBE:
822 return (array ? uimageCubeArray_type : uimageCube_type);
823 case GLSL_SAMPLER_DIM_RECT:
826 return uimage2DRect_type;
827 case GLSL_SAMPLER_DIM_BUF:
830 return uimageBuffer_type;
831 case GLSL_SAMPLER_DIM_MS:
832 return (array ? uimage2DMSArray_type : uimage2DMS_type);
833 case GLSL_SAMPLER_DIM_SUBPASS:
834 return usubpassInput_type;
835 case GLSL_SAMPLER_DIM_SUBPASS_MS:
836 return usubpassInputMS_type;
837 case GLSL_SAMPLER_DIM_EXTERNAL:
844 unreachable("switch statement above should be complete");
848 glsl_type::get_array_instance(const glsl_type *base, unsigned array_size)
850 /* Generate a name using the base type pointer in the key. This is
851 * done because the name of the base type may not be unique across
852 * shaders. For example, two shaders may have different record types
856 snprintf(key, sizeof(key), "%p[%u]", (void *) base, array_size);
858 mtx_lock(&glsl_type::hash_mutex);
860 if (array_types == NULL) {
861 array_types = _mesa_hash_table_create(NULL, _mesa_key_hash_string,
862 _mesa_key_string_equal);
865 const struct hash_entry *entry = _mesa_hash_table_search(array_types, key);
867 const glsl_type *t = new glsl_type(base, array_size);
869 entry = _mesa_hash_table_insert(array_types,
874 assert(((glsl_type *) entry->data)->base_type == GLSL_TYPE_ARRAY);
875 assert(((glsl_type *) entry->data)->length == array_size);
876 assert(((glsl_type *) entry->data)->fields.array == base);
878 mtx_unlock(&glsl_type::hash_mutex);
880 return (glsl_type *) entry->data;
885 glsl_type::record_compare(const glsl_type *b, bool match_locations) const
887 if (this->length != b->length)
890 if (this->interface_packing != b->interface_packing)
893 if (this->interface_row_major != b->interface_row_major)
896 /* From the GLSL 4.20 specification (Sec 4.2):
898 * "Structures must have the same name, sequence of type names, and
899 * type definitions, and field names to be considered the same type."
901 * GLSL ES behaves the same (Ver 1.00 Sec 4.2.4, Ver 3.00 Sec 4.2.5).
903 if (strcmp(this->name, b->name) != 0)
906 for (unsigned i = 0; i < this->length; i++) {
907 if (this->fields.structure[i].type != b->fields.structure[i].type)
909 if (strcmp(this->fields.structure[i].name,
910 b->fields.structure[i].name) != 0)
912 if (this->fields.structure[i].matrix_layout
913 != b->fields.structure[i].matrix_layout)
915 if (match_locations && this->fields.structure[i].location
916 != b->fields.structure[i].location)
918 if (this->fields.structure[i].offset
919 != b->fields.structure[i].offset)
921 if (this->fields.structure[i].interpolation
922 != b->fields.structure[i].interpolation)
924 if (this->fields.structure[i].centroid
925 != b->fields.structure[i].centroid)
927 if (this->fields.structure[i].sample
928 != b->fields.structure[i].sample)
930 if (this->fields.structure[i].patch
931 != b->fields.structure[i].patch)
933 if (this->fields.structure[i].memory_read_only
934 != b->fields.structure[i].memory_read_only)
936 if (this->fields.structure[i].memory_write_only
937 != b->fields.structure[i].memory_write_only)
939 if (this->fields.structure[i].memory_coherent
940 != b->fields.structure[i].memory_coherent)
942 if (this->fields.structure[i].memory_volatile
943 != b->fields.structure[i].memory_volatile)
945 if (this->fields.structure[i].memory_restrict
946 != b->fields.structure[i].memory_restrict)
948 if (this->fields.structure[i].image_format
949 != b->fields.structure[i].image_format)
951 if (this->fields.structure[i].precision
952 != b->fields.structure[i].precision)
954 if (this->fields.structure[i].explicit_xfb_buffer
955 != b->fields.structure[i].explicit_xfb_buffer)
957 if (this->fields.structure[i].xfb_buffer
958 != b->fields.structure[i].xfb_buffer)
960 if (this->fields.structure[i].xfb_stride
961 != b->fields.structure[i].xfb_stride)
970 glsl_type::record_key_compare(const void *a, const void *b)
972 const glsl_type *const key1 = (glsl_type *) a;
973 const glsl_type *const key2 = (glsl_type *) b;
975 return strcmp(key1->name, key2->name) == 0 && key1->record_compare(key2);
980 * Generate an integer hash value for a glsl_type structure type.
983 glsl_type::record_key_hash(const void *a)
985 const glsl_type *const key = (glsl_type *) a;
986 uintptr_t hash = key->length;
989 for (unsigned i = 0; i < key->length; i++) {
990 /* casting pointer to uintptr_t */
991 hash = (hash * 13 ) + (uintptr_t) key->fields.structure[i].type;
994 if (sizeof(hash) == 8)
995 retval = (hash & 0xffffffff) ^ ((uint64_t) hash >> 32);
1004 glsl_type::get_record_instance(const glsl_struct_field *fields,
1005 unsigned num_fields,
1008 const glsl_type key(fields, num_fields, name);
1010 mtx_lock(&glsl_type::hash_mutex);
1012 if (record_types == NULL) {
1013 record_types = _mesa_hash_table_create(NULL, record_key_hash,
1014 record_key_compare);
1017 const struct hash_entry *entry = _mesa_hash_table_search(record_types,
1019 if (entry == NULL) {
1020 const glsl_type *t = new glsl_type(fields, num_fields, name);
1022 entry = _mesa_hash_table_insert(record_types, t, (void *) t);
1025 assert(((glsl_type *) entry->data)->base_type == GLSL_TYPE_STRUCT);
1026 assert(((glsl_type *) entry->data)->length == num_fields);
1027 assert(strcmp(((glsl_type *) entry->data)->name, name) == 0);
1029 mtx_unlock(&glsl_type::hash_mutex);
1031 return (glsl_type *) entry->data;
1036 glsl_type::get_interface_instance(const glsl_struct_field *fields,
1037 unsigned num_fields,
1038 enum glsl_interface_packing packing,
1040 const char *block_name)
1042 const glsl_type key(fields, num_fields, packing, row_major, block_name);
1044 mtx_lock(&glsl_type::hash_mutex);
1046 if (interface_types == NULL) {
1047 interface_types = _mesa_hash_table_create(NULL, record_key_hash,
1048 record_key_compare);
1051 const struct hash_entry *entry = _mesa_hash_table_search(interface_types,
1053 if (entry == NULL) {
1054 const glsl_type *t = new glsl_type(fields, num_fields,
1055 packing, row_major, block_name);
1057 entry = _mesa_hash_table_insert(interface_types, t, (void *) t);
1060 assert(((glsl_type *) entry->data)->base_type == GLSL_TYPE_INTERFACE);
1061 assert(((glsl_type *) entry->data)->length == num_fields);
1062 assert(strcmp(((glsl_type *) entry->data)->name, block_name) == 0);
1064 mtx_unlock(&glsl_type::hash_mutex);
1066 return (glsl_type *) entry->data;
1070 glsl_type::get_subroutine_instance(const char *subroutine_name)
1072 const glsl_type key(subroutine_name);
1074 mtx_lock(&glsl_type::hash_mutex);
1076 if (subroutine_types == NULL) {
1077 subroutine_types = _mesa_hash_table_create(NULL, record_key_hash,
1078 record_key_compare);
1081 const struct hash_entry *entry = _mesa_hash_table_search(subroutine_types,
1083 if (entry == NULL) {
1084 const glsl_type *t = new glsl_type(subroutine_name);
1086 entry = _mesa_hash_table_insert(subroutine_types, t, (void *) t);
1089 assert(((glsl_type *) entry->data)->base_type == GLSL_TYPE_SUBROUTINE);
1090 assert(strcmp(((glsl_type *) entry->data)->name, subroutine_name) == 0);
1092 mtx_unlock(&glsl_type::hash_mutex);
1094 return (glsl_type *) entry->data;
1099 function_key_compare(const void *a, const void *b)
1101 const glsl_type *const key1 = (glsl_type *) a;
1102 const glsl_type *const key2 = (glsl_type *) b;
1104 if (key1->length != key2->length)
1107 return memcmp(key1->fields.parameters, key2->fields.parameters,
1108 (key1->length + 1) * sizeof(*key1->fields.parameters)) == 0;
1113 function_key_hash(const void *a)
1115 const glsl_type *const key = (glsl_type *) a;
1116 return _mesa_hash_data(key->fields.parameters,
1117 (key->length + 1) * sizeof(*key->fields.parameters));
1121 glsl_type::get_function_instance(const glsl_type *return_type,
1122 const glsl_function_param *params,
1123 unsigned num_params)
1125 const glsl_type key(return_type, params, num_params);
1127 mtx_lock(&glsl_type::hash_mutex);
1129 if (function_types == NULL) {
1130 function_types = _mesa_hash_table_create(NULL, function_key_hash,
1131 function_key_compare);
1134 struct hash_entry *entry = _mesa_hash_table_search(function_types, &key);
1135 if (entry == NULL) {
1136 const glsl_type *t = new glsl_type(return_type, params, num_params);
1138 entry = _mesa_hash_table_insert(function_types, t, (void *) t);
1141 const glsl_type *t = (const glsl_type *)entry->data;
1143 assert(t->base_type == GLSL_TYPE_FUNCTION);
1144 assert(t->length == num_params);
1146 mtx_unlock(&glsl_type::hash_mutex);
1153 glsl_type::get_mul_type(const glsl_type *type_a, const glsl_type *type_b)
1155 if (type_a == type_b) {
1157 } else if (type_a->is_matrix() && type_b->is_matrix()) {
1158 /* Matrix multiply. The columns of A must match the rows of B. Given
1159 * the other previously tested constraints, this means the vector type
1160 * of a row from A must be the same as the vector type of a column from
1163 if (type_a->row_type() == type_b->column_type()) {
1164 /* The resulting matrix has the number of columns of matrix B and
1165 * the number of rows of matrix A. We get the row count of A by
1166 * looking at the size of a vector that makes up a column. The
1167 * transpose (size of a row) is done for B.
1169 const glsl_type *const type =
1170 get_instance(type_a->base_type,
1171 type_a->column_type()->vector_elements,
1172 type_b->row_type()->vector_elements);
1173 assert(type != error_type);
1177 } else if (type_a->is_matrix()) {
1178 /* A is a matrix and B is a column vector. Columns of A must match
1179 * rows of B. Given the other previously tested constraints, this
1180 * means the vector type of a row from A must be the same as the
1181 * vector the type of B.
1183 if (type_a->row_type() == type_b) {
1184 /* The resulting vector has a number of elements equal to
1185 * the number of rows of matrix A. */
1186 const glsl_type *const type =
1187 get_instance(type_a->base_type,
1188 type_a->column_type()->vector_elements,
1190 assert(type != error_type);
1195 assert(type_b->is_matrix());
1197 /* A is a row vector and B is a matrix. Columns of A must match rows
1198 * of B. Given the other previously tested constraints, this means
1199 * the type of A must be the same as the vector type of a column from
1202 if (type_a == type_b->column_type()) {
1203 /* The resulting vector has a number of elements equal to
1204 * the number of columns of matrix B. */
1205 const glsl_type *const type =
1206 get_instance(type_a->base_type,
1207 type_b->row_type()->vector_elements,
1209 assert(type != error_type);
1220 glsl_type::field_type(const char *name) const
1222 if (this->base_type != GLSL_TYPE_STRUCT
1223 && this->base_type != GLSL_TYPE_INTERFACE)
1226 for (unsigned i = 0; i < this->length; i++) {
1227 if (strcmp(name, this->fields.structure[i].name) == 0)
1228 return this->fields.structure[i].type;
1236 glsl_type::field_index(const char *name) const
1238 if (this->base_type != GLSL_TYPE_STRUCT
1239 && this->base_type != GLSL_TYPE_INTERFACE)
1242 for (unsigned i = 0; i < this->length; i++) {
1243 if (strcmp(name, this->fields.structure[i].name) == 0)
1252 glsl_type::component_slots() const
1254 switch (this->base_type) {
1255 case GLSL_TYPE_UINT:
1257 case GLSL_TYPE_UINT8:
1258 case GLSL_TYPE_INT8:
1259 case GLSL_TYPE_UINT16:
1260 case GLSL_TYPE_INT16:
1261 case GLSL_TYPE_FLOAT:
1262 case GLSL_TYPE_FLOAT16:
1263 case GLSL_TYPE_BOOL:
1264 return this->components();
1266 case GLSL_TYPE_DOUBLE:
1267 case GLSL_TYPE_UINT64:
1268 case GLSL_TYPE_INT64:
1269 return 2 * this->components();
1271 case GLSL_TYPE_STRUCT:
1272 case GLSL_TYPE_INTERFACE: {
1275 for (unsigned i = 0; i < this->length; i++)
1276 size += this->fields.structure[i].type->component_slots();
1281 case GLSL_TYPE_ARRAY:
1282 return this->length * this->fields.array->component_slots();
1284 case GLSL_TYPE_SAMPLER:
1285 case GLSL_TYPE_IMAGE:
1288 case GLSL_TYPE_SUBROUTINE:
1291 case GLSL_TYPE_FUNCTION:
1292 case GLSL_TYPE_ATOMIC_UINT:
1293 case GLSL_TYPE_VOID:
1294 case GLSL_TYPE_ERROR:
1302 glsl_type::record_location_offset(unsigned length) const
1304 unsigned offset = 0;
1305 const glsl_type *t = this->without_array();
1306 if (t->is_record()) {
1307 assert(length <= t->length);
1309 for (unsigned i = 0; i < length; i++) {
1310 const glsl_type *st = t->fields.structure[i].type;
1311 const glsl_type *wa = st->without_array();
1312 if (wa->is_record()) {
1313 unsigned r_offset = wa->record_location_offset(wa->length);
1314 offset += st->is_array() ?
1315 st->arrays_of_arrays_size() * r_offset : r_offset;
1316 } else if (st->is_array() && st->fields.array->is_array()) {
1317 unsigned outer_array_size = st->length;
1318 const glsl_type *base_type = st->fields.array;
1320 /* For arrays of arrays the outer arrays take up a uniform
1321 * slot for each element. The innermost array elements share a
1322 * single slot so we ignore the innermost array when calculating
1325 while (base_type->fields.array->is_array()) {
1326 outer_array_size = outer_array_size * base_type->length;
1327 base_type = base_type->fields.array;
1329 offset += outer_array_size;
1331 /* We dont worry about arrays here because unless the array
1332 * contains a structure or another array it only takes up a single
1343 glsl_type::uniform_locations() const
1347 switch (this->base_type) {
1348 case GLSL_TYPE_UINT:
1350 case GLSL_TYPE_FLOAT:
1351 case GLSL_TYPE_FLOAT16:
1352 case GLSL_TYPE_DOUBLE:
1353 case GLSL_TYPE_UINT16:
1354 case GLSL_TYPE_UINT8:
1355 case GLSL_TYPE_INT16:
1356 case GLSL_TYPE_INT8:
1357 case GLSL_TYPE_UINT64:
1358 case GLSL_TYPE_INT64:
1359 case GLSL_TYPE_BOOL:
1360 case GLSL_TYPE_SAMPLER:
1361 case GLSL_TYPE_IMAGE:
1362 case GLSL_TYPE_SUBROUTINE:
1365 case GLSL_TYPE_STRUCT:
1366 case GLSL_TYPE_INTERFACE:
1367 for (unsigned i = 0; i < this->length; i++)
1368 size += this->fields.structure[i].type->uniform_locations();
1370 case GLSL_TYPE_ARRAY:
1371 return this->length * this->fields.array->uniform_locations();
1378 glsl_type::varying_count() const
1382 switch (this->base_type) {
1383 case GLSL_TYPE_UINT:
1385 case GLSL_TYPE_FLOAT:
1386 case GLSL_TYPE_FLOAT16:
1387 case GLSL_TYPE_DOUBLE:
1388 case GLSL_TYPE_BOOL:
1389 case GLSL_TYPE_UINT16:
1390 case GLSL_TYPE_UINT8:
1391 case GLSL_TYPE_INT16:
1392 case GLSL_TYPE_INT8:
1393 case GLSL_TYPE_UINT64:
1394 case GLSL_TYPE_INT64:
1397 case GLSL_TYPE_STRUCT:
1398 case GLSL_TYPE_INTERFACE:
1399 for (unsigned i = 0; i < this->length; i++)
1400 size += this->fields.structure[i].type->varying_count();
1402 case GLSL_TYPE_ARRAY:
1403 /* Don't count innermost array elements */
1404 if (this->without_array()->is_record() ||
1405 this->without_array()->is_interface() ||
1406 this->fields.array->is_array())
1407 return this->length * this->fields.array->varying_count();
1409 return this->fields.array->varying_count();
1411 assert(!"unsupported varying type");
1417 glsl_type::can_implicitly_convert_to(const glsl_type *desired,
1418 _mesa_glsl_parse_state *state) const
1420 if (this == desired)
1423 /* GLSL 1.10 and ESSL do not allow implicit conversions. If there is no
1424 * state, we're doing intra-stage function linking where these checks have
1425 * already been done.
1427 if (state && (state->es_shader || !state->is_version(120, 0)))
1430 /* There is no conversion among matrix types. */
1431 if (this->matrix_columns > 1 || desired->matrix_columns > 1)
1434 /* Vector size must match. */
1435 if (this->vector_elements != desired->vector_elements)
1438 /* int and uint can be converted to float. */
1439 if (desired->is_float() && this->is_integer())
1442 /* With GLSL 4.0, ARB_gpu_shader5, or MESA_shader_integer_functions, int
1443 * can be converted to uint. Note that state may be NULL here, when
1444 * resolving function calls in the linker. By this time, all the
1445 * state-dependent checks have already happened though, so allow anything
1446 * that's allowed in any shader version.
1448 if ((!state || state->is_version(400, 0) || state->ARB_gpu_shader5_enable ||
1449 state->MESA_shader_integer_functions_enable) &&
1450 desired->base_type == GLSL_TYPE_UINT && this->base_type == GLSL_TYPE_INT)
1453 /* No implicit conversions from double. */
1454 if ((!state || state->has_double()) && this->is_double())
1457 /* Conversions from different types to double. */
1458 if ((!state || state->has_double()) && desired->is_double()) {
1459 if (this->is_float())
1461 if (this->is_integer())
1469 glsl_type::std140_base_alignment(bool row_major) const
1471 unsigned N = is_64bit() ? 8 : 4;
1473 /* (1) If the member is a scalar consuming <N> basic machine units, the
1474 * base alignment is <N>.
1476 * (2) If the member is a two- or four-component vector with components
1477 * consuming <N> basic machine units, the base alignment is 2<N> or
1478 * 4<N>, respectively.
1480 * (3) If the member is a three-component vector with components consuming
1481 * <N> basic machine units, the base alignment is 4<N>.
1483 if (this->is_scalar() || this->is_vector()) {
1484 switch (this->vector_elements) {
1495 /* (4) If the member is an array of scalars or vectors, the base alignment
1496 * and array stride are set to match the base alignment of a single
1497 * array element, according to rules (1), (2), and (3), and rounded up
1498 * to the base alignment of a vec4. The array may have padding at the
1499 * end; the base offset of the member following the array is rounded up
1500 * to the next multiple of the base alignment.
1502 * (6) If the member is an array of <S> column-major matrices with <C>
1503 * columns and <R> rows, the matrix is stored identically to a row of
1504 * <S>*<C> column vectors with <R> components each, according to rule
1507 * (8) If the member is an array of <S> row-major matrices with <C> columns
1508 * and <R> rows, the matrix is stored identically to a row of <S>*<R>
1509 * row vectors with <C> components each, according to rule (4).
1511 * (10) If the member is an array of <S> structures, the <S> elements of
1512 * the array are laid out in order, according to rule (9).
1514 if (this->is_array()) {
1515 if (this->fields.array->is_scalar() ||
1516 this->fields.array->is_vector() ||
1517 this->fields.array->is_matrix()) {
1518 return MAX2(this->fields.array->std140_base_alignment(row_major), 16);
1520 assert(this->fields.array->is_record() ||
1521 this->fields.array->is_array());
1522 return this->fields.array->std140_base_alignment(row_major);
1526 /* (5) If the member is a column-major matrix with <C> columns and
1527 * <R> rows, the matrix is stored identically to an array of
1528 * <C> column vectors with <R> components each, according to
1531 * (7) If the member is a row-major matrix with <C> columns and <R>
1532 * rows, the matrix is stored identically to an array of <R>
1533 * row vectors with <C> components each, according to rule (4).
1535 if (this->is_matrix()) {
1536 const struct glsl_type *vec_type, *array_type;
1537 int c = this->matrix_columns;
1538 int r = this->vector_elements;
1541 vec_type = get_instance(base_type, c, 1);
1542 array_type = glsl_type::get_array_instance(vec_type, r);
1544 vec_type = get_instance(base_type, r, 1);
1545 array_type = glsl_type::get_array_instance(vec_type, c);
1548 return array_type->std140_base_alignment(false);
1551 /* (9) If the member is a structure, the base alignment of the
1552 * structure is <N>, where <N> is the largest base alignment
1553 * value of any of its members, and rounded up to the base
1554 * alignment of a vec4. The individual members of this
1555 * sub-structure are then assigned offsets by applying this set
1556 * of rules recursively, where the base offset of the first
1557 * member of the sub-structure is equal to the aligned offset
1558 * of the structure. The structure may have padding at the end;
1559 * the base offset of the member following the sub-structure is
1560 * rounded up to the next multiple of the base alignment of the
1563 if (this->is_record()) {
1564 unsigned base_alignment = 16;
1565 for (unsigned i = 0; i < this->length; i++) {
1566 bool field_row_major = row_major;
1567 const enum glsl_matrix_layout matrix_layout =
1568 glsl_matrix_layout(this->fields.structure[i].matrix_layout);
1569 if (matrix_layout == GLSL_MATRIX_LAYOUT_ROW_MAJOR) {
1570 field_row_major = true;
1571 } else if (matrix_layout == GLSL_MATRIX_LAYOUT_COLUMN_MAJOR) {
1572 field_row_major = false;
1575 const struct glsl_type *field_type = this->fields.structure[i].type;
1576 base_alignment = MAX2(base_alignment,
1577 field_type->std140_base_alignment(field_row_major));
1579 return base_alignment;
1582 assert(!"not reached");
1587 glsl_type::std140_size(bool row_major) const
1589 unsigned N = is_64bit() ? 8 : 4;
1591 /* (1) If the member is a scalar consuming <N> basic machine units, the
1592 * base alignment is <N>.
1594 * (2) If the member is a two- or four-component vector with components
1595 * consuming <N> basic machine units, the base alignment is 2<N> or
1596 * 4<N>, respectively.
1598 * (3) If the member is a three-component vector with components consuming
1599 * <N> basic machine units, the base alignment is 4<N>.
1601 if (this->is_scalar() || this->is_vector()) {
1602 return this->vector_elements * N;
1605 /* (5) If the member is a column-major matrix with <C> columns and
1606 * <R> rows, the matrix is stored identically to an array of
1607 * <C> column vectors with <R> components each, according to
1610 * (6) If the member is an array of <S> column-major matrices with <C>
1611 * columns and <R> rows, the matrix is stored identically to a row of
1612 * <S>*<C> column vectors with <R> components each, according to rule
1615 * (7) If the member is a row-major matrix with <C> columns and <R>
1616 * rows, the matrix is stored identically to an array of <R>
1617 * row vectors with <C> components each, according to rule (4).
1619 * (8) If the member is an array of <S> row-major matrices with <C> columns
1620 * and <R> rows, the matrix is stored identically to a row of <S>*<R>
1621 * row vectors with <C> components each, according to rule (4).
1623 if (this->without_array()->is_matrix()) {
1624 const struct glsl_type *element_type;
1625 const struct glsl_type *vec_type;
1626 unsigned int array_len;
1628 if (this->is_array()) {
1629 element_type = this->without_array();
1630 array_len = this->arrays_of_arrays_size();
1632 element_type = this;
1637 vec_type = get_instance(element_type->base_type,
1638 element_type->matrix_columns, 1);
1640 array_len *= element_type->vector_elements;
1642 vec_type = get_instance(element_type->base_type,
1643 element_type->vector_elements, 1);
1644 array_len *= element_type->matrix_columns;
1646 const glsl_type *array_type = glsl_type::get_array_instance(vec_type,
1649 return array_type->std140_size(false);
1652 /* (4) If the member is an array of scalars or vectors, the base alignment
1653 * and array stride are set to match the base alignment of a single
1654 * array element, according to rules (1), (2), and (3), and rounded up
1655 * to the base alignment of a vec4. The array may have padding at the
1656 * end; the base offset of the member following the array is rounded up
1657 * to the next multiple of the base alignment.
1659 * (10) If the member is an array of <S> structures, the <S> elements of
1660 * the array are laid out in order, according to rule (9).
1662 if (this->is_array()) {
1663 if (this->without_array()->is_record()) {
1664 return this->arrays_of_arrays_size() *
1665 this->without_array()->std140_size(row_major);
1667 unsigned element_base_align =
1668 this->without_array()->std140_base_alignment(row_major);
1669 return this->arrays_of_arrays_size() * MAX2(element_base_align, 16);
1673 /* (9) If the member is a structure, the base alignment of the
1674 * structure is <N>, where <N> is the largest base alignment
1675 * value of any of its members, and rounded up to the base
1676 * alignment of a vec4. The individual members of this
1677 * sub-structure are then assigned offsets by applying this set
1678 * of rules recursively, where the base offset of the first
1679 * member of the sub-structure is equal to the aligned offset
1680 * of the structure. The structure may have padding at the end;
1681 * the base offset of the member following the sub-structure is
1682 * rounded up to the next multiple of the base alignment of the
1685 if (this->is_record() || this->is_interface()) {
1687 unsigned max_align = 0;
1689 for (unsigned i = 0; i < this->length; i++) {
1690 bool field_row_major = row_major;
1691 const enum glsl_matrix_layout matrix_layout =
1692 glsl_matrix_layout(this->fields.structure[i].matrix_layout);
1693 if (matrix_layout == GLSL_MATRIX_LAYOUT_ROW_MAJOR) {
1694 field_row_major = true;
1695 } else if (matrix_layout == GLSL_MATRIX_LAYOUT_COLUMN_MAJOR) {
1696 field_row_major = false;
1699 const struct glsl_type *field_type = this->fields.structure[i].type;
1700 unsigned align = field_type->std140_base_alignment(field_row_major);
1702 /* Ignore unsized arrays when calculating size */
1703 if (field_type->is_unsized_array())
1706 size = glsl_align(size, align);
1707 size += field_type->std140_size(field_row_major);
1709 max_align = MAX2(align, max_align);
1711 if (field_type->is_record() && (i + 1 < this->length))
1712 size = glsl_align(size, 16);
1714 size = glsl_align(size, MAX2(max_align, 16));
1718 assert(!"not reached");
1723 glsl_type::std430_base_alignment(bool row_major) const
1726 unsigned N = is_64bit() ? 8 : 4;
1728 /* (1) If the member is a scalar consuming <N> basic machine units, the
1729 * base alignment is <N>.
1731 * (2) If the member is a two- or four-component vector with components
1732 * consuming <N> basic machine units, the base alignment is 2<N> or
1733 * 4<N>, respectively.
1735 * (3) If the member is a three-component vector with components consuming
1736 * <N> basic machine units, the base alignment is 4<N>.
1738 if (this->is_scalar() || this->is_vector()) {
1739 switch (this->vector_elements) {
1750 /* OpenGL 4.30 spec, section 7.6.2.2 "Standard Uniform Block Layout":
1752 * "When using the std430 storage layout, shader storage blocks will be
1753 * laid out in buffer storage identically to uniform and shader storage
1754 * blocks using the std140 layout, except that the base alignment and
1755 * stride of arrays of scalars and vectors in rule 4 and of structures
1756 * in rule 9 are not rounded up a multiple of the base alignment of a vec4.
1759 /* (1) If the member is a scalar consuming <N> basic machine units, the
1760 * base alignment is <N>.
1762 * (2) If the member is a two- or four-component vector with components
1763 * consuming <N> basic machine units, the base alignment is 2<N> or
1764 * 4<N>, respectively.
1766 * (3) If the member is a three-component vector with components consuming
1767 * <N> basic machine units, the base alignment is 4<N>.
1769 if (this->is_array())
1770 return this->fields.array->std430_base_alignment(row_major);
1772 /* (5) If the member is a column-major matrix with <C> columns and
1773 * <R> rows, the matrix is stored identically to an array of
1774 * <C> column vectors with <R> components each, according to
1777 * (7) If the member is a row-major matrix with <C> columns and <R>
1778 * rows, the matrix is stored identically to an array of <R>
1779 * row vectors with <C> components each, according to rule (4).
1781 if (this->is_matrix()) {
1782 const struct glsl_type *vec_type, *array_type;
1783 int c = this->matrix_columns;
1784 int r = this->vector_elements;
1787 vec_type = get_instance(base_type, c, 1);
1788 array_type = glsl_type::get_array_instance(vec_type, r);
1790 vec_type = get_instance(base_type, r, 1);
1791 array_type = glsl_type::get_array_instance(vec_type, c);
1794 return array_type->std430_base_alignment(false);
1797 /* (9) If the member is a structure, the base alignment of the
1798 * structure is <N>, where <N> is the largest base alignment
1799 * value of any of its members, and rounded up to the base
1800 * alignment of a vec4. The individual members of this
1801 * sub-structure are then assigned offsets by applying this set
1802 * of rules recursively, where the base offset of the first
1803 * member of the sub-structure is equal to the aligned offset
1804 * of the structure. The structure may have padding at the end;
1805 * the base offset of the member following the sub-structure is
1806 * rounded up to the next multiple of the base alignment of the
1809 if (this->is_record()) {
1810 unsigned base_alignment = 0;
1811 for (unsigned i = 0; i < this->length; i++) {
1812 bool field_row_major = row_major;
1813 const enum glsl_matrix_layout matrix_layout =
1814 glsl_matrix_layout(this->fields.structure[i].matrix_layout);
1815 if (matrix_layout == GLSL_MATRIX_LAYOUT_ROW_MAJOR) {
1816 field_row_major = true;
1817 } else if (matrix_layout == GLSL_MATRIX_LAYOUT_COLUMN_MAJOR) {
1818 field_row_major = false;
1821 const struct glsl_type *field_type = this->fields.structure[i].type;
1822 base_alignment = MAX2(base_alignment,
1823 field_type->std430_base_alignment(field_row_major));
1825 assert(base_alignment > 0);
1826 return base_alignment;
1828 assert(!"not reached");
1833 glsl_type::std430_array_stride(bool row_major) const
1835 unsigned N = is_64bit() ? 8 : 4;
1837 /* Notice that the array stride of a vec3 is not 3 * N but 4 * N.
1838 * See OpenGL 4.30 spec, section 7.6.2.2 "Standard Uniform Block Layout"
1840 * (3) If the member is a three-component vector with components consuming
1841 * <N> basic machine units, the base alignment is 4<N>.
1843 if (this->is_vector() && this->vector_elements == 3)
1846 /* By default use std430_size(row_major) */
1847 return this->std430_size(row_major);
1851 glsl_type::std430_size(bool row_major) const
1853 unsigned N = is_64bit() ? 8 : 4;
1855 /* OpenGL 4.30 spec, section 7.6.2.2 "Standard Uniform Block Layout":
1857 * "When using the std430 storage layout, shader storage blocks will be
1858 * laid out in buffer storage identically to uniform and shader storage
1859 * blocks using the std140 layout, except that the base alignment and
1860 * stride of arrays of scalars and vectors in rule 4 and of structures
1861 * in rule 9 are not rounded up a multiple of the base alignment of a vec4.
1863 if (this->is_scalar() || this->is_vector())
1864 return this->vector_elements * N;
1866 if (this->without_array()->is_matrix()) {
1867 const struct glsl_type *element_type;
1868 const struct glsl_type *vec_type;
1869 unsigned int array_len;
1871 if (this->is_array()) {
1872 element_type = this->without_array();
1873 array_len = this->arrays_of_arrays_size();
1875 element_type = this;
1880 vec_type = get_instance(element_type->base_type,
1881 element_type->matrix_columns, 1);
1883 array_len *= element_type->vector_elements;
1885 vec_type = get_instance(element_type->base_type,
1886 element_type->vector_elements, 1);
1887 array_len *= element_type->matrix_columns;
1889 const glsl_type *array_type = glsl_type::get_array_instance(vec_type,
1892 return array_type->std430_size(false);
1895 if (this->is_array()) {
1896 if (this->without_array()->is_record())
1897 return this->arrays_of_arrays_size() *
1898 this->without_array()->std430_size(row_major);
1900 return this->arrays_of_arrays_size() *
1901 this->without_array()->std430_base_alignment(row_major);
1904 if (this->is_record() || this->is_interface()) {
1906 unsigned max_align = 0;
1908 for (unsigned i = 0; i < this->length; i++) {
1909 bool field_row_major = row_major;
1910 const enum glsl_matrix_layout matrix_layout =
1911 glsl_matrix_layout(this->fields.structure[i].matrix_layout);
1912 if (matrix_layout == GLSL_MATRIX_LAYOUT_ROW_MAJOR) {
1913 field_row_major = true;
1914 } else if (matrix_layout == GLSL_MATRIX_LAYOUT_COLUMN_MAJOR) {
1915 field_row_major = false;
1918 const struct glsl_type *field_type = this->fields.structure[i].type;
1919 unsigned align = field_type->std430_base_alignment(field_row_major);
1920 size = glsl_align(size, align);
1921 size += field_type->std430_size(field_row_major);
1923 max_align = MAX2(align, max_align);
1925 size = glsl_align(size, max_align);
1929 assert(!"not reached");
1934 glsl_type::count_attribute_slots(bool is_vertex_input) const
1936 /* From page 31 (page 37 of the PDF) of the GLSL 1.50 spec:
1938 * "A scalar input counts the same amount against this limit as a vec4,
1939 * so applications may want to consider packing groups of four
1940 * unrelated float inputs together into a vector to better utilize the
1941 * capabilities of the underlying hardware. A matrix input will use up
1942 * multiple locations. The number of locations used will equal the
1943 * number of columns in the matrix."
1945 * The spec does not explicitly say how arrays are counted. However, it
1946 * should be safe to assume the total number of slots consumed by an array
1947 * is the number of entries in the array multiplied by the number of slots
1948 * consumed by a single element of the array.
1950 * The spec says nothing about how structs are counted, because vertex
1951 * attributes are not allowed to be (or contain) structs. However, Mesa
1952 * allows varying structs, the number of varying slots taken up by a
1953 * varying struct is simply equal to the sum of the number of slots taken
1954 * up by each element.
1956 * Doubles are counted different depending on whether they are vertex
1957 * inputs or everything else. Vertex inputs from ARB_vertex_attrib_64bit
1958 * take one location no matter what size they are, otherwise dvec3/4
1959 * take two locations.
1961 switch (this->base_type) {
1962 case GLSL_TYPE_UINT:
1964 case GLSL_TYPE_UINT8:
1965 case GLSL_TYPE_INT8:
1966 case GLSL_TYPE_UINT16:
1967 case GLSL_TYPE_INT16:
1968 case GLSL_TYPE_FLOAT:
1969 case GLSL_TYPE_FLOAT16:
1970 case GLSL_TYPE_BOOL:
1971 case GLSL_TYPE_SAMPLER:
1972 case GLSL_TYPE_IMAGE:
1973 return this->matrix_columns;
1974 case GLSL_TYPE_DOUBLE:
1975 case GLSL_TYPE_UINT64:
1976 case GLSL_TYPE_INT64:
1977 if (this->vector_elements > 2 && !is_vertex_input)
1978 return this->matrix_columns * 2;
1980 return this->matrix_columns;
1981 case GLSL_TYPE_STRUCT:
1982 case GLSL_TYPE_INTERFACE: {
1985 for (unsigned i = 0; i < this->length; i++)
1986 size += this->fields.structure[i].type->count_attribute_slots(is_vertex_input);
1991 case GLSL_TYPE_ARRAY:
1992 return this->length * this->fields.array->count_attribute_slots(is_vertex_input);
1994 case GLSL_TYPE_SUBROUTINE:
1997 case GLSL_TYPE_FUNCTION:
1998 case GLSL_TYPE_ATOMIC_UINT:
1999 case GLSL_TYPE_VOID:
2000 case GLSL_TYPE_ERROR:
2004 assert(!"Unexpected type in count_attribute_slots()");
2010 glsl_type::coordinate_components() const
2014 switch (sampler_dimensionality) {
2015 case GLSL_SAMPLER_DIM_1D:
2016 case GLSL_SAMPLER_DIM_BUF:
2019 case GLSL_SAMPLER_DIM_2D:
2020 case GLSL_SAMPLER_DIM_RECT:
2021 case GLSL_SAMPLER_DIM_MS:
2022 case GLSL_SAMPLER_DIM_EXTERNAL:
2023 case GLSL_SAMPLER_DIM_SUBPASS:
2026 case GLSL_SAMPLER_DIM_3D:
2027 case GLSL_SAMPLER_DIM_CUBE:
2031 assert(!"Should not get here.");
2036 /* Array textures need an additional component for the array index, except
2037 * for cubemap array images that behave like a 2D array of interleaved
2040 if (sampler_array &&
2041 !(is_image() && sampler_dimensionality == GLSL_SAMPLER_DIM_CUBE))
2048 * Declarations of type flyweights (glsl_type::_foo_type) and
2049 * convenience pointers (glsl_type::foo_type).
2052 #define DECL_TYPE(NAME, ...) \
2053 const glsl_type glsl_type::_##NAME##_type = glsl_type(__VA_ARGS__, #NAME); \
2054 const glsl_type *const glsl_type::NAME##_type = &glsl_type::_##NAME##_type;
2056 #define STRUCT_TYPE(NAME)
2058 #include "compiler/builtin_type_macros.h"
2062 get_struct_type_field_and_pointer_sizes(size_t *s_field_size,
2063 size_t *s_field_ptrs)
2065 *s_field_size = sizeof(glsl_struct_field);
2067 sizeof(((glsl_struct_field *)0)->type) +
2068 sizeof(((glsl_struct_field *)0)->name);
2072 encode_type_to_blob(struct blob *blob, const glsl_type *type)
2077 blob_write_uint32(blob, 0);
2081 switch (type->base_type) {
2082 case GLSL_TYPE_UINT:
2084 case GLSL_TYPE_FLOAT:
2085 case GLSL_TYPE_BOOL:
2086 case GLSL_TYPE_DOUBLE:
2087 case GLSL_TYPE_UINT64:
2088 case GLSL_TYPE_INT64:
2089 encoding = (type->base_type << 24) |
2090 (type->vector_elements << 4) |
2091 (type->matrix_columns);
2093 case GLSL_TYPE_SAMPLER:
2094 encoding = (type->base_type) << 24 |
2095 (type->sampler_dimensionality << 4) |
2096 (type->sampler_shadow << 3) |
2097 (type->sampler_array << 2) |
2098 (type->sampled_type);
2100 case GLSL_TYPE_SUBROUTINE:
2101 encoding = type->base_type << 24;
2102 blob_write_uint32(blob, encoding);
2103 blob_write_string(blob, type->name);
2105 case GLSL_TYPE_IMAGE:
2106 encoding = (type->base_type) << 24 |
2107 (type->sampler_dimensionality << 3) |
2108 (type->sampler_array << 2) |
2109 (type->sampled_type);
2111 case GLSL_TYPE_ATOMIC_UINT:
2112 encoding = (type->base_type << 24);
2114 case GLSL_TYPE_ARRAY:
2115 blob_write_uint32(blob, (type->base_type) << 24);
2116 blob_write_uint32(blob, type->length);
2117 encode_type_to_blob(blob, type->fields.array);
2119 case GLSL_TYPE_STRUCT:
2120 case GLSL_TYPE_INTERFACE:
2121 blob_write_uint32(blob, (type->base_type) << 24);
2122 blob_write_string(blob, type->name);
2123 blob_write_uint32(blob, type->length);
2125 size_t s_field_size, s_field_ptrs;
2126 get_struct_type_field_and_pointer_sizes(&s_field_size, &s_field_ptrs);
2128 for (unsigned i = 0; i < type->length; i++) {
2129 encode_type_to_blob(blob, type->fields.structure[i].type);
2130 blob_write_string(blob, type->fields.structure[i].name);
2132 /* Write the struct field skipping the pointers */
2133 blob_write_bytes(blob,
2134 ((char *)&type->fields.structure[i]) + s_field_ptrs,
2135 s_field_size - s_field_ptrs);
2138 if (type->is_interface()) {
2139 blob_write_uint32(blob, type->interface_packing);
2140 blob_write_uint32(blob, type->interface_row_major);
2143 case GLSL_TYPE_VOID:
2144 encoding = (type->base_type << 24);
2146 case GLSL_TYPE_ERROR:
2148 assert(!"Cannot encode type!");
2153 blob_write_uint32(blob, encoding);
2157 decode_type_from_blob(struct blob_reader *blob)
2159 uint32_t u = blob_read_uint32(blob);
2165 glsl_base_type base_type = (glsl_base_type) (u >> 24);
2167 switch (base_type) {
2168 case GLSL_TYPE_UINT:
2170 case GLSL_TYPE_FLOAT:
2171 case GLSL_TYPE_BOOL:
2172 case GLSL_TYPE_DOUBLE:
2173 case GLSL_TYPE_UINT64:
2174 case GLSL_TYPE_INT64:
2175 return glsl_type::get_instance(base_type, (u >> 4) & 0x0f, u & 0x0f);
2176 case GLSL_TYPE_SAMPLER:
2177 return glsl_type::get_sampler_instance((enum glsl_sampler_dim) ((u >> 4) & 0x07),
2180 (glsl_base_type) ((u >> 0) & 0x03));
2181 case GLSL_TYPE_SUBROUTINE:
2182 return glsl_type::get_subroutine_instance(blob_read_string(blob));
2183 case GLSL_TYPE_IMAGE:
2184 return glsl_type::get_image_instance((enum glsl_sampler_dim) ((u >> 3) & 0x07),
2186 (glsl_base_type) ((u >> 0) & 0x03));
2187 case GLSL_TYPE_ATOMIC_UINT:
2188 return glsl_type::atomic_uint_type;
2189 case GLSL_TYPE_ARRAY: {
2190 unsigned length = blob_read_uint32(blob);
2191 return glsl_type::get_array_instance(decode_type_from_blob(blob),
2194 case GLSL_TYPE_STRUCT:
2195 case GLSL_TYPE_INTERFACE: {
2196 char *name = blob_read_string(blob);
2197 unsigned num_fields = blob_read_uint32(blob);
2199 size_t s_field_size, s_field_ptrs;
2200 get_struct_type_field_and_pointer_sizes(&s_field_size, &s_field_ptrs);
2202 glsl_struct_field *fields =
2203 (glsl_struct_field *) malloc(s_field_size * num_fields);
2204 for (unsigned i = 0; i < num_fields; i++) {
2205 fields[i].type = decode_type_from_blob(blob);
2206 fields[i].name = blob_read_string(blob);
2208 blob_copy_bytes(blob, ((uint8_t *) &fields[i]) + s_field_ptrs,
2209 s_field_size - s_field_ptrs);
2213 if (base_type == GLSL_TYPE_INTERFACE) {
2214 enum glsl_interface_packing packing =
2215 (glsl_interface_packing) blob_read_uint32(blob);
2216 bool row_major = blob_read_uint32(blob);
2217 t = glsl_type::get_interface_instance(fields, num_fields, packing,
2220 t = glsl_type::get_record_instance(fields, num_fields, name);
2226 case GLSL_TYPE_VOID:
2227 return glsl_type::void_type;
2228 case GLSL_TYPE_ERROR:
2230 assert(!"Cannot decode type!");