Initial pass at resolving function calls
[mesa.git] / 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 <cstring>
30
31 #define GLSL_TYPE_UINT 0
32 #define GLSL_TYPE_INT 1
33 #define GLSL_TYPE_FLOAT 2
34 #define GLSL_TYPE_BOOL 3
35 #define GLSL_TYPE_SAMPLER 4
36 #define GLSL_TYPE_STRUCT 5
37 #define GLSL_TYPE_ARRAY 6
38 #define GLSL_TYPE_FUNCTION 7
39 #define GLSL_TYPE_VOID 8
40 #define GLSL_TYPE_ERROR 9
41
42 #define is_numeric_base_type(b) \
43 (((b) >= GLSL_TYPE_UINT) && ((b) <= GLSL_TYPE_FLOAT))
44
45 #define is_integer_base_type(b) \
46 (((b) == GLSL_TYPE_UINT) || ((b) == GLSL_TYPE_INT))
47
48 #define is_error_type(t) ((t)->base_type == GLSL_TYPE_ERROR)
49
50 enum glsl_sampler_dim {
51 GLSL_SAMPLER_DIM_1D = 0,
52 GLSL_SAMPLER_DIM_2D,
53 GLSL_SAMPLER_DIM_3D,
54 GLSL_SAMPLER_DIM_CUBE,
55 GLSL_SAMPLER_DIM_RECT,
56 GLSL_SAMPLER_DIM_BUF
57 };
58
59
60 struct glsl_type {
61 unsigned base_type:4;
62
63 unsigned sampler_dimensionality:3;
64 unsigned sampler_shadow:1;
65 unsigned sampler_array:1;
66 unsigned sampler_type:2; /**< Type of data returned using this sampler.
67 * only \c GLSL_TYPE_FLOAT, \c GLSL_TYPE_INT,
68 * and \c GLSL_TYPE_UINT are valid.
69 */
70
71 unsigned vector_elements:3; /**< 0, 2, 3, or 4 vector elements. */
72 unsigned matrix_rows:3; /**< 0, 2, 3, or 4 matrix rows. */
73
74 /**
75 * Name of the data type
76 *
77 * This may be \c NULL for anonymous structures, for arrays, or for
78 * function types.
79 */
80 const char *name;
81
82 /**
83 * For \c GLSL_TYPE_ARRAY, this is the length of the array. For
84 * \c GLSL_TYPE_STRUCT, it is the number of elements in the structure and
85 * the number of values pointed to by \c fields.structure (below).
86 *
87 * For \c GLSL_TYPE_FUNCTION, it is the number of parameters to the
88 * function. The return value from a function is implicitly the first
89 * parameter. The types of the parameters are stored in
90 * \c fields.parameters (below).
91 */
92 unsigned length;
93
94 /**
95 * Subtype of composite data types.
96 */
97 union {
98 const struct glsl_type *array; /**< Type of array elements. */
99 const struct glsl_type *parameters; /**< Parameters to function. */
100 const struct glsl_struct_field *structure;/**< List of struct fields. */
101 } fields;
102
103
104 glsl_type(unsigned base_type, unsigned vector_elements,
105 unsigned matrix_rows, const char *name) :
106 base_type(base_type),
107 sampler_dimensionality(0), sampler_shadow(0), sampler_array(0),
108 sampler_type(0),
109 vector_elements(vector_elements), matrix_rows(matrix_rows),
110 name(name),
111 length(0)
112 {
113 memset(& fields, 0, sizeof(fields));
114 }
115
116 glsl_type(enum glsl_sampler_dim dim, bool shadow, bool array,
117 unsigned type, const char *name) :
118 base_type(GLSL_TYPE_SAMPLER),
119 sampler_dimensionality(dim), sampler_shadow(shadow),
120 sampler_array(array), sampler_type(type),
121 vector_elements(0), matrix_rows(0),
122 name(name),
123 length(0)
124 {
125 memset(& fields, 0, sizeof(fields));
126 }
127
128 glsl_type(const glsl_struct_field *fields, unsigned num_fields,
129 const char *name) :
130 base_type(GLSL_TYPE_STRUCT),
131 sampler_dimensionality(0), sampler_shadow(0), sampler_array(0),
132 sampler_type(0),
133 vector_elements(0), matrix_rows(0),
134 name(name),
135 length(num_fields)
136 {
137 this->fields.structure = fields;
138 }
139
140 /**
141 * Query whether or not a type is a scalar (non-vector and non-matrix).
142 */
143 bool is_scalar() const
144 {
145 return (vector_elements == 0)
146 && (base_type >= GLSL_TYPE_UINT)
147 && (base_type <= GLSL_TYPE_BOOL);
148 }
149
150 /**
151 * Query whether or not a type is a vector
152 */
153 bool is_vector() const
154 {
155 return (vector_elements > 0)
156 && (matrix_rows == 0)
157 && (base_type >= GLSL_TYPE_UINT)
158 && (base_type <= GLSL_TYPE_BOOL);
159 }
160
161 /**
162 * Query whether or not a type is a matrix
163 */
164 bool is_matrix() const
165 {
166 /* GLSL only has float matrices. */
167 return (matrix_rows > 0) && (base_type == GLSL_TYPE_FLOAT);
168 }
169 };
170
171 struct glsl_struct_field {
172 const struct glsl_type *type;
173 const char *name;
174 };
175
176 struct _mesa_glsl_parse_state;
177
178 #ifdef __cplusplus
179 extern "C" {
180 #endif
181
182 extern void
183 _mesa_glsl_initialize_types(struct _mesa_glsl_parse_state *state);
184
185 extern const struct glsl_type *
186 _mesa_glsl_get_vector_type(unsigned base_type, unsigned vector_length);
187
188 extern const struct glsl_type *const glsl_error_type;
189 extern const struct glsl_type *const glsl_int_type;
190 extern const struct glsl_type *const glsl_uint_type;
191 extern const struct glsl_type *const glsl_float_type;
192 extern const struct glsl_type *const glsl_bool_type;
193
194 #ifdef __cplusplus
195 }
196 #endif
197
198 #endif /* GLSL_TYPES_H */