blob: 271bdd726b12616cfa7f7d9011cf941a952e57f0 [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
Mike Kleinc0bd9f92019-04-23 12:05:21 -05008#include "src/gpu/glsl/GrGLSLProgramBuilder.h"
egdaniel8dcdedc2015-11-11 06:27:20 -08009
John Stilesfbd050b2020-08-03 13:21:46 -040010#include <memory>
11
Mike Kleinc0bd9f92019-04-23 12:05:21 -050012#include "src/gpu/GrCaps.h"
13#include "src/gpu/GrPipeline.h"
Brian Salomon201cdbb2019-08-14 17:00:30 -040014#include "src/gpu/GrRenderTarget.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050015#include "src/gpu/GrShaderCaps.h"
Brian Salomon4cfae3b2020-07-23 10:33:24 -040016#include "src/gpu/GrTexture.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050017#include "src/gpu/glsl/GrGLSLFragmentProcessor.h"
18#include "src/gpu/glsl/GrGLSLGeometryProcessor.h"
19#include "src/gpu/glsl/GrGLSLVarying.h"
20#include "src/gpu/glsl/GrGLSLXferProcessor.h"
21#include "src/sksl/SkSLCompiler.h"
Ethan Nicholas570506d2021-02-11 13:19:38 -050022#include "src/sksl/dsl/priv/DSLFPs.h"
egdanielfa896322016-01-13 12:19:30 -080023
egdaniel8dcdedc2015-11-11 06:27:20 -080024const int GrGLSLProgramBuilder::kVarsPerBlock = 8;
25
Robert Phillips65a77752019-10-02 15:22:24 -040026GrGLSLProgramBuilder::GrGLSLProgramBuilder(GrRenderTarget* renderTarget,
Stephen Whiteb1857852020-02-07 15:33:23 +000027 const GrProgramDesc& desc,
28 const GrProgramInfo& programInfo)
Brian Salomonff168d92018-06-23 15:17:27 -040029 : fVS(this)
30 , fGS(this)
31 , fFS(this)
32 , fStageIndex(-1)
Chris Daltond7291ba2019-03-07 14:17:03 -070033 , fRenderTarget(renderTarget)
Austin Eng77fdf662020-02-07 01:26:16 +000034 , fDesc(desc)
Stephen Whiteb1857852020-02-07 15:33:23 +000035 , fProgramInfo(programInfo)
Brian Salomonff168d92018-06-23 15:17:27 -040036 , fGeometryProcessor(nullptr)
37 , fXferProcessor(nullptr)
Brian Salomonff168d92018-06-23 15:17:27 -040038 , fNumFragmentSamplers(0) {}
cdalton9c3f1432016-03-11 10:07:37 -080039
40void GrGLSLProgramBuilder::addFeature(GrShaderFlags shaders,
41 uint32_t featureBit,
42 const char* extensionName) {
43 if (shaders & kVertex_GrShaderFlag) {
44 fVS.addFeature(featureBit, extensionName);
45 }
46 if (shaders & kGeometry_GrShaderFlag) {
csmartdalton276cc412016-11-21 11:55:00 -070047 SkASSERT(this->primitiveProcessor().willUseGeoShader());
cdalton9c3f1432016-03-11 10:07:37 -080048 fGS.addFeature(featureBit, extensionName);
49 }
50 if (shaders & kFragment_GrShaderFlag) {
51 fFS.addFeature(featureBit, extensionName);
52 }
egdanielfa896322016-01-13 12:19:30 -080053}
54
Ethan Nicholas2983f402017-05-08 09:36:08 -040055bool GrGLSLProgramBuilder::emitAndInstallProcs() {
egdanielfa896322016-01-13 12:19:30 -080056 // First we loop over all of the installed processors and collect coord transforms. These will
57 // be sent to the GrGLSLPrimitiveProcessor in its emitCode function
Ethan Nicholas570506d2021-02-11 13:19:38 -050058 SkSL::dsl::Start(this->shaderCompiler());
Ethan Nicholas2983f402017-05-08 09:36:08 -040059 SkString inputColor;
60 SkString inputCoverage;
Greg Daniel9a51a862018-11-30 10:18:14 -050061 this->emitAndInstallPrimProc(&inputColor, &inputCoverage);
Ethan Nicholas2983f402017-05-08 09:36:08 -040062 this->emitAndInstallFragProcs(&inputColor, &inputCoverage);
63 this->emitAndInstallXferProc(inputColor, inputCoverage);
Ethan Nicholas58430122020-04-14 09:54:02 -040064 fGeometryProcessor->emitTransformCode(&fVS, this->uniformHandler());
Ethan Nicholas570506d2021-02-11 13:19:38 -050065 SkSL::dsl::End();
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
Chris Dalton1b1b0d52020-03-03 12:00:59 -070070void GrGLSLProgramBuilder::emitAndInstallPrimProc(SkString* outputColor, SkString* outputCoverage) {
Greg Daniel9a51a862018-11-30 10:18:14 -050071 const GrPrimitiveProcessor& proc = this->primitiveProcessor();
Robert Phillips66ae3042019-10-10 12:57:58 -040072
egdanielfa896322016-01-13 12:19:30 -080073 // Program builders have a bit of state we need to clear with each effect
74 AutoStageAdvance adv(this);
75 this->nameExpression(outputColor, "outputColor");
76 this->nameExpression(outputCoverage, "outputCoverage");
77
csmartdalton936f81b2017-02-13 15:45:35 -070078 SkASSERT(!fUniformHandles.fRTAdjustmentUni.isValid());
Robert Phillipsfe8da172018-01-24 14:52:02 +000079 GrShaderFlags rtAdjustVisibility;
csmartdalton936f81b2017-02-13 15:45:35 -070080 if (proc.willUseGeoShader()) {
Robert Phillipsfe8da172018-01-24 14:52:02 +000081 rtAdjustVisibility = kGeometry_GrShaderFlag;
Chris Dalton5a2f9622019-12-27 14:56:38 -070082 } else if (proc.willUseTessellationShaders()) {
83 rtAdjustVisibility = kTessEvaluation_GrShaderFlag;
Robert Phillipsfe8da172018-01-24 14:52:02 +000084 } 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(
Ethan Nicholas16464c32020-04-06 13:53:05 -040088 nullptr, rtAdjustVisibility, kFloat4_GrSLType, SkSL::Compiler::RTADJUST_NAME);
csmartdalton936f81b2017-02-13 15:45:35 -070089
egdanielfa896322016-01-13 12:19:30 -080090 // Enclose custom code in a block to avoid namespace conflicts
91 SkString openBrace;
92 openBrace.printf("{ // Stage %d, %s\n", fStageIndex, proc.name());
93 fFS.codeAppend(openBrace.c_str());
94 fVS.codeAppendf("// Primitive Processor %s\n", proc.name());
95
96 SkASSERT(!fGeometryProcessor);
Robert Phillips369e8b72017-08-01 16:13:04 -040097 fGeometryProcessor.reset(proc.createGLSLInstance(*this->shaderCaps()));
egdanielfa896322016-01-13 12:19:30 -080098
Brian Salomone782f842018-07-31 13:53:11 -040099 SkAutoSTMalloc<4, SamplerHandle> texSamplers(proc.numTextureSamplers());
100 for (int i = 0; i < proc.numTextureSamplers(); ++i) {
101 SkString name;
102 name.printf("TextureSampler_%d", i);
103 const auto& sampler = proc.textureSampler(i);
Chris Dalton1b1b0d52020-03-03 12:00:59 -0700104 texSamplers[i] = this->emitSampler(proc.textureSampler(i).backendFormat(),
Greg Daniel9a51a862018-11-30 10:18:14 -0500105 sampler.samplerState(),
Greg Daniel2c3398d2019-06-19 11:58:01 -0400106 sampler.swizzle(),
Greg Daniel9a51a862018-11-30 10:18:14 -0500107 name.c_str());
Brian Salomone782f842018-07-31 13:53:11 -0400108 }
egdanielfa896322016-01-13 12:19:30 -0800109
Robert Phillips901aff02019-10-08 12:32:56 -0400110 GrGLSLPrimitiveProcessor::FPCoordTransformHandler transformHandler(this->pipeline(),
bsalomona624bf32016-09-20 09:12:47 -0700111 &fTransformedCoordVars);
egdanielfa896322016-01-13 12:19:30 -0800112 GrGLSLGeometryProcessor::EmitArgs args(&fVS,
csmartdalton276cc412016-11-21 11:55:00 -0700113 proc.willUseGeoShader() ? &fGS : nullptr,
egdanielfa896322016-01-13 12:19:30 -0800114 &fFS,
115 this->varyingHandler(),
116 this->uniformHandler(),
Brian Salomon94efbf52016-11-29 13:43:05 -0500117 this->shaderCaps(),
egdanielfa896322016-01-13 12:19:30 -0800118 proc,
119 outputColor->c_str(),
120 outputCoverage->c_str(),
Brian Salomone782f842018-07-31 13:53:11 -0400121 texSamplers.get(),
bsalomona624bf32016-09-20 09:12:47 -0700122 &transformHandler);
egdanielfa896322016-01-13 12:19:30 -0800123 fGeometryProcessor->emitCode(args);
124
125 // We have to check that effects and the code they emit are consistent, ie if an effect
126 // asks for dst color, then the emit code needs to follow suit
cdalton87332102016-02-26 12:22:02 -0800127 SkDEBUGCODE(verify(proc);)
egdanielfa896322016-01-13 12:19:30 -0800128
129 fFS.codeAppend("}");
130}
131
Ethan Nicholas2983f402017-05-08 09:36:08 -0400132void GrGLSLProgramBuilder::emitAndInstallFragProcs(SkString* color, SkString* coverage) {
bsalomona624bf32016-09-20 09:12:47 -0700133 int transformedCoordVarsIdx = 0;
Brian Salomond90b3d32020-07-09 12:04:31 -0400134 int fpCount = this->pipeline().numFragmentProcessors();
John Stilesfbd050b2020-08-03 13:21:46 -0400135 fFragmentProcessors = std::make_unique<std::unique_ptr<GrGLSLFragmentProcessor>[]>(fpCount);
Brian Salomond90b3d32020-07-09 12:04:31 -0400136 for (int i = 0; i < fpCount; ++i) {
John Stilesd3feb6f2020-07-23 18:18:12 -0400137 SkString* inOut = this->pipeline().isColorFragmentProcessor(i) ? color : coverage;
Ethan Nicholas2983f402017-05-08 09:36:08 -0400138 SkString output;
egdanielfa896322016-01-13 12:19:30 -0800139 const GrFragmentProcessor& fp = this->pipeline().getFragmentProcessor(i);
Brian Salomond90b3d32020-07-09 12:04:31 -0400140 fFragmentProcessors[i] = std::unique_ptr<GrGLSLFragmentProcessor>(fp.createGLSLInstance());
John Stilesd3feb6f2020-07-23 18:18:12 -0400141 output = this->emitFragProc(fp, *fFragmentProcessors[i], transformedCoordVarsIdx, *inOut,
Brian Salomond90b3d32020-07-09 12:04:31 -0400142 output);
Brian Salomon87f4d292020-07-09 12:48:38 -0400143 for (const auto& subFP : GrFragmentProcessor::FPRange(fp)) {
Brian Osman9cf98dc2020-07-01 17:21:27 -0400144 transformedCoordVarsIdx += subFP.numVaryingCoordsUsed();
bsalomona624bf32016-09-20 09:12:47 -0700145 }
John Stilesd3feb6f2020-07-23 18:18:12 -0400146 *inOut = std::move(output);
egdanielfa896322016-01-13 12:19:30 -0800147 }
148}
149
Brian Salomond90b3d32020-07-09 12:04:31 -0400150SkString GrGLSLProgramBuilder::emitFragProc(const GrFragmentProcessor& fp,
151 GrGLSLFragmentProcessor& glslFP,
152 int transformedCoordVarsIdx,
153 const SkString& input,
154 SkString output) {
Ethan Nicholas2983f402017-05-08 09:36:08 -0400155 SkASSERT(input.size());
egdanielfa896322016-01-13 12:19:30 -0800156 // Program builders have a bit of state we need to clear with each effect
157 AutoStageAdvance adv(this);
Ethan Nicholas2983f402017-05-08 09:36:08 -0400158 this->nameExpression(&output, "output");
egdanielfa896322016-01-13 12:19:30 -0800159
Brian Salomone782f842018-07-31 13:53:11 -0400160 int samplerIdx = 0;
Brian Salomond90b3d32020-07-09 12:04:31 -0400161 for (auto [subFP, subGLSLFP] : GrGLSLFragmentProcessor::ParallelRange(fp, glslFP)) {
162 if (auto* te = subFP.asTextureEffect()) {
Brian Salomone782f842018-07-31 13:53:11 -0400163 SkString name;
164 name.printf("TextureSampler_%d", samplerIdx++);
Brian Salomond90b3d32020-07-09 12:04:31 -0400165
166 GrSamplerState samplerState = te->samplerState();
167 const GrBackendFormat& format = te->view().proxy()->backendFormat();
168 GrSwizzle swizzle = te->view().swizzle();
169 SamplerHandle handle = this->emitSampler(format, samplerState, swizzle, name.c_str());
170 static_cast<GrTextureEffect::Impl&>(subGLSLFP).setSamplerHandle(handle);
Brian Salomone782f842018-07-31 13:53:11 -0400171 }
bsalomonb58a2b42016-09-26 06:55:02 -0700172 }
Brian Osman9cf98dc2020-07-01 17:21:27 -0400173 const GrShaderVar* coordVars = fTransformedCoordVars.begin() + transformedCoordVarsIdx;
bsalomona624bf32016-09-20 09:12:47 -0700174 GrGLSLFragmentProcessor::TransformedCoordVars coords(&fp, coordVars);
egdanielfa896322016-01-13 12:19:30 -0800175 GrGLSLFragmentProcessor::EmitArgs args(&fFS,
176 this->uniformHandler(),
Brian Salomon94efbf52016-11-29 13:43:05 -0500177 this->shaderCaps(),
egdanielfa896322016-01-13 12:19:30 -0800178 fp,
John Stiles658ae8f2020-10-08 22:40:09 -0400179 "_input",
Michael Ludwige88320b2020-06-24 09:04:56 -0400180 "_coords",
John Stiles9e506ee2020-08-26 10:22:50 -0400181 coords,
182 /*forceInline=*/true);
John Stiles658ae8f2020-10-08 22:40:09 -0400183 auto name = fFS.writeProcessorFunction(&glslFP, args);
184 fFS.codeAppendf("%s = %s(%s);", output.c_str(), name.c_str(), input.c_str());
Brian Osmana8486d12020-08-04 10:18:44 -0400185
John Stiles658ae8f2020-10-08 22:40:09 -0400186 // We have to check that effects and the code they emit are consistent, ie if an effect asks
187 // for dst color, then the emit code needs to follow suit
cdalton87332102016-02-26 12:22:02 -0800188 SkDEBUGCODE(verify(fp);)
egdanielfa896322016-01-13 12:19:30 -0800189
Ethan Nicholas2983f402017-05-08 09:36:08 -0400190 return output;
egdanielfa896322016-01-13 12:19:30 -0800191}
192
Ethan Nicholas2983f402017-05-08 09:36:08 -0400193void GrGLSLProgramBuilder::emitAndInstallXferProc(const SkString& colorIn,
194 const SkString& coverageIn) {
egdanielfa896322016-01-13 12:19:30 -0800195 // Program builders have a bit of state we need to clear with each effect
196 AutoStageAdvance adv(this);
197
198 SkASSERT(!fXferProcessor);
Robert Phillips901aff02019-10-08 12:32:56 -0400199 const GrXferProcessor& xp = this->pipeline().getXferProcessor();
Robert Phillips369e8b72017-08-01 16:13:04 -0400200 fXferProcessor.reset(xp.createGLSLInstance());
egdanielfa896322016-01-13 12:19:30 -0800201
202 // Enable dual source secondary output if we have one
203 if (xp.hasSecondaryOutput()) {
204 fFS.enableSecondaryOutput();
205 }
206
Brian Salomon94efbf52016-11-29 13:43:05 -0500207 if (this->shaderCaps()->mustDeclareFragmentShaderOutput()) {
egdanielfa896322016-01-13 12:19:30 -0800208 fFS.enableCustomOutput();
209 }
210
211 SkString openBrace;
212 openBrace.printf("{ // Xfer Processor: %s\n", xp.name());
213 fFS.codeAppend(openBrace.c_str());
214
Brian Salomon18dfa982017-04-03 16:57:43 -0400215 SamplerHandle dstTextureSamplerHandle;
216 GrSurfaceOrigin dstTextureOrigin = kTopLeft_GrSurfaceOrigin;
Robert Phillipsbb581ce2017-05-29 15:05:15 -0400217
Greg Daniel524e28b2019-11-01 11:48:53 -0400218 const GrSurfaceProxyView& dstView = this->pipeline().dstProxyView();
Greg Daniel37fd6582020-09-14 12:36:09 -0400219 if (this->pipeline().usesDstTexture()) {
220 GrTextureProxy* dstTextureProxy = dstView.asTextureProxy();
221 SkASSERT(dstTextureProxy);
Greg Daniel524e28b2019-11-01 11:48:53 -0400222 const GrSwizzle& swizzle = dstView.swizzle();
Chris Dalton1b1b0d52020-03-03 12:00:59 -0700223 dstTextureSamplerHandle = this->emitSampler(dstTextureProxy->backendFormat(),
224 GrSamplerState(), swizzle, "DstTextureSampler");
Greg Daniel524e28b2019-11-01 11:48:53 -0400225 dstTextureOrigin = dstView.origin();
Robert Phillips66ae3042019-10-10 12:57:58 -0400226 SkASSERT(dstTextureProxy->textureType() != GrTextureType::kExternal);
Greg Daniel37fd6582020-09-14 12:36:09 -0400227 } else if (this->pipeline().usesInputAttachment()) {
228 const GrSwizzle& swizzle = dstView.swizzle();
229 dstTextureSamplerHandle = this->emitInputSampler(swizzle, "DstTextureInput");
Brian Salomon18dfa982017-04-03 16:57:43 -0400230 }
egdanielfa896322016-01-13 12:19:30 -0800231
Brian Salomonf19f9ca2019-09-18 15:54:26 -0400232 SkString finalInColor = colorIn.size() ? colorIn : SkString("float4(1)");
Brian Osman65cdd612019-03-14 17:01:57 -0400233
egdanielfa896322016-01-13 12:19:30 -0800234 GrGLSLXferProcessor::EmitArgs args(&fFS,
235 this->uniformHandler(),
Brian Salomon94efbf52016-11-29 13:43:05 -0500236 this->shaderCaps(),
Brian Salomon18dfa982017-04-03 16:57:43 -0400237 xp,
Brian Osman65cdd612019-03-14 17:01:57 -0400238 finalInColor.c_str(),
Ethan Nicholas5af9ea32017-07-28 15:19:46 -0400239 coverageIn.size() ? coverageIn.c_str() : "float4(1)",
egdanielfa896322016-01-13 12:19:30 -0800240 fFS.getPrimaryColorOutputName(),
241 fFS.getSecondaryColorOutputName(),
Greg Daniel37fd6582020-09-14 12:36:09 -0400242 this->pipeline().dstSampleType(),
Brian Salomon18dfa982017-04-03 16:57:43 -0400243 dstTextureSamplerHandle,
Chris Dalton6b76df02019-04-08 11:01:34 -0600244 dstTextureOrigin,
Brian Salomon982f5462020-03-30 12:52:33 -0400245 this->pipeline().writeSwizzle());
egdanielfa896322016-01-13 12:19:30 -0800246 fXferProcessor->emitCode(args);
247
248 // We have to check that effects and the code they emit are consistent, ie if an effect
249 // asks for dst color, then the emit code needs to follow suit
cdalton87332102016-02-26 12:22:02 -0800250 SkDEBUGCODE(verify(xp);)
egdanielfa896322016-01-13 12:19:30 -0800251 fFS.codeAppend("}");
252}
253
Chris Dalton1b1b0d52020-03-03 12:00:59 -0700254GrGLSLProgramBuilder::SamplerHandle GrGLSLProgramBuilder::emitSampler(
255 const GrBackendFormat& backendFormat, GrSamplerState state, const GrSwizzle& swizzle,
256 const char* name) {
Greg Daniel0f70be82018-10-08 17:35:08 +0000257 ++fNumFragmentSamplers;
Chris Dalton1b1b0d52020-03-03 12:00:59 -0700258 return this->uniformHandler()->addSampler(backendFormat, state, swizzle, name,
259 this->shaderCaps());
Brian Salomonf9f45122016-11-29 11:59:17 -0500260}
261
Greg Daniel37fd6582020-09-14 12:36:09 -0400262GrGLSLProgramBuilder::SamplerHandle GrGLSLProgramBuilder::emitInputSampler(const GrSwizzle& swizzle,
263 const char* name) {
264 return this->uniformHandler()->addInputSampler(swizzle, name);
265}
266
cdalton9c3f1432016-03-11 10:07:37 -0800267bool GrGLSLProgramBuilder::checkSamplerCounts() {
Brian Salomon1edc5b92016-11-29 13:43:46 -0500268 const GrShaderCaps& shaderCaps = *this->shaderCaps();
Brian Salomon1edc5b92016-11-29 13:43:46 -0500269 if (fNumFragmentSamplers > shaderCaps.maxFragmentSamplers()) {
cdalton9c3f1432016-03-11 10:07:37 -0800270 GrCapsDebugf(this->caps(), "Program would use too many fragment samplers\n");
271 return false;
272 }
cdalton9c3f1432016-03-11 10:07:37 -0800273 return true;
274}
275
cdalton87332102016-02-26 12:22:02 -0800276#ifdef SK_DEBUG
egdanielfa896322016-01-13 12:19:30 -0800277void GrGLSLProgramBuilder::verify(const GrPrimitiveProcessor& gp) {
Chris Daltond7291ba2019-03-07 14:17:03 -0700278 SkASSERT(!fFS.fHasReadDstColorThisStage_DebugOnly);
279 SkASSERT(fFS.fUsedProcessorFeaturesThisStage_DebugOnly == gp.requestedFeatures());
egdanielfa896322016-01-13 12:19:30 -0800280}
281
282void GrGLSLProgramBuilder::verify(const GrFragmentProcessor& fp) {
Chris Daltond7291ba2019-03-07 14:17:03 -0700283 SkASSERT(!fFS.fHasReadDstColorThisStage_DebugOnly);
284 SkASSERT(fFS.fUsedProcessorFeaturesThisStage_DebugOnly == fp.requestedFeatures());
285}
286
287void GrGLSLProgramBuilder::verify(const GrXferProcessor& xp) {
288 SkASSERT(xp.willReadDstColor() == fFS.fHasReadDstColorThisStage_DebugOnly);
289 SkASSERT(fFS.fUsedProcessorFeaturesThisStage_DebugOnly == xp.requestedFeatures());
egdaniel8dcdedc2015-11-11 06:27:20 -0800290}
cdalton87332102016-02-26 12:22:02 -0800291#endif
egdaniel8dcdedc2015-11-11 06:27:20 -0800292
John Stilesaaea0d92020-10-26 13:09:37 -0400293SkString GrGLSLProgramBuilder::nameVariable(char prefix, const char* name, bool mangle) {
294 SkString out;
egdaniel8dcdedc2015-11-11 06:27:20 -0800295 if ('\0' == prefix) {
John Stilesaaea0d92020-10-26 13:09:37 -0400296 out = name;
egdaniel8dcdedc2015-11-11 06:27:20 -0800297 } else {
John Stilesaaea0d92020-10-26 13:09:37 -0400298 out.printf("%c%s", prefix, name);
egdaniel8dcdedc2015-11-11 06:27:20 -0800299 }
300 if (mangle) {
John Stilesaaea0d92020-10-26 13:09:37 -0400301 // Names containing "__" are reserved; add "x" if needed to avoid consecutive underscores.
302 const char *underscoreSplitter = out.endsWith('_') ? "x" : "";
303
304 out.appendf("%s_Stage%d%s", underscoreSplitter, fStageIndex, fFS.getMangleString().c_str());
egdaniel8dcdedc2015-11-11 06:27:20 -0800305 }
John Stilesaaea0d92020-10-26 13:09:37 -0400306 return out;
egdaniel8dcdedc2015-11-11 06:27:20 -0800307}
308
Ethan Nicholas2983f402017-05-08 09:36:08 -0400309void GrGLSLProgramBuilder::nameExpression(SkString* output, const char* baseName) {
egdanielfa896322016-01-13 12:19:30 -0800310 // create var to hold stage result. If we already have a valid output name, just use that
311 // otherwise create a new mangled one. This name is only valid if we are reordering stages
312 // and have to tell stage exactly where to put its output.
313 SkString outName;
Ethan Nicholas2983f402017-05-08 09:36:08 -0400314 if (output->size()) {
egdanielfa896322016-01-13 12:19:30 -0800315 outName = output->c_str();
316 } else {
John Stilesaaea0d92020-10-26 13:09:37 -0400317 outName = this->nameVariable(/*prefix=*/'\0', baseName);
egdanielfa896322016-01-13 12:19:30 -0800318 }
Ethan Nicholasf7b88202017-09-18 14:10:39 -0400319 fFS.codeAppendf("half4 %s;", outName.c_str());
egdanielfa896322016-01-13 12:19:30 -0800320 *output = outName;
321}
322
cdalton5e58cee2016-02-11 12:49:47 -0800323void GrGLSLProgramBuilder::appendUniformDecls(GrShaderFlags visibility, SkString* out) const {
egdaniel7ea439b2015-12-03 09:20:44 -0800324 this->uniformHandler()->appendUniformDecls(visibility, out);
325}
326
Ethan Nicholascd700e92018-08-24 16:43:57 -0400327void GrGLSLProgramBuilder::addRTWidthUniform(const char* name) {
Brian Osmanf7924452019-09-24 10:44:37 -0400328 SkASSERT(!fUniformHandles.fRTWidthUni.isValid());
329 GrGLSLUniformHandler* uniformHandler = this->uniformHandler();
330 fUniformHandles.fRTWidthUni =
Ethan Nicholas16464c32020-04-06 13:53:05 -0400331 uniformHandler->internalAddUniformArray(nullptr, kFragment_GrShaderFlag, kHalf_GrSLType,
332 name, false, 0, nullptr);
Ethan Nicholascd700e92018-08-24 16:43:57 -0400333}
334
Greg Daniele6ab9982018-08-22 13:56:32 +0000335void GrGLSLProgramBuilder::addRTHeightUniform(const char* name) {
Brian Osmanf7924452019-09-24 10:44:37 -0400336 SkASSERT(!fUniformHandles.fRTHeightUni.isValid());
337 GrGLSLUniformHandler* uniformHandler = this->uniformHandler();
338 fUniformHandles.fRTHeightUni =
Ethan Nicholas16464c32020-04-06 13:53:05 -0400339 uniformHandler->internalAddUniformArray(nullptr, kFragment_GrShaderFlag, kHalf_GrSLType,
340 name, false, 0, nullptr);
egdaniel8dcdedc2015-11-11 06:27:20 -0800341}
342
egdaniel9f1d4152016-02-10 09:50:38 -0800343void GrGLSLProgramBuilder::finalizeShaders() {
344 this->varyingHandler()->finalize();
cdalton5e58cee2016-02-11 12:49:47 -0800345 fVS.finalize(kVertex_GrShaderFlag);
csmartdalton276cc412016-11-21 11:55:00 -0700346 if (this->primitiveProcessor().willUseGeoShader()) {
Brian Salomon94efbf52016-11-29 13:43:05 -0500347 SkASSERT(this->shaderCaps()->geometryShaderSupport());
csmartdalton276cc412016-11-21 11:55:00 -0700348 fGS.finalize(kGeometry_GrShaderFlag);
349 }
cdalton5e58cee2016-02-11 12:49:47 -0800350 fFS.finalize(kFragment_GrShaderFlag);
egdaniel9f1d4152016-02-10 09:50:38 -0800351}