• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 "include/gpu/GrTypes.h"
12 #include "include/private/base/SkDebug.h"
13 #include "src/gpu/ganesh/glsl/GrGLSLShaderBuilder.h"
14 
15 #include <cstdint>
16 
17 class GrGLSLProgramBuilder;
18 
19 namespace skgpu { enum class BlendEquation : uint8_t; }
20 
21 /*
22  * This class is used by fragment processors to build their fragment code.
23  */
24 class GrGLSLFPFragmentBuilder : virtual public GrGLSLShaderBuilder {
25 public:
26     /** Appease the compiler; the derived class initializes GrGLSLShaderBuilder. */
GrGLSLFPFragmentBuilder()27     GrGLSLFPFragmentBuilder() : GrGLSLShaderBuilder(nullptr) {
28         // Suppress unused warning error
29         (void) fPadding;
30     }
31 
32     enum class ScopeFlags {
33         // Every fragment will always execute this code, and will do it exactly once.
34         kTopLevel = 0,
35         // Either all fragments in a given primitive, or none, will execute this code.
36         kInsidePerPrimitiveBranch = (1 << 0),
37         // Any given fragment may or may not execute this code.
38         kInsidePerPixelBranch = (1 << 1),
39         // This code will be executed more than once.
40         kInsideLoop = (1 << 2)
41     };
42 
43     virtual void forceHighPrecision() = 0;
44 
45     /** Returns the variable name that holds the color of the destination pixel. This may be nullptr
46      * if no effect advertised that it will read the destination. */
47     virtual const char* dstColor() = 0;
48 
49 private:
50     // WARNING: LIke GrRenderTargetProxy, changes to this can cause issues in ASAN. This is caused
51     // by GrGLSLProgramBuilder's SkTBlockLists requiring 16 byte alignment, but since
52     // GrGLSLFragmentShaderBuilder has a virtual diamond hierarchy, ASAN requires all this pointers
53     // to start aligned, even though clang is already correctly offsetting the individual fields
54     // that require the larger alignment. In the current world, this extra padding is sufficient to
55     // correctly initialize GrGLSLXPFragmentBuilder second.
56     char fPadding[4] = {};
57 };
58 
GR_MAKE_BITFIELD_CLASS_OPS(GrGLSLFPFragmentBuilder::ScopeFlags)59 GR_MAKE_BITFIELD_CLASS_OPS(GrGLSLFPFragmentBuilder::ScopeFlags)
60 
61 /*
62  * This class is used by Xfer processors to build their fragment code.
63  */
64 class GrGLSLXPFragmentBuilder : virtual public GrGLSLShaderBuilder {
65 public:
66     /** Appease the compiler; the derived class initializes GrGLSLShaderBuilder. */
67     GrGLSLXPFragmentBuilder() : GrGLSLShaderBuilder(nullptr) {}
68 
69     virtual bool hasSecondaryOutput() const = 0;
70 
71     /** Returns the variable name that holds the color of the destination pixel. This may be nullptr
72      * if no effect advertised that it will read the destination. */
73     virtual const char* dstColor() = 0;
74 
75     /** Adds any necessary layout qualifiers in order to legalize the supplied blend equation with
76         this shader. It is only legal to call this method with an advanced blend equation, and only
77         if these equations are supported. */
78     virtual void enableAdvancedBlendEquationIfNeeded(skgpu::BlendEquation) = 0;
79 };
80 
81 /*
82  * This class implements the various fragment builder interfaces.
83  */
84 class GrGLSLFragmentShaderBuilder : public GrGLSLFPFragmentBuilder, public GrGLSLXPFragmentBuilder {
85 public:
86     GrGLSLFragmentShaderBuilder(GrGLSLProgramBuilder* program);
87 
88     // Shared FP/XP interface.
89     const char* dstColor() override;
90 
91     // GrGLSLFPFragmentBuilder interface.
forceHighPrecision()92     void forceHighPrecision() override { fForceHighPrecision = true; }
93 
94     // GrGLSLXPFragmentBuilder interface.
hasSecondaryOutput()95     bool hasSecondaryOutput() const override { return fHasSecondaryOutput; }
96     void enableAdvancedBlendEquationIfNeeded(skgpu::BlendEquation) override;
97 
98 private:
99     // Private public interface, used by GrGLProgramBuilder to build a fragment shader
100     void enableSecondaryOutput();
101     const char* getPrimaryColorOutputName() const;
102     const char* getSecondaryColorOutputName() const;
103     bool primaryColorOutputIsInOut() const;
104 
105 #ifdef SK_DEBUG
106     // As GLSLProcessors emit code, there are some conditions we need to verify.  We use the below
107     // state to track this.  The reset call is called per processor emitted.
108     bool fHasReadDstColorThisStage_DebugOnly = false;
109 
debugOnly_resetPerStageVerification()110     void debugOnly_resetPerStageVerification() {
111         fHasReadDstColorThisStage_DebugOnly = false;
112     }
113 #endif
114 
DeclaredColorOutputName()115     static const char* DeclaredColorOutputName() { return "sk_FragColor"; }
DeclaredSecondaryColorOutputName()116     static const char* DeclaredSecondaryColorOutputName() { return "fsSecondaryColorOut"; }
117 
118     GrSurfaceOrigin getSurfaceOrigin() const;
119 
120     void onFinalize() override;
121 
122     inline static constexpr const char kDstColorName[] = "_dstColor";
123 
124     bool fPrimaryColorIsInOut = false;
125     bool fSetupFragPosition = false;
126     bool fHasSecondaryOutput = false;
127     bool fHasModifiedSampleMask = false;
128     bool fForceHighPrecision = false;
129 
130     friend class GrGLSLProgramBuilder;
131     friend class GrGLProgramBuilder;
132     friend class GrVkPipelineStateBuilder;
133 };
134 
135 #endif
136