blob: 25ac628928e1cc34a97242567fb4c4f73832bd6c [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 Phillips65a77752019-10-02 15:22:24 -040023GrGLSLProgramBuilder::GrGLSLProgramBuilder(GrRenderTarget* renderTarget,
24 int numSamples,
25 GrSurfaceOrigin origin,
Robert Phillipsd0fe8752019-01-31 14:13:59 -050026 const GrPrimitiveProcessor& primProc,
Greg Daniel9a51a862018-11-30 10:18:14 -050027 const GrTextureProxy* const primProcProxies[],
Brian Salomonff168d92018-06-23 15:17:27 -040028 const GrPipeline& pipeline,
Ethan Nicholas38657112017-02-09 17:01:22 -050029 GrProgramDesc* desc)
Brian Salomonff168d92018-06-23 15:17:27 -040030 : fVS(this)
31 , fGS(this)
32 , fFS(this)
33 , fStageIndex(-1)
Chris Daltond7291ba2019-03-07 14:17:03 -070034 , fRenderTarget(renderTarget)
Robert Phillips65a77752019-10-02 15:22:24 -040035 , fNumSamples(numSamples)
Robert Phillipsd0fe8752019-01-31 14:13:59 -050036 , fOrigin(origin)
Brian Salomonff168d92018-06-23 15:17:27 -040037 , fPipeline(pipeline)
38 , fPrimProc(primProc)
Greg Daniel9a51a862018-11-30 10:18:14 -050039 , fPrimProcProxies(primProcProxies)
Brian Salomonff168d92018-06-23 15:17:27 -040040 , fDesc(desc)
41 , fGeometryProcessor(nullptr)
42 , fXferProcessor(nullptr)
Brian Salomonff168d92018-06-23 15:17:27 -040043 , fNumFragmentSamplers(0) {}
cdalton9c3f1432016-03-11 10:07:37 -080044
45void GrGLSLProgramBuilder::addFeature(GrShaderFlags shaders,
46 uint32_t featureBit,
47 const char* extensionName) {
48 if (shaders & kVertex_GrShaderFlag) {
49 fVS.addFeature(featureBit, extensionName);
50 }
51 if (shaders & kGeometry_GrShaderFlag) {
csmartdalton276cc412016-11-21 11:55:00 -070052 SkASSERT(this->primitiveProcessor().willUseGeoShader());
cdalton9c3f1432016-03-11 10:07:37 -080053 fGS.addFeature(featureBit, extensionName);
54 }
55 if (shaders & kFragment_GrShaderFlag) {
56 fFS.addFeature(featureBit, extensionName);
57 }
egdanielfa896322016-01-13 12:19:30 -080058}
59
Ethan Nicholas2983f402017-05-08 09:36:08 -040060bool GrGLSLProgramBuilder::emitAndInstallProcs() {
egdanielfa896322016-01-13 12:19:30 -080061 // First we loop over all of the installed processors and collect coord transforms. These will
62 // be sent to the GrGLSLPrimitiveProcessor in its emitCode function
Ethan Nicholas2983f402017-05-08 09:36:08 -040063 SkString inputColor;
64 SkString inputCoverage;
Greg Daniel9a51a862018-11-30 10:18:14 -050065 this->emitAndInstallPrimProc(&inputColor, &inputCoverage);
Ethan Nicholas2983f402017-05-08 09:36:08 -040066 this->emitAndInstallFragProcs(&inputColor, &inputCoverage);
67 this->emitAndInstallXferProc(inputColor, inputCoverage);
cdalton9c3f1432016-03-11 10:07:37 -080068
Brian Salomon559f5562017-11-15 14:28:33 -050069 return this->checkSamplerCounts();
egdanielfa896322016-01-13 12:19:30 -080070}
71
Greg Daniel9a51a862018-11-30 10:18:14 -050072void GrGLSLProgramBuilder::emitAndInstallPrimProc(SkString* outputColor,
Ethan Nicholas2983f402017-05-08 09:36:08 -040073 SkString* outputCoverage) {
Greg Daniel9a51a862018-11-30 10:18:14 -050074 const GrPrimitiveProcessor& proc = this->primitiveProcessor();
75 const GrTextureProxy* const* primProcProxies = this->primProcProxies();
76
egdanielfa896322016-01-13 12:19:30 -080077 // Program builders have a bit of state we need to clear with each effect
78 AutoStageAdvance adv(this);
79 this->nameExpression(outputColor, "outputColor");
80 this->nameExpression(outputCoverage, "outputCoverage");
81
csmartdalton936f81b2017-02-13 15:45:35 -070082 SkASSERT(!fUniformHandles.fRTAdjustmentUni.isValid());
Robert Phillipsfe8da172018-01-24 14:52:02 +000083 GrShaderFlags rtAdjustVisibility;
csmartdalton936f81b2017-02-13 15:45:35 -070084 if (proc.willUseGeoShader()) {
Robert Phillipsfe8da172018-01-24 14:52:02 +000085 rtAdjustVisibility = kGeometry_GrShaderFlag;
86 } else {
87 rtAdjustVisibility = kVertex_GrShaderFlag;
csmartdalton936f81b2017-02-13 15:45:35 -070088 }
Robert Phillipsfe8da172018-01-24 14:52:02 +000089 fUniformHandles.fRTAdjustmentUni = this->uniformHandler()->addUniform(
90 rtAdjustVisibility,
91 kFloat4_GrSLType,
92 SkSL::Compiler::RTADJUST_NAME);
csmartdalton936f81b2017-02-13 15:45:35 -070093 const char* rtAdjustName =
94 this->uniformHandler()->getUniformCStr(fUniformHandles.fRTAdjustmentUni);
95
egdanielfa896322016-01-13 12:19:30 -080096 // Enclose custom code in a block to avoid namespace conflicts
97 SkString openBrace;
98 openBrace.printf("{ // Stage %d, %s\n", fStageIndex, proc.name());
99 fFS.codeAppend(openBrace.c_str());
100 fVS.codeAppendf("// Primitive Processor %s\n", proc.name());
101
102 SkASSERT(!fGeometryProcessor);
Robert Phillips369e8b72017-08-01 16:13:04 -0400103 fGeometryProcessor.reset(proc.createGLSLInstance(*this->shaderCaps()));
egdanielfa896322016-01-13 12:19:30 -0800104
Brian Salomone782f842018-07-31 13:53:11 -0400105 SkAutoSTMalloc<4, SamplerHandle> texSamplers(proc.numTextureSamplers());
106 for (int i = 0; i < proc.numTextureSamplers(); ++i) {
107 SkString name;
108 name.printf("TextureSampler_%d", i);
109 const auto& sampler = proc.textureSampler(i);
Greg Daniel9a51a862018-11-30 10:18:14 -0500110 const GrTexture* texture = primProcProxies[i]->peekTexture();
111 SkASSERT(sampler.textureType() == texture->texturePriv().textureType());
Greg Daniel9a51a862018-11-30 10:18:14 -0500112 texSamplers[i] = this->emitSampler(texture,
113 sampler.samplerState(),
Greg Daniel2c3398d2019-06-19 11:58:01 -0400114 sampler.swizzle(),
Greg Daniel9a51a862018-11-30 10:18:14 -0500115 name.c_str());
Brian Salomone782f842018-07-31 13:53:11 -0400116 }
egdanielfa896322016-01-13 12:19:30 -0800117
bsalomona624bf32016-09-20 09:12:47 -0700118 GrGLSLPrimitiveProcessor::FPCoordTransformHandler transformHandler(fPipeline,
119 &fTransformedCoordVars);
egdanielfa896322016-01-13 12:19:30 -0800120 GrGLSLGeometryProcessor::EmitArgs args(&fVS,
csmartdalton276cc412016-11-21 11:55:00 -0700121 proc.willUseGeoShader() ? &fGS : nullptr,
egdanielfa896322016-01-13 12:19:30 -0800122 &fFS,
123 this->varyingHandler(),
124 this->uniformHandler(),
Brian Salomon94efbf52016-11-29 13:43:05 -0500125 this->shaderCaps(),
egdanielfa896322016-01-13 12:19:30 -0800126 proc,
127 outputColor->c_str(),
128 outputCoverage->c_str(),
csmartdalton936f81b2017-02-13 15:45:35 -0700129 rtAdjustName,
Brian Salomone782f842018-07-31 13:53:11 -0400130 texSamplers.get(),
bsalomona624bf32016-09-20 09:12:47 -0700131 &transformHandler);
egdanielfa896322016-01-13 12:19:30 -0800132 fGeometryProcessor->emitCode(args);
133
134 // We have to check that effects and the code they emit are consistent, ie if an effect
135 // asks for dst color, then the emit code needs to follow suit
cdalton87332102016-02-26 12:22:02 -0800136 SkDEBUGCODE(verify(proc);)
egdanielfa896322016-01-13 12:19:30 -0800137
138 fFS.codeAppend("}");
139}
140
Ethan Nicholas2983f402017-05-08 09:36:08 -0400141void GrGLSLProgramBuilder::emitAndInstallFragProcs(SkString* color, SkString* coverage) {
bsalomona624bf32016-09-20 09:12:47 -0700142 int transformedCoordVarsIdx = 0;
Ethan Nicholas2983f402017-05-08 09:36:08 -0400143 SkString** inOut = &color;
Brian Salomon4d3f5172018-06-07 14:42:52 -0400144 SkSTArray<8, std::unique_ptr<GrGLSLFragmentProcessor>> glslFragmentProcessors;
bsalomona624bf32016-09-20 09:12:47 -0700145 for (int i = 0; i < this->pipeline().numFragmentProcessors(); ++i) {
146 if (i == this->pipeline().numColorFragmentProcessors()) {
147 inOut = &coverage;
148 }
Ethan Nicholas2983f402017-05-08 09:36:08 -0400149 SkString output;
egdanielfa896322016-01-13 12:19:30 -0800150 const GrFragmentProcessor& fp = this->pipeline().getFragmentProcessor(i);
Brian Salomon4d3f5172018-06-07 14:42:52 -0400151 output = this->emitAndInstallFragProc(fp, i, transformedCoordVarsIdx, **inOut, output,
152 &glslFragmentProcessors);
bsalomona624bf32016-09-20 09:12:47 -0700153 GrFragmentProcessor::Iter iter(&fp);
154 while (const GrFragmentProcessor* fp = iter.next()) {
155 transformedCoordVarsIdx += fp->numCoordTransforms();
156 }
157 **inOut = output;
egdanielfa896322016-01-13 12:19:30 -0800158 }
Brian Salomon4d3f5172018-06-07 14:42:52 -0400159 fFragmentProcessorCnt = glslFragmentProcessors.count();
160 fFragmentProcessors.reset(new std::unique_ptr<GrGLSLFragmentProcessor>[fFragmentProcessorCnt]);
161 for (int i = 0; i < fFragmentProcessorCnt; ++i) {
162 fFragmentProcessors[i] = std::move(glslFragmentProcessors[i]);
163 }
egdanielfa896322016-01-13 12:19:30 -0800164}
165
166// TODO Processors cannot output zeros because an empty string is all 1s
Ethan Nicholas2983f402017-05-08 09:36:08 -0400167// the fix is to allow effects to take the SkString directly
Brian Salomon4d3f5172018-06-07 14:42:52 -0400168SkString GrGLSLProgramBuilder::emitAndInstallFragProc(
169 const GrFragmentProcessor& fp,
170 int index,
171 int transformedCoordVarsIdx,
172 const SkString& input,
173 SkString output,
174 SkTArray<std::unique_ptr<GrGLSLFragmentProcessor>>* glslFragmentProcessors) {
Ethan Nicholas2983f402017-05-08 09:36:08 -0400175 SkASSERT(input.size());
egdanielfa896322016-01-13 12:19:30 -0800176 // Program builders have a bit of state we need to clear with each effect
177 AutoStageAdvance adv(this);
Ethan Nicholas2983f402017-05-08 09:36:08 -0400178 this->nameExpression(&output, "output");
egdanielfa896322016-01-13 12:19:30 -0800179
180 // Enclose custom code in a block to avoid namespace conflicts
181 SkString openBrace;
182 openBrace.printf("{ // Stage %d, %s\n", fStageIndex, fp.name());
183 fFS.codeAppend(openBrace.c_str());
184
185 GrGLSLFragmentProcessor* fragProc = fp.createGLSLInstance();
186
Brian Salomone782f842018-07-31 13:53:11 -0400187 SkSTArray<4, SamplerHandle> texSamplers;
188 GrFragmentProcessor::Iter fpIter(&fp);
189 int samplerIdx = 0;
190 while (const auto* subFP = fpIter.next()) {
191 for (int i = 0; i < subFP->numTextureSamplers(); ++i) {
192 SkString name;
193 name.printf("TextureSampler_%d", samplerIdx++);
194 const auto& sampler = subFP->textureSampler(i);
Greg Daniel9a51a862018-11-30 10:18:14 -0500195 texSamplers.emplace_back(this->emitSampler(sampler.peekTexture(),
196 sampler.samplerState(),
Greg Daniel2c3398d2019-06-19 11:58:01 -0400197 sampler.swizzle(),
Greg Daniel0f70be82018-10-08 17:35:08 +0000198 name.c_str()));
Brian Salomone782f842018-07-31 13:53:11 -0400199 }
bsalomonb58a2b42016-09-26 06:55:02 -0700200 }
egdanielfa896322016-01-13 12:19:30 -0800201
Ethan Nicholasd4efe682019-08-29 16:10:13 -0400202 const GrGLSLPrimitiveProcessor::TransformVar* coordVars = fTransformedCoordVars.begin() +
203 transformedCoordVarsIdx;
bsalomona624bf32016-09-20 09:12:47 -0700204 GrGLSLFragmentProcessor::TransformedCoordVars coords(&fp, coordVars);
Brian Salomone782f842018-07-31 13:53:11 -0400205 GrGLSLFragmentProcessor::TextureSamplers textureSamplers(&fp, texSamplers.begin());
egdanielfa896322016-01-13 12:19:30 -0800206 GrGLSLFragmentProcessor::EmitArgs args(&fFS,
207 this->uniformHandler(),
Brian Salomon94efbf52016-11-29 13:43:05 -0500208 this->shaderCaps(),
egdanielfa896322016-01-13 12:19:30 -0800209 fp,
Ethan Nicholas2983f402017-05-08 09:36:08 -0400210 output.c_str(),
211 input.c_str(),
bsalomona624bf32016-09-20 09:12:47 -0700212 coords,
Brian Salomon662ea4b2018-07-12 14:53:49 -0400213 textureSamplers);
Ethan Nicholas6ad52892019-05-03 13:13:42 +0000214
215 fragProc->emitCode(args);
egdanielfa896322016-01-13 12:19:30 -0800216
217 // We have to check that effects and the code they emit are consistent, ie if an effect
218 // asks for dst color, then the emit code needs to follow suit
cdalton87332102016-02-26 12:22:02 -0800219 SkDEBUGCODE(verify(fp);)
Brian Salomon4d3f5172018-06-07 14:42:52 -0400220 glslFragmentProcessors->emplace_back(fragProc);
egdanielfa896322016-01-13 12:19:30 -0800221
222 fFS.codeAppend("}");
Ethan Nicholas2983f402017-05-08 09:36:08 -0400223 return output;
egdanielfa896322016-01-13 12:19:30 -0800224}
225
Ethan Nicholas2983f402017-05-08 09:36:08 -0400226void GrGLSLProgramBuilder::emitAndInstallXferProc(const SkString& colorIn,
227 const SkString& coverageIn) {
egdanielfa896322016-01-13 12:19:30 -0800228 // Program builders have a bit of state we need to clear with each effect
229 AutoStageAdvance adv(this);
230
231 SkASSERT(!fXferProcessor);
Brian Salomon18dfa982017-04-03 16:57:43 -0400232 const GrXferProcessor& xp = fPipeline.getXferProcessor();
Robert Phillips369e8b72017-08-01 16:13:04 -0400233 fXferProcessor.reset(xp.createGLSLInstance());
egdanielfa896322016-01-13 12:19:30 -0800234
235 // Enable dual source secondary output if we have one
236 if (xp.hasSecondaryOutput()) {
237 fFS.enableSecondaryOutput();
238 }
239
Brian Salomon94efbf52016-11-29 13:43:05 -0500240 if (this->shaderCaps()->mustDeclareFragmentShaderOutput()) {
egdanielfa896322016-01-13 12:19:30 -0800241 fFS.enableCustomOutput();
242 }
243
244 SkString openBrace;
245 openBrace.printf("{ // Xfer Processor: %s\n", xp.name());
246 fFS.codeAppend(openBrace.c_str());
247
Brian Salomon18dfa982017-04-03 16:57:43 -0400248 SamplerHandle dstTextureSamplerHandle;
249 GrSurfaceOrigin dstTextureOrigin = kTopLeft_GrSurfaceOrigin;
Robert Phillipsbb581ce2017-05-29 15:05:15 -0400250
251 if (GrTexture* dstTexture = fPipeline.peekDstTexture()) {
Brian Salomon18dfa982017-04-03 16:57:43 -0400252 // GrProcessor::TextureSampler sampler(dstTexture);
Greg Daniel2c3398d2019-06-19 11:58:01 -0400253 SkASSERT(fPipeline.dstTextureProxy());
254 const GrSwizzle& swizzle = fPipeline.dstTextureProxy()->textureSwizzle();
Brian Salomon18dfa982017-04-03 16:57:43 -0400255 dstTextureSamplerHandle =
Greg Daniel2c3398d2019-06-19 11:58:01 -0400256 this->emitSampler(dstTexture, GrSamplerState(), swizzle, "DstTextureSampler");
Robert Phillips16d8ec62017-07-27 16:16:25 -0400257 dstTextureOrigin = fPipeline.dstTextureProxy()->origin();
Brian Salomon60dd8c72018-07-30 10:24:13 -0400258 SkASSERT(dstTexture->texturePriv().textureType() != GrTextureType::kExternal);
Brian Salomon18dfa982017-04-03 16:57:43 -0400259 }
egdanielfa896322016-01-13 12:19:30 -0800260
Brian Salomonf19f9ca2019-09-18 15:54:26 -0400261 SkString finalInColor = colorIn.size() ? colorIn : SkString("float4(1)");
Brian Osman65cdd612019-03-14 17:01:57 -0400262
egdanielfa896322016-01-13 12:19:30 -0800263 GrGLSLXferProcessor::EmitArgs args(&fFS,
264 this->uniformHandler(),
Brian Salomon94efbf52016-11-29 13:43:05 -0500265 this->shaderCaps(),
Brian Salomon18dfa982017-04-03 16:57:43 -0400266 xp,
Brian Osman65cdd612019-03-14 17:01:57 -0400267 finalInColor.c_str(),
Ethan Nicholas5af9ea32017-07-28 15:19:46 -0400268 coverageIn.size() ? coverageIn.c_str() : "float4(1)",
egdanielfa896322016-01-13 12:19:30 -0800269 fFS.getPrimaryColorOutputName(),
270 fFS.getSecondaryColorOutputName(),
Brian Salomon18dfa982017-04-03 16:57:43 -0400271 dstTextureSamplerHandle,
Chris Dalton6b76df02019-04-08 11:01:34 -0600272 dstTextureOrigin,
273 this->desc()->header().fOutputSwizzle);
egdanielfa896322016-01-13 12:19:30 -0800274 fXferProcessor->emitCode(args);
275
276 // We have to check that effects and the code they emit are consistent, ie if an effect
277 // asks for dst color, then the emit code needs to follow suit
cdalton87332102016-02-26 12:22:02 -0800278 SkDEBUGCODE(verify(xp);)
egdanielfa896322016-01-13 12:19:30 -0800279 fFS.codeAppend("}");
280}
281
Greg Daniel9a51a862018-11-30 10:18:14 -0500282GrGLSLProgramBuilder::SamplerHandle GrGLSLProgramBuilder::emitSampler(const GrTexture* texture,
283 const GrSamplerState& state,
Greg Daniel2c3398d2019-06-19 11:58:01 -0400284 const GrSwizzle& swizzle,
Greg Daniel0f70be82018-10-08 17:35:08 +0000285 const char* name) {
286 ++fNumFragmentSamplers;
Greg Daniel2c3398d2019-06-19 11:58:01 -0400287 return this->uniformHandler()->addSampler(texture, state, swizzle, name, this->shaderCaps());
Brian Salomonf9f45122016-11-29 11:59:17 -0500288}
289
cdalton9c3f1432016-03-11 10:07:37 -0800290bool GrGLSLProgramBuilder::checkSamplerCounts() {
Brian Salomon1edc5b92016-11-29 13:43:46 -0500291 const GrShaderCaps& shaderCaps = *this->shaderCaps();
Brian Salomon1edc5b92016-11-29 13:43:46 -0500292 if (fNumFragmentSamplers > shaderCaps.maxFragmentSamplers()) {
cdalton9c3f1432016-03-11 10:07:37 -0800293 GrCapsDebugf(this->caps(), "Program would use too many fragment samplers\n");
294 return false;
295 }
cdalton9c3f1432016-03-11 10:07:37 -0800296 return true;
297}
298
cdalton87332102016-02-26 12:22:02 -0800299#ifdef SK_DEBUG
egdanielfa896322016-01-13 12:19:30 -0800300void GrGLSLProgramBuilder::verify(const GrPrimitiveProcessor& gp) {
Chris Daltond7291ba2019-03-07 14:17:03 -0700301 SkASSERT(!fFS.fHasReadDstColorThisStage_DebugOnly);
302 SkASSERT(fFS.fUsedProcessorFeaturesThisStage_DebugOnly == gp.requestedFeatures());
egdanielfa896322016-01-13 12:19:30 -0800303}
304
305void GrGLSLProgramBuilder::verify(const GrFragmentProcessor& fp) {
Chris Daltond7291ba2019-03-07 14:17:03 -0700306 SkASSERT(!fFS.fHasReadDstColorThisStage_DebugOnly);
307 SkASSERT(fFS.fUsedProcessorFeaturesThisStage_DebugOnly == fp.requestedFeatures());
308}
309
310void GrGLSLProgramBuilder::verify(const GrXferProcessor& xp) {
311 SkASSERT(xp.willReadDstColor() == fFS.fHasReadDstColorThisStage_DebugOnly);
312 SkASSERT(fFS.fUsedProcessorFeaturesThisStage_DebugOnly == xp.requestedFeatures());
egdaniel8dcdedc2015-11-11 06:27:20 -0800313}
cdalton87332102016-02-26 12:22:02 -0800314#endif
egdaniel8dcdedc2015-11-11 06:27:20 -0800315
316void GrGLSLProgramBuilder::nameVariable(SkString* out, char prefix, const char* name, bool mangle) {
317 if ('\0' == prefix) {
318 *out = name;
319 } else {
320 out->printf("%c%s", prefix, name);
321 }
322 if (mangle) {
323 if (out->endsWith('_')) {
324 // Names containing "__" are reserved.
325 out->append("x");
326 }
327 out->appendf("_Stage%d%s", fStageIndex, fFS.getMangleString().c_str());
328 }
329}
330
Ethan Nicholas2983f402017-05-08 09:36:08 -0400331void GrGLSLProgramBuilder::nameExpression(SkString* output, const char* baseName) {
egdanielfa896322016-01-13 12:19:30 -0800332 // create var to hold stage result. If we already have a valid output name, just use that
333 // otherwise create a new mangled one. This name is only valid if we are reordering stages
334 // and have to tell stage exactly where to put its output.
335 SkString outName;
Ethan Nicholas2983f402017-05-08 09:36:08 -0400336 if (output->size()) {
egdanielfa896322016-01-13 12:19:30 -0800337 outName = output->c_str();
338 } else {
339 this->nameVariable(&outName, '\0', baseName);
340 }
Ethan Nicholasf7b88202017-09-18 14:10:39 -0400341 fFS.codeAppendf("half4 %s;", outName.c_str());
egdanielfa896322016-01-13 12:19:30 -0800342 *output = outName;
343}
344
cdalton5e58cee2016-02-11 12:49:47 -0800345void GrGLSLProgramBuilder::appendUniformDecls(GrShaderFlags visibility, SkString* out) const {
egdaniel7ea439b2015-12-03 09:20:44 -0800346 this->uniformHandler()->appendUniformDecls(visibility, out);
347}
348
Ethan Nicholascd700e92018-08-24 16:43:57 -0400349void GrGLSLProgramBuilder::addRTWidthUniform(const char* name) {
Brian Osmanf7924452019-09-24 10:44:37 -0400350 SkASSERT(!fUniformHandles.fRTWidthUni.isValid());
351 GrGLSLUniformHandler* uniformHandler = this->uniformHandler();
352 fUniformHandles.fRTWidthUni =
Ethan Nicholas858fecc2019-03-07 13:19:18 -0500353 uniformHandler->internalAddUniformArray(kFragment_GrShaderFlag, kHalf_GrSLType, name,
354 false, 0, nullptr);
Ethan Nicholascd700e92018-08-24 16:43:57 -0400355}
356
Greg Daniele6ab9982018-08-22 13:56:32 +0000357void GrGLSLProgramBuilder::addRTHeightUniform(const char* name) {
Brian Osmanf7924452019-09-24 10:44:37 -0400358 SkASSERT(!fUniformHandles.fRTHeightUni.isValid());
359 GrGLSLUniformHandler* uniformHandler = this->uniformHandler();
360 fUniformHandles.fRTHeightUni =
Ethan Nicholas858fecc2019-03-07 13:19:18 -0500361 uniformHandler->internalAddUniformArray(kFragment_GrShaderFlag, kHalf_GrSLType, name,
362 false, 0, nullptr);
egdaniel8dcdedc2015-11-11 06:27:20 -0800363}
364
egdaniel9f1d4152016-02-10 09:50:38 -0800365void GrGLSLProgramBuilder::finalizeShaders() {
366 this->varyingHandler()->finalize();
cdalton5e58cee2016-02-11 12:49:47 -0800367 fVS.finalize(kVertex_GrShaderFlag);
csmartdalton276cc412016-11-21 11:55:00 -0700368 if (this->primitiveProcessor().willUseGeoShader()) {
Brian Salomon94efbf52016-11-29 13:43:05 -0500369 SkASSERT(this->shaderCaps()->geometryShaderSupport());
csmartdalton276cc412016-11-21 11:55:00 -0700370 fGS.finalize(kGeometry_GrShaderFlag);
371 }
cdalton5e58cee2016-02-11 12:49:47 -0800372 fFS.finalize(kFragment_GrShaderFlag);
egdaniel9f1d4152016-02-10 09:50:38 -0800373}