Initial bits to process initializers in variable declarations
[mesa.git] / glsl_types.h
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 <cstring>
30 #include <cassert>
31
32 #define GLSL_TYPE_UINT 0
33 #define GLSL_TYPE_INT 1
34 #define GLSL_TYPE_FLOAT 2
35 #define GLSL_TYPE_BOOL 3
36 #define GLSL_TYPE_SAMPLER 4
37 #define GLSL_TYPE_STRUCT 5
38 #define GLSL_TYPE_ARRAY 6
39 #define GLSL_TYPE_FUNCTION 7
40 #define GLSL_TYPE_VOID 8
41 #define GLSL_TYPE_ERROR 9
42
43 enum glsl_sampler_dim {
44 GLSL_SAMPLER_DIM_1D = 0,
45 GLSL_SAMPLER_DIM_2D,
46 GLSL_SAMPLER_DIM_3D,
47 GLSL_SAMPLER_DIM_CUBE,
48 GLSL_SAMPLER_DIM_RECT,
49 GLSL_SAMPLER_DIM_BUF
50 };
51
52
53 struct glsl_type {
54 unsigned base_type:4;
55
56 unsigned sampler_dimensionality:3;
57 unsigned sampler_shadow:1;
58 unsigned sampler_array:1;
59 unsigned sampler_type:2; /**< Type of data returned using this sampler.
60 * only \c GLSL_TYPE_FLOAT, \c GLSL_TYPE_INT,
61 * and \c GLSL_TYPE_UINT are valid.
62 */
63
64 /**
65 * \name Vector and matrix element counts
66 *
67 * For scalars, each of these values will be 1. For non-numeric types
68 * these will be 0.
69 */
70 /*@{*/
71 unsigned vector_elements:3; /**< 1, 2, 3, or 4 vector elements. */
72 unsigned matrix_columns:3; /**< 1, 2, 3, or 4 matrix columns. */
73 /*@}*/
74
75 /**
76 * Name of the data type
77 *
78 * This may be \c NULL for anonymous structures, for arrays, or for
79 * function types.
80 */
81 const char *name;
82
83 /**
84 * For \c GLSL_TYPE_ARRAY, this is the length of the array. For
85 * \c GLSL_TYPE_STRUCT, it is the number of elements in the structure and
86 * the number of values pointed to by \c fields.structure (below).
87 *
88 * For \c GLSL_TYPE_FUNCTION, it is the number of parameters to the
89 * function. The return value from a function is implicitly the first
90 * parameter. The types of the parameters are stored in
91 * \c fields.parameters (below).
92 */
93 unsigned length;
94
95 /**
96 * Subtype of composite data types.
97 */
98 union {
99 const struct glsl_type *array; /**< Type of array elements. */
100 const struct glsl_type *parameters; /**< Parameters to function. */
101 const struct glsl_struct_field *structure;/**< List of struct fields. */
102 } fields;
103
104
105 /**
106 * \name Pointers to various public type singletons
107 */
108 /*@{*/
109 static const glsl_type *const error_type;
110 static const glsl_type *const int_type;
111 static const glsl_type *const uint_type;
112 static const glsl_type *const float_type;
113 static const glsl_type *const bool_type;
114 /*@}*/
115
116
117 glsl_type(unsigned base_type, unsigned vector_elements,
118 unsigned matrix_columns, const char *name) :
119 base_type(base_type),
120 sampler_dimensionality(0), sampler_shadow(0), sampler_array(0),
121 sampler_type(0),
122 vector_elements(vector_elements), matrix_columns(matrix_columns),
123 name(name),
124 length(0)
125 {
126 /* Neither dimension is zero or both dimensions are zero.
127 */
128 assert((vector_elements == 0) == (matrix_columns == 0));
129 memset(& fields, 0, sizeof(fields));
130 }
131
132 glsl_type(enum glsl_sampler_dim dim, bool shadow, bool array,
133 unsigned type, const char *name) :
134 base_type(GLSL_TYPE_SAMPLER),
135 sampler_dimensionality(dim), sampler_shadow(shadow),
136 sampler_array(array), sampler_type(type),
137 vector_elements(0), matrix_columns(0),
138 name(name),
139 length(0)
140 {
141 memset(& fields, 0, sizeof(fields));
142 }
143
144 glsl_type(const glsl_struct_field *fields, unsigned num_fields,
145 const char *name) :
146 base_type(GLSL_TYPE_STRUCT),
147 sampler_dimensionality(0), sampler_shadow(0), sampler_array(0),
148 sampler_type(0),
149 vector_elements(0), matrix_columns(0),
150 name(name),
151 length(num_fields)
152 {
153 this->fields.structure = fields;
154 }
155
156 /**
157 * For numeric and boolean derrived types returns the basic scalar type
158 *
159 * If the type is a numeric or boolean scalar, vector, or matrix type,
160 * this function gets the scalar type of the individual components. For
161 * all other types, including arrays of numeric or boolean types, the
162 * error type is returned.
163 */
164 const glsl_type *get_base_type() const;
165
166 /**
167 * Get the instance of a built-in scalar, vector, or matrix type
168 */
169 static const glsl_type *get_instance(unsigned base_type, unsigned rows,
170 unsigned columns);
171
172 /**
173 * Query the total number of scalars that make up a scalar, vector or matrix
174 */
175 unsigned components() const
176 {
177 return vector_elements * matrix_columns;
178 }
179
180 /**
181 * Query whether or not a type is a scalar (non-vector and non-matrix).
182 */
183 bool is_scalar() const
184 {
185 return (vector_elements == 1)
186 && (base_type >= GLSL_TYPE_UINT)
187 && (base_type <= GLSL_TYPE_BOOL);
188 }
189
190 /**
191 * Query whether or not a type is a vector
192 */
193 bool is_vector() const
194 {
195 return (vector_elements > 1)
196 && (matrix_columns == 1)
197 && (base_type >= GLSL_TYPE_UINT)
198 && (base_type <= GLSL_TYPE_BOOL);
199 }
200
201 /**
202 * Query whether or not a type is a matrix
203 */
204 bool is_matrix() const
205 {
206 /* GLSL only has float matrices. */
207 return (matrix_columns > 1) && (base_type == GLSL_TYPE_FLOAT);
208 }
209
210 /**
211 * Query whether or not a type is a non-array numeric type
212 */
213 bool is_numeric() const
214 {
215 return (base_type >= GLSL_TYPE_UINT) && (base_type <= GLSL_TYPE_FLOAT);
216 }
217
218 /**
219 * Query whether or not a type is an integral type
220 */
221 bool is_integer() const
222 {
223 return (base_type == GLSL_TYPE_UINT) || (base_type == GLSL_TYPE_INT);
224 }
225
226 /**
227 * Query whether or not a type is a non-array boolean type
228 */
229 bool is_boolean() const
230 {
231 return base_type == GLSL_TYPE_BOOL;
232 }
233
234 /**
235 * Query whether or not a type is a sampler
236 */
237 bool is_sampler() const
238 {
239 return base_type == GLSL_TYPE_SAMPLER;
240 }
241
242 /**
243 * Query whether or not a type is the void type singleton.
244 */
245 bool is_void() const
246 {
247 return base_type == GLSL_TYPE_VOID;
248 }
249
250 /**
251 * Query whether or not a type is the error type singleton.
252 */
253 bool is_error() const
254 {
255 return base_type == GLSL_TYPE_ERROR;
256 }
257
258 /**
259 * Query the full type of a matrix row
260 *
261 * \return
262 * If the type is not a matrix, \c glsl_type::error_type is returned.
263 * Otherwise a type matching the rows of the matrix is returned.
264 */
265 const glsl_type *row_type() const
266 {
267 return is_matrix()
268 ? get_instance(base_type, matrix_columns, 1)
269 : error_type;
270 }
271
272 /**
273 * Query the full type of a matrix column
274 *
275 * \return
276 * If the type is not a matrix, \c glsl_type::error_type is returned.
277 * Otherwise a type matching the columns of the matrix is returned.
278 */
279 const glsl_type *column_type() const
280 {
281 return is_matrix()
282 ? get_instance(base_type, vector_elements, 1)
283 : error_type;
284 }
285
286 private:
287 /**
288 * \name Pointers to various private type singletons
289 */
290 /*@{*/
291 static const glsl_type *const mat2_type;
292 static const glsl_type *const mat2x3_type;
293 static const glsl_type *const mat2x4_type;
294 static const glsl_type *const mat3x2_type;
295 static const glsl_type *const mat3_type;
296 static const glsl_type *const mat3x4_type;
297 static const glsl_type *const mat4x2_type;
298 static const glsl_type *const mat4x3_type;
299 static const glsl_type *const mat4_type;
300 /*@}*/
301 };
302
303 struct glsl_struct_field {
304 const struct glsl_type *type;
305 const char *name;
306 };
307
308 struct _mesa_glsl_parse_state;
309
310 #ifdef __cplusplus
311 extern "C" {
312 #endif
313
314 extern void
315 _mesa_glsl_initialize_types(struct _mesa_glsl_parse_state *state);
316
317 extern void
318 _mesa_glsl_initialize_constructors(struct exec_list *instructions,
319 struct _mesa_glsl_parse_state *state);
320
321 #ifdef __cplusplus
322 }
323 #endif
324
325 #endif /* GLSL_TYPES_H */