blob: ed92703a01f95cc35b90c23db2602d7f4d781eed [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
8#ifndef GrGLFragmentShaderBuilder_DEFINED
9#define GrGLFragmentShaderBuilder_DEFINED
joshualitt47bb3822014-10-07 16:43:25 -070010
joshualitt30ba4362014-08-21 20:18:45 -070011#include "GrGLShaderBuilder.h"
12
egdaniel7dc4bd02015-10-29 07:57:01 -070013#include "glsl/GrGLSLProcessorTypes.h"
14
joshualitt74077b92014-10-24 11:26:03 -070015class GrGLVarying;
16
joshualittb0a8a372014-09-23 09:50:21 -070017/*
joshualitt47bb3822014-10-07 16:43:25 -070018 * This base class encapsulates the functionality which the GP uses to build fragment shaders
joshualittb0a8a372014-09-23 09:50:21 -070019 */
egdaniel29bee0f2015-04-29 11:54:42 -070020class GrGLFragmentBuilder : public GrGLShaderBuilder {
joshualittb0a8a372014-09-23 09:50:21 -070021public:
wangyix7ef45a12015-08-13 06:51:35 -070022 GrGLFragmentBuilder(GrGLProgramBuilder* program)
23 : INHERITED(program) {
24 fSubstageIndices.push_back(0);
25 }
egdaniel29bee0f2015-04-29 11:54:42 -070026 virtual ~GrGLFragmentBuilder() {}
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 {
32 kStandardDerivatives_GLSLFeature = 0,
33 kLastGLSLFeature = kStandardDerivatives_GLSLFeature
34 };
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
44 * the fragment shader. If the coordinates at index are 3-dimensional, it immediately emits a
45 * perspective divide into the fragment shader (xy / z) to convert them to 2D.
46 */
egdaniel7dc4bd02015-10-29 07:57:01 -070047 virtual SkString ensureFSCoords2D(const GrGLSLTransformedCoordsArray& coords, int index) = 0;
joshualittb0a8a372014-09-23 09:50:21 -070048
49
50 /** Returns a variable name that represents the position of the fragment in the FS. The position
51 is in device space (e.g. 0,0 is the top left and pixel centers are at half-integers). */
52 virtual const char* fragmentPosition() = 0;
53
wangyix7ef45a12015-08-13 06:51:35 -070054 /**
55 * Fragment procs with child procs should call these functions before/after calling emitCode
56 * on a child proc.
57 */
58 void onBeforeChildProcEmitCode();
59 void onAfterChildProcEmitCode();
60
wangyix7ef45a12015-08-13 06:51:35 -070061 const SkString& getMangleString() const { return fMangleString; }
62
joshualittb0a8a372014-09-23 09:50:21 -070063private:
wangyix7ef45a12015-08-13 06:51:35 -070064 /*
65 * State that tracks which child proc in the proc tree is currently emitting code. This is
66 * used to update the fMangleString, which is used to mangle the names of uniforms and functions
67 * emitted by the proc. fSubstageIndices is a stack: its count indicates how many levels deep
68 * we are in the tree, and its second-to-last value is the index of the child proc at that
69 * level which is currently emitting code. For example, if fSubstageIndices = [3, 1, 2, 0], that
70 * means we're currently emitting code for the base proc's 3rd child's 1st child's 2nd child.
71 */
72 SkTArray<int> fSubstageIndices;
73
74 /*
75 * The mangle string is used to mangle the names of uniforms/functions emitted by the child
76 * procs so no duplicate uniforms/functions appear in the generated shader program. The mangle
77 * string is simply based on fSubstageIndices. For example, if fSubstageIndices = [3, 1, 2, 0],
78 * then the manglestring will be "_c3_c1_c2", and any uniform/function emitted by that proc will
79 * have "_c3_c1_c2" appended to its name, which can be interpreted as "base proc's 3rd child's
80 * 1st child's 2nd child".
81 */
82 SkString fMangleString;
83
jvanverth50530632015-04-27 10:36:27 -070084 friend class GrGLPathProcessor;
joshualittabb52a12015-01-13 15:02:10 -080085
joshualittb0a8a372014-09-23 09:50:21 -070086 typedef GrGLShaderBuilder INHERITED;
87};
88
89/*
90 * Fragment processor's, in addition to all of the above, may need to use dst color so they use
joshualitt47bb3822014-10-07 16:43:25 -070091 * this builder to create their shader. Because this is the only shader builder the FP sees, we
92 * just call it FPShaderBuilder
joshualittb0a8a372014-09-23 09:50:21 -070093 */
egdaniel29bee0f2015-04-29 11:54:42 -070094class GrGLXPFragmentBuilder : public GrGLFragmentBuilder {
joshualittb0a8a372014-09-23 09:50:21 -070095public:
egdaniel29bee0f2015-04-29 11:54:42 -070096 GrGLXPFragmentBuilder(GrGLProgramBuilder* program) : INHERITED(program) {}
joshualitt47bb3822014-10-07 16:43:25 -070097
halcanary96fcdcc2015-08-27 07:41:13 -070098 /** Returns the variable name that holds the color of the destination pixel. This may be nullptr if
joshualittb0a8a372014-09-23 09:50:21 -070099 no effect advertised that it will read the destination. */
100 virtual const char* dstColor() = 0;
101
cdalton8917d622015-05-06 13:40:21 -0700102 /** Adds any necessary layout qualifiers in order to legalize the supplied blend equation with
103 this shader. It is only legal to call this method with an advanced blend equation, and only
104 if these equations are supported. */
105 virtual void enableAdvancedBlendEquationIfNeeded(GrBlendEquation) = 0;
106
joshualittb0a8a372014-09-23 09:50:21 -0700107private:
egdaniel29bee0f2015-04-29 11:54:42 -0700108 typedef GrGLFragmentBuilder INHERITED;
joshualittb0a8a372014-09-23 09:50:21 -0700109};
110
joshualitt47bb3822014-10-07 16:43:25 -0700111// TODO rename to Fragment Builder
egdaniel29bee0f2015-04-29 11:54:42 -0700112class GrGLFragmentShaderBuilder : public GrGLXPFragmentBuilder {
joshualitt30ba4362014-08-21 20:18:45 -0700113public:
114 typedef uint8_t DstReadKey;
115 typedef uint8_t FragPosKey;
116
bsalomon6a44c6a2015-05-26 09:49:05 -0700117 /** Returns a key for adding code to read the dst texture color in service of effects that
joshualitt30ba4362014-08-21 20:18:45 -0700118 require reading the dst. It must not return 0 because 0 indicates that there is no dst
bsalomon6a44c6a2015-05-26 09:49:05 -0700119 texture at all (in which case this function should not be called). */
120 static DstReadKey KeyForDstRead(const GrTexture* dsttexture, const GrGLCaps&);
joshualitt30ba4362014-08-21 20:18:45 -0700121
122 /** Returns a key for reading the fragment location. This should only be called if there is an
123 effect that will requires the fragment position. If the fragment position is not required,
124 the key is 0. */
125 static FragPosKey KeyForFragmentPosition(const GrRenderTarget* dst, const GrGLCaps&);
126
joshualitt79f8fae2014-10-28 17:59:26 -0700127 GrGLFragmentShaderBuilder(GrGLProgramBuilder* program, uint8_t fragPosKey);
joshualitt30ba4362014-08-21 20:18:45 -0700128
joshualitt47bb3822014-10-07 16:43:25 -0700129 // true public interface, defined explicitly in the abstract interfaces above
mtklein36352bf2015-03-25 18:17:31 -0700130 bool enableFeature(GLSLFeature) override;
egdaniel7dc4bd02015-10-29 07:57:01 -0700131 virtual SkString ensureFSCoords2D(const GrGLSLTransformedCoordsArray& coords,
mtklein36352bf2015-03-25 18:17:31 -0700132 int index) override;
133 const char* fragmentPosition() override;
134 const char* dstColor() override;
joshualittdb0d3ca2014-10-07 12:42:26 -0700135
cdalton8917d622015-05-06 13:40:21 -0700136 void enableAdvancedBlendEquationIfNeeded(GrBlendEquation) override;
137
joshualitt74077b92014-10-24 11:26:03 -0700138private:
joshualitt47bb3822014-10-07 16:43:25 -0700139 // Private public interface, used by GrGLProgramBuilder to build a fragment shader
joshualitt47bb3822014-10-07 16:43:25 -0700140 void enableCustomOutput();
141 void enableSecondaryOutput();
142 const char* getPrimaryColorOutputName() const;
143 const char* getSecondaryColorOutputName() const;
joshualitt43466a12015-02-13 17:18:27 -0800144 bool compileAndAttachShaders(GrGLuint programId, SkTDArray<GrGLuint>* shaderIds);
joshualitt47bb3822014-10-07 16:43:25 -0700145 void bindFragmentShaderLocations(GrGLuint programID);
146
joshualitt47bb3822014-10-07 16:43:25 -0700147 // As GLProcessors emit code, there are some conditions we need to verify. We use the below
148 // state to track this. The reset call is called per processor emitted.
149 bool hasReadDstColor() const { return fHasReadDstColor; }
150 bool hasReadFragmentPosition() const { return fHasReadFragmentPosition; }
151 void reset() {
152 fHasReadDstColor = false;
153 fHasReadFragmentPosition = false;
154 }
joshualitt30ba4362014-08-21 20:18:45 -0700155
joshualitt74077b92014-10-24 11:26:03 -0700156 /*
157 * An internal call for GrGLProgramBuilder to use to add varyings to the vertex shader
158 */
bsalomonc0bd6482014-12-09 10:04:14 -0800159 void addVarying(GrGLVarying*, GrSLPrecision);
joshualitt74077b92014-10-24 11:26:03 -0700160
joshualitt30ba4362014-08-21 20:18:45 -0700161 /**
162 * Features that should only be enabled by GrGLFragmentShaderBuilder itself.
163 */
164 enum GLSLPrivateFeature {
165 kFragCoordConventions_GLSLPrivateFeature = kLastGLSLFeature + 1,
cdalton8917d622015-05-06 13:40:21 -0700166 kBlendEquationAdvanced_GLSLPrivateFeature,
kkinnunend94708e2015-07-30 22:47:04 -0700167 kBlendFuncExtended_GLSLPrivateFeature,
168 kLastGLSLPrivateFeature = kBlendFuncExtended_GLSLPrivateFeature
joshualitt30ba4362014-08-21 20:18:45 -0700169 };
170
171 // Interpretation of DstReadKey when generating code
172 enum {
173 kNoDstRead_DstReadKey = 0,
174 kYesDstRead_DstReadKeyBit = 0x1, // Set if we do a dst-copy-read.
175 kUseAlphaConfig_DstReadKeyBit = 0x2, // Set if dst-copy config is alpha only.
176 kTopLeftOrigin_DstReadKeyBit = 0x4, // Set if dst-copy origin is top-left.
177 };
178
joshualitt47bb3822014-10-07 16:43:25 -0700179 // Interpretation of FragPosKey when generating code
joshualitt30ba4362014-08-21 20:18:45 -0700180 enum {
181 kNoFragPosRead_FragPosKey = 0, // The fragment positition will not be needed.
182 kTopLeftFragPosRead_FragPosKey = 0x1,// Read frag pos relative to top-left.
183 kBottomLeftFragPosRead_FragPosKey = 0x2,// Read frag pos relative to bottom-left.
184 };
185
bsalomon6a44c6a2015-05-26 09:49:05 -0700186 static const char* kDstTextureColorName;
joshualitt47bb3822014-10-07 16:43:25 -0700187
joshualitt30ba4362014-08-21 20:18:45 -0700188 bool fHasCustomColorOutput;
189 bool fHasSecondaryOutput;
190 bool fSetupFragPosition;
191 bool fTopLeftFragPosRead;
joshualittb4384b92014-10-21 12:53:15 -0700192 int fCustomColorOutputIndex;
joshualitt30ba4362014-08-21 20:18:45 -0700193
joshualitt47bb3822014-10-07 16:43:25 -0700194 // some state to verify shaders and effects are consistent, this is reset between effects by
195 // the program creator
196 bool fHasReadDstColor;
197 bool fHasReadFragmentPosition;
joshualittfe1233c2014-10-07 12:16:35 -0700198
joshualitt47bb3822014-10-07 16:43:25 -0700199 friend class GrGLProgramBuilder;
200
egdaniel29bee0f2015-04-29 11:54:42 -0700201 typedef GrGLXPFragmentBuilder INHERITED;
joshualitt30ba4362014-08-21 20:18:45 -0700202};
203
204#endif