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_autofree_context();
44 assert(glsl_type::mem_ctx
!= NULL
);
48 glsl_type::glsl_type(GLenum gl_type
,
49 glsl_base_type base_type
, unsigned vector_elements
,
50 unsigned matrix_columns
, const char *name
) :
53 sampler_dimensionality(0), sampler_shadow(0), sampler_array(0),
54 sampled_type(0), interface_packing(0),
55 vector_elements(vector_elements
), matrix_columns(matrix_columns
),
58 mtx_lock(&glsl_type::mutex
);
60 init_ralloc_type_ctx();
62 this->name
= ralloc_strdup(this->mem_ctx
, name
);
64 mtx_unlock(&glsl_type::mutex
);
66 /* Neither dimension is zero or both dimensions are zero.
68 assert((vector_elements
== 0) == (matrix_columns
== 0));
69 memset(& fields
, 0, sizeof(fields
));
72 glsl_type::glsl_type(GLenum gl_type
, glsl_base_type base_type
,
73 enum glsl_sampler_dim dim
, bool shadow
, bool array
,
74 unsigned type
, const char *name
) :
77 sampler_dimensionality(dim
), sampler_shadow(shadow
),
78 sampler_array(array
), sampled_type(type
), interface_packing(0),
81 mtx_lock(&glsl_type::mutex
);
83 init_ralloc_type_ctx();
85 this->name
= ralloc_strdup(this->mem_ctx
, name
);
87 mtx_unlock(&glsl_type::mutex
);
89 memset(& fields
, 0, sizeof(fields
));
91 if (base_type
== GLSL_TYPE_SAMPLER
) {
92 /* Samplers take no storage whatsoever. */
93 matrix_columns
= vector_elements
= 0;
95 matrix_columns
= vector_elements
= 1;
99 glsl_type::glsl_type(const glsl_struct_field
*fields
, unsigned num_fields
,
102 base_type(GLSL_TYPE_STRUCT
),
103 sampler_dimensionality(0), sampler_shadow(0), sampler_array(0),
104 sampled_type(0), interface_packing(0),
105 vector_elements(0), matrix_columns(0),
110 mtx_lock(&glsl_type::mutex
);
112 init_ralloc_type_ctx();
113 assert(name
!= NULL
);
114 this->name
= ralloc_strdup(this->mem_ctx
, name
);
115 this->fields
.structure
= ralloc_array(this->mem_ctx
,
116 glsl_struct_field
, length
);
118 for (i
= 0; i
< length
; i
++) {
119 this->fields
.structure
[i
].type
= fields
[i
].type
;
120 this->fields
.structure
[i
].name
= ralloc_strdup(this->fields
.structure
,
122 this->fields
.structure
[i
].location
= fields
[i
].location
;
123 this->fields
.structure
[i
].interpolation
= fields
[i
].interpolation
;
124 this->fields
.structure
[i
].centroid
= fields
[i
].centroid
;
125 this->fields
.structure
[i
].sample
= fields
[i
].sample
;
126 this->fields
.structure
[i
].matrix_layout
= fields
[i
].matrix_layout
;
127 this->fields
.structure
[i
].patch
= fields
[i
].patch
;
128 this->fields
.structure
[i
].image_read_only
= fields
[i
].image_read_only
;
129 this->fields
.structure
[i
].image_write_only
= fields
[i
].image_write_only
;
130 this->fields
.structure
[i
].image_coherent
= fields
[i
].image_coherent
;
131 this->fields
.structure
[i
].image_volatile
= fields
[i
].image_volatile
;
132 this->fields
.structure
[i
].image_restrict
= fields
[i
].image_restrict
;
133 this->fields
.structure
[i
].precision
= fields
[i
].precision
;
136 mtx_unlock(&glsl_type::mutex
);
139 glsl_type::glsl_type(const glsl_struct_field
*fields
, unsigned num_fields
,
140 enum glsl_interface_packing packing
, const char *name
) :
142 base_type(GLSL_TYPE_INTERFACE
),
143 sampler_dimensionality(0), sampler_shadow(0), sampler_array(0),
144 sampled_type(0), interface_packing((unsigned) packing
),
145 vector_elements(0), matrix_columns(0),
150 mtx_lock(&glsl_type::mutex
);
152 init_ralloc_type_ctx();
153 assert(name
!= NULL
);
154 this->name
= ralloc_strdup(this->mem_ctx
, name
);
155 this->fields
.structure
= ralloc_array(this->mem_ctx
,
156 glsl_struct_field
, length
);
157 for (i
= 0; i
< length
; i
++) {
158 this->fields
.structure
[i
].type
= fields
[i
].type
;
159 this->fields
.structure
[i
].name
= ralloc_strdup(this->fields
.structure
,
161 this->fields
.structure
[i
].location
= fields
[i
].location
;
162 this->fields
.structure
[i
].interpolation
= fields
[i
].interpolation
;
163 this->fields
.structure
[i
].centroid
= fields
[i
].centroid
;
164 this->fields
.structure
[i
].sample
= fields
[i
].sample
;
165 this->fields
.structure
[i
].matrix_layout
= fields
[i
].matrix_layout
;
166 this->fields
.structure
[i
].patch
= fields
[i
].patch
;
167 this->fields
.structure
[i
].image_read_only
= fields
[i
].image_read_only
;
168 this->fields
.structure
[i
].image_write_only
= fields
[i
].image_write_only
;
169 this->fields
.structure
[i
].image_coherent
= fields
[i
].image_coherent
;
170 this->fields
.structure
[i
].image_volatile
= fields
[i
].image_volatile
;
171 this->fields
.structure
[i
].image_restrict
= fields
[i
].image_restrict
;
172 this->fields
.structure
[i
].precision
= fields
[i
].precision
;
175 mtx_unlock(&glsl_type::mutex
);
178 glsl_type::glsl_type(const glsl_type
*return_type
,
179 const glsl_function_param
*params
, unsigned num_params
) :
181 base_type(GLSL_TYPE_FUNCTION
),
182 sampler_dimensionality(0), sampler_shadow(0), sampler_array(0),
183 sampled_type(0), interface_packing(0),
184 vector_elements(0), matrix_columns(0),
189 mtx_lock(&glsl_type::mutex
);
191 init_ralloc_type_ctx();
193 this->fields
.parameters
= rzalloc_array(this->mem_ctx
,
194 glsl_function_param
, num_params
+ 1);
196 /* We store the return type as the first parameter */
197 this->fields
.parameters
[0].type
= return_type
;
198 this->fields
.parameters
[0].in
= false;
199 this->fields
.parameters
[0].out
= true;
201 /* We store the i'th parameter in slot i+1 */
202 for (i
= 0; i
< length
; i
++) {
203 this->fields
.parameters
[i
+ 1].type
= params
[i
].type
;
204 this->fields
.parameters
[i
+ 1].in
= params
[i
].in
;
205 this->fields
.parameters
[i
+ 1].out
= params
[i
].out
;
208 mtx_unlock(&glsl_type::mutex
);
211 glsl_type::glsl_type(const char *subroutine_name
) :
213 base_type(GLSL_TYPE_SUBROUTINE
),
214 sampler_dimensionality(0), sampler_shadow(0), sampler_array(0),
215 sampled_type(0), interface_packing(0),
216 vector_elements(1), matrix_columns(1),
219 mtx_lock(&glsl_type::mutex
);
221 init_ralloc_type_ctx();
222 assert(subroutine_name
!= NULL
);
223 this->name
= ralloc_strdup(this->mem_ctx
, subroutine_name
);
224 mtx_unlock(&glsl_type::mutex
);
228 glsl_type::contains_sampler() const
230 if (this->is_array()) {
231 return this->fields
.array
->contains_sampler();
232 } else if (this->is_record()) {
233 for (unsigned int i
= 0; i
< this->length
; i
++) {
234 if (this->fields
.structure
[i
].type
->contains_sampler())
239 return this->is_sampler();
245 glsl_type::contains_integer() const
247 if (this->is_array()) {
248 return this->fields
.array
->contains_integer();
249 } else if (this->is_record()) {
250 for (unsigned int i
= 0; i
< this->length
; i
++) {
251 if (this->fields
.structure
[i
].type
->contains_integer())
256 return this->is_integer();
261 glsl_type::contains_double() const
263 if (this->is_array()) {
264 return this->fields
.array
->contains_double();
265 } else if (this->is_record()) {
266 for (unsigned int i
= 0; i
< this->length
; i
++) {
267 if (this->fields
.structure
[i
].type
->contains_double())
272 return this->is_double();
277 glsl_type::contains_opaque() const {
279 case GLSL_TYPE_SAMPLER
:
280 case GLSL_TYPE_IMAGE
:
281 case GLSL_TYPE_ATOMIC_UINT
:
283 case GLSL_TYPE_ARRAY
:
284 return fields
.array
->contains_opaque();
285 case GLSL_TYPE_STRUCT
:
286 for (unsigned int i
= 0; i
< length
; i
++) {
287 if (fields
.structure
[i
].type
->contains_opaque())
297 glsl_type::contains_subroutine() const
299 if (this->is_array()) {
300 return this->fields
.array
->contains_subroutine();
301 } else if (this->is_record()) {
302 for (unsigned int i
= 0; i
< this->length
; i
++) {
303 if (this->fields
.structure
[i
].type
->contains_subroutine())
308 return this->is_subroutine();
313 glsl_type::sampler_index() const
315 const glsl_type
*const t
= (this->is_array()) ? this->fields
.array
: this;
317 assert(t
->is_sampler());
319 switch (t
->sampler_dimensionality
) {
320 case GLSL_SAMPLER_DIM_1D
:
321 return (t
->sampler_array
) ? TEXTURE_1D_ARRAY_INDEX
: TEXTURE_1D_INDEX
;
322 case GLSL_SAMPLER_DIM_2D
:
323 return (t
->sampler_array
) ? TEXTURE_2D_ARRAY_INDEX
: TEXTURE_2D_INDEX
;
324 case GLSL_SAMPLER_DIM_3D
:
325 return TEXTURE_3D_INDEX
;
326 case GLSL_SAMPLER_DIM_CUBE
:
327 return (t
->sampler_array
) ? TEXTURE_CUBE_ARRAY_INDEX
: TEXTURE_CUBE_INDEX
;
328 case GLSL_SAMPLER_DIM_RECT
:
329 return TEXTURE_RECT_INDEX
;
330 case GLSL_SAMPLER_DIM_BUF
:
331 return TEXTURE_BUFFER_INDEX
;
332 case GLSL_SAMPLER_DIM_EXTERNAL
:
333 return TEXTURE_EXTERNAL_INDEX
;
334 case GLSL_SAMPLER_DIM_MS
:
335 return (t
->sampler_array
) ? TEXTURE_2D_MULTISAMPLE_ARRAY_INDEX
: TEXTURE_2D_MULTISAMPLE_INDEX
;
337 assert(!"Should not get here.");
338 return TEXTURE_BUFFER_INDEX
;
343 glsl_type::contains_image() const
345 if (this->is_array()) {
346 return this->fields
.array
->contains_image();
347 } else if (this->is_record()) {
348 for (unsigned int i
= 0; i
< this->length
; i
++) {
349 if (this->fields
.structure
[i
].type
->contains_image())
354 return this->is_image();
358 const glsl_type
*glsl_type::get_base_type() const
365 case GLSL_TYPE_FLOAT
:
367 case GLSL_TYPE_DOUBLE
:
377 const glsl_type
*glsl_type::get_scalar_type() const
379 const glsl_type
*type
= this;
382 while (type
->base_type
== GLSL_TYPE_ARRAY
)
383 type
= type
->fields
.array
;
385 /* Handle vectors and matrices */
386 switch (type
->base_type
) {
391 case GLSL_TYPE_FLOAT
:
393 case GLSL_TYPE_DOUBLE
:
398 /* Handle everything else */
405 _mesa_glsl_release_types(void)
407 /* Should only be called during atexit (either when unloading shared
408 * object, or if process terminates), so no mutex-locking should be
411 if (glsl_type::array_types
!= NULL
) {
412 _mesa_hash_table_destroy(glsl_type::array_types
, NULL
);
413 glsl_type::array_types
= NULL
;
416 if (glsl_type::record_types
!= NULL
) {
417 _mesa_hash_table_destroy(glsl_type::record_types
, NULL
);
418 glsl_type::record_types
= NULL
;
421 if (glsl_type::interface_types
!= NULL
) {
422 _mesa_hash_table_destroy(glsl_type::interface_types
, NULL
);
423 glsl_type::interface_types
= NULL
;
428 glsl_type::glsl_type(const glsl_type
*array
, unsigned length
) :
429 base_type(GLSL_TYPE_ARRAY
),
430 sampler_dimensionality(0), sampler_shadow(0), sampler_array(0),
431 sampled_type(0), interface_packing(0),
432 vector_elements(0), matrix_columns(0),
433 length(length
), name(NULL
)
435 this->fields
.array
= array
;
436 /* Inherit the gl type of the base. The GL type is used for
437 * uniform/statevar handling in Mesa and the arrayness of the type
438 * is represented by the size rather than the type.
440 this->gl_type
= array
->gl_type
;
442 /* Allow a maximum of 10 characters for the array size. This is enough
443 * for 32-bits of ~0. The extra 3 are for the '[', ']', and terminating
446 const unsigned name_length
= strlen(array
->name
) + 10 + 3;
448 mtx_lock(&glsl_type::mutex
);
449 char *const n
= (char *) ralloc_size(this->mem_ctx
, name_length
);
450 mtx_unlock(&glsl_type::mutex
);
453 snprintf(n
, name_length
, "%s[]", array
->name
);
455 /* insert outermost dimensions in the correct spot
456 * otherwise the dimension order will be backwards
458 const char *pos
= strchr(array
->name
, '[');
460 int idx
= pos
- array
->name
;
461 snprintf(n
, idx
+1, "%s", array
->name
);
462 snprintf(n
+ idx
, name_length
- idx
, "[%u]%s",
463 length
, array
->name
+ idx
);
465 snprintf(n
, name_length
, "%s[%u]", array
->name
, length
);
474 glsl_type::vec(unsigned components
)
476 if (components
== 0 || components
> 4)
479 static const glsl_type
*const ts
[] = {
480 float_type
, vec2_type
, vec3_type
, vec4_type
482 return ts
[components
- 1];
486 glsl_type::dvec(unsigned components
)
488 if (components
== 0 || components
> 4)
491 static const glsl_type
*const ts
[] = {
492 double_type
, dvec2_type
, dvec3_type
, dvec4_type
494 return ts
[components
- 1];
498 glsl_type::ivec(unsigned components
)
500 if (components
== 0 || components
> 4)
503 static const glsl_type
*const ts
[] = {
504 int_type
, ivec2_type
, ivec3_type
, ivec4_type
506 return ts
[components
- 1];
511 glsl_type::uvec(unsigned components
)
513 if (components
== 0 || components
> 4)
516 static const glsl_type
*const ts
[] = {
517 uint_type
, uvec2_type
, uvec3_type
, uvec4_type
519 return ts
[components
- 1];
524 glsl_type::bvec(unsigned components
)
526 if (components
== 0 || components
> 4)
529 static const glsl_type
*const ts
[] = {
530 bool_type
, bvec2_type
, bvec3_type
, bvec4_type
532 return ts
[components
- 1];
537 glsl_type::get_instance(unsigned base_type
, unsigned rows
, unsigned columns
)
539 if (base_type
== GLSL_TYPE_VOID
)
542 if ((rows
< 1) || (rows
> 4) || (columns
< 1) || (columns
> 4))
545 /* Treat GLSL vectors as Nx1 matrices.
553 case GLSL_TYPE_FLOAT
:
555 case GLSL_TYPE_DOUBLE
:
563 if ((base_type
!= GLSL_TYPE_FLOAT
&& base_type
!= GLSL_TYPE_DOUBLE
) || (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))
577 if (base_type
== GLSL_TYPE_DOUBLE
) {
578 switch (IDX(columns
, rows
)) {
579 case IDX(2,2): return dmat2_type
;
580 case IDX(2,3): return dmat2x3_type
;
581 case IDX(2,4): return dmat2x4_type
;
582 case IDX(3,2): return dmat3x2_type
;
583 case IDX(3,3): return dmat3_type
;
584 case IDX(3,4): return dmat3x4_type
;
585 case IDX(4,2): return dmat4x2_type
;
586 case IDX(4,3): return dmat4x3_type
;
587 case IDX(4,4): return dmat4_type
;
588 default: return error_type
;
591 switch (IDX(columns
, rows
)) {
592 case IDX(2,2): return mat2_type
;
593 case IDX(2,3): return mat2x3_type
;
594 case IDX(2,4): return mat2x4_type
;
595 case IDX(3,2): return mat3x2_type
;
596 case IDX(3,3): return mat3_type
;
597 case IDX(3,4): return mat3x4_type
;
598 case IDX(4,2): return mat4x2_type
;
599 case IDX(4,3): return mat4x3_type
;
600 case IDX(4,4): return mat4_type
;
601 default: return error_type
;
606 assert(!"Should not get here.");
611 glsl_type::get_sampler_instance(enum glsl_sampler_dim dim
,
617 case GLSL_TYPE_FLOAT
:
619 case GLSL_SAMPLER_DIM_1D
:
621 return (array
? sampler1DArrayShadow_type
: sampler1DShadow_type
);
623 return (array
? sampler1DArray_type
: sampler1D_type
);
624 case GLSL_SAMPLER_DIM_2D
:
626 return (array
? sampler2DArrayShadow_type
: sampler2DShadow_type
);
628 return (array
? sampler2DArray_type
: sampler2D_type
);
629 case GLSL_SAMPLER_DIM_3D
:
633 return sampler3D_type
;
634 case GLSL_SAMPLER_DIM_CUBE
:
636 return (array
? samplerCubeArrayShadow_type
: samplerCubeShadow_type
);
638 return (array
? samplerCubeArray_type
: samplerCube_type
);
639 case GLSL_SAMPLER_DIM_RECT
:
643 return sampler2DRectShadow_type
;
645 return sampler2DRect_type
;
646 case GLSL_SAMPLER_DIM_BUF
:
650 return samplerBuffer_type
;
651 case GLSL_SAMPLER_DIM_MS
:
654 return (array
? sampler2DMSArray_type
: sampler2DMS_type
);
655 case GLSL_SAMPLER_DIM_EXTERNAL
:
659 return samplerExternalOES_type
;
665 case GLSL_SAMPLER_DIM_1D
:
666 return (array
? isampler1DArray_type
: isampler1D_type
);
667 case GLSL_SAMPLER_DIM_2D
:
668 return (array
? isampler2DArray_type
: isampler2D_type
);
669 case GLSL_SAMPLER_DIM_3D
:
672 return isampler3D_type
;
673 case GLSL_SAMPLER_DIM_CUBE
:
674 return (array
? isamplerCubeArray_type
: isamplerCube_type
);
675 case GLSL_SAMPLER_DIM_RECT
:
678 return isampler2DRect_type
;
679 case GLSL_SAMPLER_DIM_BUF
:
682 return isamplerBuffer_type
;
683 case GLSL_SAMPLER_DIM_MS
:
684 return (array
? isampler2DMSArray_type
: isampler2DMS_type
);
685 case GLSL_SAMPLER_DIM_EXTERNAL
:
692 case GLSL_SAMPLER_DIM_1D
:
693 return (array
? usampler1DArray_type
: usampler1D_type
);
694 case GLSL_SAMPLER_DIM_2D
:
695 return (array
? usampler2DArray_type
: usampler2D_type
);
696 case GLSL_SAMPLER_DIM_3D
:
699 return usampler3D_type
;
700 case GLSL_SAMPLER_DIM_CUBE
:
701 return (array
? usamplerCubeArray_type
: usamplerCube_type
);
702 case GLSL_SAMPLER_DIM_RECT
:
705 return usampler2DRect_type
;
706 case GLSL_SAMPLER_DIM_BUF
:
709 return usamplerBuffer_type
;
710 case GLSL_SAMPLER_DIM_MS
:
711 return (array
? usampler2DMSArray_type
: usampler2DMS_type
);
712 case GLSL_SAMPLER_DIM_EXTERNAL
:
719 unreachable("switch statement above should be complete");
723 glsl_type::get_image_instance(enum glsl_sampler_dim dim
,
724 bool array
, glsl_base_type type
)
727 case GLSL_TYPE_FLOAT
:
729 case GLSL_SAMPLER_DIM_1D
:
730 return (array
? image1DArray_type
: image1D_type
);
731 case GLSL_SAMPLER_DIM_2D
:
732 return (array
? image2DArray_type
: image2D_type
);
733 case GLSL_SAMPLER_DIM_3D
:
735 case GLSL_SAMPLER_DIM_CUBE
:
736 return (array
? imageCubeArray_type
: imageCube_type
);
737 case GLSL_SAMPLER_DIM_RECT
:
741 return image2DRect_type
;
742 case GLSL_SAMPLER_DIM_BUF
:
746 return imageBuffer_type
;
747 case GLSL_SAMPLER_DIM_MS
:
748 return (array
? image2DMSArray_type
: image2DMS_type
);
749 case GLSL_SAMPLER_DIM_EXTERNAL
:
754 case GLSL_SAMPLER_DIM_1D
:
755 return (array
? iimage1DArray_type
: iimage1D_type
);
756 case GLSL_SAMPLER_DIM_2D
:
757 return (array
? iimage2DArray_type
: iimage2D_type
);
758 case GLSL_SAMPLER_DIM_3D
:
761 return iimage3D_type
;
762 case GLSL_SAMPLER_DIM_CUBE
:
763 return (array
? iimageCubeArray_type
: iimageCube_type
);
764 case GLSL_SAMPLER_DIM_RECT
:
767 return iimage2DRect_type
;
768 case GLSL_SAMPLER_DIM_BUF
:
771 return iimageBuffer_type
;
772 case GLSL_SAMPLER_DIM_MS
:
773 return (array
? iimage2DMSArray_type
: iimage2DMS_type
);
774 case GLSL_SAMPLER_DIM_EXTERNAL
:
779 case GLSL_SAMPLER_DIM_1D
:
780 return (array
? uimage1DArray_type
: uimage1D_type
);
781 case GLSL_SAMPLER_DIM_2D
:
782 return (array
? uimage2DArray_type
: uimage2D_type
);
783 case GLSL_SAMPLER_DIM_3D
:
786 return uimage3D_type
;
787 case GLSL_SAMPLER_DIM_CUBE
:
788 return (array
? uimageCubeArray_type
: uimageCube_type
);
789 case GLSL_SAMPLER_DIM_RECT
:
792 return uimage2DRect_type
;
793 case GLSL_SAMPLER_DIM_BUF
:
796 return uimageBuffer_type
;
797 case GLSL_SAMPLER_DIM_MS
:
798 return (array
? uimage2DMSArray_type
: uimage2DMS_type
);
799 case GLSL_SAMPLER_DIM_EXTERNAL
:
806 unreachable("switch statement above should be complete");
810 glsl_type::get_array_instance(const glsl_type
*base
, unsigned array_size
)
812 /* Generate a name using the base type pointer in the key. This is
813 * done because the name of the base type may not be unique across
814 * shaders. For example, two shaders may have different record types
818 snprintf(key
, sizeof(key
), "%p[%u]", (void *) base
, array_size
);
820 mtx_lock(&glsl_type::mutex
);
822 if (array_types
== NULL
) {
823 array_types
= _mesa_hash_table_create(NULL
, _mesa_key_hash_string
,
824 _mesa_key_string_equal
);
827 const struct hash_entry
*entry
= _mesa_hash_table_search(array_types
, key
);
829 mtx_unlock(&glsl_type::mutex
);
830 const glsl_type
*t
= new glsl_type(base
, array_size
);
831 mtx_lock(&glsl_type::mutex
);
833 entry
= _mesa_hash_table_insert(array_types
,
834 ralloc_strdup(mem_ctx
, key
),
838 assert(((glsl_type
*) entry
->data
)->base_type
== GLSL_TYPE_ARRAY
);
839 assert(((glsl_type
*) entry
->data
)->length
== array_size
);
840 assert(((glsl_type
*) entry
->data
)->fields
.array
== base
);
842 mtx_unlock(&glsl_type::mutex
);
844 return (glsl_type
*) entry
->data
;
849 glsl_type::record_compare(const glsl_type
*b
) const
851 if (this->length
!= b
->length
)
854 if (this->interface_packing
!= b
->interface_packing
)
857 /* From the GLSL 4.20 specification (Sec 4.2):
859 * "Structures must have the same name, sequence of type names, and
860 * type definitions, and field names to be considered the same type."
862 * GLSL ES behaves the same (Ver 1.00 Sec 4.2.4, Ver 3.00 Sec 4.2.5).
864 * Note that we cannot force type name check when comparing unnamed
865 * structure types, these have a unique name assigned during parsing.
867 if (!this->is_anonymous() && !b
->is_anonymous())
868 if (strcmp(this->name
, b
->name
) != 0)
871 for (unsigned i
= 0; i
< this->length
; i
++) {
872 if (this->fields
.structure
[i
].type
!= b
->fields
.structure
[i
].type
)
874 if (strcmp(this->fields
.structure
[i
].name
,
875 b
->fields
.structure
[i
].name
) != 0)
877 if (this->fields
.structure
[i
].matrix_layout
878 != b
->fields
.structure
[i
].matrix_layout
)
880 if (this->fields
.structure
[i
].location
881 != b
->fields
.structure
[i
].location
)
883 if (this->fields
.structure
[i
].interpolation
884 != b
->fields
.structure
[i
].interpolation
)
886 if (this->fields
.structure
[i
].centroid
887 != b
->fields
.structure
[i
].centroid
)
889 if (this->fields
.structure
[i
].sample
890 != b
->fields
.structure
[i
].sample
)
892 if (this->fields
.structure
[i
].patch
893 != b
->fields
.structure
[i
].patch
)
895 if (this->fields
.structure
[i
].image_read_only
896 != b
->fields
.structure
[i
].image_read_only
)
898 if (this->fields
.structure
[i
].image_write_only
899 != b
->fields
.structure
[i
].image_write_only
)
901 if (this->fields
.structure
[i
].image_coherent
902 != b
->fields
.structure
[i
].image_coherent
)
904 if (this->fields
.structure
[i
].image_volatile
905 != b
->fields
.structure
[i
].image_volatile
)
907 if (this->fields
.structure
[i
].image_restrict
908 != b
->fields
.structure
[i
].image_restrict
)
910 if (this->fields
.structure
[i
].precision
911 != b
->fields
.structure
[i
].precision
)
920 glsl_type::record_key_compare(const void *a
, const void *b
)
922 const glsl_type
*const key1
= (glsl_type
*) a
;
923 const glsl_type
*const key2
= (glsl_type
*) b
;
925 return strcmp(key1
->name
, key2
->name
) == 0 && key1
->record_compare(key2
);
930 * Generate an integer hash value for a glsl_type structure type.
933 glsl_type::record_key_hash(const void *a
)
935 const glsl_type
*const key
= (glsl_type
*) a
;
936 uintptr_t hash
= key
->length
;
939 for (unsigned i
= 0; i
< key
->length
; i
++) {
940 /* casting pointer to uintptr_t */
941 hash
= (hash
* 13 ) + (uintptr_t) key
->fields
.structure
[i
].type
;
944 if (sizeof(hash
) == 8)
945 retval
= (hash
& 0xffffffff) ^ ((uint64_t) hash
>> 32);
954 glsl_type::get_record_instance(const glsl_struct_field
*fields
,
958 const glsl_type
key(fields
, num_fields
, name
);
960 mtx_lock(&glsl_type::mutex
);
962 if (record_types
== NULL
) {
963 record_types
= _mesa_hash_table_create(NULL
, record_key_hash
,
967 const struct hash_entry
*entry
= _mesa_hash_table_search(record_types
,
970 mtx_unlock(&glsl_type::mutex
);
971 const glsl_type
*t
= new glsl_type(fields
, num_fields
, name
);
972 mtx_lock(&glsl_type::mutex
);
974 entry
= _mesa_hash_table_insert(record_types
, t
, (void *) t
);
977 assert(((glsl_type
*) entry
->data
)->base_type
== GLSL_TYPE_STRUCT
);
978 assert(((glsl_type
*) entry
->data
)->length
== num_fields
);
979 assert(strcmp(((glsl_type
*) entry
->data
)->name
, name
) == 0);
981 mtx_unlock(&glsl_type::mutex
);
983 return (glsl_type
*) entry
->data
;
988 glsl_type::get_interface_instance(const glsl_struct_field
*fields
,
990 enum glsl_interface_packing packing
,
991 const char *block_name
)
993 const glsl_type
key(fields
, num_fields
, packing
, block_name
);
995 mtx_lock(&glsl_type::mutex
);
997 if (interface_types
== NULL
) {
998 interface_types
= _mesa_hash_table_create(NULL
, record_key_hash
,
1002 const struct hash_entry
*entry
= _mesa_hash_table_search(interface_types
,
1004 if (entry
== NULL
) {
1005 mtx_unlock(&glsl_type::mutex
);
1006 const glsl_type
*t
= new glsl_type(fields
, num_fields
,
1007 packing
, block_name
);
1008 mtx_lock(&glsl_type::mutex
);
1010 entry
= _mesa_hash_table_insert(interface_types
, t
, (void *) t
);
1013 assert(((glsl_type
*) entry
->data
)->base_type
== GLSL_TYPE_INTERFACE
);
1014 assert(((glsl_type
*) entry
->data
)->length
== num_fields
);
1015 assert(strcmp(((glsl_type
*) entry
->data
)->name
, block_name
) == 0);
1017 mtx_unlock(&glsl_type::mutex
);
1019 return (glsl_type
*) entry
->data
;
1023 glsl_type::get_subroutine_instance(const char *subroutine_name
)
1025 const glsl_type
key(subroutine_name
);
1027 mtx_lock(&glsl_type::mutex
);
1029 if (subroutine_types
== NULL
) {
1030 subroutine_types
= _mesa_hash_table_create(NULL
, record_key_hash
,
1031 record_key_compare
);
1034 const struct hash_entry
*entry
= _mesa_hash_table_search(subroutine_types
,
1036 if (entry
== NULL
) {
1037 mtx_unlock(&glsl_type::mutex
);
1038 const glsl_type
*t
= new glsl_type(subroutine_name
);
1039 mtx_lock(&glsl_type::mutex
);
1041 entry
= _mesa_hash_table_insert(subroutine_types
, t
, (void *) t
);
1044 assert(((glsl_type
*) entry
->data
)->base_type
== GLSL_TYPE_SUBROUTINE
);
1045 assert(strcmp(((glsl_type
*) entry
->data
)->name
, subroutine_name
) == 0);
1047 mtx_unlock(&glsl_type::mutex
);
1049 return (glsl_type
*) entry
->data
;
1054 function_key_compare(const void *a
, const void *b
)
1056 const glsl_type
*const key1
= (glsl_type
*) a
;
1057 const glsl_type
*const key2
= (glsl_type
*) b
;
1059 if (key1
->length
!= key2
->length
)
1062 return memcmp(key1
->fields
.parameters
, key2
->fields
.parameters
,
1063 (key1
->length
+ 1) * sizeof(*key1
->fields
.parameters
)) == 0;
1068 function_key_hash(const void *a
)
1070 const glsl_type
*const key
= (glsl_type
*) a
;
1074 size
= snprintf(hash_key
, sizeof(hash_key
), "%08x", key
->length
);
1076 for (unsigned i
= 0; i
< key
->length
; i
++) {
1077 if (size
>= sizeof(hash_key
))
1080 size
+= snprintf(& hash_key
[size
], sizeof(hash_key
) - size
,
1081 "%p", (void *) key
->fields
.structure
[i
].type
);
1084 return _mesa_hash_string(hash_key
);
1088 glsl_type::get_function_instance(const glsl_type
*return_type
,
1089 const glsl_function_param
*params
,
1090 unsigned num_params
)
1092 const glsl_type
key(return_type
, params
, num_params
);
1094 mtx_lock(&glsl_type::mutex
);
1096 if (function_types
== NULL
) {
1097 function_types
= _mesa_hash_table_create(NULL
, function_key_hash
,
1098 function_key_compare
);
1101 struct hash_entry
*entry
= _mesa_hash_table_search(function_types
, &key
);
1102 if (entry
== NULL
) {
1103 mtx_unlock(&glsl_type::mutex
);
1104 const glsl_type
*t
= new glsl_type(return_type
, params
, num_params
);
1105 mtx_lock(&glsl_type::mutex
);
1107 entry
= _mesa_hash_table_insert(function_types
, t
, (void *) t
);
1110 const glsl_type
*t
= (const glsl_type
*)entry
->data
;
1112 assert(t
->base_type
== GLSL_TYPE_FUNCTION
);
1113 assert(t
->length
== num_params
);
1115 mtx_unlock(&glsl_type::mutex
);
1122 glsl_type::get_mul_type(const glsl_type
*type_a
, const glsl_type
*type_b
)
1124 if (type_a
== type_b
) {
1126 } else if (type_a
->is_matrix() && type_b
->is_matrix()) {
1127 /* Matrix multiply. The columns of A must match the rows of B. Given
1128 * the other previously tested constraints, this means the vector type
1129 * of a row from A must be the same as the vector type of a column from
1132 if (type_a
->row_type() == type_b
->column_type()) {
1133 /* The resulting matrix has the number of columns of matrix B and
1134 * the number of rows of matrix A. We get the row count of A by
1135 * looking at the size of a vector that makes up a column. The
1136 * transpose (size of a row) is done for B.
1138 const glsl_type
*const type
=
1139 get_instance(type_a
->base_type
,
1140 type_a
->column_type()->vector_elements
,
1141 type_b
->row_type()->vector_elements
);
1142 assert(type
!= error_type
);
1146 } else if (type_a
->is_matrix()) {
1147 /* A is a matrix and B is a column vector. Columns of A must match
1148 * rows of B. Given the other previously tested constraints, this
1149 * means the vector type of a row from A must be the same as the
1150 * vector the type of B.
1152 if (type_a
->row_type() == type_b
) {
1153 /* The resulting vector has a number of elements equal to
1154 * the number of rows of matrix A. */
1155 const glsl_type
*const type
=
1156 get_instance(type_a
->base_type
,
1157 type_a
->column_type()->vector_elements
,
1159 assert(type
!= error_type
);
1164 assert(type_b
->is_matrix());
1166 /* A is a row vector and B is a matrix. Columns of A must match rows
1167 * of B. Given the other previously tested constraints, this means
1168 * the type of A must be the same as the vector type of a column from
1171 if (type_a
== type_b
->column_type()) {
1172 /* The resulting vector has a number of elements equal to
1173 * the number of columns of matrix B. */
1174 const glsl_type
*const type
=
1175 get_instance(type_a
->base_type
,
1176 type_b
->row_type()->vector_elements
,
1178 assert(type
!= error_type
);
1189 glsl_type::field_type(const char *name
) const
1191 if (this->base_type
!= GLSL_TYPE_STRUCT
1192 && this->base_type
!= GLSL_TYPE_INTERFACE
)
1195 for (unsigned i
= 0; i
< this->length
; i
++) {
1196 if (strcmp(name
, this->fields
.structure
[i
].name
) == 0)
1197 return this->fields
.structure
[i
].type
;
1205 glsl_type::field_index(const char *name
) const
1207 if (this->base_type
!= GLSL_TYPE_STRUCT
1208 && this->base_type
!= GLSL_TYPE_INTERFACE
)
1211 for (unsigned i
= 0; i
< this->length
; i
++) {
1212 if (strcmp(name
, this->fields
.structure
[i
].name
) == 0)
1221 glsl_type::component_slots() const
1223 switch (this->base_type
) {
1224 case GLSL_TYPE_UINT
:
1226 case GLSL_TYPE_FLOAT
:
1227 case GLSL_TYPE_BOOL
:
1228 return this->components();
1230 case GLSL_TYPE_DOUBLE
:
1231 return 2 * this->components();
1233 case GLSL_TYPE_STRUCT
:
1234 case GLSL_TYPE_INTERFACE
: {
1237 for (unsigned i
= 0; i
< this->length
; i
++)
1238 size
+= this->fields
.structure
[i
].type
->component_slots();
1243 case GLSL_TYPE_ARRAY
:
1244 return this->length
* this->fields
.array
->component_slots();
1246 case GLSL_TYPE_IMAGE
:
1248 case GLSL_TYPE_SUBROUTINE
:
1251 case GLSL_TYPE_FUNCTION
:
1252 case GLSL_TYPE_SAMPLER
:
1253 case GLSL_TYPE_ATOMIC_UINT
:
1254 case GLSL_TYPE_VOID
:
1255 case GLSL_TYPE_ERROR
:
1263 glsl_type::record_location_offset(unsigned length
) const
1265 unsigned offset
= 0;
1266 const glsl_type
*t
= this->without_array();
1267 if (t
->is_record()) {
1268 assert(length
<= t
->length
);
1270 for (unsigned i
= 0; i
< length
; i
++) {
1271 const glsl_type
*st
= t
->fields
.structure
[i
].type
;
1272 const glsl_type
*wa
= st
->without_array();
1273 if (wa
->is_record()) {
1274 unsigned r_offset
= wa
->record_location_offset(wa
->length
);
1275 offset
+= st
->is_array() ?
1276 st
->arrays_of_arrays_size() * r_offset
: r_offset
;
1277 } else if (st
->is_array() && st
->fields
.array
->is_array()) {
1278 unsigned outer_array_size
= st
->length
;
1279 const glsl_type
*base_type
= st
->fields
.array
;
1281 /* For arrays of arrays the outer arrays take up a uniform
1282 * slot for each element. The innermost array elements share a
1283 * single slot so we ignore the innermost array when calculating
1286 while (base_type
->fields
.array
->is_array()) {
1287 outer_array_size
= outer_array_size
* base_type
->length
;
1288 base_type
= base_type
->fields
.array
;
1290 offset
+= outer_array_size
;
1292 /* We dont worry about arrays here because unless the array
1293 * contains a structure or another array it only takes up a single
1304 glsl_type::uniform_locations() const
1308 switch (this->base_type
) {
1309 case GLSL_TYPE_UINT
:
1311 case GLSL_TYPE_FLOAT
:
1312 case GLSL_TYPE_DOUBLE
:
1313 case GLSL_TYPE_BOOL
:
1314 case GLSL_TYPE_SAMPLER
:
1315 case GLSL_TYPE_IMAGE
:
1316 case GLSL_TYPE_SUBROUTINE
:
1319 case GLSL_TYPE_STRUCT
:
1320 case GLSL_TYPE_INTERFACE
:
1321 for (unsigned i
= 0; i
< this->length
; i
++)
1322 size
+= this->fields
.structure
[i
].type
->uniform_locations();
1324 case GLSL_TYPE_ARRAY
:
1325 return this->length
* this->fields
.array
->uniform_locations();
1332 glsl_type::can_implicitly_convert_to(const glsl_type
*desired
,
1333 _mesa_glsl_parse_state
*state
) const
1335 if (this == desired
)
1338 /* ESSL does not allow implicit conversions. If there is no state, we're
1339 * doing intra-stage function linking where these checks have already been
1342 if (state
&& state
->es_shader
)
1345 /* There is no conversion among matrix types. */
1346 if (this->matrix_columns
> 1 || desired
->matrix_columns
> 1)
1349 /* Vector size must match. */
1350 if (this->vector_elements
!= desired
->vector_elements
)
1353 /* int and uint can be converted to float. */
1354 if (desired
->is_float() && this->is_integer())
1357 /* With GLSL 4.0 / ARB_gpu_shader5, int can be converted to uint.
1358 * Note that state may be NULL here, when resolving function calls in the
1359 * linker. By this time, all the state-dependent checks have already
1360 * happened though, so allow anything that's allowed in any shader version. */
1361 if ((!state
|| state
->is_version(400, 0) || state
->ARB_gpu_shader5_enable
) &&
1362 desired
->base_type
== GLSL_TYPE_UINT
&& this->base_type
== GLSL_TYPE_INT
)
1365 /* No implicit conversions from double. */
1366 if ((!state
|| state
->has_double()) && this->is_double())
1369 /* Conversions from different types to double. */
1370 if ((!state
|| state
->has_double()) && desired
->is_double()) {
1371 if (this->is_float())
1373 if (this->is_integer())
1381 glsl_type::std140_base_alignment(bool row_major
) const
1383 unsigned N
= is_double() ? 8 : 4;
1385 /* (1) If the member is a scalar consuming <N> basic machine units, the
1386 * base alignment is <N>.
1388 * (2) If the member is a two- or four-component vector with components
1389 * consuming <N> basic machine units, the base alignment is 2<N> or
1390 * 4<N>, respectively.
1392 * (3) If the member is a three-component vector with components consuming
1393 * <N> basic machine units, the base alignment is 4<N>.
1395 if (this->is_scalar() || this->is_vector()) {
1396 switch (this->vector_elements
) {
1407 /* (4) If the member is an array of scalars or vectors, the base alignment
1408 * and array stride are set to match the base alignment of a single
1409 * array element, according to rules (1), (2), and (3), and rounded up
1410 * to the base alignment of a vec4. The array may have padding at the
1411 * end; the base offset of the member following the array is rounded up
1412 * to the next multiple of the base alignment.
1414 * (6) If the member is an array of <S> column-major matrices with <C>
1415 * columns and <R> rows, the matrix is stored identically to a row of
1416 * <S>*<C> column vectors with <R> components each, according to rule
1419 * (8) If the member is an array of <S> row-major matrices with <C> columns
1420 * and <R> rows, the matrix is stored identically to a row of <S>*<R>
1421 * row vectors with <C> components each, according to rule (4).
1423 * (10) If the member is an array of <S> structures, the <S> elements of
1424 * the array are laid out in order, according to rule (9).
1426 if (this->is_array()) {
1427 if (this->fields
.array
->is_scalar() ||
1428 this->fields
.array
->is_vector() ||
1429 this->fields
.array
->is_matrix()) {
1430 return MAX2(this->fields
.array
->std140_base_alignment(row_major
), 16);
1432 assert(this->fields
.array
->is_record() ||
1433 this->fields
.array
->is_array());
1434 return this->fields
.array
->std140_base_alignment(row_major
);
1438 /* (5) If the member is a column-major matrix with <C> columns and
1439 * <R> rows, the matrix is stored identically to an array of
1440 * <C> column vectors with <R> components each, according to
1443 * (7) If the member is a row-major matrix with <C> columns and <R>
1444 * rows, the matrix is stored identically to an array of <R>
1445 * row vectors with <C> components each, according to rule (4).
1447 if (this->is_matrix()) {
1448 const struct glsl_type
*vec_type
, *array_type
;
1449 int c
= this->matrix_columns
;
1450 int r
= this->vector_elements
;
1453 vec_type
= get_instance(base_type
, c
, 1);
1454 array_type
= glsl_type::get_array_instance(vec_type
, r
);
1456 vec_type
= get_instance(base_type
, r
, 1);
1457 array_type
= glsl_type::get_array_instance(vec_type
, c
);
1460 return array_type
->std140_base_alignment(false);
1463 /* (9) If the member is a structure, the base alignment of the
1464 * structure is <N>, where <N> is the largest base alignment
1465 * value of any of its members, and rounded up to the base
1466 * alignment of a vec4. The individual members of this
1467 * sub-structure are then assigned offsets by applying this set
1468 * of rules recursively, where the base offset of the first
1469 * member of the sub-structure is equal to the aligned offset
1470 * of the structure. The structure may have padding at the end;
1471 * the base offset of the member following the sub-structure is
1472 * rounded up to the next multiple of the base alignment of the
1475 if (this->is_record()) {
1476 unsigned base_alignment
= 16;
1477 for (unsigned i
= 0; i
< this->length
; i
++) {
1478 bool field_row_major
= row_major
;
1479 const enum glsl_matrix_layout matrix_layout
=
1480 glsl_matrix_layout(this->fields
.structure
[i
].matrix_layout
);
1481 if (matrix_layout
== GLSL_MATRIX_LAYOUT_ROW_MAJOR
) {
1482 field_row_major
= true;
1483 } else if (matrix_layout
== GLSL_MATRIX_LAYOUT_COLUMN_MAJOR
) {
1484 field_row_major
= false;
1487 const struct glsl_type
*field_type
= this->fields
.structure
[i
].type
;
1488 base_alignment
= MAX2(base_alignment
,
1489 field_type
->std140_base_alignment(field_row_major
));
1491 return base_alignment
;
1494 assert(!"not reached");
1499 glsl_type::std140_size(bool row_major
) const
1501 unsigned N
= is_double() ? 8 : 4;
1503 /* (1) If the member is a scalar consuming <N> basic machine units, the
1504 * base alignment is <N>.
1506 * (2) If the member is a two- or four-component vector with components
1507 * consuming <N> basic machine units, the base alignment is 2<N> or
1508 * 4<N>, respectively.
1510 * (3) If the member is a three-component vector with components consuming
1511 * <N> basic machine units, the base alignment is 4<N>.
1513 if (this->is_scalar() || this->is_vector()) {
1514 return this->vector_elements
* N
;
1517 /* (5) If the member is a column-major matrix with <C> columns and
1518 * <R> rows, the matrix is stored identically to an array of
1519 * <C> column vectors with <R> components each, according to
1522 * (6) If the member is an array of <S> column-major matrices with <C>
1523 * columns and <R> rows, the matrix is stored identically to a row of
1524 * <S>*<C> column vectors with <R> components each, according to rule
1527 * (7) If the member is a row-major matrix with <C> columns and <R>
1528 * rows, the matrix is stored identically to an array of <R>
1529 * row vectors with <C> components each, according to rule (4).
1531 * (8) If the member is an array of <S> row-major matrices with <C> columns
1532 * and <R> rows, the matrix is stored identically to a row of <S>*<R>
1533 * row vectors with <C> components each, according to rule (4).
1535 if (this->without_array()->is_matrix()) {
1536 const struct glsl_type
*element_type
;
1537 const struct glsl_type
*vec_type
;
1538 unsigned int array_len
;
1540 if (this->is_array()) {
1541 element_type
= this->without_array();
1542 array_len
= this->arrays_of_arrays_size();
1544 element_type
= this;
1549 vec_type
= get_instance(element_type
->base_type
,
1550 element_type
->matrix_columns
, 1);
1552 array_len
*= element_type
->vector_elements
;
1554 vec_type
= get_instance(element_type
->base_type
,
1555 element_type
->vector_elements
, 1);
1556 array_len
*= element_type
->matrix_columns
;
1558 const glsl_type
*array_type
= glsl_type::get_array_instance(vec_type
,
1561 return array_type
->std140_size(false);
1564 /* (4) If the member is an array of scalars or vectors, the base alignment
1565 * and array stride are set to match the base alignment of a single
1566 * array element, according to rules (1), (2), and (3), and rounded up
1567 * to the base alignment of a vec4. The array may have padding at the
1568 * end; the base offset of the member following the array is rounded up
1569 * to the next multiple of the base alignment.
1571 * (10) If the member is an array of <S> structures, the <S> elements of
1572 * the array are laid out in order, according to rule (9).
1574 if (this->is_array()) {
1575 if (this->without_array()->is_record()) {
1576 return this->arrays_of_arrays_size() *
1577 this->without_array()->std140_size(row_major
);
1579 unsigned element_base_align
=
1580 this->without_array()->std140_base_alignment(row_major
);
1581 return this->arrays_of_arrays_size() * MAX2(element_base_align
, 16);
1585 /* (9) If the member is a structure, the base alignment of the
1586 * structure is <N>, where <N> is the largest base alignment
1587 * value of any of its members, and rounded up to the base
1588 * alignment of a vec4. The individual members of this
1589 * sub-structure are then assigned offsets by applying this set
1590 * of rules recursively, where the base offset of the first
1591 * member of the sub-structure is equal to the aligned offset
1592 * of the structure. The structure may have padding at the end;
1593 * the base offset of the member following the sub-structure is
1594 * rounded up to the next multiple of the base alignment of the
1597 if (this->is_record() || this->is_interface()) {
1599 unsigned max_align
= 0;
1601 for (unsigned i
= 0; i
< this->length
; i
++) {
1602 bool field_row_major
= row_major
;
1603 const enum glsl_matrix_layout matrix_layout
=
1604 glsl_matrix_layout(this->fields
.structure
[i
].matrix_layout
);
1605 if (matrix_layout
== GLSL_MATRIX_LAYOUT_ROW_MAJOR
) {
1606 field_row_major
= true;
1607 } else if (matrix_layout
== GLSL_MATRIX_LAYOUT_COLUMN_MAJOR
) {
1608 field_row_major
= false;
1611 const struct glsl_type
*field_type
= this->fields
.structure
[i
].type
;
1612 unsigned align
= field_type
->std140_base_alignment(field_row_major
);
1614 /* Ignore unsized arrays when calculating size */
1615 if (field_type
->is_unsized_array())
1618 size
= glsl_align(size
, align
);
1619 size
+= field_type
->std140_size(field_row_major
);
1621 max_align
= MAX2(align
, max_align
);
1623 if (field_type
->is_record() && (i
+ 1 < this->length
))
1624 size
= glsl_align(size
, 16);
1626 size
= glsl_align(size
, MAX2(max_align
, 16));
1630 assert(!"not reached");
1635 glsl_type::std430_base_alignment(bool row_major
) const
1638 unsigned N
= is_double() ? 8 : 4;
1640 /* (1) If the member is a scalar consuming <N> basic machine units, the
1641 * base alignment is <N>.
1643 * (2) If the member is a two- or four-component vector with components
1644 * consuming <N> basic machine units, the base alignment is 2<N> or
1645 * 4<N>, respectively.
1647 * (3) If the member is a three-component vector with components consuming
1648 * <N> basic machine units, the base alignment is 4<N>.
1650 if (this->is_scalar() || this->is_vector()) {
1651 switch (this->vector_elements
) {
1662 /* OpenGL 4.30 spec, section 7.6.2.2 "Standard Uniform Block Layout":
1664 * "When using the std430 storage layout, shader storage blocks will be
1665 * laid out in buffer storage identically to uniform and shader storage
1666 * blocks using the std140 layout, except that the base alignment and
1667 * stride of arrays of scalars and vectors in rule 4 and of structures
1668 * in rule 9 are not rounded up a multiple of the base alignment of a vec4.
1671 /* (1) If the member is a scalar consuming <N> basic machine units, the
1672 * base alignment is <N>.
1674 * (2) If the member is a two- or four-component vector with components
1675 * consuming <N> basic machine units, the base alignment is 2<N> or
1676 * 4<N>, respectively.
1678 * (3) If the member is a three-component vector with components consuming
1679 * <N> basic machine units, the base alignment is 4<N>.
1681 if (this->is_array())
1682 return this->fields
.array
->std430_base_alignment(row_major
);
1684 /* (5) If the member is a column-major matrix with <C> columns and
1685 * <R> rows, the matrix is stored identically to an array of
1686 * <C> column vectors with <R> components each, according to
1689 * (7) If the member is a row-major matrix with <C> columns and <R>
1690 * rows, the matrix is stored identically to an array of <R>
1691 * row vectors with <C> components each, according to rule (4).
1693 if (this->is_matrix()) {
1694 const struct glsl_type
*vec_type
, *array_type
;
1695 int c
= this->matrix_columns
;
1696 int r
= this->vector_elements
;
1699 vec_type
= get_instance(base_type
, c
, 1);
1700 array_type
= glsl_type::get_array_instance(vec_type
, r
);
1702 vec_type
= get_instance(base_type
, r
, 1);
1703 array_type
= glsl_type::get_array_instance(vec_type
, c
);
1706 return array_type
->std430_base_alignment(false);
1709 /* (9) If the member is a structure, the base alignment of the
1710 * structure is <N>, where <N> is the largest base alignment
1711 * value of any of its members, and rounded up to the base
1712 * alignment of a vec4. The individual members of this
1713 * sub-structure are then assigned offsets by applying this set
1714 * of rules recursively, where the base offset of the first
1715 * member of the sub-structure is equal to the aligned offset
1716 * of the structure. The structure may have padding at the end;
1717 * the base offset of the member following the sub-structure is
1718 * rounded up to the next multiple of the base alignment of the
1721 if (this->is_record()) {
1722 unsigned base_alignment
= 0;
1723 for (unsigned i
= 0; i
< this->length
; i
++) {
1724 bool field_row_major
= row_major
;
1725 const enum glsl_matrix_layout matrix_layout
=
1726 glsl_matrix_layout(this->fields
.structure
[i
].matrix_layout
);
1727 if (matrix_layout
== GLSL_MATRIX_LAYOUT_ROW_MAJOR
) {
1728 field_row_major
= true;
1729 } else if (matrix_layout
== GLSL_MATRIX_LAYOUT_COLUMN_MAJOR
) {
1730 field_row_major
= false;
1733 const struct glsl_type
*field_type
= this->fields
.structure
[i
].type
;
1734 base_alignment
= MAX2(base_alignment
,
1735 field_type
->std430_base_alignment(field_row_major
));
1737 assert(base_alignment
> 0);
1738 return base_alignment
;
1740 assert(!"not reached");
1745 glsl_type::std430_array_stride(bool row_major
) const
1747 unsigned N
= is_double() ? 8 : 4;
1749 /* Notice that the array stride of a vec3 is not 3 * N but 4 * N.
1750 * See OpenGL 4.30 spec, section 7.6.2.2 "Standard Uniform Block Layout"
1752 * (3) If the member is a three-component vector with components consuming
1753 * <N> basic machine units, the base alignment is 4<N>.
1755 if (this->is_vector() && this->vector_elements
== 3)
1758 /* By default use std430_size(row_major) */
1759 return this->std430_size(row_major
);
1763 glsl_type::std430_size(bool row_major
) const
1765 unsigned N
= is_double() ? 8 : 4;
1767 /* OpenGL 4.30 spec, section 7.6.2.2 "Standard Uniform Block Layout":
1769 * "When using the std430 storage layout, shader storage blocks will be
1770 * laid out in buffer storage identically to uniform and shader storage
1771 * blocks using the std140 layout, except that the base alignment and
1772 * stride of arrays of scalars and vectors in rule 4 and of structures
1773 * in rule 9 are not rounded up a multiple of the base alignment of a vec4.
1775 if (this->is_scalar() || this->is_vector())
1776 return this->vector_elements
* N
;
1778 if (this->without_array()->is_matrix()) {
1779 const struct glsl_type
*element_type
;
1780 const struct glsl_type
*vec_type
;
1781 unsigned int array_len
;
1783 if (this->is_array()) {
1784 element_type
= this->without_array();
1785 array_len
= this->arrays_of_arrays_size();
1787 element_type
= this;
1792 vec_type
= get_instance(element_type
->base_type
,
1793 element_type
->matrix_columns
, 1);
1795 array_len
*= element_type
->vector_elements
;
1797 vec_type
= get_instance(element_type
->base_type
,
1798 element_type
->vector_elements
, 1);
1799 array_len
*= element_type
->matrix_columns
;
1801 const glsl_type
*array_type
= glsl_type::get_array_instance(vec_type
,
1804 return array_type
->std430_size(false);
1807 if (this->is_array()) {
1808 if (this->without_array()->is_record())
1809 return this->arrays_of_arrays_size() *
1810 this->without_array()->std430_size(row_major
);
1812 return this->arrays_of_arrays_size() *
1813 this->without_array()->std430_base_alignment(row_major
);
1816 if (this->is_record() || this->is_interface()) {
1818 unsigned max_align
= 0;
1820 for (unsigned i
= 0; i
< this->length
; i
++) {
1821 bool field_row_major
= row_major
;
1822 const enum glsl_matrix_layout matrix_layout
=
1823 glsl_matrix_layout(this->fields
.structure
[i
].matrix_layout
);
1824 if (matrix_layout
== GLSL_MATRIX_LAYOUT_ROW_MAJOR
) {
1825 field_row_major
= true;
1826 } else if (matrix_layout
== GLSL_MATRIX_LAYOUT_COLUMN_MAJOR
) {
1827 field_row_major
= false;
1830 const struct glsl_type
*field_type
= this->fields
.structure
[i
].type
;
1831 unsigned align
= field_type
->std430_base_alignment(field_row_major
);
1832 size
= glsl_align(size
, align
);
1833 size
+= field_type
->std430_size(field_row_major
);
1835 max_align
= MAX2(align
, max_align
);
1837 size
= glsl_align(size
, max_align
);
1841 assert(!"not reached");
1846 glsl_type::count_attribute_slots(bool vertex_input_slots
) const
1848 /* From page 31 (page 37 of the PDF) of the GLSL 1.50 spec:
1850 * "A scalar input counts the same amount against this limit as a vec4,
1851 * so applications may want to consider packing groups of four
1852 * unrelated float inputs together into a vector to better utilize the
1853 * capabilities of the underlying hardware. A matrix input will use up
1854 * multiple locations. The number of locations used will equal the
1855 * number of columns in the matrix."
1857 * The spec does not explicitly say how arrays are counted. However, it
1858 * should be safe to assume the total number of slots consumed by an array
1859 * is the number of entries in the array multiplied by the number of slots
1860 * consumed by a single element of the array.
1862 * The spec says nothing about how structs are counted, because vertex
1863 * attributes are not allowed to be (or contain) structs. However, Mesa
1864 * allows varying structs, the number of varying slots taken up by a
1865 * varying struct is simply equal to the sum of the number of slots taken
1866 * up by each element.
1868 * Doubles are counted different depending on whether they are vertex
1869 * inputs or everything else. Vertex inputs from ARB_vertex_attrib_64bit
1870 * take one location no matter what size they are, otherwise dvec3/4
1871 * take two locations.
1873 switch (this->base_type
) {
1874 case GLSL_TYPE_UINT
:
1876 case GLSL_TYPE_FLOAT
:
1877 case GLSL_TYPE_BOOL
:
1878 return this->matrix_columns
;
1879 case GLSL_TYPE_DOUBLE
:
1880 if (this->vector_elements
> 2 && !vertex_input_slots
)
1881 return this->matrix_columns
* 2;
1883 return this->matrix_columns
;
1884 case GLSL_TYPE_STRUCT
:
1885 case GLSL_TYPE_INTERFACE
: {
1888 for (unsigned i
= 0; i
< this->length
; i
++)
1889 size
+= this->fields
.structure
[i
].type
->count_attribute_slots(vertex_input_slots
);
1894 case GLSL_TYPE_ARRAY
:
1895 return this->length
* this->fields
.array
->count_attribute_slots(vertex_input_slots
);
1897 case GLSL_TYPE_FUNCTION
:
1898 case GLSL_TYPE_SAMPLER
:
1899 case GLSL_TYPE_IMAGE
:
1900 case GLSL_TYPE_ATOMIC_UINT
:
1901 case GLSL_TYPE_VOID
:
1902 case GLSL_TYPE_SUBROUTINE
:
1903 case GLSL_TYPE_ERROR
:
1907 assert(!"Unexpected type in count_attribute_slots()");
1913 glsl_type::coordinate_components() const
1917 switch (sampler_dimensionality
) {
1918 case GLSL_SAMPLER_DIM_1D
:
1919 case GLSL_SAMPLER_DIM_BUF
:
1922 case GLSL_SAMPLER_DIM_2D
:
1923 case GLSL_SAMPLER_DIM_RECT
:
1924 case GLSL_SAMPLER_DIM_MS
:
1925 case GLSL_SAMPLER_DIM_EXTERNAL
:
1928 case GLSL_SAMPLER_DIM_3D
:
1929 case GLSL_SAMPLER_DIM_CUBE
:
1933 assert(!"Should not get here.");
1938 /* Array textures need an additional component for the array index, except
1939 * for cubemap array images that behave like a 2D array of interleaved
1942 if (sampler_array
&&
1943 !(base_type
== GLSL_TYPE_IMAGE
&&
1944 sampler_dimensionality
== GLSL_SAMPLER_DIM_CUBE
))
1951 * Declarations of type flyweights (glsl_type::_foo_type) and
1952 * convenience pointers (glsl_type::foo_type).
1955 #define DECL_TYPE(NAME, ...) \
1956 const glsl_type glsl_type::_##NAME##_type = glsl_type(__VA_ARGS__, #NAME); \
1957 const glsl_type *const glsl_type::NAME##_type = &glsl_type::_##NAME##_type;
1959 #define STRUCT_TYPE(NAME)
1961 #include "compiler/builtin_type_macros.h"