glsl_type: Record type constructors are private
[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 <cstring>
30 #include <cassert>
31
32 extern "C" {
33 #include "GL/gl.h"
34 #include <talloc.h>
35 }
36
37 struct _mesa_glsl_parse_state;
38
39 extern "C" void
40 _mesa_glsl_initialize_types(struct _mesa_glsl_parse_state *state);
41
42 #define GLSL_TYPE_UINT 0
43 #define GLSL_TYPE_INT 1
44 #define GLSL_TYPE_FLOAT 2
45 #define GLSL_TYPE_BOOL 3
46 #define GLSL_TYPE_SAMPLER 4
47 #define GLSL_TYPE_STRUCT 5
48 #define GLSL_TYPE_ARRAY 6
49 #define GLSL_TYPE_FUNCTION 7
50 #define GLSL_TYPE_VOID 8
51 #define GLSL_TYPE_ERROR 9
52
53 enum glsl_sampler_dim {
54 GLSL_SAMPLER_DIM_1D = 0,
55 GLSL_SAMPLER_DIM_2D,
56 GLSL_SAMPLER_DIM_3D,
57 GLSL_SAMPLER_DIM_CUBE,
58 GLSL_SAMPLER_DIM_RECT,
59 GLSL_SAMPLER_DIM_BUF
60 };
61
62
63 struct glsl_type {
64 GLenum gl_type;
65 unsigned base_type:4;
66
67 unsigned sampler_dimensionality:3;
68 unsigned sampler_shadow:1;
69 unsigned sampler_array:1;
70 unsigned sampler_type:2; /**< Type of data returned using this sampler.
71 * only \c GLSL_TYPE_FLOAT, \c GLSL_TYPE_INT,
72 * and \c GLSL_TYPE_UINT are valid.
73 */
74
75 /* Callers of this talloc-based new need not call delete. It's
76 * easier to just talloc_free 'ctx' (or any of its ancestors). */
77 static void* operator new(size_t size, void *ctx)
78 {
79 void *type;
80
81 type = talloc_size(ctx, size);
82 assert(type != NULL);
83
84 return type;
85 }
86
87 /* If the user *does* call delete, that's OK, we will just
88 * talloc_free in that case. */
89 static void operator delete(void *type)
90 {
91 talloc_free(type);
92 }
93
94 /**
95 * \name Vector and matrix element counts
96 *
97 * For scalars, each of these values will be 1. For non-numeric types
98 * these will be 0.
99 */
100 /*@{*/
101 unsigned vector_elements:3; /**< 1, 2, 3, or 4 vector elements. */
102 unsigned matrix_columns:3; /**< 1, 2, 3, or 4 matrix columns. */
103 /*@}*/
104
105 /**
106 * Name of the data type
107 *
108 * This may be \c NULL for anonymous structures, for arrays, or for
109 * function types.
110 */
111 const char *name;
112
113 /**
114 * For \c GLSL_TYPE_ARRAY, this is the length of the array. For
115 * \c GLSL_TYPE_STRUCT, it is the number of elements in the structure and
116 * the number of values pointed to by \c fields.structure (below).
117 *
118 * For \c GLSL_TYPE_FUNCTION, it is the number of parameters to the
119 * function. The return value from a function is implicitly the first
120 * parameter. The types of the parameters are stored in
121 * \c fields.parameters (below).
122 */
123 unsigned length;
124
125 /**
126 * Subtype of composite data types.
127 */
128 union {
129 const struct glsl_type *array; /**< Type of array elements. */
130 const struct glsl_type *parameters; /**< Parameters to function. */
131 const struct glsl_struct_field *structure;/**< List of struct fields. */
132 } fields;
133
134
135 /**
136 * \name Pointers to various public type singletons
137 */
138 /*@{*/
139 static const glsl_type *const error_type;
140 static const glsl_type *const int_type;
141 static const glsl_type *const ivec4_type;
142 static const glsl_type *const uint_type;
143 static const glsl_type *const uvec4_type;
144 static const glsl_type *const float_type;
145 static const glsl_type *const vec2_type;
146 static const glsl_type *const vec3_type;
147 static const glsl_type *const vec4_type;
148 static const glsl_type *const bool_type;
149 static const glsl_type *const mat2_type;
150 static const glsl_type *const mat2x3_type;
151 static const glsl_type *const mat2x4_type;
152 static const glsl_type *const mat3x2_type;
153 static const glsl_type *const mat3_type;
154 static const glsl_type *const mat3x4_type;
155 static const glsl_type *const mat4x2_type;
156 static const glsl_type *const mat4x3_type;
157 static const glsl_type *const mat4_type;
158 /*@}*/
159
160
161 /**
162 * For numeric and boolean derrived types returns the basic scalar type
163 *
164 * If the type is a numeric or boolean scalar, vector, or matrix type,
165 * this function gets the scalar type of the individual components. For
166 * all other types, including arrays of numeric or boolean types, the
167 * error type is returned.
168 */
169 const glsl_type *get_base_type() const;
170
171 /**
172 * Query the type of elements in an array
173 *
174 * \return
175 * Pointer to the type of elements in the array for array types, or \c NULL
176 * for non-array types.
177 */
178 const glsl_type *element_type() const
179 {
180 return is_array() ? fields.array : NULL;
181 }
182
183 /**
184 * Get the instance of a built-in scalar, vector, or matrix type
185 */
186 static const glsl_type *get_instance(unsigned base_type, unsigned rows,
187 unsigned columns);
188
189 /**
190 * Get the instance of an array type
191 */
192 static const glsl_type *get_array_instance(void *ctx,
193 const glsl_type *base,
194 unsigned elements);
195
196 /**
197 * Get the instance of a record type
198 */
199 static const glsl_type *get_record_instance(const glsl_struct_field *fields,
200 unsigned num_fields,
201 const char *name);
202 /**
203 * Generate the constructor for this type and add it to the symbol table
204 */
205 class ir_function *generate_constructor(class glsl_symbol_table *) const;
206
207 /**
208 * Query the total number of scalars that make up a scalar, vector or matrix
209 */
210 unsigned components() const
211 {
212 return vector_elements * matrix_columns;
213 }
214
215 /**
216 * Calculate the number of components slots required to hold this type
217 *
218 * This is used to determine how many uniform or varying locations a type
219 * might occupy.
220 */
221 unsigned component_slots() const;
222
223
224 /**
225 * Query whether or not a type is a scalar (non-vector and non-matrix).
226 */
227 bool is_scalar() const
228 {
229 return (vector_elements == 1)
230 && (base_type >= GLSL_TYPE_UINT)
231 && (base_type <= GLSL_TYPE_BOOL);
232 }
233
234 /**
235 * Query whether or not a type is a vector
236 */
237 bool is_vector() const
238 {
239 return (vector_elements > 1)
240 && (matrix_columns == 1)
241 && (base_type >= GLSL_TYPE_UINT)
242 && (base_type <= GLSL_TYPE_BOOL);
243 }
244
245 /**
246 * Query whether or not a type is a matrix
247 */
248 bool is_matrix() const
249 {
250 /* GLSL only has float matrices. */
251 return (matrix_columns > 1) && (base_type == GLSL_TYPE_FLOAT);
252 }
253
254 /**
255 * Query whether or not a type is a non-array numeric type
256 */
257 bool is_numeric() const
258 {
259 return (base_type >= GLSL_TYPE_UINT) && (base_type <= GLSL_TYPE_FLOAT);
260 }
261
262 /**
263 * Query whether or not a type is an integral type
264 */
265 bool is_integer() const
266 {
267 return (base_type == GLSL_TYPE_UINT) || (base_type == GLSL_TYPE_INT);
268 }
269
270 /**
271 * Query whether or not a type is a float type
272 */
273 bool is_float() const
274 {
275 return base_type == GLSL_TYPE_FLOAT;
276 }
277
278 /**
279 * Query whether or not a type is a non-array boolean type
280 */
281 bool is_boolean() const
282 {
283 return base_type == GLSL_TYPE_BOOL;
284 }
285
286 /**
287 * Query whether or not a type is a sampler
288 */
289 bool is_sampler() const
290 {
291 return base_type == GLSL_TYPE_SAMPLER;
292 }
293
294 /**
295 * Query whether or not a type is an array
296 */
297 bool is_array() const
298 {
299 return base_type == GLSL_TYPE_ARRAY;
300 }
301
302 /**
303 * Query whether or not a type is a record
304 */
305 bool is_record() const
306 {
307 return base_type == GLSL_TYPE_STRUCT;
308 }
309
310 /**
311 * Query whether or not a type is the void type singleton.
312 */
313 bool is_void() const
314 {
315 return base_type == GLSL_TYPE_VOID;
316 }
317
318 /**
319 * Query whether or not a type is the error type singleton.
320 */
321 bool is_error() const
322 {
323 return base_type == GLSL_TYPE_ERROR;
324 }
325
326 /**
327 * Query the full type of a matrix row
328 *
329 * \return
330 * If the type is not a matrix, \c glsl_type::error_type is returned.
331 * Otherwise a type matching the rows of the matrix is returned.
332 */
333 const glsl_type *row_type() const
334 {
335 return is_matrix()
336 ? get_instance(base_type, matrix_columns, 1)
337 : error_type;
338 }
339
340 /**
341 * Query the full type of a matrix column
342 *
343 * \return
344 * If the type is not a matrix, \c glsl_type::error_type is returned.
345 * Otherwise a type matching the columns of the matrix is returned.
346 */
347 const glsl_type *column_type() const
348 {
349 return is_matrix()
350 ? get_instance(base_type, vector_elements, 1)
351 : error_type;
352 }
353
354
355 /**
356 * Get the type of a structure field
357 *
358 * \return
359 * Pointer to the type of the named field. If the type is not a structure
360 * or the named field does not exist, \c glsl_type::error_type is returned.
361 */
362 const glsl_type *field_type(const char *name) const;
363
364
365 /**
366 * Get the location of a filed within a record type
367 */
368 int field_index(const char *name) const;
369
370
371 /**
372 * Query the number of elements in an array type
373 *
374 * \return
375 * The number of elements in the array for array types or -1 for non-array
376 * types. If the number of elements in the array has not yet been declared,
377 * zero is returned.
378 */
379 int array_size() const
380 {
381 return is_array() ? length : -1;
382 }
383
384 private:
385 /** Constructor for vector and matrix types */
386 glsl_type(GLenum gl_type,
387 unsigned base_type, unsigned vector_elements,
388 unsigned matrix_columns, const char *name);
389
390 /** Constructor for sampler types */
391 glsl_type(GLenum gl_type,
392 enum glsl_sampler_dim dim, bool shadow, bool array,
393 unsigned type, const char *name);
394
395 /** Constructor for record types */
396 glsl_type(const glsl_struct_field *fields, unsigned num_fields,
397 const char *name);
398
399 /** Constructor for array types */
400 glsl_type(void *ctx, const glsl_type *array, unsigned length);
401
402 /** Hash table containing the known array types. */
403 static struct hash_table *array_types;
404
405 static int array_key_compare(const void *a, const void *b);
406 static unsigned array_key_hash(const void *key);
407
408 /** Hash table containing the known record types. */
409 static struct hash_table *record_types;
410
411 static int record_key_compare(const void *a, const void *b);
412 static unsigned record_key_hash(const void *key);
413
414 /**
415 * \name Pointers to various type singletons
416 */
417 /*@{*/
418 static const glsl_type _error_type;
419 static const glsl_type void_type;
420 static const glsl_type builtin_core_types[];
421 static const glsl_type builtin_structure_types[];
422 static const glsl_type builtin_110_deprecated_structure_types[];
423 static const glsl_type builtin_120_types[];
424 static const glsl_type builtin_130_types[];
425 static const glsl_type builtin_ARB_texture_rectangle_types[];
426 static const glsl_type builtin_EXT_texture_array_types[];
427 static const glsl_type builtin_EXT_texture_buffer_object_types[];
428 /*@}*/
429
430 /**
431 * \name Methods to populate a symbol table with built-in types.
432 *
433 * \internal
434 * This is one of the truely annoying things about C++. Methods that are
435 * completely internal and private to a type still have to be advertised to
436 * the world in a public header file.
437 */
438 /*@{*/
439 static void generate_110_types(class glsl_symbol_table *);
440 static void generate_120_types(class glsl_symbol_table *);
441 static void generate_130_types(class glsl_symbol_table *);
442 static void generate_ARB_texture_rectangle_types(class glsl_symbol_table *,
443 bool);
444 static void generate_EXT_texture_array_types(class glsl_symbol_table *,
445 bool);
446 /**
447 * This function is a friend because it needs to call the various
448 * generate_*_types functions and it has C linkage.
449 */
450 friend void _mesa_glsl_initialize_types(struct _mesa_glsl_parse_state *);
451 /*@}*/
452 };
453
454 struct glsl_struct_field {
455 const struct glsl_type *type;
456 const char *name;
457 };
458
459 #endif /* GLSL_TYPES_H */