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 
31 #include "shader_enums.h"
32 #include "c11/threads.h"
33 #include "util/blob.h"
34 #include "util/macros.h"
35 
36 #ifdef __cplusplus
37 #include "main/config.h"
38 #endif
39 
40 struct glsl_type;
41 
42 #ifdef __cplusplus
43 extern "C" {
44 #endif
45 
46 struct _mesa_glsl_parse_state;
47 struct glsl_symbol_table;
48 
49 extern void
50 glsl_type_singleton_init_or_ref();
51 
52 extern void
53 glsl_type_singleton_decref();
54 
55 extern void
56 _mesa_glsl_initialize_types(struct _mesa_glsl_parse_state *state);
57 
58 void encode_type_to_blob(struct blob *blob, const struct glsl_type *type);
59 
60 const struct glsl_type *decode_type_from_blob(struct blob_reader *blob);
61 
62 #ifdef __cplusplus
63 }
64 #endif
65 
66 typedef void (*glsl_type_size_align_func)(const struct glsl_type *type,
67                                           unsigned *size, unsigned *align);
68 
69 enum glsl_base_type {
70    /* Note: GLSL_TYPE_UINT, GLSL_TYPE_INT, and GLSL_TYPE_FLOAT must be 0, 1,
71     * and 2 so that they will fit in the 2 bits of glsl_type::sampled_type.
72     */
73    GLSL_TYPE_UINT = 0,
74    GLSL_TYPE_INT,
75    GLSL_TYPE_FLOAT,
76    GLSL_TYPE_FLOAT16,
77    GLSL_TYPE_DOUBLE,
78    GLSL_TYPE_UINT8,
79    GLSL_TYPE_INT8,
80    GLSL_TYPE_UINT16,
81    GLSL_TYPE_INT16,
82    GLSL_TYPE_UINT64,
83    GLSL_TYPE_INT64,
84    GLSL_TYPE_BOOL,
85    GLSL_TYPE_SAMPLER,
86    GLSL_TYPE_IMAGE,
87    GLSL_TYPE_ATOMIC_UINT,
88    GLSL_TYPE_STRUCT,
89    GLSL_TYPE_INTERFACE,
90    GLSL_TYPE_ARRAY,
91    GLSL_TYPE_VOID,
92    GLSL_TYPE_SUBROUTINE,
93    GLSL_TYPE_FUNCTION,
94    GLSL_TYPE_ERROR
95 };
96 
97 /* Return the bit size of a type. Note that this differs from
98  * glsl_get_bit_size in that it returns 32 bits for bools, whereas at
99  * the NIR level we would want to return 1 bit for bools.
100  */
glsl_base_type_bit_size(enum glsl_base_type type)101 static unsigned glsl_base_type_bit_size(enum glsl_base_type type)
102 {
103    switch (type) {
104    case GLSL_TYPE_BOOL:
105    case GLSL_TYPE_INT:
106    case GLSL_TYPE_UINT:
107    case GLSL_TYPE_FLOAT: /* TODO handle mediump */
108    case GLSL_TYPE_SUBROUTINE:
109       return 32;
110 
111    case GLSL_TYPE_FLOAT16:
112    case GLSL_TYPE_UINT16:
113    case GLSL_TYPE_INT16:
114       return 16;
115 
116    case GLSL_TYPE_UINT8:
117    case GLSL_TYPE_INT8:
118       return 8;
119 
120    case GLSL_TYPE_DOUBLE:
121    case GLSL_TYPE_INT64:
122    case GLSL_TYPE_UINT64:
123    case GLSL_TYPE_IMAGE:
124    case GLSL_TYPE_SAMPLER:
125       return 64;
126 
127    default:
128       /* For GLSL_TYPE_STRUCT etc, it should be ok to return 0. This usually
129        * happens when calling this method through is_64bit and is_16bit
130        * methods
131        */
132       return 0;
133    }
134 
135    return 0;
136 }
137 
glsl_base_type_is_16bit(enum glsl_base_type type)138 static inline bool glsl_base_type_is_16bit(enum glsl_base_type type)
139 {
140    return glsl_base_type_bit_size(type) == 16;
141 }
142 
glsl_base_type_is_64bit(enum glsl_base_type type)143 static inline bool glsl_base_type_is_64bit(enum glsl_base_type type)
144 {
145    return glsl_base_type_bit_size(type) == 64;
146 }
147 
glsl_base_type_is_integer(enum glsl_base_type type)148 static inline bool glsl_base_type_is_integer(enum glsl_base_type type)
149 {
150    return type == GLSL_TYPE_UINT8 ||
151           type == GLSL_TYPE_INT8 ||
152           type == GLSL_TYPE_UINT16 ||
153           type == GLSL_TYPE_INT16 ||
154           type == GLSL_TYPE_UINT ||
155           type == GLSL_TYPE_INT ||
156           type == GLSL_TYPE_UINT64 ||
157           type == GLSL_TYPE_INT64 ||
158           type == GLSL_TYPE_BOOL ||
159           type == GLSL_TYPE_SAMPLER ||
160           type == GLSL_TYPE_IMAGE;
161 }
162 
163 static inline unsigned int
glsl_base_type_get_bit_size(const enum glsl_base_type base_type)164 glsl_base_type_get_bit_size(const enum glsl_base_type base_type)
165 {
166    switch (base_type) {
167    case GLSL_TYPE_BOOL:
168       return 1;
169 
170    case GLSL_TYPE_INT:
171    case GLSL_TYPE_UINT:
172    case GLSL_TYPE_FLOAT: /* TODO handle mediump */
173    case GLSL_TYPE_SUBROUTINE:
174       return 32;
175 
176    case GLSL_TYPE_FLOAT16:
177    case GLSL_TYPE_UINT16:
178    case GLSL_TYPE_INT16:
179       return 16;
180 
181    case GLSL_TYPE_UINT8:
182    case GLSL_TYPE_INT8:
183       return 8;
184 
185    case GLSL_TYPE_DOUBLE:
186    case GLSL_TYPE_INT64:
187    case GLSL_TYPE_UINT64:
188    case GLSL_TYPE_IMAGE:
189    case GLSL_TYPE_SAMPLER:
190       return 64;
191 
192    default:
193       unreachable("unknown base type");
194    }
195 
196    return 0;
197 }
198 
199 static inline enum glsl_base_type
glsl_unsigned_base_type_of(enum glsl_base_type type)200 glsl_unsigned_base_type_of(enum glsl_base_type type)
201 {
202    switch (type) {
203    case GLSL_TYPE_INT:
204       return GLSL_TYPE_UINT;
205    case GLSL_TYPE_INT8:
206       return GLSL_TYPE_UINT8;
207    case GLSL_TYPE_INT16:
208       return GLSL_TYPE_UINT16;
209    case GLSL_TYPE_INT64:
210       return GLSL_TYPE_UINT64;
211    default:
212       assert(type == GLSL_TYPE_UINT ||
213              type == GLSL_TYPE_UINT8 ||
214              type == GLSL_TYPE_UINT16 ||
215              type == GLSL_TYPE_UINT64);
216       return type;
217    }
218 }
219 
220 enum glsl_sampler_dim {
221    GLSL_SAMPLER_DIM_1D = 0,
222    GLSL_SAMPLER_DIM_2D,
223    GLSL_SAMPLER_DIM_3D,
224    GLSL_SAMPLER_DIM_CUBE,
225    GLSL_SAMPLER_DIM_RECT,
226    GLSL_SAMPLER_DIM_BUF,
227    GLSL_SAMPLER_DIM_EXTERNAL,
228    GLSL_SAMPLER_DIM_MS,
229    GLSL_SAMPLER_DIM_SUBPASS, /* for vulkan input attachments */
230    GLSL_SAMPLER_DIM_SUBPASS_MS, /* for multisampled vulkan input attachments */
231 };
232 
233 enum glsl_matrix_layout {
234    /**
235     * The layout of the matrix is inherited from the object containing the
236     * matrix (the top level structure or the uniform block).
237     */
238    GLSL_MATRIX_LAYOUT_INHERITED,
239 
240    /**
241     * Explicit column-major layout
242     *
243     * If a uniform block doesn't have an explicit layout set, it will default
244     * to this layout.
245     */
246    GLSL_MATRIX_LAYOUT_COLUMN_MAJOR,
247 
248    /**
249     * Row-major layout
250     */
251    GLSL_MATRIX_LAYOUT_ROW_MAJOR
252 };
253 
254 enum {
255    GLSL_PRECISION_NONE = 0,
256    GLSL_PRECISION_HIGH,
257    GLSL_PRECISION_MEDIUM,
258    GLSL_PRECISION_LOW
259 };
260 
261 #ifdef __cplusplus
262 #include "GL/gl.h"
263 #include "util/ralloc.h"
264 #include "main/menums.h" /* for gl_texture_index, C++'s enum rules are broken */
265 
266 struct glsl_type {
267    GLenum gl_type;
268    glsl_base_type base_type:8;
269 
270    glsl_base_type sampled_type:8; /**< Type of data returned using this
271                                    * sampler or image.  Only \c
272                                    * GLSL_TYPE_FLOAT, \c GLSL_TYPE_INT,
273                                    * and \c GLSL_TYPE_UINT are valid.
274                                    */
275 
276    unsigned sampler_dimensionality:4; /**< \see glsl_sampler_dim */
277    unsigned sampler_shadow:1;
278    unsigned sampler_array:1;
279    unsigned interface_packing:2;
280    unsigned interface_row_major:1;
281 
282    /**
283     * For \c GLSL_TYPE_STRUCT this specifies if the struct is packed or not.
284     *
285     * Only used for Compute kernels
286     */
287    unsigned packed:1;
288 
289 private:
glsl_typeglsl_type290    glsl_type() : mem_ctx(NULL)
291    {
292       // Dummy constructor, just for the sake of ASSERT_BITFIELD_SIZE.
293    }
294 
295 public:
296    /**
297     * \name Vector and matrix element counts
298     *
299     * For scalars, each of these values will be 1.  For non-numeric types
300     * these will be 0.
301     */
302    /*@{*/
303    uint8_t vector_elements;    /**< 1, 2, 3, or 4 vector elements. */
304    uint8_t matrix_columns;     /**< 1, 2, 3, or 4 matrix columns. */
305    /*@}*/
306 
307    /**
308     * For \c GLSL_TYPE_ARRAY, this is the length of the array.  For
309     * \c GLSL_TYPE_STRUCT or \c GLSL_TYPE_INTERFACE, it is the number of
310     * elements in the structure and the number of values pointed to by
311     * \c fields.structure (below).
312     */
313    unsigned length;
314 
315    /**
316     * Name of the data type
317     *
318     * Will never be \c NULL.
319     */
320    const char *name;
321 
322    /**
323     * Explicit array, matrix, or vector stride.  This is used to communicate
324     * explicit array layouts from SPIR-V.  Should be 0 if the type has no
325     * explicit stride.
326     */
327    unsigned explicit_stride;
328 
329    /**
330     * Subtype of composite data types.
331     */
332    union {
333       const struct glsl_type *array;            /**< Type of array elements. */
334       struct glsl_function_param *parameters;   /**< Parameters to function. */
335       struct glsl_struct_field *structure;      /**< List of struct fields. */
336    } fields;
337 
338    /**
339     * \name Pointers to various public type singletons
340     */
341    /*@{*/
342 #undef  DECL_TYPE
343 #define DECL_TYPE(NAME, ...) \
344    static const glsl_type *const NAME##_type;
345 #undef  STRUCT_TYPE
346 #define STRUCT_TYPE(NAME) \
347    static const glsl_type *const struct_##NAME##_type;
348 #include "compiler/builtin_type_macros.h"
349    /*@}*/
350 
351    /**
352     * Convenience accessors for vector types (shorter than get_instance()).
353     * @{
354     */
355    static const glsl_type *vec(unsigned components, const glsl_type *const ts[]);
356    static const glsl_type *vec(unsigned components);
357    static const glsl_type *f16vec(unsigned components);
358    static const glsl_type *dvec(unsigned components);
359    static const glsl_type *ivec(unsigned components);
360    static const glsl_type *uvec(unsigned components);
361    static const glsl_type *bvec(unsigned components);
362    static const glsl_type *i64vec(unsigned components);
363    static const glsl_type *u64vec(unsigned components);
364    static const glsl_type *i16vec(unsigned components);
365    static const glsl_type *u16vec(unsigned components);
366    static const glsl_type *i8vec(unsigned components);
367    static const glsl_type *u8vec(unsigned components);
368    /**@}*/
369 
370    /**
371     * For numeric and boolean derived types returns the basic scalar type
372     *
373     * If the type is a numeric or boolean scalar, vector, or matrix type,
374     * this function gets the scalar type of the individual components.  For
375     * all other types, including arrays of numeric or boolean types, the
376     * error type is returned.
377     */
378    const glsl_type *get_base_type() const;
379 
380    /**
381     * Get the basic scalar type which this type aggregates.
382     *
383     * If the type is a numeric or boolean scalar, vector, or matrix, or an
384     * array of any of those, this function gets the scalar type of the
385     * individual components.  For structs and arrays of structs, this function
386     * returns the struct type.  For samplers and arrays of samplers, this
387     * function returns the sampler type.
388     */
389    const glsl_type *get_scalar_type() const;
390 
391    /**
392     * Gets the "bare" type without any decorations or layout information.
393     */
394    const glsl_type *get_bare_type() const;
395 
396    /**
397     * Get the instance of a built-in scalar, vector, or matrix type
398     */
399    static const glsl_type *get_instance(unsigned base_type, unsigned rows,
400                                         unsigned columns,
401                                         unsigned explicit_stride = 0,
402                                         bool row_major = false);
403 
404    /**
405     * Get the instance of a sampler type
406     */
407    static const glsl_type *get_sampler_instance(enum glsl_sampler_dim dim,
408                                                 bool shadow,
409                                                 bool array,
410                                                 glsl_base_type type);
411 
412    static const glsl_type *get_image_instance(enum glsl_sampler_dim dim,
413                                               bool array, glsl_base_type type);
414 
415    /**
416     * Get the instance of an array type
417     */
418    static const glsl_type *get_array_instance(const glsl_type *base,
419                                               unsigned elements,
420                                               unsigned explicit_stride = 0);
421 
422    /**
423     * Get the instance of a record type
424     */
425    static const glsl_type *get_struct_instance(const glsl_struct_field *fields,
426 					       unsigned num_fields,
427 					       const char *name,
428 					       bool packed = false);
429 
430    /**
431     * Get the instance of an interface block type
432     */
433    static const glsl_type *get_interface_instance(const glsl_struct_field *fields,
434 						  unsigned num_fields,
435 						  enum glsl_interface_packing packing,
436 						  bool row_major,
437 						  const char *block_name);
438 
439    /**
440     * Get the instance of an subroutine type
441     */
442    static const glsl_type *get_subroutine_instance(const char *subroutine_name);
443 
444    /**
445     * Get the instance of a function type
446     */
447    static const glsl_type *get_function_instance(const struct glsl_type *return_type,
448                                                  const glsl_function_param *parameters,
449                                                  unsigned num_params);
450 
451    /**
452     * Get the type resulting from a multiplication of \p type_a * \p type_b
453     */
454    static const glsl_type *get_mul_type(const glsl_type *type_a,
455                                         const glsl_type *type_b);
456 
457    /**
458     * Query the total number of scalars that make up a scalar, vector or matrix
459     */
componentsglsl_type460    unsigned components() const
461    {
462       return vector_elements * matrix_columns;
463    }
464 
465    /**
466     * Calculate the number of components slots required to hold this type
467     *
468     * This is used to determine how many uniform or varying locations a type
469     * might occupy.
470     */
471    unsigned component_slots() const;
472 
473    /**
474     * Calculate offset between the base location of the struct in
475     * uniform storage and a struct member.
476     * For the initial call, length is the index of the member to find the
477     * offset for.
478     */
479    unsigned struct_location_offset(unsigned length) const;
480 
481    /**
482     * Calculate the number of unique values from glGetUniformLocation for the
483     * elements of the type.
484     *
485     * This is used to allocate slots in the UniformRemapTable, the amount of
486     * locations may not match with actual used storage space by the driver.
487     */
488    unsigned uniform_locations() const;
489 
490    /**
491     * Used to count the number of varyings contained in the type ignoring
492     * innermost array elements.
493     */
494    unsigned varying_count() const;
495 
496    /**
497     * Calculate the number of vec4 slots required to hold this type.
498     *
499     * This is the underlying recursive type_size function for
500     * count_attribute_slots() (vertex inputs and varyings) but also for
501     * gallium's !PIPE_CAP_PACKED_UNIFORMS case.
502     */
503    unsigned count_vec4_slots(bool is_gl_vertex_input, bool bindless) const;
504 
505    /**
506     * Calculate the number of vec4 slots required to hold this type.
507     *
508     * This is the underlying recursive type_size function for
509     * gallium's PIPE_CAP_PACKED_UNIFORMS case.
510     */
511    unsigned count_dword_slots(bool bindless) const;
512 
513    /**
514     * Calculate the number of attribute slots required to hold this type
515     *
516     * This implements the language rules of GLSL 1.50 for counting the number
517     * of slots used by a vertex attribute.  It also determines the number of
518     * varying slots the type will use up in the absence of varying packing
519     * (and thus, it can be used to measure the number of varying slots used by
520     * the varyings that are generated by lower_packed_varyings).
521     *
522     * For vertex shader attributes - doubles only take one slot.
523     * For inter-shader varyings - dvec3/dvec4 take two slots.
524     *
525     * Vulkan doesn’t make this distinction so the argument should always be
526     * false.
527     */
count_attribute_slotsglsl_type528    unsigned count_attribute_slots(bool is_gl_vertex_input) const {
529       return count_vec4_slots(is_gl_vertex_input, true);
530    }
531 
532    /**
533     * Alignment in bytes of the start of this type in a std140 uniform
534     * block.
535     */
536    unsigned std140_base_alignment(bool row_major) const;
537 
538    /** Size in bytes of this type in a std140 uniform block.
539     *
540     * Note that this is not GL_UNIFORM_SIZE (which is the number of
541     * elements in the array)
542     */
543    unsigned std140_size(bool row_major) const;
544 
545    /**
546     * Gets an explicitly laid out type with the std140 layout.
547     */
548    const glsl_type *get_explicit_std140_type(bool row_major) const;
549 
550    /**
551     * Alignment in bytes of the start of this type in a std430 shader
552     * storage block.
553     */
554    unsigned std430_base_alignment(bool row_major) const;
555 
556    /**
557     * Calculate array stride in bytes of this type in a std430 shader storage
558     * block.
559     */
560    unsigned std430_array_stride(bool row_major) const;
561 
562    /**
563     * Size in bytes of this type in a std430 shader storage block.
564     *
565     * Note that this is not GL_BUFFER_SIZE
566     */
567    unsigned std430_size(bool row_major) const;
568 
569    /**
570     * Gets an explicitly laid out type with the std430 layout.
571     */
572    const glsl_type *get_explicit_std430_type(bool row_major) const;
573 
574    /**
575     * Gets an explicitly laid out interface type.
576     */
577    const glsl_type *get_explicit_interface_type(bool supports_std430) const;
578 
579    /** Returns an explicitly laid out type given a type and size/align func
580     *
581     * The size/align func is only called for scalar and vector types and the
582     * returned type is otherwise laid out in the natural way as follows:
583     *
584     *  - Arrays and matrices have a stride of ALIGN(elem_size, elem_align).
585     *
586     *  - Structure types have their elements in-order and as tightly packed as
587     *    possible following the alignment required by the size/align func.
588     *
589     *  - All composite types (structures, matrices, and arrays) have an
590     *    alignment equal to the highest alighment of any member of the composite.
591     *
592     * The types returned by this function are likely not suitable for most UBO
593     * or SSBO layout because they do not add the extra array and substructure
594     * alignment that is required by std140 and std430.
595     */
596    const glsl_type *get_explicit_type_for_size_align(glsl_type_size_align_func type_info,
597                                                      unsigned *size, unsigned *align) const;
598 
599    /**
600     * Alignment in bytes of the start of this type in OpenCL memory.
601     */
602    unsigned cl_alignment() const;
603 
604    /**
605     * Size in bytes of this type in OpenCL memory
606     */
607    unsigned cl_size() const;
608 
609    /**
610     * Size in bytes of this type based on its explicit data.
611     *
612     * When using SPIR-V shaders (ARB_gl_spirv), memory layouts are expressed
613     * through explicit offset, stride and matrix layout, so the size
614     * can/should be computed used those values.
615     *
616     * Note that the value returned by this method is only correct if such
617     * values are set, so only with SPIR-V shaders. Should not be used with
618     * GLSL shaders.
619     */
620    unsigned explicit_size(bool align_to_stride=false) const;
621 
622    /**
623     * \brief Can this type be implicitly converted to another?
624     *
625     * \return True if the types are identical or if this type can be converted
626     *         to \c desired according to Section 4.1.10 of the GLSL spec.
627     *
628     * \verbatim
629     * From page 25 (31 of the pdf) of the GLSL 1.50 spec, Section 4.1.10
630     * Implicit Conversions:
631     *
632     *     In some situations, an expression and its type will be implicitly
633     *     converted to a different type. The following table shows all allowed
634     *     implicit conversions:
635     *
636     *     Type of expression | Can be implicitly converted to
637     *     --------------------------------------------------
638     *     int                  float
639     *     uint
640     *
641     *     ivec2                vec2
642     *     uvec2
643     *
644     *     ivec3                vec3
645     *     uvec3
646     *
647     *     ivec4                vec4
648     *     uvec4
649     *
650     *     There are no implicit array or structure conversions. For example,
651     *     an array of int cannot be implicitly converted to an array of float.
652     *     There are no implicit conversions between signed and unsigned
653     *     integers.
654     * \endverbatim
655     */
656    bool can_implicitly_convert_to(const glsl_type *desired,
657                                   _mesa_glsl_parse_state *state) const;
658 
659    /**
660     * Query whether or not a type is a scalar (non-vector and non-matrix).
661     */
is_scalarglsl_type662    bool is_scalar() const
663    {
664       return (vector_elements == 1)
665 	 && (base_type >= GLSL_TYPE_UINT)
666 	 && (base_type <= GLSL_TYPE_IMAGE);
667    }
668 
669    /**
670     * Query whether or not a type is a vector
671     */
is_vectorglsl_type672    bool is_vector() const
673    {
674       return (vector_elements > 1)
675 	 && (matrix_columns == 1)
676 	 && (base_type >= GLSL_TYPE_UINT)
677 	 && (base_type <= GLSL_TYPE_BOOL);
678    }
679 
680    /**
681     * Query whether or not a type is a matrix
682     */
is_matrixglsl_type683    bool is_matrix() const
684    {
685       /* GLSL only has float matrices. */
686       return (matrix_columns > 1) && (base_type == GLSL_TYPE_FLOAT ||
687                                       base_type == GLSL_TYPE_DOUBLE ||
688                                       base_type == GLSL_TYPE_FLOAT16);
689    }
690 
691    /**
692     * Query whether or not a type is a non-array numeric type
693     */
is_numericglsl_type694    bool is_numeric() const
695    {
696       return (base_type >= GLSL_TYPE_UINT) && (base_type <= GLSL_TYPE_INT64);
697    }
698 
699    /**
700     * Query whether or not a type is an integer.
701     */
is_integerglsl_type702    bool is_integer() const
703    {
704       return glsl_base_type_is_integer(base_type);
705    }
706 
707    /**
708     * Query whether or not a type is an 32-bit integer.
709     */
is_integer_32glsl_type710    bool is_integer_32() const
711    {
712       return (base_type == GLSL_TYPE_UINT) || (base_type == GLSL_TYPE_INT);
713    }
714 
715    /**
716     * Query whether or not a type is a 64-bit integer.
717     */
is_integer_64glsl_type718    bool is_integer_64() const
719    {
720       return base_type == GLSL_TYPE_UINT64 || base_type == GLSL_TYPE_INT64;
721    }
722 
723    /**
724     * Query whether or not a type is a 32-bit or 64-bit integer
725     */
is_integer_32_64glsl_type726    bool is_integer_32_64() const
727    {
728       return is_integer_32() || is_integer_64();
729    }
730 
731    /**
732     * Query whether or not type is an integral type, or for struct and array
733     * types, contains an integral type.
734     */
735    bool contains_integer() const;
736 
737    /**
738     * Query whether or not type is a double type, or for struct, interface and
739     * array types, contains a double type.
740     */
741    bool contains_double() const;
742 
743    /**
744     * Query whether or not type is a 64-bit type, or for struct, interface and
745     * array types, contains a double type.
746     */
747    bool contains_64bit() const;
748 
749    /**
750     * Query whether or not a type is a float type
751     */
is_floatglsl_type752    bool is_float() const
753    {
754       return base_type == GLSL_TYPE_FLOAT;
755    }
756 
757    /**
758     * Query whether or not a type is a double type
759     */
is_doubleglsl_type760    bool is_double() const
761    {
762       return base_type == GLSL_TYPE_DOUBLE;
763    }
764 
765    /**
766     * Query whether a 64-bit type takes two slots.
767     */
is_dual_slotglsl_type768    bool is_dual_slot() const
769    {
770       return is_64bit() && vector_elements > 2;
771    }
772 
773    /**
774     * Query whether or not a type is 64-bit
775     */
is_64bitglsl_type776    bool is_64bit() const
777    {
778       return glsl_base_type_is_64bit(base_type);
779    }
780 
781    /**
782     * Query whether or not a type is 16-bit
783     */
is_16bitglsl_type784    bool is_16bit() const
785    {
786       return glsl_base_type_is_16bit(base_type);
787    }
788 
789    /**
790     * Query whether or not a type is 32-bit
791     */
is_32bitglsl_type792    bool is_32bit() const
793    {
794       return base_type == GLSL_TYPE_UINT ||
795              base_type == GLSL_TYPE_INT ||
796              base_type == GLSL_TYPE_FLOAT;
797    }
798 
799    /**
800     * Query whether or not a type is a non-array boolean type
801     */
is_booleanglsl_type802    bool is_boolean() const
803    {
804       return base_type == GLSL_TYPE_BOOL;
805    }
806 
807    /**
808     * Query whether or not a type is a sampler
809     */
is_samplerglsl_type810    bool is_sampler() const
811    {
812       return base_type == GLSL_TYPE_SAMPLER;
813    }
814 
815    /**
816     * Query whether or not type is a sampler, or for struct, interface and
817     * array types, contains a sampler.
818     */
819    bool contains_sampler() const;
820 
821    /**
822     * Query whether or not type is an array or for struct, interface and
823     * array types, contains an array.
824     */
825    bool contains_array() const;
826 
827    /**
828     * Get the Mesa texture target index for a sampler type.
829     */
830    gl_texture_index sampler_index() const;
831 
832    /**
833     * Query whether or not type is an image, or for struct, interface and
834     * array types, contains an image.
835     */
836    bool contains_image() const;
837 
838    /**
839     * Query whether or not a type is an image
840     */
is_imageglsl_type841    bool is_image() const
842    {
843       return base_type == GLSL_TYPE_IMAGE;
844    }
845 
846    /**
847     * Query whether or not a type is an array
848     */
is_arrayglsl_type849    bool is_array() const
850    {
851       return base_type == GLSL_TYPE_ARRAY;
852    }
853 
is_array_of_arraysglsl_type854    bool is_array_of_arrays() const
855    {
856       return is_array() && fields.array->is_array();
857    }
858 
859    /**
860     * Query whether or not a type is a record
861     */
is_structglsl_type862    bool is_struct() const
863    {
864       return base_type == GLSL_TYPE_STRUCT;
865    }
866 
867    /**
868     * Query whether or not a type is an interface
869     */
is_interfaceglsl_type870    bool is_interface() const
871    {
872       return base_type == GLSL_TYPE_INTERFACE;
873    }
874 
875    /**
876     * Query whether or not a type is the void type singleton.
877     */
is_voidglsl_type878    bool is_void() const
879    {
880       return base_type == GLSL_TYPE_VOID;
881    }
882 
883    /**
884     * Query whether or not a type is the error type singleton.
885     */
is_errorglsl_type886    bool is_error() const
887    {
888       return base_type == GLSL_TYPE_ERROR;
889    }
890 
891    /**
892     * Query if a type is unnamed/anonymous (named by the parser)
893     */
894 
is_subroutineglsl_type895    bool is_subroutine() const
896    {
897       return base_type == GLSL_TYPE_SUBROUTINE;
898    }
899    bool contains_subroutine() const;
900 
is_anonymousglsl_type901    bool is_anonymous() const
902    {
903       return !strncmp(name, "#anon", 5);
904    }
905 
906    /**
907     * Get the type stripped of any arrays
908     *
909     * \return
910     * Pointer to the type of elements of the first non-array type for array
911     * types, or pointer to itself for non-array types.
912     */
without_arrayglsl_type913    const glsl_type *without_array() const
914    {
915       const glsl_type *t = this;
916 
917       while (t->is_array())
918          t = t->fields.array;
919 
920       return t;
921    }
922 
923    /**
924     * Return the total number of elements in an array including the elements
925     * in arrays of arrays.
926     */
arrays_of_arrays_sizeglsl_type927    unsigned arrays_of_arrays_size() const
928    {
929       if (!is_array())
930          return 0;
931 
932       unsigned size = length;
933       const glsl_type *base_type = fields.array;
934 
935       while (base_type->is_array()) {
936          size = size * base_type->length;
937          base_type = base_type->fields.array;
938       }
939       return size;
940    }
941 
942    /**
943     * Return bit size for this type.
944     */
bit_sizeglsl_type945    unsigned bit_size() const
946    {
947       return glsl_base_type_bit_size(this->base_type);
948    }
949 
950 
951    /**
952     * Query whether or not a type is an atomic_uint.
953     */
is_atomic_uintglsl_type954    bool is_atomic_uint() const
955    {
956       return base_type == GLSL_TYPE_ATOMIC_UINT;
957    }
958 
959    /**
960     * Return the amount of atomic counter storage required for a type.
961     */
atomic_sizeglsl_type962    unsigned atomic_size() const
963    {
964       if (is_atomic_uint())
965          return ATOMIC_COUNTER_SIZE;
966       else if (is_array())
967          return length * fields.array->atomic_size();
968       else
969          return 0;
970    }
971 
972    /**
973     * Return whether a type contains any atomic counters.
974     */
contains_atomicglsl_type975    bool contains_atomic() const
976    {
977       return atomic_size() > 0;
978    }
979 
980    /**
981     * Return whether a type contains any opaque types.
982     */
983    bool contains_opaque() const;
984 
985    /**
986     * Query the full type of a matrix row
987     *
988     * \return
989     * If the type is not a matrix, \c glsl_type::error_type is returned.
990     * Otherwise a type matching the rows of the matrix is returned.
991     */
row_typeglsl_type992    const glsl_type *row_type() const
993    {
994       if (!is_matrix())
995          return error_type;
996 
997       if (explicit_stride && !interface_row_major)
998          return get_instance(base_type, matrix_columns, 1, explicit_stride);
999       else
1000          return get_instance(base_type, matrix_columns, 1);
1001    }
1002 
1003    /**
1004     * Query the full type of a matrix column
1005     *
1006     * \return
1007     * If the type is not a matrix, \c glsl_type::error_type is returned.
1008     * Otherwise a type matching the columns of the matrix is returned.
1009     */
column_typeglsl_type1010    const glsl_type *column_type() const
1011    {
1012       if (!is_matrix())
1013          return error_type;
1014 
1015       if (explicit_stride && interface_row_major)
1016          return get_instance(base_type, vector_elements, 1, explicit_stride);
1017       else
1018          return get_instance(base_type, vector_elements, 1);
1019    }
1020 
1021    /**
1022     * Get the type of a structure field
1023     *
1024     * \return
1025     * Pointer to the type of the named field.  If the type is not a structure
1026     * or the named field does not exist, \c glsl_type::error_type is returned.
1027     */
1028    const glsl_type *field_type(const char *name) const;
1029 
1030    /**
1031     * Get the location of a field within a record type
1032     */
1033    int field_index(const char *name) const;
1034 
1035    /**
1036     * Query the number of elements in an array type
1037     *
1038     * \return
1039     * The number of elements in the array for array types or -1 for non-array
1040     * types.  If the number of elements in the array has not yet been declared,
1041     * zero is returned.
1042     */
array_sizeglsl_type1043    int array_size() const
1044    {
1045       return is_array() ? length : -1;
1046    }
1047 
1048    /**
1049     * Query whether the array size for all dimensions has been declared.
1050     */
is_unsized_arrayglsl_type1051    bool is_unsized_array() const
1052    {
1053       return is_array() && length == 0;
1054    }
1055 
1056    /**
1057     * Return the number of coordinate components needed for this
1058     * sampler or image type.
1059     *
1060     * This is based purely on the sampler's dimensionality.  For example, this
1061     * returns 1 for sampler1D, and 3 for sampler2DArray.
1062     *
1063     * Note that this is often different than actual coordinate type used in
1064     * a texturing built-in function, since those pack additional values (such
1065     * as the shadow comparator or projector) into the coordinate type.
1066     */
1067    int coordinate_components() const;
1068 
1069    /**
1070     * Compares whether this type matches another type without taking into
1071     * account the precision in structures.
1072     *
1073     * This is applied recursively so that structures containing structure
1074     * members can also ignore the precision.
1075     */
1076    bool compare_no_precision(const glsl_type *b) const;
1077 
1078    /**
1079     * Compare a record type against another record type.
1080     *
1081     * This is useful for matching record types declared on the same shader
1082     * stage as well as across different shader stages.
1083     * The option to not match name is needed for matching record types
1084     * declared across different shader stages.
1085     * The option to not match locations is to deal with places where the
1086     * same struct is defined in a block which has a location set on it.
1087     */
1088    bool record_compare(const glsl_type *b, bool match_name,
1089                        bool match_locations = true,
1090                        bool match_precision = true) const;
1091 
1092    /**
1093     * Get the type interface packing.
1094     */
get_interface_packingglsl_type1095    enum glsl_interface_packing get_interface_packing() const
1096    {
1097       return (enum glsl_interface_packing)interface_packing;
1098    }
1099 
1100    /**
1101     * Get the type interface packing used internally. For shared and packing
1102     * layouts this is implementation defined.
1103     */
get_internal_ifc_packingglsl_type1104    enum glsl_interface_packing get_internal_ifc_packing(bool std430_supported) const
1105    {
1106       enum glsl_interface_packing packing = this->get_interface_packing();
1107       if (packing == GLSL_INTERFACE_PACKING_STD140 ||
1108           (!std430_supported &&
1109            (packing == GLSL_INTERFACE_PACKING_SHARED ||
1110             packing == GLSL_INTERFACE_PACKING_PACKED))) {
1111          return GLSL_INTERFACE_PACKING_STD140;
1112       } else {
1113          assert(packing == GLSL_INTERFACE_PACKING_STD430 ||
1114                 (std430_supported &&
1115                  (packing == GLSL_INTERFACE_PACKING_SHARED ||
1116                   packing == GLSL_INTERFACE_PACKING_PACKED)));
1117          return GLSL_INTERFACE_PACKING_STD430;
1118       }
1119    }
1120 
1121    /**
1122     * Check if the type interface is row major
1123     */
get_interface_row_majorglsl_type1124    bool get_interface_row_major() const
1125    {
1126       return (bool) interface_row_major;
1127    }
1128 
1129    ~glsl_type();
1130 
1131 private:
1132 
1133    static mtx_t hash_mutex;
1134 
1135    /**
1136     * ralloc context for the type itself.
1137     */
1138    void *mem_ctx;
1139 
1140    /** Constructor for vector and matrix types */
1141    glsl_type(GLenum gl_type,
1142              glsl_base_type base_type, unsigned vector_elements,
1143              unsigned matrix_columns, const char *name,
1144              unsigned explicit_stride = 0, bool row_major = false);
1145 
1146    /** Constructor for sampler or image types */
1147    glsl_type(GLenum gl_type, glsl_base_type base_type,
1148 	     enum glsl_sampler_dim dim, bool shadow, bool array,
1149 	     glsl_base_type type, const char *name);
1150 
1151    /** Constructor for record types */
1152    glsl_type(const glsl_struct_field *fields, unsigned num_fields,
1153 	     const char *name, bool packed = false);
1154 
1155    /** Constructor for interface types */
1156    glsl_type(const glsl_struct_field *fields, unsigned num_fields,
1157 	     enum glsl_interface_packing packing,
1158 	     bool row_major, const char *name);
1159 
1160    /** Constructor for interface types */
1161    glsl_type(const glsl_type *return_type,
1162              const glsl_function_param *params, unsigned num_params);
1163 
1164    /** Constructors for array types */
1165    glsl_type(const glsl_type *array, unsigned length, unsigned explicit_stride);
1166 
1167    /** Constructor for subroutine types */
1168    glsl_type(const char *name);
1169 
1170    /** Hash table containing the known explicit matrix and vector types. */
1171    static struct hash_table *explicit_matrix_types;
1172 
1173    /** Hash table containing the known array types. */
1174    static struct hash_table *array_types;
1175 
1176    /** Hash table containing the known struct types. */
1177    static struct hash_table *struct_types;
1178 
1179    /** Hash table containing the known interface types. */
1180    static struct hash_table *interface_types;
1181 
1182    /** Hash table containing the known subroutine types. */
1183    static struct hash_table *subroutine_types;
1184 
1185    /** Hash table containing the known function types. */
1186    static struct hash_table *function_types;
1187 
1188    static bool record_key_compare(const void *a, const void *b);
1189    static unsigned record_key_hash(const void *key);
1190 
1191    /**
1192     * \name Built-in type flyweights
1193     */
1194    /*@{*/
1195 #undef  DECL_TYPE
1196 #define DECL_TYPE(NAME, ...) static const glsl_type _##NAME##_type;
1197 #undef  STRUCT_TYPE
1198 #define STRUCT_TYPE(NAME)        static const glsl_type _struct_##NAME##_type;
1199 #include "compiler/builtin_type_macros.h"
1200    /*@}*/
1201 
1202    /**
1203     * \name Friend functions.
1204     *
1205     * These functions are friends because they must have C linkage and the
1206     * need to call various private methods or access various private static
1207     * data.
1208     */
1209    /*@{*/
1210    friend void glsl_type_singleton_init_or_ref(void);
1211    friend void glsl_type_singleton_decref(void);
1212    friend void _mesa_glsl_initialize_types(struct _mesa_glsl_parse_state *);
1213    /*@}*/
1214 };
1215 
1216 #undef DECL_TYPE
1217 #undef STRUCT_TYPE
1218 #endif /* __cplusplus */
1219 
1220 struct glsl_struct_field {
1221    const struct glsl_type *type;
1222    const char *name;
1223 
1224    /**
1225     * For interface blocks, gl_varying_slot corresponding to the input/output
1226     * if this is a built-in input/output (i.e. a member of the built-in
1227     * gl_PerVertex interface block); -1 otherwise.
1228     *
1229     * Ignored for structs.
1230     */
1231    int location;
1232 
1233    /**
1234     * For interface blocks, members may have an explicit byte offset
1235     * specified; -1 otherwise. Also used for xfb_offset layout qualifier.
1236     *
1237     * Unless used for xfb_offset this field is ignored for structs.
1238     */
1239    int offset;
1240 
1241    /**
1242     * For interface blocks, members may define a transform feedback buffer;
1243     * -1 otherwise.
1244     */
1245    int xfb_buffer;
1246 
1247    /**
1248     * For interface blocks, members may define a transform feedback stride;
1249     * -1 otherwise.
1250     */
1251    int xfb_stride;
1252 
1253    /**
1254     * For interface blocks, the interpolation mode (as in
1255     * ir_variable::interpolation).  0 otherwise.
1256     */
1257    unsigned interpolation:3;
1258 
1259    /**
1260     * For interface blocks, 1 if this variable uses centroid interpolation (as
1261     * in ir_variable::centroid).  0 otherwise.
1262     */
1263    unsigned centroid:1;
1264 
1265    /**
1266     * For interface blocks, 1 if this variable uses sample interpolation (as
1267     * in ir_variable::sample). 0 otherwise.
1268     */
1269    unsigned sample:1;
1270 
1271    /**
1272     * Layout of the matrix.  Uses glsl_matrix_layout values.
1273     */
1274    unsigned matrix_layout:2;
1275 
1276    /**
1277     * For interface blocks, 1 if this variable is a per-patch input or output
1278     * (as in ir_variable::patch). 0 otherwise.
1279     */
1280    unsigned patch:1;
1281 
1282    /**
1283     * Precision qualifier
1284     */
1285    unsigned precision:2;
1286 
1287    /**
1288     * Memory qualifiers, applicable to buffer variables defined in shader
1289     * storage buffer objects (SSBOs)
1290     */
1291    unsigned memory_read_only:1;
1292    unsigned memory_write_only:1;
1293    unsigned memory_coherent:1;
1294    unsigned memory_volatile:1;
1295    unsigned memory_restrict:1;
1296 
1297    /**
1298     * Layout format, applicable to image variables only.
1299     */
1300    unsigned image_format:16;
1301 
1302    /**
1303     * Any of the xfb_* qualifiers trigger the shader to be in transform
1304     * feedback mode so we need to keep track of whether the buffer was
1305     * explicitly set or if its just been assigned the default global value.
1306     */
1307    unsigned explicit_xfb_buffer:1;
1308 
1309    unsigned implicit_sized_array:1;
1310 #ifdef __cplusplus
1311 #define DEFAULT_CONSTRUCTORS(_type, _precision, _name)                  \
1312    type(_type), name(_name), location(-1), offset(-1), xfb_buffer(0),   \
1313    xfb_stride(0), interpolation(0), centroid(0),                        \
1314    sample(0), matrix_layout(GLSL_MATRIX_LAYOUT_INHERITED), patch(0),    \
1315    precision(_precision), memory_read_only(0),                          \
1316    memory_write_only(0), memory_coherent(0), memory_volatile(0),        \
1317    memory_restrict(0), image_format(0), explicit_xfb_buffer(0),         \
1318    implicit_sized_array(0)
1319 
glsl_struct_fieldglsl_struct_field1320    glsl_struct_field(const struct glsl_type *_type,
1321                      int _precision,
1322                      const char *_name)
1323       : DEFAULT_CONSTRUCTORS(_type, _precision, _name)
1324    {
1325       /* empty */
1326    }
1327 
glsl_struct_fieldglsl_struct_field1328    glsl_struct_field(const struct glsl_type *_type, const char *_name)
1329       : DEFAULT_CONSTRUCTORS(_type, GLSL_PRECISION_NONE, _name)
1330    {
1331       /* empty */
1332    }
1333 
glsl_struct_fieldglsl_struct_field1334    glsl_struct_field()
1335       : DEFAULT_CONSTRUCTORS(NULL, GLSL_PRECISION_NONE, NULL)
1336    {
1337       /* empty */
1338    }
1339 #undef DEFAULT_CONSTRUCTORS
1340 #endif
1341 };
1342 
1343 struct glsl_function_param {
1344    const struct glsl_type *type;
1345 
1346    bool in;
1347    bool out;
1348 };
1349 
1350 static inline unsigned int
glsl_align(unsigned int a,unsigned int align)1351 glsl_align(unsigned int a, unsigned int align)
1352 {
1353    return (a + align - 1) / align * align;
1354 }
1355 
1356 #endif /* GLSL_TYPES_H */
1357