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