glsl: Add GLSL_TYPE_FUNCTION to the base types enums
[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_DOUBLE,
54 GLSL_TYPE_BOOL,
55 GLSL_TYPE_SAMPLER,
56 GLSL_TYPE_IMAGE,
57 GLSL_TYPE_ATOMIC_UINT,
58 GLSL_TYPE_STRUCT,
59 GLSL_TYPE_FUNCTION,
60 GLSL_TYPE_INTERFACE,
61 GLSL_TYPE_ARRAY,
62 GLSL_TYPE_VOID,
63 GLSL_TYPE_ERROR
64 };
65
66 enum glsl_sampler_dim {
67 GLSL_SAMPLER_DIM_1D = 0,
68 GLSL_SAMPLER_DIM_2D,
69 GLSL_SAMPLER_DIM_3D,
70 GLSL_SAMPLER_DIM_CUBE,
71 GLSL_SAMPLER_DIM_RECT,
72 GLSL_SAMPLER_DIM_BUF,
73 GLSL_SAMPLER_DIM_EXTERNAL,
74 GLSL_SAMPLER_DIM_MS
75 };
76
77 enum glsl_interface_packing {
78 GLSL_INTERFACE_PACKING_STD140,
79 GLSL_INTERFACE_PACKING_SHARED,
80 GLSL_INTERFACE_PACKING_PACKED
81 };
82
83 enum glsl_matrix_layout {
84 /**
85 * The layout of the matrix is inherited from the object containing the
86 * matrix (the top level structure or the uniform block).
87 */
88 GLSL_MATRIX_LAYOUT_INHERITED,
89
90 /**
91 * Explicit column-major layout
92 *
93 * If a uniform block doesn't have an explicit layout set, it will default
94 * to this layout.
95 */
96 GLSL_MATRIX_LAYOUT_COLUMN_MAJOR,
97
98 /**
99 * Row-major layout
100 */
101 GLSL_MATRIX_LAYOUT_ROW_MAJOR
102 };
103
104 #ifdef __cplusplus
105 #include "GL/gl.h"
106 #include "util/ralloc.h"
107 #include "main/mtypes.h" /* for gl_texture_index, C++'s enum rules are broken */
108
109 struct glsl_type {
110 GLenum gl_type;
111 glsl_base_type base_type;
112
113 unsigned sampler_dimensionality:3; /**< \see glsl_sampler_dim */
114 unsigned sampler_shadow:1;
115 unsigned sampler_array:1;
116 unsigned sampler_type:2; /**< Type of data returned using this
117 * sampler or image. Only \c
118 * GLSL_TYPE_FLOAT, \c GLSL_TYPE_INT,
119 * and \c GLSL_TYPE_UINT are valid.
120 */
121 unsigned interface_packing:2;
122
123 /* Callers of this ralloc-based new need not call delete. It's
124 * easier to just ralloc_free 'mem_ctx' (or any of its ancestors). */
125 static void* operator new(size_t size)
126 {
127 mtx_lock(&glsl_type::mutex);
128
129 /* mem_ctx should have been created by the static members */
130 assert(glsl_type::mem_ctx != NULL);
131
132 void *type;
133
134 type = ralloc_size(glsl_type::mem_ctx, size);
135 assert(type != NULL);
136
137 mtx_unlock(&glsl_type::mutex);
138
139 return type;
140 }
141
142 /* If the user *does* call delete, that's OK, we will just
143 * ralloc_free in that case. */
144 static void operator delete(void *type)
145 {
146 mtx_lock(&glsl_type::mutex);
147 ralloc_free(type);
148 mtx_unlock(&glsl_type::mutex);
149 }
150
151 /**
152 * \name Vector and matrix element counts
153 *
154 * For scalars, each of these values will be 1. For non-numeric types
155 * these will be 0.
156 */
157 /*@{*/
158 uint8_t vector_elements; /**< 1, 2, 3, or 4 vector elements. */
159 uint8_t matrix_columns; /**< 1, 2, 3, or 4 matrix columns. */
160 /*@}*/
161
162 /**
163 * For \c GLSL_TYPE_ARRAY, this is the length of the array. For
164 * \c GLSL_TYPE_STRUCT or \c GLSL_TYPE_INTERFACE, it is the number of
165 * elements in the structure and the number of values pointed to by
166 * \c fields.structure (below).
167 */
168 unsigned length;
169
170 /**
171 * Name of the data type
172 *
173 * Will never be \c NULL.
174 */
175 const char *name;
176
177 /**
178 * Subtype of composite data types.
179 */
180 union {
181 const struct glsl_type *array; /**< Type of array elements. */
182 const struct glsl_type *parameters; /**< Parameters to function. */
183 struct glsl_struct_field *structure; /**< List of struct fields. */
184 } fields;
185
186 /**
187 * \name Pointers to various public type singletons
188 */
189 /*@{*/
190 #undef DECL_TYPE
191 #define DECL_TYPE(NAME, ...) \
192 static const glsl_type *const NAME##_type;
193 #undef STRUCT_TYPE
194 #define STRUCT_TYPE(NAME) \
195 static const glsl_type *const struct_##NAME##_type;
196 #include "builtin_type_macros.h"
197 /*@}*/
198
199 /**
200 * Convenience accessors for vector types (shorter than get_instance()).
201 * @{
202 */
203 static const glsl_type *vec(unsigned components);
204 static const glsl_type *dvec(unsigned components);
205 static const glsl_type *ivec(unsigned components);
206 static const glsl_type *uvec(unsigned components);
207 static const glsl_type *bvec(unsigned components);
208 /**@}*/
209
210 /**
211 * For numeric and boolean derived types returns the basic scalar type
212 *
213 * If the type is a numeric or boolean scalar, vector, or matrix type,
214 * this function gets the scalar type of the individual components. For
215 * all other types, including arrays of numeric or boolean types, the
216 * error type is returned.
217 */
218 const glsl_type *get_base_type() const;
219
220 /**
221 * Get the basic scalar type which this type aggregates.
222 *
223 * If the type is a numeric or boolean scalar, vector, or matrix, or an
224 * array of any of those, this function gets the scalar type of the
225 * individual components. For structs and arrays of structs, this function
226 * returns the struct type. For samplers and arrays of samplers, this
227 * function returns the sampler type.
228 */
229 const glsl_type *get_scalar_type() const;
230
231 /**
232 * Query the type of elements in an array
233 *
234 * \return
235 * Pointer to the type of elements in the array for array types, or \c NULL
236 * for non-array types.
237 */
238 const glsl_type *element_type() const
239 {
240 return is_array() ? fields.array : NULL;
241 }
242
243 /**
244 * Get the instance of a built-in scalar, vector, or matrix type
245 */
246 static const glsl_type *get_instance(unsigned base_type, unsigned rows,
247 unsigned columns);
248
249 /**
250 * Get the instance of a sampler type
251 */
252 static const glsl_type *get_sampler_instance(enum glsl_sampler_dim dim,
253 bool shadow,
254 bool array,
255 glsl_base_type type);
256
257
258 /**
259 * Get the instance of an array type
260 */
261 static const glsl_type *get_array_instance(const glsl_type *base,
262 unsigned elements);
263
264 /**
265 * Get the instance of a record type
266 */
267 static const glsl_type *get_record_instance(const glsl_struct_field *fields,
268 unsigned num_fields,
269 const char *name);
270
271 /**
272 * Get the instance of an interface block type
273 */
274 static const glsl_type *get_interface_instance(const glsl_struct_field *fields,
275 unsigned num_fields,
276 enum glsl_interface_packing packing,
277 const char *block_name);
278
279 /**
280 * Get the type resulting from a multiplication of \p type_a * \p type_b
281 */
282 static const glsl_type *get_mul_type(const glsl_type *type_a,
283 const glsl_type *type_b);
284
285 /**
286 * Query the total number of scalars that make up a scalar, vector or matrix
287 */
288 unsigned components() const
289 {
290 return vector_elements * matrix_columns;
291 }
292
293 /**
294 * Calculate the number of components slots required to hold this type
295 *
296 * This is used to determine how many uniform or varying locations a type
297 * might occupy.
298 */
299 unsigned component_slots() const;
300
301 /**
302 * Calculate the number of unique values from glGetUniformLocation for the
303 * elements of the type.
304 *
305 * This is used to allocate slots in the UniformRemapTable, the amount of
306 * locations may not match with actual used storage space by the driver.
307 */
308 unsigned uniform_locations() const;
309
310 /**
311 * Calculate the number of attribute slots required to hold this type
312 *
313 * This implements the language rules of GLSL 1.50 for counting the number
314 * of slots used by a vertex attribute. It also determines the number of
315 * varying slots the type will use up in the absence of varying packing
316 * (and thus, it can be used to measure the number of varying slots used by
317 * the varyings that are generated by lower_packed_varyings).
318 */
319 unsigned count_attribute_slots() const;
320
321
322 /**
323 * Alignment in bytes of the start of this type in a std140 uniform
324 * block.
325 */
326 unsigned std140_base_alignment(bool row_major) const;
327
328 /** Size in bytes of this type in a std140 uniform block.
329 *
330 * Note that this is not GL_UNIFORM_SIZE (which is the number of
331 * elements in the array)
332 */
333 unsigned std140_size(bool row_major) const;
334
335 /**
336 * \brief Can this type be implicitly converted to another?
337 *
338 * \return True if the types are identical or if this type can be converted
339 * to \c desired according to Section 4.1.10 of the GLSL spec.
340 *
341 * \verbatim
342 * From page 25 (31 of the pdf) of the GLSL 1.50 spec, Section 4.1.10
343 * Implicit Conversions:
344 *
345 * In some situations, an expression and its type will be implicitly
346 * converted to a different type. The following table shows all allowed
347 * implicit conversions:
348 *
349 * Type of expression | Can be implicitly converted to
350 * --------------------------------------------------
351 * int float
352 * uint
353 *
354 * ivec2 vec2
355 * uvec2
356 *
357 * ivec3 vec3
358 * uvec3
359 *
360 * ivec4 vec4
361 * uvec4
362 *
363 * There are no implicit array or structure conversions. For example,
364 * an array of int cannot be implicitly converted to an array of float.
365 * There are no implicit conversions between signed and unsigned
366 * integers.
367 * \endverbatim
368 */
369 bool can_implicitly_convert_to(const glsl_type *desired,
370 _mesa_glsl_parse_state *state) const;
371
372 /**
373 * Query whether or not a type is a scalar (non-vector and non-matrix).
374 */
375 bool is_scalar() const
376 {
377 return (vector_elements == 1)
378 && (base_type >= GLSL_TYPE_UINT)
379 && (base_type <= GLSL_TYPE_BOOL);
380 }
381
382 /**
383 * Query whether or not a type is a vector
384 */
385 bool is_vector() const
386 {
387 return (vector_elements > 1)
388 && (matrix_columns == 1)
389 && (base_type >= GLSL_TYPE_UINT)
390 && (base_type <= GLSL_TYPE_BOOL);
391 }
392
393 /**
394 * Query whether or not a type is a matrix
395 */
396 bool is_matrix() const
397 {
398 /* GLSL only has float matrices. */
399 return (matrix_columns > 1) && (base_type == GLSL_TYPE_FLOAT || base_type == GLSL_TYPE_DOUBLE);
400 }
401
402 /**
403 * Query whether or not a type is a non-array numeric type
404 */
405 bool is_numeric() const
406 {
407 return (base_type >= GLSL_TYPE_UINT) && (base_type <= GLSL_TYPE_DOUBLE);
408 }
409
410 /**
411 * Query whether or not a type is an integral type
412 */
413 bool is_integer() const
414 {
415 return (base_type == GLSL_TYPE_UINT) || (base_type == GLSL_TYPE_INT);
416 }
417
418 /**
419 * Query whether or not type is an integral type, or for struct and array
420 * types, contains an integral type.
421 */
422 bool contains_integer() const;
423
424 /**
425 * Query whether or not type is a double type, or for struct and array
426 * types, contains a double type.
427 */
428 bool contains_double() const;
429
430 /**
431 * Query whether or not a type is a float type
432 */
433 bool is_float() const
434 {
435 return base_type == GLSL_TYPE_FLOAT;
436 }
437
438 /**
439 * Query whether or not a type is a double type
440 */
441 bool is_double() const
442 {
443 return base_type == GLSL_TYPE_DOUBLE;
444 }
445
446 /**
447 * Query whether or not a type is a non-array boolean type
448 */
449 bool is_boolean() const
450 {
451 return base_type == GLSL_TYPE_BOOL;
452 }
453
454 /**
455 * Query whether or not a type is a sampler
456 */
457 bool is_sampler() const
458 {
459 return base_type == GLSL_TYPE_SAMPLER;
460 }
461
462 /**
463 * Query whether or not type is a sampler, or for struct and array
464 * types, contains a sampler.
465 */
466 bool contains_sampler() const;
467
468 /**
469 * Get the Mesa texture target index for a sampler type.
470 */
471 gl_texture_index sampler_index() const;
472
473 /**
474 * Query whether or not type is an image, or for struct and array
475 * types, contains an image.
476 */
477 bool contains_image() const;
478
479 /**
480 * Query whether or not a type is an image
481 */
482 bool is_image() const
483 {
484 return base_type == GLSL_TYPE_IMAGE;
485 }
486
487 /**
488 * Query whether or not a type is an array
489 */
490 bool is_array() const
491 {
492 return base_type == GLSL_TYPE_ARRAY;
493 }
494
495 /**
496 * Query whether or not a type is a record
497 */
498 bool is_record() const
499 {
500 return base_type == GLSL_TYPE_STRUCT;
501 }
502
503 /**
504 * Query whether or not a type is an interface
505 */
506 bool is_interface() const
507 {
508 return base_type == GLSL_TYPE_INTERFACE;
509 }
510
511 /**
512 * Query whether or not a type is the void type singleton.
513 */
514 bool is_void() const
515 {
516 return base_type == GLSL_TYPE_VOID;
517 }
518
519 /**
520 * Query whether or not a type is the error type singleton.
521 */
522 bool is_error() const
523 {
524 return base_type == GLSL_TYPE_ERROR;
525 }
526
527 /**
528 * Query if a type is unnamed/anonymous (named by the parser)
529 */
530 bool is_anonymous() const
531 {
532 return !strncmp(name, "#anon", 5);
533 }
534
535 /**
536 * Get the type stripped of any arrays
537 *
538 * \return
539 * Pointer to the type of elements of the first non-array type for array
540 * types, or pointer to itself for non-array types.
541 */
542 const glsl_type *without_array() const
543 {
544 const glsl_type *t = this;
545
546 while (t->is_array())
547 t = t->fields.array;
548
549 return t;
550 }
551
552 /**
553 * Return the amount of atomic counter storage required for a type.
554 */
555 unsigned atomic_size() const
556 {
557 if (base_type == GLSL_TYPE_ATOMIC_UINT)
558 return ATOMIC_COUNTER_SIZE;
559 else if (is_array())
560 return length * element_type()->atomic_size();
561 else
562 return 0;
563 }
564
565 /**
566 * Return whether a type contains any atomic counters.
567 */
568 bool contains_atomic() const
569 {
570 return atomic_size() > 0;
571 }
572
573 /**
574 * Return whether a type contains any opaque types.
575 */
576 bool contains_opaque() const;
577
578 /**
579 * Query the full type of a matrix row
580 *
581 * \return
582 * If the type is not a matrix, \c glsl_type::error_type is returned.
583 * Otherwise a type matching the rows of the matrix is returned.
584 */
585 const glsl_type *row_type() const
586 {
587 return is_matrix()
588 ? get_instance(base_type, matrix_columns, 1)
589 : error_type;
590 }
591
592 /**
593 * Query the full type of a matrix column
594 *
595 * \return
596 * If the type is not a matrix, \c glsl_type::error_type is returned.
597 * Otherwise a type matching the columns of the matrix is returned.
598 */
599 const glsl_type *column_type() const
600 {
601 return is_matrix()
602 ? get_instance(base_type, vector_elements, 1)
603 : error_type;
604 }
605
606 /**
607 * Get the type of a structure field
608 *
609 * \return
610 * Pointer to the type of the named field. If the type is not a structure
611 * or the named field does not exist, \c glsl_type::error_type is returned.
612 */
613 const glsl_type *field_type(const char *name) const;
614
615 /**
616 * Get the location of a filed within a record type
617 */
618 int field_index(const char *name) const;
619
620 /**
621 * Query the number of elements in an array type
622 *
623 * \return
624 * The number of elements in the array for array types or -1 for non-array
625 * types. If the number of elements in the array has not yet been declared,
626 * zero is returned.
627 */
628 int array_size() const
629 {
630 return is_array() ? length : -1;
631 }
632
633 /**
634 * Query whether the array size for all dimensions has been declared.
635 */
636 bool is_unsized_array() const
637 {
638 return is_array() && length == 0;
639 }
640
641 /**
642 * Return the number of coordinate components needed for this
643 * sampler or image type.
644 *
645 * This is based purely on the sampler's dimensionality. For example, this
646 * returns 1 for sampler1D, and 3 for sampler2DArray.
647 *
648 * Note that this is often different than actual coordinate type used in
649 * a texturing built-in function, since those pack additional values (such
650 * as the shadow comparitor or projector) into the coordinate type.
651 */
652 int coordinate_components() const;
653
654 /**
655 * Compare a record type against another record type.
656 *
657 * This is useful for matching record types declared across shader stages.
658 */
659 bool record_compare(const glsl_type *b) const;
660
661 private:
662
663 static mtx_t mutex;
664
665 /**
666 * ralloc context for all glsl_type allocations
667 *
668 * Set on the first call to \c glsl_type::new.
669 */
670 static void *mem_ctx;
671
672 void init_ralloc_type_ctx(void);
673
674 /** Constructor for vector and matrix types */
675 glsl_type(GLenum gl_type,
676 glsl_base_type base_type, unsigned vector_elements,
677 unsigned matrix_columns, const char *name);
678
679 /** Constructor for sampler or image types */
680 glsl_type(GLenum gl_type, glsl_base_type base_type,
681 enum glsl_sampler_dim dim, bool shadow, bool array,
682 unsigned type, const char *name);
683
684 /** Constructor for record types */
685 glsl_type(const glsl_struct_field *fields, unsigned num_fields,
686 const char *name);
687
688 /** Constructor for interface types */
689 glsl_type(const glsl_struct_field *fields, unsigned num_fields,
690 enum glsl_interface_packing packing, const char *name);
691
692 /** Constructor for array types */
693 glsl_type(const glsl_type *array, unsigned length);
694
695 /** Hash table containing the known array types. */
696 static struct hash_table *array_types;
697
698 /** Hash table containing the known record types. */
699 static struct hash_table *record_types;
700
701 /** Hash table containing the known interface types. */
702 static struct hash_table *interface_types;
703
704 static int record_key_compare(const void *a, const void *b);
705 static unsigned record_key_hash(const void *key);
706
707 /**
708 * \name Built-in type flyweights
709 */
710 /*@{*/
711 #undef DECL_TYPE
712 #define DECL_TYPE(NAME, ...) static const glsl_type _##NAME##_type;
713 #undef STRUCT_TYPE
714 #define STRUCT_TYPE(NAME) static const glsl_type _struct_##NAME##_type;
715 #include "builtin_type_macros.h"
716 /*@}*/
717
718 /**
719 * \name Friend functions.
720 *
721 * These functions are friends because they must have C linkage and the
722 * need to call various private methods or access various private static
723 * data.
724 */
725 /*@{*/
726 friend void _mesa_glsl_initialize_types(struct _mesa_glsl_parse_state *);
727 friend void _mesa_glsl_release_types(void);
728 /*@}*/
729 };
730
731 struct glsl_struct_field {
732 const struct glsl_type *type;
733 const char *name;
734
735 /**
736 * For interface blocks, gl_varying_slot corresponding to the input/output
737 * if this is a built-in input/output (i.e. a member of the built-in
738 * gl_PerVertex interface block); -1 otherwise.
739 *
740 * Ignored for structs.
741 */
742 int location;
743
744 /**
745 * For interface blocks, the interpolation mode (as in
746 * ir_variable::interpolation). 0 otherwise.
747 */
748 unsigned interpolation:2;
749
750 /**
751 * For interface blocks, 1 if this variable uses centroid interpolation (as
752 * in ir_variable::centroid). 0 otherwise.
753 */
754 unsigned centroid:1;
755
756 /**
757 * For interface blocks, 1 if this variable uses sample interpolation (as
758 * in ir_variable::sample). 0 otherwise.
759 */
760 unsigned sample:1;
761
762 /**
763 * Layout of the matrix. Uses glsl_matrix_layout values.
764 */
765 unsigned matrix_layout:2;
766
767 /**
768 * For interface blocks, it has a value if this variable uses multiple vertex
769 * streams (as in ir_variable::stream). -1 otherwise.
770 */
771 int stream;
772 };
773
774 static inline unsigned int
775 glsl_align(unsigned int a, unsigned int align)
776 {
777 return (a + align - 1) / align * align;
778 }
779
780 #undef DECL_TYPE
781 #undef STRUCT_TYPE
782 #endif /* __cplusplus */
783
784 #endif /* GLSL_TYPES_H */