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