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 <string.h>
26 #include "glsl_types.h"
27 #include "util/compiler.h"
28 #include "util/glheader.h"
29 #include "util/hash_table.h"
30 #include "util/macros.h"
31 #include "util/ralloc.h"
32 #include "util/u_math.h"
33 #include "util/u_string.h"
34 #include "util/simple_mtx.h"
35
36 static simple_mtx_t glsl_type_cache_mutex = SIMPLE_MTX_INITIALIZER;
37
38 static struct {
39 void *mem_ctx;
40
41 /* Use a linear (arena) allocator for all the new types, since
42 * they are not meant to be deallocated individually.
43 */
44 linear_ctx *lin_ctx;
45
46 /* There might be multiple users for types (e.g. application using OpenGL
47 * and Vulkan simultaneously or app using multiple Vulkan instances). Counter
48 * is used to make sure we don't release the types if a user is still present.
49 */
50 uint32_t users;
51
52 struct hash_table *explicit_matrix_types;
53 struct hash_table *array_types;
54 struct hash_table *cmat_types;
55 struct hash_table *struct_types;
56 struct hash_table *interface_types;
57 struct hash_table *subroutine_types;
58 } glsl_type_cache;
59
60 static const glsl_type *
make_vector_matrix_type(linear_ctx * lin_ctx,uint32_t gl_type,enum glsl_base_type base_type,unsigned vector_elements,unsigned matrix_columns,const char * name,unsigned explicit_stride,bool row_major,unsigned explicit_alignment)61 make_vector_matrix_type(linear_ctx *lin_ctx, uint32_t gl_type,
62 enum glsl_base_type base_type, unsigned vector_elements,
63 unsigned matrix_columns, const char *name,
64 unsigned explicit_stride, bool row_major,
65 unsigned explicit_alignment)
66 {
67 assert(lin_ctx != NULL);
68 assert(name != NULL);
69 assert(util_is_power_of_two_or_zero(explicit_alignment));
70
71 /* Neither dimension is zero or both dimensions are zero. */
72 assert((vector_elements == 0) == (matrix_columns == 0));
73
74 glsl_type *t = linear_zalloc(lin_ctx, glsl_type);
75 t->gl_type = gl_type;
76 t->base_type = base_type;
77 t->sampled_type = GLSL_TYPE_VOID;
78 t->interface_row_major = row_major;
79 t->vector_elements = vector_elements;
80 t->matrix_columns = matrix_columns;
81 t->explicit_stride = explicit_stride;
82 t->explicit_alignment = explicit_alignment;
83 t->name_id = (uintptr_t)linear_strdup(lin_ctx, name);
84
85 return t;
86 }
87
88 static void
fill_struct_type(glsl_type * t,const glsl_struct_field * fields,unsigned num_fields,const char * name,bool packed,unsigned explicit_alignment)89 fill_struct_type(glsl_type *t, const glsl_struct_field *fields, unsigned num_fields,
90 const char *name, bool packed, unsigned explicit_alignment)
91 {
92 assert(util_is_power_of_two_or_zero(explicit_alignment));
93 t->base_type = GLSL_TYPE_STRUCT;
94 t->sampled_type = GLSL_TYPE_VOID;
95 t->packed = packed;
96 t->length = num_fields;
97 t->name_id = (uintptr_t)name;
98 t->explicit_alignment = explicit_alignment;
99 t->fields.structure = fields;
100 }
101
102 static const glsl_type *
make_struct_type(linear_ctx * lin_ctx,const glsl_struct_field * fields,unsigned num_fields,const char * name,bool packed,unsigned explicit_alignment)103 make_struct_type(linear_ctx *lin_ctx, const glsl_struct_field *fields, unsigned num_fields,
104 const char *name, bool packed,
105 unsigned explicit_alignment)
106 {
107 assert(lin_ctx != NULL);
108 assert(name != NULL);
109
110 glsl_type *t = linear_zalloc(lin_ctx, glsl_type);
111 const char *copied_name = linear_strdup(lin_ctx, name);
112
113 glsl_struct_field *copied_fields =
114 linear_zalloc_array(lin_ctx, glsl_struct_field, num_fields);
115
116 for (unsigned i = 0; i < num_fields; i++) {
117 copied_fields[i] = fields[i];
118 copied_fields[i].name = linear_strdup(lin_ctx, fields[i].name);
119 }
120
121 fill_struct_type(t, copied_fields, num_fields, copied_name, packed, explicit_alignment);
122
123 return t;
124 }
125
126 static void
fill_interface_type(glsl_type * t,const glsl_struct_field * fields,unsigned num_fields,enum glsl_interface_packing packing,bool row_major,const char * name)127 fill_interface_type(glsl_type *t, const glsl_struct_field *fields, unsigned num_fields,
128 enum glsl_interface_packing packing,
129 bool row_major, const char *name)
130 {
131 t->base_type = GLSL_TYPE_INTERFACE;
132 t->sampled_type = GLSL_TYPE_VOID;
133 t->interface_packing = (unsigned)packing;
134 t->interface_row_major = (unsigned)row_major;
135 t->length = num_fields;
136 t->name_id = (uintptr_t)name;
137 t->fields.structure = fields;
138 }
139
140 static const glsl_type *
make_interface_type(linear_ctx * lin_ctx,const glsl_struct_field * fields,unsigned num_fields,enum glsl_interface_packing packing,bool row_major,const char * name)141 make_interface_type(linear_ctx *lin_ctx, const glsl_struct_field *fields, unsigned num_fields,
142 enum glsl_interface_packing packing,
143 bool row_major, const char *name)
144 {
145 assert(lin_ctx != NULL);
146 assert(name != NULL);
147
148 glsl_type *t = linear_zalloc(lin_ctx, glsl_type);
149 const char *copied_name = linear_strdup(lin_ctx, name);
150
151 glsl_struct_field *copied_fields =
152 linear_zalloc_array(lin_ctx, glsl_struct_field, num_fields);
153
154 for (unsigned i = 0; i < num_fields; i++) {
155 copied_fields[i] = fields[i];
156 copied_fields[i].name = linear_strdup(lin_ctx, fields[i].name);
157 }
158
159 fill_interface_type(t, copied_fields, num_fields, packing, row_major, copied_name);
160
161 return t;
162 }
163
164 static const glsl_type *
make_subroutine_type(linear_ctx * lin_ctx,const char * subroutine_name)165 make_subroutine_type(linear_ctx *lin_ctx, const char *subroutine_name)
166 {
167 assert(lin_ctx != NULL);
168 assert(subroutine_name != NULL);
169
170 glsl_type *t = linear_zalloc(lin_ctx, glsl_type);
171 t->base_type = GLSL_TYPE_SUBROUTINE;
172 t->sampled_type = GLSL_TYPE_VOID;
173 t->vector_elements = 1;
174 t->matrix_columns = 1;
175 t->name_id = (uintptr_t)linear_strdup(lin_ctx, subroutine_name);
176
177 return t;
178 }
179
180 bool
glsl_contains_sampler(const glsl_type * t)181 glsl_contains_sampler(const glsl_type *t)
182 {
183 if (glsl_type_is_array(t)) {
184 return glsl_contains_sampler(t->fields.array);
185 } else if (glsl_type_is_struct(t) || glsl_type_is_interface(t)) {
186 for (unsigned int i = 0; i < t->length; i++) {
187 if (glsl_contains_sampler(t->fields.structure[i].type))
188 return true;
189 }
190 return false;
191 } else {
192 return glsl_type_is_sampler(t);
193 }
194 }
195
196 bool
glsl_contains_array(const glsl_type * t)197 glsl_contains_array(const glsl_type *t)
198 {
199 if (glsl_type_is_struct(t) || glsl_type_is_interface(t)) {
200 for (unsigned int i = 0; i < t->length; i++) {
201 if (glsl_contains_array(t->fields.structure[i].type))
202 return true;
203 }
204 return false;
205 } else {
206 return glsl_type_is_array(t);
207 }
208 }
209
210 bool
glsl_contains_integer(const glsl_type * t)211 glsl_contains_integer(const glsl_type *t)
212 {
213 if (glsl_type_is_array(t)) {
214 return glsl_contains_integer(t->fields.array);
215 } else if (glsl_type_is_struct(t) || glsl_type_is_interface(t)) {
216 for (unsigned int i = 0; i < t->length; i++) {
217 if (glsl_contains_integer(t->fields.structure[i].type))
218 return true;
219 }
220 return false;
221 } else {
222 return glsl_type_is_integer(t);
223 }
224 }
225
226 bool
glsl_contains_double(const glsl_type * t)227 glsl_contains_double(const glsl_type *t)
228 {
229 if (glsl_type_is_array(t)) {
230 return glsl_contains_double(t->fields.array);
231 } else if (glsl_type_is_struct(t) || glsl_type_is_interface(t)) {
232 for (unsigned int i = 0; i < t->length; i++) {
233 if (glsl_contains_double(t->fields.structure[i].type))
234 return true;
235 }
236 return false;
237 } else {
238 return glsl_type_is_double(t);
239 }
240 }
241
242 bool
glsl_type_contains_32bit(const glsl_type * t)243 glsl_type_contains_32bit(const glsl_type *t)
244 {
245 if (glsl_type_is_array(t)) {
246 return glsl_type_contains_32bit(t->fields.array);
247 } else if (glsl_type_is_struct(t) || glsl_type_is_interface(t)) {
248 for (unsigned int i = 0; i < t->length; i++) {
249 if (glsl_type_contains_32bit(t->fields.structure[i].type))
250 return true;
251 }
252 return false;
253 } else {
254 return glsl_type_is_32bit(t);
255 }
256 }
257
258 bool
glsl_type_contains_64bit(const glsl_type * t)259 glsl_type_contains_64bit(const glsl_type *t)
260 {
261 if (glsl_type_is_array(t)) {
262 return glsl_type_contains_64bit(t->fields.array);
263 } else if (glsl_type_is_struct(t) || glsl_type_is_interface(t)) {
264 for (unsigned int i = 0; i < t->length; i++) {
265 if (glsl_type_contains_64bit(t->fields.structure[i].type))
266 return true;
267 }
268 return false;
269 } else {
270 return glsl_type_is_64bit(t);
271 }
272 }
273
274 bool
glsl_contains_opaque(const glsl_type * t)275 glsl_contains_opaque(const glsl_type *t)
276 {
277 switch (t->base_type) {
278 case GLSL_TYPE_SAMPLER:
279 case GLSL_TYPE_IMAGE:
280 case GLSL_TYPE_ATOMIC_UINT:
281 return true;
282 case GLSL_TYPE_ARRAY:
283 return glsl_contains_opaque(t->fields.array);
284 case GLSL_TYPE_STRUCT:
285 case GLSL_TYPE_INTERFACE:
286 for (unsigned int i = 0; i < t->length; i++) {
287 if (glsl_contains_opaque(t->fields.structure[i].type))
288 return true;
289 }
290 return false;
291 default:
292 return false;
293 }
294 }
295
296 bool
glsl_contains_subroutine(const glsl_type * t)297 glsl_contains_subroutine(const glsl_type *t)
298 {
299 if (glsl_type_is_array(t)) {
300 return glsl_contains_subroutine(t->fields.array);
301 } else if (glsl_type_is_struct(t) || glsl_type_is_interface(t)) {
302 for (unsigned int i = 0; i < t->length; i++) {
303 if (glsl_contains_subroutine(t->fields.structure[i].type))
304 return true;
305 }
306 return false;
307 } else {
308 return glsl_type_is_subroutine(t);
309 }
310 }
311
312 bool
glsl_type_contains_image(const glsl_type * t)313 glsl_type_contains_image(const glsl_type *t)
314 {
315 if (glsl_type_is_array(t)) {
316 return glsl_type_contains_image(t->fields.array);
317 } else if (glsl_type_is_struct(t) || glsl_type_is_interface(t)) {
318 for (unsigned int i = 0; i < t->length; i++) {
319 if (glsl_type_contains_image(t->fields.structure[i].type))
320 return true;
321 }
322 return false;
323 } else {
324 return glsl_type_is_image(t);
325 }
326 }
327
328 const glsl_type *
glsl_get_base_glsl_type(const glsl_type * t)329 glsl_get_base_glsl_type(const glsl_type *t)
330 {
331 switch (t->base_type) {
332 case GLSL_TYPE_UINT:
333 return &glsl_type_builtin_uint;
334 case GLSL_TYPE_UINT16:
335 return &glsl_type_builtin_uint16_t;
336 case GLSL_TYPE_UINT8:
337 return &glsl_type_builtin_uint8_t;
338 case GLSL_TYPE_INT:
339 return &glsl_type_builtin_int;
340 case GLSL_TYPE_INT16:
341 return &glsl_type_builtin_int16_t;
342 case GLSL_TYPE_INT8:
343 return &glsl_type_builtin_int8_t;
344 case GLSL_TYPE_FLOAT:
345 return &glsl_type_builtin_float;
346 case GLSL_TYPE_FLOAT16:
347 return &glsl_type_builtin_float16_t;
348 case GLSL_TYPE_DOUBLE:
349 return &glsl_type_builtin_double;
350 case GLSL_TYPE_BOOL:
351 return &glsl_type_builtin_bool;
352 case GLSL_TYPE_UINT64:
353 return &glsl_type_builtin_uint64_t;
354 case GLSL_TYPE_INT64:
355 return &glsl_type_builtin_int64_t;
356 default:
357 return &glsl_type_builtin_error;
358 }
359 }
360
361 const glsl_type *
glsl_get_scalar_type(const glsl_type * t)362 glsl_get_scalar_type(const glsl_type *t)
363 {
364 const glsl_type *type = t;
365
366 /* Handle arrays */
367 while (type->base_type == GLSL_TYPE_ARRAY)
368 type = type->fields.array;
369
370 const glsl_type *scalar_type = glsl_get_base_glsl_type(type);
371 if (scalar_type == &glsl_type_builtin_error)
372 return type;
373
374 return scalar_type;
375 }
376
377
378 const glsl_type *
glsl_get_bare_type(const glsl_type * t)379 glsl_get_bare_type(const glsl_type *t)
380 {
381 switch (t->base_type) {
382 case GLSL_TYPE_UINT8:
383 case GLSL_TYPE_INT8:
384 case GLSL_TYPE_UINT16:
385 case GLSL_TYPE_INT16:
386 case GLSL_TYPE_FLOAT16:
387 case GLSL_TYPE_UINT:
388 case GLSL_TYPE_INT:
389 case GLSL_TYPE_FLOAT:
390 case GLSL_TYPE_BOOL:
391 case GLSL_TYPE_DOUBLE:
392 case GLSL_TYPE_UINT64:
393 case GLSL_TYPE_INT64:
394 return glsl_simple_type(t->base_type, t->vector_elements,
395 t->matrix_columns);
396
397 case GLSL_TYPE_STRUCT:
398 case GLSL_TYPE_INTERFACE: {
399 glsl_struct_field *bare_fields = (glsl_struct_field *)
400 calloc(t->length, sizeof(glsl_struct_field));
401 for (unsigned i = 0; i < t->length; i++) {
402 bare_fields[i].type = glsl_get_bare_type(t->fields.structure[i].type);
403 bare_fields[i].name = t->fields.structure[i].name;
404 }
405 const glsl_type *bare_type =
406 glsl_struct_type(bare_fields, t->length, glsl_get_type_name(t), false);
407 free(bare_fields);
408 return bare_type;
409 }
410
411 case GLSL_TYPE_ARRAY:
412 return glsl_array_type(glsl_get_bare_type(t->fields.array), t->length,
413 0);
414
415 case GLSL_TYPE_COOPERATIVE_MATRIX:
416 case GLSL_TYPE_SAMPLER:
417 case GLSL_TYPE_TEXTURE:
418 case GLSL_TYPE_IMAGE:
419 case GLSL_TYPE_ATOMIC_UINT:
420 case GLSL_TYPE_VOID:
421 case GLSL_TYPE_SUBROUTINE:
422 case GLSL_TYPE_ERROR:
423 return t;
424 }
425
426 unreachable("Invalid base type");
427 }
428
429 const glsl_type *
glsl_float16_type(const glsl_type * t)430 glsl_float16_type(const glsl_type *t)
431 {
432 assert(t->base_type == GLSL_TYPE_FLOAT);
433
434 return glsl_simple_explicit_type(GLSL_TYPE_FLOAT16, t->vector_elements,
435 t->matrix_columns, t->explicit_stride,
436 t->interface_row_major, 0);
437 }
438
439 const glsl_type *
glsl_int16_type(const glsl_type * t)440 glsl_int16_type(const glsl_type *t)
441 {
442 assert(t->base_type == GLSL_TYPE_INT);
443
444 return glsl_simple_explicit_type(GLSL_TYPE_INT16, t->vector_elements,
445 t->matrix_columns, t->explicit_stride,
446 t->interface_row_major, 0);
447 }
448
449 const glsl_type *
glsl_uint16_type(const glsl_type * t)450 glsl_uint16_type(const glsl_type *t)
451 {
452 assert(t->base_type == GLSL_TYPE_UINT);
453
454 return glsl_simple_explicit_type(GLSL_TYPE_UINT16, t->vector_elements,
455 t->matrix_columns, t->explicit_stride,
456 t->interface_row_major, 0);
457 }
458
459 void
glsl_type_singleton_init_or_ref()460 glsl_type_singleton_init_or_ref()
461 {
462 /* Values of these types must fit in the two bits of
463 * glsl_type::sampled_type.
464 */
465 STATIC_ASSERT((((unsigned)GLSL_TYPE_UINT) & 3) == (unsigned)GLSL_TYPE_UINT);
466 STATIC_ASSERT((((unsigned)GLSL_TYPE_INT) & 3) == (unsigned)GLSL_TYPE_INT);
467 STATIC_ASSERT((((unsigned)GLSL_TYPE_FLOAT) & 3) == (unsigned)GLSL_TYPE_FLOAT);
468
469 ASSERT_BITFIELD_SIZE(glsl_type, base_type, GLSL_TYPE_ERROR);
470 ASSERT_BITFIELD_SIZE(glsl_type, sampled_type, GLSL_TYPE_ERROR);
471 ASSERT_BITFIELD_SIZE(glsl_type, sampler_dimensionality,
472 GLSL_SAMPLER_DIM_SUBPASS_MS);
473
474 simple_mtx_lock(&glsl_type_cache_mutex);
475 if (glsl_type_cache.users == 0) {
476 glsl_type_cache.mem_ctx = ralloc_context(NULL);
477 glsl_type_cache.lin_ctx = linear_context(glsl_type_cache.mem_ctx);
478 }
479 glsl_type_cache.users++;
480 simple_mtx_unlock(&glsl_type_cache_mutex);
481 }
482
483 void
glsl_type_singleton_decref()484 glsl_type_singleton_decref()
485 {
486 simple_mtx_lock(&glsl_type_cache_mutex);
487 assert(glsl_type_cache.users > 0);
488
489 /* Do not release glsl_types if they are still used. */
490 if (--glsl_type_cache.users) {
491 simple_mtx_unlock(&glsl_type_cache_mutex);
492 return;
493 }
494
495 ralloc_free(glsl_type_cache.mem_ctx);
496 memset(&glsl_type_cache, 0, sizeof(glsl_type_cache));
497
498 simple_mtx_unlock(&glsl_type_cache_mutex);
499 }
500
501 static const glsl_type *
make_array_type(linear_ctx * lin_ctx,const glsl_type * element_type,unsigned length,unsigned explicit_stride)502 make_array_type(linear_ctx *lin_ctx, const glsl_type *element_type, unsigned length,
503 unsigned explicit_stride)
504 {
505 assert(lin_ctx != NULL);
506
507 glsl_type *t = linear_zalloc(lin_ctx, glsl_type);
508 t->base_type = GLSL_TYPE_ARRAY;
509 t->sampled_type = GLSL_TYPE_VOID;
510 t->length = length;
511 t->explicit_stride = explicit_stride;
512 t->explicit_alignment = element_type->explicit_alignment;
513 t->fields.array = element_type;
514
515 /* Inherit the gl type of the base. The GL type is used for
516 * uniform/statevar handling in Mesa and the arrayness of the type
517 * is represented by the size rather than the type.
518 */
519 t->gl_type = element_type->gl_type;
520
521 const char *element_name = glsl_get_type_name(element_type);
522 char *n;
523 if (length == 0)
524 n = linear_asprintf(lin_ctx, "%s[]", element_name);
525 else
526 n = linear_asprintf(lin_ctx, "%s[%u]", element_name, length);
527
528 /* Flip the dimensions for a multidimensional array. The type of
529 * an array of 4 elements of type int[...] is written as int[4][...].
530 */
531 const char *pos = strchr(element_name, '[');
532 if (pos) {
533 char *base = n + (pos - element_name);
534 const unsigned element_part = strlen(pos);
535 const unsigned array_part = strlen(base) - element_part;
536
537 /* Move the outer array dimension to the front. */
538 memmove(base, base + element_part, array_part);
539
540 /* Rewrite the element array dimensions from the element name string. */
541 memcpy(base + array_part, pos, element_part);
542 }
543
544 t->name_id = (uintptr_t)n;
545
546 return t;
547 }
548
549 static const char *
glsl_cmat_use_to_string(enum glsl_cmat_use use)550 glsl_cmat_use_to_string(enum glsl_cmat_use use)
551 {
552 switch (use) {
553 case GLSL_CMAT_USE_NONE: return "NONE";
554 case GLSL_CMAT_USE_A: return "A";
555 case GLSL_CMAT_USE_B: return "B";
556 case GLSL_CMAT_USE_ACCUMULATOR: return "ACCUMULATOR";
557 default:
558 unreachable("invalid cooperative matrix use");
559 }
560 };
561
562 static const glsl_type *
vec(unsigned components,const glsl_type * const ts[])563 vec(unsigned components, const glsl_type *const ts[])
564 {
565 unsigned n = components;
566
567 if (components == 8)
568 n = 6;
569 else if (components == 16)
570 n = 7;
571
572 if (n == 0 || n > 7)
573 return &glsl_type_builtin_error;
574
575 return ts[n - 1];
576 }
577
578 #define VECN(components, sname, vname) \
579 const glsl_type * \
580 glsl_ ## vname ## _type (unsigned components) \
581 { \
582 static const glsl_type *const ts[] = { \
583 &glsl_type_builtin_ ## sname, \
584 &glsl_type_builtin_ ## vname ## 2, \
585 &glsl_type_builtin_ ## vname ## 3, \
586 &glsl_type_builtin_ ## vname ## 4, \
587 &glsl_type_builtin_ ## vname ## 5, \
588 &glsl_type_builtin_ ## vname ## 8, \
589 &glsl_type_builtin_ ## vname ## 16, \
590 }; \
591 return vec(components, ts); \
592 }
593
594 VECN(components, float, vec)
595 VECN(components, float16_t, f16vec)
596 VECN(components, double, dvec)
597 VECN(components, int, ivec)
598 VECN(components, uint, uvec)
599 VECN(components, bool, bvec)
600 VECN(components, int64_t, i64vec)
601 VECN(components, uint64_t, u64vec)
602 VECN(components, int16_t, i16vec)
603 VECN(components, uint16_t, u16vec)
604 VECN(components, int8_t, i8vec)
605 VECN(components, uint8_t, u8vec)
606
607 static const glsl_type *
608 get_explicit_matrix_instance(unsigned int base_type, unsigned int rows, unsigned int columns,
609 unsigned int explicit_stride, bool row_major, unsigned int explicit_alignment);
610
611 const glsl_type *
glsl_simple_explicit_type(unsigned base_type,unsigned rows,unsigned columns,unsigned explicit_stride,bool row_major,unsigned explicit_alignment)612 glsl_simple_explicit_type(unsigned base_type, unsigned rows, unsigned columns,
613 unsigned explicit_stride, bool row_major,
614 unsigned explicit_alignment)
615 {
616 if (base_type == GLSL_TYPE_VOID) {
617 assert(explicit_stride == 0 && explicit_alignment == 0 && !row_major);
618 return &glsl_type_builtin_void;
619 }
620
621 /* Matrix and vector types with explicit strides or alignment have to be
622 * looked up in a table so they're handled separately.
623 */
624 if (explicit_stride > 0 || explicit_alignment > 0) {
625 return get_explicit_matrix_instance(base_type, rows, columns,
626 explicit_stride, row_major,
627 explicit_alignment);
628 }
629
630 assert(!row_major);
631
632 /* Treat GLSL vectors as Nx1 matrices.
633 */
634 if (columns == 1) {
635 switch (base_type) {
636 case GLSL_TYPE_UINT:
637 return glsl_uvec_type(rows);
638 case GLSL_TYPE_INT:
639 return glsl_ivec_type(rows);
640 case GLSL_TYPE_FLOAT:
641 return glsl_vec_type(rows);
642 case GLSL_TYPE_FLOAT16:
643 return glsl_f16vec_type(rows);
644 case GLSL_TYPE_DOUBLE:
645 return glsl_dvec_type(rows);
646 case GLSL_TYPE_BOOL:
647 return glsl_bvec_type(rows);
648 case GLSL_TYPE_UINT64:
649 return glsl_u64vec_type(rows);
650 case GLSL_TYPE_INT64:
651 return glsl_i64vec_type(rows);
652 case GLSL_TYPE_UINT16:
653 return glsl_u16vec_type(rows);
654 case GLSL_TYPE_INT16:
655 return glsl_i16vec_type(rows);
656 case GLSL_TYPE_UINT8:
657 return glsl_u8vec_type(rows);
658 case GLSL_TYPE_INT8:
659 return glsl_i8vec_type(rows);
660 default:
661 return &glsl_type_builtin_error;
662 }
663 } else {
664 if ((base_type != GLSL_TYPE_FLOAT &&
665 base_type != GLSL_TYPE_DOUBLE &&
666 base_type != GLSL_TYPE_FLOAT16) || (rows == 1))
667 return &glsl_type_builtin_error;
668
669 /* GLSL matrix types are named mat{COLUMNS}x{ROWS}. Only the following
670 * combinations are valid:
671 *
672 * 1 2 3 4
673 * 1
674 * 2 x x x
675 * 3 x x x
676 * 4 x x x
677 */
678 #define IDX(c,r) (((c-1)*3) + (r-1))
679
680 switch (base_type) {
681 case GLSL_TYPE_DOUBLE: {
682 switch (IDX(columns, rows)) {
683 case IDX(2,2): return &glsl_type_builtin_dmat2;
684 case IDX(2,3): return &glsl_type_builtin_dmat2x3;
685 case IDX(2,4): return &glsl_type_builtin_dmat2x4;
686 case IDX(3,2): return &glsl_type_builtin_dmat3x2;
687 case IDX(3,3): return &glsl_type_builtin_dmat3;
688 case IDX(3,4): return &glsl_type_builtin_dmat3x4;
689 case IDX(4,2): return &glsl_type_builtin_dmat4x2;
690 case IDX(4,3): return &glsl_type_builtin_dmat4x3;
691 case IDX(4,4): return &glsl_type_builtin_dmat4;
692 default: return &glsl_type_builtin_error;
693 }
694 }
695 case GLSL_TYPE_FLOAT: {
696 switch (IDX(columns, rows)) {
697 case IDX(2,2): return &glsl_type_builtin_mat2;
698 case IDX(2,3): return &glsl_type_builtin_mat2x3;
699 case IDX(2,4): return &glsl_type_builtin_mat2x4;
700 case IDX(3,2): return &glsl_type_builtin_mat3x2;
701 case IDX(3,3): return &glsl_type_builtin_mat3;
702 case IDX(3,4): return &glsl_type_builtin_mat3x4;
703 case IDX(4,2): return &glsl_type_builtin_mat4x2;
704 case IDX(4,3): return &glsl_type_builtin_mat4x3;
705 case IDX(4,4): return &glsl_type_builtin_mat4;
706 default: return &glsl_type_builtin_error;
707 }
708 }
709 case GLSL_TYPE_FLOAT16: {
710 switch (IDX(columns, rows)) {
711 case IDX(2,2): return &glsl_type_builtin_f16mat2;
712 case IDX(2,3): return &glsl_type_builtin_f16mat2x3;
713 case IDX(2,4): return &glsl_type_builtin_f16mat2x4;
714 case IDX(3,2): return &glsl_type_builtin_f16mat3x2;
715 case IDX(3,3): return &glsl_type_builtin_f16mat3;
716 case IDX(3,4): return &glsl_type_builtin_f16mat3x4;
717 case IDX(4,2): return &glsl_type_builtin_f16mat4x2;
718 case IDX(4,3): return &glsl_type_builtin_f16mat4x3;
719 case IDX(4,4): return &glsl_type_builtin_f16mat4;
720 default: return &glsl_type_builtin_error;
721 }
722 }
723 default: return &glsl_type_builtin_error;
724 }
725 }
726
727 assert(!"Should not get here.");
728 return &glsl_type_builtin_error;
729 }
730
731 struct PACKED explicit_matrix_key {
732 /* Rows and Columns are implied in the bare type. */
733 uintptr_t bare_type;
734 uintptr_t explicit_stride;
735 uintptr_t explicit_alignment;
736 uintptr_t row_major;
737 };
738
739 DERIVE_HASH_TABLE(explicit_matrix_key);
740
741 static const glsl_type *
get_explicit_matrix_instance(unsigned int base_type,unsigned int rows,unsigned int columns,unsigned int explicit_stride,bool row_major,unsigned int explicit_alignment)742 get_explicit_matrix_instance(unsigned int base_type, unsigned int rows, unsigned int columns,
743 unsigned int explicit_stride, bool row_major, unsigned int explicit_alignment)
744 {
745 assert(explicit_stride > 0 || explicit_alignment > 0);
746 assert(base_type != GLSL_TYPE_VOID);
747
748 if (explicit_alignment > 0) {
749 assert(util_is_power_of_two_nonzero(explicit_alignment));
750 assert(explicit_stride % explicit_alignment == 0);
751 }
752
753 const glsl_type *bare_type = glsl_simple_type(base_type, rows, columns);
754
755 assert(columns > 1 || (rows > 1 && !row_major));
756
757 /* Ensure there's no internal padding, to avoid multiple hashes for same key. */
758 STATIC_ASSERT(sizeof(struct explicit_matrix_key) == (4 * sizeof(uintptr_t)));
759
760 struct explicit_matrix_key key = { 0 };
761 key.bare_type = (uintptr_t) bare_type;
762 key.explicit_stride = explicit_stride;
763 key.explicit_alignment = explicit_alignment;
764 key.row_major = row_major;
765
766 const uint32_t key_hash = explicit_matrix_key_hash(&key);
767
768 simple_mtx_lock(&glsl_type_cache_mutex);
769 assert(glsl_type_cache.users > 0);
770 void *mem_ctx = glsl_type_cache.mem_ctx;
771
772 if (glsl_type_cache.explicit_matrix_types == NULL) {
773 glsl_type_cache.explicit_matrix_types =
774 explicit_matrix_key_table_create(mem_ctx);
775 }
776 struct hash_table *explicit_matrix_types = glsl_type_cache.explicit_matrix_types;
777
778 const struct hash_entry *entry =
779 _mesa_hash_table_search_pre_hashed(explicit_matrix_types, key_hash, &key);
780 if (entry == NULL) {
781
782 char name[128];
783 snprintf(name, sizeof(name), "%sx%ua%uB%s", glsl_get_type_name(bare_type),
784 explicit_stride, explicit_alignment, row_major ? "RM" : "");
785
786 linear_ctx *lin_ctx = glsl_type_cache.lin_ctx;
787 const glsl_type *t =
788 make_vector_matrix_type(lin_ctx, bare_type->gl_type,
789 (enum glsl_base_type)base_type,
790 rows, columns, name,
791 explicit_stride, row_major,
792 explicit_alignment);
793
794 struct explicit_matrix_key *stored_key = linear_zalloc(lin_ctx, struct explicit_matrix_key);
795 memcpy(stored_key, &key, sizeof(key));
796
797 entry = _mesa_hash_table_insert_pre_hashed(explicit_matrix_types,
798 key_hash, stored_key, (void *)t);
799 }
800
801 const glsl_type *t = (const glsl_type *) entry->data;
802 simple_mtx_unlock(&glsl_type_cache_mutex);
803
804 assert(t->base_type == base_type);
805 assert(t->vector_elements == rows);
806 assert(t->matrix_columns == columns);
807 assert(t->explicit_stride == explicit_stride);
808 assert(t->explicit_alignment == explicit_alignment);
809
810 return t;
811 }
812
813 const glsl_type *
glsl_sampler_type(enum glsl_sampler_dim dim,bool shadow,bool array,enum glsl_base_type type)814 glsl_sampler_type(enum glsl_sampler_dim dim, bool shadow,
815 bool array, enum glsl_base_type type)
816 {
817 switch (type) {
818 case GLSL_TYPE_FLOAT:
819 switch (dim) {
820 case GLSL_SAMPLER_DIM_1D:
821 if (shadow)
822 return (array ? &glsl_type_builtin_sampler1DArrayShadow : &glsl_type_builtin_sampler1DShadow);
823 else
824 return (array ? &glsl_type_builtin_sampler1DArray : &glsl_type_builtin_sampler1D);
825 case GLSL_SAMPLER_DIM_2D:
826 if (shadow)
827 return (array ? &glsl_type_builtin_sampler2DArrayShadow : &glsl_type_builtin_sampler2DShadow);
828 else
829 return (array ? &glsl_type_builtin_sampler2DArray : &glsl_type_builtin_sampler2D);
830 case GLSL_SAMPLER_DIM_3D:
831 if (shadow || array)
832 return &glsl_type_builtin_error;
833 else
834 return &glsl_type_builtin_sampler3D;
835 case GLSL_SAMPLER_DIM_CUBE:
836 if (shadow)
837 return (array ? &glsl_type_builtin_samplerCubeArrayShadow : &glsl_type_builtin_samplerCubeShadow);
838 else
839 return (array ? &glsl_type_builtin_samplerCubeArray : &glsl_type_builtin_samplerCube);
840 case GLSL_SAMPLER_DIM_RECT:
841 if (array)
842 return &glsl_type_builtin_error;
843 if (shadow)
844 return &glsl_type_builtin_sampler2DRectShadow;
845 else
846 return &glsl_type_builtin_sampler2DRect;
847 case GLSL_SAMPLER_DIM_BUF:
848 if (shadow || array)
849 return &glsl_type_builtin_error;
850 else
851 return &glsl_type_builtin_samplerBuffer;
852 case GLSL_SAMPLER_DIM_MS:
853 if (shadow)
854 return &glsl_type_builtin_error;
855 return (array ? &glsl_type_builtin_sampler2DMSArray : &glsl_type_builtin_sampler2DMS);
856 case GLSL_SAMPLER_DIM_EXTERNAL:
857 if (shadow || array)
858 return &glsl_type_builtin_error;
859 else
860 return &glsl_type_builtin_samplerExternalOES;
861 case GLSL_SAMPLER_DIM_SUBPASS:
862 case GLSL_SAMPLER_DIM_SUBPASS_MS:
863 return &glsl_type_builtin_error;
864 }
865 break;
866 case GLSL_TYPE_INT:
867 if (shadow)
868 return &glsl_type_builtin_error;
869 switch (dim) {
870 case GLSL_SAMPLER_DIM_1D:
871 return (array ? &glsl_type_builtin_isampler1DArray : &glsl_type_builtin_isampler1D);
872 case GLSL_SAMPLER_DIM_2D:
873 return (array ? &glsl_type_builtin_isampler2DArray : &glsl_type_builtin_isampler2D);
874 case GLSL_SAMPLER_DIM_3D:
875 if (array)
876 return &glsl_type_builtin_error;
877 return &glsl_type_builtin_isampler3D;
878 case GLSL_SAMPLER_DIM_CUBE:
879 return (array ? &glsl_type_builtin_isamplerCubeArray : &glsl_type_builtin_isamplerCube);
880 case GLSL_SAMPLER_DIM_RECT:
881 if (array)
882 return &glsl_type_builtin_error;
883 return &glsl_type_builtin_isampler2DRect;
884 case GLSL_SAMPLER_DIM_BUF:
885 if (array)
886 return &glsl_type_builtin_error;
887 return &glsl_type_builtin_isamplerBuffer;
888 case GLSL_SAMPLER_DIM_MS:
889 return (array ? &glsl_type_builtin_isampler2DMSArray : &glsl_type_builtin_isampler2DMS);
890 case GLSL_SAMPLER_DIM_EXTERNAL:
891 return &glsl_type_builtin_error;
892 case GLSL_SAMPLER_DIM_SUBPASS:
893 case GLSL_SAMPLER_DIM_SUBPASS_MS:
894 return &glsl_type_builtin_error;
895 }
896 break;
897 case GLSL_TYPE_UINT:
898 if (shadow)
899 return &glsl_type_builtin_error;
900 switch (dim) {
901 case GLSL_SAMPLER_DIM_1D:
902 return (array ? &glsl_type_builtin_usampler1DArray : &glsl_type_builtin_usampler1D);
903 case GLSL_SAMPLER_DIM_2D:
904 return (array ? &glsl_type_builtin_usampler2DArray : &glsl_type_builtin_usampler2D);
905 case GLSL_SAMPLER_DIM_3D:
906 if (array)
907 return &glsl_type_builtin_error;
908 return &glsl_type_builtin_usampler3D;
909 case GLSL_SAMPLER_DIM_CUBE:
910 return (array ? &glsl_type_builtin_usamplerCubeArray : &glsl_type_builtin_usamplerCube);
911 case GLSL_SAMPLER_DIM_RECT:
912 if (array)
913 return &glsl_type_builtin_error;
914 return &glsl_type_builtin_usampler2DRect;
915 case GLSL_SAMPLER_DIM_BUF:
916 if (array)
917 return &glsl_type_builtin_error;
918 return &glsl_type_builtin_usamplerBuffer;
919 case GLSL_SAMPLER_DIM_MS:
920 return (array ? &glsl_type_builtin_usampler2DMSArray : &glsl_type_builtin_usampler2DMS);
921 case GLSL_SAMPLER_DIM_EXTERNAL:
922 return &glsl_type_builtin_error;
923 case GLSL_SAMPLER_DIM_SUBPASS:
924 case GLSL_SAMPLER_DIM_SUBPASS_MS:
925 return &glsl_type_builtin_error;
926 }
927 break;
928 case GLSL_TYPE_VOID:
929 return shadow ? &glsl_type_builtin_samplerShadow : &glsl_type_builtin_sampler;
930 default:
931 return &glsl_type_builtin_error;
932 }
933
934 unreachable("switch statement above should be complete");
935 }
936
937 const glsl_type *
glsl_bare_sampler_type()938 glsl_bare_sampler_type()
939 {
940 return &glsl_type_builtin_sampler;
941 }
942
943 const glsl_type *
glsl_bare_shadow_sampler_type()944 glsl_bare_shadow_sampler_type()
945 {
946 return &glsl_type_builtin_samplerShadow;
947 }
948
949 const glsl_type *
glsl_texture_type(enum glsl_sampler_dim dim,bool array,enum glsl_base_type type)950 glsl_texture_type(enum glsl_sampler_dim dim, bool array, enum glsl_base_type type)
951 {
952 switch (type) {
953 case GLSL_TYPE_FLOAT:
954 switch (dim) {
955 case GLSL_SAMPLER_DIM_1D:
956 return (array ? &glsl_type_builtin_texture1DArray : &glsl_type_builtin_texture1D);
957 case GLSL_SAMPLER_DIM_2D:
958 return (array ? &glsl_type_builtin_texture2DArray : &glsl_type_builtin_texture2D);
959 case GLSL_SAMPLER_DIM_3D:
960 return &glsl_type_builtin_texture3D;
961 case GLSL_SAMPLER_DIM_CUBE:
962 return (array ? &glsl_type_builtin_textureCubeArray : &glsl_type_builtin_textureCube);
963 case GLSL_SAMPLER_DIM_RECT:
964 if (array)
965 return &glsl_type_builtin_error;
966 else
967 return &glsl_type_builtin_texture2DRect;
968 case GLSL_SAMPLER_DIM_BUF:
969 if (array)
970 return &glsl_type_builtin_error;
971 else
972 return &glsl_type_builtin_textureBuffer;
973 case GLSL_SAMPLER_DIM_MS:
974 return (array ? &glsl_type_builtin_texture2DMSArray : &glsl_type_builtin_texture2DMS);
975 case GLSL_SAMPLER_DIM_SUBPASS:
976 return &glsl_type_builtin_textureSubpassInput;
977 case GLSL_SAMPLER_DIM_SUBPASS_MS:
978 return &glsl_type_builtin_textureSubpassInputMS;
979 case GLSL_SAMPLER_DIM_EXTERNAL:
980 if (array)
981 return &glsl_type_builtin_error;
982 else
983 return &glsl_type_builtin_textureExternalOES;
984 }
985 break;
986 case GLSL_TYPE_INT:
987 switch (dim) {
988 case GLSL_SAMPLER_DIM_1D:
989 return (array ? &glsl_type_builtin_itexture1DArray : &glsl_type_builtin_itexture1D);
990 case GLSL_SAMPLER_DIM_2D:
991 return (array ? &glsl_type_builtin_itexture2DArray : &glsl_type_builtin_itexture2D);
992 case GLSL_SAMPLER_DIM_3D:
993 if (array)
994 return &glsl_type_builtin_error;
995 return &glsl_type_builtin_itexture3D;
996 case GLSL_SAMPLER_DIM_CUBE:
997 return (array ? &glsl_type_builtin_itextureCubeArray : &glsl_type_builtin_itextureCube);
998 case GLSL_SAMPLER_DIM_RECT:
999 if (array)
1000 return &glsl_type_builtin_error;
1001 return &glsl_type_builtin_itexture2DRect;
1002 case GLSL_SAMPLER_DIM_BUF:
1003 if (array)
1004 return &glsl_type_builtin_error;
1005 return &glsl_type_builtin_itextureBuffer;
1006 case GLSL_SAMPLER_DIM_MS:
1007 return (array ? &glsl_type_builtin_itexture2DMSArray : &glsl_type_builtin_itexture2DMS);
1008 case GLSL_SAMPLER_DIM_SUBPASS:
1009 return &glsl_type_builtin_itextureSubpassInput;
1010 case GLSL_SAMPLER_DIM_SUBPASS_MS:
1011 return &glsl_type_builtin_itextureSubpassInputMS;
1012 case GLSL_SAMPLER_DIM_EXTERNAL:
1013 return &glsl_type_builtin_error;
1014 }
1015 break;
1016 case GLSL_TYPE_UINT:
1017 switch (dim) {
1018 case GLSL_SAMPLER_DIM_1D:
1019 return (array ? &glsl_type_builtin_utexture1DArray : &glsl_type_builtin_utexture1D);
1020 case GLSL_SAMPLER_DIM_2D:
1021 return (array ? &glsl_type_builtin_utexture2DArray : &glsl_type_builtin_utexture2D);
1022 case GLSL_SAMPLER_DIM_3D:
1023 if (array)
1024 return &glsl_type_builtin_error;
1025 return &glsl_type_builtin_utexture3D;
1026 case GLSL_SAMPLER_DIM_CUBE:
1027 return (array ? &glsl_type_builtin_utextureCubeArray : &glsl_type_builtin_utextureCube);
1028 case GLSL_SAMPLER_DIM_RECT:
1029 if (array)
1030 return &glsl_type_builtin_error;
1031 return &glsl_type_builtin_utexture2DRect;
1032 case GLSL_SAMPLER_DIM_BUF:
1033 if (array)
1034 return &glsl_type_builtin_error;
1035 return &glsl_type_builtin_utextureBuffer;
1036 case GLSL_SAMPLER_DIM_MS:
1037 return (array ? &glsl_type_builtin_utexture2DMSArray : &glsl_type_builtin_utexture2DMS);
1038 case GLSL_SAMPLER_DIM_SUBPASS:
1039 return &glsl_type_builtin_utextureSubpassInput;
1040 case GLSL_SAMPLER_DIM_SUBPASS_MS:
1041 return &glsl_type_builtin_utextureSubpassInputMS;
1042 case GLSL_SAMPLER_DIM_EXTERNAL:
1043 return &glsl_type_builtin_error;
1044 }
1045 break;
1046 case GLSL_TYPE_VOID:
1047 switch (dim) {
1048 case GLSL_SAMPLER_DIM_1D:
1049 return (array ? &glsl_type_builtin_vtexture1DArray : &glsl_type_builtin_vtexture1D);
1050 case GLSL_SAMPLER_DIM_2D:
1051 return (array ? &glsl_type_builtin_vtexture2DArray : &glsl_type_builtin_vtexture2D);
1052 case GLSL_SAMPLER_DIM_3D:
1053 return (array ? &glsl_type_builtin_error : &glsl_type_builtin_vtexture3D);
1054 case GLSL_SAMPLER_DIM_BUF:
1055 return (array ? &glsl_type_builtin_error : &glsl_type_builtin_vtextureBuffer);
1056 case GLSL_SAMPLER_DIM_MS:
1057 return (array ? &glsl_type_builtin_vtexture2DMSArray : &glsl_type_builtin_vtexture2DMS);
1058 default:
1059 return &glsl_type_builtin_error;
1060 }
1061 default:
1062 return &glsl_type_builtin_error;
1063 }
1064
1065 unreachable("switch statement above should be complete");
1066 }
1067
1068 const glsl_type *
glsl_image_type(enum glsl_sampler_dim dim,bool array,enum glsl_base_type type)1069 glsl_image_type(enum glsl_sampler_dim dim, bool array, enum glsl_base_type type)
1070 {
1071 switch (type) {
1072 case GLSL_TYPE_FLOAT:
1073 switch (dim) {
1074 case GLSL_SAMPLER_DIM_1D:
1075 return (array ? &glsl_type_builtin_image1DArray : &glsl_type_builtin_image1D);
1076 case GLSL_SAMPLER_DIM_2D:
1077 return (array ? &glsl_type_builtin_image2DArray : &glsl_type_builtin_image2D);
1078 case GLSL_SAMPLER_DIM_3D:
1079 return &glsl_type_builtin_image3D;
1080 case GLSL_SAMPLER_DIM_CUBE:
1081 return (array ? &glsl_type_builtin_imageCubeArray : &glsl_type_builtin_imageCube);
1082 case GLSL_SAMPLER_DIM_RECT:
1083 if (array)
1084 return &glsl_type_builtin_error;
1085 else
1086 return &glsl_type_builtin_image2DRect;
1087 case GLSL_SAMPLER_DIM_BUF:
1088 if (array)
1089 return &glsl_type_builtin_error;
1090 else
1091 return &glsl_type_builtin_imageBuffer;
1092 case GLSL_SAMPLER_DIM_MS:
1093 return (array ? &glsl_type_builtin_image2DMSArray : &glsl_type_builtin_image2DMS);
1094 case GLSL_SAMPLER_DIM_SUBPASS:
1095 return &glsl_type_builtin_subpassInput;
1096 case GLSL_SAMPLER_DIM_SUBPASS_MS:
1097 return &glsl_type_builtin_subpassInputMS;
1098 case GLSL_SAMPLER_DIM_EXTERNAL:
1099 return &glsl_type_builtin_error;
1100 }
1101 break;
1102 case GLSL_TYPE_INT:
1103 switch (dim) {
1104 case GLSL_SAMPLER_DIM_1D:
1105 return (array ? &glsl_type_builtin_iimage1DArray : &glsl_type_builtin_iimage1D);
1106 case GLSL_SAMPLER_DIM_2D:
1107 return (array ? &glsl_type_builtin_iimage2DArray : &glsl_type_builtin_iimage2D);
1108 case GLSL_SAMPLER_DIM_3D:
1109 if (array)
1110 return &glsl_type_builtin_error;
1111 return &glsl_type_builtin_iimage3D;
1112 case GLSL_SAMPLER_DIM_CUBE:
1113 return (array ? &glsl_type_builtin_iimageCubeArray : &glsl_type_builtin_iimageCube);
1114 case GLSL_SAMPLER_DIM_RECT:
1115 if (array)
1116 return &glsl_type_builtin_error;
1117 return &glsl_type_builtin_iimage2DRect;
1118 case GLSL_SAMPLER_DIM_BUF:
1119 if (array)
1120 return &glsl_type_builtin_error;
1121 return &glsl_type_builtin_iimageBuffer;
1122 case GLSL_SAMPLER_DIM_MS:
1123 return (array ? &glsl_type_builtin_iimage2DMSArray : &glsl_type_builtin_iimage2DMS);
1124 case GLSL_SAMPLER_DIM_SUBPASS:
1125 return &glsl_type_builtin_isubpassInput;
1126 case GLSL_SAMPLER_DIM_SUBPASS_MS:
1127 return &glsl_type_builtin_isubpassInputMS;
1128 case GLSL_SAMPLER_DIM_EXTERNAL:
1129 return &glsl_type_builtin_error;
1130 }
1131 break;
1132 case GLSL_TYPE_UINT:
1133 switch (dim) {
1134 case GLSL_SAMPLER_DIM_1D:
1135 return (array ? &glsl_type_builtin_uimage1DArray : &glsl_type_builtin_uimage1D);
1136 case GLSL_SAMPLER_DIM_2D:
1137 return (array ? &glsl_type_builtin_uimage2DArray : &glsl_type_builtin_uimage2D);
1138 case GLSL_SAMPLER_DIM_3D:
1139 if (array)
1140 return &glsl_type_builtin_error;
1141 return &glsl_type_builtin_uimage3D;
1142 case GLSL_SAMPLER_DIM_CUBE:
1143 return (array ? &glsl_type_builtin_uimageCubeArray : &glsl_type_builtin_uimageCube);
1144 case GLSL_SAMPLER_DIM_RECT:
1145 if (array)
1146 return &glsl_type_builtin_error;
1147 return &glsl_type_builtin_uimage2DRect;
1148 case GLSL_SAMPLER_DIM_BUF:
1149 if (array)
1150 return &glsl_type_builtin_error;
1151 return &glsl_type_builtin_uimageBuffer;
1152 case GLSL_SAMPLER_DIM_MS:
1153 return (array ? &glsl_type_builtin_uimage2DMSArray : &glsl_type_builtin_uimage2DMS);
1154 case GLSL_SAMPLER_DIM_SUBPASS:
1155 return &glsl_type_builtin_usubpassInput;
1156 case GLSL_SAMPLER_DIM_SUBPASS_MS:
1157 return &glsl_type_builtin_usubpassInputMS;
1158 case GLSL_SAMPLER_DIM_EXTERNAL:
1159 return &glsl_type_builtin_error;
1160 }
1161 break;
1162 case GLSL_TYPE_INT64:
1163 switch (dim) {
1164 case GLSL_SAMPLER_DIM_1D:
1165 return (array ? &glsl_type_builtin_i64image1DArray : &glsl_type_builtin_i64image1D);
1166 case GLSL_SAMPLER_DIM_2D:
1167 return (array ? &glsl_type_builtin_i64image2DArray : &glsl_type_builtin_i64image2D);
1168 case GLSL_SAMPLER_DIM_3D:
1169 if (array)
1170 return &glsl_type_builtin_error;
1171 return &glsl_type_builtin_i64image3D;
1172 case GLSL_SAMPLER_DIM_CUBE:
1173 return (array ? &glsl_type_builtin_i64imageCubeArray : &glsl_type_builtin_i64imageCube);
1174 case GLSL_SAMPLER_DIM_RECT:
1175 if (array)
1176 return &glsl_type_builtin_error;
1177 return &glsl_type_builtin_i64image2DRect;
1178 case GLSL_SAMPLER_DIM_BUF:
1179 if (array)
1180 return &glsl_type_builtin_error;
1181 return &glsl_type_builtin_i64imageBuffer;
1182 case GLSL_SAMPLER_DIM_MS:
1183 return (array ? &glsl_type_builtin_i64image2DMSArray : &glsl_type_builtin_i64image2DMS);
1184 case GLSL_SAMPLER_DIM_SUBPASS:
1185 case GLSL_SAMPLER_DIM_SUBPASS_MS:
1186 case GLSL_SAMPLER_DIM_EXTERNAL:
1187 return &glsl_type_builtin_error;
1188 }
1189 break;
1190 case GLSL_TYPE_UINT64:
1191 switch (dim) {
1192 case GLSL_SAMPLER_DIM_1D:
1193 return (array ? &glsl_type_builtin_u64image1DArray : &glsl_type_builtin_u64image1D);
1194 case GLSL_SAMPLER_DIM_2D:
1195 return (array ? &glsl_type_builtin_u64image2DArray : &glsl_type_builtin_u64image2D);
1196 case GLSL_SAMPLER_DIM_3D:
1197 if (array)
1198 return &glsl_type_builtin_error;
1199 return &glsl_type_builtin_u64image3D;
1200 case GLSL_SAMPLER_DIM_CUBE:
1201 return (array ? &glsl_type_builtin_u64imageCubeArray : &glsl_type_builtin_u64imageCube);
1202 case GLSL_SAMPLER_DIM_RECT:
1203 if (array)
1204 return &glsl_type_builtin_error;
1205 return &glsl_type_builtin_u64image2DRect;
1206 case GLSL_SAMPLER_DIM_BUF:
1207 if (array)
1208 return &glsl_type_builtin_error;
1209 return &glsl_type_builtin_u64imageBuffer;
1210 case GLSL_SAMPLER_DIM_MS:
1211 return (array ? &glsl_type_builtin_u64image2DMSArray : &glsl_type_builtin_u64image2DMS);
1212 case GLSL_SAMPLER_DIM_SUBPASS:
1213 case GLSL_SAMPLER_DIM_SUBPASS_MS:
1214 case GLSL_SAMPLER_DIM_EXTERNAL:
1215 return &glsl_type_builtin_error;
1216 }
1217 break;
1218 case GLSL_TYPE_VOID:
1219 switch (dim) {
1220 case GLSL_SAMPLER_DIM_1D:
1221 return (array ? &glsl_type_builtin_vimage1DArray : &glsl_type_builtin_vimage1D);
1222 case GLSL_SAMPLER_DIM_2D:
1223 return (array ? &glsl_type_builtin_vimage2DArray : &glsl_type_builtin_vimage2D);
1224 case GLSL_SAMPLER_DIM_3D:
1225 return (array ? &glsl_type_builtin_error : &glsl_type_builtin_vimage3D);
1226 case GLSL_SAMPLER_DIM_BUF:
1227 return (array ? &glsl_type_builtin_error : &glsl_type_builtin_vbuffer);
1228 case GLSL_SAMPLER_DIM_MS:
1229 return (array ? &glsl_type_builtin_vimage2DMSArray : &glsl_type_builtin_vimage2DMS);
1230 default:
1231 return &glsl_type_builtin_error;
1232 }
1233 default:
1234 return &glsl_type_builtin_error;
1235 }
1236
1237 unreachable("switch statement above should be complete");
1238 }
1239
1240 struct PACKED array_key {
1241 uintptr_t element;
1242 uintptr_t array_size;
1243 uintptr_t explicit_stride;
1244 };
1245
1246 DERIVE_HASH_TABLE(array_key);
1247
1248 const glsl_type *
glsl_array_type(const glsl_type * element,unsigned array_size,unsigned explicit_stride)1249 glsl_array_type(const glsl_type *element,
1250 unsigned array_size,
1251 unsigned explicit_stride)
1252 {
1253 /* Ensure there's no internal padding, to avoid multiple hashes for same key. */
1254 STATIC_ASSERT(sizeof(struct array_key) == (3 * sizeof(uintptr_t)));
1255
1256 struct array_key key = { 0 };
1257 key.element = (uintptr_t)element;
1258 key.array_size = array_size;
1259 key.explicit_stride = explicit_stride;
1260
1261 const uint32_t key_hash = array_key_hash(&key);
1262
1263 simple_mtx_lock(&glsl_type_cache_mutex);
1264 assert(glsl_type_cache.users > 0);
1265 void *mem_ctx = glsl_type_cache.mem_ctx;
1266
1267 if (glsl_type_cache.array_types == NULL) {
1268 glsl_type_cache.array_types = array_key_table_create(mem_ctx);
1269 }
1270 struct hash_table *array_types = glsl_type_cache.array_types;
1271
1272 const struct hash_entry *entry = _mesa_hash_table_search_pre_hashed(array_types, key_hash, &key);
1273 if (entry == NULL) {
1274 linear_ctx *lin_ctx = glsl_type_cache.lin_ctx;
1275 const glsl_type *t = make_array_type(lin_ctx, element, array_size, explicit_stride);
1276 struct array_key *stored_key = linear_zalloc(lin_ctx, struct array_key);
1277 memcpy(stored_key, &key, sizeof(key));
1278
1279 entry = _mesa_hash_table_insert_pre_hashed(array_types, key_hash,
1280 stored_key,
1281 (void *) t);
1282 }
1283
1284 const glsl_type *t = (const glsl_type *) entry->data;
1285 simple_mtx_unlock(&glsl_type_cache_mutex);
1286
1287 assert(t->base_type == GLSL_TYPE_ARRAY);
1288 assert(t->length == array_size);
1289 assert(t->fields.array == element);
1290
1291 return t;
1292 }
1293
1294 static const glsl_type *
make_cmat_type(linear_ctx * lin_ctx,const struct glsl_cmat_description desc)1295 make_cmat_type(linear_ctx *lin_ctx, const struct glsl_cmat_description desc)
1296 {
1297 assert(lin_ctx != NULL);
1298
1299 glsl_type *t = linear_zalloc(lin_ctx, glsl_type);
1300 t->base_type = GLSL_TYPE_COOPERATIVE_MATRIX;
1301 t->sampled_type = GLSL_TYPE_VOID;
1302 t->vector_elements = 1;
1303 t->cmat_desc = desc;
1304
1305 const glsl_type *element_type = glsl_simple_type(desc.element_type, 1, 1);
1306 t->name_id = (uintptr_t ) linear_asprintf(lin_ctx, "coopmat<%s, %s, %u, %u, %s>",
1307 glsl_get_type_name(element_type),
1308 mesa_scope_name((mesa_scope)desc.scope),
1309 desc.rows, desc.cols,
1310 glsl_cmat_use_to_string((enum glsl_cmat_use)desc.use));
1311
1312 return t;
1313 }
1314
1315 const glsl_type *
glsl_cmat_type(const struct glsl_cmat_description * desc)1316 glsl_cmat_type(const struct glsl_cmat_description *desc)
1317 {
1318 STATIC_ASSERT(sizeof(struct glsl_cmat_description) == 4);
1319
1320 const uint32_t key = desc->element_type | desc->scope << 5 |
1321 desc->rows << 8 | desc->cols << 16 |
1322 desc->use << 24;
1323 const uint32_t key_hash = _mesa_hash_uint(&key);
1324
1325 simple_mtx_lock(&glsl_type_cache_mutex);
1326 assert(glsl_type_cache.users > 0);
1327 void *mem_ctx = glsl_type_cache.mem_ctx;
1328
1329 if (glsl_type_cache.cmat_types == NULL) {
1330 glsl_type_cache.cmat_types =
1331 _mesa_hash_table_create_u32_keys(mem_ctx);
1332 }
1333 struct hash_table *cmat_types = glsl_type_cache.cmat_types;
1334
1335 const struct hash_entry *entry = _mesa_hash_table_search_pre_hashed(
1336 cmat_types, key_hash, (void *) (uintptr_t) key);
1337 if (entry == NULL) {
1338 const glsl_type *t = make_cmat_type(glsl_type_cache.lin_ctx, *desc);
1339 entry = _mesa_hash_table_insert_pre_hashed(cmat_types, key_hash,
1340 (void *) (uintptr_t) key, (void *) t);
1341 }
1342
1343 const glsl_type *t = (const glsl_type *)entry->data;
1344 simple_mtx_unlock(&glsl_type_cache_mutex);
1345
1346 assert(t->base_type == GLSL_TYPE_COOPERATIVE_MATRIX);
1347 assert(t->cmat_desc.element_type == desc->element_type);
1348 assert(t->cmat_desc.scope == desc->scope);
1349 assert(t->cmat_desc.rows == desc->rows);
1350 assert(t->cmat_desc.cols == desc->cols);
1351 assert(t->cmat_desc.use == desc->use);
1352
1353 return t;
1354 }
1355
1356 bool
glsl_type_compare_no_precision(const glsl_type * a,const glsl_type * b)1357 glsl_type_compare_no_precision(const glsl_type *a, const glsl_type *b)
1358 {
1359 if (a == b)
1360 return true;
1361
1362 if (glsl_type_is_array(a)) {
1363 if (!glsl_type_is_array(b) || a->length != b->length)
1364 return false;
1365
1366 const glsl_type *b_no_array = b->fields.array;
1367
1368 return glsl_type_compare_no_precision(a->fields.array, b_no_array);
1369 }
1370
1371 if (glsl_type_is_struct(a)) {
1372 if (!glsl_type_is_struct(b))
1373 return false;
1374 } else if (glsl_type_is_interface(a)) {
1375 if (!glsl_type_is_interface(b))
1376 return false;
1377 } else {
1378 return false;
1379 }
1380
1381 return glsl_record_compare(a, b,
1382 true, /* match_name */
1383 true, /* match_locations */
1384 false /* match_precision */);
1385 }
1386
1387 bool
glsl_record_compare(const glsl_type * a,const glsl_type * b,bool match_name,bool match_locations,bool match_precision)1388 glsl_record_compare(const glsl_type *a, const glsl_type *b, bool match_name,
1389 bool match_locations, bool match_precision)
1390 {
1391 if (a->length != b->length)
1392 return false;
1393
1394 if (a->interface_packing != b->interface_packing)
1395 return false;
1396
1397 if (a->interface_row_major != b->interface_row_major)
1398 return false;
1399
1400 if (a->explicit_alignment != b->explicit_alignment)
1401 return false;
1402
1403 if (a->packed != b->packed)
1404 return false;
1405
1406 /* From the GLSL 4.20 specification (Sec 4.2):
1407 *
1408 * "Structures must have the same name, sequence of type names, and
1409 * type definitions, and field names to be considered the same type."
1410 *
1411 * GLSL ES behaves the same (Ver 1.00 Sec 4.2.4, Ver 3.00 Sec 4.2.5).
1412 *
1413 * Section 7.4.1 (Shader Interface Matching) of the OpenGL 4.30 spec says:
1414 *
1415 * "Variables or block members declared as structures are considered
1416 * to match in type if and only if structure members match in name,
1417 * type, qualification, and declaration order."
1418 */
1419 if (match_name)
1420 if (strcmp(glsl_get_type_name(a), glsl_get_type_name(b)) != 0)
1421 return false;
1422
1423 for (unsigned i = 0; i < a->length; i++) {
1424 if (match_precision) {
1425 if (a->fields.structure[i].type != b->fields.structure[i].type)
1426 return false;
1427 } else {
1428 const glsl_type *ta = a->fields.structure[i].type;
1429 const glsl_type *tb = b->fields.structure[i].type;
1430 if (!glsl_type_compare_no_precision(ta, tb))
1431 return false;
1432 }
1433 if (strcmp(a->fields.structure[i].name,
1434 b->fields.structure[i].name) != 0)
1435 return false;
1436 if (a->fields.structure[i].matrix_layout
1437 != b->fields.structure[i].matrix_layout)
1438 return false;
1439 if (match_locations && a->fields.structure[i].location
1440 != b->fields.structure[i].location)
1441 return false;
1442 if (a->fields.structure[i].component
1443 != b->fields.structure[i].component)
1444 return false;
1445 if (a->fields.structure[i].offset
1446 != b->fields.structure[i].offset)
1447 return false;
1448 if (a->fields.structure[i].interpolation
1449 != b->fields.structure[i].interpolation)
1450 return false;
1451 if (a->fields.structure[i].centroid
1452 != b->fields.structure[i].centroid)
1453 return false;
1454 if (a->fields.structure[i].sample
1455 != b->fields.structure[i].sample)
1456 return false;
1457 if (a->fields.structure[i].patch
1458 != b->fields.structure[i].patch)
1459 return false;
1460 if (a->fields.structure[i].memory_read_only
1461 != b->fields.structure[i].memory_read_only)
1462 return false;
1463 if (a->fields.structure[i].memory_write_only
1464 != b->fields.structure[i].memory_write_only)
1465 return false;
1466 if (a->fields.structure[i].memory_coherent
1467 != b->fields.structure[i].memory_coherent)
1468 return false;
1469 if (a->fields.structure[i].memory_volatile
1470 != b->fields.structure[i].memory_volatile)
1471 return false;
1472 if (a->fields.structure[i].memory_restrict
1473 != b->fields.structure[i].memory_restrict)
1474 return false;
1475 if (a->fields.structure[i].image_format
1476 != b->fields.structure[i].image_format)
1477 return false;
1478 if (match_precision &&
1479 a->fields.structure[i].precision
1480 != b->fields.structure[i].precision)
1481 return false;
1482 if (a->fields.structure[i].explicit_xfb_buffer
1483 != b->fields.structure[i].explicit_xfb_buffer)
1484 return false;
1485 if (a->fields.structure[i].xfb_buffer
1486 != b->fields.structure[i].xfb_buffer)
1487 return false;
1488 if (a->fields.structure[i].xfb_stride
1489 != b->fields.structure[i].xfb_stride)
1490 return false;
1491 }
1492
1493 return true;
1494 }
1495
1496
1497 static bool
record_key_compare(const void * a,const void * b)1498 record_key_compare(const void *a, const void *b)
1499 {
1500 const glsl_type *const key1 = (glsl_type *) a;
1501 const glsl_type *const key2 = (glsl_type *) b;
1502
1503 return strcmp(glsl_get_type_name(key1), glsl_get_type_name(key2)) == 0 &&
1504 glsl_record_compare(key1, key2, true, true, true);
1505 }
1506
1507
1508 /**
1509 * Generate an integer hash value for a glsl_type structure type.
1510 */
1511 static unsigned
record_key_hash(const void * a)1512 record_key_hash(const void *a)
1513 {
1514 const glsl_type *const key = (glsl_type *) a;
1515 uintptr_t hash = key->length;
1516 unsigned retval;
1517
1518 for (unsigned i = 0; i < key->length; i++) {
1519 /* casting pointer to uintptr_t */
1520 hash = (hash * 13 ) + (uintptr_t) key->fields.structure[i].type;
1521 }
1522
1523 if (sizeof(hash) == 8)
1524 retval = (hash & 0xffffffff) ^ ((uint64_t) hash >> 32);
1525 else
1526 retval = hash;
1527
1528 return retval;
1529 }
1530
1531 const glsl_type *
glsl_struct_type_with_explicit_alignment(const glsl_struct_field * fields,unsigned num_fields,const char * name,bool packed,unsigned explicit_alignment)1532 glsl_struct_type_with_explicit_alignment(const glsl_struct_field *fields,
1533 unsigned num_fields,
1534 const char *name,
1535 bool packed, unsigned explicit_alignment)
1536 {
1537 glsl_type key = {0};
1538 fill_struct_type(&key, fields, num_fields, name, packed, explicit_alignment);
1539 const uint32_t key_hash = record_key_hash(&key);
1540
1541 simple_mtx_lock(&glsl_type_cache_mutex);
1542 assert(glsl_type_cache.users > 0);
1543 void *mem_ctx = glsl_type_cache.mem_ctx;
1544
1545 if (glsl_type_cache.struct_types == NULL) {
1546 glsl_type_cache.struct_types =
1547 _mesa_hash_table_create(mem_ctx, record_key_hash, record_key_compare);
1548 }
1549 struct hash_table *struct_types = glsl_type_cache.struct_types;
1550
1551 const struct hash_entry *entry = _mesa_hash_table_search_pre_hashed(struct_types,
1552 key_hash, &key);
1553 if (entry == NULL) {
1554 const glsl_type *t = make_struct_type(glsl_type_cache.lin_ctx, fields, num_fields,
1555 name, packed, explicit_alignment);
1556
1557 entry = _mesa_hash_table_insert_pre_hashed(struct_types, key_hash, t, (void *) t);
1558 }
1559
1560 const glsl_type *t = (const glsl_type *) entry->data;
1561 simple_mtx_unlock(&glsl_type_cache_mutex);
1562
1563 assert(t->base_type == GLSL_TYPE_STRUCT);
1564 assert(t->length == num_fields);
1565 assert(strcmp(glsl_get_type_name(t), name) == 0);
1566 assert(t->packed == packed);
1567 assert(t->explicit_alignment == explicit_alignment);
1568
1569 return t;
1570 }
1571
1572
1573 const glsl_type *
glsl_interface_type(const glsl_struct_field * fields,unsigned num_fields,enum glsl_interface_packing packing,bool row_major,const char * block_name)1574 glsl_interface_type(const glsl_struct_field *fields,
1575 unsigned num_fields,
1576 enum glsl_interface_packing packing,
1577 bool row_major,
1578 const char *block_name)
1579 {
1580 glsl_type key = {0};
1581 fill_interface_type(&key, fields, num_fields, packing, row_major, block_name);
1582 const uint32_t key_hash = record_key_hash(&key);
1583
1584 simple_mtx_lock(&glsl_type_cache_mutex);
1585 assert(glsl_type_cache.users > 0);
1586 void *mem_ctx = glsl_type_cache.mem_ctx;
1587
1588 if (glsl_type_cache.interface_types == NULL) {
1589 glsl_type_cache.interface_types =
1590 _mesa_hash_table_create(mem_ctx, record_key_hash, record_key_compare);
1591 }
1592 struct hash_table *interface_types = glsl_type_cache.interface_types;
1593
1594 const struct hash_entry *entry = _mesa_hash_table_search_pre_hashed(interface_types,
1595 key_hash, &key);
1596 if (entry == NULL) {
1597 const glsl_type *t = make_interface_type(glsl_type_cache.lin_ctx, fields, num_fields,
1598 packing, row_major, block_name);
1599
1600 entry = _mesa_hash_table_insert_pre_hashed(interface_types, key_hash, t, (void *) t);
1601 }
1602
1603 const glsl_type *t = (const glsl_type *) entry->data;
1604 simple_mtx_unlock(&glsl_type_cache_mutex);
1605
1606 assert(t->base_type == GLSL_TYPE_INTERFACE);
1607 assert(t->length == num_fields);
1608 assert(strcmp(glsl_get_type_name(t), block_name) == 0);
1609
1610 return t;
1611 }
1612
1613 const glsl_type *
glsl_subroutine_type(const char * subroutine_name)1614 glsl_subroutine_type(const char *subroutine_name)
1615 {
1616 const uint32_t key_hash = _mesa_hash_string(subroutine_name);
1617
1618 simple_mtx_lock(&glsl_type_cache_mutex);
1619 assert(glsl_type_cache.users > 0);
1620 void *mem_ctx = glsl_type_cache.mem_ctx;
1621
1622 if (glsl_type_cache.subroutine_types == NULL) {
1623 glsl_type_cache.subroutine_types =
1624 _mesa_hash_table_create(mem_ctx, _mesa_hash_string, _mesa_key_string_equal);
1625 }
1626 struct hash_table *subroutine_types = glsl_type_cache.subroutine_types;
1627
1628 const struct hash_entry *entry = _mesa_hash_table_search_pre_hashed(subroutine_types,
1629 key_hash, subroutine_name);
1630 if (entry == NULL) {
1631 const glsl_type *t = make_subroutine_type(glsl_type_cache.lin_ctx, subroutine_name);
1632
1633 entry = _mesa_hash_table_insert_pre_hashed(subroutine_types, key_hash, glsl_get_type_name(t), (void *) t);
1634 }
1635
1636 const glsl_type *t = (const glsl_type *) entry->data;
1637 simple_mtx_unlock(&glsl_type_cache_mutex);
1638
1639 assert(t->base_type == GLSL_TYPE_SUBROUTINE);
1640 assert(strcmp(glsl_get_type_name(t), subroutine_name) == 0);
1641
1642 return t;
1643 }
1644
1645 const glsl_type *
glsl_get_mul_type(const glsl_type * type_a,const glsl_type * type_b)1646 glsl_get_mul_type(const glsl_type *type_a, const glsl_type *type_b)
1647 {
1648 if (glsl_type_is_matrix(type_a) && glsl_type_is_matrix(type_b)) {
1649 /* Matrix multiply. The columns of A must match the rows of B. Given
1650 * the other previously tested constraints, this means the vector type
1651 * of a row from A must be the same as the vector type of a column from
1652 * B.
1653 */
1654 if (glsl_get_row_type(type_a) == glsl_get_column_type(type_b)) {
1655 /* The resulting matrix has the number of columns of matrix B and
1656 * the number of rows of matrix A. We get the row count of A by
1657 * looking at the size of a vector that makes up a column. The
1658 * transpose (size of a row) is done for B.
1659 */
1660 const glsl_type *const type =
1661 glsl_simple_type(type_a->base_type,
1662 glsl_get_column_type(type_a)->vector_elements,
1663 glsl_get_row_type(type_b)->vector_elements);
1664 assert(type != &glsl_type_builtin_error);
1665
1666 return type;
1667 }
1668 } else if (type_a == type_b) {
1669 return type_a;
1670 } else if (glsl_type_is_matrix(type_a)) {
1671 /* A is a matrix and B is a column vector. Columns of A must match
1672 * rows of B. Given the other previously tested constraints, this
1673 * means the vector type of a row from A must be the same as the
1674 * vector the type of B.
1675 */
1676 if (glsl_get_row_type(type_a) == type_b) {
1677 /* The resulting vector has a number of elements equal to
1678 * the number of rows of matrix A. */
1679 const glsl_type *const type =
1680 glsl_simple_type(type_a->base_type,
1681 glsl_get_column_type(type_a)->vector_elements, 1);
1682 assert(type != &glsl_type_builtin_error);
1683
1684 return type;
1685 }
1686 } else {
1687 assert(glsl_type_is_matrix(type_b));
1688
1689 /* A is a row vector and B is a matrix. Columns of A must match rows
1690 * of B. Given the other previously tested constraints, this means
1691 * the type of A must be the same as the vector type of a column from
1692 * B.
1693 */
1694 if (type_a == glsl_get_column_type(type_b)) {
1695 /* The resulting vector has a number of elements equal to
1696 * the number of columns of matrix B. */
1697 const glsl_type *const type =
1698 glsl_simple_type(type_a->base_type,
1699 glsl_get_row_type(type_b)->vector_elements, 1);
1700 assert(type != &glsl_type_builtin_error);
1701
1702 return type;
1703 }
1704 }
1705
1706 return &glsl_type_builtin_error;
1707 }
1708
1709 int
glsl_get_field_index(const glsl_type * t,const char * name)1710 glsl_get_field_index(const glsl_type *t, const char *name)
1711 {
1712 if (t->base_type != GLSL_TYPE_STRUCT &&
1713 t->base_type != GLSL_TYPE_INTERFACE)
1714 return -1;
1715
1716 for (unsigned i = 0; i < t->length; i++) {
1717 if (strcmp(name, t->fields.structure[i].name) == 0)
1718 return i;
1719 }
1720
1721 return -1;
1722 }
1723
1724 const glsl_type *
glsl_get_field_type(const glsl_type * t,const char * name)1725 glsl_get_field_type(const glsl_type *t, const char *name)
1726 {
1727 const int idx = glsl_get_field_index(t, name);
1728 if (idx == -1)
1729 return &glsl_type_builtin_error;
1730 return glsl_get_struct_field(t, (unsigned)idx);
1731 }
1732
1733 unsigned
glsl_get_component_slots(const glsl_type * t)1734 glsl_get_component_slots(const glsl_type *t)
1735 {
1736 switch (t->base_type) {
1737 case GLSL_TYPE_UINT:
1738 case GLSL_TYPE_INT:
1739 case GLSL_TYPE_UINT8:
1740 case GLSL_TYPE_INT8:
1741 case GLSL_TYPE_UINT16:
1742 case GLSL_TYPE_INT16:
1743 case GLSL_TYPE_FLOAT:
1744 case GLSL_TYPE_FLOAT16:
1745 case GLSL_TYPE_BOOL:
1746 return glsl_get_components(t);
1747
1748 case GLSL_TYPE_DOUBLE:
1749 case GLSL_TYPE_UINT64:
1750 case GLSL_TYPE_INT64:
1751 return 2 * glsl_get_components(t);
1752
1753 case GLSL_TYPE_STRUCT:
1754 case GLSL_TYPE_INTERFACE: {
1755 unsigned size = 0;
1756
1757 for (unsigned i = 0; i < t->length; i++)
1758 size += glsl_get_component_slots(t->fields.structure[i].type);
1759
1760 return size;
1761 }
1762
1763 case GLSL_TYPE_ARRAY:
1764 return t->length * glsl_get_component_slots(t->fields.array);
1765
1766 case GLSL_TYPE_SAMPLER:
1767 case GLSL_TYPE_TEXTURE:
1768 case GLSL_TYPE_IMAGE:
1769 return 2;
1770
1771 case GLSL_TYPE_SUBROUTINE:
1772 return 1;
1773
1774 case GLSL_TYPE_COOPERATIVE_MATRIX:
1775 case GLSL_TYPE_ATOMIC_UINT:
1776 case GLSL_TYPE_VOID:
1777 case GLSL_TYPE_ERROR:
1778 break;
1779 }
1780
1781 return 0;
1782 }
1783
1784 unsigned
glsl_get_component_slots_aligned(const glsl_type * t,unsigned offset)1785 glsl_get_component_slots_aligned(const glsl_type *t, unsigned offset)
1786 {
1787 /* Align 64bit type only if it crosses attribute slot boundary. */
1788 switch (t->base_type) {
1789 case GLSL_TYPE_UINT:
1790 case GLSL_TYPE_INT:
1791 case GLSL_TYPE_UINT8:
1792 case GLSL_TYPE_INT8:
1793 case GLSL_TYPE_UINT16:
1794 case GLSL_TYPE_INT16:
1795 case GLSL_TYPE_FLOAT:
1796 case GLSL_TYPE_FLOAT16:
1797 case GLSL_TYPE_BOOL:
1798 return glsl_get_components(t);
1799
1800 case GLSL_TYPE_DOUBLE:
1801 case GLSL_TYPE_UINT64:
1802 case GLSL_TYPE_INT64: {
1803 unsigned size = 2 * glsl_get_components(t);
1804 if (offset % 2 == 1 && (offset % 4 + size) > 4) {
1805 size++;
1806 }
1807
1808 return size;
1809 }
1810
1811 case GLSL_TYPE_STRUCT:
1812 case GLSL_TYPE_INTERFACE: {
1813 unsigned size = 0;
1814
1815 for (unsigned i = 0; i < t->length; i++) {
1816 const glsl_type *member = t->fields.structure[i].type;
1817 size += glsl_get_component_slots_aligned(member, size + offset);
1818 }
1819
1820 return size;
1821 }
1822
1823 case GLSL_TYPE_ARRAY: {
1824 unsigned size = 0;
1825
1826 for (unsigned i = 0; i < t->length; i++) {
1827 size += glsl_get_component_slots_aligned(t->fields.array,
1828 size + offset);
1829 }
1830
1831 return size;
1832 }
1833
1834 case GLSL_TYPE_SAMPLER:
1835 case GLSL_TYPE_TEXTURE:
1836 case GLSL_TYPE_IMAGE:
1837 return 2 + ((offset % 4) == 3 ? 1 : 0);
1838
1839 case GLSL_TYPE_SUBROUTINE:
1840 return 1;
1841
1842 case GLSL_TYPE_COOPERATIVE_MATRIX:
1843 case GLSL_TYPE_ATOMIC_UINT:
1844 case GLSL_TYPE_VOID:
1845 case GLSL_TYPE_ERROR:
1846 break;
1847 }
1848
1849 return 0;
1850 }
1851
1852 unsigned
glsl_get_struct_location_offset(const glsl_type * t,unsigned length)1853 glsl_get_struct_location_offset(const glsl_type *t, unsigned length)
1854 {
1855 unsigned offset = 0;
1856 t = glsl_without_array(t);
1857 if (glsl_type_is_struct(t)) {
1858 assert(length <= t->length);
1859
1860 for (unsigned i = 0; i < length; i++) {
1861 const glsl_type *st = t->fields.structure[i].type;
1862 const glsl_type *wa = glsl_without_array(st);
1863 if (glsl_type_is_struct(wa)) {
1864 unsigned r_offset = glsl_get_struct_location_offset(wa, wa->length);
1865 offset += glsl_type_is_array(st) ?
1866 glsl_get_aoa_size(st) * r_offset : r_offset;
1867 } else if (glsl_type_is_array(st) && glsl_type_is_array(st->fields.array)) {
1868 unsigned outer_array_size = st->length;
1869 const glsl_type *base_type = st->fields.array;
1870
1871 /* For arrays of arrays the outer arrays take up a uniform
1872 * slot for each element. The innermost array elements share a
1873 * single slot so we ignore the innermost array when calculating
1874 * the offset.
1875 */
1876 while (glsl_type_is_array(base_type->fields.array)) {
1877 outer_array_size = outer_array_size * base_type->length;
1878 base_type = base_type->fields.array;
1879 }
1880 offset += outer_array_size;
1881 } else {
1882 /* We dont worry about arrays here because unless the array
1883 * contains a structure or another array it only takes up a single
1884 * uniform slot.
1885 */
1886 offset += 1;
1887 }
1888 }
1889 }
1890 return offset;
1891 }
1892
1893 unsigned
glsl_type_uniform_locations(const glsl_type * t)1894 glsl_type_uniform_locations(const glsl_type *t)
1895 {
1896 unsigned size = 0;
1897
1898 switch (t->base_type) {
1899 case GLSL_TYPE_UINT:
1900 case GLSL_TYPE_INT:
1901 case GLSL_TYPE_FLOAT:
1902 case GLSL_TYPE_FLOAT16:
1903 case GLSL_TYPE_DOUBLE:
1904 case GLSL_TYPE_UINT16:
1905 case GLSL_TYPE_UINT8:
1906 case GLSL_TYPE_INT16:
1907 case GLSL_TYPE_INT8:
1908 case GLSL_TYPE_UINT64:
1909 case GLSL_TYPE_INT64:
1910 case GLSL_TYPE_BOOL:
1911 case GLSL_TYPE_SAMPLER:
1912 case GLSL_TYPE_TEXTURE:
1913 case GLSL_TYPE_IMAGE:
1914 case GLSL_TYPE_SUBROUTINE:
1915 return 1;
1916
1917 case GLSL_TYPE_STRUCT:
1918 case GLSL_TYPE_INTERFACE:
1919 for (unsigned i = 0; i < t->length; i++)
1920 size += glsl_type_uniform_locations(t->fields.structure[i].type);
1921 return size;
1922 case GLSL_TYPE_ARRAY:
1923 return t->length * glsl_type_uniform_locations(t->fields.array);
1924 default:
1925 return 0;
1926 }
1927 }
1928
1929 unsigned
glsl_varying_count(const glsl_type * t)1930 glsl_varying_count(const glsl_type *t)
1931 {
1932 unsigned size = 0;
1933
1934 switch (t->base_type) {
1935 case GLSL_TYPE_UINT:
1936 case GLSL_TYPE_INT:
1937 case GLSL_TYPE_FLOAT:
1938 case GLSL_TYPE_FLOAT16:
1939 case GLSL_TYPE_DOUBLE:
1940 case GLSL_TYPE_BOOL:
1941 case GLSL_TYPE_UINT16:
1942 case GLSL_TYPE_UINT8:
1943 case GLSL_TYPE_INT16:
1944 case GLSL_TYPE_INT8:
1945 case GLSL_TYPE_UINT64:
1946 case GLSL_TYPE_INT64:
1947 return 1;
1948
1949 case GLSL_TYPE_STRUCT:
1950 case GLSL_TYPE_INTERFACE:
1951 for (unsigned i = 0; i < t->length; i++)
1952 size += glsl_varying_count(t->fields.structure[i].type);
1953 return size;
1954 case GLSL_TYPE_ARRAY:
1955 /* Don't count innermost array elements */
1956 if (glsl_type_is_struct(glsl_without_array(t)) ||
1957 glsl_type_is_interface(glsl_without_array(t)) ||
1958 glsl_type_is_array(t->fields.array))
1959 return t->length * glsl_varying_count(t->fields.array);
1960 else
1961 return glsl_varying_count(t->fields.array);
1962 default:
1963 assert(!"unsupported varying type");
1964 return 0;
1965 }
1966 }
1967
1968 unsigned
glsl_get_std140_base_alignment(const glsl_type * t,bool row_major)1969 glsl_get_std140_base_alignment(const glsl_type *t, bool row_major)
1970 {
1971 unsigned N = glsl_type_is_64bit(t) ? 8 : (glsl_type_is_16bit(t) ? 2 : 4);
1972
1973 /* (1) If the member is a scalar consuming <N> basic machine units, the
1974 * base alignment is <N>.
1975 *
1976 * (2) If the member is a two- or four-component vector with components
1977 * consuming <N> basic machine units, the base alignment is 2<N> or
1978 * 4<N>, respectively.
1979 *
1980 * (3) If the member is a three-component vector with components consuming
1981 * <N> basic machine units, the base alignment is 4<N>.
1982 */
1983 if (glsl_type_is_scalar(t) || glsl_type_is_vector(t)) {
1984 switch (t->vector_elements) {
1985 case 1:
1986 return N;
1987 case 2:
1988 return 2 * N;
1989 case 3:
1990 case 4:
1991 return 4 * N;
1992 }
1993 }
1994
1995 /* (4) If the member is an array of scalars or vectors, the base alignment
1996 * and array stride are set to match the base alignment of a single
1997 * array element, according to rules (1), (2), and (3), and rounded up
1998 * to the base alignment of a vec4. The array may have padding at the
1999 * end; the base offset of the member following the array is rounded up
2000 * to the next multiple of the base alignment.
2001 *
2002 * (6) If the member is an array of <S> column-major matrices with <C>
2003 * columns and <R> rows, the matrix is stored identically to a row of
2004 * <S>*<C> column vectors with <R> components each, according to rule
2005 * (4).
2006 *
2007 * (8) If the member is an array of <S> row-major matrices with <C> columns
2008 * and <R> rows, the matrix is stored identically to a row of <S>*<R>
2009 * row vectors with <C> components each, according to rule (4).
2010 *
2011 * (10) If the member is an array of <S> structures, the <S> elements of
2012 * the array are laid out in order, according to rule (9).
2013 */
2014 if (glsl_type_is_array(t)) {
2015 if (glsl_type_is_scalar(t->fields.array) ||
2016 glsl_type_is_vector(t->fields.array) ||
2017 glsl_type_is_matrix(t->fields.array)) {
2018 return MAX2(glsl_get_std140_base_alignment(t->fields.array, row_major),
2019 16);
2020 } else {
2021 assert(glsl_type_is_struct(t->fields.array) ||
2022 glsl_type_is_array(t->fields.array));
2023 return glsl_get_std140_base_alignment(t->fields.array, row_major);
2024 }
2025 }
2026
2027 /* (5) If the member is a column-major matrix with <C> columns and
2028 * <R> rows, the matrix is stored identically to an array of
2029 * <C> column vectors with <R> components each, according to
2030 * rule (4).
2031 *
2032 * (7) If the member is a row-major matrix with <C> columns and <R>
2033 * rows, the matrix is stored identically to an array of <R>
2034 * row vectors with <C> components each, according to rule (4).
2035 */
2036 if (glsl_type_is_matrix(t)) {
2037 const glsl_type *vec_type, *array_type;
2038 int c = t->matrix_columns;
2039 int r = t->vector_elements;
2040
2041 if (row_major) {
2042 vec_type = glsl_simple_type(t->base_type, c, 1);
2043 array_type = glsl_array_type(vec_type, r, 0);
2044 } else {
2045 vec_type = glsl_simple_type(t->base_type, r, 1);
2046 array_type = glsl_array_type(vec_type, c, 0);
2047 }
2048
2049 return glsl_get_std140_base_alignment(array_type, false);
2050 }
2051
2052 /* (9) If the member is a structure, the base alignment of the
2053 * structure is <N>, where <N> is the largest base alignment
2054 * value of any of its members, and rounded up to the base
2055 * alignment of a vec4. The individual members of this
2056 * sub-structure are then assigned offsets by applying this set
2057 * of rules recursively, where the base offset of the first
2058 * member of the sub-structure is equal to the aligned offset
2059 * of the structure. The structure may have padding at the end;
2060 * the base offset of the member following the sub-structure is
2061 * rounded up to the next multiple of the base alignment of the
2062 * structure.
2063 */
2064 if (glsl_type_is_struct(t)) {
2065 unsigned base_alignment = 16;
2066 for (unsigned i = 0; i < t->length; i++) {
2067 bool field_row_major = row_major;
2068 const enum glsl_matrix_layout matrix_layout =
2069 (enum glsl_matrix_layout)t->fields.structure[i].matrix_layout;
2070 if (matrix_layout == GLSL_MATRIX_LAYOUT_ROW_MAJOR) {
2071 field_row_major = true;
2072 } else if (matrix_layout == GLSL_MATRIX_LAYOUT_COLUMN_MAJOR) {
2073 field_row_major = false;
2074 }
2075
2076 const glsl_type *field_type = t->fields.structure[i].type;
2077 base_alignment = MAX2(base_alignment,
2078 glsl_get_std140_base_alignment(field_type, field_row_major));
2079 }
2080 return base_alignment;
2081 }
2082
2083 assert(!"not reached");
2084 return -1;
2085 }
2086
2087 unsigned
glsl_get_std140_size(const glsl_type * t,bool row_major)2088 glsl_get_std140_size(const glsl_type *t, bool row_major)
2089 {
2090 unsigned N = glsl_type_is_64bit(t) ? 8 : (glsl_type_is_16bit(t) ? 2 : 4);
2091
2092 /* (1) If the member is a scalar consuming <N> basic machine units, the
2093 * base alignment is <N>.
2094 *
2095 * (2) If the member is a two- or four-component vector with components
2096 * consuming <N> basic machine units, the base alignment is 2<N> or
2097 * 4<N>, respectively.
2098 *
2099 * (3) If the member is a three-component vector with components consuming
2100 * <N> basic machine units, the base alignment is 4<N>.
2101 */
2102 if (glsl_type_is_scalar(t) || glsl_type_is_vector(t)) {
2103 assert(t->explicit_stride == 0);
2104 return t->vector_elements * N;
2105 }
2106
2107 /* (5) If the member is a column-major matrix with <C> columns and
2108 * <R> rows, the matrix is stored identically to an array of
2109 * <C> column vectors with <R> components each, according to
2110 * rule (4).
2111 *
2112 * (6) If the member is an array of <S> column-major matrices with <C>
2113 * columns and <R> rows, the matrix is stored identically to a row of
2114 * <S>*<C> column vectors with <R> components each, according to rule
2115 * (4).
2116 *
2117 * (7) If the member is a row-major matrix with <C> columns and <R>
2118 * rows, the matrix is stored identically to an array of <R>
2119 * row vectors with <C> components each, according to rule (4).
2120 *
2121 * (8) If the member is an array of <S> row-major matrices with <C> columns
2122 * and <R> rows, the matrix is stored identically to a row of <S>*<R>
2123 * row vectors with <C> components each, according to rule (4).
2124 */
2125 if (glsl_type_is_matrix(glsl_without_array(t))) {
2126 const glsl_type *element_type;
2127 const glsl_type *vec_type;
2128 unsigned int array_len;
2129
2130 if (glsl_type_is_array(t)) {
2131 element_type = glsl_without_array(t);
2132 array_len = glsl_get_aoa_size(t);
2133 } else {
2134 element_type = t;
2135 array_len = 1;
2136 }
2137
2138 if (row_major) {
2139 vec_type = glsl_simple_type(element_type->base_type,
2140 element_type->matrix_columns, 1);
2141
2142 array_len *= element_type->vector_elements;
2143 } else {
2144 vec_type = glsl_simple_type(element_type->base_type,
2145 element_type->vector_elements, 1);
2146 array_len *= element_type->matrix_columns;
2147 }
2148 const glsl_type *array_type =
2149 glsl_array_type(vec_type, array_len, 0);
2150
2151 return glsl_get_std140_size(array_type, false);
2152 }
2153
2154 /* (4) If the member is an array of scalars or vectors, the base alignment
2155 * and array stride are set to match the base alignment of a single
2156 * array element, according to rules (1), (2), and (3), and rounded up
2157 * to the base alignment of a vec4. The array may have padding at the
2158 * end; the base offset of the member following the array is rounded up
2159 * to the next multiple of the base alignment.
2160 *
2161 * (10) If the member is an array of <S> structures, the <S> elements of
2162 * the array are laid out in order, according to rule (9).
2163 */
2164 if (glsl_type_is_array(t)) {
2165 unsigned stride;
2166 if (glsl_type_is_struct(glsl_without_array(t))) {
2167 stride = glsl_get_std140_size(glsl_without_array(t), row_major);
2168 } else {
2169 unsigned element_base_align =
2170 glsl_get_std140_base_alignment(glsl_without_array(t), row_major);
2171 stride = MAX2(element_base_align, 16);
2172 }
2173
2174 unsigned size = glsl_get_aoa_size(t) * stride;
2175 assert(t->explicit_stride == 0 ||
2176 size == t->length * t->explicit_stride);
2177 return size;
2178 }
2179
2180 /* (9) If the member is a structure, the base alignment of the
2181 * structure is <N>, where <N> is the largest base alignment
2182 * value of any of its members, and rounded up to the base
2183 * alignment of a vec4. The individual members of this
2184 * sub-structure are then assigned offsets by applying this set
2185 * of rules recursively, where the base offset of the first
2186 * member of the sub-structure is equal to the aligned offset
2187 * of the structure. The structure may have padding at the end;
2188 * the base offset of the member following the sub-structure is
2189 * rounded up to the next multiple of the base alignment of the
2190 * structure.
2191 */
2192 if (glsl_type_is_struct(t) || glsl_type_is_interface(t)) {
2193 unsigned size = 0;
2194 unsigned max_align = 0;
2195
2196 for (unsigned i = 0; i < t->length; i++) {
2197 bool field_row_major = row_major;
2198 const enum glsl_matrix_layout matrix_layout =
2199 (enum glsl_matrix_layout)t->fields.structure[i].matrix_layout;
2200 if (matrix_layout == GLSL_MATRIX_LAYOUT_ROW_MAJOR) {
2201 field_row_major = true;
2202 } else if (matrix_layout == GLSL_MATRIX_LAYOUT_COLUMN_MAJOR) {
2203 field_row_major = false;
2204 }
2205
2206 const glsl_type *field_type = t->fields.structure[i].type;
2207 unsigned base_alignment = glsl_get_std140_base_alignment(field_type,
2208 field_row_major);
2209
2210 /* Ignore unsized arrays when calculating size */
2211 if (glsl_type_is_unsized_array(field_type))
2212 continue;
2213
2214 size = align(size, base_alignment);
2215 size += glsl_get_std140_size(field_type, field_row_major);
2216
2217 max_align = MAX2(base_alignment, max_align);
2218
2219 if (glsl_type_is_struct(field_type) && (i + 1 < t->length))
2220 size = align(size, 16);
2221 }
2222 size = align(size, MAX2(max_align, 16));
2223 return size;
2224 }
2225
2226 assert(!"not reached");
2227 return -1;
2228 }
2229
2230 const glsl_type *
glsl_get_explicit_std140_type(const glsl_type * t,bool row_major)2231 glsl_get_explicit_std140_type(const glsl_type *t, bool row_major)
2232 {
2233 if (glsl_type_is_vector(t) || glsl_type_is_scalar(t)) {
2234 return t;
2235 } else if (glsl_type_is_matrix(t)) {
2236 const glsl_type *vec_type;
2237 if (row_major)
2238 vec_type = glsl_simple_type(t->base_type, t->matrix_columns, 1);
2239 else
2240 vec_type = glsl_simple_type(t->base_type, t->vector_elements, 1);
2241 unsigned elem_size = glsl_get_std140_size(vec_type, false);
2242 unsigned stride = align(elem_size, 16);
2243 return glsl_simple_explicit_type(t->base_type, t->vector_elements,
2244 t->matrix_columns, stride, row_major,
2245 0);
2246 } else if (glsl_type_is_array(t)) {
2247 unsigned elem_size = glsl_get_std140_size(t->fields.array, row_major);
2248 const glsl_type *elem_type =
2249 glsl_get_explicit_std140_type(t->fields.array, row_major);
2250 unsigned stride = align(elem_size, 16);
2251 return glsl_array_type(elem_type, t->length, stride);
2252 } else if (glsl_type_is_struct(t) || glsl_type_is_interface(t)) {
2253 glsl_struct_field *fields = (glsl_struct_field *)
2254 calloc(t->length, sizeof(glsl_struct_field));
2255 unsigned offset = 0;
2256 for (unsigned i = 0; i < t->length; i++) {
2257 fields[i] = t->fields.structure[i];
2258
2259 bool field_row_major = row_major;
2260 if (fields[i].matrix_layout == GLSL_MATRIX_LAYOUT_COLUMN_MAJOR) {
2261 field_row_major = false;
2262 } else if (fields[i].matrix_layout == GLSL_MATRIX_LAYOUT_ROW_MAJOR) {
2263 field_row_major = true;
2264 }
2265 fields[i].type =
2266 glsl_get_explicit_std140_type(fields[i].type, field_row_major);
2267
2268 unsigned fsize = glsl_get_std140_size(fields[i].type,
2269 field_row_major);
2270 unsigned falign = glsl_get_std140_base_alignment(fields[i].type,
2271 field_row_major);
2272 /* From the GLSL 460 spec section "Uniform and Shader Storage Block
2273 * Layout Qualifiers":
2274 *
2275 * "The actual offset of a member is computed as follows: If
2276 * offset was declared, start with that offset, otherwise start
2277 * with the next available offset. If the resulting offset is not
2278 * a multiple of the actual alignment, increase it to the first
2279 * offset that is a multiple of the actual alignment. This results
2280 * in the actual offset the member will have."
2281 */
2282 if (fields[i].offset >= 0) {
2283 assert((unsigned)fields[i].offset >= offset);
2284 offset = fields[i].offset;
2285 }
2286 offset = align(offset, falign);
2287 fields[i].offset = offset;
2288 offset += fsize;
2289 }
2290
2291 const glsl_type *type;
2292 if (glsl_type_is_struct(t))
2293 type = glsl_struct_type(fields, t->length, glsl_get_type_name(t), false);
2294 else
2295 type = glsl_interface_type(fields, t->length,
2296 (enum glsl_interface_packing)t->interface_packing,
2297 t->interface_row_major, glsl_get_type_name(t));
2298
2299 free(fields);
2300 return type;
2301 } else {
2302 unreachable("Invalid type for UBO or SSBO");
2303 }
2304 }
2305
2306 unsigned
glsl_get_std430_base_alignment(const glsl_type * t,bool row_major)2307 glsl_get_std430_base_alignment(const glsl_type *t, bool row_major)
2308 {
2309
2310 unsigned N = glsl_type_is_64bit(t) ? 8 : (glsl_type_is_16bit(t) ? 2 : 4);
2311
2312 /* (1) If the member is a scalar consuming <N> basic machine units, the
2313 * base alignment is <N>.
2314 *
2315 * (2) If the member is a two- or four-component vector with components
2316 * consuming <N> basic machine units, the base alignment is 2<N> or
2317 * 4<N>, respectively.
2318 *
2319 * (3) If the member is a three-component vector with components consuming
2320 * <N> basic machine units, the base alignment is 4<N>.
2321 */
2322 if (glsl_type_is_scalar(t) || glsl_type_is_vector(t)) {
2323 switch (t->vector_elements) {
2324 case 1:
2325 return N;
2326 case 2:
2327 return 2 * N;
2328 case 3:
2329 case 4:
2330 return 4 * N;
2331 }
2332 }
2333
2334 /* OpenGL 4.30 spec, section 7.6.2.2 "Standard Uniform Block Layout":
2335 *
2336 * "When using the std430 storage layout, shader storage blocks will be
2337 * laid out in buffer storage identically to uniform and shader storage
2338 * blocks using the std140 layout, except that the base alignment and
2339 * stride of arrays of scalars and vectors in rule 4 and of structures
2340 * in rule 9 are not rounded up a multiple of the base alignment of a vec4.
2341 */
2342
2343 /* (1) If the member is a scalar consuming <N> basic machine units, the
2344 * base alignment is <N>.
2345 *
2346 * (2) If the member is a two- or four-component vector with components
2347 * consuming <N> basic machine units, the base alignment is 2<N> or
2348 * 4<N>, respectively.
2349 *
2350 * (3) If the member is a three-component vector with components consuming
2351 * <N> basic machine units, the base alignment is 4<N>.
2352 */
2353 if (glsl_type_is_array(t))
2354 return glsl_get_std430_base_alignment(t->fields.array, row_major);
2355
2356 /* (5) If the member is a column-major matrix with <C> columns and
2357 * <R> rows, the matrix is stored identically to an array of
2358 * <C> column vectors with <R> components each, according to
2359 * rule (4).
2360 *
2361 * (7) If the member is a row-major matrix with <C> columns and <R>
2362 * rows, the matrix is stored identically to an array of <R>
2363 * row vectors with <C> components each, according to rule (4).
2364 */
2365 if (glsl_type_is_matrix(t)) {
2366 const glsl_type *vec_type, *array_type;
2367 int c = t->matrix_columns;
2368 int r = t->vector_elements;
2369
2370 if (row_major) {
2371 vec_type = glsl_simple_type(t->base_type, c, 1);
2372 array_type = glsl_array_type(vec_type, r, 0);
2373 } else {
2374 vec_type = glsl_simple_type(t->base_type, r, 1);
2375 array_type = glsl_array_type(vec_type, c, 0);
2376 }
2377
2378 return glsl_get_std430_base_alignment(array_type, false);
2379 }
2380
2381 /* (9) If the member is a structure, the base alignment of the
2382 * structure is <N>, where <N> is the largest base alignment
2383 * value of any of its members, and rounded up to the base
2384 * alignment of a vec4. The individual members of this
2385 * sub-structure are then assigned offsets by applying this set
2386 * of rules recursively, where the base offset of the first
2387 * member of the sub-structure is equal to the aligned offset
2388 * of the structure. The structure may have padding at the end;
2389 * the base offset of the member following the sub-structure is
2390 * rounded up to the next multiple of the base alignment of the
2391 * structure.
2392 */
2393 if (glsl_type_is_struct(t)) {
2394 unsigned base_alignment = 0;
2395 for (unsigned i = 0; i < t->length; i++) {
2396 bool field_row_major = row_major;
2397 const enum glsl_matrix_layout matrix_layout =
2398 (enum glsl_matrix_layout)t->fields.structure[i].matrix_layout;
2399 if (matrix_layout == GLSL_MATRIX_LAYOUT_ROW_MAJOR) {
2400 field_row_major = true;
2401 } else if (matrix_layout == GLSL_MATRIX_LAYOUT_COLUMN_MAJOR) {
2402 field_row_major = false;
2403 }
2404
2405 const glsl_type *field_type = t->fields.structure[i].type;
2406 base_alignment = MAX2(base_alignment,
2407 glsl_get_std430_base_alignment(field_type, field_row_major));
2408 }
2409 assert(base_alignment > 0);
2410 return base_alignment;
2411 }
2412 assert(!"not reached");
2413 return -1;
2414 }
2415
2416 unsigned
glsl_get_std430_array_stride(const glsl_type * t,bool row_major)2417 glsl_get_std430_array_stride(const glsl_type *t, bool row_major)
2418 {
2419 unsigned N = glsl_type_is_64bit(t) ? 8 : (glsl_type_is_16bit(t) ? 2 : 4);
2420
2421 /* Notice that the array stride of a vec3 is not 3 * N but 4 * N.
2422 * See OpenGL 4.30 spec, section 7.6.2.2 "Standard Uniform Block Layout"
2423 *
2424 * (3) If the member is a three-component vector with components consuming
2425 * <N> basic machine units, the base alignment is 4<N>.
2426 */
2427 if (glsl_type_is_vector(t) && t->vector_elements == 3)
2428 return 4 * N;
2429
2430 /* By default use std430_size(row_major) */
2431 unsigned stride = glsl_get_std430_size(t, row_major);
2432 assert(t->explicit_stride == 0 || t->explicit_stride == stride);
2433 return stride;
2434 }
2435
2436 /* Note that the value returned by this method is only correct if the
2437 * explit offset, and stride values are set, so only with SPIR-V shaders.
2438 * Should not be used with GLSL shaders.
2439 */
2440
2441 unsigned
glsl_get_explicit_size(const glsl_type * t,bool align_to_stride)2442 glsl_get_explicit_size(const glsl_type *t, bool align_to_stride)
2443 {
2444 if (glsl_type_is_struct(t) || glsl_type_is_interface(t)) {
2445 if (t->length > 0) {
2446 unsigned size = 0;
2447
2448 for (unsigned i = 0; i < t->length; i++) {
2449 assert(t->fields.structure[i].offset >= 0);
2450 unsigned last_byte = t->fields.structure[i].offset +
2451 glsl_get_explicit_size(t->fields.structure[i].type, false);
2452 size = MAX2(size, last_byte);
2453 }
2454
2455 return size;
2456 } else {
2457 return 0;
2458 }
2459 } else if (glsl_type_is_array(t)) {
2460 /* From ARB_program_interface_query spec:
2461 *
2462 * "For the property of BUFFER_DATA_SIZE, then the implementation-dependent
2463 * minimum total buffer object size, in basic machine units, required to
2464 * hold all active variables associated with an active uniform block, shader
2465 * storage block, or atomic counter buffer is written to <params>. If the
2466 * final member of an active shader storage block is array with no declared
2467 * size, the minimum buffer size is computed assuming the array was declared
2468 * as an array with one element."
2469 *
2470 */
2471 if (glsl_type_is_unsized_array(t))
2472 return t->explicit_stride;
2473
2474 assert(t->length > 0);
2475 unsigned elem_size = align_to_stride ? t->explicit_stride : glsl_get_explicit_size(t->fields.array,
2476 false);
2477 assert(t->explicit_stride == 0 || t->explicit_stride >= elem_size);
2478
2479 return t->explicit_stride * (t->length - 1) + elem_size;
2480 } else if (glsl_type_is_matrix(t)) {
2481 const glsl_type *elem_type;
2482 unsigned length;
2483
2484 if (t->interface_row_major) {
2485 elem_type = glsl_simple_type(t->base_type, t->matrix_columns, 1);
2486 length = t->vector_elements;
2487 } else {
2488 elem_type = glsl_simple_type(t->base_type, t->vector_elements, 1);
2489 length = t->matrix_columns;
2490 }
2491
2492 unsigned elem_size = align_to_stride ? t->explicit_stride : glsl_get_explicit_size(elem_type,
2493 false);
2494
2495 assert(t->explicit_stride);
2496 return t->explicit_stride * (length - 1) + elem_size;
2497 }
2498
2499 unsigned N = glsl_base_type_bit_size(t->base_type) / 8;
2500
2501 return t->vector_elements * N;
2502 }
2503
2504 unsigned
glsl_get_std430_size(const glsl_type * t,bool row_major)2505 glsl_get_std430_size(const glsl_type *t, bool row_major)
2506 {
2507 unsigned N = glsl_type_is_64bit(t) ? 8 : (glsl_type_is_16bit(t) ? 2 : 4);
2508
2509 /* OpenGL 4.30 spec, section 7.6.2.2 "Standard Uniform Block Layout":
2510 *
2511 * "When using the std430 storage layout, shader storage blocks will be
2512 * laid out in buffer storage identically to uniform and shader storage
2513 * blocks using the std140 layout, except that the base alignment and
2514 * stride of arrays of scalars and vectors in rule 4 and of structures
2515 * in rule 9 are not rounded up a multiple of the base alignment of a vec4.
2516 */
2517 if (glsl_type_is_scalar(t) || glsl_type_is_vector(t)) {
2518 assert(t->explicit_stride == 0);
2519 return t->vector_elements * N;
2520 }
2521
2522 if (glsl_type_is_matrix(glsl_without_array(t))) {
2523 const glsl_type *element_type;
2524 const glsl_type *vec_type;
2525 unsigned int array_len;
2526
2527 if (glsl_type_is_array(t)) {
2528 element_type = glsl_without_array(t);
2529 array_len = glsl_get_aoa_size(t);
2530 } else {
2531 element_type = t;
2532 array_len = 1;
2533 }
2534
2535 if (row_major) {
2536 vec_type = glsl_simple_type(element_type->base_type,
2537 element_type->matrix_columns, 1);
2538
2539 array_len *= element_type->vector_elements;
2540 } else {
2541 vec_type = glsl_simple_type(element_type->base_type,
2542 element_type->vector_elements, 1);
2543 array_len *= element_type->matrix_columns;
2544 }
2545 const glsl_type *array_type =
2546 glsl_array_type(vec_type, array_len, 0);
2547
2548 return glsl_get_std430_size(array_type, false);
2549 }
2550
2551 if (glsl_type_is_array(t)) {
2552 unsigned stride;
2553 if (glsl_type_is_struct(glsl_without_array(t)))
2554 stride = glsl_get_std430_size(glsl_without_array(t), row_major);
2555 else
2556 stride = glsl_get_std430_base_alignment(glsl_without_array(t),
2557 row_major);
2558
2559 unsigned size = glsl_get_aoa_size(t) * stride;
2560 assert(t->explicit_stride == 0 ||
2561 size == t->length * t->explicit_stride);
2562 return size;
2563 }
2564
2565 if (glsl_type_is_struct(t) || glsl_type_is_interface(t)) {
2566 unsigned size = 0;
2567 unsigned max_align = 0;
2568
2569 for (unsigned i = 0; i < t->length; i++) {
2570 bool field_row_major = row_major;
2571 const enum glsl_matrix_layout matrix_layout =
2572 (enum glsl_matrix_layout)t->fields.structure[i].matrix_layout;
2573 if (matrix_layout == GLSL_MATRIX_LAYOUT_ROW_MAJOR) {
2574 field_row_major = true;
2575 } else if (matrix_layout == GLSL_MATRIX_LAYOUT_COLUMN_MAJOR) {
2576 field_row_major = false;
2577 }
2578
2579 const glsl_type *field_type = t->fields.structure[i].type;
2580 unsigned base_alignment = glsl_get_std430_base_alignment(field_type,
2581 field_row_major);
2582 size = align(size, base_alignment);
2583 size += glsl_get_std430_size(field_type, field_row_major);
2584
2585 max_align = MAX2(base_alignment, max_align);
2586 }
2587 size = align(size, max_align);
2588 return size;
2589 }
2590
2591 assert(!"not reached");
2592 return -1;
2593 }
2594
2595 const glsl_type *
glsl_get_explicit_std430_type(const glsl_type * t,bool row_major)2596 glsl_get_explicit_std430_type(const glsl_type *t, bool row_major)
2597 {
2598 if (glsl_type_is_vector(t) || glsl_type_is_scalar(t)) {
2599 return t;
2600 } else if (glsl_type_is_matrix(t)) {
2601 const glsl_type *vec_type;
2602 if (row_major)
2603 vec_type = glsl_simple_type(t->base_type, t->matrix_columns, 1);
2604 else
2605 vec_type = glsl_simple_type(t->base_type, t->vector_elements, 1);
2606 unsigned stride = glsl_get_std430_array_stride(vec_type, false);
2607 return glsl_simple_explicit_type(t->base_type, t->vector_elements,
2608 t->matrix_columns, stride, row_major,
2609 0);
2610 } else if (glsl_type_is_array(t)) {
2611 const glsl_type *elem_type =
2612 glsl_get_explicit_std430_type(t->fields.array, row_major);
2613 unsigned stride = glsl_get_std430_array_stride(t->fields.array,
2614 row_major);
2615 return glsl_array_type(elem_type, t->length, stride);
2616 } else if (glsl_type_is_struct(t) || glsl_type_is_interface(t)) {
2617 glsl_struct_field *fields = (glsl_struct_field *)
2618 calloc(t->length, sizeof(glsl_struct_field));
2619 unsigned offset = 0;
2620 for (unsigned i = 0; i < t->length; i++) {
2621 fields[i] = t->fields.structure[i];
2622
2623 bool field_row_major = row_major;
2624 if (fields[i].matrix_layout == GLSL_MATRIX_LAYOUT_COLUMN_MAJOR) {
2625 field_row_major = false;
2626 } else if (fields[i].matrix_layout == GLSL_MATRIX_LAYOUT_ROW_MAJOR) {
2627 field_row_major = true;
2628 }
2629 fields[i].type =
2630 glsl_get_explicit_std430_type(fields[i].type, field_row_major);
2631
2632 unsigned fsize = glsl_get_std430_size(fields[i].type,
2633 field_row_major);
2634 unsigned falign = glsl_get_std430_base_alignment(fields[i].type,
2635 field_row_major);
2636 /* From the GLSL 460 spec section "Uniform and Shader Storage Block
2637 * Layout Qualifiers":
2638 *
2639 * "The actual offset of a member is computed as follows: If
2640 * offset was declared, start with that offset, otherwise start
2641 * with the next available offset. If the resulting offset is not
2642 * a multiple of the actual alignment, increase it to the first
2643 * offset that is a multiple of the actual alignment. This results
2644 * in the actual offset the member will have."
2645 */
2646 if (fields[i].offset >= 0) {
2647 assert((unsigned)fields[i].offset >= offset);
2648 offset = fields[i].offset;
2649 }
2650 offset = align(offset, falign);
2651 fields[i].offset = offset;
2652 offset += fsize;
2653 }
2654
2655 const glsl_type *type;
2656 if (glsl_type_is_struct(t))
2657 type = glsl_struct_type(fields, t->length, glsl_get_type_name(t), false);
2658 else
2659 type = glsl_interface_type(fields, t->length,
2660 (enum glsl_interface_packing)t->interface_packing,
2661 t->interface_row_major, glsl_get_type_name(t));
2662
2663 free(fields);
2664 return type;
2665 } else {
2666 unreachable("Invalid type for SSBO");
2667 }
2668 }
2669
2670 static unsigned
explicit_type_scalar_byte_size(const glsl_type * type)2671 explicit_type_scalar_byte_size(const glsl_type *type)
2672 {
2673 if (type->base_type == GLSL_TYPE_BOOL)
2674 return 4;
2675 else
2676 return glsl_base_type_get_bit_size(type->base_type) / 8;
2677 }
2678
2679 /* This differs from get_explicit_std430_type() in that it:
2680 * - can size arrays slightly smaller ("stride * (len - 1) + elem_size" instead
2681 * of "stride * len")
2682 * - consumes a glsl_type_size_align_func which allows 8 and 16-bit values to be
2683 * packed more tightly
2684 * - overrides any struct field offsets but get_explicit_std430_type() tries to
2685 * respect any existing ones
2686 */
2687 const glsl_type *
glsl_get_explicit_type_for_size_align(const glsl_type * t,glsl_type_size_align_func type_info,unsigned * size,unsigned * alignment)2688 glsl_get_explicit_type_for_size_align(const glsl_type *t,
2689 glsl_type_size_align_func type_info,
2690 unsigned *size, unsigned *alignment)
2691 {
2692 if (glsl_type_is_image(t) || glsl_type_is_sampler(t)) {
2693 type_info(t, size, alignment);
2694 assert(*alignment > 0);
2695 return t;
2696 } else if (glsl_type_is_cmat(t)) {
2697 *size = 0;
2698 *alignment = 0;
2699 return t;
2700 } else if (glsl_type_is_scalar(t)) {
2701 type_info(t, size, alignment);
2702 assert(*size == explicit_type_scalar_byte_size(t));
2703 assert(*alignment == explicit_type_scalar_byte_size(t));
2704 return t;
2705 } else if (glsl_type_is_vector(t)) {
2706 type_info(t, size, alignment);
2707 assert(*alignment > 0);
2708 assert(*alignment % explicit_type_scalar_byte_size(t) == 0);
2709 return glsl_simple_explicit_type(t->base_type, t->vector_elements, 1, 0,
2710 false, *alignment);
2711 } else if (glsl_type_is_array(t)) {
2712 unsigned elem_size, elem_align;
2713 const glsl_type *explicit_element =
2714 glsl_get_explicit_type_for_size_align(t->fields.array, type_info,
2715 &elem_size, &elem_align);
2716
2717 unsigned stride = align(elem_size, elem_align);
2718
2719 *size = stride * (t->length - 1) + elem_size;
2720 *alignment = elem_align;
2721 return glsl_array_type(explicit_element, t->length, stride);
2722 } else if (glsl_type_is_struct(t) || glsl_type_is_interface(t)) {
2723 glsl_struct_field *fields = (glsl_struct_field *)
2724 malloc(sizeof(glsl_struct_field) * t->length);
2725
2726 *size = 0;
2727 *alignment = 1;
2728 for (unsigned i = 0; i < t->length; i++) {
2729 fields[i] = t->fields.structure[i];
2730 assert(fields[i].matrix_layout != GLSL_MATRIX_LAYOUT_ROW_MAJOR);
2731
2732 unsigned field_size, field_align;
2733 fields[i].type =
2734 glsl_get_explicit_type_for_size_align(fields[i].type, type_info,
2735 &field_size, &field_align);
2736 field_align = t->packed ? 1 : field_align;
2737 fields[i].offset = align(*size, field_align);
2738
2739 *size = fields[i].offset + field_size;
2740 *alignment = MAX2(*alignment, field_align);
2741 }
2742 /*
2743 * "The alignment of the struct is the alignment of the most-aligned
2744 * field in it."
2745 *
2746 * "Finally, the size of the struct is the current offset rounded up to
2747 * the nearest multiple of the struct's alignment."
2748 *
2749 * https://doc.rust-lang.org/reference/type-layout.html#reprc-structs
2750 */
2751 *size = align(*size, *alignment);
2752
2753 const glsl_type *type;
2754 if (glsl_type_is_struct(t)) {
2755 type = glsl_struct_type_with_explicit_alignment(fields, t->length,
2756 glsl_get_type_name(t), t->packed,
2757 *alignment);
2758 } else {
2759 assert(!t->packed);
2760 type = glsl_interface_type(fields, t->length,
2761 (enum glsl_interface_packing)t->interface_packing,
2762 t->interface_row_major, glsl_get_type_name(t));
2763 }
2764 free(fields);
2765 return type;
2766 } else if (glsl_type_is_matrix(t)) {
2767 unsigned col_size, col_align;
2768 type_info(glsl_get_column_type(t), &col_size, &col_align);
2769 unsigned stride = align(col_size, col_align);
2770
2771 *size = t->matrix_columns * stride;
2772 /* Matrix and column alignments match. See glsl_type::column_type() */
2773 assert(col_align > 0);
2774 *alignment = col_align;
2775 return glsl_simple_explicit_type(t->base_type, t->vector_elements,
2776 t->matrix_columns, stride, false,
2777 *alignment);
2778 } else {
2779 unreachable("Unhandled type.");
2780 }
2781 }
2782
2783 const glsl_type *
glsl_type_replace_vec3_with_vec4(const glsl_type * t)2784 glsl_type_replace_vec3_with_vec4(const glsl_type *t)
2785 {
2786 if (glsl_type_is_scalar(t) || glsl_type_is_vector(t) || glsl_type_is_matrix(t)) {
2787 if (t->interface_row_major) {
2788 if (t->matrix_columns == 3) {
2789 return glsl_simple_explicit_type(t->base_type, t->vector_elements,
2790 4, t->explicit_stride,
2791 t->interface_row_major,
2792 t->explicit_alignment);
2793 } else {
2794 return t;
2795 }
2796 } else {
2797 if (t->vector_elements == 3) {
2798 return glsl_simple_explicit_type(t->base_type, 4,
2799 t->matrix_columns,
2800 t->explicit_stride,
2801 t->interface_row_major,
2802 t->explicit_alignment);
2803 } else {
2804 return t;
2805 }
2806 }
2807 } else if (glsl_type_is_array(t)) {
2808 const glsl_type *vec4_elem_type =
2809 glsl_type_replace_vec3_with_vec4(t->fields.array);
2810 if (vec4_elem_type == t->fields.array)
2811 return t;
2812 return glsl_array_type(vec4_elem_type, t->length, t->explicit_stride);
2813 } else if (glsl_type_is_struct(t) || glsl_type_is_interface(t)) {
2814 glsl_struct_field *fields = (glsl_struct_field *)
2815 malloc(sizeof(glsl_struct_field) * t->length);
2816
2817 bool needs_new_type = false;
2818 for (unsigned i = 0; i < t->length; i++) {
2819 fields[i] = t->fields.structure[i];
2820 assert(fields[i].matrix_layout != GLSL_MATRIX_LAYOUT_ROW_MAJOR);
2821 fields[i].type = glsl_type_replace_vec3_with_vec4(fields[i].type);
2822 if (fields[i].type != t->fields.structure[i].type)
2823 needs_new_type = true;
2824 }
2825
2826 const glsl_type *type;
2827 if (!needs_new_type) {
2828 type = t;
2829 } else if (glsl_type_is_struct(t)) {
2830 type = glsl_struct_type_with_explicit_alignment(fields, t->length,
2831 glsl_get_type_name(t), t->packed,
2832 t->explicit_alignment);
2833 } else {
2834 assert(!t->packed);
2835 type = glsl_interface_type(fields, t->length,
2836 (enum glsl_interface_packing)t->interface_packing,
2837 t->interface_row_major, glsl_get_type_name(t));
2838 }
2839 free(fields);
2840 return type;
2841 } else {
2842 unreachable("Unhandled type.");
2843 }
2844 }
2845
2846 unsigned
glsl_count_vec4_slots(const glsl_type * t,bool is_gl_vertex_input,bool is_bindless)2847 glsl_count_vec4_slots(const glsl_type *t, bool is_gl_vertex_input, bool is_bindless)
2848 {
2849 /* From page 31 (page 37 of the PDF) of the GLSL 1.50 spec:
2850 *
2851 * "A scalar input counts the same amount against this limit as a vec4,
2852 * so applications may want to consider packing groups of four
2853 * unrelated float inputs together into a vector to better utilize the
2854 * capabilities of the underlying hardware. A matrix input will use up
2855 * multiple locations. The number of locations used will equal the
2856 * number of columns in the matrix."
2857 *
2858 * The spec does not explicitly say how arrays are counted. However, it
2859 * should be safe to assume the total number of slots consumed by an array
2860 * is the number of entries in the array multiplied by the number of slots
2861 * consumed by a single element of the array.
2862 *
2863 * The spec says nothing about how structs are counted, because vertex
2864 * attributes are not allowed to be (or contain) structs. However, Mesa
2865 * allows varying structs, the number of varying slots taken up by a
2866 * varying struct is simply equal to the sum of the number of slots taken
2867 * up by each element.
2868 *
2869 * Doubles are counted different depending on whether they are vertex
2870 * inputs or everything else. Vertex inputs from ARB_vertex_attrib_64bit
2871 * take one location no matter what size they are, otherwise dvec3/4
2872 * take two locations.
2873 */
2874 switch (t->base_type) {
2875 case GLSL_TYPE_UINT:
2876 case GLSL_TYPE_INT:
2877 case GLSL_TYPE_UINT8:
2878 case GLSL_TYPE_INT8:
2879 case GLSL_TYPE_UINT16:
2880 case GLSL_TYPE_INT16:
2881 case GLSL_TYPE_FLOAT:
2882 case GLSL_TYPE_FLOAT16:
2883 case GLSL_TYPE_BOOL:
2884 return t->matrix_columns;
2885 case GLSL_TYPE_DOUBLE:
2886 case GLSL_TYPE_UINT64:
2887 case GLSL_TYPE_INT64:
2888 if (t->vector_elements > 2 && !is_gl_vertex_input)
2889 return t->matrix_columns * 2;
2890 else
2891 return t->matrix_columns;
2892 case GLSL_TYPE_STRUCT:
2893 case GLSL_TYPE_INTERFACE: {
2894 unsigned size = 0;
2895
2896 for (unsigned i = 0; i < t->length; i++) {
2897 const glsl_type *member_type = t->fields.structure[i].type;
2898 size += glsl_count_vec4_slots(member_type, is_gl_vertex_input,
2899 is_bindless);
2900 }
2901
2902 return size;
2903 }
2904
2905 case GLSL_TYPE_ARRAY: {
2906 const glsl_type *element = t->fields.array;
2907 return t->length * glsl_count_vec4_slots(element, is_gl_vertex_input,
2908 is_bindless);
2909 }
2910
2911 case GLSL_TYPE_SAMPLER:
2912 case GLSL_TYPE_TEXTURE:
2913 case GLSL_TYPE_IMAGE:
2914 if (!is_bindless)
2915 return 0;
2916 else
2917 return 1;
2918
2919 case GLSL_TYPE_SUBROUTINE:
2920 return 1;
2921
2922 case GLSL_TYPE_COOPERATIVE_MATRIX:
2923 case GLSL_TYPE_ATOMIC_UINT:
2924 case GLSL_TYPE_VOID:
2925 case GLSL_TYPE_ERROR:
2926 break;
2927 }
2928
2929 assert(!"Unexpected type in count_attribute_slots()");
2930
2931 return 0;
2932 }
2933
2934 unsigned
glsl_count_dword_slots(const glsl_type * t,bool is_bindless)2935 glsl_count_dword_slots(const glsl_type *t, bool is_bindless)
2936 {
2937 switch (t->base_type) {
2938 case GLSL_TYPE_UINT:
2939 case GLSL_TYPE_INT:
2940 case GLSL_TYPE_FLOAT:
2941 case GLSL_TYPE_BOOL:
2942 return glsl_get_components(t);
2943 case GLSL_TYPE_UINT16:
2944 case GLSL_TYPE_INT16:
2945 case GLSL_TYPE_FLOAT16:
2946 return DIV_ROUND_UP(t->vector_elements, 2) * t->matrix_columns;
2947 case GLSL_TYPE_UINT8:
2948 case GLSL_TYPE_INT8:
2949 return DIV_ROUND_UP(glsl_get_components(t), 4);
2950 case GLSL_TYPE_IMAGE:
2951 case GLSL_TYPE_SAMPLER:
2952 case GLSL_TYPE_TEXTURE:
2953 if (!is_bindless)
2954 return 0;
2955 FALLTHROUGH;
2956 case GLSL_TYPE_DOUBLE:
2957 case GLSL_TYPE_UINT64:
2958 case GLSL_TYPE_INT64:
2959 return glsl_get_components(t) * 2;
2960 case GLSL_TYPE_ARRAY:
2961 return glsl_count_dword_slots(t->fields.array, is_bindless) *
2962 t->length;
2963
2964 case GLSL_TYPE_INTERFACE:
2965 case GLSL_TYPE_STRUCT: {
2966 unsigned size = 0;
2967 for (unsigned i = 0; i < t->length; i++) {
2968 size += glsl_count_dword_slots(t->fields.structure[i].type,
2969 is_bindless);
2970 }
2971 return size;
2972 }
2973
2974 case GLSL_TYPE_ATOMIC_UINT:
2975 return 0;
2976 case GLSL_TYPE_SUBROUTINE:
2977 return 1;
2978 case GLSL_TYPE_VOID:
2979 case GLSL_TYPE_ERROR:
2980 default:
2981 unreachable("invalid type in st_glsl_type_dword_size()");
2982 }
2983
2984 return 0;
2985 }
2986
2987 int
glsl_get_sampler_coordinate_components(const glsl_type * t)2988 glsl_get_sampler_coordinate_components(const glsl_type *t)
2989 {
2990 assert(glsl_type_is_sampler(t) ||
2991 glsl_type_is_texture(t) ||
2992 glsl_type_is_image(t));
2993
2994 enum glsl_sampler_dim dim = (enum glsl_sampler_dim)t->sampler_dimensionality;
2995 int size = glsl_get_sampler_dim_coordinate_components(dim);
2996
2997 /* Array textures need an additional component for the array index, except
2998 * for cubemap array images that behave like a 2D array of interleaved
2999 * cubemap faces.
3000 */
3001 if (t->sampler_array &&
3002 !(glsl_type_is_image(t) && t->sampler_dimensionality == GLSL_SAMPLER_DIM_CUBE))
3003 size += 1;
3004
3005 return size;
3006 }
3007
3008 union packed_type {
3009 uint32_t u32;
3010 struct {
3011 unsigned base_type:5;
3012 unsigned interface_row_major:1;
3013 unsigned vector_elements:3;
3014 unsigned matrix_columns:3;
3015 unsigned explicit_stride:16;
3016 unsigned explicit_alignment:4;
3017 } basic;
3018 struct {
3019 unsigned base_type:5;
3020 unsigned dimensionality:4;
3021 unsigned shadow:1;
3022 unsigned array:1;
3023 unsigned sampled_type:5;
3024 unsigned _pad:16;
3025 } sampler;
3026 struct {
3027 unsigned base_type:5;
3028 unsigned length:13;
3029 unsigned explicit_stride:14;
3030 } array;
3031 struct glsl_cmat_description cmat_desc;
3032 struct {
3033 unsigned base_type:5;
3034 unsigned interface_packing_or_packed:2;
3035 unsigned interface_row_major:1;
3036 unsigned length:20;
3037 unsigned explicit_alignment:4;
3038 } strct;
3039 };
3040
3041 static void
encode_glsl_struct_field(struct blob * blob,const glsl_struct_field * struct_field)3042 encode_glsl_struct_field(struct blob *blob, const glsl_struct_field *struct_field)
3043 {
3044 encode_type_to_blob(blob, struct_field->type);
3045 blob_write_string(blob, struct_field->name);
3046 blob_write_uint32(blob, struct_field->location);
3047 blob_write_uint32(blob, struct_field->component);
3048 blob_write_uint32(blob, struct_field->offset);
3049 blob_write_uint32(blob, struct_field->xfb_buffer);
3050 blob_write_uint32(blob, struct_field->xfb_stride);
3051 blob_write_uint32(blob, struct_field->image_format);
3052 blob_write_uint32(blob, struct_field->flags);
3053 }
3054
3055 static void
decode_glsl_struct_field_from_blob(struct blob_reader * blob,glsl_struct_field * struct_field)3056 decode_glsl_struct_field_from_blob(struct blob_reader *blob, glsl_struct_field *struct_field)
3057 {
3058 struct_field->type = decode_type_from_blob(blob);
3059 struct_field->name = blob_read_string(blob);
3060 struct_field->location = blob_read_uint32(blob);
3061 struct_field->component = blob_read_uint32(blob);
3062 struct_field->offset = blob_read_uint32(blob);
3063 struct_field->xfb_buffer = blob_read_uint32(blob);
3064 struct_field->xfb_stride = blob_read_uint32(blob);
3065 struct_field->image_format = (enum pipe_format)blob_read_uint32(blob);
3066 struct_field->flags = blob_read_uint32(blob);
3067 }
3068
3069 void
encode_type_to_blob(struct blob * blob,const glsl_type * type)3070 encode_type_to_blob(struct blob *blob, const glsl_type *type)
3071 {
3072 if (!type) {
3073 blob_write_uint32(blob, 0);
3074 return;
3075 }
3076
3077 STATIC_ASSERT(sizeof(union packed_type) == 4);
3078 union packed_type encoded;
3079 encoded.u32 = 0;
3080 encoded.basic.base_type = type->base_type;
3081
3082 switch (type->base_type) {
3083 case GLSL_TYPE_UINT:
3084 case GLSL_TYPE_INT:
3085 case GLSL_TYPE_FLOAT:
3086 case GLSL_TYPE_FLOAT16:
3087 case GLSL_TYPE_DOUBLE:
3088 case GLSL_TYPE_UINT8:
3089 case GLSL_TYPE_INT8:
3090 case GLSL_TYPE_UINT16:
3091 case GLSL_TYPE_INT16:
3092 case GLSL_TYPE_UINT64:
3093 case GLSL_TYPE_INT64:
3094 case GLSL_TYPE_BOOL:
3095 encoded.basic.interface_row_major = type->interface_row_major;
3096 assert(type->matrix_columns < 8);
3097 if (type->vector_elements <= 5)
3098 encoded.basic.vector_elements = type->vector_elements;
3099 else if (type->vector_elements == 8)
3100 encoded.basic.vector_elements = 6;
3101 else if (type->vector_elements == 16)
3102 encoded.basic.vector_elements = 7;
3103 encoded.basic.matrix_columns = type->matrix_columns;
3104 encoded.basic.explicit_stride = MIN2(type->explicit_stride, 0xffff);
3105 encoded.basic.explicit_alignment =
3106 MIN2(ffs(type->explicit_alignment), 0xf);
3107 blob_write_uint32(blob, encoded.u32);
3108 /* If we don't have enough bits for explicit_stride, store it
3109 * separately.
3110 */
3111 if (encoded.basic.explicit_stride == 0xffff)
3112 blob_write_uint32(blob, type->explicit_stride);
3113 if (encoded.basic.explicit_alignment == 0xf)
3114 blob_write_uint32(blob, type->explicit_alignment);
3115 return;
3116 case GLSL_TYPE_SAMPLER:
3117 case GLSL_TYPE_TEXTURE:
3118 case GLSL_TYPE_IMAGE:
3119 encoded.sampler.dimensionality = type->sampler_dimensionality;
3120 if (type->base_type == GLSL_TYPE_SAMPLER)
3121 encoded.sampler.shadow = type->sampler_shadow;
3122 else
3123 assert(!type->sampler_shadow);
3124 encoded.sampler.array = type->sampler_array;
3125 encoded.sampler.sampled_type = type->sampled_type;
3126 break;
3127 case GLSL_TYPE_SUBROUTINE:
3128 blob_write_uint32(blob, encoded.u32);
3129 blob_write_string(blob, glsl_get_type_name(type));
3130 return;
3131 case GLSL_TYPE_ATOMIC_UINT:
3132 break;
3133 case GLSL_TYPE_ARRAY:
3134 encoded.array.length = MIN2(type->length, 0x1fff);
3135 encoded.array.explicit_stride = MIN2(type->explicit_stride, 0x3fff);
3136 blob_write_uint32(blob, encoded.u32);
3137 /* If we don't have enough bits for length or explicit_stride, store it
3138 * separately.
3139 */
3140 if (encoded.array.length == 0x1fff)
3141 blob_write_uint32(blob, type->length);
3142 if (encoded.array.explicit_stride == 0x3fff)
3143 blob_write_uint32(blob, type->explicit_stride);
3144 encode_type_to_blob(blob, type->fields.array);
3145 return;
3146 case GLSL_TYPE_COOPERATIVE_MATRIX:
3147 /* The first 5 bits of encoded/decoded are used to identify the
3148 * actual type, but cmat_desc already is 32-bit without that tag, so
3149 * encode just the cmat base type first, then the actual cmat desc.
3150 */
3151 blob_write_uint32(blob, encoded.u32);
3152 encoded.cmat_desc = type->cmat_desc;
3153 blob_write_uint32(blob, encoded.u32);
3154 return;
3155 case GLSL_TYPE_STRUCT:
3156 case GLSL_TYPE_INTERFACE:
3157 encoded.strct.length = MIN2(type->length, 0xfffff);
3158 encoded.strct.explicit_alignment =
3159 MIN2(ffs(type->explicit_alignment), 0xf);
3160 if (glsl_type_is_interface(type)) {
3161 encoded.strct.interface_packing_or_packed = type->interface_packing;
3162 encoded.strct.interface_row_major = type->interface_row_major;
3163 } else {
3164 encoded.strct.interface_packing_or_packed = type->packed;
3165 }
3166 blob_write_uint32(blob, encoded.u32);
3167 blob_write_string(blob, glsl_get_type_name(type));
3168
3169 /* If we don't have enough bits for length, store it separately. */
3170 if (encoded.strct.length == 0xfffff)
3171 blob_write_uint32(blob, type->length);
3172 if (encoded.strct.explicit_alignment == 0xf)
3173 blob_write_uint32(blob, type->explicit_alignment);
3174
3175 for (unsigned i = 0; i < type->length; i++)
3176 encode_glsl_struct_field(blob, &type->fields.structure[i]);
3177 return;
3178 case GLSL_TYPE_VOID:
3179 break;
3180 case GLSL_TYPE_ERROR:
3181 default:
3182 assert(!"Cannot encode type!");
3183 encoded.u32 = 0;
3184 break;
3185 }
3186
3187 blob_write_uint32(blob, encoded.u32);
3188 }
3189
3190 const glsl_type *
decode_type_from_blob(struct blob_reader * blob)3191 decode_type_from_blob(struct blob_reader *blob)
3192 {
3193 union packed_type encoded;
3194 encoded.u32 = blob_read_uint32(blob);
3195
3196 if (encoded.u32 == 0) {
3197 return NULL;
3198 }
3199
3200 enum glsl_base_type base_type = (enum glsl_base_type)encoded.basic.base_type;
3201
3202 switch (base_type) {
3203 case GLSL_TYPE_UINT:
3204 case GLSL_TYPE_INT:
3205 case GLSL_TYPE_FLOAT:
3206 case GLSL_TYPE_FLOAT16:
3207 case GLSL_TYPE_DOUBLE:
3208 case GLSL_TYPE_UINT8:
3209 case GLSL_TYPE_INT8:
3210 case GLSL_TYPE_UINT16:
3211 case GLSL_TYPE_INT16:
3212 case GLSL_TYPE_UINT64:
3213 case GLSL_TYPE_INT64:
3214 case GLSL_TYPE_BOOL: {
3215 unsigned explicit_stride = encoded.basic.explicit_stride;
3216 if (explicit_stride == 0xffff)
3217 explicit_stride = blob_read_uint32(blob);
3218 unsigned explicit_alignment = encoded.basic.explicit_alignment;
3219 if (explicit_alignment == 0xf)
3220 explicit_alignment = blob_read_uint32(blob);
3221 else if (explicit_alignment > 0)
3222 explicit_alignment = 1 << (explicit_alignment - 1);
3223 uint32_t vector_elements = encoded.basic.vector_elements;
3224 if (vector_elements == 6)
3225 vector_elements = 8;
3226 else if (vector_elements == 7)
3227 vector_elements = 16;
3228 return glsl_simple_explicit_type(base_type, vector_elements,
3229 encoded.basic.matrix_columns,
3230 explicit_stride,
3231 encoded.basic.interface_row_major,
3232 explicit_alignment);
3233 }
3234 case GLSL_TYPE_SAMPLER:
3235 return glsl_sampler_type((enum glsl_sampler_dim)encoded.sampler.dimensionality,
3236 encoded.sampler.shadow,
3237 encoded.sampler.array,
3238 (enum glsl_base_type) encoded.sampler.sampled_type);
3239 case GLSL_TYPE_TEXTURE:
3240 return glsl_texture_type((enum glsl_sampler_dim)encoded.sampler.dimensionality,
3241 encoded.sampler.array,
3242 (enum glsl_base_type) encoded.sampler.sampled_type);
3243 case GLSL_TYPE_SUBROUTINE:
3244 return glsl_subroutine_type(blob_read_string(blob));
3245 case GLSL_TYPE_IMAGE:
3246 return glsl_image_type((enum glsl_sampler_dim)encoded.sampler.dimensionality,
3247 encoded.sampler.array,
3248 (enum glsl_base_type) encoded.sampler.sampled_type);
3249 case GLSL_TYPE_ATOMIC_UINT:
3250 return &glsl_type_builtin_atomic_uint;
3251 case GLSL_TYPE_ARRAY: {
3252 unsigned length = encoded.array.length;
3253 if (length == 0x1fff)
3254 length = blob_read_uint32(blob);
3255 unsigned explicit_stride = encoded.array.explicit_stride;
3256 if (explicit_stride == 0x3fff)
3257 explicit_stride = blob_read_uint32(blob);
3258 return glsl_array_type(decode_type_from_blob(blob), length,
3259 explicit_stride);
3260 }
3261 case GLSL_TYPE_COOPERATIVE_MATRIX: {
3262 encoded.u32 = blob_read_uint32(blob);
3263 return glsl_cmat_type(&encoded.cmat_desc);
3264 }
3265 case GLSL_TYPE_STRUCT:
3266 case GLSL_TYPE_INTERFACE: {
3267 char *name = blob_read_string(blob);
3268 unsigned num_fields = encoded.strct.length;
3269 if (num_fields == 0xfffff)
3270 num_fields = blob_read_uint32(blob);
3271 unsigned explicit_alignment = encoded.strct.explicit_alignment;
3272 if (explicit_alignment == 0xf)
3273 explicit_alignment = blob_read_uint32(blob);
3274 else if (explicit_alignment > 0)
3275 explicit_alignment = 1 << (explicit_alignment - 1);
3276
3277 glsl_struct_field *fields = (glsl_struct_field *)
3278 malloc(sizeof(glsl_struct_field) * num_fields);
3279 for (unsigned i = 0; i < num_fields; i++)
3280 decode_glsl_struct_field_from_blob(blob, &fields[i]);
3281
3282 const glsl_type *t;
3283 if (base_type == GLSL_TYPE_INTERFACE) {
3284 assert(explicit_alignment == 0);
3285 enum glsl_interface_packing packing =
3286 (enum glsl_interface_packing) encoded.strct.interface_packing_or_packed;
3287 bool row_major = encoded.strct.interface_row_major;
3288 t = glsl_interface_type(fields, num_fields, packing, row_major, name);
3289 } else {
3290 unsigned packed = encoded.strct.interface_packing_or_packed;
3291 t = glsl_struct_type_with_explicit_alignment(fields, num_fields,
3292 name, packed,
3293 explicit_alignment);
3294 }
3295
3296 free(fields);
3297 return t;
3298 }
3299 case GLSL_TYPE_VOID:
3300 return &glsl_type_builtin_void;
3301 case GLSL_TYPE_ERROR:
3302 default:
3303 assert(!"Cannot decode type!");
3304 return NULL;
3305 }
3306 }
3307
3308 unsigned
glsl_get_cl_alignment(const glsl_type * t)3309 glsl_get_cl_alignment(const glsl_type *t)
3310 {
3311 /* vectors unlike arrays are aligned to their size */
3312 if (glsl_type_is_scalar(t) || glsl_type_is_vector(t))
3313 return glsl_get_cl_size(t);
3314 else if (glsl_type_is_array(t))
3315 return glsl_get_cl_alignment(t->fields.array);
3316 else if (glsl_type_is_struct(t)) {
3317 /* Packed Structs are 0x1 aligned despite their size. */
3318 if (t->packed)
3319 return 1;
3320
3321 unsigned res = 1;
3322 for (unsigned i = 0; i < t->length; ++i) {
3323 const glsl_struct_field *field = &t->fields.structure[i];
3324 res = MAX2(res, glsl_get_cl_alignment(field->type));
3325 }
3326 return res;
3327 }
3328 return 1;
3329 }
3330
3331 unsigned
glsl_get_cl_size(const glsl_type * t)3332 glsl_get_cl_size(const glsl_type *t)
3333 {
3334 if (glsl_type_is_scalar(t) || glsl_type_is_vector(t)) {
3335 return util_next_power_of_two(t->vector_elements) *
3336 explicit_type_scalar_byte_size(t);
3337 } else if (glsl_type_is_array(t)) {
3338 unsigned size = glsl_get_cl_size(t->fields.array);
3339 return size * t->length;
3340 } else if (glsl_type_is_struct(t)) {
3341 unsigned size = 0;
3342 unsigned max_alignment = 1;
3343 for (unsigned i = 0; i < t->length; ++i) {
3344 const glsl_struct_field *field = &t->fields.structure[i];
3345 /* if a struct is packed, members don't get aligned */
3346 if (!t->packed) {
3347 unsigned alignment = glsl_get_cl_alignment(field->type);
3348 max_alignment = MAX2(max_alignment, alignment);
3349 size = align(size, alignment);
3350 }
3351 size += glsl_get_cl_size(field->type);
3352 }
3353
3354 /* Size of C structs are aligned to the biggest alignment of its fields */
3355 size = align(size, max_alignment);
3356 return size;
3357 }
3358 return 1;
3359 }
3360
3361 extern const char glsl_type_builtin_names[];
3362
3363 const char *
glsl_get_type_name(const glsl_type * type)3364 glsl_get_type_name(const glsl_type *type)
3365 {
3366 if (type->has_builtin_name) {
3367 return &glsl_type_builtin_names[type->name_id];
3368 } else {
3369 return (const char *) type->name_id;
3370 }
3371 }
3372
3373 void
glsl_get_cl_type_size_align(const glsl_type * t,unsigned * size,unsigned * align)3374 glsl_get_cl_type_size_align(const glsl_type *t,
3375 unsigned *size, unsigned *align)
3376 {
3377 *size = glsl_get_cl_size(t);
3378 *align = glsl_get_cl_alignment(t);
3379 }
3380
3381 int
glsl_get_sampler_dim_coordinate_components(enum glsl_sampler_dim dim)3382 glsl_get_sampler_dim_coordinate_components(enum glsl_sampler_dim dim)
3383 {
3384 switch (dim) {
3385 case GLSL_SAMPLER_DIM_1D:
3386 case GLSL_SAMPLER_DIM_BUF:
3387 return 1;
3388 case GLSL_SAMPLER_DIM_2D:
3389 case GLSL_SAMPLER_DIM_RECT:
3390 case GLSL_SAMPLER_DIM_MS:
3391 case GLSL_SAMPLER_DIM_EXTERNAL:
3392 case GLSL_SAMPLER_DIM_SUBPASS:
3393 case GLSL_SAMPLER_DIM_SUBPASS_MS:
3394 return 2;
3395 case GLSL_SAMPLER_DIM_3D:
3396 case GLSL_SAMPLER_DIM_CUBE:
3397 return 3;
3398 default:
3399 unreachable("Unknown sampler dim");
3400 }
3401 }
3402
3403 bool
glsl_type_is_vector(const glsl_type * t)3404 glsl_type_is_vector(const glsl_type *t)
3405 {
3406 return t->vector_elements > 1 &&
3407 t->matrix_columns == 1 &&
3408 t->base_type >= GLSL_TYPE_UINT &&
3409 t->base_type <= GLSL_TYPE_BOOL;
3410 }
3411
3412 bool
glsl_type_is_scalar(const glsl_type * t)3413 glsl_type_is_scalar(const glsl_type *t)
3414 {
3415 return t->vector_elements == 1 &&
3416 t->base_type >= GLSL_TYPE_UINT &&
3417 t->base_type <= GLSL_TYPE_IMAGE;
3418 }
3419
3420 bool
glsl_type_is_vector_or_scalar(const glsl_type * t)3421 glsl_type_is_vector_or_scalar(const glsl_type *t)
3422 {
3423 return glsl_type_is_vector(t) || glsl_type_is_scalar(t);
3424 }
3425
3426 bool
glsl_type_is_matrix(const glsl_type * t)3427 glsl_type_is_matrix(const glsl_type *t)
3428 {
3429 /* GLSL only has float matrices. */
3430 return t->matrix_columns > 1 && (t->base_type == GLSL_TYPE_FLOAT ||
3431 t->base_type == GLSL_TYPE_DOUBLE ||
3432 t->base_type == GLSL_TYPE_FLOAT16);
3433 }
3434
3435 bool
glsl_type_is_array_or_matrix(const glsl_type * t)3436 glsl_type_is_array_or_matrix(const glsl_type *t)
3437 {
3438 return glsl_type_is_array(t) || glsl_type_is_matrix(t);
3439 }
3440
3441 bool
glsl_type_is_dual_slot(const glsl_type * t)3442 glsl_type_is_dual_slot(const glsl_type *t)
3443 {
3444 return glsl_type_is_64bit(t) && t->vector_elements > 2;
3445 }
3446
3447 const glsl_type *
glsl_get_array_element(const glsl_type * t)3448 glsl_get_array_element(const glsl_type *t)
3449 {
3450 if (glsl_type_is_matrix(t))
3451 return glsl_get_column_type(t);
3452 else if (glsl_type_is_vector(t))
3453 return glsl_get_scalar_type(t);
3454 return t->fields.array;
3455 }
3456
3457 bool
glsl_type_is_leaf(const glsl_type * t)3458 glsl_type_is_leaf(const glsl_type *t)
3459 {
3460 if (glsl_type_is_struct_or_ifc(t) ||
3461 (glsl_type_is_array(t) &&
3462 (glsl_type_is_array(glsl_get_array_element(t)) ||
3463 glsl_type_is_struct_or_ifc(glsl_get_array_element(t))))) {
3464 return false;
3465 } else {
3466 return true;
3467 }
3468 }
3469
3470 bool
glsl_contains_atomic(const glsl_type * t)3471 glsl_contains_atomic(const glsl_type *t)
3472 {
3473 return glsl_atomic_size(t) > 0;
3474 }
3475
3476 const glsl_type *
glsl_without_array(const glsl_type * t)3477 glsl_without_array(const glsl_type *t)
3478 {
3479 while (glsl_type_is_array(t))
3480 t = t->fields.array;
3481 return t;
3482 }
3483
3484 const glsl_type *
glsl_without_array_or_matrix(const glsl_type * t)3485 glsl_without_array_or_matrix(const glsl_type *t)
3486 {
3487 t = glsl_without_array(t);
3488 if (glsl_type_is_matrix(t))
3489 t = glsl_get_column_type(t);
3490 return t;
3491 }
3492
3493 const glsl_type *
glsl_type_wrap_in_arrays(const glsl_type * t,const glsl_type * arrays)3494 glsl_type_wrap_in_arrays(const glsl_type *t,
3495 const glsl_type *arrays)
3496 {
3497 if (!glsl_type_is_array(arrays))
3498 return t;
3499
3500 const glsl_type *elem_type =
3501 glsl_type_wrap_in_arrays(t, glsl_get_array_element(arrays));
3502 return glsl_array_type(elem_type, glsl_get_length(arrays),
3503 glsl_get_explicit_stride(arrays));
3504 }
3505
3506 const glsl_type *
glsl_get_cmat_element(const glsl_type * t)3507 glsl_get_cmat_element(const glsl_type *t)
3508 {
3509 assert(t->base_type == GLSL_TYPE_COOPERATIVE_MATRIX);
3510 return glsl_simple_type(t->cmat_desc.element_type, 1, 1);
3511 }
3512
3513 const struct glsl_cmat_description *
glsl_get_cmat_description(const glsl_type * t)3514 glsl_get_cmat_description(const glsl_type *t)
3515 {
3516 assert(t->base_type == GLSL_TYPE_COOPERATIVE_MATRIX);
3517 return &t->cmat_desc;
3518 }
3519
3520 unsigned
glsl_get_length(const glsl_type * t)3521 glsl_get_length(const glsl_type *t)
3522 {
3523 if (glsl_type_is_matrix(t))
3524 return t->matrix_columns;
3525 else if (glsl_type_is_vector(t))
3526 return t->vector_elements;
3527 return t->length;
3528 }
3529
3530 unsigned
glsl_get_aoa_size(const glsl_type * t)3531 glsl_get_aoa_size(const glsl_type *t)
3532 {
3533 if (!glsl_type_is_array(t))
3534 return 0;
3535
3536 unsigned size = t->length;
3537 const glsl_type *array_base_type = t->fields.array;
3538
3539 while (glsl_type_is_array(array_base_type)) {
3540 size = size * array_base_type->length;
3541 array_base_type = array_base_type->fields.array;
3542 }
3543 return size;
3544 }
3545
3546 const glsl_type *
glsl_get_struct_field(const glsl_type * t,unsigned index)3547 glsl_get_struct_field(const glsl_type *t, unsigned index)
3548 {
3549 assert(glsl_type_is_struct(t) || glsl_type_is_interface(t));
3550 assert(index < t->length);
3551 return t->fields.structure[index].type;
3552 }
3553
3554 const glsl_struct_field *
glsl_get_struct_field_data(const glsl_type * t,unsigned index)3555 glsl_get_struct_field_data(const glsl_type *t, unsigned index)
3556 {
3557 assert(glsl_type_is_struct(t) || glsl_type_is_interface(t));
3558 assert(index < t->length);
3559 return &t->fields.structure[index];
3560 }
3561
3562 enum glsl_interface_packing
glsl_get_internal_ifc_packing(const glsl_type * t,bool std430_supported)3563 glsl_get_internal_ifc_packing(const glsl_type *t,
3564 bool std430_supported)
3565 {
3566 enum glsl_interface_packing packing = glsl_get_ifc_packing(t);
3567 if (packing == GLSL_INTERFACE_PACKING_STD140 ||
3568 (!std430_supported &&
3569 (packing == GLSL_INTERFACE_PACKING_SHARED ||
3570 packing == GLSL_INTERFACE_PACKING_PACKED))) {
3571 return GLSL_INTERFACE_PACKING_STD140;
3572 } else {
3573 assert(packing == GLSL_INTERFACE_PACKING_STD430 ||
3574 (std430_supported &&
3575 (packing == GLSL_INTERFACE_PACKING_SHARED ||
3576 packing == GLSL_INTERFACE_PACKING_PACKED)));
3577 return GLSL_INTERFACE_PACKING_STD430;
3578 }
3579 }
3580
3581 const glsl_type *
glsl_get_row_type(const glsl_type * t)3582 glsl_get_row_type(const glsl_type *t)
3583 {
3584 if (!glsl_type_is_matrix(t))
3585 return &glsl_type_builtin_error;
3586
3587 if (t->explicit_stride && !t->interface_row_major)
3588 return glsl_simple_explicit_type(t->base_type, t->matrix_columns, 1,
3589 t->explicit_stride, false, 0);
3590 else
3591 return glsl_simple_type(t->base_type, t->matrix_columns, 1);
3592 }
3593
3594 const glsl_type *
glsl_get_column_type(const glsl_type * t)3595 glsl_get_column_type(const glsl_type *t)
3596 {
3597 if (!glsl_type_is_matrix(t))
3598 return &glsl_type_builtin_error;
3599
3600 if (t->interface_row_major) {
3601 /* If we're row-major, the vector element stride is the same as the
3602 * matrix stride and we have no alignment (i.e. component-aligned).
3603 */
3604 return glsl_simple_explicit_type(t->base_type, t->vector_elements, 1,
3605 t->explicit_stride, false, 0);
3606 } else {
3607 /* Otherwise, the vector is tightly packed (stride=0). For
3608 * alignment, we treat a matrix as an array of columns make the same
3609 * assumption that the alignment of the column is the same as the
3610 * alignment of the whole matrix.
3611 */
3612 return glsl_simple_explicit_type(t->base_type, t->vector_elements, 1, 0,
3613 false, t->explicit_alignment);
3614 }
3615 }
3616
3617 unsigned
glsl_atomic_size(const glsl_type * t)3618 glsl_atomic_size(const glsl_type *t)
3619 {
3620 if (glsl_type_is_atomic_uint(t))
3621 return 4; /* ATOMIC_COUNTER_SIZE */
3622 else if (glsl_type_is_array(t))
3623 return t->length * glsl_atomic_size(t->fields.array);
3624 else
3625 return 0;
3626 }
3627
3628 const glsl_type *
glsl_type_to_16bit(const glsl_type * old_type)3629 glsl_type_to_16bit(const glsl_type *old_type)
3630 {
3631 if (glsl_type_is_array(old_type)) {
3632 return glsl_array_type(glsl_type_to_16bit(glsl_get_array_element(old_type)),
3633 glsl_get_length(old_type),
3634 glsl_get_explicit_stride(old_type));
3635 }
3636
3637 if (glsl_type_is_vector_or_scalar(old_type)) {
3638 switch (glsl_get_base_type(old_type)) {
3639 case GLSL_TYPE_FLOAT:
3640 return glsl_float16_type(old_type);
3641 case GLSL_TYPE_UINT:
3642 return glsl_uint16_type(old_type);
3643 case GLSL_TYPE_INT:
3644 return glsl_int16_type(old_type);
3645 default:
3646 break;
3647 }
3648 }
3649
3650 return old_type;
3651 }
3652
3653 const glsl_type *
glsl_replace_vector_type(const glsl_type * t,unsigned components)3654 glsl_replace_vector_type(const glsl_type *t, unsigned components)
3655 {
3656 if (glsl_type_is_array(t)) {
3657 return glsl_array_type(
3658 glsl_replace_vector_type(t->fields.array, components), t->length,
3659 t->explicit_stride);
3660 } else if (glsl_type_is_vector_or_scalar(t)) {
3661 return glsl_vector_type(t->base_type, components);
3662 } else {
3663 unreachable("Unhandled base type glsl_replace_vector_type()");
3664 }
3665 }
3666
3667 const glsl_type *
glsl_channel_type(const glsl_type * t)3668 glsl_channel_type(const glsl_type *t)
3669 {
3670 switch (t->base_type) {
3671 case GLSL_TYPE_ARRAY:
3672 return glsl_array_type(glsl_channel_type(t->fields.array), t->length,
3673 t->explicit_stride);
3674 case GLSL_TYPE_UINT:
3675 case GLSL_TYPE_INT:
3676 case GLSL_TYPE_FLOAT:
3677 case GLSL_TYPE_FLOAT16:
3678 case GLSL_TYPE_DOUBLE:
3679 case GLSL_TYPE_UINT8:
3680 case GLSL_TYPE_INT8:
3681 case GLSL_TYPE_UINT16:
3682 case GLSL_TYPE_INT16:
3683 case GLSL_TYPE_UINT64:
3684 case GLSL_TYPE_INT64:
3685 case GLSL_TYPE_BOOL:
3686 return glsl_simple_type(t->base_type, 1, 1);
3687 default:
3688 unreachable("Unhandled base type glsl_channel_type()");
3689 }
3690 }
3691
3692 void
glsl_size_align_handle_array_and_structs(const glsl_type * type,glsl_type_size_align_func size_align,unsigned * size,unsigned * align)3693 glsl_size_align_handle_array_and_structs(const glsl_type *type,
3694 glsl_type_size_align_func size_align,
3695 unsigned *size, unsigned *align)
3696 {
3697 if (type->base_type == GLSL_TYPE_ARRAY) {
3698 unsigned elem_size = 0, elem_align = 0;
3699 size_align(type->fields.array, &elem_size, &elem_align);
3700 *align = elem_align;
3701 *size = type->length * ALIGN_POT(elem_size, elem_align);
3702 } else {
3703 assert(type->base_type == GLSL_TYPE_STRUCT ||
3704 type->base_type == GLSL_TYPE_INTERFACE);
3705
3706 *size = 0;
3707 *align = 0;
3708 for (unsigned i = 0; i < type->length; i++) {
3709 unsigned elem_size = 0, elem_align = 0;
3710 size_align(type->fields.structure[i].type, &elem_size, &elem_align);
3711 *align = MAX2(*align, elem_align);
3712 *size = ALIGN_POT(*size, elem_align) + elem_size;
3713 }
3714 }
3715 }
3716
3717 void
glsl_get_natural_size_align_bytes(const glsl_type * type,unsigned * size,unsigned * align)3718 glsl_get_natural_size_align_bytes(const glsl_type *type,
3719 unsigned *size, unsigned *align)
3720 {
3721 switch (type->base_type) {
3722 case GLSL_TYPE_BOOL:
3723 /* We special-case Booleans to 32 bits to not cause heartburn for
3724 * drivers that suddenly get an 8-bit load.
3725 */
3726 *size = 4 * glsl_get_components(type);
3727 *align = 4;
3728 break;
3729
3730 case GLSL_TYPE_UINT8:
3731 case GLSL_TYPE_INT8:
3732 case GLSL_TYPE_UINT16:
3733 case GLSL_TYPE_INT16:
3734 case GLSL_TYPE_FLOAT16:
3735 case GLSL_TYPE_UINT:
3736 case GLSL_TYPE_INT:
3737 case GLSL_TYPE_FLOAT:
3738 case GLSL_TYPE_DOUBLE:
3739 case GLSL_TYPE_UINT64:
3740 case GLSL_TYPE_INT64: {
3741 unsigned N = glsl_get_bit_size(type) / 8;
3742 *size = N * glsl_get_components(type);
3743 *align = N;
3744 break;
3745 }
3746
3747 case GLSL_TYPE_ARRAY:
3748 case GLSL_TYPE_INTERFACE:
3749 case GLSL_TYPE_STRUCT:
3750 glsl_size_align_handle_array_and_structs(type,
3751 glsl_get_natural_size_align_bytes,
3752 size, align);
3753 break;
3754
3755 case GLSL_TYPE_SAMPLER:
3756 case GLSL_TYPE_TEXTURE:
3757 case GLSL_TYPE_IMAGE:
3758 /* Bindless samplers and images. */
3759 *size = 8;
3760 *align = 8;
3761 break;
3762
3763 case GLSL_TYPE_COOPERATIVE_MATRIX:
3764 case GLSL_TYPE_ATOMIC_UINT:
3765 case GLSL_TYPE_SUBROUTINE:
3766 case GLSL_TYPE_VOID:
3767 case GLSL_TYPE_ERROR:
3768 unreachable("type does not have a natural size");
3769 }
3770 }
3771
3772 /**
3773 * Returns a byte size/alignment for a type where each array element or struct
3774 * field is aligned to 16 bytes.
3775 */
3776 void
glsl_get_vec4_size_align_bytes(const glsl_type * type,unsigned * size,unsigned * align)3777 glsl_get_vec4_size_align_bytes(const glsl_type *type,
3778 unsigned *size, unsigned *align)
3779 {
3780 switch (type->base_type) {
3781 case GLSL_TYPE_BOOL:
3782 /* We special-case Booleans to 32 bits to not cause heartburn for
3783 * drivers that suddenly get an 8-bit load.
3784 */
3785 *size = 4 * glsl_get_components(type);
3786 *align = 16;
3787 break;
3788
3789 case GLSL_TYPE_UINT8:
3790 case GLSL_TYPE_INT8:
3791 case GLSL_TYPE_UINT16:
3792 case GLSL_TYPE_INT16:
3793 case GLSL_TYPE_FLOAT16:
3794 case GLSL_TYPE_UINT:
3795 case GLSL_TYPE_INT:
3796 case GLSL_TYPE_FLOAT:
3797 case GLSL_TYPE_DOUBLE:
3798 case GLSL_TYPE_UINT64:
3799 case GLSL_TYPE_INT64: {
3800 unsigned N = glsl_get_bit_size(type) / 8;
3801 *size = 16 * (type->matrix_columns - 1) + N * type->vector_elements;
3802 *align = 16;
3803 break;
3804 }
3805
3806 case GLSL_TYPE_ARRAY:
3807 case GLSL_TYPE_INTERFACE:
3808 case GLSL_TYPE_STRUCT:
3809 glsl_size_align_handle_array_and_structs(type,
3810 glsl_get_vec4_size_align_bytes,
3811 size, align);
3812 break;
3813
3814 case GLSL_TYPE_SAMPLER:
3815 case GLSL_TYPE_TEXTURE:
3816 case GLSL_TYPE_IMAGE:
3817 case GLSL_TYPE_COOPERATIVE_MATRIX:
3818 case GLSL_TYPE_ATOMIC_UINT:
3819 case GLSL_TYPE_SUBROUTINE:
3820 case GLSL_TYPE_VOID:
3821 case GLSL_TYPE_ERROR:
3822 unreachable("type does not make sense for glsl_get_vec4_size_align_bytes()");
3823 }
3824 }
3825
3826 static unsigned
glsl_type_count(const glsl_type * type,enum glsl_base_type base_type)3827 glsl_type_count(const glsl_type *type, enum glsl_base_type base_type)
3828 {
3829 if (glsl_type_is_array(type)) {
3830 return glsl_get_length(type) *
3831 glsl_type_count(glsl_get_array_element(type), base_type);
3832 }
3833
3834 /* Ignore interface blocks - they can only contain bindless samplers,
3835 * which we shouldn't count.
3836 */
3837 if (glsl_type_is_struct(type)) {
3838 unsigned count = 0;
3839 for (unsigned i = 0; i < glsl_get_length(type); i++)
3840 count += glsl_type_count(glsl_get_struct_field(type, i), base_type);
3841 return count;
3842 }
3843
3844 if (glsl_get_base_type(type) == base_type)
3845 return 1;
3846
3847 return 0;
3848 }
3849
3850 unsigned
glsl_type_get_sampler_count(const glsl_type * type)3851 glsl_type_get_sampler_count(const glsl_type *type)
3852 {
3853 return glsl_type_count(type, GLSL_TYPE_SAMPLER);
3854 }
3855
3856 unsigned
glsl_type_get_texture_count(const glsl_type * type)3857 glsl_type_get_texture_count(const glsl_type *type)
3858 {
3859 return glsl_type_count(type, GLSL_TYPE_TEXTURE);
3860 }
3861
3862 unsigned
glsl_type_get_image_count(const glsl_type * type)3863 glsl_type_get_image_count(const glsl_type *type)
3864 {
3865 return glsl_type_count(type, GLSL_TYPE_IMAGE);
3866 }
3867