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