096f5462af4d96e0524601dd52a79f564048ff85
[mesa.git] / src / glsl / glsl_types.h
1 /* -*- c++ -*- */
2 /*
3 * Copyright © 2009 Intel Corporation
4 *
5 * Permission is hereby granted, free of charge, to any person obtaining a
6 * copy of this software and associated documentation files (the "Software"),
7 * to deal in the Software without restriction, including without limitation
8 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
9 * and/or sell copies of the Software, and to permit persons to whom the
10 * Software is furnished to do so, subject to the following conditions:
11 *
12 * The above copyright notice and this permission notice (including the next
13 * paragraph) shall be included in all copies or substantial portions of the
14 * Software.
15 *
16 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
19 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
21 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
22 * DEALINGS IN THE SOFTWARE.
23 */
24
25 #pragma once
26 #ifndef GLSL_TYPES_H
27 #define GLSL_TYPES_H
28
29 #include <string.h>
30 #include <assert.h>
31 #include "main/mtypes.h" /* for gl_texture_index, C++'s enum rules are broken */
32
33 #ifdef __cplusplus
34 extern "C" {
35 #endif
36
37 struct _mesa_glsl_parse_state;
38 struct glsl_symbol_table;
39
40 extern void
41 _mesa_glsl_initialize_types(struct _mesa_glsl_parse_state *state);
42
43 extern void
44 _mesa_glsl_release_types(void);
45
46 #ifdef __cplusplus
47 }
48 #endif
49
50 enum glsl_base_type {
51 GLSL_TYPE_UINT = 0,
52 GLSL_TYPE_INT,
53 GLSL_TYPE_FLOAT,
54 GLSL_TYPE_BOOL,
55 GLSL_TYPE_SAMPLER,
56 GLSL_TYPE_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
107 struct glsl_type {
108 GLenum gl_type;
109 glsl_base_type base_type;
110
111 unsigned sampler_dimensionality:3; /**< \see glsl_sampler_dim */
112 unsigned sampler_shadow:1;
113 unsigned sampler_array:1;
114 unsigned sampler_type:2; /**< Type of data returned using this
115 * sampler or image. Only \c
116 * GLSL_TYPE_FLOAT, \c GLSL_TYPE_INT,
117 * and \c GLSL_TYPE_UINT are valid.
118 */
119 unsigned interface_packing:2;
120
121 /* Callers of this ralloc-based new need not call delete. It's
122 * easier to just ralloc_free 'mem_ctx' (or any of its ancestors). */
123 static void* operator new(size_t size)
124 {
125 mtx_lock(&glsl_type::mutex);
126
127 /* mem_ctx should have been created by the static members */
128 assert(glsl_type::mem_ctx != NULL);
129
130 void *type;
131
132 type = ralloc_size(glsl_type::mem_ctx, size);
133 assert(type != NULL);
134
135 mtx_unlock(&glsl_type::mutex);
136
137 return type;
138 }
139
140 /* If the user *does* call delete, that's OK, we will just
141 * ralloc_free in that case. */
142 static void operator delete(void *type)
143 {
144 mtx_lock(&glsl_type::mutex);
145 ralloc_free(type);
146 mtx_unlock(&glsl_type::mutex);
147 }
148
149 /**
150 * \name Vector and matrix element counts
151 *
152 * For scalars, each of these values will be 1. For non-numeric types
153 * these will be 0.
154 */
155 /*@{*/
156 uint8_t vector_elements; /**< 1, 2, 3, or 4 vector elements. */
157 uint8_t matrix_columns; /**< 1, 2, 3, or 4 matrix columns. */
158 /*@}*/
159
160 /**
161 * Name of the data type
162 *
163 * Will never be \c NULL.
164 */
165 const char *name;
166
167 /**
168 * For \c GLSL_TYPE_ARRAY, this is the length of the array. For
169 * \c GLSL_TYPE_STRUCT or \c GLSL_TYPE_INTERFACE, it is the number of
170 * elements in the structure and the number of values pointed to by
171 * \c fields.structure (below).
172 */
173 unsigned length;
174
175 /**
176 * Subtype of composite data types.
177 */
178 union {
179 const struct glsl_type *array; /**< Type of array elements. */
180 const struct glsl_type *parameters; /**< Parameters to function. */
181 struct glsl_struct_field *structure; /**< List of struct fields. */
182 } fields;
183
184 /**
185 * \name Pointers to various public type singletons
186 */
187 /*@{*/
188 #undef DECL_TYPE
189 #define DECL_TYPE(NAME, ...) \
190 static const glsl_type *const NAME##_type;
191 #undef STRUCT_TYPE
192 #define STRUCT_TYPE(NAME) \
193 static const glsl_type *const struct_##NAME##_type;
194 #include "builtin_type_macros.h"
195 /*@}*/
196
197 /**
198 * Convenience accessors for vector types (shorter than get_instance()).
199 * @{
200 */
201 static const glsl_type *vec(unsigned components);
202 static const glsl_type *ivec(unsigned components);
203 static const glsl_type *uvec(unsigned components);
204 static const glsl_type *bvec(unsigned components);
205 /**@}*/
206
207 /**
208 * For numeric and boolean derived types returns the basic scalar type
209 *
210 * If the type is a numeric or boolean scalar, vector, or matrix type,
211 * this function gets the scalar type of the individual components. For
212 * all other types, including arrays of numeric or boolean types, the
213 * error type is returned.
214 */
215 const glsl_type *get_base_type() const;
216
217 /**
218 * Get the basic scalar type which this type aggregates.
219 *
220 * If the type is a numeric or boolean scalar, vector, or matrix, or an
221 * array of any of those, this function gets the scalar type of the
222 * individual components. For structs and arrays of structs, this function
223 * returns the struct type. For samplers and arrays of samplers, this
224 * function returns the sampler type.
225 */
226 const glsl_type *get_scalar_type() const;
227
228 /**
229 * Query the type of elements in an array
230 *
231 * \return
232 * Pointer to the type of elements in the array for array types, or \c NULL
233 * for non-array types.
234 */
235 const glsl_type *element_type() const
236 {
237 return is_array() ? fields.array : NULL;
238 }
239
240 /**
241 * Get the instance of a built-in scalar, vector, or matrix type
242 */
243 static const glsl_type *get_instance(unsigned base_type, unsigned rows,
244 unsigned columns);
245
246 /**
247 * Get the instance of an array type
248 */
249 static const glsl_type *get_array_instance(const glsl_type *base,
250 unsigned elements);
251
252 /**
253 * Get the instance of a record type
254 */
255 static const glsl_type *get_record_instance(const glsl_struct_field *fields,
256 unsigned num_fields,
257 const char *name);
258
259 /**
260 * Get the instance of an interface block type
261 */
262 static const glsl_type *get_interface_instance(const glsl_struct_field *fields,
263 unsigned num_fields,
264 enum glsl_interface_packing packing,
265 const char *block_name);
266
267 /**
268 * Query the total number of scalars that make up a scalar, vector or matrix
269 */
270 unsigned components() const
271 {
272 return vector_elements * matrix_columns;
273 }
274
275 /**
276 * Calculate the number of components slots required to hold this type
277 *
278 * This is used to determine how many uniform or varying locations a type
279 * might occupy.
280 */
281 unsigned component_slots() const;
282
283 /**
284 * Calculate the number of unique values from glGetUniformLocation for the
285 * elements of the type.
286 *
287 * This is used to allocate slots in the UniformRemapTable, the amount of
288 * locations may not match with actual used storage space by the driver.
289 */
290 unsigned uniform_locations() const;
291
292 /**
293 * Calculate the number of attribute slots required to hold this type
294 *
295 * This implements the language rules of GLSL 1.50 for counting the number
296 * of slots used by a vertex attribute. It also determines the number of
297 * varying slots the type will use up in the absence of varying packing
298 * (and thus, it can be used to measure the number of varying slots used by
299 * the varyings that are generated by lower_packed_varyings).
300 */
301 unsigned count_attribute_slots() const;
302
303
304 /**
305 * Alignment in bytes of the start of this type in a std140 uniform
306 * block.
307 */
308 unsigned std140_base_alignment(bool row_major) const;
309
310 /** Size in bytes of this type in a std140 uniform block.
311 *
312 * Note that this is not GL_UNIFORM_SIZE (which is the number of
313 * elements in the array)
314 */
315 unsigned std140_size(bool row_major) const;
316
317 /**
318 * \brief Can this type be implicitly converted to another?
319 *
320 * \return True if the types are identical or if this type can be converted
321 * to \c desired according to Section 4.1.10 of the GLSL spec.
322 *
323 * \verbatim
324 * From page 25 (31 of the pdf) of the GLSL 1.50 spec, Section 4.1.10
325 * Implicit Conversions:
326 *
327 * In some situations, an expression and its type will be implicitly
328 * converted to a different type. The following table shows all allowed
329 * implicit conversions:
330 *
331 * Type of expression | Can be implicitly converted to
332 * --------------------------------------------------
333 * int float
334 * uint
335 *
336 * ivec2 vec2
337 * uvec2
338 *
339 * ivec3 vec3
340 * uvec3
341 *
342 * ivec4 vec4
343 * uvec4
344 *
345 * There are no implicit array or structure conversions. For example,
346 * an array of int cannot be implicitly converted to an array of float.
347 * There are no implicit conversions between signed and unsigned
348 * integers.
349 * \endverbatim
350 */
351 bool can_implicitly_convert_to(const glsl_type *desired,
352 _mesa_glsl_parse_state *state) const;
353
354 /**
355 * Query whether or not a type is a scalar (non-vector and non-matrix).
356 */
357 bool is_scalar() const
358 {
359 return (vector_elements == 1)
360 && (base_type >= GLSL_TYPE_UINT)
361 && (base_type <= GLSL_TYPE_BOOL);
362 }
363
364 /**
365 * Query whether or not a type is a vector
366 */
367 bool is_vector() const
368 {
369 return (vector_elements > 1)
370 && (matrix_columns == 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 matrix
377 */
378 bool is_matrix() const
379 {
380 /* GLSL only has float matrices. */
381 return (matrix_columns > 1) && (base_type == GLSL_TYPE_FLOAT);
382 }
383
384 /**
385 * Query whether or not a type is a non-array numeric type
386 */
387 bool is_numeric() const
388 {
389 return (base_type >= GLSL_TYPE_UINT) && (base_type <= GLSL_TYPE_FLOAT);
390 }
391
392 /**
393 * Query whether or not a type is an integral type
394 */
395 bool is_integer() const
396 {
397 return (base_type == GLSL_TYPE_UINT) || (base_type == GLSL_TYPE_INT);
398 }
399
400 /**
401 * Query whether or not type is an integral type, or for struct and array
402 * types, contains an integral type.
403 */
404 bool contains_integer() const;
405
406 /**
407 * Query whether or not a type is a float type
408 */
409 bool is_float() const
410 {
411 return base_type == GLSL_TYPE_FLOAT;
412 }
413
414 /**
415 * Query whether or not a type is a non-array boolean type
416 */
417 bool is_boolean() const
418 {
419 return base_type == GLSL_TYPE_BOOL;
420 }
421
422 /**
423 * Query whether or not a type is a sampler
424 */
425 bool is_sampler() const
426 {
427 return base_type == GLSL_TYPE_SAMPLER;
428 }
429
430 /**
431 * Query whether or not type is a sampler, or for struct and array
432 * types, contains a sampler.
433 */
434 bool contains_sampler() const;
435
436 /**
437 * Get the Mesa texture target index for a sampler type.
438 */
439 gl_texture_index sampler_index() const;
440
441 /**
442 * Query whether or not type is an image, or for struct and array
443 * types, contains an image.
444 */
445 bool contains_image() const;
446
447 /**
448 * Query whether or not a type is an image
449 */
450 bool is_image() const
451 {
452 return base_type == GLSL_TYPE_IMAGE;
453 }
454
455 /**
456 * Query whether or not a type is an array
457 */
458 bool is_array() const
459 {
460 return base_type == GLSL_TYPE_ARRAY;
461 }
462
463 /**
464 * Query whether or not a type is a record
465 */
466 bool is_record() const
467 {
468 return base_type == GLSL_TYPE_STRUCT;
469 }
470
471 /**
472 * Query whether or not a type is an interface
473 */
474 bool is_interface() const
475 {
476 return base_type == GLSL_TYPE_INTERFACE;
477 }
478
479 /**
480 * Query whether or not a type is the void type singleton.
481 */
482 bool is_void() const
483 {
484 return base_type == GLSL_TYPE_VOID;
485 }
486
487 /**
488 * Query whether or not a type is the error type singleton.
489 */
490 bool is_error() const
491 {
492 return base_type == GLSL_TYPE_ERROR;
493 }
494
495 /**
496 * Query if a type is unnamed/anonymous (named by the parser)
497 */
498 bool is_anonymous() const
499 {
500 return !strncmp(name, "#anon", 5);
501 }
502
503 /**
504 * Get the type stripped of any arrays
505 *
506 * \return
507 * Pointer to the type of elements of the first non-array type for array
508 * types, or pointer to itself for non-array types.
509 */
510 const glsl_type *without_array() const
511 {
512 return this->is_array() ? this->fields.array : this;
513 }
514
515 /**
516 * Return the amount of atomic counter storage required for a type.
517 */
518 unsigned atomic_size() const
519 {
520 if (base_type == GLSL_TYPE_ATOMIC_UINT)
521 return ATOMIC_COUNTER_SIZE;
522 else if (is_array())
523 return length * element_type()->atomic_size();
524 else
525 return 0;
526 }
527
528 /**
529 * Return whether a type contains any atomic counters.
530 */
531 bool contains_atomic() const
532 {
533 return atomic_size() > 0;
534 }
535
536 /**
537 * Return whether a type contains any opaque types.
538 */
539 bool contains_opaque() const;
540
541 /**
542 * Query the full type of a matrix row
543 *
544 * \return
545 * If the type is not a matrix, \c glsl_type::error_type is returned.
546 * Otherwise a type matching the rows of the matrix is returned.
547 */
548 const glsl_type *row_type() const
549 {
550 return is_matrix()
551 ? get_instance(base_type, matrix_columns, 1)
552 : error_type;
553 }
554
555 /**
556 * Query the full type of a matrix column
557 *
558 * \return
559 * If the type is not a matrix, \c glsl_type::error_type is returned.
560 * Otherwise a type matching the columns of the matrix is returned.
561 */
562 const glsl_type *column_type() const
563 {
564 return is_matrix()
565 ? get_instance(base_type, vector_elements, 1)
566 : error_type;
567 }
568
569 /**
570 * Get the type of a structure field
571 *
572 * \return
573 * Pointer to the type of the named field. If the type is not a structure
574 * or the named field does not exist, \c glsl_type::error_type is returned.
575 */
576 const glsl_type *field_type(const char *name) const;
577
578 /**
579 * Get the location of a filed within a record type
580 */
581 int field_index(const char *name) const;
582
583 /**
584 * Query the number of elements in an array type
585 *
586 * \return
587 * The number of elements in the array for array types or -1 for non-array
588 * types. If the number of elements in the array has not yet been declared,
589 * zero is returned.
590 */
591 int array_size() const
592 {
593 return is_array() ? length : -1;
594 }
595
596 /**
597 * Query whether the array size for all dimensions has been declared.
598 */
599 bool is_unsized_array() const
600 {
601 return is_array() && length == 0;
602 }
603
604 /**
605 * Return the number of coordinate components needed for this
606 * sampler or image type.
607 *
608 * This is based purely on the sampler's dimensionality. For example, this
609 * returns 1 for sampler1D, and 3 for sampler2DArray.
610 *
611 * Note that this is often different than actual coordinate type used in
612 * a texturing built-in function, since those pack additional values (such
613 * as the shadow comparitor or projector) into the coordinate type.
614 */
615 int coordinate_components() const;
616
617 /**
618 * Compare a record type against another record type.
619 *
620 * This is useful for matching record types declared across shader stages.
621 */
622 bool record_compare(const glsl_type *b) const;
623
624 private:
625
626 static mtx_t mutex;
627
628 /**
629 * ralloc context for all glsl_type allocations
630 *
631 * Set on the first call to \c glsl_type::new.
632 */
633 static void *mem_ctx;
634
635 void init_ralloc_type_ctx(void);
636
637 /** Constructor for vector and matrix types */
638 glsl_type(GLenum gl_type,
639 glsl_base_type base_type, unsigned vector_elements,
640 unsigned matrix_columns, const char *name);
641
642 /** Constructor for sampler or image types */
643 glsl_type(GLenum gl_type, glsl_base_type base_type,
644 enum glsl_sampler_dim dim, bool shadow, bool array,
645 unsigned type, const char *name);
646
647 /** Constructor for record types */
648 glsl_type(const glsl_struct_field *fields, unsigned num_fields,
649 const char *name);
650
651 /** Constructor for interface types */
652 glsl_type(const glsl_struct_field *fields, unsigned num_fields,
653 enum glsl_interface_packing packing, const char *name);
654
655 /** Constructor for array types */
656 glsl_type(const glsl_type *array, unsigned length);
657
658 /** Hash table containing the known array types. */
659 static struct hash_table *array_types;
660
661 /** Hash table containing the known record types. */
662 static struct hash_table *record_types;
663
664 /** Hash table containing the known interface types. */
665 static struct hash_table *interface_types;
666
667 static int record_key_compare(const void *a, const void *b);
668 static unsigned record_key_hash(const void *key);
669
670 /**
671 * \name Built-in type flyweights
672 */
673 /*@{*/
674 #undef DECL_TYPE
675 #define DECL_TYPE(NAME, ...) static const glsl_type _##NAME##_type;
676 #undef STRUCT_TYPE
677 #define STRUCT_TYPE(NAME) static const glsl_type _struct_##NAME##_type;
678 #include "builtin_type_macros.h"
679 /*@}*/
680
681 /**
682 * \name Friend functions.
683 *
684 * These functions are friends because they must have C linkage and the
685 * need to call various private methods or access various private static
686 * data.
687 */
688 /*@{*/
689 friend void _mesa_glsl_initialize_types(struct _mesa_glsl_parse_state *);
690 friend void _mesa_glsl_release_types(void);
691 /*@}*/
692 };
693
694 struct glsl_struct_field {
695 const struct glsl_type *type;
696 const char *name;
697
698 /**
699 * For interface blocks, gl_varying_slot corresponding to the input/output
700 * if this is a built-in input/output (i.e. a member of the built-in
701 * gl_PerVertex interface block); -1 otherwise.
702 *
703 * Ignored for structs.
704 */
705 int location;
706
707 /**
708 * For interface blocks, the interpolation mode (as in
709 * ir_variable::interpolation). 0 otherwise.
710 */
711 unsigned interpolation:2;
712
713 /**
714 * For interface blocks, 1 if this variable uses centroid interpolation (as
715 * in ir_variable::centroid). 0 otherwise.
716 */
717 unsigned centroid:1;
718
719 /**
720 * For interface blocks, 1 if this variable uses sample interpolation (as
721 * in ir_variable::sample). 0 otherwise.
722 */
723 unsigned sample:1;
724
725 /**
726 * Layout of the matrix. Uses glsl_matrix_layout values.
727 */
728 unsigned matrix_layout:2;
729
730 /**
731 * For interface blocks, it has a value if this variable uses multiple vertex
732 * streams (as in ir_variable::stream). -1 otherwise.
733 */
734 int stream;
735 };
736
737 static inline unsigned int
738 glsl_align(unsigned int a, unsigned int align)
739 {
740 return (a + align - 1) / align * align;
741 }
742
743 #undef DECL_TYPE
744 #undef STRUCT_TYPE
745 #endif /* __cplusplus */
746
747 #endif /* GLSL_TYPES_H */