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