blob: 7b7b18da85c09c66be0b2b76811e97f330fb1f91 [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
8#include "glsl/GrGLSLProgramBuilder.h"
9
Brian Salomon94efbf52016-11-29 13:43:05 -050010#include "GrCaps.h"
egdanielfa896322016-01-13 12:19:30 -080011#include "GrPipeline.h"
Brian Salomon94efbf52016-11-29 13:43:05 -050012#include "GrShaderCaps.h"
Brian Salomon739c5bf2016-11-07 09:53:44 -050013#include "GrTexturePriv.h"
egdanielfa896322016-01-13 12:19:30 -080014#include "glsl/GrGLSLFragmentProcessor.h"
15#include "glsl/GrGLSLGeometryProcessor.h"
egdaniel9f1d4152016-02-10 09:50:38 -080016#include "glsl/GrGLSLVarying.h"
egdanielfa896322016-01-13 12:19:30 -080017#include "glsl/GrGLSLXferProcessor.h"
Robert Phillipsfe8da172018-01-24 14:52:02 +000018#include "SkSLCompiler.h"
egdanielfa896322016-01-13 12:19:30 -080019
egdaniel8dcdedc2015-11-11 06:27:20 -080020const int GrGLSLProgramBuilder::kVarsPerBlock = 8;
21
Brian Salomonff168d92018-06-23 15:17:27 -040022GrGLSLProgramBuilder::GrGLSLProgramBuilder(const GrPrimitiveProcessor& primProc,
23 const GrPipeline& pipeline,
Ethan Nicholas38657112017-02-09 17:01:22 -050024 GrProgramDesc* desc)
Brian Salomonff168d92018-06-23 15:17:27 -040025 : fVS(this)
26 , fGS(this)
27 , fFS(this)
28 , fStageIndex(-1)
29 , fPipeline(pipeline)
30 , fPrimProc(primProc)
31 , fDesc(desc)
32 , fGeometryProcessor(nullptr)
33 , fXferProcessor(nullptr)
34 , fNumVertexSamplers(0)
35 , fNumGeometrySamplers(0)
36 , fNumFragmentSamplers(0) {}
cdalton9c3f1432016-03-11 10:07:37 -080037
38void GrGLSLProgramBuilder::addFeature(GrShaderFlags shaders,
39 uint32_t featureBit,
40 const char* extensionName) {
41 if (shaders & kVertex_GrShaderFlag) {
42 fVS.addFeature(featureBit, extensionName);
43 }
44 if (shaders & kGeometry_GrShaderFlag) {
csmartdalton276cc412016-11-21 11:55:00 -070045 SkASSERT(this->primitiveProcessor().willUseGeoShader());
cdalton9c3f1432016-03-11 10:07:37 -080046 fGS.addFeature(featureBit, extensionName);
47 }
48 if (shaders & kFragment_GrShaderFlag) {
49 fFS.addFeature(featureBit, extensionName);
50 }
egdanielfa896322016-01-13 12:19:30 -080051}
52
Ethan Nicholas2983f402017-05-08 09:36:08 -040053bool GrGLSLProgramBuilder::emitAndInstallProcs() {
egdanielfa896322016-01-13 12:19:30 -080054 // First we loop over all of the installed processors and collect coord transforms. These will
55 // be sent to the GrGLSLPrimitiveProcessor in its emitCode function
56 const GrPrimitiveProcessor& primProc = this->primitiveProcessor();
egdanielfa896322016-01-13 12:19:30 -080057
Ethan Nicholas2983f402017-05-08 09:36:08 -040058 SkString inputColor;
59 SkString inputCoverage;
60 this->emitAndInstallPrimProc(primProc, &inputColor, &inputCoverage);
61 this->emitAndInstallFragProcs(&inputColor, &inputCoverage);
62 this->emitAndInstallXferProc(inputColor, inputCoverage);
Brian Salomon42c456f2017-03-06 11:29:48 -050063 this->emitFSOutputSwizzle(this->pipeline().getXferProcessor().hasSecondaryOutput());
cdalton9c3f1432016-03-11 10:07:37 -080064
Brian Salomon559f5562017-11-15 14:28:33 -050065 return this->checkSamplerCounts();
egdanielfa896322016-01-13 12:19:30 -080066}
67
68void GrGLSLProgramBuilder::emitAndInstallPrimProc(const GrPrimitiveProcessor& proc,
Ethan Nicholas2983f402017-05-08 09:36:08 -040069 SkString* outputColor,
70 SkString* outputCoverage) {
egdanielfa896322016-01-13 12:19:30 -080071 // Program builders have a bit of state we need to clear with each effect
72 AutoStageAdvance adv(this);
73 this->nameExpression(outputColor, "outputColor");
74 this->nameExpression(outputCoverage, "outputCoverage");
75
csmartdalton936f81b2017-02-13 15:45:35 -070076 SkASSERT(!fUniformHandles.fRTAdjustmentUni.isValid());
Robert Phillipsfe8da172018-01-24 14:52:02 +000077 GrShaderFlags rtAdjustVisibility;
csmartdalton936f81b2017-02-13 15:45:35 -070078 if (proc.willUseGeoShader()) {
Robert Phillipsfe8da172018-01-24 14:52:02 +000079 rtAdjustVisibility = kGeometry_GrShaderFlag;
80 } else {
81 rtAdjustVisibility = kVertex_GrShaderFlag;
csmartdalton936f81b2017-02-13 15:45:35 -070082 }
Robert Phillipsfe8da172018-01-24 14:52:02 +000083 fUniformHandles.fRTAdjustmentUni = this->uniformHandler()->addUniform(
84 rtAdjustVisibility,
85 kFloat4_GrSLType,
86 SkSL::Compiler::RTADJUST_NAME);
csmartdalton936f81b2017-02-13 15:45:35 -070087 const char* rtAdjustName =
88 this->uniformHandler()->getUniformCStr(fUniformHandles.fRTAdjustmentUni);
89
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);
Brian Salomon7eae3e02018-08-07 14:02:38 +0000104 texSamplers[i] = this->emitSampler(sampler.textureType(), sampler.config(), name.c_str(),
105 sampler.visibility());
Brian Salomone782f842018-07-31 13:53:11 -0400106 }
egdanielfa896322016-01-13 12:19:30 -0800107
bsalomona624bf32016-09-20 09:12:47 -0700108 GrGLSLPrimitiveProcessor::FPCoordTransformHandler transformHandler(fPipeline,
109 &fTransformedCoordVars);
egdanielfa896322016-01-13 12:19:30 -0800110 GrGLSLGeometryProcessor::EmitArgs args(&fVS,
csmartdalton276cc412016-11-21 11:55:00 -0700111 proc.willUseGeoShader() ? &fGS : nullptr,
egdanielfa896322016-01-13 12:19:30 -0800112 &fFS,
113 this->varyingHandler(),
114 this->uniformHandler(),
Brian Salomon94efbf52016-11-29 13:43:05 -0500115 this->shaderCaps(),
egdanielfa896322016-01-13 12:19:30 -0800116 proc,
117 outputColor->c_str(),
118 outputCoverage->c_str(),
csmartdalton936f81b2017-02-13 15:45:35 -0700119 rtAdjustName,
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 fFS.codeAppend("}");
129}
130
Ethan Nicholas2983f402017-05-08 09:36:08 -0400131void GrGLSLProgramBuilder::emitAndInstallFragProcs(SkString* color, SkString* coverage) {
bsalomona624bf32016-09-20 09:12:47 -0700132 int transformedCoordVarsIdx = 0;
Ethan Nicholas2983f402017-05-08 09:36:08 -0400133 SkString** inOut = &color;
Brian Salomon4d3f5172018-06-07 14:42:52 -0400134 SkSTArray<8, std::unique_ptr<GrGLSLFragmentProcessor>> glslFragmentProcessors;
bsalomona624bf32016-09-20 09:12:47 -0700135 for (int i = 0; i < this->pipeline().numFragmentProcessors(); ++i) {
136 if (i == this->pipeline().numColorFragmentProcessors()) {
137 inOut = &coverage;
138 }
Ethan Nicholas2983f402017-05-08 09:36:08 -0400139 SkString output;
egdanielfa896322016-01-13 12:19:30 -0800140 const GrFragmentProcessor& fp = this->pipeline().getFragmentProcessor(i);
Brian Salomon4d3f5172018-06-07 14:42:52 -0400141 output = this->emitAndInstallFragProc(fp, i, transformedCoordVarsIdx, **inOut, output,
142 &glslFragmentProcessors);
bsalomona624bf32016-09-20 09:12:47 -0700143 GrFragmentProcessor::Iter iter(&fp);
144 while (const GrFragmentProcessor* fp = iter.next()) {
145 transformedCoordVarsIdx += fp->numCoordTransforms();
146 }
147 **inOut = output;
egdanielfa896322016-01-13 12:19:30 -0800148 }
Brian Salomon4d3f5172018-06-07 14:42:52 -0400149 fFragmentProcessorCnt = glslFragmentProcessors.count();
150 fFragmentProcessors.reset(new std::unique_ptr<GrGLSLFragmentProcessor>[fFragmentProcessorCnt]);
151 for (int i = 0; i < fFragmentProcessorCnt; ++i) {
152 fFragmentProcessors[i] = std::move(glslFragmentProcessors[i]);
153 }
egdanielfa896322016-01-13 12:19:30 -0800154}
155
156// TODO Processors cannot output zeros because an empty string is all 1s
Ethan Nicholas2983f402017-05-08 09:36:08 -0400157// the fix is to allow effects to take the SkString directly
Brian Salomon4d3f5172018-06-07 14:42:52 -0400158SkString GrGLSLProgramBuilder::emitAndInstallFragProc(
159 const GrFragmentProcessor& fp,
160 int index,
161 int transformedCoordVarsIdx,
162 const SkString& input,
163 SkString output,
164 SkTArray<std::unique_ptr<GrGLSLFragmentProcessor>>* glslFragmentProcessors) {
Ethan Nicholas2983f402017-05-08 09:36:08 -0400165 SkASSERT(input.size());
egdanielfa896322016-01-13 12:19:30 -0800166 // Program builders have a bit of state we need to clear with each effect
167 AutoStageAdvance adv(this);
Ethan Nicholas2983f402017-05-08 09:36:08 -0400168 this->nameExpression(&output, "output");
egdanielfa896322016-01-13 12:19:30 -0800169
170 // Enclose custom code in a block to avoid namespace conflicts
171 SkString openBrace;
172 openBrace.printf("{ // Stage %d, %s\n", fStageIndex, fp.name());
173 fFS.codeAppend(openBrace.c_str());
174
175 GrGLSLFragmentProcessor* fragProc = fp.createGLSLInstance();
176
Brian Salomone782f842018-07-31 13:53:11 -0400177 SkSTArray<4, SamplerHandle> texSamplers;
178 GrFragmentProcessor::Iter fpIter(&fp);
179 int samplerIdx = 0;
180 while (const auto* subFP = fpIter.next()) {
181 for (int i = 0; i < subFP->numTextureSamplers(); ++i) {
182 SkString name;
183 name.printf("TextureSampler_%d", samplerIdx++);
184 const auto& sampler = subFP->textureSampler(i);
185 GrTextureType textureType = sampler.peekTexture()->texturePriv().textureType();
186 texSamplers.emplace_back(this->emitSampler(textureType, sampler.peekTexture()->config(),
187 name.c_str(), kFragment_GrShaderFlag));
188 }
bsalomonb58a2b42016-09-26 06:55:02 -0700189 }
egdanielfa896322016-01-13 12:19:30 -0800190
bsalomona624bf32016-09-20 09:12:47 -0700191 const GrShaderVar* coordVars = fTransformedCoordVars.begin() + transformedCoordVarsIdx;
192 GrGLSLFragmentProcessor::TransformedCoordVars coords(&fp, coordVars);
Brian Salomone782f842018-07-31 13:53:11 -0400193 GrGLSLFragmentProcessor::TextureSamplers textureSamplers(&fp, texSamplers.begin());
egdanielfa896322016-01-13 12:19:30 -0800194 GrGLSLFragmentProcessor::EmitArgs args(&fFS,
195 this->uniformHandler(),
Brian Salomon94efbf52016-11-29 13:43:05 -0500196 this->shaderCaps(),
egdanielfa896322016-01-13 12:19:30 -0800197 fp,
Ethan Nicholas2983f402017-05-08 09:36:08 -0400198 output.c_str(),
199 input.c_str(),
bsalomona624bf32016-09-20 09:12:47 -0700200 coords,
Brian Salomon662ea4b2018-07-12 14:53:49 -0400201 textureSamplers);
dvonbeck9b03e7b2016-08-01 11:01:56 -0700202
egdanielfa896322016-01-13 12:19:30 -0800203 fragProc->emitCode(args);
204
205 // We have to check that effects and the code they emit are consistent, ie if an effect
206 // asks for dst color, then the emit code needs to follow suit
cdalton87332102016-02-26 12:22:02 -0800207 SkDEBUGCODE(verify(fp);)
Brian Salomon4d3f5172018-06-07 14:42:52 -0400208 glslFragmentProcessors->emplace_back(fragProc);
egdanielfa896322016-01-13 12:19:30 -0800209
210 fFS.codeAppend("}");
Ethan Nicholas2983f402017-05-08 09:36:08 -0400211 return output;
egdanielfa896322016-01-13 12:19:30 -0800212}
213
Ethan Nicholas2983f402017-05-08 09:36:08 -0400214void GrGLSLProgramBuilder::emitAndInstallXferProc(const SkString& colorIn,
215 const SkString& coverageIn) {
egdanielfa896322016-01-13 12:19:30 -0800216 // Program builders have a bit of state we need to clear with each effect
217 AutoStageAdvance adv(this);
218
219 SkASSERT(!fXferProcessor);
Brian Salomon18dfa982017-04-03 16:57:43 -0400220 const GrXferProcessor& xp = fPipeline.getXferProcessor();
Robert Phillips369e8b72017-08-01 16:13:04 -0400221 fXferProcessor.reset(xp.createGLSLInstance());
egdanielfa896322016-01-13 12:19:30 -0800222
223 // Enable dual source secondary output if we have one
224 if (xp.hasSecondaryOutput()) {
225 fFS.enableSecondaryOutput();
226 }
227
Brian Salomon94efbf52016-11-29 13:43:05 -0500228 if (this->shaderCaps()->mustDeclareFragmentShaderOutput()) {
egdanielfa896322016-01-13 12:19:30 -0800229 fFS.enableCustomOutput();
230 }
231
232 SkString openBrace;
233 openBrace.printf("{ // Xfer Processor: %s\n", xp.name());
234 fFS.codeAppend(openBrace.c_str());
235
Brian Salomon18dfa982017-04-03 16:57:43 -0400236 SamplerHandle dstTextureSamplerHandle;
237 GrSurfaceOrigin dstTextureOrigin = kTopLeft_GrSurfaceOrigin;
Robert Phillipsbb581ce2017-05-29 15:05:15 -0400238
239 if (GrTexture* dstTexture = fPipeline.peekDstTexture()) {
Brian Salomon18dfa982017-04-03 16:57:43 -0400240 // GrProcessor::TextureSampler sampler(dstTexture);
241 SkString name("DstTextureSampler");
242 dstTextureSamplerHandle =
Brian Salomon60dd8c72018-07-30 10:24:13 -0400243 this->emitSampler(dstTexture->texturePriv().textureType(), dstTexture->config(),
Brian Salomon18dfa982017-04-03 16:57:43 -0400244 "DstTextureSampler", kFragment_GrShaderFlag);
Robert Phillips16d8ec62017-07-27 16:16:25 -0400245 dstTextureOrigin = fPipeline.dstTextureProxy()->origin();
Brian Salomon60dd8c72018-07-30 10:24:13 -0400246 SkASSERT(dstTexture->texturePriv().textureType() != GrTextureType::kExternal);
Brian Salomon18dfa982017-04-03 16:57:43 -0400247 }
egdanielfa896322016-01-13 12:19:30 -0800248
249 GrGLSLXferProcessor::EmitArgs args(&fFS,
250 this->uniformHandler(),
Brian Salomon94efbf52016-11-29 13:43:05 -0500251 this->shaderCaps(),
Brian Salomon18dfa982017-04-03 16:57:43 -0400252 xp,
Ethan Nicholas5af9ea32017-07-28 15:19:46 -0400253 colorIn.size() ? colorIn.c_str() : "float4(1)",
254 coverageIn.size() ? coverageIn.c_str() : "float4(1)",
egdanielfa896322016-01-13 12:19:30 -0800255 fFS.getPrimaryColorOutputName(),
256 fFS.getSecondaryColorOutputName(),
Brian Salomon18dfa982017-04-03 16:57:43 -0400257 dstTextureSamplerHandle,
258 dstTextureOrigin);
egdanielfa896322016-01-13 12:19:30 -0800259 fXferProcessor->emitCode(args);
260
261 // We have to check that effects and the code they emit are consistent, ie if an effect
262 // asks for dst color, then the emit code needs to follow suit
cdalton87332102016-02-26 12:22:02 -0800263 SkDEBUGCODE(verify(xp);)
egdanielfa896322016-01-13 12:19:30 -0800264 fFS.codeAppend("}");
265}
266
Greg Danielbc5d4d72017-05-05 10:28:42 -0400267void GrGLSLProgramBuilder::updateSamplerCounts(GrShaderFlags visibility) {
cdalton74b8d322016-04-11 14:47:28 -0700268 if (visibility & kVertex_GrShaderFlag) {
269 ++fNumVertexSamplers;
270 }
271 if (visibility & kGeometry_GrShaderFlag) {
272 SkASSERT(this->primitiveProcessor().willUseGeoShader());
273 ++fNumGeometrySamplers;
274 }
275 if (visibility & kFragment_GrShaderFlag) {
276 ++fNumFragmentSamplers;
277 }
Greg Danielbc5d4d72017-05-05 10:28:42 -0400278}
279
Brian Salomon60dd8c72018-07-30 10:24:13 -0400280GrGLSLProgramBuilder::SamplerHandle GrGLSLProgramBuilder::emitSampler(GrTextureType textureType,
Greg Danielbc5d4d72017-05-05 10:28:42 -0400281 GrPixelConfig config,
282 const char* name,
283 GrShaderFlags visibility) {
284 this->updateSamplerCounts(visibility);
Chris Dalton47c8ed32017-11-15 18:27:09 -0700285 GrSLPrecision precision = GrSLSamplerPrecision(config);
Brian Salomon94efbf52016-11-29 13:43:05 -0500286 GrSwizzle swizzle = this->shaderCaps()->configTextureSwizzle(config);
Brian Salomon60dd8c72018-07-30 10:24:13 -0400287 return this->uniformHandler()->addSampler(visibility, swizzle, textureType, precision, name);
Brian Salomonf9f45122016-11-29 11:59:17 -0500288}
289
egdanielfa896322016-01-13 12:19:30 -0800290void GrGLSLProgramBuilder::emitFSOutputSwizzle(bool hasSecondaryOutput) {
291 // Swizzle the fragment shader outputs if necessary.
292 GrSwizzle swizzle;
Ethan Nicholas38657112017-02-09 17:01:22 -0500293 swizzle.setFromKey(this->desc()->header().fOutputSwizzle);
egdanielfa896322016-01-13 12:19:30 -0800294 if (swizzle != GrSwizzle::RGBA()) {
295 fFS.codeAppendf("%s = %s.%s;", fFS.getPrimaryColorOutputName(),
296 fFS.getPrimaryColorOutputName(),
297 swizzle.c_str());
298 if (hasSecondaryOutput) {
299 fFS.codeAppendf("%s = %s.%s;", fFS.getSecondaryColorOutputName(),
300 fFS.getSecondaryColorOutputName(),
301 swizzle.c_str());
302 }
303 }
304}
305
cdalton9c3f1432016-03-11 10:07:37 -0800306bool GrGLSLProgramBuilder::checkSamplerCounts() {
Brian Salomon1edc5b92016-11-29 13:43:46 -0500307 const GrShaderCaps& shaderCaps = *this->shaderCaps();
308 if (fNumVertexSamplers > shaderCaps.maxVertexSamplers()) {
cdalton9c3f1432016-03-11 10:07:37 -0800309 GrCapsDebugf(this->caps(), "Program would use too many vertex samplers\n");
310 return false;
311 }
Brian Salomon1edc5b92016-11-29 13:43:46 -0500312 if (fNumGeometrySamplers > shaderCaps.maxGeometrySamplers()) {
cdalton9c3f1432016-03-11 10:07:37 -0800313 GrCapsDebugf(this->caps(), "Program would use too many geometry samplers\n");
314 return false;
315 }
Brian Salomon1edc5b92016-11-29 13:43:46 -0500316 if (fNumFragmentSamplers > shaderCaps.maxFragmentSamplers()) {
cdalton9c3f1432016-03-11 10:07:37 -0800317 GrCapsDebugf(this->caps(), "Program would use too many fragment samplers\n");
318 return false;
319 }
320 // If the same sampler is used in two different shaders, it counts as two combined samplers.
321 int numCombinedSamplers = fNumVertexSamplers + fNumGeometrySamplers + fNumFragmentSamplers;
Brian Salomon1edc5b92016-11-29 13:43:46 -0500322 if (numCombinedSamplers > shaderCaps.maxCombinedSamplers()) {
cdalton9c3f1432016-03-11 10:07:37 -0800323 GrCapsDebugf(this->caps(), "Program would use too many combined samplers\n");
324 return false;
325 }
326 return true;
327}
328
cdalton87332102016-02-26 12:22:02 -0800329#ifdef SK_DEBUG
egdanielfa896322016-01-13 12:19:30 -0800330void GrGLSLProgramBuilder::verify(const GrPrimitiveProcessor& gp) {
egdanielfa896322016-01-13 12:19:30 -0800331}
332
333void GrGLSLProgramBuilder::verify(const GrXferProcessor& xp) {
334 SkASSERT(fFS.hasReadDstColor() == xp.willReadDstColor());
335}
336
337void GrGLSLProgramBuilder::verify(const GrFragmentProcessor& fp) {
egdaniel8dcdedc2015-11-11 06:27:20 -0800338}
cdalton87332102016-02-26 12:22:02 -0800339#endif
egdaniel8dcdedc2015-11-11 06:27:20 -0800340
341void GrGLSLProgramBuilder::nameVariable(SkString* out, char prefix, const char* name, bool mangle) {
342 if ('\0' == prefix) {
343 *out = name;
344 } else {
345 out->printf("%c%s", prefix, name);
346 }
347 if (mangle) {
348 if (out->endsWith('_')) {
349 // Names containing "__" are reserved.
350 out->append("x");
351 }
352 out->appendf("_Stage%d%s", fStageIndex, fFS.getMangleString().c_str());
353 }
354}
355
Ethan Nicholas2983f402017-05-08 09:36:08 -0400356void GrGLSLProgramBuilder::nameExpression(SkString* output, const char* baseName) {
egdanielfa896322016-01-13 12:19:30 -0800357 // create var to hold stage result. If we already have a valid output name, just use that
358 // otherwise create a new mangled one. This name is only valid if we are reordering stages
359 // and have to tell stage exactly where to put its output.
360 SkString outName;
Ethan Nicholas2983f402017-05-08 09:36:08 -0400361 if (output->size()) {
egdanielfa896322016-01-13 12:19:30 -0800362 outName = output->c_str();
363 } else {
364 this->nameVariable(&outName, '\0', baseName);
365 }
Ethan Nicholasf7b88202017-09-18 14:10:39 -0400366 fFS.codeAppendf("half4 %s;", outName.c_str());
egdanielfa896322016-01-13 12:19:30 -0800367 *output = outName;
368}
369
cdalton5e58cee2016-02-11 12:49:47 -0800370void GrGLSLProgramBuilder::appendUniformDecls(GrShaderFlags visibility, SkString* out) const {
egdaniel7ea439b2015-12-03 09:20:44 -0800371 this->uniformHandler()->appendUniformDecls(visibility, out);
372}
373
Ethan Nicholascd700e92018-08-24 16:43:57 -0400374void GrGLSLProgramBuilder::addRTWidthUniform(const char* name) {
375 SkASSERT(!fUniformHandles.fRTWidthUni.isValid());
376 GrGLSLUniformHandler* uniformHandler = this->uniformHandler();
377 fUniformHandles.fRTWidthUni =
378 uniformHandler->internalAddUniformArray(kFragment_GrShaderFlag,
379 kHalf_GrSLType, kDefault_GrSLPrecision,
380 name, false, 0, nullptr);
381}
382
Greg Daniele6ab9982018-08-22 13:56:32 +0000383void GrGLSLProgramBuilder::addRTHeightUniform(const char* name) {
384 SkASSERT(!fUniformHandles.fRTHeightUni.isValid());
egdaniel7ea439b2015-12-03 09:20:44 -0800385 GrGLSLUniformHandler* uniformHandler = this->uniformHandler();
Greg Daniele6ab9982018-08-22 13:56:32 +0000386 fUniformHandles.fRTHeightUni =
cdalton5e58cee2016-02-11 12:49:47 -0800387 uniformHandler->internalAddUniformArray(kFragment_GrShaderFlag,
Greg Daniele6ab9982018-08-22 13:56:32 +0000388 kHalf_GrSLType, kDefault_GrSLPrecision,
Ethan Nicholas941e7e22016-12-12 15:33:30 -0500389 name, false, 0, nullptr);
egdaniel8dcdedc2015-11-11 06:27:20 -0800390}
391
egdaniel9f1d4152016-02-10 09:50:38 -0800392void GrGLSLProgramBuilder::finalizeShaders() {
393 this->varyingHandler()->finalize();
cdalton5e58cee2016-02-11 12:49:47 -0800394 fVS.finalize(kVertex_GrShaderFlag);
csmartdalton276cc412016-11-21 11:55:00 -0700395 if (this->primitiveProcessor().willUseGeoShader()) {
Brian Salomon94efbf52016-11-29 13:43:05 -0500396 SkASSERT(this->shaderCaps()->geometryShaderSupport());
csmartdalton276cc412016-11-21 11:55:00 -0700397 fGS.finalize(kGeometry_GrShaderFlag);
398 }
cdalton5e58cee2016-02-11 12:49:47 -0800399 fFS.finalize(kFragment_GrShaderFlag);
egdaniel9f1d4152016-02-10 09:50:38 -0800400}