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