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