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