blob: 9df85a93de3ec4e12c63a844399e60708fbf4e6e [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 Salomonf19f9ca2019-09-18 15:54:26 -0400258 SkString finalInColor = colorIn.size() ? colorIn : SkString("float4(1)");
Brian Osman65cdd612019-03-14 17:01:57 -0400259
egdanielfa896322016-01-13 12:19:30 -0800260 GrGLSLXferProcessor::EmitArgs args(&fFS,
261 this->uniformHandler(),
Brian Salomon94efbf52016-11-29 13:43:05 -0500262 this->shaderCaps(),
Brian Salomon18dfa982017-04-03 16:57:43 -0400263 xp,
Brian Osman65cdd612019-03-14 17:01:57 -0400264 finalInColor.c_str(),
Ethan Nicholas5af9ea32017-07-28 15:19:46 -0400265 coverageIn.size() ? coverageIn.c_str() : "float4(1)",
egdanielfa896322016-01-13 12:19:30 -0800266 fFS.getPrimaryColorOutputName(),
267 fFS.getSecondaryColorOutputName(),
Brian Salomon18dfa982017-04-03 16:57:43 -0400268 dstTextureSamplerHandle,
Chris Dalton6b76df02019-04-08 11:01:34 -0600269 dstTextureOrigin,
270 this->desc()->header().fOutputSwizzle);
egdanielfa896322016-01-13 12:19:30 -0800271 fXferProcessor->emitCode(args);
272
273 // We have to check that effects and the code they emit are consistent, ie if an effect
274 // asks for dst color, then the emit code needs to follow suit
cdalton87332102016-02-26 12:22:02 -0800275 SkDEBUGCODE(verify(xp);)
egdanielfa896322016-01-13 12:19:30 -0800276 fFS.codeAppend("}");
277}
278
Greg Daniel9a51a862018-11-30 10:18:14 -0500279GrGLSLProgramBuilder::SamplerHandle GrGLSLProgramBuilder::emitSampler(const GrTexture* texture,
280 const GrSamplerState& state,
Greg Daniel2c3398d2019-06-19 11:58:01 -0400281 const GrSwizzle& swizzle,
Greg Daniel0f70be82018-10-08 17:35:08 +0000282 const char* name) {
283 ++fNumFragmentSamplers;
Greg Daniel2c3398d2019-06-19 11:58:01 -0400284 return this->uniformHandler()->addSampler(texture, state, swizzle, name, this->shaderCaps());
Brian Salomonf9f45122016-11-29 11:59:17 -0500285}
286
cdalton9c3f1432016-03-11 10:07:37 -0800287bool GrGLSLProgramBuilder::checkSamplerCounts() {
Brian Salomon1edc5b92016-11-29 13:43:46 -0500288 const GrShaderCaps& shaderCaps = *this->shaderCaps();
Brian Salomon1edc5b92016-11-29 13:43:46 -0500289 if (fNumFragmentSamplers > shaderCaps.maxFragmentSamplers()) {
cdalton9c3f1432016-03-11 10:07:37 -0800290 GrCapsDebugf(this->caps(), "Program would use too many fragment samplers\n");
291 return false;
292 }
cdalton9c3f1432016-03-11 10:07:37 -0800293 return true;
294}
295
cdalton87332102016-02-26 12:22:02 -0800296#ifdef SK_DEBUG
egdanielfa896322016-01-13 12:19:30 -0800297void GrGLSLProgramBuilder::verify(const GrPrimitiveProcessor& gp) {
Chris Daltond7291ba2019-03-07 14:17:03 -0700298 SkASSERT(!fFS.fHasReadDstColorThisStage_DebugOnly);
299 SkASSERT(fFS.fUsedProcessorFeaturesThisStage_DebugOnly == gp.requestedFeatures());
egdanielfa896322016-01-13 12:19:30 -0800300}
301
302void GrGLSLProgramBuilder::verify(const GrFragmentProcessor& fp) {
Chris Daltond7291ba2019-03-07 14:17:03 -0700303 SkASSERT(!fFS.fHasReadDstColorThisStage_DebugOnly);
304 SkASSERT(fFS.fUsedProcessorFeaturesThisStage_DebugOnly == fp.requestedFeatures());
305}
306
307void GrGLSLProgramBuilder::verify(const GrXferProcessor& xp) {
308 SkASSERT(xp.willReadDstColor() == fFS.fHasReadDstColorThisStage_DebugOnly);
309 SkASSERT(fFS.fUsedProcessorFeaturesThisStage_DebugOnly == xp.requestedFeatures());
egdaniel8dcdedc2015-11-11 06:27:20 -0800310}
cdalton87332102016-02-26 12:22:02 -0800311#endif
egdaniel8dcdedc2015-11-11 06:27:20 -0800312
313void GrGLSLProgramBuilder::nameVariable(SkString* out, char prefix, const char* name, bool mangle) {
314 if ('\0' == prefix) {
315 *out = name;
316 } else {
317 out->printf("%c%s", prefix, name);
318 }
319 if (mangle) {
320 if (out->endsWith('_')) {
321 // Names containing "__" are reserved.
322 out->append("x");
323 }
324 out->appendf("_Stage%d%s", fStageIndex, fFS.getMangleString().c_str());
325 }
326}
327
Ethan Nicholas2983f402017-05-08 09:36:08 -0400328void GrGLSLProgramBuilder::nameExpression(SkString* output, const char* baseName) {
egdanielfa896322016-01-13 12:19:30 -0800329 // create var to hold stage result. If we already have a valid output name, just use that
330 // otherwise create a new mangled one. This name is only valid if we are reordering stages
331 // and have to tell stage exactly where to put its output.
332 SkString outName;
Ethan Nicholas2983f402017-05-08 09:36:08 -0400333 if (output->size()) {
egdanielfa896322016-01-13 12:19:30 -0800334 outName = output->c_str();
335 } else {
336 this->nameVariable(&outName, '\0', baseName);
337 }
Ethan Nicholasf7b88202017-09-18 14:10:39 -0400338 fFS.codeAppendf("half4 %s;", outName.c_str());
egdanielfa896322016-01-13 12:19:30 -0800339 *output = outName;
340}
341
cdalton5e58cee2016-02-11 12:49:47 -0800342void GrGLSLProgramBuilder::appendUniformDecls(GrShaderFlags visibility, SkString* out) const {
egdaniel7ea439b2015-12-03 09:20:44 -0800343 this->uniformHandler()->appendUniformDecls(visibility, out);
344}
345
Ethan Nicholascd700e92018-08-24 16:43:57 -0400346void GrGLSLProgramBuilder::addRTWidthUniform(const char* name) {
347 SkASSERT(!fUniformHandles.fRTWidthUni.isValid());
348 GrGLSLUniformHandler* uniformHandler = this->uniformHandler();
349 fUniformHandles.fRTWidthUni =
Ethan Nicholas858fecc2019-03-07 13:19:18 -0500350 uniformHandler->internalAddUniformArray(kFragment_GrShaderFlag, kHalf_GrSLType, name,
351 false, 0, nullptr);
Ethan Nicholascd700e92018-08-24 16:43:57 -0400352}
353
Greg Daniele6ab9982018-08-22 13:56:32 +0000354void GrGLSLProgramBuilder::addRTHeightUniform(const char* name) {
355 SkASSERT(!fUniformHandles.fRTHeightUni.isValid());
egdaniel7ea439b2015-12-03 09:20:44 -0800356 GrGLSLUniformHandler* uniformHandler = this->uniformHandler();
Greg Daniele6ab9982018-08-22 13:56:32 +0000357 fUniformHandles.fRTHeightUni =
Ethan Nicholas858fecc2019-03-07 13:19:18 -0500358 uniformHandler->internalAddUniformArray(kFragment_GrShaderFlag, kHalf_GrSLType, name,
359 false, 0, nullptr);
egdaniel8dcdedc2015-11-11 06:27:20 -0800360}
361
egdaniel9f1d4152016-02-10 09:50:38 -0800362void GrGLSLProgramBuilder::finalizeShaders() {
363 this->varyingHandler()->finalize();
cdalton5e58cee2016-02-11 12:49:47 -0800364 fVS.finalize(kVertex_GrShaderFlag);
csmartdalton276cc412016-11-21 11:55:00 -0700365 if (this->primitiveProcessor().willUseGeoShader()) {
Brian Salomon94efbf52016-11-29 13:43:05 -0500366 SkASSERT(this->shaderCaps()->geometryShaderSupport());
csmartdalton276cc412016-11-21 11:55:00 -0700367 fGS.finalize(kGeometry_GrShaderFlag);
368 }
cdalton5e58cee2016-02-11 12:49:47 -0800369 fFS.finalize(kFragment_GrShaderFlag);
egdaniel9f1d4152016-02-10 09:50:38 -0800370}