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

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