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::function_types
= NULL
;
36 hash_table
*glsl_type::subroutine_types
= NULL
;
37 void *glsl_type::mem_ctx
= NULL
;
40 glsl_type::init_ralloc_type_ctx(void)
42 if (glsl_type::mem_ctx
== NULL
) {
43 glsl_type::mem_ctx
= ralloc_autofree_context();
44 assert(glsl_type::mem_ctx
!= NULL
);
48 glsl_type::glsl_type(GLenum gl_type
,
49 glsl_base_type base_type
, unsigned vector_elements
,
50 unsigned matrix_columns
, const char *name
) :
53 sampler_dimensionality(0), sampler_shadow(0), sampler_array(0),
54 sampler_type(0), interface_packing(0),
55 vector_elements(vector_elements
), matrix_columns(matrix_columns
),
58 mtx_lock(&glsl_type::mutex
);
60 init_ralloc_type_ctx();
62 this->name
= ralloc_strdup(this->mem_ctx
, name
);
64 mtx_unlock(&glsl_type::mutex
);
66 /* Neither dimension is zero or both dimensions are zero.
68 assert((vector_elements
== 0) == (matrix_columns
== 0));
69 memset(& fields
, 0, sizeof(fields
));
72 glsl_type::glsl_type(GLenum gl_type
, glsl_base_type base_type
,
73 enum glsl_sampler_dim dim
, bool shadow
, bool array
,
74 unsigned type
, const char *name
) :
77 sampler_dimensionality(dim
), sampler_shadow(shadow
),
78 sampler_array(array
), sampler_type(type
), interface_packing(0),
81 mtx_lock(&glsl_type::mutex
);
83 init_ralloc_type_ctx();
85 this->name
= ralloc_strdup(this->mem_ctx
, name
);
87 mtx_unlock(&glsl_type::mutex
);
89 memset(& fields
, 0, sizeof(fields
));
91 if (base_type
== GLSL_TYPE_SAMPLER
) {
92 /* Samplers take no storage whatsoever. */
93 matrix_columns
= vector_elements
= 0;
95 matrix_columns
= vector_elements
= 1;
99 glsl_type::glsl_type(const glsl_struct_field
*fields
, unsigned num_fields
,
102 base_type(GLSL_TYPE_STRUCT
),
103 sampler_dimensionality(0), sampler_shadow(0), sampler_array(0),
104 sampler_type(0), interface_packing(0),
105 vector_elements(0), matrix_columns(0),
110 mtx_lock(&glsl_type::mutex
);
112 init_ralloc_type_ctx();
113 assert(name
!= NULL
);
114 this->name
= ralloc_strdup(this->mem_ctx
, name
);
115 this->fields
.structure
= ralloc_array(this->mem_ctx
,
116 glsl_struct_field
, length
);
118 for (i
= 0; i
< length
; i
++) {
119 this->fields
.structure
[i
].type
= fields
[i
].type
;
120 this->fields
.structure
[i
].name
= ralloc_strdup(this->fields
.structure
,
122 this->fields
.structure
[i
].location
= fields
[i
].location
;
123 this->fields
.structure
[i
].interpolation
= fields
[i
].interpolation
;
124 this->fields
.structure
[i
].centroid
= fields
[i
].centroid
;
125 this->fields
.structure
[i
].sample
= fields
[i
].sample
;
126 this->fields
.structure
[i
].matrix_layout
= fields
[i
].matrix_layout
;
127 this->fields
.structure
[i
].patch
= fields
[i
].patch
;
130 mtx_unlock(&glsl_type::mutex
);
133 glsl_type::glsl_type(const glsl_struct_field
*fields
, unsigned num_fields
,
134 enum glsl_interface_packing packing
, const char *name
) :
136 base_type(GLSL_TYPE_INTERFACE
),
137 sampler_dimensionality(0), sampler_shadow(0), sampler_array(0),
138 sampler_type(0), interface_packing((unsigned) packing
),
139 vector_elements(0), matrix_columns(0),
144 mtx_lock(&glsl_type::mutex
);
146 init_ralloc_type_ctx();
147 assert(name
!= NULL
);
148 this->name
= ralloc_strdup(this->mem_ctx
, name
);
149 this->fields
.structure
= ralloc_array(this->mem_ctx
,
150 glsl_struct_field
, length
);
151 for (i
= 0; i
< length
; i
++) {
152 this->fields
.structure
[i
].type
= fields
[i
].type
;
153 this->fields
.structure
[i
].name
= ralloc_strdup(this->fields
.structure
,
155 this->fields
.structure
[i
].location
= fields
[i
].location
;
156 this->fields
.structure
[i
].interpolation
= fields
[i
].interpolation
;
157 this->fields
.structure
[i
].centroid
= fields
[i
].centroid
;
158 this->fields
.structure
[i
].sample
= fields
[i
].sample
;
159 this->fields
.structure
[i
].matrix_layout
= fields
[i
].matrix_layout
;
160 this->fields
.structure
[i
].patch
= fields
[i
].patch
;
163 mtx_unlock(&glsl_type::mutex
);
166 glsl_type::glsl_type(const glsl_type
*return_type
,
167 const glsl_function_param
*params
, unsigned num_params
) :
169 base_type(GLSL_TYPE_FUNCTION
),
170 sampler_dimensionality(0), sampler_shadow(0), sampler_array(0),
171 sampler_type(0), interface_packing(0),
172 vector_elements(0), matrix_columns(0),
177 mtx_lock(&glsl_type::mutex
);
179 init_ralloc_type_ctx();
181 this->fields
.parameters
= rzalloc_array(this->mem_ctx
,
182 glsl_function_param
, num_params
+ 1);
184 /* We store the return type as the first parameter */
185 this->fields
.parameters
[0].type
= return_type
;
186 this->fields
.parameters
[0].in
= false;
187 this->fields
.parameters
[0].out
= true;
189 /* We store the i'th parameter in slot i+1 */
190 for (i
= 0; i
< length
; i
++) {
191 this->fields
.parameters
[i
+ 1].type
= params
[i
].type
;
192 this->fields
.parameters
[i
+ 1].in
= params
[i
].in
;
193 this->fields
.parameters
[i
+ 1].out
= params
[i
].out
;
196 mtx_unlock(&glsl_type::mutex
);
199 glsl_type::glsl_type(const char *subroutine_name
) :
201 base_type(GLSL_TYPE_SUBROUTINE
),
202 sampler_dimensionality(0), sampler_shadow(0), sampler_array(0),
203 sampler_type(0), interface_packing(0),
204 vector_elements(0), matrix_columns(0),
207 mtx_lock(&glsl_type::mutex
);
209 init_ralloc_type_ctx();
210 assert(subroutine_name
!= NULL
);
211 this->name
= ralloc_strdup(this->mem_ctx
, subroutine_name
);
212 this->vector_elements
= 1;
213 mtx_unlock(&glsl_type::mutex
);
217 glsl_type::contains_sampler() const
219 if (this->is_array()) {
220 return this->fields
.array
->contains_sampler();
221 } else if (this->is_record()) {
222 for (unsigned int i
= 0; i
< this->length
; i
++) {
223 if (this->fields
.structure
[i
].type
->contains_sampler())
228 return this->is_sampler();
234 glsl_type::contains_integer() const
236 if (this->is_array()) {
237 return this->fields
.array
->contains_integer();
238 } else if (this->is_record()) {
239 for (unsigned int i
= 0; i
< this->length
; i
++) {
240 if (this->fields
.structure
[i
].type
->contains_integer())
245 return this->is_integer();
250 glsl_type::contains_double() const
252 if (this->is_array()) {
253 return this->fields
.array
->contains_double();
254 } else if (this->is_record()) {
255 for (unsigned int i
= 0; i
< this->length
; i
++) {
256 if (this->fields
.structure
[i
].type
->contains_double())
261 return this->is_double();
266 glsl_type::contains_opaque() const {
268 case GLSL_TYPE_SAMPLER
:
269 case GLSL_TYPE_IMAGE
:
270 case GLSL_TYPE_ATOMIC_UINT
:
272 case GLSL_TYPE_ARRAY
:
273 return fields
.array
->contains_opaque();
274 case GLSL_TYPE_STRUCT
:
275 for (unsigned int i
= 0; i
< length
; i
++) {
276 if (fields
.structure
[i
].type
->contains_opaque())
286 glsl_type::contains_subroutine() const
288 if (this->is_array()) {
289 return this->fields
.array
->contains_subroutine();
290 } else if (this->is_record()) {
291 for (unsigned int i
= 0; i
< this->length
; i
++) {
292 if (this->fields
.structure
[i
].type
->contains_subroutine())
297 return this->is_subroutine();
302 glsl_type::sampler_index() const
304 const glsl_type
*const t
= (this->is_array()) ? this->fields
.array
: this;
306 assert(t
->is_sampler());
308 switch (t
->sampler_dimensionality
) {
309 case GLSL_SAMPLER_DIM_1D
:
310 return (t
->sampler_array
) ? TEXTURE_1D_ARRAY_INDEX
: TEXTURE_1D_INDEX
;
311 case GLSL_SAMPLER_DIM_2D
:
312 return (t
->sampler_array
) ? TEXTURE_2D_ARRAY_INDEX
: TEXTURE_2D_INDEX
;
313 case GLSL_SAMPLER_DIM_3D
:
314 return TEXTURE_3D_INDEX
;
315 case GLSL_SAMPLER_DIM_CUBE
:
316 return (t
->sampler_array
) ? TEXTURE_CUBE_ARRAY_INDEX
: TEXTURE_CUBE_INDEX
;
317 case GLSL_SAMPLER_DIM_RECT
:
318 return TEXTURE_RECT_INDEX
;
319 case GLSL_SAMPLER_DIM_BUF
:
320 return TEXTURE_BUFFER_INDEX
;
321 case GLSL_SAMPLER_DIM_EXTERNAL
:
322 return TEXTURE_EXTERNAL_INDEX
;
323 case GLSL_SAMPLER_DIM_MS
:
324 return (t
->sampler_array
) ? TEXTURE_2D_MULTISAMPLE_ARRAY_INDEX
: TEXTURE_2D_MULTISAMPLE_INDEX
;
326 assert(!"Should not get here.");
327 return TEXTURE_BUFFER_INDEX
;
332 glsl_type::contains_image() const
334 if (this->is_array()) {
335 return this->fields
.array
->contains_image();
336 } else if (this->is_record()) {
337 for (unsigned int i
= 0; i
< this->length
; i
++) {
338 if (this->fields
.structure
[i
].type
->contains_image())
343 return this->is_image();
347 const glsl_type
*glsl_type::get_base_type() const
354 case GLSL_TYPE_FLOAT
:
356 case GLSL_TYPE_DOUBLE
:
366 const glsl_type
*glsl_type::get_scalar_type() const
368 const glsl_type
*type
= this;
371 while (type
->base_type
== GLSL_TYPE_ARRAY
)
372 type
= type
->fields
.array
;
374 /* Handle vectors and matrices */
375 switch (type
->base_type
) {
380 case GLSL_TYPE_FLOAT
:
382 case GLSL_TYPE_DOUBLE
:
387 /* Handle everything else */
394 _mesa_glsl_release_types(void)
396 /* Should only be called during atexit (either when unloading shared
397 * object, or if process terminates), so no mutex-locking should be
400 if (glsl_type::array_types
!= NULL
) {
401 _mesa_hash_table_destroy(glsl_type::array_types
, NULL
);
402 glsl_type::array_types
= NULL
;
405 if (glsl_type::record_types
!= NULL
) {
406 _mesa_hash_table_destroy(glsl_type::record_types
, NULL
);
407 glsl_type::record_types
= NULL
;
410 if (glsl_type::interface_types
!= NULL
) {
411 _mesa_hash_table_destroy(glsl_type::interface_types
, NULL
);
412 glsl_type::interface_types
= NULL
;
417 glsl_type::glsl_type(const glsl_type
*array
, unsigned length
) :
418 base_type(GLSL_TYPE_ARRAY
),
419 sampler_dimensionality(0), sampler_shadow(0), sampler_array(0),
420 sampler_type(0), interface_packing(0),
421 vector_elements(0), matrix_columns(0),
422 length(length
), name(NULL
)
424 this->fields
.array
= array
;
425 /* Inherit the gl type of the base. The GL type is used for
426 * uniform/statevar handling in Mesa and the arrayness of the type
427 * is represented by the size rather than the type.
429 this->gl_type
= array
->gl_type
;
431 /* Allow a maximum of 10 characters for the array size. This is enough
432 * for 32-bits of ~0. The extra 3 are for the '[', ']', and terminating
435 const unsigned name_length
= strlen(array
->name
) + 10 + 3;
437 mtx_lock(&glsl_type::mutex
);
438 char *const n
= (char *) ralloc_size(this->mem_ctx
, name_length
);
439 mtx_unlock(&glsl_type::mutex
);
442 snprintf(n
, name_length
, "%s[]", array
->name
);
444 /* insert outermost dimensions in the correct spot
445 * otherwise the dimension order will be backwards
447 const char *pos
= strchr(array
->name
, '[');
449 int idx
= pos
- array
->name
;
450 snprintf(n
, idx
+1, "%s", array
->name
);
451 snprintf(n
+ idx
, name_length
- idx
, "[%u]%s",
452 length
, array
->name
+ idx
);
454 snprintf(n
, name_length
, "%s[%u]", array
->name
, length
);
463 glsl_type::vec(unsigned components
)
465 if (components
== 0 || components
> 4)
468 static const glsl_type
*const ts
[] = {
469 float_type
, vec2_type
, vec3_type
, vec4_type
471 return ts
[components
- 1];
475 glsl_type::dvec(unsigned components
)
477 if (components
== 0 || components
> 4)
480 static const glsl_type
*const ts
[] = {
481 double_type
, dvec2_type
, dvec3_type
, dvec4_type
483 return ts
[components
- 1];
487 glsl_type::ivec(unsigned components
)
489 if (components
== 0 || components
> 4)
492 static const glsl_type
*const ts
[] = {
493 int_type
, ivec2_type
, ivec3_type
, ivec4_type
495 return ts
[components
- 1];
500 glsl_type::uvec(unsigned components
)
502 if (components
== 0 || components
> 4)
505 static const glsl_type
*const ts
[] = {
506 uint_type
, uvec2_type
, uvec3_type
, uvec4_type
508 return ts
[components
- 1];
513 glsl_type::bvec(unsigned components
)
515 if (components
== 0 || components
> 4)
518 static const glsl_type
*const ts
[] = {
519 bool_type
, bvec2_type
, bvec3_type
, bvec4_type
521 return ts
[components
- 1];
526 glsl_type::get_instance(unsigned base_type
, unsigned rows
, unsigned columns
)
528 if (base_type
== GLSL_TYPE_VOID
)
531 if ((rows
< 1) || (rows
> 4) || (columns
< 1) || (columns
> 4))
534 /* Treat GLSL vectors as Nx1 matrices.
542 case GLSL_TYPE_FLOAT
:
544 case GLSL_TYPE_DOUBLE
:
552 if ((base_type
!= GLSL_TYPE_FLOAT
&& base_type
!= GLSL_TYPE_DOUBLE
) || (rows
== 1))
555 /* GLSL matrix types are named mat{COLUMNS}x{ROWS}. Only the following
556 * combinations are valid:
564 #define IDX(c,r) (((c-1)*3) + (r-1))
566 if (base_type
== GLSL_TYPE_DOUBLE
) {
567 switch (IDX(columns
, rows
)) {
568 case IDX(2,2): return dmat2_type
;
569 case IDX(2,3): return dmat2x3_type
;
570 case IDX(2,4): return dmat2x4_type
;
571 case IDX(3,2): return dmat3x2_type
;
572 case IDX(3,3): return dmat3_type
;
573 case IDX(3,4): return dmat3x4_type
;
574 case IDX(4,2): return dmat4x2_type
;
575 case IDX(4,3): return dmat4x3_type
;
576 case IDX(4,4): return dmat4_type
;
577 default: return error_type
;
580 switch (IDX(columns
, rows
)) {
581 case IDX(2,2): return mat2_type
;
582 case IDX(2,3): return mat2x3_type
;
583 case IDX(2,4): return mat2x4_type
;
584 case IDX(3,2): return mat3x2_type
;
585 case IDX(3,3): return mat3_type
;
586 case IDX(3,4): return mat3x4_type
;
587 case IDX(4,2): return mat4x2_type
;
588 case IDX(4,3): return mat4x3_type
;
589 case IDX(4,4): return mat4_type
;
590 default: return error_type
;
595 assert(!"Should not get here.");
600 glsl_type::get_sampler_instance(enum glsl_sampler_dim dim
,
606 case GLSL_TYPE_FLOAT
:
608 case GLSL_SAMPLER_DIM_1D
:
610 return (array
? sampler1DArrayShadow_type
: sampler1DShadow_type
);
612 return (array
? sampler1DArray_type
: sampler1D_type
);
613 case GLSL_SAMPLER_DIM_2D
:
615 return (array
? sampler2DArrayShadow_type
: sampler2DShadow_type
);
617 return (array
? sampler2DArray_type
: sampler2D_type
);
618 case GLSL_SAMPLER_DIM_3D
:
622 return sampler3D_type
;
623 case GLSL_SAMPLER_DIM_CUBE
:
625 return (array
? samplerCubeArrayShadow_type
: samplerCubeShadow_type
);
627 return (array
? samplerCubeArray_type
: samplerCube_type
);
628 case GLSL_SAMPLER_DIM_RECT
:
632 return sampler2DRectShadow_type
;
634 return sampler2DRect_type
;
635 case GLSL_SAMPLER_DIM_BUF
:
639 return samplerBuffer_type
;
640 case GLSL_SAMPLER_DIM_MS
:
643 return (array
? sampler2DMSArray_type
: sampler2DMS_type
);
644 case GLSL_SAMPLER_DIM_EXTERNAL
:
648 return samplerExternalOES_type
;
654 case GLSL_SAMPLER_DIM_1D
:
655 return (array
? isampler1DArray_type
: isampler1D_type
);
656 case GLSL_SAMPLER_DIM_2D
:
657 return (array
? isampler2DArray_type
: isampler2D_type
);
658 case GLSL_SAMPLER_DIM_3D
:
661 return isampler3D_type
;
662 case GLSL_SAMPLER_DIM_CUBE
:
663 return (array
? isamplerCubeArray_type
: isamplerCube_type
);
664 case GLSL_SAMPLER_DIM_RECT
:
667 return isampler2DRect_type
;
668 case GLSL_SAMPLER_DIM_BUF
:
671 return isamplerBuffer_type
;
672 case GLSL_SAMPLER_DIM_MS
:
673 return (array
? isampler2DMSArray_type
: isampler2DMS_type
);
674 case GLSL_SAMPLER_DIM_EXTERNAL
:
681 case GLSL_SAMPLER_DIM_1D
:
682 return (array
? usampler1DArray_type
: usampler1D_type
);
683 case GLSL_SAMPLER_DIM_2D
:
684 return (array
? usampler2DArray_type
: usampler2D_type
);
685 case GLSL_SAMPLER_DIM_3D
:
688 return usampler3D_type
;
689 case GLSL_SAMPLER_DIM_CUBE
:
690 return (array
? usamplerCubeArray_type
: usamplerCube_type
);
691 case GLSL_SAMPLER_DIM_RECT
:
694 return usampler2DRect_type
;
695 case GLSL_SAMPLER_DIM_BUF
:
698 return usamplerBuffer_type
;
699 case GLSL_SAMPLER_DIM_MS
:
700 return (array
? usampler2DMSArray_type
: usampler2DMS_type
);
701 case GLSL_SAMPLER_DIM_EXTERNAL
:
708 unreachable("switch statement above should be complete");
712 glsl_type::get_array_instance(const glsl_type
*base
, unsigned array_size
)
714 /* Generate a name using the base type pointer in the key. This is
715 * done because the name of the base type may not be unique across
716 * shaders. For example, two shaders may have different record types
720 snprintf(key
, sizeof(key
), "%p[%u]", (void *) base
, array_size
);
722 mtx_lock(&glsl_type::mutex
);
724 if (array_types
== NULL
) {
725 array_types
= _mesa_hash_table_create(NULL
, _mesa_key_hash_string
,
726 _mesa_key_string_equal
);
729 const struct hash_entry
*entry
= _mesa_hash_table_search(array_types
, key
);
731 mtx_unlock(&glsl_type::mutex
);
732 const glsl_type
*t
= new glsl_type(base
, array_size
);
733 mtx_lock(&glsl_type::mutex
);
735 entry
= _mesa_hash_table_insert(array_types
,
736 ralloc_strdup(mem_ctx
, key
),
740 assert(((glsl_type
*) entry
->data
)->base_type
== GLSL_TYPE_ARRAY
);
741 assert(((glsl_type
*) entry
->data
)->length
== array_size
);
742 assert(((glsl_type
*) entry
->data
)->fields
.array
== base
);
744 mtx_unlock(&glsl_type::mutex
);
746 return (glsl_type
*) entry
->data
;
751 glsl_type::record_compare(const glsl_type
*b
) const
753 if (this->length
!= b
->length
)
756 if (this->interface_packing
!= b
->interface_packing
)
759 /* From the GLSL 4.20 specification (Sec 4.2):
761 * "Structures must have the same name, sequence of type names, and
762 * type definitions, and field names to be considered the same type."
764 * GLSL ES behaves the same (Ver 1.00 Sec 4.2.4, Ver 3.00 Sec 4.2.5).
766 * Note that we cannot force type name check when comparing unnamed
767 * structure types, these have a unique name assigned during parsing.
769 if (!this->is_anonymous() && !b
->is_anonymous())
770 if (strcmp(this->name
, b
->name
) != 0)
773 for (unsigned i
= 0; i
< this->length
; i
++) {
774 if (this->fields
.structure
[i
].type
!= b
->fields
.structure
[i
].type
)
776 if (strcmp(this->fields
.structure
[i
].name
,
777 b
->fields
.structure
[i
].name
) != 0)
779 if (this->fields
.structure
[i
].matrix_layout
780 != b
->fields
.structure
[i
].matrix_layout
)
782 if (this->fields
.structure
[i
].location
783 != b
->fields
.structure
[i
].location
)
785 if (this->fields
.structure
[i
].interpolation
786 != b
->fields
.structure
[i
].interpolation
)
788 if (this->fields
.structure
[i
].centroid
789 != b
->fields
.structure
[i
].centroid
)
791 if (this->fields
.structure
[i
].sample
792 != b
->fields
.structure
[i
].sample
)
794 if (this->fields
.structure
[i
].patch
795 != b
->fields
.structure
[i
].patch
)
804 glsl_type::record_key_compare(const void *a
, const void *b
)
806 const glsl_type
*const key1
= (glsl_type
*) a
;
807 const glsl_type
*const key2
= (glsl_type
*) b
;
809 return strcmp(key1
->name
, key2
->name
) == 0 && key1
->record_compare(key2
);
814 * Generate an integer hash value for a glsl_type structure type.
817 glsl_type::record_key_hash(const void *a
)
819 const glsl_type
*const key
= (glsl_type
*) a
;
820 uintptr_t hash
= key
->length
;
823 for (unsigned i
= 0; i
< key
->length
; i
++) {
824 /* casting pointer to uintptr_t */
825 hash
= (hash
* 13 ) + (uintptr_t) key
->fields
.structure
[i
].type
;
828 if (sizeof(hash
) == 8)
829 retval
= (hash
& 0xffffffff) ^ ((uint64_t) hash
>> 32);
838 glsl_type::get_record_instance(const glsl_struct_field
*fields
,
842 const glsl_type
key(fields
, num_fields
, name
);
844 mtx_lock(&glsl_type::mutex
);
846 if (record_types
== NULL
) {
847 record_types
= _mesa_hash_table_create(NULL
, record_key_hash
,
851 const struct hash_entry
*entry
= _mesa_hash_table_search(record_types
,
854 mtx_unlock(&glsl_type::mutex
);
855 const glsl_type
*t
= new glsl_type(fields
, num_fields
, name
);
856 mtx_lock(&glsl_type::mutex
);
858 entry
= _mesa_hash_table_insert(record_types
, t
, (void *) t
);
861 assert(((glsl_type
*) entry
->data
)->base_type
== GLSL_TYPE_STRUCT
);
862 assert(((glsl_type
*) entry
->data
)->length
== num_fields
);
863 assert(strcmp(((glsl_type
*) entry
->data
)->name
, name
) == 0);
865 mtx_unlock(&glsl_type::mutex
);
867 return (glsl_type
*) entry
->data
;
872 glsl_type::get_interface_instance(const glsl_struct_field
*fields
,
874 enum glsl_interface_packing packing
,
875 const char *block_name
)
877 const glsl_type
key(fields
, num_fields
, packing
, block_name
);
879 mtx_lock(&glsl_type::mutex
);
881 if (interface_types
== NULL
) {
882 interface_types
= _mesa_hash_table_create(NULL
, record_key_hash
,
886 const struct hash_entry
*entry
= _mesa_hash_table_search(interface_types
,
889 mtx_unlock(&glsl_type::mutex
);
890 const glsl_type
*t
= new glsl_type(fields
, num_fields
,
891 packing
, block_name
);
892 mtx_lock(&glsl_type::mutex
);
894 entry
= _mesa_hash_table_insert(interface_types
, t
, (void *) t
);
897 assert(((glsl_type
*) entry
->data
)->base_type
== GLSL_TYPE_INTERFACE
);
898 assert(((glsl_type
*) entry
->data
)->length
== num_fields
);
899 assert(strcmp(((glsl_type
*) entry
->data
)->name
, block_name
) == 0);
901 mtx_unlock(&glsl_type::mutex
);
903 return (glsl_type
*) entry
->data
;
907 glsl_type::get_subroutine_instance(const char *subroutine_name
)
909 const glsl_type
key(subroutine_name
);
911 mtx_lock(&glsl_type::mutex
);
913 if (subroutine_types
== NULL
) {
914 subroutine_types
= _mesa_hash_table_create(NULL
, record_key_hash
,
918 const struct hash_entry
*entry
= _mesa_hash_table_search(subroutine_types
,
921 mtx_unlock(&glsl_type::mutex
);
922 const glsl_type
*t
= new glsl_type(subroutine_name
);
923 mtx_lock(&glsl_type::mutex
);
925 entry
= _mesa_hash_table_insert(subroutine_types
, t
, (void *) t
);
928 assert(((glsl_type
*) entry
->data
)->base_type
== GLSL_TYPE_SUBROUTINE
);
929 assert(strcmp(((glsl_type
*) entry
->data
)->name
, subroutine_name
) == 0);
931 mtx_unlock(&glsl_type::mutex
);
933 return (glsl_type
*) entry
->data
;
938 function_key_compare(const void *a
, const void *b
)
940 const glsl_type
*const key1
= (glsl_type
*) a
;
941 const glsl_type
*const key2
= (glsl_type
*) b
;
943 if (key1
->length
!= key2
->length
)
946 return memcmp(key1
->fields
.parameters
, key2
->fields
.parameters
,
947 (key1
->length
+ 1) * sizeof(*key1
->fields
.parameters
));
952 function_key_hash(const void *a
)
954 const glsl_type
*const key
= (glsl_type
*) a
;
958 size
= snprintf(hash_key
, sizeof(hash_key
), "%08x", key
->length
);
960 for (unsigned i
= 0; i
< key
->length
; i
++) {
961 if (size
>= sizeof(hash_key
))
964 size
+= snprintf(& hash_key
[size
], sizeof(hash_key
) - size
,
965 "%p", (void *) key
->fields
.structure
[i
].type
);
968 return _mesa_hash_string(hash_key
);
972 glsl_type::get_function_instance(const glsl_type
*return_type
,
973 const glsl_function_param
*params
,
976 const glsl_type
key(return_type
, params
, num_params
);
978 mtx_lock(&glsl_type::mutex
);
980 if (function_types
== NULL
) {
981 function_types
= _mesa_hash_table_create(NULL
, function_key_hash
,
982 function_key_compare
);
985 struct hash_entry
*entry
= _mesa_hash_table_search(function_types
, &key
);
987 mtx_unlock(&glsl_type::mutex
);
988 const glsl_type
*t
= new glsl_type(return_type
, params
, num_params
);
989 mtx_lock(&glsl_type::mutex
);
991 _mesa_hash_table_insert(function_types
, t
, (void *) t
);
994 const glsl_type
*t
= (const glsl_type
*)entry
->data
;
996 assert(t
->base_type
== GLSL_TYPE_FUNCTION
);
997 assert(t
->length
== num_params
);
999 mtx_unlock(&glsl_type::mutex
);
1006 glsl_type::get_mul_type(const glsl_type
*type_a
, const glsl_type
*type_b
)
1008 if (type_a
== type_b
) {
1010 } else if (type_a
->is_matrix() && type_b
->is_matrix()) {
1011 /* Matrix multiply. The columns of A must match the rows of B. Given
1012 * the other previously tested constraints, this means the vector type
1013 * of a row from A must be the same as the vector type of a column from
1016 if (type_a
->row_type() == type_b
->column_type()) {
1017 /* The resulting matrix has the number of columns of matrix B and
1018 * the number of rows of matrix A. We get the row count of A by
1019 * looking at the size of a vector that makes up a column. The
1020 * transpose (size of a row) is done for B.
1022 const glsl_type
*const type
=
1023 get_instance(type_a
->base_type
,
1024 type_a
->column_type()->vector_elements
,
1025 type_b
->row_type()->vector_elements
);
1026 assert(type
!= error_type
);
1030 } else if (type_a
->is_matrix()) {
1031 /* A is a matrix and B is a column vector. Columns of A must match
1032 * rows of B. Given the other previously tested constraints, this
1033 * means the vector type of a row from A must be the same as the
1034 * vector the type of B.
1036 if (type_a
->row_type() == type_b
) {
1037 /* The resulting vector has a number of elements equal to
1038 * the number of rows of matrix A. */
1039 const glsl_type
*const type
=
1040 get_instance(type_a
->base_type
,
1041 type_a
->column_type()->vector_elements
,
1043 assert(type
!= error_type
);
1048 assert(type_b
->is_matrix());
1050 /* A is a row vector and B is a matrix. Columns of A must match rows
1051 * of B. Given the other previously tested constraints, this means
1052 * the type of A must be the same as the vector type of a column from
1055 if (type_a
== type_b
->column_type()) {
1056 /* The resulting vector has a number of elements equal to
1057 * the number of columns of matrix B. */
1058 const glsl_type
*const type
=
1059 get_instance(type_a
->base_type
,
1060 type_b
->row_type()->vector_elements
,
1062 assert(type
!= error_type
);
1073 glsl_type::field_type(const char *name
) const
1075 if (this->base_type
!= GLSL_TYPE_STRUCT
1076 && this->base_type
!= GLSL_TYPE_INTERFACE
)
1079 for (unsigned i
= 0; i
< this->length
; i
++) {
1080 if (strcmp(name
, this->fields
.structure
[i
].name
) == 0)
1081 return this->fields
.structure
[i
].type
;
1089 glsl_type::field_index(const char *name
) const
1091 if (this->base_type
!= GLSL_TYPE_STRUCT
1092 && this->base_type
!= GLSL_TYPE_INTERFACE
)
1095 for (unsigned i
= 0; i
< this->length
; i
++) {
1096 if (strcmp(name
, this->fields
.structure
[i
].name
) == 0)
1105 glsl_type::component_slots() const
1107 switch (this->base_type
) {
1108 case GLSL_TYPE_UINT
:
1110 case GLSL_TYPE_FLOAT
:
1111 case GLSL_TYPE_BOOL
:
1112 return this->components();
1114 case GLSL_TYPE_DOUBLE
:
1115 return 2 * this->components();
1117 case GLSL_TYPE_STRUCT
:
1118 case GLSL_TYPE_INTERFACE
: {
1121 for (unsigned i
= 0; i
< this->length
; i
++)
1122 size
+= this->fields
.structure
[i
].type
->component_slots();
1127 case GLSL_TYPE_ARRAY
:
1128 return this->length
* this->fields
.array
->component_slots();
1130 case GLSL_TYPE_IMAGE
:
1132 case GLSL_TYPE_SUBROUTINE
:
1134 case GLSL_TYPE_FUNCTION
:
1135 case GLSL_TYPE_SAMPLER
:
1136 case GLSL_TYPE_ATOMIC_UINT
:
1137 case GLSL_TYPE_VOID
:
1138 case GLSL_TYPE_ERROR
:
1146 glsl_type::uniform_locations() const
1150 switch (this->base_type
) {
1151 case GLSL_TYPE_UINT
:
1153 case GLSL_TYPE_FLOAT
:
1154 case GLSL_TYPE_DOUBLE
:
1155 case GLSL_TYPE_BOOL
:
1156 case GLSL_TYPE_SAMPLER
:
1157 case GLSL_TYPE_IMAGE
:
1158 case GLSL_TYPE_SUBROUTINE
:
1161 case GLSL_TYPE_STRUCT
:
1162 case GLSL_TYPE_INTERFACE
:
1163 for (unsigned i
= 0; i
< this->length
; i
++)
1164 size
+= this->fields
.structure
[i
].type
->uniform_locations();
1166 case GLSL_TYPE_ARRAY
:
1167 return this->length
* this->fields
.array
->uniform_locations();
1174 glsl_type::can_implicitly_convert_to(const glsl_type
*desired
,
1175 _mesa_glsl_parse_state
*state
) const
1177 if (this == desired
)
1180 /* There is no conversion among matrix types. */
1181 if (this->matrix_columns
> 1 || desired
->matrix_columns
> 1)
1184 /* Vector size must match. */
1185 if (this->vector_elements
!= desired
->vector_elements
)
1188 /* int and uint can be converted to float. */
1189 if (desired
->is_float() && this->is_integer())
1192 /* With GLSL 4.0 / ARB_gpu_shader5, int can be converted to uint.
1193 * Note that state may be NULL here, when resolving function calls in the
1194 * linker. By this time, all the state-dependent checks have already
1195 * happened though, so allow anything that's allowed in any shader version. */
1196 if ((!state
|| state
->is_version(400, 0) || state
->ARB_gpu_shader5_enable
) &&
1197 desired
->base_type
== GLSL_TYPE_UINT
&& this->base_type
== GLSL_TYPE_INT
)
1200 /* No implicit conversions from double. */
1201 if ((!state
|| state
->has_double()) && this->is_double())
1204 /* Conversions from different types to double. */
1205 if ((!state
|| state
->has_double()) && desired
->is_double()) {
1206 if (this->is_float())
1208 if (this->is_integer())
1216 glsl_type::std140_base_alignment(bool row_major
) const
1218 unsigned N
= is_double() ? 8 : 4;
1220 /* (1) If the member is a scalar consuming <N> basic machine units, the
1221 * base alignment is <N>.
1223 * (2) If the member is a two- or four-component vector with components
1224 * consuming <N> basic machine units, the base alignment is 2<N> or
1225 * 4<N>, respectively.
1227 * (3) If the member is a three-component vector with components consuming
1228 * <N> basic machine units, the base alignment is 4<N>.
1230 if (this->is_scalar() || this->is_vector()) {
1231 switch (this->vector_elements
) {
1242 /* (4) If the member is an array of scalars or vectors, the base alignment
1243 * and array stride are set to match the base alignment of a single
1244 * array element, according to rules (1), (2), and (3), and rounded up
1245 * to the base alignment of a vec4. The array may have padding at the
1246 * end; the base offset of the member following the array is rounded up
1247 * to the next multiple of the base alignment.
1249 * (6) If the member is an array of <S> column-major matrices with <C>
1250 * columns and <R> rows, the matrix is stored identically to a row of
1251 * <S>*<C> column vectors with <R> components each, according to rule
1254 * (8) If the member is an array of <S> row-major matrices with <C> columns
1255 * and <R> rows, the matrix is stored identically to a row of <S>*<R>
1256 * row vectors with <C> components each, according to rule (4).
1258 * (10) If the member is an array of <S> structures, the <S> elements of
1259 * the array are laid out in order, according to rule (9).
1261 if (this->is_array()) {
1262 if (this->fields
.array
->is_scalar() ||
1263 this->fields
.array
->is_vector() ||
1264 this->fields
.array
->is_matrix()) {
1265 return MAX2(this->fields
.array
->std140_base_alignment(row_major
), 16);
1267 assert(this->fields
.array
->is_record() ||
1268 this->fields
.array
->is_array());
1269 return this->fields
.array
->std140_base_alignment(row_major
);
1273 /* (5) If the member is a column-major matrix with <C> columns and
1274 * <R> rows, the matrix is stored identically to an array of
1275 * <C> column vectors with <R> components each, according to
1278 * (7) If the member is a row-major matrix with <C> columns and <R>
1279 * rows, the matrix is stored identically to an array of <R>
1280 * row vectors with <C> components each, according to rule (4).
1282 if (this->is_matrix()) {
1283 const struct glsl_type
*vec_type
, *array_type
;
1284 int c
= this->matrix_columns
;
1285 int r
= this->vector_elements
;
1288 vec_type
= get_instance(base_type
, c
, 1);
1289 array_type
= glsl_type::get_array_instance(vec_type
, r
);
1291 vec_type
= get_instance(base_type
, r
, 1);
1292 array_type
= glsl_type::get_array_instance(vec_type
, c
);
1295 return array_type
->std140_base_alignment(false);
1298 /* (9) If the member is a structure, the base alignment of the
1299 * structure is <N>, where <N> is the largest base alignment
1300 * value of any of its members, and rounded up to the base
1301 * alignment of a vec4. The individual members of this
1302 * sub-structure are then assigned offsets by applying this set
1303 * of rules recursively, where the base offset of the first
1304 * member of the sub-structure is equal to the aligned offset
1305 * of the structure. The structure may have padding at the end;
1306 * the base offset of the member following the sub-structure is
1307 * rounded up to the next multiple of the base alignment of the
1310 if (this->is_record()) {
1311 unsigned base_alignment
= 16;
1312 for (unsigned i
= 0; i
< this->length
; i
++) {
1313 bool field_row_major
= row_major
;
1314 const enum glsl_matrix_layout matrix_layout
=
1315 glsl_matrix_layout(this->fields
.structure
[i
].matrix_layout
);
1316 if (matrix_layout
== GLSL_MATRIX_LAYOUT_ROW_MAJOR
) {
1317 field_row_major
= true;
1318 } else if (matrix_layout
== GLSL_MATRIX_LAYOUT_COLUMN_MAJOR
) {
1319 field_row_major
= false;
1322 const struct glsl_type
*field_type
= this->fields
.structure
[i
].type
;
1323 base_alignment
= MAX2(base_alignment
,
1324 field_type
->std140_base_alignment(field_row_major
));
1326 return base_alignment
;
1329 assert(!"not reached");
1334 glsl_type::std140_size(bool row_major
) const
1336 unsigned N
= is_double() ? 8 : 4;
1338 /* (1) If the member is a scalar consuming <N> basic machine units, the
1339 * base alignment is <N>.
1341 * (2) If the member is a two- or four-component vector with components
1342 * consuming <N> basic machine units, the base alignment is 2<N> or
1343 * 4<N>, respectively.
1345 * (3) If the member is a three-component vector with components consuming
1346 * <N> basic machine units, the base alignment is 4<N>.
1348 if (this->is_scalar() || this->is_vector()) {
1349 return this->vector_elements
* N
;
1352 /* (5) If the member is a column-major matrix with <C> columns and
1353 * <R> rows, the matrix is stored identically to an array of
1354 * <C> column vectors with <R> components each, according to
1357 * (6) If the member is an array of <S> column-major matrices with <C>
1358 * columns and <R> rows, the matrix is stored identically to a row of
1359 * <S>*<C> column vectors with <R> components each, according to rule
1362 * (7) If the member is a row-major matrix with <C> columns and <R>
1363 * rows, the matrix is stored identically to an array of <R>
1364 * row vectors with <C> components each, according to rule (4).
1366 * (8) If the member is an array of <S> row-major matrices with <C> columns
1367 * and <R> rows, the matrix is stored identically to a row of <S>*<R>
1368 * row vectors with <C> components each, according to rule (4).
1370 if (this->without_array()->is_matrix()) {
1371 const struct glsl_type
*element_type
;
1372 const struct glsl_type
*vec_type
;
1373 unsigned int array_len
;
1375 if (this->is_array()) {
1376 element_type
= this->fields
.array
;
1377 array_len
= this->length
;
1379 element_type
= this;
1384 vec_type
= get_instance(element_type
->base_type
,
1385 element_type
->matrix_columns
, 1);
1387 array_len
*= element_type
->vector_elements
;
1389 vec_type
= get_instance(element_type
->base_type
,
1390 element_type
->vector_elements
, 1);
1391 array_len
*= element_type
->matrix_columns
;
1393 const glsl_type
*array_type
= glsl_type::get_array_instance(vec_type
,
1396 return array_type
->std140_size(false);
1399 /* (4) If the member is an array of scalars or vectors, the base alignment
1400 * and array stride are set to match the base alignment of a single
1401 * array element, according to rules (1), (2), and (3), and rounded up
1402 * to the base alignment of a vec4. The array may have padding at the
1403 * end; the base offset of the member following the array is rounded up
1404 * to the next multiple of the base alignment.
1406 * (10) If the member is an array of <S> structures, the <S> elements of
1407 * the array are laid out in order, according to rule (9).
1409 if (this->is_array()) {
1410 if (this->fields
.array
->is_record()) {
1411 return this->length
* this->fields
.array
->std140_size(row_major
);
1413 unsigned element_base_align
=
1414 this->fields
.array
->std140_base_alignment(row_major
);
1415 return this->length
* MAX2(element_base_align
, 16);
1419 /* (9) If the member is a structure, the base alignment of the
1420 * structure is <N>, where <N> is the largest base alignment
1421 * value of any of its members, and rounded up to the base
1422 * alignment of a vec4. The individual members of this
1423 * sub-structure are then assigned offsets by applying this set
1424 * of rules recursively, where the base offset of the first
1425 * member of the sub-structure is equal to the aligned offset
1426 * of the structure. The structure may have padding at the end;
1427 * the base offset of the member following the sub-structure is
1428 * rounded up to the next multiple of the base alignment of the
1431 if (this->is_record()) {
1433 unsigned max_align
= 0;
1435 for (unsigned i
= 0; i
< this->length
; i
++) {
1436 bool field_row_major
= row_major
;
1437 const enum glsl_matrix_layout matrix_layout
=
1438 glsl_matrix_layout(this->fields
.structure
[i
].matrix_layout
);
1439 if (matrix_layout
== GLSL_MATRIX_LAYOUT_ROW_MAJOR
) {
1440 field_row_major
= true;
1441 } else if (matrix_layout
== GLSL_MATRIX_LAYOUT_COLUMN_MAJOR
) {
1442 field_row_major
= false;
1445 const struct glsl_type
*field_type
= this->fields
.structure
[i
].type
;
1446 unsigned align
= field_type
->std140_base_alignment(field_row_major
);
1447 size
= glsl_align(size
, align
);
1448 size
+= field_type
->std140_size(field_row_major
);
1450 max_align
= MAX2(align
, max_align
);
1452 if (field_type
->is_record() && (i
+ 1 < this->length
))
1453 size
= glsl_align(size
, 16);
1455 size
= glsl_align(size
, MAX2(max_align
, 16));
1459 assert(!"not reached");
1465 glsl_type::count_attribute_slots() const
1467 /* From page 31 (page 37 of the PDF) of the GLSL 1.50 spec:
1469 * "A scalar input counts the same amount against this limit as a vec4,
1470 * so applications may want to consider packing groups of four
1471 * unrelated float inputs together into a vector to better utilize the
1472 * capabilities of the underlying hardware. A matrix input will use up
1473 * multiple locations. The number of locations used will equal the
1474 * number of columns in the matrix."
1476 * The spec does not explicitly say how arrays are counted. However, it
1477 * should be safe to assume the total number of slots consumed by an array
1478 * is the number of entries in the array multiplied by the number of slots
1479 * consumed by a single element of the array.
1481 * The spec says nothing about how structs are counted, because vertex
1482 * attributes are not allowed to be (or contain) structs. However, Mesa
1483 * allows varying structs, the number of varying slots taken up by a
1484 * varying struct is simply equal to the sum of the number of slots taken
1485 * up by each element.
1487 switch (this->base_type
) {
1488 case GLSL_TYPE_UINT
:
1490 case GLSL_TYPE_FLOAT
:
1491 case GLSL_TYPE_BOOL
:
1492 case GLSL_TYPE_DOUBLE
:
1493 return this->matrix_columns
;
1495 case GLSL_TYPE_STRUCT
:
1496 case GLSL_TYPE_INTERFACE
: {
1499 for (unsigned i
= 0; i
< this->length
; i
++)
1500 size
+= this->fields
.structure
[i
].type
->count_attribute_slots();
1505 case GLSL_TYPE_ARRAY
:
1506 return this->length
* this->fields
.array
->count_attribute_slots();
1508 case GLSL_TYPE_FUNCTION
:
1509 case GLSL_TYPE_SAMPLER
:
1510 case GLSL_TYPE_IMAGE
:
1511 case GLSL_TYPE_ATOMIC_UINT
:
1512 case GLSL_TYPE_VOID
:
1513 case GLSL_TYPE_SUBROUTINE
:
1514 case GLSL_TYPE_ERROR
:
1518 assert(!"Unexpected type in count_attribute_slots()");
1524 glsl_type::coordinate_components() const
1528 switch (sampler_dimensionality
) {
1529 case GLSL_SAMPLER_DIM_1D
:
1530 case GLSL_SAMPLER_DIM_BUF
:
1533 case GLSL_SAMPLER_DIM_2D
:
1534 case GLSL_SAMPLER_DIM_RECT
:
1535 case GLSL_SAMPLER_DIM_MS
:
1536 case GLSL_SAMPLER_DIM_EXTERNAL
:
1539 case GLSL_SAMPLER_DIM_3D
:
1540 case GLSL_SAMPLER_DIM_CUBE
:
1544 assert(!"Should not get here.");
1549 /* Array textures need an additional component for the array index, except
1550 * for cubemap array images that behave like a 2D array of interleaved
1553 if (sampler_array
&&
1554 !(base_type
== GLSL_TYPE_IMAGE
&&
1555 sampler_dimensionality
== GLSL_SAMPLER_DIM_CUBE
))