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::record_location_offset(unsigned length
) const
1045 unsigned offset
= 0;
1046 const glsl_type
*t
= this->without_array();
1047 if (t
->is_record()) {
1048 assert(length
<= t
->length
);
1050 for (unsigned i
= 0; i
< length
; i
++) {
1051 const glsl_type
*st
= t
->fields
.structure
[i
].type
;
1052 const glsl_type
*wa
= st
->without_array();
1053 if (wa
->is_record()) {
1054 unsigned r_offset
= wa
->record_location_offset(wa
->length
);
1055 offset
+= st
->is_array() ? st
->length
* r_offset
: r_offset
;
1057 /* We dont worry about arrays here because unless the array
1058 * contains a structure or another array it only takes up a single
1069 glsl_type::uniform_locations() const
1073 switch (this->base_type
) {
1074 case GLSL_TYPE_UINT
:
1076 case GLSL_TYPE_FLOAT
:
1077 case GLSL_TYPE_DOUBLE
:
1078 case GLSL_TYPE_BOOL
:
1079 case GLSL_TYPE_SAMPLER
:
1080 case GLSL_TYPE_IMAGE
:
1081 case GLSL_TYPE_SUBROUTINE
:
1084 case GLSL_TYPE_STRUCT
:
1085 case GLSL_TYPE_INTERFACE
:
1086 for (unsigned i
= 0; i
< this->length
; i
++)
1087 size
+= this->fields
.structure
[i
].type
->uniform_locations();
1089 case GLSL_TYPE_ARRAY
:
1090 return this->length
* this->fields
.array
->uniform_locations();
1097 glsl_type::can_implicitly_convert_to(const glsl_type
*desired
,
1098 _mesa_glsl_parse_state
*state
) const
1100 if (this == desired
)
1103 /* There is no conversion among matrix types. */
1104 if (this->matrix_columns
> 1 || desired
->matrix_columns
> 1)
1107 /* Vector size must match. */
1108 if (this->vector_elements
!= desired
->vector_elements
)
1111 /* int and uint can be converted to float. */
1112 if (desired
->is_float() && this->is_integer())
1115 /* With GLSL 4.0 / ARB_gpu_shader5, int can be converted to uint.
1116 * Note that state may be NULL here, when resolving function calls in the
1117 * linker. By this time, all the state-dependent checks have already
1118 * happened though, so allow anything that's allowed in any shader version. */
1119 if ((!state
|| state
->is_version(400, 0) || state
->ARB_gpu_shader5_enable
) &&
1120 desired
->base_type
== GLSL_TYPE_UINT
&& this->base_type
== GLSL_TYPE_INT
)
1123 /* No implicit conversions from double. */
1124 if ((!state
|| state
->has_double()) && this->is_double())
1127 /* Conversions from different types to double. */
1128 if ((!state
|| state
->has_double()) && desired
->is_double()) {
1129 if (this->is_float())
1131 if (this->is_integer())
1139 glsl_type::std140_base_alignment(bool row_major
) const
1141 unsigned N
= is_double() ? 8 : 4;
1143 /* (1) If the member is a scalar consuming <N> basic machine units, the
1144 * base alignment is <N>.
1146 * (2) If the member is a two- or four-component vector with components
1147 * consuming <N> basic machine units, the base alignment is 2<N> or
1148 * 4<N>, respectively.
1150 * (3) If the member is a three-component vector with components consuming
1151 * <N> basic machine units, the base alignment is 4<N>.
1153 if (this->is_scalar() || this->is_vector()) {
1154 switch (this->vector_elements
) {
1165 /* (4) If the member is an array of scalars or vectors, the base alignment
1166 * and array stride are set to match the base alignment of a single
1167 * array element, according to rules (1), (2), and (3), and rounded up
1168 * to the base alignment of a vec4. The array may have padding at the
1169 * end; the base offset of the member following the array is rounded up
1170 * to the next multiple of the base alignment.
1172 * (6) If the member is an array of <S> column-major matrices with <C>
1173 * columns and <R> rows, the matrix is stored identically to a row of
1174 * <S>*<C> column vectors with <R> components each, according to rule
1177 * (8) If the member is an array of <S> row-major matrices with <C> columns
1178 * and <R> rows, the matrix is stored identically to a row of <S>*<R>
1179 * row vectors with <C> components each, according to rule (4).
1181 * (10) If the member is an array of <S> structures, the <S> elements of
1182 * the array are laid out in order, according to rule (9).
1184 if (this->is_array()) {
1185 if (this->fields
.array
->is_scalar() ||
1186 this->fields
.array
->is_vector() ||
1187 this->fields
.array
->is_matrix()) {
1188 return MAX2(this->fields
.array
->std140_base_alignment(row_major
), 16);
1190 assert(this->fields
.array
->is_record() ||
1191 this->fields
.array
->is_array());
1192 return this->fields
.array
->std140_base_alignment(row_major
);
1196 /* (5) If the member is a column-major matrix with <C> columns and
1197 * <R> rows, the matrix is stored identically to an array of
1198 * <C> column vectors with <R> components each, according to
1201 * (7) If the member is a row-major matrix with <C> columns and <R>
1202 * rows, the matrix is stored identically to an array of <R>
1203 * row vectors with <C> components each, according to rule (4).
1205 if (this->is_matrix()) {
1206 const struct glsl_type
*vec_type
, *array_type
;
1207 int c
= this->matrix_columns
;
1208 int r
= this->vector_elements
;
1211 vec_type
= get_instance(base_type
, c
, 1);
1212 array_type
= glsl_type::get_array_instance(vec_type
, r
);
1214 vec_type
= get_instance(base_type
, r
, 1);
1215 array_type
= glsl_type::get_array_instance(vec_type
, c
);
1218 return array_type
->std140_base_alignment(false);
1221 /* (9) If the member is a structure, the base alignment of the
1222 * structure is <N>, where <N> is the largest base alignment
1223 * value of any of its members, and rounded up to the base
1224 * alignment of a vec4. The individual members of this
1225 * sub-structure are then assigned offsets by applying this set
1226 * of rules recursively, where the base offset of the first
1227 * member of the sub-structure is equal to the aligned offset
1228 * of the structure. The structure may have padding at the end;
1229 * the base offset of the member following the sub-structure is
1230 * rounded up to the next multiple of the base alignment of the
1233 if (this->is_record()) {
1234 unsigned base_alignment
= 16;
1235 for (unsigned i
= 0; i
< this->length
; i
++) {
1236 bool field_row_major
= row_major
;
1237 const enum glsl_matrix_layout matrix_layout
=
1238 glsl_matrix_layout(this->fields
.structure
[i
].matrix_layout
);
1239 if (matrix_layout
== GLSL_MATRIX_LAYOUT_ROW_MAJOR
) {
1240 field_row_major
= true;
1241 } else if (matrix_layout
== GLSL_MATRIX_LAYOUT_COLUMN_MAJOR
) {
1242 field_row_major
= false;
1245 const struct glsl_type
*field_type
= this->fields
.structure
[i
].type
;
1246 base_alignment
= MAX2(base_alignment
,
1247 field_type
->std140_base_alignment(field_row_major
));
1249 return base_alignment
;
1252 assert(!"not reached");
1257 glsl_type::std140_size(bool row_major
) const
1259 unsigned N
= is_double() ? 8 : 4;
1261 /* (1) If the member is a scalar consuming <N> basic machine units, the
1262 * base alignment is <N>.
1264 * (2) If the member is a two- or four-component vector with components
1265 * consuming <N> basic machine units, the base alignment is 2<N> or
1266 * 4<N>, respectively.
1268 * (3) If the member is a three-component vector with components consuming
1269 * <N> basic machine units, the base alignment is 4<N>.
1271 if (this->is_scalar() || this->is_vector()) {
1272 return this->vector_elements
* N
;
1275 /* (5) If the member is a column-major matrix with <C> columns and
1276 * <R> rows, the matrix is stored identically to an array of
1277 * <C> column vectors with <R> components each, according to
1280 * (6) If the member is an array of <S> column-major matrices with <C>
1281 * columns and <R> rows, the matrix is stored identically to a row of
1282 * <S>*<C> column vectors with <R> components each, according to rule
1285 * (7) If the member is a row-major matrix with <C> columns and <R>
1286 * rows, the matrix is stored identically to an array of <R>
1287 * row vectors with <C> components each, according to rule (4).
1289 * (8) If the member is an array of <S> row-major matrices with <C> columns
1290 * and <R> rows, the matrix is stored identically to a row of <S>*<R>
1291 * row vectors with <C> components each, according to rule (4).
1293 if (this->without_array()->is_matrix()) {
1294 const struct glsl_type
*element_type
;
1295 const struct glsl_type
*vec_type
;
1296 unsigned int array_len
;
1298 if (this->is_array()) {
1299 element_type
= this->fields
.array
;
1300 array_len
= this->length
;
1302 element_type
= this;
1307 vec_type
= get_instance(element_type
->base_type
,
1308 element_type
->matrix_columns
, 1);
1310 array_len
*= element_type
->vector_elements
;
1312 vec_type
= get_instance(element_type
->base_type
,
1313 element_type
->vector_elements
, 1);
1314 array_len
*= element_type
->matrix_columns
;
1316 const glsl_type
*array_type
= glsl_type::get_array_instance(vec_type
,
1319 return array_type
->std140_size(false);
1322 /* (4) If the member is an array of scalars or vectors, the base alignment
1323 * and array stride are set to match the base alignment of a single
1324 * array element, according to rules (1), (2), and (3), and rounded up
1325 * to the base alignment of a vec4. The array may have padding at the
1326 * end; the base offset of the member following the array is rounded up
1327 * to the next multiple of the base alignment.
1329 * (10) If the member is an array of <S> structures, the <S> elements of
1330 * the array are laid out in order, according to rule (9).
1332 if (this->is_array()) {
1333 if (this->fields
.array
->is_record()) {
1334 return this->length
* this->fields
.array
->std140_size(row_major
);
1336 unsigned element_base_align
=
1337 this->fields
.array
->std140_base_alignment(row_major
);
1338 return this->length
* MAX2(element_base_align
, 16);
1342 /* (9) If the member is a structure, the base alignment of the
1343 * structure is <N>, where <N> is the largest base alignment
1344 * value of any of its members, and rounded up to the base
1345 * alignment of a vec4. The individual members of this
1346 * sub-structure are then assigned offsets by applying this set
1347 * of rules recursively, where the base offset of the first
1348 * member of the sub-structure is equal to the aligned offset
1349 * of the structure. The structure may have padding at the end;
1350 * the base offset of the member following the sub-structure is
1351 * rounded up to the next multiple of the base alignment of the
1354 if (this->is_record()) {
1356 unsigned max_align
= 0;
1358 for (unsigned i
= 0; i
< this->length
; i
++) {
1359 bool field_row_major
= row_major
;
1360 const enum glsl_matrix_layout matrix_layout
=
1361 glsl_matrix_layout(this->fields
.structure
[i
].matrix_layout
);
1362 if (matrix_layout
== GLSL_MATRIX_LAYOUT_ROW_MAJOR
) {
1363 field_row_major
= true;
1364 } else if (matrix_layout
== GLSL_MATRIX_LAYOUT_COLUMN_MAJOR
) {
1365 field_row_major
= false;
1368 const struct glsl_type
*field_type
= this->fields
.structure
[i
].type
;
1369 unsigned align
= field_type
->std140_base_alignment(field_row_major
);
1370 size
= glsl_align(size
, align
);
1371 size
+= field_type
->std140_size(field_row_major
);
1373 max_align
= MAX2(align
, max_align
);
1375 if (field_type
->is_record() && (i
+ 1 < this->length
))
1376 size
= glsl_align(size
, 16);
1378 size
= glsl_align(size
, MAX2(max_align
, 16));
1382 assert(!"not reached");
1388 glsl_type::count_attribute_slots() const
1390 /* From page 31 (page 37 of the PDF) of the GLSL 1.50 spec:
1392 * "A scalar input counts the same amount against this limit as a vec4,
1393 * so applications may want to consider packing groups of four
1394 * unrelated float inputs together into a vector to better utilize the
1395 * capabilities of the underlying hardware. A matrix input will use up
1396 * multiple locations. The number of locations used will equal the
1397 * number of columns in the matrix."
1399 * The spec does not explicitly say how arrays are counted. However, it
1400 * should be safe to assume the total number of slots consumed by an array
1401 * is the number of entries in the array multiplied by the number of slots
1402 * consumed by a single element of the array.
1404 * The spec says nothing about how structs are counted, because vertex
1405 * attributes are not allowed to be (or contain) structs. However, Mesa
1406 * allows varying structs, the number of varying slots taken up by a
1407 * varying struct is simply equal to the sum of the number of slots taken
1408 * up by each element.
1410 switch (this->base_type
) {
1411 case GLSL_TYPE_UINT
:
1413 case GLSL_TYPE_FLOAT
:
1414 case GLSL_TYPE_BOOL
:
1415 case GLSL_TYPE_DOUBLE
:
1416 return this->matrix_columns
;
1418 case GLSL_TYPE_STRUCT
:
1419 case GLSL_TYPE_INTERFACE
: {
1422 for (unsigned i
= 0; i
< this->length
; i
++)
1423 size
+= this->fields
.structure
[i
].type
->count_attribute_slots();
1428 case GLSL_TYPE_ARRAY
:
1429 return this->length
* this->fields
.array
->count_attribute_slots();
1431 case GLSL_TYPE_SAMPLER
:
1432 case GLSL_TYPE_IMAGE
:
1433 case GLSL_TYPE_ATOMIC_UINT
:
1434 case GLSL_TYPE_VOID
:
1435 case GLSL_TYPE_SUBROUTINE
:
1436 case GLSL_TYPE_ERROR
:
1440 assert(!"Unexpected type in count_attribute_slots()");
1446 glsl_type::coordinate_components() const
1450 switch (sampler_dimensionality
) {
1451 case GLSL_SAMPLER_DIM_1D
:
1452 case GLSL_SAMPLER_DIM_BUF
:
1455 case GLSL_SAMPLER_DIM_2D
:
1456 case GLSL_SAMPLER_DIM_RECT
:
1457 case GLSL_SAMPLER_DIM_MS
:
1458 case GLSL_SAMPLER_DIM_EXTERNAL
:
1461 case GLSL_SAMPLER_DIM_3D
:
1462 case GLSL_SAMPLER_DIM_CUBE
:
1466 assert(!"Should not get here.");
1471 /* Array textures need an additional component for the array index, except
1472 * for cubemap array images that behave like a 2D array of interleaved
1475 if (sampler_array
&&
1476 !(base_type
== GLSL_TYPE_IMAGE
&&
1477 sampler_dimensionality
== GLSL_SAMPLER_DIM_CUBE
))