blob: a427be5c0cfcdc3709acf893b2f212e1e5a14f7d [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,
Stephen Whiteb1857852020-02-07 15:33:23 +000024 const GrProgramDesc& desc,
25 const GrProgramInfo& programInfo)
Brian Salomonff168d92018-06-23 15:17:27 -040026 : fVS(this)
27 , fGS(this)
28 , fFS(this)
29 , fStageIndex(-1)
Chris Daltond7291ba2019-03-07 14:17:03 -070030 , fRenderTarget(renderTarget)
Austin Eng77fdf662020-02-07 01:26:16 +000031 , fDesc(desc)
Stephen Whiteb1857852020-02-07 15:33:23 +000032 , fProgramInfo(programInfo)
Brian Salomonff168d92018-06-23 15:17:27 -040033 , fGeometryProcessor(nullptr)
34 , fXferProcessor(nullptr)
Brian Salomonff168d92018-06-23 15:17:27 -040035 , fNumFragmentSamplers(0) {}
cdalton9c3f1432016-03-11 10:07:37 -080036
37void GrGLSLProgramBuilder::addFeature(GrShaderFlags shaders,
38 uint32_t featureBit,
39 const char* extensionName) {
40 if (shaders & kVertex_GrShaderFlag) {
41 fVS.addFeature(featureBit, extensionName);
42 }
43 if (shaders & kGeometry_GrShaderFlag) {
csmartdalton276cc412016-11-21 11:55:00 -070044 SkASSERT(this->primitiveProcessor().willUseGeoShader());
cdalton9c3f1432016-03-11 10:07:37 -080045 fGS.addFeature(featureBit, extensionName);
46 }
47 if (shaders & kFragment_GrShaderFlag) {
48 fFS.addFeature(featureBit, extensionName);
49 }
egdanielfa896322016-01-13 12:19:30 -080050}
51
Ethan Nicholas2983f402017-05-08 09:36:08 -040052bool GrGLSLProgramBuilder::emitAndInstallProcs() {
egdanielfa896322016-01-13 12:19:30 -080053 // First we loop over all of the installed processors and collect coord transforms. These will
54 // be sent to the GrGLSLPrimitiveProcessor in its emitCode function
Ethan Nicholas2983f402017-05-08 09:36:08 -040055 SkString inputColor;
56 SkString inputCoverage;
Greg Daniel9a51a862018-11-30 10:18:14 -050057 this->emitAndInstallPrimProc(&inputColor, &inputCoverage);
Ethan Nicholas2983f402017-05-08 09:36:08 -040058 this->emitAndInstallFragProcs(&inputColor, &inputCoverage);
59 this->emitAndInstallXferProc(inputColor, inputCoverage);
cdalton9c3f1432016-03-11 10:07:37 -080060
Brian Salomon559f5562017-11-15 14:28:33 -050061 return this->checkSamplerCounts();
egdanielfa896322016-01-13 12:19:30 -080062}
63
Chris Dalton1b1b0d52020-03-03 12:00:59 -070064void GrGLSLProgramBuilder::emitAndInstallPrimProc(SkString* outputColor, SkString* outputCoverage) {
Greg Daniel9a51a862018-11-30 10:18:14 -050065 const GrPrimitiveProcessor& proc = this->primitiveProcessor();
Robert Phillips66ae3042019-10-10 12:57:58 -040066
egdanielfa896322016-01-13 12:19:30 -080067 // Program builders have a bit of state we need to clear with each effect
68 AutoStageAdvance adv(this);
69 this->nameExpression(outputColor, "outputColor");
70 this->nameExpression(outputCoverage, "outputCoverage");
71
csmartdalton936f81b2017-02-13 15:45:35 -070072 SkASSERT(!fUniformHandles.fRTAdjustmentUni.isValid());
Robert Phillipsfe8da172018-01-24 14:52:02 +000073 GrShaderFlags rtAdjustVisibility;
csmartdalton936f81b2017-02-13 15:45:35 -070074 if (proc.willUseGeoShader()) {
Robert Phillipsfe8da172018-01-24 14:52:02 +000075 rtAdjustVisibility = kGeometry_GrShaderFlag;
Chris Dalton5a2f9622019-12-27 14:56:38 -070076 } else if (proc.willUseTessellationShaders()) {
77 rtAdjustVisibility = kTessEvaluation_GrShaderFlag;
Robert Phillipsfe8da172018-01-24 14:52:02 +000078 } else {
79 rtAdjustVisibility = kVertex_GrShaderFlag;
csmartdalton936f81b2017-02-13 15:45:35 -070080 }
Robert Phillipsfe8da172018-01-24 14:52:02 +000081 fUniformHandles.fRTAdjustmentUni = this->uniformHandler()->addUniform(
Chris Dalton5a2f9622019-12-27 14:56:38 -070082 rtAdjustVisibility, kFloat4_GrSLType, SkSL::Compiler::RTADJUST_NAME);
csmartdalton936f81b2017-02-13 15:45:35 -070083 const char* rtAdjustName =
84 this->uniformHandler()->getUniformCStr(fUniformHandles.fRTAdjustmentUni);
85
egdanielfa896322016-01-13 12:19:30 -080086 // Enclose custom code in a block to avoid namespace conflicts
87 SkString openBrace;
88 openBrace.printf("{ // Stage %d, %s\n", fStageIndex, proc.name());
89 fFS.codeAppend(openBrace.c_str());
90 fVS.codeAppendf("// Primitive Processor %s\n", proc.name());
91
92 SkASSERT(!fGeometryProcessor);
Robert Phillips369e8b72017-08-01 16:13:04 -040093 fGeometryProcessor.reset(proc.createGLSLInstance(*this->shaderCaps()));
egdanielfa896322016-01-13 12:19:30 -080094
Brian Salomone782f842018-07-31 13:53:11 -040095 SkAutoSTMalloc<4, SamplerHandle> texSamplers(proc.numTextureSamplers());
96 for (int i = 0; i < proc.numTextureSamplers(); ++i) {
97 SkString name;
98 name.printf("TextureSampler_%d", i);
99 const auto& sampler = proc.textureSampler(i);
Chris Dalton1b1b0d52020-03-03 12:00:59 -0700100 texSamplers[i] = this->emitSampler(proc.textureSampler(i).backendFormat(),
Greg Daniel9a51a862018-11-30 10:18:14 -0500101 sampler.samplerState(),
Greg Daniel2c3398d2019-06-19 11:58:01 -0400102 sampler.swizzle(),
Greg Daniel9a51a862018-11-30 10:18:14 -0500103 name.c_str());
Brian Salomone782f842018-07-31 13:53:11 -0400104 }
egdanielfa896322016-01-13 12:19:30 -0800105
Robert Phillips901aff02019-10-08 12:32:56 -0400106 GrGLSLPrimitiveProcessor::FPCoordTransformHandler transformHandler(this->pipeline(),
bsalomona624bf32016-09-20 09:12:47 -0700107 &fTransformedCoordVars);
egdanielfa896322016-01-13 12:19:30 -0800108 GrGLSLGeometryProcessor::EmitArgs args(&fVS,
csmartdalton276cc412016-11-21 11:55:00 -0700109 proc.willUseGeoShader() ? &fGS : nullptr,
egdanielfa896322016-01-13 12:19:30 -0800110 &fFS,
111 this->varyingHandler(),
112 this->uniformHandler(),
Brian Salomon94efbf52016-11-29 13:43:05 -0500113 this->shaderCaps(),
egdanielfa896322016-01-13 12:19:30 -0800114 proc,
115 outputColor->c_str(),
116 outputCoverage->c_str(),
csmartdalton936f81b2017-02-13 15:45:35 -0700117 rtAdjustName,
Brian Salomone782f842018-07-31 13:53:11 -0400118 texSamplers.get(),
bsalomona624bf32016-09-20 09:12:47 -0700119 &transformHandler);
egdanielfa896322016-01-13 12:19:30 -0800120 fGeometryProcessor->emitCode(args);
121
122 // We have to check that effects and the code they emit are consistent, ie if an effect
123 // asks for dst color, then the emit code needs to follow suit
cdalton87332102016-02-26 12:22:02 -0800124 SkDEBUGCODE(verify(proc);)
egdanielfa896322016-01-13 12:19:30 -0800125
126 fFS.codeAppend("}");
127}
128
Ethan Nicholas2983f402017-05-08 09:36:08 -0400129void GrGLSLProgramBuilder::emitAndInstallFragProcs(SkString* color, SkString* coverage) {
bsalomona624bf32016-09-20 09:12:47 -0700130 int transformedCoordVarsIdx = 0;
Ethan Nicholas2983f402017-05-08 09:36:08 -0400131 SkString** inOut = &color;
Brian Salomon4d3f5172018-06-07 14:42:52 -0400132 SkSTArray<8, std::unique_ptr<GrGLSLFragmentProcessor>> glslFragmentProcessors;
bsalomona624bf32016-09-20 09:12:47 -0700133 for (int i = 0; i < this->pipeline().numFragmentProcessors(); ++i) {
134 if (i == this->pipeline().numColorFragmentProcessors()) {
135 inOut = &coverage;
136 }
Ethan Nicholas2983f402017-05-08 09:36:08 -0400137 SkString output;
egdanielfa896322016-01-13 12:19:30 -0800138 const GrFragmentProcessor& fp = this->pipeline().getFragmentProcessor(i);
Brian Salomon4d3f5172018-06-07 14:42:52 -0400139 output = this->emitAndInstallFragProc(fp, i, transformedCoordVarsIdx, **inOut, output,
140 &glslFragmentProcessors);
Brian Salomon7eabfe82019-12-02 14:20:20 -0500141 for (const auto& subFP : GrFragmentProcessor::FPCRange(fp)) {
Brian Salomonc241b582019-11-27 08:57:17 -0500142 transformedCoordVarsIdx += subFP.numCoordTransforms();
bsalomona624bf32016-09-20 09:12:47 -0700143 }
144 **inOut = output;
egdanielfa896322016-01-13 12:19:30 -0800145 }
Brian Salomon4d3f5172018-06-07 14:42:52 -0400146 fFragmentProcessorCnt = glslFragmentProcessors.count();
147 fFragmentProcessors.reset(new std::unique_ptr<GrGLSLFragmentProcessor>[fFragmentProcessorCnt]);
148 for (int i = 0; i < fFragmentProcessorCnt; ++i) {
149 fFragmentProcessors[i] = std::move(glslFragmentProcessors[i]);
150 }
egdanielfa896322016-01-13 12:19:30 -0800151}
152
153// TODO Processors cannot output zeros because an empty string is all 1s
Ethan Nicholas2983f402017-05-08 09:36:08 -0400154// the fix is to allow effects to take the SkString directly
Brian Salomon4d3f5172018-06-07 14:42:52 -0400155SkString GrGLSLProgramBuilder::emitAndInstallFragProc(
156 const GrFragmentProcessor& fp,
157 int index,
158 int transformedCoordVarsIdx,
159 const SkString& input,
160 SkString output,
161 SkTArray<std::unique_ptr<GrGLSLFragmentProcessor>>* glslFragmentProcessors) {
Ethan Nicholas2983f402017-05-08 09:36:08 -0400162 SkASSERT(input.size());
egdanielfa896322016-01-13 12:19:30 -0800163 // Program builders have a bit of state we need to clear with each effect
164 AutoStageAdvance adv(this);
Ethan Nicholas2983f402017-05-08 09:36:08 -0400165 this->nameExpression(&output, "output");
egdanielfa896322016-01-13 12:19:30 -0800166
167 // Enclose custom code in a block to avoid namespace conflicts
168 SkString openBrace;
169 openBrace.printf("{ // Stage %d, %s\n", fStageIndex, fp.name());
170 fFS.codeAppend(openBrace.c_str());
171
172 GrGLSLFragmentProcessor* fragProc = fp.createGLSLInstance();
173
Brian Salomone782f842018-07-31 13:53:11 -0400174 SkSTArray<4, SamplerHandle> texSamplers;
Brian Salomone782f842018-07-31 13:53:11 -0400175 int samplerIdx = 0;
Brian Salomon7eabfe82019-12-02 14:20:20 -0500176 for (const auto& subFP : GrFragmentProcessor::FPCRange(fp)) {
Brian Salomonc241b582019-11-27 08:57:17 -0500177 for (int i = 0; i < subFP.numTextureSamplers(); ++i) {
Brian Salomone782f842018-07-31 13:53:11 -0400178 SkString name;
179 name.printf("TextureSampler_%d", samplerIdx++);
Brian Salomonc241b582019-11-27 08:57:17 -0500180 const auto& sampler = subFP.textureSampler(i);
Chris Dalton1b1b0d52020-03-03 12:00:59 -0700181 texSamplers.emplace_back(this->emitSampler(sampler.view().proxy()->backendFormat(),
Greg Daniel9a51a862018-11-30 10:18:14 -0500182 sampler.samplerState(),
Robert Phillipsbd99c0c2019-12-12 13:26:58 +0000183 sampler.view().swizzle(),
Greg Daniel0f70be82018-10-08 17:35:08 +0000184 name.c_str()));
Brian Salomone782f842018-07-31 13:53:11 -0400185 }
bsalomonb58a2b42016-09-26 06:55:02 -0700186 }
Ethan Nicholasd4efe682019-08-29 16:10:13 -0400187 const GrGLSLPrimitiveProcessor::TransformVar* coordVars = fTransformedCoordVars.begin() +
188 transformedCoordVarsIdx;
bsalomona624bf32016-09-20 09:12:47 -0700189 GrGLSLFragmentProcessor::TransformedCoordVars coords(&fp, coordVars);
Brian Salomone782f842018-07-31 13:53:11 -0400190 GrGLSLFragmentProcessor::TextureSamplers textureSamplers(&fp, texSamplers.begin());
egdanielfa896322016-01-13 12:19:30 -0800191 GrGLSLFragmentProcessor::EmitArgs args(&fFS,
192 this->uniformHandler(),
Brian Salomon94efbf52016-11-29 13:43:05 -0500193 this->shaderCaps(),
egdanielfa896322016-01-13 12:19:30 -0800194 fp,
Ethan Nicholas2983f402017-05-08 09:36:08 -0400195 output.c_str(),
196 input.c_str(),
bsalomona624bf32016-09-20 09:12:47 -0700197 coords,
Brian Salomon662ea4b2018-07-12 14:53:49 -0400198 textureSamplers);
Ethan Nicholas6ad52892019-05-03 13:13:42 +0000199
200 fragProc->emitCode(args);
egdanielfa896322016-01-13 12:19:30 -0800201
202 // We have to check that effects and the code they emit are consistent, ie if an effect
203 // asks for dst color, then the emit code needs to follow suit
cdalton87332102016-02-26 12:22:02 -0800204 SkDEBUGCODE(verify(fp);)
Brian Salomon4d3f5172018-06-07 14:42:52 -0400205 glslFragmentProcessors->emplace_back(fragProc);
egdanielfa896322016-01-13 12:19:30 -0800206
207 fFS.codeAppend("}");
Ethan Nicholas2983f402017-05-08 09:36:08 -0400208 return output;
egdanielfa896322016-01-13 12:19:30 -0800209}
210
Ethan Nicholas2983f402017-05-08 09:36:08 -0400211void GrGLSLProgramBuilder::emitAndInstallXferProc(const SkString& colorIn,
212 const SkString& coverageIn) {
egdanielfa896322016-01-13 12:19:30 -0800213 // Program builders have a bit of state we need to clear with each effect
214 AutoStageAdvance adv(this);
215
216 SkASSERT(!fXferProcessor);
Robert Phillips901aff02019-10-08 12:32:56 -0400217 const GrXferProcessor& xp = this->pipeline().getXferProcessor();
Robert Phillips369e8b72017-08-01 16:13:04 -0400218 fXferProcessor.reset(xp.createGLSLInstance());
egdanielfa896322016-01-13 12:19:30 -0800219
220 // Enable dual source secondary output if we have one
221 if (xp.hasSecondaryOutput()) {
222 fFS.enableSecondaryOutput();
223 }
224
Brian Salomon94efbf52016-11-29 13:43:05 -0500225 if (this->shaderCaps()->mustDeclareFragmentShaderOutput()) {
egdanielfa896322016-01-13 12:19:30 -0800226 fFS.enableCustomOutput();
227 }
228
229 SkString openBrace;
230 openBrace.printf("{ // Xfer Processor: %s\n", xp.name());
231 fFS.codeAppend(openBrace.c_str());
232
Brian Salomon18dfa982017-04-03 16:57:43 -0400233 SamplerHandle dstTextureSamplerHandle;
234 GrSurfaceOrigin dstTextureOrigin = kTopLeft_GrSurfaceOrigin;
Robert Phillipsbb581ce2017-05-29 15:05:15 -0400235
Greg Daniel524e28b2019-11-01 11:48:53 -0400236 const GrSurfaceProxyView& dstView = this->pipeline().dstProxyView();
237 if (GrTextureProxy* dstTextureProxy = dstView.asTextureProxy()) {
Brian Salomon18dfa982017-04-03 16:57:43 -0400238 // GrProcessor::TextureSampler sampler(dstTexture);
Greg Daniel524e28b2019-11-01 11:48:53 -0400239 const GrSwizzle& swizzle = dstView.swizzle();
Chris Dalton1b1b0d52020-03-03 12:00:59 -0700240 dstTextureSamplerHandle = this->emitSampler(dstTextureProxy->backendFormat(),
241 GrSamplerState(), swizzle, "DstTextureSampler");
Greg Daniel524e28b2019-11-01 11:48:53 -0400242 dstTextureOrigin = dstView.origin();
Robert Phillips66ae3042019-10-10 12:57:58 -0400243 SkASSERT(dstTextureProxy->textureType() != GrTextureType::kExternal);
Brian Salomon18dfa982017-04-03 16:57:43 -0400244 }
egdanielfa896322016-01-13 12:19:30 -0800245
Brian Salomonf19f9ca2019-09-18 15:54:26 -0400246 SkString finalInColor = colorIn.size() ? colorIn : SkString("float4(1)");
Brian Osman65cdd612019-03-14 17:01:57 -0400247
egdanielfa896322016-01-13 12:19:30 -0800248 GrGLSLXferProcessor::EmitArgs args(&fFS,
249 this->uniformHandler(),
Brian Salomon94efbf52016-11-29 13:43:05 -0500250 this->shaderCaps(),
Brian Salomon18dfa982017-04-03 16:57:43 -0400251 xp,
Brian Osman65cdd612019-03-14 17:01:57 -0400252 finalInColor.c_str(),
Ethan Nicholas5af9ea32017-07-28 15:19:46 -0400253 coverageIn.size() ? coverageIn.c_str() : "float4(1)",
egdanielfa896322016-01-13 12:19:30 -0800254 fFS.getPrimaryColorOutputName(),
255 fFS.getSecondaryColorOutputName(),
Brian Salomon18dfa982017-04-03 16:57:43 -0400256 dstTextureSamplerHandle,
Chris Dalton6b76df02019-04-08 11:01:34 -0600257 dstTextureOrigin,
Robert Phillipsd5c1f342019-10-14 09:50:05 -0400258 this->pipeline().outputSwizzle());
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
Chris Dalton1b1b0d52020-03-03 12:00:59 -0700267GrGLSLProgramBuilder::SamplerHandle GrGLSLProgramBuilder::emitSampler(
268 const GrBackendFormat& backendFormat, GrSamplerState state, const GrSwizzle& swizzle,
269 const char* name) {
Greg Daniel0f70be82018-10-08 17:35:08 +0000270 ++fNumFragmentSamplers;
Chris Dalton1b1b0d52020-03-03 12:00:59 -0700271 return this->uniformHandler()->addSampler(backendFormat, state, swizzle, name,
272 this->shaderCaps());
Brian Salomonf9f45122016-11-29 11:59:17 -0500273}
274
cdalton9c3f1432016-03-11 10:07:37 -0800275bool GrGLSLProgramBuilder::checkSamplerCounts() {
Brian Salomon1edc5b92016-11-29 13:43:46 -0500276 const GrShaderCaps& shaderCaps = *this->shaderCaps();
Brian Salomon1edc5b92016-11-29 13:43:46 -0500277 if (fNumFragmentSamplers > shaderCaps.maxFragmentSamplers()) {
cdalton9c3f1432016-03-11 10:07:37 -0800278 GrCapsDebugf(this->caps(), "Program would use too many fragment samplers\n");
279 return false;
280 }
cdalton9c3f1432016-03-11 10:07:37 -0800281 return true;
282}
283
cdalton87332102016-02-26 12:22:02 -0800284#ifdef SK_DEBUG
egdanielfa896322016-01-13 12:19:30 -0800285void GrGLSLProgramBuilder::verify(const GrPrimitiveProcessor& gp) {
Chris Daltond7291ba2019-03-07 14:17:03 -0700286 SkASSERT(!fFS.fHasReadDstColorThisStage_DebugOnly);
287 SkASSERT(fFS.fUsedProcessorFeaturesThisStage_DebugOnly == gp.requestedFeatures());
egdanielfa896322016-01-13 12:19:30 -0800288}
289
290void GrGLSLProgramBuilder::verify(const GrFragmentProcessor& fp) {
Chris Daltond7291ba2019-03-07 14:17:03 -0700291 SkASSERT(!fFS.fHasReadDstColorThisStage_DebugOnly);
292 SkASSERT(fFS.fUsedProcessorFeaturesThisStage_DebugOnly == fp.requestedFeatures());
293}
294
295void GrGLSLProgramBuilder::verify(const GrXferProcessor& xp) {
296 SkASSERT(xp.willReadDstColor() == fFS.fHasReadDstColorThisStage_DebugOnly);
297 SkASSERT(fFS.fUsedProcessorFeaturesThisStage_DebugOnly == xp.requestedFeatures());
egdaniel8dcdedc2015-11-11 06:27:20 -0800298}
cdalton87332102016-02-26 12:22:02 -0800299#endif
egdaniel8dcdedc2015-11-11 06:27:20 -0800300
301void GrGLSLProgramBuilder::nameVariable(SkString* out, char prefix, const char* name, bool mangle) {
302 if ('\0' == prefix) {
303 *out = name;
304 } else {
305 out->printf("%c%s", prefix, name);
306 }
307 if (mangle) {
308 if (out->endsWith('_')) {
309 // Names containing "__" are reserved.
310 out->append("x");
311 }
312 out->appendf("_Stage%d%s", fStageIndex, fFS.getMangleString().c_str());
313 }
314}
315
Ethan Nicholas2983f402017-05-08 09:36:08 -0400316void GrGLSLProgramBuilder::nameExpression(SkString* output, const char* baseName) {
egdanielfa896322016-01-13 12:19:30 -0800317 // create var to hold stage result. If we already have a valid output name, just use that
318 // otherwise create a new mangled one. This name is only valid if we are reordering stages
319 // and have to tell stage exactly where to put its output.
320 SkString outName;
Ethan Nicholas2983f402017-05-08 09:36:08 -0400321 if (output->size()) {
egdanielfa896322016-01-13 12:19:30 -0800322 outName = output->c_str();
323 } else {
324 this->nameVariable(&outName, '\0', baseName);
325 }
Ethan Nicholasf7b88202017-09-18 14:10:39 -0400326 fFS.codeAppendf("half4 %s;", outName.c_str());
egdanielfa896322016-01-13 12:19:30 -0800327 *output = outName;
328}
329
cdalton5e58cee2016-02-11 12:49:47 -0800330void GrGLSLProgramBuilder::appendUniformDecls(GrShaderFlags visibility, SkString* out) const {
egdaniel7ea439b2015-12-03 09:20:44 -0800331 this->uniformHandler()->appendUniformDecls(visibility, out);
332}
333
Ethan Nicholascd700e92018-08-24 16:43:57 -0400334void GrGLSLProgramBuilder::addRTWidthUniform(const char* name) {
Brian Osmanf7924452019-09-24 10:44:37 -0400335 SkASSERT(!fUniformHandles.fRTWidthUni.isValid());
336 GrGLSLUniformHandler* uniformHandler = this->uniformHandler();
337 fUniformHandles.fRTWidthUni =
Ethan Nicholas858fecc2019-03-07 13:19:18 -0500338 uniformHandler->internalAddUniformArray(kFragment_GrShaderFlag, kHalf_GrSLType, name,
339 false, 0, nullptr);
Ethan Nicholascd700e92018-08-24 16:43:57 -0400340}
341
Greg Daniele6ab9982018-08-22 13:56:32 +0000342void GrGLSLProgramBuilder::addRTHeightUniform(const char* name) {
Brian Osmanf7924452019-09-24 10:44:37 -0400343 SkASSERT(!fUniformHandles.fRTHeightUni.isValid());
344 GrGLSLUniformHandler* uniformHandler = this->uniformHandler();
345 fUniformHandles.fRTHeightUni =
Ethan Nicholas858fecc2019-03-07 13:19:18 -0500346 uniformHandler->internalAddUniformArray(kFragment_GrShaderFlag, kHalf_GrSLType, name,
347 false, 0, nullptr);
egdaniel8dcdedc2015-11-11 06:27:20 -0800348}
349
egdaniel9f1d4152016-02-10 09:50:38 -0800350void GrGLSLProgramBuilder::finalizeShaders() {
351 this->varyingHandler()->finalize();
cdalton5e58cee2016-02-11 12:49:47 -0800352 fVS.finalize(kVertex_GrShaderFlag);
csmartdalton276cc412016-11-21 11:55:00 -0700353 if (this->primitiveProcessor().willUseGeoShader()) {
Brian Salomon94efbf52016-11-29 13:43:05 -0500354 SkASSERT(this->shaderCaps()->geometryShaderSupport());
csmartdalton276cc412016-11-21 11:55:00 -0700355 fGS.finalize(kGeometry_GrShaderFlag);
356 }
cdalton5e58cee2016-02-11 12:49:47 -0800357 fFS.finalize(kFragment_GrShaderFlag);
egdaniel9f1d4152016-02-10 09:50:38 -0800358}