blob: ff9ad863bc2ab6dfe8bab38f5d8d72682262f525 [file] [log] [blame]
egdaniel8dcdedc2015-11-11 06:27:20 -08001/*
2 * Copyright 2015 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#include "glsl/GrGLSLProgramBuilder.h"
9
Brian Salomon94efbf52016-11-29 13:43:05 -050010#include "GrCaps.h"
egdanielfa896322016-01-13 12:19:30 -080011#include "GrPipeline.h"
Robert Phillipsd0fe8752019-01-31 14:13:59 -050012#include "GrRenderTarget.h"
Brian Salomon94efbf52016-11-29 13:43:05 -050013#include "GrShaderCaps.h"
Brian Salomon739c5bf2016-11-07 09:53:44 -050014#include "GrTexturePriv.h"
egdanielfa896322016-01-13 12:19:30 -080015#include "glsl/GrGLSLFragmentProcessor.h"
16#include "glsl/GrGLSLGeometryProcessor.h"
egdaniel9f1d4152016-02-10 09:50:38 -080017#include "glsl/GrGLSLVarying.h"
egdanielfa896322016-01-13 12:19:30 -080018#include "glsl/GrGLSLXferProcessor.h"
Robert Phillipsfe8da172018-01-24 14:52:02 +000019#include "SkSLCompiler.h"
egdanielfa896322016-01-13 12:19:30 -080020
egdaniel8dcdedc2015-11-11 06:27:20 -080021const int GrGLSLProgramBuilder::kVarsPerBlock = 8;
22
Robert Phillipsd0fe8752019-01-31 14:13:59 -050023GrGLSLProgramBuilder::GrGLSLProgramBuilder(GrRenderTarget* renderTarget, GrSurfaceOrigin origin,
24 const GrPrimitiveProcessor& primProc,
Greg Daniel9a51a862018-11-30 10:18:14 -050025 const GrTextureProxy* const primProcProxies[],
Brian Salomonff168d92018-06-23 15:17:27 -040026 const GrPipeline& pipeline,
Ethan Nicholas38657112017-02-09 17:01:22 -050027 GrProgramDesc* desc)
Brian Salomonff168d92018-06-23 15:17:27 -040028 : fVS(this)
29 , fGS(this)
30 , fFS(this)
31 , fStageIndex(-1)
Chris Daltond7291ba2019-03-07 14:17:03 -070032 , fRenderTarget(renderTarget)
Robert Phillipsd0fe8752019-01-31 14:13:59 -050033 , fOrigin(origin)
Brian Salomonff168d92018-06-23 15:17:27 -040034 , fPipeline(pipeline)
35 , fPrimProc(primProc)
Greg Daniel9a51a862018-11-30 10:18:14 -050036 , fPrimProcProxies(primProcProxies)
Brian Salomonff168d92018-06-23 15:17:27 -040037 , fDesc(desc)
38 , fGeometryProcessor(nullptr)
39 , fXferProcessor(nullptr)
Brian Salomonff168d92018-06-23 15:17:27 -040040 , fNumFragmentSamplers(0) {}
cdalton9c3f1432016-03-11 10:07:37 -080041
42void GrGLSLProgramBuilder::addFeature(GrShaderFlags shaders,
43 uint32_t featureBit,
44 const char* extensionName) {
45 if (shaders & kVertex_GrShaderFlag) {
46 fVS.addFeature(featureBit, extensionName);
47 }
48 if (shaders & kGeometry_GrShaderFlag) {
csmartdalton276cc412016-11-21 11:55:00 -070049 SkASSERT(this->primitiveProcessor().willUseGeoShader());
cdalton9c3f1432016-03-11 10:07:37 -080050 fGS.addFeature(featureBit, extensionName);
51 }
52 if (shaders & kFragment_GrShaderFlag) {
53 fFS.addFeature(featureBit, extensionName);
54 }
egdanielfa896322016-01-13 12:19:30 -080055}
56
Ethan Nicholas2983f402017-05-08 09:36:08 -040057bool GrGLSLProgramBuilder::emitAndInstallProcs() {
egdanielfa896322016-01-13 12:19:30 -080058 // First we loop over all of the installed processors and collect coord transforms. These will
59 // be sent to the GrGLSLPrimitiveProcessor in its emitCode function
Ethan Nicholas2983f402017-05-08 09:36:08 -040060 SkString inputColor;
61 SkString inputCoverage;
Greg Daniel9a51a862018-11-30 10:18:14 -050062 this->emitAndInstallPrimProc(&inputColor, &inputCoverage);
Ethan Nicholas2983f402017-05-08 09:36:08 -040063 this->emitAndInstallFragProcs(&inputColor, &inputCoverage);
64 this->emitAndInstallXferProc(inputColor, inputCoverage);
Brian Salomon42c456f2017-03-06 11:29:48 -050065 this->emitFSOutputSwizzle(this->pipeline().getXferProcessor().hasSecondaryOutput());
cdalton9c3f1432016-03-11 10:07:37 -080066
Brian Salomon559f5562017-11-15 14:28:33 -050067 return this->checkSamplerCounts();
egdanielfa896322016-01-13 12:19:30 -080068}
69
Greg Daniel9a51a862018-11-30 10:18:14 -050070void GrGLSLProgramBuilder::emitAndInstallPrimProc(SkString* outputColor,
Ethan Nicholas2983f402017-05-08 09:36:08 -040071 SkString* outputCoverage) {
Greg Daniel9a51a862018-11-30 10:18:14 -050072 const GrPrimitiveProcessor& proc = this->primitiveProcessor();
73 const GrTextureProxy* const* primProcProxies = this->primProcProxies();
74
egdanielfa896322016-01-13 12:19:30 -080075 // Program builders have a bit of state we need to clear with each effect
76 AutoStageAdvance adv(this);
77 this->nameExpression(outputColor, "outputColor");
78 this->nameExpression(outputCoverage, "outputCoverage");
79
csmartdalton936f81b2017-02-13 15:45:35 -070080 SkASSERT(!fUniformHandles.fRTAdjustmentUni.isValid());
Robert Phillipsfe8da172018-01-24 14:52:02 +000081 GrShaderFlags rtAdjustVisibility;
csmartdalton936f81b2017-02-13 15:45:35 -070082 if (proc.willUseGeoShader()) {
Robert Phillipsfe8da172018-01-24 14:52:02 +000083 rtAdjustVisibility = kGeometry_GrShaderFlag;
84 } else {
85 rtAdjustVisibility = kVertex_GrShaderFlag;
csmartdalton936f81b2017-02-13 15:45:35 -070086 }
Robert Phillipsfe8da172018-01-24 14:52:02 +000087 fUniformHandles.fRTAdjustmentUni = this->uniformHandler()->addUniform(
88 rtAdjustVisibility,
89 kFloat4_GrSLType,
90 SkSL::Compiler::RTADJUST_NAME);
csmartdalton936f81b2017-02-13 15:45:35 -070091 const char* rtAdjustName =
92 this->uniformHandler()->getUniformCStr(fUniformHandles.fRTAdjustmentUni);
93
egdanielfa896322016-01-13 12:19:30 -080094 // Enclose custom code in a block to avoid namespace conflicts
95 SkString openBrace;
96 openBrace.printf("{ // Stage %d, %s\n", fStageIndex, proc.name());
97 fFS.codeAppend(openBrace.c_str());
98 fVS.codeAppendf("// Primitive Processor %s\n", proc.name());
99
100 SkASSERT(!fGeometryProcessor);
Robert Phillips369e8b72017-08-01 16:13:04 -0400101 fGeometryProcessor.reset(proc.createGLSLInstance(*this->shaderCaps()));
egdanielfa896322016-01-13 12:19:30 -0800102
Brian Salomone782f842018-07-31 13:53:11 -0400103 SkAutoSTMalloc<4, SamplerHandle> texSamplers(proc.numTextureSamplers());
104 for (int i = 0; i < proc.numTextureSamplers(); ++i) {
105 SkString name;
106 name.printf("TextureSampler_%d", i);
107 const auto& sampler = proc.textureSampler(i);
Greg Daniel9a51a862018-11-30 10:18:14 -0500108 const GrTexture* texture = primProcProxies[i]->peekTexture();
109 SkASSERT(sampler.textureType() == texture->texturePriv().textureType());
110 SkASSERT(sampler.config() == texture->config());
111 texSamplers[i] = this->emitSampler(texture,
112 sampler.samplerState(),
113 name.c_str());
Brian Salomone782f842018-07-31 13:53:11 -0400114 }
egdanielfa896322016-01-13 12:19:30 -0800115
bsalomona624bf32016-09-20 09:12:47 -0700116 GrGLSLPrimitiveProcessor::FPCoordTransformHandler transformHandler(fPipeline,
117 &fTransformedCoordVars);
egdanielfa896322016-01-13 12:19:30 -0800118 GrGLSLGeometryProcessor::EmitArgs args(&fVS,
csmartdalton276cc412016-11-21 11:55:00 -0700119 proc.willUseGeoShader() ? &fGS : nullptr,
egdanielfa896322016-01-13 12:19:30 -0800120 &fFS,
121 this->varyingHandler(),
122 this->uniformHandler(),
Brian Salomon94efbf52016-11-29 13:43:05 -0500123 this->shaderCaps(),
egdanielfa896322016-01-13 12:19:30 -0800124 proc,
125 outputColor->c_str(),
126 outputCoverage->c_str(),
csmartdalton936f81b2017-02-13 15:45:35 -0700127 rtAdjustName,
Brian Salomone782f842018-07-31 13:53:11 -0400128 texSamplers.get(),
bsalomona624bf32016-09-20 09:12:47 -0700129 &transformHandler);
egdanielfa896322016-01-13 12:19:30 -0800130 fGeometryProcessor->emitCode(args);
131
132 // We have to check that effects and the code they emit are consistent, ie if an effect
133 // asks for dst color, then the emit code needs to follow suit
cdalton87332102016-02-26 12:22:02 -0800134 SkDEBUGCODE(verify(proc);)
egdanielfa896322016-01-13 12:19:30 -0800135
136 fFS.codeAppend("}");
137}
138
Ethan Nicholas2983f402017-05-08 09:36:08 -0400139void GrGLSLProgramBuilder::emitAndInstallFragProcs(SkString* color, SkString* coverage) {
bsalomona624bf32016-09-20 09:12:47 -0700140 int transformedCoordVarsIdx = 0;
Ethan Nicholas2983f402017-05-08 09:36:08 -0400141 SkString** inOut = &color;
Brian Salomon4d3f5172018-06-07 14:42:52 -0400142 SkSTArray<8, std::unique_ptr<GrGLSLFragmentProcessor>> glslFragmentProcessors;
bsalomona624bf32016-09-20 09:12:47 -0700143 for (int i = 0; i < this->pipeline().numFragmentProcessors(); ++i) {
144 if (i == this->pipeline().numColorFragmentProcessors()) {
145 inOut = &coverage;
146 }
Ethan Nicholas2983f402017-05-08 09:36:08 -0400147 SkString output;
egdanielfa896322016-01-13 12:19:30 -0800148 const GrFragmentProcessor& fp = this->pipeline().getFragmentProcessor(i);
Brian Salomon4d3f5172018-06-07 14:42:52 -0400149 output = this->emitAndInstallFragProc(fp, i, transformedCoordVarsIdx, **inOut, output,
150 &glslFragmentProcessors);
bsalomona624bf32016-09-20 09:12:47 -0700151 GrFragmentProcessor::Iter iter(&fp);
152 while (const GrFragmentProcessor* fp = iter.next()) {
153 transformedCoordVarsIdx += fp->numCoordTransforms();
154 }
155 **inOut = output;
egdanielfa896322016-01-13 12:19:30 -0800156 }
Brian Salomon4d3f5172018-06-07 14:42:52 -0400157 fFragmentProcessorCnt = glslFragmentProcessors.count();
158 fFragmentProcessors.reset(new std::unique_ptr<GrGLSLFragmentProcessor>[fFragmentProcessorCnt]);
159 for (int i = 0; i < fFragmentProcessorCnt; ++i) {
160 fFragmentProcessors[i] = std::move(glslFragmentProcessors[i]);
161 }
egdanielfa896322016-01-13 12:19:30 -0800162}
163
164// TODO Processors cannot output zeros because an empty string is all 1s
Ethan Nicholas2983f402017-05-08 09:36:08 -0400165// the fix is to allow effects to take the SkString directly
Brian Salomon4d3f5172018-06-07 14:42:52 -0400166SkString GrGLSLProgramBuilder::emitAndInstallFragProc(
167 const GrFragmentProcessor& fp,
168 int index,
169 int transformedCoordVarsIdx,
170 const SkString& input,
171 SkString output,
172 SkTArray<std::unique_ptr<GrGLSLFragmentProcessor>>* glslFragmentProcessors) {
Ethan Nicholas2983f402017-05-08 09:36:08 -0400173 SkASSERT(input.size());
egdanielfa896322016-01-13 12:19:30 -0800174 // Program builders have a bit of state we need to clear with each effect
175 AutoStageAdvance adv(this);
Ethan Nicholas2983f402017-05-08 09:36:08 -0400176 this->nameExpression(&output, "output");
egdanielfa896322016-01-13 12:19:30 -0800177
178 // Enclose custom code in a block to avoid namespace conflicts
179 SkString openBrace;
180 openBrace.printf("{ // Stage %d, %s\n", fStageIndex, fp.name());
181 fFS.codeAppend(openBrace.c_str());
182
183 GrGLSLFragmentProcessor* fragProc = fp.createGLSLInstance();
184
Brian Salomone782f842018-07-31 13:53:11 -0400185 SkSTArray<4, SamplerHandle> texSamplers;
186 GrFragmentProcessor::Iter fpIter(&fp);
187 int samplerIdx = 0;
188 while (const auto* subFP = fpIter.next()) {
189 for (int i = 0; i < subFP->numTextureSamplers(); ++i) {
190 SkString name;
191 name.printf("TextureSampler_%d", samplerIdx++);
192 const auto& sampler = subFP->textureSampler(i);
Greg Daniel9a51a862018-11-30 10:18:14 -0500193 texSamplers.emplace_back(this->emitSampler(sampler.peekTexture(),
194 sampler.samplerState(),
Greg Daniel0f70be82018-10-08 17:35:08 +0000195 name.c_str()));
Brian Salomone782f842018-07-31 13:53:11 -0400196 }
bsalomonb58a2b42016-09-26 06:55:02 -0700197 }
egdanielfa896322016-01-13 12:19:30 -0800198
bsalomona624bf32016-09-20 09:12:47 -0700199 const GrShaderVar* coordVars = fTransformedCoordVars.begin() + transformedCoordVarsIdx;
200 GrGLSLFragmentProcessor::TransformedCoordVars coords(&fp, coordVars);
Brian Salomone782f842018-07-31 13:53:11 -0400201 GrGLSLFragmentProcessor::TextureSamplers textureSamplers(&fp, texSamplers.begin());
egdanielfa896322016-01-13 12:19:30 -0800202 GrGLSLFragmentProcessor::EmitArgs args(&fFS,
203 this->uniformHandler(),
Brian Salomon94efbf52016-11-29 13:43:05 -0500204 this->shaderCaps(),
egdanielfa896322016-01-13 12:19:30 -0800205 fp,
Ethan Nicholas2983f402017-05-08 09:36:08 -0400206 output.c_str(),
207 input.c_str(),
bsalomona624bf32016-09-20 09:12:47 -0700208 coords,
Brian Salomon662ea4b2018-07-12 14:53:49 -0400209 textureSamplers);
dvonbeck9b03e7b2016-08-01 11:01:56 -0700210
egdanielfa896322016-01-13 12:19:30 -0800211 fragProc->emitCode(args);
212
213 // We have to check that effects and the code they emit are consistent, ie if an effect
214 // asks for dst color, then the emit code needs to follow suit
cdalton87332102016-02-26 12:22:02 -0800215 SkDEBUGCODE(verify(fp);)
Brian Salomon4d3f5172018-06-07 14:42:52 -0400216 glslFragmentProcessors->emplace_back(fragProc);
egdanielfa896322016-01-13 12:19:30 -0800217
218 fFS.codeAppend("}");
Ethan Nicholas2983f402017-05-08 09:36:08 -0400219 return output;
egdanielfa896322016-01-13 12:19:30 -0800220}
221
Ethan Nicholas2983f402017-05-08 09:36:08 -0400222void GrGLSLProgramBuilder::emitAndInstallXferProc(const SkString& colorIn,
223 const SkString& coverageIn) {
egdanielfa896322016-01-13 12:19:30 -0800224 // Program builders have a bit of state we need to clear with each effect
225 AutoStageAdvance adv(this);
226
227 SkASSERT(!fXferProcessor);
Brian Salomon18dfa982017-04-03 16:57:43 -0400228 const GrXferProcessor& xp = fPipeline.getXferProcessor();
Robert Phillips369e8b72017-08-01 16:13:04 -0400229 fXferProcessor.reset(xp.createGLSLInstance());
egdanielfa896322016-01-13 12:19:30 -0800230
231 // Enable dual source secondary output if we have one
232 if (xp.hasSecondaryOutput()) {
233 fFS.enableSecondaryOutput();
234 }
235
Brian Salomon94efbf52016-11-29 13:43:05 -0500236 if (this->shaderCaps()->mustDeclareFragmentShaderOutput()) {
egdanielfa896322016-01-13 12:19:30 -0800237 fFS.enableCustomOutput();
238 }
239
240 SkString openBrace;
241 openBrace.printf("{ // Xfer Processor: %s\n", xp.name());
242 fFS.codeAppend(openBrace.c_str());
243
Brian Salomon18dfa982017-04-03 16:57:43 -0400244 SamplerHandle dstTextureSamplerHandle;
245 GrSurfaceOrigin dstTextureOrigin = kTopLeft_GrSurfaceOrigin;
Robert Phillipsbb581ce2017-05-29 15:05:15 -0400246
247 if (GrTexture* dstTexture = fPipeline.peekDstTexture()) {
Brian Salomon18dfa982017-04-03 16:57:43 -0400248 // GrProcessor::TextureSampler sampler(dstTexture);
249 SkString name("DstTextureSampler");
250 dstTextureSamplerHandle =
Greg Daniel9a51a862018-11-30 10:18:14 -0500251 this->emitSampler(dstTexture, GrSamplerState(), "DstTextureSampler");
Robert Phillips16d8ec62017-07-27 16:16:25 -0400252 dstTextureOrigin = fPipeline.dstTextureProxy()->origin();
Brian Salomon60dd8c72018-07-30 10:24:13 -0400253 SkASSERT(dstTexture->texturePriv().textureType() != GrTextureType::kExternal);
Brian Salomon18dfa982017-04-03 16:57:43 -0400254 }
egdanielfa896322016-01-13 12:19:30 -0800255
Brian Osman65cdd612019-03-14 17:01:57 -0400256 SkString finalInColor;
257 if (colorIn.size()) {
258 if (this->desc()->header().fClampBlendInput) {
259 finalInColor.printf("saturate(%s)", colorIn.c_str());
260 } else {
261 finalInColor = colorIn;
262 }
263 } else {
264 finalInColor = "float4(1)";
265 }
266
egdanielfa896322016-01-13 12:19:30 -0800267 GrGLSLXferProcessor::EmitArgs args(&fFS,
268 this->uniformHandler(),
Brian Salomon94efbf52016-11-29 13:43:05 -0500269 this->shaderCaps(),
Brian Salomon18dfa982017-04-03 16:57:43 -0400270 xp,
Brian Osman65cdd612019-03-14 17:01:57 -0400271 finalInColor.c_str(),
Ethan Nicholas5af9ea32017-07-28 15:19:46 -0400272 coverageIn.size() ? coverageIn.c_str() : "float4(1)",
egdanielfa896322016-01-13 12:19:30 -0800273 fFS.getPrimaryColorOutputName(),
274 fFS.getSecondaryColorOutputName(),
Brian Salomon18dfa982017-04-03 16:57:43 -0400275 dstTextureSamplerHandle,
276 dstTextureOrigin);
egdanielfa896322016-01-13 12:19:30 -0800277 fXferProcessor->emitCode(args);
278
279 // We have to check that effects and the code they emit are consistent, ie if an effect
280 // asks for dst color, then the emit code needs to follow suit
cdalton87332102016-02-26 12:22:02 -0800281 SkDEBUGCODE(verify(xp);)
egdanielfa896322016-01-13 12:19:30 -0800282 fFS.codeAppend("}");
283}
284
Greg Daniel9a51a862018-11-30 10:18:14 -0500285GrGLSLProgramBuilder::SamplerHandle GrGLSLProgramBuilder::emitSampler(const GrTexture* texture,
286 const GrSamplerState& state,
Greg Daniel0f70be82018-10-08 17:35:08 +0000287 const char* name) {
288 ++fNumFragmentSamplers;
Greg Daniel9a51a862018-11-30 10:18:14 -0500289 return this->uniformHandler()->addSampler(texture, state, name, this->shaderCaps());
Brian Salomonf9f45122016-11-29 11:59:17 -0500290}
291
egdanielfa896322016-01-13 12:19:30 -0800292void GrGLSLProgramBuilder::emitFSOutputSwizzle(bool hasSecondaryOutput) {
293 // Swizzle the fragment shader outputs if necessary.
294 GrSwizzle swizzle;
Ethan Nicholas38657112017-02-09 17:01:22 -0500295 swizzle.setFromKey(this->desc()->header().fOutputSwizzle);
egdanielfa896322016-01-13 12:19:30 -0800296 if (swizzle != GrSwizzle::RGBA()) {
297 fFS.codeAppendf("%s = %s.%s;", fFS.getPrimaryColorOutputName(),
298 fFS.getPrimaryColorOutputName(),
299 swizzle.c_str());
300 if (hasSecondaryOutput) {
301 fFS.codeAppendf("%s = %s.%s;", fFS.getSecondaryColorOutputName(),
302 fFS.getSecondaryColorOutputName(),
303 swizzle.c_str());
304 }
305 }
306}
307
cdalton9c3f1432016-03-11 10:07:37 -0800308bool GrGLSLProgramBuilder::checkSamplerCounts() {
Brian Salomon1edc5b92016-11-29 13:43:46 -0500309 const GrShaderCaps& shaderCaps = *this->shaderCaps();
Brian Salomon1edc5b92016-11-29 13:43:46 -0500310 if (fNumFragmentSamplers > shaderCaps.maxFragmentSamplers()) {
cdalton9c3f1432016-03-11 10:07:37 -0800311 GrCapsDebugf(this->caps(), "Program would use too many fragment samplers\n");
312 return false;
313 }
cdalton9c3f1432016-03-11 10:07:37 -0800314 return true;
315}
316
cdalton87332102016-02-26 12:22:02 -0800317#ifdef SK_DEBUG
egdanielfa896322016-01-13 12:19:30 -0800318void GrGLSLProgramBuilder::verify(const GrPrimitiveProcessor& gp) {
Chris Daltond7291ba2019-03-07 14:17:03 -0700319 SkASSERT(!fFS.fHasReadDstColorThisStage_DebugOnly);
320 SkASSERT(fFS.fUsedProcessorFeaturesThisStage_DebugOnly == gp.requestedFeatures());
egdanielfa896322016-01-13 12:19:30 -0800321}
322
323void GrGLSLProgramBuilder::verify(const GrFragmentProcessor& fp) {
Chris Daltond7291ba2019-03-07 14:17:03 -0700324 SkASSERT(!fFS.fHasReadDstColorThisStage_DebugOnly);
325 SkASSERT(fFS.fUsedProcessorFeaturesThisStage_DebugOnly == fp.requestedFeatures());
326}
327
328void GrGLSLProgramBuilder::verify(const GrXferProcessor& xp) {
329 SkASSERT(xp.willReadDstColor() == fFS.fHasReadDstColorThisStage_DebugOnly);
330 SkASSERT(fFS.fUsedProcessorFeaturesThisStage_DebugOnly == xp.requestedFeatures());
egdaniel8dcdedc2015-11-11 06:27:20 -0800331}
cdalton87332102016-02-26 12:22:02 -0800332#endif
egdaniel8dcdedc2015-11-11 06:27:20 -0800333
334void GrGLSLProgramBuilder::nameVariable(SkString* out, char prefix, const char* name, bool mangle) {
335 if ('\0' == prefix) {
336 *out = name;
337 } else {
338 out->printf("%c%s", prefix, name);
339 }
340 if (mangle) {
341 if (out->endsWith('_')) {
342 // Names containing "__" are reserved.
343 out->append("x");
344 }
345 out->appendf("_Stage%d%s", fStageIndex, fFS.getMangleString().c_str());
346 }
347}
348
Ethan Nicholas2983f402017-05-08 09:36:08 -0400349void GrGLSLProgramBuilder::nameExpression(SkString* output, const char* baseName) {
egdanielfa896322016-01-13 12:19:30 -0800350 // create var to hold stage result. If we already have a valid output name, just use that
351 // otherwise create a new mangled one. This name is only valid if we are reordering stages
352 // and have to tell stage exactly where to put its output.
353 SkString outName;
Ethan Nicholas2983f402017-05-08 09:36:08 -0400354 if (output->size()) {
egdanielfa896322016-01-13 12:19:30 -0800355 outName = output->c_str();
356 } else {
357 this->nameVariable(&outName, '\0', baseName);
358 }
Ethan Nicholasf7b88202017-09-18 14:10:39 -0400359 fFS.codeAppendf("half4 %s;", outName.c_str());
egdanielfa896322016-01-13 12:19:30 -0800360 *output = outName;
361}
362
cdalton5e58cee2016-02-11 12:49:47 -0800363void GrGLSLProgramBuilder::appendUniformDecls(GrShaderFlags visibility, SkString* out) const {
egdaniel7ea439b2015-12-03 09:20:44 -0800364 this->uniformHandler()->appendUniformDecls(visibility, out);
365}
366
Ethan Nicholascd700e92018-08-24 16:43:57 -0400367void GrGLSLProgramBuilder::addRTWidthUniform(const char* name) {
368 SkASSERT(!fUniformHandles.fRTWidthUni.isValid());
369 GrGLSLUniformHandler* uniformHandler = this->uniformHandler();
370 fUniformHandles.fRTWidthUni =
Ethan Nicholas858fecc2019-03-07 13:19:18 -0500371 uniformHandler->internalAddUniformArray(kFragment_GrShaderFlag, kHalf_GrSLType, name,
372 false, 0, nullptr);
Ethan Nicholascd700e92018-08-24 16:43:57 -0400373}
374
Greg Daniele6ab9982018-08-22 13:56:32 +0000375void GrGLSLProgramBuilder::addRTHeightUniform(const char* name) {
376 SkASSERT(!fUniformHandles.fRTHeightUni.isValid());
egdaniel7ea439b2015-12-03 09:20:44 -0800377 GrGLSLUniformHandler* uniformHandler = this->uniformHandler();
Greg Daniele6ab9982018-08-22 13:56:32 +0000378 fUniformHandles.fRTHeightUni =
Ethan Nicholas858fecc2019-03-07 13:19:18 -0500379 uniformHandler->internalAddUniformArray(kFragment_GrShaderFlag, kHalf_GrSLType, name,
380 false, 0, nullptr);
egdaniel8dcdedc2015-11-11 06:27:20 -0800381}
382
egdaniel9f1d4152016-02-10 09:50:38 -0800383void GrGLSLProgramBuilder::finalizeShaders() {
384 this->varyingHandler()->finalize();
cdalton5e58cee2016-02-11 12:49:47 -0800385 fVS.finalize(kVertex_GrShaderFlag);
csmartdalton276cc412016-11-21 11:55:00 -0700386 if (this->primitiveProcessor().willUseGeoShader()) {
Brian Salomon94efbf52016-11-29 13:43:05 -0500387 SkASSERT(this->shaderCaps()->geometryShaderSupport());
csmartdalton276cc412016-11-21 11:55:00 -0700388 fGS.finalize(kGeometry_GrShaderFlag);
389 }
cdalton5e58cee2016-02-11 12:49:47 -0800390 fFS.finalize(kFragment_GrShaderFlag);
egdaniel9f1d4152016-02-10 09:50:38 -0800391}