1 /* -*- c++ -*- */
2 /*
3 * Copyright © 2009 Intel Corporation
4 *
5 * Permission is hereby granted, free of charge, to any person obtaining a
6 * copy of this software and associated documentation files (the "Software"),
7 * to deal in the Software without restriction, including without limitation
8 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
9 * and/or sell copies of the Software, and to permit persons to whom the
10 * Software is furnished to do so, subject to the following conditions:
11 *
12 * The above copyright notice and this permission notice (including the next
13 * paragraph) shall be included in all copies or substantial portions of the
14 * Software.
15 *
16 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
19 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
21 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
22 * DEALINGS IN THE SOFTWARE.
23 */
24
25 #ifndef GLSL_TYPES_H
26 #define GLSL_TYPES_H
27
28 #include <string.h>
29 #include <assert.h>
30 #include <stdio.h>
31
32 #include "shader_enums.h"
33 #include "c11/threads.h"
34 #include "util/blob.h"
35 #include "util/format/u_format.h"
36 #include "util/macros.h"
37
38 #ifdef __cplusplus
39 extern "C" {
40 #endif
41
42 typedef struct glsl_type glsl_type;
43 typedef struct glsl_struct_field glsl_struct_field;
44
45 extern void
46 glsl_type_singleton_init_or_ref(void);
47
48 extern void
49 glsl_type_singleton_decref(void);
50
51 void encode_type_to_blob(struct blob *blob, const glsl_type *type);
52
53 const glsl_type *decode_type_from_blob(struct blob_reader *blob);
54
55 typedef void (*glsl_type_size_align_func)(const glsl_type *type,
56 unsigned *size, unsigned *alignment);
57
58 enum glsl_base_type {
59 /* Note: GLSL_TYPE_UINT, GLSL_TYPE_INT, and GLSL_TYPE_FLOAT must be 0, 1,
60 * and 2 so that they will fit in the 2 bits of glsl_type::sampled_type.
61 */
62 GLSL_TYPE_UINT = 0,
63 GLSL_TYPE_INT,
64 GLSL_TYPE_FLOAT,
65 GLSL_TYPE_FLOAT16,
66 GLSL_TYPE_DOUBLE,
67 GLSL_TYPE_UINT8,
68 GLSL_TYPE_INT8,
69 GLSL_TYPE_UINT16,
70 GLSL_TYPE_INT16,
71 GLSL_TYPE_UINT64,
72 GLSL_TYPE_INT64,
73 GLSL_TYPE_BOOL,
74 GLSL_TYPE_COOPERATIVE_MATRIX,
75 GLSL_TYPE_SAMPLER,
76 GLSL_TYPE_TEXTURE,
77 GLSL_TYPE_IMAGE,
78 GLSL_TYPE_ATOMIC_UINT,
79 GLSL_TYPE_STRUCT,
80 GLSL_TYPE_INTERFACE,
81 GLSL_TYPE_ARRAY,
82 GLSL_TYPE_VOID,
83 GLSL_TYPE_SUBROUTINE,
84 GLSL_TYPE_ERROR
85 };
86
87 /* Return the bit size of a type. Note that this differs from
88 * glsl_get_bit_size in that it returns 32 bits for bools, whereas at
89 * the NIR level we would want to return 1 bit for bools.
90 */
glsl_base_type_bit_size(enum glsl_base_type type)91 static unsigned glsl_base_type_bit_size(enum glsl_base_type type)
92 {
93 switch (type) {
94 case GLSL_TYPE_BOOL:
95 case GLSL_TYPE_INT:
96 case GLSL_TYPE_UINT:
97 case GLSL_TYPE_FLOAT: /* TODO handle mediump */
98 case GLSL_TYPE_SUBROUTINE:
99 return 32;
100
101 case GLSL_TYPE_FLOAT16:
102 case GLSL_TYPE_UINT16:
103 case GLSL_TYPE_INT16:
104 return 16;
105
106 case GLSL_TYPE_UINT8:
107 case GLSL_TYPE_INT8:
108 return 8;
109
110 case GLSL_TYPE_DOUBLE:
111 case GLSL_TYPE_INT64:
112 case GLSL_TYPE_UINT64:
113 case GLSL_TYPE_IMAGE:
114 case GLSL_TYPE_TEXTURE:
115 case GLSL_TYPE_SAMPLER:
116 return 64;
117
118 default:
119 /* For GLSL_TYPE_STRUCT etc, it should be ok to return 0. This usually
120 * happens when calling this method through is_64bit and is_16bit
121 * methods
122 */
123 return 0;
124 }
125
126 return 0;
127 }
128
glsl_base_type_is_16bit(enum glsl_base_type type)129 static inline bool glsl_base_type_is_16bit(enum glsl_base_type type)
130 {
131 return glsl_base_type_bit_size(type) == 16;
132 }
133
glsl_base_type_is_64bit(enum glsl_base_type type)134 static inline bool glsl_base_type_is_64bit(enum glsl_base_type type)
135 {
136 return glsl_base_type_bit_size(type) == 64;
137 }
138
glsl_base_type_is_integer(enum glsl_base_type type)139 static inline bool glsl_base_type_is_integer(enum glsl_base_type type)
140 {
141 return type == GLSL_TYPE_UINT8 ||
142 type == GLSL_TYPE_INT8 ||
143 type == GLSL_TYPE_UINT16 ||
144 type == GLSL_TYPE_INT16 ||
145 type == GLSL_TYPE_UINT ||
146 type == GLSL_TYPE_INT ||
147 type == GLSL_TYPE_UINT64 ||
148 type == GLSL_TYPE_INT64 ||
149 type == GLSL_TYPE_BOOL ||
150 type == GLSL_TYPE_SAMPLER ||
151 type == GLSL_TYPE_TEXTURE ||
152 type == GLSL_TYPE_IMAGE;
153 }
154
155 static inline unsigned int
glsl_base_type_get_bit_size(const enum glsl_base_type base_type)156 glsl_base_type_get_bit_size(const enum glsl_base_type base_type)
157 {
158 switch (base_type) {
159 case GLSL_TYPE_BOOL:
160 return 1;
161
162 case GLSL_TYPE_INT:
163 case GLSL_TYPE_UINT:
164 case GLSL_TYPE_FLOAT: /* TODO handle mediump */
165 case GLSL_TYPE_SUBROUTINE:
166 case GLSL_TYPE_COOPERATIVE_MATRIX:
167 return 32;
168
169 case GLSL_TYPE_FLOAT16:
170 case GLSL_TYPE_UINT16:
171 case GLSL_TYPE_INT16:
172 return 16;
173
174 case GLSL_TYPE_UINT8:
175 case GLSL_TYPE_INT8:
176 return 8;
177
178 case GLSL_TYPE_DOUBLE:
179 case GLSL_TYPE_INT64:
180 case GLSL_TYPE_UINT64:
181 case GLSL_TYPE_IMAGE:
182 case GLSL_TYPE_SAMPLER:
183 case GLSL_TYPE_TEXTURE:
184 return 64;
185
186 default:
187 unreachable("unknown base type");
188 }
189
190 return 0;
191 }
192
193 static inline enum glsl_base_type
glsl_unsigned_base_type_of(enum glsl_base_type type)194 glsl_unsigned_base_type_of(enum glsl_base_type type)
195 {
196 switch (type) {
197 case GLSL_TYPE_INT:
198 return GLSL_TYPE_UINT;
199 case GLSL_TYPE_INT8:
200 return GLSL_TYPE_UINT8;
201 case GLSL_TYPE_INT16:
202 return GLSL_TYPE_UINT16;
203 case GLSL_TYPE_INT64:
204 return GLSL_TYPE_UINT64;
205 default:
206 assert(type == GLSL_TYPE_UINT ||
207 type == GLSL_TYPE_UINT8 ||
208 type == GLSL_TYPE_UINT16 ||
209 type == GLSL_TYPE_UINT64);
210 return type;
211 }
212 }
213
214 static inline enum glsl_base_type
glsl_signed_base_type_of(enum glsl_base_type type)215 glsl_signed_base_type_of(enum glsl_base_type type)
216 {
217 switch (type) {
218 case GLSL_TYPE_UINT:
219 return GLSL_TYPE_INT;
220 case GLSL_TYPE_UINT8:
221 return GLSL_TYPE_INT8;
222 case GLSL_TYPE_UINT16:
223 return GLSL_TYPE_INT16;
224 case GLSL_TYPE_UINT64:
225 return GLSL_TYPE_INT64;
226 default:
227 assert(type == GLSL_TYPE_INT ||
228 type == GLSL_TYPE_INT8 ||
229 type == GLSL_TYPE_INT16 ||
230 type == GLSL_TYPE_INT64);
231 return type;
232 }
233 }
234
235 enum glsl_sampler_dim {
236 GLSL_SAMPLER_DIM_1D = 0,
237 GLSL_SAMPLER_DIM_2D,
238 GLSL_SAMPLER_DIM_3D,
239 GLSL_SAMPLER_DIM_CUBE,
240 GLSL_SAMPLER_DIM_RECT,
241 GLSL_SAMPLER_DIM_BUF,
242 GLSL_SAMPLER_DIM_EXTERNAL,
243 GLSL_SAMPLER_DIM_MS,
244 GLSL_SAMPLER_DIM_SUBPASS, /* for vulkan input attachments */
245 GLSL_SAMPLER_DIM_SUBPASS_MS, /* for multisampled vulkan input attachments */
246 };
247
248 int
249 glsl_get_sampler_dim_coordinate_components(enum glsl_sampler_dim dim);
250
251 enum glsl_matrix_layout {
252 /**
253 * The layout of the matrix is inherited from the object containing the
254 * matrix (the top level structure or the uniform block).
255 */
256 GLSL_MATRIX_LAYOUT_INHERITED,
257
258 /**
259 * Explicit column-major layout
260 *
261 * If a uniform block doesn't have an explicit layout set, it will default
262 * to this layout.
263 */
264 GLSL_MATRIX_LAYOUT_COLUMN_MAJOR,
265
266 /**
267 * Row-major layout
268 */
269 GLSL_MATRIX_LAYOUT_ROW_MAJOR
270 };
271
272 enum {
273 GLSL_PRECISION_NONE = 0,
274 GLSL_PRECISION_HIGH,
275 GLSL_PRECISION_MEDIUM,
276 GLSL_PRECISION_LOW
277 };
278
279 enum glsl_cmat_use {
280 GLSL_CMAT_USE_NONE = 0,
281 GLSL_CMAT_USE_A,
282 GLSL_CMAT_USE_B,
283 GLSL_CMAT_USE_ACCUMULATOR,
284 };
285
286 struct glsl_cmat_description {
287 /* MSVC can't merge bitfields of different types and also sign extend enums,
288 * so use uint8_t for those cases.
289 */
290 uint8_t element_type:5; /* enum glsl_base_type */
291 uint8_t scope:3; /* mesa_scope */
292 uint8_t rows;
293 uint8_t cols;
294 uint8_t use; /* enum glsl_cmat_use */
295 };
296
297 const char *glsl_get_type_name(const glsl_type *type);
298
299 struct glsl_type {
300 uint32_t gl_type;
301 enum glsl_base_type base_type:8;
302
303 enum glsl_base_type sampled_type:8; /**< Type of data returned using this
304 * sampler or image. Only \c
305 * GLSL_TYPE_FLOAT, \c GLSL_TYPE_INT,
306 * and \c GLSL_TYPE_UINT are valid.
307 */
308
309 unsigned sampler_dimensionality:4; /**< \see glsl_sampler_dim */
310 unsigned sampler_shadow:1;
311 unsigned sampler_array:1;
312 unsigned interface_packing:2;
313 unsigned interface_row_major:1;
314
315 struct glsl_cmat_description cmat_desc;
316
317 /**
318 * For \c GLSL_TYPE_STRUCT this specifies if the struct is packed or not.
319 *
320 * Only used for Compute kernels
321 */
322 unsigned packed:1;
323
324 unsigned has_builtin_name:1;
325
326 /**
327 * \name Vector and matrix element counts
328 *
329 * For scalars, each of these values will be 1. For non-numeric types
330 * these will be 0.
331 */
332 /*@{*/
333 uint8_t vector_elements; /**< 1, 2, 3, or 4 vector elements. */
334 uint8_t matrix_columns; /**< 1, 2, 3, or 4 matrix columns. */
335 /*@}*/
336
337 /**
338 * For \c GLSL_TYPE_ARRAY, this is the length of the array. For
339 * \c GLSL_TYPE_STRUCT or \c GLSL_TYPE_INTERFACE, it is the number of
340 * elements in the structure and the number of values pointed to by
341 * \c fields.structure (below).
342 */
343 unsigned length;
344
345 /**
346 * Identifier to the name of the data type
347 *
348 * Use glsl_get_type_name() to access the actual name.
349 */
350 uintptr_t name_id;
351
352 /**
353 * Explicit array, matrix, or vector stride. This is used to communicate
354 * explicit array layouts from SPIR-V. Should be 0 if the type has no
355 * explicit stride.
356 */
357 unsigned explicit_stride;
358
359 /**
360 * Explicit alignment. This is used to communicate explicit alignment
361 * constraints. Should be 0 if the type has no explicit alignment
362 * constraint.
363 */
364 unsigned explicit_alignment;
365
366 /**
367 * Subtype of composite data types.
368 */
369 union {
370 const glsl_type *array; /**< Type of array elements. */
371 const glsl_struct_field *structure; /**< List of struct fields. */
372 } fields;
373 };
374
375 #include "builtin_types.h"
376
377 struct glsl_struct_field {
378 const glsl_type *type;
379 const char *name;
380
381 /**
382 * For interface blocks, gl_varying_slot corresponding to the input/output
383 * if this is a built-in input/output (i.e. a member of the built-in
384 * gl_PerVertex interface block); -1 otherwise.
385 *
386 * Ignored for structs.
387 */
388 int location;
389
390 /**
391 * For interface blocks, members may explicitly assign the component used
392 * by a varying. Ignored for structs.
393 */
394 int component;
395
396 /**
397 * For interface blocks, members may have an explicit byte offset
398 * specified; -1 otherwise. Also used for xfb_offset layout qualifier.
399 *
400 * Unless used for xfb_offset this field is ignored for structs.
401 */
402 int offset;
403
404 /**
405 * For interface blocks, members may define a transform feedback buffer;
406 * -1 otherwise.
407 */
408 int xfb_buffer;
409
410 /**
411 * For interface blocks, members may define a transform feedback stride;
412 * -1 otherwise.
413 */
414 int xfb_stride;
415
416 /**
417 * Layout format, applicable to image variables only.
418 */
419 enum pipe_format image_format;
420
421 union {
422 struct {
423 /**
424 * For interface blocks, the interpolation mode (as in
425 * ir_variable::interpolation). 0 otherwise.
426 */
427 unsigned interpolation:3;
428
429 /**
430 * For interface blocks, 1 if this variable uses centroid interpolation (as
431 * in ir_variable::centroid). 0 otherwise.
432 */
433 unsigned centroid:1;
434
435 /**
436 * For interface blocks, 1 if this variable uses sample interpolation (as
437 * in ir_variable::sample). 0 otherwise.
438 */
439 unsigned sample:1;
440
441 /**
442 * Layout of the matrix. Uses glsl_matrix_layout values.
443 */
444 unsigned matrix_layout:2;
445
446 /**
447 * For interface blocks, 1 if this variable is a per-patch input or output
448 * (as in ir_variable::patch). 0 otherwise.
449 */
450 unsigned patch:1;
451
452 /**
453 * Precision qualifier
454 */
455 unsigned precision:2;
456
457 /**
458 * Memory qualifiers, applicable to buffer variables defined in shader
459 * storage buffer objects (SSBOs)
460 */
461 unsigned memory_read_only:1;
462 unsigned memory_write_only:1;
463 unsigned memory_coherent:1;
464 unsigned memory_volatile:1;
465 unsigned memory_restrict:1;
466
467 /**
468 * Any of the xfb_* qualifiers trigger the shader to be in transform
469 * feedback mode so we need to keep track of whether the buffer was
470 * explicitly set or if its just been assigned the default global value.
471 */
472 unsigned explicit_xfb_buffer:1;
473
474 unsigned implicit_sized_array:1;
475 };
476 unsigned flags;
477 };
478 #ifdef __cplusplus
479 #define DEFAULT_CONSTRUCTORS(_type, _name) \
480 type(_type), name(_name), location(-1), component(-1), offset(-1), \
481 xfb_buffer(0), xfb_stride(0), image_format(PIPE_FORMAT_NONE), flags(0) \
482
glsl_struct_fieldglsl_struct_field483 glsl_struct_field(const glsl_type *_type,
484 int _precision,
485 const char *_name)
486 : DEFAULT_CONSTRUCTORS(_type, _name)
487 {
488 matrix_layout = GLSL_MATRIX_LAYOUT_INHERITED;
489 precision = _precision;
490 }
491
glsl_struct_fieldglsl_struct_field492 glsl_struct_field(const glsl_type *_type, const char *_name)
493 : DEFAULT_CONSTRUCTORS(_type, _name)
494 {
495 matrix_layout = GLSL_MATRIX_LAYOUT_INHERITED;
496 precision = GLSL_PRECISION_NONE;
497 }
498
glsl_struct_fieldglsl_struct_field499 glsl_struct_field()
500 : DEFAULT_CONSTRUCTORS(NULL, NULL)
501 {
502 matrix_layout = GLSL_MATRIX_LAYOUT_INHERITED;
503 precision = GLSL_PRECISION_NONE;
504 }
505 #undef DEFAULT_CONSTRUCTORS
506 #endif
507 };
508
glsl_get_base_type(const glsl_type * t)509 static inline enum glsl_base_type glsl_get_base_type(const glsl_type *t) { return t->base_type; }
510
511 static inline unsigned
glsl_get_bit_size(const glsl_type * t)512 glsl_get_bit_size(const glsl_type *t)
513 {
514 return glsl_base_type_get_bit_size(glsl_get_base_type(t));
515 }
516
glsl_type_is_boolean(const glsl_type * t)517 static inline bool glsl_type_is_boolean(const glsl_type *t) { return t->base_type == GLSL_TYPE_BOOL; }
glsl_type_is_sampler(const glsl_type * t)518 static inline bool glsl_type_is_sampler(const glsl_type *t) { return t->base_type == GLSL_TYPE_SAMPLER; }
glsl_type_is_texture(const glsl_type * t)519 static inline bool glsl_type_is_texture(const glsl_type *t) { return t->base_type == GLSL_TYPE_TEXTURE; }
glsl_type_is_image(const glsl_type * t)520 static inline bool glsl_type_is_image(const glsl_type *t) { return t->base_type == GLSL_TYPE_IMAGE; }
glsl_type_is_atomic_uint(const glsl_type * t)521 static inline bool glsl_type_is_atomic_uint(const glsl_type *t) { return t->base_type == GLSL_TYPE_ATOMIC_UINT; }
glsl_type_is_struct(const glsl_type * t)522 static inline bool glsl_type_is_struct(const glsl_type *t) { return t->base_type == GLSL_TYPE_STRUCT; }
glsl_type_is_interface(const glsl_type * t)523 static inline bool glsl_type_is_interface(const glsl_type *t) { return t->base_type == GLSL_TYPE_INTERFACE; }
glsl_type_is_array(const glsl_type * t)524 static inline bool glsl_type_is_array(const glsl_type *t) { return t->base_type == GLSL_TYPE_ARRAY; }
glsl_type_is_cmat(const glsl_type * t)525 static inline bool glsl_type_is_cmat(const glsl_type *t) { return t->base_type == GLSL_TYPE_COOPERATIVE_MATRIX; }
glsl_type_is_void(const glsl_type * t)526 static inline bool glsl_type_is_void(const glsl_type *t) { return t->base_type == GLSL_TYPE_VOID; }
glsl_type_is_subroutine(const glsl_type * t)527 static inline bool glsl_type_is_subroutine(const glsl_type *t) { return t->base_type == GLSL_TYPE_SUBROUTINE; }
glsl_type_is_error(const glsl_type * t)528 static inline bool glsl_type_is_error(const glsl_type *t) { return t->base_type == GLSL_TYPE_ERROR; }
glsl_type_is_double(const glsl_type * t)529 static inline bool glsl_type_is_double(const glsl_type *t) { return t->base_type == GLSL_TYPE_DOUBLE; }
glsl_type_is_float(const glsl_type * t)530 static inline bool glsl_type_is_float(const glsl_type *t) { return t->base_type == GLSL_TYPE_FLOAT; }
531
532 static inline bool
glsl_type_is_numeric(const glsl_type * t)533 glsl_type_is_numeric(const glsl_type *t)
534 {
535 return t->base_type >= GLSL_TYPE_UINT &&
536 t->base_type <= GLSL_TYPE_INT64;
537 }
538
539 static inline bool
glsl_type_is_integer(const glsl_type * t)540 glsl_type_is_integer(const glsl_type *t)
541 {
542 return glsl_base_type_is_integer(t->base_type);
543 }
544
545 static inline bool
glsl_type_is_struct_or_ifc(const glsl_type * t)546 glsl_type_is_struct_or_ifc(const glsl_type *t)
547 {
548 return glsl_type_is_struct(t) || glsl_type_is_interface(t);
549 }
550
551 static inline bool
glsl_type_is_packed(const glsl_type * t)552 glsl_type_is_packed(const glsl_type *t)
553 {
554 return t->packed;
555 }
556
557 static inline bool
glsl_type_is_16bit(const glsl_type * t)558 glsl_type_is_16bit(const glsl_type *t)
559 {
560 return glsl_base_type_is_16bit(t->base_type);
561 }
562
563 static inline bool
glsl_type_is_32bit(const glsl_type * t)564 glsl_type_is_32bit(const glsl_type *t)
565 {
566 return t->base_type == GLSL_TYPE_UINT ||
567 t->base_type == GLSL_TYPE_INT ||
568 t->base_type == GLSL_TYPE_FLOAT;
569 }
570
571 static inline bool
glsl_type_is_64bit(const glsl_type * t)572 glsl_type_is_64bit(const glsl_type *t)
573 {
574 return glsl_base_type_is_64bit(t->base_type);
575 }
576
577 static inline bool
glsl_type_is_integer_16(const glsl_type * t)578 glsl_type_is_integer_16(const glsl_type *t)
579 {
580 return t->base_type == GLSL_TYPE_UINT16 || t->base_type == GLSL_TYPE_INT16;
581 }
582
583 static inline bool
glsl_type_is_integer_32(const glsl_type * t)584 glsl_type_is_integer_32(const glsl_type *t)
585 {
586 return t->base_type == GLSL_TYPE_UINT || t->base_type == GLSL_TYPE_INT;
587 }
588
589 static inline bool
glsl_type_is_integer_64(const glsl_type * t)590 glsl_type_is_integer_64(const glsl_type *t)
591 {
592 return t->base_type == GLSL_TYPE_UINT64 || t->base_type == GLSL_TYPE_INT64;
593 }
594
595 static inline bool
glsl_type_is_integer_32_64(const glsl_type * t)596 glsl_type_is_integer_32_64(const glsl_type *t)
597 {
598 return glsl_type_is_integer_32(t) || glsl_type_is_integer_64(t);
599 }
600
601 static inline bool
glsl_type_is_integer_16_32(const glsl_type * t)602 glsl_type_is_integer_16_32(const glsl_type *t)
603 {
604 return glsl_type_is_integer_16(t) || glsl_type_is_integer_32(t);
605 }
606
607 static inline bool
glsl_type_is_integer_16_32_64(const glsl_type * t)608 glsl_type_is_integer_16_32_64(const glsl_type *t)
609 {
610 return glsl_type_is_integer_16(t) || glsl_type_is_integer_32(t) || glsl_type_is_integer_64(t);
611 }
612
613 static inline bool
glsl_type_is_float_16(const glsl_type * t)614 glsl_type_is_float_16(const glsl_type *t)
615 {
616 return t->base_type == GLSL_TYPE_FLOAT16;
617 }
618
619 static inline bool
glsl_type_is_float_16_32(const glsl_type * t)620 glsl_type_is_float_16_32(const glsl_type *t)
621 {
622 return t->base_type == GLSL_TYPE_FLOAT16 || glsl_type_is_float(t);
623 }
624
625 static inline bool
glsl_type_is_float_16_32_64(const glsl_type * t)626 glsl_type_is_float_16_32_64(const glsl_type *t)
627 {
628 return t->base_type == GLSL_TYPE_FLOAT16 || glsl_type_is_float(t) || glsl_type_is_double(t);
629 }
630
631 static inline bool
glsl_type_is_float_32_64(const glsl_type * t)632 glsl_type_is_float_32_64(const glsl_type *t)
633 {
634 return glsl_type_is_float(t) || glsl_type_is_double(t);
635 }
636
637 static inline bool
glsl_type_is_int_16_32_64(const glsl_type * t)638 glsl_type_is_int_16_32_64(const glsl_type *t)
639 {
640 return t->base_type == GLSL_TYPE_INT16 ||
641 t->base_type == GLSL_TYPE_INT ||
642 t->base_type == GLSL_TYPE_INT64;
643 }
644
645 static inline bool
glsl_type_is_uint_16_32_64(const glsl_type * t)646 glsl_type_is_uint_16_32_64(const glsl_type *t)
647 {
648 return t->base_type == GLSL_TYPE_UINT16 ||
649 t->base_type == GLSL_TYPE_UINT ||
650 t->base_type == GLSL_TYPE_UINT64;
651 }
652
653 static inline bool
glsl_type_is_int_16_32(const glsl_type * t)654 glsl_type_is_int_16_32(const glsl_type *t)
655 {
656 return t->base_type == GLSL_TYPE_INT ||
657 t->base_type == GLSL_TYPE_INT16;
658 }
659
660 static inline bool
glsl_type_is_uint_16_32(const glsl_type * t)661 glsl_type_is_uint_16_32(const glsl_type *t)
662 {
663 return t->base_type == GLSL_TYPE_UINT ||
664 t->base_type == GLSL_TYPE_UINT16;
665 }
666
667 static inline bool
glsl_type_is_unsized_array(const glsl_type * t)668 glsl_type_is_unsized_array(const glsl_type *t)
669 {
670 return glsl_type_is_array(t) && t->length == 0;
671 }
672
673 static inline bool
glsl_type_is_array_of_arrays(const glsl_type * t)674 glsl_type_is_array_of_arrays(const glsl_type *t)
675 {
676 return glsl_type_is_array(t) && glsl_type_is_array(t->fields.array);
677 }
678
679 static inline bool
glsl_type_is_bare_sampler(const glsl_type * t)680 glsl_type_is_bare_sampler(const glsl_type *t)
681 {
682 return glsl_type_is_sampler(t) && t->sampled_type == GLSL_TYPE_VOID;
683 }
684
685 bool glsl_type_is_vector(const glsl_type *t);
686 bool glsl_type_is_scalar(const glsl_type *t);
687 bool glsl_type_is_vector_or_scalar(const glsl_type *t);
688 bool glsl_type_is_matrix(const glsl_type *t);
689 bool glsl_type_is_array_or_matrix(const glsl_type *t);
690
691 /**
692 * Query whether a 64-bit type takes two slots.
693 */
694 bool glsl_type_is_dual_slot(const glsl_type *t);
695
696 bool glsl_type_is_leaf(const glsl_type *type);
697
698 static inline bool
glsl_matrix_type_is_row_major(const glsl_type * t)699 glsl_matrix_type_is_row_major(const glsl_type *t)
700 {
701 assert((glsl_type_is_matrix(t) && t->explicit_stride) || glsl_type_is_interface(t));
702 return t->interface_row_major;
703 }
704
705 static inline bool
glsl_sampler_type_is_shadow(const glsl_type * t)706 glsl_sampler_type_is_shadow(const glsl_type *t)
707 {
708 assert(glsl_type_is_sampler(t));
709 return t->sampler_shadow;
710 }
711
712 static inline bool
glsl_sampler_type_is_array(const glsl_type * t)713 glsl_sampler_type_is_array(const glsl_type *t)
714 {
715 assert(glsl_type_is_sampler(t) ||
716 glsl_type_is_texture(t) ||
717 glsl_type_is_image(t));
718 return t->sampler_array;
719 }
720
721 static inline bool
glsl_struct_type_is_packed(const glsl_type * t)722 glsl_struct_type_is_packed(const glsl_type *t)
723 {
724 assert(glsl_type_is_struct(t));
725 return t->packed;
726 }
727
728 /**
729 * Gets the "bare" type without any decorations or layout information.
730 */
731 const glsl_type *glsl_get_bare_type(const glsl_type *t);
732
733 /**
734 * Get the basic scalar type which this type aggregates.
735 *
736 * If the type is a numeric or boolean scalar, vector, or matrix, or an
737 * array of any of those, this function gets the scalar type of the
738 * individual components. For structs and arrays of structs, this function
739 * returns the struct type. For samplers and arrays of samplers, this
740 * function returns the sampler type.
741 */
742 const glsl_type *glsl_get_scalar_type(const glsl_type *t);
743
744 /**
745 * For numeric and boolean derived types returns the basic scalar type
746 *
747 * If the type is a numeric or boolean scalar, vector, or matrix type,
748 * this function gets the scalar type of the individual components. For
749 * all other types, including arrays of numeric or boolean types, the
750 * error type is returned.
751 */
752 const glsl_type *glsl_get_base_glsl_type(const glsl_type *t);
753
754 unsigned glsl_get_length(const glsl_type *t);
755
756 static inline unsigned
glsl_get_vector_elements(const glsl_type * t)757 glsl_get_vector_elements(const glsl_type *t)
758 {
759 return t->vector_elements;
760 }
761
762 /**
763 * Query the total number of scalars that make up a scalar, vector or matrix
764 */
765 static inline unsigned
glsl_get_components(const glsl_type * t)766 glsl_get_components(const glsl_type *t)
767 {
768 return t->vector_elements * t->matrix_columns;
769 }
770
771 static inline unsigned
glsl_get_matrix_columns(const glsl_type * t)772 glsl_get_matrix_columns(const glsl_type *t)
773 {
774 return t->matrix_columns;
775 }
776
777 const glsl_type *glsl_type_wrap_in_arrays(const glsl_type *t,
778 const glsl_type *arrays);
779
780 /**
781 * Query the number of elements in an array type
782 *
783 * \return
784 * The number of elements in the array for array types or -1 for non-array
785 * types. If the number of elements in the array has not yet been declared,
786 * zero is returned.
787 */
788 static inline int
glsl_array_size(const glsl_type * t)789 glsl_array_size(const glsl_type *t)
790 {
791 return glsl_type_is_array(t) ? t->length : -1;
792 }
793
794 /**
795 * Return the total number of elements in an array including the elements
796 * in arrays of arrays.
797 */
798 unsigned glsl_get_aoa_size(const glsl_type *t);
799
800 const glsl_type *glsl_get_array_element(const glsl_type *t);
801
802 /**
803 * Get the type stripped of any arrays
804 *
805 * \return
806 * Pointer to the type of elements of the first non-array type for array
807 * types, or pointer to itself for non-array types.
808 */
809 const glsl_type *glsl_without_array(const glsl_type *t);
810
811 const glsl_type *glsl_without_array_or_matrix(const glsl_type *t);
812 const glsl_type *glsl_type_wrap_in_arrays(const glsl_type *t,
813 const glsl_type *arrays);
814
815 const glsl_type *glsl_get_cmat_element(const glsl_type *t);
816 const struct glsl_cmat_description *glsl_get_cmat_description(const glsl_type *t);
817
818 /**
819 * Return the amount of atomic counter storage required for a type.
820 */
821 unsigned glsl_atomic_size(const glsl_type *type);
822
823 /**
824 * Type A contains type B if A is B or A is a composite type (struct,
825 * interface, array) that has an element that contains B.
826 */
827 bool glsl_type_contains_32bit(const glsl_type *t);
828 bool glsl_type_contains_64bit(const glsl_type *t);
829 bool glsl_type_contains_image(const glsl_type *t);
830 bool glsl_contains_atomic(const glsl_type *t);
831 bool glsl_contains_double(const glsl_type *t);
832 bool glsl_contains_integer(const glsl_type *t);
833 bool glsl_contains_opaque(const glsl_type *t);
834 bool glsl_contains_sampler(const glsl_type *t);
835 bool glsl_contains_array(const glsl_type *t);
836 bool glsl_contains_subroutine(const glsl_type *t);
837
838 static inline enum glsl_sampler_dim
glsl_get_sampler_dim(const glsl_type * t)839 glsl_get_sampler_dim(const glsl_type *t)
840 {
841 assert(glsl_type_is_sampler(t) ||
842 glsl_type_is_texture(t) ||
843 glsl_type_is_image(t));
844 return (enum glsl_sampler_dim)t->sampler_dimensionality;
845 }
846
847 static inline enum glsl_base_type
glsl_get_sampler_result_type(const glsl_type * t)848 glsl_get_sampler_result_type(const glsl_type *t)
849 {
850 assert(glsl_type_is_sampler(t) ||
851 glsl_type_is_texture(t) ||
852 glsl_type_is_image(t));
853 return (enum glsl_base_type)t->sampled_type;
854 }
855
856 /**
857 * Return the number of coordinate components needed for this
858 * sampler or image type.
859 *
860 * This is based purely on the sampler's dimensionality. For example, this
861 * returns 1 for sampler1D, and 3 for sampler2DArray.
862 *
863 * Note that this is often different than actual coordinate type used in
864 * a texturing built-in function, since those pack additional values (such
865 * as the shadow comparator or projector) into the coordinate type.
866 */
867 int glsl_get_sampler_coordinate_components(const glsl_type *t);
868
869 /**
870 * Compares whether this type matches another type without taking into
871 * account the precision in structures.
872 *
873 * This is applied recursively so that structures containing structure
874 * members can also ignore the precision.
875 */
876 bool glsl_type_compare_no_precision(const glsl_type *a, const glsl_type *b);
877
878 /**
879 * Compare a record type against another record type.
880 *
881 * This is useful for matching record types declared on the same shader
882 * stage as well as across different shader stages.
883 * The option to not match name is needed for matching record types
884 * declared across different shader stages.
885 * The option to not match locations is to deal with places where the
886 * same struct is defined in a block which has a location set on it.
887 */
888 bool glsl_record_compare(const glsl_type *a, const glsl_type *b,
889 bool match_name, bool match_locations,
890 bool match_precision);
891
892 const glsl_type *glsl_get_struct_field(const glsl_type *t, unsigned index);
893 const glsl_struct_field *glsl_get_struct_field_data(const glsl_type *t, unsigned index);
894
895 /**
896 * Calculate offset between the base location of the struct in
897 * uniform storage and a struct member.
898 * For the initial call, length is the index of the member to find the
899 * offset for.
900 */
901 unsigned glsl_get_struct_location_offset(const glsl_type *t, unsigned length);
902
903 /**
904 * Get the location of a field within a record type
905 */
906 int glsl_get_field_index(const glsl_type *t, const char *name);
907
908 /**
909 * Get the type of a structure field
910 *
911 * \return
912 * Pointer to the type of the named field. If the type is not a structure
913 * or the named field does not exist, \c &glsl_type_builtin_error is returned.
914 */
915 const glsl_type *glsl_get_field_type(const glsl_type *t, const char *name);
916
917 static inline int
glsl_get_struct_field_offset(const glsl_type * t,unsigned index)918 glsl_get_struct_field_offset(const glsl_type *t, unsigned index)
919 {
920 return t->fields.structure[index].offset;
921 }
922
923 static inline const char *
glsl_get_struct_elem_name(const glsl_type * t,unsigned index)924 glsl_get_struct_elem_name(const glsl_type *t, unsigned index)
925 {
926 return t->fields.structure[index].name;
927 }
928
glsl_void_type(void)929 static inline const glsl_type *glsl_void_type(void) { return &glsl_type_builtin_void; }
glsl_float_type(void)930 static inline const glsl_type *glsl_float_type(void) { return &glsl_type_builtin_float; }
glsl_float16_t_type(void)931 static inline const glsl_type *glsl_float16_t_type(void) { return &glsl_type_builtin_float16_t; }
glsl_double_type(void)932 static inline const glsl_type *glsl_double_type(void) { return &glsl_type_builtin_double; }
glsl_vec2_type(void)933 static inline const glsl_type *glsl_vec2_type(void) { return &glsl_type_builtin_vec2; }
glsl_dvec2_type(void)934 static inline const glsl_type *glsl_dvec2_type(void) { return &glsl_type_builtin_dvec2; }
glsl_uvec2_type(void)935 static inline const glsl_type *glsl_uvec2_type(void) { return &glsl_type_builtin_uvec2; }
glsl_ivec2_type(void)936 static inline const glsl_type *glsl_ivec2_type(void) { return &glsl_type_builtin_ivec2; }
glsl_bvec2_type(void)937 static inline const glsl_type *glsl_bvec2_type(void) { return &glsl_type_builtin_bvec2; }
glsl_vec4_type(void)938 static inline const glsl_type *glsl_vec4_type(void) { return &glsl_type_builtin_vec4; }
glsl_dvec4_type(void)939 static inline const glsl_type *glsl_dvec4_type(void) { return &glsl_type_builtin_dvec4; }
glsl_uvec4_type(void)940 static inline const glsl_type *glsl_uvec4_type(void) { return &glsl_type_builtin_uvec4; }
glsl_ivec4_type(void)941 static inline const glsl_type *glsl_ivec4_type(void) { return &glsl_type_builtin_ivec4; }
glsl_bvec4_type(void)942 static inline const glsl_type *glsl_bvec4_type(void) { return &glsl_type_builtin_bvec4; }
glsl_int_type(void)943 static inline const glsl_type *glsl_int_type(void) { return &glsl_type_builtin_int; }
glsl_uint_type(void)944 static inline const glsl_type *glsl_uint_type(void) { return &glsl_type_builtin_uint; }
glsl_int64_t_type(void)945 static inline const glsl_type *glsl_int64_t_type(void) { return &glsl_type_builtin_int64_t; }
glsl_uint64_t_type(void)946 static inline const glsl_type *glsl_uint64_t_type(void) { return &glsl_type_builtin_uint64_t; }
glsl_int16_t_type(void)947 static inline const glsl_type *glsl_int16_t_type(void) { return &glsl_type_builtin_int16_t; }
glsl_uint16_t_type(void)948 static inline const glsl_type *glsl_uint16_t_type(void) { return &glsl_type_builtin_uint16_t; }
glsl_int8_t_type(void)949 static inline const glsl_type *glsl_int8_t_type(void) { return &glsl_type_builtin_int8_t; }
glsl_uint8_t_type(void)950 static inline const glsl_type *glsl_uint8_t_type(void) { return &glsl_type_builtin_uint8_t; }
glsl_bool_type(void)951 static inline const glsl_type *glsl_bool_type(void) { return &glsl_type_builtin_bool; }
glsl_atomic_uint_type(void)952 static inline const glsl_type *glsl_atomic_uint_type(void) { return &glsl_type_builtin_atomic_uint; }
953
954 static inline const glsl_type *
glsl_floatN_t_type(unsigned bit_size)955 glsl_floatN_t_type(unsigned bit_size)
956 {
957 switch (bit_size) {
958 case 16: return &glsl_type_builtin_float16_t;
959 case 32: return &glsl_type_builtin_float;
960 case 64: return &glsl_type_builtin_double;
961 default:
962 unreachable("Unsupported bit size");
963 }
964 }
965
966 static inline const glsl_type *
glsl_intN_t_type(unsigned bit_size)967 glsl_intN_t_type(unsigned bit_size)
968 {
969 switch (bit_size) {
970 case 8: return &glsl_type_builtin_int8_t;
971 case 16: return &glsl_type_builtin_int16_t;
972 case 32: return &glsl_type_builtin_int;
973 case 64: return &glsl_type_builtin_int64_t;
974 default:
975 unreachable("Unsupported bit size");
976 }
977 }
978
979 static inline const glsl_type *
glsl_uintN_t_type(unsigned bit_size)980 glsl_uintN_t_type(unsigned bit_size)
981 {
982 switch (bit_size) {
983 case 8: return &glsl_type_builtin_uint8_t;
984 case 16: return &glsl_type_builtin_uint16_t;
985 case 32: return &glsl_type_builtin_uint;
986 case 64: return &glsl_type_builtin_uint64_t;
987 default:
988 unreachable("Unsupported bit size");
989 }
990 }
991
992 const glsl_type *glsl_vec_type(unsigned components);
993 const glsl_type *glsl_f16vec_type(unsigned components);
994 const glsl_type *glsl_dvec_type(unsigned components);
995 const glsl_type *glsl_ivec_type(unsigned components);
996 const glsl_type *glsl_uvec_type(unsigned components);
997 const glsl_type *glsl_bvec_type(unsigned components);
998 const glsl_type *glsl_i64vec_type(unsigned components);
999 const glsl_type *glsl_u64vec_type(unsigned components);
1000 const glsl_type *glsl_i16vec_type(unsigned components);
1001 const glsl_type *glsl_u16vec_type(unsigned components);
1002 const glsl_type *glsl_i8vec_type(unsigned components);
1003 const glsl_type *glsl_u8vec_type(unsigned components);
1004
1005 const glsl_type *glsl_simple_explicit_type(unsigned base_type, unsigned rows,
1006 unsigned columns,
1007 unsigned explicit_stride,
1008 bool row_major,
1009 unsigned explicit_alignment);
1010
1011 static inline const glsl_type *
glsl_simple_type(unsigned base_type,unsigned rows,unsigned columns)1012 glsl_simple_type(unsigned base_type, unsigned rows, unsigned columns)
1013 {
1014 return glsl_simple_explicit_type(base_type, rows, columns, 0, false, 0);
1015 }
1016
1017 const glsl_type *glsl_sampler_type(enum glsl_sampler_dim dim,
1018 bool shadow,
1019 bool array,
1020 enum glsl_base_type type);
1021 const glsl_type *glsl_bare_sampler_type(void);
1022 const glsl_type *glsl_bare_shadow_sampler_type(void);
1023 const glsl_type *glsl_texture_type(enum glsl_sampler_dim dim,
1024 bool array,
1025 enum glsl_base_type type);
1026 const glsl_type *glsl_image_type(enum glsl_sampler_dim dim,
1027 bool array, enum glsl_base_type type);
1028 const glsl_type *glsl_array_type(const glsl_type *element,
1029 unsigned array_size,
1030 unsigned explicit_stride);
1031 const glsl_type *glsl_cmat_type(const struct glsl_cmat_description *desc);
1032 const glsl_type *glsl_struct_type_with_explicit_alignment(const glsl_struct_field *fields,
1033 unsigned num_fields,
1034 const char *name,
1035 bool packed,
1036 unsigned explicit_alignment);
1037
1038 static inline const glsl_type *
glsl_struct_type(const glsl_struct_field * fields,unsigned num_fields,const char * name,bool packed)1039 glsl_struct_type(const glsl_struct_field *fields, unsigned num_fields,
1040 const char *name, bool packed)
1041 {
1042 return glsl_struct_type_with_explicit_alignment(fields, num_fields, name, packed, 0);
1043 }
1044
1045 const glsl_type *glsl_interface_type(const glsl_struct_field *fields,
1046 unsigned num_fields,
1047 enum glsl_interface_packing packing,
1048 bool row_major,
1049 const char *block_name);
1050 const glsl_type *glsl_subroutine_type(const char *subroutine_name);
1051
1052 /**
1053 * Query the full type of a matrix row
1054 *
1055 * \return
1056 * If the type is not a matrix, \c glsl_type::error_type is returned.
1057 * Otherwise a type matching the rows of the matrix is returned.
1058 */
1059 const glsl_type *glsl_get_row_type(const glsl_type *t);
1060
1061 /**
1062 * Query the full type of a matrix column
1063 *
1064 * \return
1065 * If the type is not a matrix, \c glsl_type::error_type is returned.
1066 * Otherwise a type matching the columns of the matrix is returned.
1067 */
1068 const glsl_type *glsl_get_column_type(const glsl_type *t);
1069
1070 /** Returns an explicitly laid out type given a type and size/align func
1071 *
1072 * The size/align func is only called for scalar and vector types and the
1073 * returned type is otherwise laid out in the natural way as follows:
1074 *
1075 * - Arrays and matrices have a stride of align(elem_size, elem_align).
1076 *
1077 * - Structure types have their elements in-order and as tightly packed as
1078 * possible following the alignment required by the size/align func.
1079 *
1080 * - All composite types (structures, matrices, and arrays) have an
1081 * alignment equal to the highest alignment of any member of the composite.
1082 *
1083 * The types returned by this function are likely not suitable for most UBO
1084 * or SSBO layout because they do not add the extra array and substructure
1085 * alignment that is required by std140 and std430.
1086 */
1087 const glsl_type *glsl_get_explicit_type_for_size_align(const glsl_type *type,
1088 glsl_type_size_align_func type_info,
1089 unsigned *size, unsigned *alignment);
1090
1091 const glsl_type *glsl_type_replace_vec3_with_vec4(const glsl_type *type);
1092
1093 /**
1094 * Gets the float16 version of this type.
1095 */
1096 const glsl_type *glsl_float16_type(const glsl_type *t);
1097
1098 /**
1099 * Gets the int16 version of this type.
1100 */
1101 const glsl_type *glsl_int16_type(const glsl_type *t);
1102
1103 /**
1104 * Gets the uint16 version of this type.
1105 */
1106 const glsl_type *glsl_uint16_type(const glsl_type *t);
1107
1108 const glsl_type *glsl_type_to_16bit(const glsl_type *old_type);
1109
1110 static inline const glsl_type *
glsl_scalar_type(enum glsl_base_type base_type)1111 glsl_scalar_type(enum glsl_base_type base_type)
1112 {
1113 return glsl_simple_type(base_type, 1, 1);
1114 }
1115
1116 static inline const glsl_type *
glsl_vector_type(enum glsl_base_type base_type,unsigned components)1117 glsl_vector_type(enum glsl_base_type base_type, unsigned components)
1118 {
1119 const glsl_type *t = glsl_simple_type(base_type, components, 1);
1120 assert(t != &glsl_type_builtin_error);
1121 return t;
1122 }
1123
1124 static inline const glsl_type *
glsl_matrix_type(enum glsl_base_type base_type,unsigned rows,unsigned columns)1125 glsl_matrix_type(enum glsl_base_type base_type,
1126 unsigned rows, unsigned columns)
1127 {
1128 const glsl_type *t = glsl_simple_type(base_type, rows, columns);
1129 assert(t != &glsl_type_builtin_error);
1130 return t;
1131 }
1132
1133 static inline const glsl_type *
glsl_explicit_matrix_type(const glsl_type * mat,unsigned stride,bool row_major)1134 glsl_explicit_matrix_type(const glsl_type *mat, unsigned stride,
1135 bool row_major) {
1136 assert(stride > 0);
1137 const glsl_type *t = glsl_simple_explicit_type(mat->base_type,
1138 mat->vector_elements,
1139 mat->matrix_columns,
1140 stride, row_major, 0);
1141 assert(t != &glsl_type_builtin_error);
1142 return t;
1143
1144 }
1145
1146 static inline const glsl_type *
glsl_transposed_type(const glsl_type * t)1147 glsl_transposed_type(const glsl_type *t)
1148 {
1149 assert(glsl_type_is_matrix(t));
1150 return glsl_simple_type(t->base_type, t->matrix_columns, t->vector_elements);
1151 }
1152
1153 static inline const glsl_type *
glsl_texture_type_to_sampler(const glsl_type * t,bool is_shadow)1154 glsl_texture_type_to_sampler(const glsl_type *t, bool is_shadow)
1155 {
1156 assert(glsl_type_is_texture(t));
1157 return glsl_sampler_type((enum glsl_sampler_dim)t->sampler_dimensionality,
1158 is_shadow, t->sampler_array,
1159 (enum glsl_base_type)t->sampled_type);
1160 }
1161
1162 static inline const glsl_type *
glsl_sampler_type_to_texture(const glsl_type * t)1163 glsl_sampler_type_to_texture(const glsl_type *t)
1164 {
1165 assert(glsl_type_is_sampler(t) && !glsl_type_is_bare_sampler(t));
1166 return glsl_texture_type((enum glsl_sampler_dim)t->sampler_dimensionality,
1167 t->sampler_array,
1168 (enum glsl_base_type)t->sampled_type);
1169 }
1170
1171 const glsl_type *glsl_replace_vector_type(const glsl_type *t, unsigned components);
1172 const glsl_type *glsl_channel_type(const glsl_type *t);
1173
1174 /**
1175 * Get the type resulting from a multiplication of \p type_a * \p type_b
1176 */
1177 const glsl_type *glsl_get_mul_type(const glsl_type *type_a, const glsl_type *type_b);
1178
1179 unsigned glsl_type_get_sampler_count(const glsl_type *t);
1180 unsigned glsl_type_get_texture_count(const glsl_type *t);
1181 unsigned glsl_type_get_image_count(const glsl_type *t);
1182
1183 /**
1184 * Calculate the number of vec4 slots required to hold this type.
1185 *
1186 * This is the underlying recursive type_size function for
1187 * count_attribute_slots() (vertex inputs and varyings) but also for
1188 * gallium's !PIPE_CAP_PACKED_UNIFORMS case.
1189 */
1190 unsigned glsl_count_vec4_slots(const glsl_type *t, bool is_gl_vertex_input, bool is_bindless);
1191
1192 /**
1193 * Calculate the number of vec4 slots required to hold this type.
1194 *
1195 * This is the underlying recursive type_size function for
1196 * gallium's PIPE_CAP_PACKED_UNIFORMS case.
1197 */
1198 unsigned glsl_count_dword_slots(const glsl_type *t, bool is_bindless);
1199
1200 /**
1201 * Calculate the number of components slots required to hold this type
1202 *
1203 * This is used to determine how many uniform or varying locations a type
1204 * might occupy.
1205 */
1206 unsigned glsl_get_component_slots(const glsl_type *t);
1207
1208 unsigned glsl_get_component_slots_aligned(const glsl_type *t, unsigned offset);
1209
1210 /**
1211 * Used to count the number of varyings contained in the type ignoring
1212 * innermost array elements.
1213 */
1214 unsigned glsl_varying_count(const glsl_type *t);
1215
1216 /**
1217 * Calculate the number of unique values from glGetUniformLocation for the
1218 * elements of the type.
1219 *
1220 * This is used to allocate slots in the UniformRemapTable, the amount of
1221 * locations may not match with actual used storage space by the driver.
1222 */
1223 unsigned glsl_type_uniform_locations(const glsl_type *t);
1224
1225 /**
1226 * Calculate the number of attribute slots required to hold this type
1227 *
1228 * This implements the language rules of GLSL 1.50 for counting the number
1229 * of slots used by a vertex attribute. It also determines the number of
1230 * varying slots the type will use up in the absence of varying packing
1231 * (and thus, it can be used to measure the number of varying slots used by
1232 * the varyings that are generated by lower_packed_varyings).
1233 *
1234 * For vertex shader attributes - doubles only take one slot.
1235 * For inter-shader varyings - dvec3/dvec4 take two slots.
1236 *
1237 * Vulkan doesn’t make this distinction so the argument should always be
1238 * false.
1239 */
1240 static inline unsigned
glsl_count_attribute_slots(const glsl_type * t,bool is_gl_vertex_input)1241 glsl_count_attribute_slots(const glsl_type *t, bool is_gl_vertex_input)
1242 {
1243 return glsl_count_vec4_slots(t, is_gl_vertex_input, true);
1244 }
1245
1246 /**
1247 * Size in bytes of this type in OpenCL memory
1248 */
1249 unsigned glsl_get_cl_size(const glsl_type *t);
1250
1251 /**
1252 * Alignment in bytes of the start of this type in OpenCL memory.
1253 */
1254 unsigned glsl_get_cl_alignment(const glsl_type *t);
1255
1256 void glsl_get_cl_type_size_align(const glsl_type *t,
1257 unsigned *size, unsigned *align);
1258
1259 /**
1260 * Get the type interface packing used internally. For shared and packing
1261 * layouts this is implementation defined.
1262 */
1263 enum glsl_interface_packing glsl_get_internal_ifc_packing(const glsl_type *t, bool std430_supported);
1264
1265 static inline enum glsl_interface_packing
glsl_get_ifc_packing(const glsl_type * t)1266 glsl_get_ifc_packing(const glsl_type *t)
1267 {
1268 return (enum glsl_interface_packing)t->interface_packing;
1269 }
1270
1271 /**
1272 * Alignment in bytes of the start of this type in a std140 uniform
1273 * block.
1274 */
1275 unsigned glsl_get_std140_base_alignment(const glsl_type *t, bool row_major);
1276
1277 /** Size in bytes of this type in a std140 uniform block.
1278 *
1279 * Note that this is not GL_UNIFORM_SIZE (which is the number of
1280 * elements in the array)
1281 */
1282 unsigned glsl_get_std140_size(const glsl_type *t, bool row_major);
1283
1284 /**
1285 * Calculate array stride in bytes of this type in a std430 shader storage
1286 * block.
1287 */
1288 unsigned glsl_get_std430_array_stride(const glsl_type *t, bool row_major);
1289
1290 /**
1291 * Alignment in bytes of the start of this type in a std430 shader
1292 * storage block.
1293 */
1294 unsigned glsl_get_std430_base_alignment(const glsl_type *t, bool row_major);
1295
1296 /**
1297 * Size in bytes of this type in a std430 shader storage block.
1298 *
1299 * Note that this is not GL_BUFFER_SIZE
1300 */
1301 unsigned glsl_get_std430_size(const glsl_type *t, bool row_major);
1302
1303 /**
1304 * Size in bytes of this type based on its explicit data.
1305 *
1306 * When using SPIR-V shaders (ARB_gl_spirv), memory layouts are expressed
1307 * through explicit offset, stride and matrix layout, so the size
1308 * can/should be computed used those values.
1309 *
1310 * Note that the value returned by this method is only correct if such
1311 * values are set, so only with SPIR-V shaders. Should not be used with
1312 * GLSL shaders.
1313 */
1314 unsigned glsl_get_explicit_size(const glsl_type *t, bool align_to_stride);
1315
1316 static inline unsigned
glsl_get_explicit_stride(const glsl_type * t)1317 glsl_get_explicit_stride(const glsl_type *t)
1318 {
1319 return t->explicit_stride;
1320 }
1321
1322 static inline unsigned
glsl_get_explicit_alignment(const glsl_type * t)1323 glsl_get_explicit_alignment(const glsl_type *t)
1324 {
1325 return t->explicit_alignment;
1326 }
1327
1328 /**
1329 * Gets an explicitly laid out type with the std140 layout.
1330 */
1331 const glsl_type *glsl_get_explicit_std140_type(const glsl_type *t, bool row_major);
1332
1333 /**
1334 * Gets an explicitly laid out type with the std430 layout.
1335 */
1336 const glsl_type *glsl_get_explicit_std430_type(const glsl_type *t, bool row_major);
1337
1338 /**
1339 * Gets an explicitly laid out interface type.
1340 */
1341 static inline const glsl_type *
glsl_get_explicit_interface_type(const glsl_type * t,bool supports_std430)1342 glsl_get_explicit_interface_type(const glsl_type *t, bool supports_std430)
1343 {
1344 enum glsl_interface_packing packing = glsl_get_internal_ifc_packing(t, supports_std430);
1345 if (packing == GLSL_INTERFACE_PACKING_STD140) {
1346 return glsl_get_explicit_std140_type(t, t->interface_row_major);
1347 } else {
1348 assert(packing == GLSL_INTERFACE_PACKING_STD430);
1349 return glsl_get_explicit_std430_type(t, t->interface_row_major);
1350 }
1351 }
1352
1353 void glsl_size_align_handle_array_and_structs(const glsl_type *type,
1354 glsl_type_size_align_func size_align,
1355 unsigned *size, unsigned *align);
1356 void glsl_get_natural_size_align_bytes(const glsl_type *t, unsigned *size, unsigned *align);
1357 void glsl_get_vec4_size_align_bytes(const glsl_type *type, unsigned *size, unsigned *align);
1358
1359 #ifdef __cplusplus
1360 } /* extern "C" */
1361 #endif
1362
1363 #endif /* GLSL_TYPES_H */
1364