blob: 4553ac3a0efc0841821d7a8c1bd110abf3f49b6a [file] [log] [blame]
tomhudson@google.comf9ad8862012-05-11 20:38:48 +00001/*
2 * Copyright 2012 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 GrGLShaderBuilder_DEFINED
9#define GrGLShaderBuilder_DEFINED
10
11#include "GrAllocator.h"
bsalomon@google.com2eaaefd2012-10-29 19:51:22 +000012#include "GrBackendEffectFactory.h"
bsalomon@google.comb8eb2e82013-03-28 13:46:42 +000013#include "GrColor.h"
bsalomon@google.coma469c282012-10-24 18:28:34 +000014#include "GrEffect.h"
commit-bot@chromium.orga0b40282013-09-18 13:00:55 +000015#include "SkTypes.h"
commit-bot@chromium.org3390b9a2013-10-03 15:17:58 +000016#include "gl/GrGLProgramEffects.h"
tomhudson@google.comf9ad8862012-05-11 20:38:48 +000017#include "gl/GrGLSL.h"
bsalomon@google.comdbbc4e22012-07-25 17:48:39 +000018#include "gl/GrGLUniformManager.h"
tomhudson@google.comf9ad8862012-05-11 20:38:48 +000019
bsalomon@google.comf910d3b2013-03-07 17:06:57 +000020#include <stdarg.h>
21
bsalomon@google.comad5e9372012-07-11 18:11:27 +000022class GrGLContextInfo;
bsalomon@google.comc7818882013-03-20 19:19:53 +000023class GrEffectStage;
bsalomon@google.com26e18b52013-03-29 19:22:36 +000024class GrGLProgramDesc;
bsalomon@google.comdbbc4e22012-07-25 17:48:39 +000025
tomhudson@google.comf9ad8862012-05-11 20:38:48 +000026/**
bsalomon@google.comeb715c82012-07-11 15:03:31 +000027 Contains all the incremental state of a shader as it is being built,as well as helpers to
28 manipulate that state.
tomhudson@google.comf9ad8862012-05-11 20:38:48 +000029*/
tomhudson@google.comf9ad8862012-05-11 20:38:48 +000030class GrGLShaderBuilder {
tomhudson@google.comf9ad8862012-05-11 20:38:48 +000031public:
commit-bot@chromium.org5a02cb42013-08-30 20:17:31 +000032 typedef GrTAllocator<GrGLShaderVar> VarArray;
bsalomon@google.com77af6802013-10-02 13:04:56 +000033 typedef GrBackendEffectFactory::EffectKey EffectKey;
commit-bot@chromium.org3390b9a2013-10-03 15:17:58 +000034 typedef GrGLProgramEffects::TextureSampler TextureSampler;
35 typedef GrGLProgramEffects::TransformedCoordsArray TransformedCoordsArray;
bsalomon@google.comf06df1b2012-09-06 20:22:31 +000036
commit-bot@chromium.org74a3a212013-08-30 19:43:59 +000037 enum ShaderVisibility {
38 kVertex_Visibility = 0x1,
39 kGeometry_Visibility = 0x2,
40 kFragment_Visibility = 0x4,
bsalomon@google.comeb715c82012-07-11 15:03:31 +000041 };
42
commit-bot@chromium.org410552a2013-09-30 15:30:27 +000043 GrGLShaderBuilder(GrGpuGL*,
commit-bot@chromium.org5a02cb42013-08-30 20:17:31 +000044 GrGLUniformManager&,
45 const GrGLProgramDesc&,
commit-bot@chromium.orga4acf122013-09-30 15:13:58 +000046 bool needsVertexShader);
tomhudson@google.comf9ad8862012-05-11 20:38:48 +000047
bsalomon@google.comf910d3b2013-03-07 17:06:57 +000048 /**
bsalomon@google.com42eff162013-04-02 12:50:49 +000049 * Use of these features may require a GLSL extension to be enabled. Shaders may not compile
50 * if code is added that uses one of these features without calling enableFeature()
51 */
52 enum GLSLFeature {
53 kStandardDerivatives_GLSLFeature = 0,
54
55 kLastGLSLFeature = kStandardDerivatives_GLSLFeature
56 };
57
58 /**
59 * If the feature is supported then true is returned and any necessary #extension declarations
60 * are added to the shaders. If the feature is not supported then false will be returned.
61 */
62 bool enableFeature(GLSLFeature);
63
64 /**
commit-bot@chromium.org5a02cb42013-08-30 20:17:31 +000065 * Called by GrGLEffects to add code the fragment shader.
bsalomon@google.comf910d3b2013-03-07 17:06:57 +000066 */
bsalomon@google.comf910d3b2013-03-07 17:06:57 +000067 void fsCodeAppendf(const char format[], ...) SK_PRINTF_LIKE(2, 3) {
68 va_list args;
69 va_start(args, format);
commit-bot@chromium.org74a3a212013-08-30 19:43:59 +000070 fFSCode.appendf(format, args);
bsalomon@google.comf910d3b2013-03-07 17:06:57 +000071 va_end(args);
72 }
73
commit-bot@chromium.org74a3a212013-08-30 19:43:59 +000074 void fsCodeAppend(const char* str) { fFSCode.append(str); }
bsalomon@google.comf910d3b2013-03-07 17:06:57 +000075
bsalomon@google.comdbe49f72012-11-05 16:36:02 +000076 /** Appends a 2D texture sample with projection if necessary. coordType must either be Vec2f or
77 Vec3f. The latter is interpreted as projective texture coords. The vec length and swizzle
78 order of the result depends on the GrTextureAccess associated with the TextureSampler. */
bsalomon@google.com868a8e72012-08-30 19:11:34 +000079 void appendTextureLookup(SkString* out,
bsalomon@google.comf06df1b2012-09-06 20:22:31 +000080 const TextureSampler&,
bsalomon@google.comdbe49f72012-11-05 16:36:02 +000081 const char* coordName,
bsalomon@google.com868a8e72012-08-30 19:11:34 +000082 GrSLType coordType = kVec2f_GrSLType) const;
tomhudson@google.com52598142012-05-24 17:44:30 +000083
commit-bot@chromium.org74a3a212013-08-30 19:43:59 +000084 /** Version of above that appends the result to the fragment shader code instead.*/
85 void fsAppendTextureLookup(const TextureSampler&,
86 const char* coordName,
87 GrSLType coordType = kVec2f_GrSLType);
bsalomon@google.comf910d3b2013-03-07 17:06:57 +000088
89
bsalomon@google.com2d8edaf2012-09-07 14:47:31 +000090 /** Does the work of appendTextureLookup and modulates the result by modulation. The result is
91 always a vec4. modulation and the swizzle specified by TextureSampler must both be vec4 or
92 float. If modulation is "" or NULL it this function acts as though appendTextureLookup were
93 called. */
commit-bot@chromium.org74a3a212013-08-30 19:43:59 +000094 void fsAppendTextureLookupAndModulate(const char* modulation,
95 const TextureSampler&,
96 const char* coordName,
97 GrSLType coordType = kVec2f_GrSLType);
bsalomon@google.com34bcb9f2012-08-28 18:20:18 +000098
commit-bot@chromium.org74a3a212013-08-30 19:43:59 +000099 /** Emits a helper function outside of main() in the fragment shader. */
100 void fsEmitFunction(GrSLType returnType,
101 const char* name,
102 int argCnt,
103 const GrGLShaderVar* args,
104 const char* body,
105 SkString* outName);
bsalomon@google.coma1bf0ff2012-08-07 17:36:29 +0000106
commit-bot@chromium.org5a02cb42013-08-30 20:17:31 +0000107 /** Add input/output variable declarations (i.e. 'varying') to the fragment shader. */
108 GrGLShaderVar& fsInputAppend() { return fFSInputs.push_back(); }
commit-bot@chromium.org5a02cb42013-08-30 20:17:31 +0000109
bsalomon@google.com26e18b52013-03-29 19:22:36 +0000110 typedef uint8_t DstReadKey;
bsalomon@google.comb5158812013-05-13 18:50:25 +0000111 typedef uint8_t FragPosKey;
bsalomon@google.com26e18b52013-03-29 19:22:36 +0000112
113 /** Returns a key for adding code to read the copy-of-dst color in service of effects that
114 require reading the dst. It must not return 0 because 0 indicates that there is no dst
bsalomon@google.comb5158812013-05-13 18:50:25 +0000115 copy read at all (in which case this function should not be called). */
bsalomon@google.com26e18b52013-03-29 19:22:36 +0000116 static DstReadKey KeyForDstRead(const GrTexture* dstCopy, const GrGLCaps&);
117
bsalomon@google.comb5158812013-05-13 18:50:25 +0000118 /** 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. */
121 static FragPosKey KeyForFragmentPosition(const GrRenderTarget* dst, const GrGLCaps&);
122
bsalomon@google.com6d003d12012-09-11 15:45:20 +0000123 /** If texture swizzling is available using tex parameters then it is preferred over mangling
124 the generated shader code. This potentially allows greater reuse of cached shaders. */
125 static const GrGLenum* GetTexParamSwizzle(GrPixelConfig config, const GrGLCaps& caps);
126
bsalomon@google.com706f6682012-10-23 14:53:55 +0000127 /** Add a uniform variable to the current program, that has visibility in one or more shaders.
commit-bot@chromium.org74a3a212013-08-30 19:43:59 +0000128 visibility is a bitfield of ShaderVisibility values indicating from which shaders the
129 uniform should be accessible. At least one bit must be set. Geometry shader uniforms are not
bsalomon@google.com777c3aa2012-07-25 20:58:20 +0000130 supported at this time. The actual uniform name will be mangled. If outName is not NULL then
131 it will refer to the final uniform name after return. Use the addUniformArray variant to add
132 an array of uniforms.
tomhudson@google.com242ed6f2012-05-30 17:38:57 +0000133 */
bsalomon@google.comdbbc4e22012-07-25 17:48:39 +0000134 GrGLUniformManager::UniformHandle addUniform(uint32_t visibility,
135 GrSLType type,
136 const char* name,
bsalomon@google.com777c3aa2012-07-25 20:58:20 +0000137 const char** outName = NULL) {
138 return this->addUniformArray(visibility, type, name, GrGLShaderVar::kNonArray, outName);
139 }
140 GrGLUniformManager::UniformHandle addUniformArray(uint32_t visibility,
141 GrSLType type,
142 const char* name,
143 int arrayCount,
144 const char** outName = NULL);
bsalomon@google.com032b2212012-07-16 13:36:18 +0000145
commit-bot@chromium.org7425c122013-08-14 18:14:19 +0000146 const GrGLShaderVar& getUniformVariable(GrGLUniformManager::UniformHandle u) const {
147 return fUniformManager.getBuilderUniform(fUniforms, u).fVariable;
148 }
bsalomon@google.com032b2212012-07-16 13:36:18 +0000149
150 /**
bsalomon@google.com706f6682012-10-23 14:53:55 +0000151 * Shortcut for getUniformVariable(u).c_str()
bsalomon@google.com032b2212012-07-16 13:36:18 +0000152 */
bsalomon@google.comdbbc4e22012-07-25 17:48:39 +0000153 const char* getUniformCStr(GrGLUniformManager::UniformHandle u) const {
bsalomon@google.com032b2212012-07-16 13:36:18 +0000154 return this->getUniformVariable(u).c_str();
155 }
tomhudson@google.com242ed6f2012-05-30 17:38:57 +0000156
bsalomon@google.com77af6802013-10-02 13:04:56 +0000157 /**
158 * This returns a variable name to access the 2D, perspective correct version of the coords in
159 * the fragment shader. If the coordinates at index are 3-dimensional, it immediately emits a
160 * perspective divide into the fragment shader (xy / z) to convert them to 2D.
161 */
162 SkString ensureFSCoords2D(const TransformedCoordsArray&, int index);
163
bsalomon@google.com706f6682012-10-23 14:53:55 +0000164 /** Returns a variable name that represents the position of the fragment in the FS. The position
165 is in device space (e.g. 0,0 is the top left and pixel centers are at half-integers). */
166 const char* fragmentPosition();
167
bsalomon@google.com26e18b52013-03-29 19:22:36 +0000168 /** Returns the color of the destination pixel. This may be NULL if no effect advertised
169 that it will read the destination. */
bsalomon@google.com6b0cf022013-05-03 13:35:14 +0000170 const char* dstColor();
bsalomon@google.com26e18b52013-03-29 19:22:36 +0000171
bsalomon@google.comc7818882013-03-20 19:19:53 +0000172 /**
bsalomon@google.com34cccde2013-01-04 18:34:30 +0000173 * Interfaces used by GrGLProgram.
commit-bot@chromium.org410552a2013-09-30 15:30:27 +0000174 * TODO: These are used by GrGLProgram to insert a mode color filter. Remove these when the
175 * color filter is expressed as a GrEffect.
bsalomon@google.com34cccde2013-01-04 18:34:30 +0000176 */
commit-bot@chromium.org410552a2013-09-30 15:30:27 +0000177 const SkString& getInputColor() const { return fInputColor; }
178 GrSLConstantVec getKnownColorValue() const { return fKnownColorValue; }
179 const SkString& getInputCoverage() const { return fInputCoverage; }
180 GrSLConstantVec getKnownCoverageValue() const { return fKnownCoverageValue; }
bsalomon@google.comad5e9372012-07-11 18:11:27 +0000181
bsalomon@google.com504976e2013-05-09 13:45:02 +0000182 /**
commit-bot@chromium.org3390b9a2013-10-03 15:17:58 +0000183 * Adds code for effects and returns a GrGLProgramEffects* object. The caller is responsible for
184 * deleting it when finished. effectStages contains the effects to add. effectKeys[i] is the key
185 * generated from effectStages[i]. inOutFSColor specifies the input color to the first stage and
186 * is updated to be the output color of the last stage. fsInOutColorKnownValue specifies whether
187 * the input color has a known constant value and is updated to refer to the status of the
188 * output color. The handles to texture samplers for effectStage[i] are added to
189 * effectSamplerHandles[i].
bsalomon@google.com504976e2013-05-09 13:45:02 +0000190 */
commit-bot@chromium.org3390b9a2013-10-03 15:17:58 +0000191 GrGLProgramEffects* createAndEmitEffects(const GrEffectStage* effectStages[],
192 const EffectKey effectKeys[],
193 int effectCnt,
194 SkString* inOutFSColor,
195 GrSLConstantVec* fsInOutColorKnownValue);
bsalomon@google.com26e18b52013-03-29 19:22:36 +0000196
commit-bot@chromium.org410552a2013-09-30 15:30:27 +0000197 const char* getColorOutputName() const;
198 const char* enableSecondaryOutput();
199
bsalomon@google.com706f6682012-10-23 14:53:55 +0000200 GrGLUniformManager::UniformHandle getRTHeightUniform() const { return fRTHeightUniform; }
bsalomon@google.com26e18b52013-03-29 19:22:36 +0000201 GrGLUniformManager::UniformHandle getDstCopyTopLeftUniform() const {
202 return fDstCopyTopLeftUniform;
203 }
204 GrGLUniformManager::UniformHandle getDstCopyScaleUniform() const {
205 return fDstCopyScaleUniform;
206 }
commit-bot@chromium.org410552a2013-09-30 15:30:27 +0000207 GrGLUniformManager::UniformHandle getColorUniform() const { return fColorUniform; }
208 GrGLUniformManager::UniformHandle getCoverageUniform() const { return fCoverageUniform; }
bsalomon@google.com26e18b52013-03-29 19:22:36 +0000209 GrGLUniformManager::UniformHandle getDstCopySamplerUniform() const {
commit-bot@chromium.org3390b9a2013-10-03 15:17:58 +0000210 return fDstCopySamplerUniform;
bsalomon@google.com26e18b52013-03-29 19:22:36 +0000211 }
commit-bot@chromium.orgff6ea262013-03-12 12:26:08 +0000212
commit-bot@chromium.org5a02cb42013-08-30 20:17:31 +0000213 /** Helper class used to build the vertex and geometry shaders. This functionality
214 is kept separate from the rest of GrGLShaderBuilder to allow for shaders programs
215 that only use the fragment shader. */
216 class VertexBuilder {
217 public:
commit-bot@chromium.org410552a2013-09-30 15:30:27 +0000218 VertexBuilder(GrGLShaderBuilder* parent, GrGpuGL* gpu, const GrGLProgramDesc&);
commit-bot@chromium.org5a02cb42013-08-30 20:17:31 +0000219
220 /**
221 * Called by GrGLEffects to add code to one of the shaders.
222 */
223 void vsCodeAppendf(const char format[], ...) SK_PRINTF_LIKE(2, 3) {
224 va_list args;
225 va_start(args, format);
226 fVSCode.appendf(format, args);
227 va_end(args);
commit-bot@chromium.orgff6ea262013-03-12 12:26:08 +0000228 }
commit-bot@chromium.org5a02cb42013-08-30 20:17:31 +0000229
commit-bot@chromium.org5a02cb42013-08-30 20:17:31 +0000230 void vsCodeAppend(const char* str) { fVSCode.append(str); }
commit-bot@chromium.org5a02cb42013-08-30 20:17:31 +0000231
232 /** Add a vertex attribute to the current program that is passed in from the vertex data.
233 Returns false if the attribute was already there, true otherwise. */
234 bool addAttribute(GrSLType type, const char* name);
235
236 /** Add a varying variable to the current program to pass values between vertex and fragment
237 shaders. If the last two parameters are non-NULL, they are filled in with the name
238 generated. */
239 void addVarying(GrSLType type,
240 const char* name,
241 const char** vsOutName = NULL,
242 const char** fsInName = NULL);
243
244 /** Returns a vertex attribute that represents the vertex position in the VS. This is the
245 pre-matrix position and is commonly used by effects to compute texture coords via a matrix.
246 */
247 const GrGLShaderVar& positionAttribute() const { return *fPositionVar; }
248
249 /** Returns a vertex attribute that represents the local coords in the VS. This may be the same
250 as positionAttribute() or it may not be. It depends upon whether the rendering code
251 specified explicit local coords or not in the GrDrawState. */
252 const GrGLShaderVar& localCoordsAttribute() const { return *fLocalCoordsVar; }
253
254 /**
255 * Are explicit local coordinates provided as input to the vertex shader.
256 */
257 bool hasExplicitLocalCoords() const { return (fLocalCoordsVar != fPositionVar); }
258
commit-bot@chromium.org410552a2013-09-30 15:30:27 +0000259 bool addEffectAttribute(int attributeIndex, GrSLType type, const SkString& name);
260 const SkString* getEffectAttributeName(int attributeIndex) const;
261
262 GrGLUniformManager::UniformHandle getViewMatrixUniform() const {
263 return fViewMatrixUniform;
264 }
265
266 bool compileAndAttachShaders(GrGLuint programId) const;
267 void bindProgramLocations(GrGLuint programId) const;
268
269 private:
270 GrGLShaderBuilder* fParent;
271 GrGpuGL* fGpu;
272 const GrGLProgramDesc& fDesc;
273 VarArray fVSAttrs;
274 VarArray fVSOutputs;
275 VarArray fGSInputs;
276 VarArray fGSOutputs;
277
278 SkString fVSCode;
commit-bot@chromium.org5a02cb42013-08-30 20:17:31 +0000279
280 struct AttributePair {
281 void set(int index, const SkString& name) {
282 fIndex = index; fName = name;
283 }
284 int fIndex;
285 SkString fName;
286 };
commit-bot@chromium.org5a02cb42013-08-30 20:17:31 +0000287 SkSTArray<10, AttributePair, true> fEffectAttributes;
288
commit-bot@chromium.org410552a2013-09-30 15:30:27 +0000289 GrGLUniformManager::UniformHandle fViewMatrixUniform;
290
commit-bot@chromium.org5a02cb42013-08-30 20:17:31 +0000291 GrGLShaderVar* fPositionVar;
292 GrGLShaderVar* fLocalCoordsVar;
commit-bot@chromium.orgff6ea262013-03-12 12:26:08 +0000293 };
commit-bot@chromium.org5a02cb42013-08-30 20:17:31 +0000294
295 /** Gets the vertex builder that is used to construct the vertex and geometry shaders.
296 It may be NULL if this shader program is only meant to have a fragment shader. */
297 VertexBuilder* getVertexBuilder() const { return fVertexBuilder.get(); }
commit-bot@chromium.orgff6ea262013-03-12 12:26:08 +0000298
commit-bot@chromium.org410552a2013-09-30 15:30:27 +0000299 bool finish(GrGLuint* outProgramId);
bsalomon@google.com706f6682012-10-23 14:53:55 +0000300
commit-bot@chromium.org410552a2013-09-30 15:30:27 +0000301 const GrGLContextInfo& ctxInfo() const;
robertphillips@google.com13f181f2013-03-02 12:02:08 +0000302
bsalomon@google.com032b2212012-07-16 13:36:18 +0000303private:
bsalomon@google.com032b2212012-07-16 13:36:18 +0000304 void appendDecls(const VarArray&, SkString*) const;
commit-bot@chromium.org74a3a212013-08-30 19:43:59 +0000305 void appendUniformDecls(ShaderVisibility, SkString*) const;
bsalomon@google.com032b2212012-07-16 13:36:18 +0000306
commit-bot@chromium.org410552a2013-09-30 15:30:27 +0000307 bool compileAndAttachShaders(GrGLuint programId) const;
308 void bindProgramLocations(GrGLuint programId) const;
309
bsalomon@google.comdbbc4e22012-07-25 17:48:39 +0000310 typedef GrGLUniformManager::BuilderUniform BuilderUniform;
311 GrGLUniformManager::BuilderUniformArray fUniforms;
bsalomon@google.com032b2212012-07-16 13:36:18 +0000312
bsalomon@google.comad5e9372012-07-11 18:11:27 +0000313private:
commit-bot@chromium.orga0b40282013-09-18 13:00:55 +0000314 class CodeStage : public SkNoncopyable {
bsalomon@google.com504976e2013-05-09 13:45:02 +0000315 public:
commit-bot@chromium.orga4acf122013-09-30 15:13:58 +0000316 CodeStage() : fNextIndex(0), fCurrentIndex(-1), fEffectStage(NULL) {}
bsalomon@google.com504976e2013-05-09 13:45:02 +0000317
318 bool inStageCode() const {
319 this->validate();
commit-bot@chromium.orga4acf122013-09-30 15:13:58 +0000320 return NULL != fEffectStage;
bsalomon@google.com504976e2013-05-09 13:45:02 +0000321 }
322
commit-bot@chromium.orga4acf122013-09-30 15:13:58 +0000323 const GrEffectStage* effectStage() const {
bsalomon@google.com504976e2013-05-09 13:45:02 +0000324 this->validate();
commit-bot@chromium.orga4acf122013-09-30 15:13:58 +0000325 return fEffectStage;
bsalomon@google.com504976e2013-05-09 13:45:02 +0000326 }
327
328 int stageIndex() const {
skia.committer@gmail.com0f20a3f2013-05-10 07:01:04 +0000329 this->validate();
bsalomon@google.com504976e2013-05-09 13:45:02 +0000330 return fCurrentIndex;
331 }
332
commit-bot@chromium.orga0b40282013-09-18 13:00:55 +0000333 class AutoStageRestore : public SkNoncopyable {
bsalomon@google.com504976e2013-05-09 13:45:02 +0000334 public:
commit-bot@chromium.orga4acf122013-09-30 15:13:58 +0000335 AutoStageRestore(CodeStage* codeStage, const GrEffectStage* newStage) {
tfarina@chromium.orgf6de4752013-08-17 00:02:59 +0000336 SkASSERT(NULL != codeStage);
bsalomon@google.com504976e2013-05-09 13:45:02 +0000337 fSavedIndex = codeStage->fCurrentIndex;
commit-bot@chromium.orga4acf122013-09-30 15:13:58 +0000338 fSavedEffectStage = codeStage->fEffectStage;
bsalomon@google.com504976e2013-05-09 13:45:02 +0000339
commit-bot@chromium.orga4acf122013-09-30 15:13:58 +0000340 if (NULL == newStage) {
bsalomon@google.com504976e2013-05-09 13:45:02 +0000341 codeStage->fCurrentIndex = -1;
342 } else {
343 codeStage->fCurrentIndex = codeStage->fNextIndex++;
344 }
commit-bot@chromium.orga4acf122013-09-30 15:13:58 +0000345 codeStage->fEffectStage = newStage;
bsalomon@google.com504976e2013-05-09 13:45:02 +0000346
347 fCodeStage = codeStage;
348 }
349 ~AutoStageRestore() {
350 fCodeStage->fCurrentIndex = fSavedIndex;
commit-bot@chromium.orga4acf122013-09-30 15:13:58 +0000351 fCodeStage->fEffectStage = fSavedEffectStage;
bsalomon@google.com504976e2013-05-09 13:45:02 +0000352 }
353 private:
commit-bot@chromium.orga4acf122013-09-30 15:13:58 +0000354 CodeStage* fCodeStage;
355 int fSavedIndex;
356 const GrEffectStage* fSavedEffectStage;
bsalomon@google.com504976e2013-05-09 13:45:02 +0000357 };
358 private:
commit-bot@chromium.orga4acf122013-09-30 15:13:58 +0000359 void validate() const { SkASSERT((NULL == fEffectStage) == (-1 == fCurrentIndex)); }
360 int fNextIndex;
361 int fCurrentIndex;
362 const GrEffectStage* fEffectStage;
bsalomon@google.com504976e2013-05-09 13:45:02 +0000363 } fCodeStage;
bsalomon@google.com777c3aa2012-07-25 20:58:20 +0000364
bsalomon@google.com42eff162013-04-02 12:50:49 +0000365 /**
366 * Features that should only be enabled by GrGLShaderBuilder itself.
367 */
368 enum GLSLPrivateFeature {
bsalomon@google.com6b0cf022013-05-03 13:35:14 +0000369 kFragCoordConventions_GLSLPrivateFeature = kLastGLSLFeature + 1,
370 kEXTShaderFramebufferFetch_GLSLPrivateFeature,
371 kNVShaderFramebufferFetch_GLSLPrivateFeature,
bsalomon@google.com42eff162013-04-02 12:50:49 +0000372 };
373 bool enablePrivateFeature(GLSLPrivateFeature);
374
commit-bot@chromium.org74a3a212013-08-30 19:43:59 +0000375 // If we ever have VS/GS features we can expand this to take a bitmask of ShaderVisibility and
376 // track the enables separately for each shader.
bsalomon@google.com42eff162013-04-02 12:50:49 +0000377 void addFSFeature(uint32_t featureBit, const char* extensionName);
378
bsalomon@google.com504976e2013-05-09 13:45:02 +0000379 // Generates a name for a variable. The generated string will be name prefixed by the prefix
380 // char (unless the prefix is '\0'). It also mangles the name to be stage-specific if we're
381 // generating stage code.
382 void nameVariable(SkString* out, char prefix, const char* name);
383
bsalomon@google.com26e18b52013-03-29 19:22:36 +0000384 // Interpretation of DstReadKey when generating code
385 enum {
386 kNoDstRead_DstReadKey = 0,
387 kYesDstRead_DstReadKeyBit = 0x1, // Set if we do a dst-copy-read.
388 kUseAlphaConfig_DstReadKeyBit = 0x2, // Set if dst-copy config is alpha only.
389 kTopLeftOrigin_DstReadKeyBit = 0x4, // Set if dst-copy origin is top-left.
390 };
391
bsalomon@google.comb5158812013-05-13 18:50:25 +0000392 enum {
393 kNoFragPosRead_FragPosKey = 0, // The fragment positition will not be needed.
394 kTopLeftFragPosRead_FragPosKey = 0x1,// Read frag pos relative to top-left.
395 kBottomLeftFragPosRead_FragPosKey = 0x2,// Read frag pos relative to bottom-left.
396 };
397
commit-bot@chromium.org410552a2013-09-30 15:30:27 +0000398 GrGpuGL* fGpu;
bsalomon@google.com706f6682012-10-23 14:53:55 +0000399 GrGLUniformManager& fUniformManager;
bsalomon@google.com42eff162013-04-02 12:50:49 +0000400 uint32_t fFSFeaturesAddedMask;
bsalomon@google.com706f6682012-10-23 14:53:55 +0000401 SkString fFSFunctions;
bsalomon@google.com42eff162013-04-02 12:50:49 +0000402 SkString fFSExtensions;
commit-bot@chromium.org5a02cb42013-08-30 20:17:31 +0000403 VarArray fFSInputs;
404 VarArray fFSOutputs;
bsalomon@google.com26e18b52013-03-29 19:22:36 +0000405
bsalomon@google.comf910d3b2013-03-07 17:06:57 +0000406 SkString fFSCode;
bsalomon@google.comf910d3b2013-03-07 17:06:57 +0000407
bsalomon@google.com706f6682012-10-23 14:53:55 +0000408 bool fSetupFragPosition;
commit-bot@chromium.org3390b9a2013-10-03 15:17:58 +0000409 GrGLUniformManager::UniformHandle fDstCopySamplerUniform;
bsalomon@google.com26e18b52013-03-29 19:22:36 +0000410
commit-bot@chromium.org410552a2013-09-30 15:30:27 +0000411 SkString fInputColor;
412 GrSLConstantVec fKnownColorValue;
413 SkString fInputCoverage;
414 GrSLConstantVec fKnownCoverageValue;
415
416 bool fHasCustomColorOutput;
417 bool fHasSecondaryOutput;
418
bsalomon@google.com706f6682012-10-23 14:53:55 +0000419 GrGLUniformManager::UniformHandle fRTHeightUniform;
bsalomon@google.com26e18b52013-03-29 19:22:36 +0000420 GrGLUniformManager::UniformHandle fDstCopyTopLeftUniform;
421 GrGLUniformManager::UniformHandle fDstCopyScaleUniform;
commit-bot@chromium.org410552a2013-09-30 15:30:27 +0000422 GrGLUniformManager::UniformHandle fColorUniform;
423 GrGLUniformManager::UniformHandle fCoverageUniform;
bsalomon@google.com34bcb9f2012-08-28 18:20:18 +0000424
bsalomon@google.comb5158812013-05-13 18:50:25 +0000425 bool fTopLeftFragPosRead;
426
commit-bot@chromium.org410552a2013-09-30 15:30:27 +0000427 SkAutoTDelete<VertexBuilder> fVertexBuilder;
tomhudson@google.comf9ad8862012-05-11 20:38:48 +0000428};
429
430#endif