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 "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 sampler_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
), sampler_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 sampler_type(0), interface_packing(0),
105 vector_elements(0), matrix_columns(0),
110 mtx_lock(&glsl_type::mutex
);
112 init_ralloc_type_ctx();
113 assert(name
!= NULL
);
114 this->name
= ralloc_strdup(this->mem_ctx
, name
);
115 this->fields
.structure
= ralloc_array(this->mem_ctx
,
116 glsl_struct_field
, length
);
118 for (i
= 0; i
< length
; i
++) {
119 this->fields
.structure
[i
].type
= fields
[i
].type
;
120 this->fields
.structure
[i
].name
= ralloc_strdup(this->fields
.structure
,
122 this->fields
.structure
[i
].location
= fields
[i
].location
;
123 this->fields
.structure
[i
].interpolation
= fields
[i
].interpolation
;
124 this->fields
.structure
[i
].centroid
= fields
[i
].centroid
;
125 this->fields
.structure
[i
].sample
= fields
[i
].sample
;
126 this->fields
.structure
[i
].matrix_layout
= fields
[i
].matrix_layout
;
127 this->fields
.structure
[i
].patch
= fields
[i
].patch
;
128 this->fields
.structure
[i
].image_read_only
= fields
[i
].image_read_only
;
129 this->fields
.structure
[i
].image_write_only
= fields
[i
].image_write_only
;
130 this->fields
.structure
[i
].image_coherent
= fields
[i
].image_coherent
;
131 this->fields
.structure
[i
].image_volatile
= fields
[i
].image_volatile
;
132 this->fields
.structure
[i
].image_restrict
= fields
[i
].image_restrict
;
133 this->fields
.structure
[i
].precision
= fields
[i
].precision
;
136 mtx_unlock(&glsl_type::mutex
);
139 glsl_type::glsl_type(const glsl_struct_field
*fields
, unsigned num_fields
,
140 enum glsl_interface_packing packing
, const char *name
) :
142 base_type(GLSL_TYPE_INTERFACE
),
143 sampler_dimensionality(0), sampler_shadow(0), sampler_array(0),
144 sampler_type(0), interface_packing((unsigned) packing
),
145 vector_elements(0), matrix_columns(0),
150 mtx_lock(&glsl_type::mutex
);
152 init_ralloc_type_ctx();
153 assert(name
!= NULL
);
154 this->name
= ralloc_strdup(this->mem_ctx
, name
);
155 this->fields
.structure
= ralloc_array(this->mem_ctx
,
156 glsl_struct_field
, length
);
157 for (i
= 0; i
< length
; i
++) {
158 this->fields
.structure
[i
].type
= fields
[i
].type
;
159 this->fields
.structure
[i
].name
= ralloc_strdup(this->fields
.structure
,
161 this->fields
.structure
[i
].location
= fields
[i
].location
;
162 this->fields
.structure
[i
].interpolation
= fields
[i
].interpolation
;
163 this->fields
.structure
[i
].centroid
= fields
[i
].centroid
;
164 this->fields
.structure
[i
].sample
= fields
[i
].sample
;
165 this->fields
.structure
[i
].matrix_layout
= fields
[i
].matrix_layout
;
166 this->fields
.structure
[i
].patch
= fields
[i
].patch
;
167 this->fields
.structure
[i
].precision
= fields
[i
].precision
;
170 mtx_unlock(&glsl_type::mutex
);
173 glsl_type::glsl_type(const glsl_type
*return_type
,
174 const glsl_function_param
*params
, unsigned num_params
) :
176 base_type(GLSL_TYPE_FUNCTION
),
177 sampler_dimensionality(0), sampler_shadow(0), sampler_array(0),
178 sampler_type(0), interface_packing(0),
179 vector_elements(0), matrix_columns(0),
184 mtx_lock(&glsl_type::mutex
);
186 init_ralloc_type_ctx();
188 this->fields
.parameters
= rzalloc_array(this->mem_ctx
,
189 glsl_function_param
, num_params
+ 1);
191 /* We store the return type as the first parameter */
192 this->fields
.parameters
[0].type
= return_type
;
193 this->fields
.parameters
[0].in
= false;
194 this->fields
.parameters
[0].out
= true;
196 /* We store the i'th parameter in slot i+1 */
197 for (i
= 0; i
< length
; i
++) {
198 this->fields
.parameters
[i
+ 1].type
= params
[i
].type
;
199 this->fields
.parameters
[i
+ 1].in
= params
[i
].in
;
200 this->fields
.parameters
[i
+ 1].out
= params
[i
].out
;
203 mtx_unlock(&glsl_type::mutex
);
206 glsl_type::glsl_type(const char *subroutine_name
) :
208 base_type(GLSL_TYPE_SUBROUTINE
),
209 sampler_dimensionality(0), sampler_shadow(0), sampler_array(0),
210 sampler_type(0), interface_packing(0),
211 vector_elements(1), matrix_columns(1),
214 mtx_lock(&glsl_type::mutex
);
216 init_ralloc_type_ctx();
217 assert(subroutine_name
!= NULL
);
218 this->name
= ralloc_strdup(this->mem_ctx
, subroutine_name
);
219 mtx_unlock(&glsl_type::mutex
);
223 glsl_type::contains_sampler() const
225 if (this->is_array()) {
226 return this->fields
.array
->contains_sampler();
227 } else if (this->is_record()) {
228 for (unsigned int i
= 0; i
< this->length
; i
++) {
229 if (this->fields
.structure
[i
].type
->contains_sampler())
234 return this->is_sampler();
240 glsl_type::contains_integer() const
242 if (this->is_array()) {
243 return this->fields
.array
->contains_integer();
244 } else if (this->is_record()) {
245 for (unsigned int i
= 0; i
< this->length
; i
++) {
246 if (this->fields
.structure
[i
].type
->contains_integer())
251 return this->is_integer();
256 glsl_type::contains_double() const
258 if (this->is_array()) {
259 return this->fields
.array
->contains_double();
260 } else if (this->is_record()) {
261 for (unsigned int i
= 0; i
< this->length
; i
++) {
262 if (this->fields
.structure
[i
].type
->contains_double())
267 return this->is_double();
272 glsl_type::contains_opaque() const {
274 case GLSL_TYPE_SAMPLER
:
275 case GLSL_TYPE_IMAGE
:
276 case GLSL_TYPE_ATOMIC_UINT
:
278 case GLSL_TYPE_ARRAY
:
279 return fields
.array
->contains_opaque();
280 case GLSL_TYPE_STRUCT
:
281 for (unsigned int i
= 0; i
< length
; i
++) {
282 if (fields
.structure
[i
].type
->contains_opaque())
292 glsl_type::contains_subroutine() const
294 if (this->is_array()) {
295 return this->fields
.array
->contains_subroutine();
296 } else if (this->is_record()) {
297 for (unsigned int i
= 0; i
< this->length
; i
++) {
298 if (this->fields
.structure
[i
].type
->contains_subroutine())
303 return this->is_subroutine();
308 glsl_type::sampler_index() const
310 const glsl_type
*const t
= (this->is_array()) ? this->fields
.array
: this;
312 assert(t
->is_sampler());
314 switch (t
->sampler_dimensionality
) {
315 case GLSL_SAMPLER_DIM_1D
:
316 return (t
->sampler_array
) ? TEXTURE_1D_ARRAY_INDEX
: TEXTURE_1D_INDEX
;
317 case GLSL_SAMPLER_DIM_2D
:
318 return (t
->sampler_array
) ? TEXTURE_2D_ARRAY_INDEX
: TEXTURE_2D_INDEX
;
319 case GLSL_SAMPLER_DIM_3D
:
320 return TEXTURE_3D_INDEX
;
321 case GLSL_SAMPLER_DIM_CUBE
:
322 return (t
->sampler_array
) ? TEXTURE_CUBE_ARRAY_INDEX
: TEXTURE_CUBE_INDEX
;
323 case GLSL_SAMPLER_DIM_RECT
:
324 return TEXTURE_RECT_INDEX
;
325 case GLSL_SAMPLER_DIM_BUF
:
326 return TEXTURE_BUFFER_INDEX
;
327 case GLSL_SAMPLER_DIM_EXTERNAL
:
328 return TEXTURE_EXTERNAL_INDEX
;
329 case GLSL_SAMPLER_DIM_MS
:
330 return (t
->sampler_array
) ? TEXTURE_2D_MULTISAMPLE_ARRAY_INDEX
: TEXTURE_2D_MULTISAMPLE_INDEX
;
332 assert(!"Should not get here.");
333 return TEXTURE_BUFFER_INDEX
;
338 glsl_type::contains_image() const
340 if (this->is_array()) {
341 return this->fields
.array
->contains_image();
342 } else if (this->is_record()) {
343 for (unsigned int i
= 0; i
< this->length
; i
++) {
344 if (this->fields
.structure
[i
].type
->contains_image())
349 return this->is_image();
353 const glsl_type
*glsl_type::get_base_type() const
360 case GLSL_TYPE_FLOAT
:
362 case GLSL_TYPE_DOUBLE
:
372 const glsl_type
*glsl_type::get_scalar_type() const
374 const glsl_type
*type
= this;
377 while (type
->base_type
== GLSL_TYPE_ARRAY
)
378 type
= type
->fields
.array
;
380 /* Handle vectors and matrices */
381 switch (type
->base_type
) {
386 case GLSL_TYPE_FLOAT
:
388 case GLSL_TYPE_DOUBLE
:
393 /* Handle everything else */
400 _mesa_glsl_release_types(void)
402 /* Should only be called during atexit (either when unloading shared
403 * object, or if process terminates), so no mutex-locking should be
406 if (glsl_type::array_types
!= NULL
) {
407 _mesa_hash_table_destroy(glsl_type::array_types
, NULL
);
408 glsl_type::array_types
= NULL
;
411 if (glsl_type::record_types
!= NULL
) {
412 _mesa_hash_table_destroy(glsl_type::record_types
, NULL
);
413 glsl_type::record_types
= NULL
;
416 if (glsl_type::interface_types
!= NULL
) {
417 _mesa_hash_table_destroy(glsl_type::interface_types
, NULL
);
418 glsl_type::interface_types
= NULL
;
423 glsl_type::glsl_type(const glsl_type
*array
, unsigned length
) :
424 base_type(GLSL_TYPE_ARRAY
),
425 sampler_dimensionality(0), sampler_shadow(0), sampler_array(0),
426 sampler_type(0), interface_packing(0),
427 vector_elements(0), matrix_columns(0),
428 length(length
), name(NULL
)
430 this->fields
.array
= array
;
431 /* Inherit the gl type of the base. The GL type is used for
432 * uniform/statevar handling in Mesa and the arrayness of the type
433 * is represented by the size rather than the type.
435 this->gl_type
= array
->gl_type
;
437 /* Allow a maximum of 10 characters for the array size. This is enough
438 * for 32-bits of ~0. The extra 3 are for the '[', ']', and terminating
441 const unsigned name_length
= strlen(array
->name
) + 10 + 3;
443 mtx_lock(&glsl_type::mutex
);
444 char *const n
= (char *) ralloc_size(this->mem_ctx
, name_length
);
445 mtx_unlock(&glsl_type::mutex
);
448 snprintf(n
, name_length
, "%s[]", array
->name
);
450 /* insert outermost dimensions in the correct spot
451 * otherwise the dimension order will be backwards
453 const char *pos
= strchr(array
->name
, '[');
455 int idx
= pos
- array
->name
;
456 snprintf(n
, idx
+1, "%s", array
->name
);
457 snprintf(n
+ idx
, name_length
- idx
, "[%u]%s",
458 length
, array
->name
+ idx
);
460 snprintf(n
, name_length
, "%s[%u]", array
->name
, length
);
469 glsl_type::vec(unsigned components
)
471 if (components
== 0 || components
> 4)
474 static const glsl_type
*const ts
[] = {
475 float_type
, vec2_type
, vec3_type
, vec4_type
477 return ts
[components
- 1];
481 glsl_type::dvec(unsigned components
)
483 if (components
== 0 || components
> 4)
486 static const glsl_type
*const ts
[] = {
487 double_type
, dvec2_type
, dvec3_type
, dvec4_type
489 return ts
[components
- 1];
493 glsl_type::ivec(unsigned components
)
495 if (components
== 0 || components
> 4)
498 static const glsl_type
*const ts
[] = {
499 int_type
, ivec2_type
, ivec3_type
, ivec4_type
501 return ts
[components
- 1];
506 glsl_type::uvec(unsigned components
)
508 if (components
== 0 || components
> 4)
511 static const glsl_type
*const ts
[] = {
512 uint_type
, uvec2_type
, uvec3_type
, uvec4_type
514 return ts
[components
- 1];
519 glsl_type::bvec(unsigned components
)
521 if (components
== 0 || components
> 4)
524 static const glsl_type
*const ts
[] = {
525 bool_type
, bvec2_type
, bvec3_type
, bvec4_type
527 return ts
[components
- 1];
532 glsl_type::get_instance(unsigned base_type
, unsigned rows
, unsigned columns
)
534 if (base_type
== GLSL_TYPE_VOID
)
537 if ((rows
< 1) || (rows
> 4) || (columns
< 1) || (columns
> 4))
540 /* Treat GLSL vectors as Nx1 matrices.
548 case GLSL_TYPE_FLOAT
:
550 case GLSL_TYPE_DOUBLE
:
558 if ((base_type
!= GLSL_TYPE_FLOAT
&& base_type
!= GLSL_TYPE_DOUBLE
) || (rows
== 1))
561 /* GLSL matrix types are named mat{COLUMNS}x{ROWS}. Only the following
562 * combinations are valid:
570 #define IDX(c,r) (((c-1)*3) + (r-1))
572 if (base_type
== GLSL_TYPE_DOUBLE
) {
573 switch (IDX(columns
, rows
)) {
574 case IDX(2,2): return dmat2_type
;
575 case IDX(2,3): return dmat2x3_type
;
576 case IDX(2,4): return dmat2x4_type
;
577 case IDX(3,2): return dmat3x2_type
;
578 case IDX(3,3): return dmat3_type
;
579 case IDX(3,4): return dmat3x4_type
;
580 case IDX(4,2): return dmat4x2_type
;
581 case IDX(4,3): return dmat4x3_type
;
582 case IDX(4,4): return dmat4_type
;
583 default: return error_type
;
586 switch (IDX(columns
, rows
)) {
587 case IDX(2,2): return mat2_type
;
588 case IDX(2,3): return mat2x3_type
;
589 case IDX(2,4): return mat2x4_type
;
590 case IDX(3,2): return mat3x2_type
;
591 case IDX(3,3): return mat3_type
;
592 case IDX(3,4): return mat3x4_type
;
593 case IDX(4,2): return mat4x2_type
;
594 case IDX(4,3): return mat4x3_type
;
595 case IDX(4,4): return mat4_type
;
596 default: return error_type
;
601 assert(!"Should not get here.");
606 glsl_type::get_sampler_instance(enum glsl_sampler_dim dim
,
612 case GLSL_TYPE_FLOAT
:
614 case GLSL_SAMPLER_DIM_1D
:
616 return (array
? sampler1DArrayShadow_type
: sampler1DShadow_type
);
618 return (array
? sampler1DArray_type
: sampler1D_type
);
619 case GLSL_SAMPLER_DIM_2D
:
621 return (array
? sampler2DArrayShadow_type
: sampler2DShadow_type
);
623 return (array
? sampler2DArray_type
: sampler2D_type
);
624 case GLSL_SAMPLER_DIM_3D
:
628 return sampler3D_type
;
629 case GLSL_SAMPLER_DIM_CUBE
:
631 return (array
? samplerCubeArrayShadow_type
: samplerCubeShadow_type
);
633 return (array
? samplerCubeArray_type
: samplerCube_type
);
634 case GLSL_SAMPLER_DIM_RECT
:
638 return sampler2DRectShadow_type
;
640 return sampler2DRect_type
;
641 case GLSL_SAMPLER_DIM_BUF
:
645 return samplerBuffer_type
;
646 case GLSL_SAMPLER_DIM_MS
:
649 return (array
? sampler2DMSArray_type
: sampler2DMS_type
);
650 case GLSL_SAMPLER_DIM_EXTERNAL
:
654 return samplerExternalOES_type
;
660 case GLSL_SAMPLER_DIM_1D
:
661 return (array
? isampler1DArray_type
: isampler1D_type
);
662 case GLSL_SAMPLER_DIM_2D
:
663 return (array
? isampler2DArray_type
: isampler2D_type
);
664 case GLSL_SAMPLER_DIM_3D
:
667 return isampler3D_type
;
668 case GLSL_SAMPLER_DIM_CUBE
:
669 return (array
? isamplerCubeArray_type
: isamplerCube_type
);
670 case GLSL_SAMPLER_DIM_RECT
:
673 return isampler2DRect_type
;
674 case GLSL_SAMPLER_DIM_BUF
:
677 return isamplerBuffer_type
;
678 case GLSL_SAMPLER_DIM_MS
:
679 return (array
? isampler2DMSArray_type
: isampler2DMS_type
);
680 case GLSL_SAMPLER_DIM_EXTERNAL
:
687 case GLSL_SAMPLER_DIM_1D
:
688 return (array
? usampler1DArray_type
: usampler1D_type
);
689 case GLSL_SAMPLER_DIM_2D
:
690 return (array
? usampler2DArray_type
: usampler2D_type
);
691 case GLSL_SAMPLER_DIM_3D
:
694 return usampler3D_type
;
695 case GLSL_SAMPLER_DIM_CUBE
:
696 return (array
? usamplerCubeArray_type
: usamplerCube_type
);
697 case GLSL_SAMPLER_DIM_RECT
:
700 return usampler2DRect_type
;
701 case GLSL_SAMPLER_DIM_BUF
:
704 return usamplerBuffer_type
;
705 case GLSL_SAMPLER_DIM_MS
:
706 return (array
? usampler2DMSArray_type
: usampler2DMS_type
);
707 case GLSL_SAMPLER_DIM_EXTERNAL
:
714 unreachable("switch statement above should be complete");
718 glsl_type::get_image_instance(enum glsl_sampler_dim dim
,
719 bool array
, glsl_base_type type
)
722 case GLSL_TYPE_FLOAT
:
724 case GLSL_SAMPLER_DIM_1D
:
725 return (array
? image1DArray_type
: image1D_type
);
726 case GLSL_SAMPLER_DIM_2D
:
727 return (array
? image2DArray_type
: image2D_type
);
728 case GLSL_SAMPLER_DIM_3D
:
730 case GLSL_SAMPLER_DIM_CUBE
:
731 return (array
? imageCubeArray_type
: imageCube_type
);
732 case GLSL_SAMPLER_DIM_RECT
:
736 return image2DRect_type
;
737 case GLSL_SAMPLER_DIM_BUF
:
741 return imageBuffer_type
;
742 case GLSL_SAMPLER_DIM_MS
:
743 return (array
? image2DMSArray_type
: image2DMS_type
);
744 case GLSL_SAMPLER_DIM_EXTERNAL
:
749 case GLSL_SAMPLER_DIM_1D
:
750 return (array
? iimage1DArray_type
: iimage1D_type
);
751 case GLSL_SAMPLER_DIM_2D
:
752 return (array
? iimage2DArray_type
: iimage2D_type
);
753 case GLSL_SAMPLER_DIM_3D
:
756 return iimage3D_type
;
757 case GLSL_SAMPLER_DIM_CUBE
:
758 return (array
? iimageCubeArray_type
: iimageCube_type
);
759 case GLSL_SAMPLER_DIM_RECT
:
762 return iimage2DRect_type
;
763 case GLSL_SAMPLER_DIM_BUF
:
766 return iimageBuffer_type
;
767 case GLSL_SAMPLER_DIM_MS
:
768 return (array
? iimage2DMSArray_type
: iimage2DMS_type
);
769 case GLSL_SAMPLER_DIM_EXTERNAL
:
774 case GLSL_SAMPLER_DIM_1D
:
775 return (array
? uimage1DArray_type
: uimage1D_type
);
776 case GLSL_SAMPLER_DIM_2D
:
777 return (array
? uimage2DArray_type
: uimage2D_type
);
778 case GLSL_SAMPLER_DIM_3D
:
781 return uimage3D_type
;
782 case GLSL_SAMPLER_DIM_CUBE
:
783 return (array
? uimageCubeArray_type
: uimageCube_type
);
784 case GLSL_SAMPLER_DIM_RECT
:
787 return uimage2DRect_type
;
788 case GLSL_SAMPLER_DIM_BUF
:
791 return uimageBuffer_type
;
792 case GLSL_SAMPLER_DIM_MS
:
793 return (array
? uimage2DMSArray_type
: uimage2DMS_type
);
794 case GLSL_SAMPLER_DIM_EXTERNAL
:
801 unreachable("switch statement above should be complete");
805 glsl_type::get_array_instance(const glsl_type
*base
, unsigned array_size
)
807 /* Generate a name using the base type pointer in the key. This is
808 * done because the name of the base type may not be unique across
809 * shaders. For example, two shaders may have different record types
813 snprintf(key
, sizeof(key
), "%p[%u]", (void *) base
, array_size
);
815 mtx_lock(&glsl_type::mutex
);
817 if (array_types
== NULL
) {
818 array_types
= _mesa_hash_table_create(NULL
, _mesa_key_hash_string
,
819 _mesa_key_string_equal
);
822 const struct hash_entry
*entry
= _mesa_hash_table_search(array_types
, key
);
824 mtx_unlock(&glsl_type::mutex
);
825 const glsl_type
*t
= new glsl_type(base
, array_size
);
826 mtx_lock(&glsl_type::mutex
);
828 entry
= _mesa_hash_table_insert(array_types
,
829 ralloc_strdup(mem_ctx
, key
),
833 assert(((glsl_type
*) entry
->data
)->base_type
== GLSL_TYPE_ARRAY
);
834 assert(((glsl_type
*) entry
->data
)->length
== array_size
);
835 assert(((glsl_type
*) entry
->data
)->fields
.array
== base
);
837 mtx_unlock(&glsl_type::mutex
);
839 return (glsl_type
*) entry
->data
;
844 glsl_type::record_compare(const glsl_type
*b
) const
846 if (this->length
!= b
->length
)
849 if (this->interface_packing
!= b
->interface_packing
)
852 /* From the GLSL 4.20 specification (Sec 4.2):
854 * "Structures must have the same name, sequence of type names, and
855 * type definitions, and field names to be considered the same type."
857 * GLSL ES behaves the same (Ver 1.00 Sec 4.2.4, Ver 3.00 Sec 4.2.5).
859 * Note that we cannot force type name check when comparing unnamed
860 * structure types, these have a unique name assigned during parsing.
862 if (!this->is_anonymous() && !b
->is_anonymous())
863 if (strcmp(this->name
, b
->name
) != 0)
866 for (unsigned i
= 0; i
< this->length
; i
++) {
867 if (this->fields
.structure
[i
].type
!= b
->fields
.structure
[i
].type
)
869 if (strcmp(this->fields
.structure
[i
].name
,
870 b
->fields
.structure
[i
].name
) != 0)
872 if (this->fields
.structure
[i
].matrix_layout
873 != b
->fields
.structure
[i
].matrix_layout
)
875 if (this->fields
.structure
[i
].location
876 != b
->fields
.structure
[i
].location
)
878 if (this->fields
.structure
[i
].interpolation
879 != b
->fields
.structure
[i
].interpolation
)
881 if (this->fields
.structure
[i
].centroid
882 != b
->fields
.structure
[i
].centroid
)
884 if (this->fields
.structure
[i
].sample
885 != b
->fields
.structure
[i
].sample
)
887 if (this->fields
.structure
[i
].patch
888 != b
->fields
.structure
[i
].patch
)
890 if (this->fields
.structure
[i
].image_read_only
891 != b
->fields
.structure
[i
].image_read_only
)
893 if (this->fields
.structure
[i
].image_write_only
894 != b
->fields
.structure
[i
].image_write_only
)
896 if (this->fields
.structure
[i
].image_coherent
897 != b
->fields
.structure
[i
].image_coherent
)
899 if (this->fields
.structure
[i
].image_volatile
900 != b
->fields
.structure
[i
].image_volatile
)
902 if (this->fields
.structure
[i
].image_restrict
903 != b
->fields
.structure
[i
].image_restrict
)
905 if (this->fields
.structure
[i
].precision
906 != b
->fields
.structure
[i
].precision
)
915 glsl_type::record_key_compare(const void *a
, const void *b
)
917 const glsl_type
*const key1
= (glsl_type
*) a
;
918 const glsl_type
*const key2
= (glsl_type
*) b
;
920 return strcmp(key1
->name
, key2
->name
) == 0 && key1
->record_compare(key2
);
925 * Generate an integer hash value for a glsl_type structure type.
928 glsl_type::record_key_hash(const void *a
)
930 const glsl_type
*const key
= (glsl_type
*) a
;
931 uintptr_t hash
= key
->length
;
934 for (unsigned i
= 0; i
< key
->length
; i
++) {
935 /* casting pointer to uintptr_t */
936 hash
= (hash
* 13 ) + (uintptr_t) key
->fields
.structure
[i
].type
;
939 if (sizeof(hash
) == 8)
940 retval
= (hash
& 0xffffffff) ^ ((uint64_t) hash
>> 32);
949 glsl_type::get_record_instance(const glsl_struct_field
*fields
,
953 const glsl_type
key(fields
, num_fields
, name
);
955 mtx_lock(&glsl_type::mutex
);
957 if (record_types
== NULL
) {
958 record_types
= _mesa_hash_table_create(NULL
, record_key_hash
,
962 const struct hash_entry
*entry
= _mesa_hash_table_search(record_types
,
965 mtx_unlock(&glsl_type::mutex
);
966 const glsl_type
*t
= new glsl_type(fields
, num_fields
, name
);
967 mtx_lock(&glsl_type::mutex
);
969 entry
= _mesa_hash_table_insert(record_types
, t
, (void *) t
);
972 assert(((glsl_type
*) entry
->data
)->base_type
== GLSL_TYPE_STRUCT
);
973 assert(((glsl_type
*) entry
->data
)->length
== num_fields
);
974 assert(strcmp(((glsl_type
*) entry
->data
)->name
, name
) == 0);
976 mtx_unlock(&glsl_type::mutex
);
978 return (glsl_type
*) entry
->data
;
983 glsl_type::get_interface_instance(const glsl_struct_field
*fields
,
985 enum glsl_interface_packing packing
,
986 const char *block_name
)
988 const glsl_type
key(fields
, num_fields
, packing
, block_name
);
990 mtx_lock(&glsl_type::mutex
);
992 if (interface_types
== NULL
) {
993 interface_types
= _mesa_hash_table_create(NULL
, record_key_hash
,
997 const struct hash_entry
*entry
= _mesa_hash_table_search(interface_types
,
1000 mtx_unlock(&glsl_type::mutex
);
1001 const glsl_type
*t
= new glsl_type(fields
, num_fields
,
1002 packing
, block_name
);
1003 mtx_lock(&glsl_type::mutex
);
1005 entry
= _mesa_hash_table_insert(interface_types
, t
, (void *) t
);
1008 assert(((glsl_type
*) entry
->data
)->base_type
== GLSL_TYPE_INTERFACE
);
1009 assert(((glsl_type
*) entry
->data
)->length
== num_fields
);
1010 assert(strcmp(((glsl_type
*) entry
->data
)->name
, block_name
) == 0);
1012 mtx_unlock(&glsl_type::mutex
);
1014 return (glsl_type
*) entry
->data
;
1018 glsl_type::get_subroutine_instance(const char *subroutine_name
)
1020 const glsl_type
key(subroutine_name
);
1022 mtx_lock(&glsl_type::mutex
);
1024 if (subroutine_types
== NULL
) {
1025 subroutine_types
= _mesa_hash_table_create(NULL
, record_key_hash
,
1026 record_key_compare
);
1029 const struct hash_entry
*entry
= _mesa_hash_table_search(subroutine_types
,
1031 if (entry
== NULL
) {
1032 mtx_unlock(&glsl_type::mutex
);
1033 const glsl_type
*t
= new glsl_type(subroutine_name
);
1034 mtx_lock(&glsl_type::mutex
);
1036 entry
= _mesa_hash_table_insert(subroutine_types
, t
, (void *) t
);
1039 assert(((glsl_type
*) entry
->data
)->base_type
== GLSL_TYPE_SUBROUTINE
);
1040 assert(strcmp(((glsl_type
*) entry
->data
)->name
, subroutine_name
) == 0);
1042 mtx_unlock(&glsl_type::mutex
);
1044 return (glsl_type
*) entry
->data
;
1049 function_key_compare(const void *a
, const void *b
)
1051 const glsl_type
*const key1
= (glsl_type
*) a
;
1052 const glsl_type
*const key2
= (glsl_type
*) b
;
1054 if (key1
->length
!= key2
->length
)
1057 return memcmp(key1
->fields
.parameters
, key2
->fields
.parameters
,
1058 (key1
->length
+ 1) * sizeof(*key1
->fields
.parameters
));
1063 function_key_hash(const void *a
)
1065 const glsl_type
*const key
= (glsl_type
*) a
;
1069 size
= snprintf(hash_key
, sizeof(hash_key
), "%08x", key
->length
);
1071 for (unsigned i
= 0; i
< key
->length
; i
++) {
1072 if (size
>= sizeof(hash_key
))
1075 size
+= snprintf(& hash_key
[size
], sizeof(hash_key
) - size
,
1076 "%p", (void *) key
->fields
.structure
[i
].type
);
1079 return _mesa_hash_string(hash_key
);
1083 glsl_type::get_function_instance(const glsl_type
*return_type
,
1084 const glsl_function_param
*params
,
1085 unsigned num_params
)
1087 const glsl_type
key(return_type
, params
, num_params
);
1089 mtx_lock(&glsl_type::mutex
);
1091 if (function_types
== NULL
) {
1092 function_types
= _mesa_hash_table_create(NULL
, function_key_hash
,
1093 function_key_compare
);
1096 struct hash_entry
*entry
= _mesa_hash_table_search(function_types
, &key
);
1097 if (entry
== NULL
) {
1098 mtx_unlock(&glsl_type::mutex
);
1099 const glsl_type
*t
= new glsl_type(return_type
, params
, num_params
);
1100 mtx_lock(&glsl_type::mutex
);
1102 entry
= _mesa_hash_table_insert(function_types
, t
, (void *) t
);
1105 const glsl_type
*t
= (const glsl_type
*)entry
->data
;
1107 assert(t
->base_type
== GLSL_TYPE_FUNCTION
);
1108 assert(t
->length
== num_params
);
1110 mtx_unlock(&glsl_type::mutex
);
1117 glsl_type::get_mul_type(const glsl_type
*type_a
, const glsl_type
*type_b
)
1119 if (type_a
== type_b
) {
1121 } else if (type_a
->is_matrix() && type_b
->is_matrix()) {
1122 /* Matrix multiply. The columns of A must match the rows of B. Given
1123 * the other previously tested constraints, this means the vector type
1124 * of a row from A must be the same as the vector type of a column from
1127 if (type_a
->row_type() == type_b
->column_type()) {
1128 /* The resulting matrix has the number of columns of matrix B and
1129 * the number of rows of matrix A. We get the row count of A by
1130 * looking at the size of a vector that makes up a column. The
1131 * transpose (size of a row) is done for B.
1133 const glsl_type
*const type
=
1134 get_instance(type_a
->base_type
,
1135 type_a
->column_type()->vector_elements
,
1136 type_b
->row_type()->vector_elements
);
1137 assert(type
!= error_type
);
1141 } else if (type_a
->is_matrix()) {
1142 /* A is a matrix and B is a column vector. Columns of A must match
1143 * rows of B. Given the other previously tested constraints, this
1144 * means the vector type of a row from A must be the same as the
1145 * vector the type of B.
1147 if (type_a
->row_type() == type_b
) {
1148 /* The resulting vector has a number of elements equal to
1149 * the number of rows of matrix A. */
1150 const glsl_type
*const type
=
1151 get_instance(type_a
->base_type
,
1152 type_a
->column_type()->vector_elements
,
1154 assert(type
!= error_type
);
1159 assert(type_b
->is_matrix());
1161 /* A is a row vector and B is a matrix. Columns of A must match rows
1162 * of B. Given the other previously tested constraints, this means
1163 * the type of A must be the same as the vector type of a column from
1166 if (type_a
== type_b
->column_type()) {
1167 /* The resulting vector has a number of elements equal to
1168 * the number of columns of matrix B. */
1169 const glsl_type
*const type
=
1170 get_instance(type_a
->base_type
,
1171 type_b
->row_type()->vector_elements
,
1173 assert(type
!= error_type
);
1184 glsl_type::field_type(const char *name
) const
1186 if (this->base_type
!= GLSL_TYPE_STRUCT
1187 && this->base_type
!= GLSL_TYPE_INTERFACE
)
1190 for (unsigned i
= 0; i
< this->length
; i
++) {
1191 if (strcmp(name
, this->fields
.structure
[i
].name
) == 0)
1192 return this->fields
.structure
[i
].type
;
1200 glsl_type::field_index(const char *name
) const
1202 if (this->base_type
!= GLSL_TYPE_STRUCT
1203 && this->base_type
!= GLSL_TYPE_INTERFACE
)
1206 for (unsigned i
= 0; i
< this->length
; i
++) {
1207 if (strcmp(name
, this->fields
.structure
[i
].name
) == 0)
1216 glsl_type::component_slots() const
1218 switch (this->base_type
) {
1219 case GLSL_TYPE_UINT
:
1221 case GLSL_TYPE_FLOAT
:
1222 case GLSL_TYPE_BOOL
:
1223 return this->components();
1225 case GLSL_TYPE_DOUBLE
:
1226 return 2 * this->components();
1228 case GLSL_TYPE_STRUCT
:
1229 case GLSL_TYPE_INTERFACE
: {
1232 for (unsigned i
= 0; i
< this->length
; i
++)
1233 size
+= this->fields
.structure
[i
].type
->component_slots();
1238 case GLSL_TYPE_ARRAY
:
1239 return this->length
* this->fields
.array
->component_slots();
1241 case GLSL_TYPE_IMAGE
:
1243 case GLSL_TYPE_SUBROUTINE
:
1246 case GLSL_TYPE_FUNCTION
:
1247 case GLSL_TYPE_SAMPLER
:
1248 case GLSL_TYPE_ATOMIC_UINT
:
1249 case GLSL_TYPE_VOID
:
1250 case GLSL_TYPE_ERROR
:
1258 glsl_type::record_location_offset(unsigned length
) const
1260 unsigned offset
= 0;
1261 const glsl_type
*t
= this->without_array();
1262 if (t
->is_record()) {
1263 assert(length
<= t
->length
);
1265 for (unsigned i
= 0; i
< length
; i
++) {
1266 const glsl_type
*st
= t
->fields
.structure
[i
].type
;
1267 const glsl_type
*wa
= st
->without_array();
1268 if (wa
->is_record()) {
1269 unsigned r_offset
= wa
->record_location_offset(wa
->length
);
1270 offset
+= st
->is_array() ?
1271 st
->arrays_of_arrays_size() * r_offset
: r_offset
;
1272 } else if (st
->is_array() && st
->fields
.array
->is_array()) {
1273 unsigned outer_array_size
= st
->length
;
1274 const glsl_type
*base_type
= st
->fields
.array
;
1276 /* For arrays of arrays the outer arrays take up a uniform
1277 * slot for each element. The innermost array elements share a
1278 * single slot so we ignore the innermost array when calculating
1281 while (base_type
->fields
.array
->is_array()) {
1282 outer_array_size
= outer_array_size
* base_type
->length
;
1283 base_type
= base_type
->fields
.array
;
1285 offset
+= outer_array_size
;
1287 /* We dont worry about arrays here because unless the array
1288 * contains a structure or another array it only takes up a single
1299 glsl_type::uniform_locations() const
1303 switch (this->base_type
) {
1304 case GLSL_TYPE_UINT
:
1306 case GLSL_TYPE_FLOAT
:
1307 case GLSL_TYPE_DOUBLE
:
1308 case GLSL_TYPE_BOOL
:
1309 case GLSL_TYPE_SAMPLER
:
1310 case GLSL_TYPE_IMAGE
:
1311 case GLSL_TYPE_SUBROUTINE
:
1314 case GLSL_TYPE_STRUCT
:
1315 case GLSL_TYPE_INTERFACE
:
1316 for (unsigned i
= 0; i
< this->length
; i
++)
1317 size
+= this->fields
.structure
[i
].type
->uniform_locations();
1319 case GLSL_TYPE_ARRAY
:
1320 return this->length
* this->fields
.array
->uniform_locations();
1327 glsl_type::can_implicitly_convert_to(const glsl_type
*desired
,
1328 _mesa_glsl_parse_state
*state
) const
1330 if (this == desired
)
1333 /* There is no conversion among matrix types. */
1334 if (this->matrix_columns
> 1 || desired
->matrix_columns
> 1)
1337 /* Vector size must match. */
1338 if (this->vector_elements
!= desired
->vector_elements
)
1341 /* int and uint can be converted to float. */
1342 if (desired
->is_float() && this->is_integer())
1345 /* With GLSL 4.0 / ARB_gpu_shader5, int can be converted to uint.
1346 * Note that state may be NULL here, when resolving function calls in the
1347 * linker. By this time, all the state-dependent checks have already
1348 * happened though, so allow anything that's allowed in any shader version. */
1349 if ((!state
|| state
->is_version(400, 0) || state
->ARB_gpu_shader5_enable
) &&
1350 desired
->base_type
== GLSL_TYPE_UINT
&& this->base_type
== GLSL_TYPE_INT
)
1353 /* No implicit conversions from double. */
1354 if ((!state
|| state
->has_double()) && this->is_double())
1357 /* Conversions from different types to double. */
1358 if ((!state
|| state
->has_double()) && desired
->is_double()) {
1359 if (this->is_float())
1361 if (this->is_integer())
1369 glsl_type::std140_base_alignment(bool row_major
) const
1371 unsigned N
= is_double() ? 8 : 4;
1373 /* (1) If the member is a scalar consuming <N> basic machine units, the
1374 * base alignment is <N>.
1376 * (2) If the member is a two- or four-component vector with components
1377 * consuming <N> basic machine units, the base alignment is 2<N> or
1378 * 4<N>, respectively.
1380 * (3) If the member is a three-component vector with components consuming
1381 * <N> basic machine units, the base alignment is 4<N>.
1383 if (this->is_scalar() || this->is_vector()) {
1384 switch (this->vector_elements
) {
1395 /* (4) If the member is an array of scalars or vectors, the base alignment
1396 * and array stride are set to match the base alignment of a single
1397 * array element, according to rules (1), (2), and (3), and rounded up
1398 * to the base alignment of a vec4. The array may have padding at the
1399 * end; the base offset of the member following the array is rounded up
1400 * to the next multiple of the base alignment.
1402 * (6) If the member is an array of <S> column-major matrices with <C>
1403 * columns and <R> rows, the matrix is stored identically to a row of
1404 * <S>*<C> column vectors with <R> components each, according to rule
1407 * (8) If the member is an array of <S> row-major matrices with <C> columns
1408 * and <R> rows, the matrix is stored identically to a row of <S>*<R>
1409 * row vectors with <C> components each, according to rule (4).
1411 * (10) If the member is an array of <S> structures, the <S> elements of
1412 * the array are laid out in order, according to rule (9).
1414 if (this->is_array()) {
1415 if (this->fields
.array
->is_scalar() ||
1416 this->fields
.array
->is_vector() ||
1417 this->fields
.array
->is_matrix()) {
1418 return MAX2(this->fields
.array
->std140_base_alignment(row_major
), 16);
1420 assert(this->fields
.array
->is_record() ||
1421 this->fields
.array
->is_array());
1422 return this->fields
.array
->std140_base_alignment(row_major
);
1426 /* (5) If the member is a column-major matrix with <C> columns and
1427 * <R> rows, the matrix is stored identically to an array of
1428 * <C> column vectors with <R> components each, according to
1431 * (7) If the member is a row-major matrix with <C> columns and <R>
1432 * rows, the matrix is stored identically to an array of <R>
1433 * row vectors with <C> components each, according to rule (4).
1435 if (this->is_matrix()) {
1436 const struct glsl_type
*vec_type
, *array_type
;
1437 int c
= this->matrix_columns
;
1438 int r
= this->vector_elements
;
1441 vec_type
= get_instance(base_type
, c
, 1);
1442 array_type
= glsl_type::get_array_instance(vec_type
, r
);
1444 vec_type
= get_instance(base_type
, r
, 1);
1445 array_type
= glsl_type::get_array_instance(vec_type
, c
);
1448 return array_type
->std140_base_alignment(false);
1451 /* (9) If the member is a structure, the base alignment of the
1452 * structure is <N>, where <N> is the largest base alignment
1453 * value of any of its members, and rounded up to the base
1454 * alignment of a vec4. The individual members of this
1455 * sub-structure are then assigned offsets by applying this set
1456 * of rules recursively, where the base offset of the first
1457 * member of the sub-structure is equal to the aligned offset
1458 * of the structure. The structure may have padding at the end;
1459 * the base offset of the member following the sub-structure is
1460 * rounded up to the next multiple of the base alignment of the
1463 if (this->is_record()) {
1464 unsigned base_alignment
= 16;
1465 for (unsigned i
= 0; i
< this->length
; i
++) {
1466 bool field_row_major
= row_major
;
1467 const enum glsl_matrix_layout matrix_layout
=
1468 glsl_matrix_layout(this->fields
.structure
[i
].matrix_layout
);
1469 if (matrix_layout
== GLSL_MATRIX_LAYOUT_ROW_MAJOR
) {
1470 field_row_major
= true;
1471 } else if (matrix_layout
== GLSL_MATRIX_LAYOUT_COLUMN_MAJOR
) {
1472 field_row_major
= false;
1475 const struct glsl_type
*field_type
= this->fields
.structure
[i
].type
;
1476 base_alignment
= MAX2(base_alignment
,
1477 field_type
->std140_base_alignment(field_row_major
));
1479 return base_alignment
;
1482 assert(!"not reached");
1487 glsl_type::std140_size(bool row_major
) const
1489 unsigned N
= is_double() ? 8 : 4;
1491 /* (1) If the member is a scalar consuming <N> basic machine units, the
1492 * base alignment is <N>.
1494 * (2) If the member is a two- or four-component vector with components
1495 * consuming <N> basic machine units, the base alignment is 2<N> or
1496 * 4<N>, respectively.
1498 * (3) If the member is a three-component vector with components consuming
1499 * <N> basic machine units, the base alignment is 4<N>.
1501 if (this->is_scalar() || this->is_vector()) {
1502 return this->vector_elements
* N
;
1505 /* (5) If the member is a column-major matrix with <C> columns and
1506 * <R> rows, the matrix is stored identically to an array of
1507 * <C> column vectors with <R> components each, according to
1510 * (6) If the member is an array of <S> column-major matrices with <C>
1511 * columns and <R> rows, the matrix is stored identically to a row of
1512 * <S>*<C> column vectors with <R> components each, according to rule
1515 * (7) If the member is a row-major matrix with <C> columns and <R>
1516 * rows, the matrix is stored identically to an array of <R>
1517 * row vectors with <C> components each, according to rule (4).
1519 * (8) If the member is an array of <S> row-major matrices with <C> columns
1520 * and <R> rows, the matrix is stored identically to a row of <S>*<R>
1521 * row vectors with <C> components each, according to rule (4).
1523 if (this->without_array()->is_matrix()) {
1524 const struct glsl_type
*element_type
;
1525 const struct glsl_type
*vec_type
;
1526 unsigned int array_len
;
1528 if (this->is_array()) {
1529 element_type
= this->without_array();
1530 array_len
= this->arrays_of_arrays_size();
1532 element_type
= this;
1537 vec_type
= get_instance(element_type
->base_type
,
1538 element_type
->matrix_columns
, 1);
1540 array_len
*= element_type
->vector_elements
;
1542 vec_type
= get_instance(element_type
->base_type
,
1543 element_type
->vector_elements
, 1);
1544 array_len
*= element_type
->matrix_columns
;
1546 const glsl_type
*array_type
= glsl_type::get_array_instance(vec_type
,
1549 return array_type
->std140_size(false);
1552 /* (4) If the member is an array of scalars or vectors, the base alignment
1553 * and array stride are set to match the base alignment of a single
1554 * array element, according to rules (1), (2), and (3), and rounded up
1555 * to the base alignment of a vec4. The array may have padding at the
1556 * end; the base offset of the member following the array is rounded up
1557 * to the next multiple of the base alignment.
1559 * (10) If the member is an array of <S> structures, the <S> elements of
1560 * the array are laid out in order, according to rule (9).
1562 if (this->is_array()) {
1563 if (this->without_array()->is_record()) {
1564 return this->arrays_of_arrays_size() *
1565 this->without_array()->std140_size(row_major
);
1567 unsigned element_base_align
=
1568 this->without_array()->std140_base_alignment(row_major
);
1569 return this->arrays_of_arrays_size() * MAX2(element_base_align
, 16);
1573 /* (9) If the member is a structure, the base alignment of the
1574 * structure is <N>, where <N> is the largest base alignment
1575 * value of any of its members, and rounded up to the base
1576 * alignment of a vec4. The individual members of this
1577 * sub-structure are then assigned offsets by applying this set
1578 * of rules recursively, where the base offset of the first
1579 * member of the sub-structure is equal to the aligned offset
1580 * of the structure. The structure may have padding at the end;
1581 * the base offset of the member following the sub-structure is
1582 * rounded up to the next multiple of the base alignment of the
1585 if (this->is_record() || this->is_interface()) {
1587 unsigned max_align
= 0;
1589 for (unsigned i
= 0; i
< this->length
; i
++) {
1590 bool field_row_major
= row_major
;
1591 const enum glsl_matrix_layout matrix_layout
=
1592 glsl_matrix_layout(this->fields
.structure
[i
].matrix_layout
);
1593 if (matrix_layout
== GLSL_MATRIX_LAYOUT_ROW_MAJOR
) {
1594 field_row_major
= true;
1595 } else if (matrix_layout
== GLSL_MATRIX_LAYOUT_COLUMN_MAJOR
) {
1596 field_row_major
= false;
1599 const struct glsl_type
*field_type
= this->fields
.structure
[i
].type
;
1600 unsigned align
= field_type
->std140_base_alignment(field_row_major
);
1602 /* Ignore unsized arrays when calculating size */
1603 if (field_type
->is_unsized_array())
1606 size
= glsl_align(size
, align
);
1607 size
+= field_type
->std140_size(field_row_major
);
1609 max_align
= MAX2(align
, max_align
);
1611 if (field_type
->is_record() && (i
+ 1 < this->length
))
1612 size
= glsl_align(size
, 16);
1614 size
= glsl_align(size
, MAX2(max_align
, 16));
1618 assert(!"not reached");
1623 glsl_type::std430_base_alignment(bool row_major
) const
1626 unsigned N
= is_double() ? 8 : 4;
1628 /* (1) If the member is a scalar consuming <N> basic machine units, the
1629 * base alignment is <N>.
1631 * (2) If the member is a two- or four-component vector with components
1632 * consuming <N> basic machine units, the base alignment is 2<N> or
1633 * 4<N>, respectively.
1635 * (3) If the member is a three-component vector with components consuming
1636 * <N> basic machine units, the base alignment is 4<N>.
1638 if (this->is_scalar() || this->is_vector()) {
1639 switch (this->vector_elements
) {
1650 /* OpenGL 4.30 spec, section 7.6.2.2 "Standard Uniform Block Layout":
1652 * "When using the std430 storage layout, shader storage blocks will be
1653 * laid out in buffer storage identically to uniform and shader storage
1654 * blocks using the std140 layout, except that the base alignment and
1655 * stride of arrays of scalars and vectors in rule 4 and of structures
1656 * in rule 9 are not rounded up a multiple of the base alignment of a vec4.
1659 /* (1) If the member is a scalar consuming <N> basic machine units, the
1660 * base alignment is <N>.
1662 * (2) If the member is a two- or four-component vector with components
1663 * consuming <N> basic machine units, the base alignment is 2<N> or
1664 * 4<N>, respectively.
1666 * (3) If the member is a three-component vector with components consuming
1667 * <N> basic machine units, the base alignment is 4<N>.
1669 if (this->is_array())
1670 return this->fields
.array
->std430_base_alignment(row_major
);
1672 /* (5) If the member is a column-major matrix with <C> columns and
1673 * <R> rows, the matrix is stored identically to an array of
1674 * <C> column vectors with <R> components each, according to
1677 * (7) If the member is a row-major matrix with <C> columns and <R>
1678 * rows, the matrix is stored identically to an array of <R>
1679 * row vectors with <C> components each, according to rule (4).
1681 if (this->is_matrix()) {
1682 const struct glsl_type
*vec_type
, *array_type
;
1683 int c
= this->matrix_columns
;
1684 int r
= this->vector_elements
;
1687 vec_type
= get_instance(base_type
, c
, 1);
1688 array_type
= glsl_type::get_array_instance(vec_type
, r
);
1690 vec_type
= get_instance(base_type
, r
, 1);
1691 array_type
= glsl_type::get_array_instance(vec_type
, c
);
1694 return array_type
->std430_base_alignment(false);
1697 /* (9) If the member is a structure, the base alignment of the
1698 * structure is <N>, where <N> is the largest base alignment
1699 * value of any of its members, and rounded up to the base
1700 * alignment of a vec4. The individual members of this
1701 * sub-structure are then assigned offsets by applying this set
1702 * of rules recursively, where the base offset of the first
1703 * member of the sub-structure is equal to the aligned offset
1704 * of the structure. The structure may have padding at the end;
1705 * the base offset of the member following the sub-structure is
1706 * rounded up to the next multiple of the base alignment of the
1709 if (this->is_record()) {
1710 unsigned base_alignment
= 0;
1711 for (unsigned i
= 0; i
< this->length
; i
++) {
1712 bool field_row_major
= row_major
;
1713 const enum glsl_matrix_layout matrix_layout
=
1714 glsl_matrix_layout(this->fields
.structure
[i
].matrix_layout
);
1715 if (matrix_layout
== GLSL_MATRIX_LAYOUT_ROW_MAJOR
) {
1716 field_row_major
= true;
1717 } else if (matrix_layout
== GLSL_MATRIX_LAYOUT_COLUMN_MAJOR
) {
1718 field_row_major
= false;
1721 const struct glsl_type
*field_type
= this->fields
.structure
[i
].type
;
1722 base_alignment
= MAX2(base_alignment
,
1723 field_type
->std430_base_alignment(field_row_major
));
1725 assert(base_alignment
> 0);
1726 return base_alignment
;
1728 assert(!"not reached");
1733 glsl_type::std430_array_stride(bool row_major
) const
1735 unsigned N
= is_double() ? 8 : 4;
1737 /* Notice that the array stride of a vec3 is not 3 * N but 4 * N.
1738 * See OpenGL 4.30 spec, section 7.6.2.2 "Standard Uniform Block Layout"
1740 * (3) If the member is a three-component vector with components consuming
1741 * <N> basic machine units, the base alignment is 4<N>.
1743 if (this->is_vector() && this->vector_elements
== 3)
1746 /* By default use std430_size(row_major) */
1747 return this->std430_size(row_major
);
1751 glsl_type::std430_size(bool row_major
) const
1753 unsigned N
= is_double() ? 8 : 4;
1755 /* OpenGL 4.30 spec, section 7.6.2.2 "Standard Uniform Block Layout":
1757 * "When using the std430 storage layout, shader storage blocks will be
1758 * laid out in buffer storage identically to uniform and shader storage
1759 * blocks using the std140 layout, except that the base alignment and
1760 * stride of arrays of scalars and vectors in rule 4 and of structures
1761 * in rule 9 are not rounded up a multiple of the base alignment of a vec4.
1763 if (this->is_scalar() || this->is_vector())
1764 return this->vector_elements
* N
;
1766 if (this->without_array()->is_matrix()) {
1767 const struct glsl_type
*element_type
;
1768 const struct glsl_type
*vec_type
;
1769 unsigned int array_len
;
1771 if (this->is_array()) {
1772 element_type
= this->without_array();
1773 array_len
= this->arrays_of_arrays_size();
1775 element_type
= this;
1780 vec_type
= get_instance(element_type
->base_type
,
1781 element_type
->matrix_columns
, 1);
1783 array_len
*= element_type
->vector_elements
;
1785 vec_type
= get_instance(element_type
->base_type
,
1786 element_type
->vector_elements
, 1);
1787 array_len
*= element_type
->matrix_columns
;
1789 const glsl_type
*array_type
= glsl_type::get_array_instance(vec_type
,
1792 return array_type
->std430_size(false);
1795 if (this->is_array()) {
1796 if (this->without_array()->is_record())
1797 return this->arrays_of_arrays_size() *
1798 this->without_array()->std430_size(row_major
);
1800 return this->arrays_of_arrays_size() *
1801 this->without_array()->std430_base_alignment(row_major
);
1804 if (this->is_record() || this->is_interface()) {
1806 unsigned max_align
= 0;
1808 for (unsigned i
= 0; i
< this->length
; i
++) {
1809 bool field_row_major
= row_major
;
1810 const enum glsl_matrix_layout matrix_layout
=
1811 glsl_matrix_layout(this->fields
.structure
[i
].matrix_layout
);
1812 if (matrix_layout
== GLSL_MATRIX_LAYOUT_ROW_MAJOR
) {
1813 field_row_major
= true;
1814 } else if (matrix_layout
== GLSL_MATRIX_LAYOUT_COLUMN_MAJOR
) {
1815 field_row_major
= false;
1818 const struct glsl_type
*field_type
= this->fields
.structure
[i
].type
;
1819 unsigned align
= field_type
->std430_base_alignment(field_row_major
);
1820 size
= glsl_align(size
, align
);
1821 size
+= field_type
->std430_size(field_row_major
);
1823 max_align
= MAX2(align
, max_align
);
1825 size
= glsl_align(size
, max_align
);
1829 assert(!"not reached");
1834 glsl_type::count_attribute_slots() const
1836 /* From page 31 (page 37 of the PDF) of the GLSL 1.50 spec:
1838 * "A scalar input counts the same amount against this limit as a vec4,
1839 * so applications may want to consider packing groups of four
1840 * unrelated float inputs together into a vector to better utilize the
1841 * capabilities of the underlying hardware. A matrix input will use up
1842 * multiple locations. The number of locations used will equal the
1843 * number of columns in the matrix."
1845 * The spec does not explicitly say how arrays are counted. However, it
1846 * should be safe to assume the total number of slots consumed by an array
1847 * is the number of entries in the array multiplied by the number of slots
1848 * consumed by a single element of the array.
1850 * The spec says nothing about how structs are counted, because vertex
1851 * attributes are not allowed to be (or contain) structs. However, Mesa
1852 * allows varying structs, the number of varying slots taken up by a
1853 * varying struct is simply equal to the sum of the number of slots taken
1854 * up by each element.
1856 switch (this->base_type
) {
1857 case GLSL_TYPE_UINT
:
1859 case GLSL_TYPE_FLOAT
:
1860 case GLSL_TYPE_BOOL
:
1861 case GLSL_TYPE_DOUBLE
:
1862 return this->matrix_columns
;
1864 case GLSL_TYPE_STRUCT
:
1865 case GLSL_TYPE_INTERFACE
: {
1868 for (unsigned i
= 0; i
< this->length
; i
++)
1869 size
+= this->fields
.structure
[i
].type
->count_attribute_slots();
1874 case GLSL_TYPE_ARRAY
:
1875 return this->length
* this->fields
.array
->count_attribute_slots();
1877 case GLSL_TYPE_FUNCTION
:
1878 case GLSL_TYPE_SAMPLER
:
1879 case GLSL_TYPE_IMAGE
:
1880 case GLSL_TYPE_ATOMIC_UINT
:
1881 case GLSL_TYPE_VOID
:
1882 case GLSL_TYPE_SUBROUTINE
:
1883 case GLSL_TYPE_ERROR
:
1887 assert(!"Unexpected type in count_attribute_slots()");
1893 glsl_type::coordinate_components() const
1897 switch (sampler_dimensionality
) {
1898 case GLSL_SAMPLER_DIM_1D
:
1899 case GLSL_SAMPLER_DIM_BUF
:
1902 case GLSL_SAMPLER_DIM_2D
:
1903 case GLSL_SAMPLER_DIM_RECT
:
1904 case GLSL_SAMPLER_DIM_MS
:
1905 case GLSL_SAMPLER_DIM_EXTERNAL
:
1908 case GLSL_SAMPLER_DIM_3D
:
1909 case GLSL_SAMPLER_DIM_CUBE
:
1913 assert(!"Should not get here.");
1918 /* Array textures need an additional component for the array index, except
1919 * for cubemap array images that behave like a 2D array of interleaved
1922 if (sampler_array
&&
1923 !(base_type
== GLSL_TYPE_IMAGE
&&
1924 sampler_dimensionality
== GLSL_SAMPLER_DIM_CUBE
))
1931 * Declarations of type flyweights (glsl_type::_foo_type) and
1932 * convenience pointers (glsl_type::foo_type).
1935 #define DECL_TYPE(NAME, ...) \
1936 const glsl_type glsl_type::_##NAME##_type = glsl_type(__VA_ARGS__, #NAME); \
1937 const glsl_type *const glsl_type::NAME##_type = &glsl_type::_##NAME##_type;
1939 #define STRUCT_TYPE(NAME)
1941 #include "builtin_type_macros.h"