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