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
].offset
= fields
[i
].offset
;
124 this->fields
.structure
[i
].interpolation
= fields
[i
].interpolation
;
125 this->fields
.structure
[i
].centroid
= fields
[i
].centroid
;
126 this->fields
.structure
[i
].sample
= fields
[i
].sample
;
127 this->fields
.structure
[i
].matrix_layout
= fields
[i
].matrix_layout
;
128 this->fields
.structure
[i
].patch
= fields
[i
].patch
;
129 this->fields
.structure
[i
].image_read_only
= fields
[i
].image_read_only
;
130 this->fields
.structure
[i
].image_write_only
= fields
[i
].image_write_only
;
131 this->fields
.structure
[i
].image_coherent
= fields
[i
].image_coherent
;
132 this->fields
.structure
[i
].image_volatile
= fields
[i
].image_volatile
;
133 this->fields
.structure
[i
].image_restrict
= fields
[i
].image_restrict
;
134 this->fields
.structure
[i
].precision
= fields
[i
].precision
;
137 mtx_unlock(&glsl_type::mutex
);
140 glsl_type::glsl_type(const glsl_struct_field
*fields
, unsigned num_fields
,
141 enum glsl_interface_packing packing
, const char *name
) :
143 base_type(GLSL_TYPE_INTERFACE
),
144 sampler_dimensionality(0), sampler_shadow(0), sampler_array(0),
145 sampled_type(0), interface_packing((unsigned) packing
),
146 vector_elements(0), matrix_columns(0),
151 mtx_lock(&glsl_type::mutex
);
153 init_ralloc_type_ctx();
154 assert(name
!= NULL
);
155 this->name
= ralloc_strdup(this->mem_ctx
, name
);
156 this->fields
.structure
= ralloc_array(this->mem_ctx
,
157 glsl_struct_field
, length
);
158 for (i
= 0; i
< length
; i
++) {
159 this->fields
.structure
[i
].type
= fields
[i
].type
;
160 this->fields
.structure
[i
].name
= ralloc_strdup(this->fields
.structure
,
162 this->fields
.structure
[i
].location
= fields
[i
].location
;
163 this->fields
.structure
[i
].offset
= fields
[i
].offset
;
164 this->fields
.structure
[i
].interpolation
= fields
[i
].interpolation
;
165 this->fields
.structure
[i
].centroid
= fields
[i
].centroid
;
166 this->fields
.structure
[i
].sample
= fields
[i
].sample
;
167 this->fields
.structure
[i
].matrix_layout
= fields
[i
].matrix_layout
;
168 this->fields
.structure
[i
].patch
= fields
[i
].patch
;
169 this->fields
.structure
[i
].image_read_only
= fields
[i
].image_read_only
;
170 this->fields
.structure
[i
].image_write_only
= fields
[i
].image_write_only
;
171 this->fields
.structure
[i
].image_coherent
= fields
[i
].image_coherent
;
172 this->fields
.structure
[i
].image_volatile
= fields
[i
].image_volatile
;
173 this->fields
.structure
[i
].image_restrict
= fields
[i
].image_restrict
;
174 this->fields
.structure
[i
].precision
= fields
[i
].precision
;
177 mtx_unlock(&glsl_type::mutex
);
180 glsl_type::glsl_type(const glsl_type
*return_type
,
181 const glsl_function_param
*params
, unsigned num_params
) :
183 base_type(GLSL_TYPE_FUNCTION
),
184 sampler_dimensionality(0), sampler_shadow(0), sampler_array(0),
185 sampled_type(0), interface_packing(0),
186 vector_elements(0), matrix_columns(0),
191 mtx_lock(&glsl_type::mutex
);
193 init_ralloc_type_ctx();
195 this->fields
.parameters
= rzalloc_array(this->mem_ctx
,
196 glsl_function_param
, num_params
+ 1);
198 /* We store the return type as the first parameter */
199 this->fields
.parameters
[0].type
= return_type
;
200 this->fields
.parameters
[0].in
= false;
201 this->fields
.parameters
[0].out
= true;
203 /* We store the i'th parameter in slot i+1 */
204 for (i
= 0; i
< length
; i
++) {
205 this->fields
.parameters
[i
+ 1].type
= params
[i
].type
;
206 this->fields
.parameters
[i
+ 1].in
= params
[i
].in
;
207 this->fields
.parameters
[i
+ 1].out
= params
[i
].out
;
210 mtx_unlock(&glsl_type::mutex
);
213 glsl_type::glsl_type(const char *subroutine_name
) :
215 base_type(GLSL_TYPE_SUBROUTINE
),
216 sampler_dimensionality(0), sampler_shadow(0), sampler_array(0),
217 sampled_type(0), interface_packing(0),
218 vector_elements(1), matrix_columns(1),
221 mtx_lock(&glsl_type::mutex
);
223 init_ralloc_type_ctx();
224 assert(subroutine_name
!= NULL
);
225 this->name
= ralloc_strdup(this->mem_ctx
, subroutine_name
);
226 mtx_unlock(&glsl_type::mutex
);
230 glsl_type::contains_sampler() const
232 if (this->is_array()) {
233 return this->fields
.array
->contains_sampler();
234 } else if (this->is_record()) {
235 for (unsigned int i
= 0; i
< this->length
; i
++) {
236 if (this->fields
.structure
[i
].type
->contains_sampler())
241 return this->is_sampler();
247 glsl_type::contains_integer() const
249 if (this->is_array()) {
250 return this->fields
.array
->contains_integer();
251 } else if (this->is_record()) {
252 for (unsigned int i
= 0; i
< this->length
; i
++) {
253 if (this->fields
.structure
[i
].type
->contains_integer())
258 return this->is_integer();
263 glsl_type::contains_double() const
265 if (this->is_array()) {
266 return this->fields
.array
->contains_double();
267 } else if (this->is_record()) {
268 for (unsigned int i
= 0; i
< this->length
; i
++) {
269 if (this->fields
.structure
[i
].type
->contains_double())
274 return this->is_double();
279 glsl_type::contains_opaque() const {
281 case GLSL_TYPE_SAMPLER
:
282 case GLSL_TYPE_IMAGE
:
283 case GLSL_TYPE_ATOMIC_UINT
:
285 case GLSL_TYPE_ARRAY
:
286 return fields
.array
->contains_opaque();
287 case GLSL_TYPE_STRUCT
:
288 for (unsigned int i
= 0; i
< length
; i
++) {
289 if (fields
.structure
[i
].type
->contains_opaque())
299 glsl_type::contains_subroutine() const
301 if (this->is_array()) {
302 return this->fields
.array
->contains_subroutine();
303 } else if (this->is_record()) {
304 for (unsigned int i
= 0; i
< this->length
; i
++) {
305 if (this->fields
.structure
[i
].type
->contains_subroutine())
310 return this->is_subroutine();
315 glsl_type::sampler_index() const
317 const glsl_type
*const t
= (this->is_array()) ? this->fields
.array
: this;
319 assert(t
->is_sampler());
321 switch (t
->sampler_dimensionality
) {
322 case GLSL_SAMPLER_DIM_1D
:
323 return (t
->sampler_array
) ? TEXTURE_1D_ARRAY_INDEX
: TEXTURE_1D_INDEX
;
324 case GLSL_SAMPLER_DIM_2D
:
325 return (t
->sampler_array
) ? TEXTURE_2D_ARRAY_INDEX
: TEXTURE_2D_INDEX
;
326 case GLSL_SAMPLER_DIM_3D
:
327 return TEXTURE_3D_INDEX
;
328 case GLSL_SAMPLER_DIM_CUBE
:
329 return (t
->sampler_array
) ? TEXTURE_CUBE_ARRAY_INDEX
: TEXTURE_CUBE_INDEX
;
330 case GLSL_SAMPLER_DIM_RECT
:
331 return TEXTURE_RECT_INDEX
;
332 case GLSL_SAMPLER_DIM_BUF
:
333 return TEXTURE_BUFFER_INDEX
;
334 case GLSL_SAMPLER_DIM_EXTERNAL
:
335 return TEXTURE_EXTERNAL_INDEX
;
336 case GLSL_SAMPLER_DIM_MS
:
337 return (t
->sampler_array
) ? TEXTURE_2D_MULTISAMPLE_ARRAY_INDEX
: TEXTURE_2D_MULTISAMPLE_INDEX
;
339 assert(!"Should not get here.");
340 return TEXTURE_BUFFER_INDEX
;
345 glsl_type::contains_image() const
347 if (this->is_array()) {
348 return this->fields
.array
->contains_image();
349 } else if (this->is_record()) {
350 for (unsigned int i
= 0; i
< this->length
; i
++) {
351 if (this->fields
.structure
[i
].type
->contains_image())
356 return this->is_image();
360 const glsl_type
*glsl_type::get_base_type() const
367 case GLSL_TYPE_FLOAT
:
369 case GLSL_TYPE_DOUBLE
:
379 const glsl_type
*glsl_type::get_scalar_type() const
381 const glsl_type
*type
= this;
384 while (type
->base_type
== GLSL_TYPE_ARRAY
)
385 type
= type
->fields
.array
;
387 /* Handle vectors and matrices */
388 switch (type
->base_type
) {
393 case GLSL_TYPE_FLOAT
:
395 case GLSL_TYPE_DOUBLE
:
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
;
430 glsl_type::glsl_type(const glsl_type
*array
, unsigned length
) :
431 base_type(GLSL_TYPE_ARRAY
),
432 sampler_dimensionality(0), sampler_shadow(0), sampler_array(0),
433 sampled_type(0), interface_packing(0),
434 vector_elements(0), matrix_columns(0),
435 length(length
), name(NULL
)
437 this->fields
.array
= array
;
438 /* Inherit the gl type of the base. The GL type is used for
439 * uniform/statevar handling in Mesa and the arrayness of the type
440 * is represented by the size rather than the type.
442 this->gl_type
= array
->gl_type
;
444 /* Allow a maximum of 10 characters for the array size. This is enough
445 * for 32-bits of ~0. The extra 3 are for the '[', ']', and terminating
448 const unsigned name_length
= strlen(array
->name
) + 10 + 3;
450 mtx_lock(&glsl_type::mutex
);
451 char *const n
= (char *) ralloc_size(this->mem_ctx
, name_length
);
452 mtx_unlock(&glsl_type::mutex
);
455 snprintf(n
, name_length
, "%s[]", array
->name
);
457 /* insert outermost dimensions in the correct spot
458 * otherwise the dimension order will be backwards
460 const char *pos
= strchr(array
->name
, '[');
462 int idx
= pos
- array
->name
;
463 snprintf(n
, idx
+1, "%s", array
->name
);
464 snprintf(n
+ idx
, name_length
- idx
, "[%u]%s",
465 length
, array
->name
+ idx
);
467 snprintf(n
, name_length
, "%s[%u]", array
->name
, length
);
476 glsl_type::vec(unsigned components
)
478 if (components
== 0 || components
> 4)
481 static const glsl_type
*const ts
[] = {
482 float_type
, vec2_type
, vec3_type
, vec4_type
484 return ts
[components
- 1];
488 glsl_type::dvec(unsigned components
)
490 if (components
== 0 || components
> 4)
493 static const glsl_type
*const ts
[] = {
494 double_type
, dvec2_type
, dvec3_type
, dvec4_type
496 return ts
[components
- 1];
500 glsl_type::ivec(unsigned components
)
502 if (components
== 0 || components
> 4)
505 static const glsl_type
*const ts
[] = {
506 int_type
, ivec2_type
, ivec3_type
, ivec4_type
508 return ts
[components
- 1];
513 glsl_type::uvec(unsigned components
)
515 if (components
== 0 || components
> 4)
518 static const glsl_type
*const ts
[] = {
519 uint_type
, uvec2_type
, uvec3_type
, uvec4_type
521 return ts
[components
- 1];
526 glsl_type::bvec(unsigned components
)
528 if (components
== 0 || components
> 4)
531 static const glsl_type
*const ts
[] = {
532 bool_type
, bvec2_type
, bvec3_type
, bvec4_type
534 return ts
[components
- 1];
539 glsl_type::get_instance(unsigned base_type
, unsigned rows
, unsigned columns
)
541 if (base_type
== GLSL_TYPE_VOID
)
544 if ((rows
< 1) || (rows
> 4) || (columns
< 1) || (columns
> 4))
547 /* Treat GLSL vectors as Nx1 matrices.
555 case GLSL_TYPE_FLOAT
:
557 case GLSL_TYPE_DOUBLE
:
565 if ((base_type
!= GLSL_TYPE_FLOAT
&& base_type
!= GLSL_TYPE_DOUBLE
) || (rows
== 1))
568 /* GLSL matrix types are named mat{COLUMNS}x{ROWS}. Only the following
569 * combinations are valid:
577 #define IDX(c,r) (((c-1)*3) + (r-1))
579 if (base_type
== GLSL_TYPE_DOUBLE
) {
580 switch (IDX(columns
, rows
)) {
581 case IDX(2,2): return dmat2_type
;
582 case IDX(2,3): return dmat2x3_type
;
583 case IDX(2,4): return dmat2x4_type
;
584 case IDX(3,2): return dmat3x2_type
;
585 case IDX(3,3): return dmat3_type
;
586 case IDX(3,4): return dmat3x4_type
;
587 case IDX(4,2): return dmat4x2_type
;
588 case IDX(4,3): return dmat4x3_type
;
589 case IDX(4,4): return dmat4_type
;
590 default: return error_type
;
593 switch (IDX(columns
, rows
)) {
594 case IDX(2,2): return mat2_type
;
595 case IDX(2,3): return mat2x3_type
;
596 case IDX(2,4): return mat2x4_type
;
597 case IDX(3,2): return mat3x2_type
;
598 case IDX(3,3): return mat3_type
;
599 case IDX(3,4): return mat3x4_type
;
600 case IDX(4,2): return mat4x2_type
;
601 case IDX(4,3): return mat4x3_type
;
602 case IDX(4,4): return mat4_type
;
603 default: return error_type
;
608 assert(!"Should not get here.");
613 glsl_type::get_sampler_instance(enum glsl_sampler_dim dim
,
619 case GLSL_TYPE_FLOAT
:
621 case GLSL_SAMPLER_DIM_1D
:
623 return (array
? sampler1DArrayShadow_type
: sampler1DShadow_type
);
625 return (array
? sampler1DArray_type
: sampler1D_type
);
626 case GLSL_SAMPLER_DIM_2D
:
628 return (array
? sampler2DArrayShadow_type
: sampler2DShadow_type
);
630 return (array
? sampler2DArray_type
: sampler2D_type
);
631 case GLSL_SAMPLER_DIM_3D
:
635 return sampler3D_type
;
636 case GLSL_SAMPLER_DIM_CUBE
:
638 return (array
? samplerCubeArrayShadow_type
: samplerCubeShadow_type
);
640 return (array
? samplerCubeArray_type
: samplerCube_type
);
641 case GLSL_SAMPLER_DIM_RECT
:
645 return sampler2DRectShadow_type
;
647 return sampler2DRect_type
;
648 case GLSL_SAMPLER_DIM_BUF
:
652 return samplerBuffer_type
;
653 case GLSL_SAMPLER_DIM_MS
:
656 return (array
? sampler2DMSArray_type
: sampler2DMS_type
);
657 case GLSL_SAMPLER_DIM_EXTERNAL
:
661 return samplerExternalOES_type
;
667 case GLSL_SAMPLER_DIM_1D
:
668 return (array
? isampler1DArray_type
: isampler1D_type
);
669 case GLSL_SAMPLER_DIM_2D
:
670 return (array
? isampler2DArray_type
: isampler2D_type
);
671 case GLSL_SAMPLER_DIM_3D
:
674 return isampler3D_type
;
675 case GLSL_SAMPLER_DIM_CUBE
:
676 return (array
? isamplerCubeArray_type
: isamplerCube_type
);
677 case GLSL_SAMPLER_DIM_RECT
:
680 return isampler2DRect_type
;
681 case GLSL_SAMPLER_DIM_BUF
:
684 return isamplerBuffer_type
;
685 case GLSL_SAMPLER_DIM_MS
:
686 return (array
? isampler2DMSArray_type
: isampler2DMS_type
);
687 case GLSL_SAMPLER_DIM_EXTERNAL
:
694 case GLSL_SAMPLER_DIM_1D
:
695 return (array
? usampler1DArray_type
: usampler1D_type
);
696 case GLSL_SAMPLER_DIM_2D
:
697 return (array
? usampler2DArray_type
: usampler2D_type
);
698 case GLSL_SAMPLER_DIM_3D
:
701 return usampler3D_type
;
702 case GLSL_SAMPLER_DIM_CUBE
:
703 return (array
? usamplerCubeArray_type
: usamplerCube_type
);
704 case GLSL_SAMPLER_DIM_RECT
:
707 return usampler2DRect_type
;
708 case GLSL_SAMPLER_DIM_BUF
:
711 return usamplerBuffer_type
;
712 case GLSL_SAMPLER_DIM_MS
:
713 return (array
? usampler2DMSArray_type
: usampler2DMS_type
);
714 case GLSL_SAMPLER_DIM_EXTERNAL
:
721 unreachable("switch statement above should be complete");
725 glsl_type::get_image_instance(enum glsl_sampler_dim dim
,
726 bool array
, glsl_base_type type
)
729 case GLSL_TYPE_FLOAT
:
731 case GLSL_SAMPLER_DIM_1D
:
732 return (array
? image1DArray_type
: image1D_type
);
733 case GLSL_SAMPLER_DIM_2D
:
734 return (array
? image2DArray_type
: image2D_type
);
735 case GLSL_SAMPLER_DIM_3D
:
737 case GLSL_SAMPLER_DIM_CUBE
:
738 return (array
? imageCubeArray_type
: imageCube_type
);
739 case GLSL_SAMPLER_DIM_RECT
:
743 return image2DRect_type
;
744 case GLSL_SAMPLER_DIM_BUF
:
748 return imageBuffer_type
;
749 case GLSL_SAMPLER_DIM_MS
:
750 return (array
? image2DMSArray_type
: image2DMS_type
);
751 case GLSL_SAMPLER_DIM_EXTERNAL
:
756 case GLSL_SAMPLER_DIM_1D
:
757 return (array
? iimage1DArray_type
: iimage1D_type
);
758 case GLSL_SAMPLER_DIM_2D
:
759 return (array
? iimage2DArray_type
: iimage2D_type
);
760 case GLSL_SAMPLER_DIM_3D
:
763 return iimage3D_type
;
764 case GLSL_SAMPLER_DIM_CUBE
:
765 return (array
? iimageCubeArray_type
: iimageCube_type
);
766 case GLSL_SAMPLER_DIM_RECT
:
769 return iimage2DRect_type
;
770 case GLSL_SAMPLER_DIM_BUF
:
773 return iimageBuffer_type
;
774 case GLSL_SAMPLER_DIM_MS
:
775 return (array
? iimage2DMSArray_type
: iimage2DMS_type
);
776 case GLSL_SAMPLER_DIM_EXTERNAL
:
781 case GLSL_SAMPLER_DIM_1D
:
782 return (array
? uimage1DArray_type
: uimage1D_type
);
783 case GLSL_SAMPLER_DIM_2D
:
784 return (array
? uimage2DArray_type
: uimage2D_type
);
785 case GLSL_SAMPLER_DIM_3D
:
788 return uimage3D_type
;
789 case GLSL_SAMPLER_DIM_CUBE
:
790 return (array
? uimageCubeArray_type
: uimageCube_type
);
791 case GLSL_SAMPLER_DIM_RECT
:
794 return uimage2DRect_type
;
795 case GLSL_SAMPLER_DIM_BUF
:
798 return uimageBuffer_type
;
799 case GLSL_SAMPLER_DIM_MS
:
800 return (array
? uimage2DMSArray_type
: uimage2DMS_type
);
801 case GLSL_SAMPLER_DIM_EXTERNAL
:
808 unreachable("switch statement above should be complete");
812 glsl_type::get_array_instance(const glsl_type
*base
, unsigned array_size
)
814 /* Generate a name using the base type pointer in the key. This is
815 * done because the name of the base type may not be unique across
816 * shaders. For example, two shaders may have different record types
820 snprintf(key
, sizeof(key
), "%p[%u]", (void *) base
, array_size
);
822 mtx_lock(&glsl_type::mutex
);
824 if (array_types
== NULL
) {
825 array_types
= _mesa_hash_table_create(NULL
, _mesa_key_hash_string
,
826 _mesa_key_string_equal
);
829 const struct hash_entry
*entry
= _mesa_hash_table_search(array_types
, key
);
831 mtx_unlock(&glsl_type::mutex
);
832 const glsl_type
*t
= new glsl_type(base
, array_size
);
833 mtx_lock(&glsl_type::mutex
);
835 entry
= _mesa_hash_table_insert(array_types
,
836 ralloc_strdup(mem_ctx
, key
),
840 assert(((glsl_type
*) entry
->data
)->base_type
== GLSL_TYPE_ARRAY
);
841 assert(((glsl_type
*) entry
->data
)->length
== array_size
);
842 assert(((glsl_type
*) entry
->data
)->fields
.array
== base
);
844 mtx_unlock(&glsl_type::mutex
);
846 return (glsl_type
*) entry
->data
;
851 glsl_type::record_compare(const glsl_type
*b
) const
853 if (this->length
!= b
->length
)
856 if (this->interface_packing
!= b
->interface_packing
)
859 /* From the GLSL 4.20 specification (Sec 4.2):
861 * "Structures must have the same name, sequence of type names, and
862 * type definitions, and field names to be considered the same type."
864 * GLSL ES behaves the same (Ver 1.00 Sec 4.2.4, Ver 3.00 Sec 4.2.5).
866 * Note that we cannot force type name check when comparing unnamed
867 * structure types, these have a unique name assigned during parsing.
869 if (!this->is_anonymous() && !b
->is_anonymous())
870 if (strcmp(this->name
, b
->name
) != 0)
873 for (unsigned i
= 0; i
< this->length
; i
++) {
874 if (this->fields
.structure
[i
].type
!= b
->fields
.structure
[i
].type
)
876 if (strcmp(this->fields
.structure
[i
].name
,
877 b
->fields
.structure
[i
].name
) != 0)
879 if (this->fields
.structure
[i
].matrix_layout
880 != b
->fields
.structure
[i
].matrix_layout
)
882 if (this->fields
.structure
[i
].location
883 != b
->fields
.structure
[i
].location
)
885 if (this->fields
.structure
[i
].offset
886 != b
->fields
.structure
[i
].offset
)
888 if (this->fields
.structure
[i
].interpolation
889 != b
->fields
.structure
[i
].interpolation
)
891 if (this->fields
.structure
[i
].centroid
892 != b
->fields
.structure
[i
].centroid
)
894 if (this->fields
.structure
[i
].sample
895 != b
->fields
.structure
[i
].sample
)
897 if (this->fields
.structure
[i
].patch
898 != b
->fields
.structure
[i
].patch
)
900 if (this->fields
.structure
[i
].image_read_only
901 != b
->fields
.structure
[i
].image_read_only
)
903 if (this->fields
.structure
[i
].image_write_only
904 != b
->fields
.structure
[i
].image_write_only
)
906 if (this->fields
.structure
[i
].image_coherent
907 != b
->fields
.structure
[i
].image_coherent
)
909 if (this->fields
.structure
[i
].image_volatile
910 != b
->fields
.structure
[i
].image_volatile
)
912 if (this->fields
.structure
[i
].image_restrict
913 != b
->fields
.structure
[i
].image_restrict
)
915 if (this->fields
.structure
[i
].precision
916 != b
->fields
.structure
[i
].precision
)
925 glsl_type::record_key_compare(const void *a
, const void *b
)
927 const glsl_type
*const key1
= (glsl_type
*) a
;
928 const glsl_type
*const key2
= (glsl_type
*) b
;
930 return strcmp(key1
->name
, key2
->name
) == 0 && key1
->record_compare(key2
);
935 * Generate an integer hash value for a glsl_type structure type.
938 glsl_type::record_key_hash(const void *a
)
940 const glsl_type
*const key
= (glsl_type
*) a
;
941 uintptr_t hash
= key
->length
;
944 for (unsigned i
= 0; i
< key
->length
; i
++) {
945 /* casting pointer to uintptr_t */
946 hash
= (hash
* 13 ) + (uintptr_t) key
->fields
.structure
[i
].type
;
949 if (sizeof(hash
) == 8)
950 retval
= (hash
& 0xffffffff) ^ ((uint64_t) hash
>> 32);
959 glsl_type::get_record_instance(const glsl_struct_field
*fields
,
963 const glsl_type
key(fields
, num_fields
, name
);
965 mtx_lock(&glsl_type::mutex
);
967 if (record_types
== NULL
) {
968 record_types
= _mesa_hash_table_create(NULL
, record_key_hash
,
972 const struct hash_entry
*entry
= _mesa_hash_table_search(record_types
,
975 mtx_unlock(&glsl_type::mutex
);
976 const glsl_type
*t
= new glsl_type(fields
, num_fields
, name
);
977 mtx_lock(&glsl_type::mutex
);
979 entry
= _mesa_hash_table_insert(record_types
, t
, (void *) t
);
982 assert(((glsl_type
*) entry
->data
)->base_type
== GLSL_TYPE_STRUCT
);
983 assert(((glsl_type
*) entry
->data
)->length
== num_fields
);
984 assert(strcmp(((glsl_type
*) entry
->data
)->name
, name
) == 0);
986 mtx_unlock(&glsl_type::mutex
);
988 return (glsl_type
*) entry
->data
;
993 glsl_type::get_interface_instance(const glsl_struct_field
*fields
,
995 enum glsl_interface_packing packing
,
996 const char *block_name
)
998 const glsl_type
key(fields
, num_fields
, packing
, block_name
);
1000 mtx_lock(&glsl_type::mutex
);
1002 if (interface_types
== NULL
) {
1003 interface_types
= _mesa_hash_table_create(NULL
, record_key_hash
,
1004 record_key_compare
);
1007 const struct hash_entry
*entry
= _mesa_hash_table_search(interface_types
,
1009 if (entry
== NULL
) {
1010 mtx_unlock(&glsl_type::mutex
);
1011 const glsl_type
*t
= new glsl_type(fields
, num_fields
,
1012 packing
, block_name
);
1013 mtx_lock(&glsl_type::mutex
);
1015 entry
= _mesa_hash_table_insert(interface_types
, t
, (void *) t
);
1018 assert(((glsl_type
*) entry
->data
)->base_type
== GLSL_TYPE_INTERFACE
);
1019 assert(((glsl_type
*) entry
->data
)->length
== num_fields
);
1020 assert(strcmp(((glsl_type
*) entry
->data
)->name
, block_name
) == 0);
1022 mtx_unlock(&glsl_type::mutex
);
1024 return (glsl_type
*) entry
->data
;
1028 glsl_type::get_subroutine_instance(const char *subroutine_name
)
1030 const glsl_type
key(subroutine_name
);
1032 mtx_lock(&glsl_type::mutex
);
1034 if (subroutine_types
== NULL
) {
1035 subroutine_types
= _mesa_hash_table_create(NULL
, record_key_hash
,
1036 record_key_compare
);
1039 const struct hash_entry
*entry
= _mesa_hash_table_search(subroutine_types
,
1041 if (entry
== NULL
) {
1042 mtx_unlock(&glsl_type::mutex
);
1043 const glsl_type
*t
= new glsl_type(subroutine_name
);
1044 mtx_lock(&glsl_type::mutex
);
1046 entry
= _mesa_hash_table_insert(subroutine_types
, t
, (void *) t
);
1049 assert(((glsl_type
*) entry
->data
)->base_type
== GLSL_TYPE_SUBROUTINE
);
1050 assert(strcmp(((glsl_type
*) entry
->data
)->name
, subroutine_name
) == 0);
1052 mtx_unlock(&glsl_type::mutex
);
1054 return (glsl_type
*) entry
->data
;
1059 function_key_compare(const void *a
, const void *b
)
1061 const glsl_type
*const key1
= (glsl_type
*) a
;
1062 const glsl_type
*const key2
= (glsl_type
*) b
;
1064 if (key1
->length
!= key2
->length
)
1067 return memcmp(key1
->fields
.parameters
, key2
->fields
.parameters
,
1068 (key1
->length
+ 1) * sizeof(*key1
->fields
.parameters
)) == 0;
1073 function_key_hash(const void *a
)
1075 const glsl_type
*const key
= (glsl_type
*) a
;
1079 size
= snprintf(hash_key
, sizeof(hash_key
), "%08x", key
->length
);
1081 for (unsigned i
= 0; i
< key
->length
; i
++) {
1082 if (size
>= sizeof(hash_key
))
1085 size
+= snprintf(& hash_key
[size
], sizeof(hash_key
) - size
,
1086 "%p", (void *) key
->fields
.structure
[i
].type
);
1089 return _mesa_hash_string(hash_key
);
1093 glsl_type::get_function_instance(const glsl_type
*return_type
,
1094 const glsl_function_param
*params
,
1095 unsigned num_params
)
1097 const glsl_type
key(return_type
, params
, num_params
);
1099 mtx_lock(&glsl_type::mutex
);
1101 if (function_types
== NULL
) {
1102 function_types
= _mesa_hash_table_create(NULL
, function_key_hash
,
1103 function_key_compare
);
1106 struct hash_entry
*entry
= _mesa_hash_table_search(function_types
, &key
);
1107 if (entry
== NULL
) {
1108 mtx_unlock(&glsl_type::mutex
);
1109 const glsl_type
*t
= new glsl_type(return_type
, params
, num_params
);
1110 mtx_lock(&glsl_type::mutex
);
1112 entry
= _mesa_hash_table_insert(function_types
, t
, (void *) t
);
1115 const glsl_type
*t
= (const glsl_type
*)entry
->data
;
1117 assert(t
->base_type
== GLSL_TYPE_FUNCTION
);
1118 assert(t
->length
== num_params
);
1120 mtx_unlock(&glsl_type::mutex
);
1127 glsl_type::get_mul_type(const glsl_type
*type_a
, const glsl_type
*type_b
)
1129 if (type_a
== type_b
) {
1131 } else if (type_a
->is_matrix() && type_b
->is_matrix()) {
1132 /* Matrix multiply. The columns of A must match the rows of B. Given
1133 * the other previously tested constraints, this means the vector type
1134 * of a row from A must be the same as the vector type of a column from
1137 if (type_a
->row_type() == type_b
->column_type()) {
1138 /* The resulting matrix has the number of columns of matrix B and
1139 * the number of rows of matrix A. We get the row count of A by
1140 * looking at the size of a vector that makes up a column. The
1141 * transpose (size of a row) is done for B.
1143 const glsl_type
*const type
=
1144 get_instance(type_a
->base_type
,
1145 type_a
->column_type()->vector_elements
,
1146 type_b
->row_type()->vector_elements
);
1147 assert(type
!= error_type
);
1151 } else if (type_a
->is_matrix()) {
1152 /* A is a matrix and B is a column vector. Columns of A must match
1153 * rows of B. Given the other previously tested constraints, this
1154 * means the vector type of a row from A must be the same as the
1155 * vector the type of B.
1157 if (type_a
->row_type() == type_b
) {
1158 /* The resulting vector has a number of elements equal to
1159 * the number of rows of matrix A. */
1160 const glsl_type
*const type
=
1161 get_instance(type_a
->base_type
,
1162 type_a
->column_type()->vector_elements
,
1164 assert(type
!= error_type
);
1169 assert(type_b
->is_matrix());
1171 /* A is a row vector and B is a matrix. Columns of A must match rows
1172 * of B. Given the other previously tested constraints, this means
1173 * the type of A must be the same as the vector type of a column from
1176 if (type_a
== type_b
->column_type()) {
1177 /* The resulting vector has a number of elements equal to
1178 * the number of columns of matrix B. */
1179 const glsl_type
*const type
=
1180 get_instance(type_a
->base_type
,
1181 type_b
->row_type()->vector_elements
,
1183 assert(type
!= error_type
);
1194 glsl_type::field_type(const char *name
) const
1196 if (this->base_type
!= GLSL_TYPE_STRUCT
1197 && this->base_type
!= GLSL_TYPE_INTERFACE
)
1200 for (unsigned i
= 0; i
< this->length
; i
++) {
1201 if (strcmp(name
, this->fields
.structure
[i
].name
) == 0)
1202 return this->fields
.structure
[i
].type
;
1210 glsl_type::field_index(const char *name
) const
1212 if (this->base_type
!= GLSL_TYPE_STRUCT
1213 && this->base_type
!= GLSL_TYPE_INTERFACE
)
1216 for (unsigned i
= 0; i
< this->length
; i
++) {
1217 if (strcmp(name
, this->fields
.structure
[i
].name
) == 0)
1226 glsl_type::component_slots() const
1228 switch (this->base_type
) {
1229 case GLSL_TYPE_UINT
:
1231 case GLSL_TYPE_FLOAT
:
1232 case GLSL_TYPE_BOOL
:
1233 return this->components();
1235 case GLSL_TYPE_DOUBLE
:
1236 return 2 * this->components();
1238 case GLSL_TYPE_STRUCT
:
1239 case GLSL_TYPE_INTERFACE
: {
1242 for (unsigned i
= 0; i
< this->length
; i
++)
1243 size
+= this->fields
.structure
[i
].type
->component_slots();
1248 case GLSL_TYPE_ARRAY
:
1249 return this->length
* this->fields
.array
->component_slots();
1251 case GLSL_TYPE_IMAGE
:
1253 case GLSL_TYPE_SUBROUTINE
:
1256 case GLSL_TYPE_FUNCTION
:
1257 case GLSL_TYPE_SAMPLER
:
1258 case GLSL_TYPE_ATOMIC_UINT
:
1259 case GLSL_TYPE_VOID
:
1260 case GLSL_TYPE_ERROR
:
1268 glsl_type::record_location_offset(unsigned length
) const
1270 unsigned offset
= 0;
1271 const glsl_type
*t
= this->without_array();
1272 if (t
->is_record()) {
1273 assert(length
<= t
->length
);
1275 for (unsigned i
= 0; i
< length
; i
++) {
1276 const glsl_type
*st
= t
->fields
.structure
[i
].type
;
1277 const glsl_type
*wa
= st
->without_array();
1278 if (wa
->is_record()) {
1279 unsigned r_offset
= wa
->record_location_offset(wa
->length
);
1280 offset
+= st
->is_array() ?
1281 st
->arrays_of_arrays_size() * r_offset
: r_offset
;
1282 } else if (st
->is_array() && st
->fields
.array
->is_array()) {
1283 unsigned outer_array_size
= st
->length
;
1284 const glsl_type
*base_type
= st
->fields
.array
;
1286 /* For arrays of arrays the outer arrays take up a uniform
1287 * slot for each element. The innermost array elements share a
1288 * single slot so we ignore the innermost array when calculating
1291 while (base_type
->fields
.array
->is_array()) {
1292 outer_array_size
= outer_array_size
* base_type
->length
;
1293 base_type
= base_type
->fields
.array
;
1295 offset
+= outer_array_size
;
1297 /* We dont worry about arrays here because unless the array
1298 * contains a structure or another array it only takes up a single
1309 glsl_type::uniform_locations() const
1313 switch (this->base_type
) {
1314 case GLSL_TYPE_UINT
:
1316 case GLSL_TYPE_FLOAT
:
1317 case GLSL_TYPE_DOUBLE
:
1318 case GLSL_TYPE_BOOL
:
1319 case GLSL_TYPE_SAMPLER
:
1320 case GLSL_TYPE_IMAGE
:
1321 case GLSL_TYPE_SUBROUTINE
:
1324 case GLSL_TYPE_STRUCT
:
1325 case GLSL_TYPE_INTERFACE
:
1326 for (unsigned i
= 0; i
< this->length
; i
++)
1327 size
+= this->fields
.structure
[i
].type
->uniform_locations();
1329 case GLSL_TYPE_ARRAY
:
1330 return this->length
* this->fields
.array
->uniform_locations();
1337 glsl_type::can_implicitly_convert_to(const glsl_type
*desired
,
1338 _mesa_glsl_parse_state
*state
) const
1340 if (this == desired
)
1343 /* ESSL does not allow implicit conversions. If there is no state, we're
1344 * doing intra-stage function linking where these checks have already been
1347 if (state
&& state
->es_shader
)
1350 /* There is no conversion among matrix types. */
1351 if (this->matrix_columns
> 1 || desired
->matrix_columns
> 1)
1354 /* Vector size must match. */
1355 if (this->vector_elements
!= desired
->vector_elements
)
1358 /* int and uint can be converted to float. */
1359 if (desired
->is_float() && this->is_integer())
1362 /* With GLSL 4.0 / ARB_gpu_shader5, int can be converted to uint.
1363 * Note that state may be NULL here, when resolving function calls in the
1364 * linker. By this time, all the state-dependent checks have already
1365 * happened though, so allow anything that's allowed in any shader version. */
1366 if ((!state
|| state
->is_version(400, 0) || state
->ARB_gpu_shader5_enable
) &&
1367 desired
->base_type
== GLSL_TYPE_UINT
&& this->base_type
== GLSL_TYPE_INT
)
1370 /* No implicit conversions from double. */
1371 if ((!state
|| state
->has_double()) && this->is_double())
1374 /* Conversions from different types to double. */
1375 if ((!state
|| state
->has_double()) && desired
->is_double()) {
1376 if (this->is_float())
1378 if (this->is_integer())
1386 glsl_type::std140_base_alignment(bool row_major
) const
1388 unsigned N
= is_double() ? 8 : 4;
1390 /* (1) If the member is a scalar consuming <N> basic machine units, the
1391 * base alignment is <N>.
1393 * (2) If the member is a two- or four-component vector with components
1394 * consuming <N> basic machine units, the base alignment is 2<N> or
1395 * 4<N>, respectively.
1397 * (3) If the member is a three-component vector with components consuming
1398 * <N> basic machine units, the base alignment is 4<N>.
1400 if (this->is_scalar() || this->is_vector()) {
1401 switch (this->vector_elements
) {
1412 /* (4) If the member is an array of scalars or vectors, the base alignment
1413 * and array stride are set to match the base alignment of a single
1414 * array element, according to rules (1), (2), and (3), and rounded up
1415 * to the base alignment of a vec4. The array may have padding at the
1416 * end; the base offset of the member following the array is rounded up
1417 * to the next multiple of the base alignment.
1419 * (6) If the member is an array of <S> column-major matrices with <C>
1420 * columns and <R> rows, the matrix is stored identically to a row of
1421 * <S>*<C> column vectors with <R> components each, according to rule
1424 * (8) If the member is an array of <S> row-major matrices with <C> columns
1425 * and <R> rows, the matrix is stored identically to a row of <S>*<R>
1426 * row vectors with <C> components each, according to rule (4).
1428 * (10) If the member is an array of <S> structures, the <S> elements of
1429 * the array are laid out in order, according to rule (9).
1431 if (this->is_array()) {
1432 if (this->fields
.array
->is_scalar() ||
1433 this->fields
.array
->is_vector() ||
1434 this->fields
.array
->is_matrix()) {
1435 return MAX2(this->fields
.array
->std140_base_alignment(row_major
), 16);
1437 assert(this->fields
.array
->is_record() ||
1438 this->fields
.array
->is_array());
1439 return this->fields
.array
->std140_base_alignment(row_major
);
1443 /* (5) If the member is a column-major matrix with <C> columns and
1444 * <R> rows, the matrix is stored identically to an array of
1445 * <C> column vectors with <R> components each, according to
1448 * (7) If the member is a row-major matrix with <C> columns and <R>
1449 * rows, the matrix is stored identically to an array of <R>
1450 * row vectors with <C> components each, according to rule (4).
1452 if (this->is_matrix()) {
1453 const struct glsl_type
*vec_type
, *array_type
;
1454 int c
= this->matrix_columns
;
1455 int r
= this->vector_elements
;
1458 vec_type
= get_instance(base_type
, c
, 1);
1459 array_type
= glsl_type::get_array_instance(vec_type
, r
);
1461 vec_type
= get_instance(base_type
, r
, 1);
1462 array_type
= glsl_type::get_array_instance(vec_type
, c
);
1465 return array_type
->std140_base_alignment(false);
1468 /* (9) If the member is a structure, the base alignment of the
1469 * structure is <N>, where <N> is the largest base alignment
1470 * value of any of its members, and rounded up to the base
1471 * alignment of a vec4. The individual members of this
1472 * sub-structure are then assigned offsets by applying this set
1473 * of rules recursively, where the base offset of the first
1474 * member of the sub-structure is equal to the aligned offset
1475 * of the structure. The structure may have padding at the end;
1476 * the base offset of the member following the sub-structure is
1477 * rounded up to the next multiple of the base alignment of the
1480 if (this->is_record()) {
1481 unsigned base_alignment
= 16;
1482 for (unsigned i
= 0; i
< this->length
; i
++) {
1483 bool field_row_major
= row_major
;
1484 const enum glsl_matrix_layout matrix_layout
=
1485 glsl_matrix_layout(this->fields
.structure
[i
].matrix_layout
);
1486 if (matrix_layout
== GLSL_MATRIX_LAYOUT_ROW_MAJOR
) {
1487 field_row_major
= true;
1488 } else if (matrix_layout
== GLSL_MATRIX_LAYOUT_COLUMN_MAJOR
) {
1489 field_row_major
= false;
1492 const struct glsl_type
*field_type
= this->fields
.structure
[i
].type
;
1493 base_alignment
= MAX2(base_alignment
,
1494 field_type
->std140_base_alignment(field_row_major
));
1496 return base_alignment
;
1499 assert(!"not reached");
1504 glsl_type::std140_size(bool row_major
) const
1506 unsigned N
= is_double() ? 8 : 4;
1508 /* (1) If the member is a scalar consuming <N> basic machine units, the
1509 * base alignment is <N>.
1511 * (2) If the member is a two- or four-component vector with components
1512 * consuming <N> basic machine units, the base alignment is 2<N> or
1513 * 4<N>, respectively.
1515 * (3) If the member is a three-component vector with components consuming
1516 * <N> basic machine units, the base alignment is 4<N>.
1518 if (this->is_scalar() || this->is_vector()) {
1519 return this->vector_elements
* N
;
1522 /* (5) If the member is a column-major matrix with <C> columns and
1523 * <R> rows, the matrix is stored identically to an array of
1524 * <C> column vectors with <R> components each, according to
1527 * (6) If the member is an array of <S> column-major matrices with <C>
1528 * columns and <R> rows, the matrix is stored identically to a row of
1529 * <S>*<C> column vectors with <R> components each, according to rule
1532 * (7) If the member is a row-major matrix with <C> columns and <R>
1533 * rows, the matrix is stored identically to an array of <R>
1534 * row vectors with <C> components each, according to rule (4).
1536 * (8) If the member is an array of <S> row-major matrices with <C> columns
1537 * and <R> rows, the matrix is stored identically to a row of <S>*<R>
1538 * row vectors with <C> components each, according to rule (4).
1540 if (this->without_array()->is_matrix()) {
1541 const struct glsl_type
*element_type
;
1542 const struct glsl_type
*vec_type
;
1543 unsigned int array_len
;
1545 if (this->is_array()) {
1546 element_type
= this->without_array();
1547 array_len
= this->arrays_of_arrays_size();
1549 element_type
= this;
1554 vec_type
= get_instance(element_type
->base_type
,
1555 element_type
->matrix_columns
, 1);
1557 array_len
*= element_type
->vector_elements
;
1559 vec_type
= get_instance(element_type
->base_type
,
1560 element_type
->vector_elements
, 1);
1561 array_len
*= element_type
->matrix_columns
;
1563 const glsl_type
*array_type
= glsl_type::get_array_instance(vec_type
,
1566 return array_type
->std140_size(false);
1569 /* (4) If the member is an array of scalars or vectors, the base alignment
1570 * and array stride are set to match the base alignment of a single
1571 * array element, according to rules (1), (2), and (3), and rounded up
1572 * to the base alignment of a vec4. The array may have padding at the
1573 * end; the base offset of the member following the array is rounded up
1574 * to the next multiple of the base alignment.
1576 * (10) If the member is an array of <S> structures, the <S> elements of
1577 * the array are laid out in order, according to rule (9).
1579 if (this->is_array()) {
1580 if (this->without_array()->is_record()) {
1581 return this->arrays_of_arrays_size() *
1582 this->without_array()->std140_size(row_major
);
1584 unsigned element_base_align
=
1585 this->without_array()->std140_base_alignment(row_major
);
1586 return this->arrays_of_arrays_size() * MAX2(element_base_align
, 16);
1590 /* (9) If the member is a structure, the base alignment of the
1591 * structure is <N>, where <N> is the largest base alignment
1592 * value of any of its members, and rounded up to the base
1593 * alignment of a vec4. The individual members of this
1594 * sub-structure are then assigned offsets by applying this set
1595 * of rules recursively, where the base offset of the first
1596 * member of the sub-structure is equal to the aligned offset
1597 * of the structure. The structure may have padding at the end;
1598 * the base offset of the member following the sub-structure is
1599 * rounded up to the next multiple of the base alignment of the
1602 if (this->is_record() || this->is_interface()) {
1604 unsigned max_align
= 0;
1606 for (unsigned i
= 0; i
< this->length
; i
++) {
1607 bool field_row_major
= row_major
;
1608 const enum glsl_matrix_layout matrix_layout
=
1609 glsl_matrix_layout(this->fields
.structure
[i
].matrix_layout
);
1610 if (matrix_layout
== GLSL_MATRIX_LAYOUT_ROW_MAJOR
) {
1611 field_row_major
= true;
1612 } else if (matrix_layout
== GLSL_MATRIX_LAYOUT_COLUMN_MAJOR
) {
1613 field_row_major
= false;
1616 const struct glsl_type
*field_type
= this->fields
.structure
[i
].type
;
1617 unsigned align
= field_type
->std140_base_alignment(field_row_major
);
1619 /* Ignore unsized arrays when calculating size */
1620 if (field_type
->is_unsized_array())
1623 size
= glsl_align(size
, align
);
1624 size
+= field_type
->std140_size(field_row_major
);
1626 max_align
= MAX2(align
, max_align
);
1628 if (field_type
->is_record() && (i
+ 1 < this->length
))
1629 size
= glsl_align(size
, 16);
1631 size
= glsl_align(size
, MAX2(max_align
, 16));
1635 assert(!"not reached");
1640 glsl_type::std430_base_alignment(bool row_major
) const
1643 unsigned N
= is_double() ? 8 : 4;
1645 /* (1) If the member is a scalar consuming <N> basic machine units, the
1646 * base alignment is <N>.
1648 * (2) If the member is a two- or four-component vector with components
1649 * consuming <N> basic machine units, the base alignment is 2<N> or
1650 * 4<N>, respectively.
1652 * (3) If the member is a three-component vector with components consuming
1653 * <N> basic machine units, the base alignment is 4<N>.
1655 if (this->is_scalar() || this->is_vector()) {
1656 switch (this->vector_elements
) {
1667 /* OpenGL 4.30 spec, section 7.6.2.2 "Standard Uniform Block Layout":
1669 * "When using the std430 storage layout, shader storage blocks will be
1670 * laid out in buffer storage identically to uniform and shader storage
1671 * blocks using the std140 layout, except that the base alignment and
1672 * stride of arrays of scalars and vectors in rule 4 and of structures
1673 * in rule 9 are not rounded up a multiple of the base alignment of a vec4.
1676 /* (1) If the member is a scalar consuming <N> basic machine units, the
1677 * base alignment is <N>.
1679 * (2) If the member is a two- or four-component vector with components
1680 * consuming <N> basic machine units, the base alignment is 2<N> or
1681 * 4<N>, respectively.
1683 * (3) If the member is a three-component vector with components consuming
1684 * <N> basic machine units, the base alignment is 4<N>.
1686 if (this->is_array())
1687 return this->fields
.array
->std430_base_alignment(row_major
);
1689 /* (5) If the member is a column-major matrix with <C> columns and
1690 * <R> rows, the matrix is stored identically to an array of
1691 * <C> column vectors with <R> components each, according to
1694 * (7) If the member is a row-major matrix with <C> columns and <R>
1695 * rows, the matrix is stored identically to an array of <R>
1696 * row vectors with <C> components each, according to rule (4).
1698 if (this->is_matrix()) {
1699 const struct glsl_type
*vec_type
, *array_type
;
1700 int c
= this->matrix_columns
;
1701 int r
= this->vector_elements
;
1704 vec_type
= get_instance(base_type
, c
, 1);
1705 array_type
= glsl_type::get_array_instance(vec_type
, r
);
1707 vec_type
= get_instance(base_type
, r
, 1);
1708 array_type
= glsl_type::get_array_instance(vec_type
, c
);
1711 return array_type
->std430_base_alignment(false);
1714 /* (9) If the member is a structure, the base alignment of the
1715 * structure is <N>, where <N> is the largest base alignment
1716 * value of any of its members, and rounded up to the base
1717 * alignment of a vec4. The individual members of this
1718 * sub-structure are then assigned offsets by applying this set
1719 * of rules recursively, where the base offset of the first
1720 * member of the sub-structure is equal to the aligned offset
1721 * of the structure. The structure may have padding at the end;
1722 * the base offset of the member following the sub-structure is
1723 * rounded up to the next multiple of the base alignment of the
1726 if (this->is_record()) {
1727 unsigned base_alignment
= 0;
1728 for (unsigned i
= 0; i
< this->length
; i
++) {
1729 bool field_row_major
= row_major
;
1730 const enum glsl_matrix_layout matrix_layout
=
1731 glsl_matrix_layout(this->fields
.structure
[i
].matrix_layout
);
1732 if (matrix_layout
== GLSL_MATRIX_LAYOUT_ROW_MAJOR
) {
1733 field_row_major
= true;
1734 } else if (matrix_layout
== GLSL_MATRIX_LAYOUT_COLUMN_MAJOR
) {
1735 field_row_major
= false;
1738 const struct glsl_type
*field_type
= this->fields
.structure
[i
].type
;
1739 base_alignment
= MAX2(base_alignment
,
1740 field_type
->std430_base_alignment(field_row_major
));
1742 assert(base_alignment
> 0);
1743 return base_alignment
;
1745 assert(!"not reached");
1750 glsl_type::std430_array_stride(bool row_major
) const
1752 unsigned N
= is_double() ? 8 : 4;
1754 /* Notice that the array stride of a vec3 is not 3 * N but 4 * N.
1755 * See OpenGL 4.30 spec, section 7.6.2.2 "Standard Uniform Block Layout"
1757 * (3) If the member is a three-component vector with components consuming
1758 * <N> basic machine units, the base alignment is 4<N>.
1760 if (this->is_vector() && this->vector_elements
== 3)
1763 /* By default use std430_size(row_major) */
1764 return this->std430_size(row_major
);
1768 glsl_type::std430_size(bool row_major
) const
1770 unsigned N
= is_double() ? 8 : 4;
1772 /* OpenGL 4.30 spec, section 7.6.2.2 "Standard Uniform Block Layout":
1774 * "When using the std430 storage layout, shader storage blocks will be
1775 * laid out in buffer storage identically to uniform and shader storage
1776 * blocks using the std140 layout, except that the base alignment and
1777 * stride of arrays of scalars and vectors in rule 4 and of structures
1778 * in rule 9 are not rounded up a multiple of the base alignment of a vec4.
1780 if (this->is_scalar() || this->is_vector())
1781 return this->vector_elements
* N
;
1783 if (this->without_array()->is_matrix()) {
1784 const struct glsl_type
*element_type
;
1785 const struct glsl_type
*vec_type
;
1786 unsigned int array_len
;
1788 if (this->is_array()) {
1789 element_type
= this->without_array();
1790 array_len
= this->arrays_of_arrays_size();
1792 element_type
= this;
1797 vec_type
= get_instance(element_type
->base_type
,
1798 element_type
->matrix_columns
, 1);
1800 array_len
*= element_type
->vector_elements
;
1802 vec_type
= get_instance(element_type
->base_type
,
1803 element_type
->vector_elements
, 1);
1804 array_len
*= element_type
->matrix_columns
;
1806 const glsl_type
*array_type
= glsl_type::get_array_instance(vec_type
,
1809 return array_type
->std430_size(false);
1812 if (this->is_array()) {
1813 if (this->without_array()->is_record())
1814 return this->arrays_of_arrays_size() *
1815 this->without_array()->std430_size(row_major
);
1817 return this->arrays_of_arrays_size() *
1818 this->without_array()->std430_base_alignment(row_major
);
1821 if (this->is_record() || this->is_interface()) {
1823 unsigned max_align
= 0;
1825 for (unsigned i
= 0; i
< this->length
; i
++) {
1826 bool field_row_major
= row_major
;
1827 const enum glsl_matrix_layout matrix_layout
=
1828 glsl_matrix_layout(this->fields
.structure
[i
].matrix_layout
);
1829 if (matrix_layout
== GLSL_MATRIX_LAYOUT_ROW_MAJOR
) {
1830 field_row_major
= true;
1831 } else if (matrix_layout
== GLSL_MATRIX_LAYOUT_COLUMN_MAJOR
) {
1832 field_row_major
= false;
1835 const struct glsl_type
*field_type
= this->fields
.structure
[i
].type
;
1836 unsigned align
= field_type
->std430_base_alignment(field_row_major
);
1837 size
= glsl_align(size
, align
);
1838 size
+= field_type
->std430_size(field_row_major
);
1840 max_align
= MAX2(align
, max_align
);
1842 size
= glsl_align(size
, max_align
);
1846 assert(!"not reached");
1851 glsl_type::count_attribute_slots(bool vertex_input_slots
) const
1853 /* From page 31 (page 37 of the PDF) of the GLSL 1.50 spec:
1855 * "A scalar input counts the same amount against this limit as a vec4,
1856 * so applications may want to consider packing groups of four
1857 * unrelated float inputs together into a vector to better utilize the
1858 * capabilities of the underlying hardware. A matrix input will use up
1859 * multiple locations. The number of locations used will equal the
1860 * number of columns in the matrix."
1862 * The spec does not explicitly say how arrays are counted. However, it
1863 * should be safe to assume the total number of slots consumed by an array
1864 * is the number of entries in the array multiplied by the number of slots
1865 * consumed by a single element of the array.
1867 * The spec says nothing about how structs are counted, because vertex
1868 * attributes are not allowed to be (or contain) structs. However, Mesa
1869 * allows varying structs, the number of varying slots taken up by a
1870 * varying struct is simply equal to the sum of the number of slots taken
1871 * up by each element.
1873 * Doubles are counted different depending on whether they are vertex
1874 * inputs or everything else. Vertex inputs from ARB_vertex_attrib_64bit
1875 * take one location no matter what size they are, otherwise dvec3/4
1876 * take two locations.
1878 switch (this->base_type
) {
1879 case GLSL_TYPE_UINT
:
1881 case GLSL_TYPE_FLOAT
:
1882 case GLSL_TYPE_BOOL
:
1883 return this->matrix_columns
;
1884 case GLSL_TYPE_DOUBLE
:
1885 if (this->vector_elements
> 2 && !vertex_input_slots
)
1886 return this->matrix_columns
* 2;
1888 return this->matrix_columns
;
1889 case GLSL_TYPE_STRUCT
:
1890 case GLSL_TYPE_INTERFACE
: {
1893 for (unsigned i
= 0; i
< this->length
; i
++)
1894 size
+= this->fields
.structure
[i
].type
->count_attribute_slots(vertex_input_slots
);
1899 case GLSL_TYPE_ARRAY
:
1900 return this->length
* this->fields
.array
->count_attribute_slots(vertex_input_slots
);
1902 case GLSL_TYPE_FUNCTION
:
1903 case GLSL_TYPE_SAMPLER
:
1904 case GLSL_TYPE_IMAGE
:
1905 case GLSL_TYPE_ATOMIC_UINT
:
1906 case GLSL_TYPE_VOID
:
1907 case GLSL_TYPE_SUBROUTINE
:
1908 case GLSL_TYPE_ERROR
:
1912 assert(!"Unexpected type in count_attribute_slots()");
1918 glsl_type::coordinate_components() const
1922 switch (sampler_dimensionality
) {
1923 case GLSL_SAMPLER_DIM_1D
:
1924 case GLSL_SAMPLER_DIM_BUF
:
1927 case GLSL_SAMPLER_DIM_2D
:
1928 case GLSL_SAMPLER_DIM_RECT
:
1929 case GLSL_SAMPLER_DIM_MS
:
1930 case GLSL_SAMPLER_DIM_EXTERNAL
:
1933 case GLSL_SAMPLER_DIM_3D
:
1934 case GLSL_SAMPLER_DIM_CUBE
:
1938 assert(!"Should not get here.");
1943 /* Array textures need an additional component for the array index, except
1944 * for cubemap array images that behave like a 2D array of interleaved
1947 if (sampler_array
&&
1948 !(base_type
== GLSL_TYPE_IMAGE
&&
1949 sampler_dimensionality
== GLSL_SAMPLER_DIM_CUBE
))
1956 * Declarations of type flyweights (glsl_type::_foo_type) and
1957 * convenience pointers (glsl_type::foo_type).
1960 #define DECL_TYPE(NAME, ...) \
1961 const glsl_type glsl_type::_##NAME##_type = glsl_type(__VA_ARGS__, #NAME); \
1962 const glsl_type *const glsl_type::NAME##_type = &glsl_type::_##NAME##_type;
1964 #define STRUCT_TYPE(NAME)
1966 #include "compiler/builtin_type_macros.h"