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/core.h" /* for Elements, MAX2 */
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
;
135 mtx_unlock(&glsl_type::mutex
);
138 glsl_type::glsl_type(const glsl_struct_field
*fields
, unsigned num_fields
,
139 enum glsl_interface_packing packing
, const char *name
) :
141 base_type(GLSL_TYPE_INTERFACE
),
142 sampler_dimensionality(0), sampler_shadow(0), sampler_array(0),
143 sampler_type(0), interface_packing((unsigned) packing
),
144 vector_elements(0), matrix_columns(0),
149 mtx_lock(&glsl_type::mutex
);
151 init_ralloc_type_ctx();
152 assert(name
!= NULL
);
153 this->name
= ralloc_strdup(this->mem_ctx
, name
);
154 this->fields
.structure
= ralloc_array(this->mem_ctx
,
155 glsl_struct_field
, length
);
156 for (i
= 0; i
< length
; i
++) {
157 this->fields
.structure
[i
].type
= fields
[i
].type
;
158 this->fields
.structure
[i
].name
= ralloc_strdup(this->fields
.structure
,
160 this->fields
.structure
[i
].location
= fields
[i
].location
;
161 this->fields
.structure
[i
].interpolation
= fields
[i
].interpolation
;
162 this->fields
.structure
[i
].centroid
= fields
[i
].centroid
;
163 this->fields
.structure
[i
].sample
= fields
[i
].sample
;
164 this->fields
.structure
[i
].matrix_layout
= fields
[i
].matrix_layout
;
165 this->fields
.structure
[i
].patch
= fields
[i
].patch
;
166 this->fields
.structure
[i
].precision
= fields
[i
].precision
;
169 mtx_unlock(&glsl_type::mutex
);
172 glsl_type::glsl_type(const glsl_type
*return_type
,
173 const glsl_function_param
*params
, unsigned num_params
) :
175 base_type(GLSL_TYPE_FUNCTION
),
176 sampler_dimensionality(0), sampler_shadow(0), sampler_array(0),
177 sampler_type(0), interface_packing(0),
178 vector_elements(0), matrix_columns(0),
183 mtx_lock(&glsl_type::mutex
);
185 init_ralloc_type_ctx();
187 this->fields
.parameters
= rzalloc_array(this->mem_ctx
,
188 glsl_function_param
, num_params
+ 1);
190 /* We store the return type as the first parameter */
191 this->fields
.parameters
[0].type
= return_type
;
192 this->fields
.parameters
[0].in
= false;
193 this->fields
.parameters
[0].out
= true;
195 /* We store the i'th parameter in slot i+1 */
196 for (i
= 0; i
< length
; i
++) {
197 this->fields
.parameters
[i
+ 1].type
= params
[i
].type
;
198 this->fields
.parameters
[i
+ 1].in
= params
[i
].in
;
199 this->fields
.parameters
[i
+ 1].out
= params
[i
].out
;
202 mtx_unlock(&glsl_type::mutex
);
205 glsl_type::glsl_type(const char *subroutine_name
) :
207 base_type(GLSL_TYPE_SUBROUTINE
),
208 sampler_dimensionality(0), sampler_shadow(0), sampler_array(0),
209 sampler_type(0), interface_packing(0),
210 vector_elements(1), matrix_columns(1),
213 mtx_lock(&glsl_type::mutex
);
215 init_ralloc_type_ctx();
216 assert(subroutine_name
!= NULL
);
217 this->name
= ralloc_strdup(this->mem_ctx
, subroutine_name
);
218 mtx_unlock(&glsl_type::mutex
);
222 glsl_type::contains_sampler() const
224 if (this->is_array()) {
225 return this->fields
.array
->contains_sampler();
226 } else if (this->is_record()) {
227 for (unsigned int i
= 0; i
< this->length
; i
++) {
228 if (this->fields
.structure
[i
].type
->contains_sampler())
233 return this->is_sampler();
239 glsl_type::contains_integer() const
241 if (this->is_array()) {
242 return this->fields
.array
->contains_integer();
243 } else if (this->is_record()) {
244 for (unsigned int i
= 0; i
< this->length
; i
++) {
245 if (this->fields
.structure
[i
].type
->contains_integer())
250 return this->is_integer();
255 glsl_type::contains_double() const
257 if (this->is_array()) {
258 return this->fields
.array
->contains_double();
259 } else if (this->is_record()) {
260 for (unsigned int i
= 0; i
< this->length
; i
++) {
261 if (this->fields
.structure
[i
].type
->contains_double())
266 return this->is_double();
271 glsl_type::contains_opaque() const {
273 case GLSL_TYPE_SAMPLER
:
274 case GLSL_TYPE_IMAGE
:
275 case GLSL_TYPE_ATOMIC_UINT
:
277 case GLSL_TYPE_ARRAY
:
278 return fields
.array
->contains_opaque();
279 case GLSL_TYPE_STRUCT
:
280 for (unsigned int i
= 0; i
< length
; i
++) {
281 if (fields
.structure
[i
].type
->contains_opaque())
291 glsl_type::contains_subroutine() const
293 if (this->is_array()) {
294 return this->fields
.array
->contains_subroutine();
295 } else if (this->is_record()) {
296 for (unsigned int i
= 0; i
< this->length
; i
++) {
297 if (this->fields
.structure
[i
].type
->contains_subroutine())
302 return this->is_subroutine();
307 glsl_type::sampler_index() const
309 const glsl_type
*const t
= (this->is_array()) ? this->fields
.array
: this;
311 assert(t
->is_sampler());
313 switch (t
->sampler_dimensionality
) {
314 case GLSL_SAMPLER_DIM_1D
:
315 return (t
->sampler_array
) ? TEXTURE_1D_ARRAY_INDEX
: TEXTURE_1D_INDEX
;
316 case GLSL_SAMPLER_DIM_2D
:
317 return (t
->sampler_array
) ? TEXTURE_2D_ARRAY_INDEX
: TEXTURE_2D_INDEX
;
318 case GLSL_SAMPLER_DIM_3D
:
319 return TEXTURE_3D_INDEX
;
320 case GLSL_SAMPLER_DIM_CUBE
:
321 return (t
->sampler_array
) ? TEXTURE_CUBE_ARRAY_INDEX
: TEXTURE_CUBE_INDEX
;
322 case GLSL_SAMPLER_DIM_RECT
:
323 return TEXTURE_RECT_INDEX
;
324 case GLSL_SAMPLER_DIM_BUF
:
325 return TEXTURE_BUFFER_INDEX
;
326 case GLSL_SAMPLER_DIM_EXTERNAL
:
327 return TEXTURE_EXTERNAL_INDEX
;
328 case GLSL_SAMPLER_DIM_MS
:
329 return (t
->sampler_array
) ? TEXTURE_2D_MULTISAMPLE_ARRAY_INDEX
: TEXTURE_2D_MULTISAMPLE_INDEX
;
331 assert(!"Should not get here.");
332 return TEXTURE_BUFFER_INDEX
;
337 glsl_type::contains_image() const
339 if (this->is_array()) {
340 return this->fields
.array
->contains_image();
341 } else if (this->is_record()) {
342 for (unsigned int i
= 0; i
< this->length
; i
++) {
343 if (this->fields
.structure
[i
].type
->contains_image())
348 return this->is_image();
352 const glsl_type
*glsl_type::get_base_type() const
359 case GLSL_TYPE_FLOAT
:
361 case GLSL_TYPE_DOUBLE
:
371 const glsl_type
*glsl_type::get_scalar_type() const
373 const glsl_type
*type
= this;
376 while (type
->base_type
== GLSL_TYPE_ARRAY
)
377 type
= type
->fields
.array
;
379 /* Handle vectors and matrices */
380 switch (type
->base_type
) {
385 case GLSL_TYPE_FLOAT
:
387 case GLSL_TYPE_DOUBLE
:
392 /* Handle everything else */
399 _mesa_glsl_release_types(void)
401 /* Should only be called during atexit (either when unloading shared
402 * object, or if process terminates), so no mutex-locking should be
405 if (glsl_type::array_types
!= NULL
) {
406 _mesa_hash_table_destroy(glsl_type::array_types
, NULL
);
407 glsl_type::array_types
= NULL
;
410 if (glsl_type::record_types
!= NULL
) {
411 _mesa_hash_table_destroy(glsl_type::record_types
, NULL
);
412 glsl_type::record_types
= NULL
;
415 if (glsl_type::interface_types
!= NULL
) {
416 _mesa_hash_table_destroy(glsl_type::interface_types
, NULL
);
417 glsl_type::interface_types
= NULL
;
422 glsl_type::glsl_type(const glsl_type
*array
, unsigned length
) :
423 base_type(GLSL_TYPE_ARRAY
),
424 sampler_dimensionality(0), sampler_shadow(0), sampler_array(0),
425 sampler_type(0), interface_packing(0),
426 vector_elements(0), matrix_columns(0),
427 length(length
), name(NULL
)
429 this->fields
.array
= array
;
430 /* Inherit the gl type of the base. The GL type is used for
431 * uniform/statevar handling in Mesa and the arrayness of the type
432 * is represented by the size rather than the type.
434 this->gl_type
= array
->gl_type
;
436 /* Allow a maximum of 10 characters for the array size. This is enough
437 * for 32-bits of ~0. The extra 3 are for the '[', ']', and terminating
440 const unsigned name_length
= strlen(array
->name
) + 10 + 3;
442 mtx_lock(&glsl_type::mutex
);
443 char *const n
= (char *) ralloc_size(this->mem_ctx
, name_length
);
444 mtx_unlock(&glsl_type::mutex
);
447 snprintf(n
, name_length
, "%s[]", array
->name
);
449 /* insert outermost dimensions in the correct spot
450 * otherwise the dimension order will be backwards
452 const char *pos
= strchr(array
->name
, '[');
454 int idx
= pos
- array
->name
;
455 snprintf(n
, idx
+1, "%s", array
->name
);
456 snprintf(n
+ idx
, name_length
- idx
, "[%u]%s",
457 length
, array
->name
+ idx
);
459 snprintf(n
, name_length
, "%s[%u]", array
->name
, length
);
468 glsl_type::vec(unsigned components
)
470 if (components
== 0 || components
> 4)
473 static const glsl_type
*const ts
[] = {
474 float_type
, vec2_type
, vec3_type
, vec4_type
476 return ts
[components
- 1];
480 glsl_type::dvec(unsigned components
)
482 if (components
== 0 || components
> 4)
485 static const glsl_type
*const ts
[] = {
486 double_type
, dvec2_type
, dvec3_type
, dvec4_type
488 return ts
[components
- 1];
492 glsl_type::ivec(unsigned components
)
494 if (components
== 0 || components
> 4)
497 static const glsl_type
*const ts
[] = {
498 int_type
, ivec2_type
, ivec3_type
, ivec4_type
500 return ts
[components
- 1];
505 glsl_type::uvec(unsigned components
)
507 if (components
== 0 || components
> 4)
510 static const glsl_type
*const ts
[] = {
511 uint_type
, uvec2_type
, uvec3_type
, uvec4_type
513 return ts
[components
- 1];
518 glsl_type::bvec(unsigned components
)
520 if (components
== 0 || components
> 4)
523 static const glsl_type
*const ts
[] = {
524 bool_type
, bvec2_type
, bvec3_type
, bvec4_type
526 return ts
[components
- 1];
531 glsl_type::get_instance(unsigned base_type
, unsigned rows
, unsigned columns
)
533 if (base_type
== GLSL_TYPE_VOID
)
536 if ((rows
< 1) || (rows
> 4) || (columns
< 1) || (columns
> 4))
539 /* Treat GLSL vectors as Nx1 matrices.
547 case GLSL_TYPE_FLOAT
:
549 case GLSL_TYPE_DOUBLE
:
557 if ((base_type
!= GLSL_TYPE_FLOAT
&& base_type
!= GLSL_TYPE_DOUBLE
) || (rows
== 1))
560 /* GLSL matrix types are named mat{COLUMNS}x{ROWS}. Only the following
561 * combinations are valid:
569 #define IDX(c,r) (((c-1)*3) + (r-1))
571 if (base_type
== GLSL_TYPE_DOUBLE
) {
572 switch (IDX(columns
, rows
)) {
573 case IDX(2,2): return dmat2_type
;
574 case IDX(2,3): return dmat2x3_type
;
575 case IDX(2,4): return dmat2x4_type
;
576 case IDX(3,2): return dmat3x2_type
;
577 case IDX(3,3): return dmat3_type
;
578 case IDX(3,4): return dmat3x4_type
;
579 case IDX(4,2): return dmat4x2_type
;
580 case IDX(4,3): return dmat4x3_type
;
581 case IDX(4,4): return dmat4_type
;
582 default: return error_type
;
585 switch (IDX(columns
, rows
)) {
586 case IDX(2,2): return mat2_type
;
587 case IDX(2,3): return mat2x3_type
;
588 case IDX(2,4): return mat2x4_type
;
589 case IDX(3,2): return mat3x2_type
;
590 case IDX(3,3): return mat3_type
;
591 case IDX(3,4): return mat3x4_type
;
592 case IDX(4,2): return mat4x2_type
;
593 case IDX(4,3): return mat4x3_type
;
594 case IDX(4,4): return mat4_type
;
595 default: return error_type
;
600 assert(!"Should not get here.");
605 glsl_type::get_sampler_instance(enum glsl_sampler_dim dim
,
611 case GLSL_TYPE_FLOAT
:
613 case GLSL_SAMPLER_DIM_1D
:
615 return (array
? sampler1DArrayShadow_type
: sampler1DShadow_type
);
617 return (array
? sampler1DArray_type
: sampler1D_type
);
618 case GLSL_SAMPLER_DIM_2D
:
620 return (array
? sampler2DArrayShadow_type
: sampler2DShadow_type
);
622 return (array
? sampler2DArray_type
: sampler2D_type
);
623 case GLSL_SAMPLER_DIM_3D
:
627 return sampler3D_type
;
628 case GLSL_SAMPLER_DIM_CUBE
:
630 return (array
? samplerCubeArrayShadow_type
: samplerCubeShadow_type
);
632 return (array
? samplerCubeArray_type
: samplerCube_type
);
633 case GLSL_SAMPLER_DIM_RECT
:
637 return sampler2DRectShadow_type
;
639 return sampler2DRect_type
;
640 case GLSL_SAMPLER_DIM_BUF
:
644 return samplerBuffer_type
;
645 case GLSL_SAMPLER_DIM_MS
:
648 return (array
? sampler2DMSArray_type
: sampler2DMS_type
);
649 case GLSL_SAMPLER_DIM_EXTERNAL
:
653 return samplerExternalOES_type
;
659 case GLSL_SAMPLER_DIM_1D
:
660 return (array
? isampler1DArray_type
: isampler1D_type
);
661 case GLSL_SAMPLER_DIM_2D
:
662 return (array
? isampler2DArray_type
: isampler2D_type
);
663 case GLSL_SAMPLER_DIM_3D
:
666 return isampler3D_type
;
667 case GLSL_SAMPLER_DIM_CUBE
:
668 return (array
? isamplerCubeArray_type
: isamplerCube_type
);
669 case GLSL_SAMPLER_DIM_RECT
:
672 return isampler2DRect_type
;
673 case GLSL_SAMPLER_DIM_BUF
:
676 return isamplerBuffer_type
;
677 case GLSL_SAMPLER_DIM_MS
:
678 return (array
? isampler2DMSArray_type
: isampler2DMS_type
);
679 case GLSL_SAMPLER_DIM_EXTERNAL
:
686 case GLSL_SAMPLER_DIM_1D
:
687 return (array
? usampler1DArray_type
: usampler1D_type
);
688 case GLSL_SAMPLER_DIM_2D
:
689 return (array
? usampler2DArray_type
: usampler2D_type
);
690 case GLSL_SAMPLER_DIM_3D
:
693 return usampler3D_type
;
694 case GLSL_SAMPLER_DIM_CUBE
:
695 return (array
? usamplerCubeArray_type
: usamplerCube_type
);
696 case GLSL_SAMPLER_DIM_RECT
:
699 return usampler2DRect_type
;
700 case GLSL_SAMPLER_DIM_BUF
:
703 return usamplerBuffer_type
;
704 case GLSL_SAMPLER_DIM_MS
:
705 return (array
? usampler2DMSArray_type
: usampler2DMS_type
);
706 case GLSL_SAMPLER_DIM_EXTERNAL
:
713 unreachable("switch statement above should be complete");
717 glsl_type::get_image_instance(enum glsl_sampler_dim dim
,
718 bool array
, glsl_base_type type
)
721 case GLSL_TYPE_FLOAT
:
723 case GLSL_SAMPLER_DIM_1D
:
724 return (array
? image1DArray_type
: image1D_type
);
725 case GLSL_SAMPLER_DIM_2D
:
726 return (array
? image2DArray_type
: image2D_type
);
727 case GLSL_SAMPLER_DIM_3D
:
729 case GLSL_SAMPLER_DIM_CUBE
:
730 return (array
? imageCubeArray_type
: imageCube_type
);
731 case GLSL_SAMPLER_DIM_RECT
:
735 return image2DRect_type
;
736 case GLSL_SAMPLER_DIM_BUF
:
740 return imageBuffer_type
;
741 case GLSL_SAMPLER_DIM_MS
:
742 return (array
? image2DMSArray_type
: image2DMS_type
);
743 case GLSL_SAMPLER_DIM_EXTERNAL
:
748 case GLSL_SAMPLER_DIM_1D
:
749 return (array
? iimage1DArray_type
: iimage1D_type
);
750 case GLSL_SAMPLER_DIM_2D
:
751 return (array
? iimage2DArray_type
: iimage2D_type
);
752 case GLSL_SAMPLER_DIM_3D
:
755 return iimage3D_type
;
756 case GLSL_SAMPLER_DIM_CUBE
:
757 return (array
? iimageCubeArray_type
: iimageCube_type
);
758 case GLSL_SAMPLER_DIM_RECT
:
761 return iimage2DRect_type
;
762 case GLSL_SAMPLER_DIM_BUF
:
765 return iimageBuffer_type
;
766 case GLSL_SAMPLER_DIM_MS
:
767 return (array
? iimage2DMSArray_type
: iimage2DMS_type
);
768 case GLSL_SAMPLER_DIM_EXTERNAL
:
773 case GLSL_SAMPLER_DIM_1D
:
774 return (array
? uimage1DArray_type
: uimage1D_type
);
775 case GLSL_SAMPLER_DIM_2D
:
776 return (array
? uimage2DArray_type
: uimage2D_type
);
777 case GLSL_SAMPLER_DIM_3D
:
780 return uimage3D_type
;
781 case GLSL_SAMPLER_DIM_CUBE
:
782 return (array
? uimageCubeArray_type
: uimageCube_type
);
783 case GLSL_SAMPLER_DIM_RECT
:
786 return uimage2DRect_type
;
787 case GLSL_SAMPLER_DIM_BUF
:
790 return uimageBuffer_type
;
791 case GLSL_SAMPLER_DIM_MS
:
792 return (array
? uimage2DMSArray_type
: uimage2DMS_type
);
793 case GLSL_SAMPLER_DIM_EXTERNAL
:
800 unreachable("switch statement above should be complete");
804 glsl_type::get_array_instance(const glsl_type
*base
, unsigned array_size
)
806 /* Generate a name using the base type pointer in the key. This is
807 * done because the name of the base type may not be unique across
808 * shaders. For example, two shaders may have different record types
812 snprintf(key
, sizeof(key
), "%p[%u]", (void *) base
, array_size
);
814 mtx_lock(&glsl_type::mutex
);
816 if (array_types
== NULL
) {
817 array_types
= _mesa_hash_table_create(NULL
, _mesa_key_hash_string
,
818 _mesa_key_string_equal
);
821 const struct hash_entry
*entry
= _mesa_hash_table_search(array_types
, key
);
823 mtx_unlock(&glsl_type::mutex
);
824 const glsl_type
*t
= new glsl_type(base
, array_size
);
825 mtx_lock(&glsl_type::mutex
);
827 entry
= _mesa_hash_table_insert(array_types
,
828 ralloc_strdup(mem_ctx
, key
),
832 assert(((glsl_type
*) entry
->data
)->base_type
== GLSL_TYPE_ARRAY
);
833 assert(((glsl_type
*) entry
->data
)->length
== array_size
);
834 assert(((glsl_type
*) entry
->data
)->fields
.array
== base
);
836 mtx_unlock(&glsl_type::mutex
);
838 return (glsl_type
*) entry
->data
;
843 glsl_type::record_compare(const glsl_type
*b
) const
845 if (this->length
!= b
->length
)
848 if (this->interface_packing
!= b
->interface_packing
)
851 /* From the GLSL 4.20 specification (Sec 4.2):
853 * "Structures must have the same name, sequence of type names, and
854 * type definitions, and field names to be considered the same type."
856 * GLSL ES behaves the same (Ver 1.00 Sec 4.2.4, Ver 3.00 Sec 4.2.5).
858 * Note that we cannot force type name check when comparing unnamed
859 * structure types, these have a unique name assigned during parsing.
861 if (!this->is_anonymous() && !b
->is_anonymous())
862 if (strcmp(this->name
, b
->name
) != 0)
865 for (unsigned i
= 0; i
< this->length
; i
++) {
866 if (this->fields
.structure
[i
].type
!= b
->fields
.structure
[i
].type
)
868 if (strcmp(this->fields
.structure
[i
].name
,
869 b
->fields
.structure
[i
].name
) != 0)
871 if (this->fields
.structure
[i
].matrix_layout
872 != b
->fields
.structure
[i
].matrix_layout
)
874 if (this->fields
.structure
[i
].location
875 != b
->fields
.structure
[i
].location
)
877 if (this->fields
.structure
[i
].interpolation
878 != b
->fields
.structure
[i
].interpolation
)
880 if (this->fields
.structure
[i
].centroid
881 != b
->fields
.structure
[i
].centroid
)
883 if (this->fields
.structure
[i
].sample
884 != b
->fields
.structure
[i
].sample
)
886 if (this->fields
.structure
[i
].patch
887 != b
->fields
.structure
[i
].patch
)
889 if (this->fields
.structure
[i
].image_read_only
890 != b
->fields
.structure
[i
].image_read_only
)
892 if (this->fields
.structure
[i
].image_write_only
893 != b
->fields
.structure
[i
].image_write_only
)
895 if (this->fields
.structure
[i
].image_coherent
896 != b
->fields
.structure
[i
].image_coherent
)
898 if (this->fields
.structure
[i
].image_volatile
899 != b
->fields
.structure
[i
].image_volatile
)
901 if (this->fields
.structure
[i
].image_restrict
902 != b
->fields
.structure
[i
].image_restrict
)
904 if (this->fields
.structure
[i
].precision
905 != b
->fields
.structure
[i
].precision
)
914 glsl_type::record_key_compare(const void *a
, const void *b
)
916 const glsl_type
*const key1
= (glsl_type
*) a
;
917 const glsl_type
*const key2
= (glsl_type
*) b
;
919 return strcmp(key1
->name
, key2
->name
) == 0 && key1
->record_compare(key2
);
924 * Generate an integer hash value for a glsl_type structure type.
927 glsl_type::record_key_hash(const void *a
)
929 const glsl_type
*const key
= (glsl_type
*) a
;
930 uintptr_t hash
= key
->length
;
933 for (unsigned i
= 0; i
< key
->length
; i
++) {
934 /* casting pointer to uintptr_t */
935 hash
= (hash
* 13 ) + (uintptr_t) key
->fields
.structure
[i
].type
;
938 if (sizeof(hash
) == 8)
939 retval
= (hash
& 0xffffffff) ^ ((uint64_t) hash
>> 32);
948 glsl_type::get_record_instance(const glsl_struct_field
*fields
,
952 const glsl_type
key(fields
, num_fields
, name
);
954 mtx_lock(&glsl_type::mutex
);
956 if (record_types
== NULL
) {
957 record_types
= _mesa_hash_table_create(NULL
, record_key_hash
,
961 const struct hash_entry
*entry
= _mesa_hash_table_search(record_types
,
964 mtx_unlock(&glsl_type::mutex
);
965 const glsl_type
*t
= new glsl_type(fields
, num_fields
, name
);
966 mtx_lock(&glsl_type::mutex
);
968 entry
= _mesa_hash_table_insert(record_types
, t
, (void *) t
);
971 assert(((glsl_type
*) entry
->data
)->base_type
== GLSL_TYPE_STRUCT
);
972 assert(((glsl_type
*) entry
->data
)->length
== num_fields
);
973 assert(strcmp(((glsl_type
*) entry
->data
)->name
, name
) == 0);
975 mtx_unlock(&glsl_type::mutex
);
977 return (glsl_type
*) entry
->data
;
982 glsl_type::get_interface_instance(const glsl_struct_field
*fields
,
984 enum glsl_interface_packing packing
,
985 const char *block_name
)
987 const glsl_type
key(fields
, num_fields
, packing
, block_name
);
989 mtx_lock(&glsl_type::mutex
);
991 if (interface_types
== NULL
) {
992 interface_types
= _mesa_hash_table_create(NULL
, record_key_hash
,
996 const struct hash_entry
*entry
= _mesa_hash_table_search(interface_types
,
999 mtx_unlock(&glsl_type::mutex
);
1000 const glsl_type
*t
= new glsl_type(fields
, num_fields
,
1001 packing
, block_name
);
1002 mtx_lock(&glsl_type::mutex
);
1004 entry
= _mesa_hash_table_insert(interface_types
, t
, (void *) t
);
1007 assert(((glsl_type
*) entry
->data
)->base_type
== GLSL_TYPE_INTERFACE
);
1008 assert(((glsl_type
*) entry
->data
)->length
== num_fields
);
1009 assert(strcmp(((glsl_type
*) entry
->data
)->name
, block_name
) == 0);
1011 mtx_unlock(&glsl_type::mutex
);
1013 return (glsl_type
*) entry
->data
;
1017 glsl_type::get_subroutine_instance(const char *subroutine_name
)
1019 const glsl_type
key(subroutine_name
);
1021 mtx_lock(&glsl_type::mutex
);
1023 if (subroutine_types
== NULL
) {
1024 subroutine_types
= _mesa_hash_table_create(NULL
, record_key_hash
,
1025 record_key_compare
);
1028 const struct hash_entry
*entry
= _mesa_hash_table_search(subroutine_types
,
1030 if (entry
== NULL
) {
1031 mtx_unlock(&glsl_type::mutex
);
1032 const glsl_type
*t
= new glsl_type(subroutine_name
);
1033 mtx_lock(&glsl_type::mutex
);
1035 entry
= _mesa_hash_table_insert(subroutine_types
, t
, (void *) t
);
1038 assert(((glsl_type
*) entry
->data
)->base_type
== GLSL_TYPE_SUBROUTINE
);
1039 assert(strcmp(((glsl_type
*) entry
->data
)->name
, subroutine_name
) == 0);
1041 mtx_unlock(&glsl_type::mutex
);
1043 return (glsl_type
*) entry
->data
;
1048 function_key_compare(const void *a
, const void *b
)
1050 const glsl_type
*const key1
= (glsl_type
*) a
;
1051 const glsl_type
*const key2
= (glsl_type
*) b
;
1053 if (key1
->length
!= key2
->length
)
1056 return memcmp(key1
->fields
.parameters
, key2
->fields
.parameters
,
1057 (key1
->length
+ 1) * sizeof(*key1
->fields
.parameters
));
1062 function_key_hash(const void *a
)
1064 const glsl_type
*const key
= (glsl_type
*) a
;
1068 size
= snprintf(hash_key
, sizeof(hash_key
), "%08x", key
->length
);
1070 for (unsigned i
= 0; i
< key
->length
; i
++) {
1071 if (size
>= sizeof(hash_key
))
1074 size
+= snprintf(& hash_key
[size
], sizeof(hash_key
) - size
,
1075 "%p", (void *) key
->fields
.structure
[i
].type
);
1078 return _mesa_hash_string(hash_key
);
1082 glsl_type::get_function_instance(const glsl_type
*return_type
,
1083 const glsl_function_param
*params
,
1084 unsigned num_params
)
1086 const glsl_type
key(return_type
, params
, num_params
);
1088 mtx_lock(&glsl_type::mutex
);
1090 if (function_types
== NULL
) {
1091 function_types
= _mesa_hash_table_create(NULL
, function_key_hash
,
1092 function_key_compare
);
1095 struct hash_entry
*entry
= _mesa_hash_table_search(function_types
, &key
);
1096 if (entry
== NULL
) {
1097 mtx_unlock(&glsl_type::mutex
);
1098 const glsl_type
*t
= new glsl_type(return_type
, params
, num_params
);
1099 mtx_lock(&glsl_type::mutex
);
1101 entry
= _mesa_hash_table_insert(function_types
, t
, (void *) t
);
1104 const glsl_type
*t
= (const glsl_type
*)entry
->data
;
1106 assert(t
->base_type
== GLSL_TYPE_FUNCTION
);
1107 assert(t
->length
== num_params
);
1109 mtx_unlock(&glsl_type::mutex
);
1116 glsl_type::get_mul_type(const glsl_type
*type_a
, const glsl_type
*type_b
)
1118 if (type_a
== type_b
) {
1120 } else if (type_a
->is_matrix() && type_b
->is_matrix()) {
1121 /* Matrix multiply. The columns of A must match the rows of B. Given
1122 * the other previously tested constraints, this means the vector type
1123 * of a row from A must be the same as the vector type of a column from
1126 if (type_a
->row_type() == type_b
->column_type()) {
1127 /* The resulting matrix has the number of columns of matrix B and
1128 * the number of rows of matrix A. We get the row count of A by
1129 * looking at the size of a vector that makes up a column. The
1130 * transpose (size of a row) is done for B.
1132 const glsl_type
*const type
=
1133 get_instance(type_a
->base_type
,
1134 type_a
->column_type()->vector_elements
,
1135 type_b
->row_type()->vector_elements
);
1136 assert(type
!= error_type
);
1140 } else if (type_a
->is_matrix()) {
1141 /* A is a matrix and B is a column vector. Columns of A must match
1142 * rows of B. Given the other previously tested constraints, this
1143 * means the vector type of a row from A must be the same as the
1144 * vector the type of B.
1146 if (type_a
->row_type() == type_b
) {
1147 /* The resulting vector has a number of elements equal to
1148 * the number of rows of matrix A. */
1149 const glsl_type
*const type
=
1150 get_instance(type_a
->base_type
,
1151 type_a
->column_type()->vector_elements
,
1153 assert(type
!= error_type
);
1158 assert(type_b
->is_matrix());
1160 /* A is a row vector and B is a matrix. Columns of A must match rows
1161 * of B. Given the other previously tested constraints, this means
1162 * the type of A must be the same as the vector type of a column from
1165 if (type_a
== type_b
->column_type()) {
1166 /* The resulting vector has a number of elements equal to
1167 * the number of columns of matrix B. */
1168 const glsl_type
*const type
=
1169 get_instance(type_a
->base_type
,
1170 type_b
->row_type()->vector_elements
,
1172 assert(type
!= error_type
);
1183 glsl_type::field_type(const char *name
) const
1185 if (this->base_type
!= GLSL_TYPE_STRUCT
1186 && this->base_type
!= GLSL_TYPE_INTERFACE
)
1189 for (unsigned i
= 0; i
< this->length
; i
++) {
1190 if (strcmp(name
, this->fields
.structure
[i
].name
) == 0)
1191 return this->fields
.structure
[i
].type
;
1199 glsl_type::field_index(const char *name
) const
1201 if (this->base_type
!= GLSL_TYPE_STRUCT
1202 && this->base_type
!= GLSL_TYPE_INTERFACE
)
1205 for (unsigned i
= 0; i
< this->length
; i
++) {
1206 if (strcmp(name
, this->fields
.structure
[i
].name
) == 0)
1215 glsl_type::component_slots() const
1217 switch (this->base_type
) {
1218 case GLSL_TYPE_UINT
:
1220 case GLSL_TYPE_FLOAT
:
1221 case GLSL_TYPE_BOOL
:
1222 return this->components();
1224 case GLSL_TYPE_DOUBLE
:
1225 return 2 * this->components();
1227 case GLSL_TYPE_STRUCT
:
1228 case GLSL_TYPE_INTERFACE
: {
1231 for (unsigned i
= 0; i
< this->length
; i
++)
1232 size
+= this->fields
.structure
[i
].type
->component_slots();
1237 case GLSL_TYPE_ARRAY
:
1238 return this->length
* this->fields
.array
->component_slots();
1240 case GLSL_TYPE_IMAGE
:
1242 case GLSL_TYPE_SUBROUTINE
:
1245 case GLSL_TYPE_FUNCTION
:
1246 case GLSL_TYPE_SAMPLER
:
1247 case GLSL_TYPE_ATOMIC_UINT
:
1248 case GLSL_TYPE_VOID
:
1249 case GLSL_TYPE_ERROR
:
1257 glsl_type::record_location_offset(unsigned length
) const
1259 unsigned offset
= 0;
1260 const glsl_type
*t
= this->without_array();
1261 if (t
->is_record()) {
1262 assert(length
<= t
->length
);
1264 for (unsigned i
= 0; i
< length
; i
++) {
1265 const glsl_type
*st
= t
->fields
.structure
[i
].type
;
1266 const glsl_type
*wa
= st
->without_array();
1267 if (wa
->is_record()) {
1268 unsigned r_offset
= wa
->record_location_offset(wa
->length
);
1269 offset
+= st
->is_array() ?
1270 st
->arrays_of_arrays_size() * r_offset
: r_offset
;
1271 } else if (st
->is_array() && st
->fields
.array
->is_array()) {
1272 unsigned outer_array_size
= st
->length
;
1273 const glsl_type
*base_type
= st
->fields
.array
;
1275 /* For arrays of arrays the outer arrays take up a uniform
1276 * slot for each element. The innermost array elements share a
1277 * single slot so we ignore the innermost array when calculating
1280 while (base_type
->fields
.array
->is_array()) {
1281 outer_array_size
= outer_array_size
* base_type
->length
;
1282 base_type
= base_type
->fields
.array
;
1284 offset
+= outer_array_size
;
1286 /* We dont worry about arrays here because unless the array
1287 * contains a structure or another array it only takes up a single
1298 glsl_type::uniform_locations() const
1302 switch (this->base_type
) {
1303 case GLSL_TYPE_UINT
:
1305 case GLSL_TYPE_FLOAT
:
1306 case GLSL_TYPE_DOUBLE
:
1307 case GLSL_TYPE_BOOL
:
1308 case GLSL_TYPE_SAMPLER
:
1309 case GLSL_TYPE_IMAGE
:
1310 case GLSL_TYPE_SUBROUTINE
:
1313 case GLSL_TYPE_STRUCT
:
1314 case GLSL_TYPE_INTERFACE
:
1315 for (unsigned i
= 0; i
< this->length
; i
++)
1316 size
+= this->fields
.structure
[i
].type
->uniform_locations();
1318 case GLSL_TYPE_ARRAY
:
1319 return this->length
* this->fields
.array
->uniform_locations();
1326 glsl_type::can_implicitly_convert_to(const glsl_type
*desired
,
1327 _mesa_glsl_parse_state
*state
) const
1329 if (this == desired
)
1332 /* There is no conversion among matrix types. */
1333 if (this->matrix_columns
> 1 || desired
->matrix_columns
> 1)
1336 /* Vector size must match. */
1337 if (this->vector_elements
!= desired
->vector_elements
)
1340 /* int and uint can be converted to float. */
1341 if (desired
->is_float() && this->is_integer())
1344 /* With GLSL 4.0 / ARB_gpu_shader5, int can be converted to uint.
1345 * Note that state may be NULL here, when resolving function calls in the
1346 * linker. By this time, all the state-dependent checks have already
1347 * happened though, so allow anything that's allowed in any shader version. */
1348 if ((!state
|| state
->is_version(400, 0) || state
->ARB_gpu_shader5_enable
) &&
1349 desired
->base_type
== GLSL_TYPE_UINT
&& this->base_type
== GLSL_TYPE_INT
)
1352 /* No implicit conversions from double. */
1353 if ((!state
|| state
->has_double()) && this->is_double())
1356 /* Conversions from different types to double. */
1357 if ((!state
|| state
->has_double()) && desired
->is_double()) {
1358 if (this->is_float())
1360 if (this->is_integer())
1368 glsl_type::std140_base_alignment(bool row_major
) const
1370 unsigned N
= is_double() ? 8 : 4;
1372 /* (1) If the member is a scalar consuming <N> basic machine units, the
1373 * base alignment is <N>.
1375 * (2) If the member is a two- or four-component vector with components
1376 * consuming <N> basic machine units, the base alignment is 2<N> or
1377 * 4<N>, respectively.
1379 * (3) If the member is a three-component vector with components consuming
1380 * <N> basic machine units, the base alignment is 4<N>.
1382 if (this->is_scalar() || this->is_vector()) {
1383 switch (this->vector_elements
) {
1394 /* (4) If the member is an array of scalars or vectors, the base alignment
1395 * and array stride are set to match the base alignment of a single
1396 * array element, according to rules (1), (2), and (3), and rounded up
1397 * to the base alignment of a vec4. The array may have padding at the
1398 * end; the base offset of the member following the array is rounded up
1399 * to the next multiple of the base alignment.
1401 * (6) If the member is an array of <S> column-major matrices with <C>
1402 * columns and <R> rows, the matrix is stored identically to a row of
1403 * <S>*<C> column vectors with <R> components each, according to rule
1406 * (8) If the member is an array of <S> row-major matrices with <C> columns
1407 * and <R> rows, the matrix is stored identically to a row of <S>*<R>
1408 * row vectors with <C> components each, according to rule (4).
1410 * (10) If the member is an array of <S> structures, the <S> elements of
1411 * the array are laid out in order, according to rule (9).
1413 if (this->is_array()) {
1414 if (this->fields
.array
->is_scalar() ||
1415 this->fields
.array
->is_vector() ||
1416 this->fields
.array
->is_matrix()) {
1417 return MAX2(this->fields
.array
->std140_base_alignment(row_major
), 16);
1419 assert(this->fields
.array
->is_record() ||
1420 this->fields
.array
->is_array());
1421 return this->fields
.array
->std140_base_alignment(row_major
);
1425 /* (5) If the member is a column-major matrix with <C> columns and
1426 * <R> rows, the matrix is stored identically to an array of
1427 * <C> column vectors with <R> components each, according to
1430 * (7) If the member is a row-major matrix with <C> columns and <R>
1431 * rows, the matrix is stored identically to an array of <R>
1432 * row vectors with <C> components each, according to rule (4).
1434 if (this->is_matrix()) {
1435 const struct glsl_type
*vec_type
, *array_type
;
1436 int c
= this->matrix_columns
;
1437 int r
= this->vector_elements
;
1440 vec_type
= get_instance(base_type
, c
, 1);
1441 array_type
= glsl_type::get_array_instance(vec_type
, r
);
1443 vec_type
= get_instance(base_type
, r
, 1);
1444 array_type
= glsl_type::get_array_instance(vec_type
, c
);
1447 return array_type
->std140_base_alignment(false);
1450 /* (9) If the member is a structure, the base alignment of the
1451 * structure is <N>, where <N> is the largest base alignment
1452 * value of any of its members, and rounded up to the base
1453 * alignment of a vec4. The individual members of this
1454 * sub-structure are then assigned offsets by applying this set
1455 * of rules recursively, where the base offset of the first
1456 * member of the sub-structure is equal to the aligned offset
1457 * of the structure. The structure may have padding at the end;
1458 * the base offset of the member following the sub-structure is
1459 * rounded up to the next multiple of the base alignment of the
1462 if (this->is_record()) {
1463 unsigned base_alignment
= 16;
1464 for (unsigned i
= 0; i
< this->length
; i
++) {
1465 bool field_row_major
= row_major
;
1466 const enum glsl_matrix_layout matrix_layout
=
1467 glsl_matrix_layout(this->fields
.structure
[i
].matrix_layout
);
1468 if (matrix_layout
== GLSL_MATRIX_LAYOUT_ROW_MAJOR
) {
1469 field_row_major
= true;
1470 } else if (matrix_layout
== GLSL_MATRIX_LAYOUT_COLUMN_MAJOR
) {
1471 field_row_major
= false;
1474 const struct glsl_type
*field_type
= this->fields
.structure
[i
].type
;
1475 base_alignment
= MAX2(base_alignment
,
1476 field_type
->std140_base_alignment(field_row_major
));
1478 return base_alignment
;
1481 assert(!"not reached");
1486 glsl_type::std140_size(bool row_major
) const
1488 unsigned N
= is_double() ? 8 : 4;
1490 /* (1) If the member is a scalar consuming <N> basic machine units, the
1491 * base alignment is <N>.
1493 * (2) If the member is a two- or four-component vector with components
1494 * consuming <N> basic machine units, the base alignment is 2<N> or
1495 * 4<N>, respectively.
1497 * (3) If the member is a three-component vector with components consuming
1498 * <N> basic machine units, the base alignment is 4<N>.
1500 if (this->is_scalar() || this->is_vector()) {
1501 return this->vector_elements
* N
;
1504 /* (5) If the member is a column-major matrix with <C> columns and
1505 * <R> rows, the matrix is stored identically to an array of
1506 * <C> column vectors with <R> components each, according to
1509 * (6) If the member is an array of <S> column-major matrices with <C>
1510 * columns and <R> rows, the matrix is stored identically to a row of
1511 * <S>*<C> column vectors with <R> components each, according to rule
1514 * (7) If the member is a row-major matrix with <C> columns and <R>
1515 * rows, the matrix is stored identically to an array of <R>
1516 * row vectors with <C> components each, according to rule (4).
1518 * (8) If the member is an array of <S> row-major matrices with <C> columns
1519 * and <R> rows, the matrix is stored identically to a row of <S>*<R>
1520 * row vectors with <C> components each, according to rule (4).
1522 if (this->without_array()->is_matrix()) {
1523 const struct glsl_type
*element_type
;
1524 const struct glsl_type
*vec_type
;
1525 unsigned int array_len
;
1527 if (this->is_array()) {
1528 element_type
= this->without_array();
1529 array_len
= this->arrays_of_arrays_size();
1531 element_type
= this;
1536 vec_type
= get_instance(element_type
->base_type
,
1537 element_type
->matrix_columns
, 1);
1539 array_len
*= element_type
->vector_elements
;
1541 vec_type
= get_instance(element_type
->base_type
,
1542 element_type
->vector_elements
, 1);
1543 array_len
*= element_type
->matrix_columns
;
1545 const glsl_type
*array_type
= glsl_type::get_array_instance(vec_type
,
1548 return array_type
->std140_size(false);
1551 /* (4) If the member is an array of scalars or vectors, the base alignment
1552 * and array stride are set to match the base alignment of a single
1553 * array element, according to rules (1), (2), and (3), and rounded up
1554 * to the base alignment of a vec4. The array may have padding at the
1555 * end; the base offset of the member following the array is rounded up
1556 * to the next multiple of the base alignment.
1558 * (10) If the member is an array of <S> structures, the <S> elements of
1559 * the array are laid out in order, according to rule (9).
1561 if (this->is_array()) {
1562 if (this->without_array()->is_record()) {
1563 return this->arrays_of_arrays_size() *
1564 this->without_array()->std140_size(row_major
);
1566 unsigned element_base_align
=
1567 this->without_array()->std140_base_alignment(row_major
);
1568 return this->arrays_of_arrays_size() * MAX2(element_base_align
, 16);
1572 /* (9) If the member is a structure, the base alignment of the
1573 * structure is <N>, where <N> is the largest base alignment
1574 * value of any of its members, and rounded up to the base
1575 * alignment of a vec4. The individual members of this
1576 * sub-structure are then assigned offsets by applying this set
1577 * of rules recursively, where the base offset of the first
1578 * member of the sub-structure is equal to the aligned offset
1579 * of the structure. The structure may have padding at the end;
1580 * the base offset of the member following the sub-structure is
1581 * rounded up to the next multiple of the base alignment of the
1584 if (this->is_record() || this->is_interface()) {
1586 unsigned max_align
= 0;
1588 for (unsigned i
= 0; i
< this->length
; i
++) {
1589 bool field_row_major
= row_major
;
1590 const enum glsl_matrix_layout matrix_layout
=
1591 glsl_matrix_layout(this->fields
.structure
[i
].matrix_layout
);
1592 if (matrix_layout
== GLSL_MATRIX_LAYOUT_ROW_MAJOR
) {
1593 field_row_major
= true;
1594 } else if (matrix_layout
== GLSL_MATRIX_LAYOUT_COLUMN_MAJOR
) {
1595 field_row_major
= false;
1598 const struct glsl_type
*field_type
= this->fields
.structure
[i
].type
;
1599 unsigned align
= field_type
->std140_base_alignment(field_row_major
);
1601 /* Ignore unsized arrays when calculating size */
1602 if (field_type
->is_unsized_array())
1605 size
= glsl_align(size
, align
);
1606 size
+= field_type
->std140_size(field_row_major
);
1608 max_align
= MAX2(align
, max_align
);
1610 if (field_type
->is_record() && (i
+ 1 < this->length
))
1611 size
= glsl_align(size
, 16);
1613 size
= glsl_align(size
, MAX2(max_align
, 16));
1617 assert(!"not reached");
1622 glsl_type::std430_base_alignment(bool row_major
) const
1625 unsigned N
= is_double() ? 8 : 4;
1627 /* (1) If the member is a scalar consuming <N> basic machine units, the
1628 * base alignment is <N>.
1630 * (2) If the member is a two- or four-component vector with components
1631 * consuming <N> basic machine units, the base alignment is 2<N> or
1632 * 4<N>, respectively.
1634 * (3) If the member is a three-component vector with components consuming
1635 * <N> basic machine units, the base alignment is 4<N>.
1637 if (this->is_scalar() || this->is_vector()) {
1638 switch (this->vector_elements
) {
1649 /* OpenGL 4.30 spec, section 7.6.2.2 "Standard Uniform Block Layout":
1651 * "When using the std430 storage layout, shader storage blocks will be
1652 * laid out in buffer storage identically to uniform and shader storage
1653 * blocks using the std140 layout, except that the base alignment and
1654 * stride of arrays of scalars and vectors in rule 4 and of structures
1655 * in rule 9 are not rounded up a multiple of the base alignment of a vec4.
1658 /* (1) If the member is a scalar consuming <N> basic machine units, the
1659 * base alignment is <N>.
1661 * (2) If the member is a two- or four-component vector with components
1662 * consuming <N> basic machine units, the base alignment is 2<N> or
1663 * 4<N>, respectively.
1665 * (3) If the member is a three-component vector with components consuming
1666 * <N> basic machine units, the base alignment is 4<N>.
1668 if (this->is_array())
1669 return this->fields
.array
->std430_base_alignment(row_major
);
1671 /* (5) If the member is a column-major matrix with <C> columns and
1672 * <R> rows, the matrix is stored identically to an array of
1673 * <C> column vectors with <R> components each, according to
1676 * (7) If the member is a row-major matrix with <C> columns and <R>
1677 * rows, the matrix is stored identically to an array of <R>
1678 * row vectors with <C> components each, according to rule (4).
1680 if (this->is_matrix()) {
1681 const struct glsl_type
*vec_type
, *array_type
;
1682 int c
= this->matrix_columns
;
1683 int r
= this->vector_elements
;
1686 vec_type
= get_instance(base_type
, c
, 1);
1687 array_type
= glsl_type::get_array_instance(vec_type
, r
);
1689 vec_type
= get_instance(base_type
, r
, 1);
1690 array_type
= glsl_type::get_array_instance(vec_type
, c
);
1693 return array_type
->std430_base_alignment(false);
1696 /* (9) If the member is a structure, the base alignment of the
1697 * structure is <N>, where <N> is the largest base alignment
1698 * value of any of its members, and rounded up to the base
1699 * alignment of a vec4. The individual members of this
1700 * sub-structure are then assigned offsets by applying this set
1701 * of rules recursively, where the base offset of the first
1702 * member of the sub-structure is equal to the aligned offset
1703 * of the structure. The structure may have padding at the end;
1704 * the base offset of the member following the sub-structure is
1705 * rounded up to the next multiple of the base alignment of the
1708 if (this->is_record()) {
1709 unsigned base_alignment
= 0;
1710 for (unsigned i
= 0; i
< this->length
; i
++) {
1711 bool field_row_major
= row_major
;
1712 const enum glsl_matrix_layout matrix_layout
=
1713 glsl_matrix_layout(this->fields
.structure
[i
].matrix_layout
);
1714 if (matrix_layout
== GLSL_MATRIX_LAYOUT_ROW_MAJOR
) {
1715 field_row_major
= true;
1716 } else if (matrix_layout
== GLSL_MATRIX_LAYOUT_COLUMN_MAJOR
) {
1717 field_row_major
= false;
1720 const struct glsl_type
*field_type
= this->fields
.structure
[i
].type
;
1721 base_alignment
= MAX2(base_alignment
,
1722 field_type
->std430_base_alignment(field_row_major
));
1724 assert(base_alignment
> 0);
1725 return base_alignment
;
1727 assert(!"not reached");
1732 glsl_type::std430_array_stride(bool row_major
) const
1734 unsigned N
= is_double() ? 8 : 4;
1736 /* Notice that the array stride of a vec3 is not 3 * N but 4 * N.
1737 * See OpenGL 4.30 spec, section 7.6.2.2 "Standard Uniform Block Layout"
1739 * (3) If the member is a three-component vector with components consuming
1740 * <N> basic machine units, the base alignment is 4<N>.
1742 if (this->is_vector() && this->vector_elements
== 3)
1745 /* By default use std430_size(row_major) */
1746 return this->std430_size(row_major
);
1750 glsl_type::std430_size(bool row_major
) const
1752 unsigned N
= is_double() ? 8 : 4;
1754 /* OpenGL 4.30 spec, section 7.6.2.2 "Standard Uniform Block Layout":
1756 * "When using the std430 storage layout, shader storage blocks will be
1757 * laid out in buffer storage identically to uniform and shader storage
1758 * blocks using the std140 layout, except that the base alignment and
1759 * stride of arrays of scalars and vectors in rule 4 and of structures
1760 * in rule 9 are not rounded up a multiple of the base alignment of a vec4.
1762 if (this->is_scalar() || this->is_vector())
1763 return this->vector_elements
* N
;
1765 if (this->without_array()->is_matrix()) {
1766 const struct glsl_type
*element_type
;
1767 const struct glsl_type
*vec_type
;
1768 unsigned int array_len
;
1770 if (this->is_array()) {
1771 element_type
= this->without_array();
1772 array_len
= this->arrays_of_arrays_size();
1774 element_type
= this;
1779 vec_type
= get_instance(element_type
->base_type
,
1780 element_type
->matrix_columns
, 1);
1782 array_len
*= element_type
->vector_elements
;
1784 vec_type
= get_instance(element_type
->base_type
,
1785 element_type
->vector_elements
, 1);
1786 array_len
*= element_type
->matrix_columns
;
1788 const glsl_type
*array_type
= glsl_type::get_array_instance(vec_type
,
1791 return array_type
->std430_size(false);
1794 if (this->is_array()) {
1795 if (this->without_array()->is_record())
1796 return this->arrays_of_arrays_size() *
1797 this->without_array()->std430_size(row_major
);
1799 return this->arrays_of_arrays_size() *
1800 this->without_array()->std430_base_alignment(row_major
);
1803 if (this->is_record() || this->is_interface()) {
1805 unsigned max_align
= 0;
1807 for (unsigned i
= 0; i
< this->length
; i
++) {
1808 bool field_row_major
= row_major
;
1809 const enum glsl_matrix_layout matrix_layout
=
1810 glsl_matrix_layout(this->fields
.structure
[i
].matrix_layout
);
1811 if (matrix_layout
== GLSL_MATRIX_LAYOUT_ROW_MAJOR
) {
1812 field_row_major
= true;
1813 } else if (matrix_layout
== GLSL_MATRIX_LAYOUT_COLUMN_MAJOR
) {
1814 field_row_major
= false;
1817 const struct glsl_type
*field_type
= this->fields
.structure
[i
].type
;
1818 unsigned align
= field_type
->std430_base_alignment(field_row_major
);
1819 size
= glsl_align(size
, align
);
1820 size
+= field_type
->std430_size(field_row_major
);
1822 max_align
= MAX2(align
, max_align
);
1824 size
= glsl_align(size
, max_align
);
1828 assert(!"not reached");
1833 glsl_type::count_attribute_slots() const
1835 /* From page 31 (page 37 of the PDF) of the GLSL 1.50 spec:
1837 * "A scalar input counts the same amount against this limit as a vec4,
1838 * so applications may want to consider packing groups of four
1839 * unrelated float inputs together into a vector to better utilize the
1840 * capabilities of the underlying hardware. A matrix input will use up
1841 * multiple locations. The number of locations used will equal the
1842 * number of columns in the matrix."
1844 * The spec does not explicitly say how arrays are counted. However, it
1845 * should be safe to assume the total number of slots consumed by an array
1846 * is the number of entries in the array multiplied by the number of slots
1847 * consumed by a single element of the array.
1849 * The spec says nothing about how structs are counted, because vertex
1850 * attributes are not allowed to be (or contain) structs. However, Mesa
1851 * allows varying structs, the number of varying slots taken up by a
1852 * varying struct is simply equal to the sum of the number of slots taken
1853 * up by each element.
1855 switch (this->base_type
) {
1856 case GLSL_TYPE_UINT
:
1858 case GLSL_TYPE_FLOAT
:
1859 case GLSL_TYPE_BOOL
:
1860 case GLSL_TYPE_DOUBLE
:
1861 return this->matrix_columns
;
1863 case GLSL_TYPE_STRUCT
:
1864 case GLSL_TYPE_INTERFACE
: {
1867 for (unsigned i
= 0; i
< this->length
; i
++)
1868 size
+= this->fields
.structure
[i
].type
->count_attribute_slots();
1873 case GLSL_TYPE_ARRAY
:
1874 return this->length
* this->fields
.array
->count_attribute_slots();
1876 case GLSL_TYPE_FUNCTION
:
1877 case GLSL_TYPE_SAMPLER
:
1878 case GLSL_TYPE_IMAGE
:
1879 case GLSL_TYPE_ATOMIC_UINT
:
1880 case GLSL_TYPE_VOID
:
1881 case GLSL_TYPE_SUBROUTINE
:
1882 case GLSL_TYPE_ERROR
:
1886 assert(!"Unexpected type in count_attribute_slots()");
1892 glsl_type::coordinate_components() const
1896 switch (sampler_dimensionality
) {
1897 case GLSL_SAMPLER_DIM_1D
:
1898 case GLSL_SAMPLER_DIM_BUF
:
1901 case GLSL_SAMPLER_DIM_2D
:
1902 case GLSL_SAMPLER_DIM_RECT
:
1903 case GLSL_SAMPLER_DIM_MS
:
1904 case GLSL_SAMPLER_DIM_EXTERNAL
:
1907 case GLSL_SAMPLER_DIM_3D
:
1908 case GLSL_SAMPLER_DIM_CUBE
:
1912 assert(!"Should not get here.");
1917 /* Array textures need an additional component for the array index, except
1918 * for cubemap array images that behave like a 2D array of interleaved
1921 if (sampler_array
&&
1922 !(base_type
== GLSL_TYPE_IMAGE
&&
1923 sampler_dimensionality
== GLSL_SAMPLER_DIM_CUBE
))
1930 * Declarations of type flyweights (glsl_type::_foo_type) and
1931 * convenience pointers (glsl_type::foo_type).
1934 #define DECL_TYPE(NAME, ...) \
1935 const glsl_type glsl_type::_##NAME##_type = glsl_type(__VA_ARGS__, #NAME); \
1936 const glsl_type *const glsl_type::NAME##_type = &glsl_type::_##NAME##_type;
1938 #define STRUCT_TYPE(NAME)
1940 #include "builtin_type_macros.h"