Clone of mesa.
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

glsl_types.h 11KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412
  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. #pragma once
  25. #ifndef GLSL_TYPES_H
  26. #define GLSL_TYPES_H
  27. #include <cstring>
  28. #include <cassert>
  29. #define GLSL_TYPE_UINT 0
  30. #define GLSL_TYPE_INT 1
  31. #define GLSL_TYPE_FLOAT 2
  32. #define GLSL_TYPE_BOOL 3
  33. #define GLSL_TYPE_SAMPLER 4
  34. #define GLSL_TYPE_STRUCT 5
  35. #define GLSL_TYPE_ARRAY 6
  36. #define GLSL_TYPE_FUNCTION 7
  37. #define GLSL_TYPE_VOID 8
  38. #define GLSL_TYPE_ERROR 9
  39. enum glsl_sampler_dim {
  40. GLSL_SAMPLER_DIM_1D = 0,
  41. GLSL_SAMPLER_DIM_2D,
  42. GLSL_SAMPLER_DIM_3D,
  43. GLSL_SAMPLER_DIM_CUBE,
  44. GLSL_SAMPLER_DIM_RECT,
  45. GLSL_SAMPLER_DIM_BUF
  46. };
  47. struct glsl_type {
  48. unsigned base_type:4;
  49. unsigned sampler_dimensionality:3;
  50. unsigned sampler_shadow:1;
  51. unsigned sampler_array:1;
  52. unsigned sampler_type:2; /**< Type of data returned using this sampler.
  53. * only \c GLSL_TYPE_FLOAT, \c GLSL_TYPE_INT,
  54. * and \c GLSL_TYPE_UINT are valid.
  55. */
  56. /**
  57. * \name Vector and matrix element counts
  58. *
  59. * For scalars, each of these values will be 1. For non-numeric types
  60. * these will be 0.
  61. */
  62. /*@{*/
  63. unsigned vector_elements:3; /**< 1, 2, 3, or 4 vector elements. */
  64. unsigned matrix_columns:3; /**< 1, 2, 3, or 4 matrix columns. */
  65. /*@}*/
  66. /**
  67. * Name of the data type
  68. *
  69. * This may be \c NULL for anonymous structures, for arrays, or for
  70. * function types.
  71. */
  72. const char *name;
  73. /**
  74. * For \c GLSL_TYPE_ARRAY, this is the length of the array. For
  75. * \c GLSL_TYPE_STRUCT, it is the number of elements in the structure and
  76. * the number of values pointed to by \c fields.structure (below).
  77. *
  78. * For \c GLSL_TYPE_FUNCTION, it is the number of parameters to the
  79. * function. The return value from a function is implicitly the first
  80. * parameter. The types of the parameters are stored in
  81. * \c fields.parameters (below).
  82. */
  83. unsigned length;
  84. /**
  85. * Subtype of composite data types.
  86. */
  87. union {
  88. const struct glsl_type *array; /**< Type of array elements. */
  89. const struct glsl_type *parameters; /**< Parameters to function. */
  90. const struct glsl_struct_field *structure;/**< List of struct fields. */
  91. } fields;
  92. /**
  93. * \name Pointers to various public type singletons
  94. */
  95. /*@{*/
  96. static const glsl_type *const error_type;
  97. static const glsl_type *const int_type;
  98. static const glsl_type *const ivec4_type;
  99. static const glsl_type *const uint_type;
  100. static const glsl_type *const uvec4_type;
  101. static const glsl_type *const float_type;
  102. static const glsl_type *const vec2_type;
  103. static const glsl_type *const vec3_type;
  104. static const glsl_type *const vec4_type;
  105. static const glsl_type *const bool_type;
  106. static const glsl_type *const mat2_type;
  107. static const glsl_type *const mat2x3_type;
  108. static const glsl_type *const mat2x4_type;
  109. static const glsl_type *const mat3x2_type;
  110. static const glsl_type *const mat3_type;
  111. static const glsl_type *const mat3x4_type;
  112. static const glsl_type *const mat4x2_type;
  113. static const glsl_type *const mat4x3_type;
  114. static const glsl_type *const mat4_type;
  115. /*@}*/
  116. glsl_type(unsigned base_type, unsigned vector_elements,
  117. unsigned matrix_columns, const char *name) :
  118. base_type(base_type),
  119. sampler_dimensionality(0), sampler_shadow(0), sampler_array(0),
  120. sampler_type(0),
  121. vector_elements(vector_elements), matrix_columns(matrix_columns),
  122. name(name),
  123. length(0)
  124. {
  125. /* Neither dimension is zero or both dimensions are zero.
  126. */
  127. assert((vector_elements == 0) == (matrix_columns == 0));
  128. memset(& fields, 0, sizeof(fields));
  129. }
  130. glsl_type(enum glsl_sampler_dim dim, bool shadow, bool array,
  131. unsigned type, const char *name) :
  132. base_type(GLSL_TYPE_SAMPLER),
  133. sampler_dimensionality(dim), sampler_shadow(shadow),
  134. sampler_array(array), sampler_type(type),
  135. vector_elements(0), matrix_columns(0),
  136. name(name),
  137. length(0)
  138. {
  139. memset(& fields, 0, sizeof(fields));
  140. }
  141. glsl_type(const glsl_struct_field *fields, unsigned num_fields,
  142. const char *name) :
  143. base_type(GLSL_TYPE_STRUCT),
  144. sampler_dimensionality(0), sampler_shadow(0), sampler_array(0),
  145. sampler_type(0),
  146. vector_elements(0), matrix_columns(0),
  147. name(name),
  148. length(num_fields)
  149. {
  150. this->fields.structure = fields;
  151. }
  152. /**
  153. * For numeric and boolean derrived types returns the basic scalar type
  154. *
  155. * If the type is a numeric or boolean scalar, vector, or matrix type,
  156. * this function gets the scalar type of the individual components. For
  157. * all other types, including arrays of numeric or boolean types, the
  158. * error type is returned.
  159. */
  160. const glsl_type *get_base_type() const;
  161. /**
  162. * Query the type of elements in an array
  163. *
  164. * \return
  165. * Pointer to the type of elements in the array for array types, or \c NULL
  166. * for non-array types.
  167. */
  168. const glsl_type *element_type() const
  169. {
  170. return is_array() ? fields.array : NULL;
  171. }
  172. /**
  173. * Get the instance of a built-in scalar, vector, or matrix type
  174. */
  175. static const glsl_type *get_instance(unsigned base_type, unsigned rows,
  176. unsigned columns);
  177. /**
  178. * Get the instance of an array type
  179. */
  180. static const glsl_type *get_array_instance(const glsl_type *base,
  181. unsigned elements);
  182. /**
  183. * Generate the constructor for this type and add it to the symbol table
  184. */
  185. class ir_function *generate_constructor(class glsl_symbol_table *) const;
  186. /**
  187. * Query the total number of scalars that make up a scalar, vector or matrix
  188. */
  189. unsigned components() const
  190. {
  191. return vector_elements * matrix_columns;
  192. }
  193. /**
  194. * Query whether or not a type is a scalar (non-vector and non-matrix).
  195. */
  196. bool is_scalar() const
  197. {
  198. return (vector_elements == 1)
  199. && (base_type >= GLSL_TYPE_UINT)
  200. && (base_type <= GLSL_TYPE_BOOL);
  201. }
  202. /**
  203. * Query whether or not a type is a vector
  204. */
  205. bool is_vector() const
  206. {
  207. return (vector_elements > 1)
  208. && (matrix_columns == 1)
  209. && (base_type >= GLSL_TYPE_UINT)
  210. && (base_type <= GLSL_TYPE_BOOL);
  211. }
  212. /**
  213. * Query whether or not a type is a matrix
  214. */
  215. bool is_matrix() const
  216. {
  217. /* GLSL only has float matrices. */
  218. return (matrix_columns > 1) && (base_type == GLSL_TYPE_FLOAT);
  219. }
  220. /**
  221. * Query whether or not a type is a non-array numeric type
  222. */
  223. bool is_numeric() const
  224. {
  225. return (base_type >= GLSL_TYPE_UINT) && (base_type <= GLSL_TYPE_FLOAT);
  226. }
  227. /**
  228. * Query whether or not a type is an integral type
  229. */
  230. bool is_integer() const
  231. {
  232. return (base_type == GLSL_TYPE_UINT) || (base_type == GLSL_TYPE_INT);
  233. }
  234. /**
  235. * Query whether or not a type is a float type
  236. */
  237. bool is_float() const
  238. {
  239. return base_type == GLSL_TYPE_FLOAT;
  240. }
  241. /**
  242. * Query whether or not a type is a non-array boolean type
  243. */
  244. bool is_boolean() const
  245. {
  246. return base_type == GLSL_TYPE_BOOL;
  247. }
  248. /**
  249. * Query whether or not a type is a sampler
  250. */
  251. bool is_sampler() const
  252. {
  253. return base_type == GLSL_TYPE_SAMPLER;
  254. }
  255. /**
  256. * Query whether or not a type is an array
  257. */
  258. bool is_array() const
  259. {
  260. return base_type == GLSL_TYPE_ARRAY;
  261. }
  262. /**
  263. * Query whether or not a type is a record
  264. */
  265. bool is_record() const
  266. {
  267. return base_type == GLSL_TYPE_STRUCT;
  268. }
  269. /**
  270. * Query whether or not a type is the void type singleton.
  271. */
  272. bool is_void() const
  273. {
  274. return base_type == GLSL_TYPE_VOID;
  275. }
  276. /**
  277. * Query whether or not a type is the error type singleton.
  278. */
  279. bool is_error() const
  280. {
  281. return base_type == GLSL_TYPE_ERROR;
  282. }
  283. /**
  284. * Query the full type of a matrix row
  285. *
  286. * \return
  287. * If the type is not a matrix, \c glsl_type::error_type is returned.
  288. * Otherwise a type matching the rows of the matrix is returned.
  289. */
  290. const glsl_type *row_type() const
  291. {
  292. return is_matrix()
  293. ? get_instance(base_type, matrix_columns, 1)
  294. : error_type;
  295. }
  296. /**
  297. * Query the full type of a matrix column
  298. *
  299. * \return
  300. * If the type is not a matrix, \c glsl_type::error_type is returned.
  301. * Otherwise a type matching the columns of the matrix is returned.
  302. */
  303. const glsl_type *column_type() const
  304. {
  305. return is_matrix()
  306. ? get_instance(base_type, vector_elements, 1)
  307. : error_type;
  308. }
  309. /**
  310. * Get the type of a structure field
  311. *
  312. * \return
  313. * Pointer to the type of the named field. If the type is not a structure
  314. * or the named field does not exist, \c glsl_type::error_type is returned.
  315. */
  316. const glsl_type *field_type(const char *name) const;
  317. /**
  318. * Get the location of a filed within a record type
  319. */
  320. int field_index(const char *name) const;
  321. /**
  322. * Query the number of elements in an array type
  323. *
  324. * \return
  325. * The number of elements in the array for array types or -1 for non-array
  326. * types. If the number of elements in the array has not yet been declared,
  327. * zero is returned.
  328. */
  329. int array_size() const
  330. {
  331. return is_array() ? length : -1;
  332. }
  333. private:
  334. /**
  335. * Constructor for array types
  336. */
  337. glsl_type(const glsl_type *array, unsigned length);
  338. /** Hash table containing the known array types. */
  339. static struct hash_table *array_types;
  340. static int array_key_compare(const void *a, const void *b);
  341. static unsigned array_key_hash(const void *key);
  342. };
  343. struct glsl_struct_field {
  344. const struct glsl_type *type;
  345. const char *name;
  346. };
  347. struct _mesa_glsl_parse_state;
  348. #ifdef __cplusplus
  349. extern "C" {
  350. #endif
  351. extern void
  352. _mesa_glsl_initialize_types(struct _mesa_glsl_parse_state *state);
  353. extern void
  354. _mesa_glsl_initialize_constructors(struct exec_list *instructions,
  355. struct _mesa_glsl_parse_state *state);
  356. #ifdef __cplusplus
  357. }
  358. #endif
  359. #endif /* GLSL_TYPES_H */