blob: d64dfdb5195dcd5cd0d9ac5817c87463f04a0fda [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 /**
joshualittb0a8a372014-09-23 09:50:21 -070028 * This returns a variable name to access the 2D, perspective correct version of the coords in
Ethan Nicholasf7b88202017-09-18 14:10:39 -040029 * the fragment shader. The passed in coordinates must either be of type kHalf2 or kHalf3. If
bsalomon1a1aa932016-09-12 09:30:36 -070030 * the coordinates are 3-dimensional, it a perspective divide into is emitted into the
31 * fragment shader (xy / z) to convert them to 2D.
joshualittb0a8a372014-09-23 09:50:21 -070032 */
bsalomon1a1aa932016-09-12 09:30:36 -070033 virtual SkString ensureCoords2D(const GrShaderVar&) = 0;
joshualittb0a8a372014-09-23 09:50:21 -070034
cdalton85285412016-02-18 12:37:07 -080035 // TODO: remove this method.
ethannicholas22793252016-01-30 09:59:10 -080036 void declAppendf(const char* fmt, ...);
37
joshualittb0a8a372014-09-23 09:50:21 -070038private:
egdaniel2d721d32015-11-11 13:06:05 -080039 typedef GrGLSLShaderBuilder INHERITED;
joshualittb0a8a372014-09-23 09:50:21 -070040};
41
42/*
cdalton85285412016-02-18 12:37:07 -080043 * This class is used by fragment processors to build their fragment code.
joshualittb0a8a372014-09-23 09:50:21 -070044 */
cdalton85285412016-02-18 12:37:07 -080045class GrGLSLFPFragmentBuilder : virtual public GrGLSLFragmentBuilder {
joshualittb0a8a372014-09-23 09:50:21 -070046public:
cdalton85285412016-02-18 12:37:07 -080047 /** Appease the compiler; the derived class initializes GrGLSLFragmentBuilder. */
48 GrGLSLFPFragmentBuilder() : GrGLSLFragmentBuilder(nullptr) {}
joshualitt47bb3822014-10-07 16:43:25 -070049
Robert Phillips7f861922018-01-30 13:13:42 +000050 enum Coordinates {
51 kSkiaDevice_Coordinates,
52 kGLSLWindow_Coordinates,
53
54 kLast_Coordinates = kGLSLWindow_Coordinates
55 };
56
57 /**
58 * Appends the offset from the center of the pixel to a specified sample.
59 *
60 * @param sampleIdx GLSL expression of the sample index.
61 * @param Coordinates Coordinate space in which to emit the offset.
62 *
63 * A processor must call setWillUseSampleLocations in its constructor before using this method.
64 */
65 virtual void appendOffsetToSample(const char* sampleIdx, Coordinates) = 0;
66
67 /**
cdalton85285412016-02-18 12:37:07 -080068 * Fragment procs with child procs should call these functions before/after calling emitCode
69 * on a child proc.
70 */
71 virtual void onBeforeChildProcEmitCode() = 0;
72 virtual void onAfterChildProcEmitCode() = 0;
73
74 virtual const SkString& getMangleString() const = 0;
Ethan Nicholasf7b88202017-09-18 14:10:39 -040075
76 virtual void forceHighPrecision() = 0;
cdalton85285412016-02-18 12:37:07 -080077};
78
79/*
cdalton85285412016-02-18 12:37:07 -080080 * This class is used by Xfer processors to build their fragment code.
81 */
82class GrGLSLXPFragmentBuilder : virtual public GrGLSLFragmentBuilder {
83public:
84 /** Appease the compiler; the derived class initializes GrGLSLFragmentBuilder. */
85 GrGLSLXPFragmentBuilder() : GrGLSLFragmentBuilder(nullptr) {}
86
87 virtual bool hasCustomColorOutput() const = 0;
88 virtual bool hasSecondaryOutput() const = 0;
89
90 /** Returns the variable name that holds the color of the destination pixel. This may be nullptr
91 * if no effect advertised that it will read the destination. */
joshualittb0a8a372014-09-23 09:50:21 -070092 virtual const char* dstColor() = 0;
93
cdalton8917d622015-05-06 13:40:21 -070094 /** Adds any necessary layout qualifiers in order to legalize the supplied blend equation with
95 this shader. It is only legal to call this method with an advanced blend equation, and only
96 if these equations are supported. */
97 virtual void enableAdvancedBlendEquationIfNeeded(GrBlendEquation) = 0;
joshualittb0a8a372014-09-23 09:50:21 -070098};
99
cdalton85285412016-02-18 12:37:07 -0800100/*
101 * This class implements the various fragment builder interfaces.
102 */
Chris Dalton60283612018-02-14 13:38:14 -0700103class GrGLSLFragmentShaderBuilder : public GrGLSLFPFragmentBuilder, public GrGLSLXPFragmentBuilder {
joshualitt30ba4362014-08-21 20:18:45 -0700104public:
Robert Phillips7f861922018-01-30 13:13:42 +0000105 /** Returns a nonzero key for a surface's origin. This should only be called if a processor will
106 use the fragment position and/or sample locations. */
107 static uint8_t KeyForSurfaceOrigin(GrSurfaceOrigin);
108
cdalton28f45b92016-03-07 13:58:26 -0800109 GrGLSLFragmentShaderBuilder(GrGLSLProgramBuilder* program);
joshualitt30ba4362014-08-21 20:18:45 -0700110
cdalton85285412016-02-18 12:37:07 -0800111 // Shared GrGLSLFragmentBuilder interface.
bsalomon1a1aa932016-09-12 09:30:36 -0700112 virtual SkString ensureCoords2D(const GrShaderVar&) override;
joshualittdb0d3ca2014-10-07 12:42:26 -0700113
cdalton85285412016-02-18 12:37:07 -0800114 // GrGLSLFPFragmentBuilder interface.
Robert Phillips7f861922018-01-30 13:13:42 +0000115 void appendOffsetToSample(const char* sampleIdx, Coordinates) override;
cdalton85285412016-02-18 12:37:07 -0800116 const SkString& getMangleString() const override { return fMangleString; }
117 void onBeforeChildProcEmitCode() override;
118 void onAfterChildProcEmitCode() override;
Ethan Nicholasf7b88202017-09-18 14:10:39 -0400119 void forceHighPrecision() override { fForceHighPrecision = true; }
cdalton85285412016-02-18 12:37:07 -0800120
121 // GrGLSLXPFragmentBuilder interface.
122 bool hasCustomColorOutput() const override { return fHasCustomColorOutput; }
123 bool hasSecondaryOutput() const override { return fHasSecondaryOutput; }
124 const char* dstColor() override;
cdalton8917d622015-05-06 13:40:21 -0700125 void enableAdvancedBlendEquationIfNeeded(GrBlendEquation) override;
126
joshualitt74077b92014-10-24 11:26:03 -0700127private:
joshualitt47bb3822014-10-07 16:43:25 -0700128 // Private public interface, used by GrGLProgramBuilder to build a fragment shader
joshualitt47bb3822014-10-07 16:43:25 -0700129 void enableCustomOutput();
130 void enableSecondaryOutput();
131 const char* getPrimaryColorOutputName() const;
132 const char* getSecondaryColorOutputName() const;
joshualitt47bb3822014-10-07 16:43:25 -0700133
cdalton87332102016-02-26 12:22:02 -0800134#ifdef SK_DEBUG
egdaniel57d3b032015-11-13 11:57:27 -0800135 // As GLSLProcessors emit code, there are some conditions we need to verify. We use the below
joshualitt47bb3822014-10-07 16:43:25 -0700136 // state to track this. The reset call is called per processor emitted.
Robert Phillips7f861922018-01-30 13:13:42 +0000137 GrProcessor::RequiredFeatures usedProcessorFeatures() const { return fUsedProcessorFeatures; }
joshualitt47bb3822014-10-07 16:43:25 -0700138 bool hasReadDstColor() const { return fHasReadDstColor; }
cdalton87332102016-02-26 12:22:02 -0800139 void resetVerification() {
Robert Phillips7f861922018-01-30 13:13:42 +0000140 fUsedProcessorFeatures = GrProcessor::kNone_RequiredFeatures;
joshualitt47bb3822014-10-07 16:43:25 -0700141 fHasReadDstColor = false;
joshualitt47bb3822014-10-07 16:43:25 -0700142 }
cdalton87332102016-02-26 12:22:02 -0800143#endif
joshualitt30ba4362014-08-21 20:18:45 -0700144
ethannicholas5961bc92016-10-12 06:39:56 -0700145 static const char* DeclaredColorOutputName() { return "sk_FragColor"; }
egdaniel8dcdedc2015-11-11 06:27:20 -0800146 static const char* DeclaredSecondaryColorOutputName() { return "fsSecondaryColorOut"; }
147
cdalton28f45b92016-03-07 13:58:26 -0800148 GrSurfaceOrigin getSurfaceOrigin() const;
joshualitt74077b92014-10-24 11:26:03 -0700149
egdaniel574a4c12015-11-02 06:22:44 -0800150 void onFinalize() override;
Robert Phillips7f861922018-01-30 13:13:42 +0000151 void defineSampleOffsetArray(const char* name, const SkMatrix&);
joshualitt30ba4362014-08-21 20:18:45 -0700152
egdaniel138c2632016-08-17 10:59:00 -0700153 static const char* kDstColorName;
joshualitt47bb3822014-10-07 16:43:25 -0700154
cdalton85285412016-02-18 12:37:07 -0800155 /*
156 * State that tracks which child proc in the proc tree is currently emitting code. This is
157 * used to update the fMangleString, which is used to mangle the names of uniforms and functions
158 * emitted by the proc. fSubstageIndices is a stack: its count indicates how many levels deep
159 * we are in the tree, and its second-to-last value is the index of the child proc at that
160 * level which is currently emitting code. For example, if fSubstageIndices = [3, 1, 2, 0], that
161 * means we're currently emitting code for the base proc's 3rd child's 1st child's 2nd child.
162 */
163 SkTArray<int> fSubstageIndices;
164
165 /*
166 * The mangle string is used to mangle the names of uniforms/functions emitted by the child
167 * procs so no duplicate uniforms/functions appear in the generated shader program. The mangle
168 * string is simply based on fSubstageIndices. For example, if fSubstageIndices = [3, 1, 2, 0],
169 * then the manglestring will be "_c3_c1_c2", and any uniform/function emitted by that proc will
170 * have "_c3_c1_c2" appended to its name, which can be interpreted as "base proc's 3rd child's
171 * 1st child's 2nd child".
172 */
173 SkString fMangleString;
174
Chris Dalton60283612018-02-14 13:38:14 -0700175 bool fSetupFragPosition;
176 bool fHasCustomColorOutput;
177 int fCustomColorOutputIndex;
178 bool fHasSecondaryOutput;
179 uint8_t fUsedSampleOffsetArrays;
180 bool fForceHighPrecision;
joshualitt30ba4362014-08-21 20:18:45 -0700181
cdalton87332102016-02-26 12:22:02 -0800182#ifdef SK_DEBUG
joshualitt47bb3822014-10-07 16:43:25 -0700183 // some state to verify shaders and effects are consistent, this is reset between effects by
184 // the program creator
Robert Phillips7f861922018-01-30 13:13:42 +0000185 GrProcessor::RequiredFeatures fUsedProcessorFeatures;
joshualitt47bb3822014-10-07 16:43:25 -0700186 bool fHasReadDstColor;
cdalton87332102016-02-26 12:22:02 -0800187#endif
joshualittfe1233c2014-10-07 12:16:35 -0700188
egdanielfa896322016-01-13 12:19:30 -0800189 friend class GrGLSLProgramBuilder;
joshualitt47bb3822014-10-07 16:43:25 -0700190 friend class GrGLProgramBuilder;
joshualitt30ba4362014-08-21 20:18:45 -0700191};
192
193#endif