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 8 #ifndef SKSL_INTERFACEBLOCK 9 #define SKSL_INTERFACEBLOCK 10 11 #include <memory> 12 13 #include "include/core/SkStringView.h" 14 #include "include/private/SkSLProgramElement.h" 15 #include "src/sksl/ir/SkSLSymbolTable.h" 16 #include "src/sksl/ir/SkSLVarDeclarations.h" 17 18 namespace SkSL { 19 20 /** 21 * An interface block, as in: 22 * 23 * out sk_PerVertex { 24 * layout(builtin=0) float4 sk_Position; 25 * layout(builtin=1) float sk_PointSize; 26 * }; 27 * 28 * At the IR level, this is represented by a single variable of struct type. 29 */ 30 class InterfaceBlock final : public ProgramElement { 31 public: 32 inline static constexpr Kind kProgramElementKind = Kind::kInterfaceBlock; 33 InterfaceBlock(int line,const Variable & var,skstd::string_view typeName,skstd::string_view instanceName,int arraySize,std::shared_ptr<SymbolTable> typeOwner)34 InterfaceBlock(int line, const Variable& var, skstd::string_view typeName, 35 skstd::string_view instanceName, int arraySize, 36 std::shared_ptr<SymbolTable> typeOwner) 37 : INHERITED(line, kProgramElementKind) 38 , fVariable(var) 39 , fTypeName(typeName) 40 , fInstanceName(instanceName) 41 , fArraySize(arraySize) 42 , fTypeOwner(std::move(typeOwner)) {} 43 variable()44 const Variable& variable() const { 45 return fVariable; 46 } 47 typeName()48 skstd::string_view typeName() const { 49 return fTypeName; 50 } 51 instanceName()52 skstd::string_view instanceName() const { 53 return fInstanceName; 54 } 55 typeOwner()56 const std::shared_ptr<SymbolTable>& typeOwner() const { 57 return fTypeOwner; 58 } 59 arraySize()60 int arraySize() const { 61 return fArraySize; 62 } 63 clone()64 std::unique_ptr<ProgramElement> clone() const override { 65 return std::make_unique<InterfaceBlock>(fLine, this->variable(), this->typeName(), 66 this->instanceName(), this->arraySize(), 67 SymbolTable::WrapIfBuiltin(this->typeOwner())); 68 } 69 description()70 String description() const override { 71 String result = this->variable().modifiers().description() + this->typeName() + " {\n"; 72 const Type* structType = &this->variable().type(); 73 if (structType->isArray()) { 74 structType = &structType->componentType(); 75 } 76 for (const auto& f : structType->fields()) { 77 result += f.description() + "\n"; 78 } 79 result += "}"; 80 if (!this->instanceName().empty()) { 81 result += " " + this->instanceName(); 82 if (this->arraySize() > 0) { 83 result.appendf("[%d]", this->arraySize()); 84 } 85 } 86 return result + ";"; 87 } 88 89 private: 90 const Variable& fVariable; 91 skstd::string_view fTypeName; 92 skstd::string_view fInstanceName; 93 int fArraySize; 94 std::shared_ptr<SymbolTable> fTypeOwner; 95 96 using INHERITED = ProgramElement; 97 }; 98 99 } // namespace SkSL 100 101 #endif 102