blob: 7cb29fcc446900b72717eba1bf2f796e719b65df [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
Brian Salomon17f95b12021-02-23 09:35:08 -050040GrGLSLProgramBuilder::~GrGLSLProgramBuilder() = default;
41
cdalton9c3f1432016-03-11 10:07:37 -080042void 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 Nicholas570506d2021-02-11 13:19:38 -050060 SkSL::dsl::Start(this->shaderCompiler());
Ethan Nicholas2983f402017-05-08 09:36:08 -040061 SkString inputColor;
62 SkString inputCoverage;
Greg Daniel9a51a862018-11-30 10:18:14 -050063 this->emitAndInstallPrimProc(&inputColor, &inputCoverage);
Ethan Nicholas2983f402017-05-08 09:36:08 -040064 this->emitAndInstallFragProcs(&inputColor, &inputCoverage);
65 this->emitAndInstallXferProc(inputColor, inputCoverage);
Ethan Nicholas58430122020-04-14 09:54:02 -040066 fGeometryProcessor->emitTransformCode(&fVS, this->uniformHandler());
Ethan Nicholas570506d2021-02-11 13:19:38 -050067 SkSL::dsl::End();
cdalton9c3f1432016-03-11 10:07:37 -080068
Brian Salomon559f5562017-11-15 14:28:33 -050069 return this->checkSamplerCounts();
egdanielfa896322016-01-13 12:19:30 -080070}
71
Chris Dalton1b1b0d52020-03-03 12:00:59 -070072void GrGLSLProgramBuilder::emitAndInstallPrimProc(SkString* outputColor, SkString* outputCoverage) {
Greg Daniel9a51a862018-11-30 10:18:14 -050073 const GrPrimitiveProcessor& proc = this->primitiveProcessor();
Robert Phillips66ae3042019-10-10 12:57:58 -040074
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;
Chris Dalton5a2f9622019-12-27 14:56:38 -070084 } else if (proc.willUseTessellationShaders()) {
85 rtAdjustVisibility = kTessEvaluation_GrShaderFlag;
Robert Phillipsfe8da172018-01-24 14:52:02 +000086 } else {
87 rtAdjustVisibility = kVertex_GrShaderFlag;
csmartdalton936f81b2017-02-13 15:45:35 -070088 }
Robert Phillipsfe8da172018-01-24 14:52:02 +000089 fUniformHandles.fRTAdjustmentUni = this->uniformHandler()->addUniform(
Ethan Nicholas16464c32020-04-06 13:53:05 -040090 nullptr, rtAdjustVisibility, kFloat4_GrSLType, SkSL::Compiler::RTADJUST_NAME);
csmartdalton936f81b2017-02-13 15:45:35 -070091
John Stiles38847cd2021-03-09 20:16:11 -050092 fFS.codeAppendf("// Stage %d, %s\n", fStageIndex, proc.name());
egdanielfa896322016-01-13 12:19:30 -080093 fVS.codeAppendf("// Primitive Processor %s\n", proc.name());
94
95 SkASSERT(!fGeometryProcessor);
Robert Phillips369e8b72017-08-01 16:13:04 -040096 fGeometryProcessor.reset(proc.createGLSLInstance(*this->shaderCaps()));
egdanielfa896322016-01-13 12:19:30 -080097
Brian Salomone782f842018-07-31 13:53:11 -040098 SkAutoSTMalloc<4, SamplerHandle> texSamplers(proc.numTextureSamplers());
99 for (int i = 0; i < proc.numTextureSamplers(); ++i) {
100 SkString name;
101 name.printf("TextureSampler_%d", i);
102 const auto& sampler = proc.textureSampler(i);
Chris Dalton1b1b0d52020-03-03 12:00:59 -0700103 texSamplers[i] = this->emitSampler(proc.textureSampler(i).backendFormat(),
Greg Daniel9a51a862018-11-30 10:18:14 -0500104 sampler.samplerState(),
Greg Daniel2c3398d2019-06-19 11:58:01 -0400105 sampler.swizzle(),
Greg Daniel9a51a862018-11-30 10:18:14 -0500106 name.c_str());
Brian Salomone782f842018-07-31 13:53:11 -0400107 }
egdanielfa896322016-01-13 12:19:30 -0800108
Robert Phillips901aff02019-10-08 12:32:56 -0400109 GrGLSLPrimitiveProcessor::FPCoordTransformHandler transformHandler(this->pipeline(),
bsalomona624bf32016-09-20 09:12:47 -0700110 &fTransformedCoordVars);
egdanielfa896322016-01-13 12:19:30 -0800111 GrGLSLGeometryProcessor::EmitArgs args(&fVS,
csmartdalton276cc412016-11-21 11:55:00 -0700112 proc.willUseGeoShader() ? &fGS : nullptr,
egdanielfa896322016-01-13 12:19:30 -0800113 &fFS,
114 this->varyingHandler(),
115 this->uniformHandler(),
Brian Salomon94efbf52016-11-29 13:43:05 -0500116 this->shaderCaps(),
egdanielfa896322016-01-13 12:19:30 -0800117 proc,
118 outputColor->c_str(),
119 outputCoverage->c_str(),
Brian Salomone782f842018-07-31 13:53:11 -0400120 texSamplers.get(),
bsalomona624bf32016-09-20 09:12:47 -0700121 &transformHandler);
egdanielfa896322016-01-13 12:19:30 -0800122 fGeometryProcessor->emitCode(args);
123
124 // We have to check that effects and the code they emit are consistent, ie if an effect
125 // asks for dst color, then the emit code needs to follow suit
cdalton87332102016-02-26 12:22:02 -0800126 SkDEBUGCODE(verify(proc);)
egdanielfa896322016-01-13 12:19:30 -0800127}
128
Ethan Nicholas2983f402017-05-08 09:36:08 -0400129void GrGLSLProgramBuilder::emitAndInstallFragProcs(SkString* color, SkString* coverage) {
bsalomona624bf32016-09-20 09:12:47 -0700130 int transformedCoordVarsIdx = 0;
Brian Salomond90b3d32020-07-09 12:04:31 -0400131 int fpCount = this->pipeline().numFragmentProcessors();
Brian Salomon17f95b12021-02-23 09:35:08 -0500132 SkASSERT(fFPImpls.empty());
133 fFPImpls.reserve(fpCount);
Brian Salomond90b3d32020-07-09 12:04:31 -0400134 for (int i = 0; i < fpCount; ++i) {
John Stilesd3feb6f2020-07-23 18:18:12 -0400135 SkString* inOut = this->pipeline().isColorFragmentProcessor(i) ? color : coverage;
Ethan Nicholas2983f402017-05-08 09:36:08 -0400136 SkString output;
egdanielfa896322016-01-13 12:19:30 -0800137 const GrFragmentProcessor& fp = this->pipeline().getFragmentProcessor(i);
Brian Salomon17f95b12021-02-23 09:35:08 -0500138 fFPImpls.push_back(fp.makeProgramImpl());
139 output = this->emitFragProc(fp,
140 *fFPImpls.back(),
141 transformedCoordVarsIdx,
142 *inOut,
Brian Salomond90b3d32020-07-09 12:04:31 -0400143 output);
Brian Salomon87f4d292020-07-09 12:48:38 -0400144 for (const auto& subFP : GrFragmentProcessor::FPRange(fp)) {
Brian Osman9cf98dc2020-07-01 17:21:27 -0400145 transformedCoordVarsIdx += subFP.numVaryingCoordsUsed();
bsalomona624bf32016-09-20 09:12:47 -0700146 }
John Stilesd3feb6f2020-07-23 18:18:12 -0400147 *inOut = std::move(output);
egdanielfa896322016-01-13 12:19:30 -0800148 }
149}
150
Brian Salomond90b3d32020-07-09 12:04:31 -0400151SkString GrGLSLProgramBuilder::emitFragProc(const GrFragmentProcessor& fp,
152 GrGLSLFragmentProcessor& glslFP,
153 int transformedCoordVarsIdx,
154 const SkString& input,
155 SkString output) {
Ethan Nicholas2983f402017-05-08 09:36:08 -0400156 SkASSERT(input.size());
egdanielfa896322016-01-13 12:19:30 -0800157 // Program builders have a bit of state we need to clear with each effect
158 AutoStageAdvance adv(this);
Ethan Nicholas2983f402017-05-08 09:36:08 -0400159 this->nameExpression(&output, "output");
John Stiles4d7ac492021-03-09 20:16:43 -0500160 fFS.codeAppendf("half4 %s;", output.c_str());
egdanielfa896322016-01-13 12:19:30 -0800161
Brian Salomone782f842018-07-31 13:53:11 -0400162 int samplerIdx = 0;
Brian Salomond90b3d32020-07-09 12:04:31 -0400163 for (auto [subFP, subGLSLFP] : GrGLSLFragmentProcessor::ParallelRange(fp, glslFP)) {
164 if (auto* te = subFP.asTextureEffect()) {
Brian Salomone782f842018-07-31 13:53:11 -0400165 SkString name;
166 name.printf("TextureSampler_%d", samplerIdx++);
Brian Salomond90b3d32020-07-09 12:04:31 -0400167
168 GrSamplerState samplerState = te->samplerState();
169 const GrBackendFormat& format = te->view().proxy()->backendFormat();
170 GrSwizzle swizzle = te->view().swizzle();
171 SamplerHandle handle = this->emitSampler(format, samplerState, swizzle, name.c_str());
172 static_cast<GrTextureEffect::Impl&>(subGLSLFP).setSamplerHandle(handle);
Brian Salomone782f842018-07-31 13:53:11 -0400173 }
bsalomonb58a2b42016-09-26 06:55:02 -0700174 }
Brian Osman9cf98dc2020-07-01 17:21:27 -0400175 const GrShaderVar* coordVars = fTransformedCoordVars.begin() + transformedCoordVarsIdx;
bsalomona624bf32016-09-20 09:12:47 -0700176 GrGLSLFragmentProcessor::TransformedCoordVars coords(&fp, coordVars);
egdanielfa896322016-01-13 12:19:30 -0800177 GrGLSLFragmentProcessor::EmitArgs args(&fFS,
178 this->uniformHandler(),
Brian Salomon94efbf52016-11-29 13:43:05 -0500179 this->shaderCaps(),
egdanielfa896322016-01-13 12:19:30 -0800180 fp,
John Stiles658ae8f2020-10-08 22:40:09 -0400181 "_input",
Michael Ludwige88320b2020-06-24 09:04:56 -0400182 "_coords",
John Stiles9e506ee2020-08-26 10:22:50 -0400183 coords,
184 /*forceInline=*/true);
John Stiles658ae8f2020-10-08 22:40:09 -0400185 auto name = fFS.writeProcessorFunction(&glslFP, args);
186 fFS.codeAppendf("%s = %s(%s);", output.c_str(), name.c_str(), input.c_str());
Brian Osmana8486d12020-08-04 10:18:44 -0400187
John Stiles658ae8f2020-10-08 22:40:09 -0400188 // We have to check that effects and the code they emit are consistent, ie if an effect asks
189 // for dst color, then the emit code needs to follow suit
cdalton87332102016-02-26 12:22:02 -0800190 SkDEBUGCODE(verify(fp);)
egdanielfa896322016-01-13 12:19:30 -0800191
Ethan Nicholas2983f402017-05-08 09:36:08 -0400192 return output;
egdanielfa896322016-01-13 12:19:30 -0800193}
194
Ethan Nicholas2983f402017-05-08 09:36:08 -0400195void GrGLSLProgramBuilder::emitAndInstallXferProc(const SkString& colorIn,
196 const SkString& coverageIn) {
egdanielfa896322016-01-13 12:19:30 -0800197 // Program builders have a bit of state we need to clear with each effect
198 AutoStageAdvance adv(this);
199
200 SkASSERT(!fXferProcessor);
Robert Phillips901aff02019-10-08 12:32:56 -0400201 const GrXferProcessor& xp = this->pipeline().getXferProcessor();
Robert Phillips369e8b72017-08-01 16:13:04 -0400202 fXferProcessor.reset(xp.createGLSLInstance());
egdanielfa896322016-01-13 12:19:30 -0800203
204 // Enable dual source secondary output if we have one
205 if (xp.hasSecondaryOutput()) {
206 fFS.enableSecondaryOutput();
207 }
208
Brian Salomon94efbf52016-11-29 13:43:05 -0500209 if (this->shaderCaps()->mustDeclareFragmentShaderOutput()) {
egdanielfa896322016-01-13 12:19:30 -0800210 fFS.enableCustomOutput();
211 }
212
213 SkString openBrace;
214 openBrace.printf("{ // Xfer Processor: %s\n", xp.name());
215 fFS.codeAppend(openBrace.c_str());
216
Brian Salomon18dfa982017-04-03 16:57:43 -0400217 SamplerHandle dstTextureSamplerHandle;
218 GrSurfaceOrigin dstTextureOrigin = kTopLeft_GrSurfaceOrigin;
Robert Phillipsbb581ce2017-05-29 15:05:15 -0400219
Greg Daniel524e28b2019-11-01 11:48:53 -0400220 const GrSurfaceProxyView& dstView = this->pipeline().dstProxyView();
Greg Daniel37fd6582020-09-14 12:36:09 -0400221 if (this->pipeline().usesDstTexture()) {
222 GrTextureProxy* dstTextureProxy = dstView.asTextureProxy();
223 SkASSERT(dstTextureProxy);
Greg Daniel524e28b2019-11-01 11:48:53 -0400224 const GrSwizzle& swizzle = dstView.swizzle();
Chris Dalton1b1b0d52020-03-03 12:00:59 -0700225 dstTextureSamplerHandle = this->emitSampler(dstTextureProxy->backendFormat(),
226 GrSamplerState(), swizzle, "DstTextureSampler");
Greg Daniel524e28b2019-11-01 11:48:53 -0400227 dstTextureOrigin = dstView.origin();
Robert Phillips66ae3042019-10-10 12:57:58 -0400228 SkASSERT(dstTextureProxy->textureType() != GrTextureType::kExternal);
Greg Daniel37fd6582020-09-14 12:36:09 -0400229 } else if (this->pipeline().usesInputAttachment()) {
230 const GrSwizzle& swizzle = dstView.swizzle();
231 dstTextureSamplerHandle = this->emitInputSampler(swizzle, "DstTextureInput");
Brian Salomon18dfa982017-04-03 16:57:43 -0400232 }
egdanielfa896322016-01-13 12:19:30 -0800233
Brian Salomonf19f9ca2019-09-18 15:54:26 -0400234 SkString finalInColor = colorIn.size() ? colorIn : SkString("float4(1)");
Brian Osman65cdd612019-03-14 17:01:57 -0400235
egdanielfa896322016-01-13 12:19:30 -0800236 GrGLSLXferProcessor::EmitArgs args(&fFS,
237 this->uniformHandler(),
Brian Salomon94efbf52016-11-29 13:43:05 -0500238 this->shaderCaps(),
Brian Salomon18dfa982017-04-03 16:57:43 -0400239 xp,
Brian Osman65cdd612019-03-14 17:01:57 -0400240 finalInColor.c_str(),
Ethan Nicholas5af9ea32017-07-28 15:19:46 -0400241 coverageIn.size() ? coverageIn.c_str() : "float4(1)",
egdanielfa896322016-01-13 12:19:30 -0800242 fFS.getPrimaryColorOutputName(),
243 fFS.getSecondaryColorOutputName(),
Greg Daniel37fd6582020-09-14 12:36:09 -0400244 this->pipeline().dstSampleType(),
Brian Salomon18dfa982017-04-03 16:57:43 -0400245 dstTextureSamplerHandle,
Chris Dalton6b76df02019-04-08 11:01:34 -0600246 dstTextureOrigin,
Brian Salomon982f5462020-03-30 12:52:33 -0400247 this->pipeline().writeSwizzle());
egdanielfa896322016-01-13 12:19:30 -0800248 fXferProcessor->emitCode(args);
249
250 // We have to check that effects and the code they emit are consistent, ie if an effect
251 // asks for dst color, then the emit code needs to follow suit
cdalton87332102016-02-26 12:22:02 -0800252 SkDEBUGCODE(verify(xp);)
egdanielfa896322016-01-13 12:19:30 -0800253 fFS.codeAppend("}");
254}
255
Chris Dalton1b1b0d52020-03-03 12:00:59 -0700256GrGLSLProgramBuilder::SamplerHandle GrGLSLProgramBuilder::emitSampler(
257 const GrBackendFormat& backendFormat, GrSamplerState state, const GrSwizzle& swizzle,
258 const char* name) {
Greg Daniel0f70be82018-10-08 17:35:08 +0000259 ++fNumFragmentSamplers;
Chris Dalton1b1b0d52020-03-03 12:00:59 -0700260 return this->uniformHandler()->addSampler(backendFormat, state, swizzle, name,
261 this->shaderCaps());
Brian Salomonf9f45122016-11-29 11:59:17 -0500262}
263
Greg Daniel37fd6582020-09-14 12:36:09 -0400264GrGLSLProgramBuilder::SamplerHandle GrGLSLProgramBuilder::emitInputSampler(const GrSwizzle& swizzle,
265 const char* name) {
266 return this->uniformHandler()->addInputSampler(swizzle, name);
267}
268
cdalton9c3f1432016-03-11 10:07:37 -0800269bool GrGLSLProgramBuilder::checkSamplerCounts() {
Brian Salomon1edc5b92016-11-29 13:43:46 -0500270 const GrShaderCaps& shaderCaps = *this->shaderCaps();
Brian Salomon1edc5b92016-11-29 13:43:46 -0500271 if (fNumFragmentSamplers > shaderCaps.maxFragmentSamplers()) {
cdalton9c3f1432016-03-11 10:07:37 -0800272 GrCapsDebugf(this->caps(), "Program would use too many fragment samplers\n");
273 return false;
274 }
cdalton9c3f1432016-03-11 10:07:37 -0800275 return true;
276}
277
cdalton87332102016-02-26 12:22:02 -0800278#ifdef SK_DEBUG
egdanielfa896322016-01-13 12:19:30 -0800279void GrGLSLProgramBuilder::verify(const GrPrimitiveProcessor& gp) {
Chris Daltond7291ba2019-03-07 14:17:03 -0700280 SkASSERT(!fFS.fHasReadDstColorThisStage_DebugOnly);
281 SkASSERT(fFS.fUsedProcessorFeaturesThisStage_DebugOnly == gp.requestedFeatures());
egdanielfa896322016-01-13 12:19:30 -0800282}
283
284void GrGLSLProgramBuilder::verify(const GrFragmentProcessor& fp) {
Chris Daltond7291ba2019-03-07 14:17:03 -0700285 SkASSERT(!fFS.fHasReadDstColorThisStage_DebugOnly);
286 SkASSERT(fFS.fUsedProcessorFeaturesThisStage_DebugOnly == fp.requestedFeatures());
287}
288
289void GrGLSLProgramBuilder::verify(const GrXferProcessor& xp) {
290 SkASSERT(xp.willReadDstColor() == fFS.fHasReadDstColorThisStage_DebugOnly);
291 SkASSERT(fFS.fUsedProcessorFeaturesThisStage_DebugOnly == xp.requestedFeatures());
egdaniel8dcdedc2015-11-11 06:27:20 -0800292}
cdalton87332102016-02-26 12:22:02 -0800293#endif
egdaniel8dcdedc2015-11-11 06:27:20 -0800294
John Stilesaaea0d92020-10-26 13:09:37 -0400295SkString GrGLSLProgramBuilder::nameVariable(char prefix, const char* name, bool mangle) {
296 SkString out;
egdaniel8dcdedc2015-11-11 06:27:20 -0800297 if ('\0' == prefix) {
John Stilesaaea0d92020-10-26 13:09:37 -0400298 out = name;
egdaniel8dcdedc2015-11-11 06:27:20 -0800299 } else {
John Stilesaaea0d92020-10-26 13:09:37 -0400300 out.printf("%c%s", prefix, name);
egdaniel8dcdedc2015-11-11 06:27:20 -0800301 }
302 if (mangle) {
John Stilesaaea0d92020-10-26 13:09:37 -0400303 // Names containing "__" are reserved; add "x" if needed to avoid consecutive underscores.
304 const char *underscoreSplitter = out.endsWith('_') ? "x" : "";
305
306 out.appendf("%s_Stage%d%s", underscoreSplitter, fStageIndex, fFS.getMangleString().c_str());
egdaniel8dcdedc2015-11-11 06:27:20 -0800307 }
John Stilesaaea0d92020-10-26 13:09:37 -0400308 return out;
egdaniel8dcdedc2015-11-11 06:27:20 -0800309}
310
Ethan Nicholas2983f402017-05-08 09:36:08 -0400311void GrGLSLProgramBuilder::nameExpression(SkString* output, const char* baseName) {
John Stilesf71a1362021-03-10 11:21:16 -0500312 // Name a variable to hold stage result. If we already have a valid output name, use that as-is;
313 // otherwise, create a new mangled one.
314 if (output->isEmpty()) {
315 *output = this->nameVariable(/*prefix=*/'\0', baseName);
egdanielfa896322016-01-13 12:19:30 -0800316 }
egdanielfa896322016-01-13 12:19:30 -0800317}
318
cdalton5e58cee2016-02-11 12:49:47 -0800319void GrGLSLProgramBuilder::appendUniformDecls(GrShaderFlags visibility, SkString* out) const {
egdaniel7ea439b2015-12-03 09:20:44 -0800320 this->uniformHandler()->appendUniformDecls(visibility, out);
321}
322
Ethan Nicholascd700e92018-08-24 16:43:57 -0400323void GrGLSLProgramBuilder::addRTWidthUniform(const char* name) {
Brian Osmanf7924452019-09-24 10:44:37 -0400324 SkASSERT(!fUniformHandles.fRTWidthUni.isValid());
325 GrGLSLUniformHandler* uniformHandler = this->uniformHandler();
326 fUniformHandles.fRTWidthUni =
Ethan Nicholas16464c32020-04-06 13:53:05 -0400327 uniformHandler->internalAddUniformArray(nullptr, kFragment_GrShaderFlag, kHalf_GrSLType,
328 name, false, 0, nullptr);
Ethan Nicholascd700e92018-08-24 16:43:57 -0400329}
330
Greg Daniele6ab9982018-08-22 13:56:32 +0000331void GrGLSLProgramBuilder::addRTHeightUniform(const char* name) {
Brian Osmanf7924452019-09-24 10:44:37 -0400332 SkASSERT(!fUniformHandles.fRTHeightUni.isValid());
333 GrGLSLUniformHandler* uniformHandler = this->uniformHandler();
334 fUniformHandles.fRTHeightUni =
Ethan Nicholas16464c32020-04-06 13:53:05 -0400335 uniformHandler->internalAddUniformArray(nullptr, kFragment_GrShaderFlag, kHalf_GrSLType,
336 name, false, 0, nullptr);
egdaniel8dcdedc2015-11-11 06:27:20 -0800337}
338
egdaniel9f1d4152016-02-10 09:50:38 -0800339void GrGLSLProgramBuilder::finalizeShaders() {
340 this->varyingHandler()->finalize();
cdalton5e58cee2016-02-11 12:49:47 -0800341 fVS.finalize(kVertex_GrShaderFlag);
csmartdalton276cc412016-11-21 11:55:00 -0700342 if (this->primitiveProcessor().willUseGeoShader()) {
Brian Salomon94efbf52016-11-29 13:43:05 -0500343 SkASSERT(this->shaderCaps()->geometryShaderSupport());
csmartdalton276cc412016-11-21 11:55:00 -0700344 fGS.finalize(kGeometry_GrShaderFlag);
345 }
cdalton5e58cee2016-02-11 12:49:47 -0800346 fFS.finalize(kFragment_GrShaderFlag);
egdaniel9f1d4152016-02-10 09:50:38 -0800347}