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