blob: 4cd4fd6ec5ea68d026095e394653e8fcd89b9b76 [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"
18
egdaniel8dcdedc2015-11-11 06:27:20 -080019const int GrGLSLProgramBuilder::kVarsPerBlock = 8;
20
egdaniel0e1853c2016-03-17 11:35:45 -070021GrGLSLProgramBuilder::GrGLSLProgramBuilder(const GrPipeline& pipeline,
22 const GrPrimitiveProcessor& primProc,
Ethan Nicholas38657112017-02-09 17:01:22 -050023 GrProgramDesc* desc)
egdaniel8dcdedc2015-11-11 06:27:20 -080024 : fVS(this)
25 , fGS(this)
cdalton28f45b92016-03-07 13:58:26 -080026 , fFS(this)
egdaniel8dcdedc2015-11-11 06:27:20 -080027 , fStageIndex(-1)
egdaniel0e1853c2016-03-17 11:35:45 -070028 , fPipeline(pipeline)
29 , fPrimProc(primProc)
30 , fDesc(desc)
egdanielfa896322016-01-13 12:19:30 -080031 , fGeometryProcessor(nullptr)
cdalton9c3f1432016-03-11 10:07:37 -080032 , fXferProcessor(nullptr)
cdalton9c3f1432016-03-11 10:07:37 -080033 , fNumVertexSamplers(0)
34 , fNumGeometrySamplers(0)
Brian Salomonf9f45122016-11-29 11:59:17 -050035 , fNumFragmentSamplers(0)
36 , fNumVertexImageStorages(0)
37 , fNumGeometryImageStorages(0)
38 , fNumFragmentImageStorages(0) {
cdalton9c3f1432016-03-11 10:07:37 -080039}
40
41void GrGLSLProgramBuilder::addFeature(GrShaderFlags shaders,
42 uint32_t featureBit,
43 const char* extensionName) {
44 if (shaders & kVertex_GrShaderFlag) {
45 fVS.addFeature(featureBit, extensionName);
46 }
47 if (shaders & kGeometry_GrShaderFlag) {
csmartdalton276cc412016-11-21 11:55:00 -070048 SkASSERT(this->primitiveProcessor().willUseGeoShader());
cdalton9c3f1432016-03-11 10:07:37 -080049 fGS.addFeature(featureBit, extensionName);
50 }
51 if (shaders & kFragment_GrShaderFlag) {
52 fFS.addFeature(featureBit, extensionName);
53 }
egdanielfa896322016-01-13 12:19:30 -080054}
55
56bool GrGLSLProgramBuilder::emitAndInstallProcs(GrGLSLExpr4* inputColor,
cdalton9c3f1432016-03-11 10:07:37 -080057 GrGLSLExpr4* inputCoverage) {
egdanielfa896322016-01-13 12:19:30 -080058 // First we loop over all of the installed processors and collect coord transforms. These will
59 // be sent to the GrGLSLPrimitiveProcessor in its emitCode function
60 const GrPrimitiveProcessor& primProc = this->primitiveProcessor();
egdanielfa896322016-01-13 12:19:30 -080061
egdanielfa896322016-01-13 12:19:30 -080062 this->emitAndInstallPrimProc(primProc, inputColor, inputCoverage);
63
bsalomona624bf32016-09-20 09:12:47 -070064 this->emitAndInstallFragProcs(inputColor, inputCoverage);
Brian Salomon18dfa982017-04-03 16:57:43 -040065 this->emitAndInstallXferProc(*inputColor, *inputCoverage);
Brian Salomon42c456f2017-03-06 11:29:48 -050066 this->emitFSOutputSwizzle(this->pipeline().getXferProcessor().hasSecondaryOutput());
cdalton9c3f1432016-03-11 10:07:37 -080067
Brian Salomonf9f45122016-11-29 11:59:17 -050068 return this->checkSamplerCounts() && this->checkImageStorageCounts();
egdanielfa896322016-01-13 12:19:30 -080069}
70
71void GrGLSLProgramBuilder::emitAndInstallPrimProc(const GrPrimitiveProcessor& proc,
72 GrGLSLExpr4* outputColor,
73 GrGLSLExpr4* outputCoverage) {
74 // Program builders have a bit of state we need to clear with each effect
75 AutoStageAdvance adv(this);
76 this->nameExpression(outputColor, "outputColor");
77 this->nameExpression(outputCoverage, "outputCoverage");
78
dvonbeck9b03e7b2016-08-01 11:01:56 -070079 const char* distanceVectorName = nullptr;
80 if (this->fPipeline.usesDistanceVectorField() && proc.implementsDistanceVector()) {
robertphillips05a4cf52016-09-08 09:02:43 -070081 // Each individual user (FP) of the distance vector must be able to handle having this
82 // variable be undeclared. There is no single default value that will yield a reasonable
83 // result for all users.
dvonbeck9b03e7b2016-08-01 11:01:56 -070084 distanceVectorName = fFS.distanceVectorName();
dvonbeckee920632016-08-11 14:17:59 -070085 fFS.codeAppend( "// Normalized vector to the closest geometric edge (in device space)\n");
dvonbeck84bca782016-08-08 11:47:12 -070086 fFS.codeAppend( "// Distance to the edge encoded in the z-component\n");
jvanverth6c177a12016-08-17 07:59:41 -070087 fFS.codeAppendf("vec4 %s;", distanceVectorName);
dvonbeck9b03e7b2016-08-01 11:01:56 -070088 }
89
csmartdalton936f81b2017-02-13 15:45:35 -070090 SkASSERT(!fUniformHandles.fRTAdjustmentUni.isValid());
91 GrShaderFlags rtAdjustVisibility = kVertex_GrShaderFlag;
92 if (proc.willUseGeoShader()) {
93 rtAdjustVisibility |= kGeometry_GrShaderFlag;
94 }
95 fUniformHandles.fRTAdjustmentUni = this->uniformHandler()->addUniform(rtAdjustVisibility,
96 kVec4f_GrSLType,
97 kHigh_GrSLPrecision,
98 "rtAdjustment");
99 const char* rtAdjustName =
100 this->uniformHandler()->getUniformCStr(fUniformHandles.fRTAdjustmentUni);
101
egdanielfa896322016-01-13 12:19:30 -0800102 // Enclose custom code in a block to avoid namespace conflicts
103 SkString openBrace;
104 openBrace.printf("{ // Stage %d, %s\n", fStageIndex, proc.name());
105 fFS.codeAppend(openBrace.c_str());
106 fVS.codeAppendf("// Primitive Processor %s\n", proc.name());
107
108 SkASSERT(!fGeometryProcessor);
Brian Salomon94efbf52016-11-29 13:43:05 -0500109 fGeometryProcessor = proc.createGLSLInstance(*this->shaderCaps());
egdanielfa896322016-01-13 12:19:30 -0800110
Brian Salomonf9f45122016-11-29 11:59:17 -0500111 SkSTArray<4, SamplerHandle> texSamplers(proc.numTextureSamplers());
112 SkSTArray<2, SamplerHandle> bufferSamplers(proc.numBuffers());
113 SkSTArray<2, ImageStorageHandle> imageStorages(proc.numImageStorages());
114 this->emitSamplersAndImageStorages(proc, &texSamplers, &bufferSamplers, &imageStorages);
egdanielfa896322016-01-13 12:19:30 -0800115
bsalomona624bf32016-09-20 09:12:47 -0700116 GrGLSLPrimitiveProcessor::FPCoordTransformHandler transformHandler(fPipeline,
117 &fTransformedCoordVars);
egdanielfa896322016-01-13 12:19:30 -0800118 GrGLSLGeometryProcessor::EmitArgs args(&fVS,
csmartdalton276cc412016-11-21 11:55:00 -0700119 proc.willUseGeoShader() ? &fGS : nullptr,
egdanielfa896322016-01-13 12:19:30 -0800120 &fFS,
121 this->varyingHandler(),
122 this->uniformHandler(),
Brian Salomon94efbf52016-11-29 13:43:05 -0500123 this->shaderCaps(),
egdanielfa896322016-01-13 12:19:30 -0800124 proc,
125 outputColor->c_str(),
126 outputCoverage->c_str(),
dvonbeck9b03e7b2016-08-01 11:01:56 -0700127 distanceVectorName,
csmartdalton936f81b2017-02-13 15:45:35 -0700128 rtAdjustName,
egdaniel09aa1fc2016-04-20 07:09:46 -0700129 texSamplers.begin(),
130 bufferSamplers.begin(),
Brian Salomonf9f45122016-11-29 11:59:17 -0500131 imageStorages.begin(),
bsalomona624bf32016-09-20 09:12:47 -0700132 &transformHandler);
egdanielfa896322016-01-13 12:19:30 -0800133 fGeometryProcessor->emitCode(args);
134
135 // We have to check that effects and the code they emit are consistent, ie if an effect
136 // asks for dst color, then the emit code needs to follow suit
cdalton87332102016-02-26 12:22:02 -0800137 SkDEBUGCODE(verify(proc);)
egdanielfa896322016-01-13 12:19:30 -0800138
139 fFS.codeAppend("}");
140}
141
bsalomona624bf32016-09-20 09:12:47 -0700142void GrGLSLProgramBuilder::emitAndInstallFragProcs(GrGLSLExpr4* color, GrGLSLExpr4* coverage) {
143 int transformedCoordVarsIdx = 0;
144 GrGLSLExpr4** inOut = &color;
145 for (int i = 0; i < this->pipeline().numFragmentProcessors(); ++i) {
146 if (i == this->pipeline().numColorFragmentProcessors()) {
147 inOut = &coverage;
148 }
egdanielfa896322016-01-13 12:19:30 -0800149 GrGLSLExpr4 output;
150 const GrFragmentProcessor& fp = this->pipeline().getFragmentProcessor(i);
bsalomona624bf32016-09-20 09:12:47 -0700151 this->emitAndInstallFragProc(fp, i, transformedCoordVarsIdx, **inOut, &output);
152 GrFragmentProcessor::Iter iter(&fp);
153 while (const GrFragmentProcessor* fp = iter.next()) {
154 transformedCoordVarsIdx += fp->numCoordTransforms();
155 }
156 **inOut = output;
egdanielfa896322016-01-13 12:19:30 -0800157 }
158}
159
160// TODO Processors cannot output zeros because an empty string is all 1s
161// the fix is to allow effects to take the GrGLSLExpr4 directly
162void GrGLSLProgramBuilder::emitAndInstallFragProc(const GrFragmentProcessor& fp,
163 int index,
bsalomona624bf32016-09-20 09:12:47 -0700164 int transformedCoordVarsIdx,
egdanielfa896322016-01-13 12:19:30 -0800165 const GrGLSLExpr4& input,
166 GrGLSLExpr4* output) {
167 // Program builders have a bit of state we need to clear with each effect
168 AutoStageAdvance adv(this);
169 this->nameExpression(output, "output");
170
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 Salomon0bbecb22016-11-17 11:38:22 -0500178 SkSTArray<4, SamplerHandle> textureSamplerArray(fp.numTextureSamplers());
bsalomonb58a2b42016-09-26 06:55:02 -0700179 SkSTArray<2, SamplerHandle> bufferSamplerArray(fp.numBuffers());
Brian Salomonf9f45122016-11-29 11:59:17 -0500180 SkSTArray<2, ImageStorageHandle> imageStorageArray(fp.numImageStorages());
bsalomonb58a2b42016-09-26 06:55:02 -0700181 GrFragmentProcessor::Iter iter(&fp);
182 while (const GrFragmentProcessor* subFP = iter.next()) {
Brian Salomonf9f45122016-11-29 11:59:17 -0500183 this->emitSamplersAndImageStorages(*subFP, &textureSamplerArray, &bufferSamplerArray,
184 &imageStorageArray);
bsalomonb58a2b42016-09-26 06:55:02 -0700185 }
egdanielfa896322016-01-13 12:19:30 -0800186
bsalomona624bf32016-09-20 09:12:47 -0700187 const GrShaderVar* coordVars = fTransformedCoordVars.begin() + transformedCoordVarsIdx;
188 GrGLSLFragmentProcessor::TransformedCoordVars coords(&fp, coordVars);
bsalomonb58a2b42016-09-26 06:55:02 -0700189 GrGLSLFragmentProcessor::TextureSamplers textureSamplers(&fp, textureSamplerArray.begin());
190 GrGLSLFragmentProcessor::BufferSamplers bufferSamplers(&fp, bufferSamplerArray.begin());
Brian Salomonf9f45122016-11-29 11:59:17 -0500191 GrGLSLFragmentProcessor::ImageStorages imageStorages(&fp, imageStorageArray.begin());
egdanielfa896322016-01-13 12:19:30 -0800192 GrGLSLFragmentProcessor::EmitArgs args(&fFS,
193 this->uniformHandler(),
Brian Salomon94efbf52016-11-29 13:43:05 -0500194 this->shaderCaps(),
egdanielfa896322016-01-13 12:19:30 -0800195 fp,
196 output->c_str(),
197 input.isOnes() ? nullptr : input.c_str(),
bsalomona624bf32016-09-20 09:12:47 -0700198 coords,
bsalomonb58a2b42016-09-26 06:55:02 -0700199 textureSamplers,
200 bufferSamplers,
Brian Salomonf9f45122016-11-29 11:59:17 -0500201 imageStorages,
dvonbeck9b03e7b2016-08-01 11:01:56 -0700202 this->primitiveProcessor().implementsDistanceVector());
203
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);)
egdanielfa896322016-01-13 12:19:30 -0800209 fFragmentProcessors.push_back(fragProc);
210
211 fFS.codeAppend("}");
212}
213
Brian Salomon18dfa982017-04-03 16:57:43 -0400214void GrGLSLProgramBuilder::emitAndInstallXferProc(const GrGLSLExpr4& colorIn,
Brian Salomon42c456f2017-03-06 11:29:48 -0500215 const GrGLSLExpr4& 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();
egdanielfa896322016-01-13 12:19:30 -0800221 fXferProcessor = xp.createGLSLInstance();
222
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;
238 if (GrTexture* dstTexture = fPipeline.dstTexture()) {
239 // GrProcessor::TextureSampler sampler(dstTexture);
240 SkString name("DstTextureSampler");
241 dstTextureSamplerHandle =
242 this->emitSampler(dstTexture->texturePriv().samplerType(), dstTexture->config(),
243 "DstTextureSampler", kFragment_GrShaderFlag);
244 dstTextureOrigin = dstTexture->origin();
245 SkASSERT(kTextureExternalSampler_GrSLType != dstTexture->texturePriv().samplerType());
246 }
egdanielfa896322016-01-13 12:19:30 -0800247
248 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,
252 colorIn.c_str(),
Brian Salomon8c852be2017-01-04 10:44:42 -0500253 coverageIn.c_str(),
egdanielfa896322016-01-13 12:19:30 -0800254 fFS.getPrimaryColorOutputName(),
255 fFS.getSecondaryColorOutputName(),
Brian Salomon18dfa982017-04-03 16:57:43 -0400256 dstTextureSamplerHandle,
257 dstTextureOrigin);
egdanielfa896322016-01-13 12:19:30 -0800258 fXferProcessor->emitCode(args);
259
260 // We have to check that effects and the code they emit are consistent, ie if an effect
261 // asks for dst color, then the emit code needs to follow suit
cdalton87332102016-02-26 12:22:02 -0800262 SkDEBUGCODE(verify(xp);)
egdanielfa896322016-01-13 12:19:30 -0800263 fFS.codeAppend("}");
264}
265
Brian Salomonf9f45122016-11-29 11:59:17 -0500266void GrGLSLProgramBuilder::emitSamplersAndImageStorages(
267 const GrProcessor& processor,
268 SkTArray<SamplerHandle>* outTexSamplerHandles,
269 SkTArray<SamplerHandle>* outBufferSamplerHandles,
270 SkTArray<ImageStorageHandle>* outImageStorageHandles) {
cdalton9c3f1432016-03-11 10:07:37 -0800271 SkString name;
Brian Salomon0bbecb22016-11-17 11:38:22 -0500272 int numTextureSamplers = processor.numTextureSamplers();
273 for (int t = 0; t < numTextureSamplers; ++t) {
274 const GrProcessor::TextureSampler& sampler = processor.textureSampler(t);
Brian Salomonf9f45122016-11-29 11:59:17 -0500275 name.printf("TextureSampler_%d", outTexSamplerHandles->count());
Brian Salomondb4183d2016-11-17 12:48:40 -0500276 GrSLType samplerType = sampler.texture()->texturePriv().samplerType();
egdaniel990dbc82016-07-13 14:09:30 -0700277 if (kTextureExternalSampler_GrSLType == samplerType) {
Brian Salomon94efbf52016-11-29 13:43:05 -0500278 const char* externalFeatureString =
279 this->shaderCaps()->externalTextureExtensionString();
cdalton9c3f1432016-03-11 10:07:37 -0800280 // We shouldn't ever create a GrGLTexture that requires external sampler type
281 SkASSERT(externalFeatureString);
Brian Salomondb4183d2016-11-17 12:48:40 -0500282 this->addFeature(sampler.visibility(),
cdalton9c3f1432016-03-11 10:07:37 -0800283 1 << GrGLSLShaderBuilder::kExternalTexture_GLSLPrivateFeature,
284 externalFeatureString);
285 }
Brian Salomon18dfa982017-04-03 16:57:43 -0400286 outTexSamplerHandles->emplace_back(this->emitSampler(
287 samplerType, sampler.texture()->config(), name.c_str(), sampler.visibility()));
cdalton9c3f1432016-03-11 10:07:37 -0800288 }
cdalton74b8d322016-04-11 14:47:28 -0700289 if (int numBuffers = processor.numBuffers()) {
Brian Salomon94efbf52016-11-29 13:43:05 -0500290 SkASSERT(this->shaderCaps()->texelBufferSupport());
cdalton74b8d322016-04-11 14:47:28 -0700291 GrShaderFlags texelBufferVisibility = kNone_GrShaderFlags;
292
293 for (int b = 0; b < numBuffers; ++b) {
Brian Salomonb014cca2016-11-18 11:39:15 -0500294 const GrProcessor::BufferAccess& access = processor.bufferAccess(b);
Brian Salomonf9f45122016-11-29 11:59:17 -0500295 name.printf("BufferSampler_%d", outBufferSamplerHandles->count());
Brian Salomon18dfa982017-04-03 16:57:43 -0400296 outBufferSamplerHandles->emplace_back(
297 this->emitSampler(kBufferSampler_GrSLType, access.texelConfig(), name.c_str(),
298 access.visibility()));
cdalton74b8d322016-04-11 14:47:28 -0700299 texelBufferVisibility |= access.visibility();
300 }
301
Brian Salomon94efbf52016-11-29 13:43:05 -0500302 if (const char* extension = this->shaderCaps()->texelBufferExtensionString()) {
cdalton74b8d322016-04-11 14:47:28 -0700303 this->addFeature(texelBufferVisibility,
304 1 << GrGLSLShaderBuilder::kTexelBuffer_GLSLPrivateFeature,
305 extension);
306 }
307 }
Brian Salomonf9f45122016-11-29 11:59:17 -0500308 int numImageStorages = processor.numImageStorages();
309 for (int i = 0; i < numImageStorages; ++i) {
310 const GrProcessor::ImageStorageAccess& imageStorageAccess = processor.imageStorageAccess(i);
311 name.printf("Image_%d", outImageStorageHandles->count());
Brian Salomon18dfa982017-04-03 16:57:43 -0400312 outImageStorageHandles->emplace_back(
313 this->emitImageStorage(imageStorageAccess, name.c_str()));
Brian Salomonf9f45122016-11-29 11:59:17 -0500314 }
cdalton74b8d322016-04-11 14:47:28 -0700315}
316
Brian Salomon18dfa982017-04-03 16:57:43 -0400317GrGLSLProgramBuilder::SamplerHandle GrGLSLProgramBuilder::emitSampler(GrSLType samplerType,
318 GrPixelConfig config,
319 const char* name,
320 GrShaderFlags visibility) {
cdalton74b8d322016-04-11 14:47:28 -0700321 if (visibility & kVertex_GrShaderFlag) {
322 ++fNumVertexSamplers;
323 }
324 if (visibility & kGeometry_GrShaderFlag) {
325 SkASSERT(this->primitiveProcessor().willUseGeoShader());
326 ++fNumGeometrySamplers;
327 }
328 if (visibility & kFragment_GrShaderFlag) {
329 ++fNumFragmentSamplers;
330 }
Brian Salomon94efbf52016-11-29 13:43:05 -0500331 GrSLPrecision precision = this->shaderCaps()->samplerPrecision(config, visibility);
332 GrSwizzle swizzle = this->shaderCaps()->configTextureSwizzle(config);
Brian Salomon18dfa982017-04-03 16:57:43 -0400333 return this->uniformHandler()->addSampler(visibility, swizzle, samplerType, precision, name);
Brian Salomonf9f45122016-11-29 11:59:17 -0500334}
335
Brian Salomon18dfa982017-04-03 16:57:43 -0400336GrGLSLProgramBuilder::ImageStorageHandle GrGLSLProgramBuilder::emitImageStorage(
337 const GrProcessor::ImageStorageAccess& access, const char* name) {
Brian Salomonf9f45122016-11-29 11:59:17 -0500338 if (access.visibility() & kVertex_GrShaderFlag) {
339 ++fNumVertexImageStorages;
340 }
341 if (access.visibility() & kGeometry_GrShaderFlag) {
342 SkASSERT(this->primitiveProcessor().willUseGeoShader());
343 ++fNumGeometryImageStorages;
344 }
345 if (access.visibility() & kFragment_GrShaderFlag) {
346 ++fNumFragmentImageStorages;
347 }
348 GrSLType uniformType = access.texture()->texturePriv().imageStorageType();
Brian Salomon18dfa982017-04-03 16:57:43 -0400349 return this->uniformHandler()->addImageStorage(access.visibility(), uniformType,
350 access.format(), access.memoryModel(),
351 access.restrict(), access.ioType(), name);
cdalton9c3f1432016-03-11 10:07:37 -0800352}
353
egdanielfa896322016-01-13 12:19:30 -0800354void GrGLSLProgramBuilder::emitFSOutputSwizzle(bool hasSecondaryOutput) {
355 // Swizzle the fragment shader outputs if necessary.
356 GrSwizzle swizzle;
Ethan Nicholas38657112017-02-09 17:01:22 -0500357 swizzle.setFromKey(this->desc()->header().fOutputSwizzle);
egdanielfa896322016-01-13 12:19:30 -0800358 if (swizzle != GrSwizzle::RGBA()) {
359 fFS.codeAppendf("%s = %s.%s;", fFS.getPrimaryColorOutputName(),
360 fFS.getPrimaryColorOutputName(),
361 swizzle.c_str());
362 if (hasSecondaryOutput) {
363 fFS.codeAppendf("%s = %s.%s;", fFS.getSecondaryColorOutputName(),
364 fFS.getSecondaryColorOutputName(),
365 swizzle.c_str());
366 }
367 }
368}
369
cdalton9c3f1432016-03-11 10:07:37 -0800370bool GrGLSLProgramBuilder::checkSamplerCounts() {
Brian Salomon1edc5b92016-11-29 13:43:46 -0500371 const GrShaderCaps& shaderCaps = *this->shaderCaps();
372 if (fNumVertexSamplers > shaderCaps.maxVertexSamplers()) {
cdalton9c3f1432016-03-11 10:07:37 -0800373 GrCapsDebugf(this->caps(), "Program would use too many vertex samplers\n");
374 return false;
375 }
Brian Salomon1edc5b92016-11-29 13:43:46 -0500376 if (fNumGeometrySamplers > shaderCaps.maxGeometrySamplers()) {
cdalton9c3f1432016-03-11 10:07:37 -0800377 GrCapsDebugf(this->caps(), "Program would use too many geometry samplers\n");
378 return false;
379 }
Brian Salomon1edc5b92016-11-29 13:43:46 -0500380 if (fNumFragmentSamplers > shaderCaps.maxFragmentSamplers()) {
cdalton9c3f1432016-03-11 10:07:37 -0800381 GrCapsDebugf(this->caps(), "Program would use too many fragment samplers\n");
382 return false;
383 }
384 // If the same sampler is used in two different shaders, it counts as two combined samplers.
385 int numCombinedSamplers = fNumVertexSamplers + fNumGeometrySamplers + fNumFragmentSamplers;
Brian Salomon1edc5b92016-11-29 13:43:46 -0500386 if (numCombinedSamplers > shaderCaps.maxCombinedSamplers()) {
cdalton9c3f1432016-03-11 10:07:37 -0800387 GrCapsDebugf(this->caps(), "Program would use too many combined samplers\n");
388 return false;
389 }
390 return true;
391}
392
Brian Salomonf9f45122016-11-29 11:59:17 -0500393bool GrGLSLProgramBuilder::checkImageStorageCounts() {
Brian Salomon1edc5b92016-11-29 13:43:46 -0500394 const GrShaderCaps& shaderCaps = *this->shaderCaps();
395 if (fNumVertexImageStorages > shaderCaps.maxVertexImageStorages()) {
Brian Salomonf9f45122016-11-29 11:59:17 -0500396 GrCapsDebugf(this->caps(), "Program would use too many vertex images\n");
397 return false;
398 }
Brian Salomon1edc5b92016-11-29 13:43:46 -0500399 if (fNumGeometryImageStorages > shaderCaps.maxGeometryImageStorages()) {
Brian Salomonf9f45122016-11-29 11:59:17 -0500400 GrCapsDebugf(this->caps(), "Program would use too many geometry images\n");
401 return false;
402 }
Brian Salomon1edc5b92016-11-29 13:43:46 -0500403 if (fNumFragmentImageStorages > shaderCaps.maxFragmentImageStorages()) {
Brian Salomonf9f45122016-11-29 11:59:17 -0500404 GrCapsDebugf(this->caps(), "Program would use too many fragment images\n");
405 return false;
406 }
407 // If the same image is used in two different shaders, it counts as two combined images.
408 int numCombinedImages = fNumVertexImageStorages + fNumGeometryImageStorages +
409 fNumFragmentImageStorages;
Brian Salomon1edc5b92016-11-29 13:43:46 -0500410 if (numCombinedImages > shaderCaps.maxCombinedImageStorages()) {
Brian Salomonf9f45122016-11-29 11:59:17 -0500411 GrCapsDebugf(this->caps(), "Program would use too many combined images\n");
412 return false;
413 }
414 return true;
415}
416
cdalton87332102016-02-26 12:22:02 -0800417#ifdef SK_DEBUG
egdanielfa896322016-01-13 12:19:30 -0800418void GrGLSLProgramBuilder::verify(const GrPrimitiveProcessor& gp) {
cdalton87332102016-02-26 12:22:02 -0800419 SkASSERT(fFS.usedProcessorFeatures() == gp.requiredFeatures());
egdanielfa896322016-01-13 12:19:30 -0800420}
421
422void GrGLSLProgramBuilder::verify(const GrXferProcessor& xp) {
cdalton87332102016-02-26 12:22:02 -0800423 SkASSERT(fFS.usedProcessorFeatures() == xp.requiredFeatures());
egdanielfa896322016-01-13 12:19:30 -0800424 SkASSERT(fFS.hasReadDstColor() == xp.willReadDstColor());
425}
426
427void GrGLSLProgramBuilder::verify(const GrFragmentProcessor& fp) {
cdalton87332102016-02-26 12:22:02 -0800428 SkASSERT(fFS.usedProcessorFeatures() == fp.requiredFeatures());
egdaniel8dcdedc2015-11-11 06:27:20 -0800429}
cdalton87332102016-02-26 12:22:02 -0800430#endif
egdaniel8dcdedc2015-11-11 06:27:20 -0800431
432void GrGLSLProgramBuilder::nameVariable(SkString* out, char prefix, const char* name, bool mangle) {
433 if ('\0' == prefix) {
434 *out = name;
435 } else {
436 out->printf("%c%s", prefix, name);
437 }
438 if (mangle) {
439 if (out->endsWith('_')) {
440 // Names containing "__" are reserved.
441 out->append("x");
442 }
443 out->appendf("_Stage%d%s", fStageIndex, fFS.getMangleString().c_str());
444 }
445}
446
egdanielfa896322016-01-13 12:19:30 -0800447void GrGLSLProgramBuilder::nameExpression(GrGLSLExpr4* output, const char* baseName) {
448 // create var to hold stage result. If we already have a valid output name, just use that
449 // otherwise create a new mangled one. This name is only valid if we are reordering stages
450 // and have to tell stage exactly where to put its output.
451 SkString outName;
452 if (output->isValid()) {
453 outName = output->c_str();
454 } else {
455 this->nameVariable(&outName, '\0', baseName);
456 }
457 fFS.codeAppendf("vec4 %s;", outName.c_str());
458 *output = outName;
459}
460
cdalton5e58cee2016-02-11 12:49:47 -0800461void GrGLSLProgramBuilder::appendUniformDecls(GrShaderFlags visibility, SkString* out) const {
egdaniel7ea439b2015-12-03 09:20:44 -0800462 this->uniformHandler()->appendUniformDecls(visibility, out);
463}
464
Ethan Nicholas941e7e22016-12-12 15:33:30 -0500465void GrGLSLProgramBuilder::addRTHeightUniform(const char* name) {
egdaniel7ea439b2015-12-03 09:20:44 -0800466 SkASSERT(!fUniformHandles.fRTHeightUni.isValid());
467 GrGLSLUniformHandler* uniformHandler = this->uniformHandler();
468 fUniformHandles.fRTHeightUni =
cdalton5e58cee2016-02-11 12:49:47 -0800469 uniformHandler->internalAddUniformArray(kFragment_GrShaderFlag,
egdaniel7ea439b2015-12-03 09:20:44 -0800470 kFloat_GrSLType, kDefault_GrSLPrecision,
Ethan Nicholas941e7e22016-12-12 15:33:30 -0500471 name, false, 0, nullptr);
egdaniel8dcdedc2015-11-11 06:27:20 -0800472}
473
egdanielfa896322016-01-13 12:19:30 -0800474void GrGLSLProgramBuilder::cleanupFragmentProcessors() {
475 for (int i = 0; i < fFragmentProcessors.count(); ++i) {
476 delete fFragmentProcessors[i];
477 }
478}
479
egdaniel9f1d4152016-02-10 09:50:38 -0800480void GrGLSLProgramBuilder::finalizeShaders() {
481 this->varyingHandler()->finalize();
cdalton5e58cee2016-02-11 12:49:47 -0800482 fVS.finalize(kVertex_GrShaderFlag);
csmartdalton276cc412016-11-21 11:55:00 -0700483 if (this->primitiveProcessor().willUseGeoShader()) {
Brian Salomon94efbf52016-11-29 13:43:05 -0500484 SkASSERT(this->shaderCaps()->geometryShaderSupport());
csmartdalton276cc412016-11-21 11:55:00 -0700485 fGS.finalize(kGeometry_GrShaderFlag);
486 }
cdalton5e58cee2016-02-11 12:49:47 -0800487 fFS.finalize(kFragment_GrShaderFlag);
egdaniel9f1d4152016-02-10 09:50:38 -0800488}