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