SkSLIndexExpression.h 2.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  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_INDEX
  8. #define SKSL_INDEX
  9. #include "src/sksl/SkSLContext.h"
  10. #include "src/sksl/SkSLUtil.h"
  11. #include "src/sksl/ir/SkSLExpression.h"
  12. namespace SkSL {
  13. /**
  14. * Given a type, returns the type that will result from extracting an array value from it.
  15. */
  16. static const Type& index_type(const Context& context, const Type& type) {
  17. if (type.kind() == Type::kMatrix_Kind) {
  18. if (type.componentType() == *context.fFloat_Type) {
  19. switch (type.rows()) {
  20. case 2: return *context.fFloat2_Type;
  21. case 3: return *context.fFloat3_Type;
  22. case 4: return *context.fFloat4_Type;
  23. default: SkASSERT(false);
  24. }
  25. } else if (type.componentType() == *context.fHalf_Type) {
  26. switch (type.rows()) {
  27. case 2: return *context.fHalf2_Type;
  28. case 3: return *context.fHalf3_Type;
  29. case 4: return *context.fHalf4_Type;
  30. default: SkASSERT(false);
  31. }
  32. } else {
  33. SkASSERT(type.componentType() == *context.fDouble_Type);
  34. switch (type.rows()) {
  35. case 2: return *context.fDouble2_Type;
  36. case 3: return *context.fDouble3_Type;
  37. case 4: return *context.fDouble4_Type;
  38. default: SkASSERT(false);
  39. }
  40. }
  41. }
  42. return type.componentType();
  43. }
  44. /**
  45. * An expression which extracts a value from an array or matrix, as in 'm[2]'.
  46. */
  47. struct IndexExpression : public Expression {
  48. IndexExpression(const Context& context, std::unique_ptr<Expression> base,
  49. std::unique_ptr<Expression> index)
  50. : INHERITED(base->fOffset, kIndex_Kind, index_type(context, base->fType))
  51. , fBase(std::move(base))
  52. , fIndex(std::move(index)) {
  53. SkASSERT(fIndex->fType == *context.fInt_Type || fIndex->fType == *context.fUInt_Type);
  54. }
  55. bool hasSideEffects() const override {
  56. return fBase->hasSideEffects() || fIndex->hasSideEffects();
  57. }
  58. std::unique_ptr<Expression> clone() const override {
  59. return std::unique_ptr<Expression>(new IndexExpression(fBase->clone(), fIndex->clone(),
  60. &fType));
  61. }
  62. String description() const override {
  63. return fBase->description() + "[" + fIndex->description() + "]";
  64. }
  65. std::unique_ptr<Expression> fBase;
  66. std::unique_ptr<Expression> fIndex;
  67. typedef Expression INHERITED;
  68. private:
  69. IndexExpression(std::unique_ptr<Expression> base, std::unique_ptr<Expression> index,
  70. const Type* type)
  71. : INHERITED(base->fOffset, kIndex_Kind, *type)
  72. , fBase(std::move(base))
  73. , fIndex(std::move(index)) {}
  74. };
  75. } // namespace
  76. #endif