blob: 6d5bfe862cdc9d4910987263c1d2f5b754439d34 [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 Salomon096b0912019-08-14 16:56:13 -040011#include "src/gpu/GrBlend.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050012#include "src/gpu/GrProcessor.h"
Ethan Nicholasc6dce5a2019-07-24 16:51:36 -040013#include "src/gpu/glsl/GrGLSLFragmentProcessor.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050014#include "src/gpu/glsl/GrGLSLShaderBuilder.h"
egdaniel7dc4bd02015-10-29 07:57:01 -070015
egdaniel574a4c12015-11-02 06:22:44 -080016class GrRenderTarget;
egdaniel8dcdedc2015-11-11 06:27:20 -080017class GrGLSLVarying;
joshualitt74077b92014-10-24 11:26:03 -070018
joshualittb0a8a372014-09-23 09:50:21 -070019/*
cdalton85285412016-02-18 12:37:07 -080020 * This base class encapsulates the common functionality which all processors use to build fragment
21 * shaders.
joshualittb0a8a372014-09-23 09:50:21 -070022 */
egdaniel2d721d32015-11-11 13:06:05 -080023class GrGLSLFragmentBuilder : public GrGLSLShaderBuilder {
joshualittb0a8a372014-09-23 09:50:21 -070024public:
cdalton85285412016-02-18 12:37:07 -080025 GrGLSLFragmentBuilder(GrGLSLProgramBuilder* program) : INHERITED(program) {}
egdaniel2d721d32015-11-11 13:06:05 -080026 virtual ~GrGLSLFragmentBuilder() {}
cdalton85285412016-02-18 12:37:07 -080027
joshualittb0a8a372014-09-23 09:50:21 -070028 /**
joshualittb0a8a372014-09-23 09:50:21 -070029 * This returns a variable name to access the 2D, perspective correct version of the coords in
Ethan Nicholasf7b88202017-09-18 14:10:39 -040030 * the fragment shader. The passed in coordinates must either be of type kHalf2 or kHalf3. If
bsalomon1a1aa932016-09-12 09:30:36 -070031 * the coordinates are 3-dimensional, it a perspective divide into is emitted into the
32 * fragment shader (xy / z) to convert them to 2D.
joshualittb0a8a372014-09-23 09:50:21 -070033 */
bsalomon1a1aa932016-09-12 09:30:36 -070034 virtual SkString ensureCoords2D(const GrShaderVar&) = 0;
joshualittb0a8a372014-09-23 09:50:21 -070035
cdalton85285412016-02-18 12:37:07 -080036 // TODO: remove this method.
ethannicholas22793252016-01-30 09:59:10 -080037 void declAppendf(const char* fmt, ...);
38
joshualittb0a8a372014-09-23 09:50:21 -070039private:
egdaniel2d721d32015-11-11 13:06:05 -080040 typedef GrGLSLShaderBuilder INHERITED;
joshualittb0a8a372014-09-23 09:50:21 -070041};
42
43/*
cdalton85285412016-02-18 12:37:07 -080044 * This class is used by fragment processors to build their fragment code.
joshualittb0a8a372014-09-23 09:50:21 -070045 */
cdalton85285412016-02-18 12:37:07 -080046class GrGLSLFPFragmentBuilder : virtual public GrGLSLFragmentBuilder {
joshualittb0a8a372014-09-23 09:50:21 -070047public:
cdalton85285412016-02-18 12:37:07 -080048 /** Appease the compiler; the derived class initializes GrGLSLFragmentBuilder. */
49 GrGLSLFPFragmentBuilder() : GrGLSLFragmentBuilder(nullptr) {}
joshualitt47bb3822014-10-07 16:43:25 -070050
Chris Daltond7291ba2019-03-07 14:17:03 -070051 /**
52 * Returns the variable name that holds the array of sample offsets from pixel center to each
53 * sample location. Before this is called, a processor must have advertised that it will use
54 * CustomFeatures::kSampleLocations.
55 */
56 virtual const char* sampleOffsets() = 0;
57
Chris Dalton0dffbab2019-03-27 13:08:50 -060058 enum class ScopeFlags {
59 // Every fragment will always execute this code, and will do it exactly once.
60 kTopLevel = 0,
61 // Either all fragments in a given primitive, or none, will execute this code.
62 kInsidePerPrimitiveBranch = (1 << 0),
63 // Any given fragment may or may not execute this code.
64 kInsidePerPixelBranch = (1 << 1),
65 // This code will be executed more than once.
66 kInsideLoop = (1 << 2)
Robert Phillips7f861922018-01-30 13:13:42 +000067 };
68
69 /**
Chris Daltond31b5e72019-02-26 18:02:16 -070070 * Subtracts multisample coverage by AND-ing the sample mask with the provided "mask".
71 * Sample N corresponds to bit "1 << N".
72 *
73 * If the given scope is "kTopLevel" and the sample mask has not yet been modified, this method
74 * assigns the sample mask in place rather than pre-initializing it to ~0 then AND-ing it.
75 *
76 * Requires MSAA and GLSL support for sample variables.
77 */
Chris Dalton0dffbab2019-03-27 13:08:50 -060078 virtual void maskOffMultisampleCoverage(const char* mask, ScopeFlags) = 0;
79
80 /**
81 * Turns off coverage at each sample where the implicit function fn > 0.
82 *
83 * The provided "fn" value represents the implicit function at pixel center. We then approximate
84 * the implicit at each sample by riding the gradient, "grad", linearly from pixel center to
85 * each sample location.
86 *
87 * If "grad" is null, we approximate the gradient using HW derivatives.
88 *
89 * Requires MSAA and GLSL support for sample variables. Also requires HW derivatives if not
90 * providing a gradient.
91 */
92 virtual void applyFnToMultisampleMask(const char* fn, const char* grad, ScopeFlags) = 0;
Chris Daltond31b5e72019-02-26 18:02:16 -070093
94 /**
cdalton85285412016-02-18 12:37:07 -080095 * Fragment procs with child procs should call these functions before/after calling emitCode
96 * on a child proc.
97 */
98 virtual void onBeforeChildProcEmitCode() = 0;
99 virtual void onAfterChildProcEmitCode() = 0;
100
Ethan Nicholasc6dce5a2019-07-24 16:51:36 -0400101 virtual SkString writeProcessorFunction(GrGLSLFragmentProcessor* fp,
102 GrGLSLFragmentProcessor::EmitArgs& args);
103
cdalton85285412016-02-18 12:37:07 -0800104 virtual const SkString& getMangleString() const = 0;
Ethan Nicholasf7b88202017-09-18 14:10:39 -0400105
106 virtual void forceHighPrecision() = 0;
cdalton85285412016-02-18 12:37:07 -0800107};
108
Chris Dalton0dffbab2019-03-27 13:08:50 -0600109GR_MAKE_BITFIELD_CLASS_OPS(GrGLSLFPFragmentBuilder::ScopeFlags);
110
cdalton85285412016-02-18 12:37:07 -0800111/*
cdalton85285412016-02-18 12:37:07 -0800112 * This class is used by Xfer processors to build their fragment code.
113 */
114class GrGLSLXPFragmentBuilder : virtual public GrGLSLFragmentBuilder {
115public:
116 /** Appease the compiler; the derived class initializes GrGLSLFragmentBuilder. */
117 GrGLSLXPFragmentBuilder() : GrGLSLFragmentBuilder(nullptr) {}
118
119 virtual bool hasCustomColorOutput() const = 0;
120 virtual bool hasSecondaryOutput() const = 0;
121
122 /** Returns the variable name that holds the color of the destination pixel. This may be nullptr
123 * if no effect advertised that it will read the destination. */
joshualittb0a8a372014-09-23 09:50:21 -0700124 virtual const char* dstColor() = 0;
125
cdalton8917d622015-05-06 13:40:21 -0700126 /** Adds any necessary layout qualifiers in order to legalize the supplied blend equation with
127 this shader. It is only legal to call this method with an advanced blend equation, and only
128 if these equations are supported. */
129 virtual void enableAdvancedBlendEquationIfNeeded(GrBlendEquation) = 0;
joshualittb0a8a372014-09-23 09:50:21 -0700130};
131
cdalton85285412016-02-18 12:37:07 -0800132/*
133 * This class implements the various fragment builder interfaces.
134 */
Chris Dalton60283612018-02-14 13:38:14 -0700135class GrGLSLFragmentShaderBuilder : public GrGLSLFPFragmentBuilder, public GrGLSLXPFragmentBuilder {
joshualitt30ba4362014-08-21 20:18:45 -0700136public:
Robert Phillips7f861922018-01-30 13:13:42 +0000137 /** Returns a nonzero key for a surface's origin. This should only be called if a processor will
138 use the fragment position and/or sample locations. */
139 static uint8_t KeyForSurfaceOrigin(GrSurfaceOrigin);
140
cdalton28f45b92016-03-07 13:58:26 -0800141 GrGLSLFragmentShaderBuilder(GrGLSLProgramBuilder* program);
joshualitt30ba4362014-08-21 20:18:45 -0700142
cdalton85285412016-02-18 12:37:07 -0800143 // Shared GrGLSLFragmentBuilder interface.
bsalomon1a1aa932016-09-12 09:30:36 -0700144 virtual SkString ensureCoords2D(const GrShaderVar&) override;
joshualittdb0d3ca2014-10-07 12:42:26 -0700145
cdalton85285412016-02-18 12:37:07 -0800146 // GrGLSLFPFragmentBuilder interface.
Chris Daltond7291ba2019-03-07 14:17:03 -0700147 const char* sampleOffsets() override;
Chris Dalton0dffbab2019-03-27 13:08:50 -0600148 void maskOffMultisampleCoverage(const char* mask, ScopeFlags) override;
149 void applyFnToMultisampleMask(const char* fn, const char* grad, ScopeFlags) override;
cdalton85285412016-02-18 12:37:07 -0800150 const SkString& getMangleString() const override { return fMangleString; }
151 void onBeforeChildProcEmitCode() override;
152 void onAfterChildProcEmitCode() override;
Ethan Nicholasf7b88202017-09-18 14:10:39 -0400153 void forceHighPrecision() override { fForceHighPrecision = true; }
cdalton85285412016-02-18 12:37:07 -0800154
155 // GrGLSLXPFragmentBuilder interface.
156 bool hasCustomColorOutput() const override { return fHasCustomColorOutput; }
157 bool hasSecondaryOutput() const override { return fHasSecondaryOutput; }
158 const char* dstColor() override;
cdalton8917d622015-05-06 13:40:21 -0700159 void enableAdvancedBlendEquationIfNeeded(GrBlendEquation) override;
160
joshualitt74077b92014-10-24 11:26:03 -0700161private:
Chris Daltond7291ba2019-03-07 14:17:03 -0700162 using CustomFeatures = GrProcessor::CustomFeatures;
163
joshualitt47bb3822014-10-07 16:43:25 -0700164 // Private public interface, used by GrGLProgramBuilder to build a fragment shader
joshualitt47bb3822014-10-07 16:43:25 -0700165 void enableCustomOutput();
166 void enableSecondaryOutput();
167 const char* getPrimaryColorOutputName() const;
168 const char* getSecondaryColorOutputName() const;
Brian Salomondc092132018-04-04 10:14:16 -0400169 bool primaryColorOutputIsInOut() const;
joshualitt47bb3822014-10-07 16:43:25 -0700170
cdalton87332102016-02-26 12:22:02 -0800171#ifdef SK_DEBUG
egdaniel57d3b032015-11-13 11:57:27 -0800172 // As GLSLProcessors emit code, there are some conditions we need to verify. We use the below
joshualitt47bb3822014-10-07 16:43:25 -0700173 // state to track this. The reset call is called per processor emitted.
Chris Daltond7291ba2019-03-07 14:17:03 -0700174 bool fHasReadDstColorThisStage_DebugOnly = false;
175 CustomFeatures fUsedProcessorFeaturesThisStage_DebugOnly = CustomFeatures::kNone;
176 CustomFeatures fUsedProcessorFeaturesAllStages_DebugOnly = CustomFeatures::kNone;
177
178 void debugOnly_resetPerStageVerification() {
179 fHasReadDstColorThisStage_DebugOnly = false;
180 fUsedProcessorFeaturesThisStage_DebugOnly = CustomFeatures::kNone;
joshualitt47bb3822014-10-07 16:43:25 -0700181 }
cdalton87332102016-02-26 12:22:02 -0800182#endif
joshualitt30ba4362014-08-21 20:18:45 -0700183
ethannicholas5961bc92016-10-12 06:39:56 -0700184 static const char* DeclaredColorOutputName() { return "sk_FragColor"; }
egdaniel8dcdedc2015-11-11 06:27:20 -0800185 static const char* DeclaredSecondaryColorOutputName() { return "fsSecondaryColorOut"; }
186
cdalton28f45b92016-03-07 13:58:26 -0800187 GrSurfaceOrigin getSurfaceOrigin() const;
joshualitt74077b92014-10-24 11:26:03 -0700188
egdaniel574a4c12015-11-02 06:22:44 -0800189 void onFinalize() override;
joshualitt30ba4362014-08-21 20:18:45 -0700190
egdaniel138c2632016-08-17 10:59:00 -0700191 static const char* kDstColorName;
joshualitt47bb3822014-10-07 16:43:25 -0700192
cdalton85285412016-02-18 12:37:07 -0800193 /*
194 * State that tracks which child proc in the proc tree is currently emitting code. This is
195 * used to update the fMangleString, which is used to mangle the names of uniforms and functions
196 * emitted by the proc. fSubstageIndices is a stack: its count indicates how many levels deep
197 * we are in the tree, and its second-to-last value is the index of the child proc at that
198 * level which is currently emitting code. For example, if fSubstageIndices = [3, 1, 2, 0], that
199 * means we're currently emitting code for the base proc's 3rd child's 1st child's 2nd child.
200 */
201 SkTArray<int> fSubstageIndices;
202
203 /*
204 * The mangle string is used to mangle the names of uniforms/functions emitted by the child
205 * procs so no duplicate uniforms/functions appear in the generated shader program. The mangle
206 * string is simply based on fSubstageIndices. For example, if fSubstageIndices = [3, 1, 2, 0],
207 * then the manglestring will be "_c3_c1_c2", and any uniform/function emitted by that proc will
208 * have "_c3_c1_c2" appended to its name, which can be interpreted as "base proc's 3rd child's
209 * 1st child's 2nd child".
210 */
211 SkString fMangleString;
212
Chris Daltond7291ba2019-03-07 14:17:03 -0700213 bool fSetupFragPosition = false;
214 bool fHasCustomColorOutput = false;
215 int fCustomColorOutputIndex = -1;
216 bool fHasSecondaryOutput = false;
Chris Dalton0dffbab2019-03-27 13:08:50 -0600217 bool fHasModifiedSampleMask = false;
Chris Daltond7291ba2019-03-07 14:17:03 -0700218 bool fForceHighPrecision = false;
joshualittfe1233c2014-10-07 12:16:35 -0700219
egdanielfa896322016-01-13 12:19:30 -0800220 friend class GrGLSLProgramBuilder;
joshualitt47bb3822014-10-07 16:43:25 -0700221 friend class GrGLProgramBuilder;
joshualitt30ba4362014-08-21 20:18:45 -0700222};
223
224#endif