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 "program/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::function_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
;
128 mtx_unlock(&glsl_type::mutex
);
131 glsl_type::glsl_type(const glsl_struct_field
*fields
, unsigned num_fields
,
132 enum glsl_interface_packing packing
, const char *name
) :
134 base_type(GLSL_TYPE_INTERFACE
),
135 sampler_dimensionality(0), sampler_shadow(0), sampler_array(0),
136 sampler_type(0), interface_packing((unsigned) packing
),
137 vector_elements(0), matrix_columns(0),
142 mtx_lock(&glsl_type::mutex
);
144 init_ralloc_type_ctx();
145 assert(name
!= NULL
);
146 this->name
= ralloc_strdup(this->mem_ctx
, name
);
147 this->fields
.structure
= ralloc_array(this->mem_ctx
,
148 glsl_struct_field
, length
);
149 for (i
= 0; i
< length
; i
++) {
150 this->fields
.structure
[i
].type
= fields
[i
].type
;
151 this->fields
.structure
[i
].name
= ralloc_strdup(this->fields
.structure
,
153 this->fields
.structure
[i
].location
= fields
[i
].location
;
154 this->fields
.structure
[i
].interpolation
= fields
[i
].interpolation
;
155 this->fields
.structure
[i
].centroid
= fields
[i
].centroid
;
156 this->fields
.structure
[i
].sample
= fields
[i
].sample
;
157 this->fields
.structure
[i
].matrix_layout
= fields
[i
].matrix_layout
;
160 mtx_unlock(&glsl_type::mutex
);
163 glsl_type::glsl_type(const glsl_type
*return_type
,
164 const glsl_function_param
*params
, unsigned num_params
) :
166 base_type(GLSL_TYPE_FUNCTION
),
167 sampler_dimensionality(0), sampler_shadow(0), sampler_array(0),
168 sampler_type(0), interface_packing(0),
169 vector_elements(0), matrix_columns(0),
174 mtx_lock(&glsl_type::mutex
);
176 init_ralloc_type_ctx();
178 this->fields
.parameters
= rzalloc_array(this->mem_ctx
,
179 glsl_function_param
, num_params
+ 1);
181 /* We store the return type as the first parameter */
182 this->fields
.parameters
[0].type
= return_type
;
183 this->fields
.parameters
[0].in
= false;
184 this->fields
.parameters
[0].out
= true;
186 /* We store the i'th parameter in slot i+1 */
187 for (i
= 0; i
< length
; i
++) {
188 this->fields
.parameters
[i
+ 1].type
= params
[i
].type
;
189 this->fields
.parameters
[i
+ 1].in
= params
[i
].in
;
190 this->fields
.parameters
[i
+ 1].out
= params
[i
].out
;
193 mtx_unlock(&glsl_type::mutex
);
198 glsl_type::contains_sampler() const
200 if (this->is_array()) {
201 return this->fields
.array
->contains_sampler();
202 } else if (this->is_record()) {
203 for (unsigned int i
= 0; i
< this->length
; i
++) {
204 if (this->fields
.structure
[i
].type
->contains_sampler())
209 return this->is_sampler();
215 glsl_type::contains_integer() const
217 if (this->is_array()) {
218 return this->fields
.array
->contains_integer();
219 } else if (this->is_record()) {
220 for (unsigned int i
= 0; i
< this->length
; i
++) {
221 if (this->fields
.structure
[i
].type
->contains_integer())
226 return this->is_integer();
231 glsl_type::contains_double() const
233 if (this->is_array()) {
234 return this->fields
.array
->contains_double();
235 } else if (this->is_record()) {
236 for (unsigned int i
= 0; i
< this->length
; i
++) {
237 if (this->fields
.structure
[i
].type
->contains_double())
242 return this->is_double();
247 glsl_type::contains_opaque() const {
249 case GLSL_TYPE_SAMPLER
:
250 case GLSL_TYPE_IMAGE
:
251 case GLSL_TYPE_ATOMIC_UINT
:
253 case GLSL_TYPE_ARRAY
:
254 return fields
.array
->contains_opaque();
255 case GLSL_TYPE_STRUCT
:
256 for (unsigned int i
= 0; i
< length
; i
++) {
257 if (fields
.structure
[i
].type
->contains_opaque())
267 glsl_type::sampler_index() const
269 const glsl_type
*const t
= (this->is_array()) ? this->fields
.array
: this;
271 assert(t
->is_sampler());
273 switch (t
->sampler_dimensionality
) {
274 case GLSL_SAMPLER_DIM_1D
:
275 return (t
->sampler_array
) ? TEXTURE_1D_ARRAY_INDEX
: TEXTURE_1D_INDEX
;
276 case GLSL_SAMPLER_DIM_2D
:
277 return (t
->sampler_array
) ? TEXTURE_2D_ARRAY_INDEX
: TEXTURE_2D_INDEX
;
278 case GLSL_SAMPLER_DIM_3D
:
279 return TEXTURE_3D_INDEX
;
280 case GLSL_SAMPLER_DIM_CUBE
:
281 return (t
->sampler_array
) ? TEXTURE_CUBE_ARRAY_INDEX
: TEXTURE_CUBE_INDEX
;
282 case GLSL_SAMPLER_DIM_RECT
:
283 return TEXTURE_RECT_INDEX
;
284 case GLSL_SAMPLER_DIM_BUF
:
285 return TEXTURE_BUFFER_INDEX
;
286 case GLSL_SAMPLER_DIM_EXTERNAL
:
287 return TEXTURE_EXTERNAL_INDEX
;
288 case GLSL_SAMPLER_DIM_MS
:
289 return (t
->sampler_array
) ? TEXTURE_2D_MULTISAMPLE_ARRAY_INDEX
: TEXTURE_2D_MULTISAMPLE_INDEX
;
291 assert(!"Should not get here.");
292 return TEXTURE_BUFFER_INDEX
;
297 glsl_type::contains_image() const
299 if (this->is_array()) {
300 return this->fields
.array
->contains_image();
301 } else if (this->is_record()) {
302 for (unsigned int i
= 0; i
< this->length
; i
++) {
303 if (this->fields
.structure
[i
].type
->contains_image())
308 return this->is_image();
312 const glsl_type
*glsl_type::get_base_type() const
319 case GLSL_TYPE_FLOAT
:
321 case GLSL_TYPE_DOUBLE
:
331 const glsl_type
*glsl_type::get_scalar_type() const
333 const glsl_type
*type
= this;
336 while (type
->base_type
== GLSL_TYPE_ARRAY
)
337 type
= type
->fields
.array
;
339 /* Handle vectors and matrices */
340 switch (type
->base_type
) {
345 case GLSL_TYPE_FLOAT
:
347 case GLSL_TYPE_DOUBLE
:
352 /* Handle everything else */
359 _mesa_glsl_release_types(void)
361 mtx_lock(&glsl_type::mutex
);
363 if (glsl_type::array_types
!= NULL
) {
364 hash_table_dtor(glsl_type::array_types
);
365 glsl_type::array_types
= NULL
;
368 if (glsl_type::record_types
!= NULL
) {
369 hash_table_dtor(glsl_type::record_types
);
370 glsl_type::record_types
= NULL
;
373 mtx_unlock(&glsl_type::mutex
);
377 glsl_type::glsl_type(const glsl_type
*array
, unsigned length
) :
378 base_type(GLSL_TYPE_ARRAY
),
379 sampler_dimensionality(0), sampler_shadow(0), sampler_array(0),
380 sampler_type(0), interface_packing(0),
381 vector_elements(0), matrix_columns(0),
382 length(length
), name(NULL
)
384 this->fields
.array
= array
;
385 /* Inherit the gl type of the base. The GL type is used for
386 * uniform/statevar handling in Mesa and the arrayness of the type
387 * is represented by the size rather than the type.
389 this->gl_type
= array
->gl_type
;
391 /* Allow a maximum of 10 characters for the array size. This is enough
392 * for 32-bits of ~0. The extra 3 are for the '[', ']', and terminating
395 const unsigned name_length
= strlen(array
->name
) + 10 + 3;
397 mtx_lock(&glsl_type::mutex
);
398 char *const n
= (char *) ralloc_size(this->mem_ctx
, name_length
);
399 mtx_unlock(&glsl_type::mutex
);
402 snprintf(n
, name_length
, "%s[]", array
->name
);
404 /* insert outermost dimensions in the correct spot
405 * otherwise the dimension order will be backwards
407 const char *pos
= strchr(array
->name
, '[');
409 int idx
= pos
- array
->name
;
410 snprintf(n
, idx
+1, "%s", array
->name
);
411 snprintf(n
+ idx
, name_length
- idx
, "[%u]%s",
412 length
, array
->name
+ idx
);
414 snprintf(n
, name_length
, "%s[%u]", array
->name
, length
);
423 glsl_type::vec(unsigned components
)
425 if (components
== 0 || components
> 4)
428 static const glsl_type
*const ts
[] = {
429 float_type
, vec2_type
, vec3_type
, vec4_type
431 return ts
[components
- 1];
435 glsl_type::dvec(unsigned components
)
437 if (components
== 0 || components
> 4)
440 static const glsl_type
*const ts
[] = {
441 double_type
, dvec2_type
, dvec3_type
, dvec4_type
443 return ts
[components
- 1];
447 glsl_type::ivec(unsigned components
)
449 if (components
== 0 || components
> 4)
452 static const glsl_type
*const ts
[] = {
453 int_type
, ivec2_type
, ivec3_type
, ivec4_type
455 return ts
[components
- 1];
460 glsl_type::uvec(unsigned components
)
462 if (components
== 0 || components
> 4)
465 static const glsl_type
*const ts
[] = {
466 uint_type
, uvec2_type
, uvec3_type
, uvec4_type
468 return ts
[components
- 1];
473 glsl_type::bvec(unsigned components
)
475 if (components
== 0 || components
> 4)
478 static const glsl_type
*const ts
[] = {
479 bool_type
, bvec2_type
, bvec3_type
, bvec4_type
481 return ts
[components
- 1];
486 glsl_type::get_instance(unsigned base_type
, unsigned rows
, unsigned columns
)
488 if (base_type
== GLSL_TYPE_VOID
)
491 if ((rows
< 1) || (rows
> 4) || (columns
< 1) || (columns
> 4))
494 /* Treat GLSL vectors as Nx1 matrices.
502 case GLSL_TYPE_FLOAT
:
504 case GLSL_TYPE_DOUBLE
:
512 if ((base_type
!= GLSL_TYPE_FLOAT
&& base_type
!= GLSL_TYPE_DOUBLE
) || (rows
== 1))
515 /* GLSL matrix types are named mat{COLUMNS}x{ROWS}. Only the following
516 * combinations are valid:
524 #define IDX(c,r) (((c-1)*3) + (r-1))
526 if (base_type
== GLSL_TYPE_DOUBLE
) {
527 switch (IDX(columns
, rows
)) {
528 case IDX(2,2): return dmat2_type
;
529 case IDX(2,3): return dmat2x3_type
;
530 case IDX(2,4): return dmat2x4_type
;
531 case IDX(3,2): return dmat3x2_type
;
532 case IDX(3,3): return dmat3_type
;
533 case IDX(3,4): return dmat3x4_type
;
534 case IDX(4,2): return dmat4x2_type
;
535 case IDX(4,3): return dmat4x3_type
;
536 case IDX(4,4): return dmat4_type
;
537 default: return error_type
;
540 switch (IDX(columns
, rows
)) {
541 case IDX(2,2): return mat2_type
;
542 case IDX(2,3): return mat2x3_type
;
543 case IDX(2,4): return mat2x4_type
;
544 case IDX(3,2): return mat3x2_type
;
545 case IDX(3,3): return mat3_type
;
546 case IDX(3,4): return mat3x4_type
;
547 case IDX(4,2): return mat4x2_type
;
548 case IDX(4,3): return mat4x3_type
;
549 case IDX(4,4): return mat4_type
;
550 default: return error_type
;
555 assert(!"Should not get here.");
560 glsl_type::get_sampler_instance(enum glsl_sampler_dim dim
,
566 case GLSL_TYPE_FLOAT
:
568 case GLSL_SAMPLER_DIM_1D
:
570 return (array
? sampler1DArrayShadow_type
: sampler1DShadow_type
);
572 return (array
? sampler1DArray_type
: sampler1D_type
);
573 case GLSL_SAMPLER_DIM_2D
:
575 return (array
? sampler2DArrayShadow_type
: sampler2DShadow_type
);
577 return (array
? sampler2DArray_type
: sampler2D_type
);
578 case GLSL_SAMPLER_DIM_3D
:
582 return sampler3D_type
;
583 case GLSL_SAMPLER_DIM_CUBE
:
585 return (array
? samplerCubeArrayShadow_type
: samplerCubeShadow_type
);
587 return (array
? samplerCubeArray_type
: samplerCube_type
);
588 case GLSL_SAMPLER_DIM_RECT
:
592 return sampler2DRectShadow_type
;
594 return sampler2DRect_type
;
595 case GLSL_SAMPLER_DIM_BUF
:
599 return samplerBuffer_type
;
600 case GLSL_SAMPLER_DIM_MS
:
603 return (array
? sampler2DMSArray_type
: sampler2DMS_type
);
604 case GLSL_SAMPLER_DIM_EXTERNAL
:
608 return samplerExternalOES_type
;
614 case GLSL_SAMPLER_DIM_1D
:
615 return (array
? isampler1DArray_type
: isampler1D_type
);
616 case GLSL_SAMPLER_DIM_2D
:
617 return (array
? isampler2DArray_type
: isampler2D_type
);
618 case GLSL_SAMPLER_DIM_3D
:
621 return isampler3D_type
;
622 case GLSL_SAMPLER_DIM_CUBE
:
623 return (array
? isamplerCubeArray_type
: isamplerCube_type
);
624 case GLSL_SAMPLER_DIM_RECT
:
627 return isampler2DRect_type
;
628 case GLSL_SAMPLER_DIM_BUF
:
631 return isamplerBuffer_type
;
632 case GLSL_SAMPLER_DIM_MS
:
633 return (array
? isampler2DMSArray_type
: isampler2DMS_type
);
634 case GLSL_SAMPLER_DIM_EXTERNAL
:
641 case GLSL_SAMPLER_DIM_1D
:
642 return (array
? usampler1DArray_type
: usampler1D_type
);
643 case GLSL_SAMPLER_DIM_2D
:
644 return (array
? usampler2DArray_type
: usampler2D_type
);
645 case GLSL_SAMPLER_DIM_3D
:
648 return usampler3D_type
;
649 case GLSL_SAMPLER_DIM_CUBE
:
650 return (array
? usamplerCubeArray_type
: usamplerCube_type
);
651 case GLSL_SAMPLER_DIM_RECT
:
654 return usampler2DRect_type
;
655 case GLSL_SAMPLER_DIM_BUF
:
658 return usamplerBuffer_type
;
659 case GLSL_SAMPLER_DIM_MS
:
660 return (array
? usampler2DMSArray_type
: usampler2DMS_type
);
661 case GLSL_SAMPLER_DIM_EXTERNAL
:
668 unreachable("switch statement above should be complete");
672 glsl_type::get_array_instance(const glsl_type
*base
, unsigned array_size
)
674 /* Generate a name using the base type pointer in the key. This is
675 * done because the name of the base type may not be unique across
676 * shaders. For example, two shaders may have different record types
680 snprintf(key
, sizeof(key
), "%p[%u]", (void *) base
, array_size
);
682 mtx_lock(&glsl_type::mutex
);
684 if (array_types
== NULL
) {
685 array_types
= hash_table_ctor(64, hash_table_string_hash
,
686 hash_table_string_compare
);
689 const glsl_type
*t
= (glsl_type
*) hash_table_find(array_types
, key
);
692 mtx_unlock(&glsl_type::mutex
);
693 t
= new glsl_type(base
, array_size
);
694 mtx_lock(&glsl_type::mutex
);
696 hash_table_insert(array_types
, (void *) t
, ralloc_strdup(mem_ctx
, key
));
699 assert(t
->base_type
== GLSL_TYPE_ARRAY
);
700 assert(t
->length
== array_size
);
701 assert(t
->fields
.array
== base
);
703 mtx_unlock(&glsl_type::mutex
);
710 glsl_type::record_compare(const glsl_type
*b
) const
712 if (this->length
!= b
->length
)
715 if (this->interface_packing
!= b
->interface_packing
)
718 /* From the GLSL 4.20 specification (Sec 4.2):
720 * "Structures must have the same name, sequence of type names, and
721 * type definitions, and field names to be considered the same type."
723 * GLSL ES behaves the same (Ver 1.00 Sec 4.2.4, Ver 3.00 Sec 4.2.5).
725 * Note that we cannot force type name check when comparing unnamed
726 * structure types, these have a unique name assigned during parsing.
728 if (!this->is_anonymous() && !b
->is_anonymous())
729 if (strcmp(this->name
, b
->name
) != 0)
732 for (unsigned i
= 0; i
< this->length
; i
++) {
733 if (this->fields
.structure
[i
].type
!= b
->fields
.structure
[i
].type
)
735 if (strcmp(this->fields
.structure
[i
].name
,
736 b
->fields
.structure
[i
].name
) != 0)
738 if (this->fields
.structure
[i
].matrix_layout
739 != b
->fields
.structure
[i
].matrix_layout
)
741 if (this->fields
.structure
[i
].location
742 != b
->fields
.structure
[i
].location
)
744 if (this->fields
.structure
[i
].interpolation
745 != b
->fields
.structure
[i
].interpolation
)
747 if (this->fields
.structure
[i
].centroid
748 != b
->fields
.structure
[i
].centroid
)
750 if (this->fields
.structure
[i
].sample
751 != b
->fields
.structure
[i
].sample
)
760 glsl_type::record_key_compare(const void *a
, const void *b
)
762 const glsl_type
*const key1
= (glsl_type
*) a
;
763 const glsl_type
*const key2
= (glsl_type
*) b
;
765 /* Return zero is the types match (there is zero difference) or non-zero
768 if (strcmp(key1
->name
, key2
->name
) != 0)
771 return !key1
->record_compare(key2
);
776 * Generate an integer hash value for a glsl_type structure type.
779 glsl_type::record_key_hash(const void *a
)
781 const glsl_type
*const key
= (glsl_type
*) a
;
782 uintptr_t hash
= key
->length
;
785 for (unsigned i
= 0; i
< key
->length
; i
++) {
786 /* casting pointer to uintptr_t */
787 hash
= (hash
* 13 ) + (uintptr_t) key
->fields
.structure
[i
].type
;
790 if (sizeof(hash
) == 8)
791 retval
= (hash
& 0xffffffff) ^ ((uint64_t) hash
>> 32);
800 glsl_type::get_record_instance(const glsl_struct_field
*fields
,
804 const glsl_type
key(fields
, num_fields
, name
);
806 mtx_lock(&glsl_type::mutex
);
808 if (record_types
== NULL
) {
809 record_types
= hash_table_ctor(64, record_key_hash
, record_key_compare
);
812 const glsl_type
*t
= (glsl_type
*) hash_table_find(record_types
, & key
);
814 mtx_unlock(&glsl_type::mutex
);
815 t
= new glsl_type(fields
, num_fields
, name
);
816 mtx_lock(&glsl_type::mutex
);
818 hash_table_insert(record_types
, (void *) t
, t
);
821 assert(t
->base_type
== GLSL_TYPE_STRUCT
);
822 assert(t
->length
== num_fields
);
823 assert(strcmp(t
->name
, name
) == 0);
825 mtx_unlock(&glsl_type::mutex
);
832 glsl_type::get_interface_instance(const glsl_struct_field
*fields
,
834 enum glsl_interface_packing packing
,
835 const char *block_name
)
837 const glsl_type
key(fields
, num_fields
, packing
, block_name
);
839 mtx_lock(&glsl_type::mutex
);
841 if (interface_types
== NULL
) {
842 interface_types
= hash_table_ctor(64, record_key_hash
, record_key_compare
);
845 const glsl_type
*t
= (glsl_type
*) hash_table_find(interface_types
, & key
);
847 mtx_unlock(&glsl_type::mutex
);
848 t
= new glsl_type(fields
, num_fields
, packing
, block_name
);
849 mtx_lock(&glsl_type::mutex
);
851 hash_table_insert(interface_types
, (void *) t
, t
);
854 assert(t
->base_type
== GLSL_TYPE_INTERFACE
);
855 assert(t
->length
== num_fields
);
856 assert(strcmp(t
->name
, block_name
) == 0);
858 mtx_unlock(&glsl_type::mutex
);
865 function_key_compare(const void *a
, const void *b
)
867 const glsl_type
*const key1
= (glsl_type
*) a
;
868 const glsl_type
*const key2
= (glsl_type
*) b
;
870 if (key1
->length
!= key2
->length
)
873 return memcmp(key1
->fields
.parameters
, key2
->fields
.parameters
,
874 (key1
->length
+ 1) * sizeof(*key1
->fields
.parameters
));
879 function_key_hash(const void *a
)
881 const glsl_type
*const key
= (glsl_type
*) a
;
885 size
= snprintf(hash_key
, sizeof(hash_key
), "%08x", key
->length
);
887 for (unsigned i
= 0; i
< key
->length
; i
++) {
888 if (size
>= sizeof(hash_key
))
891 size
+= snprintf(& hash_key
[size
], sizeof(hash_key
) - size
,
892 "%p", (void *) key
->fields
.structure
[i
].type
);
895 return hash_table_string_hash(& hash_key
);
899 glsl_type::get_function_instance(const glsl_type
*return_type
,
900 const glsl_function_param
*params
,
903 const glsl_type
key(return_type
, params
, num_params
);
905 mtx_lock(&glsl_type::mutex
);
907 if (function_types
== NULL
) {
908 function_types
= hash_table_ctor(64, function_key_hash
,
909 function_key_compare
);
912 const glsl_type
*t
= (glsl_type
*) hash_table_find(function_types
, &key
);
914 mtx_unlock(&glsl_type::mutex
);
915 t
= new glsl_type(return_type
, params
, num_params
);
916 mtx_lock(&glsl_type::mutex
);
918 hash_table_insert(function_types
, (void *) t
, t
);
921 assert(t
->base_type
== GLSL_TYPE_FUNCTION
);
922 assert(t
->length
== num_params
);
924 mtx_unlock(&glsl_type::mutex
);
931 glsl_type::get_mul_type(const glsl_type
*type_a
, const glsl_type
*type_b
)
933 if (type_a
== type_b
) {
935 } else if (type_a
->is_matrix() && type_b
->is_matrix()) {
936 /* Matrix multiply. The columns of A must match the rows of B. Given
937 * the other previously tested constraints, this means the vector type
938 * of a row from A must be the same as the vector type of a column from
941 if (type_a
->row_type() == type_b
->column_type()) {
942 /* The resulting matrix has the number of columns of matrix B and
943 * the number of rows of matrix A. We get the row count of A by
944 * looking at the size of a vector that makes up a column. The
945 * transpose (size of a row) is done for B.
947 const glsl_type
*const type
=
948 get_instance(type_a
->base_type
,
949 type_a
->column_type()->vector_elements
,
950 type_b
->row_type()->vector_elements
);
951 assert(type
!= error_type
);
955 } else if (type_a
->is_matrix()) {
956 /* A is a matrix and B is a column vector. Columns of A must match
957 * rows of B. Given the other previously tested constraints, this
958 * means the vector type of a row from A must be the same as the
959 * vector the type of B.
961 if (type_a
->row_type() == type_b
) {
962 /* The resulting vector has a number of elements equal to
963 * the number of rows of matrix A. */
964 const glsl_type
*const type
=
965 get_instance(type_a
->base_type
,
966 type_a
->column_type()->vector_elements
,
968 assert(type
!= error_type
);
973 assert(type_b
->is_matrix());
975 /* A is a row vector and B is a matrix. Columns of A must match rows
976 * of B. Given the other previously tested constraints, this means
977 * the type of A must be the same as the vector type of a column from
980 if (type_a
== type_b
->column_type()) {
981 /* The resulting vector has a number of elements equal to
982 * the number of columns of matrix B. */
983 const glsl_type
*const type
=
984 get_instance(type_a
->base_type
,
985 type_b
->row_type()->vector_elements
,
987 assert(type
!= error_type
);
998 glsl_type::field_type(const char *name
) const
1000 if (this->base_type
!= GLSL_TYPE_STRUCT
1001 && this->base_type
!= GLSL_TYPE_INTERFACE
)
1004 for (unsigned i
= 0; i
< this->length
; i
++) {
1005 if (strcmp(name
, this->fields
.structure
[i
].name
) == 0)
1006 return this->fields
.structure
[i
].type
;
1014 glsl_type::field_index(const char *name
) const
1016 if (this->base_type
!= GLSL_TYPE_STRUCT
1017 && this->base_type
!= GLSL_TYPE_INTERFACE
)
1020 for (unsigned i
= 0; i
< this->length
; i
++) {
1021 if (strcmp(name
, this->fields
.structure
[i
].name
) == 0)
1030 glsl_type::component_slots() const
1032 switch (this->base_type
) {
1033 case GLSL_TYPE_UINT
:
1035 case GLSL_TYPE_FLOAT
:
1036 case GLSL_TYPE_BOOL
:
1037 return this->components();
1039 case GLSL_TYPE_DOUBLE
:
1040 return 2 * this->components();
1042 case GLSL_TYPE_STRUCT
:
1043 case GLSL_TYPE_INTERFACE
: {
1046 for (unsigned i
= 0; i
< this->length
; i
++)
1047 size
+= this->fields
.structure
[i
].type
->component_slots();
1052 case GLSL_TYPE_ARRAY
:
1053 return this->length
* this->fields
.array
->component_slots();
1055 case GLSL_TYPE_IMAGE
:
1058 case GLSL_TYPE_FUNCTION
:
1059 case GLSL_TYPE_SAMPLER
:
1060 case GLSL_TYPE_ATOMIC_UINT
:
1061 case GLSL_TYPE_VOID
:
1062 case GLSL_TYPE_ERROR
:
1070 glsl_type::uniform_locations() const
1074 switch (this->base_type
) {
1075 case GLSL_TYPE_UINT
:
1077 case GLSL_TYPE_FLOAT
:
1078 case GLSL_TYPE_DOUBLE
:
1079 case GLSL_TYPE_BOOL
:
1080 case GLSL_TYPE_SAMPLER
:
1081 case GLSL_TYPE_IMAGE
:
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 return this->fields
.array
->std140_base_alignment(row_major
);
1195 /* (5) If the member is a column-major matrix with <C> columns and
1196 * <R> rows, the matrix is stored identically to an array of
1197 * <C> column vectors with <R> components each, according to
1200 * (7) If the member is a row-major matrix with <C> columns and <R>
1201 * rows, the matrix is stored identically to an array of <R>
1202 * row vectors with <C> components each, according to rule (4).
1204 if (this->is_matrix()) {
1205 const struct glsl_type
*vec_type
, *array_type
;
1206 int c
= this->matrix_columns
;
1207 int r
= this->vector_elements
;
1210 vec_type
= get_instance(base_type
, c
, 1);
1211 array_type
= glsl_type::get_array_instance(vec_type
, r
);
1213 vec_type
= get_instance(base_type
, r
, 1);
1214 array_type
= glsl_type::get_array_instance(vec_type
, c
);
1217 return array_type
->std140_base_alignment(false);
1220 /* (9) If the member is a structure, the base alignment of the
1221 * structure is <N>, where <N> is the largest base alignment
1222 * value of any of its members, and rounded up to the base
1223 * alignment of a vec4. The individual members of this
1224 * sub-structure are then assigned offsets by applying this set
1225 * of rules recursively, where the base offset of the first
1226 * member of the sub-structure is equal to the aligned offset
1227 * of the structure. The structure may have padding at the end;
1228 * the base offset of the member following the sub-structure is
1229 * rounded up to the next multiple of the base alignment of the
1232 if (this->is_record()) {
1233 unsigned base_alignment
= 16;
1234 for (unsigned i
= 0; i
< this->length
; i
++) {
1235 bool field_row_major
= row_major
;
1236 const enum glsl_matrix_layout matrix_layout
=
1237 glsl_matrix_layout(this->fields
.structure
[i
].matrix_layout
);
1238 if (matrix_layout
== GLSL_MATRIX_LAYOUT_ROW_MAJOR
) {
1239 field_row_major
= true;
1240 } else if (matrix_layout
== GLSL_MATRIX_LAYOUT_COLUMN_MAJOR
) {
1241 field_row_major
= false;
1244 const struct glsl_type
*field_type
= this->fields
.structure
[i
].type
;
1245 base_alignment
= MAX2(base_alignment
,
1246 field_type
->std140_base_alignment(field_row_major
));
1248 return base_alignment
;
1251 assert(!"not reached");
1256 glsl_type::std140_size(bool row_major
) const
1258 unsigned N
= is_double() ? 8 : 4;
1260 /* (1) If the member is a scalar consuming <N> basic machine units, the
1261 * base alignment is <N>.
1263 * (2) If the member is a two- or four-component vector with components
1264 * consuming <N> basic machine units, the base alignment is 2<N> or
1265 * 4<N>, respectively.
1267 * (3) If the member is a three-component vector with components consuming
1268 * <N> basic machine units, the base alignment is 4<N>.
1270 if (this->is_scalar() || this->is_vector()) {
1271 return this->vector_elements
* N
;
1274 /* (5) If the member is a column-major matrix with <C> columns and
1275 * <R> rows, the matrix is stored identically to an array of
1276 * <C> column vectors with <R> components each, according to
1279 * (6) If the member is an array of <S> column-major matrices with <C>
1280 * columns and <R> rows, the matrix is stored identically to a row of
1281 * <S>*<C> column vectors with <R> components each, according to rule
1284 * (7) If the member is a row-major matrix with <C> columns and <R>
1285 * rows, the matrix is stored identically to an array of <R>
1286 * row vectors with <C> components each, according to rule (4).
1288 * (8) If the member is an array of <S> row-major matrices with <C> columns
1289 * and <R> rows, the matrix is stored identically to a row of <S>*<R>
1290 * row vectors with <C> components each, according to rule (4).
1292 if (this->without_array()->is_matrix()) {
1293 const struct glsl_type
*element_type
;
1294 const struct glsl_type
*vec_type
;
1295 unsigned int array_len
;
1297 if (this->is_array()) {
1298 element_type
= this->fields
.array
;
1299 array_len
= this->length
;
1301 element_type
= this;
1306 vec_type
= get_instance(element_type
->base_type
,
1307 element_type
->matrix_columns
, 1);
1309 array_len
*= element_type
->vector_elements
;
1311 vec_type
= get_instance(element_type
->base_type
,
1312 element_type
->vector_elements
, 1);
1313 array_len
*= element_type
->matrix_columns
;
1315 const glsl_type
*array_type
= glsl_type::get_array_instance(vec_type
,
1318 return array_type
->std140_size(false);
1321 /* (4) If the member is an array of scalars or vectors, the base alignment
1322 * and array stride are set to match the base alignment of a single
1323 * array element, according to rules (1), (2), and (3), and rounded up
1324 * to the base alignment of a vec4. The array may have padding at the
1325 * end; the base offset of the member following the array is rounded up
1326 * to the next multiple of the base alignment.
1328 * (10) If the member is an array of <S> structures, the <S> elements of
1329 * the array are laid out in order, according to rule (9).
1331 if (this->is_array()) {
1332 if (this->fields
.array
->is_record()) {
1333 return this->length
* this->fields
.array
->std140_size(row_major
);
1335 unsigned element_base_align
=
1336 this->fields
.array
->std140_base_alignment(row_major
);
1337 return this->length
* MAX2(element_base_align
, 16);
1341 /* (9) If the member is a structure, the base alignment of the
1342 * structure is <N>, where <N> is the largest base alignment
1343 * value of any of its members, and rounded up to the base
1344 * alignment of a vec4. The individual members of this
1345 * sub-structure are then assigned offsets by applying this set
1346 * of rules recursively, where the base offset of the first
1347 * member of the sub-structure is equal to the aligned offset
1348 * of the structure. The structure may have padding at the end;
1349 * the base offset of the member following the sub-structure is
1350 * rounded up to the next multiple of the base alignment of the
1353 if (this->is_record()) {
1355 unsigned max_align
= 0;
1357 for (unsigned i
= 0; i
< this->length
; i
++) {
1358 bool field_row_major
= row_major
;
1359 const enum glsl_matrix_layout matrix_layout
=
1360 glsl_matrix_layout(this->fields
.structure
[i
].matrix_layout
);
1361 if (matrix_layout
== GLSL_MATRIX_LAYOUT_ROW_MAJOR
) {
1362 field_row_major
= true;
1363 } else if (matrix_layout
== GLSL_MATRIX_LAYOUT_COLUMN_MAJOR
) {
1364 field_row_major
= false;
1367 const struct glsl_type
*field_type
= this->fields
.structure
[i
].type
;
1368 unsigned align
= field_type
->std140_base_alignment(field_row_major
);
1369 size
= glsl_align(size
, align
);
1370 size
+= field_type
->std140_size(field_row_major
);
1372 max_align
= MAX2(align
, max_align
);
1374 if (field_type
->is_record() && (i
+ 1 < this->length
))
1375 size
= glsl_align(size
, 16);
1377 size
= glsl_align(size
, MAX2(max_align
, 16));
1381 assert(!"not reached");
1387 glsl_type::count_attribute_slots() const
1389 /* From page 31 (page 37 of the PDF) of the GLSL 1.50 spec:
1391 * "A scalar input counts the same amount against this limit as a vec4,
1392 * so applications may want to consider packing groups of four
1393 * unrelated float inputs together into a vector to better utilize the
1394 * capabilities of the underlying hardware. A matrix input will use up
1395 * multiple locations. The number of locations used will equal the
1396 * number of columns in the matrix."
1398 * The spec does not explicitly say how arrays are counted. However, it
1399 * should be safe to assume the total number of slots consumed by an array
1400 * is the number of entries in the array multiplied by the number of slots
1401 * consumed by a single element of the array.
1403 * The spec says nothing about how structs are counted, because vertex
1404 * attributes are not allowed to be (or contain) structs. However, Mesa
1405 * allows varying structs, the number of varying slots taken up by a
1406 * varying struct is simply equal to the sum of the number of slots taken
1407 * up by each element.
1409 switch (this->base_type
) {
1410 case GLSL_TYPE_UINT
:
1412 case GLSL_TYPE_FLOAT
:
1413 case GLSL_TYPE_BOOL
:
1414 case GLSL_TYPE_DOUBLE
:
1415 return this->matrix_columns
;
1417 case GLSL_TYPE_STRUCT
:
1418 case GLSL_TYPE_INTERFACE
: {
1421 for (unsigned i
= 0; i
< this->length
; i
++)
1422 size
+= this->fields
.structure
[i
].type
->count_attribute_slots();
1427 case GLSL_TYPE_ARRAY
:
1428 return this->length
* this->fields
.array
->count_attribute_slots();
1430 case GLSL_TYPE_FUNCTION
:
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_ERROR
:
1439 assert(!"Unexpected type in count_attribute_slots()");
1445 glsl_type::coordinate_components() const
1449 switch (sampler_dimensionality
) {
1450 case GLSL_SAMPLER_DIM_1D
:
1451 case GLSL_SAMPLER_DIM_BUF
:
1454 case GLSL_SAMPLER_DIM_2D
:
1455 case GLSL_SAMPLER_DIM_RECT
:
1456 case GLSL_SAMPLER_DIM_MS
:
1457 case GLSL_SAMPLER_DIM_EXTERNAL
:
1460 case GLSL_SAMPLER_DIM_3D
:
1461 case GLSL_SAMPLER_DIM_CUBE
:
1465 assert(!"Should not get here.");
1470 /* Array textures need an additional component for the array index, except
1471 * for cubemap array images that behave like a 2D array of interleaved
1474 if (sampler_array
&&
1475 !(base_type
== GLSL_TYPE_IMAGE
&&
1476 sampler_dimensionality
== GLSL_SAMPLER_DIM_CUBE
))