• 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 "GrBlend.h"
12 #include "GrGLSLShaderBuilder.h"
13 #include "GrProcessor.h"
14 
15 class GrRenderTarget;
16 class GrGLSLVarying;
17 
18 /*
19  * This base class encapsulates the common functionality which all processors use to build fragment
20  * shaders.
21  */
22 class GrGLSLFragmentBuilder : public GrGLSLShaderBuilder {
23 public:
GrGLSLFragmentBuilder(GrGLSLProgramBuilder * program)24     GrGLSLFragmentBuilder(GrGLSLProgramBuilder* program) : INHERITED(program) {}
~GrGLSLFragmentBuilder()25     virtual ~GrGLSLFragmentBuilder() {}
26 
27     /**
28      * Use of these features may require a GLSL extension to be enabled. Shaders may not compile
29      * if code is added that uses one of these features without calling enableFeature()
30      */
31     enum GLSLFeature {
32         kMultisampleInterpolation_GLSLFeature
33     };
34 
35     /**
36      * If the feature is supported then true is returned and any necessary #extension declarations
37      * are added to the shaders. If the feature is not supported then false will be returned.
38      */
39     virtual bool enableFeature(GLSLFeature) = 0;
40 
41     /**
42      * This returns a variable name to access the 2D, perspective correct version of the coords in
43      * the fragment shader. The passed in coordinates must either be of type kHalf2 or kHalf3. If
44      * the coordinates are 3-dimensional, it a perspective divide into is emitted into the
45      * fragment shader (xy / z) to convert them to 2D.
46      */
47     virtual SkString ensureCoords2D(const GrShaderVar&) = 0;
48 
49     // TODO: remove this method.
50     void declAppendf(const char* fmt, ...);
51 
52 private:
53     typedef GrGLSLShaderBuilder INHERITED;
54 };
55 
56 /*
57  * This class is used by fragment processors to build their fragment code.
58  */
59 class GrGLSLFPFragmentBuilder : virtual public GrGLSLFragmentBuilder {
60 public:
61     /** Appease the compiler; the derived class initializes GrGLSLFragmentBuilder. */
GrGLSLFPFragmentBuilder()62     GrGLSLFPFragmentBuilder() : GrGLSLFragmentBuilder(nullptr) {}
63 
64     enum Coordinates {
65         kSkiaDevice_Coordinates,
66         kGLSLWindow_Coordinates,
67 
68         kLast_Coordinates = kGLSLWindow_Coordinates
69     };
70 
71     /**
72      * Appends the offset from the center of the pixel to a specified sample.
73      *
74      * @param sampleIdx      GLSL expression of the sample index.
75      * @param Coordinates    Coordinate space in which to emit the offset.
76      *
77      * A processor must call setWillUseSampleLocations in its constructor before using this method.
78      */
79     virtual void appendOffsetToSample(const char* sampleIdx, Coordinates) = 0;
80 
81     /**
82      * Subtracts sample coverage from the fragment. Any sample whose corresponding bit is not found
83      * in the mask will not be written out to the framebuffer.
84      *
85      * @param mask      int that contains the sample mask. Bit N corresponds to the Nth sample.
86      * @param invert    perform a bit-wise NOT on the provided mask before applying it?
87      *
88      * Requires GLSL support for sample variables.
89      */
90     virtual void maskSampleCoverage(const char* mask, bool invert = false) = 0;
91 
92     /**
93      * Fragment procs with child procs should call these functions before/after calling emitCode
94      * on a child proc.
95      */
96     virtual void onBeforeChildProcEmitCode() = 0;
97     virtual void onAfterChildProcEmitCode() = 0;
98 
99     virtual const SkString& getMangleString() const = 0;
100 
101     virtual void forceHighPrecision() = 0;
102 };
103 
104 /*
105  * This class is used by primitive processors to build their fragment code.
106  */
107 class GrGLSLPPFragmentBuilder : public GrGLSLFPFragmentBuilder {
108 public:
109     /** Appease the compiler; the derived class initializes GrGLSLFragmentBuilder. */
GrGLSLPPFragmentBuilder()110     GrGLSLPPFragmentBuilder() : GrGLSLFragmentBuilder(nullptr) {}
111 
112     /**
113      * Overrides the fragment's sample coverage. The provided mask determines which samples will now
114      * be written out to the framebuffer. Note that this mask can be reduced by a future call to
115      * maskSampleCoverage.
116      *
117      * If a primitive processor uses this method, it must guarantee that every codepath through the
118      * shader overrides the sample mask at some point.
119      *
120      * @param mask    int that contains the new coverage mask. Bit N corresponds to the Nth sample.
121      *
122      * Requires NV_sample_mask_override_coverage.
123      */
124     virtual void overrideSampleCoverage(const char* mask) = 0;
125 };
126 
127 /*
128  * This class is used by Xfer processors to build their fragment code.
129  */
130 class GrGLSLXPFragmentBuilder : virtual public GrGLSLFragmentBuilder {
131 public:
132     /** Appease the compiler; the derived class initializes GrGLSLFragmentBuilder. */
GrGLSLXPFragmentBuilder()133     GrGLSLXPFragmentBuilder() : GrGLSLFragmentBuilder(nullptr) {}
134 
135     virtual bool hasCustomColorOutput() const = 0;
136     virtual bool hasSecondaryOutput() const = 0;
137 
138     /** Returns the variable name that holds the color of the destination pixel. This may be nullptr
139      * if no effect advertised that it will read the destination. */
140     virtual const char* dstColor() = 0;
141 
142     /** Adds any necessary layout qualifiers in order to legalize the supplied blend equation with
143         this shader. It is only legal to call this method with an advanced blend equation, and only
144         if these equations are supported. */
145     virtual void enableAdvancedBlendEquationIfNeeded(GrBlendEquation) = 0;
146 };
147 
148 /*
149  * This class implements the various fragment builder interfaces.
150  */
151 class GrGLSLFragmentShaderBuilder : public GrGLSLPPFragmentBuilder, public GrGLSLXPFragmentBuilder {
152 public:
153    /** Returns a nonzero key for a surface's origin. This should only be called if a processor will
154        use the fragment position and/or sample locations. */
155     static uint8_t KeyForSurfaceOrigin(GrSurfaceOrigin);
156 
157     GrGLSLFragmentShaderBuilder(GrGLSLProgramBuilder* program);
158 
159     // Shared GrGLSLFragmentBuilder interface.
160     bool enableFeature(GLSLFeature) override;
161     virtual SkString ensureCoords2D(const GrShaderVar&) override;
162 
163     // GrGLSLFPFragmentBuilder interface.
164     void appendOffsetToSample(const char* sampleIdx, Coordinates) override;
165     void maskSampleCoverage(const char* mask, bool invert = false) override;
166     void overrideSampleCoverage(const char* mask) override;
getMangleString()167     const SkString& getMangleString() const override { return fMangleString; }
168     void onBeforeChildProcEmitCode() override;
169     void onAfterChildProcEmitCode() override;
forceHighPrecision()170     void forceHighPrecision() override { fForceHighPrecision = true; }
171 
172     // GrGLSLXPFragmentBuilder interface.
hasCustomColorOutput()173     bool hasCustomColorOutput() const override { return fHasCustomColorOutput; }
hasSecondaryOutput()174     bool hasSecondaryOutput() const override { return fHasSecondaryOutput; }
175     const char* dstColor() override;
176     void enableAdvancedBlendEquationIfNeeded(GrBlendEquation) override;
177 
178 private:
179     // Private public interface, used by GrGLProgramBuilder to build a fragment shader
180     void enableCustomOutput();
181     void enableSecondaryOutput();
182     const char* getPrimaryColorOutputName() const;
183     const char* getSecondaryColorOutputName() const;
184     bool primaryColorOutputIsInOut() const;
185 
186 #ifdef SK_DEBUG
187     // As GLSLProcessors emit code, there are some conditions we need to verify.  We use the below
188     // state to track this.  The reset call is called per processor emitted.
usedProcessorFeatures()189     GrProcessor::RequiredFeatures usedProcessorFeatures() const { return fUsedProcessorFeatures; }
hasReadDstColor()190     bool hasReadDstColor() const { return fHasReadDstColor; }
resetVerification()191     void resetVerification() {
192         fUsedProcessorFeatures = GrProcessor::kNone_RequiredFeatures;
193         fHasReadDstColor = false;
194     }
195 #endif
196 
DeclaredColorOutputName()197     static const char* DeclaredColorOutputName() { return "sk_FragColor"; }
DeclaredSecondaryColorOutputName()198     static const char* DeclaredSecondaryColorOutputName() { return "fsSecondaryColorOut"; }
199 
200     GrSurfaceOrigin getSurfaceOrigin() const;
201 
202     void onFinalize() override;
203     void defineSampleOffsetArray(const char* name, const SkMatrix&);
204 
205     static const char* kDstColorName;
206 
207     /*
208      * State that tracks which child proc in the proc tree is currently emitting code.  This is
209      * used to update the fMangleString, which is used to mangle the names of uniforms and functions
210      * emitted by the proc.  fSubstageIndices is a stack: its count indicates how many levels deep
211      * we are in the tree, and its second-to-last value is the index of the child proc at that
212      * level which is currently emitting code. For example, if fSubstageIndices = [3, 1, 2, 0], that
213      * means we're currently emitting code for the base proc's 3rd child's 1st child's 2nd child.
214      */
215     SkTArray<int> fSubstageIndices;
216 
217     /*
218      * The mangle string is used to mangle the names of uniforms/functions emitted by the child
219      * procs so no duplicate uniforms/functions appear in the generated shader program. The mangle
220      * string is simply based on fSubstageIndices. For example, if fSubstageIndices = [3, 1, 2, 0],
221      * then the manglestring will be "_c3_c1_c2", and any uniform/function emitted by that proc will
222      * have "_c3_c1_c2" appended to its name, which can be interpreted as "base proc's 3rd child's
223      * 1st child's 2nd child".
224      */
225     SkString fMangleString;
226 
227     bool          fSetupFragPosition;
228     bool          fHasCustomColorOutput;
229     int           fCustomColorOutputIndex;
230     bool          fHasSecondaryOutput;
231     uint8_t       fUsedSampleOffsetArrays;
232     bool          fHasInitializedSampleMask;
233     bool          fForceHighPrecision;
234 
235 #ifdef SK_DEBUG
236     // some state to verify shaders and effects are consistent, this is reset between effects by
237     // the program creator
238     GrProcessor::RequiredFeatures fUsedProcessorFeatures;
239     bool fHasReadDstColor;
240 #endif
241 
242     friend class GrGLSLProgramBuilder;
243     friend class GrGLProgramBuilder;
244 };
245 
246 #endif
247