blob: cae1c2ad65e3678573907576191b47a2dfb67744 [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
egdaniel0e1853c2016-03-17 11:35:45 -070022GrGLSLProgramBuilder::GrGLSLProgramBuilder(const GrPipeline& pipeline,
23 const GrPrimitiveProcessor& primProc,
Ethan Nicholas38657112017-02-09 17:01:22 -050024 GrProgramDesc* desc)
egdaniel8dcdedc2015-11-11 06:27:20 -080025 : fVS(this)
26 , fGS(this)
cdalton28f45b92016-03-07 13:58:26 -080027 , fFS(this)
egdaniel8dcdedc2015-11-11 06:27:20 -080028 , fStageIndex(-1)
egdaniel0e1853c2016-03-17 11:35:45 -070029 , fPipeline(pipeline)
30 , fPrimProc(primProc)
31 , fDesc(desc)
egdanielfa896322016-01-13 12:19:30 -080032 , fGeometryProcessor(nullptr)
cdalton9c3f1432016-03-11 10:07:37 -080033 , fXferProcessor(nullptr)
cdalton9c3f1432016-03-11 10:07:37 -080034 , fNumVertexSamplers(0)
35 , fNumGeometrySamplers(0)
Brian Salomon559f5562017-11-15 14:28:33 -050036 , fNumFragmentSamplers(0) {
cdalton9c3f1432016-03-11 10:07:37 -080037}
38
39void GrGLSLProgramBuilder::addFeature(GrShaderFlags shaders,
40 uint32_t featureBit,
41 const char* extensionName) {
42 if (shaders & kVertex_GrShaderFlag) {
43 fVS.addFeature(featureBit, extensionName);
44 }
45 if (shaders & kGeometry_GrShaderFlag) {
csmartdalton276cc412016-11-21 11:55:00 -070046 SkASSERT(this->primitiveProcessor().willUseGeoShader());
cdalton9c3f1432016-03-11 10:07:37 -080047 fGS.addFeature(featureBit, extensionName);
48 }
49 if (shaders & kFragment_GrShaderFlag) {
50 fFS.addFeature(featureBit, extensionName);
51 }
egdanielfa896322016-01-13 12:19:30 -080052}
53
Ethan Nicholas2983f402017-05-08 09:36:08 -040054bool GrGLSLProgramBuilder::emitAndInstallProcs() {
egdanielfa896322016-01-13 12:19:30 -080055 // First we loop over all of the installed processors and collect coord transforms. These will
56 // be sent to the GrGLSLPrimitiveProcessor in its emitCode function
57 const GrPrimitiveProcessor& primProc = this->primitiveProcessor();
egdanielfa896322016-01-13 12:19:30 -080058
Ethan Nicholas2983f402017-05-08 09:36:08 -040059 SkString inputColor;
60 SkString inputCoverage;
61 this->emitAndInstallPrimProc(primProc, &inputColor, &inputCoverage);
62 this->emitAndInstallFragProcs(&inputColor, &inputCoverage);
63 this->emitAndInstallXferProc(inputColor, inputCoverage);
Brian Salomon42c456f2017-03-06 11:29:48 -050064 this->emitFSOutputSwizzle(this->pipeline().getXferProcessor().hasSecondaryOutput());
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
69void GrGLSLProgramBuilder::emitAndInstallPrimProc(const GrPrimitiveProcessor& proc,
Ethan Nicholas2983f402017-05-08 09:36:08 -040070 SkString* outputColor,
71 SkString* outputCoverage) {
egdanielfa896322016-01-13 12:19:30 -080072 // Program builders have a bit of state we need to clear with each effect
73 AutoStageAdvance adv(this);
74 this->nameExpression(outputColor, "outputColor");
75 this->nameExpression(outputCoverage, "outputCoverage");
76
csmartdalton936f81b2017-02-13 15:45:35 -070077 SkASSERT(!fUniformHandles.fRTAdjustmentUni.isValid());
Robert Phillipsfe8da172018-01-24 14:52:02 +000078 GrShaderFlags rtAdjustVisibility;
csmartdalton936f81b2017-02-13 15:45:35 -070079 if (proc.willUseGeoShader()) {
Robert Phillipsfe8da172018-01-24 14:52:02 +000080 rtAdjustVisibility = kGeometry_GrShaderFlag;
81 } else {
82 rtAdjustVisibility = kVertex_GrShaderFlag;
csmartdalton936f81b2017-02-13 15:45:35 -070083 }
Robert Phillipsfe8da172018-01-24 14:52:02 +000084 fUniformHandles.fRTAdjustmentUni = this->uniformHandler()->addUniform(
85 rtAdjustVisibility,
86 kFloat4_GrSLType,
87 SkSL::Compiler::RTADJUST_NAME);
csmartdalton936f81b2017-02-13 15:45:35 -070088 const char* rtAdjustName =
89 this->uniformHandler()->getUniformCStr(fUniformHandles.fRTAdjustmentUni);
90
egdanielfa896322016-01-13 12:19:30 -080091 // Enclose custom code in a block to avoid namespace conflicts
92 SkString openBrace;
93 openBrace.printf("{ // Stage %d, %s\n", fStageIndex, proc.name());
94 fFS.codeAppend(openBrace.c_str());
95 fVS.codeAppendf("// Primitive Processor %s\n", proc.name());
96
97 SkASSERT(!fGeometryProcessor);
Robert Phillips369e8b72017-08-01 16:13:04 -040098 fGeometryProcessor.reset(proc.createGLSLInstance(*this->shaderCaps()));
egdanielfa896322016-01-13 12:19:30 -080099
Brian Salomonf9f45122016-11-29 11:59:17 -0500100 SkSTArray<4, SamplerHandle> texSamplers(proc.numTextureSamplers());
Greg Danielbc5d4d72017-05-05 10:28:42 -0400101 SkSTArray<2, TexelBufferHandle> texelBuffers(proc.numBuffers());
Brian Salomon559f5562017-11-15 14:28:33 -0500102 this->emitSamplers(proc, &texSamplers, &texelBuffers);
egdanielfa896322016-01-13 12:19:30 -0800103
bsalomona624bf32016-09-20 09:12:47 -0700104 GrGLSLPrimitiveProcessor::FPCoordTransformHandler transformHandler(fPipeline,
105 &fTransformedCoordVars);
egdanielfa896322016-01-13 12:19:30 -0800106 GrGLSLGeometryProcessor::EmitArgs args(&fVS,
csmartdalton276cc412016-11-21 11:55:00 -0700107 proc.willUseGeoShader() ? &fGS : nullptr,
egdanielfa896322016-01-13 12:19:30 -0800108 &fFS,
109 this->varyingHandler(),
110 this->uniformHandler(),
Brian Salomon94efbf52016-11-29 13:43:05 -0500111 this->shaderCaps(),
egdanielfa896322016-01-13 12:19:30 -0800112 proc,
113 outputColor->c_str(),
114 outputCoverage->c_str(),
csmartdalton936f81b2017-02-13 15:45:35 -0700115 rtAdjustName,
egdaniel09aa1fc2016-04-20 07:09:46 -0700116 texSamplers.begin(),
Greg Danielbc5d4d72017-05-05 10:28:42 -0400117 texelBuffers.begin(),
bsalomona624bf32016-09-20 09:12:47 -0700118 &transformHandler);
egdanielfa896322016-01-13 12:19:30 -0800119 fGeometryProcessor->emitCode(args);
120
121 // We have to check that effects and the code they emit are consistent, ie if an effect
122 // asks for dst color, then the emit code needs to follow suit
cdalton87332102016-02-26 12:22:02 -0800123 SkDEBUGCODE(verify(proc);)
egdanielfa896322016-01-13 12:19:30 -0800124
125 fFS.codeAppend("}");
126}
127
Ethan Nicholas2983f402017-05-08 09:36:08 -0400128void GrGLSLProgramBuilder::emitAndInstallFragProcs(SkString* color, SkString* coverage) {
bsalomona624bf32016-09-20 09:12:47 -0700129 int transformedCoordVarsIdx = 0;
Ethan Nicholas2983f402017-05-08 09:36:08 -0400130 SkString** inOut = &color;
Brian Salomon4d3f5172018-06-07 14:42:52 -0400131 SkSTArray<8, std::unique_ptr<GrGLSLFragmentProcessor>> glslFragmentProcessors;
bsalomona624bf32016-09-20 09:12:47 -0700132 for (int i = 0; i < this->pipeline().numFragmentProcessors(); ++i) {
133 if (i == this->pipeline().numColorFragmentProcessors()) {
134 inOut = &coverage;
135 }
Ethan Nicholas2983f402017-05-08 09:36:08 -0400136 SkString output;
egdanielfa896322016-01-13 12:19:30 -0800137 const GrFragmentProcessor& fp = this->pipeline().getFragmentProcessor(i);
Brian Salomon4d3f5172018-06-07 14:42:52 -0400138 output = this->emitAndInstallFragProc(fp, i, transformedCoordVarsIdx, **inOut, output,
139 &glslFragmentProcessors);
bsalomona624bf32016-09-20 09:12:47 -0700140 GrFragmentProcessor::Iter iter(&fp);
141 while (const GrFragmentProcessor* fp = iter.next()) {
142 transformedCoordVarsIdx += fp->numCoordTransforms();
143 }
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 Salomon0bbecb22016-11-17 11:38:22 -0500174 SkSTArray<4, SamplerHandle> textureSamplerArray(fp.numTextureSamplers());
Greg Danielbc5d4d72017-05-05 10:28:42 -0400175 SkSTArray<2, TexelBufferHandle> texelBufferArray(fp.numBuffers());
bsalomonb58a2b42016-09-26 06:55:02 -0700176 GrFragmentProcessor::Iter iter(&fp);
177 while (const GrFragmentProcessor* subFP = iter.next()) {
Brian Salomon559f5562017-11-15 14:28:33 -0500178 this->emitSamplers(*subFP, &textureSamplerArray, &texelBufferArray);
bsalomonb58a2b42016-09-26 06:55:02 -0700179 }
egdanielfa896322016-01-13 12:19:30 -0800180
bsalomona624bf32016-09-20 09:12:47 -0700181 const GrShaderVar* coordVars = fTransformedCoordVars.begin() + transformedCoordVarsIdx;
182 GrGLSLFragmentProcessor::TransformedCoordVars coords(&fp, coordVars);
bsalomonb58a2b42016-09-26 06:55:02 -0700183 GrGLSLFragmentProcessor::TextureSamplers textureSamplers(&fp, textureSamplerArray.begin());
Greg Danielbc5d4d72017-05-05 10:28:42 -0400184 GrGLSLFragmentProcessor::TexelBuffers texelBuffers(&fp, texelBufferArray.begin());
egdanielfa896322016-01-13 12:19:30 -0800185 GrGLSLFragmentProcessor::EmitArgs args(&fFS,
186 this->uniformHandler(),
Brian Salomon94efbf52016-11-29 13:43:05 -0500187 this->shaderCaps(),
egdanielfa896322016-01-13 12:19:30 -0800188 fp,
Ethan Nicholas2983f402017-05-08 09:36:08 -0400189 output.c_str(),
190 input.c_str(),
bsalomona624bf32016-09-20 09:12:47 -0700191 coords,
bsalomonb58a2b42016-09-26 06:55:02 -0700192 textureSamplers,
Brian Salomon559f5562017-11-15 14:28:33 -0500193 texelBuffers);
dvonbeck9b03e7b2016-08-01 11:01:56 -0700194
egdanielfa896322016-01-13 12:19:30 -0800195 fragProc->emitCode(args);
196
197 // We have to check that effects and the code they emit are consistent, ie if an effect
198 // asks for dst color, then the emit code needs to follow suit
cdalton87332102016-02-26 12:22:02 -0800199 SkDEBUGCODE(verify(fp);)
Brian Salomon4d3f5172018-06-07 14:42:52 -0400200 glslFragmentProcessors->emplace_back(fragProc);
egdanielfa896322016-01-13 12:19:30 -0800201
202 fFS.codeAppend("}");
Ethan Nicholas2983f402017-05-08 09:36:08 -0400203 return output;
egdanielfa896322016-01-13 12:19:30 -0800204}
205
Ethan Nicholas2983f402017-05-08 09:36:08 -0400206void GrGLSLProgramBuilder::emitAndInstallXferProc(const SkString& colorIn,
207 const SkString& coverageIn) {
egdanielfa896322016-01-13 12:19:30 -0800208 // Program builders have a bit of state we need to clear with each effect
209 AutoStageAdvance adv(this);
210
211 SkASSERT(!fXferProcessor);
Brian Salomon18dfa982017-04-03 16:57:43 -0400212 const GrXferProcessor& xp = fPipeline.getXferProcessor();
Robert Phillips369e8b72017-08-01 16:13:04 -0400213 fXferProcessor.reset(xp.createGLSLInstance());
egdanielfa896322016-01-13 12:19:30 -0800214
215 // Enable dual source secondary output if we have one
216 if (xp.hasSecondaryOutput()) {
217 fFS.enableSecondaryOutput();
218 }
219
Brian Salomon94efbf52016-11-29 13:43:05 -0500220 if (this->shaderCaps()->mustDeclareFragmentShaderOutput()) {
egdanielfa896322016-01-13 12:19:30 -0800221 fFS.enableCustomOutput();
222 }
223
224 SkString openBrace;
225 openBrace.printf("{ // Xfer Processor: %s\n", xp.name());
226 fFS.codeAppend(openBrace.c_str());
227
Brian Salomon18dfa982017-04-03 16:57:43 -0400228 SamplerHandle dstTextureSamplerHandle;
229 GrSurfaceOrigin dstTextureOrigin = kTopLeft_GrSurfaceOrigin;
Robert Phillipsbb581ce2017-05-29 15:05:15 -0400230
231 if (GrTexture* dstTexture = fPipeline.peekDstTexture()) {
Brian Salomon18dfa982017-04-03 16:57:43 -0400232 // GrProcessor::TextureSampler sampler(dstTexture);
233 SkString name("DstTextureSampler");
234 dstTextureSamplerHandle =
235 this->emitSampler(dstTexture->texturePriv().samplerType(), dstTexture->config(),
236 "DstTextureSampler", kFragment_GrShaderFlag);
Robert Phillips16d8ec62017-07-27 16:16:25 -0400237 dstTextureOrigin = fPipeline.dstTextureProxy()->origin();
Brian Salomon18dfa982017-04-03 16:57:43 -0400238 SkASSERT(kTextureExternalSampler_GrSLType != dstTexture->texturePriv().samplerType());
239 }
egdanielfa896322016-01-13 12:19:30 -0800240
241 GrGLSLXferProcessor::EmitArgs args(&fFS,
242 this->uniformHandler(),
Brian Salomon94efbf52016-11-29 13:43:05 -0500243 this->shaderCaps(),
Brian Salomon18dfa982017-04-03 16:57:43 -0400244 xp,
Ethan Nicholas5af9ea32017-07-28 15:19:46 -0400245 colorIn.size() ? colorIn.c_str() : "float4(1)",
246 coverageIn.size() ? coverageIn.c_str() : "float4(1)",
egdanielfa896322016-01-13 12:19:30 -0800247 fFS.getPrimaryColorOutputName(),
248 fFS.getSecondaryColorOutputName(),
Brian Salomon18dfa982017-04-03 16:57:43 -0400249 dstTextureSamplerHandle,
250 dstTextureOrigin);
egdanielfa896322016-01-13 12:19:30 -0800251 fXferProcessor->emitCode(args);
252
253 // We have to check that effects and the code they emit are consistent, ie if an effect
254 // asks for dst color, then the emit code needs to follow suit
cdalton87332102016-02-26 12:22:02 -0800255 SkDEBUGCODE(verify(xp);)
egdanielfa896322016-01-13 12:19:30 -0800256 fFS.codeAppend("}");
257}
258
Brian Salomon559f5562017-11-15 14:28:33 -0500259void GrGLSLProgramBuilder::emitSamplers(
Brian Salomonab015ef2017-04-04 10:15:51 -0400260 const GrResourceIOProcessor& processor,
Brian Salomonf9f45122016-11-29 11:59:17 -0500261 SkTArray<SamplerHandle>* outTexSamplerHandles,
Brian Salomon559f5562017-11-15 14:28:33 -0500262 SkTArray<TexelBufferHandle>* outTexelBufferHandles) {
cdalton9c3f1432016-03-11 10:07:37 -0800263 SkString name;
Brian Salomon0bbecb22016-11-17 11:38:22 -0500264 int numTextureSamplers = processor.numTextureSamplers();
265 for (int t = 0; t < numTextureSamplers; ++t) {
Brian Salomonab015ef2017-04-04 10:15:51 -0400266 const GrResourceIOProcessor::TextureSampler& sampler = processor.textureSampler(t);
Brian Salomonf9f45122016-11-29 11:59:17 -0500267 name.printf("TextureSampler_%d", outTexSamplerHandles->count());
Robert Phillips9bee2e52017-05-29 12:37:20 -0400268 GrSLType samplerType = sampler.peekTexture()->texturePriv().samplerType();
Brian Salomon18dfa982017-04-03 16:57:43 -0400269 outTexSamplerHandles->emplace_back(this->emitSampler(
Robert Phillips9bee2e52017-05-29 12:37:20 -0400270 samplerType, sampler.peekTexture()->config(), name.c_str(), sampler.visibility()));
cdalton9c3f1432016-03-11 10:07:37 -0800271 }
cdalton74b8d322016-04-11 14:47:28 -0700272 if (int numBuffers = processor.numBuffers()) {
Brian Salomon94efbf52016-11-29 13:43:05 -0500273 SkASSERT(this->shaderCaps()->texelBufferSupport());
cdalton74b8d322016-04-11 14:47:28 -0700274 GrShaderFlags texelBufferVisibility = kNone_GrShaderFlags;
275
276 for (int b = 0; b < numBuffers; ++b) {
Brian Salomonab015ef2017-04-04 10:15:51 -0400277 const GrResourceIOProcessor::BufferAccess& access = processor.bufferAccess(b);
Greg Danielbc5d4d72017-05-05 10:28:42 -0400278 name.printf("TexelBuffer_%d", outTexelBufferHandles->count());
279 outTexelBufferHandles->emplace_back(
280 this->emitTexelBuffer(access.texelConfig(), name.c_str(), access.visibility()));
cdalton74b8d322016-04-11 14:47:28 -0700281 texelBufferVisibility |= access.visibility();
282 }
283
Brian Salomon94efbf52016-11-29 13:43:05 -0500284 if (const char* extension = this->shaderCaps()->texelBufferExtensionString()) {
cdalton74b8d322016-04-11 14:47:28 -0700285 this->addFeature(texelBufferVisibility,
286 1 << GrGLSLShaderBuilder::kTexelBuffer_GLSLPrivateFeature,
287 extension);
288 }
289 }
290}
291
Greg Danielbc5d4d72017-05-05 10:28:42 -0400292void GrGLSLProgramBuilder::updateSamplerCounts(GrShaderFlags visibility) {
cdalton74b8d322016-04-11 14:47:28 -0700293 if (visibility & kVertex_GrShaderFlag) {
294 ++fNumVertexSamplers;
295 }
296 if (visibility & kGeometry_GrShaderFlag) {
297 SkASSERT(this->primitiveProcessor().willUseGeoShader());
298 ++fNumGeometrySamplers;
299 }
300 if (visibility & kFragment_GrShaderFlag) {
301 ++fNumFragmentSamplers;
302 }
Greg Danielbc5d4d72017-05-05 10:28:42 -0400303}
304
305GrGLSLProgramBuilder::SamplerHandle GrGLSLProgramBuilder::emitSampler(GrSLType samplerType,
306 GrPixelConfig config,
307 const char* name,
308 GrShaderFlags visibility) {
309 this->updateSamplerCounts(visibility);
Chris Dalton47c8ed32017-11-15 18:27:09 -0700310 GrSLPrecision precision = GrSLSamplerPrecision(config);
Brian Salomon94efbf52016-11-29 13:43:05 -0500311 GrSwizzle swizzle = this->shaderCaps()->configTextureSwizzle(config);
Brian Salomon18dfa982017-04-03 16:57:43 -0400312 return this->uniformHandler()->addSampler(visibility, swizzle, samplerType, precision, name);
Brian Salomonf9f45122016-11-29 11:59:17 -0500313}
314
Greg Danielbc5d4d72017-05-05 10:28:42 -0400315GrGLSLProgramBuilder::TexelBufferHandle GrGLSLProgramBuilder::emitTexelBuffer(
316 GrPixelConfig config, const char* name, GrShaderFlags visibility) {
317 this->updateSamplerCounts(visibility);
Chris Dalton47c8ed32017-11-15 18:27:09 -0700318 GrSLPrecision precision = GrSLSamplerPrecision(config);
Greg Danielbc5d4d72017-05-05 10:28:42 -0400319 return this->uniformHandler()->addTexelBuffer(visibility, precision, name);
320}
321
egdanielfa896322016-01-13 12:19:30 -0800322void GrGLSLProgramBuilder::emitFSOutputSwizzle(bool hasSecondaryOutput) {
323 // Swizzle the fragment shader outputs if necessary.
324 GrSwizzle swizzle;
Ethan Nicholas38657112017-02-09 17:01:22 -0500325 swizzle.setFromKey(this->desc()->header().fOutputSwizzle);
egdanielfa896322016-01-13 12:19:30 -0800326 if (swizzle != GrSwizzle::RGBA()) {
327 fFS.codeAppendf("%s = %s.%s;", fFS.getPrimaryColorOutputName(),
328 fFS.getPrimaryColorOutputName(),
329 swizzle.c_str());
330 if (hasSecondaryOutput) {
331 fFS.codeAppendf("%s = %s.%s;", fFS.getSecondaryColorOutputName(),
332 fFS.getSecondaryColorOutputName(),
333 swizzle.c_str());
334 }
335 }
336}
337
cdalton9c3f1432016-03-11 10:07:37 -0800338bool GrGLSLProgramBuilder::checkSamplerCounts() {
Brian Salomon1edc5b92016-11-29 13:43:46 -0500339 const GrShaderCaps& shaderCaps = *this->shaderCaps();
340 if (fNumVertexSamplers > shaderCaps.maxVertexSamplers()) {
cdalton9c3f1432016-03-11 10:07:37 -0800341 GrCapsDebugf(this->caps(), "Program would use too many vertex samplers\n");
342 return false;
343 }
Brian Salomon1edc5b92016-11-29 13:43:46 -0500344 if (fNumGeometrySamplers > shaderCaps.maxGeometrySamplers()) {
cdalton9c3f1432016-03-11 10:07:37 -0800345 GrCapsDebugf(this->caps(), "Program would use too many geometry samplers\n");
346 return false;
347 }
Brian Salomon1edc5b92016-11-29 13:43:46 -0500348 if (fNumFragmentSamplers > shaderCaps.maxFragmentSamplers()) {
cdalton9c3f1432016-03-11 10:07:37 -0800349 GrCapsDebugf(this->caps(), "Program would use too many fragment samplers\n");
350 return false;
351 }
352 // If the same sampler is used in two different shaders, it counts as two combined samplers.
353 int numCombinedSamplers = fNumVertexSamplers + fNumGeometrySamplers + fNumFragmentSamplers;
Brian Salomon1edc5b92016-11-29 13:43:46 -0500354 if (numCombinedSamplers > shaderCaps.maxCombinedSamplers()) {
cdalton9c3f1432016-03-11 10:07:37 -0800355 GrCapsDebugf(this->caps(), "Program would use too many combined samplers\n");
356 return false;
357 }
358 return true;
359}
360
cdalton87332102016-02-26 12:22:02 -0800361#ifdef SK_DEBUG
egdanielfa896322016-01-13 12:19:30 -0800362void GrGLSLProgramBuilder::verify(const GrPrimitiveProcessor& gp) {
egdanielfa896322016-01-13 12:19:30 -0800363}
364
365void GrGLSLProgramBuilder::verify(const GrXferProcessor& xp) {
366 SkASSERT(fFS.hasReadDstColor() == xp.willReadDstColor());
367}
368
369void GrGLSLProgramBuilder::verify(const GrFragmentProcessor& fp) {
egdaniel8dcdedc2015-11-11 06:27:20 -0800370}
cdalton87332102016-02-26 12:22:02 -0800371#endif
egdaniel8dcdedc2015-11-11 06:27:20 -0800372
373void GrGLSLProgramBuilder::nameVariable(SkString* out, char prefix, const char* name, bool mangle) {
374 if ('\0' == prefix) {
375 *out = name;
376 } else {
377 out->printf("%c%s", prefix, name);
378 }
379 if (mangle) {
380 if (out->endsWith('_')) {
381 // Names containing "__" are reserved.
382 out->append("x");
383 }
384 out->appendf("_Stage%d%s", fStageIndex, fFS.getMangleString().c_str());
385 }
386}
387
Ethan Nicholas2983f402017-05-08 09:36:08 -0400388void GrGLSLProgramBuilder::nameExpression(SkString* output, const char* baseName) {
egdanielfa896322016-01-13 12:19:30 -0800389 // create var to hold stage result. If we already have a valid output name, just use that
390 // otherwise create a new mangled one. This name is only valid if we are reordering stages
391 // and have to tell stage exactly where to put its output.
392 SkString outName;
Ethan Nicholas2983f402017-05-08 09:36:08 -0400393 if (output->size()) {
egdanielfa896322016-01-13 12:19:30 -0800394 outName = output->c_str();
395 } else {
396 this->nameVariable(&outName, '\0', baseName);
397 }
Ethan Nicholasf7b88202017-09-18 14:10:39 -0400398 fFS.codeAppendf("half4 %s;", outName.c_str());
egdanielfa896322016-01-13 12:19:30 -0800399 *output = outName;
400}
401
cdalton5e58cee2016-02-11 12:49:47 -0800402void GrGLSLProgramBuilder::appendUniformDecls(GrShaderFlags visibility, SkString* out) const {
egdaniel7ea439b2015-12-03 09:20:44 -0800403 this->uniformHandler()->appendUniformDecls(visibility, out);
404}
405
Ethan Nicholas941e7e22016-12-12 15:33:30 -0500406void GrGLSLProgramBuilder::addRTHeightUniform(const char* name) {
egdaniel7ea439b2015-12-03 09:20:44 -0800407 SkASSERT(!fUniformHandles.fRTHeightUni.isValid());
408 GrGLSLUniformHandler* uniformHandler = this->uniformHandler();
409 fUniformHandles.fRTHeightUni =
cdalton5e58cee2016-02-11 12:49:47 -0800410 uniformHandler->internalAddUniformArray(kFragment_GrShaderFlag,
Ethan Nicholasf7b88202017-09-18 14:10:39 -0400411 kHalf_GrSLType, kDefault_GrSLPrecision,
Ethan Nicholas941e7e22016-12-12 15:33:30 -0500412 name, false, 0, nullptr);
egdaniel8dcdedc2015-11-11 06:27:20 -0800413}
414
egdaniel9f1d4152016-02-10 09:50:38 -0800415void GrGLSLProgramBuilder::finalizeShaders() {
416 this->varyingHandler()->finalize();
cdalton5e58cee2016-02-11 12:49:47 -0800417 fVS.finalize(kVertex_GrShaderFlag);
csmartdalton276cc412016-11-21 11:55:00 -0700418 if (this->primitiveProcessor().willUseGeoShader()) {
Brian Salomon94efbf52016-11-29 13:43:05 -0500419 SkASSERT(this->shaderCaps()->geometryShaderSupport());
csmartdalton276cc412016-11-21 11:55:00 -0700420 fGS.finalize(kGeometry_GrShaderFlag);
421 }
cdalton5e58cee2016-02-11 12:49:47 -0800422 fFS.finalize(kFragment_GrShaderFlag);
egdaniel9f1d4152016-02-10 09:50:38 -0800423}