nir: Invalidate live SSA def information when making new SSA defs.
[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 case GLSL_TYPE_VOID:
921 return shadow ? samplerShadow_type : sampler_type;
922 default:
923 return error_type;
924 }
925
926 unreachable("switch statement above should be complete");
927 }
928
929 const glsl_type *
930 glsl_type::get_image_instance(enum glsl_sampler_dim dim,
931 bool array, glsl_base_type type)
932 {
933 switch (type) {
934 case GLSL_TYPE_FLOAT:
935 switch (dim) {
936 case GLSL_SAMPLER_DIM_1D:
937 return (array ? image1DArray_type : image1D_type);
938 case GLSL_SAMPLER_DIM_2D:
939 return (array ? image2DArray_type : image2D_type);
940 case GLSL_SAMPLER_DIM_3D:
941 return image3D_type;
942 case GLSL_SAMPLER_DIM_CUBE:
943 return (array ? imageCubeArray_type : imageCube_type);
944 case GLSL_SAMPLER_DIM_RECT:
945 if (array)
946 return error_type;
947 else
948 return image2DRect_type;
949 case GLSL_SAMPLER_DIM_BUF:
950 if (array)
951 return error_type;
952 else
953 return imageBuffer_type;
954 case GLSL_SAMPLER_DIM_MS:
955 return (array ? image2DMSArray_type : image2DMS_type);
956 case GLSL_SAMPLER_DIM_SUBPASS:
957 return subpassInput_type;
958 case GLSL_SAMPLER_DIM_SUBPASS_MS:
959 return subpassInputMS_type;
960 case GLSL_SAMPLER_DIM_EXTERNAL:
961 return error_type;
962 }
963 case GLSL_TYPE_INT:
964 switch (dim) {
965 case GLSL_SAMPLER_DIM_1D:
966 return (array ? iimage1DArray_type : iimage1D_type);
967 case GLSL_SAMPLER_DIM_2D:
968 return (array ? iimage2DArray_type : iimage2D_type);
969 case GLSL_SAMPLER_DIM_3D:
970 if (array)
971 return error_type;
972 return iimage3D_type;
973 case GLSL_SAMPLER_DIM_CUBE:
974 return (array ? iimageCubeArray_type : iimageCube_type);
975 case GLSL_SAMPLER_DIM_RECT:
976 if (array)
977 return error_type;
978 return iimage2DRect_type;
979 case GLSL_SAMPLER_DIM_BUF:
980 if (array)
981 return error_type;
982 return iimageBuffer_type;
983 case GLSL_SAMPLER_DIM_MS:
984 return (array ? iimage2DMSArray_type : iimage2DMS_type);
985 case GLSL_SAMPLER_DIM_SUBPASS:
986 return isubpassInput_type;
987 case GLSL_SAMPLER_DIM_SUBPASS_MS:
988 return isubpassInputMS_type;
989 case GLSL_SAMPLER_DIM_EXTERNAL:
990 return error_type;
991 }
992 case GLSL_TYPE_UINT:
993 switch (dim) {
994 case GLSL_SAMPLER_DIM_1D:
995 return (array ? uimage1DArray_type : uimage1D_type);
996 case GLSL_SAMPLER_DIM_2D:
997 return (array ? uimage2DArray_type : uimage2D_type);
998 case GLSL_SAMPLER_DIM_3D:
999 if (array)
1000 return error_type;
1001 return uimage3D_type;
1002 case GLSL_SAMPLER_DIM_CUBE:
1003 return (array ? uimageCubeArray_type : uimageCube_type);
1004 case GLSL_SAMPLER_DIM_RECT:
1005 if (array)
1006 return error_type;
1007 return uimage2DRect_type;
1008 case GLSL_SAMPLER_DIM_BUF:
1009 if (array)
1010 return error_type;
1011 return uimageBuffer_type;
1012 case GLSL_SAMPLER_DIM_MS:
1013 return (array ? uimage2DMSArray_type : uimage2DMS_type);
1014 case GLSL_SAMPLER_DIM_SUBPASS:
1015 return usubpassInput_type;
1016 case GLSL_SAMPLER_DIM_SUBPASS_MS:
1017 return usubpassInputMS_type;
1018 case GLSL_SAMPLER_DIM_EXTERNAL:
1019 return error_type;
1020 }
1021 default:
1022 return error_type;
1023 }
1024
1025 unreachable("switch statement above should be complete");
1026 }
1027
1028 const glsl_type *
1029 glsl_type::get_array_instance(const glsl_type *base,
1030 unsigned array_size,
1031 unsigned explicit_stride)
1032 {
1033 /* Generate a name using the base type pointer in the key. This is
1034 * done because the name of the base type may not be unique across
1035 * shaders. For example, two shaders may have different record types
1036 * named 'foo'.
1037 */
1038 char key[128];
1039 snprintf(key, sizeof(key), "%p[%u]x%uB", (void *) base, array_size,
1040 explicit_stride);
1041
1042 mtx_lock(&glsl_type::hash_mutex);
1043 assert(glsl_type_users > 0);
1044
1045 if (array_types == NULL) {
1046 array_types = _mesa_hash_table_create(NULL, _mesa_hash_string,
1047 _mesa_key_string_equal);
1048 }
1049
1050 const struct hash_entry *entry = _mesa_hash_table_search(array_types, key);
1051 if (entry == NULL) {
1052 const glsl_type *t = new glsl_type(base, array_size, explicit_stride);
1053
1054 entry = _mesa_hash_table_insert(array_types,
1055 strdup(key),
1056 (void *) t);
1057 }
1058
1059 assert(((glsl_type *) entry->data)->base_type == GLSL_TYPE_ARRAY);
1060 assert(((glsl_type *) entry->data)->length == array_size);
1061 assert(((glsl_type *) entry->data)->fields.array == base);
1062
1063 glsl_type *t = (glsl_type *) entry->data;
1064
1065 mtx_unlock(&glsl_type::hash_mutex);
1066
1067 return t;
1068 }
1069
1070 bool
1071 glsl_type::compare_no_precision(const glsl_type *b) const
1072 {
1073 if (this == b)
1074 return true;
1075
1076 if (this->is_array()) {
1077 if (!b->is_array() || this->length != b->length)
1078 return false;
1079
1080 const glsl_type *b_no_array = b->fields.array;
1081
1082 return this->fields.array->compare_no_precision(b_no_array);
1083 }
1084
1085 if (this->is_struct()) {
1086 if (!b->is_struct())
1087 return false;
1088 } else if (this->is_interface()) {
1089 if (!b->is_interface())
1090 return false;
1091 } else {
1092 return false;
1093 }
1094
1095 return record_compare(b,
1096 true, /* match_name */
1097 true, /* match_locations */
1098 false /* match_precision */);
1099 }
1100
1101 bool
1102 glsl_type::record_compare(const glsl_type *b, bool match_name,
1103 bool match_locations, bool match_precision) const
1104 {
1105 if (this->length != b->length)
1106 return false;
1107
1108 if (this->interface_packing != b->interface_packing)
1109 return false;
1110
1111 if (this->interface_row_major != b->interface_row_major)
1112 return false;
1113
1114 /* From the GLSL 4.20 specification (Sec 4.2):
1115 *
1116 * "Structures must have the same name, sequence of type names, and
1117 * type definitions, and field names to be considered the same type."
1118 *
1119 * GLSL ES behaves the same (Ver 1.00 Sec 4.2.4, Ver 3.00 Sec 4.2.5).
1120 *
1121 * Section 7.4.1 (Shader Interface Matching) of the OpenGL 4.30 spec says:
1122 *
1123 * "Variables or block members declared as structures are considered
1124 * to match in type if and only if structure members match in name,
1125 * type, qualification, and declaration order."
1126 */
1127 if (match_name)
1128 if (strcmp(this->name, b->name) != 0)
1129 return false;
1130
1131 for (unsigned i = 0; i < this->length; i++) {
1132 if (match_precision) {
1133 if (this->fields.structure[i].type != b->fields.structure[i].type)
1134 return false;
1135 } else {
1136 const glsl_type *ta = this->fields.structure[i].type;
1137 const glsl_type *tb = b->fields.structure[i].type;
1138 if (!ta->compare_no_precision(tb))
1139 return false;
1140 }
1141 if (strcmp(this->fields.structure[i].name,
1142 b->fields.structure[i].name) != 0)
1143 return false;
1144 if (this->fields.structure[i].matrix_layout
1145 != b->fields.structure[i].matrix_layout)
1146 return false;
1147 if (match_locations && this->fields.structure[i].location
1148 != b->fields.structure[i].location)
1149 return false;
1150 if (this->fields.structure[i].offset
1151 != b->fields.structure[i].offset)
1152 return false;
1153 if (this->fields.structure[i].interpolation
1154 != b->fields.structure[i].interpolation)
1155 return false;
1156 if (this->fields.structure[i].centroid
1157 != b->fields.structure[i].centroid)
1158 return false;
1159 if (this->fields.structure[i].sample
1160 != b->fields.structure[i].sample)
1161 return false;
1162 if (this->fields.structure[i].patch
1163 != b->fields.structure[i].patch)
1164 return false;
1165 if (this->fields.structure[i].memory_read_only
1166 != b->fields.structure[i].memory_read_only)
1167 return false;
1168 if (this->fields.structure[i].memory_write_only
1169 != b->fields.structure[i].memory_write_only)
1170 return false;
1171 if (this->fields.structure[i].memory_coherent
1172 != b->fields.structure[i].memory_coherent)
1173 return false;
1174 if (this->fields.structure[i].memory_volatile
1175 != b->fields.structure[i].memory_volatile)
1176 return false;
1177 if (this->fields.structure[i].memory_restrict
1178 != b->fields.structure[i].memory_restrict)
1179 return false;
1180 if (this->fields.structure[i].image_format
1181 != b->fields.structure[i].image_format)
1182 return false;
1183 if (match_precision &&
1184 this->fields.structure[i].precision
1185 != b->fields.structure[i].precision)
1186 return false;
1187 if (this->fields.structure[i].explicit_xfb_buffer
1188 != b->fields.structure[i].explicit_xfb_buffer)
1189 return false;
1190 if (this->fields.structure[i].xfb_buffer
1191 != b->fields.structure[i].xfb_buffer)
1192 return false;
1193 if (this->fields.structure[i].xfb_stride
1194 != b->fields.structure[i].xfb_stride)
1195 return false;
1196 }
1197
1198 return true;
1199 }
1200
1201
1202 bool
1203 glsl_type::record_key_compare(const void *a, const void *b)
1204 {
1205 const glsl_type *const key1 = (glsl_type *) a;
1206 const glsl_type *const key2 = (glsl_type *) b;
1207
1208 return strcmp(key1->name, key2->name) == 0 &&
1209 key1->record_compare(key2, true);
1210 }
1211
1212
1213 /**
1214 * Generate an integer hash value for a glsl_type structure type.
1215 */
1216 unsigned
1217 glsl_type::record_key_hash(const void *a)
1218 {
1219 const glsl_type *const key = (glsl_type *) a;
1220 uintptr_t hash = key->length;
1221 unsigned retval;
1222
1223 for (unsigned i = 0; i < key->length; i++) {
1224 /* casting pointer to uintptr_t */
1225 hash = (hash * 13 ) + (uintptr_t) key->fields.structure[i].type;
1226 }
1227
1228 if (sizeof(hash) == 8)
1229 retval = (hash & 0xffffffff) ^ ((uint64_t) hash >> 32);
1230 else
1231 retval = hash;
1232
1233 return retval;
1234 }
1235
1236
1237 const glsl_type *
1238 glsl_type::get_struct_instance(const glsl_struct_field *fields,
1239 unsigned num_fields,
1240 const char *name,
1241 bool packed)
1242 {
1243 const glsl_type key(fields, num_fields, name, packed);
1244
1245 mtx_lock(&glsl_type::hash_mutex);
1246 assert(glsl_type_users > 0);
1247
1248 if (struct_types == NULL) {
1249 struct_types = _mesa_hash_table_create(NULL, record_key_hash,
1250 record_key_compare);
1251 }
1252
1253 const struct hash_entry *entry = _mesa_hash_table_search(struct_types,
1254 &key);
1255 if (entry == NULL) {
1256 const glsl_type *t = new glsl_type(fields, num_fields, name, packed);
1257
1258 entry = _mesa_hash_table_insert(struct_types, t, (void *) t);
1259 }
1260
1261 assert(((glsl_type *) entry->data)->base_type == GLSL_TYPE_STRUCT);
1262 assert(((glsl_type *) entry->data)->length == num_fields);
1263 assert(strcmp(((glsl_type *) entry->data)->name, name) == 0);
1264 assert(((glsl_type *) entry->data)->packed == packed);
1265
1266 glsl_type *t = (glsl_type *) entry->data;
1267
1268 mtx_unlock(&glsl_type::hash_mutex);
1269
1270 return t;
1271 }
1272
1273
1274 const glsl_type *
1275 glsl_type::get_interface_instance(const glsl_struct_field *fields,
1276 unsigned num_fields,
1277 enum glsl_interface_packing packing,
1278 bool row_major,
1279 const char *block_name)
1280 {
1281 const glsl_type key(fields, num_fields, packing, row_major, block_name);
1282
1283 mtx_lock(&glsl_type::hash_mutex);
1284 assert(glsl_type_users > 0);
1285
1286 if (interface_types == NULL) {
1287 interface_types = _mesa_hash_table_create(NULL, record_key_hash,
1288 record_key_compare);
1289 }
1290
1291 const struct hash_entry *entry = _mesa_hash_table_search(interface_types,
1292 &key);
1293 if (entry == NULL) {
1294 const glsl_type *t = new glsl_type(fields, num_fields,
1295 packing, row_major, block_name);
1296
1297 entry = _mesa_hash_table_insert(interface_types, t, (void *) t);
1298 }
1299
1300 assert(((glsl_type *) entry->data)->base_type == GLSL_TYPE_INTERFACE);
1301 assert(((glsl_type *) entry->data)->length == num_fields);
1302 assert(strcmp(((glsl_type *) entry->data)->name, block_name) == 0);
1303
1304 glsl_type *t = (glsl_type *) entry->data;
1305
1306 mtx_unlock(&glsl_type::hash_mutex);
1307
1308 return t;
1309 }
1310
1311 const glsl_type *
1312 glsl_type::get_subroutine_instance(const char *subroutine_name)
1313 {
1314 const glsl_type key(subroutine_name);
1315
1316 mtx_lock(&glsl_type::hash_mutex);
1317 assert(glsl_type_users > 0);
1318
1319 if (subroutine_types == NULL) {
1320 subroutine_types = _mesa_hash_table_create(NULL, record_key_hash,
1321 record_key_compare);
1322 }
1323
1324 const struct hash_entry *entry = _mesa_hash_table_search(subroutine_types,
1325 &key);
1326 if (entry == NULL) {
1327 const glsl_type *t = new glsl_type(subroutine_name);
1328
1329 entry = _mesa_hash_table_insert(subroutine_types, t, (void *) t);
1330 }
1331
1332 assert(((glsl_type *) entry->data)->base_type == GLSL_TYPE_SUBROUTINE);
1333 assert(strcmp(((glsl_type *) entry->data)->name, subroutine_name) == 0);
1334
1335 glsl_type *t = (glsl_type *) entry->data;
1336
1337 mtx_unlock(&glsl_type::hash_mutex);
1338
1339 return t;
1340 }
1341
1342
1343 static bool
1344 function_key_compare(const void *a, const void *b)
1345 {
1346 const glsl_type *const key1 = (glsl_type *) a;
1347 const glsl_type *const key2 = (glsl_type *) b;
1348
1349 if (key1->length != key2->length)
1350 return false;
1351
1352 return memcmp(key1->fields.parameters, key2->fields.parameters,
1353 (key1->length + 1) * sizeof(*key1->fields.parameters)) == 0;
1354 }
1355
1356
1357 static uint32_t
1358 function_key_hash(const void *a)
1359 {
1360 const glsl_type *const key = (glsl_type *) a;
1361 return _mesa_hash_data(key->fields.parameters,
1362 (key->length + 1) * sizeof(*key->fields.parameters));
1363 }
1364
1365 const glsl_type *
1366 glsl_type::get_function_instance(const glsl_type *return_type,
1367 const glsl_function_param *params,
1368 unsigned num_params)
1369 {
1370 const glsl_type key(return_type, params, num_params);
1371
1372 mtx_lock(&glsl_type::hash_mutex);
1373 assert(glsl_type_users > 0);
1374
1375 if (function_types == NULL) {
1376 function_types = _mesa_hash_table_create(NULL, function_key_hash,
1377 function_key_compare);
1378 }
1379
1380 struct hash_entry *entry = _mesa_hash_table_search(function_types, &key);
1381 if (entry == NULL) {
1382 const glsl_type *t = new glsl_type(return_type, params, num_params);
1383
1384 entry = _mesa_hash_table_insert(function_types, t, (void *) t);
1385 }
1386
1387 const glsl_type *t = (const glsl_type *)entry->data;
1388
1389 assert(t->base_type == GLSL_TYPE_FUNCTION);
1390 assert(t->length == num_params);
1391
1392 mtx_unlock(&glsl_type::hash_mutex);
1393
1394 return t;
1395 }
1396
1397
1398 const glsl_type *
1399 glsl_type::get_mul_type(const glsl_type *type_a, const glsl_type *type_b)
1400 {
1401 if (type_a->is_matrix() && type_b->is_matrix()) {
1402 /* Matrix multiply. The columns of A must match the rows of B. Given
1403 * the other previously tested constraints, this means the vector type
1404 * of a row from A must be the same as the vector type of a column from
1405 * B.
1406 */
1407 if (type_a->row_type() == type_b->column_type()) {
1408 /* The resulting matrix has the number of columns of matrix B and
1409 * the number of rows of matrix A. We get the row count of A by
1410 * looking at the size of a vector that makes up a column. The
1411 * transpose (size of a row) is done for B.
1412 */
1413 const glsl_type *const type =
1414 get_instance(type_a->base_type,
1415 type_a->column_type()->vector_elements,
1416 type_b->row_type()->vector_elements);
1417 assert(type != error_type);
1418
1419 return type;
1420 }
1421 } else if (type_a == type_b) {
1422 return type_a;
1423 } else if (type_a->is_matrix()) {
1424 /* A is a matrix and B is a column vector. Columns of A must match
1425 * rows of B. Given the other previously tested constraints, this
1426 * means the vector type of a row from A must be the same as the
1427 * vector the type of B.
1428 */
1429 if (type_a->row_type() == type_b) {
1430 /* The resulting vector has a number of elements equal to
1431 * the number of rows of matrix A. */
1432 const glsl_type *const type =
1433 get_instance(type_a->base_type,
1434 type_a->column_type()->vector_elements,
1435 1);
1436 assert(type != error_type);
1437
1438 return type;
1439 }
1440 } else {
1441 assert(type_b->is_matrix());
1442
1443 /* A is a row vector and B is a matrix. Columns of A must match rows
1444 * of B. Given the other previously tested constraints, this means
1445 * the type of A must be the same as the vector type of a column from
1446 * B.
1447 */
1448 if (type_a == type_b->column_type()) {
1449 /* The resulting vector has a number of elements equal to
1450 * the number of columns of matrix B. */
1451 const glsl_type *const type =
1452 get_instance(type_a->base_type,
1453 type_b->row_type()->vector_elements,
1454 1);
1455 assert(type != error_type);
1456
1457 return type;
1458 }
1459 }
1460
1461 return error_type;
1462 }
1463
1464
1465 const glsl_type *
1466 glsl_type::field_type(const char *name) const
1467 {
1468 if (this->base_type != GLSL_TYPE_STRUCT
1469 && this->base_type != GLSL_TYPE_INTERFACE)
1470 return error_type;
1471
1472 for (unsigned i = 0; i < this->length; i++) {
1473 if (strcmp(name, this->fields.structure[i].name) == 0)
1474 return this->fields.structure[i].type;
1475 }
1476
1477 return error_type;
1478 }
1479
1480
1481 int
1482 glsl_type::field_index(const char *name) const
1483 {
1484 if (this->base_type != GLSL_TYPE_STRUCT
1485 && this->base_type != GLSL_TYPE_INTERFACE)
1486 return -1;
1487
1488 for (unsigned i = 0; i < this->length; i++) {
1489 if (strcmp(name, this->fields.structure[i].name) == 0)
1490 return i;
1491 }
1492
1493 return -1;
1494 }
1495
1496
1497 unsigned
1498 glsl_type::component_slots() const
1499 {
1500 switch (this->base_type) {
1501 case GLSL_TYPE_UINT:
1502 case GLSL_TYPE_INT:
1503 case GLSL_TYPE_UINT8:
1504 case GLSL_TYPE_INT8:
1505 case GLSL_TYPE_UINT16:
1506 case GLSL_TYPE_INT16:
1507 case GLSL_TYPE_FLOAT:
1508 case GLSL_TYPE_FLOAT16:
1509 case GLSL_TYPE_BOOL:
1510 return this->components();
1511
1512 case GLSL_TYPE_DOUBLE:
1513 case GLSL_TYPE_UINT64:
1514 case GLSL_TYPE_INT64:
1515 return 2 * this->components();
1516
1517 case GLSL_TYPE_STRUCT:
1518 case GLSL_TYPE_INTERFACE: {
1519 unsigned size = 0;
1520
1521 for (unsigned i = 0; i < this->length; i++)
1522 size += this->fields.structure[i].type->component_slots();
1523
1524 return size;
1525 }
1526
1527 case GLSL_TYPE_ARRAY:
1528 return this->length * this->fields.array->component_slots();
1529
1530 case GLSL_TYPE_SAMPLER:
1531 case GLSL_TYPE_IMAGE:
1532 return 2;
1533
1534 case GLSL_TYPE_SUBROUTINE:
1535 return 1;
1536
1537 case GLSL_TYPE_FUNCTION:
1538 case GLSL_TYPE_ATOMIC_UINT:
1539 case GLSL_TYPE_VOID:
1540 case GLSL_TYPE_ERROR:
1541 break;
1542 }
1543
1544 return 0;
1545 }
1546
1547 unsigned
1548 glsl_type::struct_location_offset(unsigned length) const
1549 {
1550 unsigned offset = 0;
1551 const glsl_type *t = this->without_array();
1552 if (t->is_struct()) {
1553 assert(length <= t->length);
1554
1555 for (unsigned i = 0; i < length; i++) {
1556 const glsl_type *st = t->fields.structure[i].type;
1557 const glsl_type *wa = st->without_array();
1558 if (wa->is_struct()) {
1559 unsigned r_offset = wa->struct_location_offset(wa->length);
1560 offset += st->is_array() ?
1561 st->arrays_of_arrays_size() * r_offset : r_offset;
1562 } else if (st->is_array() && st->fields.array->is_array()) {
1563 unsigned outer_array_size = st->length;
1564 const glsl_type *base_type = st->fields.array;
1565
1566 /* For arrays of arrays the outer arrays take up a uniform
1567 * slot for each element. The innermost array elements share a
1568 * single slot so we ignore the innermost array when calculating
1569 * the offset.
1570 */
1571 while (base_type->fields.array->is_array()) {
1572 outer_array_size = outer_array_size * base_type->length;
1573 base_type = base_type->fields.array;
1574 }
1575 offset += outer_array_size;
1576 } else {
1577 /* We dont worry about arrays here because unless the array
1578 * contains a structure or another array it only takes up a single
1579 * uniform slot.
1580 */
1581 offset += 1;
1582 }
1583 }
1584 }
1585 return offset;
1586 }
1587
1588 unsigned
1589 glsl_type::uniform_locations() const
1590 {
1591 unsigned size = 0;
1592
1593 switch (this->base_type) {
1594 case GLSL_TYPE_UINT:
1595 case GLSL_TYPE_INT:
1596 case GLSL_TYPE_FLOAT:
1597 case GLSL_TYPE_FLOAT16:
1598 case GLSL_TYPE_DOUBLE:
1599 case GLSL_TYPE_UINT16:
1600 case GLSL_TYPE_UINT8:
1601 case GLSL_TYPE_INT16:
1602 case GLSL_TYPE_INT8:
1603 case GLSL_TYPE_UINT64:
1604 case GLSL_TYPE_INT64:
1605 case GLSL_TYPE_BOOL:
1606 case GLSL_TYPE_SAMPLER:
1607 case GLSL_TYPE_IMAGE:
1608 case GLSL_TYPE_SUBROUTINE:
1609 return 1;
1610
1611 case GLSL_TYPE_STRUCT:
1612 case GLSL_TYPE_INTERFACE:
1613 for (unsigned i = 0; i < this->length; i++)
1614 size += this->fields.structure[i].type->uniform_locations();
1615 return size;
1616 case GLSL_TYPE_ARRAY:
1617 return this->length * this->fields.array->uniform_locations();
1618 default:
1619 return 0;
1620 }
1621 }
1622
1623 unsigned
1624 glsl_type::varying_count() const
1625 {
1626 unsigned size = 0;
1627
1628 switch (this->base_type) {
1629 case GLSL_TYPE_UINT:
1630 case GLSL_TYPE_INT:
1631 case GLSL_TYPE_FLOAT:
1632 case GLSL_TYPE_FLOAT16:
1633 case GLSL_TYPE_DOUBLE:
1634 case GLSL_TYPE_BOOL:
1635 case GLSL_TYPE_UINT16:
1636 case GLSL_TYPE_UINT8:
1637 case GLSL_TYPE_INT16:
1638 case GLSL_TYPE_INT8:
1639 case GLSL_TYPE_UINT64:
1640 case GLSL_TYPE_INT64:
1641 return 1;
1642
1643 case GLSL_TYPE_STRUCT:
1644 case GLSL_TYPE_INTERFACE:
1645 for (unsigned i = 0; i < this->length; i++)
1646 size += this->fields.structure[i].type->varying_count();
1647 return size;
1648 case GLSL_TYPE_ARRAY:
1649 /* Don't count innermost array elements */
1650 if (this->without_array()->is_struct() ||
1651 this->without_array()->is_interface() ||
1652 this->fields.array->is_array())
1653 return this->length * this->fields.array->varying_count();
1654 else
1655 return this->fields.array->varying_count();
1656 default:
1657 assert(!"unsupported varying type");
1658 return 0;
1659 }
1660 }
1661
1662 bool
1663 glsl_type::can_implicitly_convert_to(const glsl_type *desired,
1664 _mesa_glsl_parse_state *state) const
1665 {
1666 if (this == desired)
1667 return true;
1668
1669 /* GLSL 1.10 and ESSL do not allow implicit conversions. If there is no
1670 * state, we're doing intra-stage function linking where these checks have
1671 * already been done.
1672 */
1673 if (state && !state->has_implicit_conversions())
1674 return false;
1675
1676 /* There is no conversion among matrix types. */
1677 if (this->matrix_columns > 1 || desired->matrix_columns > 1)
1678 return false;
1679
1680 /* Vector size must match. */
1681 if (this->vector_elements != desired->vector_elements)
1682 return false;
1683
1684 /* int and uint can be converted to float. */
1685 if (desired->is_float() && this->is_integer_32())
1686 return true;
1687
1688 /* With GLSL 4.0, ARB_gpu_shader5, or MESA_shader_integer_functions, int
1689 * can be converted to uint. Note that state may be NULL here, when
1690 * resolving function calls in the linker. By this time, all the
1691 * state-dependent checks have already happened though, so allow anything
1692 * that's allowed in any shader version.
1693 */
1694 if ((!state || state->has_implicit_int_to_uint_conversion()) &&
1695 desired->base_type == GLSL_TYPE_UINT && this->base_type == GLSL_TYPE_INT)
1696 return true;
1697
1698 /* No implicit conversions from double. */
1699 if ((!state || state->has_double()) && this->is_double())
1700 return false;
1701
1702 /* Conversions from different types to double. */
1703 if ((!state || state->has_double()) && desired->is_double()) {
1704 if (this->is_float())
1705 return true;
1706 if (this->is_integer_32())
1707 return true;
1708 }
1709
1710 return false;
1711 }
1712
1713 unsigned
1714 glsl_type::std140_base_alignment(bool row_major) const
1715 {
1716 unsigned N = is_64bit() ? 8 : 4;
1717
1718 /* (1) If the member is a scalar consuming <N> basic machine units, the
1719 * base alignment is <N>.
1720 *
1721 * (2) If the member is a two- or four-component vector with components
1722 * consuming <N> basic machine units, the base alignment is 2<N> or
1723 * 4<N>, respectively.
1724 *
1725 * (3) If the member is a three-component vector with components consuming
1726 * <N> basic machine units, the base alignment is 4<N>.
1727 */
1728 if (this->is_scalar() || this->is_vector()) {
1729 switch (this->vector_elements) {
1730 case 1:
1731 return N;
1732 case 2:
1733 return 2 * N;
1734 case 3:
1735 case 4:
1736 return 4 * N;
1737 }
1738 }
1739
1740 /* (4) If the member is an array of scalars or vectors, the base alignment
1741 * and array stride are set to match the base alignment of a single
1742 * array element, according to rules (1), (2), and (3), and rounded up
1743 * to the base alignment of a vec4. The array may have padding at the
1744 * end; the base offset of the member following the array is rounded up
1745 * to the next multiple of the base alignment.
1746 *
1747 * (6) If the member is an array of <S> column-major matrices with <C>
1748 * columns and <R> rows, the matrix is stored identically to a row of
1749 * <S>*<C> column vectors with <R> components each, according to rule
1750 * (4).
1751 *
1752 * (8) If the member is an array of <S> row-major matrices with <C> columns
1753 * and <R> rows, the matrix is stored identically to a row of <S>*<R>
1754 * row vectors with <C> components each, according to rule (4).
1755 *
1756 * (10) If the member is an array of <S> structures, the <S> elements of
1757 * the array are laid out in order, according to rule (9).
1758 */
1759 if (this->is_array()) {
1760 if (this->fields.array->is_scalar() ||
1761 this->fields.array->is_vector() ||
1762 this->fields.array->is_matrix()) {
1763 return MAX2(this->fields.array->std140_base_alignment(row_major), 16);
1764 } else {
1765 assert(this->fields.array->is_struct() ||
1766 this->fields.array->is_array());
1767 return this->fields.array->std140_base_alignment(row_major);
1768 }
1769 }
1770
1771 /* (5) If the member is a column-major matrix with <C> columns and
1772 * <R> rows, the matrix is stored identically to an array of
1773 * <C> column vectors with <R> components each, according to
1774 * rule (4).
1775 *
1776 * (7) If the member is a row-major matrix with <C> columns and <R>
1777 * rows, the matrix is stored identically to an array of <R>
1778 * row vectors with <C> components each, according to rule (4).
1779 */
1780 if (this->is_matrix()) {
1781 const struct glsl_type *vec_type, *array_type;
1782 int c = this->matrix_columns;
1783 int r = this->vector_elements;
1784
1785 if (row_major) {
1786 vec_type = get_instance(base_type, c, 1);
1787 array_type = glsl_type::get_array_instance(vec_type, r);
1788 } else {
1789 vec_type = get_instance(base_type, r, 1);
1790 array_type = glsl_type::get_array_instance(vec_type, c);
1791 }
1792
1793 return array_type->std140_base_alignment(false);
1794 }
1795
1796 /* (9) If the member is a structure, the base alignment of the
1797 * structure is <N>, where <N> is the largest base alignment
1798 * value of any of its members, and rounded up to the base
1799 * alignment of a vec4. The individual members of this
1800 * sub-structure are then assigned offsets by applying this set
1801 * of rules recursively, where the base offset of the first
1802 * member of the sub-structure is equal to the aligned offset
1803 * of the structure. The structure may have padding at the end;
1804 * the base offset of the member following the sub-structure is
1805 * rounded up to the next multiple of the base alignment of the
1806 * structure.
1807 */
1808 if (this->is_struct()) {
1809 unsigned base_alignment = 16;
1810 for (unsigned i = 0; i < this->length; i++) {
1811 bool field_row_major = row_major;
1812 const enum glsl_matrix_layout matrix_layout =
1813 glsl_matrix_layout(this->fields.structure[i].matrix_layout);
1814 if (matrix_layout == GLSL_MATRIX_LAYOUT_ROW_MAJOR) {
1815 field_row_major = true;
1816 } else if (matrix_layout == GLSL_MATRIX_LAYOUT_COLUMN_MAJOR) {
1817 field_row_major = false;
1818 }
1819
1820 const struct glsl_type *field_type = this->fields.structure[i].type;
1821 base_alignment = MAX2(base_alignment,
1822 field_type->std140_base_alignment(field_row_major));
1823 }
1824 return base_alignment;
1825 }
1826
1827 assert(!"not reached");
1828 return -1;
1829 }
1830
1831 unsigned
1832 glsl_type::std140_size(bool row_major) const
1833 {
1834 unsigned N = is_64bit() ? 8 : 4;
1835
1836 /* (1) If the member is a scalar consuming <N> basic machine units, the
1837 * base alignment is <N>.
1838 *
1839 * (2) If the member is a two- or four-component vector with components
1840 * consuming <N> basic machine units, the base alignment is 2<N> or
1841 * 4<N>, respectively.
1842 *
1843 * (3) If the member is a three-component vector with components consuming
1844 * <N> basic machine units, the base alignment is 4<N>.
1845 */
1846 if (this->is_scalar() || this->is_vector()) {
1847 assert(this->explicit_stride == 0);
1848 return this->vector_elements * N;
1849 }
1850
1851 /* (5) If the member is a column-major matrix with <C> columns and
1852 * <R> rows, the matrix is stored identically to an array of
1853 * <C> column vectors with <R> components each, according to
1854 * rule (4).
1855 *
1856 * (6) If the member is an array of <S> column-major matrices with <C>
1857 * columns and <R> rows, the matrix is stored identically to a row of
1858 * <S>*<C> column vectors with <R> components each, according to rule
1859 * (4).
1860 *
1861 * (7) If the member is a row-major matrix with <C> columns and <R>
1862 * rows, the matrix is stored identically to an array of <R>
1863 * row vectors with <C> components each, according to rule (4).
1864 *
1865 * (8) If the member is an array of <S> row-major matrices with <C> columns
1866 * and <R> rows, the matrix is stored identically to a row of <S>*<R>
1867 * row vectors with <C> components each, according to rule (4).
1868 */
1869 if (this->without_array()->is_matrix()) {
1870 const struct glsl_type *element_type;
1871 const struct glsl_type *vec_type;
1872 unsigned int array_len;
1873
1874 if (this->is_array()) {
1875 element_type = this->without_array();
1876 array_len = this->arrays_of_arrays_size();
1877 } else {
1878 element_type = this;
1879 array_len = 1;
1880 }
1881
1882 if (row_major) {
1883 vec_type = get_instance(element_type->base_type,
1884 element_type->matrix_columns, 1);
1885
1886 array_len *= element_type->vector_elements;
1887 } else {
1888 vec_type = get_instance(element_type->base_type,
1889 element_type->vector_elements, 1);
1890 array_len *= element_type->matrix_columns;
1891 }
1892 const glsl_type *array_type = glsl_type::get_array_instance(vec_type,
1893 array_len);
1894
1895 return array_type->std140_size(false);
1896 }
1897
1898 /* (4) If the member is an array of scalars or vectors, the base alignment
1899 * and array stride are set to match the base alignment of a single
1900 * array element, according to rules (1), (2), and (3), and rounded up
1901 * to the base alignment of a vec4. The array may have padding at the
1902 * end; the base offset of the member following the array is rounded up
1903 * to the next multiple of the base alignment.
1904 *
1905 * (10) If the member is an array of <S> structures, the <S> elements of
1906 * the array are laid out in order, according to rule (9).
1907 */
1908 if (this->is_array()) {
1909 unsigned stride;
1910 if (this->without_array()->is_struct()) {
1911 stride = this->without_array()->std140_size(row_major);
1912 } else {
1913 unsigned element_base_align =
1914 this->without_array()->std140_base_alignment(row_major);
1915 stride = MAX2(element_base_align, 16);
1916 }
1917
1918 unsigned size = this->arrays_of_arrays_size() * stride;
1919 assert(this->explicit_stride == 0 ||
1920 size == this->length * this->explicit_stride);
1921 return size;
1922 }
1923
1924 /* (9) If the member is a structure, the base alignment of the
1925 * structure is <N>, where <N> is the largest base alignment
1926 * value of any of its members, and rounded up to the base
1927 * alignment of a vec4. The individual members of this
1928 * sub-structure are then assigned offsets by applying this set
1929 * of rules recursively, where the base offset of the first
1930 * member of the sub-structure is equal to the aligned offset
1931 * of the structure. The structure may have padding at the end;
1932 * the base offset of the member following the sub-structure is
1933 * rounded up to the next multiple of the base alignment of the
1934 * structure.
1935 */
1936 if (this->is_struct() || this->is_interface()) {
1937 unsigned size = 0;
1938 unsigned max_align = 0;
1939
1940 for (unsigned i = 0; i < this->length; i++) {
1941 bool field_row_major = row_major;
1942 const enum glsl_matrix_layout matrix_layout =
1943 glsl_matrix_layout(this->fields.structure[i].matrix_layout);
1944 if (matrix_layout == GLSL_MATRIX_LAYOUT_ROW_MAJOR) {
1945 field_row_major = true;
1946 } else if (matrix_layout == GLSL_MATRIX_LAYOUT_COLUMN_MAJOR) {
1947 field_row_major = false;
1948 }
1949
1950 const struct glsl_type *field_type = this->fields.structure[i].type;
1951 unsigned align = field_type->std140_base_alignment(field_row_major);
1952
1953 /* Ignore unsized arrays when calculating size */
1954 if (field_type->is_unsized_array())
1955 continue;
1956
1957 size = glsl_align(size, align);
1958 size += field_type->std140_size(field_row_major);
1959
1960 max_align = MAX2(align, max_align);
1961
1962 if (field_type->is_struct() && (i + 1 < this->length))
1963 size = glsl_align(size, 16);
1964 }
1965 size = glsl_align(size, MAX2(max_align, 16));
1966 return size;
1967 }
1968
1969 assert(!"not reached");
1970 return -1;
1971 }
1972
1973 const glsl_type *
1974 glsl_type::get_explicit_std140_type(bool row_major) const
1975 {
1976 if (this->is_vector() || this->is_scalar()) {
1977 return this;
1978 } else if (this->is_matrix()) {
1979 const glsl_type *vec_type;
1980 if (row_major)
1981 vec_type = get_instance(this->base_type, this->matrix_columns, 1);
1982 else
1983 vec_type = get_instance(this->base_type, this->vector_elements, 1);
1984 unsigned elem_size = vec_type->std140_size(false);
1985 unsigned stride = glsl_align(elem_size, 16);
1986 return get_instance(this->base_type, this->vector_elements,
1987 this->matrix_columns, stride, row_major);
1988 } else if (this->is_array()) {
1989 unsigned elem_size = this->fields.array->std140_size(row_major);
1990 const glsl_type *elem_type =
1991 this->fields.array->get_explicit_std140_type(row_major);
1992 unsigned stride = glsl_align(elem_size, 16);
1993 return get_array_instance(elem_type, this->length, stride);
1994 } else if (this->is_struct() || this->is_interface()) {
1995 glsl_struct_field *fields = new glsl_struct_field[this->length];
1996 unsigned offset = 0;
1997 for (unsigned i = 0; i < length; i++) {
1998 fields[i] = this->fields.structure[i];
1999
2000 bool field_row_major = row_major;
2001 if (fields[i].matrix_layout == GLSL_MATRIX_LAYOUT_COLUMN_MAJOR) {
2002 field_row_major = false;
2003 } else if (fields[i].matrix_layout == GLSL_MATRIX_LAYOUT_ROW_MAJOR) {
2004 field_row_major = true;
2005 }
2006 fields[i].type =
2007 fields[i].type->get_explicit_std140_type(field_row_major);
2008
2009 unsigned fsize = fields[i].type->std140_size(field_row_major);
2010 unsigned falign = fields[i].type->std140_base_alignment(field_row_major);
2011 /* From the GLSL 460 spec section "Uniform and Shader Storage Block
2012 * Layout Qualifiers":
2013 *
2014 * "The actual offset of a member is computed as follows: If
2015 * offset was declared, start with that offset, otherwise start
2016 * with the next available offset. If the resulting offset is not
2017 * a multiple of the actual alignment, increase it to the first
2018 * offset that is a multiple of the actual alignment. This results
2019 * in the actual offset the member will have."
2020 */
2021 if (fields[i].offset >= 0) {
2022 assert((unsigned)fields[i].offset >= offset);
2023 offset = fields[i].offset;
2024 }
2025 offset = glsl_align(offset, falign);
2026 fields[i].offset = offset;
2027 offset += fsize;
2028 }
2029
2030 const glsl_type *type;
2031 if (this->is_struct())
2032 type = get_struct_instance(fields, this->length, this->name);
2033 else
2034 type = get_interface_instance(fields, this->length,
2035 (enum glsl_interface_packing)this->interface_packing,
2036 this->interface_row_major,
2037 this->name);
2038
2039 delete[] fields;
2040 return type;
2041 } else {
2042 unreachable("Invalid type for UBO or SSBO");
2043 }
2044 }
2045
2046 unsigned
2047 glsl_type::std430_base_alignment(bool row_major) const
2048 {
2049
2050 unsigned N = is_64bit() ? 8 : 4;
2051
2052 /* (1) If the member is a scalar consuming <N> basic machine units, the
2053 * base alignment is <N>.
2054 *
2055 * (2) If the member is a two- or four-component vector with components
2056 * consuming <N> basic machine units, the base alignment is 2<N> or
2057 * 4<N>, respectively.
2058 *
2059 * (3) If the member is a three-component vector with components consuming
2060 * <N> basic machine units, the base alignment is 4<N>.
2061 */
2062 if (this->is_scalar() || this->is_vector()) {
2063 switch (this->vector_elements) {
2064 case 1:
2065 return N;
2066 case 2:
2067 return 2 * N;
2068 case 3:
2069 case 4:
2070 return 4 * N;
2071 }
2072 }
2073
2074 /* OpenGL 4.30 spec, section 7.6.2.2 "Standard Uniform Block Layout":
2075 *
2076 * "When using the std430 storage layout, shader storage blocks will be
2077 * laid out in buffer storage identically to uniform and shader storage
2078 * blocks using the std140 layout, except that the base alignment and
2079 * stride of arrays of scalars and vectors in rule 4 and of structures
2080 * in rule 9 are not rounded up a multiple of the base alignment of a vec4.
2081 */
2082
2083 /* (1) If the member is a scalar consuming <N> basic machine units, the
2084 * base alignment is <N>.
2085 *
2086 * (2) If the member is a two- or four-component vector with components
2087 * consuming <N> basic machine units, the base alignment is 2<N> or
2088 * 4<N>, respectively.
2089 *
2090 * (3) If the member is a three-component vector with components consuming
2091 * <N> basic machine units, the base alignment is 4<N>.
2092 */
2093 if (this->is_array())
2094 return this->fields.array->std430_base_alignment(row_major);
2095
2096 /* (5) If the member is a column-major matrix with <C> columns and
2097 * <R> rows, the matrix is stored identically to an array of
2098 * <C> column vectors with <R> components each, according to
2099 * rule (4).
2100 *
2101 * (7) If the member is a row-major matrix with <C> columns and <R>
2102 * rows, the matrix is stored identically to an array of <R>
2103 * row vectors with <C> components each, according to rule (4).
2104 */
2105 if (this->is_matrix()) {
2106 const struct glsl_type *vec_type, *array_type;
2107 int c = this->matrix_columns;
2108 int r = this->vector_elements;
2109
2110 if (row_major) {
2111 vec_type = get_instance(base_type, c, 1);
2112 array_type = glsl_type::get_array_instance(vec_type, r);
2113 } else {
2114 vec_type = get_instance(base_type, r, 1);
2115 array_type = glsl_type::get_array_instance(vec_type, c);
2116 }
2117
2118 return array_type->std430_base_alignment(false);
2119 }
2120
2121 /* (9) If the member is a structure, the base alignment of the
2122 * structure is <N>, where <N> is the largest base alignment
2123 * value of any of its members, and rounded up to the base
2124 * alignment of a vec4. The individual members of this
2125 * sub-structure are then assigned offsets by applying this set
2126 * of rules recursively, where the base offset of the first
2127 * member of the sub-structure is equal to the aligned offset
2128 * of the structure. The structure may have padding at the end;
2129 * the base offset of the member following the sub-structure is
2130 * rounded up to the next multiple of the base alignment of the
2131 * structure.
2132 */
2133 if (this->is_struct()) {
2134 unsigned base_alignment = 0;
2135 for (unsigned i = 0; i < this->length; i++) {
2136 bool field_row_major = row_major;
2137 const enum glsl_matrix_layout matrix_layout =
2138 glsl_matrix_layout(this->fields.structure[i].matrix_layout);
2139 if (matrix_layout == GLSL_MATRIX_LAYOUT_ROW_MAJOR) {
2140 field_row_major = true;
2141 } else if (matrix_layout == GLSL_MATRIX_LAYOUT_COLUMN_MAJOR) {
2142 field_row_major = false;
2143 }
2144
2145 const struct glsl_type *field_type = this->fields.structure[i].type;
2146 base_alignment = MAX2(base_alignment,
2147 field_type->std430_base_alignment(field_row_major));
2148 }
2149 assert(base_alignment > 0);
2150 return base_alignment;
2151 }
2152 assert(!"not reached");
2153 return -1;
2154 }
2155
2156 unsigned
2157 glsl_type::std430_array_stride(bool row_major) const
2158 {
2159 unsigned N = is_64bit() ? 8 : 4;
2160
2161 /* Notice that the array stride of a vec3 is not 3 * N but 4 * N.
2162 * See OpenGL 4.30 spec, section 7.6.2.2 "Standard Uniform Block Layout"
2163 *
2164 * (3) If the member is a three-component vector with components consuming
2165 * <N> basic machine units, the base alignment is 4<N>.
2166 */
2167 if (this->is_vector() && this->vector_elements == 3)
2168 return 4 * N;
2169
2170 /* By default use std430_size(row_major) */
2171 unsigned stride = this->std430_size(row_major);
2172 assert(this->explicit_stride == 0 || this->explicit_stride == stride);
2173 return stride;
2174 }
2175
2176 /* Note that the value returned by this method is only correct if the
2177 * explit offset, and stride values are set, so only with SPIR-V shaders.
2178 * Should not be used with GLSL shaders.
2179 */
2180
2181 unsigned
2182 glsl_type::explicit_size(bool align_to_stride) const
2183 {
2184 if (this->is_struct() || this->is_interface()) {
2185 if (this->length > 0) {
2186 unsigned size = 0;
2187
2188 for (unsigned i = 0; i < this->length; i++) {
2189 assert(this->fields.structure[i].offset >= 0);
2190 unsigned last_byte = this->fields.structure[i].offset +
2191 this->fields.structure[i].type->explicit_size();
2192 size = MAX2(size, last_byte);
2193 }
2194
2195 return size;
2196 } else {
2197 return 0;
2198 }
2199 } else if (this->is_array()) {
2200 /* From ARB_program_interface_query spec:
2201 *
2202 * "For the property of BUFFER_DATA_SIZE, then the implementation-dependent
2203 * minimum total buffer object size, in basic machine units, required to
2204 * hold all active variables associated with an active uniform block, shader
2205 * storage block, or atomic counter buffer is written to <params>. If the
2206 * final member of an active shader storage block is array with no declared
2207 * size, the minimum buffer size is computed assuming the array was declared
2208 * as an array with one element."
2209 *
2210 */
2211 if (this->is_unsized_array())
2212 return this->explicit_stride;
2213
2214 assert(this->length > 0);
2215 unsigned elem_size = align_to_stride ? this->explicit_stride : this->fields.array->explicit_size();
2216 assert(this->explicit_stride >= elem_size);
2217
2218 return this->explicit_stride * (this->length - 1) + elem_size;
2219 } else if (this->is_matrix()) {
2220 const struct glsl_type *elem_type;
2221 unsigned length;
2222
2223 if (this->interface_row_major) {
2224 elem_type = get_instance(this->base_type,
2225 this->matrix_columns, 1);
2226 length = this->vector_elements;
2227 } else {
2228 elem_type = get_instance(this->base_type,
2229 this->vector_elements, 1);
2230 length = this->matrix_columns;
2231 }
2232
2233 unsigned elem_size = align_to_stride ? this->explicit_stride : elem_type->explicit_size();
2234
2235 assert(this->explicit_stride);
2236 return this->explicit_stride * (length - 1) + elem_size;
2237 }
2238
2239 unsigned N = this->bit_size() / 8;
2240
2241 return this->vector_elements * N;
2242 }
2243
2244 unsigned
2245 glsl_type::std430_size(bool row_major) const
2246 {
2247 unsigned N = is_64bit() ? 8 : 4;
2248
2249 /* OpenGL 4.30 spec, section 7.6.2.2 "Standard Uniform Block Layout":
2250 *
2251 * "When using the std430 storage layout, shader storage blocks will be
2252 * laid out in buffer storage identically to uniform and shader storage
2253 * blocks using the std140 layout, except that the base alignment and
2254 * stride of arrays of scalars and vectors in rule 4 and of structures
2255 * in rule 9 are not rounded up a multiple of the base alignment of a vec4.
2256 */
2257 if (this->is_scalar() || this->is_vector()) {
2258 assert(this->explicit_stride == 0);
2259 return this->vector_elements * N;
2260 }
2261
2262 if (this->without_array()->is_matrix()) {
2263 const struct glsl_type *element_type;
2264 const struct glsl_type *vec_type;
2265 unsigned int array_len;
2266
2267 if (this->is_array()) {
2268 element_type = this->without_array();
2269 array_len = this->arrays_of_arrays_size();
2270 } else {
2271 element_type = this;
2272 array_len = 1;
2273 }
2274
2275 if (row_major) {
2276 vec_type = get_instance(element_type->base_type,
2277 element_type->matrix_columns, 1);
2278
2279 array_len *= element_type->vector_elements;
2280 } else {
2281 vec_type = get_instance(element_type->base_type,
2282 element_type->vector_elements, 1);
2283 array_len *= element_type->matrix_columns;
2284 }
2285 const glsl_type *array_type = glsl_type::get_array_instance(vec_type,
2286 array_len);
2287
2288 return array_type->std430_size(false);
2289 }
2290
2291 if (this->is_array()) {
2292 unsigned stride;
2293 if (this->without_array()->is_struct())
2294 stride = this->without_array()->std430_size(row_major);
2295 else
2296 stride = this->without_array()->std430_base_alignment(row_major);
2297
2298 unsigned size = this->arrays_of_arrays_size() * stride;
2299 assert(this->explicit_stride == 0 ||
2300 size == this->length * this->explicit_stride);
2301 return size;
2302 }
2303
2304 if (this->is_struct() || this->is_interface()) {
2305 unsigned size = 0;
2306 unsigned max_align = 0;
2307
2308 for (unsigned i = 0; i < this->length; i++) {
2309 bool field_row_major = row_major;
2310 const enum glsl_matrix_layout matrix_layout =
2311 glsl_matrix_layout(this->fields.structure[i].matrix_layout);
2312 if (matrix_layout == GLSL_MATRIX_LAYOUT_ROW_MAJOR) {
2313 field_row_major = true;
2314 } else if (matrix_layout == GLSL_MATRIX_LAYOUT_COLUMN_MAJOR) {
2315 field_row_major = false;
2316 }
2317
2318 const struct glsl_type *field_type = this->fields.structure[i].type;
2319 unsigned align = field_type->std430_base_alignment(field_row_major);
2320 size = glsl_align(size, align);
2321 size += field_type->std430_size(field_row_major);
2322
2323 max_align = MAX2(align, max_align);
2324 }
2325 size = glsl_align(size, max_align);
2326 return size;
2327 }
2328
2329 assert(!"not reached");
2330 return -1;
2331 }
2332
2333 const glsl_type *
2334 glsl_type::get_explicit_std430_type(bool row_major) const
2335 {
2336 if (this->is_vector() || this->is_scalar()) {
2337 return this;
2338 } else if (this->is_matrix()) {
2339 const glsl_type *vec_type;
2340 if (row_major)
2341 vec_type = get_instance(this->base_type, this->matrix_columns, 1);
2342 else
2343 vec_type = get_instance(this->base_type, this->vector_elements, 1);
2344 unsigned stride = vec_type->std430_array_stride(false);
2345 return get_instance(this->base_type, this->vector_elements,
2346 this->matrix_columns, stride, row_major);
2347 } else if (this->is_array()) {
2348 const glsl_type *elem_type =
2349 this->fields.array->get_explicit_std430_type(row_major);
2350 unsigned stride = this->fields.array->std430_array_stride(row_major);
2351 return get_array_instance(elem_type, this->length, stride);
2352 } else if (this->is_struct() || this->is_interface()) {
2353 glsl_struct_field *fields = new glsl_struct_field[this->length];
2354 unsigned offset = 0;
2355 for (unsigned i = 0; i < length; i++) {
2356 fields[i] = this->fields.structure[i];
2357
2358 bool field_row_major = row_major;
2359 if (fields[i].matrix_layout == GLSL_MATRIX_LAYOUT_COLUMN_MAJOR) {
2360 field_row_major = false;
2361 } else if (fields[i].matrix_layout == GLSL_MATRIX_LAYOUT_ROW_MAJOR) {
2362 field_row_major = true;
2363 }
2364 fields[i].type =
2365 fields[i].type->get_explicit_std430_type(field_row_major);
2366
2367 unsigned fsize = fields[i].type->std430_size(field_row_major);
2368 unsigned falign = fields[i].type->std430_base_alignment(field_row_major);
2369 /* From the GLSL 460 spec section "Uniform and Shader Storage Block
2370 * Layout Qualifiers":
2371 *
2372 * "The actual offset of a member is computed as follows: If
2373 * offset was declared, start with that offset, otherwise start
2374 * with the next available offset. If the resulting offset is not
2375 * a multiple of the actual alignment, increase it to the first
2376 * offset that is a multiple of the actual alignment. This results
2377 * in the actual offset the member will have."
2378 */
2379 if (fields[i].offset >= 0) {
2380 assert((unsigned)fields[i].offset >= offset);
2381 offset = fields[i].offset;
2382 }
2383 offset = glsl_align(offset, falign);
2384 fields[i].offset = offset;
2385 offset += fsize;
2386 }
2387
2388 const glsl_type *type;
2389 if (this->is_struct())
2390 type = get_struct_instance(fields, this->length, this->name);
2391 else
2392 type = get_interface_instance(fields, this->length,
2393 (enum glsl_interface_packing)this->interface_packing,
2394 this->interface_row_major,
2395 this->name);
2396
2397 delete[] fields;
2398 return type;
2399 } else {
2400 unreachable("Invalid type for SSBO");
2401 }
2402 }
2403
2404 const glsl_type *
2405 glsl_type::get_explicit_interface_type(bool supports_std430) const
2406 {
2407 enum glsl_interface_packing packing =
2408 this->get_internal_ifc_packing(supports_std430);
2409 if (packing == GLSL_INTERFACE_PACKING_STD140) {
2410 return this->get_explicit_std140_type(this->interface_row_major);
2411 } else {
2412 assert(packing == GLSL_INTERFACE_PACKING_STD430);
2413 return this->get_explicit_std430_type(this->interface_row_major);
2414 }
2415 }
2416
2417 /* This differs from get_explicit_std430_type() in that it:
2418 * - can size arrays slightly smaller ("stride * (len - 1) + elem_size" instead
2419 * of "stride * len")
2420 * - consumes a glsl_type_size_align_func which allows 8 and 16-bit values to be
2421 * packed more tightly
2422 * - overrides any struct field offsets but get_explicit_std430_type() tries to
2423 * respect any existing ones
2424 */
2425 const glsl_type *
2426 glsl_type::get_explicit_type_for_size_align(glsl_type_size_align_func type_info,
2427 unsigned *size, unsigned *alignment) const
2428 {
2429 if (this->is_scalar() || this->is_vector()) {
2430 type_info(this, size, alignment);
2431 return this;
2432 } else if (this->is_array()) {
2433 unsigned elem_size, elem_align;
2434 const struct glsl_type *explicit_element =
2435 this->fields.array->get_explicit_type_for_size_align(type_info, &elem_size, &elem_align);
2436
2437 unsigned stride = align(elem_size, elem_align);
2438
2439 *size = stride * (this->length - 1) + elem_size;
2440 *alignment = elem_align;
2441 return glsl_type::get_array_instance(explicit_element, this->length, stride);
2442 } else if (this->is_struct() || this->is_interface()) {
2443 struct glsl_struct_field *fields = (struct glsl_struct_field *)
2444 malloc(sizeof(struct glsl_struct_field) * this->length);
2445
2446 *size = 0;
2447 *alignment = 0;
2448 for (unsigned i = 0; i < this->length; i++) {
2449 fields[i] = this->fields.structure[i];
2450 assert(fields[i].matrix_layout != GLSL_MATRIX_LAYOUT_ROW_MAJOR);
2451
2452 unsigned field_size, field_align;
2453 fields[i].type =
2454 fields[i].type->get_explicit_type_for_size_align(type_info, &field_size, &field_align);
2455 fields[i].offset = align(*size, field_align);
2456
2457 *size = fields[i].offset + field_size;
2458 *alignment = MAX2(*alignment, field_align);
2459 }
2460
2461 const glsl_type *type;
2462 if (this->is_struct()) {
2463 type = get_struct_instance(fields, this->length, this->name, false);
2464 } else {
2465 type = get_interface_instance(fields, this->length,
2466 (enum glsl_interface_packing)this->interface_packing,
2467 this->interface_row_major,
2468 this->name);
2469 }
2470 free(fields);
2471 return type;
2472 } else if (this->is_matrix()) {
2473 unsigned col_size, col_align;
2474 type_info(this->column_type(), &col_size, &col_align);
2475 unsigned stride = align(col_size, col_align);
2476
2477 *size = this->matrix_columns * stride;
2478 *alignment = col_align;
2479 return glsl_type::get_instance(this->base_type, this->vector_elements,
2480 this->matrix_columns, stride, false);
2481 } else {
2482 unreachable("Unhandled type.");
2483 }
2484 }
2485
2486 unsigned
2487 glsl_type::count_vec4_slots(bool is_gl_vertex_input, bool is_bindless) const
2488 {
2489 /* From page 31 (page 37 of the PDF) of the GLSL 1.50 spec:
2490 *
2491 * "A scalar input counts the same amount against this limit as a vec4,
2492 * so applications may want to consider packing groups of four
2493 * unrelated float inputs together into a vector to better utilize the
2494 * capabilities of the underlying hardware. A matrix input will use up
2495 * multiple locations. The number of locations used will equal the
2496 * number of columns in the matrix."
2497 *
2498 * The spec does not explicitly say how arrays are counted. However, it
2499 * should be safe to assume the total number of slots consumed by an array
2500 * is the number of entries in the array multiplied by the number of slots
2501 * consumed by a single element of the array.
2502 *
2503 * The spec says nothing about how structs are counted, because vertex
2504 * attributes are not allowed to be (or contain) structs. However, Mesa
2505 * allows varying structs, the number of varying slots taken up by a
2506 * varying struct is simply equal to the sum of the number of slots taken
2507 * up by each element.
2508 *
2509 * Doubles are counted different depending on whether they are vertex
2510 * inputs or everything else. Vertex inputs from ARB_vertex_attrib_64bit
2511 * take one location no matter what size they are, otherwise dvec3/4
2512 * take two locations.
2513 */
2514 switch (this->base_type) {
2515 case GLSL_TYPE_UINT:
2516 case GLSL_TYPE_INT:
2517 case GLSL_TYPE_UINT8:
2518 case GLSL_TYPE_INT8:
2519 case GLSL_TYPE_UINT16:
2520 case GLSL_TYPE_INT16:
2521 case GLSL_TYPE_FLOAT:
2522 case GLSL_TYPE_FLOAT16:
2523 case GLSL_TYPE_BOOL:
2524 return this->matrix_columns;
2525 case GLSL_TYPE_DOUBLE:
2526 case GLSL_TYPE_UINT64:
2527 case GLSL_TYPE_INT64:
2528 if (this->vector_elements > 2 && !is_gl_vertex_input)
2529 return this->matrix_columns * 2;
2530 else
2531 return this->matrix_columns;
2532 case GLSL_TYPE_STRUCT:
2533 case GLSL_TYPE_INTERFACE: {
2534 unsigned size = 0;
2535
2536 for (unsigned i = 0; i < this->length; i++) {
2537 const glsl_type *member_type = this->fields.structure[i].type;
2538 size += member_type->count_vec4_slots(is_gl_vertex_input, is_bindless);
2539 }
2540
2541 return size;
2542 }
2543
2544 case GLSL_TYPE_ARRAY: {
2545 const glsl_type *element = this->fields.array;
2546 return this->length * element->count_vec4_slots(is_gl_vertex_input,
2547 is_bindless);
2548 }
2549
2550 case GLSL_TYPE_SAMPLER:
2551 case GLSL_TYPE_IMAGE:
2552 if (!is_bindless)
2553 return 0;
2554 else
2555 return 1;
2556
2557 case GLSL_TYPE_SUBROUTINE:
2558 return 1;
2559
2560 case GLSL_TYPE_FUNCTION:
2561 case GLSL_TYPE_ATOMIC_UINT:
2562 case GLSL_TYPE_VOID:
2563 case GLSL_TYPE_ERROR:
2564 break;
2565 }
2566
2567 assert(!"Unexpected type in count_attribute_slots()");
2568
2569 return 0;
2570 }
2571
2572 unsigned
2573 glsl_type::count_dword_slots(bool is_bindless) const
2574 {
2575 switch (this->base_type) {
2576 case GLSL_TYPE_UINT:
2577 case GLSL_TYPE_INT:
2578 case GLSL_TYPE_FLOAT:
2579 case GLSL_TYPE_BOOL:
2580 return this->components();
2581 case GLSL_TYPE_UINT16:
2582 case GLSL_TYPE_INT16:
2583 case GLSL_TYPE_FLOAT16:
2584 return DIV_ROUND_UP(this->components(), 2);
2585 case GLSL_TYPE_UINT8:
2586 case GLSL_TYPE_INT8:
2587 return DIV_ROUND_UP(this->components(), 4);
2588 case GLSL_TYPE_IMAGE:
2589 case GLSL_TYPE_SAMPLER:
2590 if (!is_bindless)
2591 return 0;
2592 /* FALLTHROUGH */
2593 case GLSL_TYPE_DOUBLE:
2594 case GLSL_TYPE_UINT64:
2595 case GLSL_TYPE_INT64:
2596 return this->components() * 2;
2597 case GLSL_TYPE_ARRAY:
2598 return this->fields.array->count_dword_slots(is_bindless) *
2599 this->length;
2600
2601 case GLSL_TYPE_INTERFACE:
2602 case GLSL_TYPE_STRUCT: {
2603 unsigned size = 0;
2604 for (unsigned i = 0; i < this->length; i++) {
2605 size += this->fields.structure[i].type->count_dword_slots(is_bindless);
2606 }
2607 return size;
2608 }
2609
2610 case GLSL_TYPE_ATOMIC_UINT:
2611 return 0;
2612 case GLSL_TYPE_SUBROUTINE:
2613 return 1;
2614 case GLSL_TYPE_VOID:
2615 case GLSL_TYPE_ERROR:
2616 case GLSL_TYPE_FUNCTION:
2617 default:
2618 unreachable("invalid type in st_glsl_type_dword_size()");
2619 }
2620
2621 return 0;
2622 }
2623
2624 int
2625 glsl_type::coordinate_components() const
2626 {
2627 enum glsl_sampler_dim dim = (enum glsl_sampler_dim)sampler_dimensionality;
2628 int size = glsl_get_sampler_dim_coordinate_components(dim);
2629
2630 /* Array textures need an additional component for the array index, except
2631 * for cubemap array images that behave like a 2D array of interleaved
2632 * cubemap faces.
2633 */
2634 if (sampler_array &&
2635 !(is_image() && sampler_dimensionality == GLSL_SAMPLER_DIM_CUBE))
2636 size += 1;
2637
2638 return size;
2639 }
2640
2641 /**
2642 * Declarations of type flyweights (glsl_type::_foo_type) and
2643 * convenience pointers (glsl_type::foo_type).
2644 * @{
2645 */
2646 #define DECL_TYPE(NAME, ...) \
2647 const glsl_type glsl_type::_##NAME##_type = glsl_type(__VA_ARGS__, #NAME); \
2648 const glsl_type *const glsl_type::NAME##_type = &glsl_type::_##NAME##_type;
2649
2650 #define STRUCT_TYPE(NAME)
2651
2652 #include "compiler/builtin_type_macros.h"
2653 /** @} */
2654
2655 union packed_type {
2656 uint32_t u32;
2657 struct {
2658 unsigned base_type:5;
2659 unsigned interface_row_major:1;
2660 unsigned vector_elements:3;
2661 unsigned matrix_columns:3;
2662 unsigned explicit_stride:20;
2663 } basic;
2664 struct {
2665 unsigned base_type:5;
2666 unsigned dimensionality:4;
2667 unsigned shadow:1;
2668 unsigned array:1;
2669 unsigned sampled_type:5;
2670 unsigned _pad:16;
2671 } sampler;
2672 struct {
2673 unsigned base_type:5;
2674 unsigned length:13;
2675 unsigned explicit_stride:14;
2676 } array;
2677 struct {
2678 unsigned base_type:5;
2679 unsigned interface_packing_or_packed:2;
2680 unsigned interface_row_major:1;
2681 unsigned length:24;
2682 } strct;
2683 };
2684
2685 static void
2686 encode_glsl_struct_field(blob *blob, const glsl_struct_field *struct_field)
2687 {
2688 encode_type_to_blob(blob, struct_field->type);
2689 blob_write_string(blob, struct_field->name);
2690 blob_write_uint32(blob, struct_field->location);
2691 blob_write_uint32(blob, struct_field->offset);
2692 blob_write_uint32(blob, struct_field->xfb_buffer);
2693 blob_write_uint32(blob, struct_field->xfb_stride);
2694 blob_write_uint32(blob, struct_field->image_format);
2695 blob_write_uint32(blob, struct_field->flags);
2696 }
2697
2698 static void
2699 decode_glsl_struct_field_from_blob(blob_reader *blob, glsl_struct_field *struct_field)
2700 {
2701 struct_field->type = decode_type_from_blob(blob);
2702 struct_field->name = blob_read_string(blob);
2703 struct_field->location = blob_read_uint32(blob);
2704 struct_field->offset = blob_read_uint32(blob);
2705 struct_field->xfb_buffer = blob_read_uint32(blob);
2706 struct_field->xfb_stride = blob_read_uint32(blob);
2707 struct_field->image_format = (pipe_format)blob_read_uint32(blob);
2708 struct_field->flags = blob_read_uint32(blob);
2709 }
2710
2711 void
2712 encode_type_to_blob(struct blob *blob, const glsl_type *type)
2713 {
2714 if (!type) {
2715 blob_write_uint32(blob, 0);
2716 return;
2717 }
2718
2719 STATIC_ASSERT(sizeof(union packed_type) == 4);
2720 union packed_type encoded;
2721 encoded.u32 = 0;
2722 encoded.basic.base_type = type->base_type;
2723
2724 switch (type->base_type) {
2725 case GLSL_TYPE_UINT:
2726 case GLSL_TYPE_INT:
2727 case GLSL_TYPE_FLOAT:
2728 case GLSL_TYPE_FLOAT16:
2729 case GLSL_TYPE_DOUBLE:
2730 case GLSL_TYPE_UINT8:
2731 case GLSL_TYPE_INT8:
2732 case GLSL_TYPE_UINT16:
2733 case GLSL_TYPE_INT16:
2734 case GLSL_TYPE_UINT64:
2735 case GLSL_TYPE_INT64:
2736 case GLSL_TYPE_BOOL:
2737 encoded.basic.interface_row_major = type->interface_row_major;
2738 assert(type->matrix_columns < 8);
2739 if (type->vector_elements <= 4)
2740 encoded.basic.vector_elements = type->vector_elements;
2741 else if (type->vector_elements == 8)
2742 encoded.basic.vector_elements = 5;
2743 else if (type->vector_elements == 16)
2744 encoded.basic.vector_elements = 6;
2745 encoded.basic.matrix_columns = type->matrix_columns;
2746 encoded.basic.explicit_stride = MIN2(type->explicit_stride, 0xfffff);
2747 blob_write_uint32(blob, encoded.u32);
2748 /* If we don't have enough bits for explicit_stride, store it
2749 * separately.
2750 */
2751 if (encoded.basic.explicit_stride == 0xfffff)
2752 blob_write_uint32(blob, type->explicit_stride);
2753 return;
2754 case GLSL_TYPE_SAMPLER:
2755 encoded.sampler.dimensionality = type->sampler_dimensionality;
2756 encoded.sampler.shadow = type->sampler_shadow;
2757 encoded.sampler.array = type->sampler_array;
2758 encoded.sampler.sampled_type = type->sampled_type;
2759 break;
2760 case GLSL_TYPE_SUBROUTINE:
2761 blob_write_uint32(blob, encoded.u32);
2762 blob_write_string(blob, type->name);
2763 return;
2764 case GLSL_TYPE_IMAGE:
2765 encoded.sampler.dimensionality = type->sampler_dimensionality;
2766 encoded.sampler.array = type->sampler_array;
2767 encoded.sampler.sampled_type = type->sampled_type;
2768 break;
2769 case GLSL_TYPE_ATOMIC_UINT:
2770 break;
2771 case GLSL_TYPE_ARRAY:
2772 encoded.array.length = MIN2(type->length, 0x1fff);
2773 encoded.array.explicit_stride = MIN2(type->explicit_stride, 0x3fff);
2774 blob_write_uint32(blob, encoded.u32);
2775 /* If we don't have enough bits for length or explicit_stride, store it
2776 * separately.
2777 */
2778 if (encoded.array.length == 0x1fff)
2779 blob_write_uint32(blob, type->length);
2780 if (encoded.array.explicit_stride == 0x3fff)
2781 blob_write_uint32(blob, type->explicit_stride);
2782 encode_type_to_blob(blob, type->fields.array);
2783 return;
2784 case GLSL_TYPE_STRUCT:
2785 case GLSL_TYPE_INTERFACE:
2786 encoded.strct.length = MIN2(type->length, 0xffffff);
2787 if (type->is_interface()) {
2788 encoded.strct.interface_packing_or_packed = type->interface_packing;
2789 encoded.strct.interface_row_major = type->interface_row_major;
2790 } else {
2791 encoded.strct.interface_packing_or_packed = type->packed;
2792 }
2793 blob_write_uint32(blob, encoded.u32);
2794 blob_write_string(blob, type->name);
2795
2796 /* If we don't have enough bits for length, store it separately. */
2797 if (encoded.strct.length == 0xffffff)
2798 blob_write_uint32(blob, type->length);
2799
2800 for (unsigned i = 0; i < type->length; i++)
2801 encode_glsl_struct_field(blob, &type->fields.structure[i]);
2802 return;
2803 case GLSL_TYPE_VOID:
2804 break;
2805 case GLSL_TYPE_ERROR:
2806 default:
2807 assert(!"Cannot encode type!");
2808 encoded.u32 = 0;
2809 break;
2810 }
2811
2812 blob_write_uint32(blob, encoded.u32);
2813 }
2814
2815 const glsl_type *
2816 decode_type_from_blob(struct blob_reader *blob)
2817 {
2818 union packed_type encoded;
2819 encoded.u32 = blob_read_uint32(blob);
2820
2821 if (encoded.u32 == 0) {
2822 return NULL;
2823 }
2824
2825 glsl_base_type base_type = (glsl_base_type)encoded.basic.base_type;
2826
2827 switch (base_type) {
2828 case GLSL_TYPE_UINT:
2829 case GLSL_TYPE_INT:
2830 case GLSL_TYPE_FLOAT:
2831 case GLSL_TYPE_FLOAT16:
2832 case GLSL_TYPE_DOUBLE:
2833 case GLSL_TYPE_UINT8:
2834 case GLSL_TYPE_INT8:
2835 case GLSL_TYPE_UINT16:
2836 case GLSL_TYPE_INT16:
2837 case GLSL_TYPE_UINT64:
2838 case GLSL_TYPE_INT64:
2839 case GLSL_TYPE_BOOL: {
2840 unsigned explicit_stride = encoded.basic.explicit_stride;
2841 if (explicit_stride == 0xfffff)
2842 explicit_stride = blob_read_uint32(blob);
2843 uint32_t vector_elements = encoded.basic.vector_elements;
2844 if (vector_elements == 5)
2845 vector_elements = 8;
2846 else if (vector_elements == 6)
2847 vector_elements = 16;
2848 return glsl_type::get_instance(base_type, encoded.basic.vector_elements,
2849 encoded.basic.matrix_columns,
2850 explicit_stride,
2851 encoded.basic.interface_row_major);
2852 }
2853 case GLSL_TYPE_SAMPLER:
2854 return glsl_type::get_sampler_instance((enum glsl_sampler_dim)encoded.sampler.dimensionality,
2855 encoded.sampler.shadow,
2856 encoded.sampler.array,
2857 (glsl_base_type) encoded.sampler.sampled_type);
2858 case GLSL_TYPE_SUBROUTINE:
2859 return glsl_type::get_subroutine_instance(blob_read_string(blob));
2860 case GLSL_TYPE_IMAGE:
2861 return glsl_type::get_image_instance((enum glsl_sampler_dim)encoded.sampler.dimensionality,
2862 encoded.sampler.array,
2863 (glsl_base_type) encoded.sampler.sampled_type);
2864 case GLSL_TYPE_ATOMIC_UINT:
2865 return glsl_type::atomic_uint_type;
2866 case GLSL_TYPE_ARRAY: {
2867 unsigned length = encoded.array.length;
2868 if (length == 0x1fff)
2869 length = blob_read_uint32(blob);
2870 unsigned explicit_stride = encoded.array.explicit_stride;
2871 if (explicit_stride == 0x3fff)
2872 explicit_stride = blob_read_uint32(blob);
2873 return glsl_type::get_array_instance(decode_type_from_blob(blob),
2874 length, explicit_stride);
2875 }
2876 case GLSL_TYPE_STRUCT:
2877 case GLSL_TYPE_INTERFACE: {
2878 char *name = blob_read_string(blob);
2879 unsigned num_fields = encoded.strct.length;
2880 if (num_fields == 0xffffff)
2881 num_fields = blob_read_uint32(blob);
2882
2883 glsl_struct_field *fields =
2884 (glsl_struct_field *) malloc(sizeof(glsl_struct_field) * num_fields);
2885 for (unsigned i = 0; i < num_fields; i++)
2886 decode_glsl_struct_field_from_blob(blob, &fields[i]);
2887
2888 const glsl_type *t;
2889 if (base_type == GLSL_TYPE_INTERFACE) {
2890 enum glsl_interface_packing packing =
2891 (glsl_interface_packing) encoded.strct.interface_packing_or_packed;
2892 bool row_major = encoded.strct.interface_row_major;
2893 t = glsl_type::get_interface_instance(fields, num_fields, packing,
2894 row_major, name);
2895 } else {
2896 unsigned packed = encoded.strct.interface_packing_or_packed;
2897 t = glsl_type::get_struct_instance(fields, num_fields, name, packed);
2898 }
2899
2900 free(fields);
2901 return t;
2902 }
2903 case GLSL_TYPE_VOID:
2904 return glsl_type::void_type;
2905 case GLSL_TYPE_ERROR:
2906 default:
2907 assert(!"Cannot decode type!");
2908 return NULL;
2909 }
2910 }
2911
2912 unsigned
2913 glsl_type::cl_alignment() const
2914 {
2915 /* vectors unlike arrays are aligned to their size */
2916 if (this->is_scalar() || this->is_vector())
2917 return this->cl_size();
2918 else if (this->is_array())
2919 return this->without_array()->cl_alignment();
2920 else if (this->is_struct()) {
2921 /* Packed Structs are 0x1 aligned despite their size. */
2922 if (this->packed)
2923 return 1;
2924
2925 unsigned res = 1;
2926 for (unsigned i = 0; i < this->length; ++i) {
2927 struct glsl_struct_field &field = this->fields.structure[i];
2928 res = MAX2(res, field.type->cl_alignment());
2929 }
2930 return res;
2931 }
2932 return 1;
2933 }
2934
2935 unsigned
2936 glsl_type::cl_size() const
2937 {
2938 if (this->is_scalar()) {
2939 return glsl_base_type_get_bit_size(this->base_type) / 8;
2940 } else if (this->is_vector()) {
2941 unsigned vec_elemns = this->vector_elements == 3 ? 4 : this->vector_elements;
2942 return vec_elemns * glsl_base_type_get_bit_size(this->base_type) / 8;
2943 } else if (this->is_array()) {
2944 unsigned size = this->without_array()->cl_size();
2945 return size * this->length;
2946 } else if (this->is_struct()) {
2947 unsigned size = 0;
2948 for (unsigned i = 0; i < this->length; ++i) {
2949 struct glsl_struct_field &field = this->fields.structure[i];
2950 /* if a struct is packed, members don't get aligned */
2951 if (!this->packed)
2952 size = align(size, field.type->cl_alignment());
2953 size += field.type->cl_size();
2954 }
2955 return size;
2956 }
2957 return 1;
2958 }
2959
2960 extern "C" {
2961
2962 int
2963 glsl_get_sampler_dim_coordinate_components(enum glsl_sampler_dim dim)
2964 {
2965 switch (dim) {
2966 case GLSL_SAMPLER_DIM_1D:
2967 case GLSL_SAMPLER_DIM_BUF:
2968 return 1;
2969 case GLSL_SAMPLER_DIM_2D:
2970 case GLSL_SAMPLER_DIM_RECT:
2971 case GLSL_SAMPLER_DIM_MS:
2972 case GLSL_SAMPLER_DIM_EXTERNAL:
2973 case GLSL_SAMPLER_DIM_SUBPASS:
2974 case GLSL_SAMPLER_DIM_SUBPASS_MS:
2975 return 2;
2976 case GLSL_SAMPLER_DIM_3D:
2977 case GLSL_SAMPLER_DIM_CUBE:
2978 return 3;
2979 default:
2980 unreachable("Unknown sampler dim");
2981 }
2982 }
2983
2984 void
2985 glsl_print_type(FILE *f, const glsl_type *t)
2986 {
2987 if (t->is_array()) {
2988 fprintf(f, "(array ");
2989 glsl_print_type(f, t->fields.array);
2990 fprintf(f, " %u)", t->length);
2991 } else if (t->is_struct() && !is_gl_identifier(t->name)) {
2992 fprintf(f, "%s@%p", t->name, (void *) t);
2993 } else {
2994 fprintf(f, "%s", t->name);
2995 }
2996 }
2997
2998 }