summaryrefslogtreecommitdiffstats
path: root/gfx/skia/skia/src/sksl/ir/SkSLInterfaceBlock.h
blob: a9447dd9cb5ff758092c478befb4c16bec4a2e3e (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
/*
 * Copyright 2016 Google Inc.
 *
 * Use of this source code is governed by a BSD-style license that can be
 * found in the LICENSE file.
 */

#ifndef SKSL_INTERFACEBLOCK
#define SKSL_INTERFACEBLOCK

#include "include/core/SkTypes.h"
#include "include/private/SkSLIRNode.h"
#include "include/private/SkSLProgramElement.h"
#include "include/sksl/SkSLPosition.h"
#include "src/sksl/ir/SkSLType.h"
#include "src/sksl/ir/SkSLVariable.h"

#include <memory>
#include <optional>
#include <string>
#include <string_view>
#include <utility>

namespace SkSL {

class Context;
class SymbolTable;

/**
 * An interface block, as in:
 *
 * out sk_PerVertex {
 *   layout(builtin=0) float4 sk_Position;
 *   layout(builtin=1) float sk_PointSize;
 * };
 *
 * At the IR level, this is represented by a single variable of struct type.
 */
class InterfaceBlock final : public ProgramElement {
public:
    inline static constexpr Kind kIRNodeKind = Kind::kInterfaceBlock;

    InterfaceBlock(Position pos,
                   Variable* var,
                   std::shared_ptr<SymbolTable> typeOwner)
            : INHERITED(pos, kIRNodeKind)
            , fVariable(var)
            , fTypeOwner(std::move(typeOwner)) {
        SkASSERT(fVariable->type().componentType().isInterfaceBlock());
        fVariable->setInterfaceBlock(this);
    }

    ~InterfaceBlock() override;

    // Returns an InterfaceBlock; errors are reported to the ErrorReporter.
    // The caller is responsible for adding the InterfaceBlock to the program elements.
    // The program's RTAdjustData will be updated if the InterfaceBlock contains sk_RTAdjust.
    // The passed-in symbol table will be updated with a reference to the interface block variable
    // (if it is named) or each of the interface block fields (if it is anonymous).
    static std::unique_ptr<InterfaceBlock> Convert(const Context& context,
                                                   Position pos,
                                                   Variable* variable,
                                                   std::shared_ptr<SymbolTable> symbols);

    // Returns an InterfaceBlock; errors are reported via SkASSERT.
    // The caller is responsible for adding the InterfaceBlock to the program elements.
    // If the InterfaceBlock contains sk_RTAdjust, the caller is responsible for passing its field
    // index in `rtAdjustIndex`.
    // The passed-in symbol table will be updated with a reference to the interface block variable
    // (if it is named) or each of the interface block fields (if it is anonymous).
    static std::unique_ptr<InterfaceBlock> Make(const Context& context,
                                                Position pos,
                                                Variable* variable,
                                                std::optional<int> rtAdjustIndex,
                                                std::shared_ptr<SymbolTable> symbols);

    Variable* var() const {
        return fVariable;
    }

    void detachDeadVariable() {
        fVariable = nullptr;
    }

    std::string_view typeName() const {
        return fVariable->type().componentType().name();
    }

    std::string_view instanceName() const {
        return fVariable->name();
    }

    const std::shared_ptr<SymbolTable>& typeOwner() const {
        return fTypeOwner;
    }

    int arraySize() const {
        return fVariable->type().isArray() ? fVariable->type().columns() : 0;
    }

    std::unique_ptr<ProgramElement> clone() const override;

    std::string description() const override;

private:
    Variable* fVariable;
    std::shared_ptr<SymbolTable> fTypeOwner;

    using INHERITED = ProgramElement;
};

}  // namespace SkSL

#endif