blob: bf8569c073ecb47f94dd1b0a9817c08c0efc9433 [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
egdaniel2d721d32015-11-11 13:06:05 -080011#include "GrGLSLShaderBuilder.h"
joshualitt30ba4362014-08-21 20:18:45 -070012
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 {
ethannicholasddb37d62016-10-20 09:54:00 -070032 kPixelLocalStorage_GLSLFeature = kLastGLSLPrivateFeature + 1,
cdalton4a98cdb2016-03-01 12:12:20 -080033 kMultisampleInterpolation_GLSLFeature
joshualittb0a8a372014-09-23 09:50:21 -070034 };
35
36 /**
37 * If the feature is supported then true is returned and any necessary #extension declarations
38 * are added to the shaders. If the feature is not supported then false will be returned.
39 */
40 virtual bool enableFeature(GLSLFeature) = 0;
41
42 /**
43 * This returns a variable name to access the 2D, perspective correct version of the coords in
bsalomon1a1aa932016-09-12 09:30:36 -070044 * the fragment shader. The passed in coordinates must either be of type kVec2f or kVec3f. If
45 * the coordinates are 3-dimensional, it a perspective divide into is emitted into the
46 * fragment shader (xy / z) to convert them to 2D.
joshualittb0a8a372014-09-23 09:50:21 -070047 */
bsalomon1a1aa932016-09-12 09:30:36 -070048 virtual SkString ensureCoords2D(const GrShaderVar&) = 0;
joshualittb0a8a372014-09-23 09:50:21 -070049
50
51 /** Returns a variable name that represents the position of the fragment in the FS. The position
52 is in device space (e.g. 0,0 is the top left and pixel centers are at half-integers). */
53 virtual const char* fragmentPosition() = 0;
54
cdalton85285412016-02-18 12:37:07 -080055 // TODO: remove this method.
ethannicholas22793252016-01-30 09:59:10 -080056 void declAppendf(const char* fmt, ...);
57
joshualittb0a8a372014-09-23 09:50:21 -070058private:
egdaniel2d721d32015-11-11 13:06:05 -080059 typedef GrGLSLShaderBuilder INHERITED;
joshualittb0a8a372014-09-23 09:50:21 -070060};
61
62/*
cdalton85285412016-02-18 12:37:07 -080063 * This class is used by fragment processors to build their fragment code.
joshualittb0a8a372014-09-23 09:50:21 -070064 */
cdalton85285412016-02-18 12:37:07 -080065class GrGLSLFPFragmentBuilder : virtual public GrGLSLFragmentBuilder {
joshualittb0a8a372014-09-23 09:50:21 -070066public:
cdalton85285412016-02-18 12:37:07 -080067 /** Appease the compiler; the derived class initializes GrGLSLFragmentBuilder. */
68 GrGLSLFPFragmentBuilder() : GrGLSLFragmentBuilder(nullptr) {}
joshualitt47bb3822014-10-07 16:43:25 -070069
cdalton28f45b92016-03-07 13:58:26 -080070 enum Coordinates {
71 kSkiaDevice_Coordinates,
72 kGLSLWindow_Coordinates,
73
74 kLast_Coordinates = kGLSLWindow_Coordinates
75 };
76
77 /**
78 * Appends the offset from the center of the pixel to a specified sample.
79 *
80 * @param sampleIdx GLSL expression of the sample index.
81 * @param Coordinates Coordinate space in which to emit the offset.
82 *
83 * A processor must call setWillUseSampleLocations in its constructor before using this method.
84 */
85 virtual void appendOffsetToSample(const char* sampleIdx, Coordinates) = 0;
86
cdalton85285412016-02-18 12:37:07 -080087 /**
cdalton33ad7012016-02-22 07:55:44 -080088 * Subtracts sample coverage from the fragment. Any sample whose corresponding bit is not found
89 * in the mask will not be written out to the framebuffer.
90 *
91 * @param mask int that contains the sample mask. Bit N corresponds to the Nth sample.
92 * @param invert perform a bit-wise NOT on the provided mask before applying it?
93 *
94 * Requires GLSL support for sample variables.
95 */
96 virtual void maskSampleCoverage(const char* mask, bool invert = false) = 0;
97
dvonbeck9b03e7b2016-08-01 11:01:56 -070098 /** Returns a variable name that represents a vector to the nearest edge of the shape, in source
99 space coordinates. */
100 virtual const char* distanceVectorName() const = 0;
101
cdalton33ad7012016-02-22 07:55:44 -0800102 /**
cdalton85285412016-02-18 12:37:07 -0800103 * Fragment procs with child procs should call these functions before/after calling emitCode
104 * on a child proc.
105 */
106 virtual void onBeforeChildProcEmitCode() = 0;
107 virtual void onAfterChildProcEmitCode() = 0;
108
109 virtual const SkString& getMangleString() const = 0;
110};
111
112/*
113 * This class is used by primitive processors to build their fragment code.
114 */
115class GrGLSLPPFragmentBuilder : public GrGLSLFPFragmentBuilder {
116public:
117 /** Appease the compiler; the derived class initializes GrGLSLFragmentBuilder. */
118 GrGLSLPPFragmentBuilder() : GrGLSLFragmentBuilder(nullptr) {}
cdalton33ad7012016-02-22 07:55:44 -0800119
120 /**
121 * Overrides the fragment's sample coverage. The provided mask determines which samples will now
122 * be written out to the framebuffer. Note that this mask can be reduced by a future call to
123 * maskSampleCoverage.
124 *
125 * If a primitive processor uses this method, it must guarantee that every codepath through the
126 * shader overrides the sample mask at some point.
127 *
128 * @param mask int that contains the new coverage mask. Bit N corresponds to the Nth sample.
129 *
130 * Requires NV_sample_mask_override_coverage.
131 */
132 virtual void overrideSampleCoverage(const char* mask) = 0;
cdalton85285412016-02-18 12:37:07 -0800133};
134
135/*
136 * This class is used by Xfer processors to build their fragment code.
137 */
138class GrGLSLXPFragmentBuilder : virtual public GrGLSLFragmentBuilder {
139public:
140 /** Appease the compiler; the derived class initializes GrGLSLFragmentBuilder. */
141 GrGLSLXPFragmentBuilder() : GrGLSLFragmentBuilder(nullptr) {}
142
143 virtual bool hasCustomColorOutput() const = 0;
144 virtual bool hasSecondaryOutput() const = 0;
145
146 /** Returns the variable name that holds the color of the destination pixel. This may be nullptr
147 * if no effect advertised that it will read the destination. */
joshualittb0a8a372014-09-23 09:50:21 -0700148 virtual const char* dstColor() = 0;
149
cdalton8917d622015-05-06 13:40:21 -0700150 /** Adds any necessary layout qualifiers in order to legalize the supplied blend equation with
151 this shader. It is only legal to call this method with an advanced blend equation, and only
152 if these equations are supported. */
153 virtual void enableAdvancedBlendEquationIfNeeded(GrBlendEquation) = 0;
joshualittb0a8a372014-09-23 09:50:21 -0700154};
155
cdalton85285412016-02-18 12:37:07 -0800156/*
157 * This class implements the various fragment builder interfaces.
158 */
159class GrGLSLFragmentShaderBuilder : public GrGLSLPPFragmentBuilder, public GrGLSLXPFragmentBuilder {
joshualitt30ba4362014-08-21 20:18:45 -0700160public:
cdalton28f45b92016-03-07 13:58:26 -0800161 /** Returns a nonzero key for a surface's origin. This should only be called if a processor will
162 use the fragment position and/or sample locations. */
163 static uint8_t KeyForSurfaceOrigin(GrSurfaceOrigin);
joshualitt30ba4362014-08-21 20:18:45 -0700164
cdalton28f45b92016-03-07 13:58:26 -0800165 GrGLSLFragmentShaderBuilder(GrGLSLProgramBuilder* program);
joshualitt30ba4362014-08-21 20:18:45 -0700166
cdalton85285412016-02-18 12:37:07 -0800167 // Shared GrGLSLFragmentBuilder interface.
mtklein36352bf2015-03-25 18:17:31 -0700168 bool enableFeature(GLSLFeature) override;
bsalomon1a1aa932016-09-12 09:30:36 -0700169 virtual SkString ensureCoords2D(const GrShaderVar&) override;
mtklein36352bf2015-03-25 18:17:31 -0700170 const char* fragmentPosition() override;
dvonbeck9b03e7b2016-08-01 11:01:56 -0700171 const char* distanceVectorName() const override;
joshualittdb0d3ca2014-10-07 12:42:26 -0700172
cdalton85285412016-02-18 12:37:07 -0800173 // GrGLSLFPFragmentBuilder interface.
cdalton28f45b92016-03-07 13:58:26 -0800174 void appendOffsetToSample(const char* sampleIdx, Coordinates) override;
cdalton33ad7012016-02-22 07:55:44 -0800175 void maskSampleCoverage(const char* mask, bool invert = false) override;
176 void overrideSampleCoverage(const char* mask) override;
cdalton85285412016-02-18 12:37:07 -0800177 const SkString& getMangleString() const override { return fMangleString; }
178 void onBeforeChildProcEmitCode() override;
179 void onAfterChildProcEmitCode() override;
180
181 // GrGLSLXPFragmentBuilder interface.
182 bool hasCustomColorOutput() const override { return fHasCustomColorOutput; }
183 bool hasSecondaryOutput() const override { return fHasSecondaryOutput; }
184 const char* dstColor() override;
cdalton8917d622015-05-06 13:40:21 -0700185 void enableAdvancedBlendEquationIfNeeded(GrBlendEquation) override;
186
joshualitt74077b92014-10-24 11:26:03 -0700187private:
joshualitt47bb3822014-10-07 16:43:25 -0700188 // Private public interface, used by GrGLProgramBuilder to build a fragment shader
joshualitt47bb3822014-10-07 16:43:25 -0700189 void enableCustomOutput();
190 void enableSecondaryOutput();
191 const char* getPrimaryColorOutputName() const;
192 const char* getSecondaryColorOutputName() const;
joshualitt47bb3822014-10-07 16:43:25 -0700193
cdalton87332102016-02-26 12:22:02 -0800194#ifdef SK_DEBUG
egdaniel57d3b032015-11-13 11:57:27 -0800195 // As GLSLProcessors emit code, there are some conditions we need to verify. We use the below
joshualitt47bb3822014-10-07 16:43:25 -0700196 // state to track this. The reset call is called per processor emitted.
cdalton87332102016-02-26 12:22:02 -0800197 GrProcessor::RequiredFeatures usedProcessorFeatures() const { return fUsedProcessorFeatures; }
joshualitt47bb3822014-10-07 16:43:25 -0700198 bool hasReadDstColor() const { return fHasReadDstColor; }
cdalton87332102016-02-26 12:22:02 -0800199 void resetVerification() {
200 fUsedProcessorFeatures = GrProcessor::kNone_RequiredFeatures;
joshualitt47bb3822014-10-07 16:43:25 -0700201 fHasReadDstColor = false;
joshualitt47bb3822014-10-07 16:43:25 -0700202 }
cdalton87332102016-02-26 12:22:02 -0800203#endif
joshualitt30ba4362014-08-21 20:18:45 -0700204
ethannicholas5961bc92016-10-12 06:39:56 -0700205 static const char* DeclaredColorOutputName() { return "sk_FragColor"; }
egdaniel8dcdedc2015-11-11 06:27:20 -0800206 static const char* DeclaredSecondaryColorOutputName() { return "fsSecondaryColorOut"; }
207
cdalton28f45b92016-03-07 13:58:26 -0800208 GrSurfaceOrigin getSurfaceOrigin() const;
joshualitt74077b92014-10-24 11:26:03 -0700209
egdaniel574a4c12015-11-02 06:22:44 -0800210 void onFinalize() override;
cdalton28f45b92016-03-07 13:58:26 -0800211 void defineSampleOffsetArray(const char* name, const SkMatrix&);
joshualitt30ba4362014-08-21 20:18:45 -0700212
egdaniel138c2632016-08-17 10:59:00 -0700213 static const char* kDstColorName;
joshualitt47bb3822014-10-07 16:43:25 -0700214
cdalton85285412016-02-18 12:37:07 -0800215 /*
216 * State that tracks which child proc in the proc tree is currently emitting code. This is
217 * used to update the fMangleString, which is used to mangle the names of uniforms and functions
218 * emitted by the proc. fSubstageIndices is a stack: its count indicates how many levels deep
219 * we are in the tree, and its second-to-last value is the index of the child proc at that
220 * level which is currently emitting code. For example, if fSubstageIndices = [3, 1, 2, 0], that
221 * means we're currently emitting code for the base proc's 3rd child's 1st child's 2nd child.
222 */
223 SkTArray<int> fSubstageIndices;
224
225 /*
226 * The mangle string is used to mangle the names of uniforms/functions emitted by the child
227 * procs so no duplicate uniforms/functions appear in the generated shader program. The mangle
228 * string is simply based on fSubstageIndices. For example, if fSubstageIndices = [3, 1, 2, 0],
229 * then the manglestring will be "_c3_c1_c2", and any uniform/function emitted by that proc will
230 * have "_c3_c1_c2" appended to its name, which can be interpreted as "base proc's 3rd child's
231 * 1st child's 2nd child".
232 */
233 SkString fMangleString;
234
cdalton28f45b92016-03-07 13:58:26 -0800235 bool fSetupFragPosition;
236 bool fHasCustomColorOutput;
237 int fCustomColorOutputIndex;
238 bool fHasSecondaryOutput;
239 uint8_t fUsedSampleOffsetArrays;
240 bool fHasInitializedSampleMask;
dvonbeck9b03e7b2016-08-01 11:01:56 -0700241 SkString fDistanceVectorOutput;
joshualitt30ba4362014-08-21 20:18:45 -0700242
cdalton87332102016-02-26 12:22:02 -0800243#ifdef SK_DEBUG
joshualitt47bb3822014-10-07 16:43:25 -0700244 // some state to verify shaders and effects are consistent, this is reset between effects by
245 // the program creator
cdalton87332102016-02-26 12:22:02 -0800246 GrProcessor::RequiredFeatures fUsedProcessorFeatures;
joshualitt47bb3822014-10-07 16:43:25 -0700247 bool fHasReadDstColor;
cdalton87332102016-02-26 12:22:02 -0800248#endif
joshualittfe1233c2014-10-07 12:16:35 -0700249
egdanielfa896322016-01-13 12:19:30 -0800250 friend class GrGLSLProgramBuilder;
joshualitt47bb3822014-10-07 16:43:25 -0700251 friend class GrGLProgramBuilder;
joshualitt30ba4362014-08-21 20:18:45 -0700252};
253
254#endif