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