Coverage Report

Created: 2021-08-22 09:07

/src/skia/src/gpu/glsl/GrGLSLFragmentShaderBuilder.h
Line
Count
Source (jump to first uncovered line)
1
/*
2
 * Copyright 2014 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 GrGLSLFragmentShaderBuilder_DEFINED
9
#define GrGLSLFragmentShaderBuilder_DEFINED
10
11
#include "src/gpu/GrBlend.h"
12
#include "src/gpu/GrFragmentProcessor.h"
13
#include "src/gpu/GrProcessor.h"
14
#include "src/gpu/glsl/GrGLSLShaderBuilder.h"
15
16
class GrRenderTarget;
17
class GrGLSLVarying;
18
19
/*
20
 * This class is used by fragment processors to build their fragment code.
21
 */
22
class GrGLSLFPFragmentBuilder : virtual public GrGLSLShaderBuilder {
23
public:
24
    /** Appease the compiler; the derived class initializes GrGLSLShaderBuilder. */
25
0
    GrGLSLFPFragmentBuilder() : GrGLSLShaderBuilder(nullptr) {
26
        // Suppress unused warning error
27
0
        (void) fPadding;
28
0
    }
29
30
    enum class ScopeFlags {
31
        // Every fragment will always execute this code, and will do it exactly once.
32
        kTopLevel = 0,
33
        // Either all fragments in a given primitive, or none, will execute this code.
34
        kInsidePerPrimitiveBranch = (1 << 0),
35
        // Any given fragment may or may not execute this code.
36
        kInsidePerPixelBranch = (1 << 1),
37
        // This code will be executed more than once.
38
        kInsideLoop = (1 << 2)
39
    };
40
41
    void writeProcessorFunction(GrFragmentProcessor::ProgramImpl*,
42
                                GrFragmentProcessor::ProgramImpl::EmitArgs&);
43
44
    virtual void forceHighPrecision() = 0;
45
46
    /** Returns the variable name that holds the color of the destination pixel. This may be nullptr
47
     * if no effect advertised that it will read the destination. */
48
    virtual const char* dstColor() = 0;
49
50
private:
51
    /**
52
     * These are called before/after calling emitCode on a child proc to update mangling.
53
     */
54
    virtual void onBeforeChildProcEmitCode() = 0;
55
    virtual void onAfterChildProcEmitCode() = 0;
56
57
    virtual const SkString& getMangleString() const = 0;
58
59
    // WARNING: LIke GrRenderTargetProxy, changes to this can cause issues in ASAN. This is caused
60
    // by GrGLSLProgramBuilder's GrTBlockLists requiring 16 byte alignment, but since
61
    // GrGLSLFragmentShaderBuilder has a virtual diamond hierarchy, ASAN requires all this pointers
62
    // to start aligned, even though clang is already correctly offsetting the individual fields
63
    // that require the larger alignment. In the current world, this extra padding is sufficient to
64
    // correctly initialize GrGLSLXPFragmentBuilder second.
65
    char fPadding[4] = {};
66
};
67
68
GR_MAKE_BITFIELD_CLASS_OPS(GrGLSLFPFragmentBuilder::ScopeFlags)
69
70
/*
71
 * This class is used by Xfer processors to build their fragment code.
72
 */
73
class GrGLSLXPFragmentBuilder : virtual public GrGLSLShaderBuilder {
74
public:
75
    /** Appease the compiler; the derived class initializes GrGLSLShaderBuilder. */
76
0
    GrGLSLXPFragmentBuilder() : GrGLSLShaderBuilder(nullptr) {}
77
78
    virtual bool hasCustomColorOutput() const = 0;
79
    virtual bool hasSecondaryOutput() const = 0;
80
81
    /** Returns the variable name that holds the color of the destination pixel. This may be nullptr
82
     * if no effect advertised that it will read the destination. */
83
    virtual const char* dstColor() = 0;
84
85
    /** Adds any necessary layout qualifiers in order to legalize the supplied blend equation with
86
        this shader. It is only legal to call this method with an advanced blend equation, and only
87
        if these equations are supported. */
88
    virtual void enableAdvancedBlendEquationIfNeeded(GrBlendEquation) = 0;
89
};
90
91
/*
92
 * This class implements the various fragment builder interfaces.
93
 */
