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