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