90517a5b52f067cb7ada0fcba983affc1d65e766
[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_locations) const
1012 {
1013 if (this->length != b->length)
1014 return false;
1015
1016 if (this->interface_packing != b->interface_packing)
1017 return false;
1018
1019 if (this->interface_row_major != b->interface_row_major)
1020 return false;
1021
1022 /* From the GLSL 4.20 specification (Sec 4.2):
1023 *
1024 * "Structures must have the same name, sequence of type names, and
1025 * type definitions, and field names to be considered the same type."
1026 *
1027 * GLSL ES behaves the same (Ver 1.00 Sec 4.2.4, Ver 3.00 Sec 4.2.5).
1028 */
1029 if (strcmp(this->name, b->name) != 0)
1030 return false;
1031
1032 for (unsigned i = 0; i < this->length; i++) {
1033 if (this->fields.structure[i].type != b->fields.structure[i].type)
1034 return false;
1035 if (strcmp(this->fields.structure[i].name,
1036 b->fields.structure[i].name) != 0)
1037 return false;
1038 if (this->fields.structure[i].matrix_layout
1039 != b->fields.structure[i].matrix_layout)
1040 return false;
1041 if (match_locations && this->fields.structure[i].location
1042 != b->fields.structure[i].location)
1043 return false;
1044 if (this->fields.structure[i].offset
1045 != b->fields.structure[i].offset)
1046 return false;
1047 if (this->fields.structure[i].interpolation
1048 != b->fields.structure[i].interpolation)
1049 return false;
1050 if (this->fields.structure[i].centroid
1051 != b->fields.structure[i].centroid)
1052 return false;
1053 if (this->fields.structure[i].sample
1054 != b->fields.structure[i].sample)
1055 return false;
1056 if (this->fields.structure[i].patch
1057 != b->fields.structure[i].patch)
1058 return false;
1059 if (this->fields.structure[i].memory_read_only
1060 != b->fields.structure[i].memory_read_only)
1061 return false;
1062 if (this->fields.structure[i].memory_write_only
1063 != b->fields.structure[i].memory_write_only)
1064 return false;
1065 if (this->fields.structure[i].memory_coherent
1066 != b->fields.structure[i].memory_coherent)
1067 return false;
1068 if (this->fields.structure[i].memory_volatile
1069 != b->fields.structure[i].memory_volatile)
1070 return false;
1071 if (this->fields.structure[i].memory_restrict
1072 != b->fields.structure[i].memory_restrict)
1073 return false;
1074 if (this->fields.structure[i].image_format
1075 != b->fields.structure[i].image_format)
1076 return false;
1077 if (this->fields.structure[i].precision
1078 != b->fields.structure[i].precision)
1079 return false;
1080 if (this->fields.structure[i].explicit_xfb_buffer
1081 != b->fields.structure[i].explicit_xfb_buffer)
1082 return false;
1083 if (this->fields.structure[i].xfb_buffer
1084 != b->fields.structure[i].xfb_buffer)
1085 return false;
1086 if (this->fields.structure[i].xfb_stride
1087 != b->fields.structure[i].xfb_stride)
1088 return false;
1089 }
1090
1091 return true;
1092 }
1093
1094
1095 bool
1096 glsl_type::record_key_compare(const void *a, const void *b)
1097 {
1098 const glsl_type *const key1 = (glsl_type *) a;
1099 const glsl_type *const key2 = (glsl_type *) b;
1100
1101 return strcmp(key1->name, key2->name) == 0 && key1->record_compare(key2);
1102 }
1103
1104
1105 /**
1106 * Generate an integer hash value for a glsl_type structure type.
1107 */
1108 unsigned
1109 glsl_type::record_key_hash(const void *a)
1110 {
1111 const glsl_type *const key = (glsl_type *) a;
1112 uintptr_t hash = key->length;
1113 unsigned retval;
1114
1115 for (unsigned i = 0; i < key->length; i++) {
1116 /* casting pointer to uintptr_t */
1117 hash = (hash * 13 ) + (uintptr_t) key->fields.structure[i].type;
1118 }
1119
1120 if (sizeof(hash) == 8)
1121 retval = (hash & 0xffffffff) ^ ((uint64_t) hash >> 32);
1122 else
1123 retval = hash;
1124
1125 return retval;
1126 }
1127
1128
1129 const glsl_type *
1130 glsl_type::get_struct_instance(const glsl_struct_field *fields,
1131 unsigned num_fields,
1132 const char *name,
1133 bool packed)
1134 {
1135 const glsl_type key(fields, num_fields, name, packed);
1136
1137 mtx_lock(&glsl_type::hash_mutex);
1138
1139 if (struct_types == NULL) {
1140 struct_types = _mesa_hash_table_create(NULL, record_key_hash,
1141 record_key_compare);
1142 }
1143
1144 const struct hash_entry *entry = _mesa_hash_table_search(struct_types,
1145 &key);
1146 if (entry == NULL) {
1147 const glsl_type *t = new glsl_type(fields, num_fields, name, packed);
1148
1149 entry = _mesa_hash_table_insert(struct_types, t, (void *) t);
1150 }
1151
1152 assert(((glsl_type *) entry->data)->base_type == GLSL_TYPE_STRUCT);
1153 assert(((glsl_type *) entry->data)->length == num_fields);
1154 assert(strcmp(((glsl_type *) entry->data)->name, name) == 0);
1155 assert(((glsl_type *) entry->data)->packed == packed);
1156
1157 mtx_unlock(&glsl_type::hash_mutex);
1158
1159 return (glsl_type *) entry->data;
1160 }
1161
1162
1163 const glsl_type *
1164 glsl_type::get_interface_instance(const glsl_struct_field *fields,
1165 unsigned num_fields,
1166 enum glsl_interface_packing packing,
1167 bool row_major,
1168 const char *block_name)
1169 {
1170 const glsl_type key(fields, num_fields, packing, row_major, block_name);
1171
1172 mtx_lock(&glsl_type::hash_mutex);
1173
1174 if (interface_types == NULL) {
1175 interface_types = _mesa_hash_table_create(NULL, record_key_hash,
1176 record_key_compare);
1177 }
1178
1179 const struct hash_entry *entry = _mesa_hash_table_search(interface_types,
1180 &key);
1181 if (entry == NULL) {
1182 const glsl_type *t = new glsl_type(fields, num_fields,
1183 packing, row_major, block_name);
1184
1185 entry = _mesa_hash_table_insert(interface_types, t, (void *) t);
1186 }
1187
1188 assert(((glsl_type *) entry->data)->base_type == GLSL_TYPE_INTERFACE);
1189 assert(((glsl_type *) entry->data)->length == num_fields);
1190 assert(strcmp(((glsl_type *) entry->data)->name, block_name) == 0);
1191
1192 mtx_unlock(&glsl_type::hash_mutex);
1193
1194 return (glsl_type *) entry->data;
1195 }
1196
1197 const glsl_type *
1198 glsl_type::get_subroutine_instance(const char *subroutine_name)
1199 {
1200 const glsl_type key(subroutine_name);
1201
1202 mtx_lock(&glsl_type::hash_mutex);
1203
1204 if (subroutine_types == NULL) {
1205 subroutine_types = _mesa_hash_table_create(NULL, record_key_hash,
1206 record_key_compare);
1207 }
1208
1209 const struct hash_entry *entry = _mesa_hash_table_search(subroutine_types,
1210 &key);
1211 if (entry == NULL) {
1212 const glsl_type *t = new glsl_type(subroutine_name);
1213
1214 entry = _mesa_hash_table_insert(subroutine_types, t, (void *) t);
1215 }
1216
1217 assert(((glsl_type *) entry->data)->base_type == GLSL_TYPE_SUBROUTINE);
1218 assert(strcmp(((glsl_type *) entry->data)->name, subroutine_name) == 0);
1219
1220 mtx_unlock(&glsl_type::hash_mutex);
1221
1222 return (glsl_type *) entry->data;
1223 }
1224
1225
1226 static bool
1227 function_key_compare(const void *a, const void *b)
1228 {
1229 const glsl_type *const key1 = (glsl_type *) a;
1230 const glsl_type *const key2 = (glsl_type *) b;
1231
1232 if (key1->length != key2->length)
1233 return false;
1234
1235 return memcmp(key1->fields.parameters, key2->fields.parameters,
1236 (key1->length + 1) * sizeof(*key1->fields.parameters)) == 0;
1237 }
1238
1239
1240 static uint32_t
1241 function_key_hash(const void *a)
1242 {
1243 const glsl_type *const key = (glsl_type *) a;
1244 return _mesa_hash_data(key->fields.parameters,
1245 (key->length + 1) * sizeof(*key->fields.parameters));
1246 }
1247
1248 const glsl_type *
1249 glsl_type::get_function_instance(const glsl_type *return_type,
1250 const glsl_function_param *params,
1251 unsigned num_params)
1252 {
1253 const glsl_type key(return_type, params, num_params);
1254
1255 mtx_lock(&glsl_type::hash_mutex);
1256
1257 if (function_types == NULL) {
1258 function_types = _mesa_hash_table_create(NULL, function_key_hash,
1259 function_key_compare);
1260 }
1261
1262 struct hash_entry *entry = _mesa_hash_table_search(function_types, &key);
1263 if (entry == NULL) {
1264 const glsl_type *t = new glsl_type(return_type, params, num_params);
1265
1266 entry = _mesa_hash_table_insert(function_types, t, (void *) t);
1267 }
1268
1269 const glsl_type *t = (const glsl_type *)entry->data;
1270
1271 assert(t->base_type == GLSL_TYPE_FUNCTION);
1272 assert(t->length == num_params);
1273
1274 mtx_unlock(&glsl_type::hash_mutex);
1275
1276 return t;
1277 }
1278
1279
1280 const glsl_type *
1281 glsl_type::get_mul_type(const glsl_type *type_a, const glsl_type *type_b)
1282 {
1283 if (type_a == type_b) {
1284 return type_a;
1285 } else if (type_a->is_matrix() && type_b->is_matrix()) {
1286 /* Matrix multiply. The columns of A must match the rows of B. Given
1287 * the other previously tested constraints, this means the vector type
1288 * of a row from A must be the same as the vector type of a column from
1289 * B.
1290 */
1291 if (type_a->row_type() == type_b->column_type()) {
1292 /* The resulting matrix has the number of columns of matrix B and
1293 * the number of rows of matrix A. We get the row count of A by
1294 * looking at the size of a vector that makes up a column. The
1295 * transpose (size of a row) is done for B.
1296 */
1297 const glsl_type *const type =
1298 get_instance(type_a->base_type,
1299 type_a->column_type()->vector_elements,
1300 type_b->row_type()->vector_elements);
1301 assert(type != error_type);
1302
1303 return type;
1304 }
1305 } else if (type_a->is_matrix()) {
1306 /* A is a matrix and B is a column vector. Columns of A must match
1307 * rows of B. Given the other previously tested constraints, this
1308 * means the vector type of a row from A must be the same as the
1309 * vector the type of B.
1310 */
1311 if (type_a->row_type() == type_b) {
1312 /* The resulting vector has a number of elements equal to
1313 * the number of rows of matrix A. */
1314 const glsl_type *const type =
1315 get_instance(type_a->base_type,
1316 type_a->column_type()->vector_elements,
1317 1);
1318 assert(type != error_type);
1319
1320 return type;
1321 }
1322 } else {
1323 assert(type_b->is_matrix());
1324
1325 /* A is a row vector and B is a matrix. Columns of A must match rows
1326 * of B. Given the other previously tested constraints, this means
1327 * the type of A must be the same as the vector type of a column from
1328 * B.
1329 */
1330 if (type_a == type_b->column_type()) {
1331 /* The resulting vector has a number of elements equal to
1332 * the number of columns of matrix B. */
1333 const glsl_type *const type =
1334 get_instance(type_a->base_type,
1335 type_b->row_type()->vector_elements,
1336 1);
1337 assert(type != error_type);
1338
1339 return type;
1340 }
1341 }
1342
1343 return error_type;
1344 }
1345
1346
1347 const glsl_type *
1348 glsl_type::field_type(const char *name) const
1349 {
1350 if (this->base_type != GLSL_TYPE_STRUCT
1351 && this->base_type != GLSL_TYPE_INTERFACE)
1352 return error_type;
1353
1354 for (unsigned i = 0; i < this->length; i++) {
1355 if (strcmp(name, this->fields.structure[i].name) == 0)
1356 return this->fields.structure[i].type;
1357 }
1358
1359 return error_type;
1360 }
1361
1362
1363 int
1364 glsl_type::field_index(const char *name) const
1365 {
1366 if (this->base_type != GLSL_TYPE_STRUCT
1367 && this->base_type != GLSL_TYPE_INTERFACE)
1368 return -1;
1369
1370 for (unsigned i = 0; i < this->length; i++) {
1371 if (strcmp(name, this->fields.structure[i].name) == 0)
1372 return i;
1373 }
1374
1375 return -1;
1376 }
1377
1378
1379 unsigned
1380 glsl_type::component_slots() const
1381 {
1382 switch (this->base_type) {
1383 case GLSL_TYPE_UINT:
1384 case GLSL_TYPE_INT:
1385 case GLSL_TYPE_UINT8:
1386 case GLSL_TYPE_INT8:
1387 case GLSL_TYPE_UINT16:
1388 case GLSL_TYPE_INT16:
1389 case GLSL_TYPE_FLOAT:
1390 case GLSL_TYPE_FLOAT16:
1391 case GLSL_TYPE_BOOL:
1392 return this->components();
1393
1394 case GLSL_TYPE_DOUBLE:
1395 case GLSL_TYPE_UINT64:
1396 case GLSL_TYPE_INT64:
1397 return 2 * this->components();
1398
1399 case GLSL_TYPE_STRUCT:
1400 case GLSL_TYPE_INTERFACE: {
1401 unsigned size = 0;
1402
1403 for (unsigned i = 0; i < this->length; i++)
1404 size += this->fields.structure[i].type->component_slots();
1405
1406 return size;
1407 }
1408
1409 case GLSL_TYPE_ARRAY:
1410 return this->length * this->fields.array->component_slots();
1411
1412 case GLSL_TYPE_SAMPLER:
1413 case GLSL_TYPE_IMAGE:
1414 return 2;
1415
1416 case GLSL_TYPE_SUBROUTINE:
1417 return 1;
1418
1419 case GLSL_TYPE_FUNCTION:
1420 case GLSL_TYPE_ATOMIC_UINT:
1421 case GLSL_TYPE_VOID:
1422 case GLSL_TYPE_ERROR:
1423 break;
1424 }
1425
1426 return 0;
1427 }
1428
1429 unsigned
1430 glsl_type::struct_location_offset(unsigned length) const
1431 {
1432 unsigned offset = 0;
1433 const glsl_type *t = this->without_array();
1434 if (t->is_struct()) {
1435 assert(length <= t->length);
1436
1437 for (unsigned i = 0; i < length; i++) {
1438 const glsl_type *st = t->fields.structure[i].type;
1439 const glsl_type *wa = st->without_array();
1440 if (wa->is_struct()) {
1441 unsigned r_offset = wa->struct_location_offset(wa->length);
1442 offset += st->is_array() ?
1443 st->arrays_of_arrays_size() * r_offset : r_offset;
1444 } else if (st->is_array() && st->fields.array->is_array()) {
1445 unsigned outer_array_size = st->length;
1446 const glsl_type *base_type = st->fields.array;
1447
1448 /* For arrays of arrays the outer arrays take up a uniform
1449 * slot for each element. The innermost array elements share a
1450 * single slot so we ignore the innermost array when calculating
1451 * the offset.
1452 */
1453 while (base_type->fields.array->is_array()) {
1454 outer_array_size = outer_array_size * base_type->length;
1455 base_type = base_type->fields.array;
1456 }
1457 offset += outer_array_size;
1458 } else {
1459 /* We dont worry about arrays here because unless the array
1460 * contains a structure or another array it only takes up a single
1461 * uniform slot.
1462 */
1463 offset += 1;
1464 }
1465 }
1466 }
1467 return offset;
1468 }
1469
1470 unsigned
1471 glsl_type::uniform_locations() const
1472 {
1473 unsigned size = 0;
1474
1475 switch (this->base_type) {
1476 case GLSL_TYPE_UINT:
1477 case GLSL_TYPE_INT:
1478 case GLSL_TYPE_FLOAT:
1479 case GLSL_TYPE_FLOAT16:
1480 case GLSL_TYPE_DOUBLE:
1481 case GLSL_TYPE_UINT16:
1482 case GLSL_TYPE_UINT8:
1483 case GLSL_TYPE_INT16:
1484 case GLSL_TYPE_INT8:
1485 case GLSL_TYPE_UINT64:
1486 case GLSL_TYPE_INT64:
1487 case GLSL_TYPE_BOOL:
1488 case GLSL_TYPE_SAMPLER:
1489 case GLSL_TYPE_IMAGE:
1490 case GLSL_TYPE_SUBROUTINE:
1491 return 1;
1492
1493 case GLSL_TYPE_STRUCT:
1494 case GLSL_TYPE_INTERFACE:
1495 for (unsigned i = 0; i < this->length; i++)
1496 size += this->fields.structure[i].type->uniform_locations();
1497 return size;
1498 case GLSL_TYPE_ARRAY:
1499 return this->length * this->fields.array->uniform_locations();
1500 default:
1501 return 0;
1502 }
1503 }
1504
1505 unsigned
1506 glsl_type::varying_count() const
1507 {
1508 unsigned size = 0;
1509
1510 switch (this->base_type) {
1511 case GLSL_TYPE_UINT:
1512 case GLSL_TYPE_INT:
1513 case GLSL_TYPE_FLOAT:
1514 case GLSL_TYPE_FLOAT16:
1515 case GLSL_TYPE_DOUBLE:
1516 case GLSL_TYPE_BOOL:
1517 case GLSL_TYPE_UINT16:
1518 case GLSL_TYPE_UINT8:
1519 case GLSL_TYPE_INT16:
1520 case GLSL_TYPE_INT8:
1521 case GLSL_TYPE_UINT64:
1522 case GLSL_TYPE_INT64:
1523 return 1;
1524
1525 case GLSL_TYPE_STRUCT:
1526 case GLSL_TYPE_INTERFACE:
1527 for (unsigned i = 0; i < this->length; i++)
1528 size += this->fields.structure[i].type->varying_count();
1529 return size;
1530 case GLSL_TYPE_ARRAY:
1531 /* Don't count innermost array elements */
1532 if (this->without_array()->is_struct() ||
1533 this->without_array()->is_interface() ||
1534 this->fields.array->is_array())
1535 return this->length * this->fields.array->varying_count();
1536 else
1537 return this->fields.array->varying_count();
1538 default:
1539 assert(!"unsupported varying type");
1540 return 0;
1541 }
1542 }
1543
1544 bool
1545 glsl_type::can_implicitly_convert_to(const glsl_type *desired,
1546 _mesa_glsl_parse_state *state) const
1547 {
1548 if (this == desired)
1549 return true;
1550
1551 /* GLSL 1.10 and ESSL do not allow implicit conversions. If there is no
1552 * state, we're doing intra-stage function linking where these checks have
1553 * already been done.
1554 */
1555 if (state && !state->has_implicit_conversions())
1556 return false;
1557
1558 /* There is no conversion among matrix types. */
1559 if (this->matrix_columns > 1 || desired->matrix_columns > 1)
1560 return false;
1561
1562 /* Vector size must match. */
1563 if (this->vector_elements != desired->vector_elements)
1564 return false;
1565
1566 /* int and uint can be converted to float. */
1567 if (desired->is_float() && this->is_integer())
1568 return true;
1569
1570 /* With GLSL 4.0, ARB_gpu_shader5, or MESA_shader_integer_functions, int
1571 * can be converted to uint. Note that state may be NULL here, when
1572 * resolving function calls in the linker. By this time, all the
1573 * state-dependent checks have already happened though, so allow anything
1574 * that's allowed in any shader version.
1575 */
1576 if ((!state || state->has_implicit_uint_to_int_conversion()) &&
1577 desired->base_type == GLSL_TYPE_UINT && this->base_type == GLSL_TYPE_INT)
1578 return true;
1579
1580 /* No implicit conversions from double. */
1581 if ((!state || state->has_double()) && this->is_double())
1582 return false;
1583
1584 /* Conversions from different types to double. */
1585 if ((!state || state->has_double()) && desired->is_double()) {
1586 if (this->is_float())
1587 return true;
1588 if (this->is_integer())
1589 return true;
1590 }
1591
1592 return false;
1593 }
1594
1595 unsigned
1596 glsl_type::std140_base_alignment(bool row_major) const
1597 {
1598 unsigned N = is_64bit() ? 8 : 4;
1599
1600 /* (1) If the member is a scalar consuming <N> basic machine units, the
1601 * base alignment is <N>.
1602 *
1603 * (2) If the member is a two- or four-component vector with components
1604 * consuming <N> basic machine units, the base alignment is 2<N> or
1605 * 4<N>, respectively.
1606 *
1607 * (3) If the member is a three-component vector with components consuming
1608 * <N> basic machine units, the base alignment is 4<N>.
1609 */
1610 if (this->is_scalar() || this->is_vector()) {
1611 switch (this->vector_elements) {
1612 case 1:
1613 return N;
1614 case 2:
1615 return 2 * N;
1616 case 3:
1617 case 4:
1618 return 4 * N;
1619 }
1620 }
1621
1622 /* (4) If the member is an array of scalars or vectors, the base alignment
1623 * and array stride are set to match the base alignment of a single
1624 * array element, according to rules (1), (2), and (3), and rounded up
1625 * to the base alignment of a vec4. The array may have padding at the
1626 * end; the base offset of the member following the array is rounded up
1627 * to the next multiple of the base alignment.
1628 *
1629 * (6) If the member is an array of <S> column-major matrices with <C>
1630 * columns and <R> rows, the matrix is stored identically to a row of
1631 * <S>*<C> column vectors with <R> components each, according to rule
1632 * (4).
1633 *
1634 * (8) If the member is an array of <S> row-major matrices with <C> columns
1635 * and <R> rows, the matrix is stored identically to a row of <S>*<R>
1636 * row vectors with <C> components each, according to rule (4).
1637 *
1638 * (10) If the member is an array of <S> structures, the <S> elements of
1639 * the array are laid out in order, according to rule (9).
1640 */
1641 if (this->is_array()) {
1642 if (this->fields.array->is_scalar() ||
1643 this->fields.array->is_vector() ||
1644 this->fields.array->is_matrix()) {
1645 return MAX2(this->fields.array->std140_base_alignment(row_major), 16);
1646 } else {
1647 assert(this->fields.array->is_struct() ||
1648 this->fields.array->is_array());
1649 return this->fields.array->std140_base_alignment(row_major);
1650 }
1651 }
1652
1653 /* (5) If the member is a column-major matrix with <C> columns and
1654 * <R> rows, the matrix is stored identically to an array of
1655 * <C> column vectors with <R> components each, according to
1656 * rule (4).
1657 *
1658 * (7) If the member is a row-major matrix with <C> columns and <R>
1659 * rows, the matrix is stored identically to an array of <R>
1660 * row vectors with <C> components each, according to rule (4).
1661 */
1662 if (this->is_matrix()) {
1663 const struct glsl_type *vec_type, *array_type;
1664 int c = this->matrix_columns;
1665 int r = this->vector_elements;
1666
1667 if (row_major) {
1668 vec_type = get_instance(base_type, c, 1);
1669 array_type = glsl_type::get_array_instance(vec_type, r);
1670 } else {
1671 vec_type = get_instance(base_type, r, 1);
1672 array_type = glsl_type::get_array_instance(vec_type, c);
1673 }
1674
1675 return array_type->std140_base_alignment(false);
1676 }
1677
1678 /* (9) If the member is a structure, the base alignment of the
1679 * structure is <N>, where <N> is the largest base alignment
1680 * value of any of its members, and rounded up to the base
1681 * alignment of a vec4. The individual members of this
1682 * sub-structure are then assigned offsets by applying this set
1683 * of rules recursively, where the base offset of the first
1684 * member of the sub-structure is equal to the aligned offset
1685 * of the structure. The structure may have padding at the end;
1686 * the base offset of the member following the sub-structure is
1687 * rounded up to the next multiple of the base alignment of the
1688 * structure.
1689 */
1690 if (this->is_struct()) {
1691 unsigned base_alignment = 16;
1692 for (unsigned i = 0; i < this->length; i++) {
1693 bool field_row_major = row_major;
1694 const enum glsl_matrix_layout matrix_layout =
1695 glsl_matrix_layout(this->fields.structure[i].matrix_layout);
1696 if (matrix_layout == GLSL_MATRIX_LAYOUT_ROW_MAJOR) {
1697 field_row_major = true;
1698 } else if (matrix_layout == GLSL_MATRIX_LAYOUT_COLUMN_MAJOR) {
1699 field_row_major = false;
1700 }
1701
1702 const struct glsl_type *field_type = this->fields.structure[i].type;
1703 base_alignment = MAX2(base_alignment,
1704 field_type->std140_base_alignment(field_row_major));
1705 }
1706 return base_alignment;
1707 }
1708
1709 assert(!"not reached");
1710 return -1;
1711 }
1712
1713 unsigned
1714 glsl_type::std140_size(bool row_major) const
1715 {
1716 unsigned N = is_64bit() ? 8 : 4;
1717
1718 /* (1) If the member is a scalar consuming <N> basic machine units, the
1719 * base alignment is <N>.
1720 *
1721 * (2) If the member is a two- or four-component vector with components
1722 * consuming <N> basic machine units, the base alignment is 2<N> or
1723 * 4<N>, respectively.
1724 *
1725 * (3) If the member is a three-component vector with components consuming
1726 * <N> basic machine units, the base alignment is 4<N>.
1727 */
1728 if (this->is_scalar() || this->is_vector()) {
1729 assert(this->explicit_stride == 0);
1730 return this->vector_elements * N;
1731 }
1732
1733 /* (5) If the member is a column-major matrix with <C> columns and
1734 * <R> rows, the matrix is stored identically to an array of
1735 * <C> column vectors with <R> components each, according to
1736 * rule (4).
1737 *
1738 * (6) If the member is an array of <S> column-major matrices with <C>
1739 * columns and <R> rows, the matrix is stored identically to a row of
1740 * <S>*<C> column vectors with <R> components each, according to rule
1741 * (4).
1742 *
1743 * (7) If the member is a row-major matrix with <C> columns and <R>
1744 * rows, the matrix is stored identically to an array of <R>
1745 * row vectors with <C> components each, according to rule (4).
1746 *
1747 * (8) If the member is an array of <S> row-major matrices with <C> columns
1748 * and <R> rows, the matrix is stored identically to a row of <S>*<R>
1749 * row vectors with <C> components each, according to rule (4).
1750 */
1751 if (this->without_array()->is_matrix()) {
1752 const struct glsl_type *element_type;
1753 const struct glsl_type *vec_type;
1754 unsigned int array_len;
1755
1756 if (this->is_array()) {
1757 element_type = this->without_array();
1758 array_len = this->arrays_of_arrays_size();
1759 } else {
1760 element_type = this;
1761 array_len = 1;
1762 }
1763
1764 if (row_major) {
1765 vec_type = get_instance(element_type->base_type,
1766 element_type->matrix_columns, 1);
1767
1768 array_len *= element_type->vector_elements;
1769 } else {
1770 vec_type = get_instance(element_type->base_type,
1771 element_type->vector_elements, 1);
1772 array_len *= element_type->matrix_columns;
1773 }
1774 const glsl_type *array_type = glsl_type::get_array_instance(vec_type,
1775 array_len);
1776
1777 return array_type->std140_size(false);
1778 }
1779
1780 /* (4) If the member is an array of scalars or vectors, the base alignment
1781 * and array stride are set to match the base alignment of a single
1782 * array element, according to rules (1), (2), and (3), and rounded up
1783 * to the base alignment of a vec4. The array may have padding at the
1784 * end; the base offset of the member following the array is rounded up
1785 * to the next multiple of the base alignment.
1786 *
1787 * (10) If the member is an array of <S> structures, the <S> elements of
1788 * the array are laid out in order, according to rule (9).
1789 */
1790 if (this->is_array()) {
1791 unsigned stride;
1792 if (this->without_array()->is_struct()) {
1793 stride = this->without_array()->std140_size(row_major);
1794 } else {
1795 unsigned element_base_align =
1796 this->without_array()->std140_base_alignment(row_major);
1797 stride = MAX2(element_base_align, 16);
1798 }
1799
1800 unsigned size = this->arrays_of_arrays_size() * stride;
1801 assert(this->explicit_stride == 0 ||
1802 size == this->length * this->explicit_stride);
1803 return size;
1804 }
1805
1806 /* (9) If the member is a structure, the base alignment of the
1807 * structure is <N>, where <N> is the largest base alignment
1808 * value of any of its members, and rounded up to the base
1809 * alignment of a vec4. The individual members of this
1810 * sub-structure are then assigned offsets by applying this set
1811 * of rules recursively, where the base offset of the first
1812 * member of the sub-structure is equal to the aligned offset
1813 * of the structure. The structure may have padding at the end;
1814 * the base offset of the member following the sub-structure is
1815 * rounded up to the next multiple of the base alignment of the
1816 * structure.
1817 */
1818 if (this->is_struct() || this->is_interface()) {
1819 unsigned size = 0;
1820 unsigned max_align = 0;
1821
1822 for (unsigned i = 0; i < this->length; i++) {
1823 bool field_row_major = row_major;
1824 const enum glsl_matrix_layout matrix_layout =
1825 glsl_matrix_layout(this->fields.structure[i].matrix_layout);
1826 if (matrix_layout == GLSL_MATRIX_LAYOUT_ROW_MAJOR) {
1827 field_row_major = true;
1828 } else if (matrix_layout == GLSL_MATRIX_LAYOUT_COLUMN_MAJOR) {
1829 field_row_major = false;
1830 }
1831
1832 const struct glsl_type *field_type = this->fields.structure[i].type;
1833 unsigned align = field_type->std140_base_alignment(field_row_major);
1834
1835 /* Ignore unsized arrays when calculating size */
1836 if (field_type->is_unsized_array())
1837 continue;
1838
1839 size = glsl_align(size, align);
1840 size += field_type->std140_size(field_row_major);
1841
1842 max_align = MAX2(align, max_align);
1843
1844 if (field_type->is_struct() && (i + 1 < this->length))
1845 size = glsl_align(size, 16);
1846 }
1847 size = glsl_align(size, MAX2(max_align, 16));
1848 return size;
1849 }
1850
1851 assert(!"not reached");
1852 return -1;
1853 }
1854
1855 const glsl_type *
1856 glsl_type::get_explicit_std140_type(bool row_major) const
1857 {
1858 if (this->is_vector() || this->is_scalar()) {
1859 return this;
1860 } else if (this->is_matrix()) {
1861 const glsl_type *vec_type;
1862 if (row_major)
1863 vec_type = get_instance(this->base_type, this->matrix_columns, 1);
1864 else
1865 vec_type = get_instance(this->base_type, this->vector_elements, 1);
1866 unsigned elem_size = vec_type->std140_size(false);
1867 unsigned stride = glsl_align(elem_size, 16);
1868 return get_instance(this->base_type, this->vector_elements,
1869 this->matrix_columns, stride, row_major);
1870 } else if (this->is_array()) {
1871 unsigned elem_size = this->fields.array->std140_size(row_major);
1872 const glsl_type *elem_type =
1873 this->fields.array->get_explicit_std140_type(row_major);
1874 unsigned stride = glsl_align(elem_size, 16);
1875 return get_array_instance(elem_type, this->length, stride);
1876 } else if (this->is_struct() || this->is_interface()) {
1877 glsl_struct_field *fields = new glsl_struct_field[this->length];
1878 unsigned offset = 0;
1879 for (unsigned i = 0; i < length; i++) {
1880 fields[i] = this->fields.structure[i];
1881
1882 bool field_row_major = row_major;
1883 if (fields[i].matrix_layout == GLSL_MATRIX_LAYOUT_COLUMN_MAJOR) {
1884 field_row_major = false;
1885 } else if (fields[i].matrix_layout == GLSL_MATRIX_LAYOUT_ROW_MAJOR) {
1886 field_row_major = true;
1887 }
1888 fields[i].type =
1889 fields[i].type->get_explicit_std140_type(field_row_major);
1890
1891 unsigned fsize = fields[i].type->std140_size(field_row_major);
1892 unsigned falign = fields[i].type->std140_base_alignment(field_row_major);
1893 /* From the GLSL 460 spec section "Uniform and Shader Storage Block
1894 * Layout Qualifiers":
1895 *
1896 * "The actual offset of a member is computed as follows: If
1897 * offset was declared, start with that offset, otherwise start
1898 * with the next available offset. If the resulting offset is not
1899 * a multiple of the actual alignment, increase it to the first
1900 * offset that is a multiple of the actual alignment. This results
1901 * in the actual offset the member will have."
1902 */
1903 if (fields[i].offset >= 0) {
1904 assert((unsigned)fields[i].offset >= offset);
1905 offset = fields[i].offset;
1906 }
1907 offset = glsl_align(offset, falign);
1908 fields[i].offset = offset;
1909 offset += fsize;
1910 }
1911
1912 const glsl_type *type;
1913 if (this->is_struct())
1914 type = get_struct_instance(fields, this->length, this->name);
1915 else
1916 type = get_interface_instance(fields, this->length,
1917 (enum glsl_interface_packing)this->interface_packing,
1918 this->interface_row_major,
1919 this->name);
1920
1921 delete[] fields;
1922 return type;
1923 } else {
1924 unreachable("Invalid type for UBO or SSBO");
1925 }
1926 }
1927
1928 unsigned
1929 glsl_type::std430_base_alignment(bool row_major) const
1930 {
1931
1932 unsigned N = is_64bit() ? 8 : 4;
1933
1934 /* (1) If the member is a scalar consuming <N> basic machine units, the
1935 * base alignment is <N>.
1936 *
1937 * (2) If the member is a two- or four-component vector with components
1938 * consuming <N> basic machine units, the base alignment is 2<N> or
1939 * 4<N>, respectively.
1940 *
1941 * (3) If the member is a three-component vector with components consuming
1942 * <N> basic machine units, the base alignment is 4<N>.
1943 */
1944 if (this->is_scalar() || this->is_vector()) {
1945 switch (this->vector_elements) {
1946 case 1:
1947 return N;
1948 case 2:
1949 return 2 * N;
1950 case 3:
1951 case 4:
1952 return 4 * N;
1953 }
1954 }
1955
1956 /* OpenGL 4.30 spec, section 7.6.2.2 "Standard Uniform Block Layout":
1957 *
1958 * "When using the std430 storage layout, shader storage blocks will be
1959 * laid out in buffer storage identically to uniform and shader storage
1960 * blocks using the std140 layout, except that the base alignment and
1961 * stride of arrays of scalars and vectors in rule 4 and of structures
1962 * in rule 9 are not rounded up a multiple of the base alignment of a vec4.
1963 */
1964
1965 /* (1) If the member is a scalar consuming <N> basic machine units, the
1966 * base alignment is <N>.
1967 *
1968 * (2) If the member is a two- or four-component vector with components
1969 * consuming <N> basic machine units, the base alignment is 2<N> or
1970 * 4<N>, respectively.
1971 *
1972 * (3) If the member is a three-component vector with components consuming
1973 * <N> basic machine units, the base alignment is 4<N>.
1974 */
1975 if (this->is_array())
1976 return this->fields.array->std430_base_alignment(row_major);
1977
1978 /* (5) If the member is a column-major matrix with <C> columns and
1979 * <R> rows, the matrix is stored identically to an array of
1980 * <C> column vectors with <R> components each, according to
1981 * rule (4).
1982 *
1983 * (7) If the member is a row-major matrix with <C> columns and <R>
1984 * rows, the matrix is stored identically to an array of <R>
1985 * row vectors with <C> components each, according to rule (4).
1986 */
1987 if (this->is_matrix()) {
1988 const struct glsl_type *vec_type, *array_type;
1989 int c = this->matrix_columns;
1990 int r = this->vector_elements;
1991
1992 if (row_major) {
1993 vec_type = get_instance(base_type, c, 1);
1994 array_type = glsl_type::get_array_instance(vec_type, r);
1995 } else {
1996 vec_type = get_instance(base_type, r, 1);
1997 array_type = glsl_type::get_array_instance(vec_type, c);
1998 }
1999
2000 return array_type->std430_base_alignment(false);
2001 }
2002
2003 /* (9) If the member is a structure, the base alignment of the
2004 * structure is <N>, where <N> is the largest base alignment
2005 * value of any of its members, and rounded up to the base
2006 * alignment of a vec4. The individual members of this
2007 * sub-structure are then assigned offsets by applying this set
2008 * of rules recursively, where the base offset of the first
2009 * member of the sub-structure is equal to the aligned offset
2010 * of the structure. The structure may have padding at the end;
2011 * the base offset of the member following the sub-structure is
2012 * rounded up to the next multiple of the base alignment of the
2013 * structure.
2014 */
2015 if (this->is_struct()) {
2016 unsigned base_alignment = 0;
2017 for (unsigned i = 0; i < this->length; i++) {
2018 bool field_row_major = row_major;
2019 const enum glsl_matrix_layout matrix_layout =
2020 glsl_matrix_layout(this->fields.structure[i].matrix_layout);
2021 if (matrix_layout == GLSL_MATRIX_LAYOUT_ROW_MAJOR) {
2022 field_row_major = true;
2023 } else if (matrix_layout == GLSL_MATRIX_LAYOUT_COLUMN_MAJOR) {
2024 field_row_major = false;
2025 }
2026
2027 const struct glsl_type *field_type = this->fields.structure[i].type;
2028 base_alignment = MAX2(base_alignment,
2029 field_type->std430_base_alignment(field_row_major));
2030 }
2031 assert(base_alignment > 0);
2032 return base_alignment;
2033 }
2034 assert(!"not reached");
2035 return -1;
2036 }
2037
2038 unsigned
2039 glsl_type::std430_array_stride(bool row_major) const
2040 {
2041 unsigned N = is_64bit() ? 8 : 4;
2042
2043 /* Notice that the array stride of a vec3 is not 3 * N but 4 * N.
2044 * See OpenGL 4.30 spec, section 7.6.2.2 "Standard Uniform Block Layout"
2045 *
2046 * (3) If the member is a three-component vector with components consuming
2047 * <N> basic machine units, the base alignment is 4<N>.
2048 */
2049 if (this->is_vector() && this->vector_elements == 3)
2050 return 4 * N;
2051
2052 /* By default use std430_size(row_major) */
2053 unsigned stride = this->std430_size(row_major);
2054 assert(this->explicit_stride == 0 || this->explicit_stride == stride);
2055 return stride;
2056 }
2057
2058 unsigned
2059 glsl_type::std430_size(bool row_major) const
2060 {
2061 unsigned N = is_64bit() ? 8 : 4;
2062
2063 /* OpenGL 4.30 spec, section 7.6.2.2 "Standard Uniform Block Layout":
2064 *
2065 * "When using the std430 storage layout, shader storage blocks will be
2066 * laid out in buffer storage identically to uniform and shader storage
2067 * blocks using the std140 layout, except that the base alignment and
2068 * stride of arrays of scalars and vectors in rule 4 and of structures
2069 * in rule 9 are not rounded up a multiple of the base alignment of a vec4.
2070 */
2071 if (this->is_scalar() || this->is_vector()) {
2072 assert(this->explicit_stride == 0);
2073 return this->vector_elements * N;
2074 }
2075
2076 if (this->without_array()->is_matrix()) {
2077 const struct glsl_type *element_type;
2078 const struct glsl_type *vec_type;
2079 unsigned int array_len;
2080
2081 if (this->is_array()) {
2082 element_type = this->without_array();
2083 array_len = this->arrays_of_arrays_size();
2084 } else {
2085 element_type = this;
2086 array_len = 1;
2087 }
2088
2089 if (row_major) {
2090 vec_type = get_instance(element_type->base_type,
2091 element_type->matrix_columns, 1);
2092
2093 array_len *= element_type->vector_elements;
2094 } else {
2095 vec_type = get_instance(element_type->base_type,
2096 element_type->vector_elements, 1);
2097 array_len *= element_type->matrix_columns;
2098 }
2099 const glsl_type *array_type = glsl_type::get_array_instance(vec_type,
2100 array_len);
2101
2102 return array_type->std430_size(false);
2103 }
2104
2105 if (this->is_array()) {
2106 unsigned stride;
2107 if (this->without_array()->is_struct())
2108 stride = this->without_array()->std430_size(row_major);
2109 else
2110 stride = this->without_array()->std430_base_alignment(row_major);
2111
2112 unsigned size = this->arrays_of_arrays_size() * stride;
2113 assert(this->explicit_stride == 0 ||
2114 size == this->length * this->explicit_stride);
2115 return size;
2116 }
2117
2118 if (this->is_struct() || this->is_interface()) {
2119 unsigned size = 0;
2120 unsigned max_align = 0;
2121
2122 for (unsigned i = 0; i < this->length; i++) {
2123 bool field_row_major = row_major;
2124 const enum glsl_matrix_layout matrix_layout =
2125 glsl_matrix_layout(this->fields.structure[i].matrix_layout);
2126 if (matrix_layout == GLSL_MATRIX_LAYOUT_ROW_MAJOR) {
2127 field_row_major = true;
2128 } else if (matrix_layout == GLSL_MATRIX_LAYOUT_COLUMN_MAJOR) {
2129 field_row_major = false;
2130 }
2131
2132 const struct glsl_type *field_type = this->fields.structure[i].type;
2133 unsigned align = field_type->std430_base_alignment(field_row_major);
2134 size = glsl_align(size, align);
2135 size += field_type->std430_size(field_row_major);
2136
2137 max_align = MAX2(align, max_align);
2138 }
2139 size = glsl_align(size, max_align);
2140 return size;
2141 }
2142
2143 assert(!"not reached");
2144 return -1;
2145 }
2146
2147 const glsl_type *
2148 glsl_type::get_explicit_std430_type(bool row_major) const
2149 {
2150 if (this->is_vector() || this->is_scalar()) {
2151 return this;
2152 } else if (this->is_matrix()) {
2153 const glsl_type *vec_type;
2154 if (row_major)
2155 vec_type = get_instance(this->base_type, this->matrix_columns, 1);
2156 else
2157 vec_type = get_instance(this->base_type, this->vector_elements, 1);
2158 unsigned stride = vec_type->std430_array_stride(false);
2159 return get_instance(this->base_type, this->vector_elements,
2160 this->matrix_columns, stride, row_major);
2161 } else if (this->is_array()) {
2162 const glsl_type *elem_type =
2163 this->fields.array->get_explicit_std430_type(row_major);
2164 unsigned stride = this->fields.array->std430_array_stride(row_major);
2165 return get_array_instance(elem_type, this->length, stride);
2166 } else if (this->is_struct() || this->is_interface()) {
2167 glsl_struct_field *fields = new glsl_struct_field[this->length];
2168 unsigned offset = 0;
2169 for (unsigned i = 0; i < length; i++) {
2170 fields[i] = this->fields.structure[i];
2171
2172 bool field_row_major = row_major;
2173 if (fields[i].matrix_layout == GLSL_MATRIX_LAYOUT_COLUMN_MAJOR) {
2174 field_row_major = false;
2175 } else if (fields[i].matrix_layout == GLSL_MATRIX_LAYOUT_ROW_MAJOR) {
2176 field_row_major = true;
2177 }
2178 fields[i].type =
2179 fields[i].type->get_explicit_std430_type(field_row_major);
2180
2181 unsigned fsize = fields[i].type->std430_size(field_row_major);
2182 unsigned falign = fields[i].type->std430_base_alignment(field_row_major);
2183 /* From the GLSL 460 spec section "Uniform and Shader Storage Block
2184 * Layout Qualifiers":
2185 *
2186 * "The actual offset of a member is computed as follows: If
2187 * offset was declared, start with that offset, otherwise start
2188 * with the next available offset. If the resulting offset is not
2189 * a multiple of the actual alignment, increase it to the first
2190 * offset that is a multiple of the actual alignment. This results
2191 * in the actual offset the member will have."
2192 */
2193 if (fields[i].offset >= 0) {
2194 assert((unsigned)fields[i].offset >= offset);
2195 offset = fields[i].offset;
2196 }
2197 offset = glsl_align(offset, falign);
2198 fields[i].offset = offset;
2199 offset += fsize;
2200 }
2201
2202 const glsl_type *type;
2203 if (this->is_struct())
2204 type = get_struct_instance(fields, this->length, this->name);
2205 else
2206 type = get_interface_instance(fields, this->length,
2207 (enum glsl_interface_packing)this->interface_packing,
2208 this->interface_row_major,
2209 this->name);
2210
2211 delete[] fields;
2212 return type;
2213 } else {
2214 unreachable("Invalid type for SSBO");
2215 }
2216 }
2217
2218 const glsl_type *
2219 glsl_type::get_explicit_interface_type(bool supports_std430) const
2220 {
2221 enum glsl_interface_packing packing =
2222 this->get_internal_ifc_packing(supports_std430);
2223 if (packing == GLSL_INTERFACE_PACKING_STD140) {
2224 return this->get_explicit_std140_type(this->interface_row_major);
2225 } else {
2226 assert(packing == GLSL_INTERFACE_PACKING_STD430);
2227 return this->get_explicit_std430_type(this->interface_row_major);
2228 }
2229 }
2230
2231 unsigned
2232 glsl_type::count_attribute_slots(bool is_gl_vertex_input) const
2233 {
2234 /* From page 31 (page 37 of the PDF) of the GLSL 1.50 spec:
2235 *
2236 * "A scalar input counts the same amount against this limit as a vec4,
2237 * so applications may want to consider packing groups of four
2238 * unrelated float inputs together into a vector to better utilize the
2239 * capabilities of the underlying hardware. A matrix input will use up
2240 * multiple locations. The number of locations used will equal the
2241 * number of columns in the matrix."
2242 *
2243 * The spec does not explicitly say how arrays are counted. However, it
2244 * should be safe to assume the total number of slots consumed by an array
2245 * is the number of entries in the array multiplied by the number of slots
2246 * consumed by a single element of the array.
2247 *
2248 * The spec says nothing about how structs are counted, because vertex
2249 * attributes are not allowed to be (or contain) structs. However, Mesa
2250 * allows varying structs, the number of varying slots taken up by a
2251 * varying struct is simply equal to the sum of the number of slots taken
2252 * up by each element.
2253 *
2254 * Doubles are counted different depending on whether they are vertex
2255 * inputs or everything else. Vertex inputs from ARB_vertex_attrib_64bit
2256 * take one location no matter what size they are, otherwise dvec3/4
2257 * take two locations.
2258 */
2259 switch (this->base_type) {
2260 case GLSL_TYPE_UINT:
2261 case GLSL_TYPE_INT:
2262 case GLSL_TYPE_UINT8:
2263 case GLSL_TYPE_INT8:
2264 case GLSL_TYPE_UINT16:
2265 case GLSL_TYPE_INT16:
2266 case GLSL_TYPE_FLOAT:
2267 case GLSL_TYPE_FLOAT16:
2268 case GLSL_TYPE_BOOL:
2269 case GLSL_TYPE_SAMPLER:
2270 case GLSL_TYPE_IMAGE:
2271 return this->matrix_columns;
2272 case GLSL_TYPE_DOUBLE:
2273 case GLSL_TYPE_UINT64:
2274 case GLSL_TYPE_INT64:
2275 if (this->vector_elements > 2 && !is_gl_vertex_input)
2276 return this->matrix_columns * 2;
2277 else
2278 return this->matrix_columns;
2279 case GLSL_TYPE_STRUCT:
2280 case GLSL_TYPE_INTERFACE: {
2281 unsigned size = 0;
2282
2283 for (unsigned i = 0; i < this->length; i++) {
2284 const glsl_type *member_type = this->fields.structure[i].type;
2285 size += member_type->count_attribute_slots(is_gl_vertex_input);
2286 }
2287
2288 return size;
2289 }
2290
2291 case GLSL_TYPE_ARRAY: {
2292 const glsl_type *element = this->fields.array;
2293 return this->length * element->count_attribute_slots(is_gl_vertex_input);
2294 }
2295
2296 case GLSL_TYPE_SUBROUTINE:
2297 return 1;
2298
2299 case GLSL_TYPE_FUNCTION:
2300 case GLSL_TYPE_ATOMIC_UINT:
2301 case GLSL_TYPE_VOID:
2302 case GLSL_TYPE_ERROR:
2303 break;
2304 }
2305
2306 assert(!"Unexpected type in count_attribute_slots()");
2307
2308 return 0;
2309 }
2310
2311 int
2312 glsl_type::coordinate_components() const
2313 {
2314 int size;
2315
2316 switch (sampler_dimensionality) {
2317 case GLSL_SAMPLER_DIM_1D:
2318 case GLSL_SAMPLER_DIM_BUF:
2319 size = 1;
2320 break;
2321 case GLSL_SAMPLER_DIM_2D:
2322 case GLSL_SAMPLER_DIM_RECT:
2323 case GLSL_SAMPLER_DIM_MS:
2324 case GLSL_SAMPLER_DIM_EXTERNAL:
2325 case GLSL_SAMPLER_DIM_SUBPASS:
2326 size = 2;
2327 break;
2328 case GLSL_SAMPLER_DIM_3D:
2329 case GLSL_SAMPLER_DIM_CUBE:
2330 size = 3;
2331 break;
2332 default:
2333 assert(!"Should not get here.");
2334 size = 1;
2335 break;
2336 }
2337
2338 /* Array textures need an additional component for the array index, except
2339 * for cubemap array images that behave like a 2D array of interleaved
2340 * cubemap faces.
2341 */
2342 if (sampler_array &&
2343 !(is_image() && sampler_dimensionality == GLSL_SAMPLER_DIM_CUBE))
2344 size += 1;
2345
2346 return size;
2347 }
2348
2349 /**
2350 * Declarations of type flyweights (glsl_type::_foo_type) and
2351 * convenience pointers (glsl_type::foo_type).
2352 * @{
2353 */
2354 #define DECL_TYPE(NAME, ...) \
2355 const glsl_type glsl_type::_##NAME##_type = glsl_type(__VA_ARGS__, #NAME); \
2356 const glsl_type *const glsl_type::NAME##_type = &glsl_type::_##NAME##_type;
2357
2358 #define STRUCT_TYPE(NAME)
2359
2360 #include "compiler/builtin_type_macros.h"
2361 /** @} */
2362
2363 static void
2364 get_struct_type_field_and_pointer_sizes(size_t *s_field_size,
2365 size_t *s_field_ptrs)
2366 {
2367 *s_field_size = sizeof(glsl_struct_field);
2368 *s_field_ptrs =
2369 sizeof(((glsl_struct_field *)0)->type) +
2370 sizeof(((glsl_struct_field *)0)->name);
2371 }
2372
2373 void
2374 encode_type_to_blob(struct blob *blob, const glsl_type *type)
2375 {
2376 uint32_t encoding;
2377
2378 if (!type) {
2379 blob_write_uint32(blob, 0);
2380 return;
2381 }
2382
2383 switch (type->base_type) {
2384 case GLSL_TYPE_UINT:
2385 case GLSL_TYPE_INT:
2386 case GLSL_TYPE_FLOAT:
2387 case GLSL_TYPE_FLOAT16:
2388 case GLSL_TYPE_DOUBLE:
2389 case GLSL_TYPE_UINT8:
2390 case GLSL_TYPE_INT8:
2391 case GLSL_TYPE_UINT16:
2392 case GLSL_TYPE_INT16:
2393 case GLSL_TYPE_UINT64:
2394 case GLSL_TYPE_INT64:
2395 case GLSL_TYPE_BOOL:
2396 encoding = (type->base_type << 24) |
2397 (type->interface_row_major << 10) |
2398 (type->vector_elements << 4) |
2399 (type->matrix_columns);
2400 blob_write_uint32(blob, encoding);
2401 blob_write_uint32(blob, type->explicit_stride);
2402 return;
2403 case GLSL_TYPE_SAMPLER:
2404 encoding = (type->base_type) << 24 |
2405 (type->sampler_dimensionality << 4) |
2406 (type->sampler_shadow << 3) |
2407 (type->sampler_array << 2) |
2408 (type->sampled_type);
2409 break;
2410 case GLSL_TYPE_SUBROUTINE:
2411 encoding = type->base_type << 24;
2412 blob_write_uint32(blob, encoding);
2413 blob_write_string(blob, type->name);
2414 return;
2415 case GLSL_TYPE_IMAGE:
2416 encoding = (type->base_type) << 24 |
2417 (type->sampler_dimensionality << 3) |
2418 (type->sampler_array << 2) |
2419 (type->sampled_type);
2420 break;
2421 case GLSL_TYPE_ATOMIC_UINT:
2422 encoding = (type->base_type << 24);
2423 break;
2424 case GLSL_TYPE_ARRAY:
2425 blob_write_uint32(blob, (type->base_type) << 24);
2426 blob_write_uint32(blob, type->length);
2427 blob_write_uint32(blob, type->explicit_stride);
2428 encode_type_to_blob(blob, type->fields.array);
2429 return;
2430 case GLSL_TYPE_STRUCT:
2431 case GLSL_TYPE_INTERFACE:
2432 blob_write_uint32(blob, (type->base_type) << 24);
2433 blob_write_string(blob, type->name);
2434 blob_write_uint32(blob, type->length);
2435
2436 size_t s_field_size, s_field_ptrs;
2437 get_struct_type_field_and_pointer_sizes(&s_field_size, &s_field_ptrs);
2438
2439 for (unsigned i = 0; i < type->length; i++) {
2440 encode_type_to_blob(blob, type->fields.structure[i].type);
2441 blob_write_string(blob, type->fields.structure[i].name);
2442
2443 /* Write the struct field skipping the pointers */
2444 blob_write_bytes(blob,
2445 ((char *)&type->fields.structure[i]) + s_field_ptrs,
2446 s_field_size - s_field_ptrs);
2447 }
2448
2449 if (type->is_interface()) {
2450 blob_write_uint32(blob, type->interface_packing);
2451 blob_write_uint32(blob, type->interface_row_major);
2452 } else {
2453 blob_write_uint32(blob, type->packed);
2454 }
2455 return;
2456 case GLSL_TYPE_VOID:
2457 encoding = (type->base_type << 24);
2458 break;
2459 case GLSL_TYPE_ERROR:
2460 default:
2461 assert(!"Cannot encode type!");
2462 encoding = 0;
2463 break;
2464 }
2465
2466 blob_write_uint32(blob, encoding);
2467 }
2468
2469 const glsl_type *
2470 decode_type_from_blob(struct blob_reader *blob)
2471 {
2472 uint32_t u = blob_read_uint32(blob);
2473
2474 if (u == 0) {
2475 return NULL;
2476 }
2477
2478 glsl_base_type base_type = (glsl_base_type) (u >> 24);
2479
2480 switch (base_type) {
2481 case GLSL_TYPE_UINT:
2482 case GLSL_TYPE_INT:
2483 case GLSL_TYPE_FLOAT:
2484 case GLSL_TYPE_FLOAT16:
2485 case GLSL_TYPE_DOUBLE:
2486 case GLSL_TYPE_UINT8:
2487 case GLSL_TYPE_INT8:
2488 case GLSL_TYPE_UINT16:
2489 case GLSL_TYPE_INT16:
2490 case GLSL_TYPE_UINT64:
2491 case GLSL_TYPE_INT64:
2492 case GLSL_TYPE_BOOL: {
2493 unsigned explicit_stride = blob_read_uint32(blob);
2494 return glsl_type::get_instance(base_type, (u >> 4) & 0x0f, u & 0x0f,
2495 explicit_stride, (u >> 10) & 0x1);
2496 }
2497 case GLSL_TYPE_SAMPLER:
2498 return glsl_type::get_sampler_instance((enum glsl_sampler_dim) ((u >> 4) & 0x0f),
2499 (u >> 3) & 0x01,
2500 (u >> 2) & 0x01,
2501 (glsl_base_type) ((u >> 0) & 0x03));
2502 case GLSL_TYPE_SUBROUTINE:
2503 return glsl_type::get_subroutine_instance(blob_read_string(blob));
2504 case GLSL_TYPE_IMAGE:
2505 return glsl_type::get_image_instance((enum glsl_sampler_dim) ((u >> 3) & 0x0f),
2506 (u >> 2) & 0x01,
2507 (glsl_base_type) ((u >> 0) & 0x03));
2508 case GLSL_TYPE_ATOMIC_UINT:
2509 return glsl_type::atomic_uint_type;
2510 case GLSL_TYPE_ARRAY: {
2511 unsigned length = blob_read_uint32(blob);
2512 unsigned explicit_stride = blob_read_uint32(blob);
2513 return glsl_type::get_array_instance(decode_type_from_blob(blob),
2514 length, explicit_stride);
2515 }
2516 case GLSL_TYPE_STRUCT:
2517 case GLSL_TYPE_INTERFACE: {
2518 char *name = blob_read_string(blob);
2519 unsigned num_fields = blob_read_uint32(blob);
2520
2521 size_t s_field_size, s_field_ptrs;
2522 get_struct_type_field_and_pointer_sizes(&s_field_size, &s_field_ptrs);
2523
2524 glsl_struct_field *fields =
2525 (glsl_struct_field *) malloc(s_field_size * num_fields);
2526 for (unsigned i = 0; i < num_fields; i++) {
2527 fields[i].type = decode_type_from_blob(blob);
2528 fields[i].name = blob_read_string(blob);
2529
2530 blob_copy_bytes(blob, ((uint8_t *) &fields[i]) + s_field_ptrs,
2531 s_field_size - s_field_ptrs);
2532 }
2533
2534 const glsl_type *t;
2535 if (base_type == GLSL_TYPE_INTERFACE) {
2536 enum glsl_interface_packing packing =
2537 (glsl_interface_packing) blob_read_uint32(blob);
2538 bool row_major = blob_read_uint32(blob);
2539 t = glsl_type::get_interface_instance(fields, num_fields, packing,
2540 row_major, name);
2541 } else {
2542 unsigned packed = blob_read_uint32(blob);
2543 t = glsl_type::get_struct_instance(fields, num_fields, name, packed);
2544 }
2545
2546 free(fields);
2547 return t;
2548 }
2549 case GLSL_TYPE_VOID:
2550 return glsl_type::void_type;
2551 case GLSL_TYPE_ERROR:
2552 default:
2553 assert(!"Cannot decode type!");
2554 return NULL;
2555 }
2556 }
2557
2558 unsigned
2559 glsl_type::cl_alignment() const
2560 {
2561 /* vectors unlike arrays are aligned to their size */
2562 if (this->is_scalar() || this->is_vector())
2563 return this->cl_size();
2564 else if (this->is_array())
2565 return this->without_array()->cl_alignment();
2566 else if (this->is_struct()) {
2567 /* Packed Structs are 0x1 aligned despite their size. */
2568 if (this->packed)
2569 return 1;
2570
2571 unsigned res = 1;
2572 for (unsigned i = 0; i < this->length; ++i) {
2573 struct glsl_struct_field &field = this->fields.structure[i];
2574 res = MAX2(res, field.type->cl_alignment());
2575 }
2576 return res;
2577 }
2578 return 1;
2579 }
2580
2581 unsigned
2582 glsl_type::cl_size() const
2583 {
2584 if (this->is_scalar()) {
2585 return glsl_base_type_get_bit_size(this->base_type) / 8;
2586 } else if (this->is_vector()) {
2587 unsigned vec_elemns = this->vector_elements == 3 ? 4 : this->vector_elements;
2588 return vec_elemns * glsl_base_type_get_bit_size(this->base_type) / 8;
2589 } else if (this->is_array()) {
2590 unsigned size = this->without_array()->cl_size();
2591 return size * this->length;
2592 } else if (this->is_struct()) {
2593 unsigned size = 0;
2594 for (unsigned i = 0; i < this->length; ++i) {
2595 struct glsl_struct_field &field = this->fields.structure[i];
2596 /* if a struct is packed, members don't get aligned */
2597 if (!this->packed)
2598 size = align(size, field.type->cl_alignment());
2599 size += field.type->cl_size();
2600 }
2601 return size;
2602 }
2603 return 1;
2604 }