glsl: Restrict functions to not return arrays or SOAs in GLSL 1.00.
[mesa.git] / src / compiler / glsl_types.cpp
1 /*
2 * Copyright © 2009 Intel Corporation
3 *
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:
10 *
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
13 * Software.
14 *
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.
22 */
23
24 #include <stdio.h>
25 #include "main/macros.h"
26 #include "compiler/glsl/glsl_parser_extras.h"
27 #include "glsl_types.h"
28 #include "util/hash_table.h"
29
30
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;
38
39 void
40 glsl_type::init_ralloc_type_ctx(void)
41 {
42 if (glsl_type::mem_ctx == NULL) {
43 glsl_type::mem_ctx = ralloc_context(NULL);
44 assert(glsl_type::mem_ctx != NULL);
45 }
46 }
47
48 glsl_type::glsl_type(GLenum gl_type,
49 glsl_base_type base_type, unsigned vector_elements,
50 unsigned matrix_columns, const char *name) :
51 gl_type(gl_type),
52 base_type(base_type),
53 sampler_dimensionality(0), sampler_shadow(0), sampler_array(0),
54 sampled_type(0), interface_packing(0), interface_row_major(0),
55 vector_elements(vector_elements), matrix_columns(matrix_columns),
56 length(0)
57 {
58 /* Values of these types must fit in the two bits of
59 * glsl_type::sampled_type.
60 */
61 STATIC_ASSERT((unsigned(GLSL_TYPE_UINT) & 3) == unsigned(GLSL_TYPE_UINT));
62 STATIC_ASSERT((unsigned(GLSL_TYPE_INT) & 3) == unsigned(GLSL_TYPE_INT));
63 STATIC_ASSERT((unsigned(GLSL_TYPE_FLOAT) & 3) == unsigned(GLSL_TYPE_FLOAT));
64
65 mtx_lock(&glsl_type::mutex);
66
67 init_ralloc_type_ctx();
68 assert(name != NULL);
69 this->name = ralloc_strdup(this->mem_ctx, name);
70
71 mtx_unlock(&glsl_type::mutex);
72
73 /* Neither dimension is zero or both dimensions are zero.
74 */
75 assert((vector_elements == 0) == (matrix_columns == 0));
76 memset(& fields, 0, sizeof(fields));
77 }
78
79 glsl_type::glsl_type(GLenum gl_type, glsl_base_type base_type,
80 enum glsl_sampler_dim dim, bool shadow, bool array,
81 unsigned type, const char *name) :
82 gl_type(gl_type),
83 base_type(base_type),
84 sampler_dimensionality(dim), sampler_shadow(shadow),
85 sampler_array(array), sampled_type(type), interface_packing(0),
86 interface_row_major(0), length(0)
87 {
88 mtx_lock(&glsl_type::mutex);
89
90 init_ralloc_type_ctx();
91 assert(name != NULL);
92 this->name = ralloc_strdup(this->mem_ctx, name);
93
94 mtx_unlock(&glsl_type::mutex);
95
96 memset(& fields, 0, sizeof(fields));
97
98 matrix_columns = vector_elements = 1;
99 }
100
101 glsl_type::glsl_type(const glsl_struct_field *fields, unsigned num_fields,
102 const char *name) :
103 gl_type(0),
104 base_type(GLSL_TYPE_STRUCT),
105 sampler_dimensionality(0), sampler_shadow(0), sampler_array(0),
106 sampled_type(0), interface_packing(0), interface_row_major(0),
107 vector_elements(0), matrix_columns(0),
108 length(num_fields)
109 {
110 unsigned int i;
111
112 mtx_lock(&glsl_type::mutex);
113
114 init_ralloc_type_ctx();
115 assert(name != NULL);
116 this->name = ralloc_strdup(this->mem_ctx, name);
117 this->fields.structure = ralloc_array(this->mem_ctx,
118 glsl_struct_field, length);
119
120 for (i = 0; i < length; i++) {
121 this->fields.structure[i] = fields[i];
122 this->fields.structure[i].name = ralloc_strdup(this->fields.structure,
123 fields[i].name);
124 }
125
126 mtx_unlock(&glsl_type::mutex);
127 }
128
129 glsl_type::glsl_type(const glsl_struct_field *fields, unsigned num_fields,
130 enum glsl_interface_packing packing,
131 bool row_major, const char *name) :
132 gl_type(0),
133 base_type(GLSL_TYPE_INTERFACE),
134 sampler_dimensionality(0), sampler_shadow(0), sampler_array(0),
135 sampled_type(0), interface_packing((unsigned) packing),
136 interface_row_major((unsigned) row_major),
137 vector_elements(0), matrix_columns(0),
138 length(num_fields)
139 {
140 unsigned int i;
141
142 mtx_lock(&glsl_type::mutex);
143
144 init_ralloc_type_ctx();
145 assert(name != NULL);
146 this->name = ralloc_strdup(this->mem_ctx, name);
147 this->fields.structure = rzalloc_array(this->mem_ctx,
148 glsl_struct_field, length);
149 for (i = 0; i < length; i++) {
150 this->fields.structure[i] = fields[i];
151 this->fields.structure[i].name = ralloc_strdup(this->fields.structure,
152 fields[i].name);
153 }
154
155 mtx_unlock(&glsl_type::mutex);
156 }
157
158 glsl_type::glsl_type(const glsl_type *return_type,
159 const glsl_function_param *params, unsigned num_params) :
160 gl_type(0),
161 base_type(GLSL_TYPE_FUNCTION),
162 sampler_dimensionality(0), sampler_shadow(0), sampler_array(0),
163 sampled_type(0), interface_packing(0), interface_row_major(0),
164 vector_elements(0), matrix_columns(0),
165 length(num_params)
166 {
167 unsigned int i;
168
169 mtx_lock(&glsl_type::mutex);
170
171 init_ralloc_type_ctx();
172
173 this->fields.parameters = rzalloc_array(this->mem_ctx,
174 glsl_function_param, num_params + 1);
175
176 /* We store the return type as the first parameter */
177 this->fields.parameters[0].type = return_type;
178 this->fields.parameters[0].in = false;
179 this->fields.parameters[0].out = true;
180
181 /* We store the i'th parameter in slot i+1 */
182 for (i = 0; i < length; i++) {
183 this->fields.parameters[i + 1].type = params[i].type;
184 this->fields.parameters[i + 1].in = params[i].in;
185 this->fields.parameters[i + 1].out = params[i].out;
186 }
187
188 mtx_unlock(&glsl_type::mutex);
189 }
190
191 glsl_type::glsl_type(const char *subroutine_name) :
192 gl_type(0),
193 base_type(GLSL_TYPE_SUBROUTINE),
194 sampler_dimensionality(0), sampler_shadow(0), sampler_array(0),
195 sampled_type(0), interface_packing(0), interface_row_major(0),
196 vector_elements(1), matrix_columns(1),
197 length(0)
198 {
199 mtx_lock(&glsl_type::mutex);
200
201 init_ralloc_type_ctx();
202 assert(subroutine_name != NULL);
203 this->name = ralloc_strdup(this->mem_ctx, subroutine_name);
204 mtx_unlock(&glsl_type::mutex);
205 }
206
207 bool
208 glsl_type::contains_sampler() const
209 {
210 if (this->is_array()) {
211 return this->fields.array->contains_sampler();
212 } else if (this->is_record() || this->is_interface()) {
213 for (unsigned int i = 0; i < this->length; i++) {
214 if (this->fields.structure[i].type->contains_sampler())
215 return true;
216 }
217 return false;
218 } else {
219 return this->is_sampler();
220 }
221 }
222
223 bool
224 glsl_type::contains_array() const
225 {
226 if (this->is_record() || this->is_interface()) {
227 for (unsigned int i = 0; i < this->length; i++) {
228 if (this->fields.structure[i].type->contains_array())
229 return true;
230 }
231 return false;
232 } else {
233 return this->is_array();
234 }
235 }
236
237 bool
238 glsl_type::contains_integer() const
239 {
240 if (this->is_array()) {
241 return this->fields.array->contains_integer();
242 } else if (this->is_record() || this->is_interface()) {
243 for (unsigned int i = 0; i < this->length; i++) {
244 if (this->fields.structure[i].type->contains_integer())
245 return true;
246 }
247 return false;
248 } else {
249 return this->is_integer();
250 }
251 }
252
253 bool
254 glsl_type::contains_double() const
255 {
256 if (this->is_array()) {
257 return this->fields.array->contains_double();
258 } else if (this->is_record() || this->is_interface()) {
259 for (unsigned int i = 0; i < this->length; i++) {
260 if (this->fields.structure[i].type->contains_double())
261 return true;
262 }
263 return false;
264 } else {
265 return this->is_double();
266 }
267 }
268
269 bool
270 glsl_type::contains_opaque() const {
271 switch (base_type) {
272 case GLSL_TYPE_SAMPLER:
273 case GLSL_TYPE_IMAGE:
274 case GLSL_TYPE_ATOMIC_UINT:
275 return true;
276 case GLSL_TYPE_ARRAY:
277 return fields.array->contains_opaque();
278 case GLSL_TYPE_STRUCT:
279 case GLSL_TYPE_INTERFACE:
280 for (unsigned int i = 0; i < length; i++) {
281 if (fields.structure[i].type->contains_opaque())
282 return true;
283 }
284 return false;
285 default:
286 return false;
287 }
288 }
289
290 bool
291 glsl_type::contains_subroutine() const
292 {
293 if (this->is_array()) {
294 return this->fields.array->contains_subroutine();
295 } else if (this->is_record() || this->is_interface()) {
296 for (unsigned int i = 0; i < this->length; i++) {
297 if (this->fields.structure[i].type->contains_subroutine())
298 return true;
299 }
300 return false;
301 } else {
302 return this->is_subroutine();
303 }
304 }
305
306 gl_texture_index
307 glsl_type::sampler_index() const
308 {
309 const glsl_type *const t = (this->is_array()) ? this->fields.array : this;
310
311 assert(t->is_sampler() || t->is_image());
312
313 switch (t->sampler_dimensionality) {
314 case GLSL_SAMPLER_DIM_1D:
315 return (t->sampler_array) ? TEXTURE_1D_ARRAY_INDEX : TEXTURE_1D_INDEX;
316 case GLSL_SAMPLER_DIM_2D:
317 return (t->sampler_array) ? TEXTURE_2D_ARRAY_INDEX : TEXTURE_2D_INDEX;
318 case GLSL_SAMPLER_DIM_3D:
319 return TEXTURE_3D_INDEX;
320 case GLSL_SAMPLER_DIM_CUBE:
321 return (t->sampler_array) ? TEXTURE_CUBE_ARRAY_INDEX : TEXTURE_CUBE_INDEX;
322 case GLSL_SAMPLER_DIM_RECT:
323 return TEXTURE_RECT_INDEX;
324 case GLSL_SAMPLER_DIM_BUF:
325 return TEXTURE_BUFFER_INDEX;
326 case GLSL_SAMPLER_DIM_EXTERNAL:
327 return TEXTURE_EXTERNAL_INDEX;
328 case GLSL_SAMPLER_DIM_MS:
329 return (t->sampler_array) ? TEXTURE_2D_MULTISAMPLE_ARRAY_INDEX : TEXTURE_2D_MULTISAMPLE_INDEX;
330 default:
331 assert(!"Should not get here.");
332 return TEXTURE_BUFFER_INDEX;
333 }
334 }
335
336 bool
337 glsl_type::contains_image() const
338 {
339 if (this->is_array()) {
340 return this->fields.array->contains_image();
341 } else if (this->is_record() || this->is_interface()) {
342 for (unsigned int i = 0; i < this->length; i++) {
343 if (this->fields.structure[i].type->contains_image())
344 return true;
345 }
346 return false;
347 } else {
348 return this->is_image();
349 }
350 }
351
352 const glsl_type *glsl_type::get_base_type() const
353 {
354 switch (base_type) {
355 case GLSL_TYPE_UINT:
356 return uint_type;
357 case GLSL_TYPE_INT:
358 return int_type;
359 case GLSL_TYPE_FLOAT:
360 return float_type;
361 case GLSL_TYPE_DOUBLE:
362 return double_type;
363 case GLSL_TYPE_BOOL:
364 return bool_type;
365 case GLSL_TYPE_UINT64:
366 return uint64_t_type;
367 case GLSL_TYPE_INT64:
368 return int64_t_type;
369 default:
370 return error_type;
371 }
372 }
373
374
375 const glsl_type *glsl_type::get_scalar_type() const
376 {
377 const glsl_type *type = this;
378
379 /* Handle arrays */
380 while (type->base_type == GLSL_TYPE_ARRAY)
381 type = type->fields.array;
382
383 /* Handle vectors and matrices */
384 switch (type->base_type) {
385 case GLSL_TYPE_UINT:
386 return uint_type;
387 case GLSL_TYPE_INT:
388 return int_type;
389 case GLSL_TYPE_FLOAT:
390 return float_type;
391 case GLSL_TYPE_DOUBLE:
392 return double_type;
393 case GLSL_TYPE_BOOL:
394 return bool_type;
395 case GLSL_TYPE_UINT64:
396 return uint64_t_type;
397 case GLSL_TYPE_INT64:
398 return int64_t_type;
399 default:
400 /* Handle everything else */
401 return type;
402 }
403 }
404
405
406 void
407 _mesa_glsl_release_types(void)
408 {
409 /* Should only be called during atexit (either when unloading shared
410 * object, or if process terminates), so no mutex-locking should be
411 * necessary.
412 */
413 if (glsl_type::array_types != NULL) {
414 _mesa_hash_table_destroy(glsl_type::array_types, NULL);
415 glsl_type::array_types = NULL;
416 }
417
418 if (glsl_type::record_types != NULL) {
419 _mesa_hash_table_destroy(glsl_type::record_types, NULL);
420 glsl_type::record_types = NULL;
421 }
422
423 if (glsl_type::interface_types != NULL) {
424 _mesa_hash_table_destroy(glsl_type::interface_types, NULL);
425 glsl_type::interface_types = NULL;
426 }
427
428 if (glsl_type::function_types != NULL) {
429 _mesa_hash_table_destroy(glsl_type::function_types, NULL);
430 glsl_type::function_types = NULL;
431 }
432
433 if (glsl_type::subroutine_types != NULL) {
434 _mesa_hash_table_destroy(glsl_type::subroutine_types, NULL);
435 glsl_type::subroutine_types = NULL;
436 }
437
438 ralloc_free(glsl_type::mem_ctx);
439 glsl_type::mem_ctx = NULL;
440 }
441
442
443 glsl_type::glsl_type(const glsl_type *array, unsigned length) :
444 base_type(GLSL_TYPE_ARRAY),
445 sampler_dimensionality(0), sampler_shadow(0), sampler_array(0),
446 sampled_type(0), interface_packing(0), interface_row_major(0),
447 vector_elements(0), matrix_columns(0),
448 length(length), name(NULL)
449 {
450 this->fields.array = array;
451 /* Inherit the gl type of the base. The GL type is used for
452 * uniform/statevar handling in Mesa and the arrayness of the type
453 * is represented by the size rather than the type.
454 */
455 this->gl_type = array->gl_type;
456
457 /* Allow a maximum of 10 characters for the array size. This is enough
458 * for 32-bits of ~0. The extra 3 are for the '[', ']', and terminating
459 * NUL.
460 */
461 const unsigned name_length = strlen(array->name) + 10 + 3;
462
463 mtx_lock(&glsl_type::mutex);
464 char *const n = (char *) ralloc_size(this->mem_ctx, name_length);
465 mtx_unlock(&glsl_type::mutex);
466
467 if (length == 0)
468 snprintf(n, name_length, "%s[]", array->name);
469 else {
470 /* insert outermost dimensions in the correct spot
471 * otherwise the dimension order will be backwards
472 */
473 const char *pos = strchr(array->name, '[');
474 if (pos) {
475 int idx = pos - array->name;
476 snprintf(n, idx+1, "%s", array->name);
477 snprintf(n + idx, name_length - idx, "[%u]%s",
478 length, array->name + idx);
479 } else {
480 snprintf(n, name_length, "%s[%u]", array->name, length);
481 }
482 }
483
484 this->name = n;
485 }
486
487
488 const glsl_type *
489 glsl_type::vec(unsigned components)
490 {
491 if (components == 0 || components > 4)
492 return error_type;
493
494 static const glsl_type *const ts[] = {
495 float_type, vec2_type, vec3_type, vec4_type
496 };
497 return ts[components - 1];
498 }
499
500 const glsl_type *
501 glsl_type::dvec(unsigned components)
502 {
503 if (components == 0 || components > 4)
504 return error_type;
505
506 static const glsl_type *const ts[] = {
507 double_type, dvec2_type, dvec3_type, dvec4_type
508 };
509 return ts[components - 1];
510 }
511
512 const glsl_type *
513 glsl_type::ivec(unsigned components)
514 {
515 if (components == 0 || components > 4)
516 return error_type;
517
518 static const glsl_type *const ts[] = {
519 int_type, ivec2_type, ivec3_type, ivec4_type
520 };
521 return ts[components - 1];
522 }
523
524
525 const glsl_type *
526 glsl_type::uvec(unsigned components)
527 {
528 if (components == 0 || components > 4)
529 return error_type;
530
531 static const glsl_type *const ts[] = {
532 uint_type, uvec2_type, uvec3_type, uvec4_type
533 };
534 return ts[components - 1];
535 }
536
537
538 const glsl_type *
539 glsl_type::bvec(unsigned components)
540 {
541 if (components == 0 || components > 4)
542 return error_type;
543
544 static const glsl_type *const ts[] = {
545 bool_type, bvec2_type, bvec3_type, bvec4_type
546 };
547 return ts[components - 1];
548 }
549
550
551 const glsl_type *
552 glsl_type::i64vec(unsigned components)
553 {
554 if (components == 0 || components > 4)
555 return error_type;
556
557 static const glsl_type *const ts[] = {
558 int64_t_type, i64vec2_type, i64vec3_type, i64vec4_type
559 };
560 return ts[components - 1];
561 }
562
563
564 const glsl_type *
565 glsl_type::u64vec(unsigned components)
566 {
567 if (components == 0 || components > 4)
568 return error_type;
569
570 static const glsl_type *const ts[] = {
571 uint64_t_type, u64vec2_type, u64vec3_type, u64vec4_type
572 };
573 return ts[components - 1];
574 }
575
576 const glsl_type *
577 glsl_type::get_instance(unsigned base_type, unsigned rows, unsigned columns)
578 {
579 if (base_type == GLSL_TYPE_VOID)
580 return void_type;
581
582 if ((rows < 1) || (rows > 4) || (columns < 1) || (columns > 4))
583 return error_type;
584
585 /* Treat GLSL vectors as Nx1 matrices.
586 */
587 if (columns == 1) {
588 switch (base_type) {
589 case GLSL_TYPE_UINT:
590 return uvec(rows);
591 case GLSL_TYPE_INT:
592 return ivec(rows);
593 case GLSL_TYPE_FLOAT:
594 return vec(rows);
595 case GLSL_TYPE_DOUBLE:
596 return dvec(rows);
597 case GLSL_TYPE_BOOL:
598 return bvec(rows);
599 case GLSL_TYPE_UINT64:
600 return u64vec(rows);
601 case GLSL_TYPE_INT64:
602 return i64vec(rows);
603 default:
604 return error_type;
605 }
606 } else {
607 if ((base_type != GLSL_TYPE_FLOAT && base_type != GLSL_TYPE_DOUBLE) || (rows == 1))
608 return error_type;
609
610 /* GLSL matrix types are named mat{COLUMNS}x{ROWS}. Only the following
611 * combinations are valid:
612 *
613 * 1 2 3 4
614 * 1
615 * 2 x x x
616 * 3 x x x
617 * 4 x x x
618 */
619 #define IDX(c,r) (((c-1)*3) + (r-1))
620
621 if (base_type == GLSL_TYPE_DOUBLE) {
622 switch (IDX(columns, rows)) {
623 case IDX(2,2): return dmat2_type;
624 case IDX(2,3): return dmat2x3_type;
625 case IDX(2,4): return dmat2x4_type;
626 case IDX(3,2): return dmat3x2_type;
627 case IDX(3,3): return dmat3_type;
628 case IDX(3,4): return dmat3x4_type;
629 case IDX(4,2): return dmat4x2_type;
630 case IDX(4,3): return dmat4x3_type;
631 case IDX(4,4): return dmat4_type;
632 default: return error_type;
633 }
634 } else {
635 switch (IDX(columns, rows)) {
636 case IDX(2,2): return mat2_type;
637 case IDX(2,3): return mat2x3_type;
638 case IDX(2,4): return mat2x4_type;
639 case IDX(3,2): return mat3x2_type;
640 case IDX(3,3): return mat3_type;
641 case IDX(3,4): return mat3x4_type;
642 case IDX(4,2): return mat4x2_type;
643 case IDX(4,3): return mat4x3_type;
644 case IDX(4,4): return mat4_type;
645 default: return error_type;
646 }
647 }
648 }
649
650 assert(!"Should not get here.");
651 return error_type;
652 }
653
654 const glsl_type *
655 glsl_type::get_sampler_instance(enum glsl_sampler_dim dim,
656 bool shadow,
657 bool array,
658 glsl_base_type type)
659 {
660 switch (type) {
661 case GLSL_TYPE_FLOAT:
662 switch (dim) {
663 case GLSL_SAMPLER_DIM_1D:
664 if (shadow)
665 return (array ? sampler1DArrayShadow_type : sampler1DShadow_type);
666 else
667 return (array ? sampler1DArray_type : sampler1D_type);
668 case GLSL_SAMPLER_DIM_2D:
669 if (shadow)
670 return (array ? sampler2DArrayShadow_type : sampler2DShadow_type);
671 else
672 return (array ? sampler2DArray_type : sampler2D_type);
673 case GLSL_SAMPLER_DIM_3D:
674 if (shadow || array)
675 return error_type;
676 else
677 return sampler3D_type;
678 case GLSL_SAMPLER_DIM_CUBE:
679 if (shadow)
680 return (array ? samplerCubeArrayShadow_type : samplerCubeShadow_type);
681 else
682 return (array ? samplerCubeArray_type : samplerCube_type);
683 case GLSL_SAMPLER_DIM_RECT:
684 if (array)
685 return error_type;
686 if (shadow)
687 return sampler2DRectShadow_type;
688 else
689 return sampler2DRect_type;
690 case GLSL_SAMPLER_DIM_BUF:
691 if (shadow || array)
692 return error_type;
693 else
694 return samplerBuffer_type;
695 case GLSL_SAMPLER_DIM_MS:
696 if (shadow)
697 return error_type;
698 return (array ? sampler2DMSArray_type : sampler2DMS_type);
699 case GLSL_SAMPLER_DIM_EXTERNAL:
700 if (shadow || array)
701 return error_type;
702 else
703 return samplerExternalOES_type;
704 case GLSL_SAMPLER_DIM_SUBPASS:
705 case GLSL_SAMPLER_DIM_SUBPASS_MS:
706 return error_type;
707 }
708 case GLSL_TYPE_INT:
709 if (shadow)
710 return error_type;
711 switch (dim) {
712 case GLSL_SAMPLER_DIM_1D:
713 return (array ? isampler1DArray_type : isampler1D_type);
714 case GLSL_SAMPLER_DIM_2D:
715 return (array ? isampler2DArray_type : isampler2D_type);
716 case GLSL_SAMPLER_DIM_3D:
717 if (array)
718 return error_type;
719 return isampler3D_type;
720 case GLSL_SAMPLER_DIM_CUBE:
721 return (array ? isamplerCubeArray_type : isamplerCube_type);
722 case GLSL_SAMPLER_DIM_RECT:
723 if (array)
724 return error_type;
725 return isampler2DRect_type;
726 case GLSL_SAMPLER_DIM_BUF:
727 if (array)
728 return error_type;
729 return isamplerBuffer_type;
730 case GLSL_SAMPLER_DIM_MS:
731 return (array ? isampler2DMSArray_type : isampler2DMS_type);
732 case GLSL_SAMPLER_DIM_EXTERNAL:
733 return error_type;
734 case GLSL_SAMPLER_DIM_SUBPASS:
735 case GLSL_SAMPLER_DIM_SUBPASS_MS:
736 return error_type;
737 }
738 case GLSL_TYPE_UINT:
739 if (shadow)
740 return error_type;
741 switch (dim) {
742 case GLSL_SAMPLER_DIM_1D:
743 return (array ? usampler1DArray_type : usampler1D_type);
744 case GLSL_SAMPLER_DIM_2D:
745 return (array ? usampler2DArray_type : usampler2D_type);
746 case GLSL_SAMPLER_DIM_3D:
747 if (array)
748 return error_type;
749 return usampler3D_type;
750 case GLSL_SAMPLER_DIM_CUBE:
751 return (array ? usamplerCubeArray_type : usamplerCube_type);
752 case GLSL_SAMPLER_DIM_RECT:
753 if (array)
754 return error_type;
755 return usampler2DRect_type;
756 case GLSL_SAMPLER_DIM_BUF:
757 if (array)
758 return error_type;
759 return usamplerBuffer_type;
760 case GLSL_SAMPLER_DIM_MS:
761 return (array ? usampler2DMSArray_type : usampler2DMS_type);
762 case GLSL_SAMPLER_DIM_EXTERNAL:
763 return error_type;
764 case GLSL_SAMPLER_DIM_SUBPASS:
765 case GLSL_SAMPLER_DIM_SUBPASS_MS:
766 return error_type;
767 }
768 default:
769 return error_type;
770 }
771
772 unreachable("switch statement above should be complete");
773 }
774
775 const glsl_type *
776 glsl_type::get_image_instance(enum glsl_sampler_dim dim,
777 bool array, glsl_base_type type)
778 {
779 switch (type) {
780 case GLSL_TYPE_FLOAT:
781 switch (dim) {
782 case GLSL_SAMPLER_DIM_1D:
783 return (array ? image1DArray_type : image1D_type);
784 case GLSL_SAMPLER_DIM_2D:
785 return (array ? image2DArray_type : image2D_type);
786 case GLSL_SAMPLER_DIM_3D:
787 return image3D_type;
788 case GLSL_SAMPLER_DIM_CUBE:
789 return (array ? imageCubeArray_type : imageCube_type);
790 case GLSL_SAMPLER_DIM_RECT:
791 if (array)
792 return error_type;
793 else
794 return image2DRect_type;
795 case GLSL_SAMPLER_DIM_BUF:
796 if (array)
797 return error_type;
798 else
799 return imageBuffer_type;
800 case GLSL_SAMPLER_DIM_MS:
801 return (array ? image2DMSArray_type : image2DMS_type);
802 case GLSL_SAMPLER_DIM_SUBPASS:
803 return subpassInput_type;
804 case GLSL_SAMPLER_DIM_SUBPASS_MS:
805 return subpassInputMS_type;
806 case GLSL_SAMPLER_DIM_EXTERNAL:
807 return error_type;
808 }
809 case GLSL_TYPE_INT:
810 switch (dim) {
811 case GLSL_SAMPLER_DIM_1D:
812 return (array ? iimage1DArray_type : iimage1D_type);
813 case GLSL_SAMPLER_DIM_2D:
814 return (array ? iimage2DArray_type : iimage2D_type);
815 case GLSL_SAMPLER_DIM_3D:
816 if (array)
817 return error_type;
818 return iimage3D_type;
819 case GLSL_SAMPLER_DIM_CUBE:
820 return (array ? iimageCubeArray_type : iimageCube_type);
821 case GLSL_SAMPLER_DIM_RECT:
822 if (array)
823 return error_type;
824 return iimage2DRect_type;
825 case GLSL_SAMPLER_DIM_BUF:
826 if (array)
827 return error_type;
828 return iimageBuffer_type;
829 case GLSL_SAMPLER_DIM_MS:
830 return (array ? iimage2DMSArray_type : iimage2DMS_type);
831 case GLSL_SAMPLER_DIM_SUBPASS:
832 return isubpassInput_type;
833 case GLSL_SAMPLER_DIM_SUBPASS_MS:
834 return isubpassInputMS_type;
835 case GLSL_SAMPLER_DIM_EXTERNAL:
836 return error_type;
837 }
838 case GLSL_TYPE_UINT:
839 switch (dim) {
840 case GLSL_SAMPLER_DIM_1D:
841 return (array ? uimage1DArray_type : uimage1D_type);
842 case GLSL_SAMPLER_DIM_2D:
843 return (array ? uimage2DArray_type : uimage2D_type);
844 case GLSL_SAMPLER_DIM_3D:
845 if (array)
846 return error_type;
847 return uimage3D_type;
848 case GLSL_SAMPLER_DIM_CUBE:
849 return (array ? uimageCubeArray_type : uimageCube_type);
850 case GLSL_SAMPLER_DIM_RECT:
851 if (array)
852 return error_type;
853 return uimage2DRect_type;
854 case GLSL_SAMPLER_DIM_BUF:
855 if (array)
856 return error_type;
857 return uimageBuffer_type;
858 case GLSL_SAMPLER_DIM_MS:
859 return (array ? uimage2DMSArray_type : uimage2DMS_type);
860 case GLSL_SAMPLER_DIM_SUBPASS:
861 return usubpassInput_type;
862 case GLSL_SAMPLER_DIM_SUBPASS_MS:
863 return usubpassInputMS_type;
864 case GLSL_SAMPLER_DIM_EXTERNAL:
865 return error_type;
866 }
867 default:
868 return error_type;
869 }
870
871 unreachable("switch statement above should be complete");
872 }
873
874 const glsl_type *
875 glsl_type::get_array_instance(const glsl_type *base, unsigned array_size)
876 {
877 /* Generate a name using the base type pointer in the key. This is
878 * done because the name of the base type may not be unique across
879 * shaders. For example, two shaders may have different record types
880 * named 'foo'.
881 */
882 char key[128];
883 snprintf(key, sizeof(key), "%p[%u]", (void *) base, array_size);
884
885 mtx_lock(&glsl_type::mutex);
886
887 if (array_types == NULL) {
888 array_types = _mesa_hash_table_create(NULL, _mesa_key_hash_string,
889 _mesa_key_string_equal);
890 }
891
892 const struct hash_entry *entry = _mesa_hash_table_search(array_types, key);
893 if (entry == NULL) {
894 mtx_unlock(&glsl_type::mutex);
895 const glsl_type *t = new glsl_type(base, array_size);
896 mtx_lock(&glsl_type::mutex);
897
898 entry = _mesa_hash_table_insert(array_types,
899 ralloc_strdup(mem_ctx, key),
900 (void *) t);
901 }
902
903 assert(((glsl_type *) entry->data)->base_type == GLSL_TYPE_ARRAY);
904 assert(((glsl_type *) entry->data)->length == array_size);
905 assert(((glsl_type *) entry->data)->fields.array == base);
906
907 mtx_unlock(&glsl_type::mutex);
908
909 return (glsl_type *) entry->data;
910 }
911
912
913 bool
914 glsl_type::record_compare(const glsl_type *b, bool match_locations) const
915 {
916 if (this->length != b->length)
917 return false;
918
919 if (this->interface_packing != b->interface_packing)
920 return false;
921
922 if (this->interface_row_major != b->interface_row_major)
923 return false;
924
925 /* From the GLSL 4.20 specification (Sec 4.2):
926 *
927 * "Structures must have the same name, sequence of type names, and
928 * type definitions, and field names to be considered the same type."
929 *
930 * GLSL ES behaves the same (Ver 1.00 Sec 4.2.4, Ver 3.00 Sec 4.2.5).
931 *
932 * Note that we cannot force type name check when comparing unnamed
933 * structure types, these have a unique name assigned during parsing.
934 */
935 if (!this->is_anonymous() && !b->is_anonymous())
936 if (strcmp(this->name, b->name) != 0)
937 return false;
938
939 for (unsigned i = 0; i < this->length; i++) {
940 if (this->fields.structure[i].type != b->fields.structure[i].type)
941 return false;
942 if (strcmp(this->fields.structure[i].name,
943 b->fields.structure[i].name) != 0)
944 return false;
945 if (this->fields.structure[i].matrix_layout
946 != b->fields.structure[i].matrix_layout)
947 return false;
948 if (match_locations && this->fields.structure[i].location
949 != b->fields.structure[i].location)
950 return false;
951 if (this->fields.structure[i].offset
952 != b->fields.structure[i].offset)
953 return false;
954 if (this->fields.structure[i].interpolation
955 != b->fields.structure[i].interpolation)
956 return false;
957 if (this->fields.structure[i].centroid
958 != b->fields.structure[i].centroid)
959 return false;
960 if (this->fields.structure[i].sample
961 != b->fields.structure[i].sample)
962 return false;
963 if (this->fields.structure[i].patch
964 != b->fields.structure[i].patch)
965 return false;
966 if (this->fields.structure[i].memory_read_only
967 != b->fields.structure[i].memory_read_only)
968 return false;
969 if (this->fields.structure[i].memory_write_only
970 != b->fields.structure[i].memory_write_only)
971 return false;
972 if (this->fields.structure[i].memory_coherent
973 != b->fields.structure[i].memory_coherent)
974 return false;
975 if (this->fields.structure[i].memory_volatile
976 != b->fields.structure[i].memory_volatile)
977 return false;
978 if (this->fields.structure[i].memory_restrict
979 != b->fields.structure[i].memory_restrict)
980 return false;
981 if (this->fields.structure[i].image_format
982 != b->fields.structure[i].image_format)
983 return false;
984 if (this->fields.structure[i].precision
985 != b->fields.structure[i].precision)
986 return false;
987 if (this->fields.structure[i].explicit_xfb_buffer
988 != b->fields.structure[i].explicit_xfb_buffer)
989 return false;
990 if (this->fields.structure[i].xfb_buffer
991 != b->fields.structure[i].xfb_buffer)
992 return false;
993 if (this->fields.structure[i].xfb_stride
994 != b->fields.structure[i].xfb_stride)
995 return false;
996 }
997
998 return true;
999 }
1000
1001
1002 bool
1003 glsl_type::record_key_compare(const void *a, const void *b)
1004 {
1005 const glsl_type *const key1 = (glsl_type *) a;
1006 const glsl_type *const key2 = (glsl_type *) b;
1007
1008 return strcmp(key1->name, key2->name) == 0 && key1->record_compare(key2);
1009 }
1010
1011
1012 /**
1013 * Generate an integer hash value for a glsl_type structure type.
1014 */
1015 unsigned
1016 glsl_type::record_key_hash(const void *a)
1017 {
1018 const glsl_type *const key = (glsl_type *) a;
1019 uintptr_t hash = key->length;
1020 unsigned retval;
1021
1022 for (unsigned i = 0; i < key->length; i++) {
1023 /* casting pointer to uintptr_t */
1024 hash = (hash * 13 ) + (uintptr_t) key->fields.structure[i].type;
1025 }
1026
1027 if (sizeof(hash) == 8)
1028 retval = (hash & 0xffffffff) ^ ((uint64_t) hash >> 32);
1029 else
1030 retval = hash;
1031
1032 return retval;
1033 }
1034
1035
1036 const glsl_type *
1037 glsl_type::get_record_instance(const glsl_struct_field *fields,
1038 unsigned num_fields,
1039 const char *name)
1040 {
1041 const glsl_type key(fields, num_fields, name);
1042
1043 mtx_lock(&glsl_type::mutex);
1044
1045 if (record_types == NULL) {
1046 record_types = _mesa_hash_table_create(NULL, record_key_hash,
1047 record_key_compare);
1048 }
1049
1050 const struct hash_entry *entry = _mesa_hash_table_search(record_types,
1051 &key);
1052 if (entry == NULL) {
1053 mtx_unlock(&glsl_type::mutex);
1054 const glsl_type *t = new glsl_type(fields, num_fields, name);
1055 mtx_lock(&glsl_type::mutex);
1056
1057 entry = _mesa_hash_table_insert(record_types, t, (void *) t);
1058 }
1059
1060 assert(((glsl_type *) entry->data)->base_type == GLSL_TYPE_STRUCT);
1061 assert(((glsl_type *) entry->data)->length == num_fields);
1062 assert(strcmp(((glsl_type *) entry->data)->name, name) == 0);
1063
1064 mtx_unlock(&glsl_type::mutex);
1065
1066 return (glsl_type *) entry->data;
1067 }
1068
1069
1070 const glsl_type *
1071 glsl_type::get_interface_instance(const glsl_struct_field *fields,
1072 unsigned num_fields,
1073 enum glsl_interface_packing packing,
1074 bool row_major,
1075 const char *block_name)
1076 {
1077 const glsl_type key(fields, num_fields, packing, row_major, block_name);
1078
1079 mtx_lock(&glsl_type::mutex);
1080
1081 if (interface_types == NULL) {
1082 interface_types = _mesa_hash_table_create(NULL, record_key_hash,
1083 record_key_compare);
1084 }
1085
1086 const struct hash_entry *entry = _mesa_hash_table_search(interface_types,
1087 &key);
1088 if (entry == NULL) {
1089 mtx_unlock(&glsl_type::mutex);
1090 const glsl_type *t = new glsl_type(fields, num_fields,
1091 packing, row_major, block_name);
1092 mtx_lock(&glsl_type::mutex);
1093
1094 entry = _mesa_hash_table_insert(interface_types, t, (void *) t);
1095 }
1096
1097 assert(((glsl_type *) entry->data)->base_type == GLSL_TYPE_INTERFACE);
1098 assert(((glsl_type *) entry->data)->length == num_fields);
1099 assert(strcmp(((glsl_type *) entry->data)->name, block_name) == 0);
1100
1101 mtx_unlock(&glsl_type::mutex);
1102
1103 return (glsl_type *) entry->data;
1104 }
1105
1106 const glsl_type *
1107 glsl_type::get_subroutine_instance(const char *subroutine_name)
1108 {
1109 const glsl_type key(subroutine_name);
1110
1111 mtx_lock(&glsl_type::mutex);
1112
1113 if (subroutine_types == NULL) {
1114 subroutine_types = _mesa_hash_table_create(NULL, record_key_hash,
1115 record_key_compare);
1116 }
1117
1118 const struct hash_entry *entry = _mesa_hash_table_search(subroutine_types,
1119 &key);
1120 if (entry == NULL) {
1121 mtx_unlock(&glsl_type::mutex);
1122 const glsl_type *t = new glsl_type(subroutine_name);
1123 mtx_lock(&glsl_type::mutex);
1124
1125 entry = _mesa_hash_table_insert(subroutine_types, t, (void *) t);
1126 }
1127
1128 assert(((glsl_type *) entry->data)->base_type == GLSL_TYPE_SUBROUTINE);
1129 assert(strcmp(((glsl_type *) entry->data)->name, subroutine_name) == 0);
1130
1131 mtx_unlock(&glsl_type::mutex);
1132
1133 return (glsl_type *) entry->data;
1134 }
1135
1136
1137 static bool
1138 function_key_compare(const void *a, const void *b)
1139 {
1140 const glsl_type *const key1 = (glsl_type *) a;
1141 const glsl_type *const key2 = (glsl_type *) b;
1142
1143 if (key1->length != key2->length)
1144 return false;
1145
1146 return memcmp(key1->fields.parameters, key2->fields.parameters,
1147 (key1->length + 1) * sizeof(*key1->fields.parameters)) == 0;
1148 }
1149
1150
1151 static uint32_t
1152 function_key_hash(const void *a)
1153 {
1154 const glsl_type *const key = (glsl_type *) a;
1155 return _mesa_hash_data(key->fields.parameters,
1156 (key->length + 1) * sizeof(*key->fields.parameters));
1157 }
1158
1159 const glsl_type *
1160 glsl_type::get_function_instance(const glsl_type *return_type,
1161 const glsl_function_param *params,
1162 unsigned num_params)
1163 {
1164 const glsl_type key(return_type, params, num_params);
1165
1166 mtx_lock(&glsl_type::mutex);
1167
1168 if (function_types == NULL) {
1169 function_types = _mesa_hash_table_create(NULL, function_key_hash,
1170 function_key_compare);
1171 }
1172
1173 struct hash_entry *entry = _mesa_hash_table_search(function_types, &key);
1174 if (entry == NULL) {
1175 mtx_unlock(&glsl_type::mutex);
1176 const glsl_type *t = new glsl_type(return_type, params, num_params);
1177 mtx_lock(&glsl_type::mutex);
1178
1179 entry = _mesa_hash_table_insert(function_types, t, (void *) t);
1180 }
1181
1182 const glsl_type *t = (const glsl_type *)entry->data;
1183
1184 assert(t->base_type == GLSL_TYPE_FUNCTION);
1185 assert(t->length == num_params);
1186
1187 mtx_unlock(&glsl_type::mutex);
1188
1189 return t;
1190 }
1191
1192
1193 const glsl_type *
1194 glsl_type::get_mul_type(const glsl_type *type_a, const glsl_type *type_b)
1195 {
1196 if (type_a == type_b) {
1197 return type_a;
1198 } else if (type_a->is_matrix() && type_b->is_matrix()) {
1199 /* Matrix multiply. The columns of A must match the rows of B. Given
1200 * the other previously tested constraints, this means the vector type
1201 * of a row from A must be the same as the vector type of a column from
1202 * B.
1203 */
1204 if (type_a->row_type() == type_b->column_type()) {
1205 /* The resulting matrix has the number of columns of matrix B and
1206 * the number of rows of matrix A. We get the row count of A by
1207 * looking at the size of a vector that makes up a column. The
1208 * transpose (size of a row) is done for B.
1209 */
1210 const glsl_type *const type =
1211 get_instance(type_a->base_type,
1212 type_a->column_type()->vector_elements,
1213 type_b->row_type()->vector_elements);
1214 assert(type != error_type);
1215
1216 return type;
1217 }
1218 } else if (type_a->is_matrix()) {
1219 /* A is a matrix and B is a column vector. Columns of A must match
1220 * rows of B. Given the other previously tested constraints, this
1221 * means the vector type of a row from A must be the same as the
1222 * vector the type of B.
1223 */
1224 if (type_a->row_type() == type_b) {
1225 /* The resulting vector has a number of elements equal to
1226 * the number of rows of matrix A. */
1227 const glsl_type *const type =
1228 get_instance(type_a->base_type,
1229 type_a->column_type()->vector_elements,
1230 1);
1231 assert(type != error_type);
1232
1233 return type;
1234 }
1235 } else {
1236 assert(type_b->is_matrix());
1237
1238 /* A is a row vector and B is a matrix. Columns of A must match rows
1239 * of B. Given the other previously tested constraints, this means
1240 * the type of A must be the same as the vector type of a column from
1241 * B.
1242 */
1243 if (type_a == type_b->column_type()) {
1244 /* The resulting vector has a number of elements equal to
1245 * the number of columns of matrix B. */
1246 const glsl_type *const type =
1247 get_instance(type_a->base_type,
1248 type_b->row_type()->vector_elements,
1249 1);
1250 assert(type != error_type);
1251
1252 return type;
1253 }
1254 }
1255
1256 return error_type;
1257 }
1258
1259
1260 const glsl_type *
1261 glsl_type::field_type(const char *name) const
1262 {
1263 if (this->base_type != GLSL_TYPE_STRUCT
1264 && this->base_type != GLSL_TYPE_INTERFACE)
1265 return error_type;
1266
1267 for (unsigned i = 0; i < this->length; i++) {
1268 if (strcmp(name, this->fields.structure[i].name) == 0)
1269 return this->fields.structure[i].type;
1270 }
1271
1272 return error_type;
1273 }
1274
1275
1276 int
1277 glsl_type::field_index(const char *name) const
1278 {
1279 if (this->base_type != GLSL_TYPE_STRUCT
1280 && this->base_type != GLSL_TYPE_INTERFACE)
1281 return -1;
1282
1283 for (unsigned i = 0; i < this->length; i++) {
1284 if (strcmp(name, this->fields.structure[i].name) == 0)
1285 return i;
1286 }
1287
1288 return -1;
1289 }
1290
1291
1292 unsigned
1293 glsl_type::component_slots() const
1294 {
1295 switch (this->base_type) {
1296 case GLSL_TYPE_UINT:
1297 case GLSL_TYPE_INT:
1298 case GLSL_TYPE_FLOAT:
1299 case GLSL_TYPE_BOOL:
1300 return this->components();
1301
1302 case GLSL_TYPE_DOUBLE:
1303 case GLSL_TYPE_UINT64:
1304 case GLSL_TYPE_INT64:
1305 return 2 * this->components();
1306
1307 case GLSL_TYPE_STRUCT:
1308 case GLSL_TYPE_INTERFACE: {
1309 unsigned size = 0;
1310
1311 for (unsigned i = 0; i < this->length; i++)
1312 size += this->fields.structure[i].type->component_slots();
1313
1314 return size;
1315 }
1316
1317 case GLSL_TYPE_ARRAY:
1318 return this->length * this->fields.array->component_slots();
1319
1320 case GLSL_TYPE_SAMPLER:
1321 case GLSL_TYPE_IMAGE:
1322 return 2;
1323
1324 case GLSL_TYPE_SUBROUTINE:
1325 return 1;
1326
1327 case GLSL_TYPE_FUNCTION:
1328 case GLSL_TYPE_ATOMIC_UINT:
1329 case GLSL_TYPE_VOID:
1330 case GLSL_TYPE_ERROR:
1331 break;
1332 }
1333
1334 return 0;
1335 }
1336
1337 unsigned
1338 glsl_type::record_location_offset(unsigned length) const
1339 {
1340 unsigned offset = 0;
1341 const glsl_type *t = this->without_array();
1342 if (t->is_record()) {
1343 assert(length <= t->length);
1344
1345 for (unsigned i = 0; i < length; i++) {
1346 const glsl_type *st = t->fields.structure[i].type;
1347 const glsl_type *wa = st->without_array();
1348 if (wa->is_record()) {
1349 unsigned r_offset = wa->record_location_offset(wa->length);
1350 offset += st->is_array() ?
1351 st->arrays_of_arrays_size() * r_offset : r_offset;
1352 } else if (st->is_array() && st->fields.array->is_array()) {
1353 unsigned outer_array_size = st->length;
1354 const glsl_type *base_type = st->fields.array;
1355
1356 /* For arrays of arrays the outer arrays take up a uniform
1357 * slot for each element. The innermost array elements share a
1358 * single slot so we ignore the innermost array when calculating
1359 * the offset.
1360 */
1361 while (base_type->fields.array->is_array()) {
1362 outer_array_size = outer_array_size * base_type->length;
1363 base_type = base_type->fields.array;
1364 }
1365 offset += outer_array_size;
1366 } else {
1367 /* We dont worry about arrays here because unless the array
1368 * contains a structure or another array it only takes up a single
1369 * uniform slot.
1370 */
1371 offset += 1;
1372 }
1373 }
1374 }
1375 return offset;
1376 }
1377
1378 unsigned
1379 glsl_type::uniform_locations() const
1380 {
1381 unsigned size = 0;
1382
1383 switch (this->base_type) {
1384 case GLSL_TYPE_UINT:
1385 case GLSL_TYPE_INT:
1386 case GLSL_TYPE_FLOAT:
1387 case GLSL_TYPE_DOUBLE:
1388 case GLSL_TYPE_UINT64:
1389 case GLSL_TYPE_INT64:
1390 case GLSL_TYPE_BOOL:
1391 case GLSL_TYPE_SAMPLER:
1392 case GLSL_TYPE_IMAGE:
1393 case GLSL_TYPE_SUBROUTINE:
1394 return 1;
1395
1396 case GLSL_TYPE_STRUCT:
1397 case GLSL_TYPE_INTERFACE:
1398 for (unsigned i = 0; i < this->length; i++)
1399 size += this->fields.structure[i].type->uniform_locations();
1400 return size;
1401 case GLSL_TYPE_ARRAY:
1402 return this->length * this->fields.array->uniform_locations();
1403 default:
1404 return 0;
1405 }
1406 }
1407
1408 unsigned
1409 glsl_type::varying_count() const
1410 {
1411 unsigned size = 0;
1412
1413 switch (this->base_type) {
1414 case GLSL_TYPE_UINT:
1415 case GLSL_TYPE_INT:
1416 case GLSL_TYPE_FLOAT:
1417 case GLSL_TYPE_DOUBLE:
1418 case GLSL_TYPE_BOOL:
1419 case GLSL_TYPE_UINT64:
1420 case GLSL_TYPE_INT64:
1421 return 1;
1422
1423 case GLSL_TYPE_STRUCT:
1424 case GLSL_TYPE_INTERFACE:
1425 for (unsigned i = 0; i < this->length; i++)
1426 size += this->fields.structure[i].type->varying_count();
1427 return size;
1428 case GLSL_TYPE_ARRAY:
1429 /* Don't count innermost array elements */
1430 if (this->without_array()->is_record() ||
1431 this->without_array()->is_interface() ||
1432 this->fields.array->is_array())
1433 return this->length * this->fields.array->varying_count();
1434 else
1435 return this->fields.array->varying_count();
1436 default:
1437 assert(!"unsupported varying type");
1438 return 0;
1439 }
1440 }
1441
1442 bool
1443 glsl_type::can_implicitly_convert_to(const glsl_type *desired,
1444 _mesa_glsl_parse_state *state) const
1445 {
1446 if (this == desired)
1447 return true;
1448
1449 /* GLSL 1.10 and ESSL do not allow implicit conversions. If there is no
1450 * state, we're doing intra-stage function linking where these checks have
1451 * already been done.
1452 */
1453 if (state && (state->es_shader || !state->is_version(120, 0)))
1454 return false;
1455
1456 /* There is no conversion among matrix types. */
1457 if (this->matrix_columns > 1 || desired->matrix_columns > 1)
1458 return false;
1459
1460 /* Vector size must match. */
1461 if (this->vector_elements != desired->vector_elements)
1462 return false;
1463
1464 /* int and uint can be converted to float. */
1465 if (desired->is_float() && this->is_integer())
1466 return true;
1467
1468 /* With GLSL 4.0, ARB_gpu_shader5, or MESA_shader_integer_functions, int
1469 * can be converted to uint. Note that state may be NULL here, when
1470 * resolving function calls in the linker. By this time, all the
1471 * state-dependent checks have already happened though, so allow anything
1472 * that's allowed in any shader version.
1473 */
1474 if ((!state || state->is_version(400, 0) || state->ARB_gpu_shader5_enable ||
1475 state->MESA_shader_integer_functions_enable) &&
1476 desired->base_type == GLSL_TYPE_UINT && this->base_type == GLSL_TYPE_INT)
1477 return true;
1478
1479 /* No implicit conversions from double. */
1480 if ((!state || state->has_double()) && this->is_double())
1481 return false;
1482
1483 /* Conversions from different types to double. */
1484 if ((!state || state->has_double()) && desired->is_double()) {
1485 if (this->is_float())
1486 return true;
1487 if (this->is_integer())
1488 return true;
1489 }
1490
1491 return false;
1492 }
1493
1494 unsigned
1495 glsl_type::std140_base_alignment(bool row_major) const
1496 {
1497 unsigned N = is_64bit() ? 8 : 4;
1498
1499 /* (1) If the member is a scalar consuming <N> basic machine units, the
1500 * base alignment is <N>.
1501 *
1502 * (2) If the member is a two- or four-component vector with components
1503 * consuming <N> basic machine units, the base alignment is 2<N> or
1504 * 4<N>, respectively.
1505 *
1506 * (3) If the member is a three-component vector with components consuming
1507 * <N> basic machine units, the base alignment is 4<N>.
1508 */
1509 if (this->is_scalar() || this->is_vector()) {
1510 switch (this->vector_elements) {
1511 case 1:
1512 return N;
1513 case 2:
1514 return 2 * N;
1515 case 3:
1516 case 4:
1517 return 4 * N;
1518 }
1519 }
1520
1521 /* (4) If the member is an array of scalars or vectors, the base alignment
1522 * and array stride are set to match the base alignment of a single
1523 * array element, according to rules (1), (2), and (3), and rounded up
1524 * to the base alignment of a vec4. The array may have padding at the
1525 * end; the base offset of the member following the array is rounded up
1526 * to the next multiple of the base alignment.
1527 *
1528 * (6) If the member is an array of <S> column-major matrices with <C>
1529 * columns and <R> rows, the matrix is stored identically to a row of
1530 * <S>*<C> column vectors with <R> components each, according to rule
1531 * (4).
1532 *
1533 * (8) If the member is an array of <S> row-major matrices with <C> columns
1534 * and <R> rows, the matrix is stored identically to a row of <S>*<R>
1535 * row vectors with <C> components each, according to rule (4).
1536 *
1537 * (10) If the member is an array of <S> structures, the <S> elements of
1538 * the array are laid out in order, according to rule (9).
1539 */
1540 if (this->is_array()) {
1541 if (this->fields.array->is_scalar() ||
1542 this->fields.array->is_vector() ||
1543 this->fields.array->is_matrix()) {
1544 return MAX2(this->fields.array->std140_base_alignment(row_major), 16);
1545 } else {
1546 assert(this->fields.array->is_record() ||
1547 this->fields.array->is_array());
1548 return this->fields.array->std140_base_alignment(row_major);
1549 }
1550 }
1551
1552 /* (5) If the member is a column-major matrix with <C> columns and
1553 * <R> rows, the matrix is stored identically to an array of
1554 * <C> column vectors with <R> components each, according to
1555 * rule (4).
1556 *
1557 * (7) If the member is a row-major matrix with <C> columns and <R>
1558 * rows, the matrix is stored identically to an array of <R>
1559 * row vectors with <C> components each, according to rule (4).
1560 */
1561 if (this->is_matrix()) {
1562 const struct glsl_type *vec_type, *array_type;
1563 int c = this->matrix_columns;
1564 int r = this->vector_elements;
1565
1566 if (row_major) {
1567 vec_type = get_instance(base_type, c, 1);
1568 array_type = glsl_type::get_array_instance(vec_type, r);
1569 } else {
1570 vec_type = get_instance(base_type, r, 1);
1571 array_type = glsl_type::get_array_instance(vec_type, c);
1572 }
1573
1574 return array_type->std140_base_alignment(false);
1575 }
1576
1577 /* (9) If the member is a structure, the base alignment of the
1578 * structure is <N>, where <N> is the largest base alignment
1579 * value of any of its members, and rounded up to the base
1580 * alignment of a vec4. The individual members of this
1581 * sub-structure are then assigned offsets by applying this set
1582 * of rules recursively, where the base offset of the first
1583 * member of the sub-structure is equal to the aligned offset
1584 * of the structure. The structure may have padding at the end;
1585 * the base offset of the member following the sub-structure is
1586 * rounded up to the next multiple of the base alignment of the
1587 * structure.
1588 */
1589 if (this->is_record()) {
1590 unsigned base_alignment = 16;
1591 for (unsigned i = 0; i < this->length; i++) {
1592 bool field_row_major = row_major;
1593 const enum glsl_matrix_layout matrix_layout =
1594 glsl_matrix_layout(this->fields.structure[i].matrix_layout);
1595 if (matrix_layout == GLSL_MATRIX_LAYOUT_ROW_MAJOR) {
1596 field_row_major = true;
1597 } else if (matrix_layout == GLSL_MATRIX_LAYOUT_COLUMN_MAJOR) {
1598 field_row_major = false;
1599 }
1600
1601 const struct glsl_type *field_type = this->fields.structure[i].type;
1602 base_alignment = MAX2(base_alignment,
1603 field_type->std140_base_alignment(field_row_major));
1604 }
1605 return base_alignment;
1606 }
1607
1608 assert(!"not reached");
1609 return -1;
1610 }
1611
1612 unsigned
1613 glsl_type::std140_size(bool row_major) const
1614 {
1615 unsigned N = is_64bit() ? 8 : 4;
1616
1617 /* (1) If the member is a scalar consuming <N> basic machine units, the
1618 * base alignment is <N>.
1619 *
1620 * (2) If the member is a two- or four-component vector with components
1621 * consuming <N> basic machine units, the base alignment is 2<N> or
1622 * 4<N>, respectively.
1623 *
1624 * (3) If the member is a three-component vector with components consuming
1625 * <N> basic machine units, the base alignment is 4<N>.
1626 */
1627 if (this->is_scalar() || this->is_vector()) {
1628 return this->vector_elements * N;
1629 }
1630
1631 /* (5) If the member is a column-major matrix with <C> columns and
1632 * <R> rows, the matrix is stored identically to an array of
1633 * <C> column vectors with <R> components each, according to
1634 * rule (4).
1635 *
1636 * (6) If the member is an array of <S> column-major matrices with <C>
1637 * columns and <R> rows, the matrix is stored identically to a row of
1638 * <S>*<C> column vectors with <R> components each, according to rule
1639 * (4).
1640 *
1641 * (7) If the member is a row-major matrix with <C> columns and <R>
1642 * rows, the matrix is stored identically to an array of <R>
1643 * row vectors with <C> components each, according to rule (4).
1644 *
1645 * (8) If the member is an array of <S> row-major matrices with <C> columns
1646 * and <R> rows, the matrix is stored identically to a row of <S>*<R>
1647 * row vectors with <C> components each, according to rule (4).
1648 */
1649 if (this->without_array()->is_matrix()) {
1650 const struct glsl_type *element_type;
1651 const struct glsl_type *vec_type;
1652 unsigned int array_len;
1653
1654 if (this->is_array()) {
1655 element_type = this->without_array();
1656 array_len = this->arrays_of_arrays_size();
1657 } else {
1658 element_type = this;
1659 array_len = 1;
1660 }
1661
1662 if (row_major) {
1663 vec_type = get_instance(element_type->base_type,
1664 element_type->matrix_columns, 1);
1665
1666 array_len *= element_type->vector_elements;
1667 } else {
1668 vec_type = get_instance(element_type->base_type,
1669 element_type->vector_elements, 1);
1670 array_len *= element_type->matrix_columns;
1671 }
1672 const glsl_type *array_type = glsl_type::get_array_instance(vec_type,
1673 array_len);
1674
1675 return array_type->std140_size(false);
1676 }
1677
1678 /* (4) If the member is an array of scalars or vectors, the base alignment
1679 * and array stride are set to match the base alignment of a single
1680 * array element, according to rules (1), (2), and (3), and rounded up
1681 * to the base alignment of a vec4. The array may have padding at the
1682 * end; the base offset of the member following the array is rounded up
1683 * to the next multiple of the base alignment.
1684 *
1685 * (10) If the member is an array of <S> structures, the <S> elements of
1686 * the array are laid out in order, according to rule (9).
1687 */
1688 if (this->is_array()) {
1689 if (this->without_array()->is_record()) {
1690 return this->arrays_of_arrays_size() *
1691 this->without_array()->std140_size(row_major);
1692 } else {
1693 unsigned element_base_align =
1694 this->without_array()->std140_base_alignment(row_major);
1695 return this->arrays_of_arrays_size() * MAX2(element_base_align, 16);
1696 }
1697 }
1698
1699 /* (9) If the member is a structure, the base alignment of the
1700 * structure is <N>, where <N> is the largest base alignment
1701 * value of any of its members, and rounded up to the base
1702 * alignment of a vec4. The individual members of this
1703 * sub-structure are then assigned offsets by applying this set
1704 * of rules recursively, where the base offset of the first
1705 * member of the sub-structure is equal to the aligned offset
1706 * of the structure. The structure may have padding at the end;
1707 * the base offset of the member following the sub-structure is
1708 * rounded up to the next multiple of the base alignment of the
1709 * structure.
1710 */
1711 if (this->is_record() || this->is_interface()) {
1712 unsigned size = 0;
1713 unsigned max_align = 0;
1714
1715 for (unsigned i = 0; i < this->length; i++) {
1716 bool field_row_major = row_major;
1717 const enum glsl_matrix_layout matrix_layout =
1718 glsl_matrix_layout(this->fields.structure[i].matrix_layout);
1719 if (matrix_layout == GLSL_MATRIX_LAYOUT_ROW_MAJOR) {
1720 field_row_major = true;
1721 } else if (matrix_layout == GLSL_MATRIX_LAYOUT_COLUMN_MAJOR) {
1722 field_row_major = false;
1723 }
1724
1725 const struct glsl_type *field_type = this->fields.structure[i].type;
1726 unsigned align = field_type->std140_base_alignment(field_row_major);
1727
1728 /* Ignore unsized arrays when calculating size */
1729 if (field_type->is_unsized_array())
1730 continue;
1731
1732 size = glsl_align(size, align);
1733 size += field_type->std140_size(field_row_major);
1734
1735 max_align = MAX2(align, max_align);
1736
1737 if (field_type->is_record() && (i + 1 < this->length))
1738 size = glsl_align(size, 16);
1739 }
1740 size = glsl_align(size, MAX2(max_align, 16));
1741 return size;
1742 }
1743
1744 assert(!"not reached");
1745 return -1;
1746 }
1747
1748 unsigned
1749 glsl_type::std430_base_alignment(bool row_major) const
1750 {
1751
1752 unsigned N = is_64bit() ? 8 : 4;
1753
1754 /* (1) If the member is a scalar consuming <N> basic machine units, the
1755 * base alignment is <N>.
1756 *
1757 * (2) If the member is a two- or four-component vector with components
1758 * consuming <N> basic machine units, the base alignment is 2<N> or
1759 * 4<N>, respectively.
1760 *
1761 * (3) If the member is a three-component vector with components consuming
1762 * <N> basic machine units, the base alignment is 4<N>.
1763 */
1764 if (this->is_scalar() || this->is_vector()) {
1765 switch (this->vector_elements) {
1766 case 1:
1767 return N;
1768 case 2:
1769 return 2 * N;
1770 case 3:
1771 case 4:
1772 return 4 * N;
1773 }
1774 }
1775
1776 /* OpenGL 4.30 spec, section 7.6.2.2 "Standard Uniform Block Layout":
1777 *
1778 * "When using the std430 storage layout, shader storage blocks will be
1779 * laid out in buffer storage identically to uniform and shader storage
1780 * blocks using the std140 layout, except that the base alignment and
1781 * stride of arrays of scalars and vectors in rule 4 and of structures
1782 * in rule 9 are not rounded up a multiple of the base alignment of a vec4.
1783 */
1784
1785 /* (1) If the member is a scalar consuming <N> basic machine units, the
1786 * base alignment is <N>.
1787 *
1788 * (2) If the member is a two- or four-component vector with components
1789 * consuming <N> basic machine units, the base alignment is 2<N> or
1790 * 4<N>, respectively.
1791 *
1792 * (3) If the member is a three-component vector with components consuming
1793 * <N> basic machine units, the base alignment is 4<N>.
1794 */
1795 if (this->is_array())
1796 return this->fields.array->std430_base_alignment(row_major);
1797
1798 /* (5) If the member is a column-major matrix with <C> columns and
1799 * <R> rows, the matrix is stored identically to an array of
1800 * <C> column vectors with <R> components each, according to
1801 * rule (4).
1802 *
1803 * (7) If the member is a row-major matrix with <C> columns and <R>
1804 * rows, the matrix is stored identically to an array of <R>
1805 * row vectors with <C> components each, according to rule (4).
1806 */
1807 if (this->is_matrix()) {
1808 const struct glsl_type *vec_type, *array_type;
1809 int c = this->matrix_columns;
1810 int r = this->vector_elements;
1811
1812 if (row_major) {
1813 vec_type = get_instance(base_type, c, 1);
1814 array_type = glsl_type::get_array_instance(vec_type, r);
1815 } else {
1816 vec_type = get_instance(base_type, r, 1);
1817 array_type = glsl_type::get_array_instance(vec_type, c);
1818 }
1819
1820 return array_type->std430_base_alignment(false);
1821 }
1822
1823 /* (9) If the member is a structure, the base alignment of the
1824 * structure is <N>, where <N> is the largest base alignment
1825 * value of any of its members, and rounded up to the base
1826 * alignment of a vec4. The individual members of this
1827 * sub-structure are then assigned offsets by applying this set
1828 * of rules recursively, where the base offset of the first
1829 * member of the sub-structure is equal to the aligned offset
1830 * of the structure. The structure may have padding at the end;
1831 * the base offset of the member following the sub-structure is
1832 * rounded up to the next multiple of the base alignment of the
1833 * structure.
1834 */
1835 if (this->is_record()) {
1836 unsigned base_alignment = 0;
1837 for (unsigned i = 0; i < this->length; i++) {
1838 bool field_row_major = row_major;
1839 const enum glsl_matrix_layout matrix_layout =
1840 glsl_matrix_layout(this->fields.structure[i].matrix_layout);
1841 if (matrix_layout == GLSL_MATRIX_LAYOUT_ROW_MAJOR) {
1842 field_row_major = true;
1843 } else if (matrix_layout == GLSL_MATRIX_LAYOUT_COLUMN_MAJOR) {
1844 field_row_major = false;
1845 }
1846
1847 const struct glsl_type *field_type = this->fields.structure[i].type;
1848 base_alignment = MAX2(base_alignment,
1849 field_type->std430_base_alignment(field_row_major));
1850 }
1851 assert(base_alignment > 0);
1852 return base_alignment;
1853 }
1854 assert(!"not reached");
1855 return -1;
1856 }
1857
1858 unsigned
1859 glsl_type::std430_array_stride(bool row_major) const
1860 {
1861 unsigned N = is_64bit() ? 8 : 4;
1862
1863 /* Notice that the array stride of a vec3 is not 3 * N but 4 * N.
1864 * See OpenGL 4.30 spec, section 7.6.2.2 "Standard Uniform Block Layout"
1865 *
1866 * (3) If the member is a three-component vector with components consuming
1867 * <N> basic machine units, the base alignment is 4<N>.
1868 */
1869 if (this->is_vector() && this->vector_elements == 3)
1870 return 4 * N;
1871
1872 /* By default use std430_size(row_major) */
1873 return this->std430_size(row_major);
1874 }
1875
1876 unsigned
1877 glsl_type::std430_size(bool row_major) const
1878 {
1879 unsigned N = is_64bit() ? 8 : 4;
1880
1881 /* OpenGL 4.30 spec, section 7.6.2.2 "Standard Uniform Block Layout":
1882 *
1883 * "When using the std430 storage layout, shader storage blocks will be
1884 * laid out in buffer storage identically to uniform and shader storage
1885 * blocks using the std140 layout, except that the base alignment and
1886 * stride of arrays of scalars and vectors in rule 4 and of structures
1887 * in rule 9 are not rounded up a multiple of the base alignment of a vec4.
1888 */
1889 if (this->is_scalar() || this->is_vector())
1890 return this->vector_elements * N;
1891
1892 if (this->without_array()->is_matrix()) {
1893 const struct glsl_type *element_type;
1894 const struct glsl_type *vec_type;
1895 unsigned int array_len;
1896
1897 if (this->is_array()) {
1898 element_type = this->without_array();
1899 array_len = this->arrays_of_arrays_size();
1900 } else {
1901 element_type = this;
1902 array_len = 1;
1903 }
1904
1905 if (row_major) {
1906 vec_type = get_instance(element_type->base_type,
1907 element_type->matrix_columns, 1);
1908
1909 array_len *= element_type->vector_elements;
1910 } else {
1911 vec_type = get_instance(element_type->base_type,
1912 element_type->vector_elements, 1);
1913 array_len *= element_type->matrix_columns;
1914 }
1915 const glsl_type *array_type = glsl_type::get_array_instance(vec_type,
1916 array_len);
1917
1918 return array_type->std430_size(false);
1919 }
1920
1921 if (this->is_array()) {
1922 if (this->without_array()->is_record())
1923 return this->arrays_of_arrays_size() *
1924 this->without_array()->std430_size(row_major);
1925 else
1926 return this->arrays_of_arrays_size() *
1927 this->without_array()->std430_base_alignment(row_major);
1928 }
1929
1930 if (this->is_record() || this->is_interface()) {
1931 unsigned size = 0;
1932 unsigned max_align = 0;
1933
1934 for (unsigned i = 0; i < this->length; i++) {
1935 bool field_row_major = row_major;
1936 const enum glsl_matrix_layout matrix_layout =
1937 glsl_matrix_layout(this->fields.structure[i].matrix_layout);
1938 if (matrix_layout == GLSL_MATRIX_LAYOUT_ROW_MAJOR) {
1939 field_row_major = true;
1940 } else if (matrix_layout == GLSL_MATRIX_LAYOUT_COLUMN_MAJOR) {
1941 field_row_major = false;
1942 }
1943
1944 const struct glsl_type *field_type = this->fields.structure[i].type;
1945 unsigned align = field_type->std430_base_alignment(field_row_major);
1946 size = glsl_align(size, align);
1947 size += field_type->std430_size(field_row_major);
1948
1949 max_align = MAX2(align, max_align);
1950 }
1951 size = glsl_align(size, max_align);
1952 return size;
1953 }
1954
1955 assert(!"not reached");
1956 return -1;
1957 }
1958
1959 unsigned
1960 glsl_type::count_attribute_slots(bool is_vertex_input) const
1961 {
1962 /* From page 31 (page 37 of the PDF) of the GLSL 1.50 spec:
1963 *
1964 * "A scalar input counts the same amount against this limit as a vec4,
1965 * so applications may want to consider packing groups of four
1966 * unrelated float inputs together into a vector to better utilize the
1967 * capabilities of the underlying hardware. A matrix input will use up
1968 * multiple locations. The number of locations used will equal the
1969 * number of columns in the matrix."
1970 *
1971 * The spec does not explicitly say how arrays are counted. However, it
1972 * should be safe to assume the total number of slots consumed by an array
1973 * is the number of entries in the array multiplied by the number of slots
1974 * consumed by a single element of the array.
1975 *
1976 * The spec says nothing about how structs are counted, because vertex
1977 * attributes are not allowed to be (or contain) structs. However, Mesa
1978 * allows varying structs, the number of varying slots taken up by a
1979 * varying struct is simply equal to the sum of the number of slots taken
1980 * up by each element.
1981 *
1982 * Doubles are counted different depending on whether they are vertex
1983 * inputs or everything else. Vertex inputs from ARB_vertex_attrib_64bit
1984 * take one location no matter what size they are, otherwise dvec3/4
1985 * take two locations.
1986 */
1987 switch (this->base_type) {
1988 case GLSL_TYPE_UINT:
1989 case GLSL_TYPE_INT:
1990 case GLSL_TYPE_FLOAT:
1991 case GLSL_TYPE_BOOL:
1992 case GLSL_TYPE_SAMPLER:
1993 case GLSL_TYPE_IMAGE:
1994 return this->matrix_columns;
1995 case GLSL_TYPE_DOUBLE:
1996 case GLSL_TYPE_UINT64:
1997 case GLSL_TYPE_INT64:
1998 if (this->vector_elements > 2 && !is_vertex_input)
1999 return this->matrix_columns * 2;
2000 else
2001 return this->matrix_columns;
2002 case GLSL_TYPE_STRUCT:
2003 case GLSL_TYPE_INTERFACE: {
2004 unsigned size = 0;
2005
2006 for (unsigned i = 0; i < this->length; i++)
2007 size += this->fields.structure[i].type->count_attribute_slots(is_vertex_input);
2008
2009 return size;
2010 }
2011
2012 case GLSL_TYPE_ARRAY:
2013 return this->length * this->fields.array->count_attribute_slots(is_vertex_input);
2014
2015 case GLSL_TYPE_FUNCTION:
2016 case GLSL_TYPE_ATOMIC_UINT:
2017 case GLSL_TYPE_VOID:
2018 case GLSL_TYPE_SUBROUTINE:
2019 case GLSL_TYPE_ERROR:
2020 break;
2021 }
2022
2023 assert(!"Unexpected type in count_attribute_slots()");
2024
2025 return 0;
2026 }
2027
2028 int
2029 glsl_type::coordinate_components() const
2030 {
2031 int size;
2032
2033 switch (sampler_dimensionality) {
2034 case GLSL_SAMPLER_DIM_1D:
2035 case GLSL_SAMPLER_DIM_BUF:
2036 size = 1;
2037 break;
2038 case GLSL_SAMPLER_DIM_2D:
2039 case GLSL_SAMPLER_DIM_RECT:
2040 case GLSL_SAMPLER_DIM_MS:
2041 case GLSL_SAMPLER_DIM_EXTERNAL:
2042 case GLSL_SAMPLER_DIM_SUBPASS:
2043 size = 2;
2044 break;
2045 case GLSL_SAMPLER_DIM_3D:
2046 case GLSL_SAMPLER_DIM_CUBE:
2047 size = 3;
2048 break;
2049 default:
2050 assert(!"Should not get here.");
2051 size = 1;
2052 break;
2053 }
2054
2055 /* Array textures need an additional component for the array index, except
2056 * for cubemap array images that behave like a 2D array of interleaved
2057 * cubemap faces.
2058 */
2059 if (sampler_array &&
2060 !(is_image() && sampler_dimensionality == GLSL_SAMPLER_DIM_CUBE))
2061 size += 1;
2062
2063 return size;
2064 }
2065
2066 /**
2067 * Declarations of type flyweights (glsl_type::_foo_type) and
2068 * convenience pointers (glsl_type::foo_type).
2069 * @{
2070 */
2071 #define DECL_TYPE(NAME, ...) \
2072 const glsl_type glsl_type::_##NAME##_type = glsl_type(__VA_ARGS__, #NAME); \
2073 const glsl_type *const glsl_type::NAME##_type = &glsl_type::_##NAME##_type;
2074
2075 #define STRUCT_TYPE(NAME)
2076
2077 #include "compiler/builtin_type_macros.h"
2078 /** @} */