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