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