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_STRUCT, 57 GLSL_TYPE_ARRAY, 58 GLSL_TYPE_VOID, 59 GLSL_TYPE_ERROR 60 }; 61 62 enum glsl_sampler_dim { 63 GLSL_SAMPLER_DIM_1D = 0, 64 GLSL_SAMPLER_DIM_2D, 65 GLSL_SAMPLER_DIM_3D, 66 GLSL_SAMPLER_DIM_CUBE, 67 GLSL_SAMPLER_DIM_RECT, 68 GLSL_SAMPLER_DIM_BUF, 69 GLSL_SAMPLER_DIM_EXTERNAL 70 }; 71 72 #ifdef __cplusplus 73 #include "GL/gl.h" 74 #include "ralloc.h" 75 76 struct glsl_type { 77 GLenum gl_type; 78 glsl_base_type base_type; 79 80 unsigned sampler_dimensionality:3; /**< \see glsl_sampler_dim */ 81 unsigned sampler_shadow:1; 82 unsigned sampler_array:1; 83 unsigned sampler_type:2; /**< Type of data returned using this sampler. 84 * only \c GLSL_TYPE_FLOAT, \c GLSL_TYPE_INT, 85 * and \c GLSL_TYPE_UINT are valid. 86 */ 87 88 /* Callers of this ralloc-based new need not call delete. It's 89 * easier to just ralloc_free 'mem_ctx' (or any of its ancestors). */ 90 static void* operator new(size_t size) 91 { 92 if (glsl_type::mem_ctx == NULL) { 93 glsl_type::mem_ctx = ralloc_context(NULL); 94 assert(glsl_type::mem_ctx != NULL); 95 } 96 97 void *type; 98 99 type = ralloc_size(glsl_type::mem_ctx, size); 100 assert(type != NULL); 101 102 return type; 103 } 104 105 /* If the user *does* call delete, that's OK, we will just 106 * ralloc_free in that case. */ 107 static void operator delete(void *type) 108 { 109 ralloc_free(type); 110 } 111 112 /** 113 * \name Vector and matrix element counts 114 * 115 * For scalars, each of these values will be 1. For non-numeric types 116 * these will be 0. 117 */ 118 /*@{*/ 119 unsigned vector_elements:3; /**< 1, 2, 3, or 4 vector elements. */ 120 unsigned matrix_columns:3; /**< 1, 2, 3, or 4 matrix columns. */ 121 /*@}*/ 122 123 /** 124 * Name of the data type 125 * 126 * This may be \c NULL for anonymous structures, for arrays, or for 127 * function types. 128 */ 129 const char *name; 130 131 /** 132 * For \c GLSL_TYPE_ARRAY, this is the length of the array. For 133 * \c GLSL_TYPE_STRUCT, it is the number of elements in the structure and 134 * the number of values pointed to by \c fields.structure (below). 135 */ 136 unsigned length; 137 138 /** 139 * Subtype of composite data types. 140 */ 141 union { 142 const struct glsl_type *array; /**< Type of array elements. */ 143 const struct glsl_type *parameters; /**< Parameters to function. */ 144 struct glsl_struct_field *structure; /**< List of struct fields. */ 145 } fields; 146 147 148 /** 149 * \name Pointers to various public type singletons 150 */ 151 /*@{*/ 152 static const glsl_type *const error_type; 153 static const glsl_type *const void_type; 154 static const glsl_type *const int_type; 155 static const glsl_type *const ivec2_type; 156 static const glsl_type *const ivec3_type; 157 static const glsl_type *const ivec4_type; 158 static const glsl_type *const uint_type; 159 static const glsl_type *const uvec2_type; 160 static const glsl_type *const uvec3_type; 161 static const glsl_type *const uvec4_type; 162 static const glsl_type *const float_type; 163 static const glsl_type *const vec2_type; 164 static const glsl_type *const vec3_type; 165 static const glsl_type *const vec4_type; 166 static const glsl_type *const bool_type; 167 static const glsl_type *const bvec2_type; 168 static const glsl_type *const bvec3_type; 169 static const glsl_type *const bvec4_type; 170 static const glsl_type *const mat2_type; 171 static const glsl_type *const mat2x3_type; 172 static const glsl_type *const mat2x4_type; 173 static const glsl_type *const mat3x2_type; 174 static const glsl_type *const mat3_type; 175 static const glsl_type *const mat3x4_type; 176 static const glsl_type *const mat4x2_type; 177 static const glsl_type *const mat4x3_type; 178 static const glsl_type *const mat4_type; 179 /*@}*/ 180 181 182 /** 183 * For numeric and boolean derrived 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 * Query the total number of scalars that make up a scalar, vector or matrix 236 */ 237 unsigned components() const 238 { 239 return vector_elements * matrix_columns; 240 } 241 242 /** 243 * Calculate the number of components slots required to hold this type 244 * 245 * This is used to determine how many uniform or varying locations a type 246 * might occupy. 247 */ 248 unsigned component_slots() const; 249 250 /** 251 * Alignment in bytes of the start of this type in a std140 uniform 252 * block. 253 */ 254 unsigned std140_base_alignment(bool row_major) const; 255 256 /** Size in bytes of this type in a std140 uniform block. 257 * 258 * Note that this is not GL_UNIFORM_SIZE (which is the number of 259 * elements in the array) 260 */ 261 unsigned std140_size(bool row_major) const; 262 263 /** 264 * \brief Can this type be implicitly converted to another? 265 * 266 * \return True if the types are identical or if this type can be converted 267 * to \c desired according to Section 4.1.10 of the GLSL spec. 268 * 269 * \verbatim 270 * From page 25 (31 of the pdf) of the GLSL 1.50 spec, Section 4.1.10 271 * Implicit Conversions: 272 * 273 * In some situations, an expression and its type will be implicitly 274 * converted to a different type. The following table shows all allowed 275 * implicit conversions: 276 * 277 * Type of expression | Can be implicitly converted to 278 * -------------------------------------------------- 279 * int float 280 * uint 281 * 282 * ivec2 vec2 283 * uvec2 284 * 285 * ivec3 vec3 286 * uvec3 287 * 288 * ivec4 vec4 289 * uvec4 290 * 291 * There are no implicit array or structure conversions. For example, 292 * an array of int cannot be implicitly converted to an array of float. 293 * There are no implicit conversions between signed and unsigned 294 * integers. 295 * \endverbatim 296 */ 297 bool can_implicitly_convert_to(const glsl_type *desired) const; 298 299 /** 300 * Query whether or not a type is a scalar (non-vector and non-matrix). 301 */ 302 bool is_scalar() const 303 { 304 return (vector_elements == 1) 305 && (base_type >= GLSL_TYPE_UINT) 306 && (base_type <= GLSL_TYPE_BOOL); 307 } 308 309 /** 310 * Query whether or not a type is a vector 311 */ 312 bool is_vector() const 313 { 314 return (vector_elements > 1) 315 && (matrix_columns == 1) 316 && (base_type >= GLSL_TYPE_UINT) 317 && (base_type <= GLSL_TYPE_BOOL); 318 } 319 320 /** 321 * Query whether or not a type is a matrix 322 */ 323 bool is_matrix() const 324 { 325 /* GLSL only has float matrices. */ 326 return (matrix_columns > 1) && (base_type == GLSL_TYPE_FLOAT); 327 } 328 329 /** 330 * Query whether or not a type is a non-array numeric type 331 */ 332 bool is_numeric() const 333 { 334 return (base_type >= GLSL_TYPE_UINT) && (base_type <= GLSL_TYPE_FLOAT); 335 } 336 337 /** 338 * Query whether or not a type is an integral type 339 */ 340 bool is_integer() const 341 { 342 return (base_type == GLSL_TYPE_UINT) || (base_type == GLSL_TYPE_INT); 343 } 344 345 /** 346 * Query whether or not a type is a float type 347 */ 348 bool is_float() const 349 { 350 return base_type == GLSL_TYPE_FLOAT; 351 } 352 353 /** 354 * Query whether or not a type is a non-array boolean type 355 */ 356 bool is_boolean() const 357 { 358 return base_type == GLSL_TYPE_BOOL; 359 } 360 361 /** 362 * Query whether or not a type is a sampler 363 */ 364 bool is_sampler() const 365 { 366 return base_type == GLSL_TYPE_SAMPLER; 367 } 368 369 /** 370 * Query whether or not type is a sampler, or for struct and array 371 * types, contains a sampler. 372 */ 373 bool contains_sampler() const; 374 375 /** 376 * Get the Mesa texture target index for a sampler type. 377 */ 378 gl_texture_index sampler_index() const; 379 380 /** 381 * Query whether or not a type is an array 382 */ 383 bool is_array() const 384 { 385 return base_type == GLSL_TYPE_ARRAY; 386 } 387 388 /** 389 * Query whether or not a type is a record 390 */ 391 bool is_record() const 392 { 393 return base_type == GLSL_TYPE_STRUCT; 394 } 395 396 /** 397 * Query whether or not a type is the void type singleton. 398 */ 399 bool is_void() const 400 { 401 return base_type == GLSL_TYPE_VOID; 402 } 403 404 /** 405 * Query whether or not a type is the error type singleton. 406 */ 407 bool is_error() const 408 { 409 return base_type == GLSL_TYPE_ERROR; 410 } 411 412 /** 413 * Query the full type of a matrix row 414 * 415 * \return 416 * If the type is not a matrix, \c glsl_type::error_type is returned. 417 * Otherwise a type matching the rows of the matrix is returned. 418 */ 419 const glsl_type *row_type() const 420 { 421 return is_matrix() 422 ? get_instance(base_type, matrix_columns, 1) 423 : error_type; 424 } 425 426 /** 427 * Query the full type of a matrix column 428 * 429 * \return 430 * If the type is not a matrix, \c glsl_type::error_type is returned. 431 * Otherwise a type matching the columns of the matrix is returned. 432 */ 433 const glsl_type *column_type() const 434 { 435 return is_matrix() 436 ? get_instance(base_type, vector_elements, 1) 437 : error_type; 438 } 439 440 441 /** 442 * Get the type of a structure field 443 * 444 * \return 445 * Pointer to the type of the named field. If the type is not a structure 446 * or the named field does not exist, \c glsl_type::error_type is returned. 447 */ 448 const glsl_type *field_type(const char *name) const; 449 450 451 /** 452 * Get the location of a filed within a record type 453 */ 454 int field_index(const char *name) const; 455 456 457 /** 458 * Query the number of elements in an array type 459 * 460 * \return 461 * The number of elements in the array for array types or -1 for non-array 462 * types. If the number of elements in the array has not yet been declared, 463 * zero is returned. 464 */ 465 int array_size() const 466 { 467 return is_array() ? length : -1; 468 } 469 470 private: 471 /** 472 * ralloc context for all glsl_type allocations 473 * 474 * Set on the first call to \c glsl_type::new. 475 */ 476 static void *mem_ctx; 477 478 void init_ralloc_type_ctx(void); 479 480 /** Constructor for vector and matrix types */ 481 glsl_type(GLenum gl_type, 482 glsl_base_type base_type, unsigned vector_elements, 483 unsigned matrix_columns, const char *name); 484 485 /** Constructor for sampler types */ 486 glsl_type(GLenum gl_type, 487 enum glsl_sampler_dim dim, bool shadow, bool array, 488 unsigned type, const char *name); 489 490 /** Constructor for record types */ 491 glsl_type(const glsl_struct_field *fields, unsigned num_fields, 492 const char *name); 493 494 /** Constructor for array types */ 495 glsl_type(const glsl_type *array, unsigned length); 496 497 /** Hash table containing the known array types. */ 498 static struct hash_table *array_types; 499 500 /** Hash table containing the known record types. */ 501 static struct hash_table *record_types; 502 503 static int record_key_compare(const void *a, const void *b); 504 static unsigned record_key_hash(const void *key); 505 506 /** 507 * \name Pointers to various type singletons 508 */ 509 /*@{*/ 510 static const glsl_type _error_type; 511 static const glsl_type _void_type; 512 static const glsl_type _sampler3D_type; 513 static const glsl_type builtin_core_types[]; 514 static const glsl_type builtin_structure_types[]; 515 static const glsl_type builtin_110_deprecated_structure_types[]; 516 static const glsl_type builtin_110_types[]; 517 static const glsl_type builtin_120_types[]; 518 static const glsl_type builtin_130_types[]; 519 static const glsl_type builtin_140_types[]; 520 static const glsl_type builtin_ARB_texture_rectangle_types[]; 521 static const glsl_type builtin_EXT_texture_array_types[]; 522 static const glsl_type builtin_EXT_texture_buffer_object_types[]; 523 static const glsl_type builtin_OES_EGL_image_external_types[]; 524 /*@}*/ 525 526 /** 527 * \name Methods to populate a symbol table with built-in types. 528 * 529 * \internal 530 * This is one of the truely annoying things about C++. Methods that are 531 * completely internal and private to a type still have to be advertised to 532 * the world in a public header file. 533 */ 534 /*@{*/ 535 static void generate_100ES_types(glsl_symbol_table *); 536 static void generate_110_types(glsl_symbol_table *, bool add_deprecated); 537 static void generate_120_types(glsl_symbol_table *, bool add_deprecated); 538 static void generate_130_types(glsl_symbol_table *, bool add_deprecated); 539 static void generate_140_types(glsl_symbol_table *); 540 static void generate_ARB_texture_rectangle_types(glsl_symbol_table *, bool); 541 static void generate_EXT_texture_array_types(glsl_symbol_table *, bool); 542 static void generate_OES_texture_3D_types(glsl_symbol_table *, bool); 543 static void generate_OES_EGL_image_external_types(glsl_symbol_table *, bool); 544 /*@}*/ 545 546 /** 547 * \name Friend functions. 548 * 549 * These functions are friends because they must have C linkage and the 550 * need to call various private methods or access various private static 551 * data. 552 */ 553 /*@{*/ 554 friend void _mesa_glsl_initialize_types(struct _mesa_glsl_parse_state *); 555 friend void _mesa_glsl_release_types(void); 556 /*@}*/ 557 }; 558 559 struct glsl_struct_field { 560 const struct glsl_type *type; 561 const char *name; 562 }; 563 564 #endif /* __cplusplus */ 565 566 #endif /* GLSL_TYPES_H */ 567