blob: 73fe51f171f1bc22b564b23f6a99fdd6e969f96b [file] [log] [blame]
joshualitt30ba4362014-08-21 20:18:45 -07001/*
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
egdaniel2d721d32015-11-11 13:06:05 -08008#ifndef GrGLSLFragmentShaderBuilder_DEFINED
9#define GrGLSLFragmentShaderBuilder_DEFINED
joshualitt47bb3822014-10-07 16:43:25 -070010
Brian Salomon99938a82016-11-21 13:41:08 -050011#include "GrBlend.h"
egdaniel2d721d32015-11-11 13:06:05 -080012#include "GrGLSLShaderBuilder.h"
cdalton87332102016-02-26 12:22:02 -080013#include "GrProcessor.h"
egdaniel7dc4bd02015-10-29 07:57:01 -070014
egdaniel574a4c12015-11-02 06:22:44 -080015class GrRenderTarget;
egdaniel8dcdedc2015-11-11 06:27:20 -080016class GrGLSLVarying;
joshualitt74077b92014-10-24 11:26:03 -070017
joshualittb0a8a372014-09-23 09:50:21 -070018/*
cdalton85285412016-02-18 12:37:07 -080019 * This base class encapsulates the common functionality which all processors use to build fragment
20 * shaders.
joshualittb0a8a372014-09-23 09:50:21 -070021 */
egdaniel2d721d32015-11-11 13:06:05 -080022class GrGLSLFragmentBuilder : public GrGLSLShaderBuilder {
joshualittb0a8a372014-09-23 09:50:21 -070023public:
cdalton85285412016-02-18 12:37:07 -080024 GrGLSLFragmentBuilder(GrGLSLProgramBuilder* program) : INHERITED(program) {}
egdaniel2d721d32015-11-11 13:06:05 -080025 virtual ~GrGLSLFragmentBuilder() {}
cdalton85285412016-02-18 12:37:07 -080026
joshualittb0a8a372014-09-23 09:50:21 -070027 /**
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 {
cdalton4a98cdb2016-03-01 12:12:20 -080032 kMultisampleInterpolation_GLSLFeature
joshualittb0a8a372014-09-23 09:50:21 -070033 };
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
Brian Salomon1d816b92017-08-17 11:07:59 -040043 * the fragment shader. The passed in coordinates must either be of type kVec2f or kVec3f. If
bsalomon1a1aa932016-09-12 09:30:36 -070044 * the coordinates are 3-dimensional, it a perspective divide into is emitted into the
45 * fragment shader (xy / z) to convert them to 2D.
joshualittb0a8a372014-09-23 09:50:21 -070046 */
bsalomon1a1aa932016-09-12 09:30:36 -070047 virtual SkString ensureCoords2D(const GrShaderVar&) = 0;
joshualittb0a8a372014-09-23 09:50:21 -070048
cdalton85285412016-02-18 12:37:07 -080049 // TODO: remove this method.
ethannicholas22793252016-01-30 09:59:10 -080050 void declAppendf(const char* fmt, ...);
51
joshualittb0a8a372014-09-23 09:50:21 -070052private:
egdaniel2d721d32015-11-11 13:06:05 -080053 typedef GrGLSLShaderBuilder INHERITED;
joshualittb0a8a372014-09-23 09:50:21 -070054};
55
56/*
cdalton85285412016-02-18 12:37:07 -080057 * This class is used by fragment processors to build their fragment code.
joshualittb0a8a372014-09-23 09:50:21 -070058 */
cdalton85285412016-02-18 12:37:07 -080059class GrGLSLFPFragmentBuilder : virtual public GrGLSLFragmentBuilder {
joshualittb0a8a372014-09-23 09:50:21 -070060public:
cdalton85285412016-02-18 12:37:07 -080061 /** Appease the compiler; the derived class initializes GrGLSLFragmentBuilder. */
62 GrGLSLFPFragmentBuilder() : GrGLSLFragmentBuilder(nullptr) {}
joshualitt47bb3822014-10-07 16:43:25 -070063
cdalton28f45b92016-03-07 13:58:26 -080064 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
cdalton85285412016-02-18 12:37:07 -080081 /**
cdalton33ad7012016-02-22 07:55:44 -080082 * 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 /**
Brian Salomon1d816b92017-08-17 11:07:59 -040093 * Overrides the default precision for the entire fragment program. Processors that require
94 * high precision input (eg from incoming texture samples) may use this. For calculations that
95 * are limited to a single processor's code, it is better to annotate individual declarations.
96 */
97 virtual void elevateDefaultPrecision(GrSLPrecision) = 0;
98
99 /**
cdalton85285412016-02-18 12:37:07 -0800100 * Fragment procs with child procs should call these functions before/after calling emitCode
101 * on a child proc.
102 */
103 virtual void onBeforeChildProcEmitCode() = 0;
104 virtual void onAfterChildProcEmitCode() = 0;
105
106 virtual const SkString& getMangleString() const = 0;
107};
108
109/*
110 * This class is used by primitive processors to build their fragment code.
111 */
112class GrGLSLPPFragmentBuilder : public GrGLSLFPFragmentBuilder {
113public:
114 /** Appease the compiler; the derived class initializes GrGLSLFragmentBuilder. */
115 GrGLSLPPFragmentBuilder() : GrGLSLFragmentBuilder(nullptr) {}
cdalton33ad7012016-02-22 07:55:44 -0800116
117 /**
118 * Overrides the fragment's sample coverage. The provided mask determines which samples will now
119 * be written out to the framebuffer. Note that this mask can be reduced by a future call to
120 * maskSampleCoverage.
121 *
122 * If a primitive processor uses this method, it must guarantee that every codepath through the
123 * shader overrides the sample mask at some point.
124 *
125 * @param mask int that contains the new coverage mask. Bit N corresponds to the Nth sample.
126 *
127 * Requires NV_sample_mask_override_coverage.
128 */
129 virtual void overrideSampleCoverage(const char* mask) = 0;
cdalton85285412016-02-18 12:37:07 -0800130};
131
132/*
133 * This class is used by Xfer processors to build their fragment code.
134 */
135class GrGLSLXPFragmentBuilder : virtual public GrGLSLFragmentBuilder {
136public:
137 /** Appease the compiler; the derived class initializes GrGLSLFragmentBuilder. */
138 GrGLSLXPFragmentBuilder() : GrGLSLFragmentBuilder(nullptr) {}
139
140 virtual bool hasCustomColorOutput() const = 0;
141 virtual bool hasSecondaryOutput() const = 0;
142
143 /** Returns the variable name that holds the color of the destination pixel. This may be nullptr
144 * if no effect advertised that it will read the destination. */
joshualittb0a8a372014-09-23 09:50:21 -0700145 virtual const char* dstColor() = 0;
146
cdalton8917d622015-05-06 13:40:21 -0700147 /** Adds any necessary layout qualifiers in order to legalize the supplied blend equation with
148 this shader. It is only legal to call this method with an advanced blend equation, and only
149 if these equations are supported. */
150 virtual void enableAdvancedBlendEquationIfNeeded(GrBlendEquation) = 0;
joshualittb0a8a372014-09-23 09:50:21 -0700151};
152
cdalton85285412016-02-18 12:37:07 -0800153/*
154 * This class implements the various fragment builder interfaces.
155 */
156class GrGLSLFragmentShaderBuilder : public GrGLSLPPFragmentBuilder, public GrGLSLXPFragmentBuilder {
joshualitt30ba4362014-08-21 20:18:45 -0700157public:
cdalton28f45b92016-03-07 13:58:26 -0800158 /** Returns a nonzero key for a surface's origin. This should only be called if a processor will
159 use the fragment position and/or sample locations. */
160 static uint8_t KeyForSurfaceOrigin(GrSurfaceOrigin);
joshualitt30ba4362014-08-21 20:18:45 -0700161
cdalton28f45b92016-03-07 13:58:26 -0800162 GrGLSLFragmentShaderBuilder(GrGLSLProgramBuilder* program);
joshualitt30ba4362014-08-21 20:18:45 -0700163
cdalton85285412016-02-18 12:37:07 -0800164 // Shared GrGLSLFragmentBuilder interface.
mtklein36352bf2015-03-25 18:17:31 -0700165 bool enableFeature(GLSLFeature) override;
bsalomon1a1aa932016-09-12 09:30:36 -0700166 virtual SkString ensureCoords2D(const GrShaderVar&) override;
joshualittdb0d3ca2014-10-07 12:42:26 -0700167
cdalton85285412016-02-18 12:37:07 -0800168 // GrGLSLFPFragmentBuilder interface.
cdalton28f45b92016-03-07 13:58:26 -0800169 void appendOffsetToSample(const char* sampleIdx, Coordinates) override;
cdalton33ad7012016-02-22 07:55:44 -0800170 void maskSampleCoverage(const char* mask, bool invert = false) override;
171 void overrideSampleCoverage(const char* mask) override;
Brian Salomon1d816b92017-08-17 11:07:59 -0400172 void elevateDefaultPrecision(GrSLPrecision) override;
cdalton85285412016-02-18 12:37:07 -0800173 const SkString& getMangleString() const override { return fMangleString; }
174 void onBeforeChildProcEmitCode() override;
175 void onAfterChildProcEmitCode() override;
176
177 // GrGLSLXPFragmentBuilder interface.
178 bool hasCustomColorOutput() const override { return fHasCustomColorOutput; }
179 bool hasSecondaryOutput() const override { return fHasSecondaryOutput; }
180 const char* dstColor() override;
cdalton8917d622015-05-06 13:40:21 -0700181 void enableAdvancedBlendEquationIfNeeded(GrBlendEquation) override;
182
joshualitt74077b92014-10-24 11:26:03 -0700183private:
joshualitt47bb3822014-10-07 16:43:25 -0700184 // Private public interface, used by GrGLProgramBuilder to build a fragment shader
joshualitt47bb3822014-10-07 16:43:25 -0700185 void enableCustomOutput();
186 void enableSecondaryOutput();
187 const char* getPrimaryColorOutputName() const;
188 const char* getSecondaryColorOutputName() const;
joshualitt47bb3822014-10-07 16:43:25 -0700189
cdalton87332102016-02-26 12:22:02 -0800190#ifdef SK_DEBUG
egdaniel57d3b032015-11-13 11:57:27 -0800191 // As GLSLProcessors emit code, there are some conditions we need to verify. We use the below
joshualitt47bb3822014-10-07 16:43:25 -0700192 // state to track this. The reset call is called per processor emitted.
cdalton87332102016-02-26 12:22:02 -0800193 GrProcessor::RequiredFeatures usedProcessorFeatures() const { return fUsedProcessorFeatures; }
joshualitt47bb3822014-10-07 16:43:25 -0700194 bool hasReadDstColor() const { return fHasReadDstColor; }
cdalton87332102016-02-26 12:22:02 -0800195 void resetVerification() {
196 fUsedProcessorFeatures = GrProcessor::kNone_RequiredFeatures;
joshualitt47bb3822014-10-07 16:43:25 -0700197 fHasReadDstColor = false;
joshualitt47bb3822014-10-07 16:43:25 -0700198 }
cdalton87332102016-02-26 12:22:02 -0800199#endif
joshualitt30ba4362014-08-21 20:18:45 -0700200
ethannicholas5961bc92016-10-12 06:39:56 -0700201 static const char* DeclaredColorOutputName() { return "sk_FragColor"; }
egdaniel8dcdedc2015-11-11 06:27:20 -0800202 static const char* DeclaredSecondaryColorOutputName() { return "fsSecondaryColorOut"; }
203
cdalton28f45b92016-03-07 13:58:26 -0800204 GrSurfaceOrigin getSurfaceOrigin() const;
joshualitt74077b92014-10-24 11:26:03 -0700205
egdaniel574a4c12015-11-02 06:22:44 -0800206 void onFinalize() override;
cdalton28f45b92016-03-07 13:58:26 -0800207 void defineSampleOffsetArray(const char* name, const SkMatrix&);
joshualitt30ba4362014-08-21 20:18:45 -0700208
egdaniel138c2632016-08-17 10:59:00 -0700209 static const char* kDstColorName;
joshualitt47bb3822014-10-07 16:43:25 -0700210
cdalton85285412016-02-18 12:37:07 -0800211 /*
212 * State that tracks which child proc in the proc tree is currently emitting code. This is
213 * used to update the fMangleString, which is used to mangle the names of uniforms and functions
214 * emitted by the proc. fSubstageIndices is a stack: its count indicates how many levels deep
215 * we are in the tree, and its second-to-last value is the index of the child proc at that
216 * level which is currently emitting code. For example, if fSubstageIndices = [3, 1, 2, 0], that
217 * means we're currently emitting code for the base proc's 3rd child's 1st child's 2nd child.
218 */
219 SkTArray<int> fSubstageIndices;
220
221 /*
222 * The mangle string is used to mangle the names of uniforms/functions emitted by the child
223 * procs so no duplicate uniforms/functions appear in the generated shader program. The mangle
224 * string is simply based on fSubstageIndices. For example, if fSubstageIndices = [3, 1, 2, 0],
225 * then the manglestring will be "_c3_c1_c2", and any uniform/function emitted by that proc will
226 * have "_c3_c1_c2" appended to its name, which can be interpreted as "base proc's 3rd child's
227 * 1st child's 2nd child".
228 */
229 SkString fMangleString;
230
Brian Osman33aa2c72017-04-05 09:26:15 -0400231 bool fSetupFragPosition;
232 bool fHasCustomColorOutput;
233 int fCustomColorOutputIndex;
234 bool fHasSecondaryOutput;
235 uint8_t fUsedSampleOffsetArrays;
236 bool fHasInitializedSampleMask;
Brian Salomon1d816b92017-08-17 11:07:59 -0400237 GrSLPrecision fDefaultPrecision;
joshualitt30ba4362014-08-21 20:18:45 -0700238
cdalton87332102016-02-26 12:22:02 -0800239#ifdef SK_DEBUG
joshualitt47bb3822014-10-07 16:43:25 -0700240 // some state to verify shaders and effects are consistent, this is reset between effects by
241 // the program creator
cdalton87332102016-02-26 12:22:02 -0800242 GrProcessor::RequiredFeatures fUsedProcessorFeatures;
joshualitt47bb3822014-10-07 16:43:25 -0700243 bool fHasReadDstColor;
cdalton87332102016-02-26 12:22:02 -0800244#endif
joshualittfe1233c2014-10-07 12:16:35 -0700245
egdanielfa896322016-01-13 12:19:30 -0800246 friend class GrGLSLProgramBuilder;
joshualitt47bb3822014-10-07 16:43:25 -0700247 friend class GrGLProgramBuilder;
joshualitt30ba4362014-08-21 20:18:45 -0700248};
249
250#endif