blob: e998458158af65115066d10c521afca711dd1f5b [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,
ethannicholas22793252016-01-30 09:59:10 -080036 kPixelLocalStorage_GLSLFeature = 1,
37 kLastGLSLFeature = kPixelLocalStorage_GLSLFeature
joshualittb0a8a372014-09-23 09:50:21 -070038 };
39
40 /**
41 * If the feature is supported then true is returned and any necessary #extension declarations
42 * are added to the shaders. If the feature is not supported then false will be returned.
43 */
44 virtual bool enableFeature(GLSLFeature) = 0;
45
46 /**
47 * This returns a variable name to access the 2D, perspective correct version of the coords in
48 * the fragment shader. If the coordinates at index are 3-dimensional, it immediately emits a
49 * perspective divide into the fragment shader (xy / z) to convert them to 2D.
50 */
egdaniel7dc4bd02015-10-29 07:57:01 -070051 virtual SkString ensureFSCoords2D(const GrGLSLTransformedCoordsArray& coords, int index) = 0;
joshualittb0a8a372014-09-23 09:50:21 -070052
53
54 /** Returns a variable name that represents the position of the fragment in the FS. The position
55 is in device space (e.g. 0,0 is the top left and pixel centers are at half-integers). */
56 virtual const char* fragmentPosition() = 0;
57
wangyix7ef45a12015-08-13 06:51:35 -070058 /**
59 * Fragment procs with child procs should call these functions before/after calling emitCode
60 * on a child proc.
61 */
62 void onBeforeChildProcEmitCode();
63 void onAfterChildProcEmitCode();
64
wangyix7ef45a12015-08-13 06:51:35 -070065 const SkString& getMangleString() const { return fMangleString; }
66
egdaniel8dcdedc2015-11-11 06:27:20 -080067 bool hasCustomColorOutput() const { return fHasCustomColorOutput; }
68 bool hasSecondaryOutput() const { return fHasSecondaryOutput; }
69
ethannicholas22793252016-01-30 09:59:10 -080070 void declAppendf(const char* fmt, ...);
71
egdaniel8dcdedc2015-11-11 06:27:20 -080072protected:
73 bool fHasCustomColorOutput;
74 bool fHasSecondaryOutput;
75
joshualittb0a8a372014-09-23 09:50:21 -070076private:
wangyix7ef45a12015-08-13 06:51:35 -070077 /*
78 * State that tracks which child proc in the proc tree is currently emitting code. This is
79 * used to update the fMangleString, which is used to mangle the names of uniforms and functions
80 * emitted by the proc. fSubstageIndices is a stack: its count indicates how many levels deep
81 * we are in the tree, and its second-to-last value is the index of the child proc at that
82 * level which is currently emitting code. For example, if fSubstageIndices = [3, 1, 2, 0], that
83 * means we're currently emitting code for the base proc's 3rd child's 1st child's 2nd child.
84 */
85 SkTArray<int> fSubstageIndices;
86
87 /*
88 * The mangle string is used to mangle the names of uniforms/functions emitted by the child
89 * procs so no duplicate uniforms/functions appear in the generated shader program. The mangle
90 * string is simply based on fSubstageIndices. For example, if fSubstageIndices = [3, 1, 2, 0],
91 * then the manglestring will be "_c3_c1_c2", and any uniform/function emitted by that proc will
92 * have "_c3_c1_c2" appended to its name, which can be interpreted as "base proc's 3rd child's
93 * 1st child's 2nd child".
94 */
95 SkString fMangleString;
96
jvanverth50530632015-04-27 10:36:27 -070097 friend class GrGLPathProcessor;
joshualittabb52a12015-01-13 15:02:10 -080098
egdaniel2d721d32015-11-11 13:06:05 -080099 typedef GrGLSLShaderBuilder INHERITED;
joshualittb0a8a372014-09-23 09:50:21 -0700100};
101
102/*
103 * 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 -0700104 * this builder to create their shader. Because this is the only shader builder the FP sees, we
105 * just call it FPShaderBuilder
joshualittb0a8a372014-09-23 09:50:21 -0700106 */
egdaniel2d721d32015-11-11 13:06:05 -0800107class GrGLSLXPFragmentBuilder : public GrGLSLFragmentBuilder {
joshualittb0a8a372014-09-23 09:50:21 -0700108public:
egdaniel2d721d32015-11-11 13:06:05 -0800109 GrGLSLXPFragmentBuilder(GrGLSLProgramBuilder* program) : INHERITED(program) {}
joshualitt47bb3822014-10-07 16:43:25 -0700110
halcanary96fcdcc2015-08-27 07:41:13 -0700111 /** Returns the variable name that holds the color of the destination pixel. This may be nullptr if
joshualittb0a8a372014-09-23 09:50:21 -0700112 no effect advertised that it will read the destination. */
113 virtual const char* dstColor() = 0;
114
cdalton8917d622015-05-06 13:40:21 -0700115 /** Adds any necessary layout qualifiers in order to legalize the supplied blend equation with
116 this shader. It is only legal to call this method with an advanced blend equation, and only
117 if these equations are supported. */
118 virtual void enableAdvancedBlendEquationIfNeeded(GrBlendEquation) = 0;
119
joshualittb0a8a372014-09-23 09:50:21 -0700120private:
egdaniel2d721d32015-11-11 13:06:05 -0800121 typedef GrGLSLFragmentBuilder INHERITED;
joshualittb0a8a372014-09-23 09:50:21 -0700122};
123
joshualitt47bb3822014-10-07 16:43:25 -0700124// TODO rename to Fragment Builder
egdaniel2d721d32015-11-11 13:06:05 -0800125class GrGLSLFragmentShaderBuilder : public GrGLSLXPFragmentBuilder {
joshualitt30ba4362014-08-21 20:18:45 -0700126public:
joshualitt30ba4362014-08-21 20:18:45 -0700127 typedef uint8_t FragPosKey;
128
joshualitt30ba4362014-08-21 20:18:45 -0700129 /** Returns a key for reading the fragment location. This should only be called if there is an
130 effect that will requires the fragment position. If the fragment position is not required,
131 the key is 0. */
egdaniel574a4c12015-11-02 06:22:44 -0800132 static FragPosKey KeyForFragmentPosition(const GrRenderTarget* dst);
joshualitt30ba4362014-08-21 20:18:45 -0700133
egdaniel2d721d32015-11-11 13:06:05 -0800134 GrGLSLFragmentShaderBuilder(GrGLSLProgramBuilder* program, uint8_t fragPosKey);
joshualitt30ba4362014-08-21 20:18:45 -0700135
joshualitt47bb3822014-10-07 16:43:25 -0700136 // true public interface, defined explicitly in the abstract interfaces above
mtklein36352bf2015-03-25 18:17:31 -0700137 bool enableFeature(GLSLFeature) override;
egdaniel7dc4bd02015-10-29 07:57:01 -0700138 virtual SkString ensureFSCoords2D(const GrGLSLTransformedCoordsArray& coords,
mtklein36352bf2015-03-25 18:17:31 -0700139 int index) override;
140 const char* fragmentPosition() override;
141 const char* dstColor() override;
joshualittdb0d3ca2014-10-07 12:42:26 -0700142
cdalton8917d622015-05-06 13:40:21 -0700143 void enableAdvancedBlendEquationIfNeeded(GrBlendEquation) override;
144
joshualitt74077b92014-10-24 11:26:03 -0700145private:
joshualitt47bb3822014-10-07 16:43:25 -0700146 // Private public interface, used by GrGLProgramBuilder to build a fragment shader
joshualitt47bb3822014-10-07 16:43:25 -0700147 void enableCustomOutput();
148 void enableSecondaryOutput();
149 const char* getPrimaryColorOutputName() const;
150 const char* getSecondaryColorOutputName() const;
joshualitt47bb3822014-10-07 16:43:25 -0700151
egdaniel57d3b032015-11-13 11:57:27 -0800152 // As GLSLProcessors emit code, there are some conditions we need to verify. We use the below
joshualitt47bb3822014-10-07 16:43:25 -0700153 // state to track this. The reset call is called per processor emitted.
154 bool hasReadDstColor() const { return fHasReadDstColor; }
155 bool hasReadFragmentPosition() const { return fHasReadFragmentPosition; }
156 void reset() {
157 fHasReadDstColor = false;
158 fHasReadFragmentPosition = false;
159 }
joshualitt30ba4362014-08-21 20:18:45 -0700160
egdaniel8dcdedc2015-11-11 06:27:20 -0800161 static const char* DeclaredColorOutputName() { return "fsColorOut"; }
162 static const char* DeclaredSecondaryColorOutputName() { return "fsSecondaryColorOut"; }
163
joshualitt74077b92014-10-24 11:26:03 -0700164 /*
165 * An internal call for GrGLProgramBuilder to use to add varyings to the vertex shader
166 */
egdaniel8dcdedc2015-11-11 06:27:20 -0800167 void addVarying(GrGLSLVarying*, GrSLPrecision);
joshualitt74077b92014-10-24 11:26:03 -0700168
egdaniel574a4c12015-11-02 06:22:44 -0800169 void onFinalize() override;
170
joshualitt30ba4362014-08-21 20:18:45 -0700171 /**
egdaniel2d721d32015-11-11 13:06:05 -0800172 * Features that should only be enabled by GrGLSLFragmentShaderBuilder itself.
joshualitt30ba4362014-08-21 20:18:45 -0700173 */
174 enum GLSLPrivateFeature {
175 kFragCoordConventions_GLSLPrivateFeature = kLastGLSLFeature + 1,
cdalton8917d622015-05-06 13:40:21 -0700176 kBlendEquationAdvanced_GLSLPrivateFeature,
kkinnunend94708e2015-07-30 22:47:04 -0700177 kBlendFuncExtended_GLSLPrivateFeature,
bsalomon7ea33f52015-11-22 14:51:00 -0800178 kExternalTexture_GLSLPrivateFeature,
kkinnunend94708e2015-07-30 22:47:04 -0700179 kLastGLSLPrivateFeature = kBlendFuncExtended_GLSLPrivateFeature
joshualitt30ba4362014-08-21 20:18:45 -0700180 };
181
joshualitt47bb3822014-10-07 16:43:25 -0700182 // Interpretation of FragPosKey when generating code
joshualitt30ba4362014-08-21 20:18:45 -0700183 enum {
184 kNoFragPosRead_FragPosKey = 0, // The fragment positition will not be needed.
185 kTopLeftFragPosRead_FragPosKey = 0x1,// Read frag pos relative to top-left.
186 kBottomLeftFragPosRead_FragPosKey = 0x2,// Read frag pos relative to bottom-left.
187 };
188
bsalomon6a44c6a2015-05-26 09:49:05 -0700189 static const char* kDstTextureColorName;
joshualitt47bb3822014-10-07 16:43:25 -0700190
joshualitt30ba4362014-08-21 20:18:45 -0700191 bool fSetupFragPosition;
192 bool fTopLeftFragPosRead;
joshualittb4384b92014-10-21 12:53:15 -0700193 int fCustomColorOutputIndex;
joshualitt30ba4362014-08-21 20:18:45 -0700194
joshualitt47bb3822014-10-07 16:43:25 -0700195 // some state to verify shaders and effects are consistent, this is reset between effects by
196 // the program creator
197 bool fHasReadDstColor;
198 bool fHasReadFragmentPosition;
joshualittfe1233c2014-10-07 12:16:35 -0700199
egdanielfa896322016-01-13 12:19:30 -0800200 friend class GrGLSLProgramBuilder;
joshualitt47bb3822014-10-07 16:43:25 -0700201 friend class GrGLProgramBuilder;
202
egdaniel2d721d32015-11-11 13:06:05 -0800203 typedef GrGLSLXPFragmentBuilder INHERITED;
joshualitt30ba4362014-08-21 20:18:45 -0700204};
205
206#endif