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