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
].precision
= fields
[i
].precision
;
169 mtx_unlock(&glsl_type::mutex
);
172 glsl_type::glsl_type(const char *subroutine_name
) :
174 base_type(GLSL_TYPE_SUBROUTINE
),
175 sampler_dimensionality(0), sampler_shadow(0), sampler_array(0),
176 sampler_type(0), interface_packing(0),
177 vector_elements(1), matrix_columns(1),
180 mtx_lock(&glsl_type::mutex
);
182 init_ralloc_type_ctx();
183 assert(subroutine_name
!= NULL
);
184 this->name
= ralloc_strdup(this->mem_ctx
, subroutine_name
);
185 mtx_unlock(&glsl_type::mutex
);
189 glsl_type::contains_sampler() const
191 if (this->is_array()) {
192 return this->fields
.array
->contains_sampler();
193 } else if (this->is_record()) {
194 for (unsigned int i
= 0; i
< this->length
; i
++) {
195 if (this->fields
.structure
[i
].type
->contains_sampler())
200 return this->is_sampler();
206 glsl_type::contains_integer() const
208 if (this->is_array()) {
209 return this->fields
.array
->contains_integer();
210 } else if (this->is_record()) {
211 for (unsigned int i
= 0; i
< this->length
; i
++) {
212 if (this->fields
.structure
[i
].type
->contains_integer())
217 return this->is_integer();
222 glsl_type::contains_double() const
224 if (this->is_array()) {
225 return this->fields
.array
->contains_double();
226 } else if (this->is_record()) {
227 for (unsigned int i
= 0; i
< this->length
; i
++) {
228 if (this->fields
.structure
[i
].type
->contains_double())
233 return this->is_double();
238 glsl_type::contains_opaque() const {
240 case GLSL_TYPE_SAMPLER
:
241 case GLSL_TYPE_IMAGE
:
242 case GLSL_TYPE_ATOMIC_UINT
:
244 case GLSL_TYPE_ARRAY
:
245 return fields
.array
->contains_opaque();
246 case GLSL_TYPE_STRUCT
:
247 for (unsigned int i
= 0; i
< length
; i
++) {
248 if (fields
.structure
[i
].type
->contains_opaque())
258 glsl_type::contains_subroutine() const
260 if (this->is_array()) {
261 return this->fields
.array
->contains_subroutine();
262 } else if (this->is_record()) {
263 for (unsigned int i
= 0; i
< this->length
; i
++) {
264 if (this->fields
.structure
[i
].type
->contains_subroutine())
269 return this->is_subroutine();
274 glsl_type::sampler_index() const
276 const glsl_type
*const t
= (this->is_array()) ? this->fields
.array
: this;
278 assert(t
->is_sampler());
280 switch (t
->sampler_dimensionality
) {
281 case GLSL_SAMPLER_DIM_1D
:
282 return (t
->sampler_array
) ? TEXTURE_1D_ARRAY_INDEX
: TEXTURE_1D_INDEX
;
283 case GLSL_SAMPLER_DIM_2D
:
284 return (t
->sampler_array
) ? TEXTURE_2D_ARRAY_INDEX
: TEXTURE_2D_INDEX
;
285 case GLSL_SAMPLER_DIM_3D
:
286 return TEXTURE_3D_INDEX
;
287 case GLSL_SAMPLER_DIM_CUBE
:
288 return (t
->sampler_array
) ? TEXTURE_CUBE_ARRAY_INDEX
: TEXTURE_CUBE_INDEX
;
289 case GLSL_SAMPLER_DIM_RECT
:
290 return TEXTURE_RECT_INDEX
;
291 case GLSL_SAMPLER_DIM_BUF
:
292 return TEXTURE_BUFFER_INDEX
;
293 case GLSL_SAMPLER_DIM_EXTERNAL
:
294 return TEXTURE_EXTERNAL_INDEX
;
295 case GLSL_SAMPLER_DIM_MS
:
296 return (t
->sampler_array
) ? TEXTURE_2D_MULTISAMPLE_ARRAY_INDEX
: TEXTURE_2D_MULTISAMPLE_INDEX
;
298 assert(!"Should not get here.");
299 return TEXTURE_BUFFER_INDEX
;
304 glsl_type::contains_image() const
306 if (this->is_array()) {
307 return this->fields
.array
->contains_image();
308 } else if (this->is_record()) {
309 for (unsigned int i
= 0; i
< this->length
; i
++) {
310 if (this->fields
.structure
[i
].type
->contains_image())
315 return this->is_image();
319 const glsl_type
*glsl_type::get_base_type() const
326 case GLSL_TYPE_FLOAT
:
328 case GLSL_TYPE_DOUBLE
:
338 const glsl_type
*glsl_type::get_scalar_type() const
340 const glsl_type
*type
= this;
343 while (type
->base_type
== GLSL_TYPE_ARRAY
)
344 type
= type
->fields
.array
;
346 /* Handle vectors and matrices */
347 switch (type
->base_type
) {
352 case GLSL_TYPE_FLOAT
:
354 case GLSL_TYPE_DOUBLE
:
359 /* Handle everything else */
366 _mesa_glsl_release_types(void)
368 /* Should only be called during atexit (either when unloading shared
369 * object, or if process terminates), so no mutex-locking should be
372 if (glsl_type::array_types
!= NULL
) {
373 _mesa_hash_table_destroy(glsl_type::array_types
, NULL
);
374 glsl_type::array_types
= NULL
;
377 if (glsl_type::record_types
!= NULL
) {
378 _mesa_hash_table_destroy(glsl_type::record_types
, NULL
);
379 glsl_type::record_types
= NULL
;
382 if (glsl_type::interface_types
!= NULL
) {
383 _mesa_hash_table_destroy(glsl_type::interface_types
, NULL
);
384 glsl_type::interface_types
= NULL
;
389 glsl_type::glsl_type(const glsl_type
*array
, unsigned length
) :
390 base_type(GLSL_TYPE_ARRAY
),
391 sampler_dimensionality(0), sampler_shadow(0), sampler_array(0),
392 sampler_type(0), interface_packing(0),
393 vector_elements(0), matrix_columns(0),
394 length(length
), name(NULL
)
396 this->fields
.array
= array
;
397 /* Inherit the gl type of the base. The GL type is used for
398 * uniform/statevar handling in Mesa and the arrayness of the type
399 * is represented by the size rather than the type.
401 this->gl_type
= array
->gl_type
;
403 /* Allow a maximum of 10 characters for the array size. This is enough
404 * for 32-bits of ~0. The extra 3 are for the '[', ']', and terminating
407 const unsigned name_length
= strlen(array
->name
) + 10 + 3;
409 mtx_lock(&glsl_type::mutex
);
410 char *const n
= (char *) ralloc_size(this->mem_ctx
, name_length
);
411 mtx_unlock(&glsl_type::mutex
);
414 snprintf(n
, name_length
, "%s[]", array
->name
);
416 /* insert outermost dimensions in the correct spot
417 * otherwise the dimension order will be backwards
419 const char *pos
= strchr(array
->name
, '[');
421 int idx
= pos
- array
->name
;
422 snprintf(n
, idx
+1, "%s", array
->name
);
423 snprintf(n
+ idx
, name_length
- idx
, "[%u]%s",
424 length
, array
->name
+ idx
);
426 snprintf(n
, name_length
, "%s[%u]", array
->name
, length
);
435 glsl_type::vec(unsigned components
)
437 if (components
== 0 || components
> 4)
440 static const glsl_type
*const ts
[] = {
441 float_type
, vec2_type
, vec3_type
, vec4_type
443 return ts
[components
- 1];
447 glsl_type::dvec(unsigned components
)
449 if (components
== 0 || components
> 4)
452 static const glsl_type
*const ts
[] = {
453 double_type
, dvec2_type
, dvec3_type
, dvec4_type
455 return ts
[components
- 1];
459 glsl_type::ivec(unsigned components
)
461 if (components
== 0 || components
> 4)
464 static const glsl_type
*const ts
[] = {
465 int_type
, ivec2_type
, ivec3_type
, ivec4_type
467 return ts
[components
- 1];
472 glsl_type::uvec(unsigned components
)
474 if (components
== 0 || components
> 4)
477 static const glsl_type
*const ts
[] = {
478 uint_type
, uvec2_type
, uvec3_type
, uvec4_type
480 return ts
[components
- 1];
485 glsl_type::bvec(unsigned components
)
487 if (components
== 0 || components
> 4)
490 static const glsl_type
*const ts
[] = {
491 bool_type
, bvec2_type
, bvec3_type
, bvec4_type
493 return ts
[components
- 1];
498 glsl_type::get_instance(unsigned base_type
, unsigned rows
, unsigned columns
)
500 if (base_type
== GLSL_TYPE_VOID
)
503 if ((rows
< 1) || (rows
> 4) || (columns
< 1) || (columns
> 4))
506 /* Treat GLSL vectors as Nx1 matrices.
514 case GLSL_TYPE_FLOAT
:
516 case GLSL_TYPE_DOUBLE
:
524 if ((base_type
!= GLSL_TYPE_FLOAT
&& base_type
!= GLSL_TYPE_DOUBLE
) || (rows
== 1))
527 /* GLSL matrix types are named mat{COLUMNS}x{ROWS}. Only the following
528 * combinations are valid:
536 #define IDX(c,r) (((c-1)*3) + (r-1))
538 if (base_type
== GLSL_TYPE_DOUBLE
) {
539 switch (IDX(columns
, rows
)) {
540 case IDX(2,2): return dmat2_type
;
541 case IDX(2,3): return dmat2x3_type
;
542 case IDX(2,4): return dmat2x4_type
;
543 case IDX(3,2): return dmat3x2_type
;
544 case IDX(3,3): return dmat3_type
;
545 case IDX(3,4): return dmat3x4_type
;
546 case IDX(4,2): return dmat4x2_type
;
547 case IDX(4,3): return dmat4x3_type
;
548 case IDX(4,4): return dmat4_type
;
549 default: return error_type
;
552 switch (IDX(columns
, rows
)) {
553 case IDX(2,2): return mat2_type
;
554 case IDX(2,3): return mat2x3_type
;
555 case IDX(2,4): return mat2x4_type
;
556 case IDX(3,2): return mat3x2_type
;
557 case IDX(3,3): return mat3_type
;
558 case IDX(3,4): return mat3x4_type
;
559 case IDX(4,2): return mat4x2_type
;
560 case IDX(4,3): return mat4x3_type
;
561 case IDX(4,4): return mat4_type
;
562 default: return error_type
;
567 assert(!"Should not get here.");
572 glsl_type::get_sampler_instance(enum glsl_sampler_dim dim
,
578 case GLSL_TYPE_FLOAT
:
580 case GLSL_SAMPLER_DIM_1D
:
582 return (array
? sampler1DArrayShadow_type
: sampler1DShadow_type
);
584 return (array
? sampler1DArray_type
: sampler1D_type
);
585 case GLSL_SAMPLER_DIM_2D
:
587 return (array
? sampler2DArrayShadow_type
: sampler2DShadow_type
);
589 return (array
? sampler2DArray_type
: sampler2D_type
);
590 case GLSL_SAMPLER_DIM_3D
:
594 return sampler3D_type
;
595 case GLSL_SAMPLER_DIM_CUBE
:
597 return (array
? samplerCubeArrayShadow_type
: samplerCubeShadow_type
);
599 return (array
? samplerCubeArray_type
: samplerCube_type
);
600 case GLSL_SAMPLER_DIM_RECT
:
604 return sampler2DRectShadow_type
;
606 return sampler2DRect_type
;
607 case GLSL_SAMPLER_DIM_BUF
:
611 return samplerBuffer_type
;
612 case GLSL_SAMPLER_DIM_MS
:
615 return (array
? sampler2DMSArray_type
: sampler2DMS_type
);
616 case GLSL_SAMPLER_DIM_EXTERNAL
:
620 return samplerExternalOES_type
;
626 case GLSL_SAMPLER_DIM_1D
:
627 return (array
? isampler1DArray_type
: isampler1D_type
);
628 case GLSL_SAMPLER_DIM_2D
:
629 return (array
? isampler2DArray_type
: isampler2D_type
);
630 case GLSL_SAMPLER_DIM_3D
:
633 return isampler3D_type
;
634 case GLSL_SAMPLER_DIM_CUBE
:
635 return (array
? isamplerCubeArray_type
: isamplerCube_type
);
636 case GLSL_SAMPLER_DIM_RECT
:
639 return isampler2DRect_type
;
640 case GLSL_SAMPLER_DIM_BUF
:
643 return isamplerBuffer_type
;
644 case GLSL_SAMPLER_DIM_MS
:
645 return (array
? isampler2DMSArray_type
: isampler2DMS_type
);
646 case GLSL_SAMPLER_DIM_EXTERNAL
:
653 case GLSL_SAMPLER_DIM_1D
:
654 return (array
? usampler1DArray_type
: usampler1D_type
);
655 case GLSL_SAMPLER_DIM_2D
:
656 return (array
? usampler2DArray_type
: usampler2D_type
);
657 case GLSL_SAMPLER_DIM_3D
:
660 return usampler3D_type
;
661 case GLSL_SAMPLER_DIM_CUBE
:
662 return (array
? usamplerCubeArray_type
: usamplerCube_type
);
663 case GLSL_SAMPLER_DIM_RECT
:
666 return usampler2DRect_type
;
667 case GLSL_SAMPLER_DIM_BUF
:
670 return usamplerBuffer_type
;
671 case GLSL_SAMPLER_DIM_MS
:
672 return (array
? usampler2DMSArray_type
: usampler2DMS_type
);
673 case GLSL_SAMPLER_DIM_EXTERNAL
:
680 unreachable("switch statement above should be complete");
684 glsl_type::get_array_instance(const glsl_type
*base
, unsigned array_size
)
686 /* Generate a name using the base type pointer in the key. This is
687 * done because the name of the base type may not be unique across
688 * shaders. For example, two shaders may have different record types
692 snprintf(key
, sizeof(key
), "%p[%u]", (void *) base
, array_size
);
694 mtx_lock(&glsl_type::mutex
);
696 if (array_types
== NULL
) {
697 array_types
= _mesa_hash_table_create(NULL
, _mesa_key_hash_string
,
698 _mesa_key_string_equal
);
701 const struct hash_entry
*entry
= _mesa_hash_table_search(array_types
, key
);
703 mtx_unlock(&glsl_type::mutex
);
704 const glsl_type
*t
= new glsl_type(base
, array_size
);
705 mtx_lock(&glsl_type::mutex
);
707 entry
= _mesa_hash_table_insert(array_types
,
708 ralloc_strdup(mem_ctx
, key
),
712 assert(((glsl_type
*) entry
->data
)->base_type
== GLSL_TYPE_ARRAY
);
713 assert(((glsl_type
*) entry
->data
)->length
== array_size
);
714 assert(((glsl_type
*) entry
->data
)->fields
.array
== base
);
716 mtx_unlock(&glsl_type::mutex
);
718 return (glsl_type
*) entry
->data
;
723 glsl_type::record_compare(const glsl_type
*b
) const
725 if (this->length
!= b
->length
)
728 if (this->interface_packing
!= b
->interface_packing
)
731 /* From the GLSL 4.20 specification (Sec 4.2):
733 * "Structures must have the same name, sequence of type names, and
734 * type definitions, and field names to be considered the same type."
736 * GLSL ES behaves the same (Ver 1.00 Sec 4.2.4, Ver 3.00 Sec 4.2.5).
738 * Note that we cannot force type name check when comparing unnamed
739 * structure types, these have a unique name assigned during parsing.
741 if (!this->is_anonymous() && !b
->is_anonymous())
742 if (strcmp(this->name
, b
->name
) != 0)
745 for (unsigned i
= 0; i
< this->length
; i
++) {
746 if (this->fields
.structure
[i
].type
!= b
->fields
.structure
[i
].type
)
748 if (strcmp(this->fields
.structure
[i
].name
,
749 b
->fields
.structure
[i
].name
) != 0)
751 if (this->fields
.structure
[i
].matrix_layout
752 != b
->fields
.structure
[i
].matrix_layout
)
754 if (this->fields
.structure
[i
].location
755 != b
->fields
.structure
[i
].location
)
757 if (this->fields
.structure
[i
].interpolation
758 != b
->fields
.structure
[i
].interpolation
)
760 if (this->fields
.structure
[i
].centroid
761 != b
->fields
.structure
[i
].centroid
)
763 if (this->fields
.structure
[i
].sample
764 != b
->fields
.structure
[i
].sample
)
766 if (this->fields
.structure
[i
].patch
767 != b
->fields
.structure
[i
].patch
)
769 if (this->fields
.structure
[i
].image_read_only
770 != b
->fields
.structure
[i
].image_read_only
)
772 if (this->fields
.structure
[i
].image_write_only
773 != b
->fields
.structure
[i
].image_write_only
)
775 if (this->fields
.structure
[i
].image_coherent
776 != b
->fields
.structure
[i
].image_coherent
)
778 if (this->fields
.structure
[i
].image_volatile
779 != b
->fields
.structure
[i
].image_volatile
)
781 if (this->fields
.structure
[i
].image_restrict
782 != b
->fields
.structure
[i
].image_restrict
)
784 if (this->fields
.structure
[i
].precision
785 != b
->fields
.structure
[i
].precision
)
794 glsl_type::record_key_compare(const void *a
, const void *b
)
796 const glsl_type
*const key1
= (glsl_type
*) a
;
797 const glsl_type
*const key2
= (glsl_type
*) b
;
799 return strcmp(key1
->name
, key2
->name
) == 0 && key1
->record_compare(key2
);
804 * Generate an integer hash value for a glsl_type structure type.
807 glsl_type::record_key_hash(const void *a
)
809 const glsl_type
*const key
= (glsl_type
*) a
;
810 uintptr_t hash
= key
->length
;
813 for (unsigned i
= 0; i
< key
->length
; i
++) {
814 /* casting pointer to uintptr_t */
815 hash
= (hash
* 13 ) + (uintptr_t) key
->fields
.structure
[i
].type
;
818 if (sizeof(hash
) == 8)
819 retval
= (hash
& 0xffffffff) ^ ((uint64_t) hash
>> 32);
828 glsl_type::get_record_instance(const glsl_struct_field
*fields
,
832 const glsl_type
key(fields
, num_fields
, name
);
834 mtx_lock(&glsl_type::mutex
);
836 if (record_types
== NULL
) {
837 record_types
= _mesa_hash_table_create(NULL
, record_key_hash
,
841 const struct hash_entry
*entry
= _mesa_hash_table_search(record_types
,
844 mtx_unlock(&glsl_type::mutex
);
845 const glsl_type
*t
= new glsl_type(fields
, num_fields
, name
);
846 mtx_lock(&glsl_type::mutex
);
848 entry
= _mesa_hash_table_insert(record_types
, t
, (void *) t
);
851 assert(((glsl_type
*) entry
->data
)->base_type
== GLSL_TYPE_STRUCT
);
852 assert(((glsl_type
*) entry
->data
)->length
== num_fields
);
853 assert(strcmp(((glsl_type
*) entry
->data
)->name
, name
) == 0);
855 mtx_unlock(&glsl_type::mutex
);
857 return (glsl_type
*) entry
->data
;
862 glsl_type::get_interface_instance(const glsl_struct_field
*fields
,
864 enum glsl_interface_packing packing
,
865 const char *block_name
)
867 const glsl_type
key(fields
, num_fields
, packing
, block_name
);
869 mtx_lock(&glsl_type::mutex
);
871 if (interface_types
== NULL
) {
872 interface_types
= _mesa_hash_table_create(NULL
, record_key_hash
,
876 const struct hash_entry
*entry
= _mesa_hash_table_search(interface_types
,
879 mtx_unlock(&glsl_type::mutex
);
880 const glsl_type
*t
= new glsl_type(fields
, num_fields
,
881 packing
, block_name
);
882 mtx_lock(&glsl_type::mutex
);
884 entry
= _mesa_hash_table_insert(interface_types
, t
, (void *) t
);
887 assert(((glsl_type
*) entry
->data
)->base_type
== GLSL_TYPE_INTERFACE
);
888 assert(((glsl_type
*) entry
->data
)->length
== num_fields
);
889 assert(strcmp(((glsl_type
*) entry
->data
)->name
, block_name
) == 0);
891 mtx_unlock(&glsl_type::mutex
);
893 return (glsl_type
*) entry
->data
;
897 glsl_type::get_subroutine_instance(const char *subroutine_name
)
899 const glsl_type
key(subroutine_name
);
901 mtx_lock(&glsl_type::mutex
);
903 if (subroutine_types
== NULL
) {
904 subroutine_types
= _mesa_hash_table_create(NULL
, record_key_hash
,
908 const struct hash_entry
*entry
= _mesa_hash_table_search(subroutine_types
,
911 mtx_unlock(&glsl_type::mutex
);
912 const glsl_type
*t
= new glsl_type(subroutine_name
);
913 mtx_lock(&glsl_type::mutex
);
915 entry
= _mesa_hash_table_insert(subroutine_types
, t
, (void *) t
);
918 assert(((glsl_type
*) entry
->data
)->base_type
== GLSL_TYPE_SUBROUTINE
);
919 assert(strcmp(((glsl_type
*) entry
->data
)->name
, subroutine_name
) == 0);
921 mtx_unlock(&glsl_type::mutex
);
923 return (glsl_type
*) entry
->data
;
928 glsl_type::get_mul_type(const glsl_type
*type_a
, const glsl_type
*type_b
)
930 if (type_a
== type_b
) {
932 } else if (type_a
->is_matrix() && type_b
->is_matrix()) {
933 /* Matrix multiply. The columns of A must match the rows of B. Given
934 * the other previously tested constraints, this means the vector type
935 * of a row from A must be the same as the vector type of a column from
938 if (type_a
->row_type() == type_b
->column_type()) {
939 /* The resulting matrix has the number of columns of matrix B and
940 * the number of rows of matrix A. We get the row count of A by
941 * looking at the size of a vector that makes up a column. The
942 * transpose (size of a row) is done for B.
944 const glsl_type
*const type
=
945 get_instance(type_a
->base_type
,
946 type_a
->column_type()->vector_elements
,
947 type_b
->row_type()->vector_elements
);
948 assert(type
!= error_type
);
952 } else if (type_a
->is_matrix()) {
953 /* A is a matrix and B is a column vector. Columns of A must match
954 * rows of B. Given the other previously tested constraints, this
955 * means the vector type of a row from A must be the same as the
956 * vector the type of B.
958 if (type_a
->row_type() == type_b
) {
959 /* The resulting vector has a number of elements equal to
960 * the number of rows of matrix A. */
961 const glsl_type
*const type
=
962 get_instance(type_a
->base_type
,
963 type_a
->column_type()->vector_elements
,
965 assert(type
!= error_type
);
970 assert(type_b
->is_matrix());
972 /* A is a row vector and B is a matrix. Columns of A must match rows
973 * of B. Given the other previously tested constraints, this means
974 * the type of A must be the same as the vector type of a column from
977 if (type_a
== type_b
->column_type()) {
978 /* The resulting vector has a number of elements equal to
979 * the number of columns of matrix B. */
980 const glsl_type
*const type
=
981 get_instance(type_a
->base_type
,
982 type_b
->row_type()->vector_elements
,
984 assert(type
!= error_type
);
995 glsl_type::field_type(const char *name
) const
997 if (this->base_type
!= GLSL_TYPE_STRUCT
998 && this->base_type
!= GLSL_TYPE_INTERFACE
)
1001 for (unsigned i
= 0; i
< this->length
; i
++) {
1002 if (strcmp(name
, this->fields
.structure
[i
].name
) == 0)
1003 return this->fields
.structure
[i
].type
;
1011 glsl_type::field_index(const char *name
) const
1013 if (this->base_type
!= GLSL_TYPE_STRUCT
1014 && this->base_type
!= GLSL_TYPE_INTERFACE
)
1017 for (unsigned i
= 0; i
< this->length
; i
++) {
1018 if (strcmp(name
, this->fields
.structure
[i
].name
) == 0)
1027 glsl_type::component_slots() const
1029 switch (this->base_type
) {
1030 case GLSL_TYPE_UINT
:
1032 case GLSL_TYPE_FLOAT
:
1033 case GLSL_TYPE_BOOL
:
1034 return this->components();
1036 case GLSL_TYPE_DOUBLE
:
1037 return 2 * this->components();
1039 case GLSL_TYPE_STRUCT
:
1040 case GLSL_TYPE_INTERFACE
: {
1043 for (unsigned i
= 0; i
< this->length
; i
++)
1044 size
+= this->fields
.structure
[i
].type
->component_slots();
1049 case GLSL_TYPE_ARRAY
:
1050 return this->length
* this->fields
.array
->component_slots();
1052 case GLSL_TYPE_IMAGE
:
1054 case GLSL_TYPE_SUBROUTINE
:
1056 case GLSL_TYPE_SAMPLER
:
1057 case GLSL_TYPE_ATOMIC_UINT
:
1058 case GLSL_TYPE_VOID
:
1059 case GLSL_TYPE_ERROR
:
1067 glsl_type::record_location_offset(unsigned length
) const
1069 unsigned offset
= 0;
1070 const glsl_type
*t
= this->without_array();
1071 if (t
->is_record()) {
1072 assert(length
<= t
->length
);
1074 for (unsigned i
= 0; i
< length
; i
++) {
1075 const glsl_type
*st
= t
->fields
.structure
[i
].type
;
1076 const glsl_type
*wa
= st
->without_array();
1077 if (wa
->is_record()) {
1078 unsigned r_offset
= wa
->record_location_offset(wa
->length
);
1079 offset
+= st
->is_array() ?
1080 st
->arrays_of_arrays_size() * r_offset
: r_offset
;
1081 } else if (st
->is_array() && st
->fields
.array
->is_array()) {
1082 unsigned outer_array_size
= st
->length
;
1083 const glsl_type
*base_type
= st
->fields
.array
;
1085 /* For arrays of arrays the outer arrays take up a uniform
1086 * slot for each element. The innermost array elements share a
1087 * single slot so we ignore the innermost array when calculating
1090 while (base_type
->fields
.array
->is_array()) {
1091 outer_array_size
= outer_array_size
* base_type
->length
;
1092 base_type
= base_type
->fields
.array
;
1094 offset
+= outer_array_size
;
1096 /* We dont worry about arrays here because unless the array
1097 * contains a structure or another array it only takes up a single
1108 glsl_type::uniform_locations() const
1112 switch (this->base_type
) {
1113 case GLSL_TYPE_UINT
:
1115 case GLSL_TYPE_FLOAT
:
1116 case GLSL_TYPE_DOUBLE
:
1117 case GLSL_TYPE_BOOL
:
1118 case GLSL_TYPE_SAMPLER
:
1119 case GLSL_TYPE_IMAGE
:
1120 case GLSL_TYPE_SUBROUTINE
:
1123 case GLSL_TYPE_STRUCT
:
1124 case GLSL_TYPE_INTERFACE
:
1125 for (unsigned i
= 0; i
< this->length
; i
++)
1126 size
+= this->fields
.structure
[i
].type
->uniform_locations();
1128 case GLSL_TYPE_ARRAY
:
1129 return this->length
* this->fields
.array
->uniform_locations();
1136 glsl_type::can_implicitly_convert_to(const glsl_type
*desired
,
1137 _mesa_glsl_parse_state
*state
) const
1139 if (this == desired
)
1142 /* There is no conversion among matrix types. */
1143 if (this->matrix_columns
> 1 || desired
->matrix_columns
> 1)
1146 /* Vector size must match. */
1147 if (this->vector_elements
!= desired
->vector_elements
)
1150 /* int and uint can be converted to float. */
1151 if (desired
->is_float() && this->is_integer())
1154 /* With GLSL 4.0 / ARB_gpu_shader5, int can be converted to uint.
1155 * Note that state may be NULL here, when resolving function calls in the
1156 * linker. By this time, all the state-dependent checks have already
1157 * happened though, so allow anything that's allowed in any shader version. */
1158 if ((!state
|| state
->is_version(400, 0) || state
->ARB_gpu_shader5_enable
) &&
1159 desired
->base_type
== GLSL_TYPE_UINT
&& this->base_type
== GLSL_TYPE_INT
)
1162 /* No implicit conversions from double. */
1163 if ((!state
|| state
->has_double()) && this->is_double())
1166 /* Conversions from different types to double. */
1167 if ((!state
|| state
->has_double()) && desired
->is_double()) {
1168 if (this->is_float())
1170 if (this->is_integer())
1178 glsl_type::std140_base_alignment(bool row_major
) const
1180 unsigned N
= is_double() ? 8 : 4;
1182 /* (1) If the member is a scalar consuming <N> basic machine units, the
1183 * base alignment is <N>.
1185 * (2) If the member is a two- or four-component vector with components
1186 * consuming <N> basic machine units, the base alignment is 2<N> or
1187 * 4<N>, respectively.
1189 * (3) If the member is a three-component vector with components consuming
1190 * <N> basic machine units, the base alignment is 4<N>.
1192 if (this->is_scalar() || this->is_vector()) {
1193 switch (this->vector_elements
) {
1204 /* (4) If the member is an array of scalars or vectors, the base alignment
1205 * and array stride are set to match the base alignment of a single
1206 * array element, according to rules (1), (2), and (3), and rounded up
1207 * to the base alignment of a vec4. The array may have padding at the
1208 * end; the base offset of the member following the array is rounded up
1209 * to the next multiple of the base alignment.
1211 * (6) If the member is an array of <S> column-major matrices with <C>
1212 * columns and <R> rows, the matrix is stored identically to a row of
1213 * <S>*<C> column vectors with <R> components each, according to rule
1216 * (8) If the member is an array of <S> row-major matrices with <C> columns
1217 * and <R> rows, the matrix is stored identically to a row of <S>*<R>
1218 * row vectors with <C> components each, according to rule (4).
1220 * (10) If the member is an array of <S> structures, the <S> elements of
1221 * the array are laid out in order, according to rule (9).
1223 if (this->is_array()) {
1224 if (this->fields
.array
->is_scalar() ||
1225 this->fields
.array
->is_vector() ||
1226 this->fields
.array
->is_matrix()) {
1227 return MAX2(this->fields
.array
->std140_base_alignment(row_major
), 16);
1229 assert(this->fields
.array
->is_record() ||
1230 this->fields
.array
->is_array());
1231 return this->fields
.array
->std140_base_alignment(row_major
);
1235 /* (5) If the member is a column-major matrix with <C> columns and
1236 * <R> rows, the matrix is stored identically to an array of
1237 * <C> column vectors with <R> components each, according to
1240 * (7) If the member is a row-major matrix with <C> columns and <R>
1241 * rows, the matrix is stored identically to an array of <R>
1242 * row vectors with <C> components each, according to rule (4).
1244 if (this->is_matrix()) {
1245 const struct glsl_type
*vec_type
, *array_type
;
1246 int c
= this->matrix_columns
;
1247 int r
= this->vector_elements
;
1250 vec_type
= get_instance(base_type
, c
, 1);
1251 array_type
= glsl_type::get_array_instance(vec_type
, r
);
1253 vec_type
= get_instance(base_type
, r
, 1);
1254 array_type
= glsl_type::get_array_instance(vec_type
, c
);
1257 return array_type
->std140_base_alignment(false);
1260 /* (9) If the member is a structure, the base alignment of the
1261 * structure is <N>, where <N> is the largest base alignment
1262 * value of any of its members, and rounded up to the base
1263 * alignment of a vec4. The individual members of this
1264 * sub-structure are then assigned offsets by applying this set
1265 * of rules recursively, where the base offset of the first
1266 * member of the sub-structure is equal to the aligned offset
1267 * of the structure. The structure may have padding at the end;
1268 * the base offset of the member following the sub-structure is
1269 * rounded up to the next multiple of the base alignment of the
1272 if (this->is_record()) {
1273 unsigned base_alignment
= 16;
1274 for (unsigned i
= 0; i
< this->length
; i
++) {
1275 bool field_row_major
= row_major
;
1276 const enum glsl_matrix_layout matrix_layout
=
1277 glsl_matrix_layout(this->fields
.structure
[i
].matrix_layout
);
1278 if (matrix_layout
== GLSL_MATRIX_LAYOUT_ROW_MAJOR
) {
1279 field_row_major
= true;
1280 } else if (matrix_layout
== GLSL_MATRIX_LAYOUT_COLUMN_MAJOR
) {
1281 field_row_major
= false;
1284 const struct glsl_type
*field_type
= this->fields
.structure
[i
].type
;
1285 base_alignment
= MAX2(base_alignment
,
1286 field_type
->std140_base_alignment(field_row_major
));
1288 return base_alignment
;
1291 assert(!"not reached");
1296 glsl_type::std140_size(bool row_major
) const
1298 unsigned N
= is_double() ? 8 : 4;
1300 /* (1) If the member is a scalar consuming <N> basic machine units, the
1301 * base alignment is <N>.
1303 * (2) If the member is a two- or four-component vector with components
1304 * consuming <N> basic machine units, the base alignment is 2<N> or
1305 * 4<N>, respectively.
1307 * (3) If the member is a three-component vector with components consuming
1308 * <N> basic machine units, the base alignment is 4<N>.
1310 if (this->is_scalar() || this->is_vector()) {
1311 return this->vector_elements
* N
;
1314 /* (5) If the member is a column-major matrix with <C> columns and
1315 * <R> rows, the matrix is stored identically to an array of
1316 * <C> column vectors with <R> components each, according to
1319 * (6) If the member is an array of <S> column-major matrices with <C>
1320 * columns and <R> rows, the matrix is stored identically to a row of
1321 * <S>*<C> column vectors with <R> components each, according to rule
1324 * (7) If the member is a row-major matrix with <C> columns and <R>
1325 * rows, the matrix is stored identically to an array of <R>
1326 * row vectors with <C> components each, according to rule (4).
1328 * (8) If the member is an array of <S> row-major matrices with <C> columns
1329 * and <R> rows, the matrix is stored identically to a row of <S>*<R>
1330 * row vectors with <C> components each, according to rule (4).
1332 if (this->without_array()->is_matrix()) {
1333 const struct glsl_type
*element_type
;
1334 const struct glsl_type
*vec_type
;
1335 unsigned int array_len
;
1337 if (this->is_array()) {
1338 element_type
= this->without_array();
1339 array_len
= this->arrays_of_arrays_size();
1341 element_type
= this;
1346 vec_type
= get_instance(element_type
->base_type
,
1347 element_type
->matrix_columns
, 1);
1349 array_len
*= element_type
->vector_elements
;
1351 vec_type
= get_instance(element_type
->base_type
,
1352 element_type
->vector_elements
, 1);
1353 array_len
*= element_type
->matrix_columns
;
1355 const glsl_type
*array_type
= glsl_type::get_array_instance(vec_type
,
1358 return array_type
->std140_size(false);
1361 /* (4) If the member is an array of scalars or vectors, the base alignment
1362 * and array stride are set to match the base alignment of a single
1363 * array element, according to rules (1), (2), and (3), and rounded up
1364 * to the base alignment of a vec4. The array may have padding at the
1365 * end; the base offset of the member following the array is rounded up
1366 * to the next multiple of the base alignment.
1368 * (10) If the member is an array of <S> structures, the <S> elements of
1369 * the array are laid out in order, according to rule (9).
1371 if (this->is_array()) {
1372 if (this->without_array()->is_record()) {
1373 return this->arrays_of_arrays_size() *
1374 this->without_array()->std140_size(row_major
);
1376 unsigned element_base_align
=
1377 this->without_array()->std140_base_alignment(row_major
);
1378 return this->arrays_of_arrays_size() * MAX2(element_base_align
, 16);
1382 /* (9) If the member is a structure, the base alignment of the
1383 * structure is <N>, where <N> is the largest base alignment
1384 * value of any of its members, and rounded up to the base
1385 * alignment of a vec4. The individual members of this
1386 * sub-structure are then assigned offsets by applying this set
1387 * of rules recursively, where the base offset of the first
1388 * member of the sub-structure is equal to the aligned offset
1389 * of the structure. The structure may have padding at the end;
1390 * the base offset of the member following the sub-structure is
1391 * rounded up to the next multiple of the base alignment of the
1394 if (this->is_record() || this->is_interface()) {
1396 unsigned max_align
= 0;
1398 for (unsigned i
= 0; i
< this->length
; i
++) {
1399 bool field_row_major
= row_major
;
1400 const enum glsl_matrix_layout matrix_layout
=
1401 glsl_matrix_layout(this->fields
.structure
[i
].matrix_layout
);
1402 if (matrix_layout
== GLSL_MATRIX_LAYOUT_ROW_MAJOR
) {
1403 field_row_major
= true;
1404 } else if (matrix_layout
== GLSL_MATRIX_LAYOUT_COLUMN_MAJOR
) {
1405 field_row_major
= false;
1408 const struct glsl_type
*field_type
= this->fields
.structure
[i
].type
;
1409 unsigned align
= field_type
->std140_base_alignment(field_row_major
);
1411 /* Ignore unsized arrays when calculating size */
1412 if (field_type
->is_unsized_array())
1415 size
= glsl_align(size
, align
);
1416 size
+= field_type
->std140_size(field_row_major
);
1418 max_align
= MAX2(align
, max_align
);
1420 if (field_type
->is_record() && (i
+ 1 < this->length
))
1421 size
= glsl_align(size
, 16);
1423 size
= glsl_align(size
, MAX2(max_align
, 16));
1427 assert(!"not reached");
1432 glsl_type::std430_base_alignment(bool row_major
) const
1435 unsigned N
= is_double() ? 8 : 4;
1437 /* (1) If the member is a scalar consuming <N> basic machine units, the
1438 * base alignment is <N>.
1440 * (2) If the member is a two- or four-component vector with components
1441 * consuming <N> basic machine units, the base alignment is 2<N> or
1442 * 4<N>, respectively.
1444 * (3) If the member is a three-component vector with components consuming
1445 * <N> basic machine units, the base alignment is 4<N>.
1447 if (this->is_scalar() || this->is_vector()) {
1448 switch (this->vector_elements
) {
1459 /* OpenGL 4.30 spec, section 7.6.2.2 "Standard Uniform Block Layout":
1461 * "When using the std430 storage layout, shader storage blocks will be
1462 * laid out in buffer storage identically to uniform and shader storage
1463 * blocks using the std140 layout, except that the base alignment and
1464 * stride of arrays of scalars and vectors in rule 4 and of structures
1465 * in rule 9 are not rounded up a multiple of the base alignment of a vec4.
1468 /* (1) If the member is a scalar consuming <N> basic machine units, the
1469 * base alignment is <N>.
1471 * (2) If the member is a two- or four-component vector with components
1472 * consuming <N> basic machine units, the base alignment is 2<N> or
1473 * 4<N>, respectively.
1475 * (3) If the member is a three-component vector with components consuming
1476 * <N> basic machine units, the base alignment is 4<N>.
1478 if (this->is_array())
1479 return this->fields
.array
->std430_base_alignment(row_major
);
1481 /* (5) If the member is a column-major matrix with <C> columns and
1482 * <R> rows, the matrix is stored identically to an array of
1483 * <C> column vectors with <R> components each, according to
1486 * (7) If the member is a row-major matrix with <C> columns and <R>
1487 * rows, the matrix is stored identically to an array of <R>
1488 * row vectors with <C> components each, according to rule (4).
1490 if (this->is_matrix()) {
1491 const struct glsl_type
*vec_type
, *array_type
;
1492 int c
= this->matrix_columns
;
1493 int r
= this->vector_elements
;
1496 vec_type
= get_instance(base_type
, c
, 1);
1497 array_type
= glsl_type::get_array_instance(vec_type
, r
);
1499 vec_type
= get_instance(base_type
, r
, 1);
1500 array_type
= glsl_type::get_array_instance(vec_type
, c
);
1503 return array_type
->std430_base_alignment(false);
1506 /* (9) If the member is a structure, the base alignment of the
1507 * structure is <N>, where <N> is the largest base alignment
1508 * value of any of its members, and rounded up to the base
1509 * alignment of a vec4. The individual members of this
1510 * sub-structure are then assigned offsets by applying this set
1511 * of rules recursively, where the base offset of the first
1512 * member of the sub-structure is equal to the aligned offset
1513 * of the structure. The structure may have padding at the end;
1514 * the base offset of the member following the sub-structure is
1515 * rounded up to the next multiple of the base alignment of the
1518 if (this->is_record()) {
1519 unsigned base_alignment
= 0;
1520 for (unsigned i
= 0; i
< this->length
; i
++) {
1521 bool field_row_major
= row_major
;
1522 const enum glsl_matrix_layout matrix_layout
=
1523 glsl_matrix_layout(this->fields
.structure
[i
].matrix_layout
);
1524 if (matrix_layout
== GLSL_MATRIX_LAYOUT_ROW_MAJOR
) {
1525 field_row_major
= true;
1526 } else if (matrix_layout
== GLSL_MATRIX_LAYOUT_COLUMN_MAJOR
) {
1527 field_row_major
= false;
1530 const struct glsl_type
*field_type
= this->fields
.structure
[i
].type
;
1531 base_alignment
= MAX2(base_alignment
,
1532 field_type
->std430_base_alignment(field_row_major
));
1534 assert(base_alignment
> 0);
1535 return base_alignment
;
1537 assert(!"not reached");
1542 glsl_type::std430_array_stride(bool row_major
) const
1544 unsigned N
= is_double() ? 8 : 4;
1546 /* Notice that the array stride of a vec3 is not 3 * N but 4 * N.
1547 * See OpenGL 4.30 spec, section 7.6.2.2 "Standard Uniform Block Layout"
1549 * (3) If the member is a three-component vector with components consuming
1550 * <N> basic machine units, the base alignment is 4<N>.
1552 if (this->is_vector() && this->vector_elements
== 3)
1555 /* By default use std430_size(row_major) */
1556 return this->std430_size(row_major
);
1560 glsl_type::std430_size(bool row_major
) const
1562 unsigned N
= is_double() ? 8 : 4;
1564 /* OpenGL 4.30 spec, section 7.6.2.2 "Standard Uniform Block Layout":
1566 * "When using the std430 storage layout, shader storage blocks will be
1567 * laid out in buffer storage identically to uniform and shader storage
1568 * blocks using the std140 layout, except that the base alignment and
1569 * stride of arrays of scalars and vectors in rule 4 and of structures
1570 * in rule 9 are not rounded up a multiple of the base alignment of a vec4.
1572 if (this->is_scalar() || this->is_vector())
1573 return this->vector_elements
* N
;
1575 if (this->without_array()->is_matrix()) {
1576 const struct glsl_type
*element_type
;
1577 const struct glsl_type
*vec_type
;
1578 unsigned int array_len
;
1580 if (this->is_array()) {
1581 element_type
= this->without_array();
1582 array_len
= this->arrays_of_arrays_size();
1584 element_type
= this;
1589 vec_type
= get_instance(element_type
->base_type
,
1590 element_type
->matrix_columns
, 1);
1592 array_len
*= element_type
->vector_elements
;
1594 vec_type
= get_instance(element_type
->base_type
,
1595 element_type
->vector_elements
, 1);
1596 array_len
*= element_type
->matrix_columns
;
1598 const glsl_type
*array_type
= glsl_type::get_array_instance(vec_type
,
1601 return array_type
->std430_size(false);
1604 if (this->is_array()) {
1605 if (this->without_array()->is_record())
1606 return this->arrays_of_arrays_size() *
1607 this->without_array()->std430_size(row_major
);
1609 return this->arrays_of_arrays_size() *
1610 this->without_array()->std430_base_alignment(row_major
);
1613 if (this->is_record() || this->is_interface()) {
1615 unsigned max_align
= 0;
1617 for (unsigned i
= 0; i
< this->length
; i
++) {
1618 bool field_row_major
= row_major
;
1619 const enum glsl_matrix_layout matrix_layout
=
1620 glsl_matrix_layout(this->fields
.structure
[i
].matrix_layout
);
1621 if (matrix_layout
== GLSL_MATRIX_LAYOUT_ROW_MAJOR
) {
1622 field_row_major
= true;
1623 } else if (matrix_layout
== GLSL_MATRIX_LAYOUT_COLUMN_MAJOR
) {
1624 field_row_major
= false;
1627 const struct glsl_type
*field_type
= this->fields
.structure
[i
].type
;
1628 unsigned align
= field_type
->std430_base_alignment(field_row_major
);
1629 size
= glsl_align(size
, align
);
1630 size
+= field_type
->std430_size(field_row_major
);
1632 max_align
= MAX2(align
, max_align
);
1634 size
= glsl_align(size
, max_align
);
1638 assert(!"not reached");
1643 glsl_type::count_attribute_slots(bool vertex_input_slots
) const
1645 /* From page 31 (page 37 of the PDF) of the GLSL 1.50 spec:
1647 * "A scalar input counts the same amount against this limit as a vec4,
1648 * so applications may want to consider packing groups of four
1649 * unrelated float inputs together into a vector to better utilize the
1650 * capabilities of the underlying hardware. A matrix input will use up
1651 * multiple locations. The number of locations used will equal the
1652 * number of columns in the matrix."
1654 * The spec does not explicitly say how arrays are counted. However, it
1655 * should be safe to assume the total number of slots consumed by an array
1656 * is the number of entries in the array multiplied by the number of slots
1657 * consumed by a single element of the array.
1659 * The spec says nothing about how structs are counted, because vertex
1660 * attributes are not allowed to be (or contain) structs. However, Mesa
1661 * allows varying structs, the number of varying slots taken up by a
1662 * varying struct is simply equal to the sum of the number of slots taken
1663 * up by each element.
1665 * Doubles are counted different depending on whether they are vertex
1666 * inputs or everything else. Vertex inputs from ARB_vertex_attrib_64bit
1667 * take one location no matter what size they are, otherwise dvec3/4
1668 * take two locations.
1670 switch (this->base_type
) {
1671 case GLSL_TYPE_UINT
:
1673 case GLSL_TYPE_FLOAT
:
1674 case GLSL_TYPE_BOOL
:
1675 return this->matrix_columns
;
1676 case GLSL_TYPE_DOUBLE
:
1677 if (this->vector_elements
> 2 && !vertex_input_slots
)
1678 return this->matrix_columns
* 2;
1680 return this->matrix_columns
;
1681 case GLSL_TYPE_STRUCT
:
1682 case GLSL_TYPE_INTERFACE
: {
1685 for (unsigned i
= 0; i
< this->length
; i
++)
1686 size
+= this->fields
.structure
[i
].type
->count_attribute_slots(vertex_input_slots
);
1691 case GLSL_TYPE_ARRAY
:
1692 return this->length
* this->fields
.array
->count_attribute_slots(vertex_input_slots
);
1694 case GLSL_TYPE_SAMPLER
:
1695 case GLSL_TYPE_IMAGE
:
1696 case GLSL_TYPE_ATOMIC_UINT
:
1697 case GLSL_TYPE_VOID
:
1698 case GLSL_TYPE_SUBROUTINE
:
1699 case GLSL_TYPE_ERROR
:
1703 assert(!"Unexpected type in count_attribute_slots()");
1709 glsl_type::coordinate_components() const
1713 switch (sampler_dimensionality
) {
1714 case GLSL_SAMPLER_DIM_1D
:
1715 case GLSL_SAMPLER_DIM_BUF
:
1718 case GLSL_SAMPLER_DIM_2D
:
1719 case GLSL_SAMPLER_DIM_RECT
:
1720 case GLSL_SAMPLER_DIM_MS
:
1721 case GLSL_SAMPLER_DIM_EXTERNAL
:
1724 case GLSL_SAMPLER_DIM_3D
:
1725 case GLSL_SAMPLER_DIM_CUBE
:
1729 assert(!"Should not get here.");
1734 /* Array textures need an additional component for the array index, except
1735 * for cubemap array images that behave like a 2D array of interleaved
1738 if (sampler_array
&&
1739 !(base_type
== GLSL_TYPE_IMAGE
&&
1740 sampler_dimensionality
== GLSL_SAMPLER_DIM_CUBE
))
1747 * Declarations of type flyweights (glsl_type::_foo_type) and
1748 * convenience pointers (glsl_type::foo_type).
1751 #define DECL_TYPE(NAME, ...) \
1752 const glsl_type glsl_type::_##NAME##_type = glsl_type(__VA_ARGS__, #NAME); \
1753 const glsl_type *const glsl_type::NAME##_type = &glsl_type::_##NAME##_type;
1755 #define STRUCT_TYPE(NAME)
1757 #include "compiler/builtin_type_macros.h"