SkSLSwizzle.h 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166
  1. /*
  2. * Copyright 2016 Google Inc.
  3. *
  4. * Use of this source code is governed by a BSD-style license that can be
  5. * found in the LICENSE file.
  6. */
  7. #ifndef SKSL_SWIZZLE
  8. #define SKSL_SWIZZLE
  9. #include "src/sksl/SkSLContext.h"
  10. #include "src/sksl/SkSLIRGenerator.h"
  11. #include "src/sksl/SkSLUtil.h"
  12. #include "src/sksl/ir/SkSLConstructor.h"
  13. #include "src/sksl/ir/SkSLExpression.h"
  14. namespace SkSL {
  15. // represents a swizzle component of constant 0, as in x.rgb0
  16. const int SKSL_SWIZZLE_0 = -2;
  17. // represents a swizzle component of constant 1, as in x.rgb1
  18. const int SKSL_SWIZZLE_1 = -1;
  19. /**
  20. * Given a type and a swizzle component count, returns the type that will result from swizzling. For
  21. * instance, swizzling a float3 with two components will result in a float2. It is possible to
  22. * swizzle with more components than the source vector, as in 'float2(1).xxxx'.
  23. */
  24. static const Type& get_type(const Context& context, Expression& value, size_t count) {
  25. const Type& base = value.fType.componentType();
  26. if (count == 1) {
  27. return base;
  28. }
  29. if (base == *context.fFloat_Type) {
  30. switch (count) {
  31. case 2: return *context.fFloat2_Type;
  32. case 3: return *context.fFloat3_Type;
  33. case 4: return *context.fFloat4_Type;
  34. }
  35. } else if (base == *context.fHalf_Type) {
  36. switch (count) {
  37. case 2: return *context.fHalf2_Type;
  38. case 3: return *context.fHalf3_Type;
  39. case 4: return *context.fHalf4_Type;
  40. }
  41. } else if (base == *context.fDouble_Type) {
  42. switch (count) {
  43. case 2: return *context.fDouble2_Type;
  44. case 3: return *context.fDouble3_Type;
  45. case 4: return *context.fDouble4_Type;
  46. }
  47. } else if (base == *context.fInt_Type) {
  48. switch (count) {
  49. case 2: return *context.fInt2_Type;
  50. case 3: return *context.fInt3_Type;
  51. case 4: return *context.fInt4_Type;
  52. }
  53. } else if (base == *context.fShort_Type) {
  54. switch (count) {
  55. case 2: return *context.fShort2_Type;
  56. case 3: return *context.fShort3_Type;
  57. case 4: return *context.fShort4_Type;
  58. }
  59. } else if (base == *context.fByte_Type) {
  60. switch (count) {
  61. case 2: return *context.fByte2_Type;
  62. case 3: return *context.fByte3_Type;
  63. case 4: return *context.fByte4_Type;
  64. }
  65. } else if (base == *context.fUInt_Type) {
  66. switch (count) {
  67. case 2: return *context.fUInt2_Type;
  68. case 3: return *context.fUInt3_Type;
  69. case 4: return *context.fUInt4_Type;
  70. }
  71. } else if (base == *context.fUShort_Type) {
  72. switch (count) {
  73. case 2: return *context.fUShort2_Type;
  74. case 3: return *context.fUShort3_Type;
  75. case 4: return *context.fUShort4_Type;
  76. }
  77. } else if (base == *context.fUByte_Type) {
  78. switch (count) {
  79. case 2: return *context.fUByte2_Type;
  80. case 3: return *context.fUByte3_Type;
  81. case 4: return *context.fUByte4_Type;
  82. }
  83. } else if (base == *context.fBool_Type) {
  84. switch (count) {
  85. case 2: return *context.fBool2_Type;
  86. case 3: return *context.fBool3_Type;
  87. case 4: return *context.fBool4_Type;
  88. }
  89. }
  90. ABORT("cannot swizzle %s\n", value.description().c_str());
  91. }
  92. /**
  93. * Represents a vector swizzle operation such as 'float2(1, 2, 3).zyx'.
  94. */
  95. struct Swizzle : public Expression {
  96. Swizzle(const Context& context, std::unique_ptr<Expression> base, std::vector<int> components)
  97. : INHERITED(base->fOffset, kSwizzle_Kind, get_type(context, *base, components.size()))
  98. , fBase(std::move(base))
  99. , fComponents(std::move(components)) {
  100. SkASSERT(fComponents.size() >= 1 && fComponents.size() <= 4);
  101. }
  102. std::unique_ptr<Expression> constantPropagate(const IRGenerator& irGenerator,
  103. const DefinitionMap& definitions) override {
  104. if (fBase->fKind == Expression::kConstructor_Kind && fBase->isConstant()) {
  105. // we're swizzling a constant vector, e.g. float4(1).x. Simplify it.
  106. SkASSERT(fBase->fKind == Expression::kConstructor_Kind);
  107. if (fType.isInteger()) {
  108. SkASSERT(fComponents.size() == 1);
  109. int64_t value = ((Constructor&) *fBase).getIVecComponent(fComponents[0]);
  110. return std::unique_ptr<Expression>(new IntLiteral(irGenerator.fContext,
  111. -1,
  112. value));
  113. } else if (fType.isFloat()) {
  114. SkASSERT(fComponents.size() == 1);
  115. double value = ((Constructor&) *fBase).getFVecComponent(fComponents[0]);
  116. return std::unique_ptr<Expression>(new FloatLiteral(irGenerator.fContext,
  117. -1,
  118. value));
  119. }
  120. }
  121. return nullptr;
  122. }
  123. bool hasSideEffects() const override {
  124. return fBase->hasSideEffects();
  125. }
  126. std::unique_ptr<Expression> clone() const override {
  127. return std::unique_ptr<Expression>(new Swizzle(fType, fBase->clone(), fComponents));
  128. }
  129. String description() const override {
  130. String result = fBase->description() + ".";
  131. for (int x : fComponents) {
  132. result += "01xyzw"[x + 2];
  133. }
  134. return result;
  135. }
  136. std::unique_ptr<Expression> fBase;
  137. const std::vector<int> fComponents;
  138. typedef Expression INHERITED;
  139. private:
  140. Swizzle(const Type& type, std::unique_ptr<Expression> base, std::vector<int> components)
  141. : INHERITED(base->fOffset, kSwizzle_Kind, type)
  142. , fBase(std::move(base))
  143. , fComponents(std::move(components)) {
  144. SkASSERT(fComponents.size() >= 1 && fComponents.size() <= 4);
  145. }
  146. };
  147. } // namespace
  148. #endif