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.

ir.cpp 10.0KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370
  1. /*
  2. * Copyright © 2010 Intel Corporation
  3. *
  4. * Permission is hereby granted, free of charge, to any person obtaining a
  5. * copy of this software and associated documentation files (the "Software"),
  6. * to deal in the Software without restriction, including without limitation
  7. * the rights to use, copy, modify, merge, publish, distribute, sublicense,
  8. * and/or sell copies of the Software, and to permit persons to whom the
  9. * Software is furnished to do so, subject to the following conditions:
  10. *
  11. * The above copyright notice and this permission notice (including the next
  12. * paragraph) shall be included in all copies or substantial portions of the
  13. * Software.
  14. *
  15. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  16. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  17. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
  18. * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  19. * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
  20. * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
  21. * DEALINGS IN THE SOFTWARE.
  22. */
  23. #include <string.h>
  24. #include "main/imports.h"
  25. #include "main/simple_list.h"
  26. #include "ir.h"
  27. #include "ir_visitor.h"
  28. #include "glsl_types.h"
  29. ir_assignment::ir_assignment(ir_rvalue *lhs, ir_rvalue *rhs,
  30. ir_rvalue *condition)
  31. {
  32. this->lhs = lhs;
  33. this->rhs = rhs;
  34. this->condition = condition;
  35. }
  36. ir_expression::ir_expression(int op, const struct glsl_type *type,
  37. ir_rvalue *op0, ir_rvalue *op1)
  38. {
  39. this->type = type;
  40. this->operation = ir_expression_operation(op);
  41. this->operands[0] = op0;
  42. this->operands[1] = op1;
  43. }
  44. unsigned int
  45. ir_expression::get_num_operands(void)
  46. {
  47. /* Update ir_print_visitor.cpp when updating this list. */
  48. const int num_operands[] = {
  49. 1, /* ir_unop_bit_not */
  50. 1, /* ir_unop_logic_not */
  51. 1, /* ir_unop_neg */
  52. 1, /* ir_unop_abs */
  53. 1, /* ir_unop_rcp */
  54. 1, /* ir_unop_rsq */
  55. 1, /* ir_unop_sqrt */
  56. 1, /* ir_unop_exp */
  57. 1, /* ir_unop_log */
  58. 1, /* ir_unop_exp2 */
  59. 1, /* ir_unop_log2 */
  60. 1, /* ir_unop_f2i */
  61. 1, /* ir_unop_i2f */
  62. 1, /* ir_unop_f2b */
  63. 1, /* ir_unop_b2f */
  64. 1, /* ir_unop_i2b */
  65. 1, /* ir_unop_b2i */
  66. 1, /* ir_unop_u2f */
  67. 1, /* ir_unop_trunc */
  68. 1, /* ir_unop_ceil */
  69. 1, /* ir_unop_floor */
  70. 2, /* ir_binop_add */
  71. 2, /* ir_binop_sub */
  72. 2, /* ir_binop_mul */
  73. 2, /* ir_binop_div */
  74. 2, /* ir_binop_mod */
  75. 2, /* ir_binop_less */
  76. 2, /* ir_binop_greater */
  77. 2, /* ir_binop_lequal */
  78. 2, /* ir_binop_gequal */
  79. 2, /* ir_binop_equal */
  80. 2, /* ir_binop_nequal */
  81. 2, /* ir_binop_lshift */
  82. 2, /* ir_binop_rshift */
  83. 2, /* ir_binop_bit_and */
  84. 2, /* ir_binop_bit_xor */
  85. 2, /* ir_binop_bit_or */
  86. 2, /* ir_binop_logic_and */
  87. 2, /* ir_binop_logic_xor */
  88. 2, /* ir_binop_logic_or */
  89. 2, /* ir_binop_dot */
  90. 2, /* ir_binop_min */
  91. 2, /* ir_binop_max */
  92. 2, /* ir_binop_pow */
  93. };
  94. assert(sizeof(num_operands) / sizeof(num_operands[0]) == ir_binop_pow + 1);
  95. return num_operands[this->operation];
  96. }
  97. ir_label::ir_label(const char *label, ir_function_signature *signature)
  98. : label(label), signature(signature)
  99. {
  100. /* empty */
  101. }
  102. ir_constant::ir_constant(const struct glsl_type *type, const void *data)
  103. {
  104. unsigned size = 0;
  105. this->type = type;
  106. switch (type->base_type) {
  107. case GLSL_TYPE_UINT: size = sizeof(this->value.u[0]); break;
  108. case GLSL_TYPE_INT: size = sizeof(this->value.i[0]); break;
  109. case GLSL_TYPE_FLOAT: size = sizeof(this->value.f[0]); break;
  110. case GLSL_TYPE_BOOL: size = sizeof(this->value.b[0]); break;
  111. default:
  112. /* FINISHME: What to do? Exceptions are not the answer.
  113. */
  114. break;
  115. }
  116. memcpy(& this->value, data, size * type->components());
  117. }
  118. ir_constant::ir_constant(float f)
  119. {
  120. this->type = glsl_type::float_type;
  121. this->value.f[0] = f;
  122. }
  123. ir_constant::ir_constant(unsigned int u)
  124. {
  125. this->type = glsl_type::uint_type;
  126. this->value.u[0] = u;
  127. }
  128. ir_constant::ir_constant(int i)
  129. {
  130. this->type = glsl_type::int_type;
  131. this->value.i[0] = i;
  132. }
  133. ir_constant::ir_constant(bool b)
  134. {
  135. this->type = glsl_type::bool_type;
  136. this->value.b[0] = b;
  137. }
  138. ir_dereference::ir_dereference(ir_instruction *var)
  139. {
  140. this->mode = ir_reference_variable;
  141. this->var = var;
  142. this->type = (var != NULL) ? var->type : glsl_type::error_type;
  143. }
  144. ir_dereference::ir_dereference(ir_instruction *var,
  145. ir_rvalue *array_index)
  146. : mode(ir_reference_array), var(var)
  147. {
  148. type = glsl_type::error_type;
  149. if (var != NULL) {
  150. const glsl_type *const vt = var->type;
  151. if (vt->is_array()) {
  152. type = vt->element_type();
  153. } else if (vt->is_matrix()) {
  154. type = vt->column_type();
  155. } else if (vt->is_vector()) {
  156. type = vt->get_base_type();
  157. }
  158. }
  159. this->selector.array_index = array_index;
  160. }
  161. bool
  162. ir_dereference::is_lvalue()
  163. {
  164. if (var == NULL)
  165. return false;
  166. if (mode == ir_reference_variable) {
  167. ir_variable *const as_var = var->as_variable();
  168. if (as_var == NULL)
  169. return false;
  170. if (as_var->type->is_array() && !as_var->array_lvalue)
  171. return false;
  172. return !as_var->read_only;
  173. } else if (mode == ir_reference_array) {
  174. /* FINISHME: Walk up the dereference chain and figure out if
  175. * FINISHME: the variable is read-only.
  176. */
  177. }
  178. return true;
  179. }
  180. ir_swizzle::ir_swizzle(ir_rvalue *val, unsigned x, unsigned y, unsigned z,
  181. unsigned w, unsigned count)
  182. : val(val)
  183. {
  184. assert((count >= 1) && (count <= 4));
  185. const unsigned dup_mask = 0
  186. | ((count > 1) ? ((1U << y) & ((1U << x) )) : 0)
  187. | ((count > 2) ? ((1U << z) & ((1U << x) | (1U << y) )) : 0)
  188. | ((count > 3) ? ((1U << w) & ((1U << x) | (1U << y) | (1U << z))) : 0);
  189. assert(x <= 3);
  190. assert(y <= 3);
  191. assert(z <= 3);
  192. assert(w <= 3);
  193. mask.x = x;
  194. mask.y = y;
  195. mask.z = z;
  196. mask.w = w;
  197. mask.num_components = count;
  198. mask.has_duplicates = dup_mask != 0;
  199. /* Based on the number of elements in the swizzle and the base type
  200. * (i.e., float, int, unsigned, or bool) of the vector being swizzled,
  201. * generate the type of the resulting value.
  202. */
  203. type = glsl_type::get_instance(val->type->base_type, mask.num_components, 1);
  204. }
  205. #define X 1
  206. #define R 5
  207. #define S 9
  208. #define I 13
  209. ir_swizzle *
  210. ir_swizzle::create(ir_rvalue *val, const char *str, unsigned vector_length)
  211. {
  212. /* For each possible swizzle character, this table encodes the value in
  213. * \c idx_map that represents the 0th element of the vector. For invalid
  214. * swizzle characters (e.g., 'k'), a special value is used that will allow
  215. * detection of errors.
  216. */
  217. static const unsigned char base_idx[26] = {
  218. /* a b c d e f g h i j k l m */
  219. R, R, I, I, I, I, R, I, I, I, I, I, I,
  220. /* n o p q r s t u v w x y z */
  221. I, I, S, S, R, S, S, I, I, X, X, X, X
  222. };
  223. /* Each valid swizzle character has an entry in the previous table. This
  224. * table encodes the base index encoded in the previous table plus the actual
  225. * index of the swizzle character. When processing swizzles, the first
  226. * character in the string is indexed in the previous table. Each character
  227. * in the string is indexed in this table, and the value found there has the
  228. * value form the first table subtracted. The result must be on the range
  229. * [0,3].
  230. *
  231. * For example, the string "wzyx" will get X from the first table. Each of
  232. * the charcaters will get X+3, X+2, X+1, and X+0 from this table. After
  233. * subtraction, the swizzle values are { 3, 2, 1, 0 }.
  234. *
  235. * The string "wzrg" will get X from the first table. Each of the characters
  236. * will get X+3, X+2, R+0, and R+1 from this table. After subtraction, the
  237. * swizzle values are { 3, 2, 4, 5 }. Since 4 and 5 are outside the range
  238. * [0,3], the error is detected.
  239. */
  240. static const unsigned char idx_map[26] = {
  241. /* a b c d e f g h i j k l m */
  242. R+3, R+2, 0, 0, 0, 0, R+1, 0, 0, 0, 0, 0, 0,
  243. /* n o p q r s t u v w x y z */
  244. 0, 0, S+2, S+3, R+0, S+0, S+1, 0, 0, X+3, X+0, X+1, X+2
  245. };
  246. int swiz_idx[4] = { 0, 0, 0, 0 };
  247. unsigned i;
  248. /* Validate the first character in the swizzle string and look up the base
  249. * index value as described above.
  250. */
  251. if ((str[0] < 'a') || (str[0] > 'z'))
  252. return NULL;
  253. const unsigned base = base_idx[str[0] - 'a'];
  254. for (i = 0; (i < 4) && (str[i] != '\0'); i++) {
  255. /* Validate the next character, and, as described above, convert it to a
  256. * swizzle index.
  257. */
  258. if ((str[i] < 'a') || (str[i] > 'z'))
  259. return NULL;
  260. swiz_idx[i] = idx_map[str[i] - 'a'] - base;
  261. if ((swiz_idx[i] < 0) || (swiz_idx[i] >= (int) vector_length))
  262. return NULL;
  263. }
  264. if (str[i] != '\0')
  265. return NULL;
  266. return new ir_swizzle(val, swiz_idx[0], swiz_idx[1], swiz_idx[2],
  267. swiz_idx[3], i);
  268. }
  269. #undef X
  270. #undef R
  271. #undef S
  272. #undef I
  273. ir_variable::ir_variable(const struct glsl_type *type, const char *name)
  274. : max_array_access(0), read_only(false), centroid(false), invariant(false),
  275. mode(ir_var_auto), interpolation(ir_var_smooth)
  276. {
  277. this->type = type;
  278. this->name = name;
  279. this->constant_value = NULL;
  280. if (type && type->base_type == GLSL_TYPE_SAMPLER)
  281. this->read_only = true;
  282. }
  283. ir_function_signature::ir_function_signature(const glsl_type *return_type)
  284. : return_type(return_type), definition(NULL)
  285. {
  286. /* empty */
  287. }
  288. ir_function::ir_function(const char *name)
  289. : name(name)
  290. {
  291. /* empty */
  292. }
  293. ir_call *
  294. ir_call::get_error_instruction()
  295. {
  296. ir_call *call = new ir_call;
  297. call->type = glsl_type::error_type;
  298. return call;
  299. }
  300. void
  301. visit_exec_list(exec_list *list, ir_visitor *visitor)
  302. {
  303. foreach_iter(exec_list_iterator, iter, *list) {
  304. ((ir_instruction *)iter.get())->accept(visitor);
  305. }
  306. }