Clone of mesa.
選択できるのは25トピックまでです。 トピックは、先頭が英数字で、英数字とダッシュ('-')を使用した35文字以内のものにしてください。

ir_vec_index_to_swizzle.cpp 4.4KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156
  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. /**
  24. * \file ir_vec_index_to_swizzle.cpp
  25. *
  26. * Turns constant indexing into vector types to swizzles. This will
  27. * let other swizzle-aware optimization passes catch these constructs,
  28. * and codegen backends not have to worry about this case.
  29. */
  30. #include "ir.h"
  31. #include "ir_visitor.h"
  32. #include "ir_optimization.h"
  33. #include "glsl_types.h"
  34. /**
  35. * Visitor class for replacing expressions with ir_constant values.
  36. */
  37. class ir_vec_index_to_swizzle_visitor : public ir_hierarchical_visitor {
  38. public:
  39. ir_vec_index_to_swizzle_visitor()
  40. {
  41. progress = false;
  42. }
  43. ir_rvalue *convert_vec_index_to_swizzle(ir_rvalue *val);
  44. virtual ir_visitor_status visit_enter(ir_expression *);
  45. virtual ir_visitor_status visit_enter(ir_swizzle *);
  46. virtual ir_visitor_status visit_enter(ir_assignment *);
  47. virtual ir_visitor_status visit_enter(ir_return *);
  48. virtual ir_visitor_status visit_enter(ir_call *);
  49. virtual ir_visitor_status visit_enter(ir_if *);
  50. bool progress;
  51. };
  52. ir_rvalue *
  53. ir_vec_index_to_swizzle_visitor::convert_vec_index_to_swizzle(ir_rvalue *ir)
  54. {
  55. ir_dereference_array *deref = ir->as_dereference_array();
  56. ir_constant *ir_constant;
  57. if (!deref)
  58. return ir;
  59. if (deref->array->type->is_matrix() || deref->array->type->is_array())
  60. return ir;
  61. assert(deref->array_index->type->base_type == GLSL_TYPE_INT);
  62. ir_constant = deref->array_index->constant_expression_value();
  63. if (!ir_constant)
  64. return ir;
  65. this->progress = true;
  66. return new ir_swizzle(deref->array, ir_constant->value.i[0], 0, 0, 0, 1);
  67. }
  68. ir_visitor_status
  69. ir_vec_index_to_swizzle_visitor::visit_enter(ir_expression *ir)
  70. {
  71. unsigned int i;
  72. for (i = 0; i < ir->get_num_operands(); i++) {
  73. ir->operands[i] = convert_vec_index_to_swizzle(ir->operands[i]);
  74. }
  75. return visit_continue;
  76. }
  77. ir_visitor_status
  78. ir_vec_index_to_swizzle_visitor::visit_enter(ir_swizzle *ir)
  79. {
  80. /* Can't be hit from normal GLSL, since you can't swizzle a scalar (which
  81. * the result of indexing a vector is. But maybe at some point we'll end up
  82. * using swizzling of scalars for vector construction.
  83. */
  84. ir->val = convert_vec_index_to_swizzle(ir->val);
  85. return visit_continue;
  86. }
  87. ir_visitor_status
  88. ir_vec_index_to_swizzle_visitor::visit_enter(ir_assignment *ir)
  89. {
  90. ir->lhs = convert_vec_index_to_swizzle(ir->lhs);
  91. ir->rhs = convert_vec_index_to_swizzle(ir->rhs);
  92. return visit_continue;
  93. }
  94. ir_visitor_status
  95. ir_vec_index_to_swizzle_visitor::visit_enter(ir_call *ir)
  96. {
  97. foreach_iter(exec_list_iterator, iter, *ir) {
  98. ir_rvalue *param = (ir_rvalue *)iter.get();
  99. ir_rvalue *new_param = convert_vec_index_to_swizzle(param);
  100. if (new_param != param) {
  101. param->insert_before(new_param);
  102. param->remove();
  103. }
  104. }
  105. return visit_continue;
  106. }
  107. ir_visitor_status
  108. ir_vec_index_to_swizzle_visitor::visit_enter(ir_return *ir)
  109. {
  110. if (ir->value) {
  111. ir->value = convert_vec_index_to_swizzle(ir->value);
  112. }
  113. return visit_continue;
  114. }
  115. ir_visitor_status
  116. ir_vec_index_to_swizzle_visitor::visit_enter(ir_if *ir)
  117. {
  118. ir->condition = convert_vec_index_to_swizzle(ir->condition);
  119. return visit_continue;
  120. }
  121. bool
  122. do_vec_index_to_swizzle(exec_list *instructions)
  123. {
  124. ir_vec_index_to_swizzle_visitor v;
  125. v.run(instructions);
  126. return false;
  127. }