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