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 #pragma once
26 #ifndef GLSL_TYPES_H
27 #define GLSL_TYPES_H
28 
29 #include <string.h>
30 #include <assert.h>
31 #include "c99_compat.h"
32 #include "main/mtypes.h" /* for gl_texture_index, C++'s enum rules are broken */
33 
34 #ifdef __cplusplus
35 extern "C" {
36 #endif
37 
38 struct _mesa_glsl_parse_state;
39 struct glsl_symbol_table;
40 
41 extern void
42 _mesa_glsl_initialize_types(struct _mesa_glsl_parse_state *state);
43 
44 extern void
45 _mesa_glsl_release_types(void);
46 
47 #ifdef __cplusplus
48 }
49 #endif
50 
51 enum glsl_base_type {
52    GLSL_TYPE_UINT = 0,
53    GLSL_TYPE_INT,
54    GLSL_TYPE_FLOAT,
55    GLSL_TYPE_BOOL,
56    GLSL_TYPE_SAMPLER,
57    GLSL_TYPE_IMAGE,
58    GLSL_TYPE_ATOMIC_UINT,
59    GLSL_TYPE_STRUCT,
60    GLSL_TYPE_INTERFACE,
61    GLSL_TYPE_ARRAY,
62    GLSL_TYPE_VOID,
63    GLSL_TYPE_ERROR
64 };
65 
66 enum glsl_sampler_dim {
67    GLSL_SAMPLER_DIM_1D = 0,
68    GLSL_SAMPLER_DIM_2D,
69    GLSL_SAMPLER_DIM_3D,
70    GLSL_SAMPLER_DIM_CUBE,
71    GLSL_SAMPLER_DIM_RECT,
72    GLSL_SAMPLER_DIM_BUF,
73    GLSL_SAMPLER_DIM_EXTERNAL,
74    GLSL_SAMPLER_DIM_MS
75 };
76 
77 enum glsl_interface_packing {
78    GLSL_INTERFACE_PACKING_STD140,
79    GLSL_INTERFACE_PACKING_SHARED,
80    GLSL_INTERFACE_PACKING_PACKED
81 };
82 
83 enum glsl_precision {
84 	glsl_precision_high = 0,
85 	glsl_precision_medium,
86 	glsl_precision_low,
87 	glsl_precision_undefined,
88 };
89 
90 enum glsl_matrix_layout {
91    /**
92     * The layout of the matrix is inherited from the object containing the
93     * matrix (the top level structure or the uniform block).
94     */
95    GLSL_MATRIX_LAYOUT_INHERITED,
96 
97    /**
98     * Explicit column-major layout
99     *
100     * If a uniform block doesn't have an explicit layout set, it will default
101     * to this layout.
102     */
103    GLSL_MATRIX_LAYOUT_COLUMN_MAJOR,
104 
105    /**
106     * Row-major layout
107     */
108    GLSL_MATRIX_LAYOUT_ROW_MAJOR
109 };
110 
111 #ifdef __cplusplus
112 #include "../mesa/main/glminimal.h"
113 #include "util/ralloc.h"
114 
115 struct glsl_type {
116    GLenum gl_type;
117    glsl_base_type base_type;
118 
119    unsigned sampler_dimensionality:3; /**< \see glsl_sampler_dim */
120    unsigned sampler_shadow:1;
121    unsigned sampler_array:1;
122    unsigned sampler_type:2;    /**< Type of data returned using this
123 				* sampler or image.  Only \c
124 				* GLSL_TYPE_FLOAT, \c GLSL_TYPE_INT,
125 				* and \c GLSL_TYPE_UINT are valid.
126 				*/
127    unsigned interface_packing:2;
128 
129    /* Callers of this ralloc-based new need not call delete. It's
130     * easier to just ralloc_free 'mem_ctx' (or any of its ancestors). */
newglsl_type131    static void* operator new(size_t size)
132    {
133       if (glsl_type::mem_ctx == NULL) {
134 	 glsl_type::mem_ctx = ralloc_context(NULL);
135 	 assert(glsl_type::mem_ctx != NULL);
136       }
137 
138       void *type;
139 
140       type = ralloc_size(glsl_type::mem_ctx, size);
141       assert(type != NULL);
142 
143       return type;
144    }
145 
146    /* If the user *does* call delete, that's OK, we will just
147     * ralloc_free in that case. */
deleteglsl_type148    static void operator delete(void *type)
149    {
150       ralloc_free(type);
151    }
152 
153    /**
154     * \name Vector and matrix element counts
155     *
156     * For scalars, each of these values will be 1.  For non-numeric types
157     * these will be 0.
158     */
159    /*@{*/
160    unsigned vector_elements:3; /**< 1, 2, 3, or 4 vector elements. */
161    unsigned matrix_columns:3;  /**< 1, 2, 3, or 4 matrix columns. */
162    /*@}*/
163 
164    /**
165     * Name of the data type
166     *
167     * Will never be \c NULL.
168     */
169    const char *name;
170 
171    /**
172     * For \c GLSL_TYPE_ARRAY, this is the length of the array.  For
173     * \c GLSL_TYPE_STRUCT or \c GLSL_TYPE_INTERFACE, it is the number of
174     * elements in the structure and the number of values pointed to by
175     * \c fields.structure (below).
176     */
177    unsigned length;
178 
179    /**
180     * Subtype of composite data types.
181     */
182    union {
183       const struct glsl_type *array;            /**< Type of array elements. */
184       const struct glsl_type *parameters;       /**< Parameters to function. */
185       struct glsl_struct_field *structure;      /**< List of struct fields. */
186    } fields;
187 
188    /**
189     * \name Pointers to various public type singletons
190     */
191    /*@{*/
192 #undef  DECL_TYPE
193 #define DECL_TYPE(NAME, ...) \
194    static const glsl_type *const NAME##_type;
195 #undef  STRUCT_TYPE
196 #define STRUCT_TYPE(NAME) \
197    static const glsl_type *const struct_##NAME##_type;
198 #include "builtin_type_macros.h"
199    /*@}*/
200 
201    /**
202     * Convenience accessors for vector types (shorter than get_instance()).
203     * @{
204     */
205    static const glsl_type *vec(unsigned components);
206    static const glsl_type *ivec(unsigned components);
207    static const glsl_type *uvec(unsigned components);
208    static const glsl_type *bvec(unsigned components);
209    /**@}*/
210 
211    /**
212     * For numeric and boolean derived types returns the basic scalar type
213     *
214     * If the type is a numeric or boolean scalar, vector, or matrix type,
215     * this function gets the scalar type of the individual components.  For
216     * all other types, including arrays of numeric or boolean types, the
217     * error type is returned.
218     */
219    const glsl_type *get_base_type() const;
220 
221    /**
222     * Get the basic scalar type which this type aggregates.
223     *
224     * If the type is a numeric or boolean scalar, vector, or matrix, or an
225     * array of any of those, this function gets the scalar type of the
226     * individual components.  For structs and arrays of structs, this function
227     * returns the struct type.  For samplers and arrays of samplers, this
228     * function returns the sampler type.
229     */
230    const glsl_type *get_scalar_type() const;
231 
232    /**
233     * Query the type of elements in an array
234     *
235     * \return
236     * Pointer to the type of elements in the array for array types, or \c NULL
237     * for non-array types.
238     */
element_typeglsl_type239    const glsl_type *element_type() const
240    {
241       return is_array() ? fields.array : NULL;
242    }
243 
244    /**
245     * Get the instance of a built-in scalar, vector, or matrix type
246     */
247    static const glsl_type *get_instance(unsigned base_type, unsigned rows,
248 					unsigned columns);
249 
250    /**
251     * Get the instance of an array type
252     */
253    static const glsl_type *get_array_instance(const glsl_type *base,
254 					      unsigned elements);
255 
256    /**
257     * Get the instance of a record type
258     */
259    static const glsl_type *get_record_instance(const glsl_struct_field *fields,
260 					       unsigned num_fields,
261 					       const char *name);
262 
263    /**
264     * Get the instance of an interface block type
265     */
266    static const glsl_type *get_interface_instance(const glsl_struct_field *fields,
267 						  unsigned num_fields,
268 						  enum glsl_interface_packing packing,
269 						  const char *block_name);
270 
271    /**
272     * Query the total number of scalars that make up a scalar, vector or matrix
273     */
componentsglsl_type274    unsigned components() const
275    {
276       return vector_elements * matrix_columns;
277    }
278 
279    /**
280     * Calculate the number of components slots required to hold this type
281     *
282     * This is used to determine how many uniform or varying locations a type
283     * might occupy.
284     */
285    unsigned component_slots() const;
286 
287    /**
288     * Calculate the number of unique values from glGetUniformLocation for the
289     * elements of the type.
290     *
291     * This is used to allocate slots in the UniformRemapTable, the amount of
292     * locations may not match with actual used storage space by the driver.
293     */
294    unsigned uniform_locations() const;
295 
296    /**
297     * Calculate the number of attribute slots required to hold this type
298     *
299     * This implements the language rules of GLSL 1.50 for counting the number
300     * of slots used by a vertex attribute.  It also determines the number of
301     * varying slots the type will use up in the absence of varying packing
302     * (and thus, it can be used to measure the number of varying slots used by
303     * the varyings that are generated by lower_packed_varyings).
304     */
305    unsigned count_attribute_slots() const;
306 
307 
308    /**
309     * Alignment in bytes of the start of this type in a std140 uniform
310     * block.
311     */
312    unsigned std140_base_alignment(bool row_major) const;
313 
314    /** Size in bytes of this type in a std140 uniform block.
315     *
316     * Note that this is not GL_UNIFORM_SIZE (which is the number of
317     * elements in the array)
318     */
319    unsigned std140_size(bool row_major) const;
320 
321    /**
322     * \brief Can this type be implicitly converted to another?
323     *
324     * \return True if the types are identical or if this type can be converted
325     *         to \c desired according to Section 4.1.10 of the GLSL spec.
326     *
327     * \verbatim
328     * From page 25 (31 of the pdf) of the GLSL 1.50 spec, Section 4.1.10
329     * Implicit Conversions:
330     *
331     *     In some situations, an expression and its type will be implicitly
332     *     converted to a different type. The following table shows all allowed
333     *     implicit conversions:
334     *
335     *     Type of expression | Can be implicitly converted to
336     *     --------------------------------------------------
337     *     int                  float
338     *     uint
339     *
340     *     ivec2                vec2
341     *     uvec2
342     *
343     *     ivec3                vec3
344     *     uvec3
345     *
346     *     ivec4                vec4
347     *     uvec4
348     *
349     *     There are no implicit array or structure conversions. For example,
350     *     an array of int cannot be implicitly converted to an array of float.
351     *     There are no implicit conversions between signed and unsigned
352     *     integers.
353     * \endverbatim
354     */
355    bool can_implicitly_convert_to(const glsl_type *desired,
356                                   _mesa_glsl_parse_state *state) const;
357 
358    /**
359     * Query whether or not a type is a scalar (non-vector and non-matrix).
360     */
is_scalarglsl_type361    bool is_scalar() const
362    {
363       return (vector_elements == 1)
364 	 && (base_type >= GLSL_TYPE_UINT)
365 	 && (base_type <= GLSL_TYPE_BOOL);
366    }
367 
368    /**
369     * Query whether or not a type is a vector
370     */
is_vectorglsl_type371    bool is_vector() const
372    {
373       return (vector_elements > 1)
374 	 && (matrix_columns == 1)
375 	 && (base_type >= GLSL_TYPE_UINT)
376 	 && (base_type <= GLSL_TYPE_BOOL);
377    }
378 
379    /**
380     * Query whether or not a type is a matrix
381     */
is_matrixglsl_type382    bool is_matrix() const
383    {
384       /* GLSL only has float matrices. */
385       return (matrix_columns > 1) && (base_type == GLSL_TYPE_FLOAT);
386    }
387 
388    /**
389     * Query whether or not a type is a non-array numeric type
390     */
is_numericglsl_type391    bool is_numeric() const
392    {
393       return (base_type >= GLSL_TYPE_UINT) && (base_type <= GLSL_TYPE_FLOAT);
394    }
395 
396    /**
397     * Query whether or not a type is an integral type
398     */
is_integerglsl_type399    bool is_integer() const
400    {
401       return (base_type == GLSL_TYPE_UINT) || (base_type == GLSL_TYPE_INT);
402    }
403 
404    /**
405     * Query whether or not type is an integral type, or for struct and array
406     * types, contains an integral type.
407     */
408    bool contains_integer() const;
409 
410    /**
411     * Query whether or not a type is a float type
412     */
is_floatglsl_type413    bool is_float() const
414    {
415       return base_type == GLSL_TYPE_FLOAT;
416    }
417 
418    /**
419     * Query whether or not a type is a non-array boolean type
420     */
is_booleanglsl_type421    bool is_boolean() const
422    {
423       return base_type == GLSL_TYPE_BOOL;
424    }
425 
426    /**
427     * Query whether or not a type is a sampler
428     */
is_samplerglsl_type429    bool is_sampler() const
430    {
431       return base_type == GLSL_TYPE_SAMPLER;
432    }
433 
434    /**
435     * Query whether or not type is a sampler, or for struct and array
436     * types, contains a sampler.
437     */
438    bool contains_sampler() const;
439 
440    /**
441     * Get the Mesa texture target index for a sampler type.
442     */
443    gl_texture_index sampler_index() const;
444 
445    /**
446     * Query whether or not type is an image, or for struct and array
447     * types, contains an image.
448     */
449    bool contains_image() const;
450 
451    /**
452     * Query whether or not a type is an image
453     */
is_imageglsl_type454    bool is_image() const
455    {
456       return base_type == GLSL_TYPE_IMAGE;
457    }
458 
459    /**
460     * Query whether or not a type is an array
461     */
is_arrayglsl_type462    bool is_array() const
463    {
464       return base_type == GLSL_TYPE_ARRAY;
465    }
466 
467    /**
468     * Query whether or not a type is a record
469     */
is_recordglsl_type470    bool is_record() const
471    {
472       return base_type == GLSL_TYPE_STRUCT;
473    }
474 
475    /**
476     * Query whether or not a type is an interface
477     */
is_interfaceglsl_type478    bool is_interface() const
479    {
480       return base_type == GLSL_TYPE_INTERFACE;
481    }
482 
483    /**
484     * Query whether or not a type is the void type singleton.
485     */
is_voidglsl_type486    bool is_void() const
487    {
488       return base_type == GLSL_TYPE_VOID;
489    }
490 
491    /**
492     * Query whether or not a type is the error type singleton.
493     */
is_errorglsl_type494    bool is_error() const
495    {
496       return base_type == GLSL_TYPE_ERROR;
497    }
498 
499    /**
500     * Query if a type is unnamed/anonymous (named by the parser)
501     */
is_anonymousglsl_type502    bool is_anonymous() const
503    {
504       return !strncmp(name, "#anon", 5);
505    }
506 
507    /**
508     * Get the type stripped of any arrays
509     *
510     * \return
511     * Pointer to the type of elements of the first non-array type for array
512     * types, or pointer to itself for non-array types.
513     */
without_arrayglsl_type514    const glsl_type *without_array() const
515    {
516       return this->is_array() ? this->fields.array : this;
517    }
518 
519    /**
520     * Return the amount of atomic counter storage required for a type.
521     */
atomic_sizeglsl_type522    unsigned atomic_size() const
523    {
524       if (base_type == GLSL_TYPE_ATOMIC_UINT)
525          return ATOMIC_COUNTER_SIZE;
526       else if (is_array())
527          return length * element_type()->atomic_size();
528       else
529          return 0;
530    }
531 
532    /**
533     * Return whether a type contains any atomic counters.
534     */
contains_atomicglsl_type535    bool contains_atomic() const
536    {
537       return atomic_size() > 0;
538    }
539 
540    /**
541     * Return whether a type contains any opaque types.
542     */
543    bool contains_opaque() const;
544 
545    /**
546     * Query the full type of a matrix row
547     *
548     * \return
549     * If the type is not a matrix, \c glsl_type::error_type is returned.
550     * Otherwise a type matching the rows of the matrix is returned.
551     */
row_typeglsl_type552    const glsl_type *row_type() const
553    {
554       return is_matrix()
555 	 ? get_instance(base_type, matrix_columns, 1)
556 	 : error_type;
557    }
558 
559    /**
560     * Query the full type of a matrix column
561     *
562     * \return
563     * If the type is not a matrix, \c glsl_type::error_type is returned.
564     * Otherwise a type matching the columns of the matrix is returned.
565     */
column_typeglsl_type566    const glsl_type *column_type() const
567    {
568       return is_matrix()
569 	 ? get_instance(base_type, vector_elements, 1)
570 	 : error_type;
571    }
572 
573    /**
574     * Get the type of a structure field
575     *
576     * \return
577     * Pointer to the type of the named field.  If the type is not a structure
578     * or the named field does not exist, \c glsl_type::error_type is returned.
579     */
580    const glsl_type *field_type(const char *name) const;
581 
582    glsl_precision field_precision(const char *name) const;
583 
584 
585    /**
586     * Get the location of a filed within a record type
587     */
588    int field_index(const char *name) const;
589 
590    /**
591     * Query the number of elements in an array type
592     *
593     * \return
594     * The number of elements in the array for array types or -1 for non-array
595     * types.  If the number of elements in the array has not yet been declared,
596     * zero is returned.
597     */
array_sizeglsl_type598    int array_size() const
599    {
600       return is_array() ? length : -1;
601    }
602 
603    /**
604     * Query whether the array size for all dimensions has been declared.
605     */
is_unsized_arrayglsl_type606    bool is_unsized_array() const
607    {
608       return is_array() && length == 0;
609    }
610 
611    /**
612     * Return the number of coordinate components needed for this
613     * sampler or image type.
614     *
615     * This is based purely on the sampler's dimensionality.  For example, this
616     * returns 1 for sampler1D, and 3 for sampler2DArray.
617     *
618     * Note that this is often different than actual coordinate type used in
619     * a texturing built-in function, since those pack additional values (such
620     * as the shadow comparitor or projector) into the coordinate type.
621     */
622    int coordinate_components() const;
623 
624    /**
625     * Compare a record type against another record type.
626     *
627     * This is useful for matching record types declared across shader stages.
628     */
629    bool record_compare(const glsl_type *b) const;
630 
631 private:
632    /**
633     * ralloc context for all glsl_type allocations
634     *
635     * Set on the first call to \c glsl_type::new.
636     */
637    static void *mem_ctx;
638 
639    void init_ralloc_type_ctx(void);
640 
641    /** Constructor for vector and matrix types */
642    glsl_type(GLenum gl_type,
643 	     glsl_base_type base_type, unsigned vector_elements,
644 	     unsigned matrix_columns, const char *name);
645 
646    /** Constructor for sampler or image types */
647    glsl_type(GLenum gl_type, glsl_base_type base_type,
648 	     enum glsl_sampler_dim dim, bool shadow, bool array,
649 	     unsigned type, const char *name);
650 
651    /** Constructor for record types */
652    glsl_type(const glsl_struct_field *fields, unsigned num_fields,
653 	     const char *name);
654 
655    /** Constructor for interface types */
656    glsl_type(const glsl_struct_field *fields, unsigned num_fields,
657 	     enum glsl_interface_packing packing, const char *name);
658 
659    /** Constructor for array types */
660    glsl_type(const glsl_type *array, unsigned length);
661 
662    /** Hash table containing the known array types. */
663    static struct hash_table *array_types;
664 
665    /** Hash table containing the known record types. */
666    static struct hash_table *record_types;
667 
668    /** Hash table containing the known interface types. */
669    static struct hash_table *interface_types;
670 
671    static int record_key_compare(const void *a, const void *b);
672    static unsigned record_key_hash(const void *key);
673 
674    /**
675     * \name Built-in type flyweights
676     */
677    /*@{*/
678 #undef  DECL_TYPE
679 #define DECL_TYPE(NAME, ...) static const glsl_type _##NAME##_type;
680 #undef  STRUCT_TYPE
681 #define STRUCT_TYPE(NAME)        static const glsl_type _struct_##NAME##_type;
682 #include "builtin_type_macros.h"
683    /*@}*/
684 
685    /**
686     * \name Friend functions.
687     *
688     * These functions are friends because they must have C linkage and the
689     * need to call various private methods or access various private static
690     * data.
691     */
692    /*@{*/
693    friend void _mesa_glsl_initialize_types(struct _mesa_glsl_parse_state *);
694    friend void _mesa_glsl_release_types(void);
695    /*@}*/
696 };
697 
698 struct glsl_struct_field {
699    const struct glsl_type *type;
700    const char *name;
701    glsl_precision precision;
702 
703    /**
704     * For interface blocks, gl_varying_slot corresponding to the input/output
705     * if this is a built-in input/output (i.e. a member of the built-in
706     * gl_PerVertex interface block); -1 otherwise.
707     *
708     * Ignored for structs.
709     */
710    int location;
711 
712    /**
713     * For interface blocks, the interpolation mode (as in
714     * ir_variable::interpolation).  0 otherwise.
715     */
716    unsigned interpolation:2;
717 
718    /**
719     * For interface blocks, 1 if this variable uses centroid interpolation (as
720     * in ir_variable::centroid).  0 otherwise.
721     */
722    unsigned centroid:1;
723 
724    /**
725     * For interface blocks, 1 if this variable uses sample interpolation (as
726     * in ir_variable::sample). 0 otherwise.
727     */
728    unsigned sample:1;
729 
730    /**
731     * Layout of the matrix.  Uses glsl_matrix_layout values.
732     */
733    unsigned matrix_layout:2;
734 
735    /**
736     * For interface blocks, it has a value if this variable uses multiple vertex
737     * streams (as in ir_variable::stream). -1 otherwise.
738     */
739    int stream;
740 };
741 
742 static inline unsigned int
glsl_align(unsigned int a,unsigned int align)743 glsl_align(unsigned int a, unsigned int align)
744 {
745    return (a + align - 1) / align * align;
746 }
747 
748 #undef DECL_TYPE
749 #undef STRUCT_TYPE
750 #endif /* __cplusplus */
751 
752 #endif /* GLSL_TYPES_H */
753