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