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