Merge branch 'llvm-cliptest-viewport'
[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 #define GLSL_TYPE_UINT 0
47 #define GLSL_TYPE_INT 1
48 #define GLSL_TYPE_FLOAT 2
49 #define GLSL_TYPE_BOOL 3
50 #define GLSL_TYPE_SAMPLER 4
51 #define GLSL_TYPE_STRUCT 5
52 #define GLSL_TYPE_ARRAY 6
53 #define GLSL_TYPE_FUNCTION 7
54 #define GLSL_TYPE_VOID 8
55 #define GLSL_TYPE_ERROR 9
56
57 enum glsl_sampler_dim {
58 GLSL_SAMPLER_DIM_1D = 0,
59 GLSL_SAMPLER_DIM_2D,
60 GLSL_SAMPLER_DIM_3D,
61 GLSL_SAMPLER_DIM_CUBE,
62 GLSL_SAMPLER_DIM_RECT,
63 GLSL_SAMPLER_DIM_BUF
64 };
65
66
67 struct glsl_type {
68 GLenum gl_type;
69 unsigned base_type:4;
70
71 unsigned sampler_dimensionality:3;
72 unsigned sampler_shadow:1;
73 unsigned sampler_array:1;
74 unsigned sampler_type:2; /**< Type of data returned using this sampler.
75 * only \c GLSL_TYPE_FLOAT, \c GLSL_TYPE_INT,
76 * and \c GLSL_TYPE_UINT are valid.
77 */
78
79 /* Callers of this talloc-based new need not call delete. It's
80 * easier to just talloc_free 'mem_ctx' (or any of its ancestors). */
81 static void* operator new(size_t size)
82 {
83 if (glsl_type::mem_ctx == NULL) {
84 glsl_type::mem_ctx = talloc_init("glsl_type");
85 assert(glsl_type::mem_ctx != NULL);
86 }
87
88 void *type;
89
90 type = talloc_size(glsl_type::mem_ctx, size);
91 assert(type != NULL);
92
93 return type;
94 }
95
96 /* If the user *does* call delete, that's OK, we will just
97 * talloc_free in that case. */
98 static void operator delete(void *type)
99 {
100 talloc_free(type);
101 }
102
103 /**
104 * \name Vector and matrix element counts
105 *
106 * For scalars, each of these values will be 1. For non-numeric types
107 * these will be 0.
108 */
109 /*@{*/
110 unsigned vector_elements:3; /**< 1, 2, 3, or 4 vector elements. */
111 unsigned matrix_columns:3; /**< 1, 2, 3, or 4 matrix columns. */
112 /*@}*/
113
114 /**
115 * Name of the data type
116 *
117 * This may be \c NULL for anonymous structures, for arrays, or for
118 * function types.
119 */
120 const char *name;
121
122 /**
123 * For \c GLSL_TYPE_ARRAY, this is the length of the array. For
124 * \c GLSL_TYPE_STRUCT, it is the number of elements in the structure and
125 * the number of values pointed to by \c fields.structure (below).
126 *
127 * For \c GLSL_TYPE_FUNCTION, it is the number of parameters to the
128 * function. The return value from a function is implicitly the first
129 * parameter. The types of the parameters are stored in
130 * \c fields.parameters (below).
131 */
132 unsigned length;
133
134 /**
135 * Subtype of composite data types.
136 */
137 union {
138 const struct glsl_type *array; /**< Type of array elements. */
139 const struct glsl_type *parameters; /**< Parameters to function. */
140 struct glsl_struct_field *structure; /**< List of struct fields. */
141 } fields;
142
143
144 /**
145 * \name Pointers to various public type singletons
146 */
147 /*@{*/
148 static const glsl_type *const error_type;
149 static const glsl_type *const int_type;
150 static const glsl_type *const ivec4_type;
151 static const glsl_type *const uint_type;
152 static const glsl_type *const uvec2_type;
153 static const glsl_type *const uvec3_type;
154 static const glsl_type *const uvec4_type;
155 static const glsl_type *const float_type;
156 static const glsl_type *const vec2_type;
157 static const glsl_type *const vec3_type;
158 static const glsl_type *const vec4_type;
159 static const glsl_type *const bool_type;
160 static const glsl_type *const mat2_type;
161 static const glsl_type *const mat2x3_type;
162 static const glsl_type *const mat2x4_type;
163 static const glsl_type *const mat3x2_type;
164 static const glsl_type *const mat3_type;
165 static const glsl_type *const mat3x4_type;
166 static const glsl_type *const mat4x2_type;
167 static const glsl_type *const mat4x3_type;
168 static const glsl_type *const mat4_type;
169 /*@}*/
170
171
172 /**
173 * For numeric and boolean derrived types returns the basic scalar type
174 *
175 * If the type is a numeric or boolean scalar, vector, or matrix type,
176 * this function gets the scalar type of the individual components. For
177 * all other types, including arrays of numeric or boolean types, the
178 * error type is returned.
179 */
180 const glsl_type *get_base_type() const;
181
182 /**
183 * Query the type of elements in an array
184 *
185 * \return
186 * Pointer to the type of elements in the array for array types, or \c NULL
187 * for non-array types.
188 */
189 const glsl_type *element_type() const
190 {
191 return is_array() ? fields.array : NULL;
192 }
193
194 /**
195 * Get the instance of a built-in scalar, vector, or matrix type
196 */
197 static const glsl_type *get_instance(unsigned base_type, unsigned rows,
198 unsigned columns);
199
200 /**
201 * Get the instance of an array type
202 */
203 static const glsl_type *get_array_instance(const glsl_type *base,
204 unsigned elements);
205
206 /**
207 * Get the instance of a record type
208 */
209 static const glsl_type *get_record_instance(const glsl_struct_field *fields,
210 unsigned num_fields,
211 const char *name);
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 /**
392 * talloc context for all glsl_type allocations
393 *
394 * Set on the first call to \c glsl_type::new.
395 */
396 static void *mem_ctx;
397
398 void init_talloc_type_ctx(void);
399
400 /** Constructor for vector and matrix types */
401 glsl_type(GLenum gl_type,
402 unsigned base_type, unsigned vector_elements,
403 unsigned matrix_columns, const char *name);
404
405 /** Constructor for sampler types */
406 glsl_type(GLenum gl_type,
407 enum glsl_sampler_dim dim, bool shadow, bool array,
408 unsigned type, const char *name);
409
410 /** Constructor for record types */
411 glsl_type(const glsl_struct_field *fields, unsigned num_fields,
412 const char *name);
413
414 /** Constructor for array types */
415 glsl_type(const glsl_type *array, unsigned length);
416
417 /** Hash table containing the known array types. */
418 static struct hash_table *array_types;
419
420 /** Hash table containing the known record types. */
421 static struct hash_table *record_types;
422
423 static int record_key_compare(const void *a, const void *b);
424 static unsigned record_key_hash(const void *key);
425
426 /**
427 * \name Pointers to various type singletons
428 */
429 /*@{*/
430 static const glsl_type _error_type;
431 static const glsl_type void_type;
432 static const glsl_type builtin_core_types[];
433 static const glsl_type builtin_structure_types[];
434 static const glsl_type builtin_110_deprecated_structure_types[];
435 static const glsl_type builtin_110_types[];
436 static const glsl_type builtin_120_types[];
437 static const glsl_type builtin_130_types[];
438 static const glsl_type builtin_ARB_texture_rectangle_types[];
439 static const glsl_type builtin_EXT_texture_array_types[];
440 static const glsl_type builtin_EXT_texture_buffer_object_types[];
441 /*@}*/
442
443 /**
444 * \name Methods to populate a symbol table with built-in types.
445 *
446 * \internal
447 * This is one of the truely annoying things about C++. Methods that are
448 * completely internal and private to a type still have to be advertised to
449 * the world in a public header file.
450 */
451 /*@{*/
452 static void generate_100ES_types(glsl_symbol_table *);
453 static void generate_110_types(glsl_symbol_table *);
454 static void generate_120_types(glsl_symbol_table *);
455 static void generate_130_types(glsl_symbol_table *);
456 static void generate_ARB_texture_rectangle_types(glsl_symbol_table *, bool);
457 static void generate_EXT_texture_array_types(glsl_symbol_table *, bool);
458 /*@}*/
459
460 /**
461 * \name Friend functions.
462 *
463 * These functions are friends because they must have C linkage and the
464 * need to call various private methods or access various private static
465 * data.
466 */
467 /*@{*/
468 friend void _mesa_glsl_initialize_types(struct _mesa_glsl_parse_state *);
469 friend void _mesa_glsl_release_types(void);
470 /*@}*/
471 };
472
473 struct glsl_struct_field {
474 const struct glsl_type *type;
475 const char *name;
476 };
477
478 #endif /* GLSL_TYPES_H */