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"
31 mtx_t
glsl_type::mutex
= _MTX_INITIALIZER_NP
;
32 hash_table
*glsl_type::array_types
= NULL
;
33 hash_table
*glsl_type::record_types
= NULL
;
34 hash_table
*glsl_type::interface_types
= NULL
;
35 hash_table
*glsl_type::function_types
= NULL
;
36 hash_table
*glsl_type::subroutine_types
= NULL
;
37 void *glsl_type::mem_ctx
= NULL
;
40 glsl_type::init_ralloc_type_ctx(void)
42 if (glsl_type::mem_ctx
== NULL
) {
43 glsl_type::mem_ctx
= ralloc_context(NULL
);
44 assert(glsl_type::mem_ctx
!= NULL
);
48 glsl_type::glsl_type(GLenum gl_type
,
49 glsl_base_type base_type
, unsigned vector_elements
,
50 unsigned matrix_columns
, const char *name
) :
53 sampler_dimensionality(0), sampler_shadow(0), sampler_array(0),
54 sampled_type(0), interface_packing(0), interface_row_major(0),
55 vector_elements(vector_elements
), matrix_columns(matrix_columns
),
58 /* Values of these types must fit in the two bits of
59 * glsl_type::sampled_type.
61 STATIC_ASSERT((unsigned(GLSL_TYPE_UINT
) & 3) == unsigned(GLSL_TYPE_UINT
));
62 STATIC_ASSERT((unsigned(GLSL_TYPE_INT
) & 3) == unsigned(GLSL_TYPE_INT
));
63 STATIC_ASSERT((unsigned(GLSL_TYPE_FLOAT
) & 3) == unsigned(GLSL_TYPE_FLOAT
));
65 mtx_lock(&glsl_type::mutex
);
67 init_ralloc_type_ctx();
69 this->name
= ralloc_strdup(this->mem_ctx
, name
);
71 mtx_unlock(&glsl_type::mutex
);
73 /* Neither dimension is zero or both dimensions are zero.
75 assert((vector_elements
== 0) == (matrix_columns
== 0));
76 memset(& fields
, 0, sizeof(fields
));
79 glsl_type::glsl_type(GLenum gl_type
, glsl_base_type base_type
,
80 enum glsl_sampler_dim dim
, bool shadow
, bool array
,
81 unsigned type
, const char *name
) :
84 sampler_dimensionality(dim
), sampler_shadow(shadow
),
85 sampler_array(array
), sampled_type(type
), interface_packing(0),
86 interface_row_major(0), length(0)
88 mtx_lock(&glsl_type::mutex
);
90 init_ralloc_type_ctx();
92 this->name
= ralloc_strdup(this->mem_ctx
, name
);
94 mtx_unlock(&glsl_type::mutex
);
96 memset(& fields
, 0, sizeof(fields
));
98 matrix_columns
= vector_elements
= 1;
101 glsl_type::glsl_type(const glsl_struct_field
*fields
, unsigned num_fields
,
104 base_type(GLSL_TYPE_STRUCT
),
105 sampler_dimensionality(0), sampler_shadow(0), sampler_array(0),
106 sampled_type(0), interface_packing(0), interface_row_major(0),
107 vector_elements(0), matrix_columns(0),
112 mtx_lock(&glsl_type::mutex
);
114 init_ralloc_type_ctx();
115 assert(name
!= NULL
);
116 this->name
= ralloc_strdup(this->mem_ctx
, name
);
117 this->fields
.structure
= ralloc_array(this->mem_ctx
,
118 glsl_struct_field
, length
);
120 for (i
= 0; i
< length
; i
++) {
121 this->fields
.structure
[i
] = fields
[i
];
122 this->fields
.structure
[i
].name
= ralloc_strdup(this->fields
.structure
,
126 mtx_unlock(&glsl_type::mutex
);
129 glsl_type::glsl_type(const glsl_struct_field
*fields
, unsigned num_fields
,
130 enum glsl_interface_packing packing
,
131 bool row_major
, const char *name
) :
133 base_type(GLSL_TYPE_INTERFACE
),
134 sampler_dimensionality(0), sampler_shadow(0), sampler_array(0),
135 sampled_type(0), interface_packing((unsigned) packing
),
136 interface_row_major((unsigned) row_major
),
137 vector_elements(0), matrix_columns(0),
142 mtx_lock(&glsl_type::mutex
);
144 init_ralloc_type_ctx();
145 assert(name
!= NULL
);
146 this->name
= ralloc_strdup(this->mem_ctx
, name
);
147 this->fields
.structure
= rzalloc_array(this->mem_ctx
,
148 glsl_struct_field
, length
);
149 for (i
= 0; i
< length
; i
++) {
150 this->fields
.structure
[i
] = fields
[i
];
151 this->fields
.structure
[i
].name
= ralloc_strdup(this->fields
.structure
,
155 mtx_unlock(&glsl_type::mutex
);
158 glsl_type::glsl_type(const glsl_type
*return_type
,
159 const glsl_function_param
*params
, unsigned num_params
) :
161 base_type(GLSL_TYPE_FUNCTION
),
162 sampler_dimensionality(0), sampler_shadow(0), sampler_array(0),
163 sampled_type(0), interface_packing(0), interface_row_major(0),
164 vector_elements(0), matrix_columns(0),
169 mtx_lock(&glsl_type::mutex
);
171 init_ralloc_type_ctx();
173 this->fields
.parameters
= rzalloc_array(this->mem_ctx
,
174 glsl_function_param
, num_params
+ 1);
176 /* We store the return type as the first parameter */
177 this->fields
.parameters
[0].type
= return_type
;
178 this->fields
.parameters
[0].in
= false;
179 this->fields
.parameters
[0].out
= true;
181 /* We store the i'th parameter in slot i+1 */
182 for (i
= 0; i
< length
; i
++) {
183 this->fields
.parameters
[i
+ 1].type
= params
[i
].type
;
184 this->fields
.parameters
[i
+ 1].in
= params
[i
].in
;
185 this->fields
.parameters
[i
+ 1].out
= params
[i
].out
;
188 mtx_unlock(&glsl_type::mutex
);
191 glsl_type::glsl_type(const char *subroutine_name
) :
193 base_type(GLSL_TYPE_SUBROUTINE
),
194 sampler_dimensionality(0), sampler_shadow(0), sampler_array(0),
195 sampled_type(0), interface_packing(0), interface_row_major(0),
196 vector_elements(1), matrix_columns(1),
199 mtx_lock(&glsl_type::mutex
);
201 init_ralloc_type_ctx();
202 assert(subroutine_name
!= NULL
);
203 this->name
= ralloc_strdup(this->mem_ctx
, subroutine_name
);
204 mtx_unlock(&glsl_type::mutex
);
208 glsl_type::contains_sampler() const
210 if (this->is_array()) {
211 return this->fields
.array
->contains_sampler();
212 } else if (this->is_record() || this->is_interface()) {
213 for (unsigned int i
= 0; i
< this->length
; i
++) {
214 if (this->fields
.structure
[i
].type
->contains_sampler())
219 return this->is_sampler();
224 glsl_type::contains_array() const
226 if (this->is_record() || this->is_interface()) {
227 for (unsigned int i
= 0; i
< this->length
; i
++) {
228 if (this->fields
.structure
[i
].type
->contains_array())
233 return this->is_array();
238 glsl_type::contains_integer() const
240 if (this->is_array()) {
241 return this->fields
.array
->contains_integer();
242 } else if (this->is_record() || this->is_interface()) {
243 for (unsigned int i
= 0; i
< this->length
; i
++) {
244 if (this->fields
.structure
[i
].type
->contains_integer())
249 return this->is_integer();
254 glsl_type::contains_double() const
256 if (this->is_array()) {
257 return this->fields
.array
->contains_double();
258 } else if (this->is_record() || this->is_interface()) {
259 for (unsigned int i
= 0; i
< this->length
; i
++) {
260 if (this->fields
.structure
[i
].type
->contains_double())
265 return this->is_double();
270 glsl_type::contains_opaque() const {
272 case GLSL_TYPE_SAMPLER
:
273 case GLSL_TYPE_IMAGE
:
274 case GLSL_TYPE_ATOMIC_UINT
:
276 case GLSL_TYPE_ARRAY
:
277 return fields
.array
->contains_opaque();
278 case GLSL_TYPE_STRUCT
:
279 case GLSL_TYPE_INTERFACE
:
280 for (unsigned int i
= 0; i
< length
; i
++) {
281 if (fields
.structure
[i
].type
->contains_opaque())
291 glsl_type::contains_subroutine() const
293 if (this->is_array()) {
294 return this->fields
.array
->contains_subroutine();
295 } else if (this->is_record() || this->is_interface()) {
296 for (unsigned int i
= 0; i
< this->length
; i
++) {
297 if (this->fields
.structure
[i
].type
->contains_subroutine())
302 return this->is_subroutine();
307 glsl_type::sampler_index() const
309 const glsl_type
*const t
= (this->is_array()) ? this->fields
.array
: this;
311 assert(t
->is_sampler() || t
->is_image());
313 switch (t
->sampler_dimensionality
) {
314 case GLSL_SAMPLER_DIM_1D
:
315 return (t
->sampler_array
) ? TEXTURE_1D_ARRAY_INDEX
: TEXTURE_1D_INDEX
;
316 case GLSL_SAMPLER_DIM_2D
:
317 return (t
->sampler_array
) ? TEXTURE_2D_ARRAY_INDEX
: TEXTURE_2D_INDEX
;
318 case GLSL_SAMPLER_DIM_3D
:
319 return TEXTURE_3D_INDEX
;
320 case GLSL_SAMPLER_DIM_CUBE
:
321 return (t
->sampler_array
) ? TEXTURE_CUBE_ARRAY_INDEX
: TEXTURE_CUBE_INDEX
;
322 case GLSL_SAMPLER_DIM_RECT
:
323 return TEXTURE_RECT_INDEX
;
324 case GLSL_SAMPLER_DIM_BUF
:
325 return TEXTURE_BUFFER_INDEX
;
326 case GLSL_SAMPLER_DIM_EXTERNAL
:
327 return TEXTURE_EXTERNAL_INDEX
;
328 case GLSL_SAMPLER_DIM_MS
:
329 return (t
->sampler_array
) ? TEXTURE_2D_MULTISAMPLE_ARRAY_INDEX
: TEXTURE_2D_MULTISAMPLE_INDEX
;
331 assert(!"Should not get here.");
332 return TEXTURE_BUFFER_INDEX
;
337 glsl_type::contains_image() const
339 if (this->is_array()) {
340 return this->fields
.array
->contains_image();
341 } else if (this->is_record() || this->is_interface()) {
342 for (unsigned int i
= 0; i
< this->length
; i
++) {
343 if (this->fields
.structure
[i
].type
->contains_image())
348 return this->is_image();
352 const glsl_type
*glsl_type::get_base_type() const
359 case GLSL_TYPE_FLOAT
:
361 case GLSL_TYPE_DOUBLE
:
365 case GLSL_TYPE_UINT64
:
366 return uint64_t_type
;
367 case GLSL_TYPE_INT64
:
375 const glsl_type
*glsl_type::get_scalar_type() const
377 const glsl_type
*type
= this;
380 while (type
->base_type
== GLSL_TYPE_ARRAY
)
381 type
= type
->fields
.array
;
383 /* Handle vectors and matrices */
384 switch (type
->base_type
) {
389 case GLSL_TYPE_FLOAT
:
391 case GLSL_TYPE_DOUBLE
:
395 case GLSL_TYPE_UINT64
:
396 return uint64_t_type
;
397 case GLSL_TYPE_INT64
:
400 /* Handle everything else */
407 _mesa_glsl_release_types(void)
409 /* Should only be called during atexit (either when unloading shared
410 * object, or if process terminates), so no mutex-locking should be
413 if (glsl_type::array_types
!= NULL
) {
414 _mesa_hash_table_destroy(glsl_type::array_types
, NULL
);
415 glsl_type::array_types
= NULL
;
418 if (glsl_type::record_types
!= NULL
) {
419 _mesa_hash_table_destroy(glsl_type::record_types
, NULL
);
420 glsl_type::record_types
= NULL
;
423 if (glsl_type::interface_types
!= NULL
) {
424 _mesa_hash_table_destroy(glsl_type::interface_types
, NULL
);
425 glsl_type::interface_types
= NULL
;
428 if (glsl_type::function_types
!= NULL
) {
429 _mesa_hash_table_destroy(glsl_type::function_types
, NULL
);
430 glsl_type::function_types
= NULL
;
433 if (glsl_type::subroutine_types
!= NULL
) {
434 _mesa_hash_table_destroy(glsl_type::subroutine_types
, NULL
);
435 glsl_type::subroutine_types
= NULL
;
438 ralloc_free(glsl_type::mem_ctx
);
439 glsl_type::mem_ctx
= NULL
;
443 glsl_type::glsl_type(const glsl_type
*array
, unsigned length
) :
444 base_type(GLSL_TYPE_ARRAY
),
445 sampler_dimensionality(0), sampler_shadow(0), sampler_array(0),
446 sampled_type(0), interface_packing(0), interface_row_major(0),
447 vector_elements(0), matrix_columns(0),
448 length(length
), name(NULL
)
450 this->fields
.array
= array
;
451 /* Inherit the gl type of the base. The GL type is used for
452 * uniform/statevar handling in Mesa and the arrayness of the type
453 * is represented by the size rather than the type.
455 this->gl_type
= array
->gl_type
;
457 /* Allow a maximum of 10 characters for the array size. This is enough
458 * for 32-bits of ~0. The extra 3 are for the '[', ']', and terminating
461 const unsigned name_length
= strlen(array
->name
) + 10 + 3;
463 mtx_lock(&glsl_type::mutex
);
464 char *const n
= (char *) ralloc_size(this->mem_ctx
, name_length
);
465 mtx_unlock(&glsl_type::mutex
);
468 snprintf(n
, name_length
, "%s[]", array
->name
);
470 /* insert outermost dimensions in the correct spot
471 * otherwise the dimension order will be backwards
473 const char *pos
= strchr(array
->name
, '[');
475 int idx
= pos
- array
->name
;
476 snprintf(n
, idx
+1, "%s", array
->name
);
477 snprintf(n
+ idx
, name_length
- idx
, "[%u]%s",
478 length
, array
->name
+ idx
);
480 snprintf(n
, name_length
, "%s[%u]", array
->name
, length
);
489 glsl_type::vec(unsigned components
)
491 if (components
== 0 || components
> 4)
494 static const glsl_type
*const ts
[] = {
495 float_type
, vec2_type
, vec3_type
, vec4_type
497 return ts
[components
- 1];
501 glsl_type::dvec(unsigned components
)
503 if (components
== 0 || components
> 4)
506 static const glsl_type
*const ts
[] = {
507 double_type
, dvec2_type
, dvec3_type
, dvec4_type
509 return ts
[components
- 1];
513 glsl_type::ivec(unsigned components
)
515 if (components
== 0 || components
> 4)
518 static const glsl_type
*const ts
[] = {
519 int_type
, ivec2_type
, ivec3_type
, ivec4_type
521 return ts
[components
- 1];
526 glsl_type::uvec(unsigned components
)
528 if (components
== 0 || components
> 4)
531 static const glsl_type
*const ts
[] = {
532 uint_type
, uvec2_type
, uvec3_type
, uvec4_type
534 return ts
[components
- 1];
539 glsl_type::bvec(unsigned components
)
541 if (components
== 0 || components
> 4)
544 static const glsl_type
*const ts
[] = {
545 bool_type
, bvec2_type
, bvec3_type
, bvec4_type
547 return ts
[components
- 1];
552 glsl_type::i64vec(unsigned components
)
554 if (components
== 0 || components
> 4)
557 static const glsl_type
*const ts
[] = {
558 int64_t_type
, i64vec2_type
, i64vec3_type
, i64vec4_type
560 return ts
[components
- 1];
565 glsl_type::u64vec(unsigned components
)
567 if (components
== 0 || components
> 4)
570 static const glsl_type
*const ts
[] = {
571 uint64_t_type
, u64vec2_type
, u64vec3_type
, u64vec4_type
573 return ts
[components
- 1];
577 glsl_type::get_instance(unsigned base_type
, unsigned rows
, unsigned columns
)
579 if (base_type
== GLSL_TYPE_VOID
)
582 if ((rows
< 1) || (rows
> 4) || (columns
< 1) || (columns
> 4))
585 /* Treat GLSL vectors as Nx1 matrices.
593 case GLSL_TYPE_FLOAT
:
595 case GLSL_TYPE_DOUBLE
:
599 case GLSL_TYPE_UINT64
:
601 case GLSL_TYPE_INT64
:
607 if ((base_type
!= GLSL_TYPE_FLOAT
&& base_type
!= GLSL_TYPE_DOUBLE
) || (rows
== 1))
610 /* GLSL matrix types are named mat{COLUMNS}x{ROWS}. Only the following
611 * combinations are valid:
619 #define IDX(c,r) (((c-1)*3) + (r-1))
621 if (base_type
== GLSL_TYPE_DOUBLE
) {
622 switch (IDX(columns
, rows
)) {
623 case IDX(2,2): return dmat2_type
;
624 case IDX(2,3): return dmat2x3_type
;
625 case IDX(2,4): return dmat2x4_type
;
626 case IDX(3,2): return dmat3x2_type
;
627 case IDX(3,3): return dmat3_type
;
628 case IDX(3,4): return dmat3x4_type
;
629 case IDX(4,2): return dmat4x2_type
;
630 case IDX(4,3): return dmat4x3_type
;
631 case IDX(4,4): return dmat4_type
;
632 default: return error_type
;
635 switch (IDX(columns
, rows
)) {
636 case IDX(2,2): return mat2_type
;
637 case IDX(2,3): return mat2x3_type
;
638 case IDX(2,4): return mat2x4_type
;
639 case IDX(3,2): return mat3x2_type
;
640 case IDX(3,3): return mat3_type
;
641 case IDX(3,4): return mat3x4_type
;
642 case IDX(4,2): return mat4x2_type
;
643 case IDX(4,3): return mat4x3_type
;
644 case IDX(4,4): return mat4_type
;
645 default: return error_type
;
650 assert(!"Should not get here.");
655 glsl_type::get_sampler_instance(enum glsl_sampler_dim dim
,
661 case GLSL_TYPE_FLOAT
:
663 case GLSL_SAMPLER_DIM_1D
:
665 return (array
? sampler1DArrayShadow_type
: sampler1DShadow_type
);
667 return (array
? sampler1DArray_type
: sampler1D_type
);
668 case GLSL_SAMPLER_DIM_2D
:
670 return (array
? sampler2DArrayShadow_type
: sampler2DShadow_type
);
672 return (array
? sampler2DArray_type
: sampler2D_type
);
673 case GLSL_SAMPLER_DIM_3D
:
677 return sampler3D_type
;
678 case GLSL_SAMPLER_DIM_CUBE
:
680 return (array
? samplerCubeArrayShadow_type
: samplerCubeShadow_type
);
682 return (array
? samplerCubeArray_type
: samplerCube_type
);
683 case GLSL_SAMPLER_DIM_RECT
:
687 return sampler2DRectShadow_type
;
689 return sampler2DRect_type
;
690 case GLSL_SAMPLER_DIM_BUF
:
694 return samplerBuffer_type
;
695 case GLSL_SAMPLER_DIM_MS
:
698 return (array
? sampler2DMSArray_type
: sampler2DMS_type
);
699 case GLSL_SAMPLER_DIM_EXTERNAL
:
703 return samplerExternalOES_type
;
704 case GLSL_SAMPLER_DIM_SUBPASS
:
705 case GLSL_SAMPLER_DIM_SUBPASS_MS
:
712 case GLSL_SAMPLER_DIM_1D
:
713 return (array
? isampler1DArray_type
: isampler1D_type
);
714 case GLSL_SAMPLER_DIM_2D
:
715 return (array
? isampler2DArray_type
: isampler2D_type
);
716 case GLSL_SAMPLER_DIM_3D
:
719 return isampler3D_type
;
720 case GLSL_SAMPLER_DIM_CUBE
:
721 return (array
? isamplerCubeArray_type
: isamplerCube_type
);
722 case GLSL_SAMPLER_DIM_RECT
:
725 return isampler2DRect_type
;
726 case GLSL_SAMPLER_DIM_BUF
:
729 return isamplerBuffer_type
;
730 case GLSL_SAMPLER_DIM_MS
:
731 return (array
? isampler2DMSArray_type
: isampler2DMS_type
);
732 case GLSL_SAMPLER_DIM_EXTERNAL
:
734 case GLSL_SAMPLER_DIM_SUBPASS
:
735 case GLSL_SAMPLER_DIM_SUBPASS_MS
:
742 case GLSL_SAMPLER_DIM_1D
:
743 return (array
? usampler1DArray_type
: usampler1D_type
);
744 case GLSL_SAMPLER_DIM_2D
:
745 return (array
? usampler2DArray_type
: usampler2D_type
);
746 case GLSL_SAMPLER_DIM_3D
:
749 return usampler3D_type
;
750 case GLSL_SAMPLER_DIM_CUBE
:
751 return (array
? usamplerCubeArray_type
: usamplerCube_type
);
752 case GLSL_SAMPLER_DIM_RECT
:
755 return usampler2DRect_type
;
756 case GLSL_SAMPLER_DIM_BUF
:
759 return usamplerBuffer_type
;
760 case GLSL_SAMPLER_DIM_MS
:
761 return (array
? usampler2DMSArray_type
: usampler2DMS_type
);
762 case GLSL_SAMPLER_DIM_EXTERNAL
:
764 case GLSL_SAMPLER_DIM_SUBPASS
:
765 case GLSL_SAMPLER_DIM_SUBPASS_MS
:
772 unreachable("switch statement above should be complete");
776 glsl_type::get_image_instance(enum glsl_sampler_dim dim
,
777 bool array
, glsl_base_type type
)
780 case GLSL_TYPE_FLOAT
:
782 case GLSL_SAMPLER_DIM_1D
:
783 return (array
? image1DArray_type
: image1D_type
);
784 case GLSL_SAMPLER_DIM_2D
:
785 return (array
? image2DArray_type
: image2D_type
);
786 case GLSL_SAMPLER_DIM_3D
:
788 case GLSL_SAMPLER_DIM_CUBE
:
789 return (array
? imageCubeArray_type
: imageCube_type
);
790 case GLSL_SAMPLER_DIM_RECT
:
794 return image2DRect_type
;
795 case GLSL_SAMPLER_DIM_BUF
:
799 return imageBuffer_type
;
800 case GLSL_SAMPLER_DIM_MS
:
801 return (array
? image2DMSArray_type
: image2DMS_type
);
802 case GLSL_SAMPLER_DIM_SUBPASS
:
803 return subpassInput_type
;
804 case GLSL_SAMPLER_DIM_SUBPASS_MS
:
805 return subpassInputMS_type
;
806 case GLSL_SAMPLER_DIM_EXTERNAL
:
811 case GLSL_SAMPLER_DIM_1D
:
812 return (array
? iimage1DArray_type
: iimage1D_type
);
813 case GLSL_SAMPLER_DIM_2D
:
814 return (array
? iimage2DArray_type
: iimage2D_type
);
815 case GLSL_SAMPLER_DIM_3D
:
818 return iimage3D_type
;
819 case GLSL_SAMPLER_DIM_CUBE
:
820 return (array
? iimageCubeArray_type
: iimageCube_type
);
821 case GLSL_SAMPLER_DIM_RECT
:
824 return iimage2DRect_type
;
825 case GLSL_SAMPLER_DIM_BUF
:
828 return iimageBuffer_type
;
829 case GLSL_SAMPLER_DIM_MS
:
830 return (array
? iimage2DMSArray_type
: iimage2DMS_type
);
831 case GLSL_SAMPLER_DIM_SUBPASS
:
832 return isubpassInput_type
;
833 case GLSL_SAMPLER_DIM_SUBPASS_MS
:
834 return isubpassInputMS_type
;
835 case GLSL_SAMPLER_DIM_EXTERNAL
:
840 case GLSL_SAMPLER_DIM_1D
:
841 return (array
? uimage1DArray_type
: uimage1D_type
);
842 case GLSL_SAMPLER_DIM_2D
:
843 return (array
? uimage2DArray_type
: uimage2D_type
);
844 case GLSL_SAMPLER_DIM_3D
:
847 return uimage3D_type
;
848 case GLSL_SAMPLER_DIM_CUBE
:
849 return (array
? uimageCubeArray_type
: uimageCube_type
);
850 case GLSL_SAMPLER_DIM_RECT
:
853 return uimage2DRect_type
;
854 case GLSL_SAMPLER_DIM_BUF
:
857 return uimageBuffer_type
;
858 case GLSL_SAMPLER_DIM_MS
:
859 return (array
? uimage2DMSArray_type
: uimage2DMS_type
);
860 case GLSL_SAMPLER_DIM_SUBPASS
:
861 return usubpassInput_type
;
862 case GLSL_SAMPLER_DIM_SUBPASS_MS
:
863 return usubpassInputMS_type
;
864 case GLSL_SAMPLER_DIM_EXTERNAL
:
871 unreachable("switch statement above should be complete");
875 glsl_type::get_array_instance(const glsl_type
*base
, unsigned array_size
)
877 /* Generate a name using the base type pointer in the key. This is
878 * done because the name of the base type may not be unique across
879 * shaders. For example, two shaders may have different record types
883 snprintf(key
, sizeof(key
), "%p[%u]", (void *) base
, array_size
);
885 mtx_lock(&glsl_type::mutex
);
887 if (array_types
== NULL
) {
888 array_types
= _mesa_hash_table_create(NULL
, _mesa_key_hash_string
,
889 _mesa_key_string_equal
);
892 const struct hash_entry
*entry
= _mesa_hash_table_search(array_types
, key
);
894 mtx_unlock(&glsl_type::mutex
);
895 const glsl_type
*t
= new glsl_type(base
, array_size
);
896 mtx_lock(&glsl_type::mutex
);
898 entry
= _mesa_hash_table_insert(array_types
,
899 ralloc_strdup(mem_ctx
, key
),
903 assert(((glsl_type
*) entry
->data
)->base_type
== GLSL_TYPE_ARRAY
);
904 assert(((glsl_type
*) entry
->data
)->length
== array_size
);
905 assert(((glsl_type
*) entry
->data
)->fields
.array
== base
);
907 mtx_unlock(&glsl_type::mutex
);
909 return (glsl_type
*) entry
->data
;
914 glsl_type::record_compare(const glsl_type
*b
, bool match_locations
) const
916 if (this->length
!= b
->length
)
919 if (this->interface_packing
!= b
->interface_packing
)
922 if (this->interface_row_major
!= b
->interface_row_major
)
925 /* From the GLSL 4.20 specification (Sec 4.2):
927 * "Structures must have the same name, sequence of type names, and
928 * type definitions, and field names to be considered the same type."
930 * GLSL ES behaves the same (Ver 1.00 Sec 4.2.4, Ver 3.00 Sec 4.2.5).
932 * Note that we cannot force type name check when comparing unnamed
933 * structure types, these have a unique name assigned during parsing.
935 if (!this->is_anonymous() && !b
->is_anonymous())
936 if (strcmp(this->name
, b
->name
) != 0)
939 for (unsigned i
= 0; i
< this->length
; i
++) {
940 if (this->fields
.structure
[i
].type
!= b
->fields
.structure
[i
].type
)
942 if (strcmp(this->fields
.structure
[i
].name
,
943 b
->fields
.structure
[i
].name
) != 0)
945 if (this->fields
.structure
[i
].matrix_layout
946 != b
->fields
.structure
[i
].matrix_layout
)
948 if (match_locations
&& this->fields
.structure
[i
].location
949 != b
->fields
.structure
[i
].location
)
951 if (this->fields
.structure
[i
].offset
952 != b
->fields
.structure
[i
].offset
)
954 if (this->fields
.structure
[i
].interpolation
955 != b
->fields
.structure
[i
].interpolation
)
957 if (this->fields
.structure
[i
].centroid
958 != b
->fields
.structure
[i
].centroid
)
960 if (this->fields
.structure
[i
].sample
961 != b
->fields
.structure
[i
].sample
)
963 if (this->fields
.structure
[i
].patch
964 != b
->fields
.structure
[i
].patch
)
966 if (this->fields
.structure
[i
].memory_read_only
967 != b
->fields
.structure
[i
].memory_read_only
)
969 if (this->fields
.structure
[i
].memory_write_only
970 != b
->fields
.structure
[i
].memory_write_only
)
972 if (this->fields
.structure
[i
].memory_coherent
973 != b
->fields
.structure
[i
].memory_coherent
)
975 if (this->fields
.structure
[i
].memory_volatile
976 != b
->fields
.structure
[i
].memory_volatile
)
978 if (this->fields
.structure
[i
].memory_restrict
979 != b
->fields
.structure
[i
].memory_restrict
)
981 if (this->fields
.structure
[i
].image_format
982 != b
->fields
.structure
[i
].image_format
)
984 if (this->fields
.structure
[i
].precision
985 != b
->fields
.structure
[i
].precision
)
987 if (this->fields
.structure
[i
].explicit_xfb_buffer
988 != b
->fields
.structure
[i
].explicit_xfb_buffer
)
990 if (this->fields
.structure
[i
].xfb_buffer
991 != b
->fields
.structure
[i
].xfb_buffer
)
993 if (this->fields
.structure
[i
].xfb_stride
994 != b
->fields
.structure
[i
].xfb_stride
)
1003 glsl_type::record_key_compare(const void *a
, const void *b
)
1005 const glsl_type
*const key1
= (glsl_type
*) a
;
1006 const glsl_type
*const key2
= (glsl_type
*) b
;
1008 return strcmp(key1
->name
, key2
->name
) == 0 && key1
->record_compare(key2
);
1013 * Generate an integer hash value for a glsl_type structure type.
1016 glsl_type::record_key_hash(const void *a
)
1018 const glsl_type
*const key
= (glsl_type
*) a
;
1019 uintptr_t hash
= key
->length
;
1022 for (unsigned i
= 0; i
< key
->length
; i
++) {
1023 /* casting pointer to uintptr_t */
1024 hash
= (hash
* 13 ) + (uintptr_t) key
->fields
.structure
[i
].type
;
1027 if (sizeof(hash
) == 8)
1028 retval
= (hash
& 0xffffffff) ^ ((uint64_t) hash
>> 32);
1037 glsl_type::get_record_instance(const glsl_struct_field
*fields
,
1038 unsigned num_fields
,
1041 const glsl_type
key(fields
, num_fields
, name
);
1043 mtx_lock(&glsl_type::mutex
);
1045 if (record_types
== NULL
) {
1046 record_types
= _mesa_hash_table_create(NULL
, record_key_hash
,
1047 record_key_compare
);
1050 const struct hash_entry
*entry
= _mesa_hash_table_search(record_types
,
1052 if (entry
== NULL
) {
1053 mtx_unlock(&glsl_type::mutex
);
1054 const glsl_type
*t
= new glsl_type(fields
, num_fields
, name
);
1055 mtx_lock(&glsl_type::mutex
);
1057 entry
= _mesa_hash_table_insert(record_types
, t
, (void *) t
);
1060 assert(((glsl_type
*) entry
->data
)->base_type
== GLSL_TYPE_STRUCT
);
1061 assert(((glsl_type
*) entry
->data
)->length
== num_fields
);
1062 assert(strcmp(((glsl_type
*) entry
->data
)->name
, name
) == 0);
1064 mtx_unlock(&glsl_type::mutex
);
1066 return (glsl_type
*) entry
->data
;
1071 glsl_type::get_interface_instance(const glsl_struct_field
*fields
,
1072 unsigned num_fields
,
1073 enum glsl_interface_packing packing
,
1075 const char *block_name
)
1077 const glsl_type
key(fields
, num_fields
, packing
, row_major
, block_name
);
1079 mtx_lock(&glsl_type::mutex
);
1081 if (interface_types
== NULL
) {
1082 interface_types
= _mesa_hash_table_create(NULL
, record_key_hash
,
1083 record_key_compare
);
1086 const struct hash_entry
*entry
= _mesa_hash_table_search(interface_types
,
1088 if (entry
== NULL
) {
1089 mtx_unlock(&glsl_type::mutex
);
1090 const glsl_type
*t
= new glsl_type(fields
, num_fields
,
1091 packing
, row_major
, block_name
);
1092 mtx_lock(&glsl_type::mutex
);
1094 entry
= _mesa_hash_table_insert(interface_types
, t
, (void *) t
);
1097 assert(((glsl_type
*) entry
->data
)->base_type
== GLSL_TYPE_INTERFACE
);
1098 assert(((glsl_type
*) entry
->data
)->length
== num_fields
);
1099 assert(strcmp(((glsl_type
*) entry
->data
)->name
, block_name
) == 0);
1101 mtx_unlock(&glsl_type::mutex
);
1103 return (glsl_type
*) entry
->data
;
1107 glsl_type::get_subroutine_instance(const char *subroutine_name
)
1109 const glsl_type
key(subroutine_name
);
1111 mtx_lock(&glsl_type::mutex
);
1113 if (subroutine_types
== NULL
) {
1114 subroutine_types
= _mesa_hash_table_create(NULL
, record_key_hash
,
1115 record_key_compare
);
1118 const struct hash_entry
*entry
= _mesa_hash_table_search(subroutine_types
,
1120 if (entry
== NULL
) {
1121 mtx_unlock(&glsl_type::mutex
);
1122 const glsl_type
*t
= new glsl_type(subroutine_name
);
1123 mtx_lock(&glsl_type::mutex
);
1125 entry
= _mesa_hash_table_insert(subroutine_types
, t
, (void *) t
);
1128 assert(((glsl_type
*) entry
->data
)->base_type
== GLSL_TYPE_SUBROUTINE
);
1129 assert(strcmp(((glsl_type
*) entry
->data
)->name
, subroutine_name
) == 0);
1131 mtx_unlock(&glsl_type::mutex
);
1133 return (glsl_type
*) entry
->data
;
1138 function_key_compare(const void *a
, const void *b
)
1140 const glsl_type
*const key1
= (glsl_type
*) a
;
1141 const glsl_type
*const key2
= (glsl_type
*) b
;
1143 if (key1
->length
!= key2
->length
)
1146 return memcmp(key1
->fields
.parameters
, key2
->fields
.parameters
,
1147 (key1
->length
+ 1) * sizeof(*key1
->fields
.parameters
)) == 0;
1152 function_key_hash(const void *a
)
1154 const glsl_type
*const key
= (glsl_type
*) a
;
1155 return _mesa_hash_data(key
->fields
.parameters
,
1156 (key
->length
+ 1) * sizeof(*key
->fields
.parameters
));
1160 glsl_type::get_function_instance(const glsl_type
*return_type
,
1161 const glsl_function_param
*params
,
1162 unsigned num_params
)
1164 const glsl_type
key(return_type
, params
, num_params
);
1166 mtx_lock(&glsl_type::mutex
);
1168 if (function_types
== NULL
) {
1169 function_types
= _mesa_hash_table_create(NULL
, function_key_hash
,
1170 function_key_compare
);
1173 struct hash_entry
*entry
= _mesa_hash_table_search(function_types
, &key
);
1174 if (entry
== NULL
) {
1175 mtx_unlock(&glsl_type::mutex
);
1176 const glsl_type
*t
= new glsl_type(return_type
, params
, num_params
);
1177 mtx_lock(&glsl_type::mutex
);
1179 entry
= _mesa_hash_table_insert(function_types
, t
, (void *) t
);
1182 const glsl_type
*t
= (const glsl_type
*)entry
->data
;
1184 assert(t
->base_type
== GLSL_TYPE_FUNCTION
);
1185 assert(t
->length
== num_params
);
1187 mtx_unlock(&glsl_type::mutex
);
1194 glsl_type::get_mul_type(const glsl_type
*type_a
, const glsl_type
*type_b
)
1196 if (type_a
== type_b
) {
1198 } else if (type_a
->is_matrix() && type_b
->is_matrix()) {
1199 /* Matrix multiply. The columns of A must match the rows of B. Given
1200 * the other previously tested constraints, this means the vector type
1201 * of a row from A must be the same as the vector type of a column from
1204 if (type_a
->row_type() == type_b
->column_type()) {
1205 /* The resulting matrix has the number of columns of matrix B and
1206 * the number of rows of matrix A. We get the row count of A by
1207 * looking at the size of a vector that makes up a column. The
1208 * transpose (size of a row) is done for B.
1210 const glsl_type
*const type
=
1211 get_instance(type_a
->base_type
,
1212 type_a
->column_type()->vector_elements
,
1213 type_b
->row_type()->vector_elements
);
1214 assert(type
!= error_type
);
1218 } else if (type_a
->is_matrix()) {
1219 /* A is a matrix and B is a column vector. Columns of A must match
1220 * rows of B. Given the other previously tested constraints, this
1221 * means the vector type of a row from A must be the same as the
1222 * vector the type of B.
1224 if (type_a
->row_type() == type_b
) {
1225 /* The resulting vector has a number of elements equal to
1226 * the number of rows of matrix A. */
1227 const glsl_type
*const type
=
1228 get_instance(type_a
->base_type
,
1229 type_a
->column_type()->vector_elements
,
1231 assert(type
!= error_type
);
1236 assert(type_b
->is_matrix());
1238 /* A is a row vector and B is a matrix. Columns of A must match rows
1239 * of B. Given the other previously tested constraints, this means
1240 * the type of A must be the same as the vector type of a column from
1243 if (type_a
== type_b
->column_type()) {
1244 /* The resulting vector has a number of elements equal to
1245 * the number of columns of matrix B. */
1246 const glsl_type
*const type
=
1247 get_instance(type_a
->base_type
,
1248 type_b
->row_type()->vector_elements
,
1250 assert(type
!= error_type
);
1261 glsl_type::field_type(const char *name
) const
1263 if (this->base_type
!= GLSL_TYPE_STRUCT
1264 && this->base_type
!= GLSL_TYPE_INTERFACE
)
1267 for (unsigned i
= 0; i
< this->length
; i
++) {
1268 if (strcmp(name
, this->fields
.structure
[i
].name
) == 0)
1269 return this->fields
.structure
[i
].type
;
1277 glsl_type::field_index(const char *name
) const
1279 if (this->base_type
!= GLSL_TYPE_STRUCT
1280 && this->base_type
!= GLSL_TYPE_INTERFACE
)
1283 for (unsigned i
= 0; i
< this->length
; i
++) {
1284 if (strcmp(name
, this->fields
.structure
[i
].name
) == 0)
1293 glsl_type::component_slots() const
1295 switch (this->base_type
) {
1296 case GLSL_TYPE_UINT
:
1298 case GLSL_TYPE_FLOAT
:
1299 case GLSL_TYPE_BOOL
:
1300 return this->components();
1302 case GLSL_TYPE_DOUBLE
:
1303 case GLSL_TYPE_UINT64
:
1304 case GLSL_TYPE_INT64
:
1305 return 2 * this->components();
1307 case GLSL_TYPE_STRUCT
:
1308 case GLSL_TYPE_INTERFACE
: {
1311 for (unsigned i
= 0; i
< this->length
; i
++)
1312 size
+= this->fields
.structure
[i
].type
->component_slots();
1317 case GLSL_TYPE_ARRAY
:
1318 return this->length
* this->fields
.array
->component_slots();
1320 case GLSL_TYPE_SAMPLER
:
1321 case GLSL_TYPE_IMAGE
:
1324 case GLSL_TYPE_SUBROUTINE
:
1327 case GLSL_TYPE_FUNCTION
:
1328 case GLSL_TYPE_ATOMIC_UINT
:
1329 case GLSL_TYPE_VOID
:
1330 case GLSL_TYPE_ERROR
:
1338 glsl_type::record_location_offset(unsigned length
) const
1340 unsigned offset
= 0;
1341 const glsl_type
*t
= this->without_array();
1342 if (t
->is_record()) {
1343 assert(length
<= t
->length
);
1345 for (unsigned i
= 0; i
< length
; i
++) {
1346 const glsl_type
*st
= t
->fields
.structure
[i
].type
;
1347 const glsl_type
*wa
= st
->without_array();
1348 if (wa
->is_record()) {
1349 unsigned r_offset
= wa
->record_location_offset(wa
->length
);
1350 offset
+= st
->is_array() ?
1351 st
->arrays_of_arrays_size() * r_offset
: r_offset
;
1352 } else if (st
->is_array() && st
->fields
.array
->is_array()) {
1353 unsigned outer_array_size
= st
->length
;
1354 const glsl_type
*base_type
= st
->fields
.array
;
1356 /* For arrays of arrays the outer arrays take up a uniform
1357 * slot for each element. The innermost array elements share a
1358 * single slot so we ignore the innermost array when calculating
1361 while (base_type
->fields
.array
->is_array()) {
1362 outer_array_size
= outer_array_size
* base_type
->length
;
1363 base_type
= base_type
->fields
.array
;
1365 offset
+= outer_array_size
;
1367 /* We dont worry about arrays here because unless the array
1368 * contains a structure or another array it only takes up a single
1379 glsl_type::uniform_locations() const
1383 switch (this->base_type
) {
1384 case GLSL_TYPE_UINT
:
1386 case GLSL_TYPE_FLOAT
:
1387 case GLSL_TYPE_DOUBLE
:
1388 case GLSL_TYPE_UINT64
:
1389 case GLSL_TYPE_INT64
:
1390 case GLSL_TYPE_BOOL
:
1391 case GLSL_TYPE_SAMPLER
:
1392 case GLSL_TYPE_IMAGE
:
1393 case GLSL_TYPE_SUBROUTINE
:
1396 case GLSL_TYPE_STRUCT
:
1397 case GLSL_TYPE_INTERFACE
:
1398 for (unsigned i
= 0; i
< this->length
; i
++)
1399 size
+= this->fields
.structure
[i
].type
->uniform_locations();
1401 case GLSL_TYPE_ARRAY
:
1402 return this->length
* this->fields
.array
->uniform_locations();
1409 glsl_type::varying_count() const
1413 switch (this->base_type
) {
1414 case GLSL_TYPE_UINT
:
1416 case GLSL_TYPE_FLOAT
:
1417 case GLSL_TYPE_DOUBLE
:
1418 case GLSL_TYPE_BOOL
:
1419 case GLSL_TYPE_UINT64
:
1420 case GLSL_TYPE_INT64
:
1423 case GLSL_TYPE_STRUCT
:
1424 case GLSL_TYPE_INTERFACE
:
1425 for (unsigned i
= 0; i
< this->length
; i
++)
1426 size
+= this->fields
.structure
[i
].type
->varying_count();
1428 case GLSL_TYPE_ARRAY
:
1429 /* Don't count innermost array elements */
1430 if (this->without_array()->is_record() ||
1431 this->without_array()->is_interface() ||
1432 this->fields
.array
->is_array())
1433 return this->length
* this->fields
.array
->varying_count();
1435 return this->fields
.array
->varying_count();
1437 assert(!"unsupported varying type");
1443 glsl_type::can_implicitly_convert_to(const glsl_type
*desired
,
1444 _mesa_glsl_parse_state
*state
) const
1446 if (this == desired
)
1449 /* GLSL 1.10 and ESSL do not allow implicit conversions. If there is no
1450 * state, we're doing intra-stage function linking where these checks have
1451 * already been done.
1453 if (state
&& (state
->es_shader
|| !state
->is_version(120, 0)))
1456 /* There is no conversion among matrix types. */
1457 if (this->matrix_columns
> 1 || desired
->matrix_columns
> 1)
1460 /* Vector size must match. */
1461 if (this->vector_elements
!= desired
->vector_elements
)
1464 /* int and uint can be converted to float. */
1465 if (desired
->is_float() && this->is_integer())
1468 /* With GLSL 4.0, ARB_gpu_shader5, or MESA_shader_integer_functions, int
1469 * can be converted to uint. Note that state may be NULL here, when
1470 * resolving function calls in the linker. By this time, all the
1471 * state-dependent checks have already happened though, so allow anything
1472 * that's allowed in any shader version.
1474 if ((!state
|| state
->is_version(400, 0) || state
->ARB_gpu_shader5_enable
||
1475 state
->MESA_shader_integer_functions_enable
) &&
1476 desired
->base_type
== GLSL_TYPE_UINT
&& this->base_type
== GLSL_TYPE_INT
)
1479 /* No implicit conversions from double. */
1480 if ((!state
|| state
->has_double()) && this->is_double())
1483 /* Conversions from different types to double. */
1484 if ((!state
|| state
->has_double()) && desired
->is_double()) {
1485 if (this->is_float())
1487 if (this->is_integer())
1495 glsl_type::std140_base_alignment(bool row_major
) const
1497 unsigned N
= is_64bit() ? 8 : 4;
1499 /* (1) If the member is a scalar consuming <N> basic machine units, the
1500 * base alignment is <N>.
1502 * (2) If the member is a two- or four-component vector with components
1503 * consuming <N> basic machine units, the base alignment is 2<N> or
1504 * 4<N>, respectively.
1506 * (3) If the member is a three-component vector with components consuming
1507 * <N> basic machine units, the base alignment is 4<N>.
1509 if (this->is_scalar() || this->is_vector()) {
1510 switch (this->vector_elements
) {
1521 /* (4) If the member is an array of scalars or vectors, the base alignment
1522 * and array stride are set to match the base alignment of a single
1523 * array element, according to rules (1), (2), and (3), and rounded up
1524 * to the base alignment of a vec4. The array may have padding at the
1525 * end; the base offset of the member following the array is rounded up
1526 * to the next multiple of the base alignment.
1528 * (6) If the member is an array of <S> column-major matrices with <C>
1529 * columns and <R> rows, the matrix is stored identically to a row of
1530 * <S>*<C> column vectors with <R> components each, according to rule
1533 * (8) If the member is an array of <S> row-major matrices with <C> columns
1534 * and <R> rows, the matrix is stored identically to a row of <S>*<R>
1535 * row vectors with <C> components each, according to rule (4).
1537 * (10) If the member is an array of <S> structures, the <S> elements of
1538 * the array are laid out in order, according to rule (9).
1540 if (this->is_array()) {
1541 if (this->fields
.array
->is_scalar() ||
1542 this->fields
.array
->is_vector() ||
1543 this->fields
.array
->is_matrix()) {
1544 return MAX2(this->fields
.array
->std140_base_alignment(row_major
), 16);
1546 assert(this->fields
.array
->is_record() ||
1547 this->fields
.array
->is_array());
1548 return this->fields
.array
->std140_base_alignment(row_major
);
1552 /* (5) If the member is a column-major matrix with <C> columns and
1553 * <R> rows, the matrix is stored identically to an array of
1554 * <C> column vectors with <R> components each, according to
1557 * (7) If the member is a row-major matrix with <C> columns and <R>
1558 * rows, the matrix is stored identically to an array of <R>
1559 * row vectors with <C> components each, according to rule (4).
1561 if (this->is_matrix()) {
1562 const struct glsl_type
*vec_type
, *array_type
;
1563 int c
= this->matrix_columns
;
1564 int r
= this->vector_elements
;
1567 vec_type
= get_instance(base_type
, c
, 1);
1568 array_type
= glsl_type::get_array_instance(vec_type
, r
);
1570 vec_type
= get_instance(base_type
, r
, 1);
1571 array_type
= glsl_type::get_array_instance(vec_type
, c
);
1574 return array_type
->std140_base_alignment(false);
1577 /* (9) If the member is a structure, the base alignment of the
1578 * structure is <N>, where <N> is the largest base alignment
1579 * value of any of its members, and rounded up to the base
1580 * alignment of a vec4. The individual members of this
1581 * sub-structure are then assigned offsets by applying this set
1582 * of rules recursively, where the base offset of the first
1583 * member of the sub-structure is equal to the aligned offset
1584 * of the structure. The structure may have padding at the end;
1585 * the base offset of the member following the sub-structure is
1586 * rounded up to the next multiple of the base alignment of the
1589 if (this->is_record()) {
1590 unsigned base_alignment
= 16;
1591 for (unsigned i
= 0; i
< this->length
; i
++) {
1592 bool field_row_major
= row_major
;
1593 const enum glsl_matrix_layout matrix_layout
=
1594 glsl_matrix_layout(this->fields
.structure
[i
].matrix_layout
);
1595 if (matrix_layout
== GLSL_MATRIX_LAYOUT_ROW_MAJOR
) {
1596 field_row_major
= true;
1597 } else if (matrix_layout
== GLSL_MATRIX_LAYOUT_COLUMN_MAJOR
) {
1598 field_row_major
= false;
1601 const struct glsl_type
*field_type
= this->fields
.structure
[i
].type
;
1602 base_alignment
= MAX2(base_alignment
,
1603 field_type
->std140_base_alignment(field_row_major
));
1605 return base_alignment
;
1608 assert(!"not reached");
1613 glsl_type::std140_size(bool row_major
) const
1615 unsigned N
= is_64bit() ? 8 : 4;
1617 /* (1) If the member is a scalar consuming <N> basic machine units, the
1618 * base alignment is <N>.
1620 * (2) If the member is a two- or four-component vector with components
1621 * consuming <N> basic machine units, the base alignment is 2<N> or
1622 * 4<N>, respectively.
1624 * (3) If the member is a three-component vector with components consuming
1625 * <N> basic machine units, the base alignment is 4<N>.
1627 if (this->is_scalar() || this->is_vector()) {
1628 return this->vector_elements
* N
;
1631 /* (5) If the member is a column-major matrix with <C> columns and
1632 * <R> rows, the matrix is stored identically to an array of
1633 * <C> column vectors with <R> components each, according to
1636 * (6) If the member is an array of <S> column-major matrices with <C>
1637 * columns and <R> rows, the matrix is stored identically to a row of
1638 * <S>*<C> column vectors with <R> components each, according to rule
1641 * (7) If the member is a row-major matrix with <C> columns and <R>
1642 * rows, the matrix is stored identically to an array of <R>
1643 * row vectors with <C> components each, according to rule (4).
1645 * (8) If the member is an array of <S> row-major matrices with <C> columns
1646 * and <R> rows, the matrix is stored identically to a row of <S>*<R>
1647 * row vectors with <C> components each, according to rule (4).
1649 if (this->without_array()->is_matrix()) {
1650 const struct glsl_type
*element_type
;
1651 const struct glsl_type
*vec_type
;
1652 unsigned int array_len
;
1654 if (this->is_array()) {
1655 element_type
= this->without_array();
1656 array_len
= this->arrays_of_arrays_size();
1658 element_type
= this;
1663 vec_type
= get_instance(element_type
->base_type
,
1664 element_type
->matrix_columns
, 1);
1666 array_len
*= element_type
->vector_elements
;
1668 vec_type
= get_instance(element_type
->base_type
,
1669 element_type
->vector_elements
, 1);
1670 array_len
*= element_type
->matrix_columns
;
1672 const glsl_type
*array_type
= glsl_type::get_array_instance(vec_type
,
1675 return array_type
->std140_size(false);
1678 /* (4) If the member is an array of scalars or vectors, the base alignment
1679 * and array stride are set to match the base alignment of a single
1680 * array element, according to rules (1), (2), and (3), and rounded up
1681 * to the base alignment of a vec4. The array may have padding at the
1682 * end; the base offset of the member following the array is rounded up
1683 * to the next multiple of the base alignment.
1685 * (10) If the member is an array of <S> structures, the <S> elements of
1686 * the array are laid out in order, according to rule (9).
1688 if (this->is_array()) {
1689 if (this->without_array()->is_record()) {
1690 return this->arrays_of_arrays_size() *
1691 this->without_array()->std140_size(row_major
);
1693 unsigned element_base_align
=
1694 this->without_array()->std140_base_alignment(row_major
);
1695 return this->arrays_of_arrays_size() * MAX2(element_base_align
, 16);
1699 /* (9) If the member is a structure, the base alignment of the
1700 * structure is <N>, where <N> is the largest base alignment
1701 * value of any of its members, and rounded up to the base
1702 * alignment of a vec4. The individual members of this
1703 * sub-structure are then assigned offsets by applying this set
1704 * of rules recursively, where the base offset of the first
1705 * member of the sub-structure is equal to the aligned offset
1706 * of the structure. The structure may have padding at the end;
1707 * the base offset of the member following the sub-structure is
1708 * rounded up to the next multiple of the base alignment of the
1711 if (this->is_record() || this->is_interface()) {
1713 unsigned max_align
= 0;
1715 for (unsigned i
= 0; i
< this->length
; i
++) {
1716 bool field_row_major
= row_major
;
1717 const enum glsl_matrix_layout matrix_layout
=
1718 glsl_matrix_layout(this->fields
.structure
[i
].matrix_layout
);
1719 if (matrix_layout
== GLSL_MATRIX_LAYOUT_ROW_MAJOR
) {
1720 field_row_major
= true;
1721 } else if (matrix_layout
== GLSL_MATRIX_LAYOUT_COLUMN_MAJOR
) {
1722 field_row_major
= false;
1725 const struct glsl_type
*field_type
= this->fields
.structure
[i
].type
;
1726 unsigned align
= field_type
->std140_base_alignment(field_row_major
);
1728 /* Ignore unsized arrays when calculating size */
1729 if (field_type
->is_unsized_array())
1732 size
= glsl_align(size
, align
);
1733 size
+= field_type
->std140_size(field_row_major
);
1735 max_align
= MAX2(align
, max_align
);
1737 if (field_type
->is_record() && (i
+ 1 < this->length
))
1738 size
= glsl_align(size
, 16);
1740 size
= glsl_align(size
, MAX2(max_align
, 16));
1744 assert(!"not reached");
1749 glsl_type::std430_base_alignment(bool row_major
) const
1752 unsigned N
= is_64bit() ? 8 : 4;
1754 /* (1) If the member is a scalar consuming <N> basic machine units, the
1755 * base alignment is <N>.
1757 * (2) If the member is a two- or four-component vector with components
1758 * consuming <N> basic machine units, the base alignment is 2<N> or
1759 * 4<N>, respectively.
1761 * (3) If the member is a three-component vector with components consuming
1762 * <N> basic machine units, the base alignment is 4<N>.
1764 if (this->is_scalar() || this->is_vector()) {
1765 switch (this->vector_elements
) {
1776 /* OpenGL 4.30 spec, section 7.6.2.2 "Standard Uniform Block Layout":
1778 * "When using the std430 storage layout, shader storage blocks will be
1779 * laid out in buffer storage identically to uniform and shader storage
1780 * blocks using the std140 layout, except that the base alignment and
1781 * stride of arrays of scalars and vectors in rule 4 and of structures
1782 * in rule 9 are not rounded up a multiple of the base alignment of a vec4.
1785 /* (1) If the member is a scalar consuming <N> basic machine units, the
1786 * base alignment is <N>.
1788 * (2) If the member is a two- or four-component vector with components
1789 * consuming <N> basic machine units, the base alignment is 2<N> or
1790 * 4<N>, respectively.
1792 * (3) If the member is a three-component vector with components consuming
1793 * <N> basic machine units, the base alignment is 4<N>.
1795 if (this->is_array())
1796 return this->fields
.array
->std430_base_alignment(row_major
);
1798 /* (5) If the member is a column-major matrix with <C> columns and
1799 * <R> rows, the matrix is stored identically to an array of
1800 * <C> column vectors with <R> components each, according to
1803 * (7) If the member is a row-major matrix with <C> columns and <R>
1804 * rows, the matrix is stored identically to an array of <R>
1805 * row vectors with <C> components each, according to rule (4).
1807 if (this->is_matrix()) {
1808 const struct glsl_type
*vec_type
, *array_type
;
1809 int c
= this->matrix_columns
;
1810 int r
= this->vector_elements
;
1813 vec_type
= get_instance(base_type
, c
, 1);
1814 array_type
= glsl_type::get_array_instance(vec_type
, r
);
1816 vec_type
= get_instance(base_type
, r
, 1);
1817 array_type
= glsl_type::get_array_instance(vec_type
, c
);
1820 return array_type
->std430_base_alignment(false);
1823 /* (9) If the member is a structure, the base alignment of the
1824 * structure is <N>, where <N> is the largest base alignment
1825 * value of any of its members, and rounded up to the base
1826 * alignment of a vec4. The individual members of this
1827 * sub-structure are then assigned offsets by applying this set
1828 * of rules recursively, where the base offset of the first
1829 * member of the sub-structure is equal to the aligned offset
1830 * of the structure. The structure may have padding at the end;
1831 * the base offset of the member following the sub-structure is
1832 * rounded up to the next multiple of the base alignment of the
1835 if (this->is_record()) {
1836 unsigned base_alignment
= 0;
1837 for (unsigned i
= 0; i
< this->length
; i
++) {
1838 bool field_row_major
= row_major
;
1839 const enum glsl_matrix_layout matrix_layout
=
1840 glsl_matrix_layout(this->fields
.structure
[i
].matrix_layout
);
1841 if (matrix_layout
== GLSL_MATRIX_LAYOUT_ROW_MAJOR
) {
1842 field_row_major
= true;
1843 } else if (matrix_layout
== GLSL_MATRIX_LAYOUT_COLUMN_MAJOR
) {
1844 field_row_major
= false;
1847 const struct glsl_type
*field_type
= this->fields
.structure
[i
].type
;
1848 base_alignment
= MAX2(base_alignment
,
1849 field_type
->std430_base_alignment(field_row_major
));
1851 assert(base_alignment
> 0);
1852 return base_alignment
;
1854 assert(!"not reached");
1859 glsl_type::std430_array_stride(bool row_major
) const
1861 unsigned N
= is_64bit() ? 8 : 4;
1863 /* Notice that the array stride of a vec3 is not 3 * N but 4 * N.
1864 * See OpenGL 4.30 spec, section 7.6.2.2 "Standard Uniform Block Layout"
1866 * (3) If the member is a three-component vector with components consuming
1867 * <N> basic machine units, the base alignment is 4<N>.
1869 if (this->is_vector() && this->vector_elements
== 3)
1872 /* By default use std430_size(row_major) */
1873 return this->std430_size(row_major
);
1877 glsl_type::std430_size(bool row_major
) const
1879 unsigned N
= is_64bit() ? 8 : 4;
1881 /* OpenGL 4.30 spec, section 7.6.2.2 "Standard Uniform Block Layout":
1883 * "When using the std430 storage layout, shader storage blocks will be
1884 * laid out in buffer storage identically to uniform and shader storage
1885 * blocks using the std140 layout, except that the base alignment and
1886 * stride of arrays of scalars and vectors in rule 4 and of structures
1887 * in rule 9 are not rounded up a multiple of the base alignment of a vec4.
1889 if (this->is_scalar() || this->is_vector())
1890 return this->vector_elements
* N
;
1892 if (this->without_array()->is_matrix()) {
1893 const struct glsl_type
*element_type
;
1894 const struct glsl_type
*vec_type
;
1895 unsigned int array_len
;
1897 if (this->is_array()) {
1898 element_type
= this->without_array();
1899 array_len
= this->arrays_of_arrays_size();
1901 element_type
= this;
1906 vec_type
= get_instance(element_type
->base_type
,
1907 element_type
->matrix_columns
, 1);
1909 array_len
*= element_type
->vector_elements
;
1911 vec_type
= get_instance(element_type
->base_type
,
1912 element_type
->vector_elements
, 1);
1913 array_len
*= element_type
->matrix_columns
;
1915 const glsl_type
*array_type
= glsl_type::get_array_instance(vec_type
,
1918 return array_type
->std430_size(false);
1921 if (this->is_array()) {
1922 if (this->without_array()->is_record())
1923 return this->arrays_of_arrays_size() *
1924 this->without_array()->std430_size(row_major
);
1926 return this->arrays_of_arrays_size() *
1927 this->without_array()->std430_base_alignment(row_major
);
1930 if (this->is_record() || this->is_interface()) {
1932 unsigned max_align
= 0;
1934 for (unsigned i
= 0; i
< this->length
; i
++) {
1935 bool field_row_major
= row_major
;
1936 const enum glsl_matrix_layout matrix_layout
=
1937 glsl_matrix_layout(this->fields
.structure
[i
].matrix_layout
);
1938 if (matrix_layout
== GLSL_MATRIX_LAYOUT_ROW_MAJOR
) {
1939 field_row_major
= true;
1940 } else if (matrix_layout
== GLSL_MATRIX_LAYOUT_COLUMN_MAJOR
) {
1941 field_row_major
= false;
1944 const struct glsl_type
*field_type
= this->fields
.structure
[i
].type
;
1945 unsigned align
= field_type
->std430_base_alignment(field_row_major
);
1946 size
= glsl_align(size
, align
);
1947 size
+= field_type
->std430_size(field_row_major
);
1949 max_align
= MAX2(align
, max_align
);
1951 size
= glsl_align(size
, max_align
);
1955 assert(!"not reached");
1960 glsl_type::count_attribute_slots(bool is_vertex_input
) const
1962 /* From page 31 (page 37 of the PDF) of the GLSL 1.50 spec:
1964 * "A scalar input counts the same amount against this limit as a vec4,
1965 * so applications may want to consider packing groups of four
1966 * unrelated float inputs together into a vector to better utilize the
1967 * capabilities of the underlying hardware. A matrix input will use up
1968 * multiple locations. The number of locations used will equal the
1969 * number of columns in the matrix."
1971 * The spec does not explicitly say how arrays are counted. However, it
1972 * should be safe to assume the total number of slots consumed by an array
1973 * is the number of entries in the array multiplied by the number of slots
1974 * consumed by a single element of the array.
1976 * The spec says nothing about how structs are counted, because vertex
1977 * attributes are not allowed to be (or contain) structs. However, Mesa
1978 * allows varying structs, the number of varying slots taken up by a
1979 * varying struct is simply equal to the sum of the number of slots taken
1980 * up by each element.
1982 * Doubles are counted different depending on whether they are vertex
1983 * inputs or everything else. Vertex inputs from ARB_vertex_attrib_64bit
1984 * take one location no matter what size they are, otherwise dvec3/4
1985 * take two locations.
1987 switch (this->base_type
) {
1988 case GLSL_TYPE_UINT
:
1990 case GLSL_TYPE_FLOAT
:
1991 case GLSL_TYPE_BOOL
:
1992 case GLSL_TYPE_SAMPLER
:
1993 case GLSL_TYPE_IMAGE
:
1994 return this->matrix_columns
;
1995 case GLSL_TYPE_DOUBLE
:
1996 case GLSL_TYPE_UINT64
:
1997 case GLSL_TYPE_INT64
:
1998 if (this->vector_elements
> 2 && !is_vertex_input
)
1999 return this->matrix_columns
* 2;
2001 return this->matrix_columns
;
2002 case GLSL_TYPE_STRUCT
:
2003 case GLSL_TYPE_INTERFACE
: {
2006 for (unsigned i
= 0; i
< this->length
; i
++)
2007 size
+= this->fields
.structure
[i
].type
->count_attribute_slots(is_vertex_input
);
2012 case GLSL_TYPE_ARRAY
:
2013 return this->length
* this->fields
.array
->count_attribute_slots(is_vertex_input
);
2015 case GLSL_TYPE_FUNCTION
:
2016 case GLSL_TYPE_ATOMIC_UINT
:
2017 case GLSL_TYPE_VOID
:
2018 case GLSL_TYPE_SUBROUTINE
:
2019 case GLSL_TYPE_ERROR
:
2023 assert(!"Unexpected type in count_attribute_slots()");
2029 glsl_type::coordinate_components() const
2033 switch (sampler_dimensionality
) {
2034 case GLSL_SAMPLER_DIM_1D
:
2035 case GLSL_SAMPLER_DIM_BUF
:
2038 case GLSL_SAMPLER_DIM_2D
:
2039 case GLSL_SAMPLER_DIM_RECT
:
2040 case GLSL_SAMPLER_DIM_MS
:
2041 case GLSL_SAMPLER_DIM_EXTERNAL
:
2042 case GLSL_SAMPLER_DIM_SUBPASS
:
2045 case GLSL_SAMPLER_DIM_3D
:
2046 case GLSL_SAMPLER_DIM_CUBE
:
2050 assert(!"Should not get here.");
2055 /* Array textures need an additional component for the array index, except
2056 * for cubemap array images that behave like a 2D array of interleaved
2059 if (sampler_array
&&
2060 !(is_image() && sampler_dimensionality
== GLSL_SAMPLER_DIM_CUBE
))
2067 * Declarations of type flyweights (glsl_type::_foo_type) and
2068 * convenience pointers (glsl_type::foo_type).
2071 #define DECL_TYPE(NAME, ...) \
2072 const glsl_type glsl_type::_##NAME##_type = glsl_type(__VA_ARGS__, #NAME); \
2073 const glsl_type *const glsl_type::NAME##_type = &glsl_type::_##NAME##_type;
2075 #define STRUCT_TYPE(NAME)
2077 #include "compiler/builtin_type_macros.h"