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 void *glsl_type::mem_ctx
= NULL
;
38 glsl_type::init_ralloc_type_ctx(void)
40 if (glsl_type::mem_ctx
== NULL
) {
41 glsl_type::mem_ctx
= ralloc_autofree_context();
42 assert(glsl_type::mem_ctx
!= NULL
);
46 glsl_type::glsl_type(GLenum gl_type
,
47 glsl_base_type base_type
, unsigned vector_elements
,
48 unsigned matrix_columns
, const char *name
) :
51 sampler_dimensionality(0), sampler_shadow(0), sampler_array(0),
52 sampler_type(0), interface_packing(0),
53 vector_elements(vector_elements
), matrix_columns(matrix_columns
),
56 mtx_lock(&glsl_type::mutex
);
58 init_ralloc_type_ctx();
60 this->name
= ralloc_strdup(this->mem_ctx
, name
);
62 mtx_unlock(&glsl_type::mutex
);
64 /* Neither dimension is zero or both dimensions are zero.
66 assert((vector_elements
== 0) == (matrix_columns
== 0));
67 memset(& fields
, 0, sizeof(fields
));
70 glsl_type::glsl_type(GLenum gl_type
, glsl_base_type base_type
,
71 enum glsl_sampler_dim dim
, bool shadow
, bool array
,
72 unsigned type
, const char *name
) :
75 sampler_dimensionality(dim
), sampler_shadow(shadow
),
76 sampler_array(array
), sampler_type(type
), interface_packing(0),
79 mtx_lock(&glsl_type::mutex
);
81 init_ralloc_type_ctx();
83 this->name
= ralloc_strdup(this->mem_ctx
, name
);
85 mtx_unlock(&glsl_type::mutex
);
87 memset(& fields
, 0, sizeof(fields
));
89 if (base_type
== GLSL_TYPE_SAMPLER
) {
90 /* Samplers take no storage whatsoever. */
91 matrix_columns
= vector_elements
= 0;
93 matrix_columns
= vector_elements
= 1;
97 glsl_type::glsl_type(const glsl_struct_field
*fields
, unsigned num_fields
,
100 base_type(GLSL_TYPE_STRUCT
),
101 sampler_dimensionality(0), sampler_shadow(0), sampler_array(0),
102 sampler_type(0), interface_packing(0),
103 vector_elements(0), matrix_columns(0),
108 mtx_lock(&glsl_type::mutex
);
110 init_ralloc_type_ctx();
111 assert(name
!= NULL
);
112 this->name
= ralloc_strdup(this->mem_ctx
, name
);
113 this->fields
.structure
= ralloc_array(this->mem_ctx
,
114 glsl_struct_field
, length
);
116 for (i
= 0; i
< length
; i
++) {
117 this->fields
.structure
[i
].type
= fields
[i
].type
;
118 this->fields
.structure
[i
].name
= ralloc_strdup(this->fields
.structure
,
120 this->fields
.structure
[i
].location
= fields
[i
].location
;
121 this->fields
.structure
[i
].interpolation
= fields
[i
].interpolation
;
122 this->fields
.structure
[i
].centroid
= fields
[i
].centroid
;
123 this->fields
.structure
[i
].sample
= fields
[i
].sample
;
124 this->fields
.structure
[i
].matrix_layout
= fields
[i
].matrix_layout
;
127 mtx_unlock(&glsl_type::mutex
);
130 glsl_type::glsl_type(const glsl_struct_field
*fields
, unsigned num_fields
,
131 enum glsl_interface_packing packing
, const char *name
) :
133 base_type(GLSL_TYPE_INTERFACE
),
134 sampler_dimensionality(0), sampler_shadow(0), sampler_array(0),
135 sampler_type(0), interface_packing((unsigned) packing
),
136 vector_elements(0), matrix_columns(0),
141 mtx_lock(&glsl_type::mutex
);
143 init_ralloc_type_ctx();
144 assert(name
!= NULL
);
145 this->name
= ralloc_strdup(this->mem_ctx
, name
);
146 this->fields
.structure
= ralloc_array(this->mem_ctx
,
147 glsl_struct_field
, length
);
148 for (i
= 0; i
< length
; i
++) {
149 this->fields
.structure
[i
].type
= fields
[i
].type
;
150 this->fields
.structure
[i
].name
= ralloc_strdup(this->fields
.structure
,
152 this->fields
.structure
[i
].location
= fields
[i
].location
;
153 this->fields
.structure
[i
].interpolation
= fields
[i
].interpolation
;
154 this->fields
.structure
[i
].centroid
= fields
[i
].centroid
;
155 this->fields
.structure
[i
].sample
= fields
[i
].sample
;
156 this->fields
.structure
[i
].matrix_layout
= fields
[i
].matrix_layout
;
159 mtx_unlock(&glsl_type::mutex
);
164 glsl_type::contains_sampler() const
166 if (this->is_array()) {
167 return this->fields
.array
->contains_sampler();
168 } else if (this->is_record()) {
169 for (unsigned int i
= 0; i
< this->length
; i
++) {
170 if (this->fields
.structure
[i
].type
->contains_sampler())
175 return this->is_sampler();
181 glsl_type::contains_integer() const
183 if (this->is_array()) {
184 return this->fields
.array
->contains_integer();
185 } else if (this->is_record()) {
186 for (unsigned int i
= 0; i
< this->length
; i
++) {
187 if (this->fields
.structure
[i
].type
->contains_integer())
192 return this->is_integer();
197 glsl_type::contains_double() const
199 if (this->is_array()) {
200 return this->fields
.array
->contains_double();
201 } else if (this->is_record()) {
202 for (unsigned int i
= 0; i
< this->length
; i
++) {
203 if (this->fields
.structure
[i
].type
->contains_double())
208 return this->is_double();
213 glsl_type::contains_opaque() const {
215 case GLSL_TYPE_SAMPLER
:
216 case GLSL_TYPE_IMAGE
:
217 case GLSL_TYPE_ATOMIC_UINT
:
219 case GLSL_TYPE_ARRAY
:
220 return element_type()->contains_opaque();
221 case GLSL_TYPE_STRUCT
:
222 for (unsigned int i
= 0; i
< length
; i
++) {
223 if (fields
.structure
[i
].type
->contains_opaque())
233 glsl_type::sampler_index() const
235 const glsl_type
*const t
= (this->is_array()) ? this->fields
.array
: this;
237 assert(t
->is_sampler());
239 switch (t
->sampler_dimensionality
) {
240 case GLSL_SAMPLER_DIM_1D
:
241 return (t
->sampler_array
) ? TEXTURE_1D_ARRAY_INDEX
: TEXTURE_1D_INDEX
;
242 case GLSL_SAMPLER_DIM_2D
:
243 return (t
->sampler_array
) ? TEXTURE_2D_ARRAY_INDEX
: TEXTURE_2D_INDEX
;
244 case GLSL_SAMPLER_DIM_3D
:
245 return TEXTURE_3D_INDEX
;
246 case GLSL_SAMPLER_DIM_CUBE
:
247 return (t
->sampler_array
) ? TEXTURE_CUBE_ARRAY_INDEX
: TEXTURE_CUBE_INDEX
;
248 case GLSL_SAMPLER_DIM_RECT
:
249 return TEXTURE_RECT_INDEX
;
250 case GLSL_SAMPLER_DIM_BUF
:
251 return TEXTURE_BUFFER_INDEX
;
252 case GLSL_SAMPLER_DIM_EXTERNAL
:
253 return TEXTURE_EXTERNAL_INDEX
;
254 case GLSL_SAMPLER_DIM_MS
:
255 return (t
->sampler_array
) ? TEXTURE_2D_MULTISAMPLE_ARRAY_INDEX
: TEXTURE_2D_MULTISAMPLE_INDEX
;
257 assert(!"Should not get here.");
258 return TEXTURE_BUFFER_INDEX
;
263 glsl_type::contains_image() const
265 if (this->is_array()) {
266 return this->fields
.array
->contains_image();
267 } else if (this->is_record()) {
268 for (unsigned int i
= 0; i
< this->length
; i
++) {
269 if (this->fields
.structure
[i
].type
->contains_image())
274 return this->is_image();
278 const glsl_type
*glsl_type::get_base_type() const
285 case GLSL_TYPE_FLOAT
:
287 case GLSL_TYPE_DOUBLE
:
297 const glsl_type
*glsl_type::get_scalar_type() const
299 const glsl_type
*type
= this;
302 while (type
->base_type
== GLSL_TYPE_ARRAY
)
303 type
= type
->fields
.array
;
305 /* Handle vectors and matrices */
306 switch (type
->base_type
) {
311 case GLSL_TYPE_FLOAT
:
313 case GLSL_TYPE_DOUBLE
:
318 /* Handle everything else */
325 _mesa_glsl_release_types(void)
327 mtx_lock(&glsl_type::mutex
);
329 if (glsl_type::array_types
!= NULL
) {
330 hash_table_dtor(glsl_type::array_types
);
331 glsl_type::array_types
= NULL
;
334 if (glsl_type::record_types
!= NULL
) {
335 hash_table_dtor(glsl_type::record_types
);
336 glsl_type::record_types
= NULL
;
339 mtx_unlock(&glsl_type::mutex
);
343 glsl_type::glsl_type(const glsl_type
*array
, unsigned length
) :
344 base_type(GLSL_TYPE_ARRAY
),
345 sampler_dimensionality(0), sampler_shadow(0), sampler_array(0),
346 sampler_type(0), interface_packing(0),
347 vector_elements(0), matrix_columns(0),
348 length(length
), name(NULL
)
350 this->fields
.array
= array
;
351 /* Inherit the gl type of the base. The GL type is used for
352 * uniform/statevar handling in Mesa and the arrayness of the type
353 * is represented by the size rather than the type.
355 this->gl_type
= array
->gl_type
;
357 /* Allow a maximum of 10 characters for the array size. This is enough
358 * for 32-bits of ~0. The extra 3 are for the '[', ']', and terminating
361 const unsigned name_length
= strlen(array
->name
) + 10 + 3;
363 mtx_lock(&glsl_type::mutex
);
364 char *const n
= (char *) ralloc_size(this->mem_ctx
, name_length
);
365 mtx_unlock(&glsl_type::mutex
);
368 snprintf(n
, name_length
, "%s[]", array
->name
);
370 /* insert outermost dimensions in the correct spot
371 * otherwise the dimension order will be backwards
373 const char *pos
= strchr(array
->name
, '[');
375 int idx
= pos
- array
->name
;
376 snprintf(n
, idx
+1, "%s", array
->name
);
377 snprintf(n
+ idx
, name_length
- idx
, "[%u]%s",
378 length
, array
->name
+ idx
);
380 snprintf(n
, name_length
, "%s[%u]", array
->name
, length
);
389 glsl_type::vec(unsigned components
)
391 if (components
== 0 || components
> 4)
394 static const glsl_type
*const ts
[] = {
395 float_type
, vec2_type
, vec3_type
, vec4_type
397 return ts
[components
- 1];
401 glsl_type::dvec(unsigned components
)
403 if (components
== 0 || components
> 4)
406 static const glsl_type
*const ts
[] = {
407 double_type
, dvec2_type
, dvec3_type
, dvec4_type
409 return ts
[components
- 1];
413 glsl_type::ivec(unsigned components
)
415 if (components
== 0 || components
> 4)
418 static const glsl_type
*const ts
[] = {
419 int_type
, ivec2_type
, ivec3_type
, ivec4_type
421 return ts
[components
- 1];
426 glsl_type::uvec(unsigned components
)
428 if (components
== 0 || components
> 4)
431 static const glsl_type
*const ts
[] = {
432 uint_type
, uvec2_type
, uvec3_type
, uvec4_type
434 return ts
[components
- 1];
439 glsl_type::bvec(unsigned components
)
441 if (components
== 0 || components
> 4)
444 static const glsl_type
*const ts
[] = {
445 bool_type
, bvec2_type
, bvec3_type
, bvec4_type
447 return ts
[components
- 1];
452 glsl_type::get_instance(unsigned base_type
, unsigned rows
, unsigned columns
)
454 if (base_type
== GLSL_TYPE_VOID
)
457 if ((rows
< 1) || (rows
> 4) || (columns
< 1) || (columns
> 4))
460 /* Treat GLSL vectors as Nx1 matrices.
468 case GLSL_TYPE_FLOAT
:
470 case GLSL_TYPE_DOUBLE
:
478 if ((base_type
!= GLSL_TYPE_FLOAT
&& base_type
!= GLSL_TYPE_DOUBLE
) || (rows
== 1))
481 /* GLSL matrix types are named mat{COLUMNS}x{ROWS}. Only the following
482 * combinations are valid:
490 #define IDX(c,r) (((c-1)*3) + (r-1))
492 if (base_type
== GLSL_TYPE_DOUBLE
) {
493 switch (IDX(columns
, rows
)) {
494 case IDX(2,2): return dmat2_type
;
495 case IDX(2,3): return dmat2x3_type
;
496 case IDX(2,4): return dmat2x4_type
;
497 case IDX(3,2): return dmat3x2_type
;
498 case IDX(3,3): return dmat3_type
;
499 case IDX(3,4): return dmat3x4_type
;
500 case IDX(4,2): return dmat4x2_type
;
501 case IDX(4,3): return dmat4x3_type
;
502 case IDX(4,4): return dmat4_type
;
503 default: return error_type
;
506 switch (IDX(columns
, rows
)) {
507 case IDX(2,2): return mat2_type
;
508 case IDX(2,3): return mat2x3_type
;
509 case IDX(2,4): return mat2x4_type
;
510 case IDX(3,2): return mat3x2_type
;
511 case IDX(3,3): return mat3_type
;
512 case IDX(3,4): return mat3x4_type
;
513 case IDX(4,2): return mat4x2_type
;
514 case IDX(4,3): return mat4x3_type
;
515 case IDX(4,4): return mat4_type
;
516 default: return error_type
;
521 assert(!"Should not get here.");
526 glsl_type::get_sampler_instance(enum glsl_sampler_dim dim
,
532 case GLSL_TYPE_FLOAT
:
534 case GLSL_SAMPLER_DIM_1D
:
536 return (array
? sampler1DArrayShadow_type
: sampler1DShadow_type
);
538 return (array
? sampler1DArray_type
: sampler1D_type
);
539 case GLSL_SAMPLER_DIM_2D
:
541 return (array
? sampler2DArrayShadow_type
: sampler2DShadow_type
);
543 return (array
? sampler2DArray_type
: sampler2D_type
);
544 case GLSL_SAMPLER_DIM_3D
:
548 return sampler3D_type
;
549 case GLSL_SAMPLER_DIM_CUBE
:
551 return (array
? samplerCubeArrayShadow_type
: samplerCubeShadow_type
);
553 return (array
? samplerCubeArray_type
: samplerCube_type
);
554 case GLSL_SAMPLER_DIM_RECT
:
558 return sampler2DRectShadow_type
;
560 return sampler2DRect_type
;
561 case GLSL_SAMPLER_DIM_BUF
:
565 return samplerBuffer_type
;
566 case GLSL_SAMPLER_DIM_MS
:
569 return (array
? sampler2DMSArray_type
: sampler2DMS_type
);
570 case GLSL_SAMPLER_DIM_EXTERNAL
:
574 return samplerExternalOES_type
;
580 case GLSL_SAMPLER_DIM_1D
:
581 return (array
? isampler1DArray_type
: isampler1D_type
);
582 case GLSL_SAMPLER_DIM_2D
:
583 return (array
? isampler2DArray_type
: isampler2D_type
);
584 case GLSL_SAMPLER_DIM_3D
:
587 return isampler3D_type
;
588 case GLSL_SAMPLER_DIM_CUBE
:
589 return (array
? isamplerCubeArray_type
: isamplerCube_type
);
590 case GLSL_SAMPLER_DIM_RECT
:
593 return isampler2DRect_type
;
594 case GLSL_SAMPLER_DIM_BUF
:
597 return isamplerBuffer_type
;
598 case GLSL_SAMPLER_DIM_MS
:
599 return (array
? isampler2DMSArray_type
: isampler2DMS_type
);
600 case GLSL_SAMPLER_DIM_EXTERNAL
:
607 case GLSL_SAMPLER_DIM_1D
:
608 return (array
? usampler1DArray_type
: usampler1D_type
);
609 case GLSL_SAMPLER_DIM_2D
:
610 return (array
? usampler2DArray_type
: usampler2D_type
);
611 case GLSL_SAMPLER_DIM_3D
:
614 return usampler3D_type
;
615 case GLSL_SAMPLER_DIM_CUBE
:
616 return (array
? usamplerCubeArray_type
: usamplerCube_type
);
617 case GLSL_SAMPLER_DIM_RECT
:
620 return usampler2DRect_type
;
621 case GLSL_SAMPLER_DIM_BUF
:
624 return usamplerBuffer_type
;
625 case GLSL_SAMPLER_DIM_MS
:
626 return (array
? usampler2DMSArray_type
: usampler2DMS_type
);
627 case GLSL_SAMPLER_DIM_EXTERNAL
:
634 unreachable("switch statement above should be complete");
638 glsl_type::get_array_instance(const glsl_type
*base
, unsigned array_size
)
640 /* Generate a name using the base type pointer in the key. This is
641 * done because the name of the base type may not be unique across
642 * shaders. For example, two shaders may have different record types
646 snprintf(key
, sizeof(key
), "%p[%u]", (void *) base
, array_size
);
648 mtx_lock(&glsl_type::mutex
);
650 if (array_types
== NULL
) {
651 array_types
= hash_table_ctor(64, hash_table_string_hash
,
652 hash_table_string_compare
);
655 const glsl_type
*t
= (glsl_type
*) hash_table_find(array_types
, key
);
658 mtx_unlock(&glsl_type::mutex
);
659 t
= new glsl_type(base
, array_size
);
660 mtx_lock(&glsl_type::mutex
);
662 hash_table_insert(array_types
, (void *) t
, ralloc_strdup(mem_ctx
, key
));
665 assert(t
->base_type
== GLSL_TYPE_ARRAY
);
666 assert(t
->length
== array_size
);
667 assert(t
->fields
.array
== base
);
669 mtx_unlock(&glsl_type::mutex
);
676 glsl_type::record_compare(const glsl_type
*b
) const
678 if (this->length
!= b
->length
)
681 if (this->interface_packing
!= b
->interface_packing
)
684 /* From the GLSL 4.20 specification (Sec 4.2):
686 * "Structures must have the same name, sequence of type names, and
687 * type definitions, and field names to be considered the same type."
689 * GLSL ES behaves the same (Ver 1.00 Sec 4.2.4, Ver 3.00 Sec 4.2.5).
691 * Note that we cannot force type name check when comparing unnamed
692 * structure types, these have a unique name assigned during parsing.
694 if (!this->is_anonymous() && !b
->is_anonymous())
695 if (strcmp(this->name
, b
->name
) != 0)
698 for (unsigned i
= 0; i
< this->length
; i
++) {
699 if (this->fields
.structure
[i
].type
!= b
->fields
.structure
[i
].type
)
701 if (strcmp(this->fields
.structure
[i
].name
,
702 b
->fields
.structure
[i
].name
) != 0)
704 if (this->fields
.structure
[i
].matrix_layout
705 != b
->fields
.structure
[i
].matrix_layout
)
707 if (this->fields
.structure
[i
].location
708 != b
->fields
.structure
[i
].location
)
710 if (this->fields
.structure
[i
].interpolation
711 != b
->fields
.structure
[i
].interpolation
)
713 if (this->fields
.structure
[i
].centroid
714 != b
->fields
.structure
[i
].centroid
)
716 if (this->fields
.structure
[i
].sample
717 != b
->fields
.structure
[i
].sample
)
726 glsl_type::record_key_compare(const void *a
, const void *b
)
728 const glsl_type
*const key1
= (glsl_type
*) a
;
729 const glsl_type
*const key2
= (glsl_type
*) b
;
731 /* Return zero is the types match (there is zero difference) or non-zero
734 if (strcmp(key1
->name
, key2
->name
) != 0)
737 return !key1
->record_compare(key2
);
742 * Generate an integer hash value for a glsl_type structure type.
745 glsl_type::record_key_hash(const void *a
)
747 const glsl_type
*const key
= (glsl_type
*) a
;
748 uintptr_t hash
= key
->length
;
751 for (unsigned i
= 0; i
< key
->length
; i
++) {
752 /* casting pointer to uintptr_t */
753 hash
= (hash
* 13 ) + (uintptr_t) key
->fields
.structure
[i
].type
;
756 if (sizeof(hash
) == 8)
757 retval
= (hash
& 0xffffffff) ^ ((uint64_t) hash
>> 32);
766 glsl_type::get_record_instance(const glsl_struct_field
*fields
,
770 const glsl_type
key(fields
, num_fields
, name
);
772 mtx_lock(&glsl_type::mutex
);
774 if (record_types
== NULL
) {
775 record_types
= hash_table_ctor(64, record_key_hash
, record_key_compare
);
778 const glsl_type
*t
= (glsl_type
*) hash_table_find(record_types
, & key
);
780 mtx_unlock(&glsl_type::mutex
);
781 t
= new glsl_type(fields
, num_fields
, name
);
782 mtx_lock(&glsl_type::mutex
);
784 hash_table_insert(record_types
, (void *) t
, t
);
787 assert(t
->base_type
== GLSL_TYPE_STRUCT
);
788 assert(t
->length
== num_fields
);
789 assert(strcmp(t
->name
, name
) == 0);
791 mtx_unlock(&glsl_type::mutex
);
798 glsl_type::get_interface_instance(const glsl_struct_field
*fields
,
800 enum glsl_interface_packing packing
,
801 const char *block_name
)
803 const glsl_type
key(fields
, num_fields
, packing
, block_name
);
805 mtx_lock(&glsl_type::mutex
);
807 if (interface_types
== NULL
) {
808 interface_types
= hash_table_ctor(64, record_key_hash
, record_key_compare
);
811 const glsl_type
*t
= (glsl_type
*) hash_table_find(interface_types
, & key
);
813 mtx_unlock(&glsl_type::mutex
);
814 t
= new glsl_type(fields
, num_fields
, packing
, block_name
);
815 mtx_lock(&glsl_type::mutex
);
817 hash_table_insert(interface_types
, (void *) t
, t
);
820 assert(t
->base_type
== GLSL_TYPE_INTERFACE
);
821 assert(t
->length
== num_fields
);
822 assert(strcmp(t
->name
, block_name
) == 0);
824 mtx_unlock(&glsl_type::mutex
);
831 glsl_type::get_mul_type(const glsl_type
*type_a
, const glsl_type
*type_b
)
833 if (type_a
== type_b
) {
835 } else if (type_a
->is_matrix() && type_b
->is_matrix()) {
836 /* Matrix multiply. The columns of A must match the rows of B. Given
837 * the other previously tested constraints, this means the vector type
838 * of a row from A must be the same as the vector type of a column from
841 if (type_a
->row_type() == type_b
->column_type()) {
842 /* The resulting matrix has the number of columns of matrix B and
843 * the number of rows of matrix A. We get the row count of A by
844 * looking at the size of a vector that makes up a column. The
845 * transpose (size of a row) is done for B.
847 const glsl_type
*const type
=
848 get_instance(type_a
->base_type
,
849 type_a
->column_type()->vector_elements
,
850 type_b
->row_type()->vector_elements
);
851 assert(type
!= error_type
);
855 } else if (type_a
->is_matrix()) {
856 /* A is a matrix and B is a column vector. Columns of A must match
857 * rows of B. Given the other previously tested constraints, this
858 * means the vector type of a row from A must be the same as the
859 * vector the type of B.
861 if (type_a
->row_type() == type_b
) {
862 /* The resulting vector has a number of elements equal to
863 * the number of rows of matrix A. */
864 const glsl_type
*const type
=
865 get_instance(type_a
->base_type
,
866 type_a
->column_type()->vector_elements
,
868 assert(type
!= error_type
);
873 assert(type_b
->is_matrix());
875 /* A is a row vector and B is a matrix. Columns of A must match rows
876 * of B. Given the other previously tested constraints, this means
877 * the type of A must be the same as the vector type of a column from
880 if (type_a
== type_b
->column_type()) {
881 /* The resulting vector has a number of elements equal to
882 * the number of columns of matrix B. */
883 const glsl_type
*const type
=
884 get_instance(type_a
->base_type
,
885 type_b
->row_type()->vector_elements
,
887 assert(type
!= error_type
);
898 glsl_type::field_type(const char *name
) const
900 if (this->base_type
!= GLSL_TYPE_STRUCT
901 && this->base_type
!= GLSL_TYPE_INTERFACE
)
904 for (unsigned i
= 0; i
< this->length
; i
++) {
905 if (strcmp(name
, this->fields
.structure
[i
].name
) == 0)
906 return this->fields
.structure
[i
].type
;
914 glsl_type::field_index(const char *name
) const
916 if (this->base_type
!= GLSL_TYPE_STRUCT
917 && this->base_type
!= GLSL_TYPE_INTERFACE
)
920 for (unsigned i
= 0; i
< this->length
; i
++) {
921 if (strcmp(name
, this->fields
.structure
[i
].name
) == 0)
930 glsl_type::component_slots() const
932 switch (this->base_type
) {
935 case GLSL_TYPE_FLOAT
:
937 return this->components();
939 case GLSL_TYPE_DOUBLE
:
940 return 2 * this->components();
942 case GLSL_TYPE_STRUCT
:
943 case GLSL_TYPE_INTERFACE
: {
946 for (unsigned i
= 0; i
< this->length
; i
++)
947 size
+= this->fields
.structure
[i
].type
->component_slots();
952 case GLSL_TYPE_ARRAY
:
953 return this->length
* this->fields
.array
->component_slots();
955 case GLSL_TYPE_IMAGE
:
958 case GLSL_TYPE_SAMPLER
:
959 case GLSL_TYPE_ATOMIC_UINT
:
961 case GLSL_TYPE_ERROR
:
969 glsl_type::uniform_locations() const
973 switch (this->base_type
) {
976 case GLSL_TYPE_FLOAT
:
977 case GLSL_TYPE_DOUBLE
:
979 case GLSL_TYPE_SAMPLER
:
980 case GLSL_TYPE_IMAGE
:
983 case GLSL_TYPE_STRUCT
:
984 case GLSL_TYPE_INTERFACE
:
985 for (unsigned i
= 0; i
< this->length
; i
++)
986 size
+= this->fields
.structure
[i
].type
->uniform_locations();
988 case GLSL_TYPE_ARRAY
:
989 return this->length
* this->fields
.array
->uniform_locations();
996 glsl_type::can_implicitly_convert_to(const glsl_type
*desired
,
997 _mesa_glsl_parse_state
*state
) const
1002 /* There is no conversion among matrix types. */
1003 if (this->matrix_columns
> 1 || desired
->matrix_columns
> 1)
1006 /* Vector size must match. */
1007 if (this->vector_elements
!= desired
->vector_elements
)
1010 /* int and uint can be converted to float. */
1011 if (desired
->is_float() && this->is_integer())
1014 /* With GLSL 4.0 / ARB_gpu_shader5, int can be converted to uint.
1015 * Note that state may be NULL here, when resolving function calls in the
1016 * linker. By this time, all the state-dependent checks have already
1017 * happened though, so allow anything that's allowed in any shader version. */
1018 if ((!state
|| state
->is_version(400, 0) || state
->ARB_gpu_shader5_enable
) &&
1019 desired
->base_type
== GLSL_TYPE_UINT
&& this->base_type
== GLSL_TYPE_INT
)
1022 /* No implicit conversions from double. */
1023 if ((!state
|| state
->has_double()) && this->is_double())
1026 /* Conversions from different types to double. */
1027 if ((!state
|| state
->has_double()) && desired
->is_double()) {
1028 if (this->is_float())
1030 if (this->is_integer())
1038 glsl_type::std140_base_alignment(bool row_major
) const
1040 unsigned N
= is_double() ? 8 : 4;
1042 /* (1) If the member is a scalar consuming <N> basic machine units, the
1043 * base alignment is <N>.
1045 * (2) If the member is a two- or four-component vector with components
1046 * consuming <N> basic machine units, the base alignment is 2<N> or
1047 * 4<N>, respectively.
1049 * (3) If the member is a three-component vector with components consuming
1050 * <N> basic machine units, the base alignment is 4<N>.
1052 if (this->is_scalar() || this->is_vector()) {
1053 switch (this->vector_elements
) {
1064 /* (4) If the member is an array of scalars or vectors, the base alignment
1065 * and array stride are set to match the base alignment of a single
1066 * array element, according to rules (1), (2), and (3), and rounded up
1067 * to the base alignment of a vec4. The array may have padding at the
1068 * end; the base offset of the member following the array is rounded up
1069 * to the next multiple of the base alignment.
1071 * (6) If the member is an array of <S> column-major matrices with <C>
1072 * columns and <R> rows, the matrix is stored identically to a row of
1073 * <S>*<C> column vectors with <R> components each, according to rule
1076 * (8) If the member is an array of <S> row-major matrices with <C> columns
1077 * and <R> rows, the matrix is stored identically to a row of <S>*<R>
1078 * row vectors with <C> components each, according to rule (4).
1080 * (10) If the member is an array of <S> structures, the <S> elements of
1081 * the array are laid out in order, according to rule (9).
1083 if (this->is_array()) {
1084 if (this->fields
.array
->is_scalar() ||
1085 this->fields
.array
->is_vector() ||
1086 this->fields
.array
->is_matrix()) {
1087 return MAX2(this->fields
.array
->std140_base_alignment(row_major
), 16);
1089 assert(this->fields
.array
->is_record());
1090 return this->fields
.array
->std140_base_alignment(row_major
);
1094 /* (5) If the member is a column-major matrix with <C> columns and
1095 * <R> rows, the matrix is stored identically to an array of
1096 * <C> column vectors with <R> components each, according to
1099 * (7) If the member is a row-major matrix with <C> columns and <R>
1100 * rows, the matrix is stored identically to an array of <R>
1101 * row vectors with <C> components each, according to rule (4).
1103 if (this->is_matrix()) {
1104 const struct glsl_type
*vec_type
, *array_type
;
1105 int c
= this->matrix_columns
;
1106 int r
= this->vector_elements
;
1109 vec_type
= get_instance(base_type
, c
, 1);
1110 array_type
= glsl_type::get_array_instance(vec_type
, r
);
1112 vec_type
= get_instance(base_type
, r
, 1);
1113 array_type
= glsl_type::get_array_instance(vec_type
, c
);
1116 return array_type
->std140_base_alignment(false);
1119 /* (9) If the member is a structure, the base alignment of the
1120 * structure is <N>, where <N> is the largest base alignment
1121 * value of any of its members, and rounded up to the base
1122 * alignment of a vec4. The individual members of this
1123 * sub-structure are then assigned offsets by applying this set
1124 * of rules recursively, where the base offset of the first
1125 * member of the sub-structure is equal to the aligned offset
1126 * of the structure. The structure may have padding at the end;
1127 * the base offset of the member following the sub-structure is
1128 * rounded up to the next multiple of the base alignment of the
1131 if (this->is_record()) {
1132 unsigned base_alignment
= 16;
1133 for (unsigned i
= 0; i
< this->length
; i
++) {
1134 bool field_row_major
= row_major
;
1135 const enum glsl_matrix_layout matrix_layout
=
1136 glsl_matrix_layout(this->fields
.structure
[i
].matrix_layout
);
1137 if (matrix_layout
== GLSL_MATRIX_LAYOUT_ROW_MAJOR
) {
1138 field_row_major
= true;
1139 } else if (matrix_layout
== GLSL_MATRIX_LAYOUT_COLUMN_MAJOR
) {
1140 field_row_major
= false;
1143 const struct glsl_type
*field_type
= this->fields
.structure
[i
].type
;
1144 base_alignment
= MAX2(base_alignment
,
1145 field_type
->std140_base_alignment(field_row_major
));
1147 return base_alignment
;
1150 assert(!"not reached");
1155 glsl_type::std140_size(bool row_major
) const
1157 unsigned N
= is_double() ? 8 : 4;
1159 /* (1) If the member is a scalar consuming <N> basic machine units, the
1160 * base alignment is <N>.
1162 * (2) If the member is a two- or four-component vector with components
1163 * consuming <N> basic machine units, the base alignment is 2<N> or
1164 * 4<N>, respectively.
1166 * (3) If the member is a three-component vector with components consuming
1167 * <N> basic machine units, the base alignment is 4<N>.
1169 if (this->is_scalar() || this->is_vector()) {
1170 return this->vector_elements
* N
;
1173 /* (5) If the member is a column-major matrix with <C> columns and
1174 * <R> rows, the matrix is stored identically to an array of
1175 * <C> column vectors with <R> components each, according to
1178 * (6) If the member is an array of <S> column-major matrices with <C>
1179 * columns and <R> rows, the matrix is stored identically to a row of
1180 * <S>*<C> column vectors with <R> components each, according to rule
1183 * (7) If the member is a row-major matrix with <C> columns and <R>
1184 * rows, the matrix is stored identically to an array of <R>
1185 * row vectors with <C> components each, according to rule (4).
1187 * (8) If the member is an array of <S> row-major matrices with <C> columns
1188 * and <R> rows, the matrix is stored identically to a row of <S>*<R>
1189 * row vectors with <C> components each, according to rule (4).
1191 if (this->without_array()->is_matrix()) {
1192 const struct glsl_type
*element_type
;
1193 const struct glsl_type
*vec_type
;
1194 unsigned int array_len
;
1196 if (this->is_array()) {
1197 element_type
= this->fields
.array
;
1198 array_len
= this->length
;
1200 element_type
= this;
1205 vec_type
= get_instance(element_type
->base_type
,
1206 element_type
->matrix_columns
, 1);
1208 array_len
*= element_type
->vector_elements
;
1210 vec_type
= get_instance(element_type
->base_type
,
1211 element_type
->vector_elements
, 1);
1212 array_len
*= element_type
->matrix_columns
;
1214 const glsl_type
*array_type
= glsl_type::get_array_instance(vec_type
,
1217 return array_type
->std140_size(false);
1220 /* (4) If the member is an array of scalars or vectors, the base alignment
1221 * and array stride are set to match the base alignment of a single
1222 * array element, according to rules (1), (2), and (3), and rounded up
1223 * to the base alignment of a vec4. The array may have padding at the
1224 * end; the base offset of the member following the array is rounded up
1225 * to the next multiple of the base alignment.
1227 * (10) If the member is an array of <S> structures, the <S> elements of
1228 * the array are laid out in order, according to rule (9).
1230 if (this->is_array()) {
1231 if (this->fields
.array
->is_record()) {
1232 return this->length
* this->fields
.array
->std140_size(row_major
);
1234 unsigned element_base_align
=
1235 this->fields
.array
->std140_base_alignment(row_major
);
1236 return this->length
* MAX2(element_base_align
, 16);
1240 /* (9) If the member is a structure, the base alignment of the
1241 * structure is <N>, where <N> is the largest base alignment
1242 * value of any of its members, and rounded up to the base
1243 * alignment of a vec4. The individual members of this
1244 * sub-structure are then assigned offsets by applying this set
1245 * of rules recursively, where the base offset of the first
1246 * member of the sub-structure is equal to the aligned offset
1247 * of the structure. The structure may have padding at the end;
1248 * the base offset of the member following the sub-structure is
1249 * rounded up to the next multiple of the base alignment of the
1252 if (this->is_record()) {
1254 unsigned max_align
= 0;
1256 for (unsigned i
= 0; i
< this->length
; i
++) {
1257 bool field_row_major
= row_major
;
1258 const enum glsl_matrix_layout matrix_layout
=
1259 glsl_matrix_layout(this->fields
.structure
[i
].matrix_layout
);
1260 if (matrix_layout
== GLSL_MATRIX_LAYOUT_ROW_MAJOR
) {
1261 field_row_major
= true;
1262 } else if (matrix_layout
== GLSL_MATRIX_LAYOUT_COLUMN_MAJOR
) {
1263 field_row_major
= false;
1266 const struct glsl_type
*field_type
= this->fields
.structure
[i
].type
;
1267 unsigned align
= field_type
->std140_base_alignment(field_row_major
);
1268 size
= glsl_align(size
, align
);
1269 size
+= field_type
->std140_size(field_row_major
);
1271 max_align
= MAX2(align
, max_align
);
1273 if (field_type
->is_record() && (i
+ 1 < this->length
))
1274 size
= glsl_align(size
, 16);
1276 size
= glsl_align(size
, MAX2(max_align
, 16));
1280 assert(!"not reached");
1286 glsl_type::count_attribute_slots() const
1288 /* From page 31 (page 37 of the PDF) of the GLSL 1.50 spec:
1290 * "A scalar input counts the same amount against this limit as a vec4,
1291 * so applications may want to consider packing groups of four
1292 * unrelated float inputs together into a vector to better utilize the
1293 * capabilities of the underlying hardware. A matrix input will use up
1294 * multiple locations. The number of locations used will equal the
1295 * number of columns in the matrix."
1297 * The spec does not explicitly say how arrays are counted. However, it
1298 * should be safe to assume the total number of slots consumed by an array
1299 * is the number of entries in the array multiplied by the number of slots
1300 * consumed by a single element of the array.
1302 * The spec says nothing about how structs are counted, because vertex
1303 * attributes are not allowed to be (or contain) structs. However, Mesa
1304 * allows varying structs, the number of varying slots taken up by a
1305 * varying struct is simply equal to the sum of the number of slots taken
1306 * up by each element.
1308 switch (this->base_type
) {
1309 case GLSL_TYPE_UINT
:
1311 case GLSL_TYPE_FLOAT
:
1312 case GLSL_TYPE_BOOL
:
1313 case GLSL_TYPE_DOUBLE
:
1314 return this->matrix_columns
;
1316 case GLSL_TYPE_STRUCT
:
1317 case GLSL_TYPE_INTERFACE
: {
1320 for (unsigned i
= 0; i
< this->length
; i
++)
1321 size
+= this->fields
.structure
[i
].type
->count_attribute_slots();
1326 case GLSL_TYPE_ARRAY
:
1327 return this->length
* this->fields
.array
->count_attribute_slots();
1329 case GLSL_TYPE_SAMPLER
:
1330 case GLSL_TYPE_IMAGE
:
1331 case GLSL_TYPE_ATOMIC_UINT
:
1332 case GLSL_TYPE_VOID
:
1333 case GLSL_TYPE_ERROR
:
1337 assert(!"Unexpected type in count_attribute_slots()");
1343 glsl_type::coordinate_components() const
1347 switch (sampler_dimensionality
) {
1348 case GLSL_SAMPLER_DIM_1D
:
1349 case GLSL_SAMPLER_DIM_BUF
:
1352 case GLSL_SAMPLER_DIM_2D
:
1353 case GLSL_SAMPLER_DIM_RECT
:
1354 case GLSL_SAMPLER_DIM_MS
:
1355 case GLSL_SAMPLER_DIM_EXTERNAL
:
1358 case GLSL_SAMPLER_DIM_3D
:
1359 case GLSL_SAMPLER_DIM_CUBE
:
1363 assert(!"Should not get here.");
1368 /* Array textures need an additional component for the array index, except
1369 * for cubemap array images that behave like a 2D array of interleaved
1372 if (sampler_array
&&
1373 !(base_type
== GLSL_TYPE_IMAGE
&&
1374 sampler_dimensionality
== GLSL_SAMPLER_DIM_CUBE
))