7359e94764c14fbd1e3cf4642b63f00671c73bab
[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 * Query the type of elements in an array
232 *
233 * \return
234 * Pointer to the type of elements in the array for array types, or \c NULL
235 * for non-array types.
236 */
237 const glsl_type *element_type() const
238 {
239 return is_array() ? fields.array : NULL;
240 }
241
242 /**
243 * Get the instance of a built-in scalar, vector, or matrix type
244 */
245 static const glsl_type *get_instance(unsigned base_type, unsigned rows,
246 unsigned columns);
247
248 /**
249 * Get the instance of a sampler type
250 */
251 static const glsl_type *get_sampler_instance(enum glsl_sampler_dim dim,
252 bool shadow,
253 bool array,
254 glsl_base_type type);
255
256
257 /**
258 * Get the instance of an array type
259 */
260 static const glsl_type *get_array_instance(const glsl_type *base,
261 unsigned elements);
262
263 /**
264 * Get the instance of a record type
265 */
266 static const glsl_type *get_record_instance(const glsl_struct_field *fields,
267 unsigned num_fields,
268 const char *name);
269
270 /**
271 * Get the instance of an interface block type
272 */
273 static const glsl_type *get_interface_instance(const glsl_struct_field *fields,
274 unsigned num_fields,
275 enum glsl_interface_packing packing,
276 const char *block_name);
277
278 /**
279 * Query the total number of scalars that make up a scalar, vector or matrix
280 */
281 unsigned components() const
282 {
283 return vector_elements * matrix_columns;
284 }
285
286 /**
287 * Calculate the number of components slots required to hold this type
288 *
289 * This is used to determine how many uniform or varying locations a type
290 * might occupy.
291 */
292 unsigned component_slots() const;
293
294 /**
295 * Calculate the number of unique values from glGetUniformLocation for the
296 * elements of the type.
297 *
298 * This is used to allocate slots in the UniformRemapTable, the amount of
299 * locations may not match with actual used storage space by the driver.
300 */
301 unsigned uniform_locations() const;
302
303 /**
304 * Calculate the number of attribute slots required to hold this type
305 *
306 * This implements the language rules of GLSL 1.50 for counting the number
307 * of slots used by a vertex attribute. It also determines the number of
308 * varying slots the type will use up in the absence of varying packing
309 * (and thus, it can be used to measure the number of varying slots used by
310 * the varyings that are generated by lower_packed_varyings).
311 */
312 unsigned count_attribute_slots() const;
313
314
315 /**
316 * Alignment in bytes of the start of this type in a std140 uniform
317 * block.
318 */
319 unsigned std140_base_alignment(bool row_major) const;
320
321 /** Size in bytes of this type in a std140 uniform block.
322 *
323 * Note that this is not GL_UNIFORM_SIZE (which is the number of
324 * elements in the array)
325 */
326 unsigned std140_size(bool row_major) const;
327
328 /**
329 * \brief Can this type be implicitly converted to another?
330 *
331 * \return True if the types are identical or if this type can be converted
332 * to \c desired according to Section 4.1.10 of the GLSL spec.
333 *
334 * \verbatim
335 * From page 25 (31 of the pdf) of the GLSL 1.50 spec, Section 4.1.10
336 * Implicit Conversions:
337 *
338 * In some situations, an expression and its type will be implicitly
339 * converted to a different type. The following table shows all allowed
340 * implicit conversions:
341 *
342 * Type of expression | Can be implicitly converted to
343 * --------------------------------------------------
344 * int float
345 * uint
346 *
347 * ivec2 vec2
348 * uvec2
349 *
350 * ivec3 vec3
351 * uvec3
352 *
353 * ivec4 vec4
354 * uvec4
355 *
356 * There are no implicit array or structure conversions. For example,
357 * an array of int cannot be implicitly converted to an array of float.
358 * There are no implicit conversions between signed and unsigned
359 * integers.
360 * \endverbatim
361 */
362 bool can_implicitly_convert_to(const glsl_type *desired,
363 _mesa_glsl_parse_state *state) const;
364
365 /**
366 * Query whether or not a type is a scalar (non-vector and non-matrix).
367 */
368 bool is_scalar() const
369 {
370 return (vector_elements == 1)
371 && (base_type >= GLSL_TYPE_UINT)
372 && (base_type <= GLSL_TYPE_BOOL);
373 }
374
375 /**
376 * Query whether or not a type is a vector
377 */
378 bool is_vector() const
379 {
380 return (vector_elements > 1)
381 && (matrix_columns == 1)
382 && (base_type >= GLSL_TYPE_UINT)
383 && (base_type <= GLSL_TYPE_BOOL);
384 }
385
386 /**
387 * Query whether or not a type is a matrix
388 */
389 bool is_matrix() const
390 {
391 /* GLSL only has float matrices. */
392 return (matrix_columns > 1) && (base_type == GLSL_TYPE_FLOAT || base_type == GLSL_TYPE_DOUBLE);
393 }
394
395 /**
396 * Query whether or not a type is a non-array numeric type
397 */
398 bool is_numeric() const
399 {
400 return (base_type >= GLSL_TYPE_UINT) && (base_type <= GLSL_TYPE_DOUBLE);
401 }
402
403 /**
404 * Query whether or not a type is an integral type
405 */
406 bool is_integer() const
407 {
408 return (base_type == GLSL_TYPE_UINT) || (base_type == GLSL_TYPE_INT);
409 }
410
411 /**
412 * Query whether or not type is an integral type, or for struct and array
413 * types, contains an integral type.
414 */
415 bool contains_integer() const;
416
417 /**
418 * Query whether or not type is a double type, or for struct and array
419 * types, contains a double type.
420 */
421 bool contains_double() const;
422
423 /**
424 * Query whether or not a type is a float type
425 */
426 bool is_float() const
427 {
428 return base_type == GLSL_TYPE_FLOAT;
429 }
430
431 /**
432 * Query whether or not a type is a double type
433 */
434 bool is_double() const
435 {
436 return base_type == GLSL_TYPE_DOUBLE;
437 }
438
439 /**
440 * Query whether or not a type is a non-array boolean type
441 */
442 bool is_boolean() const
443 {
444 return base_type == GLSL_TYPE_BOOL;
445 }
446
447 /**
448 * Query whether or not a type is a sampler
449 */
450 bool is_sampler() const
451 {
452 return base_type == GLSL_TYPE_SAMPLER;
453 }
454
455 /**
456 * Query whether or not type is a sampler, or for struct and array
457 * types, contains a sampler.
458 */
459 bool contains_sampler() const;
460
461 /**
462 * Get the Mesa texture target index for a sampler type.
463 */
464 gl_texture_index sampler_index() const;
465
466 /**
467 * Query whether or not type is an image, or for struct and array
468 * types, contains an image.
469 */
470 bool contains_image() const;
471
472 /**
473 * Query whether or not a type is an image
474 */
475 bool is_image() const
476 {
477 return base_type == GLSL_TYPE_IMAGE;
478 }
479
480 /**
481 * Query whether or not a type is an array
482 */
483 bool is_array() const
484 {
485 return base_type == GLSL_TYPE_ARRAY;
486 }
487
488 /**
489 * Query whether or not a type is a record
490 */
491 bool is_record() const
492 {
493 return base_type == GLSL_TYPE_STRUCT;
494 }
495
496 /**
497 * Query whether or not a type is an interface
498 */
499 bool is_interface() const
500 {
501 return base_type == GLSL_TYPE_INTERFACE;
502 }
503
504 /**
505 * Query whether or not a type is the void type singleton.
506 */
507 bool is_void() const
508 {
509 return base_type == GLSL_TYPE_VOID;
510 }
511
512 /**
513 * Query whether or not a type is the error type singleton.
514 */
515 bool is_error() const
516 {
517 return base_type == GLSL_TYPE_ERROR;
518 }
519
520 /**
521 * Query if a type is unnamed/anonymous (named by the parser)
522 */
523 bool is_anonymous() const
524 {
525 return !strncmp(name, "#anon", 5);
526 }
527
528 /**
529 * Get the type stripped of any arrays
530 *
531 * \return
532 * Pointer to the type of elements of the first non-array type for array
533 * types, or pointer to itself for non-array types.
534 */
535 const glsl_type *without_array() const
536 {
537 return this->is_array() ? this->fields.array : this;
538 }
539
540 /**
541 * Return the amount of atomic counter storage required for a type.
542 */
543 unsigned atomic_size() const
544 {
545 if (base_type == GLSL_TYPE_ATOMIC_UINT)
546 return ATOMIC_COUNTER_SIZE;
547 else if (is_array())
548 return length * element_type()->atomic_size();
549 else
550 return 0;
551 }
552
553 /**
554 * Return whether a type contains any atomic counters.
555 */
556 bool contains_atomic() const
557 {
558 return atomic_size() > 0;
559 }
560
561 /**
562 * Return whether a type contains any opaque types.
563 */
564 bool contains_opaque() const;
565
566 /**
567 * Query the full type of a matrix row
568 *
569 * \return
570 * If the type is not a matrix, \c glsl_type::error_type is returned.
571 * Otherwise a type matching the rows of the matrix is returned.
572 */
573 const glsl_type *row_type() const
574 {
575 return is_matrix()
576 ? get_instance(base_type, matrix_columns, 1)
577 : error_type;
578 }
579
580 /**
581 * Query the full type of a matrix column
582 *
583 * \return
584 * If the type is not a matrix, \c glsl_type::error_type is returned.
585 * Otherwise a type matching the columns of the matrix is returned.
586 */
587 const glsl_type *column_type() const
588 {
589 return is_matrix()
590 ? get_instance(base_type, vector_elements, 1)
591 : error_type;
592 }
593
594 /**
595 * Get the type of a structure field
596 *
597 * \return
598 * Pointer to the type of the named field. If the type is not a structure
599 * or the named field does not exist, \c glsl_type::error_type is returned.
600 */
601 const glsl_type *field_type(const char *name) const;
602
603 /**
604 * Get the location of a filed within a record type
605 */
606 int field_index(const char *name) const;
607
608 /**
609 * Query the number of elements in an array type
610 *
611 * \return
612 * The number of elements in the array for array types or -1 for non-array
613 * types. If the number of elements in the array has not yet been declared,
614 * zero is returned.
615 */
616 int array_size() const
617 {
618 return is_array() ? length : -1;
619 }
620
621 /**
622 * Query whether the array size for all dimensions has been declared.
623 */
624 bool is_unsized_array() const
625 {
626 return is_array() && length == 0;
627 }
628
629 /**
630 * Return the number of coordinate components needed for this
631 * sampler or image type.
632 *
633 * This is based purely on the sampler's dimensionality. For example, this
634 * returns 1 for sampler1D, and 3 for sampler2DArray.
635 *
636 * Note that this is often different than actual coordinate type used in
637 * a texturing built-in function, since those pack additional values (such
638 * as the shadow comparitor or projector) into the coordinate type.
639 */
640 int coordinate_components() const;
641
642 /**
643 * Compare a record type against another record type.
644 *
645 * This is useful for matching record types declared across shader stages.
646 */
647 bool record_compare(const glsl_type *b) const;
648
649 private:
650
651 static mtx_t mutex;
652
653 /**
654 * ralloc context for all glsl_type allocations
655 *
656 * Set on the first call to \c glsl_type::new.
657 */
658 static void *mem_ctx;
659
660 void init_ralloc_type_ctx(void);
661
662 /** Constructor for vector and matrix types */
663 glsl_type(GLenum gl_type,
664 glsl_base_type base_type, unsigned vector_elements,
665 unsigned matrix_columns, const char *name);
666
667 /** Constructor for sampler or image types */
668 glsl_type(GLenum gl_type, glsl_base_type base_type,
669 enum glsl_sampler_dim dim, bool shadow, bool array,
670 unsigned type, const char *name);
671
672 /** Constructor for record types */
673 glsl_type(const glsl_struct_field *fields, unsigned num_fields,
674 const char *name);
675
676 /** Constructor for interface types */
677 glsl_type(const glsl_struct_field *fields, unsigned num_fields,
678 enum glsl_interface_packing packing, const char *name);
679
680 /** Constructor for array types */
681 glsl_type(const glsl_type *array, unsigned length);
682
683 /** Hash table containing the known array types. */
684 static struct hash_table *array_types;
685
686 /** Hash table containing the known record types. */
687 static struct hash_table *record_types;
688
689 /** Hash table containing the known interface types. */
690 static struct hash_table *interface_types;
691
692 static int record_key_compare(const void *a, const void *b);
693 static unsigned record_key_hash(const void *key);
694
695 /**
696 * \name Built-in type flyweights
697 */
698 /*@{*/
699 #undef DECL_TYPE
700 #define DECL_TYPE(NAME, ...) static const glsl_type _##NAME##_type;
701 #undef STRUCT_TYPE
702 #define STRUCT_TYPE(NAME) static const glsl_type _struct_##NAME##_type;
703 #include "builtin_type_macros.h"
704 /*@}*/
705
706 /**
707 * \name Friend functions.
708 *
709 * These functions are friends because they must have C linkage and the
710 * need to call various private methods or access various private static
711 * data.
712 */
713 /*@{*/
714 friend void _mesa_glsl_initialize_types(struct _mesa_glsl_parse_state *);
715 friend void _mesa_glsl_release_types(void);
716 /*@}*/
717 };
718
719 struct glsl_struct_field {
720 const struct glsl_type *type;
721 const char *name;
722
723 /**
724 * For interface blocks, gl_varying_slot corresponding to the input/output
725 * if this is a built-in input/output (i.e. a member of the built-in
726 * gl_PerVertex interface block); -1 otherwise.
727 *
728 * Ignored for structs.
729 */
730 int location;
731
732 /**
733 * For interface blocks, the interpolation mode (as in
734 * ir_variable::interpolation). 0 otherwise.
735 */
736 unsigned interpolation:2;
737
738 /**
739 * For interface blocks, 1 if this variable uses centroid interpolation (as
740 * in ir_variable::centroid). 0 otherwise.
741 */
742 unsigned centroid:1;
743
744 /**
745 * For interface blocks, 1 if this variable uses sample interpolation (as
746 * in ir_variable::sample). 0 otherwise.
747 */
748 unsigned sample:1;
749
750 /**
751 * Layout of the matrix. Uses glsl_matrix_layout values.
752 */
753 unsigned matrix_layout:2;
754
755 /**
756 * For interface blocks, it has a value if this variable uses multiple vertex
757 * streams (as in ir_variable::stream). -1 otherwise.
758 */
759 int stream;
760 };
761
762 static inline unsigned int
763 glsl_align(unsigned int a, unsigned int align)
764 {
765 return (a + align - 1) / align * align;
766 }
767
768 #undef DECL_TYPE
769 #undef STRUCT_TYPE
770 #endif /* __cplusplus */
771
772 #endif /* GLSL_TYPES_H */