94
class GrGLSLFragmentShaderBuilder : public GrGLSLFPFragmentBuilder, public GrGLSLXPFragmentBuilder {
95
public:
96
    GrGLSLFragmentShaderBuilder(GrGLSLProgramBuilder* program);
97
98
    // Shared FP/XP interface.
99
    const char* dstColor() override;
100
101
    // GrGLSLFPFragmentBuilder interface.
102
0
    void forceHighPrecision() override { fForceHighPrecision = true; }
103
104
    // GrGLSLXPFragmentBuilder interface.
105
0
    bool hasCustomColorOutput() const override { return SkToBool(fCustomColorOutput); }
106
0
    bool hasSecondaryOutput() const override { return fHasSecondaryOutput; }
107
    void enableAdvancedBlendEquationIfNeeded(GrBlendEquation) override;
108
109
private:
110
    // GrGLSLFPFragmentBuilder private interface.
111
    void onBeforeChildProcEmitCode() override;
112
    void onAfterChildProcEmitCode() override;
113
0
    const SkString& getMangleString() const override { return fMangleString; }
114
115
    // Private public interface, used by GrGLProgramBuilder to build a fragment shader
116
    void enableCustomOutput();
117
    void enableSecondaryOutput();
118
    const char* getPrimaryColorOutputName() const;
119
    const char* getSecondaryColorOutputName() const;
120
    bool primaryColorOutputIsInOut() const;
121
122
#ifdef SK_DEBUG
123
    // As GLSLProcessors emit code, there are some conditions we need to verify.  We use the below
124
    // state to track this.  The reset call is called per processor emitted.
125
    bool fHasReadDstColorThisStage_DebugOnly = false;
126
127
0
    void debugOnly_resetPerStageVerification() {
128
0
        fHasReadDstColorThisStage_DebugOnly = false;
129
0
    }
130
#endif
131
132
0
    static const char* DeclaredColorOutputName() { return "sk_FragColor"; }
133
0
    static const char* DeclaredSecondaryColorOutputName() { return "fsSecondaryColorOut"; }
134
135
    GrSurfaceOrigin getSurfaceOrigin() const;
136
137
    void onFinalize() override;
138
139
    static constexpr const char kDstColorName[] = "_dstColor";
140
141
    /*
142
     * State that tracks which child proc in the proc tree is currently emitting code.  This is
143
     * used to update the fMangleString, which is used to mangle the names of uniforms and functions
144
     * emitted by the proc.  fSubstageIndices is a stack: its count indicates how many levels deep
145
     * we are in the tree, and its second-to-last value is the index of the child proc at that
146
     * level which is currently emitting code. For example, if fSubstageIndices = [3, 1, 2, 0], that
147
     * means we're currently emitting code for the base proc's 3rd child's 1st child's 2nd child.
148
     */
149
    SkTArray<int> fSubstageIndices;
150
151
    /*
152
     * The mangle string is used to mangle the names of uniforms/functions emitted by the child
153
     * procs so no duplicate uniforms/functions appear in the generated shader program. The mangle
154
     * string is simply based on fSubstageIndices. For example, if fSubstageIndices = [3, 1, 2, 0],
155
     * then the manglestring will be "_c3_c1_c2", and any uniform/function emitted by that proc will
156
     * have "_c3_c1_c2" appended to its name, which can be interpreted as "base proc's 3rd child's
157
     * 1st child's 2nd child".
158
     */
159
    SkString fMangleString;
160
161
    GrShaderVar* fCustomColorOutput = nullptr;
162
163
    bool fSetupFragPosition = false;
164
    bool fHasSecondaryOutput = false;
165
    bool fHasModifiedSampleMask = false;
166
    bool fForceHighPrecision = false;
167
168
    friend class GrGLSLProgramBuilder;
169
    friend class GrGLProgramBuilder;
170
    friend class GrVkPipelineStateBuilder;
171
};
172
173
#endif