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
].xfb_buffer
= fields
[i
].xfb_buffer
;
136 this->fields
.structure
[i
].xfb_stride
= fields
[i
].xfb_stride
;
139 mtx_unlock(&glsl_type::mutex
);
142 glsl_type::glsl_type(const glsl_struct_field
*fields
, unsigned num_fields
,
143 enum glsl_interface_packing packing
, const char *name
) :
145 base_type(GLSL_TYPE_INTERFACE
),
146 sampler_dimensionality(0), sampler_shadow(0), sampler_array(0),
147 sampled_type(0), interface_packing((unsigned) packing
),
148 vector_elements(0), matrix_columns(0),
153 mtx_lock(&glsl_type::mutex
);
155 init_ralloc_type_ctx();
156 assert(name
!= NULL
);
157 this->name
= ralloc_strdup(this->mem_ctx
, name
);
158 this->fields
.structure
= ralloc_array(this->mem_ctx
,
159 glsl_struct_field
, length
);
160 for (i
= 0; i
< length
; i
++) {
161 this->fields
.structure
[i
].type
= fields
[i
].type
;
162 this->fields
.structure
[i
].name
= ralloc_strdup(this->fields
.structure
,
164 this->fields
.structure
[i
].location
= fields
[i
].location
;
165 this->fields
.structure
[i
].offset
= fields
[i
].offset
;
166 this->fields
.structure
[i
].interpolation
= fields
[i
].interpolation
;
167 this->fields
.structure
[i
].centroid
= fields
[i
].centroid
;
168 this->fields
.structure
[i
].sample
= fields
[i
].sample
;
169 this->fields
.structure
[i
].matrix_layout
= fields
[i
].matrix_layout
;
170 this->fields
.structure
[i
].patch
= fields
[i
].patch
;
171 this->fields
.structure
[i
].image_read_only
= fields
[i
].image_read_only
;
172 this->fields
.structure
[i
].image_write_only
= fields
[i
].image_write_only
;
173 this->fields
.structure
[i
].image_coherent
= fields
[i
].image_coherent
;
174 this->fields
.structure
[i
].image_volatile
= fields
[i
].image_volatile
;
175 this->fields
.structure
[i
].image_restrict
= fields
[i
].image_restrict
;
176 this->fields
.structure
[i
].precision
= fields
[i
].precision
;
177 this->fields
.structure
[i
].xfb_buffer
= fields
[i
].xfb_buffer
;
178 this->fields
.structure
[i
].xfb_stride
= fields
[i
].xfb_stride
;
181 mtx_unlock(&glsl_type::mutex
);
184 glsl_type::glsl_type(const glsl_type
*return_type
,
185 const glsl_function_param
*params
, unsigned num_params
) :
187 base_type(GLSL_TYPE_FUNCTION
),
188 sampler_dimensionality(0), sampler_shadow(0), sampler_array(0),
189 sampled_type(0), interface_packing(0),
190 vector_elements(0), matrix_columns(0),
195 mtx_lock(&glsl_type::mutex
);
197 init_ralloc_type_ctx();
199 this->fields
.parameters
= rzalloc_array(this->mem_ctx
,
200 glsl_function_param
, num_params
+ 1);
202 /* We store the return type as the first parameter */
203 this->fields
.parameters
[0].type
= return_type
;
204 this->fields
.parameters
[0].in
= false;
205 this->fields
.parameters
[0].out
= true;
207 /* We store the i'th parameter in slot i+1 */
208 for (i
= 0; i
< length
; i
++) {
209 this->fields
.parameters
[i
+ 1].type
= params
[i
].type
;
210 this->fields
.parameters
[i
+ 1].in
= params
[i
].in
;
211 this->fields
.parameters
[i
+ 1].out
= params
[i
].out
;
214 mtx_unlock(&glsl_type::mutex
);
217 glsl_type::glsl_type(const char *subroutine_name
) :
219 base_type(GLSL_TYPE_SUBROUTINE
),
220 sampler_dimensionality(0), sampler_shadow(0), sampler_array(0),
221 sampled_type(0), interface_packing(0),
222 vector_elements(1), matrix_columns(1),
225 mtx_lock(&glsl_type::mutex
);
227 init_ralloc_type_ctx();
228 assert(subroutine_name
!= NULL
);
229 this->name
= ralloc_strdup(this->mem_ctx
, subroutine_name
);
230 mtx_unlock(&glsl_type::mutex
);
234 glsl_type::contains_sampler() const
236 if (this->is_array()) {
237 return this->fields
.array
->contains_sampler();
238 } else if (this->is_record()) {
239 for (unsigned int i
= 0; i
< this->length
; i
++) {
240 if (this->fields
.structure
[i
].type
->contains_sampler())
245 return this->is_sampler();
251 glsl_type::contains_integer() const
253 if (this->is_array()) {
254 return this->fields
.array
->contains_integer();
255 } else if (this->is_record()) {
256 for (unsigned int i
= 0; i
< this->length
; i
++) {
257 if (this->fields
.structure
[i
].type
->contains_integer())
262 return this->is_integer();
267 glsl_type::contains_double() const
269 if (this->is_array()) {
270 return this->fields
.array
->contains_double();
271 } else if (this->is_record()) {
272 for (unsigned int i
= 0; i
< this->length
; i
++) {
273 if (this->fields
.structure
[i
].type
->contains_double())
278 return this->is_double();
283 glsl_type::contains_opaque() const {
285 case GLSL_TYPE_SAMPLER
:
286 case GLSL_TYPE_IMAGE
:
287 case GLSL_TYPE_ATOMIC_UINT
:
289 case GLSL_TYPE_ARRAY
:
290 return fields
.array
->contains_opaque();
291 case GLSL_TYPE_STRUCT
:
292 for (unsigned int i
= 0; i
< length
; i
++) {
293 if (fields
.structure
[i
].type
->contains_opaque())
303 glsl_type::contains_subroutine() const
305 if (this->is_array()) {
306 return this->fields
.array
->contains_subroutine();
307 } else if (this->is_record()) {
308 for (unsigned int i
= 0; i
< this->length
; i
++) {
309 if (this->fields
.structure
[i
].type
->contains_subroutine())
314 return this->is_subroutine();
319 glsl_type::sampler_index() const
321 const glsl_type
*const t
= (this->is_array()) ? this->fields
.array
: this;
323 assert(t
->is_sampler());
325 switch (t
->sampler_dimensionality
) {
326 case GLSL_SAMPLER_DIM_1D
:
327 return (t
->sampler_array
) ? TEXTURE_1D_ARRAY_INDEX
: TEXTURE_1D_INDEX
;
328 case GLSL_SAMPLER_DIM_2D
:
329 return (t
->sampler_array
) ? TEXTURE_2D_ARRAY_INDEX
: TEXTURE_2D_INDEX
;
330 case GLSL_SAMPLER_DIM_3D
:
331 return TEXTURE_3D_INDEX
;
332 case GLSL_SAMPLER_DIM_CUBE
:
333 return (t
->sampler_array
) ? TEXTURE_CUBE_ARRAY_INDEX
: TEXTURE_CUBE_INDEX
;
334 case GLSL_SAMPLER_DIM_RECT
:
335 return TEXTURE_RECT_INDEX
;
336 case GLSL_SAMPLER_DIM_BUF
:
337 return TEXTURE_BUFFER_INDEX
;
338 case GLSL_SAMPLER_DIM_EXTERNAL
:
339 return TEXTURE_EXTERNAL_INDEX
;
340 case GLSL_SAMPLER_DIM_MS
:
341 return (t
->sampler_array
) ? TEXTURE_2D_MULTISAMPLE_ARRAY_INDEX
: TEXTURE_2D_MULTISAMPLE_INDEX
;
343 assert(!"Should not get here.");
344 return TEXTURE_BUFFER_INDEX
;
349 glsl_type::contains_image() const
351 if (this->is_array()) {
352 return this->fields
.array
->contains_image();
353 } else if (this->is_record()) {
354 for (unsigned int i
= 0; i
< this->length
; i
++) {
355 if (this->fields
.structure
[i
].type
->contains_image())
360 return this->is_image();
364 const glsl_type
*glsl_type::get_base_type() const
371 case GLSL_TYPE_FLOAT
:
373 case GLSL_TYPE_DOUBLE
:
383 const glsl_type
*glsl_type::get_scalar_type() const
385 const glsl_type
*type
= this;
388 while (type
->base_type
== GLSL_TYPE_ARRAY
)
389 type
= type
->fields
.array
;
391 /* Handle vectors and matrices */
392 switch (type
->base_type
) {
397 case GLSL_TYPE_FLOAT
:
399 case GLSL_TYPE_DOUBLE
:
404 /* Handle everything else */
411 _mesa_glsl_release_types(void)
413 /* Should only be called during atexit (either when unloading shared
414 * object, or if process terminates), so no mutex-locking should be
417 if (glsl_type::array_types
!= NULL
) {
418 _mesa_hash_table_destroy(glsl_type::array_types
, NULL
);
419 glsl_type::array_types
= NULL
;
422 if (glsl_type::record_types
!= NULL
) {
423 _mesa_hash_table_destroy(glsl_type::record_types
, NULL
);
424 glsl_type::record_types
= NULL
;
427 if (glsl_type::interface_types
!= NULL
) {
428 _mesa_hash_table_destroy(glsl_type::interface_types
, NULL
);
429 glsl_type::interface_types
= NULL
;
434 glsl_type::glsl_type(const glsl_type
*array
, unsigned length
) :
435 base_type(GLSL_TYPE_ARRAY
),
436 sampler_dimensionality(0), sampler_shadow(0), sampler_array(0),
437 sampled_type(0), interface_packing(0),
438 vector_elements(0), matrix_columns(0),
439 length(length
), name(NULL
)
441 this->fields
.array
= array
;
442 /* Inherit the gl type of the base. The GL type is used for
443 * uniform/statevar handling in Mesa and the arrayness of the type
444 * is represented by the size rather than the type.
446 this->gl_type
= array
->gl_type
;
448 /* Allow a maximum of 10 characters for the array size. This is enough
449 * for 32-bits of ~0. The extra 3 are for the '[', ']', and terminating
452 const unsigned name_length
= strlen(array
->name
) + 10 + 3;
454 mtx_lock(&glsl_type::mutex
);
455 char *const n
= (char *) ralloc_size(this->mem_ctx
, name_length
);
456 mtx_unlock(&glsl_type::mutex
);
459 snprintf(n
, name_length
, "%s[]", array
->name
);
461 /* insert outermost dimensions in the correct spot
462 * otherwise the dimension order will be backwards
464 const char *pos
= strchr(array
->name
, '[');
466 int idx
= pos
- array
->name
;
467 snprintf(n
, idx
+1, "%s", array
->name
);
468 snprintf(n
+ idx
, name_length
- idx
, "[%u]%s",
469 length
, array
->name
+ idx
);
471 snprintf(n
, name_length
, "%s[%u]", array
->name
, length
);
480 glsl_type::vec(unsigned components
)
482 if (components
== 0 || components
> 4)
485 static const glsl_type
*const ts
[] = {
486 float_type
, vec2_type
, vec3_type
, vec4_type
488 return ts
[components
- 1];
492 glsl_type::dvec(unsigned components
)
494 if (components
== 0 || components
> 4)
497 static const glsl_type
*const ts
[] = {
498 double_type
, dvec2_type
, dvec3_type
, dvec4_type
500 return ts
[components
- 1];
504 glsl_type::ivec(unsigned components
)
506 if (components
== 0 || components
> 4)
509 static const glsl_type
*const ts
[] = {
510 int_type
, ivec2_type
, ivec3_type
, ivec4_type
512 return ts
[components
- 1];
517 glsl_type::uvec(unsigned components
)
519 if (components
== 0 || components
> 4)
522 static const glsl_type
*const ts
[] = {
523 uint_type
, uvec2_type
, uvec3_type
, uvec4_type
525 return ts
[components
- 1];
530 glsl_type::bvec(unsigned components
)
532 if (components
== 0 || components
> 4)
535 static const glsl_type
*const ts
[] = {
536 bool_type
, bvec2_type
, bvec3_type
, bvec4_type
538 return ts
[components
- 1];
543 glsl_type::get_instance(unsigned base_type
, unsigned rows
, unsigned columns
)
545 if (base_type
== GLSL_TYPE_VOID
)
548 if ((rows
< 1) || (rows
> 4) || (columns
< 1) || (columns
> 4))
551 /* Treat GLSL vectors as Nx1 matrices.
559 case GLSL_TYPE_FLOAT
:
561 case GLSL_TYPE_DOUBLE
:
569 if ((base_type
!= GLSL_TYPE_FLOAT
&& base_type
!= GLSL_TYPE_DOUBLE
) || (rows
== 1))
572 /* GLSL matrix types are named mat{COLUMNS}x{ROWS}. Only the following
573 * combinations are valid:
581 #define IDX(c,r) (((c-1)*3) + (r-1))
583 if (base_type
== GLSL_TYPE_DOUBLE
) {
584 switch (IDX(columns
, rows
)) {
585 case IDX(2,2): return dmat2_type
;
586 case IDX(2,3): return dmat2x3_type
;
587 case IDX(2,4): return dmat2x4_type
;
588 case IDX(3,2): return dmat3x2_type
;
589 case IDX(3,3): return dmat3_type
;
590 case IDX(3,4): return dmat3x4_type
;
591 case IDX(4,2): return dmat4x2_type
;
592 case IDX(4,3): return dmat4x3_type
;
593 case IDX(4,4): return dmat4_type
;
594 default: return error_type
;
597 switch (IDX(columns
, rows
)) {
598 case IDX(2,2): return mat2_type
;
599 case IDX(2,3): return mat2x3_type
;
600 case IDX(2,4): return mat2x4_type
;
601 case IDX(3,2): return mat3x2_type
;
602 case IDX(3,3): return mat3_type
;
603 case IDX(3,4): return mat3x4_type
;
604 case IDX(4,2): return mat4x2_type
;
605 case IDX(4,3): return mat4x3_type
;
606 case IDX(4,4): return mat4_type
;
607 default: return error_type
;
612 assert(!"Should not get here.");
617 glsl_type::get_sampler_instance(enum glsl_sampler_dim dim
,
623 case GLSL_TYPE_FLOAT
:
625 case GLSL_SAMPLER_DIM_1D
:
627 return (array
? sampler1DArrayShadow_type
: sampler1DShadow_type
);
629 return (array
? sampler1DArray_type
: sampler1D_type
);
630 case GLSL_SAMPLER_DIM_2D
:
632 return (array
? sampler2DArrayShadow_type
: sampler2DShadow_type
);
634 return (array
? sampler2DArray_type
: sampler2D_type
);
635 case GLSL_SAMPLER_DIM_3D
:
639 return sampler3D_type
;
640 case GLSL_SAMPLER_DIM_CUBE
:
642 return (array
? samplerCubeArrayShadow_type
: samplerCubeShadow_type
);
644 return (array
? samplerCubeArray_type
: samplerCube_type
);
645 case GLSL_SAMPLER_DIM_RECT
:
649 return sampler2DRectShadow_type
;
651 return sampler2DRect_type
;
652 case GLSL_SAMPLER_DIM_BUF
:
656 return samplerBuffer_type
;
657 case GLSL_SAMPLER_DIM_MS
:
660 return (array
? sampler2DMSArray_type
: sampler2DMS_type
);
661 case GLSL_SAMPLER_DIM_EXTERNAL
:
665 return samplerExternalOES_type
;
671 case GLSL_SAMPLER_DIM_1D
:
672 return (array
? isampler1DArray_type
: isampler1D_type
);
673 case GLSL_SAMPLER_DIM_2D
:
674 return (array
? isampler2DArray_type
: isampler2D_type
);
675 case GLSL_SAMPLER_DIM_3D
:
678 return isampler3D_type
;
679 case GLSL_SAMPLER_DIM_CUBE
:
680 return (array
? isamplerCubeArray_type
: isamplerCube_type
);
681 case GLSL_SAMPLER_DIM_RECT
:
684 return isampler2DRect_type
;
685 case GLSL_SAMPLER_DIM_BUF
:
688 return isamplerBuffer_type
;
689 case GLSL_SAMPLER_DIM_MS
:
690 return (array
? isampler2DMSArray_type
: isampler2DMS_type
);
691 case GLSL_SAMPLER_DIM_EXTERNAL
:
698 case GLSL_SAMPLER_DIM_1D
:
699 return (array
? usampler1DArray_type
: usampler1D_type
);
700 case GLSL_SAMPLER_DIM_2D
:
701 return (array
? usampler2DArray_type
: usampler2D_type
);
702 case GLSL_SAMPLER_DIM_3D
:
705 return usampler3D_type
;
706 case GLSL_SAMPLER_DIM_CUBE
:
707 return (array
? usamplerCubeArray_type
: usamplerCube_type
);
708 case GLSL_SAMPLER_DIM_RECT
:
711 return usampler2DRect_type
;
712 case GLSL_SAMPLER_DIM_BUF
:
715 return usamplerBuffer_type
;
716 case GLSL_SAMPLER_DIM_MS
:
717 return (array
? usampler2DMSArray_type
: usampler2DMS_type
);
718 case GLSL_SAMPLER_DIM_EXTERNAL
:
725 unreachable("switch statement above should be complete");
729 glsl_type::get_image_instance(enum glsl_sampler_dim dim
,
730 bool array
, glsl_base_type type
)
733 case GLSL_TYPE_FLOAT
:
735 case GLSL_SAMPLER_DIM_1D
:
736 return (array
? image1DArray_type
: image1D_type
);
737 case GLSL_SAMPLER_DIM_2D
:
738 return (array
? image2DArray_type
: image2D_type
);
739 case GLSL_SAMPLER_DIM_3D
:
741 case GLSL_SAMPLER_DIM_CUBE
:
742 return (array
? imageCubeArray_type
: imageCube_type
);
743 case GLSL_SAMPLER_DIM_RECT
:
747 return image2DRect_type
;
748 case GLSL_SAMPLER_DIM_BUF
:
752 return imageBuffer_type
;
753 case GLSL_SAMPLER_DIM_MS
:
754 return (array
? image2DMSArray_type
: image2DMS_type
);
755 case GLSL_SAMPLER_DIM_EXTERNAL
:
760 case GLSL_SAMPLER_DIM_1D
:
761 return (array
? iimage1DArray_type
: iimage1D_type
);
762 case GLSL_SAMPLER_DIM_2D
:
763 return (array
? iimage2DArray_type
: iimage2D_type
);
764 case GLSL_SAMPLER_DIM_3D
:
767 return iimage3D_type
;
768 case GLSL_SAMPLER_DIM_CUBE
:
769 return (array
? iimageCubeArray_type
: iimageCube_type
);
770 case GLSL_SAMPLER_DIM_RECT
:
773 return iimage2DRect_type
;
774 case GLSL_SAMPLER_DIM_BUF
:
777 return iimageBuffer_type
;
778 case GLSL_SAMPLER_DIM_MS
:
779 return (array
? iimage2DMSArray_type
: iimage2DMS_type
);
780 case GLSL_SAMPLER_DIM_EXTERNAL
:
785 case GLSL_SAMPLER_DIM_1D
:
786 return (array
? uimage1DArray_type
: uimage1D_type
);
787 case GLSL_SAMPLER_DIM_2D
:
788 return (array
? uimage2DArray_type
: uimage2D_type
);
789 case GLSL_SAMPLER_DIM_3D
:
792 return uimage3D_type
;
793 case GLSL_SAMPLER_DIM_CUBE
:
794 return (array
? uimageCubeArray_type
: uimageCube_type
);
795 case GLSL_SAMPLER_DIM_RECT
:
798 return uimage2DRect_type
;
799 case GLSL_SAMPLER_DIM_BUF
:
802 return uimageBuffer_type
;
803 case GLSL_SAMPLER_DIM_MS
:
804 return (array
? uimage2DMSArray_type
: uimage2DMS_type
);
805 case GLSL_SAMPLER_DIM_EXTERNAL
:
812 unreachable("switch statement above should be complete");
816 glsl_type::get_array_instance(const glsl_type
*base
, unsigned array_size
)
818 /* Generate a name using the base type pointer in the key. This is
819 * done because the name of the base type may not be unique across
820 * shaders. For example, two shaders may have different record types
824 snprintf(key
, sizeof(key
), "%p[%u]", (void *) base
, array_size
);
826 mtx_lock(&glsl_type::mutex
);
828 if (array_types
== NULL
) {
829 array_types
= _mesa_hash_table_create(NULL
, _mesa_key_hash_string
,
830 _mesa_key_string_equal
);
833 const struct hash_entry
*entry
= _mesa_hash_table_search(array_types
, key
);
835 mtx_unlock(&glsl_type::mutex
);
836 const glsl_type
*t
= new glsl_type(base
, array_size
);
837 mtx_lock(&glsl_type::mutex
);
839 entry
= _mesa_hash_table_insert(array_types
,
840 ralloc_strdup(mem_ctx
, key
),
844 assert(((glsl_type
*) entry
->data
)->base_type
== GLSL_TYPE_ARRAY
);
845 assert(((glsl_type
*) entry
->data
)->length
== array_size
);
846 assert(((glsl_type
*) entry
->data
)->fields
.array
== base
);
848 mtx_unlock(&glsl_type::mutex
);
850 return (glsl_type
*) entry
->data
;
855 glsl_type::record_compare(const glsl_type
*b
) const
857 if (this->length
!= b
->length
)
860 if (this->interface_packing
!= b
->interface_packing
)
863 /* From the GLSL 4.20 specification (Sec 4.2):
865 * "Structures must have the same name, sequence of type names, and
866 * type definitions, and field names to be considered the same type."
868 * GLSL ES behaves the same (Ver 1.00 Sec 4.2.4, Ver 3.00 Sec 4.2.5).
870 * Note that we cannot force type name check when comparing unnamed
871 * structure types, these have a unique name assigned during parsing.
873 if (!this->is_anonymous() && !b
->is_anonymous())
874 if (strcmp(this->name
, b
->name
) != 0)
877 for (unsigned i
= 0; i
< this->length
; i
++) {
878 if (this->fields
.structure
[i
].type
!= b
->fields
.structure
[i
].type
)
880 if (strcmp(this->fields
.structure
[i
].name
,
881 b
->fields
.structure
[i
].name
) != 0)
883 if (this->fields
.structure
[i
].matrix_layout
884 != b
->fields
.structure
[i
].matrix_layout
)
886 if (this->fields
.structure
[i
].location
887 != b
->fields
.structure
[i
].location
)
889 if (this->fields
.structure
[i
].offset
890 != b
->fields
.structure
[i
].offset
)
892 if (this->fields
.structure
[i
].interpolation
893 != b
->fields
.structure
[i
].interpolation
)
895 if (this->fields
.structure
[i
].centroid
896 != b
->fields
.structure
[i
].centroid
)
898 if (this->fields
.structure
[i
].sample
899 != b
->fields
.structure
[i
].sample
)
901 if (this->fields
.structure
[i
].patch
902 != b
->fields
.structure
[i
].patch
)
904 if (this->fields
.structure
[i
].image_read_only
905 != b
->fields
.structure
[i
].image_read_only
)
907 if (this->fields
.structure
[i
].image_write_only
908 != b
->fields
.structure
[i
].image_write_only
)
910 if (this->fields
.structure
[i
].image_coherent
911 != b
->fields
.structure
[i
].image_coherent
)
913 if (this->fields
.structure
[i
].image_volatile
914 != b
->fields
.structure
[i
].image_volatile
)
916 if (this->fields
.structure
[i
].image_restrict
917 != b
->fields
.structure
[i
].image_restrict
)
919 if (this->fields
.structure
[i
].precision
920 != b
->fields
.structure
[i
].precision
)
922 if (this->fields
.structure
[i
].xfb_buffer
923 != b
->fields
.structure
[i
].xfb_buffer
)
925 if (this->fields
.structure
[i
].xfb_stride
926 != b
->fields
.structure
[i
].xfb_stride
)
935 glsl_type::record_key_compare(const void *a
, const void *b
)
937 const glsl_type
*const key1
= (glsl_type
*) a
;
938 const glsl_type
*const key2
= (glsl_type
*) b
;
940 return strcmp(key1
->name
, key2
->name
) == 0 && key1
->record_compare(key2
);
945 * Generate an integer hash value for a glsl_type structure type.
948 glsl_type::record_key_hash(const void *a
)
950 const glsl_type
*const key
= (glsl_type
*) a
;
951 uintptr_t hash
= key
->length
;
954 for (unsigned i
= 0; i
< key
->length
; i
++) {
955 /* casting pointer to uintptr_t */
956 hash
= (hash
* 13 ) + (uintptr_t) key
->fields
.structure
[i
].type
;
959 if (sizeof(hash
) == 8)
960 retval
= (hash
& 0xffffffff) ^ ((uint64_t) hash
>> 32);
969 glsl_type::get_record_instance(const glsl_struct_field
*fields
,
973 const glsl_type
key(fields
, num_fields
, name
);
975 mtx_lock(&glsl_type::mutex
);
977 if (record_types
== NULL
) {
978 record_types
= _mesa_hash_table_create(NULL
, record_key_hash
,
982 const struct hash_entry
*entry
= _mesa_hash_table_search(record_types
,
985 mtx_unlock(&glsl_type::mutex
);
986 const glsl_type
*t
= new glsl_type(fields
, num_fields
, name
);
987 mtx_lock(&glsl_type::mutex
);
989 entry
= _mesa_hash_table_insert(record_types
, t
, (void *) t
);
992 assert(((glsl_type
*) entry
->data
)->base_type
== GLSL_TYPE_STRUCT
);
993 assert(((glsl_type
*) entry
->data
)->length
== num_fields
);
994 assert(strcmp(((glsl_type
*) entry
->data
)->name
, name
) == 0);
996 mtx_unlock(&glsl_type::mutex
);
998 return (glsl_type
*) entry
->data
;
1003 glsl_type::get_interface_instance(const glsl_struct_field
*fields
,
1004 unsigned num_fields
,
1005 enum glsl_interface_packing packing
,
1006 const char *block_name
)
1008 const glsl_type
key(fields
, num_fields
, packing
, block_name
);
1010 mtx_lock(&glsl_type::mutex
);
1012 if (interface_types
== NULL
) {
1013 interface_types
= _mesa_hash_table_create(NULL
, record_key_hash
,
1014 record_key_compare
);
1017 const struct hash_entry
*entry
= _mesa_hash_table_search(interface_types
,
1019 if (entry
== NULL
) {
1020 mtx_unlock(&glsl_type::mutex
);
1021 const glsl_type
*t
= new glsl_type(fields
, num_fields
,
1022 packing
, block_name
);
1023 mtx_lock(&glsl_type::mutex
);
1025 entry
= _mesa_hash_table_insert(interface_types
, t
, (void *) t
);
1028 assert(((glsl_type
*) entry
->data
)->base_type
== GLSL_TYPE_INTERFACE
);
1029 assert(((glsl_type
*) entry
->data
)->length
== num_fields
);
1030 assert(strcmp(((glsl_type
*) entry
->data
)->name
, block_name
) == 0);
1032 mtx_unlock(&glsl_type::mutex
);
1034 return (glsl_type
*) entry
->data
;
1038 glsl_type::get_subroutine_instance(const char *subroutine_name
)
1040 const glsl_type
key(subroutine_name
);
1042 mtx_lock(&glsl_type::mutex
);
1044 if (subroutine_types
== NULL
) {
1045 subroutine_types
= _mesa_hash_table_create(NULL
, record_key_hash
,
1046 record_key_compare
);
1049 const struct hash_entry
*entry
= _mesa_hash_table_search(subroutine_types
,
1051 if (entry
== NULL
) {
1052 mtx_unlock(&glsl_type::mutex
);
1053 const glsl_type
*t
= new glsl_type(subroutine_name
);
1054 mtx_lock(&glsl_type::mutex
);
1056 entry
= _mesa_hash_table_insert(subroutine_types
, t
, (void *) t
);
1059 assert(((glsl_type
*) entry
->data
)->base_type
== GLSL_TYPE_SUBROUTINE
);
1060 assert(strcmp(((glsl_type
*) entry
->data
)->name
, subroutine_name
) == 0);
1062 mtx_unlock(&glsl_type::mutex
);
1064 return (glsl_type
*) entry
->data
;
1069 function_key_compare(const void *a
, const void *b
)
1071 const glsl_type
*const key1
= (glsl_type
*) a
;
1072 const glsl_type
*const key2
= (glsl_type
*) b
;
1074 if (key1
->length
!= key2
->length
)
1077 return memcmp(key1
->fields
.parameters
, key2
->fields
.parameters
,
1078 (key1
->length
+ 1) * sizeof(*key1
->fields
.parameters
)) == 0;
1083 function_key_hash(const void *a
)
1085 const glsl_type
*const key
= (glsl_type
*) a
;
1089 size
= snprintf(hash_key
, sizeof(hash_key
), "%08x", key
->length
);
1091 for (unsigned i
= 0; i
< key
->length
; i
++) {
1092 if (size
>= sizeof(hash_key
))
1095 size
+= snprintf(& hash_key
[size
], sizeof(hash_key
) - size
,
1096 "%p", (void *) key
->fields
.structure
[i
].type
);
1099 return _mesa_hash_string(hash_key
);
1103 glsl_type::get_function_instance(const glsl_type
*return_type
,
1104 const glsl_function_param
*params
,
1105 unsigned num_params
)
1107 const glsl_type
key(return_type
, params
, num_params
);
1109 mtx_lock(&glsl_type::mutex
);
1111 if (function_types
== NULL
) {
1112 function_types
= _mesa_hash_table_create(NULL
, function_key_hash
,
1113 function_key_compare
);
1116 struct hash_entry
*entry
= _mesa_hash_table_search(function_types
, &key
);
1117 if (entry
== NULL
) {
1118 mtx_unlock(&glsl_type::mutex
);
1119 const glsl_type
*t
= new glsl_type(return_type
, params
, num_params
);
1120 mtx_lock(&glsl_type::mutex
);
1122 entry
= _mesa_hash_table_insert(function_types
, t
, (void *) t
);
1125 const glsl_type
*t
= (const glsl_type
*)entry
->data
;
1127 assert(t
->base_type
== GLSL_TYPE_FUNCTION
);
1128 assert(t
->length
== num_params
);
1130 mtx_unlock(&glsl_type::mutex
);
1137 glsl_type::get_mul_type(const glsl_type
*type_a
, const glsl_type
*type_b
)
1139 if (type_a
== type_b
) {
1141 } else if (type_a
->is_matrix() && type_b
->is_matrix()) {
1142 /* Matrix multiply. The columns of A must match the rows of B. Given
1143 * the other previously tested constraints, this means the vector type
1144 * of a row from A must be the same as the vector type of a column from
1147 if (type_a
->row_type() == type_b
->column_type()) {
1148 /* The resulting matrix has the number of columns of matrix B and
1149 * the number of rows of matrix A. We get the row count of A by
1150 * looking at the size of a vector that makes up a column. The
1151 * transpose (size of a row) is done for B.
1153 const glsl_type
*const type
=
1154 get_instance(type_a
->base_type
,
1155 type_a
->column_type()->vector_elements
,
1156 type_b
->row_type()->vector_elements
);
1157 assert(type
!= error_type
);
1161 } else if (type_a
->is_matrix()) {
1162 /* A is a matrix and B is a column vector. Columns of A must match
1163 * rows of B. Given the other previously tested constraints, this
1164 * means the vector type of a row from A must be the same as the
1165 * vector the type of B.
1167 if (type_a
->row_type() == type_b
) {
1168 /* The resulting vector has a number of elements equal to
1169 * the number of rows of matrix A. */
1170 const glsl_type
*const type
=
1171 get_instance(type_a
->base_type
,
1172 type_a
->column_type()->vector_elements
,
1174 assert(type
!= error_type
);
1179 assert(type_b
->is_matrix());
1181 /* A is a row vector and B is a matrix. Columns of A must match rows
1182 * of B. Given the other previously tested constraints, this means
1183 * the type of A must be the same as the vector type of a column from
1186 if (type_a
== type_b
->column_type()) {
1187 /* The resulting vector has a number of elements equal to
1188 * the number of columns of matrix B. */
1189 const glsl_type
*const type
=
1190 get_instance(type_a
->base_type
,
1191 type_b
->row_type()->vector_elements
,
1193 assert(type
!= error_type
);
1204 glsl_type::field_type(const char *name
) const
1206 if (this->base_type
!= GLSL_TYPE_STRUCT
1207 && this->base_type
!= GLSL_TYPE_INTERFACE
)
1210 for (unsigned i
= 0; i
< this->length
; i
++) {
1211 if (strcmp(name
, this->fields
.structure
[i
].name
) == 0)
1212 return this->fields
.structure
[i
].type
;
1220 glsl_type::field_index(const char *name
) const
1222 if (this->base_type
!= GLSL_TYPE_STRUCT
1223 && this->base_type
!= GLSL_TYPE_INTERFACE
)
1226 for (unsigned i
= 0; i
< this->length
; i
++) {
1227 if (strcmp(name
, this->fields
.structure
[i
].name
) == 0)
1236 glsl_type::component_slots() const
1238 switch (this->base_type
) {
1239 case GLSL_TYPE_UINT
:
1241 case GLSL_TYPE_FLOAT
:
1242 case GLSL_TYPE_BOOL
:
1243 return this->components();
1245 case GLSL_TYPE_DOUBLE
:
1246 return 2 * this->components();
1248 case GLSL_TYPE_STRUCT
:
1249 case GLSL_TYPE_INTERFACE
: {
1252 for (unsigned i
= 0; i
< this->length
; i
++)
1253 size
+= this->fields
.structure
[i
].type
->component_slots();
1258 case GLSL_TYPE_ARRAY
:
1259 return this->length
* this->fields
.array
->component_slots();
1261 case GLSL_TYPE_IMAGE
:
1263 case GLSL_TYPE_SUBROUTINE
:
1266 case GLSL_TYPE_FUNCTION
:
1267 case GLSL_TYPE_SAMPLER
:
1268 case GLSL_TYPE_ATOMIC_UINT
:
1269 case GLSL_TYPE_VOID
:
1270 case GLSL_TYPE_ERROR
:
1278 glsl_type::record_location_offset(unsigned length
) const
1280 unsigned offset
= 0;
1281 const glsl_type
*t
= this->without_array();
1282 if (t
->is_record()) {
1283 assert(length
<= t
->length
);
1285 for (unsigned i
= 0; i
< length
; i
++) {
1286 const glsl_type
*st
= t
->fields
.structure
[i
].type
;
1287 const glsl_type
*wa
= st
->without_array();
1288 if (wa
->is_record()) {
1289 unsigned r_offset
= wa
->record_location_offset(wa
->length
);
1290 offset
+= st
->is_array() ?
1291 st
->arrays_of_arrays_size() * r_offset
: r_offset
;
1292 } else if (st
->is_array() && st
->fields
.array
->is_array()) {
1293 unsigned outer_array_size
= st
->length
;
1294 const glsl_type
*base_type
= st
->fields
.array
;
1296 /* For arrays of arrays the outer arrays take up a uniform
1297 * slot for each element. The innermost array elements share a
1298 * single slot so we ignore the innermost array when calculating
1301 while (base_type
->fields
.array
->is_array()) {
1302 outer_array_size
= outer_array_size
* base_type
->length
;
1303 base_type
= base_type
->fields
.array
;
1305 offset
+= outer_array_size
;
1307 /* We dont worry about arrays here because unless the array
1308 * contains a structure or another array it only takes up a single
1319 glsl_type::uniform_locations() const
1323 switch (this->base_type
) {
1324 case GLSL_TYPE_UINT
:
1326 case GLSL_TYPE_FLOAT
:
1327 case GLSL_TYPE_DOUBLE
:
1328 case GLSL_TYPE_BOOL
:
1329 case GLSL_TYPE_SAMPLER
:
1330 case GLSL_TYPE_IMAGE
:
1331 case GLSL_TYPE_SUBROUTINE
:
1334 case GLSL_TYPE_STRUCT
:
1335 case GLSL_TYPE_INTERFACE
:
1336 for (unsigned i
= 0; i
< this->length
; i
++)
1337 size
+= this->fields
.structure
[i
].type
->uniform_locations();
1339 case GLSL_TYPE_ARRAY
:
1340 return this->length
* this->fields
.array
->uniform_locations();
1347 glsl_type::can_implicitly_convert_to(const glsl_type
*desired
,
1348 _mesa_glsl_parse_state
*state
) const
1350 if (this == desired
)
1353 /* ESSL does not allow implicit conversions. If there is no state, we're
1354 * doing intra-stage function linking where these checks have already been
1357 if (state
&& state
->es_shader
)
1360 /* There is no conversion among matrix types. */
1361 if (this->matrix_columns
> 1 || desired
->matrix_columns
> 1)
1364 /* Vector size must match. */
1365 if (this->vector_elements
!= desired
->vector_elements
)
1368 /* int and uint can be converted to float. */
1369 if (desired
->is_float() && this->is_integer())
1372 /* With GLSL 4.0 / ARB_gpu_shader5, int can be converted to uint.
1373 * Note that state may be NULL here, when resolving function calls in the
1374 * linker. By this time, all the state-dependent checks have already
1375 * happened though, so allow anything that's allowed in any shader version. */
1376 if ((!state
|| state
->is_version(400, 0) || state
->ARB_gpu_shader5_enable
) &&
1377 desired
->base_type
== GLSL_TYPE_UINT
&& this->base_type
== GLSL_TYPE_INT
)
1380 /* No implicit conversions from double. */
1381 if ((!state
|| state
->has_double()) && this->is_double())
1384 /* Conversions from different types to double. */
1385 if ((!state
|| state
->has_double()) && desired
->is_double()) {
1386 if (this->is_float())
1388 if (this->is_integer())
1396 glsl_type::std140_base_alignment(bool row_major
) const
1398 unsigned N
= is_double() ? 8 : 4;
1400 /* (1) If the member is a scalar consuming <N> basic machine units, the
1401 * base alignment is <N>.
1403 * (2) If the member is a two- or four-component vector with components
1404 * consuming <N> basic machine units, the base alignment is 2<N> or
1405 * 4<N>, respectively.
1407 * (3) If the member is a three-component vector with components consuming
1408 * <N> basic machine units, the base alignment is 4<N>.
1410 if (this->is_scalar() || this->is_vector()) {
1411 switch (this->vector_elements
) {
1422 /* (4) If the member is an array of scalars or vectors, the base alignment
1423 * and array stride are set to match the base alignment of a single
1424 * array element, according to rules (1), (2), and (3), and rounded up
1425 * to the base alignment of a vec4. The array may have padding at the
1426 * end; the base offset of the member following the array is rounded up
1427 * to the next multiple of the base alignment.
1429 * (6) If the member is an array of <S> column-major matrices with <C>
1430 * columns and <R> rows, the matrix is stored identically to a row of
1431 * <S>*<C> column vectors with <R> components each, according to rule
1434 * (8) If the member is an array of <S> row-major matrices with <C> columns
1435 * and <R> rows, the matrix is stored identically to a row of <S>*<R>
1436 * row vectors with <C> components each, according to rule (4).
1438 * (10) If the member is an array of <S> structures, the <S> elements of
1439 * the array are laid out in order, according to rule (9).
1441 if (this->is_array()) {
1442 if (this->fields
.array
->is_scalar() ||
1443 this->fields
.array
->is_vector() ||
1444 this->fields
.array
->is_matrix()) {
1445 return MAX2(this->fields
.array
->std140_base_alignment(row_major
), 16);
1447 assert(this->fields
.array
->is_record() ||
1448 this->fields
.array
->is_array());
1449 return this->fields
.array
->std140_base_alignment(row_major
);
1453 /* (5) If the member is a column-major matrix with <C> columns and
1454 * <R> rows, the matrix is stored identically to an array of
1455 * <C> column vectors with <R> components each, according to
1458 * (7) If the member is a row-major matrix with <C> columns and <R>
1459 * rows, the matrix is stored identically to an array of <R>
1460 * row vectors with <C> components each, according to rule (4).
1462 if (this->is_matrix()) {
1463 const struct glsl_type
*vec_type
, *array_type
;
1464 int c
= this->matrix_columns
;
1465 int r
= this->vector_elements
;
1468 vec_type
= get_instance(base_type
, c
, 1);
1469 array_type
= glsl_type::get_array_instance(vec_type
, r
);
1471 vec_type
= get_instance(base_type
, r
, 1);
1472 array_type
= glsl_type::get_array_instance(vec_type
, c
);
1475 return array_type
->std140_base_alignment(false);
1478 /* (9) If the member is a structure, the base alignment of the
1479 * structure is <N>, where <N> is the largest base alignment
1480 * value of any of its members, and rounded up to the base
1481 * alignment of a vec4. The individual members of this
1482 * sub-structure are then assigned offsets by applying this set
1483 * of rules recursively, where the base offset of the first
1484 * member of the sub-structure is equal to the aligned offset
1485 * of the structure. The structure may have padding at the end;
1486 * the base offset of the member following the sub-structure is
1487 * rounded up to the next multiple of the base alignment of the
1490 if (this->is_record()) {
1491 unsigned base_alignment
= 16;
1492 for (unsigned i
= 0; i
< this->length
; i
++) {
1493 bool field_row_major
= row_major
;
1494 const enum glsl_matrix_layout matrix_layout
=
1495 glsl_matrix_layout(this->fields
.structure
[i
].matrix_layout
);
1496 if (matrix_layout
== GLSL_MATRIX_LAYOUT_ROW_MAJOR
) {
1497 field_row_major
= true;
1498 } else if (matrix_layout
== GLSL_MATRIX_LAYOUT_COLUMN_MAJOR
) {
1499 field_row_major
= false;
1502 const struct glsl_type
*field_type
= this->fields
.structure
[i
].type
;
1503 base_alignment
= MAX2(base_alignment
,
1504 field_type
->std140_base_alignment(field_row_major
));
1506 return base_alignment
;
1509 assert(!"not reached");
1514 glsl_type::std140_size(bool row_major
) const
1516 unsigned N
= is_double() ? 8 : 4;
1518 /* (1) If the member is a scalar consuming <N> basic machine units, the
1519 * base alignment is <N>.
1521 * (2) If the member is a two- or four-component vector with components
1522 * consuming <N> basic machine units, the base alignment is 2<N> or
1523 * 4<N>, respectively.
1525 * (3) If the member is a three-component vector with components consuming
1526 * <N> basic machine units, the base alignment is 4<N>.
1528 if (this->is_scalar() || this->is_vector()) {
1529 return this->vector_elements
* N
;
1532 /* (5) If the member is a column-major matrix with <C> columns and
1533 * <R> rows, the matrix is stored identically to an array of
1534 * <C> column vectors with <R> components each, according to
1537 * (6) If the member is an array of <S> column-major matrices with <C>
1538 * columns and <R> rows, the matrix is stored identically to a row of
1539 * <S>*<C> column vectors with <R> components each, according to rule
1542 * (7) If the member is a row-major matrix with <C> columns and <R>
1543 * rows, the matrix is stored identically to an array of <R>
1544 * row vectors with <C> components each, according to rule (4).
1546 * (8) If the member is an array of <S> row-major matrices with <C> columns
1547 * and <R> rows, the matrix is stored identically to a row of <S>*<R>
1548 * row vectors with <C> components each, according to rule (4).
1550 if (this->without_array()->is_matrix()) {
1551 const struct glsl_type
*element_type
;
1552 const struct glsl_type
*vec_type
;
1553 unsigned int array_len
;
1555 if (this->is_array()) {
1556 element_type
= this->without_array();
1557 array_len
= this->arrays_of_arrays_size();
1559 element_type
= this;
1564 vec_type
= get_instance(element_type
->base_type
,
1565 element_type
->matrix_columns
, 1);
1567 array_len
*= element_type
->vector_elements
;
1569 vec_type
= get_instance(element_type
->base_type
,
1570 element_type
->vector_elements
, 1);
1571 array_len
*= element_type
->matrix_columns
;
1573 const glsl_type
*array_type
= glsl_type::get_array_instance(vec_type
,
1576 return array_type
->std140_size(false);
1579 /* (4) If the member is an array of scalars or vectors, the base alignment
1580 * and array stride are set to match the base alignment of a single
1581 * array element, according to rules (1), (2), and (3), and rounded up
1582 * to the base alignment of a vec4. The array may have padding at the
1583 * end; the base offset of the member following the array is rounded up
1584 * to the next multiple of the base alignment.
1586 * (10) If the member is an array of <S> structures, the <S> elements of
1587 * the array are laid out in order, according to rule (9).
1589 if (this->is_array()) {
1590 if (this->without_array()->is_record()) {
1591 return this->arrays_of_arrays_size() *
1592 this->without_array()->std140_size(row_major
);
1594 unsigned element_base_align
=
1595 this->without_array()->std140_base_alignment(row_major
);
1596 return this->arrays_of_arrays_size() * MAX2(element_base_align
, 16);
1600 /* (9) If the member is a structure, the base alignment of the
1601 * structure is <N>, where <N> is the largest base alignment
1602 * value of any of its members, and rounded up to the base
1603 * alignment of a vec4. The individual members of this
1604 * sub-structure are then assigned offsets by applying this set
1605 * of rules recursively, where the base offset of the first
1606 * member of the sub-structure is equal to the aligned offset
1607 * of the structure. The structure may have padding at the end;
1608 * the base offset of the member following the sub-structure is
1609 * rounded up to the next multiple of the base alignment of the
1612 if (this->is_record() || this->is_interface()) {
1614 unsigned max_align
= 0;
1616 for (unsigned i
= 0; i
< this->length
; i
++) {
1617 bool field_row_major
= row_major
;
1618 const enum glsl_matrix_layout matrix_layout
=
1619 glsl_matrix_layout(this->fields
.structure
[i
].matrix_layout
);
1620 if (matrix_layout
== GLSL_MATRIX_LAYOUT_ROW_MAJOR
) {
1621 field_row_major
= true;
1622 } else if (matrix_layout
== GLSL_MATRIX_LAYOUT_COLUMN_MAJOR
) {
1623 field_row_major
= false;
1626 const struct glsl_type
*field_type
= this->fields
.structure
[i
].type
;
1627 unsigned align
= field_type
->std140_base_alignment(field_row_major
);
1629 /* Ignore unsized arrays when calculating size */
1630 if (field_type
->is_unsized_array())
1633 size
= glsl_align(size
, align
);
1634 size
+= field_type
->std140_size(field_row_major
);
1636 max_align
= MAX2(align
, max_align
);
1638 if (field_type
->is_record() && (i
+ 1 < this->length
))
1639 size
= glsl_align(size
, 16);
1641 size
= glsl_align(size
, MAX2(max_align
, 16));
1645 assert(!"not reached");
1650 glsl_type::std430_base_alignment(bool row_major
) const
1653 unsigned N
= is_double() ? 8 : 4;
1655 /* (1) If the member is a scalar consuming <N> basic machine units, the
1656 * base alignment is <N>.
1658 * (2) If the member is a two- or four-component vector with components
1659 * consuming <N> basic machine units, the base alignment is 2<N> or
1660 * 4<N>, respectively.
1662 * (3) If the member is a three-component vector with components consuming
1663 * <N> basic machine units, the base alignment is 4<N>.
1665 if (this->is_scalar() || this->is_vector()) {
1666 switch (this->vector_elements
) {
1677 /* OpenGL 4.30 spec, section 7.6.2.2 "Standard Uniform Block Layout":
1679 * "When using the std430 storage layout, shader storage blocks will be
1680 * laid out in buffer storage identically to uniform and shader storage
1681 * blocks using the std140 layout, except that the base alignment and
1682 * stride of arrays of scalars and vectors in rule 4 and of structures
1683 * in rule 9 are not rounded up a multiple of the base alignment of a vec4.
1686 /* (1) If the member is a scalar consuming <N> basic machine units, the
1687 * base alignment is <N>.
1689 * (2) If the member is a two- or four-component vector with components
1690 * consuming <N> basic machine units, the base alignment is 2<N> or
1691 * 4<N>, respectively.
1693 * (3) If the member is a three-component vector with components consuming
1694 * <N> basic machine units, the base alignment is 4<N>.
1696 if (this->is_array())
1697 return this->fields
.array
->std430_base_alignment(row_major
);
1699 /* (5) If the member is a column-major matrix with <C> columns and
1700 * <R> rows, the matrix is stored identically to an array of
1701 * <C> column vectors with <R> components each, according to
1704 * (7) If the member is a row-major matrix with <C> columns and <R>
1705 * rows, the matrix is stored identically to an array of <R>
1706 * row vectors with <C> components each, according to rule (4).
1708 if (this->is_matrix()) {
1709 const struct glsl_type
*vec_type
, *array_type
;
1710 int c
= this->matrix_columns
;
1711 int r
= this->vector_elements
;
1714 vec_type
= get_instance(base_type
, c
, 1);
1715 array_type
= glsl_type::get_array_instance(vec_type
, r
);
1717 vec_type
= get_instance(base_type
, r
, 1);
1718 array_type
= glsl_type::get_array_instance(vec_type
, c
);
1721 return array_type
->std430_base_alignment(false);
1724 /* (9) If the member is a structure, the base alignment of the
1725 * structure is <N>, where <N> is the largest base alignment
1726 * value of any of its members, and rounded up to the base
1727 * alignment of a vec4. The individual members of this
1728 * sub-structure are then assigned offsets by applying this set
1729 * of rules recursively, where the base offset of the first
1730 * member of the sub-structure is equal to the aligned offset
1731 * of the structure. The structure may have padding at the end;
1732 * the base offset of the member following the sub-structure is
1733 * rounded up to the next multiple of the base alignment of the
1736 if (this->is_record()) {
1737 unsigned base_alignment
= 0;
1738 for (unsigned i
= 0; i
< this->length
; i
++) {
1739 bool field_row_major
= row_major
;
1740 const enum glsl_matrix_layout matrix_layout
=
1741 glsl_matrix_layout(this->fields
.structure
[i
].matrix_layout
);
1742 if (matrix_layout
== GLSL_MATRIX_LAYOUT_ROW_MAJOR
) {
1743 field_row_major
= true;
1744 } else if (matrix_layout
== GLSL_MATRIX_LAYOUT_COLUMN_MAJOR
) {
1745 field_row_major
= false;
1748 const struct glsl_type
*field_type
= this->fields
.structure
[i
].type
;
1749 base_alignment
= MAX2(base_alignment
,
1750 field_type
->std430_base_alignment(field_row_major
));
1752 assert(base_alignment
> 0);
1753 return base_alignment
;
1755 assert(!"not reached");
1760 glsl_type::std430_array_stride(bool row_major
) const
1762 unsigned N
= is_double() ? 8 : 4;
1764 /* Notice that the array stride of a vec3 is not 3 * N but 4 * N.
1765 * See OpenGL 4.30 spec, section 7.6.2.2 "Standard Uniform Block Layout"
1767 * (3) If the member is a three-component vector with components consuming
1768 * <N> basic machine units, the base alignment is 4<N>.
1770 if (this->is_vector() && this->vector_elements
== 3)
1773 /* By default use std430_size(row_major) */
1774 return this->std430_size(row_major
);
1778 glsl_type::std430_size(bool row_major
) const
1780 unsigned N
= is_double() ? 8 : 4;
1782 /* OpenGL 4.30 spec, section 7.6.2.2 "Standard Uniform Block Layout":
1784 * "When using the std430 storage layout, shader storage blocks will be
1785 * laid out in buffer storage identically to uniform and shader storage
1786 * blocks using the std140 layout, except that the base alignment and
1787 * stride of arrays of scalars and vectors in rule 4 and of structures
1788 * in rule 9 are not rounded up a multiple of the base alignment of a vec4.
1790 if (this->is_scalar() || this->is_vector())
1791 return this->vector_elements
* N
;
1793 if (this->without_array()->is_matrix()) {
1794 const struct glsl_type
*element_type
;
1795 const struct glsl_type
*vec_type
;
1796 unsigned int array_len
;
1798 if (this->is_array()) {
1799 element_type
= this->without_array();
1800 array_len
= this->arrays_of_arrays_size();
1802 element_type
= this;
1807 vec_type
= get_instance(element_type
->base_type
,
1808 element_type
->matrix_columns
, 1);
1810 array_len
*= element_type
->vector_elements
;
1812 vec_type
= get_instance(element_type
->base_type
,
1813 element_type
->vector_elements
, 1);
1814 array_len
*= element_type
->matrix_columns
;
1816 const glsl_type
*array_type
= glsl_type::get_array_instance(vec_type
,
1819 return array_type
->std430_size(false);
1822 if (this->is_array()) {
1823 if (this->without_array()->is_record())
1824 return this->arrays_of_arrays_size() *
1825 this->without_array()->std430_size(row_major
);
1827 return this->arrays_of_arrays_size() *
1828 this->without_array()->std430_base_alignment(row_major
);
1831 if (this->is_record() || this->is_interface()) {
1833 unsigned max_align
= 0;
1835 for (unsigned i
= 0; i
< this->length
; i
++) {
1836 bool field_row_major
= row_major
;
1837 const enum glsl_matrix_layout matrix_layout
=
1838 glsl_matrix_layout(this->fields
.structure
[i
].matrix_layout
);
1839 if (matrix_layout
== GLSL_MATRIX_LAYOUT_ROW_MAJOR
) {
1840 field_row_major
= true;
1841 } else if (matrix_layout
== GLSL_MATRIX_LAYOUT_COLUMN_MAJOR
) {
1842 field_row_major
= false;
1845 const struct glsl_type
*field_type
= this->fields
.structure
[i
].type
;
1846 unsigned align
= field_type
->std430_base_alignment(field_row_major
);
1847 size
= glsl_align(size
, align
);
1848 size
+= field_type
->std430_size(field_row_major
);
1850 max_align
= MAX2(align
, max_align
);
1852 size
= glsl_align(size
, max_align
);
1856 assert(!"not reached");
1861 glsl_type::count_attribute_slots(bool vertex_input_slots
) const
1863 /* From page 31 (page 37 of the PDF) of the GLSL 1.50 spec:
1865 * "A scalar input counts the same amount against this limit as a vec4,
1866 * so applications may want to consider packing groups of four
1867 * unrelated float inputs together into a vector to better utilize the
1868 * capabilities of the underlying hardware. A matrix input will use up
1869 * multiple locations. The number of locations used will equal the
1870 * number of columns in the matrix."
1872 * The spec does not explicitly say how arrays are counted. However, it
1873 * should be safe to assume the total number of slots consumed by an array
1874 * is the number of entries in the array multiplied by the number of slots
1875 * consumed by a single element of the array.
1877 * The spec says nothing about how structs are counted, because vertex
1878 * attributes are not allowed to be (or contain) structs. However, Mesa
1879 * allows varying structs, the number of varying slots taken up by a
1880 * varying struct is simply equal to the sum of the number of slots taken
1881 * up by each element.
1883 * Doubles are counted different depending on whether they are vertex
1884 * inputs or everything else. Vertex inputs from ARB_vertex_attrib_64bit
1885 * take one location no matter what size they are, otherwise dvec3/4
1886 * take two locations.
1888 switch (this->base_type
) {
1889 case GLSL_TYPE_UINT
:
1891 case GLSL_TYPE_FLOAT
:
1892 case GLSL_TYPE_BOOL
:
1893 return this->matrix_columns
;
1894 case GLSL_TYPE_DOUBLE
:
1895 if (this->vector_elements
> 2 && !vertex_input_slots
)
1896 return this->matrix_columns
* 2;
1898 return this->matrix_columns
;
1899 case GLSL_TYPE_STRUCT
:
1900 case GLSL_TYPE_INTERFACE
: {
1903 for (unsigned i
= 0; i
< this->length
; i
++)
1904 size
+= this->fields
.structure
[i
].type
->count_attribute_slots(vertex_input_slots
);
1909 case GLSL_TYPE_ARRAY
:
1910 return this->length
* this->fields
.array
->count_attribute_slots(vertex_input_slots
);
1912 case GLSL_TYPE_FUNCTION
:
1913 case GLSL_TYPE_SAMPLER
:
1914 case GLSL_TYPE_IMAGE
:
1915 case GLSL_TYPE_ATOMIC_UINT
:
1916 case GLSL_TYPE_VOID
:
1917 case GLSL_TYPE_SUBROUTINE
:
1918 case GLSL_TYPE_ERROR
:
1922 assert(!"Unexpected type in count_attribute_slots()");
1928 glsl_type::coordinate_components() const
1932 switch (sampler_dimensionality
) {
1933 case GLSL_SAMPLER_DIM_1D
:
1934 case GLSL_SAMPLER_DIM_BUF
:
1937 case GLSL_SAMPLER_DIM_2D
:
1938 case GLSL_SAMPLER_DIM_RECT
:
1939 case GLSL_SAMPLER_DIM_MS
:
1940 case GLSL_SAMPLER_DIM_EXTERNAL
:
1943 case GLSL_SAMPLER_DIM_3D
:
1944 case GLSL_SAMPLER_DIM_CUBE
:
1948 assert(!"Should not get here.");
1953 /* Array textures need an additional component for the array index, except
1954 * for cubemap array images that behave like a 2D array of interleaved
1957 if (sampler_array
&&
1958 !(base_type
== GLSL_TYPE_IMAGE
&&
1959 sampler_dimensionality
== GLSL_SAMPLER_DIM_CUBE
))
1966 * Declarations of type flyweights (glsl_type::_foo_type) and
1967 * convenience pointers (glsl_type::foo_type).
1970 #define DECL_TYPE(NAME, ...) \
1971 const glsl_type glsl_type::_##NAME##_type = glsl_type(__VA_ARGS__, #NAME); \
1972 const glsl_type *const glsl_type::NAME##_type = &glsl_type::_##NAME##_type;
1974 #define STRUCT_TYPE(NAME)
1976 #include "compiler/builtin_type_macros.h"