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