glsl2: Associate the GLenum for the type with builtin GLSL types.
[mesa.git] / src / glsl / 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 extern "C" {
33 #include "GL/gl.h"
34 #include <talloc.h>
35 }
36
37 #define GLSL_TYPE_UINT 0
38 #define GLSL_TYPE_INT 1
39 #define GLSL_TYPE_FLOAT 2
40 #define GLSL_TYPE_BOOL 3
41 #define GLSL_TYPE_SAMPLER 4
42 #define GLSL_TYPE_STRUCT 5
43 #define GLSL_TYPE_ARRAY 6
44 #define GLSL_TYPE_FUNCTION 7
45 #define GLSL_TYPE_VOID 8
46 #define GLSL_TYPE_ERROR 9
47
48 enum glsl_sampler_dim {
49 GLSL_SAMPLER_DIM_1D = 0,
50 GLSL_SAMPLER_DIM_2D,
51 GLSL_SAMPLER_DIM_3D,
52 GLSL_SAMPLER_DIM_CUBE,
53 GLSL_SAMPLER_DIM_RECT,
54 GLSL_SAMPLER_DIM_BUF
55 };
56
57
58 struct glsl_type {
59 GLenum gl_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 /* Callers of this talloc-based new need not call delete. It's
71 * easier to just talloc_free 'ctx' (or any of its ancestors). */
72 static void* operator new(size_t size, void *ctx)
73 {
74 void *type;
75
76 type = talloc_size(ctx, size);
77 assert(type != NULL);
78
79 return type;
80 }
81
82 /* If the user *does* call delete, that's OK, we will just
83 * talloc_free in that case. */
84 static void operator delete(void *type)
85 {
86 talloc_free(type);
87 }
88
89 /**
90 * \name Vector and matrix element counts
91 *
92 * For scalars, each of these values will be 1. For non-numeric types
93 * these will be 0.
94 */
95 /*@{*/
96 unsigned vector_elements:3; /**< 1, 2, 3, or 4 vector elements. */
97 unsigned matrix_columns:3; /**< 1, 2, 3, or 4 matrix columns. */
98 /*@}*/
99
100 /**
101 * Name of the data type
102 *
103 * This may be \c NULL for anonymous structures, for arrays, or for
104 * function types.
105 */
106 const char *name;
107
108 /**
109 * For \c GLSL_TYPE_ARRAY, this is the length of the array. For
110 * \c GLSL_TYPE_STRUCT, it is the number of elements in the structure and
111 * the number of values pointed to by \c fields.structure (below).
112 *
113 * For \c GLSL_TYPE_FUNCTION, it is the number of parameters to the
114 * function. The return value from a function is implicitly the first
115 * parameter. The types of the parameters are stored in
116 * \c fields.parameters (below).
117 */
118 unsigned length;
119
120 /**
121 * Subtype of composite data types.
122 */
123 union {
124 const struct glsl_type *array; /**< Type of array elements. */
125 const struct glsl_type *parameters; /**< Parameters to function. */
126 const struct glsl_struct_field *structure;/**< List of struct fields. */
127 } fields;
128
129
130 /**
131 * \name Pointers to various public type singletons
132 */
133 /*@{*/
134 static const glsl_type *const error_type;
135 static const glsl_type *const int_type;
136 static const glsl_type *const ivec4_type;
137 static const glsl_type *const uint_type;
138 static const glsl_type *const uvec4_type;
139 static const glsl_type *const float_type;
140 static const glsl_type *const vec2_type;
141 static const glsl_type *const vec3_type;
142 static const glsl_type *const vec4_type;
143 static const glsl_type *const bool_type;
144 static const glsl_type *const mat2_type;
145 static const glsl_type *const mat2x3_type;
146 static const glsl_type *const mat2x4_type;
147 static const glsl_type *const mat3x2_type;
148 static const glsl_type *const mat3_type;
149 static const glsl_type *const mat3x4_type;
150 static const glsl_type *const mat4x2_type;
151 static const glsl_type *const mat4x3_type;
152 static const glsl_type *const mat4_type;
153 /*@}*/
154
155
156 glsl_type(GLenum gl_type,
157 unsigned base_type, unsigned vector_elements,
158 unsigned matrix_columns, const char *name) :
159 gl_type(gl_type),
160 base_type(base_type),
161 sampler_dimensionality(0), sampler_shadow(0), sampler_array(0),
162 sampler_type(0),
163 vector_elements(vector_elements), matrix_columns(matrix_columns),
164 name(name),
165 length(0)
166 {
167 /* Neither dimension is zero or both dimensions are zero.
168 */
169 assert((vector_elements == 0) == (matrix_columns == 0));
170 memset(& fields, 0, sizeof(fields));
171 }
172
173 glsl_type(GLenum gl_type,
174 enum glsl_sampler_dim dim, bool shadow, bool array,
175 unsigned type, const char *name) :
176 gl_type(gl_type),
177 base_type(GLSL_TYPE_SAMPLER),
178 sampler_dimensionality(dim), sampler_shadow(shadow),
179 sampler_array(array), sampler_type(type),
180 vector_elements(0), matrix_columns(0),
181 name(name),
182 length(0)
183 {
184 memset(& fields, 0, sizeof(fields));
185 }
186
187 glsl_type(const glsl_struct_field *fields, unsigned num_fields,
188 const char *name) :
189 base_type(GLSL_TYPE_STRUCT),
190 sampler_dimensionality(0), sampler_shadow(0), sampler_array(0),
191 sampler_type(0),
192 vector_elements(0), matrix_columns(0),
193 name(name),
194 length(num_fields)
195 {
196 this->fields.structure = fields;
197 }
198
199 /**
200 * For numeric and boolean derrived types returns the basic scalar type
201 *
202 * If the type is a numeric or boolean scalar, vector, or matrix type,
203 * this function gets the scalar type of the individual components. For
204 * all other types, including arrays of numeric or boolean types, the
205 * error type is returned.
206 */
207 const glsl_type *get_base_type() const;
208
209 /**
210 * Query the type of elements in an array
211 *
212 * \return
213 * Pointer to the type of elements in the array for array types, or \c NULL
214 * for non-array types.
215 */
216 const glsl_type *element_type() const
217 {
218 return is_array() ? fields.array : NULL;
219 }
220
221 /**
222 * Get the instance of a built-in scalar, vector, or matrix type
223 */
224 static const glsl_type *get_instance(unsigned base_type, unsigned rows,
225 unsigned columns);
226
227 /**
228 * Get the instance of an array type
229 */
230 static const glsl_type *get_array_instance(void *ctx,
231 const glsl_type *base,
232 unsigned elements);
233
234 /**
235 * Generate the constructor for this type and add it to the symbol table
236 */
237 class ir_function *generate_constructor(class glsl_symbol_table *) const;
238
239 /**
240 * Query the total number of scalars that make up a scalar, vector or matrix
241 */
242 unsigned components() const
243 {
244 return vector_elements * matrix_columns;
245 }
246
247 /**
248 * Calculate the number of components slots required to hold this type
249 *
250 * This is used to determine how many uniform or varying locations a type
251 * might occupy.
252 */
253 unsigned component_slots() const;
254
255
256 /**
257 * Query whether or not a type is a scalar (non-vector and non-matrix).
258 */
259 bool is_scalar() const
260 {
261 return (vector_elements == 1)
262 && (base_type >= GLSL_TYPE_UINT)
263 && (base_type <= GLSL_TYPE_BOOL);
264 }
265
266 /**
267 * Query whether or not a type is a vector
268 */
269 bool is_vector() const
270 {
271 return (vector_elements > 1)
272 && (matrix_columns == 1)
273 && (base_type >= GLSL_TYPE_UINT)
274 && (base_type <= GLSL_TYPE_BOOL);
275 }
276
277 /**
278 * Query whether or not a type is a matrix
279 */
280 bool is_matrix() const
281 {
282 /* GLSL only has float matrices. */
283 return (matrix_columns > 1) && (base_type == GLSL_TYPE_FLOAT);
284 }
285
286 /**
287 * Query whether or not a type is a non-array numeric type
288 */
289 bool is_numeric() const
290 {
291 return (base_type >= GLSL_TYPE_UINT) && (base_type <= GLSL_TYPE_FLOAT);
292 }
293
294 /**
295 * Query whether or not a type is an integral type
296 */
297 bool is_integer() const
298 {
299 return (base_type == GLSL_TYPE_UINT) || (base_type == GLSL_TYPE_INT);
300 }
301
302 /**
303 * Query whether or not a type is a float type
304 */
305 bool is_float() const
306 {
307 return base_type == GLSL_TYPE_FLOAT;
308 }
309
310 /**
311 * Query whether or not a type is a non-array boolean type
312 */
313 bool is_boolean() const
314 {
315 return base_type == GLSL_TYPE_BOOL;
316 }
317
318 /**
319 * Query whether or not a type is a sampler
320 */
321 bool is_sampler() const
322 {
323 return base_type == GLSL_TYPE_SAMPLER;
324 }
325
326 /**
327 * Query whether or not a type is an array
328 */
329 bool is_array() const
330 {
331 return base_type == GLSL_TYPE_ARRAY;
332 }
333
334 /**
335 * Query whether or not a type is a record
336 */
337 bool is_record() const
338 {
339 return base_type == GLSL_TYPE_STRUCT;
340 }
341
342 /**
343 * Query whether or not a type is the void type singleton.
344 */
345 bool is_void() const
346 {
347 return base_type == GLSL_TYPE_VOID;
348 }
349
350 /**
351 * Query whether or not a type is the error type singleton.
352 */
353 bool is_error() const
354 {
355 return base_type == GLSL_TYPE_ERROR;
356 }
357
358 /**
359 * Query the full type of a matrix row
360 *
361 * \return
362 * If the type is not a matrix, \c glsl_type::error_type is returned.
363 * Otherwise a type matching the rows of the matrix is returned.
364 */
365 const glsl_type *row_type() const
366 {
367 return is_matrix()
368 ? get_instance(base_type, matrix_columns, 1)
369 : error_type;
370 }
371
372 /**
373 * Query the full type of a matrix column
374 *
375 * \return
376 * If the type is not a matrix, \c glsl_type::error_type is returned.
377 * Otherwise a type matching the columns of the matrix is returned.
378 */
379 const glsl_type *column_type() const
380 {
381 return is_matrix()
382 ? get_instance(base_type, vector_elements, 1)
383 : error_type;
384 }
385
386
387 /**
388 * Get the type of a structure field
389 *
390 * \return
391 * Pointer to the type of the named field. If the type is not a structure
392 * or the named field does not exist, \c glsl_type::error_type is returned.
393 */
394 const glsl_type *field_type(const char *name) const;
395
396
397 /**
398 * Get the location of a filed within a record type
399 */
400 int field_index(const char *name) const;
401
402
403 /**
404 * Query the number of elements in an array type
405 *
406 * \return
407 * The number of elements in the array for array types or -1 for non-array
408 * types. If the number of elements in the array has not yet been declared,
409 * zero is returned.
410 */
411 int array_size() const
412 {
413 return is_array() ? length : -1;
414 }
415
416 private:
417 /**
418 * Constructor for array types
419 */
420 glsl_type(void *ctx, const glsl_type *array, unsigned length);
421
422 /** Hash table containing the known array types. */
423 static struct hash_table *array_types;
424
425 static int array_key_compare(const void *a, const void *b);
426 static unsigned array_key_hash(const void *key);
427 };
428
429 struct glsl_struct_field {
430 const struct glsl_type *type;
431 const char *name;
432 };
433
434 struct _mesa_glsl_parse_state;
435
436 #ifdef __cplusplus
437 extern "C" {
438 #endif
439
440 extern void
441 _mesa_glsl_initialize_types(struct _mesa_glsl_parse_state *state);
442
443 extern void
444 _mesa_glsl_initialize_constructors(struct exec_list *instructions,
445 struct _mesa_glsl_parse_state *state);
446
447 #ifdef __cplusplus
448 }
449 #endif
450
451 #endif /* GLSL_TYPES_H */