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::function_types
= NULL
;
36 hash_table
*glsl_type::subroutine_types
= NULL
;
37 void *glsl_type::mem_ctx
= NULL
;
40 glsl_type::init_ralloc_type_ctx(void)
42 if (glsl_type::mem_ctx
== NULL
) {
43 glsl_type::mem_ctx
= ralloc_autofree_context();
44 assert(glsl_type::mem_ctx
!= NULL
);
48 glsl_type::glsl_type(GLenum gl_type
,
49 glsl_base_type base_type
, unsigned vector_elements
,
50 unsigned matrix_columns
, const char *name
) :
53 sampler_dimensionality(0), sampler_shadow(0), sampler_array(0),
54 sampled_type(0), interface_packing(0),
55 vector_elements(vector_elements
), matrix_columns(matrix_columns
),
58 mtx_lock(&glsl_type::mutex
);
60 init_ralloc_type_ctx();
62 this->name
= ralloc_strdup(this->mem_ctx
, name
);
64 mtx_unlock(&glsl_type::mutex
);
66 /* Neither dimension is zero or both dimensions are zero.
68 assert((vector_elements
== 0) == (matrix_columns
== 0));
69 memset(& fields
, 0, sizeof(fields
));
72 glsl_type::glsl_type(GLenum gl_type
, glsl_base_type base_type
,
73 enum glsl_sampler_dim dim
, bool shadow
, bool array
,
74 unsigned type
, const char *name
) :
77 sampler_dimensionality(dim
), sampler_shadow(shadow
),
78 sampler_array(array
), sampled_type(type
), interface_packing(0),
81 mtx_lock(&glsl_type::mutex
);
83 init_ralloc_type_ctx();
85 this->name
= ralloc_strdup(this->mem_ctx
, name
);
87 mtx_unlock(&glsl_type::mutex
);
89 memset(& fields
, 0, sizeof(fields
));
91 if (base_type
== GLSL_TYPE_SAMPLER
) {
92 /* Samplers take no storage whatsoever. */
93 matrix_columns
= vector_elements
= 0;
95 matrix_columns
= vector_elements
= 1;
99 glsl_type::glsl_type(const glsl_struct_field
*fields
, unsigned num_fields
,
102 base_type(GLSL_TYPE_STRUCT
),
103 sampler_dimensionality(0), sampler_shadow(0), sampler_array(0),
104 sampled_type(0), interface_packing(0),
105 vector_elements(0), matrix_columns(0),
110 mtx_lock(&glsl_type::mutex
);
112 init_ralloc_type_ctx();
113 assert(name
!= NULL
);
114 this->name
= ralloc_strdup(this->mem_ctx
, name
);
115 this->fields
.structure
= ralloc_array(this->mem_ctx
,
116 glsl_struct_field
, length
);
118 for (i
= 0; i
< length
; i
++) {
119 this->fields
.structure
[i
].type
= fields
[i
].type
;
120 this->fields
.structure
[i
].name
= ralloc_strdup(this->fields
.structure
,
122 this->fields
.structure
[i
].location
= fields
[i
].location
;
123 this->fields
.structure
[i
].offset
= fields
[i
].offset
;
124 this->fields
.structure
[i
].interpolation
= fields
[i
].interpolation
;
125 this->fields
.structure
[i
].centroid
= fields
[i
].centroid
;
126 this->fields
.structure
[i
].sample
= fields
[i
].sample
;
127 this->fields
.structure
[i
].matrix_layout
= fields
[i
].matrix_layout
;
128 this->fields
.structure
[i
].patch
= fields
[i
].patch
;
129 this->fields
.structure
[i
].image_read_only
= fields
[i
].image_read_only
;
130 this->fields
.structure
[i
].image_write_only
= fields
[i
].image_write_only
;
131 this->fields
.structure
[i
].image_coherent
= fields
[i
].image_coherent
;
132 this->fields
.structure
[i
].image_volatile
= fields
[i
].image_volatile
;
133 this->fields
.structure
[i
].image_restrict
= fields
[i
].image_restrict
;
134 this->fields
.structure
[i
].precision
= fields
[i
].precision
;
135 this->fields
.structure
[i
].explicit_xfb_buffer
=
136 fields
[i
].explicit_xfb_buffer
;
137 this->fields
.structure
[i
].xfb_buffer
= fields
[i
].xfb_buffer
;
138 this->fields
.structure
[i
].xfb_stride
= fields
[i
].xfb_stride
;
141 mtx_unlock(&glsl_type::mutex
);
144 glsl_type::glsl_type(const glsl_struct_field
*fields
, unsigned num_fields
,
145 enum glsl_interface_packing packing
, const char *name
) :
147 base_type(GLSL_TYPE_INTERFACE
),
148 sampler_dimensionality(0), sampler_shadow(0), sampler_array(0),
149 sampled_type(0), interface_packing((unsigned) packing
),
150 vector_elements(0), matrix_columns(0),
155 mtx_lock(&glsl_type::mutex
);
157 init_ralloc_type_ctx();
158 assert(name
!= NULL
);
159 this->name
= ralloc_strdup(this->mem_ctx
, name
);
160 this->fields
.structure
= ralloc_array(this->mem_ctx
,
161 glsl_struct_field
, length
);
162 for (i
= 0; i
< length
; i
++) {
163 this->fields
.structure
[i
].type
= fields
[i
].type
;
164 this->fields
.structure
[i
].name
= ralloc_strdup(this->fields
.structure
,
166 this->fields
.structure
[i
].location
= fields
[i
].location
;
167 this->fields
.structure
[i
].offset
= fields
[i
].offset
;
168 this->fields
.structure
[i
].interpolation
= fields
[i
].interpolation
;
169 this->fields
.structure
[i
].centroid
= fields
[i
].centroid
;
170 this->fields
.structure
[i
].sample
= fields
[i
].sample
;
171 this->fields
.structure
[i
].matrix_layout
= fields
[i
].matrix_layout
;
172 this->fields
.structure
[i
].patch
= fields
[i
].patch
;
173 this->fields
.structure
[i
].image_read_only
= fields
[i
].image_read_only
;
174 this->fields
.structure
[i
].image_write_only
= fields
[i
].image_write_only
;
175 this->fields
.structure
[i
].image_coherent
= fields
[i
].image_coherent
;
176 this->fields
.structure
[i
].image_volatile
= fields
[i
].image_volatile
;
177 this->fields
.structure
[i
].image_restrict
= fields
[i
].image_restrict
;
178 this->fields
.structure
[i
].precision
= fields
[i
].precision
;
179 this->fields
.structure
[i
].explicit_xfb_buffer
=
180 fields
[i
].explicit_xfb_buffer
;
181 this->fields
.structure
[i
].xfb_buffer
= fields
[i
].xfb_buffer
;
182 this->fields
.structure
[i
].xfb_stride
= fields
[i
].xfb_stride
;
185 mtx_unlock(&glsl_type::mutex
);
188 glsl_type::glsl_type(const glsl_type
*return_type
,
189 const glsl_function_param
*params
, unsigned num_params
) :
191 base_type(GLSL_TYPE_FUNCTION
),
192 sampler_dimensionality(0), sampler_shadow(0), sampler_array(0),
193 sampled_type(0), interface_packing(0),
194 vector_elements(0), matrix_columns(0),
199 mtx_lock(&glsl_type::mutex
);
201 init_ralloc_type_ctx();
203 this->fields
.parameters
= rzalloc_array(this->mem_ctx
,
204 glsl_function_param
, num_params
+ 1);
206 /* We store the return type as the first parameter */
207 this->fields
.parameters
[0].type
= return_type
;
208 this->fields
.parameters
[0].in
= false;
209 this->fields
.parameters
[0].out
= true;
211 /* We store the i'th parameter in slot i+1 */
212 for (i
= 0; i
< length
; i
++) {
213 this->fields
.parameters
[i
+ 1].type
= params
[i
].type
;
214 this->fields
.parameters
[i
+ 1].in
= params
[i
].in
;
215 this->fields
.parameters
[i
+ 1].out
= params
[i
].out
;
218 mtx_unlock(&glsl_type::mutex
);
221 glsl_type::glsl_type(const char *subroutine_name
) :
223 base_type(GLSL_TYPE_SUBROUTINE
),
224 sampler_dimensionality(0), sampler_shadow(0), sampler_array(0),
225 sampled_type(0), interface_packing(0),
226 vector_elements(1), matrix_columns(1),
229 mtx_lock(&glsl_type::mutex
);
231 init_ralloc_type_ctx();
232 assert(subroutine_name
!= NULL
);
233 this->name
= ralloc_strdup(this->mem_ctx
, subroutine_name
);
234 mtx_unlock(&glsl_type::mutex
);
238 glsl_type::contains_sampler() const
240 if (this->is_array()) {
241 return this->fields
.array
->contains_sampler();
242 } else if (this->is_record()) {
243 for (unsigned int i
= 0; i
< this->length
; i
++) {
244 if (this->fields
.structure
[i
].type
->contains_sampler())
249 return this->is_sampler();
255 glsl_type::contains_integer() const
257 if (this->is_array()) {
258 return this->fields
.array
->contains_integer();
259 } else if (this->is_record()) {
260 for (unsigned int i
= 0; i
< this->length
; i
++) {
261 if (this->fields
.structure
[i
].type
->contains_integer())
266 return this->is_integer();
271 glsl_type::contains_double() const
273 if (this->is_array()) {
274 return this->fields
.array
->contains_double();
275 } else if (this->is_record()) {
276 for (unsigned int i
= 0; i
< this->length
; i
++) {
277 if (this->fields
.structure
[i
].type
->contains_double())
282 return this->is_double();
287 glsl_type::contains_opaque() const {
289 case GLSL_TYPE_SAMPLER
:
290 case GLSL_TYPE_IMAGE
:
291 case GLSL_TYPE_ATOMIC_UINT
:
293 case GLSL_TYPE_ARRAY
:
294 return fields
.array
->contains_opaque();
295 case GLSL_TYPE_STRUCT
:
296 for (unsigned int i
= 0; i
< length
; i
++) {
297 if (fields
.structure
[i
].type
->contains_opaque())
307 glsl_type::contains_subroutine() const
309 if (this->is_array()) {
310 return this->fields
.array
->contains_subroutine();
311 } else if (this->is_record()) {
312 for (unsigned int i
= 0; i
< this->length
; i
++) {
313 if (this->fields
.structure
[i
].type
->contains_subroutine())
318 return this->is_subroutine();
323 glsl_type::sampler_index() const
325 const glsl_type
*const t
= (this->is_array()) ? this->fields
.array
: this;
327 assert(t
->is_sampler());
329 switch (t
->sampler_dimensionality
) {
330 case GLSL_SAMPLER_DIM_1D
:
331 return (t
->sampler_array
) ? TEXTURE_1D_ARRAY_INDEX
: TEXTURE_1D_INDEX
;
332 case GLSL_SAMPLER_DIM_2D
:
333 return (t
->sampler_array
) ? TEXTURE_2D_ARRAY_INDEX
: TEXTURE_2D_INDEX
;
334 case GLSL_SAMPLER_DIM_3D
:
335 return TEXTURE_3D_INDEX
;
336 case GLSL_SAMPLER_DIM_CUBE
:
337 return (t
->sampler_array
) ? TEXTURE_CUBE_ARRAY_INDEX
: TEXTURE_CUBE_INDEX
;
338 case GLSL_SAMPLER_DIM_RECT
:
339 return TEXTURE_RECT_INDEX
;
340 case GLSL_SAMPLER_DIM_BUF
:
341 return TEXTURE_BUFFER_INDEX
;
342 case GLSL_SAMPLER_DIM_EXTERNAL
:
343 return TEXTURE_EXTERNAL_INDEX
;
344 case GLSL_SAMPLER_DIM_MS
:
345 return (t
->sampler_array
) ? TEXTURE_2D_MULTISAMPLE_ARRAY_INDEX
: TEXTURE_2D_MULTISAMPLE_INDEX
;
347 assert(!"Should not get here.");
348 return TEXTURE_BUFFER_INDEX
;
353 glsl_type::contains_image() const
355 if (this->is_array()) {
356 return this->fields
.array
->contains_image();
357 } else if (this->is_record()) {
358 for (unsigned int i
= 0; i
< this->length
; i
++) {
359 if (this->fields
.structure
[i
].type
->contains_image())
364 return this->is_image();
368 const glsl_type
*glsl_type::get_base_type() const
375 case GLSL_TYPE_FLOAT
:
377 case GLSL_TYPE_DOUBLE
:
387 const glsl_type
*glsl_type::get_scalar_type() const
389 const glsl_type
*type
= this;
392 while (type
->base_type
== GLSL_TYPE_ARRAY
)
393 type
= type
->fields
.array
;
395 /* Handle vectors and matrices */
396 switch (type
->base_type
) {
401 case GLSL_TYPE_FLOAT
:
403 case GLSL_TYPE_DOUBLE
:
408 /* Handle everything else */
415 _mesa_glsl_release_types(void)
417 /* Should only be called during atexit (either when unloading shared
418 * object, or if process terminates), so no mutex-locking should be
421 if (glsl_type::array_types
!= NULL
) {
422 _mesa_hash_table_destroy(glsl_type::array_types
, NULL
);
423 glsl_type::array_types
= NULL
;
426 if (glsl_type::record_types
!= NULL
) {
427 _mesa_hash_table_destroy(glsl_type::record_types
, NULL
);
428 glsl_type::record_types
= NULL
;
431 if (glsl_type::interface_types
!= NULL
) {
432 _mesa_hash_table_destroy(glsl_type::interface_types
, NULL
);
433 glsl_type::interface_types
= NULL
;
438 glsl_type::glsl_type(const glsl_type
*array
, unsigned length
) :
439 base_type(GLSL_TYPE_ARRAY
),
440 sampler_dimensionality(0), sampler_shadow(0), sampler_array(0),
441 sampled_type(0), interface_packing(0),
442 vector_elements(0), matrix_columns(0),
443 length(length
), name(NULL
)
445 this->fields
.array
= array
;
446 /* Inherit the gl type of the base. The GL type is used for
447 * uniform/statevar handling in Mesa and the arrayness of the type
448 * is represented by the size rather than the type.
450 this->gl_type
= array
->gl_type
;
452 /* Allow a maximum of 10 characters for the array size. This is enough
453 * for 32-bits of ~0. The extra 3 are for the '[', ']', and terminating
456 const unsigned name_length
= strlen(array
->name
) + 10 + 3;
458 mtx_lock(&glsl_type::mutex
);
459 char *const n
= (char *) ralloc_size(this->mem_ctx
, name_length
);
460 mtx_unlock(&glsl_type::mutex
);
463 snprintf(n
, name_length
, "%s[]", array
->name
);
465 /* insert outermost dimensions in the correct spot
466 * otherwise the dimension order will be backwards
468 const char *pos
= strchr(array
->name
, '[');
470 int idx
= pos
- array
->name
;
471 snprintf(n
, idx
+1, "%s", array
->name
);
472 snprintf(n
+ idx
, name_length
- idx
, "[%u]%s",
473 length
, array
->name
+ idx
);
475 snprintf(n
, name_length
, "%s[%u]", array
->name
, length
);
484 glsl_type::vec(unsigned components
)
486 if (components
== 0 || components
> 4)
489 static const glsl_type
*const ts
[] = {
490 float_type
, vec2_type
, vec3_type
, vec4_type
492 return ts
[components
- 1];
496 glsl_type::dvec(unsigned components
)
498 if (components
== 0 || components
> 4)
501 static const glsl_type
*const ts
[] = {
502 double_type
, dvec2_type
, dvec3_type
, dvec4_type
504 return ts
[components
- 1];
508 glsl_type::ivec(unsigned components
)
510 if (components
== 0 || components
> 4)
513 static const glsl_type
*const ts
[] = {
514 int_type
, ivec2_type
, ivec3_type
, ivec4_type
516 return ts
[components
- 1];
521 glsl_type::uvec(unsigned components
)
523 if (components
== 0 || components
> 4)
526 static const glsl_type
*const ts
[] = {
527 uint_type
, uvec2_type
, uvec3_type
, uvec4_type
529 return ts
[components
- 1];
534 glsl_type::bvec(unsigned components
)
536 if (components
== 0 || components
> 4)
539 static const glsl_type
*const ts
[] = {
540 bool_type
, bvec2_type
, bvec3_type
, bvec4_type
542 return ts
[components
- 1];
547 glsl_type::get_instance(unsigned base_type
, unsigned rows
, unsigned columns
)
549 if (base_type
== GLSL_TYPE_VOID
)
552 if ((rows
< 1) || (rows
> 4) || (columns
< 1) || (columns
> 4))
555 /* Treat GLSL vectors as Nx1 matrices.
563 case GLSL_TYPE_FLOAT
:
565 case GLSL_TYPE_DOUBLE
:
573 if ((base_type
!= GLSL_TYPE_FLOAT
&& base_type
!= GLSL_TYPE_DOUBLE
) || (rows
== 1))
576 /* GLSL matrix types are named mat{COLUMNS}x{ROWS}. Only the following
577 * combinations are valid:
585 #define IDX(c,r) (((c-1)*3) + (r-1))
587 if (base_type
== GLSL_TYPE_DOUBLE
) {
588 switch (IDX(columns
, rows
)) {
589 case IDX(2,2): return dmat2_type
;
590 case IDX(2,3): return dmat2x3_type
;
591 case IDX(2,4): return dmat2x4_type
;
592 case IDX(3,2): return dmat3x2_type
;
593 case IDX(3,3): return dmat3_type
;
594 case IDX(3,4): return dmat3x4_type
;
595 case IDX(4,2): return dmat4x2_type
;
596 case IDX(4,3): return dmat4x3_type
;
597 case IDX(4,4): return dmat4_type
;
598 default: return error_type
;
601 switch (IDX(columns
, rows
)) {
602 case IDX(2,2): return mat2_type
;
603 case IDX(2,3): return mat2x3_type
;
604 case IDX(2,4): return mat2x4_type
;
605 case IDX(3,2): return mat3x2_type
;
606 case IDX(3,3): return mat3_type
;
607 case IDX(3,4): return mat3x4_type
;
608 case IDX(4,2): return mat4x2_type
;
609 case IDX(4,3): return mat4x3_type
;
610 case IDX(4,4): return mat4_type
;
611 default: return error_type
;
616 assert(!"Should not get here.");
621 glsl_type::get_sampler_instance(enum glsl_sampler_dim dim
,
627 case GLSL_TYPE_FLOAT
:
629 case GLSL_SAMPLER_DIM_1D
:
631 return (array
? sampler1DArrayShadow_type
: sampler1DShadow_type
);
633 return (array
? sampler1DArray_type
: sampler1D_type
);
634 case GLSL_SAMPLER_DIM_2D
:
636 return (array
? sampler2DArrayShadow_type
: sampler2DShadow_type
);
638 return (array
? sampler2DArray_type
: sampler2D_type
);
639 case GLSL_SAMPLER_DIM_3D
:
643 return sampler3D_type
;
644 case GLSL_SAMPLER_DIM_CUBE
:
646 return (array
? samplerCubeArrayShadow_type
: samplerCubeShadow_type
);
648 return (array
? samplerCubeArray_type
: samplerCube_type
);
649 case GLSL_SAMPLER_DIM_RECT
:
653 return sampler2DRectShadow_type
;
655 return sampler2DRect_type
;
656 case GLSL_SAMPLER_DIM_BUF
:
660 return samplerBuffer_type
;
661 case GLSL_SAMPLER_DIM_MS
:
664 return (array
? sampler2DMSArray_type
: sampler2DMS_type
);
665 case GLSL_SAMPLER_DIM_EXTERNAL
:
669 return samplerExternalOES_type
;
675 case GLSL_SAMPLER_DIM_1D
:
676 return (array
? isampler1DArray_type
: isampler1D_type
);
677 case GLSL_SAMPLER_DIM_2D
:
678 return (array
? isampler2DArray_type
: isampler2D_type
);
679 case GLSL_SAMPLER_DIM_3D
:
682 return isampler3D_type
;
683 case GLSL_SAMPLER_DIM_CUBE
:
684 return (array
? isamplerCubeArray_type
: isamplerCube_type
);
685 case GLSL_SAMPLER_DIM_RECT
:
688 return isampler2DRect_type
;
689 case GLSL_SAMPLER_DIM_BUF
:
692 return isamplerBuffer_type
;
693 case GLSL_SAMPLER_DIM_MS
:
694 return (array
? isampler2DMSArray_type
: isampler2DMS_type
);
695 case GLSL_SAMPLER_DIM_EXTERNAL
:
702 case GLSL_SAMPLER_DIM_1D
:
703 return (array
? usampler1DArray_type
: usampler1D_type
);
704 case GLSL_SAMPLER_DIM_2D
:
705 return (array
? usampler2DArray_type
: usampler2D_type
);
706 case GLSL_SAMPLER_DIM_3D
:
709 return usampler3D_type
;
710 case GLSL_SAMPLER_DIM_CUBE
:
711 return (array
? usamplerCubeArray_type
: usamplerCube_type
);
712 case GLSL_SAMPLER_DIM_RECT
:
715 return usampler2DRect_type
;
716 case GLSL_SAMPLER_DIM_BUF
:
719 return usamplerBuffer_type
;
720 case GLSL_SAMPLER_DIM_MS
:
721 return (array
? usampler2DMSArray_type
: usampler2DMS_type
);
722 case GLSL_SAMPLER_DIM_EXTERNAL
:
729 unreachable("switch statement above should be complete");
733 glsl_type::get_image_instance(enum glsl_sampler_dim dim
,
734 bool array
, glsl_base_type type
)
737 case GLSL_TYPE_FLOAT
:
739 case GLSL_SAMPLER_DIM_1D
:
740 return (array
? image1DArray_type
: image1D_type
);
741 case GLSL_SAMPLER_DIM_2D
:
742 return (array
? image2DArray_type
: image2D_type
);
743 case GLSL_SAMPLER_DIM_3D
:
745 case GLSL_SAMPLER_DIM_CUBE
:
746 return (array
? imageCubeArray_type
: imageCube_type
);
747 case GLSL_SAMPLER_DIM_RECT
:
751 return image2DRect_type
;
752 case GLSL_SAMPLER_DIM_BUF
:
756 return imageBuffer_type
;
757 case GLSL_SAMPLER_DIM_MS
:
758 return (array
? image2DMSArray_type
: image2DMS_type
);
759 case GLSL_SAMPLER_DIM_EXTERNAL
:
764 case GLSL_SAMPLER_DIM_1D
:
765 return (array
? iimage1DArray_type
: iimage1D_type
);
766 case GLSL_SAMPLER_DIM_2D
:
767 return (array
? iimage2DArray_type
: iimage2D_type
);
768 case GLSL_SAMPLER_DIM_3D
:
771 return iimage3D_type
;
772 case GLSL_SAMPLER_DIM_CUBE
:
773 return (array
? iimageCubeArray_type
: iimageCube_type
);
774 case GLSL_SAMPLER_DIM_RECT
:
777 return iimage2DRect_type
;
778 case GLSL_SAMPLER_DIM_BUF
:
781 return iimageBuffer_type
;
782 case GLSL_SAMPLER_DIM_MS
:
783 return (array
? iimage2DMSArray_type
: iimage2DMS_type
);
784 case GLSL_SAMPLER_DIM_EXTERNAL
:
789 case GLSL_SAMPLER_DIM_1D
:
790 return (array
? uimage1DArray_type
: uimage1D_type
);
791 case GLSL_SAMPLER_DIM_2D
:
792 return (array
? uimage2DArray_type
: uimage2D_type
);
793 case GLSL_SAMPLER_DIM_3D
:
796 return uimage3D_type
;
797 case GLSL_SAMPLER_DIM_CUBE
:
798 return (array
? uimageCubeArray_type
: uimageCube_type
);
799 case GLSL_SAMPLER_DIM_RECT
:
802 return uimage2DRect_type
;
803 case GLSL_SAMPLER_DIM_BUF
:
806 return uimageBuffer_type
;
807 case GLSL_SAMPLER_DIM_MS
:
808 return (array
? uimage2DMSArray_type
: uimage2DMS_type
);
809 case GLSL_SAMPLER_DIM_EXTERNAL
:
816 unreachable("switch statement above should be complete");
820 glsl_type::get_array_instance(const glsl_type
*base
, unsigned array_size
)
822 /* Generate a name using the base type pointer in the key. This is
823 * done because the name of the base type may not be unique across
824 * shaders. For example, two shaders may have different record types
828 snprintf(key
, sizeof(key
), "%p[%u]", (void *) base
, array_size
);
830 mtx_lock(&glsl_type::mutex
);
832 if (array_types
== NULL
) {
833 array_types
= _mesa_hash_table_create(NULL
, _mesa_key_hash_string
,
834 _mesa_key_string_equal
);
837 const struct hash_entry
*entry
= _mesa_hash_table_search(array_types
, key
);
839 mtx_unlock(&glsl_type::mutex
);
840 const glsl_type
*t
= new glsl_type(base
, array_size
);
841 mtx_lock(&glsl_type::mutex
);
843 entry
= _mesa_hash_table_insert(array_types
,
844 ralloc_strdup(mem_ctx
, key
),
848 assert(((glsl_type
*) entry
->data
)->base_type
== GLSL_TYPE_ARRAY
);
849 assert(((glsl_type
*) entry
->data
)->length
== array_size
);
850 assert(((glsl_type
*) entry
->data
)->fields
.array
== base
);
852 mtx_unlock(&glsl_type::mutex
);
854 return (glsl_type
*) entry
->data
;
859 glsl_type::record_compare(const glsl_type
*b
, bool match_locations
) const
861 if (this->length
!= b
->length
)
864 if (this->interface_packing
!= b
->interface_packing
)
867 /* From the GLSL 4.20 specification (Sec 4.2):
869 * "Structures must have the same name, sequence of type names, and
870 * type definitions, and field names to be considered the same type."
872 * GLSL ES behaves the same (Ver 1.00 Sec 4.2.4, Ver 3.00 Sec 4.2.5).
874 * Note that we cannot force type name check when comparing unnamed
875 * structure types, these have a unique name assigned during parsing.
877 if (!this->is_anonymous() && !b
->is_anonymous())
878 if (strcmp(this->name
, b
->name
) != 0)
881 for (unsigned i
= 0; i
< this->length
; i
++) {
882 if (this->fields
.structure
[i
].type
!= b
->fields
.structure
[i
].type
)
884 if (strcmp(this->fields
.structure
[i
].name
,
885 b
->fields
.structure
[i
].name
) != 0)
887 if (this->fields
.structure
[i
].matrix_layout
888 != b
->fields
.structure
[i
].matrix_layout
)
890 if (match_locations
&& this->fields
.structure
[i
].location
891 != b
->fields
.structure
[i
].location
)
893 if (this->fields
.structure
[i
].offset
894 != b
->fields
.structure
[i
].offset
)
896 if (this->fields
.structure
[i
].interpolation
897 != b
->fields
.structure
[i
].interpolation
)
899 if (this->fields
.structure
[i
].centroid
900 != b
->fields
.structure
[i
].centroid
)
902 if (this->fields
.structure
[i
].sample
903 != b
->fields
.structure
[i
].sample
)
905 if (this->fields
.structure
[i
].patch
906 != b
->fields
.structure
[i
].patch
)
908 if (this->fields
.structure
[i
].image_read_only
909 != b
->fields
.structure
[i
].image_read_only
)
911 if (this->fields
.structure
[i
].image_write_only
912 != b
->fields
.structure
[i
].image_write_only
)
914 if (this->fields
.structure
[i
].image_coherent
915 != b
->fields
.structure
[i
].image_coherent
)
917 if (this->fields
.structure
[i
].image_volatile
918 != b
->fields
.structure
[i
].image_volatile
)
920 if (this->fields
.structure
[i
].image_restrict
921 != b
->fields
.structure
[i
].image_restrict
)
923 if (this->fields
.structure
[i
].precision
924 != b
->fields
.structure
[i
].precision
)
926 if (this->fields
.structure
[i
].explicit_xfb_buffer
927 != b
->fields
.structure
[i
].explicit_xfb_buffer
)
929 if (this->fields
.structure
[i
].xfb_buffer
930 != b
->fields
.structure
[i
].xfb_buffer
)
932 if (this->fields
.structure
[i
].xfb_stride
933 != b
->fields
.structure
[i
].xfb_stride
)
942 glsl_type::record_key_compare(const void *a
, const void *b
)
944 const glsl_type
*const key1
= (glsl_type
*) a
;
945 const glsl_type
*const key2
= (glsl_type
*) b
;
947 return strcmp(key1
->name
, key2
->name
) == 0 && key1
->record_compare(key2
);
952 * Generate an integer hash value for a glsl_type structure type.
955 glsl_type::record_key_hash(const void *a
)
957 const glsl_type
*const key
= (glsl_type
*) a
;
958 uintptr_t hash
= key
->length
;
961 for (unsigned i
= 0; i
< key
->length
; i
++) {
962 /* casting pointer to uintptr_t */
963 hash
= (hash
* 13 ) + (uintptr_t) key
->fields
.structure
[i
].type
;
966 if (sizeof(hash
) == 8)
967 retval
= (hash
& 0xffffffff) ^ ((uint64_t) hash
>> 32);
976 glsl_type::get_record_instance(const glsl_struct_field
*fields
,
980 const glsl_type
key(fields
, num_fields
, name
);
982 mtx_lock(&glsl_type::mutex
);
984 if (record_types
== NULL
) {
985 record_types
= _mesa_hash_table_create(NULL
, record_key_hash
,
989 const struct hash_entry
*entry
= _mesa_hash_table_search(record_types
,
992 mtx_unlock(&glsl_type::mutex
);
993 const glsl_type
*t
= new glsl_type(fields
, num_fields
, name
);
994 mtx_lock(&glsl_type::mutex
);
996 entry
= _mesa_hash_table_insert(record_types
, t
, (void *) t
);
999 assert(((glsl_type
*) entry
->data
)->base_type
== GLSL_TYPE_STRUCT
);
1000 assert(((glsl_type
*) entry
->data
)->length
== num_fields
);
1001 assert(strcmp(((glsl_type
*) entry
->data
)->name
, name
) == 0);
1003 mtx_unlock(&glsl_type::mutex
);
1005 return (glsl_type
*) entry
->data
;
1010 glsl_type::get_interface_instance(const glsl_struct_field
*fields
,
1011 unsigned num_fields
,
1012 enum glsl_interface_packing packing
,
1013 const char *block_name
)
1015 const glsl_type
key(fields
, num_fields
, packing
, block_name
);
1017 mtx_lock(&glsl_type::mutex
);
1019 if (interface_types
== NULL
) {
1020 interface_types
= _mesa_hash_table_create(NULL
, record_key_hash
,
1021 record_key_compare
);
1024 const struct hash_entry
*entry
= _mesa_hash_table_search(interface_types
,
1026 if (entry
== NULL
) {
1027 mtx_unlock(&glsl_type::mutex
);
1028 const glsl_type
*t
= new glsl_type(fields
, num_fields
,
1029 packing
, block_name
);
1030 mtx_lock(&glsl_type::mutex
);
1032 entry
= _mesa_hash_table_insert(interface_types
, t
, (void *) t
);
1035 assert(((glsl_type
*) entry
->data
)->base_type
== GLSL_TYPE_INTERFACE
);
1036 assert(((glsl_type
*) entry
->data
)->length
== num_fields
);
1037 assert(strcmp(((glsl_type
*) entry
->data
)->name
, block_name
) == 0);
1039 mtx_unlock(&glsl_type::mutex
);
1041 return (glsl_type
*) entry
->data
;
1045 glsl_type::get_subroutine_instance(const char *subroutine_name
)
1047 const glsl_type
key(subroutine_name
);
1049 mtx_lock(&glsl_type::mutex
);
1051 if (subroutine_types
== NULL
) {
1052 subroutine_types
= _mesa_hash_table_create(NULL
, record_key_hash
,
1053 record_key_compare
);
1056 const struct hash_entry
*entry
= _mesa_hash_table_search(subroutine_types
,
1058 if (entry
== NULL
) {
1059 mtx_unlock(&glsl_type::mutex
);
1060 const glsl_type
*t
= new glsl_type(subroutine_name
);
1061 mtx_lock(&glsl_type::mutex
);
1063 entry
= _mesa_hash_table_insert(subroutine_types
, t
, (void *) t
);
1066 assert(((glsl_type
*) entry
->data
)->base_type
== GLSL_TYPE_SUBROUTINE
);
1067 assert(strcmp(((glsl_type
*) entry
->data
)->name
, subroutine_name
) == 0);
1069 mtx_unlock(&glsl_type::mutex
);
1071 return (glsl_type
*) entry
->data
;
1076 function_key_compare(const void *a
, const void *b
)
1078 const glsl_type
*const key1
= (glsl_type
*) a
;
1079 const glsl_type
*const key2
= (glsl_type
*) b
;
1081 if (key1
->length
!= key2
->length
)
1084 return memcmp(key1
->fields
.parameters
, key2
->fields
.parameters
,
1085 (key1
->length
+ 1) * sizeof(*key1
->fields
.parameters
)) == 0;
1090 function_key_hash(const void *a
)
1092 const glsl_type
*const key
= (glsl_type
*) a
;
1096 size
= snprintf(hash_key
, sizeof(hash_key
), "%08x", key
->length
);
1098 for (unsigned i
= 0; i
< key
->length
; i
++) {
1099 if (size
>= sizeof(hash_key
))
1102 size
+= snprintf(& hash_key
[size
], sizeof(hash_key
) - size
,
1103 "%p", (void *) key
->fields
.structure
[i
].type
);
1106 return _mesa_hash_string(hash_key
);
1110 glsl_type::get_function_instance(const glsl_type
*return_type
,
1111 const glsl_function_param
*params
,
1112 unsigned num_params
)
1114 const glsl_type
key(return_type
, params
, num_params
);
1116 mtx_lock(&glsl_type::mutex
);
1118 if (function_types
== NULL
) {
1119 function_types
= _mesa_hash_table_create(NULL
, function_key_hash
,
1120 function_key_compare
);
1123 struct hash_entry
*entry
= _mesa_hash_table_search(function_types
, &key
);
1124 if (entry
== NULL
) {
1125 mtx_unlock(&glsl_type::mutex
);
1126 const glsl_type
*t
= new glsl_type(return_type
, params
, num_params
);
1127 mtx_lock(&glsl_type::mutex
);
1129 entry
= _mesa_hash_table_insert(function_types
, t
, (void *) t
);
1132 const glsl_type
*t
= (const glsl_type
*)entry
->data
;
1134 assert(t
->base_type
== GLSL_TYPE_FUNCTION
);
1135 assert(t
->length
== num_params
);
1137 mtx_unlock(&glsl_type::mutex
);
1144 glsl_type::get_mul_type(const glsl_type
*type_a
, const glsl_type
*type_b
)
1146 if (type_a
== type_b
) {
1148 } else if (type_a
->is_matrix() && type_b
->is_matrix()) {
1149 /* Matrix multiply. The columns of A must match the rows of B. Given
1150 * the other previously tested constraints, this means the vector type
1151 * of a row from A must be the same as the vector type of a column from
1154 if (type_a
->row_type() == type_b
->column_type()) {
1155 /* The resulting matrix has the number of columns of matrix B and
1156 * the number of rows of matrix A. We get the row count of A by
1157 * looking at the size of a vector that makes up a column. The
1158 * transpose (size of a row) is done for B.
1160 const glsl_type
*const type
=
1161 get_instance(type_a
->base_type
,
1162 type_a
->column_type()->vector_elements
,
1163 type_b
->row_type()->vector_elements
);
1164 assert(type
!= error_type
);
1168 } else if (type_a
->is_matrix()) {
1169 /* A is a matrix and B is a column vector. Columns of A must match
1170 * rows of B. Given the other previously tested constraints, this
1171 * means the vector type of a row from A must be the same as the
1172 * vector the type of B.
1174 if (type_a
->row_type() == type_b
) {
1175 /* The resulting vector has a number of elements equal to
1176 * the number of rows of matrix A. */
1177 const glsl_type
*const type
=
1178 get_instance(type_a
->base_type
,
1179 type_a
->column_type()->vector_elements
,
1181 assert(type
!= error_type
);
1186 assert(type_b
->is_matrix());
1188 /* A is a row vector and B is a matrix. Columns of A must match rows
1189 * of B. Given the other previously tested constraints, this means
1190 * the type of A must be the same as the vector type of a column from
1193 if (type_a
== type_b
->column_type()) {
1194 /* The resulting vector has a number of elements equal to
1195 * the number of columns of matrix B. */
1196 const glsl_type
*const type
=
1197 get_instance(type_a
->base_type
,
1198 type_b
->row_type()->vector_elements
,
1200 assert(type
!= error_type
);
1211 glsl_type::field_type(const char *name
) const
1213 if (this->base_type
!= GLSL_TYPE_STRUCT
1214 && this->base_type
!= GLSL_TYPE_INTERFACE
)
1217 for (unsigned i
= 0; i
< this->length
; i
++) {
1218 if (strcmp(name
, this->fields
.structure
[i
].name
) == 0)
1219 return this->fields
.structure
[i
].type
;
1227 glsl_type::field_index(const char *name
) const
1229 if (this->base_type
!= GLSL_TYPE_STRUCT
1230 && this->base_type
!= GLSL_TYPE_INTERFACE
)
1233 for (unsigned i
= 0; i
< this->length
; i
++) {
1234 if (strcmp(name
, this->fields
.structure
[i
].name
) == 0)
1243 glsl_type::component_slots() const
1245 switch (this->base_type
) {
1246 case GLSL_TYPE_UINT
:
1248 case GLSL_TYPE_FLOAT
:
1249 case GLSL_TYPE_BOOL
:
1250 return this->components();
1252 case GLSL_TYPE_DOUBLE
:
1253 return 2 * this->components();
1255 case GLSL_TYPE_STRUCT
:
1256 case GLSL_TYPE_INTERFACE
: {
1259 for (unsigned i
= 0; i
< this->length
; i
++)
1260 size
+= this->fields
.structure
[i
].type
->component_slots();
1265 case GLSL_TYPE_ARRAY
:
1266 return this->length
* this->fields
.array
->component_slots();
1268 case GLSL_TYPE_IMAGE
:
1270 case GLSL_TYPE_SUBROUTINE
:
1273 case GLSL_TYPE_FUNCTION
:
1274 case GLSL_TYPE_SAMPLER
:
1275 case GLSL_TYPE_ATOMIC_UINT
:
1276 case GLSL_TYPE_VOID
:
1277 case GLSL_TYPE_ERROR
:
1285 glsl_type::record_location_offset(unsigned length
) const
1287 unsigned offset
= 0;
1288 const glsl_type
*t
= this->without_array();
1289 if (t
->is_record()) {
1290 assert(length
<= t
->length
);
1292 for (unsigned i
= 0; i
< length
; i
++) {
1293 const glsl_type
*st
= t
->fields
.structure
[i
].type
;
1294 const glsl_type
*wa
= st
->without_array();
1295 if (wa
->is_record()) {
1296 unsigned r_offset
= wa
->record_location_offset(wa
->length
);
1297 offset
+= st
->is_array() ?
1298 st
->arrays_of_arrays_size() * r_offset
: r_offset
;
1299 } else if (st
->is_array() && st
->fields
.array
->is_array()) {
1300 unsigned outer_array_size
= st
->length
;
1301 const glsl_type
*base_type
= st
->fields
.array
;
1303 /* For arrays of arrays the outer arrays take up a uniform
1304 * slot for each element. The innermost array elements share a
1305 * single slot so we ignore the innermost array when calculating
1308 while (base_type
->fields
.array
->is_array()) {
1309 outer_array_size
= outer_array_size
* base_type
->length
;
1310 base_type
= base_type
->fields
.array
;
1312 offset
+= outer_array_size
;
1314 /* We dont worry about arrays here because unless the array
1315 * contains a structure or another array it only takes up a single
1326 glsl_type::uniform_locations() const
1330 switch (this->base_type
) {
1331 case GLSL_TYPE_UINT
:
1333 case GLSL_TYPE_FLOAT
:
1334 case GLSL_TYPE_DOUBLE
:
1335 case GLSL_TYPE_BOOL
:
1336 case GLSL_TYPE_SAMPLER
:
1337 case GLSL_TYPE_IMAGE
:
1338 case GLSL_TYPE_SUBROUTINE
:
1341 case GLSL_TYPE_STRUCT
:
1342 case GLSL_TYPE_INTERFACE
:
1343 for (unsigned i
= 0; i
< this->length
; i
++)
1344 size
+= this->fields
.structure
[i
].type
->uniform_locations();
1346 case GLSL_TYPE_ARRAY
:
1347 return this->length
* this->fields
.array
->uniform_locations();
1354 glsl_type::varying_count() const
1358 switch (this->base_type
) {
1359 case GLSL_TYPE_UINT
:
1361 case GLSL_TYPE_FLOAT
:
1362 case GLSL_TYPE_DOUBLE
:
1363 case GLSL_TYPE_BOOL
:
1366 case GLSL_TYPE_STRUCT
:
1367 case GLSL_TYPE_INTERFACE
:
1368 for (unsigned i
= 0; i
< this->length
; i
++)
1369 size
+= this->fields
.structure
[i
].type
->varying_count();
1371 case GLSL_TYPE_ARRAY
:
1372 /* Don't count innermost array elements */
1373 if (this->without_array()->is_record() ||
1374 this->without_array()->is_interface() ||
1375 this->fields
.array
->is_array())
1376 return this->length
* this->fields
.array
->varying_count();
1378 return this->fields
.array
->varying_count();
1380 assert(!"unsupported varying type");
1386 glsl_type::can_implicitly_convert_to(const glsl_type
*desired
,
1387 _mesa_glsl_parse_state
*state
) const
1389 if (this == desired
)
1392 /* ESSL does not allow implicit conversions. If there is no state, we're
1393 * doing intra-stage function linking where these checks have already been
1396 if (state
&& state
->es_shader
)
1399 /* There is no conversion among matrix types. */
1400 if (this->matrix_columns
> 1 || desired
->matrix_columns
> 1)
1403 /* Vector size must match. */
1404 if (this->vector_elements
!= desired
->vector_elements
)
1407 /* int and uint can be converted to float. */
1408 if (desired
->is_float() && this->is_integer())
1411 /* With GLSL 4.0 / ARB_gpu_shader5, int can be converted to uint.
1412 * Note that state may be NULL here, when resolving function calls in the
1413 * linker. By this time, all the state-dependent checks have already
1414 * happened though, so allow anything that's allowed in any shader version. */
1415 if ((!state
|| state
->is_version(400, 0) || state
->ARB_gpu_shader5_enable
) &&
1416 desired
->base_type
== GLSL_TYPE_UINT
&& this->base_type
== GLSL_TYPE_INT
)
1419 /* No implicit conversions from double. */
1420 if ((!state
|| state
->has_double()) && this->is_double())
1423 /* Conversions from different types to double. */
1424 if ((!state
|| state
->has_double()) && desired
->is_double()) {
1425 if (this->is_float())
1427 if (this->is_integer())
1435 glsl_type::std140_base_alignment(bool row_major
) const
1437 unsigned N
= is_double() ? 8 : 4;
1439 /* (1) If the member is a scalar consuming <N> basic machine units, the
1440 * base alignment is <N>.
1442 * (2) If the member is a two- or four-component vector with components
1443 * consuming <N> basic machine units, the base alignment is 2<N> or
1444 * 4<N>, respectively.
1446 * (3) If the member is a three-component vector with components consuming
1447 * <N> basic machine units, the base alignment is 4<N>.
1449 if (this->is_scalar() || this->is_vector()) {
1450 switch (this->vector_elements
) {
1461 /* (4) If the member is an array of scalars or vectors, the base alignment
1462 * and array stride are set to match the base alignment of a single
1463 * array element, according to rules (1), (2), and (3), and rounded up
1464 * to the base alignment of a vec4. The array may have padding at the
1465 * end; the base offset of the member following the array is rounded up
1466 * to the next multiple of the base alignment.
1468 * (6) If the member is an array of <S> column-major matrices with <C>
1469 * columns and <R> rows, the matrix is stored identically to a row of
1470 * <S>*<C> column vectors with <R> components each, according to rule
1473 * (8) If the member is an array of <S> row-major matrices with <C> columns
1474 * and <R> rows, the matrix is stored identically to a row of <S>*<R>
1475 * row vectors with <C> components each, according to rule (4).
1477 * (10) If the member is an array of <S> structures, the <S> elements of
1478 * the array are laid out in order, according to rule (9).
1480 if (this->is_array()) {
1481 if (this->fields
.array
->is_scalar() ||
1482 this->fields
.array
->is_vector() ||
1483 this->fields
.array
->is_matrix()) {
1484 return MAX2(this->fields
.array
->std140_base_alignment(row_major
), 16);
1486 assert(this->fields
.array
->is_record() ||
1487 this->fields
.array
->is_array());
1488 return this->fields
.array
->std140_base_alignment(row_major
);
1492 /* (5) If the member is a column-major matrix with <C> columns and
1493 * <R> rows, the matrix is stored identically to an array of
1494 * <C> column vectors with <R> components each, according to
1497 * (7) If the member is a row-major matrix with <C> columns and <R>
1498 * rows, the matrix is stored identically to an array of <R>
1499 * row vectors with <C> components each, according to rule (4).
1501 if (this->is_matrix()) {
1502 const struct glsl_type
*vec_type
, *array_type
;
1503 int c
= this->matrix_columns
;
1504 int r
= this->vector_elements
;
1507 vec_type
= get_instance(base_type
, c
, 1);
1508 array_type
= glsl_type::get_array_instance(vec_type
, r
);
1510 vec_type
= get_instance(base_type
, r
, 1);
1511 array_type
= glsl_type::get_array_instance(vec_type
, c
);
1514 return array_type
->std140_base_alignment(false);
1517 /* (9) If the member is a structure, the base alignment of the
1518 * structure is <N>, where <N> is the largest base alignment
1519 * value of any of its members, and rounded up to the base
1520 * alignment of a vec4. The individual members of this
1521 * sub-structure are then assigned offsets by applying this set
1522 * of rules recursively, where the base offset of the first
1523 * member of the sub-structure is equal to the aligned offset
1524 * of the structure. The structure may have padding at the end;
1525 * the base offset of the member following the sub-structure is
1526 * rounded up to the next multiple of the base alignment of the
1529 if (this->is_record()) {
1530 unsigned base_alignment
= 16;
1531 for (unsigned i
= 0; i
< this->length
; i
++) {
1532 bool field_row_major
= row_major
;
1533 const enum glsl_matrix_layout matrix_layout
=
1534 glsl_matrix_layout(this->fields
.structure
[i
].matrix_layout
);
1535 if (matrix_layout
== GLSL_MATRIX_LAYOUT_ROW_MAJOR
) {
1536 field_row_major
= true;
1537 } else if (matrix_layout
== GLSL_MATRIX_LAYOUT_COLUMN_MAJOR
) {
1538 field_row_major
= false;
1541 const struct glsl_type
*field_type
= this->fields
.structure
[i
].type
;
1542 base_alignment
= MAX2(base_alignment
,
1543 field_type
->std140_base_alignment(field_row_major
));
1545 return base_alignment
;
1548 assert(!"not reached");
1553 glsl_type::std140_size(bool row_major
) const
1555 unsigned N
= is_double() ? 8 : 4;
1557 /* (1) If the member is a scalar consuming <N> basic machine units, the
1558 * base alignment is <N>.
1560 * (2) If the member is a two- or four-component vector with components
1561 * consuming <N> basic machine units, the base alignment is 2<N> or
1562 * 4<N>, respectively.
1564 * (3) If the member is a three-component vector with components consuming
1565 * <N> basic machine units, the base alignment is 4<N>.
1567 if (this->is_scalar() || this->is_vector()) {
1568 return this->vector_elements
* N
;
1571 /* (5) If the member is a column-major matrix with <C> columns and
1572 * <R> rows, the matrix is stored identically to an array of
1573 * <C> column vectors with <R> components each, according to
1576 * (6) If the member is an array of <S> column-major matrices with <C>
1577 * columns and <R> rows, the matrix is stored identically to a row of
1578 * <S>*<C> column vectors with <R> components each, according to rule
1581 * (7) If the member is a row-major matrix with <C> columns and <R>
1582 * rows, the matrix is stored identically to an array of <R>
1583 * row vectors with <C> components each, according to rule (4).
1585 * (8) If the member is an array of <S> row-major matrices with <C> columns
1586 * and <R> rows, the matrix is stored identically to a row of <S>*<R>
1587 * row vectors with <C> components each, according to rule (4).
1589 if (this->without_array()->is_matrix()) {
1590 const struct glsl_type
*element_type
;
1591 const struct glsl_type
*vec_type
;
1592 unsigned int array_len
;
1594 if (this->is_array()) {
1595 element_type
= this->without_array();
1596 array_len
= this->arrays_of_arrays_size();
1598 element_type
= this;
1603 vec_type
= get_instance(element_type
->base_type
,
1604 element_type
->matrix_columns
, 1);
1606 array_len
*= element_type
->vector_elements
;
1608 vec_type
= get_instance(element_type
->base_type
,
1609 element_type
->vector_elements
, 1);
1610 array_len
*= element_type
->matrix_columns
;
1612 const glsl_type
*array_type
= glsl_type::get_array_instance(vec_type
,
1615 return array_type
->std140_size(false);
1618 /* (4) If the member is an array of scalars or vectors, the base alignment
1619 * and array stride are set to match the base alignment of a single
1620 * array element, according to rules (1), (2), and (3), and rounded up
1621 * to the base alignment of a vec4. The array may have padding at the
1622 * end; the base offset of the member following the array is rounded up
1623 * to the next multiple of the base alignment.
1625 * (10) If the member is an array of <S> structures, the <S> elements of
1626 * the array are laid out in order, according to rule (9).
1628 if (this->is_array()) {
1629 if (this->without_array()->is_record()) {
1630 return this->arrays_of_arrays_size() *
1631 this->without_array()->std140_size(row_major
);
1633 unsigned element_base_align
=
1634 this->without_array()->std140_base_alignment(row_major
);
1635 return this->arrays_of_arrays_size() * MAX2(element_base_align
, 16);
1639 /* (9) If the member is a structure, the base alignment of the
1640 * structure is <N>, where <N> is the largest base alignment
1641 * value of any of its members, and rounded up to the base
1642 * alignment of a vec4. The individual members of this
1643 * sub-structure are then assigned offsets by applying this set
1644 * of rules recursively, where the base offset of the first
1645 * member of the sub-structure is equal to the aligned offset
1646 * of the structure. The structure may have padding at the end;
1647 * the base offset of the member following the sub-structure is
1648 * rounded up to the next multiple of the base alignment of the
1651 if (this->is_record() || this->is_interface()) {
1653 unsigned max_align
= 0;
1655 for (unsigned i
= 0; i
< this->length
; i
++) {
1656 bool field_row_major
= row_major
;
1657 const enum glsl_matrix_layout matrix_layout
=
1658 glsl_matrix_layout(this->fields
.structure
[i
].matrix_layout
);
1659 if (matrix_layout
== GLSL_MATRIX_LAYOUT_ROW_MAJOR
) {
1660 field_row_major
= true;
1661 } else if (matrix_layout
== GLSL_MATRIX_LAYOUT_COLUMN_MAJOR
) {
1662 field_row_major
= false;
1665 const struct glsl_type
*field_type
= this->fields
.structure
[i
].type
;
1666 unsigned align
= field_type
->std140_base_alignment(field_row_major
);
1668 /* Ignore unsized arrays when calculating size */
1669 if (field_type
->is_unsized_array())
1672 size
= glsl_align(size
, align
);
1673 size
+= field_type
->std140_size(field_row_major
);
1675 max_align
= MAX2(align
, max_align
);
1677 if (field_type
->is_record() && (i
+ 1 < this->length
))
1678 size
= glsl_align(size
, 16);
1680 size
= glsl_align(size
, MAX2(max_align
, 16));
1684 assert(!"not reached");
1689 glsl_type::std430_base_alignment(bool row_major
) const
1692 unsigned N
= is_double() ? 8 : 4;
1694 /* (1) If the member is a scalar consuming <N> basic machine units, the
1695 * base alignment is <N>.
1697 * (2) If the member is a two- or four-component vector with components
1698 * consuming <N> basic machine units, the base alignment is 2<N> or
1699 * 4<N>, respectively.
1701 * (3) If the member is a three-component vector with components consuming
1702 * <N> basic machine units, the base alignment is 4<N>.
1704 if (this->is_scalar() || this->is_vector()) {
1705 switch (this->vector_elements
) {
1716 /* OpenGL 4.30 spec, section 7.6.2.2 "Standard Uniform Block Layout":
1718 * "When using the std430 storage layout, shader storage blocks will be
1719 * laid out in buffer storage identically to uniform and shader storage
1720 * blocks using the std140 layout, except that the base alignment and
1721 * stride of arrays of scalars and vectors in rule 4 and of structures
1722 * in rule 9 are not rounded up a multiple of the base alignment of a vec4.
1725 /* (1) If the member is a scalar consuming <N> basic machine units, the
1726 * base alignment is <N>.
1728 * (2) If the member is a two- or four-component vector with components
1729 * consuming <N> basic machine units, the base alignment is 2<N> or
1730 * 4<N>, respectively.
1732 * (3) If the member is a three-component vector with components consuming
1733 * <N> basic machine units, the base alignment is 4<N>.
1735 if (this->is_array())
1736 return this->fields
.array
->std430_base_alignment(row_major
);
1738 /* (5) If the member is a column-major matrix with <C> columns and
1739 * <R> rows, the matrix is stored identically to an array of
1740 * <C> column vectors with <R> components each, according to
1743 * (7) If the member is a row-major matrix with <C> columns and <R>
1744 * rows, the matrix is stored identically to an array of <R>
1745 * row vectors with <C> components each, according to rule (4).
1747 if (this->is_matrix()) {
1748 const struct glsl_type
*vec_type
, *array_type
;
1749 int c
= this->matrix_columns
;
1750 int r
= this->vector_elements
;
1753 vec_type
= get_instance(base_type
, c
, 1);
1754 array_type
= glsl_type::get_array_instance(vec_type
, r
);
1756 vec_type
= get_instance(base_type
, r
, 1);
1757 array_type
= glsl_type::get_array_instance(vec_type
, c
);
1760 return array_type
->std430_base_alignment(false);
1763 /* (9) If the member is a structure, the base alignment of the
1764 * structure is <N>, where <N> is the largest base alignment
1765 * value of any of its members, and rounded up to the base
1766 * alignment of a vec4. The individual members of this
1767 * sub-structure are then assigned offsets by applying this set
1768 * of rules recursively, where the base offset of the first
1769 * member of the sub-structure is equal to the aligned offset
1770 * of the structure. The structure may have padding at the end;
1771 * the base offset of the member following the sub-structure is
1772 * rounded up to the next multiple of the base alignment of the
1775 if (this->is_record()) {
1776 unsigned base_alignment
= 0;
1777 for (unsigned i
= 0; i
< this->length
; i
++) {
1778 bool field_row_major
= row_major
;
1779 const enum glsl_matrix_layout matrix_layout
=
1780 glsl_matrix_layout(this->fields
.structure
[i
].matrix_layout
);
1781 if (matrix_layout
== GLSL_MATRIX_LAYOUT_ROW_MAJOR
) {
1782 field_row_major
= true;
1783 } else if (matrix_layout
== GLSL_MATRIX_LAYOUT_COLUMN_MAJOR
) {
1784 field_row_major
= false;
1787 const struct glsl_type
*field_type
= this->fields
.structure
[i
].type
;
1788 base_alignment
= MAX2(base_alignment
,
1789 field_type
->std430_base_alignment(field_row_major
));
1791 assert(base_alignment
> 0);
1792 return base_alignment
;
1794 assert(!"not reached");
1799 glsl_type::std430_array_stride(bool row_major
) const
1801 unsigned N
= is_double() ? 8 : 4;
1803 /* Notice that the array stride of a vec3 is not 3 * N but 4 * N.
1804 * See OpenGL 4.30 spec, section 7.6.2.2 "Standard Uniform Block Layout"
1806 * (3) If the member is a three-component vector with components consuming
1807 * <N> basic machine units, the base alignment is 4<N>.
1809 if (this->is_vector() && this->vector_elements
== 3)
1812 /* By default use std430_size(row_major) */
1813 return this->std430_size(row_major
);
1817 glsl_type::std430_size(bool row_major
) const
1819 unsigned N
= is_double() ? 8 : 4;
1821 /* OpenGL 4.30 spec, section 7.6.2.2 "Standard Uniform Block Layout":
1823 * "When using the std430 storage layout, shader storage blocks will be
1824 * laid out in buffer storage identically to uniform and shader storage
1825 * blocks using the std140 layout, except that the base alignment and
1826 * stride of arrays of scalars and vectors in rule 4 and of structures
1827 * in rule 9 are not rounded up a multiple of the base alignment of a vec4.
1829 if (this->is_scalar() || this->is_vector())
1830 return this->vector_elements
* N
;
1832 if (this->without_array()->is_matrix()) {
1833 const struct glsl_type
*element_type
;
1834 const struct glsl_type
*vec_type
;
1835 unsigned int array_len
;
1837 if (this->is_array()) {
1838 element_type
= this->without_array();
1839 array_len
= this->arrays_of_arrays_size();
1841 element_type
= this;
1846 vec_type
= get_instance(element_type
->base_type
,
1847 element_type
->matrix_columns
, 1);
1849 array_len
*= element_type
->vector_elements
;
1851 vec_type
= get_instance(element_type
->base_type
,
1852 element_type
->vector_elements
, 1);
1853 array_len
*= element_type
->matrix_columns
;
1855 const glsl_type
*array_type
= glsl_type::get_array_instance(vec_type
,
1858 return array_type
->std430_size(false);
1861 if (this->is_array()) {
1862 if (this->without_array()->is_record())
1863 return this->arrays_of_arrays_size() *
1864 this->without_array()->std430_size(row_major
);
1866 return this->arrays_of_arrays_size() *
1867 this->without_array()->std430_base_alignment(row_major
);
1870 if (this->is_record() || this->is_interface()) {
1872 unsigned max_align
= 0;
1874 for (unsigned i
= 0; i
< this->length
; i
++) {
1875 bool field_row_major
= row_major
;
1876 const enum glsl_matrix_layout matrix_layout
=
1877 glsl_matrix_layout(this->fields
.structure
[i
].matrix_layout
);
1878 if (matrix_layout
== GLSL_MATRIX_LAYOUT_ROW_MAJOR
) {
1879 field_row_major
= true;
1880 } else if (matrix_layout
== GLSL_MATRIX_LAYOUT_COLUMN_MAJOR
) {
1881 field_row_major
= false;
1884 const struct glsl_type
*field_type
= this->fields
.structure
[i
].type
;
1885 unsigned align
= field_type
->std430_base_alignment(field_row_major
);
1886 size
= glsl_align(size
, align
);
1887 size
+= field_type
->std430_size(field_row_major
);
1889 max_align
= MAX2(align
, max_align
);
1891 size
= glsl_align(size
, max_align
);
1895 assert(!"not reached");
1900 glsl_type::count_attribute_slots(bool is_vertex_input
) const
1902 /* From page 31 (page 37 of the PDF) of the GLSL 1.50 spec:
1904 * "A scalar input counts the same amount against this limit as a vec4,
1905 * so applications may want to consider packing groups of four
1906 * unrelated float inputs together into a vector to better utilize the
1907 * capabilities of the underlying hardware. A matrix input will use up
1908 * multiple locations. The number of locations used will equal the
1909 * number of columns in the matrix."
1911 * The spec does not explicitly say how arrays are counted. However, it
1912 * should be safe to assume the total number of slots consumed by an array
1913 * is the number of entries in the array multiplied by the number of slots
1914 * consumed by a single element of the array.
1916 * The spec says nothing about how structs are counted, because vertex
1917 * attributes are not allowed to be (or contain) structs. However, Mesa
1918 * allows varying structs, the number of varying slots taken up by a
1919 * varying struct is simply equal to the sum of the number of slots taken
1920 * up by each element.
1922 * Doubles are counted different depending on whether they are vertex
1923 * inputs or everything else. Vertex inputs from ARB_vertex_attrib_64bit
1924 * take one location no matter what size they are, otherwise dvec3/4
1925 * take two locations.
1927 switch (this->base_type
) {
1928 case GLSL_TYPE_UINT
:
1930 case GLSL_TYPE_FLOAT
:
1931 case GLSL_TYPE_BOOL
:
1932 return this->matrix_columns
;
1933 case GLSL_TYPE_DOUBLE
:
1934 if (this->vector_elements
> 2 && !is_vertex_input
)
1935 return this->matrix_columns
* 2;
1937 return this->matrix_columns
;
1938 case GLSL_TYPE_STRUCT
:
1939 case GLSL_TYPE_INTERFACE
: {
1942 for (unsigned i
= 0; i
< this->length
; i
++)
1943 size
+= this->fields
.structure
[i
].type
->count_attribute_slots(is_vertex_input
);
1948 case GLSL_TYPE_ARRAY
:
1949 return this->length
* this->fields
.array
->count_attribute_slots(is_vertex_input
);
1951 case GLSL_TYPE_FUNCTION
:
1952 case GLSL_TYPE_SAMPLER
:
1953 case GLSL_TYPE_IMAGE
:
1954 case GLSL_TYPE_ATOMIC_UINT
:
1955 case GLSL_TYPE_VOID
:
1956 case GLSL_TYPE_SUBROUTINE
:
1957 case GLSL_TYPE_ERROR
:
1961 assert(!"Unexpected type in count_attribute_slots()");
1967 glsl_type::coordinate_components() const
1971 switch (sampler_dimensionality
) {
1972 case GLSL_SAMPLER_DIM_1D
:
1973 case GLSL_SAMPLER_DIM_BUF
:
1976 case GLSL_SAMPLER_DIM_2D
:
1977 case GLSL_SAMPLER_DIM_RECT
:
1978 case GLSL_SAMPLER_DIM_MS
:
1979 case GLSL_SAMPLER_DIM_EXTERNAL
:
1982 case GLSL_SAMPLER_DIM_3D
:
1983 case GLSL_SAMPLER_DIM_CUBE
:
1987 assert(!"Should not get here.");
1992 /* Array textures need an additional component for the array index, except
1993 * for cubemap array images that behave like a 2D array of interleaved
1996 if (sampler_array
&&
1997 !(base_type
== GLSL_TYPE_IMAGE
&&
1998 sampler_dimensionality
== GLSL_SAMPLER_DIM_CUBE
))
2005 * Declarations of type flyweights (glsl_type::_foo_type) and
2006 * convenience pointers (glsl_type::foo_type).
2009 #define DECL_TYPE(NAME, ...) \
2010 const glsl_type glsl_type::_##NAME##_type = glsl_type(__VA_ARGS__, #NAME); \
2011 const glsl_type *const glsl_type::NAME##_type = &glsl_type::_##NAME##_type;
2013 #define STRUCT_TYPE(NAME)
2015 #include "compiler/builtin_type_macros.h"