SkSLInterfaceBlock.h 2.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283
  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_INTERFACEBLOCK
  8. #define SKSL_INTERFACEBLOCK
  9. #include "src/sksl/ir/SkSLProgramElement.h"
  10. #include "src/sksl/ir/SkSLSymbolTable.h"
  11. #include "src/sksl/ir/SkSLVarDeclarations.h"
  12. namespace SkSL {
  13. /**
  14. * An interface block, as in:
  15. *
  16. * out sk_PerVertex {
  17. * layout(builtin=0) float4 sk_Position;
  18. * layout(builtin=1) float sk_PointSize;
  19. * };
  20. *
  21. * At the IR level, this is represented by a single variable of struct type.
  22. */
  23. struct InterfaceBlock : public ProgramElement {
  24. InterfaceBlock(int offset, const Variable* var, String typeName, String instanceName,
  25. std::vector<std::unique_ptr<Expression>> sizes,
  26. std::shared_ptr<SymbolTable> typeOwner)
  27. : INHERITED(offset, kInterfaceBlock_Kind)
  28. , fVariable(*var)
  29. , fTypeName(std::move(typeName))
  30. , fInstanceName(std::move(instanceName))
  31. , fSizes(std::move(sizes))
  32. , fTypeOwner(typeOwner) {}
  33. std::unique_ptr<ProgramElement> clone() const override {
  34. std::vector<std::unique_ptr<Expression>> sizesClone;
  35. for (const auto& s : fSizes) {
  36. sizesClone.push_back(s->clone());
  37. }
  38. return std::unique_ptr<ProgramElement>(new InterfaceBlock(fOffset, &fVariable, fTypeName,
  39. fInstanceName,
  40. std::move(sizesClone),
  41. fTypeOwner));
  42. }
  43. String description() const override {
  44. String result = fVariable.fModifiers.description() + fTypeName + " {\n";
  45. const Type* structType = &fVariable.fType;
  46. while (structType->kind() == Type::kArray_Kind) {
  47. structType = &structType->componentType();
  48. }
  49. for (const auto& f : structType->fields()) {
  50. result += f.description() + "\n";
  51. }
  52. result += "}";
  53. if (fInstanceName.size()) {
  54. result += " " + fInstanceName;
  55. for (const auto& size : fSizes) {
  56. result += "[";
  57. if (size) {
  58. result += size->description();
  59. }
  60. result += "]";
  61. }
  62. }
  63. return result + ";";
  64. }
  65. const Variable& fVariable;
  66. const String fTypeName;
  67. const String fInstanceName;
  68. std::vector<std::unique_ptr<Expression>> fSizes;
  69. const std::shared_ptr<SymbolTable> fTypeOwner;
  70. typedef ProgramElement INHERITED;
  71. };
  72. } // namespace
  73. #endif