blob: 39ca5e7dad6885fb1e8c504890b5e08ff539db67 [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
egdaniel574a4c12015-11-02 06:22:44 -080013#include "gl/GrGLTypes.h"
egdaniel7dc4bd02015-10-29 07:57:01 -070014#include "glsl/GrGLSLProcessorTypes.h"
15
egdaniel574a4c12015-11-02 06:22:44 -080016class GrRenderTarget;
joshualitt74077b92014-10-24 11:26:03 -070017class GrGLVarying;
18
joshualittb0a8a372014-09-23 09:50:21 -070019/*
joshualitt47bb3822014-10-07 16:43:25 -070020 * This base class encapsulates the functionality which the GP uses to build fragment shaders
joshualittb0a8a372014-09-23 09:50:21 -070021 */
egdaniel29bee0f2015-04-29 11:54:42 -070022class GrGLFragmentBuilder : public GrGLShaderBuilder {
joshualittb0a8a372014-09-23 09:50:21 -070023public:
wangyix7ef45a12015-08-13 06:51:35 -070024 GrGLFragmentBuilder(GrGLProgramBuilder* program)
25 : INHERITED(program) {
26 fSubstageIndices.push_back(0);
27 }
egdaniel29bee0f2015-04-29 11:54:42 -070028 virtual ~GrGLFragmentBuilder() {}
joshualittb0a8a372014-09-23 09:50:21 -070029 /**
30 * Use of these features may require a GLSL extension to be enabled. Shaders may not compile
31 * if code is added that uses one of these features without calling enableFeature()
32 */
33 enum GLSLFeature {
34 kStandardDerivatives_GLSLFeature = 0,
35 kLastGLSLFeature = kStandardDerivatives_GLSLFeature
36 };
37
38 /**
39 * If the feature is supported then true is returned and any necessary #extension declarations
40 * are added to the shaders. If the feature is not supported then false will be returned.
41 */
42 virtual bool enableFeature(GLSLFeature) = 0;
43
44 /**
45 * This returns a variable name to access the 2D, perspective correct version of the coords in
46 * the fragment shader. If the coordinates at index are 3-dimensional, it immediately emits a
47 * perspective divide into the fragment shader (xy / z) to convert them to 2D.
48 */
egdaniel7dc4bd02015-10-29 07:57:01 -070049 virtual SkString ensureFSCoords2D(const GrGLSLTransformedCoordsArray& coords, int index) = 0;
joshualittb0a8a372014-09-23 09:50:21 -070050
51
52 /** Returns a variable name that represents the position of the fragment in the FS. The position
53 is in device space (e.g. 0,0 is the top left and pixel centers are at half-integers). */
54 virtual const char* fragmentPosition() = 0;
55
wangyix7ef45a12015-08-13 06:51:35 -070056 /**
57 * Fragment procs with child procs should call these functions before/after calling emitCode
58 * on a child proc.
59 */
60 void onBeforeChildProcEmitCode();
61 void onAfterChildProcEmitCode();
62
wangyix7ef45a12015-08-13 06:51:35 -070063 const SkString& getMangleString() const { return fMangleString; }
64
joshualittb0a8a372014-09-23 09:50:21 -070065private:
wangyix7ef45a12015-08-13 06:51:35 -070066 /*
67 * State that tracks which child proc in the proc tree is currently emitting code. This is
68 * used to update the fMangleString, which is used to mangle the names of uniforms and functions
69 * emitted by the proc. fSubstageIndices is a stack: its count indicates how many levels deep
70 * we are in the tree, and its second-to-last value is the index of the child proc at that
71 * level which is currently emitting code. For example, if fSubstageIndices = [3, 1, 2, 0], that
72 * means we're currently emitting code for the base proc's 3rd child's 1st child's 2nd child.
73 */
74 SkTArray<int> fSubstageIndices;
75
76 /*
77 * The mangle string is used to mangle the names of uniforms/functions emitted by the child
78 * procs so no duplicate uniforms/functions appear in the generated shader program. The mangle
79 * string is simply based on fSubstageIndices. For example, if fSubstageIndices = [3, 1, 2, 0],
80 * then the manglestring will be "_c3_c1_c2", and any uniform/function emitted by that proc will
81 * have "_c3_c1_c2" appended to its name, which can be interpreted as "base proc's 3rd child's
82 * 1st child's 2nd child".
83 */
84 SkString fMangleString;
85
jvanverth50530632015-04-27 10:36:27 -070086 friend class GrGLPathProcessor;
joshualittabb52a12015-01-13 15:02:10 -080087
joshualittb0a8a372014-09-23 09:50:21 -070088 typedef GrGLShaderBuilder INHERITED;
89};
90
91/*
92 * 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 -070093 * this builder to create their shader. Because this is the only shader builder the FP sees, we
94 * just call it FPShaderBuilder
joshualittb0a8a372014-09-23 09:50:21 -070095 */
egdaniel29bee0f2015-04-29 11:54:42 -070096class GrGLXPFragmentBuilder : public GrGLFragmentBuilder {
joshualittb0a8a372014-09-23 09:50:21 -070097public:
egdaniel29bee0f2015-04-29 11:54:42 -070098 GrGLXPFragmentBuilder(GrGLProgramBuilder* program) : INHERITED(program) {}
joshualitt47bb3822014-10-07 16:43:25 -070099
halcanary96fcdcc2015-08-27 07:41:13 -0700100 /** Returns the variable name that holds the color of the destination pixel. This may be nullptr if
joshualittb0a8a372014-09-23 09:50:21 -0700101 no effect advertised that it will read the destination. */
102 virtual const char* dstColor() = 0;
103
cdalton8917d622015-05-06 13:40:21 -0700104 /** Adds any necessary layout qualifiers in order to legalize the supplied blend equation with
105 this shader. It is only legal to call this method with an advanced blend equation, and only
106 if these equations are supported. */
107 virtual void enableAdvancedBlendEquationIfNeeded(GrBlendEquation) = 0;
108
joshualittb0a8a372014-09-23 09:50:21 -0700109private:
egdaniel29bee0f2015-04-29 11:54:42 -0700110 typedef GrGLFragmentBuilder INHERITED;
joshualittb0a8a372014-09-23 09:50:21 -0700111};
112
joshualitt47bb3822014-10-07 16:43:25 -0700113// TODO rename to Fragment Builder
egdaniel29bee0f2015-04-29 11:54:42 -0700114class GrGLFragmentShaderBuilder : public GrGLXPFragmentBuilder {
joshualitt30ba4362014-08-21 20:18:45 -0700115public:
joshualitt30ba4362014-08-21 20:18:45 -0700116 typedef uint8_t FragPosKey;
117
joshualitt30ba4362014-08-21 20:18:45 -0700118 /** Returns a key for reading the fragment location. This should only be called if there is an
119 effect that will requires the fragment position. If the fragment position is not required,
120 the key is 0. */
egdaniel574a4c12015-11-02 06:22:44 -0800121 static FragPosKey KeyForFragmentPosition(const GrRenderTarget* dst);
joshualitt30ba4362014-08-21 20:18:45 -0700122
joshualitt79f8fae2014-10-28 17:59:26 -0700123 GrGLFragmentShaderBuilder(GrGLProgramBuilder* program, uint8_t fragPosKey);
joshualitt30ba4362014-08-21 20:18:45 -0700124
joshualitt47bb3822014-10-07 16:43:25 -0700125 // true public interface, defined explicitly in the abstract interfaces above
mtklein36352bf2015-03-25 18:17:31 -0700126 bool enableFeature(GLSLFeature) override;
egdaniel7dc4bd02015-10-29 07:57:01 -0700127 virtual SkString ensureFSCoords2D(const GrGLSLTransformedCoordsArray& coords,
mtklein36352bf2015-03-25 18:17:31 -0700128 int index) override;
129 const char* fragmentPosition() override;
130 const char* dstColor() override;
joshualittdb0d3ca2014-10-07 12:42:26 -0700131
cdalton8917d622015-05-06 13:40:21 -0700132 void enableAdvancedBlendEquationIfNeeded(GrBlendEquation) override;
133
joshualitt74077b92014-10-24 11:26:03 -0700134private:
joshualitt47bb3822014-10-07 16:43:25 -0700135 // Private public interface, used by GrGLProgramBuilder to build a fragment shader
joshualitt47bb3822014-10-07 16:43:25 -0700136 void enableCustomOutput();
137 void enableSecondaryOutput();
138 const char* getPrimaryColorOutputName() const;
139 const char* getSecondaryColorOutputName() const;
joshualitt47bb3822014-10-07 16:43:25 -0700140 void bindFragmentShaderLocations(GrGLuint programID);
141
joshualitt47bb3822014-10-07 16:43:25 -0700142 // As GLProcessors emit code, there are some conditions we need to verify. We use the below
143 // state to track this. The reset call is called per processor emitted.
144 bool hasReadDstColor() const { return fHasReadDstColor; }
145 bool hasReadFragmentPosition() const { return fHasReadFragmentPosition; }
146 void reset() {
147 fHasReadDstColor = false;
148 fHasReadFragmentPosition = false;
149 }
joshualitt30ba4362014-08-21 20:18:45 -0700150
joshualitt74077b92014-10-24 11:26:03 -0700151 /*
152 * An internal call for GrGLProgramBuilder to use to add varyings to the vertex shader
153 */
bsalomonc0bd6482014-12-09 10:04:14 -0800154 void addVarying(GrGLVarying*, GrSLPrecision);
joshualitt74077b92014-10-24 11:26:03 -0700155
egdaniel574a4c12015-11-02 06:22:44 -0800156 void onFinalize() override;
157
joshualitt30ba4362014-08-21 20:18:45 -0700158 /**
159 * Features that should only be enabled by GrGLFragmentShaderBuilder itself.
160 */
161 enum GLSLPrivateFeature {
162 kFragCoordConventions_GLSLPrivateFeature = kLastGLSLFeature + 1,
cdalton8917d622015-05-06 13:40:21 -0700163 kBlendEquationAdvanced_GLSLPrivateFeature,
kkinnunend94708e2015-07-30 22:47:04 -0700164 kBlendFuncExtended_GLSLPrivateFeature,
165 kLastGLSLPrivateFeature = kBlendFuncExtended_GLSLPrivateFeature
joshualitt30ba4362014-08-21 20:18:45 -0700166 };
167
joshualitt47bb3822014-10-07 16:43:25 -0700168 // Interpretation of FragPosKey when generating code
joshualitt30ba4362014-08-21 20:18:45 -0700169 enum {
170 kNoFragPosRead_FragPosKey = 0, // The fragment positition will not be needed.
171 kTopLeftFragPosRead_FragPosKey = 0x1,// Read frag pos relative to top-left.
172 kBottomLeftFragPosRead_FragPosKey = 0x2,// Read frag pos relative to bottom-left.
173 };
174
bsalomon6a44c6a2015-05-26 09:49:05 -0700175 static const char* kDstTextureColorName;
joshualitt47bb3822014-10-07 16:43:25 -0700176
joshualitt30ba4362014-08-21 20:18:45 -0700177 bool fHasCustomColorOutput;
178 bool fHasSecondaryOutput;
179 bool fSetupFragPosition;
180 bool fTopLeftFragPosRead;
joshualittb4384b92014-10-21 12:53:15 -0700181 int fCustomColorOutputIndex;
joshualitt30ba4362014-08-21 20:18:45 -0700182
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
185 bool fHasReadDstColor;
186 bool fHasReadFragmentPosition;
joshualittfe1233c2014-10-07 12:16:35 -0700187
joshualitt47bb3822014-10-07 16:43:25 -0700188 friend class GrGLProgramBuilder;
189
egdaniel29bee0f2015-04-29 11:54:42 -0700190 typedef GrGLXPFragmentBuilder INHERITED;
joshualitt30ba4362014-08-21 20:18:45 -0700191};
192
193#endif