IR print visitor: Add some support for printing types and constants
[mesa.git] / glsl_types.h
1 /*
2 * Copyright © 2009 Intel Corporation
3 *
4 * Permission is hereby granted, free of charge, to any person obtaining a
5 * copy of this software and associated documentation files (the "Software"),
6 * to deal in the Software without restriction, including without limitation
7 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
8 * and/or sell copies of the Software, and to permit persons to whom the
9 * Software is furnished to do so, subject to the following conditions:
10 *
11 * The above copyright notice and this permission notice (including the next
12 * paragraph) shall be included in all copies or substantial portions of the
13 * Software.
14 *
15 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
18 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
20 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
21 * DEALINGS IN THE SOFTWARE.
22 */
23
24 #pragma once
25 #ifndef GLSL_TYPES_H
26 #define GLSL_TYPES_H
27
28 #include <cstring>
29
30 #define GLSL_TYPE_UINT 0
31 #define GLSL_TYPE_INT 1
32 #define GLSL_TYPE_FLOAT 2
33 #define GLSL_TYPE_BOOL 3
34 #define GLSL_TYPE_SAMPLER 4
35 #define GLSL_TYPE_STRUCT 5
36 #define GLSL_TYPE_ARRAY 6
37 #define GLSL_TYPE_FUNCTION 7
38 #define GLSL_TYPE_VOID 8
39 #define GLSL_TYPE_ERROR 9
40
41 #define is_numeric_base_type(b) \
42 (((b) >= GLSL_TYPE_UINT) && ((b) <= GLSL_TYPE_FLOAT))
43
44 #define is_integer_base_type(b) \
45 (((b) == GLSL_TYPE_UINT) || ((b) == GLSL_TYPE_INT))
46
47 #define is_error_type(t) ((t)->base_type == GLSL_TYPE_ERROR)
48
49 enum glsl_sampler_dim {
50 GLSL_SAMPLER_DIM_1D = 0,
51 GLSL_SAMPLER_DIM_2D,
52 GLSL_SAMPLER_DIM_3D,
53 GLSL_SAMPLER_DIM_CUBE,
54 GLSL_SAMPLER_DIM_RECT,
55 GLSL_SAMPLER_DIM_BUF
56 };
57
58
59 struct glsl_type {
60 unsigned base_type:4;
61
62 unsigned sampler_dimensionality:3;
63 unsigned sampler_shadow:1;
64 unsigned sampler_array:1;
65 unsigned sampler_type:2; /**< Type of data returned using this sampler.
66 * only \c GLSL_TYPE_FLOAT, \c GLSL_TYPE_INT,
67 * and \c GLSL_TYPE_UINT are valid.
68 */
69
70 unsigned vector_elements:3; /**< 0, 2, 3, or 4 vector elements. */
71 unsigned matrix_rows:3; /**< 0, 2, 3, or 4 matrix rows. */
72
73 /**
74 * Name of the data type
75 *
76 * This may be \c NULL for anonymous structures, for arrays, or for
77 * function types.
78 */
79 const char *name;
80
81 /**
82 * For \c GLSL_TYPE_ARRAY, this is the length of the array. For
83 * \c GLSL_TYPE_STRUCT, it is the number of elements in the structure and
84 * the number of values pointed to by \c fields.structure (below).
85 *
86 * For \c GLSL_TYPE_FUNCTION, it is the number of parameters to the
87 * function. The return value from a function is implicitly the first
88 * parameter. The types of the parameters are stored in
89 * \c fields.parameters (below).
90 */
91 unsigned length;
92
93 /**
94 * Subtype of composite data types.
95 */
96 union {
97 const struct glsl_type *array; /**< Type of array elements. */
98 const struct glsl_type *parameters; /**< Parameters to function. */
99 const struct glsl_struct_field *structure;/**< List of struct fields. */
100 } fields;
101
102
103 glsl_type(unsigned base_type, unsigned vector_elements,
104 unsigned matrix_rows, const char *name) :
105 base_type(base_type),
106 sampler_dimensionality(0), sampler_shadow(0), sampler_array(0),
107 sampler_type(0),
108 vector_elements(vector_elements), matrix_rows(matrix_rows),
109 name(name),
110 length(0)
111 {
112 memset(& fields, 0, sizeof(fields));
113 }
114
115 glsl_type(enum glsl_sampler_dim dim, bool shadow, bool array,
116 unsigned type, const char *name) :
117 base_type(GLSL_TYPE_SAMPLER),
118 sampler_dimensionality(dim), sampler_shadow(shadow),
119 sampler_array(array), sampler_type(type),
120 vector_elements(0), matrix_rows(0),
121 name(name),
122 length(0)
123 {
124 memset(& fields, 0, sizeof(fields));
125 }
126
127 glsl_type(const glsl_struct_field *fields, unsigned num_fields,
128 const char *name) :
129 base_type(GLSL_TYPE_STRUCT),
130 sampler_dimensionality(0), sampler_shadow(0), sampler_array(0),
131 sampler_type(0),
132 vector_elements(0), matrix_rows(0),
133 name(name),
134 length(num_fields)
135 {
136 this->fields.structure = fields;
137 }
138
139 /**
140 * Query whether or not a type is a scalar (non-vector and non-matrix).
141 */
142 bool is_scalar() const
143 {
144 return (vector_elements == 0)
145 && (base_type >= GLSL_TYPE_UINT)
146 && (base_type <= GLSL_TYPE_BOOL);
147 }
148
149 /**
150 * Query whether or not a type is a vector
151 */
152 bool is_vector() const
153 {
154 return (vector_elements > 0)
155 && (matrix_rows == 0)
156 && (base_type >= GLSL_TYPE_UINT)
157 && (base_type <= GLSL_TYPE_BOOL);
158 }
159
160 /**
161 * Query whether or not a type is a matrix
162 */
163 bool is_matrix() const
164 {
165 /* GLSL only has float matrices. */
166 return (matrix_rows > 0) && (base_type == GLSL_TYPE_FLOAT);
167 }
168 };
169
170 struct glsl_struct_field {
171 const struct glsl_type *type;
172 const char *name;
173 };
174
175 struct _mesa_glsl_parse_state;
176
177 #ifdef __cplusplus
178 extern "C" {
179 #endif
180
181 extern void
182 _mesa_glsl_initialize_types(struct _mesa_glsl_parse_state *state);
183
184 extern const struct glsl_type *
185 _mesa_glsl_get_vector_type(unsigned base_type, unsigned vector_length);
186
187 extern const struct glsl_type *const glsl_error_type;
188 extern const struct glsl_type *const glsl_int_type;
189 extern const struct glsl_type *const glsl_uint_type;
190 extern const struct glsl_type *const glsl_float_type;
191 extern const struct glsl_type *const glsl_bool_type;
192
193 #ifdef __cplusplus
194 }
195 #endif
196
197 #endif /* GLSL_TYPES_H */