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