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