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), interface_row_major(0),
55 vector_elements(vector_elements
), matrix_columns(matrix_columns
),
58 /* Values of these types must fit in the two bits of
59 * glsl_type::sampled_type.
61 STATIC_ASSERT((unsigned(GLSL_TYPE_UINT
) & 3) == unsigned(GLSL_TYPE_UINT
));
62 STATIC_ASSERT((unsigned(GLSL_TYPE_INT
) & 3) == unsigned(GLSL_TYPE_INT
));
63 STATIC_ASSERT((unsigned(GLSL_TYPE_FLOAT
) & 3) == unsigned(GLSL_TYPE_FLOAT
));
65 mtx_lock(&glsl_type::mutex
);
67 init_ralloc_type_ctx();
69 this->name
= ralloc_strdup(this->mem_ctx
, name
);
71 mtx_unlock(&glsl_type::mutex
);
73 /* Neither dimension is zero or both dimensions are zero.
75 assert((vector_elements
== 0) == (matrix_columns
== 0));
76 memset(& fields
, 0, sizeof(fields
));
79 glsl_type::glsl_type(GLenum gl_type
, glsl_base_type base_type
,
80 enum glsl_sampler_dim dim
, bool shadow
, bool array
,
81 unsigned type
, const char *name
) :
84 sampler_dimensionality(dim
), sampler_shadow(shadow
),
85 sampler_array(array
), sampled_type(type
), interface_packing(0),
86 interface_row_major(0), length(0)
88 mtx_lock(&glsl_type::mutex
);
90 init_ralloc_type_ctx();
92 this->name
= ralloc_strdup(this->mem_ctx
, name
);
94 mtx_unlock(&glsl_type::mutex
);
96 memset(& fields
, 0, sizeof(fields
));
98 if (base_type
== GLSL_TYPE_SAMPLER
) {
99 /* Samplers take no storage whatsoever. */
100 matrix_columns
= vector_elements
= 0;
102 matrix_columns
= vector_elements
= 1;
106 glsl_type::glsl_type(const glsl_struct_field
*fields
, unsigned num_fields
,
109 base_type(GLSL_TYPE_STRUCT
),
110 sampler_dimensionality(0), sampler_shadow(0), sampler_array(0),
111 sampled_type(0), interface_packing(0), interface_row_major(0),
112 vector_elements(0), matrix_columns(0),
117 mtx_lock(&glsl_type::mutex
);
119 init_ralloc_type_ctx();
120 assert(name
!= NULL
);
121 this->name
= ralloc_strdup(this->mem_ctx
, name
);
122 this->fields
.structure
= ralloc_array(this->mem_ctx
,
123 glsl_struct_field
, length
);
125 for (i
= 0; i
< length
; i
++) {
126 this->fields
.structure
[i
].type
= fields
[i
].type
;
127 this->fields
.structure
[i
].name
= ralloc_strdup(this->fields
.structure
,
129 this->fields
.structure
[i
].location
= fields
[i
].location
;
130 this->fields
.structure
[i
].offset
= fields
[i
].offset
;
131 this->fields
.structure
[i
].interpolation
= fields
[i
].interpolation
;
132 this->fields
.structure
[i
].centroid
= fields
[i
].centroid
;
133 this->fields
.structure
[i
].sample
= fields
[i
].sample
;
134 this->fields
.structure
[i
].matrix_layout
= fields
[i
].matrix_layout
;
135 this->fields
.structure
[i
].patch
= fields
[i
].patch
;
136 this->fields
.structure
[i
].image_read_only
= fields
[i
].image_read_only
;
137 this->fields
.structure
[i
].image_write_only
= fields
[i
].image_write_only
;
138 this->fields
.structure
[i
].image_coherent
= fields
[i
].image_coherent
;
139 this->fields
.structure
[i
].image_volatile
= fields
[i
].image_volatile
;
140 this->fields
.structure
[i
].image_restrict
= fields
[i
].image_restrict
;
141 this->fields
.structure
[i
].precision
= fields
[i
].precision
;
142 this->fields
.structure
[i
].explicit_xfb_buffer
=
143 fields
[i
].explicit_xfb_buffer
;
144 this->fields
.structure
[i
].xfb_buffer
= fields
[i
].xfb_buffer
;
145 this->fields
.structure
[i
].xfb_stride
= fields
[i
].xfb_stride
;
148 mtx_unlock(&glsl_type::mutex
);
151 glsl_type::glsl_type(const glsl_struct_field
*fields
, unsigned num_fields
,
152 enum glsl_interface_packing packing
,
153 bool row_major
, const char *name
) :
155 base_type(GLSL_TYPE_INTERFACE
),
156 sampler_dimensionality(0), sampler_shadow(0), sampler_array(0),
157 sampled_type(0), interface_packing((unsigned) packing
),
158 interface_row_major((unsigned) row_major
),
159 vector_elements(0), matrix_columns(0),
164 mtx_lock(&glsl_type::mutex
);
166 init_ralloc_type_ctx();
167 assert(name
!= NULL
);
168 this->name
= ralloc_strdup(this->mem_ctx
, name
);
169 this->fields
.structure
= ralloc_array(this->mem_ctx
,
170 glsl_struct_field
, length
);
171 for (i
= 0; i
< length
; i
++) {
172 this->fields
.structure
[i
].type
= fields
[i
].type
;
173 this->fields
.structure
[i
].name
= ralloc_strdup(this->fields
.structure
,
175 this->fields
.structure
[i
].location
= fields
[i
].location
;
176 this->fields
.structure
[i
].offset
= fields
[i
].offset
;
177 this->fields
.structure
[i
].interpolation
= fields
[i
].interpolation
;
178 this->fields
.structure
[i
].centroid
= fields
[i
].centroid
;
179 this->fields
.structure
[i
].sample
= fields
[i
].sample
;
180 this->fields
.structure
[i
].matrix_layout
= fields
[i
].matrix_layout
;
181 this->fields
.structure
[i
].patch
= fields
[i
].patch
;
182 this->fields
.structure
[i
].image_read_only
= fields
[i
].image_read_only
;
183 this->fields
.structure
[i
].image_write_only
= fields
[i
].image_write_only
;
184 this->fields
.structure
[i
].image_coherent
= fields
[i
].image_coherent
;
185 this->fields
.structure
[i
].image_volatile
= fields
[i
].image_volatile
;
186 this->fields
.structure
[i
].image_restrict
= fields
[i
].image_restrict
;
187 this->fields
.structure
[i
].precision
= fields
[i
].precision
;
188 this->fields
.structure
[i
].explicit_xfb_buffer
=
189 fields
[i
].explicit_xfb_buffer
;
190 this->fields
.structure
[i
].xfb_buffer
= fields
[i
].xfb_buffer
;
191 this->fields
.structure
[i
].xfb_stride
= fields
[i
].xfb_stride
;
194 mtx_unlock(&glsl_type::mutex
);
197 glsl_type::glsl_type(const glsl_type
*return_type
,
198 const glsl_function_param
*params
, unsigned num_params
) :
200 base_type(GLSL_TYPE_FUNCTION
),
201 sampler_dimensionality(0), sampler_shadow(0), sampler_array(0),
202 sampled_type(0), interface_packing(0), interface_row_major(0),
203 vector_elements(0), matrix_columns(0),
208 mtx_lock(&glsl_type::mutex
);
210 init_ralloc_type_ctx();
212 this->fields
.parameters
= rzalloc_array(this->mem_ctx
,
213 glsl_function_param
, num_params
+ 1);
215 /* We store the return type as the first parameter */
216 this->fields
.parameters
[0].type
= return_type
;
217 this->fields
.parameters
[0].in
= false;
218 this->fields
.parameters
[0].out
= true;
220 /* We store the i'th parameter in slot i+1 */
221 for (i
= 0; i
< length
; i
++) {
222 this->fields
.parameters
[i
+ 1].type
= params
[i
].type
;
223 this->fields
.parameters
[i
+ 1].in
= params
[i
].in
;
224 this->fields
.parameters
[i
+ 1].out
= params
[i
].out
;
227 mtx_unlock(&glsl_type::mutex
);
230 glsl_type::glsl_type(const char *subroutine_name
) :
232 base_type(GLSL_TYPE_SUBROUTINE
),
233 sampler_dimensionality(0), sampler_shadow(0), sampler_array(0),
234 sampled_type(0), interface_packing(0), interface_row_major(0),
235 vector_elements(1), matrix_columns(1),
238 mtx_lock(&glsl_type::mutex
);
240 init_ralloc_type_ctx();
241 assert(subroutine_name
!= NULL
);
242 this->name
= ralloc_strdup(this->mem_ctx
, subroutine_name
);
243 mtx_unlock(&glsl_type::mutex
);
247 glsl_type::contains_sampler() const
249 if (this->is_array()) {
250 return this->fields
.array
->contains_sampler();
251 } else if (this->is_record()) {
252 for (unsigned int i
= 0; i
< this->length
; i
++) {
253 if (this->fields
.structure
[i
].type
->contains_sampler())
258 return this->is_sampler();
264 glsl_type::contains_integer() const
266 if (this->is_array()) {
267 return this->fields
.array
->contains_integer();
268 } else if (this->is_record()) {
269 for (unsigned int i
= 0; i
< this->length
; i
++) {
270 if (this->fields
.structure
[i
].type
->contains_integer())
275 return this->is_integer();
280 glsl_type::contains_double() const
282 if (this->is_array()) {
283 return this->fields
.array
->contains_double();
284 } else if (this->is_record()) {
285 for (unsigned int i
= 0; i
< this->length
; i
++) {
286 if (this->fields
.structure
[i
].type
->contains_double())
291 return this->is_double();
296 glsl_type::contains_opaque() const {
298 case GLSL_TYPE_SAMPLER
:
299 case GLSL_TYPE_IMAGE
:
300 case GLSL_TYPE_ATOMIC_UINT
:
302 case GLSL_TYPE_ARRAY
:
303 return fields
.array
->contains_opaque();
304 case GLSL_TYPE_STRUCT
:
305 for (unsigned int i
= 0; i
< length
; i
++) {
306 if (fields
.structure
[i
].type
->contains_opaque())
316 glsl_type::contains_subroutine() const
318 if (this->is_array()) {
319 return this->fields
.array
->contains_subroutine();
320 } else if (this->is_record()) {
321 for (unsigned int i
= 0; i
< this->length
; i
++) {
322 if (this->fields
.structure
[i
].type
->contains_subroutine())
327 return this->is_subroutine();
332 glsl_type::sampler_index() const
334 const glsl_type
*const t
= (this->is_array()) ? this->fields
.array
: this;
336 assert(t
->is_sampler());
338 switch (t
->sampler_dimensionality
) {
339 case GLSL_SAMPLER_DIM_1D
:
340 return (t
->sampler_array
) ? TEXTURE_1D_ARRAY_INDEX
: TEXTURE_1D_INDEX
;
341 case GLSL_SAMPLER_DIM_2D
:
342 return (t
->sampler_array
) ? TEXTURE_2D_ARRAY_INDEX
: TEXTURE_2D_INDEX
;
343 case GLSL_SAMPLER_DIM_3D
:
344 return TEXTURE_3D_INDEX
;
345 case GLSL_SAMPLER_DIM_CUBE
:
346 return (t
->sampler_array
) ? TEXTURE_CUBE_ARRAY_INDEX
: TEXTURE_CUBE_INDEX
;
347 case GLSL_SAMPLER_DIM_RECT
:
348 return TEXTURE_RECT_INDEX
;
349 case GLSL_SAMPLER_DIM_BUF
:
350 return TEXTURE_BUFFER_INDEX
;
351 case GLSL_SAMPLER_DIM_EXTERNAL
:
352 return TEXTURE_EXTERNAL_INDEX
;
353 case GLSL_SAMPLER_DIM_MS
:
354 return (t
->sampler_array
) ? TEXTURE_2D_MULTISAMPLE_ARRAY_INDEX
: TEXTURE_2D_MULTISAMPLE_INDEX
;
356 assert(!"Should not get here.");
357 return TEXTURE_BUFFER_INDEX
;
362 glsl_type::contains_image() const
364 if (this->is_array()) {
365 return this->fields
.array
->contains_image();
366 } else if (this->is_record()) {
367 for (unsigned int i
= 0; i
< this->length
; i
++) {
368 if (this->fields
.structure
[i
].type
->contains_image())
373 return this->is_image();
377 const glsl_type
*glsl_type::get_base_type() const
384 case GLSL_TYPE_FLOAT
:
386 case GLSL_TYPE_DOUBLE
:
396 const glsl_type
*glsl_type::get_scalar_type() const
398 const glsl_type
*type
= this;
401 while (type
->base_type
== GLSL_TYPE_ARRAY
)
402 type
= type
->fields
.array
;
404 /* Handle vectors and matrices */
405 switch (type
->base_type
) {
410 case GLSL_TYPE_FLOAT
:
412 case GLSL_TYPE_DOUBLE
:
417 /* Handle everything else */
424 _mesa_glsl_release_types(void)
426 /* Should only be called during atexit (either when unloading shared
427 * object, or if process terminates), so no mutex-locking should be
430 if (glsl_type::array_types
!= NULL
) {
431 _mesa_hash_table_destroy(glsl_type::array_types
, NULL
);
432 glsl_type::array_types
= NULL
;
435 if (glsl_type::record_types
!= NULL
) {
436 _mesa_hash_table_destroy(glsl_type::record_types
, NULL
);
437 glsl_type::record_types
= NULL
;
440 if (glsl_type::interface_types
!= NULL
) {
441 _mesa_hash_table_destroy(glsl_type::interface_types
, NULL
);
442 glsl_type::interface_types
= NULL
;
447 glsl_type::glsl_type(const glsl_type
*array
, unsigned length
) :
448 base_type(GLSL_TYPE_ARRAY
),
449 sampler_dimensionality(0), sampler_shadow(0), sampler_array(0),
450 sampled_type(0), interface_packing(0), interface_row_major(0),
451 vector_elements(0), matrix_columns(0),
452 length(length
), name(NULL
)
454 this->fields
.array
= array
;
455 /* Inherit the gl type of the base. The GL type is used for
456 * uniform/statevar handling in Mesa and the arrayness of the type
457 * is represented by the size rather than the type.
459 this->gl_type
= array
->gl_type
;
461 /* Allow a maximum of 10 characters for the array size. This is enough
462 * for 32-bits of ~0. The extra 3 are for the '[', ']', and terminating
465 const unsigned name_length
= strlen(array
->name
) + 10 + 3;
467 mtx_lock(&glsl_type::mutex
);
468 char *const n
= (char *) ralloc_size(this->mem_ctx
, name_length
);
469 mtx_unlock(&glsl_type::mutex
);
472 snprintf(n
, name_length
, "%s[]", array
->name
);
474 /* insert outermost dimensions in the correct spot
475 * otherwise the dimension order will be backwards
477 const char *pos
= strchr(array
->name
, '[');
479 int idx
= pos
- array
->name
;
480 snprintf(n
, idx
+1, "%s", array
->name
);
481 snprintf(n
+ idx
, name_length
- idx
, "[%u]%s",
482 length
, array
->name
+ idx
);
484 snprintf(n
, name_length
, "%s[%u]", array
->name
, length
);
493 glsl_type::vec(unsigned components
)
495 if (components
== 0 || components
> 4)
498 static const glsl_type
*const ts
[] = {
499 float_type
, vec2_type
, vec3_type
, vec4_type
501 return ts
[components
- 1];
505 glsl_type::dvec(unsigned components
)
507 if (components
== 0 || components
> 4)
510 static const glsl_type
*const ts
[] = {
511 double_type
, dvec2_type
, dvec3_type
, dvec4_type
513 return ts
[components
- 1];
517 glsl_type::ivec(unsigned components
)
519 if (components
== 0 || components
> 4)
522 static const glsl_type
*const ts
[] = {
523 int_type
, ivec2_type
, ivec3_type
, ivec4_type
525 return ts
[components
- 1];
530 glsl_type::uvec(unsigned components
)
532 if (components
== 0 || components
> 4)
535 static const glsl_type
*const ts
[] = {
536 uint_type
, uvec2_type
, uvec3_type
, uvec4_type
538 return ts
[components
- 1];
543 glsl_type::bvec(unsigned components
)
545 if (components
== 0 || components
> 4)
548 static const glsl_type
*const ts
[] = {
549 bool_type
, bvec2_type
, bvec3_type
, bvec4_type
551 return ts
[components
- 1];
556 glsl_type::get_instance(unsigned base_type
, unsigned rows
, unsigned columns
)
558 if (base_type
== GLSL_TYPE_VOID
)
561 if ((rows
< 1) || (rows
> 4) || (columns
< 1) || (columns
> 4))
564 /* Treat GLSL vectors as Nx1 matrices.
572 case GLSL_TYPE_FLOAT
:
574 case GLSL_TYPE_DOUBLE
:
582 if ((base_type
!= GLSL_TYPE_FLOAT
&& base_type
!= GLSL_TYPE_DOUBLE
) || (rows
== 1))
585 /* GLSL matrix types are named mat{COLUMNS}x{ROWS}. Only the following
586 * combinations are valid:
594 #define IDX(c,r) (((c-1)*3) + (r-1))
596 if (base_type
== GLSL_TYPE_DOUBLE
) {
597 switch (IDX(columns
, rows
)) {
598 case IDX(2,2): return dmat2_type
;
599 case IDX(2,3): return dmat2x3_type
;
600 case IDX(2,4): return dmat2x4_type
;
601 case IDX(3,2): return dmat3x2_type
;
602 case IDX(3,3): return dmat3_type
;
603 case IDX(3,4): return dmat3x4_type
;
604 case IDX(4,2): return dmat4x2_type
;
605 case IDX(4,3): return dmat4x3_type
;
606 case IDX(4,4): return dmat4_type
;
607 default: return error_type
;
610 switch (IDX(columns
, rows
)) {
611 case IDX(2,2): return mat2_type
;
612 case IDX(2,3): return mat2x3_type
;
613 case IDX(2,4): return mat2x4_type
;
614 case IDX(3,2): return mat3x2_type
;
615 case IDX(3,3): return mat3_type
;
616 case IDX(3,4): return mat3x4_type
;
617 case IDX(4,2): return mat4x2_type
;
618 case IDX(4,3): return mat4x3_type
;
619 case IDX(4,4): return mat4_type
;
620 default: return error_type
;
625 assert(!"Should not get here.");
630 glsl_type::get_sampler_instance(enum glsl_sampler_dim dim
,
636 case GLSL_TYPE_FLOAT
:
638 case GLSL_SAMPLER_DIM_1D
:
640 return (array
? sampler1DArrayShadow_type
: sampler1DShadow_type
);
642 return (array
? sampler1DArray_type
: sampler1D_type
);
643 case GLSL_SAMPLER_DIM_2D
:
645 return (array
? sampler2DArrayShadow_type
: sampler2DShadow_type
);
647 return (array
? sampler2DArray_type
: sampler2D_type
);
648 case GLSL_SAMPLER_DIM_3D
:
652 return sampler3D_type
;
653 case GLSL_SAMPLER_DIM_CUBE
:
655 return (array
? samplerCubeArrayShadow_type
: samplerCubeShadow_type
);
657 return (array
? samplerCubeArray_type
: samplerCube_type
);
658 case GLSL_SAMPLER_DIM_RECT
:
662 return sampler2DRectShadow_type
;
664 return sampler2DRect_type
;
665 case GLSL_SAMPLER_DIM_BUF
:
669 return samplerBuffer_type
;
670 case GLSL_SAMPLER_DIM_MS
:
673 return (array
? sampler2DMSArray_type
: sampler2DMS_type
);
674 case GLSL_SAMPLER_DIM_EXTERNAL
:
678 return samplerExternalOES_type
;
679 case GLSL_SAMPLER_DIM_SUBPASS
:
686 case GLSL_SAMPLER_DIM_1D
:
687 return (array
? isampler1DArray_type
: isampler1D_type
);
688 case GLSL_SAMPLER_DIM_2D
:
689 return (array
? isampler2DArray_type
: isampler2D_type
);
690 case GLSL_SAMPLER_DIM_3D
:
693 return isampler3D_type
;
694 case GLSL_SAMPLER_DIM_CUBE
:
695 return (array
? isamplerCubeArray_type
: isamplerCube_type
);
696 case GLSL_SAMPLER_DIM_RECT
:
699 return isampler2DRect_type
;
700 case GLSL_SAMPLER_DIM_BUF
:
703 return isamplerBuffer_type
;
704 case GLSL_SAMPLER_DIM_MS
:
705 return (array
? isampler2DMSArray_type
: isampler2DMS_type
);
706 case GLSL_SAMPLER_DIM_EXTERNAL
:
708 case GLSL_SAMPLER_DIM_SUBPASS
:
715 case GLSL_SAMPLER_DIM_1D
:
716 return (array
? usampler1DArray_type
: usampler1D_type
);
717 case GLSL_SAMPLER_DIM_2D
:
718 return (array
? usampler2DArray_type
: usampler2D_type
);
719 case GLSL_SAMPLER_DIM_3D
:
722 return usampler3D_type
;
723 case GLSL_SAMPLER_DIM_CUBE
:
724 return (array
? usamplerCubeArray_type
: usamplerCube_type
);
725 case GLSL_SAMPLER_DIM_RECT
:
728 return usampler2DRect_type
;
729 case GLSL_SAMPLER_DIM_BUF
:
732 return usamplerBuffer_type
;
733 case GLSL_SAMPLER_DIM_MS
:
734 return (array
? usampler2DMSArray_type
: usampler2DMS_type
);
735 case GLSL_SAMPLER_DIM_EXTERNAL
:
737 case GLSL_SAMPLER_DIM_SUBPASS
:
744 unreachable("switch statement above should be complete");
748 glsl_type::get_image_instance(enum glsl_sampler_dim dim
,
749 bool array
, glsl_base_type type
)
751 if (dim
== GLSL_SAMPLER_DIM_SUBPASS
)
752 return subpassInput_type
;
754 case GLSL_TYPE_FLOAT
:
756 case GLSL_SAMPLER_DIM_1D
:
757 return (array
? image1DArray_type
: image1D_type
);
758 case GLSL_SAMPLER_DIM_2D
:
759 return (array
? image2DArray_type
: image2D_type
);
760 case GLSL_SAMPLER_DIM_3D
:
762 case GLSL_SAMPLER_DIM_CUBE
:
763 return (array
? imageCubeArray_type
: imageCube_type
);
764 case GLSL_SAMPLER_DIM_RECT
:
768 return image2DRect_type
;
769 case GLSL_SAMPLER_DIM_BUF
:
773 return imageBuffer_type
;
774 case GLSL_SAMPLER_DIM_MS
:
775 return (array
? image2DMSArray_type
: image2DMS_type
);
776 case GLSL_SAMPLER_DIM_EXTERNAL
:
777 case GLSL_SAMPLER_DIM_SUBPASS
:
782 case GLSL_SAMPLER_DIM_1D
:
783 return (array
? iimage1DArray_type
: iimage1D_type
);
784 case GLSL_SAMPLER_DIM_2D
:
785 return (array
? iimage2DArray_type
: iimage2D_type
);
786 case GLSL_SAMPLER_DIM_3D
:
789 return iimage3D_type
;
790 case GLSL_SAMPLER_DIM_CUBE
:
791 return (array
? iimageCubeArray_type
: iimageCube_type
);
792 case GLSL_SAMPLER_DIM_RECT
:
795 return iimage2DRect_type
;
796 case GLSL_SAMPLER_DIM_BUF
:
799 return iimageBuffer_type
;
800 case GLSL_SAMPLER_DIM_MS
:
801 return (array
? iimage2DMSArray_type
: iimage2DMS_type
);
802 case GLSL_SAMPLER_DIM_EXTERNAL
:
803 case GLSL_SAMPLER_DIM_SUBPASS
:
808 case GLSL_SAMPLER_DIM_1D
:
809 return (array
? uimage1DArray_type
: uimage1D_type
);
810 case GLSL_SAMPLER_DIM_2D
:
811 return (array
? uimage2DArray_type
: uimage2D_type
);
812 case GLSL_SAMPLER_DIM_3D
:
815 return uimage3D_type
;
816 case GLSL_SAMPLER_DIM_CUBE
:
817 return (array
? uimageCubeArray_type
: uimageCube_type
);
818 case GLSL_SAMPLER_DIM_RECT
:
821 return uimage2DRect_type
;
822 case GLSL_SAMPLER_DIM_BUF
:
825 return uimageBuffer_type
;
826 case GLSL_SAMPLER_DIM_MS
:
827 return (array
? uimage2DMSArray_type
: uimage2DMS_type
);
828 case GLSL_SAMPLER_DIM_EXTERNAL
:
829 case GLSL_SAMPLER_DIM_SUBPASS
:
836 unreachable("switch statement above should be complete");
840 glsl_type::get_array_instance(const glsl_type
*base
, unsigned array_size
)
842 /* Generate a name using the base type pointer in the key. This is
843 * done because the name of the base type may not be unique across
844 * shaders. For example, two shaders may have different record types
848 snprintf(key
, sizeof(key
), "%p[%u]", (void *) base
, array_size
);
850 mtx_lock(&glsl_type::mutex
);
852 if (array_types
== NULL
) {
853 array_types
= _mesa_hash_table_create(NULL
, _mesa_key_hash_string
,
854 _mesa_key_string_equal
);
857 const struct hash_entry
*entry
= _mesa_hash_table_search(array_types
, key
);
859 mtx_unlock(&glsl_type::mutex
);
860 const glsl_type
*t
= new glsl_type(base
, array_size
);
861 mtx_lock(&glsl_type::mutex
);
863 entry
= _mesa_hash_table_insert(array_types
,
864 ralloc_strdup(mem_ctx
, key
),
868 assert(((glsl_type
*) entry
->data
)->base_type
== GLSL_TYPE_ARRAY
);
869 assert(((glsl_type
*) entry
->data
)->length
== array_size
);
870 assert(((glsl_type
*) entry
->data
)->fields
.array
== base
);
872 mtx_unlock(&glsl_type::mutex
);
874 return (glsl_type
*) entry
->data
;
879 glsl_type::record_compare(const glsl_type
*b
, bool match_locations
) const
881 if (this->length
!= b
->length
)
884 if (this->interface_packing
!= b
->interface_packing
)
887 if (this->interface_row_major
!= b
->interface_row_major
)
890 /* From the GLSL 4.20 specification (Sec 4.2):
892 * "Structures must have the same name, sequence of type names, and
893 * type definitions, and field names to be considered the same type."
895 * GLSL ES behaves the same (Ver 1.00 Sec 4.2.4, Ver 3.00 Sec 4.2.5).
897 * Note that we cannot force type name check when comparing unnamed
898 * structure types, these have a unique name assigned during parsing.
900 if (!this->is_anonymous() && !b
->is_anonymous())
901 if (strcmp(this->name
, b
->name
) != 0)
904 for (unsigned i
= 0; i
< this->length
; i
++) {
905 if (this->fields
.structure
[i
].type
!= b
->fields
.structure
[i
].type
)
907 if (strcmp(this->fields
.structure
[i
].name
,
908 b
->fields
.structure
[i
].name
) != 0)
910 if (this->fields
.structure
[i
].matrix_layout
911 != b
->fields
.structure
[i
].matrix_layout
)
913 if (match_locations
&& this->fields
.structure
[i
].location
914 != b
->fields
.structure
[i
].location
)
916 if (this->fields
.structure
[i
].offset
917 != b
->fields
.structure
[i
].offset
)
919 if (this->fields
.structure
[i
].interpolation
920 != b
->fields
.structure
[i
].interpolation
)
922 if (this->fields
.structure
[i
].centroid
923 != b
->fields
.structure
[i
].centroid
)
925 if (this->fields
.structure
[i
].sample
926 != b
->fields
.structure
[i
].sample
)
928 if (this->fields
.structure
[i
].patch
929 != b
->fields
.structure
[i
].patch
)
931 if (this->fields
.structure
[i
].image_read_only
932 != b
->fields
.structure
[i
].image_read_only
)
934 if (this->fields
.structure
[i
].image_write_only
935 != b
->fields
.structure
[i
].image_write_only
)
937 if (this->fields
.structure
[i
].image_coherent
938 != b
->fields
.structure
[i
].image_coherent
)
940 if (this->fields
.structure
[i
].image_volatile
941 != b
->fields
.structure
[i
].image_volatile
)
943 if (this->fields
.structure
[i
].image_restrict
944 != b
->fields
.structure
[i
].image_restrict
)
946 if (this->fields
.structure
[i
].precision
947 != b
->fields
.structure
[i
].precision
)
949 if (this->fields
.structure
[i
].explicit_xfb_buffer
950 != b
->fields
.structure
[i
].explicit_xfb_buffer
)
952 if (this->fields
.structure
[i
].xfb_buffer
953 != b
->fields
.structure
[i
].xfb_buffer
)
955 if (this->fields
.structure
[i
].xfb_stride
956 != b
->fields
.structure
[i
].xfb_stride
)
965 glsl_type::record_key_compare(const void *a
, const void *b
)
967 const glsl_type
*const key1
= (glsl_type
*) a
;
968 const glsl_type
*const key2
= (glsl_type
*) b
;
970 return strcmp(key1
->name
, key2
->name
) == 0 && key1
->record_compare(key2
);
975 * Generate an integer hash value for a glsl_type structure type.
978 glsl_type::record_key_hash(const void *a
)
980 const glsl_type
*const key
= (glsl_type
*) a
;
981 uintptr_t hash
= key
->length
;
984 for (unsigned i
= 0; i
< key
->length
; i
++) {
985 /* casting pointer to uintptr_t */
986 hash
= (hash
* 13 ) + (uintptr_t) key
->fields
.structure
[i
].type
;
989 if (sizeof(hash
) == 8)
990 retval
= (hash
& 0xffffffff) ^ ((uint64_t) hash
>> 32);
999 glsl_type::get_record_instance(const glsl_struct_field
*fields
,
1000 unsigned num_fields
,
1003 const glsl_type
key(fields
, num_fields
, name
);
1005 mtx_lock(&glsl_type::mutex
);
1007 if (record_types
== NULL
) {
1008 record_types
= _mesa_hash_table_create(NULL
, record_key_hash
,
1009 record_key_compare
);
1012 const struct hash_entry
*entry
= _mesa_hash_table_search(record_types
,
1014 if (entry
== NULL
) {
1015 mtx_unlock(&glsl_type::mutex
);
1016 const glsl_type
*t
= new glsl_type(fields
, num_fields
, name
);
1017 mtx_lock(&glsl_type::mutex
);
1019 entry
= _mesa_hash_table_insert(record_types
, t
, (void *) t
);
1022 assert(((glsl_type
*) entry
->data
)->base_type
== GLSL_TYPE_STRUCT
);
1023 assert(((glsl_type
*) entry
->data
)->length
== num_fields
);
1024 assert(strcmp(((glsl_type
*) entry
->data
)->name
, name
) == 0);
1026 mtx_unlock(&glsl_type::mutex
);
1028 return (glsl_type
*) entry
->data
;
1033 glsl_type::get_interface_instance(const glsl_struct_field
*fields
,
1034 unsigned num_fields
,
1035 enum glsl_interface_packing packing
,
1037 const char *block_name
)
1039 const glsl_type
key(fields
, num_fields
, packing
, row_major
, block_name
);
1041 mtx_lock(&glsl_type::mutex
);
1043 if (interface_types
== NULL
) {
1044 interface_types
= _mesa_hash_table_create(NULL
, record_key_hash
,
1045 record_key_compare
);
1048 const struct hash_entry
*entry
= _mesa_hash_table_search(interface_types
,
1050 if (entry
== NULL
) {
1051 mtx_unlock(&glsl_type::mutex
);
1052 const glsl_type
*t
= new glsl_type(fields
, num_fields
,
1053 packing
, row_major
, block_name
);
1054 mtx_lock(&glsl_type::mutex
);
1056 entry
= _mesa_hash_table_insert(interface_types
, t
, (void *) t
);
1059 assert(((glsl_type
*) entry
->data
)->base_type
== GLSL_TYPE_INTERFACE
);
1060 assert(((glsl_type
*) entry
->data
)->length
== num_fields
);
1061 assert(strcmp(((glsl_type
*) entry
->data
)->name
, block_name
) == 0);
1063 mtx_unlock(&glsl_type::mutex
);
1065 return (glsl_type
*) entry
->data
;
1069 glsl_type::get_subroutine_instance(const char *subroutine_name
)
1071 const glsl_type
key(subroutine_name
);
1073 mtx_lock(&glsl_type::mutex
);
1075 if (subroutine_types
== NULL
) {
1076 subroutine_types
= _mesa_hash_table_create(NULL
, record_key_hash
,
1077 record_key_compare
);
1080 const struct hash_entry
*entry
= _mesa_hash_table_search(subroutine_types
,
1082 if (entry
== NULL
) {
1083 mtx_unlock(&glsl_type::mutex
);
1084 const glsl_type
*t
= new glsl_type(subroutine_name
);
1085 mtx_lock(&glsl_type::mutex
);
1087 entry
= _mesa_hash_table_insert(subroutine_types
, t
, (void *) t
);
1090 assert(((glsl_type
*) entry
->data
)->base_type
== GLSL_TYPE_SUBROUTINE
);
1091 assert(strcmp(((glsl_type
*) entry
->data
)->name
, subroutine_name
) == 0);
1093 mtx_unlock(&glsl_type::mutex
);
1095 return (glsl_type
*) entry
->data
;
1100 function_key_compare(const void *a
, const void *b
)
1102 const glsl_type
*const key1
= (glsl_type
*) a
;
1103 const glsl_type
*const key2
= (glsl_type
*) b
;
1105 if (key1
->length
!= key2
->length
)
1108 return memcmp(key1
->fields
.parameters
, key2
->fields
.parameters
,
1109 (key1
->length
+ 1) * sizeof(*key1
->fields
.parameters
)) == 0;
1114 function_key_hash(const void *a
)
1116 const glsl_type
*const key
= (glsl_type
*) a
;
1117 return _mesa_hash_data(key
->fields
.parameters
,
1118 (key
->length
+ 1) * sizeof(*key
->fields
.parameters
));
1122 glsl_type::get_function_instance(const glsl_type
*return_type
,
1123 const glsl_function_param
*params
,
1124 unsigned num_params
)
1126 const glsl_type
key(return_type
, params
, num_params
);
1128 mtx_lock(&glsl_type::mutex
);
1130 if (function_types
== NULL
) {
1131 function_types
= _mesa_hash_table_create(NULL
, function_key_hash
,
1132 function_key_compare
);
1135 struct hash_entry
*entry
= _mesa_hash_table_search(function_types
, &key
);
1136 if (entry
== NULL
) {
1137 mtx_unlock(&glsl_type::mutex
);
1138 const glsl_type
*t
= new glsl_type(return_type
, params
, num_params
);
1139 mtx_lock(&glsl_type::mutex
);
1141 entry
= _mesa_hash_table_insert(function_types
, t
, (void *) t
);
1144 const glsl_type
*t
= (const glsl_type
*)entry
->data
;
1146 assert(t
->base_type
== GLSL_TYPE_FUNCTION
);
1147 assert(t
->length
== num_params
);
1149 mtx_unlock(&glsl_type::mutex
);
1156 glsl_type::get_mul_type(const glsl_type
*type_a
, const glsl_type
*type_b
)
1158 if (type_a
== type_b
) {
1160 } else if (type_a
->is_matrix() && type_b
->is_matrix()) {
1161 /* Matrix multiply. The columns of A must match the rows of B. Given
1162 * the other previously tested constraints, this means the vector type
1163 * of a row from A must be the same as the vector type of a column from
1166 if (type_a
->row_type() == type_b
->column_type()) {
1167 /* The resulting matrix has the number of columns of matrix B and
1168 * the number of rows of matrix A. We get the row count of A by
1169 * looking at the size of a vector that makes up a column. The
1170 * transpose (size of a row) is done for B.
1172 const glsl_type
*const type
=
1173 get_instance(type_a
->base_type
,
1174 type_a
->column_type()->vector_elements
,
1175 type_b
->row_type()->vector_elements
);
1176 assert(type
!= error_type
);
1180 } else if (type_a
->is_matrix()) {
1181 /* A is a matrix and B is a column vector. Columns of A must match
1182 * rows of B. Given the other previously tested constraints, this
1183 * means the vector type of a row from A must be the same as the
1184 * vector the type of B.
1186 if (type_a
->row_type() == type_b
) {
1187 /* The resulting vector has a number of elements equal to
1188 * the number of rows of matrix A. */
1189 const glsl_type
*const type
=
1190 get_instance(type_a
->base_type
,
1191 type_a
->column_type()->vector_elements
,
1193 assert(type
!= error_type
);
1198 assert(type_b
->is_matrix());
1200 /* A is a row vector and B is a matrix. Columns of A must match rows
1201 * of B. Given the other previously tested constraints, this means
1202 * the type of A must be the same as the vector type of a column from
1205 if (type_a
== type_b
->column_type()) {
1206 /* The resulting vector has a number of elements equal to
1207 * the number of columns of matrix B. */
1208 const glsl_type
*const type
=
1209 get_instance(type_a
->base_type
,
1210 type_b
->row_type()->vector_elements
,
1212 assert(type
!= error_type
);
1223 glsl_type::field_type(const char *name
) const
1225 if (this->base_type
!= GLSL_TYPE_STRUCT
1226 && this->base_type
!= GLSL_TYPE_INTERFACE
)
1229 for (unsigned i
= 0; i
< this->length
; i
++) {
1230 if (strcmp(name
, this->fields
.structure
[i
].name
) == 0)
1231 return this->fields
.structure
[i
].type
;
1239 glsl_type::field_index(const char *name
) const
1241 if (this->base_type
!= GLSL_TYPE_STRUCT
1242 && this->base_type
!= GLSL_TYPE_INTERFACE
)
1245 for (unsigned i
= 0; i
< this->length
; i
++) {
1246 if (strcmp(name
, this->fields
.structure
[i
].name
) == 0)
1255 glsl_type::component_slots() const
1257 switch (this->base_type
) {
1258 case GLSL_TYPE_UINT
:
1260 case GLSL_TYPE_FLOAT
:
1261 case GLSL_TYPE_BOOL
:
1262 return this->components();
1264 case GLSL_TYPE_DOUBLE
:
1265 return 2 * this->components();
1267 case GLSL_TYPE_STRUCT
:
1268 case GLSL_TYPE_INTERFACE
: {
1271 for (unsigned i
= 0; i
< this->length
; i
++)
1272 size
+= this->fields
.structure
[i
].type
->component_slots();
1277 case GLSL_TYPE_ARRAY
:
1278 return this->length
* this->fields
.array
->component_slots();
1280 case GLSL_TYPE_IMAGE
:
1282 case GLSL_TYPE_SUBROUTINE
:
1285 case GLSL_TYPE_FUNCTION
:
1286 case GLSL_TYPE_SAMPLER
:
1287 case GLSL_TYPE_ATOMIC_UINT
:
1288 case GLSL_TYPE_VOID
:
1289 case GLSL_TYPE_ERROR
:
1297 glsl_type::record_location_offset(unsigned length
) const
1299 unsigned offset
= 0;
1300 const glsl_type
*t
= this->without_array();
1301 if (t
->is_record()) {
1302 assert(length
<= t
->length
);
1304 for (unsigned i
= 0; i
< length
; i
++) {
1305 const glsl_type
*st
= t
->fields
.structure
[i
].type
;
1306 const glsl_type
*wa
= st
->without_array();
1307 if (wa
->is_record()) {
1308 unsigned r_offset
= wa
->record_location_offset(wa
->length
);
1309 offset
+= st
->is_array() ?
1310 st
->arrays_of_arrays_size() * r_offset
: r_offset
;
1311 } else if (st
->is_array() && st
->fields
.array
->is_array()) {
1312 unsigned outer_array_size
= st
->length
;
1313 const glsl_type
*base_type
= st
->fields
.array
;
1315 /* For arrays of arrays the outer arrays take up a uniform
1316 * slot for each element. The innermost array elements share a
1317 * single slot so we ignore the innermost array when calculating
1320 while (base_type
->fields
.array
->is_array()) {
1321 outer_array_size
= outer_array_size
* base_type
->length
;
1322 base_type
= base_type
->fields
.array
;
1324 offset
+= outer_array_size
;
1326 /* We dont worry about arrays here because unless the array
1327 * contains a structure or another array it only takes up a single
1338 glsl_type::uniform_locations() const
1342 switch (this->base_type
) {
1343 case GLSL_TYPE_UINT
:
1345 case GLSL_TYPE_FLOAT
:
1346 case GLSL_TYPE_DOUBLE
:
1347 case GLSL_TYPE_BOOL
:
1348 case GLSL_TYPE_SAMPLER
:
1349 case GLSL_TYPE_IMAGE
:
1350 case GLSL_TYPE_SUBROUTINE
:
1353 case GLSL_TYPE_STRUCT
:
1354 case GLSL_TYPE_INTERFACE
:
1355 for (unsigned i
= 0; i
< this->length
; i
++)
1356 size
+= this->fields
.structure
[i
].type
->uniform_locations();
1358 case GLSL_TYPE_ARRAY
:
1359 return this->length
* this->fields
.array
->uniform_locations();
1366 glsl_type::varying_count() const
1370 switch (this->base_type
) {
1371 case GLSL_TYPE_UINT
:
1373 case GLSL_TYPE_FLOAT
:
1374 case GLSL_TYPE_DOUBLE
:
1375 case GLSL_TYPE_BOOL
:
1378 case GLSL_TYPE_STRUCT
:
1379 case GLSL_TYPE_INTERFACE
:
1380 for (unsigned i
= 0; i
< this->length
; i
++)
1381 size
+= this->fields
.structure
[i
].type
->varying_count();
1383 case GLSL_TYPE_ARRAY
:
1384 /* Don't count innermost array elements */
1385 if (this->without_array()->is_record() ||
1386 this->without_array()->is_interface() ||
1387 this->fields
.array
->is_array())
1388 return this->length
* this->fields
.array
->varying_count();
1390 return this->fields
.array
->varying_count();
1392 assert(!"unsupported varying type");
1398 glsl_type::can_implicitly_convert_to(const glsl_type
*desired
,
1399 _mesa_glsl_parse_state
*state
) const
1401 if (this == desired
)
1404 /* GLSL 1.10 and ESSL do not allow implicit conversions. If there is no
1405 * state, we're doing intra-stage function linking where these checks have
1406 * already been done.
1408 if (state
&& (state
->es_shader
|| !state
->is_version(120, 0)))
1411 /* There is no conversion among matrix types. */
1412 if (this->matrix_columns
> 1 || desired
->matrix_columns
> 1)
1415 /* Vector size must match. */
1416 if (this->vector_elements
!= desired
->vector_elements
)
1419 /* int and uint can be converted to float. */
1420 if (desired
->is_float() && this->is_integer())
1423 /* With GLSL 4.0, ARB_gpu_shader5, or MESA_shader_integer_functions, int
1424 * can be converted to uint. Note that state may be NULL here, when
1425 * resolving function calls in the linker. By this time, all the
1426 * state-dependent checks have already happened though, so allow anything
1427 * that's allowed in any shader version.
1429 if ((!state
|| state
->is_version(400, 0) || state
->ARB_gpu_shader5_enable
||
1430 state
->MESA_shader_integer_functions_enable
) &&
1431 desired
->base_type
== GLSL_TYPE_UINT
&& this->base_type
== GLSL_TYPE_INT
)
1434 /* No implicit conversions from double. */
1435 if ((!state
|| state
->has_double()) && this->is_double())
1438 /* Conversions from different types to double. */
1439 if ((!state
|| state
->has_double()) && desired
->is_double()) {
1440 if (this->is_float())
1442 if (this->is_integer())
1450 glsl_type::std140_base_alignment(bool row_major
) const
1452 unsigned N
= is_64bit() ? 8 : 4;
1454 /* (1) If the member is a scalar consuming <N> basic machine units, the
1455 * base alignment is <N>.
1457 * (2) If the member is a two- or four-component vector with components
1458 * consuming <N> basic machine units, the base alignment is 2<N> or
1459 * 4<N>, respectively.
1461 * (3) If the member is a three-component vector with components consuming
1462 * <N> basic machine units, the base alignment is 4<N>.
1464 if (this->is_scalar() || this->is_vector()) {
1465 switch (this->vector_elements
) {
1476 /* (4) If the member is an array of scalars or vectors, the base alignment
1477 * and array stride are set to match the base alignment of a single
1478 * array element, according to rules (1), (2), and (3), and rounded up
1479 * to the base alignment of a vec4. The array may have padding at the
1480 * end; the base offset of the member following the array is rounded up
1481 * to the next multiple of the base alignment.
1483 * (6) If the member is an array of <S> column-major matrices with <C>
1484 * columns and <R> rows, the matrix is stored identically to a row of
1485 * <S>*<C> column vectors with <R> components each, according to rule
1488 * (8) If the member is an array of <S> row-major matrices with <C> columns
1489 * and <R> rows, the matrix is stored identically to a row of <S>*<R>
1490 * row vectors with <C> components each, according to rule (4).
1492 * (10) If the member is an array of <S> structures, the <S> elements of
1493 * the array are laid out in order, according to rule (9).
1495 if (this->is_array()) {
1496 if (this->fields
.array
->is_scalar() ||
1497 this->fields
.array
->is_vector() ||
1498 this->fields
.array
->is_matrix()) {
1499 return MAX2(this->fields
.array
->std140_base_alignment(row_major
), 16);
1501 assert(this->fields
.array
->is_record() ||
1502 this->fields
.array
->is_array());
1503 return this->fields
.array
->std140_base_alignment(row_major
);
1507 /* (5) If the member is a column-major matrix with <C> columns and
1508 * <R> rows, the matrix is stored identically to an array of
1509 * <C> column vectors with <R> components each, according to
1512 * (7) If the member is a row-major matrix with <C> columns and <R>
1513 * rows, the matrix is stored identically to an array of <R>
1514 * row vectors with <C> components each, according to rule (4).
1516 if (this->is_matrix()) {
1517 const struct glsl_type
*vec_type
, *array_type
;
1518 int c
= this->matrix_columns
;
1519 int r
= this->vector_elements
;
1522 vec_type
= get_instance(base_type
, c
, 1);
1523 array_type
= glsl_type::get_array_instance(vec_type
, r
);
1525 vec_type
= get_instance(base_type
, r
, 1);
1526 array_type
= glsl_type::get_array_instance(vec_type
, c
);
1529 return array_type
->std140_base_alignment(false);
1532 /* (9) If the member is a structure, the base alignment of the
1533 * structure is <N>, where <N> is the largest base alignment
1534 * value of any of its members, and rounded up to the base
1535 * alignment of a vec4. The individual members of this
1536 * sub-structure are then assigned offsets by applying this set
1537 * of rules recursively, where the base offset of the first
1538 * member of the sub-structure is equal to the aligned offset
1539 * of the structure. The structure may have padding at the end;
1540 * the base offset of the member following the sub-structure is
1541 * rounded up to the next multiple of the base alignment of the
1544 if (this->is_record()) {
1545 unsigned base_alignment
= 16;
1546 for (unsigned i
= 0; i
< this->length
; i
++) {
1547 bool field_row_major
= row_major
;
1548 const enum glsl_matrix_layout matrix_layout
=
1549 glsl_matrix_layout(this->fields
.structure
[i
].matrix_layout
);
1550 if (matrix_layout
== GLSL_MATRIX_LAYOUT_ROW_MAJOR
) {
1551 field_row_major
= true;
1552 } else if (matrix_layout
== GLSL_MATRIX_LAYOUT_COLUMN_MAJOR
) {
1553 field_row_major
= false;
1556 const struct glsl_type
*field_type
= this->fields
.structure
[i
].type
;
1557 base_alignment
= MAX2(base_alignment
,
1558 field_type
->std140_base_alignment(field_row_major
));
1560 return base_alignment
;
1563 assert(!"not reached");
1568 glsl_type::std140_size(bool row_major
) const
1570 unsigned N
= is_64bit() ? 8 : 4;
1572 /* (1) If the member is a scalar consuming <N> basic machine units, the
1573 * base alignment is <N>.
1575 * (2) If the member is a two- or four-component vector with components
1576 * consuming <N> basic machine units, the base alignment is 2<N> or
1577 * 4<N>, respectively.
1579 * (3) If the member is a three-component vector with components consuming
1580 * <N> basic machine units, the base alignment is 4<N>.
1582 if (this->is_scalar() || this->is_vector()) {
1583 return this->vector_elements
* N
;
1586 /* (5) If the member is a column-major matrix with <C> columns and
1587 * <R> rows, the matrix is stored identically to an array of
1588 * <C> column vectors with <R> components each, according to
1591 * (6) If the member is an array of <S> column-major matrices with <C>
1592 * columns and <R> rows, the matrix is stored identically to a row of
1593 * <S>*<C> column vectors with <R> components each, according to rule
1596 * (7) If the member is a row-major matrix with <C> columns and <R>
1597 * rows, the matrix is stored identically to an array of <R>
1598 * row vectors with <C> components each, according to rule (4).
1600 * (8) If the member is an array of <S> row-major matrices with <C> columns
1601 * and <R> rows, the matrix is stored identically to a row of <S>*<R>
1602 * row vectors with <C> components each, according to rule (4).
1604 if (this->without_array()->is_matrix()) {
1605 const struct glsl_type
*element_type
;
1606 const struct glsl_type
*vec_type
;
1607 unsigned int array_len
;
1609 if (this->is_array()) {
1610 element_type
= this->without_array();
1611 array_len
= this->arrays_of_arrays_size();
1613 element_type
= this;
1618 vec_type
= get_instance(element_type
->base_type
,
1619 element_type
->matrix_columns
, 1);
1621 array_len
*= element_type
->vector_elements
;
1623 vec_type
= get_instance(element_type
->base_type
,
1624 element_type
->vector_elements
, 1);
1625 array_len
*= element_type
->matrix_columns
;
1627 const glsl_type
*array_type
= glsl_type::get_array_instance(vec_type
,
1630 return array_type
->std140_size(false);
1633 /* (4) If the member is an array of scalars or vectors, the base alignment
1634 * and array stride are set to match the base alignment of a single
1635 * array element, according to rules (1), (2), and (3), and rounded up
1636 * to the base alignment of a vec4. The array may have padding at the
1637 * end; the base offset of the member following the array is rounded up
1638 * to the next multiple of the base alignment.
1640 * (10) If the member is an array of <S> structures, the <S> elements of
1641 * the array are laid out in order, according to rule (9).
1643 if (this->is_array()) {
1644 if (this->without_array()->is_record()) {
1645 return this->arrays_of_arrays_size() *
1646 this->without_array()->std140_size(row_major
);
1648 unsigned element_base_align
=
1649 this->without_array()->std140_base_alignment(row_major
);
1650 return this->arrays_of_arrays_size() * MAX2(element_base_align
, 16);
1654 /* (9) If the member is a structure, the base alignment of the
1655 * structure is <N>, where <N> is the largest base alignment
1656 * value of any of its members, and rounded up to the base
1657 * alignment of a vec4. The individual members of this
1658 * sub-structure are then assigned offsets by applying this set
1659 * of rules recursively, where the base offset of the first
1660 * member of the sub-structure is equal to the aligned offset
1661 * of the structure. The structure may have padding at the end;
1662 * the base offset of the member following the sub-structure is
1663 * rounded up to the next multiple of the base alignment of the
1666 if (this->is_record() || this->is_interface()) {
1668 unsigned max_align
= 0;
1670 for (unsigned i
= 0; i
< this->length
; i
++) {
1671 bool field_row_major
= row_major
;
1672 const enum glsl_matrix_layout matrix_layout
=
1673 glsl_matrix_layout(this->fields
.structure
[i
].matrix_layout
);
1674 if (matrix_layout
== GLSL_MATRIX_LAYOUT_ROW_MAJOR
) {
1675 field_row_major
= true;
1676 } else if (matrix_layout
== GLSL_MATRIX_LAYOUT_COLUMN_MAJOR
) {
1677 field_row_major
= false;
1680 const struct glsl_type
*field_type
= this->fields
.structure
[i
].type
;
1681 unsigned align
= field_type
->std140_base_alignment(field_row_major
);
1683 /* Ignore unsized arrays when calculating size */
1684 if (field_type
->is_unsized_array())
1687 size
= glsl_align(size
, align
);
1688 size
+= field_type
->std140_size(field_row_major
);
1690 max_align
= MAX2(align
, max_align
);
1692 if (field_type
->is_record() && (i
+ 1 < this->length
))
1693 size
= glsl_align(size
, 16);
1695 size
= glsl_align(size
, MAX2(max_align
, 16));
1699 assert(!"not reached");
1704 glsl_type::std430_base_alignment(bool row_major
) const
1707 unsigned N
= is_64bit() ? 8 : 4;
1709 /* (1) If the member is a scalar consuming <N> basic machine units, the
1710 * base alignment is <N>.
1712 * (2) If the member is a two- or four-component vector with components
1713 * consuming <N> basic machine units, the base alignment is 2<N> or
1714 * 4<N>, respectively.
1716 * (3) If the member is a three-component vector with components consuming
1717 * <N> basic machine units, the base alignment is 4<N>.
1719 if (this->is_scalar() || this->is_vector()) {
1720 switch (this->vector_elements
) {
1731 /* OpenGL 4.30 spec, section 7.6.2.2 "Standard Uniform Block Layout":
1733 * "When using the std430 storage layout, shader storage blocks will be
1734 * laid out in buffer storage identically to uniform and shader storage
1735 * blocks using the std140 layout, except that the base alignment and
1736 * stride of arrays of scalars and vectors in rule 4 and of structures
1737 * in rule 9 are not rounded up a multiple of the base alignment of a vec4.
1740 /* (1) If the member is a scalar consuming <N> basic machine units, the
1741 * base alignment is <N>.
1743 * (2) If the member is a two- or four-component vector with components
1744 * consuming <N> basic machine units, the base alignment is 2<N> or
1745 * 4<N>, respectively.
1747 * (3) If the member is a three-component vector with components consuming
1748 * <N> basic machine units, the base alignment is 4<N>.
1750 if (this->is_array())
1751 return this->fields
.array
->std430_base_alignment(row_major
);
1753 /* (5) If the member is a column-major matrix with <C> columns and
1754 * <R> rows, the matrix is stored identically to an array of
1755 * <C> column vectors with <R> components each, according to
1758 * (7) If the member is a row-major matrix with <C> columns and <R>
1759 * rows, the matrix is stored identically to an array of <R>
1760 * row vectors with <C> components each, according to rule (4).
1762 if (this->is_matrix()) {
1763 const struct glsl_type
*vec_type
, *array_type
;
1764 int c
= this->matrix_columns
;
1765 int r
= this->vector_elements
;
1768 vec_type
= get_instance(base_type
, c
, 1);
1769 array_type
= glsl_type::get_array_instance(vec_type
, r
);
1771 vec_type
= get_instance(base_type
, r
, 1);
1772 array_type
= glsl_type::get_array_instance(vec_type
, c
);
1775 return array_type
->std430_base_alignment(false);
1778 /* (9) If the member is a structure, the base alignment of the
1779 * structure is <N>, where <N> is the largest base alignment
1780 * value of any of its members, and rounded up to the base
1781 * alignment of a vec4. The individual members of this
1782 * sub-structure are then assigned offsets by applying this set
1783 * of rules recursively, where the base offset of the first
1784 * member of the sub-structure is equal to the aligned offset
1785 * of the structure. The structure may have padding at the end;
1786 * the base offset of the member following the sub-structure is
1787 * rounded up to the next multiple of the base alignment of the
1790 if (this->is_record()) {
1791 unsigned base_alignment
= 0;
1792 for (unsigned i
= 0; i
< this->length
; i
++) {
1793 bool field_row_major
= row_major
;
1794 const enum glsl_matrix_layout matrix_layout
=
1795 glsl_matrix_layout(this->fields
.structure
[i
].matrix_layout
);
1796 if (matrix_layout
== GLSL_MATRIX_LAYOUT_ROW_MAJOR
) {
1797 field_row_major
= true;
1798 } else if (matrix_layout
== GLSL_MATRIX_LAYOUT_COLUMN_MAJOR
) {
1799 field_row_major
= false;
1802 const struct glsl_type
*field_type
= this->fields
.structure
[i
].type
;
1803 base_alignment
= MAX2(base_alignment
,
1804 field_type
->std430_base_alignment(field_row_major
));
1806 assert(base_alignment
> 0);
1807 return base_alignment
;
1809 assert(!"not reached");
1814 glsl_type::std430_array_stride(bool row_major
) const
1816 unsigned N
= is_64bit() ? 8 : 4;
1818 /* Notice that the array stride of a vec3 is not 3 * N but 4 * N.
1819 * See OpenGL 4.30 spec, section 7.6.2.2 "Standard Uniform Block Layout"
1821 * (3) If the member is a three-component vector with components consuming
1822 * <N> basic machine units, the base alignment is 4<N>.
1824 if (this->is_vector() && this->vector_elements
== 3)
1827 /* By default use std430_size(row_major) */
1828 return this->std430_size(row_major
);
1832 glsl_type::std430_size(bool row_major
) const
1834 unsigned N
= is_64bit() ? 8 : 4;
1836 /* OpenGL 4.30 spec, section 7.6.2.2 "Standard Uniform Block Layout":
1838 * "When using the std430 storage layout, shader storage blocks will be
1839 * laid out in buffer storage identically to uniform and shader storage
1840 * blocks using the std140 layout, except that the base alignment and
1841 * stride of arrays of scalars and vectors in rule 4 and of structures
1842 * in rule 9 are not rounded up a multiple of the base alignment of a vec4.
1844 if (this->is_scalar() || this->is_vector())
1845 return this->vector_elements
* N
;
1847 if (this->without_array()->is_matrix()) {
1848 const struct glsl_type
*element_type
;
1849 const struct glsl_type
*vec_type
;
1850 unsigned int array_len
;
1852 if (this->is_array()) {
1853 element_type
= this->without_array();
1854 array_len
= this->arrays_of_arrays_size();
1856 element_type
= this;
1861 vec_type
= get_instance(element_type
->base_type
,
1862 element_type
->matrix_columns
, 1);
1864 array_len
*= element_type
->vector_elements
;
1866 vec_type
= get_instance(element_type
->base_type
,
1867 element_type
->vector_elements
, 1);
1868 array_len
*= element_type
->matrix_columns
;
1870 const glsl_type
*array_type
= glsl_type::get_array_instance(vec_type
,
1873 return array_type
->std430_size(false);
1876 if (this->is_array()) {
1877 if (this->without_array()->is_record())
1878 return this->arrays_of_arrays_size() *
1879 this->without_array()->std430_size(row_major
);
1881 return this->arrays_of_arrays_size() *
1882 this->without_array()->std430_base_alignment(row_major
);
1885 if (this->is_record() || this->is_interface()) {
1887 unsigned max_align
= 0;
1889 for (unsigned i
= 0; i
< this->length
; i
++) {
1890 bool field_row_major
= row_major
;
1891 const enum glsl_matrix_layout matrix_layout
=
1892 glsl_matrix_layout(this->fields
.structure
[i
].matrix_layout
);
1893 if (matrix_layout
== GLSL_MATRIX_LAYOUT_ROW_MAJOR
) {
1894 field_row_major
= true;
1895 } else if (matrix_layout
== GLSL_MATRIX_LAYOUT_COLUMN_MAJOR
) {
1896 field_row_major
= false;
1899 const struct glsl_type
*field_type
= this->fields
.structure
[i
].type
;
1900 unsigned align
= field_type
->std430_base_alignment(field_row_major
);
1901 size
= glsl_align(size
, align
);
1902 size
+= field_type
->std430_size(field_row_major
);
1904 max_align
= MAX2(align
, max_align
);
1906 size
= glsl_align(size
, max_align
);
1910 assert(!"not reached");
1915 glsl_type::count_attribute_slots(bool is_vertex_input
) const
1917 /* From page 31 (page 37 of the PDF) of the GLSL 1.50 spec:
1919 * "A scalar input counts the same amount against this limit as a vec4,
1920 * so applications may want to consider packing groups of four
1921 * unrelated float inputs together into a vector to better utilize the
1922 * capabilities of the underlying hardware. A matrix input will use up
1923 * multiple locations. The number of locations used will equal the
1924 * number of columns in the matrix."
1926 * The spec does not explicitly say how arrays are counted. However, it
1927 * should be safe to assume the total number of slots consumed by an array
1928 * is the number of entries in the array multiplied by the number of slots
1929 * consumed by a single element of the array.
1931 * The spec says nothing about how structs are counted, because vertex
1932 * attributes are not allowed to be (or contain) structs. However, Mesa
1933 * allows varying structs, the number of varying slots taken up by a
1934 * varying struct is simply equal to the sum of the number of slots taken
1935 * up by each element.
1937 * Doubles are counted different depending on whether they are vertex
1938 * inputs or everything else. Vertex inputs from ARB_vertex_attrib_64bit
1939 * take one location no matter what size they are, otherwise dvec3/4
1940 * take two locations.
1942 switch (this->base_type
) {
1943 case GLSL_TYPE_UINT
:
1945 case GLSL_TYPE_FLOAT
:
1946 case GLSL_TYPE_BOOL
:
1947 return this->matrix_columns
;
1948 case GLSL_TYPE_DOUBLE
:
1949 if (this->vector_elements
> 2 && !is_vertex_input
)
1950 return this->matrix_columns
* 2;
1952 return this->matrix_columns
;
1953 case GLSL_TYPE_STRUCT
:
1954 case GLSL_TYPE_INTERFACE
: {
1957 for (unsigned i
= 0; i
< this->length
; i
++)
1958 size
+= this->fields
.structure
[i
].type
->count_attribute_slots(is_vertex_input
);
1963 case GLSL_TYPE_ARRAY
:
1964 return this->length
* this->fields
.array
->count_attribute_slots(is_vertex_input
);
1966 case GLSL_TYPE_FUNCTION
:
1967 case GLSL_TYPE_SAMPLER
:
1968 case GLSL_TYPE_IMAGE
:
1969 case GLSL_TYPE_ATOMIC_UINT
:
1970 case GLSL_TYPE_VOID
:
1971 case GLSL_TYPE_SUBROUTINE
:
1972 case GLSL_TYPE_ERROR
:
1976 assert(!"Unexpected type in count_attribute_slots()");
1982 glsl_type::coordinate_components() const
1986 switch (sampler_dimensionality
) {
1987 case GLSL_SAMPLER_DIM_1D
:
1988 case GLSL_SAMPLER_DIM_BUF
:
1991 case GLSL_SAMPLER_DIM_2D
:
1992 case GLSL_SAMPLER_DIM_RECT
:
1993 case GLSL_SAMPLER_DIM_MS
:
1994 case GLSL_SAMPLER_DIM_EXTERNAL
:
1995 case GLSL_SAMPLER_DIM_SUBPASS
:
1998 case GLSL_SAMPLER_DIM_3D
:
1999 case GLSL_SAMPLER_DIM_CUBE
:
2003 assert(!"Should not get here.");
2008 /* Array textures need an additional component for the array index, except
2009 * for cubemap array images that behave like a 2D array of interleaved
2012 if (sampler_array
&&
2013 !(base_type
== GLSL_TYPE_IMAGE
&&
2014 sampler_dimensionality
== GLSL_SAMPLER_DIM_CUBE
))
2021 * Declarations of type flyweights (glsl_type::_foo_type) and
2022 * convenience pointers (glsl_type::foo_type).
2025 #define DECL_TYPE(NAME, ...) \
2026 const glsl_type glsl_type::_##NAME##_type = glsl_type(__VA_ARGS__, #NAME); \
2027 const glsl_type *const glsl_type::NAME##_type = &glsl_type::_##NAME##_type;
2029 #define STRUCT_TYPE(NAME)
2031 #include "compiler/builtin_type_macros.h"