blob: 853028ef007ef56bcf116b785123deb2dd27ff2c [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"
egdanielfa896322016-01-13 12:19:30 -080022
egdaniel8dcdedc2015-11-11 06:27:20 -080023const int GrGLSLProgramBuilder::kVarsPerBlock = 8;
24
Robert Phillips65a77752019-10-02 15:22:24 -040025GrGLSLProgramBuilder::GrGLSLProgramBuilder(GrRenderTarget* renderTarget,
Stephen Whiteb1857852020-02-07 15:33:23 +000026 const GrProgramDesc& desc,
27 const GrProgramInfo& programInfo)
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)
Austin Eng77fdf662020-02-07 01:26:16 +000033 , fDesc(desc)
Stephen Whiteb1857852020-02-07 15:33:23 +000034 , fProgramInfo(programInfo)
Brian Salomonff168d92018-06-23 15:17:27 -040035 , fGeometryProcessor(nullptr)
36 , fXferProcessor(nullptr)
Brian Salomonff168d92018-06-23 15:17:27 -040037 , fNumFragmentSamplers(0) {}
cdalton9c3f1432016-03-11 10:07:37 -080038
39void GrGLSLProgramBuilder::addFeature(GrShaderFlags shaders,
40 uint32_t featureBit,
41 const char* extensionName) {
42 if (shaders & kVertex_GrShaderFlag) {
43 fVS.addFeature(featureBit, extensionName);
44 }
45 if (shaders & kGeometry_GrShaderFlag) {
csmartdalton276cc412016-11-21 11:55:00 -070046 SkASSERT(this->primitiveProcessor().willUseGeoShader());
cdalton9c3f1432016-03-11 10:07:37 -080047 fGS.addFeature(featureBit, extensionName);
48 }
49 if (shaders & kFragment_GrShaderFlag) {
50 fFS.addFeature(featureBit, extensionName);
51 }
egdanielfa896322016-01-13 12:19:30 -080052}
53
Ethan Nicholas2983f402017-05-08 09:36:08 -040054bool GrGLSLProgramBuilder::emitAndInstallProcs() {
egdanielfa896322016-01-13 12:19:30 -080055 // First we loop over all of the installed processors and collect coord transforms. These will
56 // be sent to the GrGLSLPrimitiveProcessor in its emitCode function
Ethan Nicholas2983f402017-05-08 09:36:08 -040057 SkString inputColor;
58 SkString inputCoverage;
Greg Daniel9a51a862018-11-30 10:18:14 -050059 this->emitAndInstallPrimProc(&inputColor, &inputCoverage);
Ethan Nicholas2983f402017-05-08 09:36:08 -040060 this->emitAndInstallFragProcs(&inputColor, &inputCoverage);
61 this->emitAndInstallXferProc(inputColor, inputCoverage);
Ethan Nicholas58430122020-04-14 09:54:02 -040062 fGeometryProcessor->emitTransformCode(&fVS, this->uniformHandler());
cdalton9c3f1432016-03-11 10:07:37 -080063
Brian Salomon559f5562017-11-15 14:28:33 -050064 return this->checkSamplerCounts();
egdanielfa896322016-01-13 12:19:30 -080065}
66
Chris Dalton1b1b0d52020-03-03 12:00:59 -070067void GrGLSLProgramBuilder::emitAndInstallPrimProc(SkString* outputColor, SkString* outputCoverage) {
Greg Daniel9a51a862018-11-30 10:18:14 -050068 const GrPrimitiveProcessor& proc = this->primitiveProcessor();
Robert Phillips66ae3042019-10-10 12:57:58 -040069
egdanielfa896322016-01-13 12:19:30 -080070 // Program builders have a bit of state we need to clear with each effect
71 AutoStageAdvance adv(this);
72 this->nameExpression(outputColor, "outputColor");
73 this->nameExpression(outputCoverage, "outputCoverage");
74
csmartdalton936f81b2017-02-13 15:45:35 -070075 SkASSERT(!fUniformHandles.fRTAdjustmentUni.isValid());
Robert Phillipsfe8da172018-01-24 14:52:02 +000076 GrShaderFlags rtAdjustVisibility;
csmartdalton936f81b2017-02-13 15:45:35 -070077 if (proc.willUseGeoShader()) {
Robert Phillipsfe8da172018-01-24 14:52:02 +000078 rtAdjustVisibility = kGeometry_GrShaderFlag;
Chris Dalton5a2f9622019-12-27 14:56:38 -070079 } else if (proc.willUseTessellationShaders()) {
80 rtAdjustVisibility = kTessEvaluation_GrShaderFlag;
Robert Phillipsfe8da172018-01-24 14:52:02 +000081 } else {
82 rtAdjustVisibility = kVertex_GrShaderFlag;
csmartdalton936f81b2017-02-13 15:45:35 -070083 }
Robert Phillipsfe8da172018-01-24 14:52:02 +000084 fUniformHandles.fRTAdjustmentUni = this->uniformHandler()->addUniform(
Ethan Nicholas16464c32020-04-06 13:53:05 -040085 nullptr, rtAdjustVisibility, kFloat4_GrSLType, SkSL::Compiler::RTADJUST_NAME);
csmartdalton936f81b2017-02-13 15:45:35 -070086
egdanielfa896322016-01-13 12:19:30 -080087 // Enclose custom code in a block to avoid namespace conflicts
88 SkString openBrace;
89 openBrace.printf("{ // Stage %d, %s\n", fStageIndex, proc.name());
90 fFS.codeAppend(openBrace.c_str());
91 fVS.codeAppendf("// Primitive Processor %s\n", proc.name());
92
93 SkASSERT(!fGeometryProcessor);
Robert Phillips369e8b72017-08-01 16:13:04 -040094 fGeometryProcessor.reset(proc.createGLSLInstance(*this->shaderCaps()));
egdanielfa896322016-01-13 12:19:30 -080095
Brian Salomone782f842018-07-31 13:53:11 -040096 SkAutoSTMalloc<4, SamplerHandle> texSamplers(proc.numTextureSamplers());
97 for (int i = 0; i < proc.numTextureSamplers(); ++i) {
98 SkString name;
99 name.printf("TextureSampler_%d", i);
100 const auto& sampler = proc.textureSampler(i);
Chris Dalton1b1b0d52020-03-03 12:00:59 -0700101 texSamplers[i] = this->emitSampler(proc.textureSampler(i).backendFormat(),
Greg Daniel9a51a862018-11-30 10:18:14 -0500102 sampler.samplerState(),
Greg Daniel2c3398d2019-06-19 11:58:01 -0400103 sampler.swizzle(),
Greg Daniel9a51a862018-11-30 10:18:14 -0500104 name.c_str());
Brian Salomone782f842018-07-31 13:53:11 -0400105 }
egdanielfa896322016-01-13 12:19:30 -0800106
Robert Phillips901aff02019-10-08 12:32:56 -0400107 GrGLSLPrimitiveProcessor::FPCoordTransformHandler transformHandler(this->pipeline(),
bsalomona624bf32016-09-20 09:12:47 -0700108 &fTransformedCoordVars);
egdanielfa896322016-01-13 12:19:30 -0800109 GrGLSLGeometryProcessor::EmitArgs args(&fVS,
csmartdalton276cc412016-11-21 11:55:00 -0700110 proc.willUseGeoShader() ? &fGS : nullptr,
egdanielfa896322016-01-13 12:19:30 -0800111 &fFS,
112 this->varyingHandler(),
113 this->uniformHandler(),
Brian Salomon94efbf52016-11-29 13:43:05 -0500114 this->shaderCaps(),
egdanielfa896322016-01-13 12:19:30 -0800115 proc,
116 outputColor->c_str(),
117 outputCoverage->c_str(),
Brian Salomone782f842018-07-31 13:53:11 -0400118 texSamplers.get(),
bsalomona624bf32016-09-20 09:12:47 -0700119 &transformHandler);
egdanielfa896322016-01-13 12:19:30 -0800120 fGeometryProcessor->emitCode(args);
121
122 // We have to check that effects and the code they emit are consistent, ie if an effect
123 // asks for dst color, then the emit code needs to follow suit
cdalton87332102016-02-26 12:22:02 -0800124 SkDEBUGCODE(verify(proc);)
egdanielfa896322016-01-13 12:19:30 -0800125
126 fFS.codeAppend("}");
127}
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();
John Stilesfbd050b2020-08-03 13:21:46 -0400132 fFragmentProcessors = std::make_unique<std::unique_ptr<GrGLSLFragmentProcessor>[]>(fpCount);
Brian Salomond90b3d32020-07-09 12:04:31 -0400133 for (int i = 0; i < fpCount; ++i) {
John Stilesd3feb6f2020-07-23 18:18:12 -0400134 SkString* inOut = this->pipeline().isColorFragmentProcessor(i) ? color : coverage;
Ethan Nicholas2983f402017-05-08 09:36:08 -0400135 SkString output;
egdanielfa896322016-01-13 12:19:30 -0800136 const GrFragmentProcessor& fp = this->pipeline().getFragmentProcessor(i);
Brian Salomond90b3d32020-07-09 12:04:31 -0400137 fFragmentProcessors[i] = std::unique_ptr<GrGLSLFragmentProcessor>(fp.createGLSLInstance());
John Stilesd3feb6f2020-07-23 18:18:12 -0400138 output = this->emitFragProc(fp, *fFragmentProcessors[i], transformedCoordVarsIdx, *inOut,
Brian Salomond90b3d32020-07-09 12:04:31 -0400139 output);
Brian Salomon87f4d292020-07-09 12:48:38 -0400140 for (const auto& subFP : GrFragmentProcessor::FPRange(fp)) {
Brian Osman9cf98dc2020-07-01 17:21:27 -0400141 transformedCoordVarsIdx += subFP.numVaryingCoordsUsed();
bsalomona624bf32016-09-20 09:12:47 -0700142 }
John Stilesd3feb6f2020-07-23 18:18:12 -0400143 *inOut = std::move(output);
egdanielfa896322016-01-13 12:19:30 -0800144 }
145}
146
Brian Salomond90b3d32020-07-09 12:04:31 -0400147SkString GrGLSLProgramBuilder::emitFragProc(const GrFragmentProcessor& fp,
148 GrGLSLFragmentProcessor& glslFP,
149 int transformedCoordVarsIdx,
150 const SkString& input,
151 SkString output) {
Ethan Nicholas2983f402017-05-08 09:36:08 -0400152 SkASSERT(input.size());
egdanielfa896322016-01-13 12:19:30 -0800153 // Program builders have a bit of state we need to clear with each effect
154 AutoStageAdvance adv(this);
Ethan Nicholas2983f402017-05-08 09:36:08 -0400155 this->nameExpression(&output, "output");
egdanielfa896322016-01-13 12:19:30 -0800156
Brian Salomone782f842018-07-31 13:53:11 -0400157 int samplerIdx = 0;
Brian Salomond90b3d32020-07-09 12:04:31 -0400158 for (auto [subFP, subGLSLFP] : GrGLSLFragmentProcessor::ParallelRange(fp, glslFP)) {
159 if (auto* te = subFP.asTextureEffect()) {
Brian Salomone782f842018-07-31 13:53:11 -0400160 SkString name;
161 name.printf("TextureSampler_%d", samplerIdx++);
Brian Salomond90b3d32020-07-09 12:04:31 -0400162
163 GrSamplerState samplerState = te->samplerState();
164 const GrBackendFormat& format = te->view().proxy()->backendFormat();
165 GrSwizzle swizzle = te->view().swizzle();
166 SamplerHandle handle = this->emitSampler(format, samplerState, swizzle, name.c_str());
167 static_cast<GrTextureEffect::Impl&>(subGLSLFP).setSamplerHandle(handle);
Brian Salomone782f842018-07-31 13:53:11 -0400168 }
bsalomonb58a2b42016-09-26 06:55:02 -0700169 }
Brian Osman9cf98dc2020-07-01 17:21:27 -0400170 const GrShaderVar* coordVars = fTransformedCoordVars.begin() + transformedCoordVarsIdx;
bsalomona624bf32016-09-20 09:12:47 -0700171 GrGLSLFragmentProcessor::TransformedCoordVars coords(&fp, coordVars);
egdanielfa896322016-01-13 12:19:30 -0800172 GrGLSLFragmentProcessor::EmitArgs args(&fFS,
173 this->uniformHandler(),
Brian Salomon94efbf52016-11-29 13:43:05 -0500174 this->shaderCaps(),
egdanielfa896322016-01-13 12:19:30 -0800175 fp,
Brian Osmana8486d12020-08-04 10:18:44 -0400176 output.c_str(),
177 input.c_str(),
Michael Ludwige88320b2020-06-24 09:04:56 -0400178 "_coords",
John Stiles9e506ee2020-08-26 10:22:50 -0400179 coords,
180 /*forceInline=*/true);
Brian Osmana8486d12020-08-04 10:18:44 -0400181
Brian Osman767f4442020-08-13 16:59:48 -0400182 if (fp.usesExplicitReturn()) {
John Stiles9e506ee2020-08-26 10:22:50 -0400183 // FPs that explicitly return their output color must be in a helper function, but we inline
184 // it if at all possible.
Brian Osman767f4442020-08-13 16:59:48 -0400185 args.fInputColor = "_input";
186 args.fOutputColor = "_output";
187 auto name = fFS.writeProcessorFunction(&glslFP, args);
188 fFS.codeAppendf("%s = %s(%s);", output.c_str(), name.c_str(), input.c_str());
189 } else {
190 // Enclose custom code in a block to avoid namespace conflicts
191 fFS.codeAppendf("{ // Stage %d, %s\n", fStageIndex, fp.name());
Brian Osmana8486d12020-08-04 10:18:44 -0400192
Brian Osman767f4442020-08-13 16:59:48 -0400193 if (fp.referencesSampleCoords()) {
194 // The fp's generated code expects a _coords variable, but we're at the root so _coords
195 // is just the local coordinates produced by the primitive processor.
196 SkASSERT(fp.usesVaryingCoordsDirectly());
197
198 const GrShaderVar& varying = coordVars[0];
199 switch(varying.getType()) {
200 case kFloat2_GrSLType:
201 fFS.codeAppendf("float2 %s = %s.xy;\n",
202 args.fSampleCoord, varying.getName().c_str());
203 break;
204 case kFloat3_GrSLType:
205 fFS.codeAppendf("float2 %s = %s.xy / %s.z;\n",
206 args.fSampleCoord,
207 varying.getName().c_str(),
208 varying.getName().c_str());
209 break;
210 default:
211 SkDEBUGFAILF("Unexpected type for varying: %d named %s\n",
212 (int) varying.getType(), varying.getName().c_str());
213 break;
214 }
Brian Osmana8486d12020-08-04 10:18:44 -0400215 }
Brian Osmana8486d12020-08-04 10:18:44 -0400216
Brian Osman767f4442020-08-13 16:59:48 -0400217 glslFP.emitCode(args);
218
219 fFS.codeAppend("}");
220 }
egdanielfa896322016-01-13 12:19:30 -0800221
222 // We have to check that effects and the code they emit are consistent, ie if an effect
223 // asks for dst color, then the emit code needs to follow suit
cdalton87332102016-02-26 12:22:02 -0800224 SkDEBUGCODE(verify(fp);)
egdanielfa896322016-01-13 12:19:30 -0800225
Ethan Nicholas2983f402017-05-08 09:36:08 -0400226 return output;
egdanielfa896322016-01-13 12:19:30 -0800227}
228
Ethan Nicholas2983f402017-05-08 09:36:08 -0400229void GrGLSLProgramBuilder::emitAndInstallXferProc(const SkString& colorIn,
230 const SkString& coverageIn) {
egdanielfa896322016-01-13 12:19:30 -0800231 // Program builders have a bit of state we need to clear with each effect
232 AutoStageAdvance adv(this);
233
234 SkASSERT(!fXferProcessor);
Robert Phillips901aff02019-10-08 12:32:56 -0400235 const GrXferProcessor& xp = this->pipeline().getXferProcessor();
Robert Phillips369e8b72017-08-01 16:13:04 -0400236 fXferProcessor.reset(xp.createGLSLInstance());
egdanielfa896322016-01-13 12:19:30 -0800237
238 // Enable dual source secondary output if we have one
239 if (xp.hasSecondaryOutput()) {
240 fFS.enableSecondaryOutput();
241 }
242
Brian Salomon94efbf52016-11-29 13:43:05 -0500243 if (this->shaderCaps()->mustDeclareFragmentShaderOutput()) {
egdanielfa896322016-01-13 12:19:30 -0800244 fFS.enableCustomOutput();
245 }
246
247 SkString openBrace;
248 openBrace.printf("{ // Xfer Processor: %s\n", xp.name());
249 fFS.codeAppend(openBrace.c_str());
250
Brian Salomon18dfa982017-04-03 16:57:43 -0400251 SamplerHandle dstTextureSamplerHandle;
252 GrSurfaceOrigin dstTextureOrigin = kTopLeft_GrSurfaceOrigin;
Robert Phillipsbb581ce2017-05-29 15:05:15 -0400253
Greg Daniel524e28b2019-11-01 11:48:53 -0400254 const GrSurfaceProxyView& dstView = this->pipeline().dstProxyView();
Greg Daniel37fd6582020-09-14 12:36:09 -0400255 if (this->pipeline().usesDstTexture()) {
256 GrTextureProxy* dstTextureProxy = dstView.asTextureProxy();
257 SkASSERT(dstTextureProxy);
Greg Daniel524e28b2019-11-01 11:48:53 -0400258 const GrSwizzle& swizzle = dstView.swizzle();
Chris Dalton1b1b0d52020-03-03 12:00:59 -0700259 dstTextureSamplerHandle = this->emitSampler(dstTextureProxy->backendFormat(),
260 GrSamplerState(), swizzle, "DstTextureSampler");
Greg Daniel524e28b2019-11-01 11:48:53 -0400261 dstTextureOrigin = dstView.origin();
Robert Phillips66ae3042019-10-10 12:57:58 -0400262 SkASSERT(dstTextureProxy->textureType() != GrTextureType::kExternal);
Greg Daniel37fd6582020-09-14 12:36:09 -0400263 } else if (this->pipeline().usesInputAttachment()) {
264 const GrSwizzle& swizzle = dstView.swizzle();
265 dstTextureSamplerHandle = this->emitInputSampler(swizzle, "DstTextureInput");
Brian Salomon18dfa982017-04-03 16:57:43 -0400266 }
egdanielfa896322016-01-13 12:19:30 -0800267
Brian Salomonf19f9ca2019-09-18 15:54:26 -0400268 SkString finalInColor = colorIn.size() ? colorIn : SkString("float4(1)");
Brian Osman65cdd612019-03-14 17:01:57 -0400269
egdanielfa896322016-01-13 12:19:30 -0800270 GrGLSLXferProcessor::EmitArgs args(&fFS,
271 this->uniformHandler(),
Brian Salomon94efbf52016-11-29 13:43:05 -0500272 this->shaderCaps(),
Brian Salomon18dfa982017-04-03 16:57:43 -0400273 xp,
Brian Osman65cdd612019-03-14 17:01:57 -0400274 finalInColor.c_str(),
Ethan Nicholas5af9ea32017-07-28 15:19:46 -0400275 coverageIn.size() ? coverageIn.c_str() : "float4(1)",
egdanielfa896322016-01-13 12:19:30 -0800276 fFS.getPrimaryColorOutputName(),
277 fFS.getSecondaryColorOutputName(),
Greg Daniel37fd6582020-09-14 12:36:09 -0400278 this->pipeline().dstSampleType(),
Brian Salomon18dfa982017-04-03 16:57:43 -0400279 dstTextureSamplerHandle,
Chris Dalton6b76df02019-04-08 11:01:34 -0600280 dstTextureOrigin,
Brian Salomon982f5462020-03-30 12:52:33 -0400281 this->pipeline().writeSwizzle());
egdanielfa896322016-01-13 12:19:30 -0800282 fXferProcessor->emitCode(args);
283
284 // We have to check that effects and the code they emit are consistent, ie if an effect
285 // asks for dst color, then the emit code needs to follow suit
cdalton87332102016-02-26 12:22:02 -0800286 SkDEBUGCODE(verify(xp);)
egdanielfa896322016-01-13 12:19:30 -0800287 fFS.codeAppend("}");
288}
289
Chris Dalton1b1b0d52020-03-03 12:00:59 -0700290GrGLSLProgramBuilder::SamplerHandle GrGLSLProgramBuilder::emitSampler(
291 const GrBackendFormat& backendFormat, GrSamplerState state, const GrSwizzle& swizzle,
292 const char* name) {
Greg Daniel0f70be82018-10-08 17:35:08 +0000293 ++fNumFragmentSamplers;
Chris Dalton1b1b0d52020-03-03 12:00:59 -0700294 return this->uniformHandler()->addSampler(backendFormat, state, swizzle, name,
295 this->shaderCaps());
Brian Salomonf9f45122016-11-29 11:59:17 -0500296}
297
Greg Daniel37fd6582020-09-14 12:36:09 -0400298GrGLSLProgramBuilder::SamplerHandle GrGLSLProgramBuilder::emitInputSampler(const GrSwizzle& swizzle,
299 const char* name) {
300 return this->uniformHandler()->addInputSampler(swizzle, name);
301}
302
cdalton9c3f1432016-03-11 10:07:37 -0800303bool GrGLSLProgramBuilder::checkSamplerCounts() {
Brian Salomon1edc5b92016-11-29 13:43:46 -0500304 const GrShaderCaps& shaderCaps = *this->shaderCaps();
Brian Salomon1edc5b92016-11-29 13:43:46 -0500305 if (fNumFragmentSamplers > shaderCaps.maxFragmentSamplers()) {
cdalton9c3f1432016-03-11 10:07:37 -0800306 GrCapsDebugf(this->caps(), "Program would use too many fragment samplers\n");
307 return false;
308 }
cdalton9c3f1432016-03-11 10:07:37 -0800309 return true;
310}
311
cdalton87332102016-02-26 12:22:02 -0800312#ifdef SK_DEBUG
egdanielfa896322016-01-13 12:19:30 -0800313void GrGLSLProgramBuilder::verify(const GrPrimitiveProcessor& gp) {
Chris Daltond7291ba2019-03-07 14:17:03 -0700314 SkASSERT(!fFS.fHasReadDstColorThisStage_DebugOnly);
315 SkASSERT(fFS.fUsedProcessorFeaturesThisStage_DebugOnly == gp.requestedFeatures());
egdanielfa896322016-01-13 12:19:30 -0800316}
317
318void GrGLSLProgramBuilder::verify(const GrFragmentProcessor& fp) {
Chris Daltond7291ba2019-03-07 14:17:03 -0700319 SkASSERT(!fFS.fHasReadDstColorThisStage_DebugOnly);
320 SkASSERT(fFS.fUsedProcessorFeaturesThisStage_DebugOnly == fp.requestedFeatures());
321}
322
323void GrGLSLProgramBuilder::verify(const GrXferProcessor& xp) {
324 SkASSERT(xp.willReadDstColor() == fFS.fHasReadDstColorThisStage_DebugOnly);
325 SkASSERT(fFS.fUsedProcessorFeaturesThisStage_DebugOnly == xp.requestedFeatures());
egdaniel8dcdedc2015-11-11 06:27:20 -0800326}
cdalton87332102016-02-26 12:22:02 -0800327#endif
egdaniel8dcdedc2015-11-11 06:27:20 -0800328
329void GrGLSLProgramBuilder::nameVariable(SkString* out, char prefix, const char* name, bool mangle) {
330 if ('\0' == prefix) {
331 *out = name;
332 } else {
333 out->printf("%c%s", prefix, name);
334 }
335 if (mangle) {
336 if (out->endsWith('_')) {
337 // Names containing "__" are reserved.
338 out->append("x");
339 }
340 out->appendf("_Stage%d%s", fStageIndex, fFS.getMangleString().c_str());
341 }
342}
343
Ethan Nicholas2983f402017-05-08 09:36:08 -0400344void GrGLSLProgramBuilder::nameExpression(SkString* output, const char* baseName) {
egdanielfa896322016-01-13 12:19:30 -0800345 // create var to hold stage result. If we already have a valid output name, just use that
346 // otherwise create a new mangled one. This name is only valid if we are reordering stages
347 // and have to tell stage exactly where to put its output.
348 SkString outName;
Ethan Nicholas2983f402017-05-08 09:36:08 -0400349 if (output->size()) {
egdanielfa896322016-01-13 12:19:30 -0800350 outName = output->c_str();
351 } else {
352 this->nameVariable(&outName, '\0', baseName);
353 }
Ethan Nicholasf7b88202017-09-18 14:10:39 -0400354 fFS.codeAppendf("half4 %s;", outName.c_str());
egdanielfa896322016-01-13 12:19:30 -0800355 *output = outName;
356}
357
cdalton5e58cee2016-02-11 12:49:47 -0800358void GrGLSLProgramBuilder::appendUniformDecls(GrShaderFlags visibility, SkString* out) const {
egdaniel7ea439b2015-12-03 09:20:44 -0800359 this->uniformHandler()->appendUniformDecls(visibility, out);
360}
361
Ethan Nicholascd700e92018-08-24 16:43:57 -0400362void GrGLSLProgramBuilder::addRTWidthUniform(const char* name) {
Brian Osmanf7924452019-09-24 10:44:37 -0400363 SkASSERT(!fUniformHandles.fRTWidthUni.isValid());
364 GrGLSLUniformHandler* uniformHandler = this->uniformHandler();
365 fUniformHandles.fRTWidthUni =
Ethan Nicholas16464c32020-04-06 13:53:05 -0400366 uniformHandler->internalAddUniformArray(nullptr, kFragment_GrShaderFlag, kHalf_GrSLType,
367 name, false, 0, nullptr);
Ethan Nicholascd700e92018-08-24 16:43:57 -0400368}
369
Greg Daniele6ab9982018-08-22 13:56:32 +0000370void GrGLSLProgramBuilder::addRTHeightUniform(const char* name) {
Brian Osmanf7924452019-09-24 10:44:37 -0400371 SkASSERT(!fUniformHandles.fRTHeightUni.isValid());
372 GrGLSLUniformHandler* uniformHandler = this->uniformHandler();
373 fUniformHandles.fRTHeightUni =
Ethan Nicholas16464c32020-04-06 13:53:05 -0400374 uniformHandler->internalAddUniformArray(nullptr, kFragment_GrShaderFlag, kHalf_GrSLType,
375 name, false, 0, nullptr);
egdaniel8dcdedc2015-11-11 06:27:20 -0800376}
377
egdaniel9f1d4152016-02-10 09:50:38 -0800378void GrGLSLProgramBuilder::finalizeShaders() {
379 this->varyingHandler()->finalize();
cdalton5e58cee2016-02-11 12:49:47 -0800380 fVS.finalize(kVertex_GrShaderFlag);
csmartdalton276cc412016-11-21 11:55:00 -0700381 if (this->primitiveProcessor().willUseGeoShader()) {
Brian Salomon94efbf52016-11-29 13:43:05 -0500382 SkASSERT(this->shaderCaps()->geometryShaderSupport());
csmartdalton276cc412016-11-21 11:55:00 -0700383 fGS.finalize(kGeometry_GrShaderFlag);
384 }
cdalton5e58cee2016-02-11 12:49:47 -0800385 fFS.finalize(kFragment_GrShaderFlag);
egdaniel9f1d4152016-02-10 09:50:38 -0800386}