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