blob: b0cf8169d4d5fa41d9287a89f0f5595ae6f216a6 [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 Salomonfdf05f42018-08-06 17:51:35 -0400104 GrTextureType textureType = sampler.peekTexture()->texturePriv().textureType();
105 texSamplers[i] = this->emitSampler(textureType, sampler.peekTexture()->config(),
106 name.c_str(), sampler.visibility());
Brian Salomone782f842018-07-31 13:53:11 -0400107 }
egdanielfa896322016-01-13 12:19:30 -0800108
bsalomona624bf32016-09-20 09:12:47 -0700109 GrGLSLPrimitiveProcessor::FPCoordTransformHandler transformHandler(fPipeline,
110 &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(),
csmartdalton936f81b2017-02-13 15:45:35 -0700120 rtAdjustName,
Brian Salomone782f842018-07-31 13:53:11 -0400121 texSamplers.get(),
bsalomona624bf32016-09-20 09:12:47 -0700122 &transformHandler);
egdanielfa896322016-01-13 12:19:30 -0800123 fGeometryProcessor->emitCode(args);
124
125 // We have to check that effects and the code they emit are consistent, ie if an effect
126 // asks for dst color, then the emit code needs to follow suit
cdalton87332102016-02-26 12:22:02 -0800127 SkDEBUGCODE(verify(proc);)
egdanielfa896322016-01-13 12:19:30 -0800128
129 fFS.codeAppend("}");
130}
131
Ethan Nicholas2983f402017-05-08 09:36:08 -0400132void GrGLSLProgramBuilder::emitAndInstallFragProcs(SkString* color, SkString* coverage) {
bsalomona624bf32016-09-20 09:12:47 -0700133 int transformedCoordVarsIdx = 0;
Ethan Nicholas2983f402017-05-08 09:36:08 -0400134 SkString** inOut = &color;
Brian Salomon4d3f5172018-06-07 14:42:52 -0400135 SkSTArray<8, std::unique_ptr<GrGLSLFragmentProcessor>> glslFragmentProcessors;
bsalomona624bf32016-09-20 09:12:47 -0700136 for (int i = 0; i < this->pipeline().numFragmentProcessors(); ++i) {
137 if (i == this->pipeline().numColorFragmentProcessors()) {
138 inOut = &coverage;
139 }
Ethan Nicholas2983f402017-05-08 09:36:08 -0400140 SkString output;
egdanielfa896322016-01-13 12:19:30 -0800141 const GrFragmentProcessor& fp = this->pipeline().getFragmentProcessor(i);
Brian Salomon4d3f5172018-06-07 14:42:52 -0400142 output = this->emitAndInstallFragProc(fp, i, transformedCoordVarsIdx, **inOut, output,
143 &glslFragmentProcessors);
bsalomona624bf32016-09-20 09:12:47 -0700144 GrFragmentProcessor::Iter iter(&fp);
145 while (const GrFragmentProcessor* fp = iter.next()) {
146 transformedCoordVarsIdx += fp->numCoordTransforms();
147 }
148 **inOut = output;
egdanielfa896322016-01-13 12:19:30 -0800149 }
Brian Salomon4d3f5172018-06-07 14:42:52 -0400150 fFragmentProcessorCnt = glslFragmentProcessors.count();
151 fFragmentProcessors.reset(new std::unique_ptr<GrGLSLFragmentProcessor>[fFragmentProcessorCnt]);
152 for (int i = 0; i < fFragmentProcessorCnt; ++i) {
153 fFragmentProcessors[i] = std::move(glslFragmentProcessors[i]);
154 }
egdanielfa896322016-01-13 12:19:30 -0800155}
156
157// TODO Processors cannot output zeros because an empty string is all 1s
Ethan Nicholas2983f402017-05-08 09:36:08 -0400158// the fix is to allow effects to take the SkString directly
Brian Salomon4d3f5172018-06-07 14:42:52 -0400159SkString GrGLSLProgramBuilder::emitAndInstallFragProc(
160 const GrFragmentProcessor& fp,
161 int index,
162 int transformedCoordVarsIdx,
163 const SkString& input,
164 SkString output,
165 SkTArray<std::unique_ptr<GrGLSLFragmentProcessor>>* glslFragmentProcessors) {
Ethan Nicholas2983f402017-05-08 09:36:08 -0400166 SkASSERT(input.size());
egdanielfa896322016-01-13 12:19:30 -0800167 // Program builders have a bit of state we need to clear with each effect
168 AutoStageAdvance adv(this);
Ethan Nicholas2983f402017-05-08 09:36:08 -0400169 this->nameExpression(&output, "output");
egdanielfa896322016-01-13 12:19:30 -0800170
171 // Enclose custom code in a block to avoid namespace conflicts
172 SkString openBrace;
173 openBrace.printf("{ // Stage %d, %s\n", fStageIndex, fp.name());
174 fFS.codeAppend(openBrace.c_str());
175
176 GrGLSLFragmentProcessor* fragProc = fp.createGLSLInstance();
177
Brian Salomone782f842018-07-31 13:53:11 -0400178 SkSTArray<4, SamplerHandle> texSamplers;
179 GrFragmentProcessor::Iter fpIter(&fp);
180 int samplerIdx = 0;
181 while (const auto* subFP = fpIter.next()) {
182 for (int i = 0; i < subFP->numTextureSamplers(); ++i) {
183 SkString name;
184 name.printf("TextureSampler_%d", samplerIdx++);
185 const auto& sampler = subFP->textureSampler(i);
186 GrTextureType textureType = sampler.peekTexture()->texturePriv().textureType();
187 texSamplers.emplace_back(this->emitSampler(textureType, sampler.peekTexture()->config(),
188 name.c_str(), kFragment_GrShaderFlag));
189 }
bsalomonb58a2b42016-09-26 06:55:02 -0700190 }
egdanielfa896322016-01-13 12:19:30 -0800191
bsalomona624bf32016-09-20 09:12:47 -0700192 const GrShaderVar* coordVars = fTransformedCoordVars.begin() + transformedCoordVarsIdx;
193 GrGLSLFragmentProcessor::TransformedCoordVars coords(&fp, coordVars);
Brian Salomone782f842018-07-31 13:53:11 -0400194 GrGLSLFragmentProcessor::TextureSamplers textureSamplers(&fp, texSamplers.begin());
egdanielfa896322016-01-13 12:19:30 -0800195 GrGLSLFragmentProcessor::EmitArgs args(&fFS,
196 this->uniformHandler(),
Brian Salomon94efbf52016-11-29 13:43:05 -0500197 this->shaderCaps(),
egdanielfa896322016-01-13 12:19:30 -0800198 fp,
Ethan Nicholas2983f402017-05-08 09:36:08 -0400199 output.c_str(),
200 input.c_str(),
bsalomona624bf32016-09-20 09:12:47 -0700201 coords,
Brian Salomon662ea4b2018-07-12 14:53:49 -0400202 textureSamplers);
dvonbeck9b03e7b2016-08-01 11:01:56 -0700203
egdanielfa896322016-01-13 12:19:30 -0800204 fragProc->emitCode(args);
205
206 // We have to check that effects and the code they emit are consistent, ie if an effect
207 // asks for dst color, then the emit code needs to follow suit
cdalton87332102016-02-26 12:22:02 -0800208 SkDEBUGCODE(verify(fp);)
Brian Salomon4d3f5172018-06-07 14:42:52 -0400209 glslFragmentProcessors->emplace_back(fragProc);
egdanielfa896322016-01-13 12:19:30 -0800210
211 fFS.codeAppend("}");
Ethan Nicholas2983f402017-05-08 09:36:08 -0400212 return output;
egdanielfa896322016-01-13 12:19:30 -0800213}
214
Ethan Nicholas2983f402017-05-08 09:36:08 -0400215void GrGLSLProgramBuilder::emitAndInstallXferProc(const SkString& colorIn,
216 const SkString& coverageIn) {
egdanielfa896322016-01-13 12:19:30 -0800217 // Program builders have a bit of state we need to clear with each effect
218 AutoStageAdvance adv(this);
219
220 SkASSERT(!fXferProcessor);
Brian Salomon18dfa982017-04-03 16:57:43 -0400221 const GrXferProcessor& xp = fPipeline.getXferProcessor();
Robert Phillips369e8b72017-08-01 16:13:04 -0400222 fXferProcessor.reset(xp.createGLSLInstance());
egdanielfa896322016-01-13 12:19:30 -0800223
224 // Enable dual source secondary output if we have one
225 if (xp.hasSecondaryOutput()) {
226 fFS.enableSecondaryOutput();
227 }
228
Brian Salomon94efbf52016-11-29 13:43:05 -0500229 if (this->shaderCaps()->mustDeclareFragmentShaderOutput()) {
egdanielfa896322016-01-13 12:19:30 -0800230 fFS.enableCustomOutput();
231 }
232
233 SkString openBrace;
234 openBrace.printf("{ // Xfer Processor: %s\n", xp.name());
235 fFS.codeAppend(openBrace.c_str());
236
Brian Salomon18dfa982017-04-03 16:57:43 -0400237 SamplerHandle dstTextureSamplerHandle;
238 GrSurfaceOrigin dstTextureOrigin = kTopLeft_GrSurfaceOrigin;
Robert Phillipsbb581ce2017-05-29 15:05:15 -0400239
240 if (GrTexture* dstTexture = fPipeline.peekDstTexture()) {
Brian Salomon18dfa982017-04-03 16:57:43 -0400241 // GrProcessor::TextureSampler sampler(dstTexture);
242 SkString name("DstTextureSampler");
243 dstTextureSamplerHandle =
Brian Salomon60dd8c72018-07-30 10:24:13 -0400244 this->emitSampler(dstTexture->texturePriv().textureType(), dstTexture->config(),
Brian Salomon18dfa982017-04-03 16:57:43 -0400245 "DstTextureSampler", kFragment_GrShaderFlag);
Robert Phillips16d8ec62017-07-27 16:16:25 -0400246 dstTextureOrigin = fPipeline.dstTextureProxy()->origin();
Brian Salomon60dd8c72018-07-30 10:24:13 -0400247 SkASSERT(dstTexture->texturePriv().textureType() != GrTextureType::kExternal);
Brian Salomon18dfa982017-04-03 16:57:43 -0400248 }
egdanielfa896322016-01-13 12:19:30 -0800249
250 GrGLSLXferProcessor::EmitArgs args(&fFS,
251 this->uniformHandler(),
Brian Salomon94efbf52016-11-29 13:43:05 -0500252 this->shaderCaps(),
Brian Salomon18dfa982017-04-03 16:57:43 -0400253 xp,
Ethan Nicholas5af9ea32017-07-28 15:19:46 -0400254 colorIn.size() ? colorIn.c_str() : "float4(1)",
255 coverageIn.size() ? coverageIn.c_str() : "float4(1)",
egdanielfa896322016-01-13 12:19:30 -0800256 fFS.getPrimaryColorOutputName(),
257 fFS.getSecondaryColorOutputName(),
Brian Salomon18dfa982017-04-03 16:57:43 -0400258 dstTextureSamplerHandle,
259 dstTextureOrigin);
egdanielfa896322016-01-13 12:19:30 -0800260 fXferProcessor->emitCode(args);
261
262 // We have to check that effects and the code they emit are consistent, ie if an effect
263 // asks for dst color, then the emit code needs to follow suit
cdalton87332102016-02-26 12:22:02 -0800264 SkDEBUGCODE(verify(xp);)
egdanielfa896322016-01-13 12:19:30 -0800265 fFS.codeAppend("}");
266}
267
Greg Danielbc5d4d72017-05-05 10:28:42 -0400268void GrGLSLProgramBuilder::updateSamplerCounts(GrShaderFlags visibility) {
cdalton74b8d322016-04-11 14:47:28 -0700269 if (visibility & kVertex_GrShaderFlag) {
270 ++fNumVertexSamplers;
271 }
272 if (visibility & kGeometry_GrShaderFlag) {
273 SkASSERT(this->primitiveProcessor().willUseGeoShader());
274 ++fNumGeometrySamplers;
275 }
276 if (visibility & kFragment_GrShaderFlag) {
277 ++fNumFragmentSamplers;
278 }
Greg Danielbc5d4d72017-05-05 10:28:42 -0400279}
280
Brian Salomon60dd8c72018-07-30 10:24:13 -0400281GrGLSLProgramBuilder::SamplerHandle GrGLSLProgramBuilder::emitSampler(GrTextureType textureType,
Greg Danielbc5d4d72017-05-05 10:28:42 -0400282 GrPixelConfig config,
283 const char* name,
284 GrShaderFlags visibility) {
285 this->updateSamplerCounts(visibility);
Chris Dalton47c8ed32017-11-15 18:27:09 -0700286 GrSLPrecision precision = GrSLSamplerPrecision(config);
Brian Salomon94efbf52016-11-29 13:43:05 -0500287 GrSwizzle swizzle = this->shaderCaps()->configTextureSwizzle(config);
Brian Salomon60dd8c72018-07-30 10:24:13 -0400288 return this->uniformHandler()->addSampler(visibility, swizzle, textureType, precision, name);
Brian Salomonf9f45122016-11-29 11:59:17 -0500289}
290
egdanielfa896322016-01-13 12:19:30 -0800291void GrGLSLProgramBuilder::emitFSOutputSwizzle(bool hasSecondaryOutput) {
292 // Swizzle the fragment shader outputs if necessary.
293 GrSwizzle swizzle;
Ethan Nicholas38657112017-02-09 17:01:22 -0500294 swizzle.setFromKey(this->desc()->header().fOutputSwizzle);
egdanielfa896322016-01-13 12:19:30 -0800295 if (swizzle != GrSwizzle::RGBA()) {
296 fFS.codeAppendf("%s = %s.%s;", fFS.getPrimaryColorOutputName(),
297 fFS.getPrimaryColorOutputName(),
298 swizzle.c_str());
299 if (hasSecondaryOutput) {
300 fFS.codeAppendf("%s = %s.%s;", fFS.getSecondaryColorOutputName(),
301 fFS.getSecondaryColorOutputName(),
302 swizzle.c_str());
303 }
304 }
305}
306
cdalton9c3f1432016-03-11 10:07:37 -0800307bool GrGLSLProgramBuilder::checkSamplerCounts() {
Brian Salomon1edc5b92016-11-29 13:43:46 -0500308 const GrShaderCaps& shaderCaps = *this->shaderCaps();
309 if (fNumVertexSamplers > shaderCaps.maxVertexSamplers()) {
cdalton9c3f1432016-03-11 10:07:37 -0800310 GrCapsDebugf(this->caps(), "Program would use too many vertex samplers\n");
311 return false;
312 }
Brian Salomon1edc5b92016-11-29 13:43:46 -0500313 if (fNumGeometrySamplers > shaderCaps.maxGeometrySamplers()) {
cdalton9c3f1432016-03-11 10:07:37 -0800314 GrCapsDebugf(this->caps(), "Program would use too many geometry samplers\n");
315 return false;
316 }
Brian Salomon1edc5b92016-11-29 13:43:46 -0500317 if (fNumFragmentSamplers > shaderCaps.maxFragmentSamplers()) {
cdalton9c3f1432016-03-11 10:07:37 -0800318 GrCapsDebugf(this->caps(), "Program would use too many fragment samplers\n");
319 return false;
320 }
321 // If the same sampler is used in two different shaders, it counts as two combined samplers.
322 int numCombinedSamplers = fNumVertexSamplers + fNumGeometrySamplers + fNumFragmentSamplers;
Brian Salomon1edc5b92016-11-29 13:43:46 -0500323 if (numCombinedSamplers > shaderCaps.maxCombinedSamplers()) {
cdalton9c3f1432016-03-11 10:07:37 -0800324 GrCapsDebugf(this->caps(), "Program would use too many combined samplers\n");
325 return false;
326 }
327 return true;
328}
329
cdalton87332102016-02-26 12:22:02 -0800330#ifdef SK_DEBUG
egdanielfa896322016-01-13 12:19:30 -0800331void GrGLSLProgramBuilder::verify(const GrPrimitiveProcessor& gp) {
egdanielfa896322016-01-13 12:19:30 -0800332}
333
334void GrGLSLProgramBuilder::verify(const GrXferProcessor& xp) {
335 SkASSERT(fFS.hasReadDstColor() == xp.willReadDstColor());
336}
337
338void GrGLSLProgramBuilder::verify(const GrFragmentProcessor& fp) {
egdaniel8dcdedc2015-11-11 06:27:20 -0800339}
cdalton87332102016-02-26 12:22:02 -0800340#endif
egdaniel8dcdedc2015-11-11 06:27:20 -0800341
342void GrGLSLProgramBuilder::nameVariable(SkString* out, char prefix, const char* name, bool mangle) {
343 if ('\0' == prefix) {
344 *out = name;
345 } else {
346 out->printf("%c%s", prefix, name);
347 }
348 if (mangle) {
349 if (out->endsWith('_')) {
350 // Names containing "__" are reserved.
351 out->append("x");
352 }
353 out->appendf("_Stage%d%s", fStageIndex, fFS.getMangleString().c_str());
354 }
355}
356
Ethan Nicholas2983f402017-05-08 09:36:08 -0400357void GrGLSLProgramBuilder::nameExpression(SkString* output, const char* baseName) {
egdanielfa896322016-01-13 12:19:30 -0800358 // create var to hold stage result. If we already have a valid output name, just use that
359 // otherwise create a new mangled one. This name is only valid if we are reordering stages
360 // and have to tell stage exactly where to put its output.
361 SkString outName;
Ethan Nicholas2983f402017-05-08 09:36:08 -0400362 if (output->size()) {
egdanielfa896322016-01-13 12:19:30 -0800363 outName = output->c_str();
364 } else {
365 this->nameVariable(&outName, '\0', baseName);
366 }
Ethan Nicholasf7b88202017-09-18 14:10:39 -0400367 fFS.codeAppendf("half4 %s;", outName.c_str());
egdanielfa896322016-01-13 12:19:30 -0800368 *output = outName;
369}
370
cdalton5e58cee2016-02-11 12:49:47 -0800371void GrGLSLProgramBuilder::appendUniformDecls(GrShaderFlags visibility, SkString* out) const {
egdaniel7ea439b2015-12-03 09:20:44 -0800372 this->uniformHandler()->appendUniformDecls(visibility, out);
373}
374
Ethan Nicholas941e7e22016-12-12 15:33:30 -0500375void GrGLSLProgramBuilder::addRTHeightUniform(const char* name) {
egdaniel7ea439b2015-12-03 09:20:44 -0800376 SkASSERT(!fUniformHandles.fRTHeightUni.isValid());
377 GrGLSLUniformHandler* uniformHandler = this->uniformHandler();
378 fUniformHandles.fRTHeightUni =
cdalton5e58cee2016-02-11 12:49:47 -0800379 uniformHandler->internalAddUniformArray(kFragment_GrShaderFlag,
Ethan Nicholasf7b88202017-09-18 14:10:39 -0400380 kHalf_GrSLType, kDefault_GrSLPrecision,
Ethan Nicholas941e7e22016-12-12 15:33:30 -0500381 name, false, 0, nullptr);
egdaniel8dcdedc2015-11-11 06:27:20 -0800382}
383
egdaniel9f1d4152016-02-10 09:50:38 -0800384void GrGLSLProgramBuilder::finalizeShaders() {
385 this->varyingHandler()->finalize();
cdalton5e58cee2016-02-11 12:49:47 -0800386 fVS.finalize(kVertex_GrShaderFlag);
csmartdalton276cc412016-11-21 11:55:00 -0700387 if (this->primitiveProcessor().willUseGeoShader()) {
Brian Salomon94efbf52016-11-29 13:43:05 -0500388 SkASSERT(this->shaderCaps()->geometryShaderSupport());
csmartdalton276cc412016-11-21 11:55:00 -0700389 fGS.finalize(kGeometry_GrShaderFlag);
390 }
cdalton5e58cee2016-02-11 12:49:47 -0800391 fFS.finalize(kFragment_GrShaderFlag);
egdaniel9f1d4152016-02-10 09:50:38 -0800392}