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