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 10KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387
  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 uint_type;
  99. static const glsl_type *const float_type;
  100. static const glsl_type *const bool_type;
  101. /*@}*/
  102. glsl_type(unsigned base_type, unsigned vector_elements,
  103. unsigned matrix_columns, const char *name) :
  104. base_type(base_type),
  105. sampler_dimensionality(0), sampler_shadow(0), sampler_array(0),
  106. sampler_type(0),
  107. vector_elements(vector_elements), matrix_columns(matrix_columns),
  108. name(name),
  109. length(0)
  110. {
  111. /* Neither dimension is zero or both dimensions are zero.
  112. */
  113. assert((vector_elements == 0) == (matrix_columns == 0));
  114. memset(& fields, 0, sizeof(fields));
  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_columns(0),
  122. name(name),
  123. length(0)
  124. {
  125. memset(& fields, 0, sizeof(fields));
  126. }
  127. glsl_type(const glsl_struct_field *fields, unsigned num_fields,
  128. const char *name) :
  129. base_type(GLSL_TYPE_STRUCT),
  130. sampler_dimensionality(0), sampler_shadow(0), sampler_array(0),
  131. sampler_type(0),
  132. vector_elements(0), matrix_columns(0),
  133. name(name),
  134. length(num_fields)
  135. {
  136. this->fields.structure = fields;
  137. }
  138. /**
  139. * For numeric and boolean derrived types returns the basic scalar type
  140. *
  141. * If the type is a numeric or boolean scalar, vector, or matrix type,
  142. * this function gets the scalar type of the individual components. For
  143. * all other types, including arrays of numeric or boolean types, the
  144. * error type is returned.
  145. */
  146. const glsl_type *get_base_type() const;
  147. /**
  148. * Query the type of elements in an array
  149. *
  150. * \return
  151. * Pointer to the type of elements in the array for array types, or \c NULL
  152. * for non-array types.
  153. */
  154. const glsl_type *element_type() const
  155. {
  156. return is_array() ? fields.array : NULL;
  157. }
  158. /**
  159. * Get the instance of a built-in scalar, vector, or matrix type
  160. */
  161. static const glsl_type *get_instance(unsigned base_type, unsigned rows,
  162. unsigned columns);
  163. /**
  164. * Get the instance of an array type
  165. */
  166. static const glsl_type *get_array_instance(const glsl_type *base,
  167. unsigned elements);
  168. class ir_function *generate_constructor_prototype(class glsl_symbol_table *)
  169. const;
  170. /**
  171. * Query the total number of scalars that make up a scalar, vector or matrix
  172. */
  173. unsigned components() const
  174. {
  175. return vector_elements * matrix_columns;
  176. }
  177. /**
  178. * Query whether or not a type is a scalar (non-vector and non-matrix).
  179. */
  180. bool is_scalar() const
  181. {
  182. return (vector_elements == 1)
  183. && (base_type >= GLSL_TYPE_UINT)
  184. && (base_type <= GLSL_TYPE_BOOL);
  185. }
  186. /**
  187. * Query whether or not a type is a vector
  188. */
  189. bool is_vector() const
  190. {
  191. return (vector_elements > 1)
  192. && (matrix_columns == 1)
  193. && (base_type >= GLSL_TYPE_UINT)
  194. && (base_type <= GLSL_TYPE_BOOL);
  195. }
  196. /**
  197. * Query whether or not a type is a matrix
  198. */
  199. bool is_matrix() const
  200. {
  201. /* GLSL only has float matrices. */
  202. return (matrix_columns > 1) && (base_type == GLSL_TYPE_FLOAT);
  203. }
  204. /**
  205. * Query whether or not a type is a non-array numeric type
  206. */
  207. bool is_numeric() const
  208. {
  209. return (base_type >= GLSL_TYPE_UINT) && (base_type <= GLSL_TYPE_FLOAT);
  210. }
  211. /**
  212. * Query whether or not a type is an integral type
  213. */
  214. bool is_integer() const
  215. {
  216. return (base_type == GLSL_TYPE_UINT) || (base_type == GLSL_TYPE_INT);
  217. }
  218. /**
  219. * Query whether or not a type is a float type
  220. */
  221. bool is_float() const
  222. {
  223. return base_type == GLSL_TYPE_FLOAT;
  224. }
  225. /**
  226. * Query whether or not a type is a non-array boolean type
  227. */
  228. bool is_boolean() const
  229. {
  230. return base_type == GLSL_TYPE_BOOL;
  231. }
  232. /**
  233. * Query whether or not a type is a sampler
  234. */
  235. bool is_sampler() const
  236. {
  237. return base_type == GLSL_TYPE_SAMPLER;
  238. }
  239. /**
  240. * Query whether or not a type is an array
  241. */
  242. bool is_array() const
  243. {
  244. return base_type == GLSL_TYPE_ARRAY;
  245. }
  246. /**
  247. * Query whether or not a type is the void type singleton.
  248. */
  249. bool is_void() const
  250. {
  251. return base_type == GLSL_TYPE_VOID;
  252. }
  253. /**
  254. * Query whether or not a type is the error type singleton.
  255. */
  256. bool is_error() const
  257. {
  258. return base_type == GLSL_TYPE_ERROR;
  259. }
  260. /**
  261. * Query the full type of a matrix row
  262. *
  263. * \return
  264. * If the type is not a matrix, \c glsl_type::error_type is returned.
  265. * Otherwise a type matching the rows of the matrix is returned.
  266. */
  267. const glsl_type *row_type() const
  268. {
  269. return is_matrix()
  270. ? get_instance(base_type, matrix_columns, 1)
  271. : error_type;
  272. }
  273. /**
  274. * Query the full type of a matrix column
  275. *
  276. * \return
  277. * If the type is not a matrix, \c glsl_type::error_type is returned.
  278. * Otherwise a type matching the columns of the matrix is returned.
  279. */
  280. const glsl_type *column_type() const
  281. {
  282. return is_matrix()
  283. ? get_instance(base_type, vector_elements, 1)
  284. : error_type;
  285. }
  286. /**
  287. * Query the number of elements in an array type
  288. *
  289. * \return
  290. * The number of elements in the array for array types or -1 for non-array
  291. * types. If the number of elements in the array has not yet been declared,
  292. * zero is returned.
  293. */
  294. int array_size() const
  295. {
  296. return is_array() ? length : -1;
  297. }
  298. private:
  299. /**
  300. * Constructor for array types
  301. */
  302. glsl_type(const glsl_type *array, unsigned length);
  303. /**
  304. * \name Pointers to various private type singletons
  305. */
  306. /*@{*/
  307. static const glsl_type *const mat2_type;
  308. static const glsl_type *const mat2x3_type;
  309. static const glsl_type *const mat2x4_type;
  310. static const glsl_type *const mat3x2_type;
  311. static const glsl_type *const mat3_type;
  312. static const glsl_type *const mat3x4_type;
  313. static const glsl_type *const mat4x2_type;
  314. static const glsl_type *const mat4x3_type;
  315. static const glsl_type *const mat4_type;
  316. /*@}*/
  317. /** Hash table containing the known array types. */
  318. static struct hash_table *array_types;
  319. static int array_key_compare(const void *a, const void *b);
  320. static unsigned array_key_hash(const void *key);
  321. };
  322. struct glsl_struct_field {
  323. const struct glsl_type *type;
  324. const char *name;
  325. };
  326. struct _mesa_glsl_parse_state;
  327. #ifdef __cplusplus
  328. extern "C" {
  329. #endif
  330. extern void
  331. _mesa_glsl_initialize_types(struct _mesa_glsl_parse_state *state);
  332. extern void
  333. _mesa_glsl_initialize_constructors(struct exec_list *instructions,
  334. struct _mesa_glsl_parse_state *state);
  335. #ifdef __cplusplus
  336. }
  337. #endif
  338. #endif /* GLSL_TYPES_H */