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