blob: 924e3110dfddc217d2260679aa2021d83c82eada [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
egdaniel7dc4bd02015-10-29 07:57:01 -070013#include "glsl/GrGLSLProcessorTypes.h"
14
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/*
joshualitt47bb3822014-10-07 16:43:25 -070019 * This base class encapsulates the functionality which the GP uses to build fragment shaders
joshualittb0a8a372014-09-23 09:50:21 -070020 */
egdaniel2d721d32015-11-11 13:06:05 -080021class GrGLSLFragmentBuilder : public GrGLSLShaderBuilder {
joshualittb0a8a372014-09-23 09:50:21 -070022public:
egdaniel2d721d32015-11-11 13:06:05 -080023 GrGLSLFragmentBuilder(GrGLSLProgramBuilder* program)
egdaniel8dcdedc2015-11-11 06:27:20 -080024 : INHERITED(program)
25 , fHasCustomColorOutput(false)
26 , fHasSecondaryOutput(false) {
wangyix7ef45a12015-08-13 06:51:35 -070027 fSubstageIndices.push_back(0);
28 }
egdaniel2d721d32015-11-11 13:06:05 -080029 virtual ~GrGLSLFragmentBuilder() {}
joshualittb0a8a372014-09-23 09:50:21 -070030 /**
31 * Use of these features may require a GLSL extension to be enabled. Shaders may not compile
32 * if code is added that uses one of these features without calling enableFeature()
33 */
34 enum GLSLFeature {
35 kStandardDerivatives_GLSLFeature = 0,
36 kLastGLSLFeature = kStandardDerivatives_GLSLFeature
37 };
38
39 /**
40 * If the feature is supported then true is returned and any necessary #extension declarations
41 * are added to the shaders. If the feature is not supported then false will be returned.
42 */
43 virtual bool enableFeature(GLSLFeature) = 0;
44
45 /**
46 * This returns a variable name to access the 2D, perspective correct version of the coords in
47 * the fragment shader. If the coordinates at index are 3-dimensional, it immediately emits a
48 * perspective divide into the fragment shader (xy / z) to convert them to 2D.
49 */
egdaniel7dc4bd02015-10-29 07:57:01 -070050 virtual SkString ensureFSCoords2D(const GrGLSLTransformedCoordsArray& coords, int index) = 0;
joshualittb0a8a372014-09-23 09:50:21 -070051
52
53 /** Returns a variable name that represents the position of the fragment in the FS. The position
54 is in device space (e.g. 0,0 is the top left and pixel centers are at half-integers). */
55 virtual const char* fragmentPosition() = 0;
56
wangyix7ef45a12015-08-13 06:51:35 -070057 /**
58 * Fragment procs with child procs should call these functions before/after calling emitCode
59 * on a child proc.
60 */
61 void onBeforeChildProcEmitCode();
62 void onAfterChildProcEmitCode();
63
wangyix7ef45a12015-08-13 06:51:35 -070064 const SkString& getMangleString() const { return fMangleString; }
65
egdaniel8dcdedc2015-11-11 06:27:20 -080066 bool hasCustomColorOutput() const { return fHasCustomColorOutput; }
67 bool hasSecondaryOutput() const { return fHasSecondaryOutput; }
68
69protected:
70 bool fHasCustomColorOutput;
71 bool fHasSecondaryOutput;
72
joshualittb0a8a372014-09-23 09:50:21 -070073private:
wangyix7ef45a12015-08-13 06:51:35 -070074 /*
75 * State that tracks which child proc in the proc tree is currently emitting code. This is
76 * used to update the fMangleString, which is used to mangle the names of uniforms and functions
77 * emitted by the proc. fSubstageIndices is a stack: its count indicates how many levels deep
78 * we are in the tree, and its second-to-last value is the index of the child proc at that
79 * level which is currently emitting code. For example, if fSubstageIndices = [3, 1, 2, 0], that
80 * means we're currently emitting code for the base proc's 3rd child's 1st child's 2nd child.
81 */
82 SkTArray<int> fSubstageIndices;
83
84 /*
85 * The mangle string is used to mangle the names of uniforms/functions emitted by the child
86 * procs so no duplicate uniforms/functions appear in the generated shader program. The mangle
87 * string is simply based on fSubstageIndices. For example, if fSubstageIndices = [3, 1, 2, 0],
88 * then the manglestring will be "_c3_c1_c2", and any uniform/function emitted by that proc will
89 * have "_c3_c1_c2" appended to its name, which can be interpreted as "base proc's 3rd child's
90 * 1st child's 2nd child".
91 */
92 SkString fMangleString;
93
jvanverth50530632015-04-27 10:36:27 -070094 friend class GrGLPathProcessor;
joshualittabb52a12015-01-13 15:02:10 -080095
egdaniel2d721d32015-11-11 13:06:05 -080096 typedef GrGLSLShaderBuilder INHERITED;
joshualittb0a8a372014-09-23 09:50:21 -070097};
98
99/*
100 * 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 -0700101 * this builder to create their shader. Because this is the only shader builder the FP sees, we
102 * just call it FPShaderBuilder
joshualittb0a8a372014-09-23 09:50:21 -0700103 */
egdaniel2d721d32015-11-11 13:06:05 -0800104class GrGLSLXPFragmentBuilder : public GrGLSLFragmentBuilder {
joshualittb0a8a372014-09-23 09:50:21 -0700105public:
egdaniel2d721d32015-11-11 13:06:05 -0800106 GrGLSLXPFragmentBuilder(GrGLSLProgramBuilder* program) : INHERITED(program) {}
joshualitt47bb3822014-10-07 16:43:25 -0700107
halcanary96fcdcc2015-08-27 07:41:13 -0700108 /** Returns the variable name that holds the color of the destination pixel. This may be nullptr if
joshualittb0a8a372014-09-23 09:50:21 -0700109 no effect advertised that it will read the destination. */
110 virtual const char* dstColor() = 0;
111
cdalton8917d622015-05-06 13:40:21 -0700112 /** Adds any necessary layout qualifiers in order to legalize the supplied blend equation with
113 this shader. It is only legal to call this method with an advanced blend equation, and only
114 if these equations are supported. */
115 virtual void enableAdvancedBlendEquationIfNeeded(GrBlendEquation) = 0;
116
joshualittb0a8a372014-09-23 09:50:21 -0700117private:
egdaniel2d721d32015-11-11 13:06:05 -0800118 typedef GrGLSLFragmentBuilder INHERITED;
joshualittb0a8a372014-09-23 09:50:21 -0700119};
120
joshualitt47bb3822014-10-07 16:43:25 -0700121// TODO rename to Fragment Builder
egdaniel2d721d32015-11-11 13:06:05 -0800122class GrGLSLFragmentShaderBuilder : public GrGLSLXPFragmentBuilder {
joshualitt30ba4362014-08-21 20:18:45 -0700123public:
joshualitt30ba4362014-08-21 20:18:45 -0700124 typedef uint8_t FragPosKey;
125
joshualitt30ba4362014-08-21 20:18:45 -0700126 /** Returns a key for reading the fragment location. This should only be called if there is an
127 effect that will requires the fragment position. If the fragment position is not required,
128 the key is 0. */
egdaniel574a4c12015-11-02 06:22:44 -0800129 static FragPosKey KeyForFragmentPosition(const GrRenderTarget* dst);
joshualitt30ba4362014-08-21 20:18:45 -0700130
egdaniel2d721d32015-11-11 13:06:05 -0800131 GrGLSLFragmentShaderBuilder(GrGLSLProgramBuilder* program, uint8_t fragPosKey);
joshualitt30ba4362014-08-21 20:18:45 -0700132
joshualitt47bb3822014-10-07 16:43:25 -0700133 // true public interface, defined explicitly in the abstract interfaces above
mtklein36352bf2015-03-25 18:17:31 -0700134 bool enableFeature(GLSLFeature) override;
egdaniel7dc4bd02015-10-29 07:57:01 -0700135 virtual SkString ensureFSCoords2D(const GrGLSLTransformedCoordsArray& coords,
mtklein36352bf2015-03-25 18:17:31 -0700136 int index) override;
137 const char* fragmentPosition() override;
138 const char* dstColor() override;
joshualittdb0d3ca2014-10-07 12:42:26 -0700139
cdalton8917d622015-05-06 13:40:21 -0700140 void enableAdvancedBlendEquationIfNeeded(GrBlendEquation) override;
141
joshualitt74077b92014-10-24 11:26:03 -0700142private:
joshualitt47bb3822014-10-07 16:43:25 -0700143 // Private public interface, used by GrGLProgramBuilder to build a fragment shader
joshualitt47bb3822014-10-07 16:43:25 -0700144 void enableCustomOutput();
145 void enableSecondaryOutput();
146 const char* getPrimaryColorOutputName() const;
147 const char* getSecondaryColorOutputName() const;
joshualitt47bb3822014-10-07 16:43:25 -0700148
egdaniel57d3b032015-11-13 11:57:27 -0800149 // As GLSLProcessors emit code, there are some conditions we need to verify. We use the below
joshualitt47bb3822014-10-07 16:43:25 -0700150 // state to track this. The reset call is called per processor emitted.
151 bool hasReadDstColor() const { return fHasReadDstColor; }
152 bool hasReadFragmentPosition() const { return fHasReadFragmentPosition; }
153 void reset() {
154 fHasReadDstColor = false;
155 fHasReadFragmentPosition = false;
156 }
joshualitt30ba4362014-08-21 20:18:45 -0700157
egdaniel8dcdedc2015-11-11 06:27:20 -0800158 static const char* DeclaredColorOutputName() { return "fsColorOut"; }
159 static const char* DeclaredSecondaryColorOutputName() { return "fsSecondaryColorOut"; }
160
joshualitt74077b92014-10-24 11:26:03 -0700161 /*
162 * An internal call for GrGLProgramBuilder to use to add varyings to the vertex shader
163 */
egdaniel8dcdedc2015-11-11 06:27:20 -0800164 void addVarying(GrGLSLVarying*, GrSLPrecision);
joshualitt74077b92014-10-24 11:26:03 -0700165
egdaniel574a4c12015-11-02 06:22:44 -0800166 void onFinalize() override;
167
joshualitt30ba4362014-08-21 20:18:45 -0700168 /**
egdaniel2d721d32015-11-11 13:06:05 -0800169 * Features that should only be enabled by GrGLSLFragmentShaderBuilder itself.
joshualitt30ba4362014-08-21 20:18:45 -0700170 */
171 enum GLSLPrivateFeature {
172 kFragCoordConventions_GLSLPrivateFeature = kLastGLSLFeature + 1,
cdalton8917d622015-05-06 13:40:21 -0700173 kBlendEquationAdvanced_GLSLPrivateFeature,
kkinnunend94708e2015-07-30 22:47:04 -0700174 kBlendFuncExtended_GLSLPrivateFeature,
175 kLastGLSLPrivateFeature = kBlendFuncExtended_GLSLPrivateFeature
joshualitt30ba4362014-08-21 20:18:45 -0700176 };
177
joshualitt47bb3822014-10-07 16:43:25 -0700178 // Interpretation of FragPosKey when generating code
joshualitt30ba4362014-08-21 20:18:45 -0700179 enum {
180 kNoFragPosRead_FragPosKey = 0, // The fragment positition will not be needed.
181 kTopLeftFragPosRead_FragPosKey = 0x1,// Read frag pos relative to top-left.
182 kBottomLeftFragPosRead_FragPosKey = 0x2,// Read frag pos relative to bottom-left.
183 };
184
bsalomon6a44c6a2015-05-26 09:49:05 -0700185 static const char* kDstTextureColorName;
joshualitt47bb3822014-10-07 16:43:25 -0700186
joshualitt30ba4362014-08-21 20:18:45 -0700187 bool fSetupFragPosition;
188 bool fTopLeftFragPosRead;
joshualittb4384b92014-10-21 12:53:15 -0700189 int fCustomColorOutputIndex;
joshualitt30ba4362014-08-21 20:18:45 -0700190
joshualitt47bb3822014-10-07 16:43:25 -0700191 // some state to verify shaders and effects are consistent, this is reset between effects by
192 // the program creator
193 bool fHasReadDstColor;
194 bool fHasReadFragmentPosition;
joshualittfe1233c2014-10-07 12:16:35 -0700195
joshualitt47bb3822014-10-07 16:43:25 -0700196 friend class GrGLProgramBuilder;
197
egdaniel2d721d32015-11-11 13:06:05 -0800198 typedef GrGLSLXPFragmentBuilder INHERITED;
joshualitt30ba4362014-08-21 20:18:45 -0700199};
200
201#endif