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
;
168 mtx_unlock(&glsl_type::mutex
);
171 glsl_type::glsl_type(const glsl_type
*return_type
,
172 const glsl_function_param
*params
, unsigned num_params
) :
174 base_type(GLSL_TYPE_FUNCTION
),
175 sampler_dimensionality(0), sampler_shadow(0), sampler_array(0),
176 sampler_type(0), interface_packing(0),
177 vector_elements(0), matrix_columns(0),
182 mtx_lock(&glsl_type::mutex
);
184 init_ralloc_type_ctx();
186 this->fields
.parameters
= rzalloc_array(this->mem_ctx
,
187 glsl_function_param
, num_params
+ 1);
189 /* We store the return type as the first parameter */
190 this->fields
.parameters
[0].type
= return_type
;
191 this->fields
.parameters
[0].in
= false;
192 this->fields
.parameters
[0].out
= true;
194 /* We store the i'th parameter in slot i+1 */
195 for (i
= 0; i
< length
; i
++) {
196 this->fields
.parameters
[i
+ 1].type
= params
[i
].type
;
197 this->fields
.parameters
[i
+ 1].in
= params
[i
].in
;
198 this->fields
.parameters
[i
+ 1].out
= params
[i
].out
;
201 mtx_unlock(&glsl_type::mutex
);
204 glsl_type::glsl_type(const char *subroutine_name
) :
206 base_type(GLSL_TYPE_SUBROUTINE
),
207 sampler_dimensionality(0), sampler_shadow(0), sampler_array(0),
208 sampler_type(0), interface_packing(0),
209 vector_elements(0), matrix_columns(0),
212 mtx_lock(&glsl_type::mutex
);
214 init_ralloc_type_ctx();
215 assert(subroutine_name
!= NULL
);
216 this->name
= ralloc_strdup(this->mem_ctx
, subroutine_name
);
217 this->vector_elements
= 1;
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_array_instance(const glsl_type
*base
, unsigned array_size
)
719 /* Generate a name using the base type pointer in the key. This is
720 * done because the name of the base type may not be unique across
721 * shaders. For example, two shaders may have different record types
725 snprintf(key
, sizeof(key
), "%p[%u]", (void *) base
, array_size
);
727 mtx_lock(&glsl_type::mutex
);
729 if (array_types
== NULL
) {
730 array_types
= _mesa_hash_table_create(NULL
, _mesa_key_hash_string
,
731 _mesa_key_string_equal
);
734 const struct hash_entry
*entry
= _mesa_hash_table_search(array_types
, key
);
736 mtx_unlock(&glsl_type::mutex
);
737 const glsl_type
*t
= new glsl_type(base
, array_size
);
738 mtx_lock(&glsl_type::mutex
);
740 entry
= _mesa_hash_table_insert(array_types
,
741 ralloc_strdup(mem_ctx
, key
),
745 assert(((glsl_type
*) entry
->data
)->base_type
== GLSL_TYPE_ARRAY
);
746 assert(((glsl_type
*) entry
->data
)->length
== array_size
);
747 assert(((glsl_type
*) entry
->data
)->fields
.array
== base
);
749 mtx_unlock(&glsl_type::mutex
);
751 return (glsl_type
*) entry
->data
;
756 glsl_type::record_compare(const glsl_type
*b
) const
758 if (this->length
!= b
->length
)
761 if (this->interface_packing
!= b
->interface_packing
)
764 /* From the GLSL 4.20 specification (Sec 4.2):
766 * "Structures must have the same name, sequence of type names, and
767 * type definitions, and field names to be considered the same type."
769 * GLSL ES behaves the same (Ver 1.00 Sec 4.2.4, Ver 3.00 Sec 4.2.5).
771 * Note that we cannot force type name check when comparing unnamed
772 * structure types, these have a unique name assigned during parsing.
774 if (!this->is_anonymous() && !b
->is_anonymous())
775 if (strcmp(this->name
, b
->name
) != 0)
778 for (unsigned i
= 0; i
< this->length
; i
++) {
779 if (this->fields
.structure
[i
].type
!= b
->fields
.structure
[i
].type
)
781 if (strcmp(this->fields
.structure
[i
].name
,
782 b
->fields
.structure
[i
].name
) != 0)
784 if (this->fields
.structure
[i
].matrix_layout
785 != b
->fields
.structure
[i
].matrix_layout
)
787 if (this->fields
.structure
[i
].location
788 != b
->fields
.structure
[i
].location
)
790 if (this->fields
.structure
[i
].interpolation
791 != b
->fields
.structure
[i
].interpolation
)
793 if (this->fields
.structure
[i
].centroid
794 != b
->fields
.structure
[i
].centroid
)
796 if (this->fields
.structure
[i
].sample
797 != b
->fields
.structure
[i
].sample
)
799 if (this->fields
.structure
[i
].patch
800 != b
->fields
.structure
[i
].patch
)
802 if (this->fields
.structure
[i
].image_read_only
803 != b
->fields
.structure
[i
].image_read_only
)
805 if (this->fields
.structure
[i
].image_write_only
806 != b
->fields
.structure
[i
].image_write_only
)
808 if (this->fields
.structure
[i
].image_coherent
809 != b
->fields
.structure
[i
].image_coherent
)
811 if (this->fields
.structure
[i
].image_volatile
812 != b
->fields
.structure
[i
].image_volatile
)
814 if (this->fields
.structure
[i
].image_restrict
815 != b
->fields
.structure
[i
].image_restrict
)
824 glsl_type::record_key_compare(const void *a
, const void *b
)
826 const glsl_type
*const key1
= (glsl_type
*) a
;
827 const glsl_type
*const key2
= (glsl_type
*) b
;
829 return strcmp(key1
->name
, key2
->name
) == 0 && key1
->record_compare(key2
);
834 * Generate an integer hash value for a glsl_type structure type.
837 glsl_type::record_key_hash(const void *a
)
839 const glsl_type
*const key
= (glsl_type
*) a
;
840 uintptr_t hash
= key
->length
;
843 for (unsigned i
= 0; i
< key
->length
; i
++) {
844 /* casting pointer to uintptr_t */
845 hash
= (hash
* 13 ) + (uintptr_t) key
->fields
.structure
[i
].type
;
848 if (sizeof(hash
) == 8)
849 retval
= (hash
& 0xffffffff) ^ ((uint64_t) hash
>> 32);
858 glsl_type::get_record_instance(const glsl_struct_field
*fields
,
862 const glsl_type
key(fields
, num_fields
, name
);
864 mtx_lock(&glsl_type::mutex
);
866 if (record_types
== NULL
) {
867 record_types
= _mesa_hash_table_create(NULL
, record_key_hash
,
871 const struct hash_entry
*entry
= _mesa_hash_table_search(record_types
,
874 mtx_unlock(&glsl_type::mutex
);
875 const glsl_type
*t
= new glsl_type(fields
, num_fields
, name
);
876 mtx_lock(&glsl_type::mutex
);
878 entry
= _mesa_hash_table_insert(record_types
, t
, (void *) t
);
881 assert(((glsl_type
*) entry
->data
)->base_type
== GLSL_TYPE_STRUCT
);
882 assert(((glsl_type
*) entry
->data
)->length
== num_fields
);
883 assert(strcmp(((glsl_type
*) entry
->data
)->name
, name
) == 0);
885 mtx_unlock(&glsl_type::mutex
);
887 return (glsl_type
*) entry
->data
;
892 glsl_type::get_interface_instance(const glsl_struct_field
*fields
,
894 enum glsl_interface_packing packing
,
895 const char *block_name
)
897 const glsl_type
key(fields
, num_fields
, packing
, block_name
);
899 mtx_lock(&glsl_type::mutex
);
901 if (interface_types
== NULL
) {
902 interface_types
= _mesa_hash_table_create(NULL
, record_key_hash
,
906 const struct hash_entry
*entry
= _mesa_hash_table_search(interface_types
,
909 mtx_unlock(&glsl_type::mutex
);
910 const glsl_type
*t
= new glsl_type(fields
, num_fields
,
911 packing
, block_name
);
912 mtx_lock(&glsl_type::mutex
);
914 entry
= _mesa_hash_table_insert(interface_types
, t
, (void *) t
);
917 assert(((glsl_type
*) entry
->data
)->base_type
== GLSL_TYPE_INTERFACE
);
918 assert(((glsl_type
*) entry
->data
)->length
== num_fields
);
919 assert(strcmp(((glsl_type
*) entry
->data
)->name
, block_name
) == 0);
921 mtx_unlock(&glsl_type::mutex
);
923 return (glsl_type
*) entry
->data
;
927 glsl_type::get_subroutine_instance(const char *subroutine_name
)
929 const glsl_type
key(subroutine_name
);
931 mtx_lock(&glsl_type::mutex
);
933 if (subroutine_types
== NULL
) {
934 subroutine_types
= _mesa_hash_table_create(NULL
, record_key_hash
,
938 const struct hash_entry
*entry
= _mesa_hash_table_search(subroutine_types
,
941 mtx_unlock(&glsl_type::mutex
);
942 const glsl_type
*t
= new glsl_type(subroutine_name
);
943 mtx_lock(&glsl_type::mutex
);
945 entry
= _mesa_hash_table_insert(subroutine_types
, t
, (void *) t
);
948 assert(((glsl_type
*) entry
->data
)->base_type
== GLSL_TYPE_SUBROUTINE
);
949 assert(strcmp(((glsl_type
*) entry
->data
)->name
, subroutine_name
) == 0);
951 mtx_unlock(&glsl_type::mutex
);
953 return (glsl_type
*) entry
->data
;
958 function_key_compare(const void *a
, const void *b
)
960 const glsl_type
*const key1
= (glsl_type
*) a
;
961 const glsl_type
*const key2
= (glsl_type
*) b
;
963 if (key1
->length
!= key2
->length
)
966 return memcmp(key1
->fields
.parameters
, key2
->fields
.parameters
,
967 (key1
->length
+ 1) * sizeof(*key1
->fields
.parameters
));
972 function_key_hash(const void *a
)
974 const glsl_type
*const key
= (glsl_type
*) a
;
978 size
= snprintf(hash_key
, sizeof(hash_key
), "%08x", key
->length
);
980 for (unsigned i
= 0; i
< key
->length
; i
++) {
981 if (size
>= sizeof(hash_key
))
984 size
+= snprintf(& hash_key
[size
], sizeof(hash_key
) - size
,
985 "%p", (void *) key
->fields
.structure
[i
].type
);
988 return _mesa_hash_string(hash_key
);
992 glsl_type::get_function_instance(const glsl_type
*return_type
,
993 const glsl_function_param
*params
,
996 const glsl_type
key(return_type
, params
, num_params
);
998 mtx_lock(&glsl_type::mutex
);
1000 if (function_types
== NULL
) {
1001 function_types
= _mesa_hash_table_create(NULL
, function_key_hash
,
1002 function_key_compare
);
1005 struct hash_entry
*entry
= _mesa_hash_table_search(function_types
, &key
);
1006 if (entry
== NULL
) {
1007 mtx_unlock(&glsl_type::mutex
);
1008 const glsl_type
*t
= new glsl_type(return_type
, params
, num_params
);
1009 mtx_lock(&glsl_type::mutex
);
1011 entry
= _mesa_hash_table_insert(function_types
, t
, (void *) t
);
1014 const glsl_type
*t
= (const glsl_type
*)entry
->data
;
1016 assert(t
->base_type
== GLSL_TYPE_FUNCTION
);
1017 assert(t
->length
== num_params
);
1019 mtx_unlock(&glsl_type::mutex
);
1026 glsl_type::get_mul_type(const glsl_type
*type_a
, const glsl_type
*type_b
)
1028 if (type_a
== type_b
) {
1030 } else if (type_a
->is_matrix() && type_b
->is_matrix()) {
1031 /* Matrix multiply. The columns of A must match the rows of B. Given
1032 * the other previously tested constraints, this means the vector type
1033 * of a row from A must be the same as the vector type of a column from
1036 if (type_a
->row_type() == type_b
->column_type()) {
1037 /* The resulting matrix has the number of columns of matrix B and
1038 * the number of rows of matrix A. We get the row count of A by
1039 * looking at the size of a vector that makes up a column. The
1040 * transpose (size of a row) is done for B.
1042 const glsl_type
*const type
=
1043 get_instance(type_a
->base_type
,
1044 type_a
->column_type()->vector_elements
,
1045 type_b
->row_type()->vector_elements
);
1046 assert(type
!= error_type
);
1050 } else if (type_a
->is_matrix()) {
1051 /* A is a matrix and B is a column vector. Columns of A must match
1052 * rows of B. Given the other previously tested constraints, this
1053 * means the vector type of a row from A must be the same as the
1054 * vector the type of B.
1056 if (type_a
->row_type() == type_b
) {
1057 /* The resulting vector has a number of elements equal to
1058 * the number of rows of matrix A. */
1059 const glsl_type
*const type
=
1060 get_instance(type_a
->base_type
,
1061 type_a
->column_type()->vector_elements
,
1063 assert(type
!= error_type
);
1068 assert(type_b
->is_matrix());
1070 /* A is a row vector and B is a matrix. Columns of A must match rows
1071 * of B. Given the other previously tested constraints, this means
1072 * the type of A must be the same as the vector type of a column from
1075 if (type_a
== type_b
->column_type()) {
1076 /* The resulting vector has a number of elements equal to
1077 * the number of columns of matrix B. */
1078 const glsl_type
*const type
=
1079 get_instance(type_a
->base_type
,
1080 type_b
->row_type()->vector_elements
,
1082 assert(type
!= error_type
);
1093 glsl_type::field_type(const char *name
) const
1095 if (this->base_type
!= GLSL_TYPE_STRUCT
1096 && this->base_type
!= GLSL_TYPE_INTERFACE
)
1099 for (unsigned i
= 0; i
< this->length
; i
++) {
1100 if (strcmp(name
, this->fields
.structure
[i
].name
) == 0)
1101 return this->fields
.structure
[i
].type
;
1109 glsl_type::field_index(const char *name
) const
1111 if (this->base_type
!= GLSL_TYPE_STRUCT
1112 && this->base_type
!= GLSL_TYPE_INTERFACE
)
1115 for (unsigned i
= 0; i
< this->length
; i
++) {
1116 if (strcmp(name
, this->fields
.structure
[i
].name
) == 0)
1125 glsl_type::component_slots() const
1127 switch (this->base_type
) {
1128 case GLSL_TYPE_UINT
:
1130 case GLSL_TYPE_FLOAT
:
1131 case GLSL_TYPE_BOOL
:
1132 return this->components();
1134 case GLSL_TYPE_DOUBLE
:
1135 return 2 * this->components();
1137 case GLSL_TYPE_STRUCT
:
1138 case GLSL_TYPE_INTERFACE
: {
1141 for (unsigned i
= 0; i
< this->length
; i
++)
1142 size
+= this->fields
.structure
[i
].type
->component_slots();
1147 case GLSL_TYPE_ARRAY
:
1148 return this->length
* this->fields
.array
->component_slots();
1150 case GLSL_TYPE_IMAGE
:
1152 case GLSL_TYPE_SUBROUTINE
:
1155 case GLSL_TYPE_FUNCTION
:
1156 case GLSL_TYPE_SAMPLER
:
1157 case GLSL_TYPE_ATOMIC_UINT
:
1158 case GLSL_TYPE_VOID
:
1159 case GLSL_TYPE_ERROR
:
1167 glsl_type::record_location_offset(unsigned length
) const
1169 unsigned offset
= 0;
1170 const glsl_type
*t
= this->without_array();
1171 if (t
->is_record()) {
1172 assert(length
<= t
->length
);
1174 for (unsigned i
= 0; i
< length
; i
++) {
1175 const glsl_type
*st
= t
->fields
.structure
[i
].type
;
1176 const glsl_type
*wa
= st
->without_array();
1177 if (wa
->is_record()) {
1178 unsigned r_offset
= wa
->record_location_offset(wa
->length
);
1179 offset
+= st
->is_array() ? st
->length
* r_offset
: r_offset
;
1181 /* We dont worry about arrays here because unless the array
1182 * contains a structure or another array it only takes up a single
1193 glsl_type::uniform_locations() const
1197 switch (this->base_type
) {
1198 case GLSL_TYPE_UINT
:
1200 case GLSL_TYPE_FLOAT
:
1201 case GLSL_TYPE_DOUBLE
:
1202 case GLSL_TYPE_BOOL
:
1203 case GLSL_TYPE_SAMPLER
:
1204 case GLSL_TYPE_IMAGE
:
1205 case GLSL_TYPE_SUBROUTINE
:
1208 case GLSL_TYPE_STRUCT
:
1209 case GLSL_TYPE_INTERFACE
:
1210 for (unsigned i
= 0; i
< this->length
; i
++)
1211 size
+= this->fields
.structure
[i
].type
->uniform_locations();
1213 case GLSL_TYPE_ARRAY
:
1214 return this->length
* this->fields
.array
->uniform_locations();
1221 glsl_type::can_implicitly_convert_to(const glsl_type
*desired
,
1222 _mesa_glsl_parse_state
*state
) const
1224 if (this == desired
)
1227 /* There is no conversion among matrix types. */
1228 if (this->matrix_columns
> 1 || desired
->matrix_columns
> 1)
1231 /* Vector size must match. */
1232 if (this->vector_elements
!= desired
->vector_elements
)
1235 /* int and uint can be converted to float. */
1236 if (desired
->is_float() && this->is_integer())
1239 /* With GLSL 4.0 / ARB_gpu_shader5, int can be converted to uint.
1240 * Note that state may be NULL here, when resolving function calls in the
1241 * linker. By this time, all the state-dependent checks have already
1242 * happened though, so allow anything that's allowed in any shader version. */
1243 if ((!state
|| state
->is_version(400, 0) || state
->ARB_gpu_shader5_enable
) &&
1244 desired
->base_type
== GLSL_TYPE_UINT
&& this->base_type
== GLSL_TYPE_INT
)
1247 /* No implicit conversions from double. */
1248 if ((!state
|| state
->has_double()) && this->is_double())
1251 /* Conversions from different types to double. */
1252 if ((!state
|| state
->has_double()) && desired
->is_double()) {
1253 if (this->is_float())
1255 if (this->is_integer())
1263 glsl_type::std140_base_alignment(bool row_major
) const
1265 unsigned N
= is_double() ? 8 : 4;
1267 /* (1) If the member is a scalar consuming <N> basic machine units, the
1268 * base alignment is <N>.
1270 * (2) If the member is a two- or four-component vector with components
1271 * consuming <N> basic machine units, the base alignment is 2<N> or
1272 * 4<N>, respectively.
1274 * (3) If the member is a three-component vector with components consuming
1275 * <N> basic machine units, the base alignment is 4<N>.
1277 if (this->is_scalar() || this->is_vector()) {
1278 switch (this->vector_elements
) {
1289 /* (4) If the member is an array of scalars or vectors, the base alignment
1290 * and array stride are set to match the base alignment of a single
1291 * array element, according to rules (1), (2), and (3), and rounded up
1292 * to the base alignment of a vec4. The array may have padding at the
1293 * end; the base offset of the member following the array is rounded up
1294 * to the next multiple of the base alignment.
1296 * (6) If the member is an array of <S> column-major matrices with <C>
1297 * columns and <R> rows, the matrix is stored identically to a row of
1298 * <S>*<C> column vectors with <R> components each, according to rule
1301 * (8) If the member is an array of <S> row-major matrices with <C> columns
1302 * and <R> rows, the matrix is stored identically to a row of <S>*<R>
1303 * row vectors with <C> components each, according to rule (4).
1305 * (10) If the member is an array of <S> structures, the <S> elements of
1306 * the array are laid out in order, according to rule (9).
1308 if (this->is_array()) {
1309 if (this->fields
.array
->is_scalar() ||
1310 this->fields
.array
->is_vector() ||
1311 this->fields
.array
->is_matrix()) {
1312 return MAX2(this->fields
.array
->std140_base_alignment(row_major
), 16);
1314 assert(this->fields
.array
->is_record() ||
1315 this->fields
.array
->is_array());
1316 return this->fields
.array
->std140_base_alignment(row_major
);
1320 /* (5) If the member is a column-major matrix with <C> columns and
1321 * <R> rows, the matrix is stored identically to an array of
1322 * <C> column vectors with <R> components each, according to
1325 * (7) If the member is a row-major matrix with <C> columns and <R>
1326 * rows, the matrix is stored identically to an array of <R>
1327 * row vectors with <C> components each, according to rule (4).
1329 if (this->is_matrix()) {
1330 const struct glsl_type
*vec_type
, *array_type
;
1331 int c
= this->matrix_columns
;
1332 int r
= this->vector_elements
;
1335 vec_type
= get_instance(base_type
, c
, 1);
1336 array_type
= glsl_type::get_array_instance(vec_type
, r
);
1338 vec_type
= get_instance(base_type
, r
, 1);
1339 array_type
= glsl_type::get_array_instance(vec_type
, c
);
1342 return array_type
->std140_base_alignment(false);
1345 /* (9) If the member is a structure, the base alignment of the
1346 * structure is <N>, where <N> is the largest base alignment
1347 * value of any of its members, and rounded up to the base
1348 * alignment of a vec4. The individual members of this
1349 * sub-structure are then assigned offsets by applying this set
1350 * of rules recursively, where the base offset of the first
1351 * member of the sub-structure is equal to the aligned offset
1352 * of the structure. The structure may have padding at the end;
1353 * the base offset of the member following the sub-structure is
1354 * rounded up to the next multiple of the base alignment of the
1357 if (this->is_record()) {
1358 unsigned base_alignment
= 16;
1359 for (unsigned i
= 0; i
< this->length
; i
++) {
1360 bool field_row_major
= row_major
;
1361 const enum glsl_matrix_layout matrix_layout
=
1362 glsl_matrix_layout(this->fields
.structure
[i
].matrix_layout
);
1363 if (matrix_layout
== GLSL_MATRIX_LAYOUT_ROW_MAJOR
) {
1364 field_row_major
= true;
1365 } else if (matrix_layout
== GLSL_MATRIX_LAYOUT_COLUMN_MAJOR
) {
1366 field_row_major
= false;
1369 const struct glsl_type
*field_type
= this->fields
.structure
[i
].type
;
1370 base_alignment
= MAX2(base_alignment
,
1371 field_type
->std140_base_alignment(field_row_major
));
1373 return base_alignment
;
1376 assert(!"not reached");
1381 glsl_type::std140_size(bool row_major
) const
1383 unsigned N
= is_double() ? 8 : 4;
1385 /* (1) If the member is a scalar consuming <N> basic machine units, the
1386 * base alignment is <N>.
1388 * (2) If the member is a two- or four-component vector with components
1389 * consuming <N> basic machine units, the base alignment is 2<N> or
1390 * 4<N>, respectively.
1392 * (3) If the member is a three-component vector with components consuming
1393 * <N> basic machine units, the base alignment is 4<N>.
1395 if (this->is_scalar() || this->is_vector()) {
1396 return this->vector_elements
* N
;
1399 /* (5) If the member is a column-major matrix with <C> columns and
1400 * <R> rows, the matrix is stored identically to an array of
1401 * <C> column vectors with <R> components each, according to
1404 * (6) If the member is an array of <S> column-major matrices with <C>
1405 * columns and <R> rows, the matrix is stored identically to a row of
1406 * <S>*<C> column vectors with <R> components each, according to rule
1409 * (7) If the member is a row-major matrix with <C> columns and <R>
1410 * rows, the matrix is stored identically to an array of <R>
1411 * row vectors with <C> components each, according to rule (4).
1413 * (8) If the member is an array of <S> row-major matrices with <C> columns
1414 * and <R> rows, the matrix is stored identically to a row of <S>*<R>
1415 * row vectors with <C> components each, according to rule (4).
1417 if (this->without_array()->is_matrix()) {
1418 const struct glsl_type
*element_type
;
1419 const struct glsl_type
*vec_type
;
1420 unsigned int array_len
;
1422 if (this->is_array()) {
1423 element_type
= this->fields
.array
;
1424 array_len
= this->length
;
1426 element_type
= this;
1431 vec_type
= get_instance(element_type
->base_type
,
1432 element_type
->matrix_columns
, 1);
1434 array_len
*= element_type
->vector_elements
;
1436 vec_type
= get_instance(element_type
->base_type
,
1437 element_type
->vector_elements
, 1);
1438 array_len
*= element_type
->matrix_columns
;
1440 const glsl_type
*array_type
= glsl_type::get_array_instance(vec_type
,
1443 return array_type
->std140_size(false);
1446 /* (4) If the member is an array of scalars or vectors, the base alignment
1447 * and array stride are set to match the base alignment of a single
1448 * array element, according to rules (1), (2), and (3), and rounded up
1449 * to the base alignment of a vec4. The array may have padding at the
1450 * end; the base offset of the member following the array is rounded up
1451 * to the next multiple of the base alignment.
1453 * (10) If the member is an array of <S> structures, the <S> elements of
1454 * the array are laid out in order, according to rule (9).
1456 if (this->is_array()) {
1457 if (this->fields
.array
->is_record()) {
1458 return this->length
* this->fields
.array
->std140_size(row_major
);
1460 unsigned element_base_align
=
1461 this->fields
.array
->std140_base_alignment(row_major
);
1462 return this->length
* MAX2(element_base_align
, 16);
1466 /* (9) If the member is a structure, the base alignment of the
1467 * structure is <N>, where <N> is the largest base alignment
1468 * value of any of its members, and rounded up to the base
1469 * alignment of a vec4. The individual members of this
1470 * sub-structure are then assigned offsets by applying this set
1471 * of rules recursively, where the base offset of the first
1472 * member of the sub-structure is equal to the aligned offset
1473 * of the structure. The structure may have padding at the end;
1474 * the base offset of the member following the sub-structure is
1475 * rounded up to the next multiple of the base alignment of the
1478 if (this->is_record() || this->is_interface()) {
1480 unsigned max_align
= 0;
1482 for (unsigned i
= 0; i
< this->length
; i
++) {
1483 bool field_row_major
= row_major
;
1484 const enum glsl_matrix_layout matrix_layout
=
1485 glsl_matrix_layout(this->fields
.structure
[i
].matrix_layout
);
1486 if (matrix_layout
== GLSL_MATRIX_LAYOUT_ROW_MAJOR
) {
1487 field_row_major
= true;
1488 } else if (matrix_layout
== GLSL_MATRIX_LAYOUT_COLUMN_MAJOR
) {
1489 field_row_major
= false;
1492 const struct glsl_type
*field_type
= this->fields
.structure
[i
].type
;
1493 unsigned align
= field_type
->std140_base_alignment(field_row_major
);
1495 /* Ignore unsized arrays when calculating size */
1496 if (field_type
->is_unsized_array())
1499 size
= glsl_align(size
, align
);
1500 size
+= field_type
->std140_size(field_row_major
);
1502 max_align
= MAX2(align
, max_align
);
1504 if (field_type
->is_record() && (i
+ 1 < this->length
))
1505 size
= glsl_align(size
, 16);
1507 size
= glsl_align(size
, MAX2(max_align
, 16));
1511 assert(!"not reached");
1516 glsl_type::std430_base_alignment(bool row_major
) const
1519 unsigned N
= is_double() ? 8 : 4;
1521 /* (1) If the member is a scalar consuming <N> basic machine units, the
1522 * base alignment is <N>.
1524 * (2) If the member is a two- or four-component vector with components
1525 * consuming <N> basic machine units, the base alignment is 2<N> or
1526 * 4<N>, respectively.
1528 * (3) If the member is a three-component vector with components consuming
1529 * <N> basic machine units, the base alignment is 4<N>.
1531 if (this->is_scalar() || this->is_vector()) {
1532 switch (this->vector_elements
) {
1543 /* OpenGL 4.30 spec, section 7.6.2.2 "Standard Uniform Block Layout":
1545 * "When using the std430 storage layout, shader storage blocks will be
1546 * laid out in buffer storage identically to uniform and shader storage
1547 * blocks using the std140 layout, except that the base alignment and
1548 * stride of arrays of scalars and vectors in rule 4 and of structures
1549 * in rule 9 are not rounded up a multiple of the base alignment of a vec4.
1552 /* (1) If the member is a scalar consuming <N> basic machine units, the
1553 * base alignment is <N>.
1555 * (2) If the member is a two- or four-component vector with components
1556 * consuming <N> basic machine units, the base alignment is 2<N> or
1557 * 4<N>, respectively.
1559 * (3) If the member is a three-component vector with components consuming
1560 * <N> basic machine units, the base alignment is 4<N>.
1562 if (this->is_array())
1563 return this->fields
.array
->std430_base_alignment(row_major
);
1565 /* (5) If the member is a column-major matrix with <C> columns and
1566 * <R> rows, the matrix is stored identically to an array of
1567 * <C> column vectors with <R> components each, according to
1570 * (7) If the member is a row-major matrix with <C> columns and <R>
1571 * rows, the matrix is stored identically to an array of <R>
1572 * row vectors with <C> components each, according to rule (4).
1574 if (this->is_matrix()) {
1575 const struct glsl_type
*vec_type
, *array_type
;
1576 int c
= this->matrix_columns
;
1577 int r
= this->vector_elements
;
1580 vec_type
= get_instance(base_type
, c
, 1);
1581 array_type
= glsl_type::get_array_instance(vec_type
, r
);
1583 vec_type
= get_instance(base_type
, r
, 1);
1584 array_type
= glsl_type::get_array_instance(vec_type
, c
);
1587 return array_type
->std430_base_alignment(false);
1590 /* (9) If the member is a structure, the base alignment of the
1591 * structure is <N>, where <N> is the largest base alignment
1592 * value of any of its members, and rounded up to the base
1593 * alignment of a vec4. The individual members of this
1594 * sub-structure are then assigned offsets by applying this set
1595 * of rules recursively, where the base offset of the first
1596 * member of the sub-structure is equal to the aligned offset
1597 * of the structure. The structure may have padding at the end;
1598 * the base offset of the member following the sub-structure is
1599 * rounded up to the next multiple of the base alignment of the
1602 if (this->is_record()) {
1603 unsigned base_alignment
= 0;
1604 for (unsigned i
= 0; i
< this->length
; i
++) {
1605 bool field_row_major
= row_major
;
1606 const enum glsl_matrix_layout matrix_layout
=
1607 glsl_matrix_layout(this->fields
.structure
[i
].matrix_layout
);
1608 if (matrix_layout
== GLSL_MATRIX_LAYOUT_ROW_MAJOR
) {
1609 field_row_major
= true;
1610 } else if (matrix_layout
== GLSL_MATRIX_LAYOUT_COLUMN_MAJOR
) {
1611 field_row_major
= false;
1614 const struct glsl_type
*field_type
= this->fields
.structure
[i
].type
;
1615 base_alignment
= MAX2(base_alignment
,
1616 field_type
->std430_base_alignment(field_row_major
));
1618 return base_alignment
;
1620 assert(!"not reached");
1625 glsl_type::std430_array_stride(bool row_major
) const
1627 unsigned N
= is_double() ? 8 : 4;
1629 /* Notice that the array stride of a vec3 is not 3 * N but 4 * N.
1630 * See OpenGL 4.30 spec, section 7.6.2.2 "Standard Uniform Block Layout"
1632 * (3) If the member is a three-component vector with components consuming
1633 * <N> basic machine units, the base alignment is 4<N>.
1635 if (this->is_vector() && this->vector_elements
== 3)
1638 /* By default use std430_size(row_major) */
1639 return this->std430_size(row_major
);
1643 glsl_type::std430_size(bool row_major
) const
1645 unsigned N
= is_double() ? 8 : 4;
1647 /* OpenGL 4.30 spec, section 7.6.2.2 "Standard Uniform Block Layout":
1649 * "When using the std430 storage layout, shader storage blocks will be
1650 * laid out in buffer storage identically to uniform and shader storage
1651 * blocks using the std140 layout, except that the base alignment and
1652 * stride of arrays of scalars and vectors in rule 4 and of structures
1653 * in rule 9 are not rounded up a multiple of the base alignment of a vec4.
1655 if (this->is_scalar() || this->is_vector())
1656 return this->vector_elements
* N
;
1658 if (this->without_array()->is_matrix()) {
1659 const struct glsl_type
*element_type
;
1660 const struct glsl_type
*vec_type
;
1661 unsigned int array_len
;
1663 if (this->is_array()) {
1664 element_type
= this->fields
.array
;
1665 array_len
= this->length
;
1667 element_type
= this;
1672 vec_type
= get_instance(element_type
->base_type
,
1673 element_type
->matrix_columns
, 1);
1675 array_len
*= element_type
->vector_elements
;
1677 vec_type
= get_instance(element_type
->base_type
,
1678 element_type
->vector_elements
, 1);
1679 array_len
*= element_type
->matrix_columns
;
1681 const glsl_type
*array_type
= glsl_type::get_array_instance(vec_type
,
1684 return array_type
->std430_size(false);
1687 if (this->is_array()) {
1688 if (this->fields
.array
->is_record())
1689 return this->length
* this->fields
.array
->std430_size(row_major
);
1691 return this->length
* this->fields
.array
->std430_base_alignment(row_major
);
1694 if (this->is_record() || this->is_interface()) {
1696 unsigned max_align
= 0;
1698 for (unsigned i
= 0; i
< this->length
; i
++) {
1699 bool field_row_major
= row_major
;
1700 const enum glsl_matrix_layout matrix_layout
=
1701 glsl_matrix_layout(this->fields
.structure
[i
].matrix_layout
);
1702 if (matrix_layout
== GLSL_MATRIX_LAYOUT_ROW_MAJOR
) {
1703 field_row_major
= true;
1704 } else if (matrix_layout
== GLSL_MATRIX_LAYOUT_COLUMN_MAJOR
) {
1705 field_row_major
= false;
1708 const struct glsl_type
*field_type
= this->fields
.structure
[i
].type
;
1709 unsigned align
= field_type
->std430_base_alignment(field_row_major
);
1710 size
= glsl_align(size
, align
);
1711 size
+= field_type
->std430_size(field_row_major
);
1713 max_align
= MAX2(align
, max_align
);
1715 size
= glsl_align(size
, max_align
);
1719 assert(!"not reached");
1724 glsl_type::count_attribute_slots() const
1726 /* From page 31 (page 37 of the PDF) of the GLSL 1.50 spec:
1728 * "A scalar input counts the same amount against this limit as a vec4,
1729 * so applications may want to consider packing groups of four
1730 * unrelated float inputs together into a vector to better utilize the
1731 * capabilities of the underlying hardware. A matrix input will use up
1732 * multiple locations. The number of locations used will equal the
1733 * number of columns in the matrix."
1735 * The spec does not explicitly say how arrays are counted. However, it
1736 * should be safe to assume the total number of slots consumed by an array
1737 * is the number of entries in the array multiplied by the number of slots
1738 * consumed by a single element of the array.
1740 * The spec says nothing about how structs are counted, because vertex
1741 * attributes are not allowed to be (or contain) structs. However, Mesa
1742 * allows varying structs, the number of varying slots taken up by a
1743 * varying struct is simply equal to the sum of the number of slots taken
1744 * up by each element.
1746 switch (this->base_type
) {
1747 case GLSL_TYPE_UINT
:
1749 case GLSL_TYPE_FLOAT
:
1750 case GLSL_TYPE_BOOL
:
1751 case GLSL_TYPE_DOUBLE
:
1752 return this->matrix_columns
;
1754 case GLSL_TYPE_STRUCT
:
1755 case GLSL_TYPE_INTERFACE
: {
1758 for (unsigned i
= 0; i
< this->length
; i
++)
1759 size
+= this->fields
.structure
[i
].type
->count_attribute_slots();
1764 case GLSL_TYPE_ARRAY
:
1765 return this->length
* this->fields
.array
->count_attribute_slots();
1767 case GLSL_TYPE_FUNCTION
:
1768 case GLSL_TYPE_SAMPLER
:
1769 case GLSL_TYPE_IMAGE
:
1770 case GLSL_TYPE_ATOMIC_UINT
:
1771 case GLSL_TYPE_VOID
:
1772 case GLSL_TYPE_SUBROUTINE
:
1773 case GLSL_TYPE_ERROR
:
1777 assert(!"Unexpected type in count_attribute_slots()");
1783 glsl_type::coordinate_components() const
1787 switch (sampler_dimensionality
) {
1788 case GLSL_SAMPLER_DIM_1D
:
1789 case GLSL_SAMPLER_DIM_BUF
:
1792 case GLSL_SAMPLER_DIM_2D
:
1793 case GLSL_SAMPLER_DIM_RECT
:
1794 case GLSL_SAMPLER_DIM_MS
:
1795 case GLSL_SAMPLER_DIM_EXTERNAL
:
1798 case GLSL_SAMPLER_DIM_3D
:
1799 case GLSL_SAMPLER_DIM_CUBE
:
1803 assert(!"Should not get here.");
1808 /* Array textures need an additional component for the array index, except
1809 * for cubemap array images that behave like a 2D array of interleaved
1812 if (sampler_array
&&
1813 !(base_type
== GLSL_TYPE_IMAGE
&&
1814 sampler_dimensionality
== GLSL_SAMPLER_DIM_CUBE
))