blob: c71071b6f7cfaaff6a0c3affff04ddee7cdd2c7d [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
Mike Kleinc0bd9f92019-04-23 12:05:21 -050010#include "src/gpu/GrCaps.h"
11#include "src/gpu/GrPipeline.h"
Brian Salomon201cdbb2019-08-14 17:00:30 -040012#include "src/gpu/GrRenderTarget.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050013#include "src/gpu/GrShaderCaps.h"
14#include "src/gpu/GrTexturePriv.h"
15#include "src/gpu/glsl/GrGLSLFragmentProcessor.h"
16#include "src/gpu/glsl/GrGLSLGeometryProcessor.h"
17#include "src/gpu/glsl/GrGLSLVarying.h"
18#include "src/gpu/glsl/GrGLSLXferProcessor.h"
19#include "src/sksl/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);
cdalton9c3f1432016-03-11 10:07:37 -080065
Brian Salomon559f5562017-11-15 14:28:33 -050066 return this->checkSamplerCounts();
egdanielfa896322016-01-13 12:19:30 -080067}
68
Greg Daniel9a51a862018-11-30 10:18:14 -050069void GrGLSLProgramBuilder::emitAndInstallPrimProc(SkString* outputColor,
Ethan Nicholas2983f402017-05-08 09:36:08 -040070 SkString* outputCoverage) {
Greg Daniel9a51a862018-11-30 10:18:14 -050071 const GrPrimitiveProcessor& proc = this->primitiveProcessor();
72 const GrTextureProxy* const* primProcProxies = this->primProcProxies();
73
egdanielfa896322016-01-13 12:19:30 -080074 // Program builders have a bit of state we need to clear with each effect
75 AutoStageAdvance adv(this);
76 this->nameExpression(outputColor, "outputColor");
77 this->nameExpression(outputCoverage, "outputCoverage");
78
csmartdalton936f81b2017-02-13 15:45:35 -070079 SkASSERT(!fUniformHandles.fRTAdjustmentUni.isValid());
Robert Phillipsfe8da172018-01-24 14:52:02 +000080 GrShaderFlags rtAdjustVisibility;
csmartdalton936f81b2017-02-13 15:45:35 -070081 if (proc.willUseGeoShader()) {
Robert Phillipsfe8da172018-01-24 14:52:02 +000082 rtAdjustVisibility = kGeometry_GrShaderFlag;
83 } else {
84 rtAdjustVisibility = kVertex_GrShaderFlag;
csmartdalton936f81b2017-02-13 15:45:35 -070085 }
Robert Phillipsfe8da172018-01-24 14:52:02 +000086 fUniformHandles.fRTAdjustmentUni = this->uniformHandler()->addUniform(
87 rtAdjustVisibility,
88 kFloat4_GrSLType,
89 SkSL::Compiler::RTADJUST_NAME);
csmartdalton936f81b2017-02-13 15:45:35 -070090 const char* rtAdjustName =
91 this->uniformHandler()->getUniformCStr(fUniformHandles.fRTAdjustmentUni);
92
egdanielfa896322016-01-13 12:19:30 -080093 // Enclose custom code in a block to avoid namespace conflicts
94 SkString openBrace;
95 openBrace.printf("{ // Stage %d, %s\n", fStageIndex, proc.name());
96 fFS.codeAppend(openBrace.c_str());
97 fVS.codeAppendf("// Primitive Processor %s\n", proc.name());
98
99 SkASSERT(!fGeometryProcessor);
Robert Phillips369e8b72017-08-01 16:13:04 -0400100 fGeometryProcessor.reset(proc.createGLSLInstance(*this->shaderCaps()));
egdanielfa896322016-01-13 12:19:30 -0800101
Brian Salomone782f842018-07-31 13:53:11 -0400102 SkAutoSTMalloc<4, SamplerHandle> texSamplers(proc.numTextureSamplers());
103 for (int i = 0; i < proc.numTextureSamplers(); ++i) {
104 SkString name;
105 name.printf("TextureSampler_%d", i);
106 const auto& sampler = proc.textureSampler(i);
Greg Daniel9a51a862018-11-30 10:18:14 -0500107 const GrTexture* texture = primProcProxies[i]->peekTexture();
108 SkASSERT(sampler.textureType() == texture->texturePriv().textureType());
Greg Daniel9a51a862018-11-30 10:18:14 -0500109 texSamplers[i] = this->emitSampler(texture,
110 sampler.samplerState(),
Greg Daniel2c3398d2019-06-19 11:58:01 -0400111 sampler.swizzle(),
Greg Daniel9a51a862018-11-30 10:18:14 -0500112 name.c_str());
Brian Salomone782f842018-07-31 13:53:11 -0400113 }
egdanielfa896322016-01-13 12:19:30 -0800114
bsalomona624bf32016-09-20 09:12:47 -0700115 GrGLSLPrimitiveProcessor::FPCoordTransformHandler transformHandler(fPipeline,
116 &fTransformedCoordVars);
egdanielfa896322016-01-13 12:19:30 -0800117 GrGLSLGeometryProcessor::EmitArgs args(&fVS,
csmartdalton276cc412016-11-21 11:55:00 -0700118 proc.willUseGeoShader() ? &fGS : nullptr,
egdanielfa896322016-01-13 12:19:30 -0800119 &fFS,
120 this->varyingHandler(),
121 this->uniformHandler(),
Brian Salomon94efbf52016-11-29 13:43:05 -0500122 this->shaderCaps(),
egdanielfa896322016-01-13 12:19:30 -0800123 proc,
124 outputColor->c_str(),
125 outputCoverage->c_str(),
csmartdalton936f81b2017-02-13 15:45:35 -0700126 rtAdjustName,
Brian Salomone782f842018-07-31 13:53:11 -0400127 texSamplers.get(),
bsalomona624bf32016-09-20 09:12:47 -0700128 &transformHandler);
egdanielfa896322016-01-13 12:19:30 -0800129 fGeometryProcessor->emitCode(args);
130
131 // We have to check that effects and the code they emit are consistent, ie if an effect
132 // asks for dst color, then the emit code needs to follow suit
cdalton87332102016-02-26 12:22:02 -0800133 SkDEBUGCODE(verify(proc);)
egdanielfa896322016-01-13 12:19:30 -0800134
135 fFS.codeAppend("}");
136}
137
Ethan Nicholas2983f402017-05-08 09:36:08 -0400138void GrGLSLProgramBuilder::emitAndInstallFragProcs(SkString* color, SkString* coverage) {
bsalomona624bf32016-09-20 09:12:47 -0700139 int transformedCoordVarsIdx = 0;
Ethan Nicholas2983f402017-05-08 09:36:08 -0400140 SkString** inOut = &color;
Brian Salomon4d3f5172018-06-07 14:42:52 -0400141 SkSTArray<8, std::unique_ptr<GrGLSLFragmentProcessor>> glslFragmentProcessors;
bsalomona624bf32016-09-20 09:12:47 -0700142 for (int i = 0; i < this->pipeline().numFragmentProcessors(); ++i) {
143 if (i == this->pipeline().numColorFragmentProcessors()) {
144 inOut = &coverage;
145 }
Ethan Nicholas2983f402017-05-08 09:36:08 -0400146 SkString output;
egdanielfa896322016-01-13 12:19:30 -0800147 const GrFragmentProcessor& fp = this->pipeline().getFragmentProcessor(i);
Brian Salomon4d3f5172018-06-07 14:42:52 -0400148 output = this->emitAndInstallFragProc(fp, i, transformedCoordVarsIdx, **inOut, output,
149 &glslFragmentProcessors);
bsalomona624bf32016-09-20 09:12:47 -0700150 GrFragmentProcessor::Iter iter(&fp);
151 while (const GrFragmentProcessor* fp = iter.next()) {
152 transformedCoordVarsIdx += fp->numCoordTransforms();
153 }
154 **inOut = output;
egdanielfa896322016-01-13 12:19:30 -0800155 }
Brian Salomon4d3f5172018-06-07 14:42:52 -0400156 fFragmentProcessorCnt = glslFragmentProcessors.count();
157 fFragmentProcessors.reset(new std::unique_ptr<GrGLSLFragmentProcessor>[fFragmentProcessorCnt]);
158 for (int i = 0; i < fFragmentProcessorCnt; ++i) {
159 fFragmentProcessors[i] = std::move(glslFragmentProcessors[i]);
160 }
egdanielfa896322016-01-13 12:19:30 -0800161}
162
163// TODO Processors cannot output zeros because an empty string is all 1s
Ethan Nicholas2983f402017-05-08 09:36:08 -0400164// the fix is to allow effects to take the SkString directly
Brian Salomon4d3f5172018-06-07 14:42:52 -0400165SkString GrGLSLProgramBuilder::emitAndInstallFragProc(
166 const GrFragmentProcessor& fp,
167 int index,
168 int transformedCoordVarsIdx,
169 const SkString& input,
170 SkString output,
171 SkTArray<std::unique_ptr<GrGLSLFragmentProcessor>>* glslFragmentProcessors) {
Ethan Nicholas2983f402017-05-08 09:36:08 -0400172 SkASSERT(input.size());
egdanielfa896322016-01-13 12:19:30 -0800173 // Program builders have a bit of state we need to clear with each effect
174 AutoStageAdvance adv(this);
Ethan Nicholas2983f402017-05-08 09:36:08 -0400175 this->nameExpression(&output, "output");
egdanielfa896322016-01-13 12:19:30 -0800176
177 // Enclose custom code in a block to avoid namespace conflicts
178 SkString openBrace;
179 openBrace.printf("{ // Stage %d, %s\n", fStageIndex, fp.name());
180 fFS.codeAppend(openBrace.c_str());
181
182 GrGLSLFragmentProcessor* fragProc = fp.createGLSLInstance();
183
Brian Salomone782f842018-07-31 13:53:11 -0400184 SkSTArray<4, SamplerHandle> texSamplers;
185 GrFragmentProcessor::Iter fpIter(&fp);
186 int samplerIdx = 0;
187 while (const auto* subFP = fpIter.next()) {
188 for (int i = 0; i < subFP->numTextureSamplers(); ++i) {
189 SkString name;
190 name.printf("TextureSampler_%d", samplerIdx++);
191 const auto& sampler = subFP->textureSampler(i);
Greg Daniel9a51a862018-11-30 10:18:14 -0500192 texSamplers.emplace_back(this->emitSampler(sampler.peekTexture(),
193 sampler.samplerState(),
Greg Daniel2c3398d2019-06-19 11:58:01 -0400194 sampler.swizzle(),
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
Ethan Nicholasd4efe682019-08-29 16:10:13 -0400199 const GrGLSLPrimitiveProcessor::TransformVar* coordVars = fTransformedCoordVars.begin() +
200 transformedCoordVarsIdx;
bsalomona624bf32016-09-20 09:12:47 -0700201 GrGLSLFragmentProcessor::TransformedCoordVars coords(&fp, coordVars);
Brian Salomone782f842018-07-31 13:53:11 -0400202 GrGLSLFragmentProcessor::TextureSamplers textureSamplers(&fp, texSamplers.begin());
egdanielfa896322016-01-13 12:19:30 -0800203 GrGLSLFragmentProcessor::EmitArgs args(&fFS,
204 this->uniformHandler(),
Brian Salomon94efbf52016-11-29 13:43:05 -0500205 this->shaderCaps(),
egdanielfa896322016-01-13 12:19:30 -0800206 fp,
Ethan Nicholas2983f402017-05-08 09:36:08 -0400207 output.c_str(),
208 input.c_str(),
bsalomona624bf32016-09-20 09:12:47 -0700209 coords,
Brian Salomon662ea4b2018-07-12 14:53:49 -0400210 textureSamplers);
Ethan Nicholas6ad52892019-05-03 13:13:42 +0000211
212 fragProc->emitCode(args);
egdanielfa896322016-01-13 12:19:30 -0800213
214 // We have to check that effects and the code they emit are consistent, ie if an effect
215 // asks for dst color, then the emit code needs to follow suit
cdalton87332102016-02-26 12:22:02 -0800216 SkDEBUGCODE(verify(fp);)
Brian Salomon4d3f5172018-06-07 14:42:52 -0400217 glslFragmentProcessors->emplace_back(fragProc);
egdanielfa896322016-01-13 12:19:30 -0800218
219 fFS.codeAppend("}");
Ethan Nicholas2983f402017-05-08 09:36:08 -0400220 return output;
egdanielfa896322016-01-13 12:19:30 -0800221}
222
Ethan Nicholas2983f402017-05-08 09:36:08 -0400223void GrGLSLProgramBuilder::emitAndInstallXferProc(const SkString& colorIn,
224 const SkString& coverageIn) {
egdanielfa896322016-01-13 12:19:30 -0800225 // Program builders have a bit of state we need to clear with each effect
226 AutoStageAdvance adv(this);
227
228 SkASSERT(!fXferProcessor);
Brian Salomon18dfa982017-04-03 16:57:43 -0400229 const GrXferProcessor& xp = fPipeline.getXferProcessor();
Robert Phillips369e8b72017-08-01 16:13:04 -0400230 fXferProcessor.reset(xp.createGLSLInstance());
egdanielfa896322016-01-13 12:19:30 -0800231
232 // Enable dual source secondary output if we have one
233 if (xp.hasSecondaryOutput()) {
234 fFS.enableSecondaryOutput();
235 }
236
Brian Salomon94efbf52016-11-29 13:43:05 -0500237 if (this->shaderCaps()->mustDeclareFragmentShaderOutput()) {
egdanielfa896322016-01-13 12:19:30 -0800238 fFS.enableCustomOutput();
239 }
240
241 SkString openBrace;
242 openBrace.printf("{ // Xfer Processor: %s\n", xp.name());
243 fFS.codeAppend(openBrace.c_str());
244
Brian Salomon18dfa982017-04-03 16:57:43 -0400245 SamplerHandle dstTextureSamplerHandle;
246 GrSurfaceOrigin dstTextureOrigin = kTopLeft_GrSurfaceOrigin;
Robert Phillipsbb581ce2017-05-29 15:05:15 -0400247
248 if (GrTexture* dstTexture = fPipeline.peekDstTexture()) {
Brian Salomon18dfa982017-04-03 16:57:43 -0400249 // GrProcessor::TextureSampler sampler(dstTexture);
Greg Daniel2c3398d2019-06-19 11:58:01 -0400250 SkASSERT(fPipeline.dstTextureProxy());
251 const GrSwizzle& swizzle = fPipeline.dstTextureProxy()->textureSwizzle();
Brian Salomon18dfa982017-04-03 16:57:43 -0400252 dstTextureSamplerHandle =
Greg Daniel2c3398d2019-06-19 11:58:01 -0400253 this->emitSampler(dstTexture, GrSamplerState(), swizzle, "DstTextureSampler");
Robert Phillips16d8ec62017-07-27 16:16:25 -0400254 dstTextureOrigin = fPipeline.dstTextureProxy()->origin();
Brian Salomon60dd8c72018-07-30 10:24:13 -0400255 SkASSERT(dstTexture->texturePriv().textureType() != GrTextureType::kExternal);
Brian Salomon18dfa982017-04-03 16:57:43 -0400256 }
egdanielfa896322016-01-13 12:19:30 -0800257
Brian Osman65cdd612019-03-14 17:01:57 -0400258 SkString finalInColor;
259 if (colorIn.size()) {
260 if (this->desc()->header().fClampBlendInput) {
261 finalInColor.printf("saturate(%s)", colorIn.c_str());
262 } else {
263 finalInColor = colorIn;
264 }
265 } else {
266 finalInColor = "float4(1)";
267 }
268
egdanielfa896322016-01-13 12:19:30 -0800269 GrGLSLXferProcessor::EmitArgs args(&fFS,
270 this->uniformHandler(),
Brian Salomon94efbf52016-11-29 13:43:05 -0500271 this->shaderCaps(),
Brian Salomon18dfa982017-04-03 16:57:43 -0400272 xp,
Brian Osman65cdd612019-03-14 17:01:57 -0400273 finalInColor.c_str(),
Ethan Nicholas5af9ea32017-07-28 15:19:46 -0400274 coverageIn.size() ? coverageIn.c_str() : "float4(1)",
egdanielfa896322016-01-13 12:19:30 -0800275 fFS.getPrimaryColorOutputName(),
276 fFS.getSecondaryColorOutputName(),
Brian Salomon18dfa982017-04-03 16:57:43 -0400277 dstTextureSamplerHandle,
Chris Dalton6b76df02019-04-08 11:01:34 -0600278 dstTextureOrigin,
279 this->desc()->header().fOutputSwizzle);
egdanielfa896322016-01-13 12:19:30 -0800280 fXferProcessor->emitCode(args);
281
282 // We have to check that effects and the code they emit are consistent, ie if an effect
283 // asks for dst color, then the emit code needs to follow suit
cdalton87332102016-02-26 12:22:02 -0800284 SkDEBUGCODE(verify(xp);)
egdanielfa896322016-01-13 12:19:30 -0800285 fFS.codeAppend("}");
286}
287
Greg Daniel9a51a862018-11-30 10:18:14 -0500288GrGLSLProgramBuilder::SamplerHandle GrGLSLProgramBuilder::emitSampler(const GrTexture* texture,
289 const GrSamplerState& state,
Greg Daniel2c3398d2019-06-19 11:58:01 -0400290 const GrSwizzle& swizzle,
Greg Daniel0f70be82018-10-08 17:35:08 +0000291 const char* name) {
292 ++fNumFragmentSamplers;
Greg Daniel2c3398d2019-06-19 11:58:01 -0400293 return this->uniformHandler()->addSampler(texture, state, swizzle, name, this->shaderCaps());
Brian Salomonf9f45122016-11-29 11:59:17 -0500294}
295
cdalton9c3f1432016-03-11 10:07:37 -0800296bool GrGLSLProgramBuilder::checkSamplerCounts() {
Brian Salomon1edc5b92016-11-29 13:43:46 -0500297 const GrShaderCaps& shaderCaps = *this->shaderCaps();
Brian Salomon1edc5b92016-11-29 13:43:46 -0500298 if (fNumFragmentSamplers > shaderCaps.maxFragmentSamplers()) {
cdalton9c3f1432016-03-11 10:07:37 -0800299 GrCapsDebugf(this->caps(), "Program would use too many fragment samplers\n");
300 return false;
301 }
cdalton9c3f1432016-03-11 10:07:37 -0800302 return true;
303}
304
cdalton87332102016-02-26 12:22:02 -0800305#ifdef SK_DEBUG
egdanielfa896322016-01-13 12:19:30 -0800306void GrGLSLProgramBuilder::verify(const GrPrimitiveProcessor& gp) {
Chris Daltond7291ba2019-03-07 14:17:03 -0700307 SkASSERT(!fFS.fHasReadDstColorThisStage_DebugOnly);
308 SkASSERT(fFS.fUsedProcessorFeaturesThisStage_DebugOnly == gp.requestedFeatures());
egdanielfa896322016-01-13 12:19:30 -0800309}
310
311void GrGLSLProgramBuilder::verify(const GrFragmentProcessor& fp) {
Chris Daltond7291ba2019-03-07 14:17:03 -0700312 SkASSERT(!fFS.fHasReadDstColorThisStage_DebugOnly);
313 SkASSERT(fFS.fUsedProcessorFeaturesThisStage_DebugOnly == fp.requestedFeatures());
314}
315
316void GrGLSLProgramBuilder::verify(const GrXferProcessor& xp) {
317 SkASSERT(xp.willReadDstColor() == fFS.fHasReadDstColorThisStage_DebugOnly);
318 SkASSERT(fFS.fUsedProcessorFeaturesThisStage_DebugOnly == xp.requestedFeatures());
egdaniel8dcdedc2015-11-11 06:27:20 -0800319}
cdalton87332102016-02-26 12:22:02 -0800320#endif
egdaniel8dcdedc2015-11-11 06:27:20 -0800321
322void GrGLSLProgramBuilder::nameVariable(SkString* out, char prefix, const char* name, bool mangle) {
323 if ('\0' == prefix) {
324 *out = name;
325 } else {
326 out->printf("%c%s", prefix, name);
327 }
328 if (mangle) {
329 if (out->endsWith('_')) {
330 // Names containing "__" are reserved.
331 out->append("x");
332 }
333 out->appendf("_Stage%d%s", fStageIndex, fFS.getMangleString().c_str());
334 }
335}
336
Ethan Nicholas2983f402017-05-08 09:36:08 -0400337void GrGLSLProgramBuilder::nameExpression(SkString* output, const char* baseName) {
egdanielfa896322016-01-13 12:19:30 -0800338 // create var to hold stage result. If we already have a valid output name, just use that
339 // otherwise create a new mangled one. This name is only valid if we are reordering stages
340 // and have to tell stage exactly where to put its output.
341 SkString outName;
Ethan Nicholas2983f402017-05-08 09:36:08 -0400342 if (output->size()) {
egdanielfa896322016-01-13 12:19:30 -0800343 outName = output->c_str();
344 } else {
345 this->nameVariable(&outName, '\0', baseName);
346 }
Ethan Nicholasf7b88202017-09-18 14:10:39 -0400347 fFS.codeAppendf("half4 %s;", outName.c_str());
egdanielfa896322016-01-13 12:19:30 -0800348 *output = outName;
349}
350
cdalton5e58cee2016-02-11 12:49:47 -0800351void GrGLSLProgramBuilder::appendUniformDecls(GrShaderFlags visibility, SkString* out) const {
egdaniel7ea439b2015-12-03 09:20:44 -0800352 this->uniformHandler()->appendUniformDecls(visibility, out);
353}
354
Ethan Nicholascd700e92018-08-24 16:43:57 -0400355void GrGLSLProgramBuilder::addRTWidthUniform(const char* name) {
356 SkASSERT(!fUniformHandles.fRTWidthUni.isValid());
357 GrGLSLUniformHandler* uniformHandler = this->uniformHandler();
358 fUniformHandles.fRTWidthUni =
Ethan Nicholas858fecc2019-03-07 13:19:18 -0500359 uniformHandler->internalAddUniformArray(kFragment_GrShaderFlag, kHalf_GrSLType, name,
360 false, 0, nullptr);
Ethan Nicholascd700e92018-08-24 16:43:57 -0400361}
362
Greg Daniele6ab9982018-08-22 13:56:32 +0000363void GrGLSLProgramBuilder::addRTHeightUniform(const char* name) {
364 SkASSERT(!fUniformHandles.fRTHeightUni.isValid());
egdaniel7ea439b2015-12-03 09:20:44 -0800365 GrGLSLUniformHandler* uniformHandler = this->uniformHandler();
Greg Daniele6ab9982018-08-22 13:56:32 +0000366 fUniformHandles.fRTHeightUni =
Ethan Nicholas858fecc2019-03-07 13:19:18 -0500367 uniformHandler->internalAddUniformArray(kFragment_GrShaderFlag, kHalf_GrSLType, name,
368 false, 0, nullptr);
egdaniel8dcdedc2015-11-11 06:27:20 -0800369}
370
egdaniel9f1d4152016-02-10 09:50:38 -0800371void GrGLSLProgramBuilder::finalizeShaders() {
372 this->varyingHandler()->finalize();
cdalton5e58cee2016-02-11 12:49:47 -0800373 fVS.finalize(kVertex_GrShaderFlag);
csmartdalton276cc412016-11-21 11:55:00 -0700374 if (this->primitiveProcessor().willUseGeoShader()) {
Brian Salomon94efbf52016-11-29 13:43:05 -0500375 SkASSERT(this->shaderCaps()->geometryShaderSupport());
csmartdalton276cc412016-11-21 11:55:00 -0700376 fGS.finalize(kGeometry_GrShaderFlag);
377 }
cdalton5e58cee2016-02-11 12:49:47 -0800378 fFS.finalize(kFragment_GrShaderFlag);
egdaniel9f1d4152016-02-10 09:50:38 -0800379}