2 * Copyright © 2009 Intel Corporation
4 * Permission is hereby granted, free of charge, to any person obtaining a
5 * copy of this software and associated documentation files (the "Software"),
6 * to deal in the Software without restriction, including without limitation
7 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
8 * and/or sell copies of the Software, and to permit persons to whom the
9 * Software is furnished to do so, subject to the following conditions:
11 * The above copyright notice and this permission notice (including the next
12 * paragraph) shall be included in all copies or substantial portions of the
15 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
18 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
20 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
21 * DEALINGS IN THE SOFTWARE.
25 #include "main/macros.h"
26 #include "compiler/glsl/glsl_parser_extras.h"
27 #include "glsl_types.h"
28 #include "util/hash_table.h"
31 mtx_t
glsl_type::mutex
= _MTX_INITIALIZER_NP
;
32 hash_table
*glsl_type::array_types
= NULL
;
33 hash_table
*glsl_type::record_types
= NULL
;
34 hash_table
*glsl_type::interface_types
= NULL
;
35 hash_table
*glsl_type::subroutine_types
= NULL
;
36 void *glsl_type::mem_ctx
= NULL
;
39 glsl_type::init_ralloc_type_ctx(void)
41 if (glsl_type::mem_ctx
== NULL
) {
42 glsl_type::mem_ctx
= ralloc_autofree_context();
43 assert(glsl_type::mem_ctx
!= NULL
);
47 glsl_type::glsl_type(GLenum gl_type
,
48 glsl_base_type base_type
, unsigned vector_elements
,
49 unsigned matrix_columns
, const char *name
) :
52 sampler_dimensionality(0), sampler_shadow(0), sampler_array(0),
53 sampler_type(0), interface_packing(0),
54 vector_elements(vector_elements
), matrix_columns(matrix_columns
),
57 mtx_lock(&glsl_type::mutex
);
59 init_ralloc_type_ctx();
61 this->name
= ralloc_strdup(this->mem_ctx
, name
);
63 mtx_unlock(&glsl_type::mutex
);
65 /* Neither dimension is zero or both dimensions are zero.
67 assert((vector_elements
== 0) == (matrix_columns
== 0));
68 memset(& fields
, 0, sizeof(fields
));
71 glsl_type::glsl_type(GLenum gl_type
, glsl_base_type base_type
,
72 enum glsl_sampler_dim dim
, bool shadow
, bool array
,
73 unsigned type
, const char *name
) :
76 sampler_dimensionality(dim
), sampler_shadow(shadow
),
77 sampler_array(array
), sampler_type(type
), interface_packing(0),
80 mtx_lock(&glsl_type::mutex
);
82 init_ralloc_type_ctx();
84 this->name
= ralloc_strdup(this->mem_ctx
, name
);
86 mtx_unlock(&glsl_type::mutex
);
88 memset(& fields
, 0, sizeof(fields
));
90 if (base_type
== GLSL_TYPE_SAMPLER
) {
91 /* Samplers take no storage whatsoever. */
92 matrix_columns
= vector_elements
= 0;
94 matrix_columns
= vector_elements
= 1;
98 glsl_type::glsl_type(const glsl_struct_field
*fields
, unsigned num_fields
,
101 base_type(GLSL_TYPE_STRUCT
),
102 sampler_dimensionality(0), sampler_shadow(0), sampler_array(0),
103 sampler_type(0), interface_packing(0),
104 vector_elements(0), matrix_columns(0),
109 mtx_lock(&glsl_type::mutex
);
111 init_ralloc_type_ctx();
112 assert(name
!= NULL
);
113 this->name
= ralloc_strdup(this->mem_ctx
, name
);
114 this->fields
.structure
= ralloc_array(this->mem_ctx
,
115 glsl_struct_field
, length
);
117 for (i
= 0; i
< length
; i
++) {
118 this->fields
.structure
[i
].type
= fields
[i
].type
;
119 this->fields
.structure
[i
].name
= ralloc_strdup(this->fields
.structure
,
121 this->fields
.structure
[i
].location
= fields
[i
].location
;
122 this->fields
.structure
[i
].interpolation
= fields
[i
].interpolation
;
123 this->fields
.structure
[i
].centroid
= fields
[i
].centroid
;
124 this->fields
.structure
[i
].sample
= fields
[i
].sample
;
125 this->fields
.structure
[i
].matrix_layout
= fields
[i
].matrix_layout
;
126 this->fields
.structure
[i
].patch
= fields
[i
].patch
;
127 this->fields
.structure
[i
].image_read_only
= fields
[i
].image_read_only
;
128 this->fields
.structure
[i
].image_write_only
= fields
[i
].image_write_only
;
129 this->fields
.structure
[i
].image_coherent
= fields
[i
].image_coherent
;
130 this->fields
.structure
[i
].image_volatile
= fields
[i
].image_volatile
;
131 this->fields
.structure
[i
].image_restrict
= fields
[i
].image_restrict
;
132 this->fields
.structure
[i
].precision
= fields
[i
].precision
;
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
].image_read_only
= fields
[i
].image_read_only
;
167 this->fields
.structure
[i
].image_write_only
= fields
[i
].image_write_only
;
168 this->fields
.structure
[i
].image_coherent
= fields
[i
].image_coherent
;
169 this->fields
.structure
[i
].image_volatile
= fields
[i
].image_volatile
;
170 this->fields
.structure
[i
].image_restrict
= fields
[i
].image_restrict
;
171 this->fields
.structure
[i
].precision
= fields
[i
].precision
;
174 mtx_unlock(&glsl_type::mutex
);
177 glsl_type::glsl_type(const char *subroutine_name
) :
179 base_type(GLSL_TYPE_SUBROUTINE
),
180 sampler_dimensionality(0), sampler_shadow(0), sampler_array(0),
181 sampler_type(0), interface_packing(0),
182 vector_elements(1), matrix_columns(1),
185 mtx_lock(&glsl_type::mutex
);
187 init_ralloc_type_ctx();
188 assert(subroutine_name
!= NULL
);
189 this->name
= ralloc_strdup(this->mem_ctx
, subroutine_name
);
190 mtx_unlock(&glsl_type::mutex
);
194 glsl_type::contains_sampler() const
196 if (this->is_array()) {
197 return this->fields
.array
->contains_sampler();
198 } else if (this->is_record()) {
199 for (unsigned int i
= 0; i
< this->length
; i
++) {
200 if (this->fields
.structure
[i
].type
->contains_sampler())
205 return this->is_sampler();
211 glsl_type::contains_integer() const
213 if (this->is_array()) {
214 return this->fields
.array
->contains_integer();
215 } else if (this->is_record()) {
216 for (unsigned int i
= 0; i
< this->length
; i
++) {
217 if (this->fields
.structure
[i
].type
->contains_integer())
222 return this->is_integer();
227 glsl_type::contains_double() const
229 if (this->is_array()) {
230 return this->fields
.array
->contains_double();
231 } else if (this->is_record()) {
232 for (unsigned int i
= 0; i
< this->length
; i
++) {
233 if (this->fields
.structure
[i
].type
->contains_double())
238 return this->is_double();
243 glsl_type::contains_opaque() const {
245 case GLSL_TYPE_SAMPLER
:
246 case GLSL_TYPE_IMAGE
:
247 case GLSL_TYPE_ATOMIC_UINT
:
249 case GLSL_TYPE_ARRAY
:
250 return fields
.array
->contains_opaque();
251 case GLSL_TYPE_STRUCT
:
252 for (unsigned int i
= 0; i
< length
; i
++) {
253 if (fields
.structure
[i
].type
->contains_opaque())
263 glsl_type::contains_subroutine() const
265 if (this->is_array()) {
266 return this->fields
.array
->contains_subroutine();
267 } else if (this->is_record()) {
268 for (unsigned int i
= 0; i
< this->length
; i
++) {
269 if (this->fields
.structure
[i
].type
->contains_subroutine())
274 return this->is_subroutine();
279 glsl_type::sampler_index() const
281 const glsl_type
*const t
= (this->is_array()) ? this->fields
.array
: this;
283 assert(t
->is_sampler());
285 switch (t
->sampler_dimensionality
) {
286 case GLSL_SAMPLER_DIM_1D
:
287 return (t
->sampler_array
) ? TEXTURE_1D_ARRAY_INDEX
: TEXTURE_1D_INDEX
;
288 case GLSL_SAMPLER_DIM_2D
:
289 return (t
->sampler_array
) ? TEXTURE_2D_ARRAY_INDEX
: TEXTURE_2D_INDEX
;
290 case GLSL_SAMPLER_DIM_3D
:
291 return TEXTURE_3D_INDEX
;
292 case GLSL_SAMPLER_DIM_CUBE
:
293 return (t
->sampler_array
) ? TEXTURE_CUBE_ARRAY_INDEX
: TEXTURE_CUBE_INDEX
;
294 case GLSL_SAMPLER_DIM_RECT
:
295 return TEXTURE_RECT_INDEX
;
296 case GLSL_SAMPLER_DIM_BUF
:
297 return TEXTURE_BUFFER_INDEX
;
298 case GLSL_SAMPLER_DIM_EXTERNAL
:
299 return TEXTURE_EXTERNAL_INDEX
;
300 case GLSL_SAMPLER_DIM_MS
:
301 return (t
->sampler_array
) ? TEXTURE_2D_MULTISAMPLE_ARRAY_INDEX
: TEXTURE_2D_MULTISAMPLE_INDEX
;
303 assert(!"Should not get here.");
304 return TEXTURE_BUFFER_INDEX
;
309 glsl_type::contains_image() const
311 if (this->is_array()) {
312 return this->fields
.array
->contains_image();
313 } else if (this->is_record()) {
314 for (unsigned int i
= 0; i
< this->length
; i
++) {
315 if (this->fields
.structure
[i
].type
->contains_image())
320 return this->is_image();
324 const glsl_type
*glsl_type::get_base_type() const
331 case GLSL_TYPE_FLOAT
:
333 case GLSL_TYPE_DOUBLE
:
343 const glsl_type
*glsl_type::get_scalar_type() const
345 const glsl_type
*type
= this;
348 while (type
->base_type
== GLSL_TYPE_ARRAY
)
349 type
= type
->fields
.array
;
351 /* Handle vectors and matrices */
352 switch (type
->base_type
) {
357 case GLSL_TYPE_FLOAT
:
359 case GLSL_TYPE_DOUBLE
:
364 /* Handle everything else */
371 _mesa_glsl_release_types(void)
373 /* Should only be called during atexit (either when unloading shared
374 * object, or if process terminates), so no mutex-locking should be
377 if (glsl_type::array_types
!= NULL
) {
378 _mesa_hash_table_destroy(glsl_type::array_types
, NULL
);
379 glsl_type::array_types
= NULL
;
382 if (glsl_type::record_types
!= NULL
) {
383 _mesa_hash_table_destroy(glsl_type::record_types
, NULL
);
384 glsl_type::record_types
= NULL
;
387 if (glsl_type::interface_types
!= NULL
) {
388 _mesa_hash_table_destroy(glsl_type::interface_types
, NULL
);
389 glsl_type::interface_types
= NULL
;
394 glsl_type::glsl_type(const glsl_type
*array
, unsigned length
) :
395 base_type(GLSL_TYPE_ARRAY
),
396 sampler_dimensionality(0), sampler_shadow(0), sampler_array(0),
397 sampler_type(0), interface_packing(0),
398 vector_elements(0), matrix_columns(0),
399 length(length
), name(NULL
)
401 this->fields
.array
= array
;
402 /* Inherit the gl type of the base. The GL type is used for
403 * uniform/statevar handling in Mesa and the arrayness of the type
404 * is represented by the size rather than the type.
406 this->gl_type
= array
->gl_type
;
408 /* Allow a maximum of 10 characters for the array size. This is enough
409 * for 32-bits of ~0. The extra 3 are for the '[', ']', and terminating
412 const unsigned name_length
= strlen(array
->name
) + 10 + 3;
414 mtx_lock(&glsl_type::mutex
);
415 char *const n
= (char *) ralloc_size(this->mem_ctx
, name_length
);
416 mtx_unlock(&glsl_type::mutex
);
419 snprintf(n
, name_length
, "%s[]", array
->name
);
421 /* insert outermost dimensions in the correct spot
422 * otherwise the dimension order will be backwards
424 const char *pos
= strchr(array
->name
, '[');
426 int idx
= pos
- array
->name
;
427 snprintf(n
, idx
+1, "%s", array
->name
);
428 snprintf(n
+ idx
, name_length
- idx
, "[%u]%s",
429 length
, array
->name
+ idx
);
431 snprintf(n
, name_length
, "%s[%u]", array
->name
, length
);
440 glsl_type::vec(unsigned components
)
442 if (components
== 0 || components
> 4)
445 static const glsl_type
*const ts
[] = {
446 float_type
, vec2_type
, vec3_type
, vec4_type
448 return ts
[components
- 1];
452 glsl_type::dvec(unsigned components
)
454 if (components
== 0 || components
> 4)
457 static const glsl_type
*const ts
[] = {
458 double_type
, dvec2_type
, dvec3_type
, dvec4_type
460 return ts
[components
- 1];
464 glsl_type::ivec(unsigned components
)
466 if (components
== 0 || components
> 4)
469 static const glsl_type
*const ts
[] = {
470 int_type
, ivec2_type
, ivec3_type
, ivec4_type
472 return ts
[components
- 1];
477 glsl_type::uvec(unsigned components
)
479 if (components
== 0 || components
> 4)
482 static const glsl_type
*const ts
[] = {
483 uint_type
, uvec2_type
, uvec3_type
, uvec4_type
485 return ts
[components
- 1];
490 glsl_type::bvec(unsigned components
)
492 if (components
== 0 || components
> 4)
495 static const glsl_type
*const ts
[] = {
496 bool_type
, bvec2_type
, bvec3_type
, bvec4_type
498 return ts
[components
- 1];
503 glsl_type::get_instance(unsigned base_type
, unsigned rows
, unsigned columns
)
505 if (base_type
== GLSL_TYPE_VOID
)
508 if ((rows
< 1) || (rows
> 4) || (columns
< 1) || (columns
> 4))
511 /* Treat GLSL vectors as Nx1 matrices.
519 case GLSL_TYPE_FLOAT
:
521 case GLSL_TYPE_DOUBLE
:
529 if ((base_type
!= GLSL_TYPE_FLOAT
&& base_type
!= GLSL_TYPE_DOUBLE
) || (rows
== 1))
532 /* GLSL matrix types are named mat{COLUMNS}x{ROWS}. Only the following
533 * combinations are valid:
541 #define IDX(c,r) (((c-1)*3) + (r-1))
543 if (base_type
== GLSL_TYPE_DOUBLE
) {
544 switch (IDX(columns
, rows
)) {
545 case IDX(2,2): return dmat2_type
;
546 case IDX(2,3): return dmat2x3_type
;
547 case IDX(2,4): return dmat2x4_type
;
548 case IDX(3,2): return dmat3x2_type
;
549 case IDX(3,3): return dmat3_type
;
550 case IDX(3,4): return dmat3x4_type
;
551 case IDX(4,2): return dmat4x2_type
;
552 case IDX(4,3): return dmat4x3_type
;
553 case IDX(4,4): return dmat4_type
;
554 default: return error_type
;
557 switch (IDX(columns
, rows
)) {
558 case IDX(2,2): return mat2_type
;
559 case IDX(2,3): return mat2x3_type
;
560 case IDX(2,4): return mat2x4_type
;
561 case IDX(3,2): return mat3x2_type
;
562 case IDX(3,3): return mat3_type
;
563 case IDX(3,4): return mat3x4_type
;
564 case IDX(4,2): return mat4x2_type
;
565 case IDX(4,3): return mat4x3_type
;
566 case IDX(4,4): return mat4_type
;
567 default: return error_type
;
572 assert(!"Should not get here.");
577 glsl_type::get_sampler_instance(enum glsl_sampler_dim dim
,
583 case GLSL_TYPE_FLOAT
:
585 case GLSL_SAMPLER_DIM_1D
:
587 return (array
? sampler1DArrayShadow_type
: sampler1DShadow_type
);
589 return (array
? sampler1DArray_type
: sampler1D_type
);
590 case GLSL_SAMPLER_DIM_2D
:
592 return (array
? sampler2DArrayShadow_type
: sampler2DShadow_type
);
594 return (array
? sampler2DArray_type
: sampler2D_type
);
595 case GLSL_SAMPLER_DIM_3D
:
599 return sampler3D_type
;
600 case GLSL_SAMPLER_DIM_CUBE
:
602 return (array
? samplerCubeArrayShadow_type
: samplerCubeShadow_type
);
604 return (array
? samplerCubeArray_type
: samplerCube_type
);
605 case GLSL_SAMPLER_DIM_RECT
:
609 return sampler2DRectShadow_type
;
611 return sampler2DRect_type
;
612 case GLSL_SAMPLER_DIM_BUF
:
616 return samplerBuffer_type
;
617 case GLSL_SAMPLER_DIM_MS
:
620 return (array
? sampler2DMSArray_type
: sampler2DMS_type
);
621 case GLSL_SAMPLER_DIM_EXTERNAL
:
625 return samplerExternalOES_type
;
631 case GLSL_SAMPLER_DIM_1D
:
632 return (array
? isampler1DArray_type
: isampler1D_type
);
633 case GLSL_SAMPLER_DIM_2D
:
634 return (array
? isampler2DArray_type
: isampler2D_type
);
635 case GLSL_SAMPLER_DIM_3D
:
638 return isampler3D_type
;
639 case GLSL_SAMPLER_DIM_CUBE
:
640 return (array
? isamplerCubeArray_type
: isamplerCube_type
);
641 case GLSL_SAMPLER_DIM_RECT
:
644 return isampler2DRect_type
;
645 case GLSL_SAMPLER_DIM_BUF
:
648 return isamplerBuffer_type
;
649 case GLSL_SAMPLER_DIM_MS
:
650 return (array
? isampler2DMSArray_type
: isampler2DMS_type
);
651 case GLSL_SAMPLER_DIM_EXTERNAL
:
658 case GLSL_SAMPLER_DIM_1D
:
659 return (array
? usampler1DArray_type
: usampler1D_type
);
660 case GLSL_SAMPLER_DIM_2D
:
661 return (array
? usampler2DArray_type
: usampler2D_type
);
662 case GLSL_SAMPLER_DIM_3D
:
665 return usampler3D_type
;
666 case GLSL_SAMPLER_DIM_CUBE
:
667 return (array
? usamplerCubeArray_type
: usamplerCube_type
);
668 case GLSL_SAMPLER_DIM_RECT
:
671 return usampler2DRect_type
;
672 case GLSL_SAMPLER_DIM_BUF
:
675 return usamplerBuffer_type
;
676 case GLSL_SAMPLER_DIM_MS
:
677 return (array
? usampler2DMSArray_type
: usampler2DMS_type
);
678 case GLSL_SAMPLER_DIM_EXTERNAL
:
685 unreachable("switch statement above should be complete");
689 glsl_type::get_array_instance(const glsl_type
*base
, unsigned array_size
)
691 /* Generate a name using the base type pointer in the key. This is
692 * done because the name of the base type may not be unique across
693 * shaders. For example, two shaders may have different record types
697 snprintf(key
, sizeof(key
), "%p[%u]", (void *) base
, array_size
);
699 mtx_lock(&glsl_type::mutex
);
701 if (array_types
== NULL
) {
702 array_types
= _mesa_hash_table_create(NULL
, _mesa_key_hash_string
,
703 _mesa_key_string_equal
);
706 const struct hash_entry
*entry
= _mesa_hash_table_search(array_types
, key
);
708 mtx_unlock(&glsl_type::mutex
);
709 const glsl_type
*t
= new glsl_type(base
, array_size
);
710 mtx_lock(&glsl_type::mutex
);
712 entry
= _mesa_hash_table_insert(array_types
,
713 ralloc_strdup(mem_ctx
, key
),
717 assert(((glsl_type
*) entry
->data
)->base_type
== GLSL_TYPE_ARRAY
);
718 assert(((glsl_type
*) entry
->data
)->length
== array_size
);
719 assert(((glsl_type
*) entry
->data
)->fields
.array
== base
);
721 mtx_unlock(&glsl_type::mutex
);
723 return (glsl_type
*) entry
->data
;
728 glsl_type::record_compare(const glsl_type
*b
) const
730 if (this->length
!= b
->length
)
733 if (this->interface_packing
!= b
->interface_packing
)
736 /* From the GLSL 4.20 specification (Sec 4.2):
738 * "Structures must have the same name, sequence of type names, and
739 * type definitions, and field names to be considered the same type."
741 * GLSL ES behaves the same (Ver 1.00 Sec 4.2.4, Ver 3.00 Sec 4.2.5).
743 * Note that we cannot force type name check when comparing unnamed
744 * structure types, these have a unique name assigned during parsing.
746 if (!this->is_anonymous() && !b
->is_anonymous())
747 if (strcmp(this->name
, b
->name
) != 0)
750 for (unsigned i
= 0; i
< this->length
; i
++) {
751 if (this->fields
.structure
[i
].type
!= b
->fields
.structure
[i
].type
)
753 if (strcmp(this->fields
.structure
[i
].name
,
754 b
->fields
.structure
[i
].name
) != 0)
756 if (this->fields
.structure
[i
].matrix_layout
757 != b
->fields
.structure
[i
].matrix_layout
)
759 if (this->fields
.structure
[i
].location
760 != b
->fields
.structure
[i
].location
)
762 if (this->fields
.structure
[i
].interpolation
763 != b
->fields
.structure
[i
].interpolation
)
765 if (this->fields
.structure
[i
].centroid
766 != b
->fields
.structure
[i
].centroid
)
768 if (this->fields
.structure
[i
].sample
769 != b
->fields
.structure
[i
].sample
)
771 if (this->fields
.structure
[i
].patch
772 != b
->fields
.structure
[i
].patch
)
774 if (this->fields
.structure
[i
].image_read_only
775 != b
->fields
.structure
[i
].image_read_only
)
777 if (this->fields
.structure
[i
].image_write_only
778 != b
->fields
.structure
[i
].image_write_only
)
780 if (this->fields
.structure
[i
].image_coherent
781 != b
->fields
.structure
[i
].image_coherent
)
783 if (this->fields
.structure
[i
].image_volatile
784 != b
->fields
.structure
[i
].image_volatile
)
786 if (this->fields
.structure
[i
].image_restrict
787 != b
->fields
.structure
[i
].image_restrict
)
789 if (this->fields
.structure
[i
].precision
790 != b
->fields
.structure
[i
].precision
)
799 glsl_type::record_key_compare(const void *a
, const void *b
)
801 const glsl_type
*const key1
= (glsl_type
*) a
;
802 const glsl_type
*const key2
= (glsl_type
*) b
;
804 return strcmp(key1
->name
, key2
->name
) == 0 && key1
->record_compare(key2
);
809 * Generate an integer hash value for a glsl_type structure type.
812 glsl_type::record_key_hash(const void *a
)
814 const glsl_type
*const key
= (glsl_type
*) a
;
815 uintptr_t hash
= key
->length
;
818 for (unsigned i
= 0; i
< key
->length
; i
++) {
819 /* casting pointer to uintptr_t */
820 hash
= (hash
* 13 ) + (uintptr_t) key
->fields
.structure
[i
].type
;
823 if (sizeof(hash
) == 8)
824 retval
= (hash
& 0xffffffff) ^ ((uint64_t) hash
>> 32);
833 glsl_type::get_record_instance(const glsl_struct_field
*fields
,
837 const glsl_type
key(fields
, num_fields
, name
);
839 mtx_lock(&glsl_type::mutex
);
841 if (record_types
== NULL
) {
842 record_types
= _mesa_hash_table_create(NULL
, record_key_hash
,
846 const struct hash_entry
*entry
= _mesa_hash_table_search(record_types
,
849 mtx_unlock(&glsl_type::mutex
);
850 const glsl_type
*t
= new glsl_type(fields
, num_fields
, name
);
851 mtx_lock(&glsl_type::mutex
);
853 entry
= _mesa_hash_table_insert(record_types
, t
, (void *) t
);
856 assert(((glsl_type
*) entry
->data
)->base_type
== GLSL_TYPE_STRUCT
);
857 assert(((glsl_type
*) entry
->data
)->length
== num_fields
);
858 assert(strcmp(((glsl_type
*) entry
->data
)->name
, name
) == 0);
860 mtx_unlock(&glsl_type::mutex
);
862 return (glsl_type
*) entry
->data
;
867 glsl_type::get_interface_instance(const glsl_struct_field
*fields
,
869 enum glsl_interface_packing packing
,
870 const char *block_name
)
872 const glsl_type
key(fields
, num_fields
, packing
, block_name
);
874 mtx_lock(&glsl_type::mutex
);
876 if (interface_types
== NULL
) {
877 interface_types
= _mesa_hash_table_create(NULL
, record_key_hash
,
881 const struct hash_entry
*entry
= _mesa_hash_table_search(interface_types
,
884 mtx_unlock(&glsl_type::mutex
);
885 const glsl_type
*t
= new glsl_type(fields
, num_fields
,
886 packing
, block_name
);
887 mtx_lock(&glsl_type::mutex
);
889 entry
= _mesa_hash_table_insert(interface_types
, t
, (void *) t
);
892 assert(((glsl_type
*) entry
->data
)->base_type
== GLSL_TYPE_INTERFACE
);
893 assert(((glsl_type
*) entry
->data
)->length
== num_fields
);
894 assert(strcmp(((glsl_type
*) entry
->data
)->name
, block_name
) == 0);
896 mtx_unlock(&glsl_type::mutex
);
898 return (glsl_type
*) entry
->data
;
902 glsl_type::get_subroutine_instance(const char *subroutine_name
)
904 const glsl_type
key(subroutine_name
);
906 mtx_lock(&glsl_type::mutex
);
908 if (subroutine_types
== NULL
) {
909 subroutine_types
= _mesa_hash_table_create(NULL
, record_key_hash
,
913 const struct hash_entry
*entry
= _mesa_hash_table_search(subroutine_types
,
916 mtx_unlock(&glsl_type::mutex
);
917 const glsl_type
*t
= new glsl_type(subroutine_name
);
918 mtx_lock(&glsl_type::mutex
);
920 entry
= _mesa_hash_table_insert(subroutine_types
, t
, (void *) t
);
923 assert(((glsl_type
*) entry
->data
)->base_type
== GLSL_TYPE_SUBROUTINE
);
924 assert(strcmp(((glsl_type
*) entry
->data
)->name
, subroutine_name
) == 0);
926 mtx_unlock(&glsl_type::mutex
);
928 return (glsl_type
*) entry
->data
;
933 glsl_type::get_mul_type(const glsl_type
*type_a
, const glsl_type
*type_b
)
935 if (type_a
== type_b
) {
937 } else if (type_a
->is_matrix() && type_b
->is_matrix()) {
938 /* Matrix multiply. The columns of A must match the rows of B. Given
939 * the other previously tested constraints, this means the vector type
940 * of a row from A must be the same as the vector type of a column from
943 if (type_a
->row_type() == type_b
->column_type()) {
944 /* The resulting matrix has the number of columns of matrix B and
945 * the number of rows of matrix A. We get the row count of A by
946 * looking at the size of a vector that makes up a column. The
947 * transpose (size of a row) is done for B.
949 const glsl_type
*const type
=
950 get_instance(type_a
->base_type
,
951 type_a
->column_type()->vector_elements
,
952 type_b
->row_type()->vector_elements
);
953 assert(type
!= error_type
);
957 } else if (type_a
->is_matrix()) {
958 /* A is a matrix and B is a column vector. Columns of A must match
959 * rows of B. Given the other previously tested constraints, this
960 * means the vector type of a row from A must be the same as the
961 * vector the type of B.
963 if (type_a
->row_type() == type_b
) {
964 /* The resulting vector has a number of elements equal to
965 * the number of rows of matrix A. */
966 const glsl_type
*const type
=
967 get_instance(type_a
->base_type
,
968 type_a
->column_type()->vector_elements
,
970 assert(type
!= error_type
);
975 assert(type_b
->is_matrix());
977 /* A is a row vector and B is a matrix. Columns of A must match rows
978 * of B. Given the other previously tested constraints, this means
979 * the type of A must be the same as the vector type of a column from
982 if (type_a
== type_b
->column_type()) {
983 /* The resulting vector has a number of elements equal to
984 * the number of columns of matrix B. */
985 const glsl_type
*const type
=
986 get_instance(type_a
->base_type
,
987 type_b
->row_type()->vector_elements
,
989 assert(type
!= error_type
);
1000 glsl_type::field_type(const char *name
) const
1002 if (this->base_type
!= GLSL_TYPE_STRUCT
1003 && this->base_type
!= GLSL_TYPE_INTERFACE
)
1006 for (unsigned i
= 0; i
< this->length
; i
++) {
1007 if (strcmp(name
, this->fields
.structure
[i
].name
) == 0)
1008 return this->fields
.structure
[i
].type
;
1016 glsl_type::field_index(const char *name
) const
1018 if (this->base_type
!= GLSL_TYPE_STRUCT
1019 && this->base_type
!= GLSL_TYPE_INTERFACE
)
1022 for (unsigned i
= 0; i
< this->length
; i
++) {
1023 if (strcmp(name
, this->fields
.structure
[i
].name
) == 0)
1032 glsl_type::component_slots() const
1034 switch (this->base_type
) {
1035 case GLSL_TYPE_UINT
:
1037 case GLSL_TYPE_FLOAT
:
1038 case GLSL_TYPE_BOOL
:
1039 return this->components();
1041 case GLSL_TYPE_DOUBLE
:
1042 return 2 * this->components();
1044 case GLSL_TYPE_STRUCT
:
1045 case GLSL_TYPE_INTERFACE
: {
1048 for (unsigned i
= 0; i
< this->length
; i
++)
1049 size
+= this->fields
.structure
[i
].type
->component_slots();
1054 case GLSL_TYPE_ARRAY
:
1055 return this->length
* this->fields
.array
->component_slots();
1057 case GLSL_TYPE_IMAGE
:
1059 case GLSL_TYPE_SUBROUTINE
:
1061 case GLSL_TYPE_SAMPLER
:
1062 case GLSL_TYPE_ATOMIC_UINT
:
1063 case GLSL_TYPE_VOID
:
1064 case GLSL_TYPE_ERROR
:
1072 glsl_type::record_location_offset(unsigned length
) const
1074 unsigned offset
= 0;
1075 const glsl_type
*t
= this->without_array();
1076 if (t
->is_record()) {
1077 assert(length
<= t
->length
);
1079 for (unsigned i
= 0; i
< length
; i
++) {
1080 const glsl_type
*st
= t
->fields
.structure
[i
].type
;
1081 const glsl_type
*wa
= st
->without_array();
1082 if (wa
->is_record()) {
1083 unsigned r_offset
= wa
->record_location_offset(wa
->length
);
1084 offset
+= st
->is_array() ?
1085 st
->arrays_of_arrays_size() * r_offset
: r_offset
;
1086 } else if (st
->is_array() && st
->fields
.array
->is_array()) {
1087 unsigned outer_array_size
= st
->length
;
1088 const glsl_type
*base_type
= st
->fields
.array
;
1090 /* For arrays of arrays the outer arrays take up a uniform
1091 * slot for each element. The innermost array elements share a
1092 * single slot so we ignore the innermost array when calculating
1095 while (base_type
->fields
.array
->is_array()) {
1096 outer_array_size
= outer_array_size
* base_type
->length
;
1097 base_type
= base_type
->fields
.array
;
1099 offset
+= outer_array_size
;
1101 /* We dont worry about arrays here because unless the array
1102 * contains a structure or another array it only takes up a single
1113 glsl_type::uniform_locations() const
1117 switch (this->base_type
) {
1118 case GLSL_TYPE_UINT
:
1120 case GLSL_TYPE_FLOAT
:
1121 case GLSL_TYPE_DOUBLE
:
1122 case GLSL_TYPE_BOOL
:
1123 case GLSL_TYPE_SAMPLER
:
1124 case GLSL_TYPE_IMAGE
:
1125 case GLSL_TYPE_SUBROUTINE
:
1128 case GLSL_TYPE_STRUCT
:
1129 case GLSL_TYPE_INTERFACE
:
1130 for (unsigned i
= 0; i
< this->length
; i
++)
1131 size
+= this->fields
.structure
[i
].type
->uniform_locations();
1133 case GLSL_TYPE_ARRAY
:
1134 return this->length
* this->fields
.array
->uniform_locations();
1141 glsl_type::can_implicitly_convert_to(const glsl_type
*desired
,
1142 _mesa_glsl_parse_state
*state
) const
1144 if (this == desired
)
1147 /* ESSL does not allow implicit conversions. If there is no state, we're
1148 * doing intra-stage function linking where these checks have already been
1151 if (state
&& state
->es_shader
)
1154 /* There is no conversion among matrix types. */
1155 if (this->matrix_columns
> 1 || desired
->matrix_columns
> 1)
1158 /* Vector size must match. */
1159 if (this->vector_elements
!= desired
->vector_elements
)
1162 /* int and uint can be converted to float. */
1163 if (desired
->is_float() && this->is_integer())
1166 /* With GLSL 4.0 / ARB_gpu_shader5, int can be converted to uint.
1167 * Note that state may be NULL here, when resolving function calls in the
1168 * linker. By this time, all the state-dependent checks have already
1169 * happened though, so allow anything that's allowed in any shader version. */
1170 if ((!state
|| state
->is_version(400, 0) || state
->ARB_gpu_shader5_enable
) &&
1171 desired
->base_type
== GLSL_TYPE_UINT
&& this->base_type
== GLSL_TYPE_INT
)
1174 /* No implicit conversions from double. */
1175 if ((!state
|| state
->has_double()) && this->is_double())
1178 /* Conversions from different types to double. */
1179 if ((!state
|| state
->has_double()) && desired
->is_double()) {
1180 if (this->is_float())
1182 if (this->is_integer())
1190 glsl_type::std140_base_alignment(bool row_major
) const
1192 unsigned N
= is_double() ? 8 : 4;
1194 /* (1) If the member is a scalar consuming <N> basic machine units, the
1195 * base alignment is <N>.
1197 * (2) If the member is a two- or four-component vector with components
1198 * consuming <N> basic machine units, the base alignment is 2<N> or
1199 * 4<N>, respectively.
1201 * (3) If the member is a three-component vector with components consuming
1202 * <N> basic machine units, the base alignment is 4<N>.
1204 if (this->is_scalar() || this->is_vector()) {
1205 switch (this->vector_elements
) {
1216 /* (4) If the member is an array of scalars or vectors, the base alignment
1217 * and array stride are set to match the base alignment of a single
1218 * array element, according to rules (1), (2), and (3), and rounded up
1219 * to the base alignment of a vec4. The array may have padding at the
1220 * end; the base offset of the member following the array is rounded up
1221 * to the next multiple of the base alignment.
1223 * (6) If the member is an array of <S> column-major matrices with <C>
1224 * columns and <R> rows, the matrix is stored identically to a row of
1225 * <S>*<C> column vectors with <R> components each, according to rule
1228 * (8) If the member is an array of <S> row-major matrices with <C> columns
1229 * and <R> rows, the matrix is stored identically to a row of <S>*<R>
1230 * row vectors with <C> components each, according to rule (4).
1232 * (10) If the member is an array of <S> structures, the <S> elements of
1233 * the array are laid out in order, according to rule (9).
1235 if (this->is_array()) {
1236 if (this->fields
.array
->is_scalar() ||
1237 this->fields
.array
->is_vector() ||
1238 this->fields
.array
->is_matrix()) {
1239 return MAX2(this->fields
.array
->std140_base_alignment(row_major
), 16);
1241 assert(this->fields
.array
->is_record() ||
1242 this->fields
.array
->is_array());
1243 return this->fields
.array
->std140_base_alignment(row_major
);
1247 /* (5) If the member is a column-major matrix with <C> columns and
1248 * <R> rows, the matrix is stored identically to an array of
1249 * <C> column vectors with <R> components each, according to
1252 * (7) If the member is a row-major matrix with <C> columns and <R>
1253 * rows, the matrix is stored identically to an array of <R>
1254 * row vectors with <C> components each, according to rule (4).
1256 if (this->is_matrix()) {
1257 const struct glsl_type
*vec_type
, *array_type
;
1258 int c
= this->matrix_columns
;
1259 int r
= this->vector_elements
;
1262 vec_type
= get_instance(base_type
, c
, 1);
1263 array_type
= glsl_type::get_array_instance(vec_type
, r
);
1265 vec_type
= get_instance(base_type
, r
, 1);
1266 array_type
= glsl_type::get_array_instance(vec_type
, c
);
1269 return array_type
->std140_base_alignment(false);
1272 /* (9) If the member is a structure, the base alignment of the
1273 * structure is <N>, where <N> is the largest base alignment
1274 * value of any of its members, and rounded up to the base
1275 * alignment of a vec4. The individual members of this
1276 * sub-structure are then assigned offsets by applying this set
1277 * of rules recursively, where the base offset of the first
1278 * member of the sub-structure is equal to the aligned offset
1279 * of the structure. The structure may have padding at the end;
1280 * the base offset of the member following the sub-structure is
1281 * rounded up to the next multiple of the base alignment of the
1284 if (this->is_record()) {
1285 unsigned base_alignment
= 16;
1286 for (unsigned i
= 0; i
< this->length
; i
++) {
1287 bool field_row_major
= row_major
;
1288 const enum glsl_matrix_layout matrix_layout
=
1289 glsl_matrix_layout(this->fields
.structure
[i
].matrix_layout
);
1290 if (matrix_layout
== GLSL_MATRIX_LAYOUT_ROW_MAJOR
) {
1291 field_row_major
= true;
1292 } else if (matrix_layout
== GLSL_MATRIX_LAYOUT_COLUMN_MAJOR
) {
1293 field_row_major
= false;
1296 const struct glsl_type
*field_type
= this->fields
.structure
[i
].type
;
1297 base_alignment
= MAX2(base_alignment
,
1298 field_type
->std140_base_alignment(field_row_major
));
1300 return base_alignment
;
1303 assert(!"not reached");
1308 glsl_type::std140_size(bool row_major
) const
1310 unsigned N
= is_double() ? 8 : 4;
1312 /* (1) If the member is a scalar consuming <N> basic machine units, the
1313 * base alignment is <N>.
1315 * (2) If the member is a two- or four-component vector with components
1316 * consuming <N> basic machine units, the base alignment is 2<N> or
1317 * 4<N>, respectively.
1319 * (3) If the member is a three-component vector with components consuming
1320 * <N> basic machine units, the base alignment is 4<N>.
1322 if (this->is_scalar() || this->is_vector()) {
1323 return this->vector_elements
* N
;
1326 /* (5) If the member is a column-major matrix with <C> columns and
1327 * <R> rows, the matrix is stored identically to an array of
1328 * <C> column vectors with <R> components each, according to
1331 * (6) If the member is an array of <S> column-major matrices with <C>
1332 * columns and <R> rows, the matrix is stored identically to a row of
1333 * <S>*<C> column vectors with <R> components each, according to rule
1336 * (7) If the member is a row-major matrix with <C> columns and <R>
1337 * rows, the matrix is stored identically to an array of <R>
1338 * row vectors with <C> components each, according to rule (4).
1340 * (8) If the member is an array of <S> row-major matrices with <C> columns
1341 * and <R> rows, the matrix is stored identically to a row of <S>*<R>
1342 * row vectors with <C> components each, according to rule (4).
1344 if (this->without_array()->is_matrix()) {
1345 const struct glsl_type
*element_type
;
1346 const struct glsl_type
*vec_type
;
1347 unsigned int array_len
;
1349 if (this->is_array()) {
1350 element_type
= this->without_array();
1351 array_len
= this->arrays_of_arrays_size();
1353 element_type
= this;
1358 vec_type
= get_instance(element_type
->base_type
,
1359 element_type
->matrix_columns
, 1);
1361 array_len
*= element_type
->vector_elements
;
1363 vec_type
= get_instance(element_type
->base_type
,
1364 element_type
->vector_elements
, 1);
1365 array_len
*= element_type
->matrix_columns
;
1367 const glsl_type
*array_type
= glsl_type::get_array_instance(vec_type
,
1370 return array_type
->std140_size(false);
1373 /* (4) If the member is an array of scalars or vectors, the base alignment
1374 * and array stride are set to match the base alignment of a single
1375 * array element, according to rules (1), (2), and (3), and rounded up
1376 * to the base alignment of a vec4. The array may have padding at the
1377 * end; the base offset of the member following the array is rounded up
1378 * to the next multiple of the base alignment.
1380 * (10) If the member is an array of <S> structures, the <S> elements of
1381 * the array are laid out in order, according to rule (9).
1383 if (this->is_array()) {
1384 if (this->without_array()->is_record()) {
1385 return this->arrays_of_arrays_size() *
1386 this->without_array()->std140_size(row_major
);
1388 unsigned element_base_align
=
1389 this->without_array()->std140_base_alignment(row_major
);
1390 return this->arrays_of_arrays_size() * MAX2(element_base_align
, 16);
1394 /* (9) If the member is a structure, the base alignment of the
1395 * structure is <N>, where <N> is the largest base alignment
1396 * value of any of its members, and rounded up to the base
1397 * alignment of a vec4. The individual members of this
1398 * sub-structure are then assigned offsets by applying this set
1399 * of rules recursively, where the base offset of the first
1400 * member of the sub-structure is equal to the aligned offset
1401 * of the structure. The structure may have padding at the end;
1402 * the base offset of the member following the sub-structure is
1403 * rounded up to the next multiple of the base alignment of the
1406 if (this->is_record() || this->is_interface()) {
1408 unsigned max_align
= 0;
1410 for (unsigned i
= 0; i
< this->length
; i
++) {
1411 bool field_row_major
= row_major
;
1412 const enum glsl_matrix_layout matrix_layout
=
1413 glsl_matrix_layout(this->fields
.structure
[i
].matrix_layout
);
1414 if (matrix_layout
== GLSL_MATRIX_LAYOUT_ROW_MAJOR
) {
1415 field_row_major
= true;
1416 } else if (matrix_layout
== GLSL_MATRIX_LAYOUT_COLUMN_MAJOR
) {
1417 field_row_major
= false;
1420 const struct glsl_type
*field_type
= this->fields
.structure
[i
].type
;
1421 unsigned align
= field_type
->std140_base_alignment(field_row_major
);
1423 /* Ignore unsized arrays when calculating size */
1424 if (field_type
->is_unsized_array())
1427 size
= glsl_align(size
, align
);
1428 size
+= field_type
->std140_size(field_row_major
);
1430 max_align
= MAX2(align
, max_align
);
1432 if (field_type
->is_record() && (i
+ 1 < this->length
))
1433 size
= glsl_align(size
, 16);
1435 size
= glsl_align(size
, MAX2(max_align
, 16));
1439 assert(!"not reached");
1444 glsl_type::std430_base_alignment(bool row_major
) const
1447 unsigned N
= is_double() ? 8 : 4;
1449 /* (1) If the member is a scalar consuming <N> basic machine units, the
1450 * base alignment is <N>.
1452 * (2) If the member is a two- or four-component vector with components
1453 * consuming <N> basic machine units, the base alignment is 2<N> or
1454 * 4<N>, respectively.
1456 * (3) If the member is a three-component vector with components consuming
1457 * <N> basic machine units, the base alignment is 4<N>.
1459 if (this->is_scalar() || this->is_vector()) {
1460 switch (this->vector_elements
) {
1471 /* OpenGL 4.30 spec, section 7.6.2.2 "Standard Uniform Block Layout":
1473 * "When using the std430 storage layout, shader storage blocks will be
1474 * laid out in buffer storage identically to uniform and shader storage
1475 * blocks using the std140 layout, except that the base alignment and
1476 * stride of arrays of scalars and vectors in rule 4 and of structures
1477 * in rule 9 are not rounded up a multiple of the base alignment of a vec4.
1480 /* (1) If the member is a scalar consuming <N> basic machine units, the
1481 * base alignment is <N>.
1483 * (2) If the member is a two- or four-component vector with components
1484 * consuming <N> basic machine units, the base alignment is 2<N> or
1485 * 4<N>, respectively.
1487 * (3) If the member is a three-component vector with components consuming
1488 * <N> basic machine units, the base alignment is 4<N>.
1490 if (this->is_array())
1491 return this->fields
.array
->std430_base_alignment(row_major
);
1493 /* (5) If the member is a column-major matrix with <C> columns and
1494 * <R> rows, the matrix is stored identically to an array of
1495 * <C> column vectors with <R> components each, according to
1498 * (7) If the member is a row-major matrix with <C> columns and <R>
1499 * rows, the matrix is stored identically to an array of <R>
1500 * row vectors with <C> components each, according to rule (4).
1502 if (this->is_matrix()) {
1503 const struct glsl_type
*vec_type
, *array_type
;
1504 int c
= this->matrix_columns
;
1505 int r
= this->vector_elements
;
1508 vec_type
= get_instance(base_type
, c
, 1);
1509 array_type
= glsl_type::get_array_instance(vec_type
, r
);
1511 vec_type
= get_instance(base_type
, r
, 1);
1512 array_type
= glsl_type::get_array_instance(vec_type
, c
);
1515 return array_type
->std430_base_alignment(false);
1518 /* (9) If the member is a structure, the base alignment of the
1519 * structure is <N>, where <N> is the largest base alignment
1520 * value of any of its members, and rounded up to the base
1521 * alignment of a vec4. The individual members of this
1522 * sub-structure are then assigned offsets by applying this set
1523 * of rules recursively, where the base offset of the first
1524 * member of the sub-structure is equal to the aligned offset
1525 * of the structure. The structure may have padding at the end;
1526 * the base offset of the member following the sub-structure is
1527 * rounded up to the next multiple of the base alignment of the
1530 if (this->is_record()) {
1531 unsigned base_alignment
= 0;
1532 for (unsigned i
= 0; i
< this->length
; i
++) {
1533 bool field_row_major
= row_major
;
1534 const enum glsl_matrix_layout matrix_layout
=
1535 glsl_matrix_layout(this->fields
.structure
[i
].matrix_layout
);
1536 if (matrix_layout
== GLSL_MATRIX_LAYOUT_ROW_MAJOR
) {
1537 field_row_major
= true;
1538 } else if (matrix_layout
== GLSL_MATRIX_LAYOUT_COLUMN_MAJOR
) {
1539 field_row_major
= false;
1542 const struct glsl_type
*field_type
= this->fields
.structure
[i
].type
;
1543 base_alignment
= MAX2(base_alignment
,
1544 field_type
->std430_base_alignment(field_row_major
));
1546 assert(base_alignment
> 0);
1547 return base_alignment
;
1549 assert(!"not reached");
1554 glsl_type::std430_array_stride(bool row_major
) const
1556 unsigned N
= is_double() ? 8 : 4;
1558 /* Notice that the array stride of a vec3 is not 3 * N but 4 * N.
1559 * See OpenGL 4.30 spec, section 7.6.2.2 "Standard Uniform Block Layout"
1561 * (3) If the member is a three-component vector with components consuming
1562 * <N> basic machine units, the base alignment is 4<N>.
1564 if (this->is_vector() && this->vector_elements
== 3)
1567 /* By default use std430_size(row_major) */
1568 return this->std430_size(row_major
);
1572 glsl_type::std430_size(bool row_major
) const
1574 unsigned N
= is_double() ? 8 : 4;
1576 /* OpenGL 4.30 spec, section 7.6.2.2 "Standard Uniform Block Layout":
1578 * "When using the std430 storage layout, shader storage blocks will be
1579 * laid out in buffer storage identically to uniform and shader storage
1580 * blocks using the std140 layout, except that the base alignment and
1581 * stride of arrays of scalars and vectors in rule 4 and of structures
1582 * in rule 9 are not rounded up a multiple of the base alignment of a vec4.
1584 if (this->is_scalar() || this->is_vector())
1585 return this->vector_elements
* N
;
1587 if (this->without_array()->is_matrix()) {
1588 const struct glsl_type
*element_type
;
1589 const struct glsl_type
*vec_type
;
1590 unsigned int array_len
;
1592 if (this->is_array()) {
1593 element_type
= this->without_array();
1594 array_len
= this->arrays_of_arrays_size();
1596 element_type
= this;
1601 vec_type
= get_instance(element_type
->base_type
,
1602 element_type
->matrix_columns
, 1);
1604 array_len
*= element_type
->vector_elements
;
1606 vec_type
= get_instance(element_type
->base_type
,
1607 element_type
->vector_elements
, 1);
1608 array_len
*= element_type
->matrix_columns
;
1610 const glsl_type
*array_type
= glsl_type::get_array_instance(vec_type
,
1613 return array_type
->std430_size(false);
1616 if (this->is_array()) {
1617 if (this->without_array()->is_record())
1618 return this->arrays_of_arrays_size() *
1619 this->without_array()->std430_size(row_major
);
1621 return this->arrays_of_arrays_size() *
1622 this->without_array()->std430_base_alignment(row_major
);
1625 if (this->is_record() || this->is_interface()) {
1627 unsigned max_align
= 0;
1629 for (unsigned i
= 0; i
< this->length
; i
++) {
1630 bool field_row_major
= row_major
;
1631 const enum glsl_matrix_layout matrix_layout
=
1632 glsl_matrix_layout(this->fields
.structure
[i
].matrix_layout
);
1633 if (matrix_layout
== GLSL_MATRIX_LAYOUT_ROW_MAJOR
) {
1634 field_row_major
= true;
1635 } else if (matrix_layout
== GLSL_MATRIX_LAYOUT_COLUMN_MAJOR
) {
1636 field_row_major
= false;
1639 const struct glsl_type
*field_type
= this->fields
.structure
[i
].type
;
1640 unsigned align
= field_type
->std430_base_alignment(field_row_major
);
1641 size
= glsl_align(size
, align
);
1642 size
+= field_type
->std430_size(field_row_major
);
1644 max_align
= MAX2(align
, max_align
);
1646 size
= glsl_align(size
, max_align
);
1650 assert(!"not reached");
1655 glsl_type::count_attribute_slots(bool vertex_input_slots
) const
1657 /* From page 31 (page 37 of the PDF) of the GLSL 1.50 spec:
1659 * "A scalar input counts the same amount against this limit as a vec4,
1660 * so applications may want to consider packing groups of four
1661 * unrelated float inputs together into a vector to better utilize the
1662 * capabilities of the underlying hardware. A matrix input will use up
1663 * multiple locations. The number of locations used will equal the
1664 * number of columns in the matrix."
1666 * The spec does not explicitly say how arrays are counted. However, it
1667 * should be safe to assume the total number of slots consumed by an array
1668 * is the number of entries in the array multiplied by the number of slots
1669 * consumed by a single element of the array.
1671 * The spec says nothing about how structs are counted, because vertex
1672 * attributes are not allowed to be (or contain) structs. However, Mesa
1673 * allows varying structs, the number of varying slots taken up by a
1674 * varying struct is simply equal to the sum of the number of slots taken
1675 * up by each element.
1677 * Doubles are counted different depending on whether they are vertex
1678 * inputs or everything else. Vertex inputs from ARB_vertex_attrib_64bit
1679 * take one location no matter what size they are, otherwise dvec3/4
1680 * take two locations.
1682 switch (this->base_type
) {
1683 case GLSL_TYPE_UINT
:
1685 case GLSL_TYPE_FLOAT
:
1686 case GLSL_TYPE_BOOL
:
1687 return this->matrix_columns
;
1688 case GLSL_TYPE_DOUBLE
:
1689 if (this->vector_elements
> 2 && !vertex_input_slots
)
1690 return this->matrix_columns
* 2;
1692 return this->matrix_columns
;
1693 case GLSL_TYPE_STRUCT
:
1694 case GLSL_TYPE_INTERFACE
: {
1697 for (unsigned i
= 0; i
< this->length
; i
++)
1698 size
+= this->fields
.structure
[i
].type
->count_attribute_slots(vertex_input_slots
);
1703 case GLSL_TYPE_ARRAY
:
1704 return this->length
* this->fields
.array
->count_attribute_slots(vertex_input_slots
);
1706 case GLSL_TYPE_SAMPLER
:
1707 case GLSL_TYPE_IMAGE
:
1708 case GLSL_TYPE_ATOMIC_UINT
:
1709 case GLSL_TYPE_VOID
:
1710 case GLSL_TYPE_SUBROUTINE
:
1711 case GLSL_TYPE_ERROR
:
1715 assert(!"Unexpected type in count_attribute_slots()");
1721 glsl_type::coordinate_components() const
1725 switch (sampler_dimensionality
) {
1726 case GLSL_SAMPLER_DIM_1D
:
1727 case GLSL_SAMPLER_DIM_BUF
:
1730 case GLSL_SAMPLER_DIM_2D
:
1731 case GLSL_SAMPLER_DIM_RECT
:
1732 case GLSL_SAMPLER_DIM_MS
:
1733 case GLSL_SAMPLER_DIM_EXTERNAL
:
1736 case GLSL_SAMPLER_DIM_3D
:
1737 case GLSL_SAMPLER_DIM_CUBE
:
1741 assert(!"Should not get here.");
1746 /* Array textures need an additional component for the array index, except
1747 * for cubemap array images that behave like a 2D array of interleaved
1750 if (sampler_array
&&
1751 !(base_type
== GLSL_TYPE_IMAGE
&&
1752 sampler_dimensionality
== GLSL_SAMPLER_DIM_CUBE
))
1759 * Declarations of type flyweights (glsl_type::_foo_type) and
1760 * convenience pointers (glsl_type::foo_type).
1763 #define DECL_TYPE(NAME, ...) \
1764 const glsl_type glsl_type::_##NAME##_type = glsl_type(__VA_ARGS__, #NAME); \
1765 const glsl_type *const glsl_type::NAME##_type = &glsl_type::_##NAME##_type;
1767 #define STRUCT_TYPE(NAME)
1769 #include "compiler/builtin_type_macros.h"