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/macros.h"
26 #include "compiler/glsl/glsl_parser_extras.h"
27 #include "glsl_types.h"
28 #include "util/hash_table.h"
29 #include "util/u_string.h"
32 mtx_t
glsl_type::hash_mutex
= _MTX_INITIALIZER_NP
;
33 hash_table
*glsl_type::array_types
= NULL
;
34 hash_table
*glsl_type::record_types
= NULL
;
35 hash_table
*glsl_type::interface_types
= NULL
;
36 hash_table
*glsl_type::function_types
= NULL
;
37 hash_table
*glsl_type::subroutine_types
= NULL
;
39 glsl_type::glsl_type(GLenum gl_type
,
40 glsl_base_type base_type
, unsigned vector_elements
,
41 unsigned matrix_columns
, const char *name
) :
43 base_type(base_type
), sampled_type(GLSL_TYPE_VOID
),
44 sampler_dimensionality(0), sampler_shadow(0), sampler_array(0),
45 interface_packing(0), interface_row_major(0),
46 vector_elements(vector_elements
), matrix_columns(matrix_columns
),
49 /* Values of these types must fit in the two bits of
50 * glsl_type::sampled_type.
52 STATIC_ASSERT((unsigned(GLSL_TYPE_UINT
) & 3) == unsigned(GLSL_TYPE_UINT
));
53 STATIC_ASSERT((unsigned(GLSL_TYPE_INT
) & 3) == unsigned(GLSL_TYPE_INT
));
54 STATIC_ASSERT((unsigned(GLSL_TYPE_FLOAT
) & 3) == unsigned(GLSL_TYPE_FLOAT
));
56 ASSERT_BITFIELD_SIZE(glsl_type
, base_type
, GLSL_TYPE_ERROR
);
57 ASSERT_BITFIELD_SIZE(glsl_type
, sampled_type
, GLSL_TYPE_ERROR
);
58 ASSERT_BITFIELD_SIZE(glsl_type
, sampler_dimensionality
,
59 GLSL_SAMPLER_DIM_SUBPASS_MS
);
61 this->mem_ctx
= ralloc_context(NULL
);
62 assert(this->mem_ctx
!= NULL
);
65 this->name
= ralloc_strdup(this->mem_ctx
, name
);
67 /* Neither dimension is zero or both dimensions are zero.
69 assert((vector_elements
== 0) == (matrix_columns
== 0));
70 memset(& fields
, 0, sizeof(fields
));
73 glsl_type::glsl_type(GLenum gl_type
, glsl_base_type base_type
,
74 enum glsl_sampler_dim dim
, bool shadow
, bool array
,
75 glsl_base_type type
, const char *name
) :
77 base_type(base_type
), sampled_type(type
),
78 sampler_dimensionality(dim
), sampler_shadow(shadow
),
79 sampler_array(array
), interface_packing(0),
80 interface_row_major(0), length(0)
82 this->mem_ctx
= ralloc_context(NULL
);
83 assert(this->mem_ctx
!= NULL
);
86 this->name
= ralloc_strdup(this->mem_ctx
, name
);
88 memset(& fields
, 0, sizeof(fields
));
90 matrix_columns
= vector_elements
= 1;
93 glsl_type::glsl_type(const glsl_struct_field
*fields
, unsigned num_fields
,
96 base_type(GLSL_TYPE_STRUCT
), sampled_type(GLSL_TYPE_VOID
),
97 sampler_dimensionality(0), sampler_shadow(0), sampler_array(0),
98 interface_packing(0), interface_row_major(0),
99 vector_elements(0), matrix_columns(0),
104 this->mem_ctx
= ralloc_context(NULL
);
105 assert(this->mem_ctx
!= NULL
);
107 assert(name
!= NULL
);
108 this->name
= ralloc_strdup(this->mem_ctx
, name
);
109 /* Zero-fill to prevent spurious Valgrind errors when serializing NIR
110 * due to uninitialized unused bits in bit fields. */
111 this->fields
.structure
= rzalloc_array(this->mem_ctx
,
112 glsl_struct_field
, length
);
114 for (i
= 0; i
< length
; i
++) {
115 this->fields
.structure
[i
] = fields
[i
];
116 this->fields
.structure
[i
].name
= ralloc_strdup(this->fields
.structure
,
121 glsl_type::glsl_type(const glsl_struct_field
*fields
, unsigned num_fields
,
122 enum glsl_interface_packing packing
,
123 bool row_major
, const char *name
) :
125 base_type(GLSL_TYPE_INTERFACE
), sampled_type(GLSL_TYPE_VOID
),
126 sampler_dimensionality(0), sampler_shadow(0), sampler_array(0),
127 interface_packing((unsigned) packing
),
128 interface_row_major((unsigned) row_major
),
129 vector_elements(0), matrix_columns(0),
134 this->mem_ctx
= ralloc_context(NULL
);
135 assert(this->mem_ctx
!= NULL
);
137 assert(name
!= NULL
);
138 this->name
= ralloc_strdup(this->mem_ctx
, name
);
139 this->fields
.structure
= rzalloc_array(this->mem_ctx
,
140 glsl_struct_field
, length
);
141 for (i
= 0; i
< length
; i
++) {
142 this->fields
.structure
[i
] = fields
[i
];
143 this->fields
.structure
[i
].name
= ralloc_strdup(this->fields
.structure
,
148 glsl_type::glsl_type(const glsl_type
*return_type
,
149 const glsl_function_param
*params
, unsigned num_params
) :
151 base_type(GLSL_TYPE_FUNCTION
), sampled_type(GLSL_TYPE_VOID
),
152 sampler_dimensionality(0), sampler_shadow(0), sampler_array(0),
153 interface_packing(0), interface_row_major(0),
154 vector_elements(0), matrix_columns(0),
159 this->mem_ctx
= ralloc_context(NULL
);
160 assert(this->mem_ctx
!= NULL
);
162 this->fields
.parameters
= rzalloc_array(this->mem_ctx
,
163 glsl_function_param
, num_params
+ 1);
165 /* We store the return type as the first parameter */
166 this->fields
.parameters
[0].type
= return_type
;
167 this->fields
.parameters
[0].in
= false;
168 this->fields
.parameters
[0].out
= true;
170 /* We store the i'th parameter in slot i+1 */
171 for (i
= 0; i
< length
; i
++) {
172 this->fields
.parameters
[i
+ 1].type
= params
[i
].type
;
173 this->fields
.parameters
[i
+ 1].in
= params
[i
].in
;
174 this->fields
.parameters
[i
+ 1].out
= params
[i
].out
;
178 glsl_type::glsl_type(const char *subroutine_name
) :
180 base_type(GLSL_TYPE_SUBROUTINE
), sampled_type(GLSL_TYPE_VOID
),
181 sampler_dimensionality(0), sampler_shadow(0), sampler_array(0),
182 interface_packing(0), interface_row_major(0),
183 vector_elements(1), matrix_columns(1),
186 this->mem_ctx
= ralloc_context(NULL
);
187 assert(this->mem_ctx
!= NULL
);
189 assert(subroutine_name
!= NULL
);
190 this->name
= ralloc_strdup(this->mem_ctx
, subroutine_name
);
193 glsl_type::~glsl_type()
195 ralloc_free(this->mem_ctx
);
199 glsl_type::contains_sampler() const
201 if (this->is_array()) {
202 return this->fields
.array
->contains_sampler();
203 } else if (this->is_record() || this->is_interface()) {
204 for (unsigned int i
= 0; i
< this->length
; i
++) {
205 if (this->fields
.structure
[i
].type
->contains_sampler())
210 return this->is_sampler();
215 glsl_type::contains_array() const
217 if (this->is_record() || this->is_interface()) {
218 for (unsigned int i
= 0; i
< this->length
; i
++) {
219 if (this->fields
.structure
[i
].type
->contains_array())
224 return this->is_array();
229 glsl_type::contains_integer() const
231 if (this->is_array()) {
232 return this->fields
.array
->contains_integer();
233 } else if (this->is_record() || this->is_interface()) {
234 for (unsigned int i
= 0; i
< this->length
; i
++) {
235 if (this->fields
.structure
[i
].type
->contains_integer())
240 return this->is_integer();
245 glsl_type::contains_double() const
247 if (this->is_array()) {
248 return this->fields
.array
->contains_double();
249 } else if (this->is_record() || this->is_interface()) {
250 for (unsigned int i
= 0; i
< this->length
; i
++) {
251 if (this->fields
.structure
[i
].type
->contains_double())
256 return this->is_double();
261 glsl_type::contains_opaque() const {
263 case GLSL_TYPE_SAMPLER
:
264 case GLSL_TYPE_IMAGE
:
265 case GLSL_TYPE_ATOMIC_UINT
:
267 case GLSL_TYPE_ARRAY
:
268 return fields
.array
->contains_opaque();
269 case GLSL_TYPE_STRUCT
:
270 case GLSL_TYPE_INTERFACE
:
271 for (unsigned int i
= 0; i
< length
; i
++) {
272 if (fields
.structure
[i
].type
->contains_opaque())
282 glsl_type::contains_subroutine() const
284 if (this->is_array()) {
285 return this->fields
.array
->contains_subroutine();
286 } else if (this->is_record() || this->is_interface()) {
287 for (unsigned int i
= 0; i
< this->length
; i
++) {
288 if (this->fields
.structure
[i
].type
->contains_subroutine())
293 return this->is_subroutine();
298 glsl_type::sampler_index() const
300 const glsl_type
*const t
= (this->is_array()) ? this->fields
.array
: this;
302 assert(t
->is_sampler() || t
->is_image());
304 switch (t
->sampler_dimensionality
) {
305 case GLSL_SAMPLER_DIM_1D
:
306 return (t
->sampler_array
) ? TEXTURE_1D_ARRAY_INDEX
: TEXTURE_1D_INDEX
;
307 case GLSL_SAMPLER_DIM_2D
:
308 return (t
->sampler_array
) ? TEXTURE_2D_ARRAY_INDEX
: TEXTURE_2D_INDEX
;
309 case GLSL_SAMPLER_DIM_3D
:
310 return TEXTURE_3D_INDEX
;
311 case GLSL_SAMPLER_DIM_CUBE
:
312 return (t
->sampler_array
) ? TEXTURE_CUBE_ARRAY_INDEX
: TEXTURE_CUBE_INDEX
;
313 case GLSL_SAMPLER_DIM_RECT
:
314 return TEXTURE_RECT_INDEX
;
315 case GLSL_SAMPLER_DIM_BUF
:
316 return TEXTURE_BUFFER_INDEX
;
317 case GLSL_SAMPLER_DIM_EXTERNAL
:
318 return TEXTURE_EXTERNAL_INDEX
;
319 case GLSL_SAMPLER_DIM_MS
:
320 return (t
->sampler_array
) ? TEXTURE_2D_MULTISAMPLE_ARRAY_INDEX
: TEXTURE_2D_MULTISAMPLE_INDEX
;
322 assert(!"Should not get here.");
323 return TEXTURE_BUFFER_INDEX
;
328 glsl_type::contains_image() const
330 if (this->is_array()) {
331 return this->fields
.array
->contains_image();
332 } else if (this->is_record() || this->is_interface()) {
333 for (unsigned int i
= 0; i
< this->length
; i
++) {
334 if (this->fields
.structure
[i
].type
->contains_image())
339 return this->is_image();
343 const glsl_type
*glsl_type::get_base_type() const
348 case GLSL_TYPE_UINT16
:
349 return uint16_t_type
;
350 case GLSL_TYPE_UINT8
:
354 case GLSL_TYPE_INT16
:
358 case GLSL_TYPE_FLOAT
:
360 case GLSL_TYPE_FLOAT16
:
361 return float16_t_type
;
362 case GLSL_TYPE_DOUBLE
:
366 case GLSL_TYPE_UINT64
:
367 return uint64_t_type
;
368 case GLSL_TYPE_INT64
:
376 const glsl_type
*glsl_type::get_scalar_type() const
378 const glsl_type
*type
= this;
381 while (type
->base_type
== GLSL_TYPE_ARRAY
)
382 type
= type
->fields
.array
;
384 const glsl_type
*scalar_type
= type
->get_base_type();
385 if (scalar_type
== error_type
)
393 hash_free_type_function(struct hash_entry
*entry
)
395 glsl_type
*type
= (glsl_type
*) entry
->data
;
397 if (type
->is_array())
398 free((void*)entry
->key
);
404 _mesa_glsl_release_types(void)
406 /* Should only be called during atexit (either when unloading shared
407 * object, or if process terminates), so no mutex-locking should be
410 if (glsl_type::array_types
!= NULL
) {
411 _mesa_hash_table_destroy(glsl_type::array_types
, hash_free_type_function
);
412 glsl_type::array_types
= NULL
;
415 if (glsl_type::record_types
!= NULL
) {
416 _mesa_hash_table_destroy(glsl_type::record_types
, hash_free_type_function
);
417 glsl_type::record_types
= NULL
;
420 if (glsl_type::interface_types
!= NULL
) {
421 _mesa_hash_table_destroy(glsl_type::interface_types
, hash_free_type_function
);
422 glsl_type::interface_types
= NULL
;
425 if (glsl_type::function_types
!= NULL
) {
426 _mesa_hash_table_destroy(glsl_type::function_types
, hash_free_type_function
);
427 glsl_type::function_types
= NULL
;
430 if (glsl_type::subroutine_types
!= NULL
) {
431 _mesa_hash_table_destroy(glsl_type::subroutine_types
, hash_free_type_function
);
432 glsl_type::subroutine_types
= NULL
;
437 glsl_type::glsl_type(const glsl_type
*array
, unsigned length
) :
438 base_type(GLSL_TYPE_ARRAY
), sampled_type(GLSL_TYPE_VOID
),
439 sampler_dimensionality(0), sampler_shadow(0), sampler_array(0),
440 interface_packing(0), interface_row_major(0),
441 vector_elements(0), matrix_columns(0),
442 length(length
), name(NULL
)
444 this->fields
.array
= array
;
445 /* Inherit the gl type of the base. The GL type is used for
446 * uniform/statevar handling in Mesa and the arrayness of the type
447 * is represented by the size rather than the type.
449 this->gl_type
= array
->gl_type
;
451 /* Allow a maximum of 10 characters for the array size. This is enough
452 * for 32-bits of ~0. The extra 3 are for the '[', ']', and terminating
455 const unsigned name_length
= strlen(array
->name
) + 10 + 3;
457 this->mem_ctx
= ralloc_context(NULL
);
458 assert(this->mem_ctx
!= NULL
);
460 char *const n
= (char *) ralloc_size(this->mem_ctx
, name_length
);
463 util_snprintf(n
, name_length
, "%s[]", array
->name
);
465 /* insert outermost dimensions in the correct spot
466 * otherwise the dimension order will be backwards
468 const char *pos
= strchr(array
->name
, '[');
470 int idx
= pos
- array
->name
;
471 util_snprintf(n
, idx
+1, "%s", array
->name
);
472 util_snprintf(n
+ idx
, name_length
- idx
, "[%u]%s",
473 length
, array
->name
+ idx
);
475 util_snprintf(n
, name_length
, "%s[%u]", array
->name
, length
);
483 glsl_type::vec(unsigned components
, const glsl_type
*const ts
[])
485 unsigned n
= components
;
489 else if (components
== 16)
498 #define VECN(components, sname, vname) \
500 glsl_type:: vname (unsigned components) \
502 static const glsl_type *const ts[] = { \
503 sname ## _type, vname ## 2_type, \
504 vname ## 3_type, vname ## 4_type, \
505 vname ## 8_type, vname ## 16_type, \
507 return glsl_type::vec(components, ts); \
510 VECN(components
, float, vec
)
511 VECN(components
, float16_t
, f16vec
)
512 VECN(components
, double, dvec
)
513 VECN(components
, int, ivec
)
514 VECN(components
, uint
, uvec
)
515 VECN(components
, bool, bvec
)
516 VECN(components
, int64_t, i64vec
)
517 VECN(components
, uint64_t, u64vec
)
518 VECN(components
, int16_t, i16vec
)
519 VECN(components
, uint16_t, u16vec
)
520 VECN(components
, int8_t, i8vec
)
521 VECN(components
, uint8_t, u8vec
)
524 glsl_type::get_instance(unsigned base_type
, unsigned rows
, unsigned columns
)
526 if (base_type
== GLSL_TYPE_VOID
)
529 /* Treat GLSL vectors as Nx1 matrices.
537 case GLSL_TYPE_FLOAT
:
539 case GLSL_TYPE_FLOAT16
:
541 case GLSL_TYPE_DOUBLE
:
545 case GLSL_TYPE_UINT64
:
547 case GLSL_TYPE_INT64
:
549 case GLSL_TYPE_UINT16
:
551 case GLSL_TYPE_INT16
:
553 case GLSL_TYPE_UINT8
:
561 if ((base_type
!= GLSL_TYPE_FLOAT
&&
562 base_type
!= GLSL_TYPE_DOUBLE
&&
563 base_type
!= GLSL_TYPE_FLOAT16
) || (rows
== 1))
566 /* GLSL matrix types are named mat{COLUMNS}x{ROWS}. Only the following
567 * combinations are valid:
575 #define IDX(c,r) (((c-1)*3) + (r-1))
578 case GLSL_TYPE_DOUBLE
: {
579 switch (IDX(columns
, rows
)) {
580 case IDX(2,2): return dmat2_type
;
581 case IDX(2,3): return dmat2x3_type
;
582 case IDX(2,4): return dmat2x4_type
;
583 case IDX(3,2): return dmat3x2_type
;
584 case IDX(3,3): return dmat3_type
;
585 case IDX(3,4): return dmat3x4_type
;
586 case IDX(4,2): return dmat4x2_type
;
587 case IDX(4,3): return dmat4x3_type
;
588 case IDX(4,4): return dmat4_type
;
589 default: return error_type
;
592 case GLSL_TYPE_FLOAT
: {
593 switch (IDX(columns
, rows
)) {
594 case IDX(2,2): return mat2_type
;
595 case IDX(2,3): return mat2x3_type
;
596 case IDX(2,4): return mat2x4_type
;
597 case IDX(3,2): return mat3x2_type
;
598 case IDX(3,3): return mat3_type
;
599 case IDX(3,4): return mat3x4_type
;
600 case IDX(4,2): return mat4x2_type
;
601 case IDX(4,3): return mat4x3_type
;
602 case IDX(4,4): return mat4_type
;
603 default: return error_type
;
606 case GLSL_TYPE_FLOAT16
: {
607 switch (IDX(columns
, rows
)) {
608 case IDX(2,2): return f16mat2_type
;
609 case IDX(2,3): return f16mat2x3_type
;
610 case IDX(2,4): return f16mat2x4_type
;
611 case IDX(3,2): return f16mat3x2_type
;
612 case IDX(3,3): return f16mat3_type
;
613 case IDX(3,4): return f16mat3x4_type
;
614 case IDX(4,2): return f16mat4x2_type
;
615 case IDX(4,3): return f16mat4x3_type
;
616 case IDX(4,4): return f16mat4_type
;
617 default: return error_type
;
620 default: return error_type
;
624 assert(!"Should not get here.");
629 glsl_type::get_sampler_instance(enum glsl_sampler_dim dim
,
635 case GLSL_TYPE_FLOAT
:
637 case GLSL_SAMPLER_DIM_1D
:
639 return (array
? sampler1DArrayShadow_type
: sampler1DShadow_type
);
641 return (array
? sampler1DArray_type
: sampler1D_type
);
642 case GLSL_SAMPLER_DIM_2D
:
644 return (array
? sampler2DArrayShadow_type
: sampler2DShadow_type
);
646 return (array
? sampler2DArray_type
: sampler2D_type
);
647 case GLSL_SAMPLER_DIM_3D
:
651 return sampler3D_type
;
652 case GLSL_SAMPLER_DIM_CUBE
:
654 return (array
? samplerCubeArrayShadow_type
: samplerCubeShadow_type
);
656 return (array
? samplerCubeArray_type
: samplerCube_type
);
657 case GLSL_SAMPLER_DIM_RECT
:
661 return sampler2DRectShadow_type
;
663 return sampler2DRect_type
;
664 case GLSL_SAMPLER_DIM_BUF
:
668 return samplerBuffer_type
;
669 case GLSL_SAMPLER_DIM_MS
:
672 return (array
? sampler2DMSArray_type
: sampler2DMS_type
);
673 case GLSL_SAMPLER_DIM_EXTERNAL
:
677 return samplerExternalOES_type
;
678 case GLSL_SAMPLER_DIM_SUBPASS
:
679 case GLSL_SAMPLER_DIM_SUBPASS_MS
:
686 case GLSL_SAMPLER_DIM_1D
:
687 return (array
? isampler1DArray_type
: isampler1D_type
);
688 case GLSL_SAMPLER_DIM_2D
:
689 return (array
? isampler2DArray_type
: isampler2D_type
);
690 case GLSL_SAMPLER_DIM_3D
:
693 return isampler3D_type
;
694 case GLSL_SAMPLER_DIM_CUBE
:
695 return (array
? isamplerCubeArray_type
: isamplerCube_type
);
696 case GLSL_SAMPLER_DIM_RECT
:
699 return isampler2DRect_type
;
700 case GLSL_SAMPLER_DIM_BUF
:
703 return isamplerBuffer_type
;
704 case GLSL_SAMPLER_DIM_MS
:
705 return (array
? isampler2DMSArray_type
: isampler2DMS_type
);
706 case GLSL_SAMPLER_DIM_EXTERNAL
:
708 case GLSL_SAMPLER_DIM_SUBPASS
:
709 case GLSL_SAMPLER_DIM_SUBPASS_MS
:
716 case GLSL_SAMPLER_DIM_1D
:
717 return (array
? usampler1DArray_type
: usampler1D_type
);
718 case GLSL_SAMPLER_DIM_2D
:
719 return (array
? usampler2DArray_type
: usampler2D_type
);
720 case GLSL_SAMPLER_DIM_3D
:
723 return usampler3D_type
;
724 case GLSL_SAMPLER_DIM_CUBE
:
725 return (array
? usamplerCubeArray_type
: usamplerCube_type
);
726 case GLSL_SAMPLER_DIM_RECT
:
729 return usampler2DRect_type
;
730 case GLSL_SAMPLER_DIM_BUF
:
733 return usamplerBuffer_type
;
734 case GLSL_SAMPLER_DIM_MS
:
735 return (array
? usampler2DMSArray_type
: usampler2DMS_type
);
736 case GLSL_SAMPLER_DIM_EXTERNAL
:
738 case GLSL_SAMPLER_DIM_SUBPASS
:
739 case GLSL_SAMPLER_DIM_SUBPASS_MS
:
746 unreachable("switch statement above should be complete");
750 glsl_type::get_image_instance(enum glsl_sampler_dim dim
,
751 bool array
, glsl_base_type type
)
754 case GLSL_TYPE_FLOAT
:
756 case GLSL_SAMPLER_DIM_1D
:
757 return (array
? image1DArray_type
: image1D_type
);
758 case GLSL_SAMPLER_DIM_2D
:
759 return (array
? image2DArray_type
: image2D_type
);
760 case GLSL_SAMPLER_DIM_3D
:
762 case GLSL_SAMPLER_DIM_CUBE
:
763 return (array
? imageCubeArray_type
: imageCube_type
);
764 case GLSL_SAMPLER_DIM_RECT
:
768 return image2DRect_type
;
769 case GLSL_SAMPLER_DIM_BUF
:
773 return imageBuffer_type
;
774 case GLSL_SAMPLER_DIM_MS
:
775 return (array
? image2DMSArray_type
: image2DMS_type
);
776 case GLSL_SAMPLER_DIM_SUBPASS
:
777 return subpassInput_type
;
778 case GLSL_SAMPLER_DIM_SUBPASS_MS
:
779 return subpassInputMS_type
;
780 case GLSL_SAMPLER_DIM_EXTERNAL
:
785 case GLSL_SAMPLER_DIM_1D
:
786 return (array
? iimage1DArray_type
: iimage1D_type
);
787 case GLSL_SAMPLER_DIM_2D
:
788 return (array
? iimage2DArray_type
: iimage2D_type
);
789 case GLSL_SAMPLER_DIM_3D
:
792 return iimage3D_type
;
793 case GLSL_SAMPLER_DIM_CUBE
:
794 return (array
? iimageCubeArray_type
: iimageCube_type
);
795 case GLSL_SAMPLER_DIM_RECT
:
798 return iimage2DRect_type
;
799 case GLSL_SAMPLER_DIM_BUF
:
802 return iimageBuffer_type
;
803 case GLSL_SAMPLER_DIM_MS
:
804 return (array
? iimage2DMSArray_type
: iimage2DMS_type
);
805 case GLSL_SAMPLER_DIM_SUBPASS
:
806 return isubpassInput_type
;
807 case GLSL_SAMPLER_DIM_SUBPASS_MS
:
808 return isubpassInputMS_type
;
809 case GLSL_SAMPLER_DIM_EXTERNAL
:
814 case GLSL_SAMPLER_DIM_1D
:
815 return (array
? uimage1DArray_type
: uimage1D_type
);
816 case GLSL_SAMPLER_DIM_2D
:
817 return (array
? uimage2DArray_type
: uimage2D_type
);
818 case GLSL_SAMPLER_DIM_3D
:
821 return uimage3D_type
;
822 case GLSL_SAMPLER_DIM_CUBE
:
823 return (array
? uimageCubeArray_type
: uimageCube_type
);
824 case GLSL_SAMPLER_DIM_RECT
:
827 return uimage2DRect_type
;
828 case GLSL_SAMPLER_DIM_BUF
:
831 return uimageBuffer_type
;
832 case GLSL_SAMPLER_DIM_MS
:
833 return (array
? uimage2DMSArray_type
: uimage2DMS_type
);
834 case GLSL_SAMPLER_DIM_SUBPASS
:
835 return usubpassInput_type
;
836 case GLSL_SAMPLER_DIM_SUBPASS_MS
:
837 return usubpassInputMS_type
;
838 case GLSL_SAMPLER_DIM_EXTERNAL
:
845 unreachable("switch statement above should be complete");
849 glsl_type::get_array_instance(const glsl_type
*base
, unsigned array_size
)
851 /* Generate a name using the base type pointer in the key. This is
852 * done because the name of the base type may not be unique across
853 * shaders. For example, two shaders may have different record types
857 util_snprintf(key
, sizeof(key
), "%p[%u]", (void *) base
, array_size
);
859 mtx_lock(&glsl_type::hash_mutex
);
861 if (array_types
== NULL
) {
862 array_types
= _mesa_hash_table_create(NULL
, _mesa_key_hash_string
,
863 _mesa_key_string_equal
);
866 const struct hash_entry
*entry
= _mesa_hash_table_search(array_types
, key
);
868 const glsl_type
*t
= new glsl_type(base
, array_size
);
870 entry
= _mesa_hash_table_insert(array_types
,
875 assert(((glsl_type
*) entry
->data
)->base_type
== GLSL_TYPE_ARRAY
);
876 assert(((glsl_type
*) entry
->data
)->length
== array_size
);
877 assert(((glsl_type
*) entry
->data
)->fields
.array
== base
);
879 mtx_unlock(&glsl_type::hash_mutex
);
881 return (glsl_type
*) entry
->data
;
886 glsl_type::record_compare(const glsl_type
*b
, bool match_locations
) const
888 if (this->length
!= b
->length
)
891 if (this->interface_packing
!= b
->interface_packing
)
894 if (this->interface_row_major
!= b
->interface_row_major
)
897 /* From the GLSL 4.20 specification (Sec 4.2):
899 * "Structures must have the same name, sequence of type names, and
900 * type definitions, and field names to be considered the same type."
902 * GLSL ES behaves the same (Ver 1.00 Sec 4.2.4, Ver 3.00 Sec 4.2.5).
904 if (strcmp(this->name
, b
->name
) != 0)
907 for (unsigned i
= 0; i
< this->length
; i
++) {
908 if (this->fields
.structure
[i
].type
!= b
->fields
.structure
[i
].type
)
910 if (strcmp(this->fields
.structure
[i
].name
,
911 b
->fields
.structure
[i
].name
) != 0)
913 if (this->fields
.structure
[i
].matrix_layout
914 != b
->fields
.structure
[i
].matrix_layout
)
916 if (match_locations
&& this->fields
.structure
[i
].location
917 != b
->fields
.structure
[i
].location
)
919 if (this->fields
.structure
[i
].offset
920 != b
->fields
.structure
[i
].offset
)
922 if (this->fields
.structure
[i
].interpolation
923 != b
->fields
.structure
[i
].interpolation
)
925 if (this->fields
.structure
[i
].centroid
926 != b
->fields
.structure
[i
].centroid
)
928 if (this->fields
.structure
[i
].sample
929 != b
->fields
.structure
[i
].sample
)
931 if (this->fields
.structure
[i
].patch
932 != b
->fields
.structure
[i
].patch
)
934 if (this->fields
.structure
[i
].memory_read_only
935 != b
->fields
.structure
[i
].memory_read_only
)
937 if (this->fields
.structure
[i
].memory_write_only
938 != b
->fields
.structure
[i
].memory_write_only
)
940 if (this->fields
.structure
[i
].memory_coherent
941 != b
->fields
.structure
[i
].memory_coherent
)
943 if (this->fields
.structure
[i
].memory_volatile
944 != b
->fields
.structure
[i
].memory_volatile
)
946 if (this->fields
.structure
[i
].memory_restrict
947 != b
->fields
.structure
[i
].memory_restrict
)
949 if (this->fields
.structure
[i
].image_format
950 != b
->fields
.structure
[i
].image_format
)
952 if (this->fields
.structure
[i
].precision
953 != b
->fields
.structure
[i
].precision
)
955 if (this->fields
.structure
[i
].explicit_xfb_buffer
956 != b
->fields
.structure
[i
].explicit_xfb_buffer
)
958 if (this->fields
.structure
[i
].xfb_buffer
959 != b
->fields
.structure
[i
].xfb_buffer
)
961 if (this->fields
.structure
[i
].xfb_stride
962 != b
->fields
.structure
[i
].xfb_stride
)
971 glsl_type::record_key_compare(const void *a
, const void *b
)
973 const glsl_type
*const key1
= (glsl_type
*) a
;
974 const glsl_type
*const key2
= (glsl_type
*) b
;
976 return strcmp(key1
->name
, key2
->name
) == 0 && key1
->record_compare(key2
);
981 * Generate an integer hash value for a glsl_type structure type.
984 glsl_type::record_key_hash(const void *a
)
986 const glsl_type
*const key
= (glsl_type
*) a
;
987 uintptr_t hash
= key
->length
;
990 for (unsigned i
= 0; i
< key
->length
; i
++) {
991 /* casting pointer to uintptr_t */
992 hash
= (hash
* 13 ) + (uintptr_t) key
->fields
.structure
[i
].type
;
995 if (sizeof(hash
) == 8)
996 retval
= (hash
& 0xffffffff) ^ ((uint64_t) hash
>> 32);
1005 glsl_type::get_record_instance(const glsl_struct_field
*fields
,
1006 unsigned num_fields
,
1009 const glsl_type
key(fields
, num_fields
, name
);
1011 mtx_lock(&glsl_type::hash_mutex
);
1013 if (record_types
== NULL
) {
1014 record_types
= _mesa_hash_table_create(NULL
, record_key_hash
,
1015 record_key_compare
);
1018 const struct hash_entry
*entry
= _mesa_hash_table_search(record_types
,
1020 if (entry
== NULL
) {
1021 const glsl_type
*t
= new glsl_type(fields
, num_fields
, name
);
1023 entry
= _mesa_hash_table_insert(record_types
, t
, (void *) t
);
1026 assert(((glsl_type
*) entry
->data
)->base_type
== GLSL_TYPE_STRUCT
);
1027 assert(((glsl_type
*) entry
->data
)->length
== num_fields
);
1028 assert(strcmp(((glsl_type
*) entry
->data
)->name
, name
) == 0);
1030 mtx_unlock(&glsl_type::hash_mutex
);
1032 return (glsl_type
*) entry
->data
;
1037 glsl_type::get_interface_instance(const glsl_struct_field
*fields
,
1038 unsigned num_fields
,
1039 enum glsl_interface_packing packing
,
1041 const char *block_name
)
1043 const glsl_type
key(fields
, num_fields
, packing
, row_major
, block_name
);
1045 mtx_lock(&glsl_type::hash_mutex
);
1047 if (interface_types
== NULL
) {
1048 interface_types
= _mesa_hash_table_create(NULL
, record_key_hash
,
1049 record_key_compare
);
1052 const struct hash_entry
*entry
= _mesa_hash_table_search(interface_types
,
1054 if (entry
== NULL
) {
1055 const glsl_type
*t
= new glsl_type(fields
, num_fields
,
1056 packing
, row_major
, block_name
);
1058 entry
= _mesa_hash_table_insert(interface_types
, t
, (void *) t
);
1061 assert(((glsl_type
*) entry
->data
)->base_type
== GLSL_TYPE_INTERFACE
);
1062 assert(((glsl_type
*) entry
->data
)->length
== num_fields
);
1063 assert(strcmp(((glsl_type
*) entry
->data
)->name
, block_name
) == 0);
1065 mtx_unlock(&glsl_type::hash_mutex
);
1067 return (glsl_type
*) entry
->data
;
1071 glsl_type::get_subroutine_instance(const char *subroutine_name
)
1073 const glsl_type
key(subroutine_name
);
1075 mtx_lock(&glsl_type::hash_mutex
);
1077 if (subroutine_types
== NULL
) {
1078 subroutine_types
= _mesa_hash_table_create(NULL
, record_key_hash
,
1079 record_key_compare
);
1082 const struct hash_entry
*entry
= _mesa_hash_table_search(subroutine_types
,
1084 if (entry
== NULL
) {
1085 const glsl_type
*t
= new glsl_type(subroutine_name
);
1087 entry
= _mesa_hash_table_insert(subroutine_types
, t
, (void *) t
);
1090 assert(((glsl_type
*) entry
->data
)->base_type
== GLSL_TYPE_SUBROUTINE
);
1091 assert(strcmp(((glsl_type
*) entry
->data
)->name
, subroutine_name
) == 0);
1093 mtx_unlock(&glsl_type::hash_mutex
);
1095 return (glsl_type
*) entry
->data
;
1100 function_key_compare(const void *a
, const void *b
)
1102 const glsl_type
*const key1
= (glsl_type
*) a
;
1103 const glsl_type
*const key2
= (glsl_type
*) b
;
1105 if (key1
->length
!= key2
->length
)
1108 return memcmp(key1
->fields
.parameters
, key2
->fields
.parameters
,
1109 (key1
->length
+ 1) * sizeof(*key1
->fields
.parameters
)) == 0;
1114 function_key_hash(const void *a
)
1116 const glsl_type
*const key
= (glsl_type
*) a
;
1117 return _mesa_hash_data(key
->fields
.parameters
,
1118 (key
->length
+ 1) * sizeof(*key
->fields
.parameters
));
1122 glsl_type::get_function_instance(const glsl_type
*return_type
,
1123 const glsl_function_param
*params
,
1124 unsigned num_params
)
1126 const glsl_type
key(return_type
, params
, num_params
);
1128 mtx_lock(&glsl_type::hash_mutex
);
1130 if (function_types
== NULL
) {
1131 function_types
= _mesa_hash_table_create(NULL
, function_key_hash
,
1132 function_key_compare
);
1135 struct hash_entry
*entry
= _mesa_hash_table_search(function_types
, &key
);
1136 if (entry
== NULL
) {
1137 const glsl_type
*t
= new glsl_type(return_type
, params
, num_params
);
1139 entry
= _mesa_hash_table_insert(function_types
, t
, (void *) t
);
1142 const glsl_type
*t
= (const glsl_type
*)entry
->data
;
1144 assert(t
->base_type
== GLSL_TYPE_FUNCTION
);
1145 assert(t
->length
== num_params
);
1147 mtx_unlock(&glsl_type::hash_mutex
);
1154 glsl_type::get_mul_type(const glsl_type
*type_a
, const glsl_type
*type_b
)
1156 if (type_a
== type_b
) {
1158 } else if (type_a
->is_matrix() && type_b
->is_matrix()) {
1159 /* Matrix multiply. The columns of A must match the rows of B. Given
1160 * the other previously tested constraints, this means the vector type
1161 * of a row from A must be the same as the vector type of a column from
1164 if (type_a
->row_type() == type_b
->column_type()) {
1165 /* The resulting matrix has the number of columns of matrix B and
1166 * the number of rows of matrix A. We get the row count of A by
1167 * looking at the size of a vector that makes up a column. The
1168 * transpose (size of a row) is done for B.
1170 const glsl_type
*const type
=
1171 get_instance(type_a
->base_type
,
1172 type_a
->column_type()->vector_elements
,
1173 type_b
->row_type()->vector_elements
);
1174 assert(type
!= error_type
);
1178 } else if (type_a
->is_matrix()) {
1179 /* A is a matrix and B is a column vector. Columns of A must match
1180 * rows of B. Given the other previously tested constraints, this
1181 * means the vector type of a row from A must be the same as the
1182 * vector the type of B.
1184 if (type_a
->row_type() == type_b
) {
1185 /* The resulting vector has a number of elements equal to
1186 * the number of rows of matrix A. */
1187 const glsl_type
*const type
=
1188 get_instance(type_a
->base_type
,
1189 type_a
->column_type()->vector_elements
,
1191 assert(type
!= error_type
);
1196 assert(type_b
->is_matrix());
1198 /* A is a row vector and B is a matrix. Columns of A must match rows
1199 * of B. Given the other previously tested constraints, this means
1200 * the type of A must be the same as the vector type of a column from
1203 if (type_a
== type_b
->column_type()) {
1204 /* The resulting vector has a number of elements equal to
1205 * the number of columns of matrix B. */
1206 const glsl_type
*const type
=
1207 get_instance(type_a
->base_type
,
1208 type_b
->row_type()->vector_elements
,
1210 assert(type
!= error_type
);
1221 glsl_type::field_type(const char *name
) const
1223 if (this->base_type
!= GLSL_TYPE_STRUCT
1224 && this->base_type
!= GLSL_TYPE_INTERFACE
)
1227 for (unsigned i
= 0; i
< this->length
; i
++) {
1228 if (strcmp(name
, this->fields
.structure
[i
].name
) == 0)
1229 return this->fields
.structure
[i
].type
;
1237 glsl_type::field_index(const char *name
) const
1239 if (this->base_type
!= GLSL_TYPE_STRUCT
1240 && this->base_type
!= GLSL_TYPE_INTERFACE
)
1243 for (unsigned i
= 0; i
< this->length
; i
++) {
1244 if (strcmp(name
, this->fields
.structure
[i
].name
) == 0)
1253 glsl_type::component_slots() const
1255 switch (this->base_type
) {
1256 case GLSL_TYPE_UINT
:
1258 case GLSL_TYPE_UINT8
:
1259 case GLSL_TYPE_INT8
:
1260 case GLSL_TYPE_UINT16
:
1261 case GLSL_TYPE_INT16
:
1262 case GLSL_TYPE_FLOAT
:
1263 case GLSL_TYPE_FLOAT16
:
1264 case GLSL_TYPE_BOOL
:
1265 return this->components();
1267 case GLSL_TYPE_DOUBLE
:
1268 case GLSL_TYPE_UINT64
:
1269 case GLSL_TYPE_INT64
:
1270 return 2 * this->components();
1272 case GLSL_TYPE_STRUCT
:
1273 case GLSL_TYPE_INTERFACE
: {
1276 for (unsigned i
= 0; i
< this->length
; i
++)
1277 size
+= this->fields
.structure
[i
].type
->component_slots();
1282 case GLSL_TYPE_ARRAY
:
1283 return this->length
* this->fields
.array
->component_slots();
1285 case GLSL_TYPE_SAMPLER
:
1286 case GLSL_TYPE_IMAGE
:
1289 case GLSL_TYPE_SUBROUTINE
:
1292 case GLSL_TYPE_FUNCTION
:
1293 case GLSL_TYPE_ATOMIC_UINT
:
1294 case GLSL_TYPE_VOID
:
1295 case GLSL_TYPE_ERROR
:
1303 glsl_type::record_location_offset(unsigned length
) const
1305 unsigned offset
= 0;
1306 const glsl_type
*t
= this->without_array();
1307 if (t
->is_record()) {
1308 assert(length
<= t
->length
);
1310 for (unsigned i
= 0; i
< length
; i
++) {
1311 const glsl_type
*st
= t
->fields
.structure
[i
].type
;
1312 const glsl_type
*wa
= st
->without_array();
1313 if (wa
->is_record()) {
1314 unsigned r_offset
= wa
->record_location_offset(wa
->length
);
1315 offset
+= st
->is_array() ?
1316 st
->arrays_of_arrays_size() * r_offset
: r_offset
;
1317 } else if (st
->is_array() && st
->fields
.array
->is_array()) {
1318 unsigned outer_array_size
= st
->length
;
1319 const glsl_type
*base_type
= st
->fields
.array
;
1321 /* For arrays of arrays the outer arrays take up a uniform
1322 * slot for each element. The innermost array elements share a
1323 * single slot so we ignore the innermost array when calculating
1326 while (base_type
->fields
.array
->is_array()) {
1327 outer_array_size
= outer_array_size
* base_type
->length
;
1328 base_type
= base_type
->fields
.array
;
1330 offset
+= outer_array_size
;
1332 /* We dont worry about arrays here because unless the array
1333 * contains a structure or another array it only takes up a single
1344 glsl_type::uniform_locations() const
1348 switch (this->base_type
) {
1349 case GLSL_TYPE_UINT
:
1351 case GLSL_TYPE_FLOAT
:
1352 case GLSL_TYPE_FLOAT16
:
1353 case GLSL_TYPE_DOUBLE
:
1354 case GLSL_TYPE_UINT16
:
1355 case GLSL_TYPE_UINT8
:
1356 case GLSL_TYPE_INT16
:
1357 case GLSL_TYPE_INT8
:
1358 case GLSL_TYPE_UINT64
:
1359 case GLSL_TYPE_INT64
:
1360 case GLSL_TYPE_BOOL
:
1361 case GLSL_TYPE_SAMPLER
:
1362 case GLSL_TYPE_IMAGE
:
1363 case GLSL_TYPE_SUBROUTINE
:
1366 case GLSL_TYPE_STRUCT
:
1367 case GLSL_TYPE_INTERFACE
:
1368 for (unsigned i
= 0; i
< this->length
; i
++)
1369 size
+= this->fields
.structure
[i
].type
->uniform_locations();
1371 case GLSL_TYPE_ARRAY
:
1372 return this->length
* this->fields
.array
->uniform_locations();
1379 glsl_type::varying_count() const
1383 switch (this->base_type
) {
1384 case GLSL_TYPE_UINT
:
1386 case GLSL_TYPE_FLOAT
:
1387 case GLSL_TYPE_FLOAT16
:
1388 case GLSL_TYPE_DOUBLE
:
1389 case GLSL_TYPE_BOOL
:
1390 case GLSL_TYPE_UINT16
:
1391 case GLSL_TYPE_UINT8
:
1392 case GLSL_TYPE_INT16
:
1393 case GLSL_TYPE_INT8
:
1394 case GLSL_TYPE_UINT64
:
1395 case GLSL_TYPE_INT64
:
1398 case GLSL_TYPE_STRUCT
:
1399 case GLSL_TYPE_INTERFACE
:
1400 for (unsigned i
= 0; i
< this->length
; i
++)
1401 size
+= this->fields
.structure
[i
].type
->varying_count();
1403 case GLSL_TYPE_ARRAY
:
1404 /* Don't count innermost array elements */
1405 if (this->without_array()->is_record() ||
1406 this->without_array()->is_interface() ||
1407 this->fields
.array
->is_array())
1408 return this->length
* this->fields
.array
->varying_count();
1410 return this->fields
.array
->varying_count();
1412 assert(!"unsupported varying type");
1418 glsl_type::can_implicitly_convert_to(const glsl_type
*desired
,
1419 _mesa_glsl_parse_state
*state
) const
1421 if (this == desired
)
1424 /* GLSL 1.10 and ESSL do not allow implicit conversions. If there is no
1425 * state, we're doing intra-stage function linking where these checks have
1426 * already been done.
1428 if (state
&& !state
->has_implicit_conversions())
1431 /* There is no conversion among matrix types. */
1432 if (this->matrix_columns
> 1 || desired
->matrix_columns
> 1)
1435 /* Vector size must match. */
1436 if (this->vector_elements
!= desired
->vector_elements
)
1439 /* int and uint can be converted to float. */
1440 if (desired
->is_float() && this->is_integer())
1443 /* With GLSL 4.0, ARB_gpu_shader5, or MESA_shader_integer_functions, int
1444 * can be converted to uint. Note that state may be NULL here, when
1445 * resolving function calls in the linker. By this time, all the
1446 * state-dependent checks have already happened though, so allow anything
1447 * that's allowed in any shader version.
1449 if ((!state
|| state
->is_version(400, 0) || state
->ARB_gpu_shader5_enable
||
1450 state
->MESA_shader_integer_functions_enable
) &&
1451 desired
->base_type
== GLSL_TYPE_UINT
&& this->base_type
== GLSL_TYPE_INT
)
1454 /* No implicit conversions from double. */
1455 if ((!state
|| state
->has_double()) && this->is_double())
1458 /* Conversions from different types to double. */
1459 if ((!state
|| state
->has_double()) && desired
->is_double()) {
1460 if (this->is_float())
1462 if (this->is_integer())
1470 glsl_type::std140_base_alignment(bool row_major
) const
1472 unsigned N
= is_64bit() ? 8 : 4;
1474 /* (1) If the member is a scalar consuming <N> basic machine units, the
1475 * base alignment is <N>.
1477 * (2) If the member is a two- or four-component vector with components
1478 * consuming <N> basic machine units, the base alignment is 2<N> or
1479 * 4<N>, respectively.
1481 * (3) If the member is a three-component vector with components consuming
1482 * <N> basic machine units, the base alignment is 4<N>.
1484 if (this->is_scalar() || this->is_vector()) {
1485 switch (this->vector_elements
) {
1496 /* (4) If the member is an array of scalars or vectors, the base alignment
1497 * and array stride are set to match the base alignment of a single
1498 * array element, according to rules (1), (2), and (3), and rounded up
1499 * to the base alignment of a vec4. The array may have padding at the
1500 * end; the base offset of the member following the array is rounded up
1501 * to the next multiple of the base alignment.
1503 * (6) If the member is an array of <S> column-major matrices with <C>
1504 * columns and <R> rows, the matrix is stored identically to a row of
1505 * <S>*<C> column vectors with <R> components each, according to rule
1508 * (8) If the member is an array of <S> row-major matrices with <C> columns
1509 * and <R> rows, the matrix is stored identically to a row of <S>*<R>
1510 * row vectors with <C> components each, according to rule (4).
1512 * (10) If the member is an array of <S> structures, the <S> elements of
1513 * the array are laid out in order, according to rule (9).
1515 if (this->is_array()) {
1516 if (this->fields
.array
->is_scalar() ||
1517 this->fields
.array
->is_vector() ||
1518 this->fields
.array
->is_matrix()) {
1519 return MAX2(this->fields
.array
->std140_base_alignment(row_major
), 16);
1521 assert(this->fields
.array
->is_record() ||
1522 this->fields
.array
->is_array());
1523 return this->fields
.array
->std140_base_alignment(row_major
);
1527 /* (5) If the member is a column-major matrix with <C> columns and
1528 * <R> rows, the matrix is stored identically to an array of
1529 * <C> column vectors with <R> components each, according to
1532 * (7) If the member is a row-major matrix with <C> columns and <R>
1533 * rows, the matrix is stored identically to an array of <R>
1534 * row vectors with <C> components each, according to rule (4).
1536 if (this->is_matrix()) {
1537 const struct glsl_type
*vec_type
, *array_type
;
1538 int c
= this->matrix_columns
;
1539 int r
= this->vector_elements
;
1542 vec_type
= get_instance(base_type
, c
, 1);
1543 array_type
= glsl_type::get_array_instance(vec_type
, r
);
1545 vec_type
= get_instance(base_type
, r
, 1);
1546 array_type
= glsl_type::get_array_instance(vec_type
, c
);
1549 return array_type
->std140_base_alignment(false);
1552 /* (9) If the member is a structure, the base alignment of the
1553 * structure is <N>, where <N> is the largest base alignment
1554 * value of any of its members, and rounded up to the base
1555 * alignment of a vec4. The individual members of this
1556 * sub-structure are then assigned offsets by applying this set
1557 * of rules recursively, where the base offset of the first
1558 * member of the sub-structure is equal to the aligned offset
1559 * of the structure. The structure may have padding at the end;
1560 * the base offset of the member following the sub-structure is
1561 * rounded up to the next multiple of the base alignment of the
1564 if (this->is_record()) {
1565 unsigned base_alignment
= 16;
1566 for (unsigned i
= 0; i
< this->length
; i
++) {
1567 bool field_row_major
= row_major
;
1568 const enum glsl_matrix_layout matrix_layout
=
1569 glsl_matrix_layout(this->fields
.structure
[i
].matrix_layout
);
1570 if (matrix_layout
== GLSL_MATRIX_LAYOUT_ROW_MAJOR
) {
1571 field_row_major
= true;
1572 } else if (matrix_layout
== GLSL_MATRIX_LAYOUT_COLUMN_MAJOR
) {
1573 field_row_major
= false;
1576 const struct glsl_type
*field_type
= this->fields
.structure
[i
].type
;
1577 base_alignment
= MAX2(base_alignment
,
1578 field_type
->std140_base_alignment(field_row_major
));
1580 return base_alignment
;
1583 assert(!"not reached");
1588 glsl_type::std140_size(bool row_major
) const
1590 unsigned N
= is_64bit() ? 8 : 4;
1592 /* (1) If the member is a scalar consuming <N> basic machine units, the
1593 * base alignment is <N>.
1595 * (2) If the member is a two- or four-component vector with components
1596 * consuming <N> basic machine units, the base alignment is 2<N> or
1597 * 4<N>, respectively.
1599 * (3) If the member is a three-component vector with components consuming
1600 * <N> basic machine units, the base alignment is 4<N>.
1602 if (this->is_scalar() || this->is_vector()) {
1603 return this->vector_elements
* N
;
1606 /* (5) If the member is a column-major matrix with <C> columns and
1607 * <R> rows, the matrix is stored identically to an array of
1608 * <C> column vectors with <R> components each, according to
1611 * (6) If the member is an array of <S> column-major matrices with <C>
1612 * columns and <R> rows, the matrix is stored identically to a row of
1613 * <S>*<C> column vectors with <R> components each, according to rule
1616 * (7) If the member is a row-major matrix with <C> columns and <R>
1617 * rows, the matrix is stored identically to an array of <R>
1618 * row vectors with <C> components each, according to rule (4).
1620 * (8) If the member is an array of <S> row-major matrices with <C> columns
1621 * and <R> rows, the matrix is stored identically to a row of <S>*<R>
1622 * row vectors with <C> components each, according to rule (4).
1624 if (this->without_array()->is_matrix()) {
1625 const struct glsl_type
*element_type
;
1626 const struct glsl_type
*vec_type
;
1627 unsigned int array_len
;
1629 if (this->is_array()) {
1630 element_type
= this->without_array();
1631 array_len
= this->arrays_of_arrays_size();
1633 element_type
= this;
1638 vec_type
= get_instance(element_type
->base_type
,
1639 element_type
->matrix_columns
, 1);
1641 array_len
*= element_type
->vector_elements
;
1643 vec_type
= get_instance(element_type
->base_type
,
1644 element_type
->vector_elements
, 1);
1645 array_len
*= element_type
->matrix_columns
;
1647 const glsl_type
*array_type
= glsl_type::get_array_instance(vec_type
,
1650 return array_type
->std140_size(false);
1653 /* (4) If the member is an array of scalars or vectors, the base alignment
1654 * and array stride are set to match the base alignment of a single
1655 * array element, according to rules (1), (2), and (3), and rounded up
1656 * to the base alignment of a vec4. The array may have padding at the
1657 * end; the base offset of the member following the array is rounded up
1658 * to the next multiple of the base alignment.
1660 * (10) If the member is an array of <S> structures, the <S> elements of
1661 * the array are laid out in order, according to rule (9).
1663 if (this->is_array()) {
1664 if (this->without_array()->is_record()) {
1665 return this->arrays_of_arrays_size() *
1666 this->without_array()->std140_size(row_major
);
1668 unsigned element_base_align
=
1669 this->without_array()->std140_base_alignment(row_major
);
1670 return this->arrays_of_arrays_size() * MAX2(element_base_align
, 16);
1674 /* (9) If the member is a structure, the base alignment of the
1675 * structure is <N>, where <N> is the largest base alignment
1676 * value of any of its members, and rounded up to the base
1677 * alignment of a vec4. The individual members of this
1678 * sub-structure are then assigned offsets by applying this set
1679 * of rules recursively, where the base offset of the first
1680 * member of the sub-structure is equal to the aligned offset
1681 * of the structure. The structure may have padding at the end;
1682 * the base offset of the member following the sub-structure is
1683 * rounded up to the next multiple of the base alignment of the
1686 if (this->is_record() || this->is_interface()) {
1688 unsigned max_align
= 0;
1690 for (unsigned i
= 0; i
< this->length
; i
++) {
1691 bool field_row_major
= row_major
;
1692 const enum glsl_matrix_layout matrix_layout
=
1693 glsl_matrix_layout(this->fields
.structure
[i
].matrix_layout
);
1694 if (matrix_layout
== GLSL_MATRIX_LAYOUT_ROW_MAJOR
) {
1695 field_row_major
= true;
1696 } else if (matrix_layout
== GLSL_MATRIX_LAYOUT_COLUMN_MAJOR
) {
1697 field_row_major
= false;
1700 const struct glsl_type
*field_type
= this->fields
.structure
[i
].type
;
1701 unsigned align
= field_type
->std140_base_alignment(field_row_major
);
1703 /* Ignore unsized arrays when calculating size */
1704 if (field_type
->is_unsized_array())
1707 size
= glsl_align(size
, align
);
1708 size
+= field_type
->std140_size(field_row_major
);
1710 max_align
= MAX2(align
, max_align
);
1712 if (field_type
->is_record() && (i
+ 1 < this->length
))
1713 size
= glsl_align(size
, 16);
1715 size
= glsl_align(size
, MAX2(max_align
, 16));
1719 assert(!"not reached");
1724 glsl_type::std430_base_alignment(bool row_major
) const
1727 unsigned N
= is_64bit() ? 8 : 4;
1729 /* (1) If the member is a scalar consuming <N> basic machine units, the
1730 * base alignment is <N>.
1732 * (2) If the member is a two- or four-component vector with components
1733 * consuming <N> basic machine units, the base alignment is 2<N> or
1734 * 4<N>, respectively.
1736 * (3) If the member is a three-component vector with components consuming
1737 * <N> basic machine units, the base alignment is 4<N>.
1739 if (this->is_scalar() || this->is_vector()) {
1740 switch (this->vector_elements
) {
1751 /* OpenGL 4.30 spec, section 7.6.2.2 "Standard Uniform Block Layout":
1753 * "When using the std430 storage layout, shader storage blocks will be
1754 * laid out in buffer storage identically to uniform and shader storage
1755 * blocks using the std140 layout, except that the base alignment and
1756 * stride of arrays of scalars and vectors in rule 4 and of structures
1757 * in rule 9 are not rounded up a multiple of the base alignment of a vec4.
1760 /* (1) If the member is a scalar consuming <N> basic machine units, the
1761 * base alignment is <N>.
1763 * (2) If the member is a two- or four-component vector with components
1764 * consuming <N> basic machine units, the base alignment is 2<N> or
1765 * 4<N>, respectively.
1767 * (3) If the member is a three-component vector with components consuming
1768 * <N> basic machine units, the base alignment is 4<N>.
1770 if (this->is_array())
1771 return this->fields
.array
->std430_base_alignment(row_major
);
1773 /* (5) If the member is a column-major matrix with <C> columns and
1774 * <R> rows, the matrix is stored identically to an array of
1775 * <C> column vectors with <R> components each, according to
1778 * (7) If the member is a row-major matrix with <C> columns and <R>
1779 * rows, the matrix is stored identically to an array of <R>
1780 * row vectors with <C> components each, according to rule (4).
1782 if (this->is_matrix()) {
1783 const struct glsl_type
*vec_type
, *array_type
;
1784 int c
= this->matrix_columns
;
1785 int r
= this->vector_elements
;
1788 vec_type
= get_instance(base_type
, c
, 1);
1789 array_type
= glsl_type::get_array_instance(vec_type
, r
);
1791 vec_type
= get_instance(base_type
, r
, 1);
1792 array_type
= glsl_type::get_array_instance(vec_type
, c
);
1795 return array_type
->std430_base_alignment(false);
1798 /* (9) If the member is a structure, the base alignment of the
1799 * structure is <N>, where <N> is the largest base alignment
1800 * value of any of its members, and rounded up to the base
1801 * alignment of a vec4. The individual members of this
1802 * sub-structure are then assigned offsets by applying this set
1803 * of rules recursively, where the base offset of the first
1804 * member of the sub-structure is equal to the aligned offset
1805 * of the structure. The structure may have padding at the end;
1806 * the base offset of the member following the sub-structure is
1807 * rounded up to the next multiple of the base alignment of the
1810 if (this->is_record()) {
1811 unsigned base_alignment
= 0;
1812 for (unsigned i
= 0; i
< this->length
; i
++) {
1813 bool field_row_major
= row_major
;
1814 const enum glsl_matrix_layout matrix_layout
=
1815 glsl_matrix_layout(this->fields
.structure
[i
].matrix_layout
);
1816 if (matrix_layout
== GLSL_MATRIX_LAYOUT_ROW_MAJOR
) {
1817 field_row_major
= true;
1818 } else if (matrix_layout
== GLSL_MATRIX_LAYOUT_COLUMN_MAJOR
) {
1819 field_row_major
= false;
1822 const struct glsl_type
*field_type
= this->fields
.structure
[i
].type
;
1823 base_alignment
= MAX2(base_alignment
,
1824 field_type
->std430_base_alignment(field_row_major
));
1826 assert(base_alignment
> 0);
1827 return base_alignment
;
1829 assert(!"not reached");
1834 glsl_type::std430_array_stride(bool row_major
) const
1836 unsigned N
= is_64bit() ? 8 : 4;
1838 /* Notice that the array stride of a vec3 is not 3 * N but 4 * N.
1839 * See OpenGL 4.30 spec, section 7.6.2.2 "Standard Uniform Block Layout"
1841 * (3) If the member is a three-component vector with components consuming
1842 * <N> basic machine units, the base alignment is 4<N>.
1844 if (this->is_vector() && this->vector_elements
== 3)
1847 /* By default use std430_size(row_major) */
1848 return this->std430_size(row_major
);
1852 glsl_type::std430_size(bool row_major
) const
1854 unsigned N
= is_64bit() ? 8 : 4;
1856 /* OpenGL 4.30 spec, section 7.6.2.2 "Standard Uniform Block Layout":
1858 * "When using the std430 storage layout, shader storage blocks will be
1859 * laid out in buffer storage identically to uniform and shader storage
1860 * blocks using the std140 layout, except that the base alignment and
1861 * stride of arrays of scalars and vectors in rule 4 and of structures
1862 * in rule 9 are not rounded up a multiple of the base alignment of a vec4.
1864 if (this->is_scalar() || this->is_vector())
1865 return this->vector_elements
* N
;
1867 if (this->without_array()->is_matrix()) {
1868 const struct glsl_type
*element_type
;
1869 const struct glsl_type
*vec_type
;
1870 unsigned int array_len
;
1872 if (this->is_array()) {
1873 element_type
= this->without_array();
1874 array_len
= this->arrays_of_arrays_size();
1876 element_type
= this;
1881 vec_type
= get_instance(element_type
->base_type
,
1882 element_type
->matrix_columns
, 1);
1884 array_len
*= element_type
->vector_elements
;
1886 vec_type
= get_instance(element_type
->base_type
,
1887 element_type
->vector_elements
, 1);
1888 array_len
*= element_type
->matrix_columns
;
1890 const glsl_type
*array_type
= glsl_type::get_array_instance(vec_type
,
1893 return array_type
->std430_size(false);
1896 if (this->is_array()) {
1897 if (this->without_array()->is_record())
1898 return this->arrays_of_arrays_size() *
1899 this->without_array()->std430_size(row_major
);
1901 return this->arrays_of_arrays_size() *
1902 this->without_array()->std430_base_alignment(row_major
);
1905 if (this->is_record() || this->is_interface()) {
1907 unsigned max_align
= 0;
1909 for (unsigned i
= 0; i
< this->length
; i
++) {
1910 bool field_row_major
= row_major
;
1911 const enum glsl_matrix_layout matrix_layout
=
1912 glsl_matrix_layout(this->fields
.structure
[i
].matrix_layout
);
1913 if (matrix_layout
== GLSL_MATRIX_LAYOUT_ROW_MAJOR
) {
1914 field_row_major
= true;
1915 } else if (matrix_layout
== GLSL_MATRIX_LAYOUT_COLUMN_MAJOR
) {
1916 field_row_major
= false;
1919 const struct glsl_type
*field_type
= this->fields
.structure
[i
].type
;
1920 unsigned align
= field_type
->std430_base_alignment(field_row_major
);
1921 size
= glsl_align(size
, align
);
1922 size
+= field_type
->std430_size(field_row_major
);
1924 max_align
= MAX2(align
, max_align
);
1926 size
= glsl_align(size
, max_align
);
1930 assert(!"not reached");
1935 glsl_type::count_attribute_slots(bool is_vertex_input
) const
1937 /* From page 31 (page 37 of the PDF) of the GLSL 1.50 spec:
1939 * "A scalar input counts the same amount against this limit as a vec4,
1940 * so applications may want to consider packing groups of four
1941 * unrelated float inputs together into a vector to better utilize the
1942 * capabilities of the underlying hardware. A matrix input will use up
1943 * multiple locations. The number of locations used will equal the
1944 * number of columns in the matrix."
1946 * The spec does not explicitly say how arrays are counted. However, it
1947 * should be safe to assume the total number of slots consumed by an array
1948 * is the number of entries in the array multiplied by the number of slots
1949 * consumed by a single element of the array.
1951 * The spec says nothing about how structs are counted, because vertex
1952 * attributes are not allowed to be (or contain) structs. However, Mesa
1953 * allows varying structs, the number of varying slots taken up by a
1954 * varying struct is simply equal to the sum of the number of slots taken
1955 * up by each element.
1957 * Doubles are counted different depending on whether they are vertex
1958 * inputs or everything else. Vertex inputs from ARB_vertex_attrib_64bit
1959 * take one location no matter what size they are, otherwise dvec3/4
1960 * take two locations.
1962 switch (this->base_type
) {
1963 case GLSL_TYPE_UINT
:
1965 case GLSL_TYPE_UINT8
:
1966 case GLSL_TYPE_INT8
:
1967 case GLSL_TYPE_UINT16
:
1968 case GLSL_TYPE_INT16
:
1969 case GLSL_TYPE_FLOAT
:
1970 case GLSL_TYPE_FLOAT16
:
1971 case GLSL_TYPE_BOOL
:
1972 case GLSL_TYPE_SAMPLER
:
1973 case GLSL_TYPE_IMAGE
:
1974 return this->matrix_columns
;
1975 case GLSL_TYPE_DOUBLE
:
1976 case GLSL_TYPE_UINT64
:
1977 case GLSL_TYPE_INT64
:
1978 if (this->vector_elements
> 2 && !is_vertex_input
)
1979 return this->matrix_columns
* 2;
1981 return this->matrix_columns
;
1982 case GLSL_TYPE_STRUCT
:
1983 case GLSL_TYPE_INTERFACE
: {
1986 for (unsigned i
= 0; i
< this->length
; i
++)
1987 size
+= this->fields
.structure
[i
].type
->count_attribute_slots(is_vertex_input
);
1992 case GLSL_TYPE_ARRAY
:
1993 return this->length
* this->fields
.array
->count_attribute_slots(is_vertex_input
);
1995 case GLSL_TYPE_SUBROUTINE
:
1998 case GLSL_TYPE_FUNCTION
:
1999 case GLSL_TYPE_ATOMIC_UINT
:
2000 case GLSL_TYPE_VOID
:
2001 case GLSL_TYPE_ERROR
:
2005 assert(!"Unexpected type in count_attribute_slots()");
2011 glsl_type::coordinate_components() const
2015 switch (sampler_dimensionality
) {
2016 case GLSL_SAMPLER_DIM_1D
:
2017 case GLSL_SAMPLER_DIM_BUF
:
2020 case GLSL_SAMPLER_DIM_2D
:
2021 case GLSL_SAMPLER_DIM_RECT
:
2022 case GLSL_SAMPLER_DIM_MS
:
2023 case GLSL_SAMPLER_DIM_EXTERNAL
:
2024 case GLSL_SAMPLER_DIM_SUBPASS
:
2027 case GLSL_SAMPLER_DIM_3D
:
2028 case GLSL_SAMPLER_DIM_CUBE
:
2032 assert(!"Should not get here.");
2037 /* Array textures need an additional component for the array index, except
2038 * for cubemap array images that behave like a 2D array of interleaved
2041 if (sampler_array
&&
2042 !(is_image() && sampler_dimensionality
== GLSL_SAMPLER_DIM_CUBE
))
2049 * Declarations of type flyweights (glsl_type::_foo_type) and
2050 * convenience pointers (glsl_type::foo_type).
2053 #define DECL_TYPE(NAME, ...) \
2054 const glsl_type glsl_type::_##NAME##_type = glsl_type(__VA_ARGS__, #NAME); \
2055 const glsl_type *const glsl_type::NAME##_type = &glsl_type::_##NAME##_type;
2057 #define STRUCT_TYPE(NAME)
2059 #include "compiler/builtin_type_macros.h"
2063 get_struct_type_field_and_pointer_sizes(size_t *s_field_size
,
2064 size_t *s_field_ptrs
)
2066 *s_field_size
= sizeof(glsl_struct_field
);
2068 sizeof(((glsl_struct_field
*)0)->type
) +
2069 sizeof(((glsl_struct_field
*)0)->name
);
2073 encode_type_to_blob(struct blob
*blob
, const glsl_type
*type
)
2078 blob_write_uint32(blob
, 0);
2082 switch (type
->base_type
) {
2083 case GLSL_TYPE_UINT
:
2085 case GLSL_TYPE_FLOAT
:
2086 case GLSL_TYPE_BOOL
:
2087 case GLSL_TYPE_DOUBLE
:
2088 case GLSL_TYPE_UINT64
:
2089 case GLSL_TYPE_INT64
:
2090 encoding
= (type
->base_type
<< 24) |
2091 (type
->vector_elements
<< 4) |
2092 (type
->matrix_columns
);
2094 case GLSL_TYPE_SAMPLER
:
2095 encoding
= (type
->base_type
) << 24 |
2096 (type
->sampler_dimensionality
<< 4) |
2097 (type
->sampler_shadow
<< 3) |
2098 (type
->sampler_array
<< 2) |
2099 (type
->sampled_type
);
2101 case GLSL_TYPE_SUBROUTINE
:
2102 encoding
= type
->base_type
<< 24;
2103 blob_write_uint32(blob
, encoding
);
2104 blob_write_string(blob
, type
->name
);
2106 case GLSL_TYPE_IMAGE
:
2107 encoding
= (type
->base_type
) << 24 |
2108 (type
->sampler_dimensionality
<< 3) |
2109 (type
->sampler_array
<< 2) |
2110 (type
->sampled_type
);
2112 case GLSL_TYPE_ATOMIC_UINT
:
2113 encoding
= (type
->base_type
<< 24);
2115 case GLSL_TYPE_ARRAY
:
2116 blob_write_uint32(blob
, (type
->base_type
) << 24);
2117 blob_write_uint32(blob
, type
->length
);
2118 encode_type_to_blob(blob
, type
->fields
.array
);
2120 case GLSL_TYPE_STRUCT
:
2121 case GLSL_TYPE_INTERFACE
:
2122 blob_write_uint32(blob
, (type
->base_type
) << 24);
2123 blob_write_string(blob
, type
->name
);
2124 blob_write_uint32(blob
, type
->length
);
2126 size_t s_field_size
, s_field_ptrs
;
2127 get_struct_type_field_and_pointer_sizes(&s_field_size
, &s_field_ptrs
);
2129 for (unsigned i
= 0; i
< type
->length
; i
++) {
2130 encode_type_to_blob(blob
, type
->fields
.structure
[i
].type
);
2131 blob_write_string(blob
, type
->fields
.structure
[i
].name
);
2133 /* Write the struct field skipping the pointers */
2134 blob_write_bytes(blob
,
2135 ((char *)&type
->fields
.structure
[i
]) + s_field_ptrs
,
2136 s_field_size
- s_field_ptrs
);
2139 if (type
->is_interface()) {
2140 blob_write_uint32(blob
, type
->interface_packing
);
2141 blob_write_uint32(blob
, type
->interface_row_major
);
2144 case GLSL_TYPE_VOID
:
2145 encoding
= (type
->base_type
<< 24);
2147 case GLSL_TYPE_ERROR
:
2149 assert(!"Cannot encode type!");
2154 blob_write_uint32(blob
, encoding
);
2158 decode_type_from_blob(struct blob_reader
*blob
)
2160 uint32_t u
= blob_read_uint32(blob
);
2166 glsl_base_type base_type
= (glsl_base_type
) (u
>> 24);
2168 switch (base_type
) {
2169 case GLSL_TYPE_UINT
:
2171 case GLSL_TYPE_FLOAT
:
2172 case GLSL_TYPE_BOOL
:
2173 case GLSL_TYPE_DOUBLE
:
2174 case GLSL_TYPE_UINT64
:
2175 case GLSL_TYPE_INT64
:
2176 return glsl_type::get_instance(base_type
, (u
>> 4) & 0x0f, u
& 0x0f);
2177 case GLSL_TYPE_SAMPLER
:
2178 return glsl_type::get_sampler_instance((enum glsl_sampler_dim
) ((u
>> 4) & 0x07),
2181 (glsl_base_type
) ((u
>> 0) & 0x03));
2182 case GLSL_TYPE_SUBROUTINE
:
2183 return glsl_type::get_subroutine_instance(blob_read_string(blob
));
2184 case GLSL_TYPE_IMAGE
:
2185 return glsl_type::get_image_instance((enum glsl_sampler_dim
) ((u
>> 3) & 0x07),
2187 (glsl_base_type
) ((u
>> 0) & 0x03));
2188 case GLSL_TYPE_ATOMIC_UINT
:
2189 return glsl_type::atomic_uint_type
;
2190 case GLSL_TYPE_ARRAY
: {
2191 unsigned length
= blob_read_uint32(blob
);
2192 return glsl_type::get_array_instance(decode_type_from_blob(blob
),
2195 case GLSL_TYPE_STRUCT
:
2196 case GLSL_TYPE_INTERFACE
: {
2197 char *name
= blob_read_string(blob
);
2198 unsigned num_fields
= blob_read_uint32(blob
);
2200 size_t s_field_size
, s_field_ptrs
;
2201 get_struct_type_field_and_pointer_sizes(&s_field_size
, &s_field_ptrs
);
2203 glsl_struct_field
*fields
=
2204 (glsl_struct_field
*) malloc(s_field_size
* num_fields
);
2205 for (unsigned i
= 0; i
< num_fields
; i
++) {
2206 fields
[i
].type
= decode_type_from_blob(blob
);
2207 fields
[i
].name
= blob_read_string(blob
);
2209 blob_copy_bytes(blob
, ((uint8_t *) &fields
[i
]) + s_field_ptrs
,
2210 s_field_size
- s_field_ptrs
);
2214 if (base_type
== GLSL_TYPE_INTERFACE
) {
2215 enum glsl_interface_packing packing
=
2216 (glsl_interface_packing
) blob_read_uint32(blob
);
2217 bool row_major
= blob_read_uint32(blob
);
2218 t
= glsl_type::get_interface_instance(fields
, num_fields
, packing
,
2221 t
= glsl_type::get_record_instance(fields
, num_fields
, name
);
2227 case GLSL_TYPE_VOID
:
2228 return glsl_type::void_type
;
2229 case GLSL_TYPE_ERROR
:
2231 assert(!"Cannot decode type!");