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