compiler: move glsl_interface_packing enum to shader_enums.h
[mesa.git] / src / compiler / 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 #ifndef GLSL_TYPES_H
26 #define GLSL_TYPES_H
27
28 #include <string.h>
29 #include <assert.h>
30
31 #include "shader_enums.h"
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 /* Note: GLSL_TYPE_UINT, GLSL_TYPE_INT, and GLSL_TYPE_FLOAT must be 0, 1,
52 * and 2 so that they will fit in the 2 bits of glsl_type::sampled_type.
53 */
54 GLSL_TYPE_UINT = 0,
55 GLSL_TYPE_INT,
56 GLSL_TYPE_FLOAT,
57 GLSL_TYPE_DOUBLE,
58 GLSL_TYPE_UINT64,
59 GLSL_TYPE_INT64,
60 GLSL_TYPE_BOOL,
61 GLSL_TYPE_SAMPLER,
62 GLSL_TYPE_IMAGE,
63 GLSL_TYPE_ATOMIC_UINT,
64 GLSL_TYPE_STRUCT,
65 GLSL_TYPE_INTERFACE,
66 GLSL_TYPE_ARRAY,
67 GLSL_TYPE_VOID,
68 GLSL_TYPE_SUBROUTINE,
69 GLSL_TYPE_FUNCTION,
70 GLSL_TYPE_ERROR
71 };
72
73 static inline bool glsl_base_type_is_64bit(enum glsl_base_type type)
74 {
75 return type == GLSL_TYPE_DOUBLE ||
76 type == GLSL_TYPE_UINT64 ||
77 type == GLSL_TYPE_INT64 ||
78 type == GLSL_TYPE_IMAGE ||
79 type == GLSL_TYPE_SAMPLER;
80 }
81
82 static inline bool glsl_base_type_is_integer(enum glsl_base_type type)
83 {
84 return type == GLSL_TYPE_UINT ||
85 type == GLSL_TYPE_INT ||
86 type == GLSL_TYPE_UINT64 ||
87 type == GLSL_TYPE_INT64 ||
88 type == GLSL_TYPE_BOOL ||
89 type == GLSL_TYPE_SAMPLER ||
90 type == GLSL_TYPE_IMAGE;
91 }
92
93 enum glsl_sampler_dim {
94 GLSL_SAMPLER_DIM_1D = 0,
95 GLSL_SAMPLER_DIM_2D,
96 GLSL_SAMPLER_DIM_3D,
97 GLSL_SAMPLER_DIM_CUBE,
98 GLSL_SAMPLER_DIM_RECT,
99 GLSL_SAMPLER_DIM_BUF,
100 GLSL_SAMPLER_DIM_EXTERNAL,
101 GLSL_SAMPLER_DIM_MS,
102 GLSL_SAMPLER_DIM_SUBPASS, /* for vulkan input attachments */
103 GLSL_SAMPLER_DIM_SUBPASS_MS, /* for multisampled vulkan input attachments */
104 };
105
106 enum glsl_matrix_layout {
107 /**
108 * The layout of the matrix is inherited from the object containing the
109 * matrix (the top level structure or the uniform block).
110 */
111 GLSL_MATRIX_LAYOUT_INHERITED,
112
113 /**
114 * Explicit column-major layout
115 *
116 * If a uniform block doesn't have an explicit layout set, it will default
117 * to this layout.
118 */
119 GLSL_MATRIX_LAYOUT_COLUMN_MAJOR,
120
121 /**
122 * Row-major layout
123 */
124 GLSL_MATRIX_LAYOUT_ROW_MAJOR
125 };
126
127 enum {
128 GLSL_PRECISION_NONE = 0,
129 GLSL_PRECISION_HIGH,
130 GLSL_PRECISION_MEDIUM,
131 GLSL_PRECISION_LOW
132 };
133
134 #ifdef __cplusplus
135 #include "GL/gl.h"
136 #include "util/ralloc.h"
137 #include "main/mtypes.h" /* for gl_texture_index, C++'s enum rules are broken */
138
139 struct glsl_type {
140 GLenum gl_type;
141 glsl_base_type base_type;
142
143 unsigned sampler_dimensionality:4; /**< \see glsl_sampler_dim */
144 unsigned sampler_shadow:1;
145 unsigned sampler_array:1;
146 unsigned sampled_type:2; /**< Type of data returned using this
147 * sampler or image. Only \c
148 * GLSL_TYPE_FLOAT, \c GLSL_TYPE_INT,
149 * and \c GLSL_TYPE_UINT are valid.
150 */
151 unsigned interface_packing:2;
152 unsigned interface_row_major:1;
153
154 /* Callers of this ralloc-based new need not call delete. It's
155 * easier to just ralloc_free 'mem_ctx' (or any of its ancestors). */
156 static void* operator new(size_t size)
157 {
158 mtx_lock(&glsl_type::mem_mutex);
159
160 /* mem_ctx should have been created by the static members */
161 assert(glsl_type::mem_ctx != NULL);
162
163 void *type;
164
165 type = ralloc_size(glsl_type::mem_ctx, size);
166 assert(type != NULL);
167
168 mtx_unlock(&glsl_type::mem_mutex);
169
170 return type;
171 }
172
173 /* If the user *does* call delete, that's OK, we will just
174 * ralloc_free in that case. */
175 static void operator delete(void *type)
176 {
177 mtx_lock(&glsl_type::mem_mutex);
178 ralloc_free(type);
179 mtx_unlock(&glsl_type::mem_mutex);
180 }
181
182 /**
183 * \name Vector and matrix element counts
184 *
185 * For scalars, each of these values will be 1. For non-numeric types
186 * these will be 0.
187 */
188 /*@{*/
189 uint8_t vector_elements; /**< 1, 2, 3, or 4 vector elements. */
190 uint8_t matrix_columns; /**< 1, 2, 3, or 4 matrix columns. */
191 /*@}*/
192
193 /**
194 * For \c GLSL_TYPE_ARRAY, this is the length of the array. For
195 * \c GLSL_TYPE_STRUCT or \c GLSL_TYPE_INTERFACE, it is the number of
196 * elements in the structure and the number of values pointed to by
197 * \c fields.structure (below).
198 */
199 unsigned length;
200
201 /**
202 * Name of the data type
203 *
204 * Will never be \c NULL.
205 */
206 const char *name;
207
208 /**
209 * Subtype of composite data types.
210 */
211 union {
212 const struct glsl_type *array; /**< Type of array elements. */
213 struct glsl_function_param *parameters; /**< Parameters to function. */
214 struct glsl_struct_field *structure; /**< List of struct fields. */
215 } fields;
216
217 /**
218 * \name Pointers to various public type singletons
219 */
220 /*@{*/
221 #undef DECL_TYPE
222 #define DECL_TYPE(NAME, ...) \
223 static const glsl_type *const NAME##_type;
224 #undef STRUCT_TYPE
225 #define STRUCT_TYPE(NAME) \
226 static const glsl_type *const struct_##NAME##_type;
227 #include "compiler/builtin_type_macros.h"
228 /*@}*/
229
230 /**
231 * Convenience accessors for vector types (shorter than get_instance()).
232 * @{
233 */
234 static const glsl_type *vec(unsigned components);
235 static const glsl_type *dvec(unsigned components);
236 static const glsl_type *ivec(unsigned components);
237 static const glsl_type *uvec(unsigned components);
238 static const glsl_type *bvec(unsigned components);
239 static const glsl_type *i64vec(unsigned components);
240 static const glsl_type *u64vec(unsigned components);
241 /**@}*/
242
243 /**
244 * For numeric and boolean derived types returns the basic scalar type
245 *
246 * If the type is a numeric or boolean scalar, vector, or matrix type,
247 * this function gets the scalar type of the individual components. For
248 * all other types, including arrays of numeric or boolean types, the
249 * error type is returned.
250 */
251 const glsl_type *get_base_type() const;
252
253 /**
254 * Get the basic scalar type which this type aggregates.
255 *
256 * If the type is a numeric or boolean scalar, vector, or matrix, or an
257 * array of any of those, this function gets the scalar type of the
258 * individual components. For structs and arrays of structs, this function
259 * returns the struct type. For samplers and arrays of samplers, this
260 * function returns the sampler type.
261 */
262 const glsl_type *get_scalar_type() const;
263
264 /**
265 * Get the instance of a built-in scalar, vector, or matrix type
266 */
267 static const glsl_type *get_instance(unsigned base_type, unsigned rows,
268 unsigned columns);
269
270 /**
271 * Get the instance of a sampler type
272 */
273 static const glsl_type *get_sampler_instance(enum glsl_sampler_dim dim,
274 bool shadow,
275 bool array,
276 glsl_base_type type);
277
278 static const glsl_type *get_image_instance(enum glsl_sampler_dim dim,
279 bool array, glsl_base_type type);
280
281 /**
282 * Get the instance of an array type
283 */
284 static const glsl_type *get_array_instance(const glsl_type *base,
285 unsigned elements);
286
287 /**
288 * Get the instance of a record type
289 */
290 static const glsl_type *get_record_instance(const glsl_struct_field *fields,
291 unsigned num_fields,
292 const char *name);
293
294 /**
295 * Get the instance of an interface block type
296 */
297 static const glsl_type *get_interface_instance(const glsl_struct_field *fields,
298 unsigned num_fields,
299 enum glsl_interface_packing packing,
300 bool row_major,
301 const char *block_name);
302
303 /**
304 * Get the instance of an subroutine type
305 */
306 static const glsl_type *get_subroutine_instance(const char *subroutine_name);
307
308 /**
309 * Get the instance of a function type
310 */
311 static const glsl_type *get_function_instance(const struct glsl_type *return_type,
312 const glsl_function_param *parameters,
313 unsigned num_params);
314
315 /**
316 * Get the type resulting from a multiplication of \p type_a * \p type_b
317 */
318 static const glsl_type *get_mul_type(const glsl_type *type_a,
319 const glsl_type *type_b);
320
321 /**
322 * Query the total number of scalars that make up a scalar, vector or matrix
323 */
324 unsigned components() const
325 {
326 return vector_elements * matrix_columns;
327 }
328
329 /**
330 * Calculate the number of components slots required to hold this type
331 *
332 * This is used to determine how many uniform or varying locations a type
333 * might occupy.
334 */
335 unsigned component_slots() const;
336
337 /**
338 * Calculate offset between the base location of the struct in
339 * uniform storage and a struct member.
340 * For the initial call, length is the index of the member to find the
341 * offset for.
342 */
343 unsigned record_location_offset(unsigned length) const;
344
345 /**
346 * Calculate the number of unique values from glGetUniformLocation for the
347 * elements of the type.
348 *
349 * This is used to allocate slots in the UniformRemapTable, the amount of
350 * locations may not match with actual used storage space by the driver.
351 */
352 unsigned uniform_locations() const;
353
354 /**
355 * Used to count the number of varyings contained in the type ignoring
356 * innermost array elements.
357 */
358 unsigned varying_count() const;
359
360 /**
361 * Calculate the number of attribute slots required to hold this type
362 *
363 * This implements the language rules of GLSL 1.50 for counting the number
364 * of slots used by a vertex attribute. It also determines the number of
365 * varying slots the type will use up in the absence of varying packing
366 * (and thus, it can be used to measure the number of varying slots used by
367 * the varyings that are generated by lower_packed_varyings).
368 *
369 * For vertex shader attributes - doubles only take one slot.
370 * For inter-shader varyings - dvec3/dvec4 take two slots.
371 */
372 unsigned count_attribute_slots(bool is_vertex_input) const;
373
374 /**
375 * Alignment in bytes of the start of this type in a std140 uniform
376 * block.
377 */
378 unsigned std140_base_alignment(bool row_major) const;
379
380 /** Size in bytes of this type in a std140 uniform block.
381 *
382 * Note that this is not GL_UNIFORM_SIZE (which is the number of
383 * elements in the array)
384 */
385 unsigned std140_size(bool row_major) const;
386
387 /**
388 * Alignment in bytes of the start of this type in a std430 shader
389 * storage block.
390 */
391 unsigned std430_base_alignment(bool row_major) const;
392
393 /**
394 * Calculate array stride in bytes of this type in a std430 shader storage
395 * block.
396 */
397 unsigned std430_array_stride(bool row_major) const;
398
399 /**
400 * Size in bytes of this type in a std430 shader storage block.
401 *
402 * Note that this is not GL_BUFFER_SIZE
403 */
404 unsigned std430_size(bool row_major) const;
405
406 /**
407 * \brief Can this type be implicitly converted to another?
408 *
409 * \return True if the types are identical or if this type can be converted
410 * to \c desired according to Section 4.1.10 of the GLSL spec.
411 *
412 * \verbatim
413 * From page 25 (31 of the pdf) of the GLSL 1.50 spec, Section 4.1.10
414 * Implicit Conversions:
415 *
416 * In some situations, an expression and its type will be implicitly
417 * converted to a different type. The following table shows all allowed
418 * implicit conversions:
419 *
420 * Type of expression | Can be implicitly converted to
421 * --------------------------------------------------
422 * int float
423 * uint
424 *
425 * ivec2 vec2
426 * uvec2
427 *
428 * ivec3 vec3
429 * uvec3
430 *
431 * ivec4 vec4
432 * uvec4
433 *
434 * There are no implicit array or structure conversions. For example,
435 * an array of int cannot be implicitly converted to an array of float.
436 * There are no implicit conversions between signed and unsigned
437 * integers.
438 * \endverbatim
439 */
440 bool can_implicitly_convert_to(const glsl_type *desired,
441 _mesa_glsl_parse_state *state) const;
442
443 /**
444 * Query whether or not a type is a scalar (non-vector and non-matrix).
445 */
446 bool is_scalar() const
447 {
448 return (vector_elements == 1)
449 && (base_type >= GLSL_TYPE_UINT)
450 && (base_type <= GLSL_TYPE_IMAGE);
451 }
452
453 /**
454 * Query whether or not a type is a vector
455 */
456 bool is_vector() const
457 {
458 return (vector_elements > 1)
459 && (matrix_columns == 1)
460 && (base_type >= GLSL_TYPE_UINT)
461 && (base_type <= GLSL_TYPE_BOOL);
462 }
463
464 /**
465 * Query whether or not a type is a matrix
466 */
467 bool is_matrix() const
468 {
469 /* GLSL only has float matrices. */
470 return (matrix_columns > 1) && (base_type == GLSL_TYPE_FLOAT || base_type == GLSL_TYPE_DOUBLE);
471 }
472
473 /**
474 * Query whether or not a type is a non-array numeric type
475 */
476 bool is_numeric() const
477 {
478 return (base_type >= GLSL_TYPE_UINT) && (base_type <= GLSL_TYPE_INT64);
479 }
480
481 /**
482 * Query whether or not a type is an integral type
483 */
484 bool is_integer() const
485 {
486 return (base_type == GLSL_TYPE_UINT) || (base_type == GLSL_TYPE_INT);
487 }
488
489 /**
490 * Query whether or not a type is a 64-bit integer.
491 */
492 bool is_integer_64() const
493 {
494 return base_type == GLSL_TYPE_UINT64 || base_type == GLSL_TYPE_INT64;
495 }
496
497 /**
498 * Query whether or not a type is a 32-bit or 64-bit integer
499 */
500 bool is_integer_32_64() const
501 {
502 return is_integer() || is_integer_64();
503 }
504
505 /**
506 * Query whether or not type is an integral type, or for struct and array
507 * types, contains an integral type.
508 */
509 bool contains_integer() const;
510
511 /**
512 * Query whether or not type is a double type, or for struct, interface and
513 * array types, contains a double type.
514 */
515 bool contains_double() const;
516
517 /**
518 * Query whether or not a type is a float type
519 */
520 bool is_float() const
521 {
522 return base_type == GLSL_TYPE_FLOAT;
523 }
524
525 /**
526 * Query whether or not a type is a double type
527 */
528 bool is_double() const
529 {
530 return base_type == GLSL_TYPE_DOUBLE;
531 }
532
533 /**
534 * Query whether a 64-bit type takes two slots.
535 */
536 bool is_dual_slot() const
537 {
538 return is_64bit() && vector_elements > 2;
539 }
540
541 /**
542 * Query whether or not a type is 64-bit
543 */
544 bool is_64bit() const
545 {
546 return glsl_base_type_is_64bit(base_type);
547 }
548
549 /**
550 * Query whether or not a type is a non-array boolean type
551 */
552 bool is_boolean() const
553 {
554 return base_type == GLSL_TYPE_BOOL;
555 }
556
557 /**
558 * Query whether or not a type is a sampler
559 */
560 bool is_sampler() const
561 {
562 return base_type == GLSL_TYPE_SAMPLER;
563 }
564
565 /**
566 * Query whether or not type is a sampler, or for struct, interface and
567 * array types, contains a sampler.
568 */
569 bool contains_sampler() const;
570
571 /**
572 * Query whether or not type is an array or for struct, interface and
573 * array types, contains an array.
574 */
575 bool contains_array() const;
576
577 /**
578 * Get the Mesa texture target index for a sampler type.
579 */
580 gl_texture_index sampler_index() const;
581
582 /**
583 * Query whether or not type is an image, or for struct, interface and
584 * array types, contains an image.
585 */
586 bool contains_image() const;
587
588 /**
589 * Query whether or not a type is an image
590 */
591 bool is_image() const
592 {
593 return base_type == GLSL_TYPE_IMAGE;
594 }
595
596 /**
597 * Query whether or not a type is an array
598 */
599 bool is_array() const
600 {
601 return base_type == GLSL_TYPE_ARRAY;
602 }
603
604 bool is_array_of_arrays() const
605 {
606 return is_array() && fields.array->is_array();
607 }
608
609 /**
610 * Query whether or not a type is a record
611 */
612 bool is_record() const
613 {
614 return base_type == GLSL_TYPE_STRUCT;
615 }
616
617 /**
618 * Query whether or not a type is an interface
619 */
620 bool is_interface() const
621 {
622 return base_type == GLSL_TYPE_INTERFACE;
623 }
624
625 /**
626 * Query whether or not a type is the void type singleton.
627 */
628 bool is_void() const
629 {
630 return base_type == GLSL_TYPE_VOID;
631 }
632
633 /**
634 * Query whether or not a type is the error type singleton.
635 */
636 bool is_error() const
637 {
638 return base_type == GLSL_TYPE_ERROR;
639 }
640
641 /**
642 * Query if a type is unnamed/anonymous (named by the parser)
643 */
644
645 bool is_subroutine() const
646 {
647 return base_type == GLSL_TYPE_SUBROUTINE;
648 }
649 bool contains_subroutine() const;
650
651 bool is_anonymous() const
652 {
653 return !strncmp(name, "#anon", 5);
654 }
655
656 /**
657 * Get the type stripped of any arrays
658 *
659 * \return
660 * Pointer to the type of elements of the first non-array type for array
661 * types, or pointer to itself for non-array types.
662 */
663 const glsl_type *without_array() const
664 {
665 const glsl_type *t = this;
666
667 while (t->is_array())
668 t = t->fields.array;
669
670 return t;
671 }
672
673 /**
674 * Return the total number of elements in an array including the elements
675 * in arrays of arrays.
676 */
677 unsigned arrays_of_arrays_size() const
678 {
679 if (!is_array())
680 return 0;
681
682 unsigned size = length;
683 const glsl_type *base_type = fields.array;
684
685 while (base_type->is_array()) {
686 size = size * base_type->length;
687 base_type = base_type->fields.array;
688 }
689 return size;
690 }
691
692 /**
693 * Query whether or not a type is an atomic_uint.
694 */
695 bool is_atomic_uint() const
696 {
697 return base_type == GLSL_TYPE_ATOMIC_UINT;
698 }
699
700 /**
701 * Return the amount of atomic counter storage required for a type.
702 */
703 unsigned atomic_size() const
704 {
705 if (is_atomic_uint())
706 return ATOMIC_COUNTER_SIZE;
707 else if (is_array())
708 return length * fields.array->atomic_size();
709 else
710 return 0;
711 }
712
713 /**
714 * Return whether a type contains any atomic counters.
715 */
716 bool contains_atomic() const
717 {
718 return atomic_size() > 0;
719 }
720
721 /**
722 * Return whether a type contains any opaque types.
723 */
724 bool contains_opaque() const;
725
726 /**
727 * Query the full type of a matrix row
728 *
729 * \return
730 * If the type is not a matrix, \c glsl_type::error_type is returned.
731 * Otherwise a type matching the rows of the matrix is returned.
732 */
733 const glsl_type *row_type() const
734 {
735 return is_matrix()
736 ? get_instance(base_type, matrix_columns, 1)
737 : error_type;
738 }
739
740 /**
741 * Query the full type of a matrix column
742 *
743 * \return
744 * If the type is not a matrix, \c glsl_type::error_type is returned.
745 * Otherwise a type matching the columns of the matrix is returned.
746 */
747 const glsl_type *column_type() const
748 {
749 return is_matrix()
750 ? get_instance(base_type, vector_elements, 1)
751 : error_type;
752 }
753
754 /**
755 * Get the type of a structure field
756 *
757 * \return
758 * Pointer to the type of the named field. If the type is not a structure
759 * or the named field does not exist, \c glsl_type::error_type is returned.
760 */
761 const glsl_type *field_type(const char *name) const;
762
763 /**
764 * Get the location of a field within a record type
765 */
766 int field_index(const char *name) const;
767
768 /**
769 * Query the number of elements in an array type
770 *
771 * \return
772 * The number of elements in the array for array types or -1 for non-array
773 * types. If the number of elements in the array has not yet been declared,
774 * zero is returned.
775 */
776 int array_size() const
777 {
778 return is_array() ? length : -1;
779 }
780
781 /**
782 * Query whether the array size for all dimensions has been declared.
783 */
784 bool is_unsized_array() const
785 {
786 return is_array() && length == 0;
787 }
788
789 /**
790 * Return the number of coordinate components needed for this
791 * sampler or image type.
792 *
793 * This is based purely on the sampler's dimensionality. For example, this
794 * returns 1 for sampler1D, and 3 for sampler2DArray.
795 *
796 * Note that this is often different than actual coordinate type used in
797 * a texturing built-in function, since those pack additional values (such
798 * as the shadow comparator or projector) into the coordinate type.
799 */
800 int coordinate_components() const;
801
802 /**
803 * Compare a record type against another record type.
804 *
805 * This is useful for matching record types declared across shader stages.
806 * The option to not match locations is to deal with places where the
807 * same struct is defined in a block which has a location set on it.
808 */
809 bool record_compare(const glsl_type *b, bool match_locations = true) const;
810
811 /**
812 * Get the type interface packing.
813 */
814 enum glsl_interface_packing get_interface_packing() const
815 {
816 return (enum glsl_interface_packing)interface_packing;
817 }
818
819 /**
820 * Check if the type interface is row major
821 */
822 bool get_interface_row_major() const
823 {
824 return (bool) interface_row_major;
825 }
826
827 private:
828
829 static mtx_t mem_mutex;
830 static mtx_t hash_mutex;
831
832 /**
833 * ralloc context for all glsl_type allocations
834 *
835 * Set on the first call to \c glsl_type::new.
836 */
837 static void *mem_ctx;
838
839 void init_ralloc_type_ctx(void);
840
841 /** Constructor for vector and matrix types */
842 glsl_type(GLenum gl_type,
843 glsl_base_type base_type, unsigned vector_elements,
844 unsigned matrix_columns, const char *name);
845
846 /** Constructor for sampler or image types */
847 glsl_type(GLenum gl_type, glsl_base_type base_type,
848 enum glsl_sampler_dim dim, bool shadow, bool array,
849 unsigned type, const char *name);
850
851 /** Constructor for record types */
852 glsl_type(const glsl_struct_field *fields, unsigned num_fields,
853 const char *name);
854
855 /** Constructor for interface types */
856 glsl_type(const glsl_struct_field *fields, unsigned num_fields,
857 enum glsl_interface_packing packing,
858 bool row_major, const char *name);
859
860 /** Constructor for interface types */
861 glsl_type(const glsl_type *return_type,
862 const glsl_function_param *params, unsigned num_params);
863
864 /** Constructor for array types */
865 glsl_type(const glsl_type *array, unsigned length);
866
867 /** Constructor for subroutine types */
868 glsl_type(const char *name);
869
870 /** Hash table containing the known array types. */
871 static struct hash_table *array_types;
872
873 /** Hash table containing the known record types. */
874 static struct hash_table *record_types;
875
876 /** Hash table containing the known interface types. */
877 static struct hash_table *interface_types;
878
879 /** Hash table containing the known subroutine types. */
880 static struct hash_table *subroutine_types;
881
882 /** Hash table containing the known function types. */
883 static struct hash_table *function_types;
884
885 static bool record_key_compare(const void *a, const void *b);
886 static unsigned record_key_hash(const void *key);
887
888 /**
889 * \name Built-in type flyweights
890 */
891 /*@{*/
892 #undef DECL_TYPE
893 #define DECL_TYPE(NAME, ...) static const glsl_type _##NAME##_type;
894 #undef STRUCT_TYPE
895 #define STRUCT_TYPE(NAME) static const glsl_type _struct_##NAME##_type;
896 #include "compiler/builtin_type_macros.h"
897 /*@}*/
898
899 /**
900 * \name Friend functions.
901 *
902 * These functions are friends because they must have C linkage and the
903 * need to call various private methods or access various private static
904 * data.
905 */
906 /*@{*/
907 friend void _mesa_glsl_initialize_types(struct _mesa_glsl_parse_state *);
908 friend void _mesa_glsl_release_types(void);
909 /*@}*/
910 };
911
912 #undef DECL_TYPE
913 #undef STRUCT_TYPE
914 #endif /* __cplusplus */
915
916 struct glsl_struct_field {
917 const struct glsl_type *type;
918 const char *name;
919
920 /**
921 * For interface blocks, gl_varying_slot corresponding to the input/output
922 * if this is a built-in input/output (i.e. a member of the built-in
923 * gl_PerVertex interface block); -1 otherwise.
924 *
925 * Ignored for structs.
926 */
927 int location;
928
929 /**
930 * For interface blocks, members may have an explicit byte offset
931 * specified; -1 otherwise. Also used for xfb_offset layout qualifier.
932 *
933 * Unless used for xfb_offset this field is ignored for structs.
934 */
935 int offset;
936
937 /**
938 * For interface blocks, members may define a transform feedback buffer;
939 * -1 otherwise.
940 */
941 int xfb_buffer;
942
943 /**
944 * For interface blocks, members may define a transform feedback stride;
945 * -1 otherwise.
946 */
947 int xfb_stride;
948
949 /**
950 * For interface blocks, the interpolation mode (as in
951 * ir_variable::interpolation). 0 otherwise.
952 */
953 unsigned interpolation:2;
954
955 /**
956 * For interface blocks, 1 if this variable uses centroid interpolation (as
957 * in ir_variable::centroid). 0 otherwise.
958 */
959 unsigned centroid:1;
960
961 /**
962 * For interface blocks, 1 if this variable uses sample interpolation (as
963 * in ir_variable::sample). 0 otherwise.
964 */
965 unsigned sample:1;
966
967 /**
968 * Layout of the matrix. Uses glsl_matrix_layout values.
969 */
970 unsigned matrix_layout:2;
971
972 /**
973 * For interface blocks, 1 if this variable is a per-patch input or output
974 * (as in ir_variable::patch). 0 otherwise.
975 */
976 unsigned patch:1;
977
978 /**
979 * Precision qualifier
980 */
981 unsigned precision:2;
982
983 /**
984 * Memory qualifiers, applicable to buffer variables defined in shader
985 * storage buffer objects (SSBOs)
986 */
987 unsigned memory_read_only:1;
988 unsigned memory_write_only:1;
989 unsigned memory_coherent:1;
990 unsigned memory_volatile:1;
991 unsigned memory_restrict:1;
992
993 /**
994 * Layout format, applicable to image variables only.
995 */
996 unsigned image_format:16;
997
998 /**
999 * Any of the xfb_* qualifiers trigger the shader to be in transform
1000 * feedback mode so we need to keep track of whether the buffer was
1001 * explicitly set or if its just been assigned the default global value.
1002 */
1003 unsigned explicit_xfb_buffer:1;
1004
1005 unsigned implicit_sized_array:1;
1006 #ifdef __cplusplus
1007 glsl_struct_field(const struct glsl_type *_type, const char *_name)
1008 : type(_type), name(_name), location(-1), offset(0), xfb_buffer(0),
1009 xfb_stride(0), interpolation(0), centroid(0),
1010 sample(0), matrix_layout(GLSL_MATRIX_LAYOUT_INHERITED), patch(0),
1011 precision(GLSL_PRECISION_NONE), memory_read_only(0),
1012 memory_write_only(0), memory_coherent(0), memory_volatile(0),
1013 memory_restrict(0), image_format(0), explicit_xfb_buffer(0),
1014 implicit_sized_array(0)
1015 {
1016 /* empty */
1017 }
1018
1019 glsl_struct_field()
1020 {
1021 /* empty */
1022 }
1023 #endif
1024 };
1025
1026 struct glsl_function_param {
1027 const struct glsl_type *type;
1028
1029 bool in;
1030 bool out;
1031 };
1032
1033 static inline unsigned int
1034 glsl_align(unsigned int a, unsigned int align)
1035 {
1036 return (a + align - 1) / align * align;
1037 }
1038
1039 #endif /* GLSL_TYPES_H */