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/core.h" /* for Elements, MAX2 */
26 #include "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
;
129 mtx_unlock(&glsl_type::mutex
);
132 glsl_type::glsl_type(const glsl_struct_field
*fields
, unsigned num_fields
,
133 enum glsl_interface_packing packing
, const char *name
) :
135 base_type(GLSL_TYPE_INTERFACE
),
136 sampler_dimensionality(0), sampler_shadow(0), sampler_array(0),
137 sampler_type(0), interface_packing((unsigned) packing
),
138 vector_elements(0), matrix_columns(0),
143 mtx_lock(&glsl_type::mutex
);
145 init_ralloc_type_ctx();
146 assert(name
!= NULL
);
147 this->name
= ralloc_strdup(this->mem_ctx
, name
);
148 this->fields
.structure
= ralloc_array(this->mem_ctx
,
149 glsl_struct_field
, length
);
150 for (i
= 0; i
< length
; i
++) {
151 this->fields
.structure
[i
].type
= fields
[i
].type
;
152 this->fields
.structure
[i
].name
= ralloc_strdup(this->fields
.structure
,
154 this->fields
.structure
[i
].location
= fields
[i
].location
;
155 this->fields
.structure
[i
].interpolation
= fields
[i
].interpolation
;
156 this->fields
.structure
[i
].centroid
= fields
[i
].centroid
;
157 this->fields
.structure
[i
].sample
= fields
[i
].sample
;
158 this->fields
.structure
[i
].matrix_layout
= fields
[i
].matrix_layout
;
159 this->fields
.structure
[i
].patch
= fields
[i
].patch
;
162 mtx_unlock(&glsl_type::mutex
);
165 glsl_type::glsl_type(const char *subroutine_name
) :
167 base_type(GLSL_TYPE_SUBROUTINE
),
168 sampler_dimensionality(0), sampler_shadow(0), sampler_array(0),
169 sampler_type(0), interface_packing(0),
170 vector_elements(0), matrix_columns(0),
173 mtx_lock(&glsl_type::mutex
);
175 init_ralloc_type_ctx();
176 assert(subroutine_name
!= NULL
);
177 this->name
= ralloc_strdup(this->mem_ctx
, subroutine_name
);
178 this->vector_elements
= 1;
179 mtx_unlock(&glsl_type::mutex
);
183 glsl_type::contains_sampler() const
185 if (this->is_array()) {
186 return this->fields
.array
->contains_sampler();
187 } else if (this->is_record()) {
188 for (unsigned int i
= 0; i
< this->length
; i
++) {
189 if (this->fields
.structure
[i
].type
->contains_sampler())
194 return this->is_sampler();
200 glsl_type::contains_integer() const
202 if (this->is_array()) {
203 return this->fields
.array
->contains_integer();
204 } else if (this->is_record()) {
205 for (unsigned int i
= 0; i
< this->length
; i
++) {
206 if (this->fields
.structure
[i
].type
->contains_integer())
211 return this->is_integer();
216 glsl_type::contains_double() const
218 if (this->is_array()) {
219 return this->fields
.array
->contains_double();
220 } else if (this->is_record()) {
221 for (unsigned int i
= 0; i
< this->length
; i
++) {
222 if (this->fields
.structure
[i
].type
->contains_double())
227 return this->is_double();
232 glsl_type::contains_opaque() const {
234 case GLSL_TYPE_SAMPLER
:
235 case GLSL_TYPE_IMAGE
:
236 case GLSL_TYPE_ATOMIC_UINT
:
238 case GLSL_TYPE_ARRAY
:
239 return fields
.array
->contains_opaque();
240 case GLSL_TYPE_STRUCT
:
241 for (unsigned int i
= 0; i
< length
; i
++) {
242 if (fields
.structure
[i
].type
->contains_opaque())
252 glsl_type::contains_subroutine() const
254 if (this->is_array()) {
255 return this->fields
.array
->contains_subroutine();
256 } else if (this->is_record()) {
257 for (unsigned int i
= 0; i
< this->length
; i
++) {
258 if (this->fields
.structure
[i
].type
->contains_subroutine())
263 return this->is_subroutine();
268 glsl_type::sampler_index() const
270 const glsl_type
*const t
= (this->is_array()) ? this->fields
.array
: this;
272 assert(t
->is_sampler());
274 switch (t
->sampler_dimensionality
) {
275 case GLSL_SAMPLER_DIM_1D
:
276 return (t
->sampler_array
) ? TEXTURE_1D_ARRAY_INDEX
: TEXTURE_1D_INDEX
;
277 case GLSL_SAMPLER_DIM_2D
:
278 return (t
->sampler_array
) ? TEXTURE_2D_ARRAY_INDEX
: TEXTURE_2D_INDEX
;
279 case GLSL_SAMPLER_DIM_3D
:
280 return TEXTURE_3D_INDEX
;
281 case GLSL_SAMPLER_DIM_CUBE
:
282 return (t
->sampler_array
) ? TEXTURE_CUBE_ARRAY_INDEX
: TEXTURE_CUBE_INDEX
;
283 case GLSL_SAMPLER_DIM_RECT
:
284 return TEXTURE_RECT_INDEX
;
285 case GLSL_SAMPLER_DIM_BUF
:
286 return TEXTURE_BUFFER_INDEX
;
287 case GLSL_SAMPLER_DIM_EXTERNAL
:
288 return TEXTURE_EXTERNAL_INDEX
;
289 case GLSL_SAMPLER_DIM_MS
:
290 return (t
->sampler_array
) ? TEXTURE_2D_MULTISAMPLE_ARRAY_INDEX
: TEXTURE_2D_MULTISAMPLE_INDEX
;
292 assert(!"Should not get here.");
293 return TEXTURE_BUFFER_INDEX
;
298 glsl_type::contains_image() const
300 if (this->is_array()) {
301 return this->fields
.array
->contains_image();
302 } else if (this->is_record()) {
303 for (unsigned int i
= 0; i
< this->length
; i
++) {
304 if (this->fields
.structure
[i
].type
->contains_image())
309 return this->is_image();
313 const glsl_type
*glsl_type::get_base_type() const
320 case GLSL_TYPE_FLOAT
:
322 case GLSL_TYPE_DOUBLE
:
332 const glsl_type
*glsl_type::get_scalar_type() const
334 const glsl_type
*type
= this;
337 while (type
->base_type
== GLSL_TYPE_ARRAY
)
338 type
= type
->fields
.array
;
340 /* Handle vectors and matrices */
341 switch (type
->base_type
) {
346 case GLSL_TYPE_FLOAT
:
348 case GLSL_TYPE_DOUBLE
:
353 /* Handle everything else */
360 _mesa_glsl_release_types(void)
362 /* Should only be called during atexit (either when unloading shared
363 * object, or if process terminates), so no mutex-locking should be
366 if (glsl_type::array_types
!= NULL
) {
367 _mesa_hash_table_destroy(glsl_type::array_types
, NULL
);
368 glsl_type::array_types
= NULL
;
371 if (glsl_type::record_types
!= NULL
) {
372 _mesa_hash_table_destroy(glsl_type::record_types
, NULL
);
373 glsl_type::record_types
= NULL
;
376 if (glsl_type::interface_types
!= NULL
) {
377 _mesa_hash_table_destroy(glsl_type::interface_types
, NULL
);
378 glsl_type::interface_types
= NULL
;
383 glsl_type::glsl_type(const glsl_type
*array
, unsigned length
) :
384 base_type(GLSL_TYPE_ARRAY
),
385 sampler_dimensionality(0), sampler_shadow(0), sampler_array(0),
386 sampler_type(0), interface_packing(0),
387 vector_elements(0), matrix_columns(0),
388 length(length
), name(NULL
)
390 this->fields
.array
= array
;
391 /* Inherit the gl type of the base. The GL type is used for
392 * uniform/statevar handling in Mesa and the arrayness of the type
393 * is represented by the size rather than the type.
395 this->gl_type
= array
->gl_type
;
397 /* Allow a maximum of 10 characters for the array size. This is enough
398 * for 32-bits of ~0. The extra 3 are for the '[', ']', and terminating
401 const unsigned name_length
= strlen(array
->name
) + 10 + 3;
403 mtx_lock(&glsl_type::mutex
);
404 char *const n
= (char *) ralloc_size(this->mem_ctx
, name_length
);
405 mtx_unlock(&glsl_type::mutex
);
408 snprintf(n
, name_length
, "%s[]", array
->name
);
410 /* insert outermost dimensions in the correct spot
411 * otherwise the dimension order will be backwards
413 const char *pos
= strchr(array
->name
, '[');
415 int idx
= pos
- array
->name
;
416 snprintf(n
, idx
+1, "%s", array
->name
);
417 snprintf(n
+ idx
, name_length
- idx
, "[%u]%s",
418 length
, array
->name
+ idx
);
420 snprintf(n
, name_length
, "%s[%u]", array
->name
, length
);
429 glsl_type::vec(unsigned components
)
431 if (components
== 0 || components
> 4)
434 static const glsl_type
*const ts
[] = {
435 float_type
, vec2_type
, vec3_type
, vec4_type
437 return ts
[components
- 1];
441 glsl_type::dvec(unsigned components
)
443 if (components
== 0 || components
> 4)
446 static const glsl_type
*const ts
[] = {
447 double_type
, dvec2_type
, dvec3_type
, dvec4_type
449 return ts
[components
- 1];
453 glsl_type::ivec(unsigned components
)
455 if (components
== 0 || components
> 4)
458 static const glsl_type
*const ts
[] = {
459 int_type
, ivec2_type
, ivec3_type
, ivec4_type
461 return ts
[components
- 1];
466 glsl_type::uvec(unsigned components
)
468 if (components
== 0 || components
> 4)
471 static const glsl_type
*const ts
[] = {
472 uint_type
, uvec2_type
, uvec3_type
, uvec4_type
474 return ts
[components
- 1];
479 glsl_type::bvec(unsigned components
)
481 if (components
== 0 || components
> 4)
484 static const glsl_type
*const ts
[] = {
485 bool_type
, bvec2_type
, bvec3_type
, bvec4_type
487 return ts
[components
- 1];
492 glsl_type::get_instance(unsigned base_type
, unsigned rows
, unsigned columns
)
494 if (base_type
== GLSL_TYPE_VOID
)
497 if ((rows
< 1) || (rows
> 4) || (columns
< 1) || (columns
> 4))
500 /* Treat GLSL vectors as Nx1 matrices.
508 case GLSL_TYPE_FLOAT
:
510 case GLSL_TYPE_DOUBLE
:
518 if ((base_type
!= GLSL_TYPE_FLOAT
&& base_type
!= GLSL_TYPE_DOUBLE
) || (rows
== 1))
521 /* GLSL matrix types are named mat{COLUMNS}x{ROWS}. Only the following
522 * combinations are valid:
530 #define IDX(c,r) (((c-1)*3) + (r-1))
532 if (base_type
== GLSL_TYPE_DOUBLE
) {
533 switch (IDX(columns
, rows
)) {
534 case IDX(2,2): return dmat2_type
;
535 case IDX(2,3): return dmat2x3_type
;
536 case IDX(2,4): return dmat2x4_type
;
537 case IDX(3,2): return dmat3x2_type
;
538 case IDX(3,3): return dmat3_type
;
539 case IDX(3,4): return dmat3x4_type
;
540 case IDX(4,2): return dmat4x2_type
;
541 case IDX(4,3): return dmat4x3_type
;
542 case IDX(4,4): return dmat4_type
;
543 default: return error_type
;
546 switch (IDX(columns
, rows
)) {
547 case IDX(2,2): return mat2_type
;
548 case IDX(2,3): return mat2x3_type
;
549 case IDX(2,4): return mat2x4_type
;
550 case IDX(3,2): return mat3x2_type
;
551 case IDX(3,3): return mat3_type
;
552 case IDX(3,4): return mat3x4_type
;
553 case IDX(4,2): return mat4x2_type
;
554 case IDX(4,3): return mat4x3_type
;
555 case IDX(4,4): return mat4_type
;
556 default: return error_type
;
561 assert(!"Should not get here.");
566 glsl_type::get_sampler_instance(enum glsl_sampler_dim dim
,
572 case GLSL_TYPE_FLOAT
:
574 case GLSL_SAMPLER_DIM_1D
:
576 return (array
? sampler1DArrayShadow_type
: sampler1DShadow_type
);
578 return (array
? sampler1DArray_type
: sampler1D_type
);
579 case GLSL_SAMPLER_DIM_2D
:
581 return (array
? sampler2DArrayShadow_type
: sampler2DShadow_type
);
583 return (array
? sampler2DArray_type
: sampler2D_type
);
584 case GLSL_SAMPLER_DIM_3D
:
588 return sampler3D_type
;
589 case GLSL_SAMPLER_DIM_CUBE
:
591 return (array
? samplerCubeArrayShadow_type
: samplerCubeShadow_type
);
593 return (array
? samplerCubeArray_type
: samplerCube_type
);
594 case GLSL_SAMPLER_DIM_RECT
:
598 return sampler2DRectShadow_type
;
600 return sampler2DRect_type
;
601 case GLSL_SAMPLER_DIM_BUF
:
605 return samplerBuffer_type
;
606 case GLSL_SAMPLER_DIM_MS
:
609 return (array
? sampler2DMSArray_type
: sampler2DMS_type
);
610 case GLSL_SAMPLER_DIM_EXTERNAL
:
614 return samplerExternalOES_type
;
620 case GLSL_SAMPLER_DIM_1D
:
621 return (array
? isampler1DArray_type
: isampler1D_type
);
622 case GLSL_SAMPLER_DIM_2D
:
623 return (array
? isampler2DArray_type
: isampler2D_type
);
624 case GLSL_SAMPLER_DIM_3D
:
627 return isampler3D_type
;
628 case GLSL_SAMPLER_DIM_CUBE
:
629 return (array
? isamplerCubeArray_type
: isamplerCube_type
);
630 case GLSL_SAMPLER_DIM_RECT
:
633 return isampler2DRect_type
;
634 case GLSL_SAMPLER_DIM_BUF
:
637 return isamplerBuffer_type
;
638 case GLSL_SAMPLER_DIM_MS
:
639 return (array
? isampler2DMSArray_type
: isampler2DMS_type
);
640 case GLSL_SAMPLER_DIM_EXTERNAL
:
647 case GLSL_SAMPLER_DIM_1D
:
648 return (array
? usampler1DArray_type
: usampler1D_type
);
649 case GLSL_SAMPLER_DIM_2D
:
650 return (array
? usampler2DArray_type
: usampler2D_type
);
651 case GLSL_SAMPLER_DIM_3D
:
654 return usampler3D_type
;
655 case GLSL_SAMPLER_DIM_CUBE
:
656 return (array
? usamplerCubeArray_type
: usamplerCube_type
);
657 case GLSL_SAMPLER_DIM_RECT
:
660 return usampler2DRect_type
;
661 case GLSL_SAMPLER_DIM_BUF
:
664 return usamplerBuffer_type
;
665 case GLSL_SAMPLER_DIM_MS
:
666 return (array
? usampler2DMSArray_type
: usampler2DMS_type
);
667 case GLSL_SAMPLER_DIM_EXTERNAL
:
674 unreachable("switch statement above should be complete");
678 glsl_type::get_array_instance(const glsl_type
*base
, unsigned array_size
)
680 /* Generate a name using the base type pointer in the key. This is
681 * done because the name of the base type may not be unique across
682 * shaders. For example, two shaders may have different record types
686 snprintf(key
, sizeof(key
), "%p[%u]", (void *) base
, array_size
);
688 mtx_lock(&glsl_type::mutex
);
690 if (array_types
== NULL
) {
691 array_types
= _mesa_hash_table_create(NULL
, _mesa_key_hash_string
,
692 _mesa_key_string_equal
);
695 const struct hash_entry
*entry
= _mesa_hash_table_search(array_types
, key
);
697 mtx_unlock(&glsl_type::mutex
);
698 const glsl_type
*t
= new glsl_type(base
, array_size
);
699 mtx_lock(&glsl_type::mutex
);
701 entry
= _mesa_hash_table_insert(array_types
,
702 ralloc_strdup(mem_ctx
, key
),
706 assert(((glsl_type
*) entry
->data
)->base_type
== GLSL_TYPE_ARRAY
);
707 assert(((glsl_type
*) entry
->data
)->length
== array_size
);
708 assert(((glsl_type
*) entry
->data
)->fields
.array
== base
);
710 mtx_unlock(&glsl_type::mutex
);
712 return (glsl_type
*) entry
->data
;
717 glsl_type::record_compare(const glsl_type
*b
) const
719 if (this->length
!= b
->length
)
722 if (this->interface_packing
!= b
->interface_packing
)
725 /* From the GLSL 4.20 specification (Sec 4.2):
727 * "Structures must have the same name, sequence of type names, and
728 * type definitions, and field names to be considered the same type."
730 * GLSL ES behaves the same (Ver 1.00 Sec 4.2.4, Ver 3.00 Sec 4.2.5).
732 * Note that we cannot force type name check when comparing unnamed
733 * structure types, these have a unique name assigned during parsing.
735 if (!this->is_anonymous() && !b
->is_anonymous())
736 if (strcmp(this->name
, b
->name
) != 0)
739 for (unsigned i
= 0; i
< this->length
; i
++) {
740 if (this->fields
.structure
[i
].type
!= b
->fields
.structure
[i
].type
)
742 if (strcmp(this->fields
.structure
[i
].name
,
743 b
->fields
.structure
[i
].name
) != 0)
745 if (this->fields
.structure
[i
].matrix_layout
746 != b
->fields
.structure
[i
].matrix_layout
)
748 if (this->fields
.structure
[i
].location
749 != b
->fields
.structure
[i
].location
)
751 if (this->fields
.structure
[i
].interpolation
752 != b
->fields
.structure
[i
].interpolation
)
754 if (this->fields
.structure
[i
].centroid
755 != b
->fields
.structure
[i
].centroid
)
757 if (this->fields
.structure
[i
].sample
758 != b
->fields
.structure
[i
].sample
)
760 if (this->fields
.structure
[i
].patch
761 != b
->fields
.structure
[i
].patch
)
770 glsl_type::record_key_compare(const void *a
, const void *b
)
772 const glsl_type
*const key1
= (glsl_type
*) a
;
773 const glsl_type
*const key2
= (glsl_type
*) b
;
775 return strcmp(key1
->name
, key2
->name
) == 0 && key1
->record_compare(key2
);
780 * Generate an integer hash value for a glsl_type structure type.
783 glsl_type::record_key_hash(const void *a
)
785 const glsl_type
*const key
= (glsl_type
*) a
;
786 uintptr_t hash
= key
->length
;
789 for (unsigned i
= 0; i
< key
->length
; i
++) {
790 /* casting pointer to uintptr_t */
791 hash
= (hash
* 13 ) + (uintptr_t) key
->fields
.structure
[i
].type
;
794 if (sizeof(hash
) == 8)
795 retval
= (hash
& 0xffffffff) ^ ((uint64_t) hash
>> 32);
804 glsl_type::get_record_instance(const glsl_struct_field
*fields
,
808 const glsl_type
key(fields
, num_fields
, name
);
810 mtx_lock(&glsl_type::mutex
);
812 if (record_types
== NULL
) {
813 record_types
= _mesa_hash_table_create(NULL
, record_key_hash
,
817 const struct hash_entry
*entry
= _mesa_hash_table_search(record_types
,
820 mtx_unlock(&glsl_type::mutex
);
821 const glsl_type
*t
= new glsl_type(fields
, num_fields
, name
);
822 mtx_lock(&glsl_type::mutex
);
824 entry
= _mesa_hash_table_insert(record_types
, t
, (void *) t
);
827 assert(((glsl_type
*) entry
->data
)->base_type
== GLSL_TYPE_STRUCT
);
828 assert(((glsl_type
*) entry
->data
)->length
== num_fields
);
829 assert(strcmp(((glsl_type
*) entry
->data
)->name
, name
) == 0);
831 mtx_unlock(&glsl_type::mutex
);
833 return (glsl_type
*) entry
->data
;
838 glsl_type::get_interface_instance(const glsl_struct_field
*fields
,
840 enum glsl_interface_packing packing
,
841 const char *block_name
)
843 const glsl_type
key(fields
, num_fields
, packing
, block_name
);
845 mtx_lock(&glsl_type::mutex
);
847 if (interface_types
== NULL
) {
848 interface_types
= _mesa_hash_table_create(NULL
, record_key_hash
,
852 const struct hash_entry
*entry
= _mesa_hash_table_search(interface_types
,
855 mtx_unlock(&glsl_type::mutex
);
856 const glsl_type
*t
= new glsl_type(fields
, num_fields
,
857 packing
, block_name
);
858 mtx_lock(&glsl_type::mutex
);
860 entry
= _mesa_hash_table_insert(interface_types
, t
, (void *) t
);
863 assert(((glsl_type
*) entry
->data
)->base_type
== GLSL_TYPE_INTERFACE
);
864 assert(((glsl_type
*) entry
->data
)->length
== num_fields
);
865 assert(strcmp(((glsl_type
*) entry
->data
)->name
, block_name
) == 0);
867 mtx_unlock(&glsl_type::mutex
);
869 return (glsl_type
*) entry
->data
;
873 glsl_type::get_subroutine_instance(const char *subroutine_name
)
875 const glsl_type
key(subroutine_name
);
877 mtx_lock(&glsl_type::mutex
);
879 if (subroutine_types
== NULL
) {
880 subroutine_types
= _mesa_hash_table_create(NULL
, record_key_hash
,
884 const struct hash_entry
*entry
= _mesa_hash_table_search(subroutine_types
,
887 mtx_unlock(&glsl_type::mutex
);
888 const glsl_type
*t
= new glsl_type(subroutine_name
);
889 mtx_lock(&glsl_type::mutex
);
891 entry
= _mesa_hash_table_insert(subroutine_types
, t
, (void *) t
);
894 assert(((glsl_type
*) entry
->data
)->base_type
== GLSL_TYPE_SUBROUTINE
);
895 assert(strcmp(((glsl_type
*) entry
->data
)->name
, subroutine_name
) == 0);
897 mtx_unlock(&glsl_type::mutex
);
899 return (glsl_type
*) entry
->data
;
904 glsl_type::get_mul_type(const glsl_type
*type_a
, const glsl_type
*type_b
)
906 if (type_a
== type_b
) {
908 } else if (type_a
->is_matrix() && type_b
->is_matrix()) {
909 /* Matrix multiply. The columns of A must match the rows of B. Given
910 * the other previously tested constraints, this means the vector type
911 * of a row from A must be the same as the vector type of a column from
914 if (type_a
->row_type() == type_b
->column_type()) {
915 /* The resulting matrix has the number of columns of matrix B and
916 * the number of rows of matrix A. We get the row count of A by
917 * looking at the size of a vector that makes up a column. The
918 * transpose (size of a row) is done for B.
920 const glsl_type
*const type
=
921 get_instance(type_a
->base_type
,
922 type_a
->column_type()->vector_elements
,
923 type_b
->row_type()->vector_elements
);
924 assert(type
!= error_type
);
928 } else if (type_a
->is_matrix()) {
929 /* A is a matrix and B is a column vector. Columns of A must match
930 * rows of B. Given the other previously tested constraints, this
931 * means the vector type of a row from A must be the same as the
932 * vector the type of B.
934 if (type_a
->row_type() == type_b
) {
935 /* The resulting vector has a number of elements equal to
936 * the number of rows of matrix A. */
937 const glsl_type
*const type
=
938 get_instance(type_a
->base_type
,
939 type_a
->column_type()->vector_elements
,
941 assert(type
!= error_type
);
946 assert(type_b
->is_matrix());
948 /* A is a row vector and B is a matrix. Columns of A must match rows
949 * of B. Given the other previously tested constraints, this means
950 * the type of A must be the same as the vector type of a column from
953 if (type_a
== type_b
->column_type()) {
954 /* The resulting vector has a number of elements equal to
955 * the number of columns of matrix B. */
956 const glsl_type
*const type
=
957 get_instance(type_a
->base_type
,
958 type_b
->row_type()->vector_elements
,
960 assert(type
!= error_type
);
971 glsl_type::field_type(const char *name
) const
973 if (this->base_type
!= GLSL_TYPE_STRUCT
974 && this->base_type
!= GLSL_TYPE_INTERFACE
)
977 for (unsigned i
= 0; i
< this->length
; i
++) {
978 if (strcmp(name
, this->fields
.structure
[i
].name
) == 0)
979 return this->fields
.structure
[i
].type
;
987 glsl_type::field_index(const char *name
) const
989 if (this->base_type
!= GLSL_TYPE_STRUCT
990 && this->base_type
!= GLSL_TYPE_INTERFACE
)
993 for (unsigned i
= 0; i
< this->length
; i
++) {
994 if (strcmp(name
, this->fields
.structure
[i
].name
) == 0)
1003 glsl_type::component_slots() const
1005 switch (this->base_type
) {
1006 case GLSL_TYPE_UINT
:
1008 case GLSL_TYPE_FLOAT
:
1009 case GLSL_TYPE_BOOL
:
1010 return this->components();
1012 case GLSL_TYPE_DOUBLE
:
1013 return 2 * this->components();
1015 case GLSL_TYPE_STRUCT
:
1016 case GLSL_TYPE_INTERFACE
: {
1019 for (unsigned i
= 0; i
< this->length
; i
++)
1020 size
+= this->fields
.structure
[i
].type
->component_slots();
1025 case GLSL_TYPE_ARRAY
:
1026 return this->length
* this->fields
.array
->component_slots();
1028 case GLSL_TYPE_IMAGE
:
1030 case GLSL_TYPE_SUBROUTINE
:
1032 case GLSL_TYPE_SAMPLER
:
1033 case GLSL_TYPE_ATOMIC_UINT
:
1034 case GLSL_TYPE_VOID
:
1035 case GLSL_TYPE_ERROR
:
1043 glsl_type::uniform_locations() const
1047 switch (this->base_type
) {
1048 case GLSL_TYPE_UINT
:
1050 case GLSL_TYPE_FLOAT
:
1051 case GLSL_TYPE_DOUBLE
:
1052 case GLSL_TYPE_BOOL
:
1053 case GLSL_TYPE_SAMPLER
:
1054 case GLSL_TYPE_IMAGE
:
1055 case GLSL_TYPE_SUBROUTINE
:
1058 case GLSL_TYPE_STRUCT
:
1059 case GLSL_TYPE_INTERFACE
:
1060 for (unsigned i
= 0; i
< this->length
; i
++)
1061 size
+= this->fields
.structure
[i
].type
->uniform_locations();
1063 case GLSL_TYPE_ARRAY
:
1064 return this->length
* this->fields
.array
->uniform_locations();
1071 glsl_type::can_implicitly_convert_to(const glsl_type
*desired
,
1072 _mesa_glsl_parse_state
*state
) const
1074 if (this == desired
)
1077 /* There is no conversion among matrix types. */
1078 if (this->matrix_columns
> 1 || desired
->matrix_columns
> 1)
1081 /* Vector size must match. */
1082 if (this->vector_elements
!= desired
->vector_elements
)
1085 /* int and uint can be converted to float. */
1086 if (desired
->is_float() && this->is_integer())
1089 /* With GLSL 4.0 / ARB_gpu_shader5, int can be converted to uint.
1090 * Note that state may be NULL here, when resolving function calls in the
1091 * linker. By this time, all the state-dependent checks have already
1092 * happened though, so allow anything that's allowed in any shader version. */
1093 if ((!state
|| state
->is_version(400, 0) || state
->ARB_gpu_shader5_enable
) &&
1094 desired
->base_type
== GLSL_TYPE_UINT
&& this->base_type
== GLSL_TYPE_INT
)
1097 /* No implicit conversions from double. */
1098 if ((!state
|| state
->has_double()) && this->is_double())
1101 /* Conversions from different types to double. */
1102 if ((!state
|| state
->has_double()) && desired
->is_double()) {
1103 if (this->is_float())
1105 if (this->is_integer())
1113 glsl_type::std140_base_alignment(bool row_major
) const
1115 unsigned N
= is_double() ? 8 : 4;
1117 /* (1) If the member is a scalar consuming <N> basic machine units, the
1118 * base alignment is <N>.
1120 * (2) If the member is a two- or four-component vector with components
1121 * consuming <N> basic machine units, the base alignment is 2<N> or
1122 * 4<N>, respectively.
1124 * (3) If the member is a three-component vector with components consuming
1125 * <N> basic machine units, the base alignment is 4<N>.
1127 if (this->is_scalar() || this->is_vector()) {
1128 switch (this->vector_elements
) {
1139 /* (4) If the member is an array of scalars or vectors, the base alignment
1140 * and array stride are set to match the base alignment of a single
1141 * array element, according to rules (1), (2), and (3), and rounded up
1142 * to the base alignment of a vec4. The array may have padding at the
1143 * end; the base offset of the member following the array is rounded up
1144 * to the next multiple of the base alignment.
1146 * (6) If the member is an array of <S> column-major matrices with <C>
1147 * columns and <R> rows, the matrix is stored identically to a row of
1148 * <S>*<C> column vectors with <R> components each, according to rule
1151 * (8) If the member is an array of <S> row-major matrices with <C> columns
1152 * and <R> rows, the matrix is stored identically to a row of <S>*<R>
1153 * row vectors with <C> components each, according to rule (4).
1155 * (10) If the member is an array of <S> structures, the <S> elements of
1156 * the array are laid out in order, according to rule (9).
1158 if (this->is_array()) {
1159 if (this->fields
.array
->is_scalar() ||
1160 this->fields
.array
->is_vector() ||
1161 this->fields
.array
->is_matrix()) {
1162 return MAX2(this->fields
.array
->std140_base_alignment(row_major
), 16);
1164 assert(this->fields
.array
->is_record() ||
1165 this->fields
.array
->is_array());
1166 return this->fields
.array
->std140_base_alignment(row_major
);
1170 /* (5) If the member is a column-major matrix with <C> columns and
1171 * <R> rows, the matrix is stored identically to an array of
1172 * <C> column vectors with <R> components each, according to
1175 * (7) If the member is a row-major matrix with <C> columns and <R>
1176 * rows, the matrix is stored identically to an array of <R>
1177 * row vectors with <C> components each, according to rule (4).
1179 if (this->is_matrix()) {
1180 const struct glsl_type
*vec_type
, *array_type
;
1181 int c
= this->matrix_columns
;
1182 int r
= this->vector_elements
;
1185 vec_type
= get_instance(base_type
, c
, 1);
1186 array_type
= glsl_type::get_array_instance(vec_type
, r
);
1188 vec_type
= get_instance(base_type
, r
, 1);
1189 array_type
= glsl_type::get_array_instance(vec_type
, c
);
1192 return array_type
->std140_base_alignment(false);
1195 /* (9) If the member is a structure, the base alignment of the
1196 * structure is <N>, where <N> is the largest base alignment
1197 * value of any of its members, and rounded up to the base
1198 * alignment of a vec4. The individual members of this
1199 * sub-structure are then assigned offsets by applying this set
1200 * of rules recursively, where the base offset of the first
1201 * member of the sub-structure is equal to the aligned offset
1202 * of the structure. The structure may have padding at the end;
1203 * the base offset of the member following the sub-structure is
1204 * rounded up to the next multiple of the base alignment of the
1207 if (this->is_record()) {
1208 unsigned base_alignment
= 16;
1209 for (unsigned i
= 0; i
< this->length
; i
++) {
1210 bool field_row_major
= row_major
;
1211 const enum glsl_matrix_layout matrix_layout
=
1212 glsl_matrix_layout(this->fields
.structure
[i
].matrix_layout
);
1213 if (matrix_layout
== GLSL_MATRIX_LAYOUT_ROW_MAJOR
) {
1214 field_row_major
= true;
1215 } else if (matrix_layout
== GLSL_MATRIX_LAYOUT_COLUMN_MAJOR
) {
1216 field_row_major
= false;
1219 const struct glsl_type
*field_type
= this->fields
.structure
[i
].type
;
1220 base_alignment
= MAX2(base_alignment
,
1221 field_type
->std140_base_alignment(field_row_major
));
1223 return base_alignment
;
1226 assert(!"not reached");
1231 glsl_type::std140_size(bool row_major
) const
1233 unsigned N
= is_double() ? 8 : 4;
1235 /* (1) If the member is a scalar consuming <N> basic machine units, the
1236 * base alignment is <N>.
1238 * (2) If the member is a two- or four-component vector with components
1239 * consuming <N> basic machine units, the base alignment is 2<N> or
1240 * 4<N>, respectively.
1242 * (3) If the member is a three-component vector with components consuming
1243 * <N> basic machine units, the base alignment is 4<N>.
1245 if (this->is_scalar() || this->is_vector()) {
1246 return this->vector_elements
* N
;
1249 /* (5) If the member is a column-major matrix with <C> columns and
1250 * <R> rows, the matrix is stored identically to an array of
1251 * <C> column vectors with <R> components each, according to
1254 * (6) If the member is an array of <S> column-major matrices with <C>
1255 * columns and <R> rows, the matrix is stored identically to a row of
1256 * <S>*<C> column vectors with <R> components each, according to rule
1259 * (7) If the member is a row-major matrix with <C> columns and <R>
1260 * rows, the matrix is stored identically to an array of <R>
1261 * row vectors with <C> components each, according to rule (4).
1263 * (8) If the member is an array of <S> row-major matrices with <C> columns
1264 * and <R> rows, the matrix is stored identically to a row of <S>*<R>
1265 * row vectors with <C> components each, according to rule (4).
1267 if (this->without_array()->is_matrix()) {
1268 const struct glsl_type
*element_type
;
1269 const struct glsl_type
*vec_type
;
1270 unsigned int array_len
;
1272 if (this->is_array()) {
1273 element_type
= this->fields
.array
;
1274 array_len
= this->length
;
1276 element_type
= this;
1281 vec_type
= get_instance(element_type
->base_type
,
1282 element_type
->matrix_columns
, 1);
1284 array_len
*= element_type
->vector_elements
;
1286 vec_type
= get_instance(element_type
->base_type
,
1287 element_type
->vector_elements
, 1);
1288 array_len
*= element_type
->matrix_columns
;
1290 const glsl_type
*array_type
= glsl_type::get_array_instance(vec_type
,
1293 return array_type
->std140_size(false);
1296 /* (4) If the member is an array of scalars or vectors, the base alignment
1297 * and array stride are set to match the base alignment of a single
1298 * array element, according to rules (1), (2), and (3), and rounded up
1299 * to the base alignment of a vec4. The array may have padding at the
1300 * end; the base offset of the member following the array is rounded up
1301 * to the next multiple of the base alignment.
1303 * (10) If the member is an array of <S> structures, the <S> elements of
1304 * the array are laid out in order, according to rule (9).
1306 if (this->is_array()) {
1307 if (this->fields
.array
->is_record()) {
1308 return this->length
* this->fields
.array
->std140_size(row_major
);
1310 unsigned element_base_align
=
1311 this->fields
.array
->std140_base_alignment(row_major
);
1312 return this->length
* MAX2(element_base_align
, 16);
1316 /* (9) If the member is a structure, the base alignment of the
1317 * structure is <N>, where <N> is the largest base alignment
1318 * value of any of its members, and rounded up to the base
1319 * alignment of a vec4. The individual members of this
1320 * sub-structure are then assigned offsets by applying this set
1321 * of rules recursively, where the base offset of the first
1322 * member of the sub-structure is equal to the aligned offset
1323 * of the structure. The structure may have padding at the end;
1324 * the base offset of the member following the sub-structure is
1325 * rounded up to the next multiple of the base alignment of the
1328 if (this->is_record()) {
1330 unsigned max_align
= 0;
1332 for (unsigned i
= 0; i
< this->length
; i
++) {
1333 bool field_row_major
= row_major
;
1334 const enum glsl_matrix_layout matrix_layout
=
1335 glsl_matrix_layout(this->fields
.structure
[i
].matrix_layout
);
1336 if (matrix_layout
== GLSL_MATRIX_LAYOUT_ROW_MAJOR
) {
1337 field_row_major
= true;
1338 } else if (matrix_layout
== GLSL_MATRIX_LAYOUT_COLUMN_MAJOR
) {
1339 field_row_major
= false;
1342 const struct glsl_type
*field_type
= this->fields
.structure
[i
].type
;
1343 unsigned align
= field_type
->std140_base_alignment(field_row_major
);
1344 size
= glsl_align(size
, align
);
1345 size
+= field_type
->std140_size(field_row_major
);
1347 max_align
= MAX2(align
, max_align
);
1349 if (field_type
->is_record() && (i
+ 1 < this->length
))
1350 size
= glsl_align(size
, 16);
1352 size
= glsl_align(size
, MAX2(max_align
, 16));
1356 assert(!"not reached");
1362 glsl_type::count_attribute_slots() const
1364 /* From page 31 (page 37 of the PDF) of the GLSL 1.50 spec:
1366 * "A scalar input counts the same amount against this limit as a vec4,
1367 * so applications may want to consider packing groups of four
1368 * unrelated float inputs together into a vector to better utilize the
1369 * capabilities of the underlying hardware. A matrix input will use up
1370 * multiple locations. The number of locations used will equal the
1371 * number of columns in the matrix."
1373 * The spec does not explicitly say how arrays are counted. However, it
1374 * should be safe to assume the total number of slots consumed by an array
1375 * is the number of entries in the array multiplied by the number of slots
1376 * consumed by a single element of the array.
1378 * The spec says nothing about how structs are counted, because vertex
1379 * attributes are not allowed to be (or contain) structs. However, Mesa
1380 * allows varying structs, the number of varying slots taken up by a
1381 * varying struct is simply equal to the sum of the number of slots taken
1382 * up by each element.
1384 switch (this->base_type
) {
1385 case GLSL_TYPE_UINT
:
1387 case GLSL_TYPE_FLOAT
:
1388 case GLSL_TYPE_BOOL
:
1389 case GLSL_TYPE_DOUBLE
:
1390 return this->matrix_columns
;
1392 case GLSL_TYPE_STRUCT
:
1393 case GLSL_TYPE_INTERFACE
: {
1396 for (unsigned i
= 0; i
< this->length
; i
++)
1397 size
+= this->fields
.structure
[i
].type
->count_attribute_slots();
1402 case GLSL_TYPE_ARRAY
:
1403 return this->length
* this->fields
.array
->count_attribute_slots();
1405 case GLSL_TYPE_SAMPLER
:
1406 case GLSL_TYPE_IMAGE
:
1407 case GLSL_TYPE_ATOMIC_UINT
:
1408 case GLSL_TYPE_VOID
:
1409 case GLSL_TYPE_SUBROUTINE
:
1410 case GLSL_TYPE_ERROR
:
1414 assert(!"Unexpected type in count_attribute_slots()");
1420 glsl_type::coordinate_components() const
1424 switch (sampler_dimensionality
) {
1425 case GLSL_SAMPLER_DIM_1D
:
1426 case GLSL_SAMPLER_DIM_BUF
:
1429 case GLSL_SAMPLER_DIM_2D
:
1430 case GLSL_SAMPLER_DIM_RECT
:
1431 case GLSL_SAMPLER_DIM_MS
:
1432 case GLSL_SAMPLER_DIM_EXTERNAL
:
1435 case GLSL_SAMPLER_DIM_3D
:
1436 case GLSL_SAMPLER_DIM_CUBE
:
1440 assert(!"Should not get here.");
1445 /* Array textures need an additional component for the array index, except
1446 * for cubemap array images that behave like a 2D array of interleaved
1449 if (sampler_array
&&
1450 !(base_type
== GLSL_TYPE_IMAGE
&&
1451 sampler_dimensionality
== GLSL_SAMPLER_DIM_CUBE
))