blob: 0c1661d3b779b90330bf6de2bd1b51cd4b058e21 [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
egdanielfa896322016-01-13 12:19:30 -080010#include "GrPipeline.h"
Brian Salomon739c5bf2016-11-07 09:53:44 -050011#include "GrTexturePriv.h"
Brian Salomon99938a82016-11-21 13:41:08 -050012#include "glsl/GrGLSLCaps.h"
egdanielfa896322016-01-13 12:19:30 -080013#include "glsl/GrGLSLFragmentProcessor.h"
14#include "glsl/GrGLSLGeometryProcessor.h"
egdaniel9f1d4152016-02-10 09:50:38 -080015#include "glsl/GrGLSLVarying.h"
egdanielfa896322016-01-13 12:19:30 -080016#include "glsl/GrGLSLXferProcessor.h"
17
egdaniel8dcdedc2015-11-11 06:27:20 -080018const int GrGLSLProgramBuilder::kVarsPerBlock = 8;
19
egdaniel0e1853c2016-03-17 11:35:45 -070020GrGLSLProgramBuilder::GrGLSLProgramBuilder(const GrPipeline& pipeline,
21 const GrPrimitiveProcessor& primProc,
22 const GrProgramDesc& desc)
egdaniel8dcdedc2015-11-11 06:27:20 -080023 : fVS(this)
24 , fGS(this)
cdalton28f45b92016-03-07 13:58:26 -080025 , fFS(this)
egdaniel8dcdedc2015-11-11 06:27:20 -080026 , fStageIndex(-1)
egdaniel0e1853c2016-03-17 11:35:45 -070027 , fPipeline(pipeline)
28 , fPrimProc(primProc)
29 , fDesc(desc)
egdanielfa896322016-01-13 12:19:30 -080030 , fGeometryProcessor(nullptr)
cdalton9c3f1432016-03-11 10:07:37 -080031 , fXferProcessor(nullptr)
cdalton9c3f1432016-03-11 10:07:37 -080032 , fNumVertexSamplers(0)
33 , fNumGeometrySamplers(0)
Brian Salomonf9f45122016-11-29 11:59:17 -050034 , fNumFragmentSamplers(0)
35 , fNumVertexImageStorages(0)
36 , fNumGeometryImageStorages(0)
37 , fNumFragmentImageStorages(0) {
cdalton9c3f1432016-03-11 10:07:37 -080038}
39
40void GrGLSLProgramBuilder::addFeature(GrShaderFlags shaders,
41 uint32_t featureBit,
42 const char* extensionName) {
43 if (shaders & kVertex_GrShaderFlag) {
44 fVS.addFeature(featureBit, extensionName);
45 }
46 if (shaders & kGeometry_GrShaderFlag) {
csmartdalton276cc412016-11-21 11:55:00 -070047 SkASSERT(this->primitiveProcessor().willUseGeoShader());
cdalton9c3f1432016-03-11 10:07:37 -080048 fGS.addFeature(featureBit, extensionName);
49 }
50 if (shaders & kFragment_GrShaderFlag) {
51 fFS.addFeature(featureBit, extensionName);
52 }
egdanielfa896322016-01-13 12:19:30 -080053}
54
55bool GrGLSLProgramBuilder::emitAndInstallProcs(GrGLSLExpr4* inputColor,
cdalton9c3f1432016-03-11 10:07:37 -080056 GrGLSLExpr4* inputCoverage) {
egdanielfa896322016-01-13 12:19:30 -080057 // First we loop over all of the installed processors and collect coord transforms. These will
58 // be sent to the GrGLSLPrimitiveProcessor in its emitCode function
59 const GrPrimitiveProcessor& primProc = this->primitiveProcessor();
egdanielfa896322016-01-13 12:19:30 -080060
egdanielfa896322016-01-13 12:19:30 -080061 this->emitAndInstallPrimProc(primProc, inputColor, inputCoverage);
62
bsalomona624bf32016-09-20 09:12:47 -070063 this->emitAndInstallFragProcs(inputColor, inputCoverage);
halcanary9d524f22016-03-29 09:03:52 -070064 if (primProc.getPixelLocalStorageState() !=
ethannicholas22793252016-01-30 09:59:10 -080065 GrPixelLocalStorageState::kDraw_GrPixelLocalStorageState) {
halcanary9d524f22016-03-29 09:03:52 -070066 this->emitAndInstallXferProc(this->pipeline().getXferProcessor(), *inputColor,
ethannicholas22793252016-01-30 09:59:10 -080067 *inputCoverage, this->pipeline().ignoresCoverage(),
68 primProc.getPixelLocalStorageState());
69 this->emitFSOutputSwizzle(this->pipeline().getXferProcessor().hasSecondaryOutput());
70 }
cdalton9c3f1432016-03-11 10:07:37 -080071
Brian Salomonf9f45122016-11-29 11:59:17 -050072 return this->checkSamplerCounts() && this->checkImageStorageCounts();
egdanielfa896322016-01-13 12:19:30 -080073}
74
75void GrGLSLProgramBuilder::emitAndInstallPrimProc(const GrPrimitiveProcessor& proc,
76 GrGLSLExpr4* outputColor,
77 GrGLSLExpr4* outputCoverage) {
78 // Program builders have a bit of state we need to clear with each effect
79 AutoStageAdvance adv(this);
80 this->nameExpression(outputColor, "outputColor");
81 this->nameExpression(outputCoverage, "outputCoverage");
82
dvonbeck9b03e7b2016-08-01 11:01:56 -070083 const char* distanceVectorName = nullptr;
84 if (this->fPipeline.usesDistanceVectorField() && proc.implementsDistanceVector()) {
robertphillips05a4cf52016-09-08 09:02:43 -070085 // Each individual user (FP) of the distance vector must be able to handle having this
86 // variable be undeclared. There is no single default value that will yield a reasonable
87 // result for all users.
dvonbeck9b03e7b2016-08-01 11:01:56 -070088 distanceVectorName = fFS.distanceVectorName();
dvonbeckee920632016-08-11 14:17:59 -070089 fFS.codeAppend( "// Normalized vector to the closest geometric edge (in device space)\n");
dvonbeck84bca782016-08-08 11:47:12 -070090 fFS.codeAppend( "// Distance to the edge encoded in the z-component\n");
jvanverth6c177a12016-08-17 07:59:41 -070091 fFS.codeAppendf("vec4 %s;", distanceVectorName);
dvonbeck9b03e7b2016-08-01 11:01:56 -070092 }
93
egdanielfa896322016-01-13 12:19:30 -080094 // Enclose custom code in a block to avoid namespace conflicts
95 SkString openBrace;
96 openBrace.printf("{ // Stage %d, %s\n", fStageIndex, proc.name());
97 fFS.codeAppend(openBrace.c_str());
98 fVS.codeAppendf("// Primitive Processor %s\n", proc.name());
99
100 SkASSERT(!fGeometryProcessor);
101 fGeometryProcessor = proc.createGLSLInstance(*this->glslCaps());
102
Brian Salomonf9f45122016-11-29 11:59:17 -0500103 SkSTArray<4, SamplerHandle> texSamplers(proc.numTextureSamplers());
104 SkSTArray<2, SamplerHandle> bufferSamplers(proc.numBuffers());
105 SkSTArray<2, ImageStorageHandle> imageStorages(proc.numImageStorages());
106 this->emitSamplersAndImageStorages(proc, &texSamplers, &bufferSamplers, &imageStorages);
egdanielfa896322016-01-13 12:19:30 -0800107
bsalomona624bf32016-09-20 09:12:47 -0700108 GrGLSLPrimitiveProcessor::FPCoordTransformHandler transformHandler(fPipeline,
109 &fTransformedCoordVars);
egdanielfa896322016-01-13 12:19:30 -0800110 GrGLSLGeometryProcessor::EmitArgs args(&fVS,
csmartdalton276cc412016-11-21 11:55:00 -0700111 proc.willUseGeoShader() ? &fGS : nullptr,
egdanielfa896322016-01-13 12:19:30 -0800112 &fFS,
113 this->varyingHandler(),
114 this->uniformHandler(),
115 this->glslCaps(),
116 proc,
117 outputColor->c_str(),
118 outputCoverage->c_str(),
dvonbeck9b03e7b2016-08-01 11:01:56 -0700119 distanceVectorName,
egdaniel09aa1fc2016-04-20 07:09:46 -0700120 texSamplers.begin(),
121 bufferSamplers.begin(),
Brian Salomonf9f45122016-11-29 11:59:17 -0500122 imageStorages.begin(),
bsalomona624bf32016-09-20 09:12:47 -0700123 &transformHandler);
egdanielfa896322016-01-13 12:19:30 -0800124 fGeometryProcessor->emitCode(args);
125
126 // We have to check that effects and the code they emit are consistent, ie if an effect
127 // asks for dst color, then the emit code needs to follow suit
cdalton87332102016-02-26 12:22:02 -0800128 SkDEBUGCODE(verify(proc);)
egdanielfa896322016-01-13 12:19:30 -0800129
130 fFS.codeAppend("}");
131}
132
bsalomona624bf32016-09-20 09:12:47 -0700133void GrGLSLProgramBuilder::emitAndInstallFragProcs(GrGLSLExpr4* color, GrGLSLExpr4* coverage) {
134 int transformedCoordVarsIdx = 0;
135 GrGLSLExpr4** inOut = &color;
136 for (int i = 0; i < this->pipeline().numFragmentProcessors(); ++i) {
137 if (i == this->pipeline().numColorFragmentProcessors()) {
138 inOut = &coverage;
139 }
egdanielfa896322016-01-13 12:19:30 -0800140 GrGLSLExpr4 output;
141 const GrFragmentProcessor& fp = this->pipeline().getFragmentProcessor(i);
bsalomona624bf32016-09-20 09:12:47 -0700142 this->emitAndInstallFragProc(fp, i, transformedCoordVarsIdx, **inOut, &output);
143 GrFragmentProcessor::Iter iter(&fp);
144 while (const GrFragmentProcessor* fp = iter.next()) {
145 transformedCoordVarsIdx += fp->numCoordTransforms();
146 }
147 **inOut = output;
egdanielfa896322016-01-13 12:19:30 -0800148 }
149}
150
151// TODO Processors cannot output zeros because an empty string is all 1s
152// the fix is to allow effects to take the GrGLSLExpr4 directly
153void GrGLSLProgramBuilder::emitAndInstallFragProc(const GrFragmentProcessor& fp,
154 int index,
bsalomona624bf32016-09-20 09:12:47 -0700155 int transformedCoordVarsIdx,
egdanielfa896322016-01-13 12:19:30 -0800156 const GrGLSLExpr4& input,
157 GrGLSLExpr4* output) {
158 // Program builders have a bit of state we need to clear with each effect
159 AutoStageAdvance adv(this);
160 this->nameExpression(output, "output");
161
162 // Enclose custom code in a block to avoid namespace conflicts
163 SkString openBrace;
164 openBrace.printf("{ // Stage %d, %s\n", fStageIndex, fp.name());
165 fFS.codeAppend(openBrace.c_str());
166
167 GrGLSLFragmentProcessor* fragProc = fp.createGLSLInstance();
168
Brian Salomon0bbecb22016-11-17 11:38:22 -0500169 SkSTArray<4, SamplerHandle> textureSamplerArray(fp.numTextureSamplers());
bsalomonb58a2b42016-09-26 06:55:02 -0700170 SkSTArray<2, SamplerHandle> bufferSamplerArray(fp.numBuffers());
Brian Salomonf9f45122016-11-29 11:59:17 -0500171 SkSTArray<2, ImageStorageHandle> imageStorageArray(fp.numImageStorages());
bsalomonb58a2b42016-09-26 06:55:02 -0700172 GrFragmentProcessor::Iter iter(&fp);
173 while (const GrFragmentProcessor* subFP = iter.next()) {
Brian Salomonf9f45122016-11-29 11:59:17 -0500174 this->emitSamplersAndImageStorages(*subFP, &textureSamplerArray, &bufferSamplerArray,
175 &imageStorageArray);
bsalomonb58a2b42016-09-26 06:55:02 -0700176 }
egdanielfa896322016-01-13 12:19:30 -0800177
bsalomona624bf32016-09-20 09:12:47 -0700178 const GrShaderVar* coordVars = fTransformedCoordVars.begin() + transformedCoordVarsIdx;
179 GrGLSLFragmentProcessor::TransformedCoordVars coords(&fp, coordVars);
bsalomonb58a2b42016-09-26 06:55:02 -0700180 GrGLSLFragmentProcessor::TextureSamplers textureSamplers(&fp, textureSamplerArray.begin());
181 GrGLSLFragmentProcessor::BufferSamplers bufferSamplers(&fp, bufferSamplerArray.begin());
Brian Salomonf9f45122016-11-29 11:59:17 -0500182 GrGLSLFragmentProcessor::ImageStorages imageStorages(&fp, imageStorageArray.begin());
egdanielfa896322016-01-13 12:19:30 -0800183 GrGLSLFragmentProcessor::EmitArgs args(&fFS,
184 this->uniformHandler(),
185 this->glslCaps(),
186 fp,
187 output->c_str(),
188 input.isOnes() ? nullptr : input.c_str(),
bsalomona624bf32016-09-20 09:12:47 -0700189 coords,
bsalomonb58a2b42016-09-26 06:55:02 -0700190 textureSamplers,
191 bufferSamplers,
Brian Salomonf9f45122016-11-29 11:59:17 -0500192 imageStorages,
dvonbeck9b03e7b2016-08-01 11:01:56 -0700193 this->primitiveProcessor().implementsDistanceVector());
194
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);)
egdanielfa896322016-01-13 12:19:30 -0800200 fFragmentProcessors.push_back(fragProc);
201
202 fFS.codeAppend("}");
203}
204
205void GrGLSLProgramBuilder::emitAndInstallXferProc(const GrXferProcessor& xp,
206 const GrGLSLExpr4& colorIn,
207 const GrGLSLExpr4& coverageIn,
ethannicholas22793252016-01-30 09:59:10 -0800208 bool ignoresCoverage,
209 GrPixelLocalStorageState plsState) {
egdanielfa896322016-01-13 12:19:30 -0800210 // Program builders have a bit of state we need to clear with each effect
211 AutoStageAdvance adv(this);
212
213 SkASSERT(!fXferProcessor);
214 fXferProcessor = xp.createGLSLInstance();
215
216 // Enable dual source secondary output if we have one
217 if (xp.hasSecondaryOutput()) {
218 fFS.enableSecondaryOutput();
219 }
220
221 if (this->glslCaps()->mustDeclareFragmentShaderOutput()) {
222 fFS.enableCustomOutput();
223 }
224
225 SkString openBrace;
226 openBrace.printf("{ // Xfer Processor: %s\n", xp.name());
227 fFS.codeAppend(openBrace.c_str());
228
Brian Salomonf9f45122016-11-29 11:59:17 -0500229 SkSTArray<4, SamplerHandle> texSamplers(xp.numTextureSamplers());
230 SkSTArray<2, SamplerHandle> bufferSamplers(xp.numBuffers());
231 SkSTArray<2, ImageStorageHandle> imageStorageArray(xp.numImageStorages());
232 this->emitSamplersAndImageStorages(xp, &texSamplers, &bufferSamplers, &imageStorageArray);
egdanielfa896322016-01-13 12:19:30 -0800233
ethannicholas22793252016-01-30 09:59:10 -0800234 bool usePLSDstRead = (plsState == GrPixelLocalStorageState::kFinish_GrPixelLocalStorageState);
egdanielfa896322016-01-13 12:19:30 -0800235 GrGLSLXferProcessor::EmitArgs args(&fFS,
236 this->uniformHandler(),
237 this->glslCaps(),
238 xp, colorIn.c_str(),
239 ignoresCoverage ? nullptr : coverageIn.c_str(),
240 fFS.getPrimaryColorOutputName(),
241 fFS.getSecondaryColorOutputName(),
egdaniel09aa1fc2016-04-20 07:09:46 -0700242 texSamplers.begin(),
243 bufferSamplers.begin(),
Brian Salomonf9f45122016-11-29 11:59:17 -0500244 imageStorageArray.begin(),
ethannicholas22793252016-01-30 09:59:10 -0800245 usePLSDstRead);
egdanielfa896322016-01-13 12:19:30 -0800246 fXferProcessor->emitCode(args);
247
248 // We have to check that effects and the code they emit are consistent, ie if an effect
249 // asks for dst color, then the emit code needs to follow suit
cdalton87332102016-02-26 12:22:02 -0800250 SkDEBUGCODE(verify(xp);)
egdanielfa896322016-01-13 12:19:30 -0800251 fFS.codeAppend("}");
252}
253
Brian Salomonf9f45122016-11-29 11:59:17 -0500254void GrGLSLProgramBuilder::emitSamplersAndImageStorages(
255 const GrProcessor& processor,
256 SkTArray<SamplerHandle>* outTexSamplerHandles,
257 SkTArray<SamplerHandle>* outBufferSamplerHandles,
258 SkTArray<ImageStorageHandle>* outImageStorageHandles) {
cdalton9c3f1432016-03-11 10:07:37 -0800259 SkString name;
Brian Salomon0bbecb22016-11-17 11:38:22 -0500260 int numTextureSamplers = processor.numTextureSamplers();
261 for (int t = 0; t < numTextureSamplers; ++t) {
262 const GrProcessor::TextureSampler& sampler = processor.textureSampler(t);
Brian Salomonf9f45122016-11-29 11:59:17 -0500263 name.printf("TextureSampler_%d", outTexSamplerHandles->count());
Brian Salomondb4183d2016-11-17 12:48:40 -0500264 GrSLType samplerType = sampler.texture()->texturePriv().samplerType();
egdaniel990dbc82016-07-13 14:09:30 -0700265 if (kTextureExternalSampler_GrSLType == samplerType) {
cdalton9c3f1432016-03-11 10:07:37 -0800266 const char* externalFeatureString = this->glslCaps()->externalTextureExtensionString();
267 // We shouldn't ever create a GrGLTexture that requires external sampler type
268 SkASSERT(externalFeatureString);
Brian Salomondb4183d2016-11-17 12:48:40 -0500269 this->addFeature(sampler.visibility(),
cdalton9c3f1432016-03-11 10:07:37 -0800270 1 << GrGLSLShaderBuilder::kExternalTexture_GLSLPrivateFeature,
271 externalFeatureString);
272 }
Brian Salomonf9f45122016-11-29 11:59:17 -0500273 this->emitSampler(samplerType, sampler.texture()->config(), name.c_str(),
274 sampler.visibility(), outTexSamplerHandles);
275
cdalton9c3f1432016-03-11 10:07:37 -0800276 }
cdalton74b8d322016-04-11 14:47:28 -0700277
278 if (int numBuffers = processor.numBuffers()) {
279 SkASSERT(this->glslCaps()->texelBufferSupport());
280 GrShaderFlags texelBufferVisibility = kNone_GrShaderFlags;
281
282 for (int b = 0; b < numBuffers; ++b) {
Brian Salomonb014cca2016-11-18 11:39:15 -0500283 const GrProcessor::BufferAccess& access = processor.bufferAccess(b);
Brian Salomonf9f45122016-11-29 11:59:17 -0500284 name.printf("BufferSampler_%d", outBufferSamplerHandles->count());
csmartdalton22458032016-11-16 11:28:16 -0700285 this->emitSampler(kBufferSampler_GrSLType, access.texelConfig(), name.c_str(),
Brian Salomonf9f45122016-11-29 11:59:17 -0500286 access.visibility(), outBufferSamplerHandles);
cdalton74b8d322016-04-11 14:47:28 -0700287 texelBufferVisibility |= access.visibility();
288 }
289
290 if (const char* extension = this->glslCaps()->texelBufferExtensionString()) {
291 this->addFeature(texelBufferVisibility,
292 1 << GrGLSLShaderBuilder::kTexelBuffer_GLSLPrivateFeature,
293 extension);
294 }
295 }
Brian Salomonf9f45122016-11-29 11:59:17 -0500296 int numImageStorages = processor.numImageStorages();
297 for (int i = 0; i < numImageStorages; ++i) {
298 const GrProcessor::ImageStorageAccess& imageStorageAccess = processor.imageStorageAccess(i);
299 name.printf("Image_%d", outImageStorageHandles->count());
300 this->emitImageStorage(imageStorageAccess, name.c_str(), outImageStorageHandles);
301 }
cdalton74b8d322016-04-11 14:47:28 -0700302}
303
304void GrGLSLProgramBuilder::emitSampler(GrSLType samplerType,
305 GrPixelConfig config,
306 const char* name,
307 GrShaderFlags visibility,
Brian Salomonf9f45122016-11-29 11:59:17 -0500308 SkTArray<SamplerHandle>* outSamplerHandles) {
cdalton74b8d322016-04-11 14:47:28 -0700309 if (visibility & kVertex_GrShaderFlag) {
310 ++fNumVertexSamplers;
311 }
312 if (visibility & kGeometry_GrShaderFlag) {
313 SkASSERT(this->primitiveProcessor().willUseGeoShader());
314 ++fNumGeometrySamplers;
315 }
316 if (visibility & kFragment_GrShaderFlag) {
317 ++fNumFragmentSamplers;
318 }
319 GrSLPrecision precision = this->glslCaps()->samplerPrecision(config, visibility);
Brian Salomon101b8442016-11-18 11:58:54 -0500320 GrSwizzle swizzle = this->glslCaps()->configTextureSwizzle(config);
Brian Salomonf9f45122016-11-29 11:59:17 -0500321 outSamplerHandles->emplace_back(this->uniformHandler()->addSampler(visibility,
322 swizzle,
323 samplerType,
324 precision,
325 name));
326}
327
328void GrGLSLProgramBuilder::emitImageStorage(const GrProcessor::ImageStorageAccess& access,
329 const char* name,
330 SkTArray<ImageStorageHandle>* outImageStorageHandles) {
331 if (access.visibility() & kVertex_GrShaderFlag) {
332 ++fNumVertexImageStorages;
333 }
334 if (access.visibility() & kGeometry_GrShaderFlag) {
335 SkASSERT(this->primitiveProcessor().willUseGeoShader());
336 ++fNumGeometryImageStorages;
337 }
338 if (access.visibility() & kFragment_GrShaderFlag) {
339 ++fNumFragmentImageStorages;
340 }
341 GrSLType uniformType = access.texture()->texturePriv().imageStorageType();
342 ImageStorageHandle handle = this->uniformHandler()->addImageStorage(access.visibility(),
343 uniformType, access.format(), access.memoryModel(), access.restrict(), access.ioType(),
344 name);
345 outImageStorageHandles->emplace_back(handle);
cdalton9c3f1432016-03-11 10:07:37 -0800346}
347
egdanielfa896322016-01-13 12:19:30 -0800348void GrGLSLProgramBuilder::emitFSOutputSwizzle(bool hasSecondaryOutput) {
349 // Swizzle the fragment shader outputs if necessary.
350 GrSwizzle swizzle;
351 swizzle.setFromKey(this->desc().header().fOutputSwizzle);
352 if (swizzle != GrSwizzle::RGBA()) {
353 fFS.codeAppendf("%s = %s.%s;", fFS.getPrimaryColorOutputName(),
354 fFS.getPrimaryColorOutputName(),
355 swizzle.c_str());
356 if (hasSecondaryOutput) {
357 fFS.codeAppendf("%s = %s.%s;", fFS.getSecondaryColorOutputName(),
358 fFS.getSecondaryColorOutputName(),
359 swizzle.c_str());
360 }
361 }
362}
363
cdalton9c3f1432016-03-11 10:07:37 -0800364bool GrGLSLProgramBuilder::checkSamplerCounts() {
365 const GrGLSLCaps& glslCaps = *this->glslCaps();
366 if (fNumVertexSamplers > glslCaps.maxVertexSamplers()) {
367 GrCapsDebugf(this->caps(), "Program would use too many vertex samplers\n");
368 return false;
369 }
370 if (fNumGeometrySamplers > glslCaps.maxGeometrySamplers()) {
371 GrCapsDebugf(this->caps(), "Program would use too many geometry samplers\n");
372 return false;
373 }
374 if (fNumFragmentSamplers > glslCaps.maxFragmentSamplers()) {
375 GrCapsDebugf(this->caps(), "Program would use too many fragment samplers\n");
376 return false;
377 }
378 // If the same sampler is used in two different shaders, it counts as two combined samplers.
379 int numCombinedSamplers = fNumVertexSamplers + fNumGeometrySamplers + fNumFragmentSamplers;
380 if (numCombinedSamplers > glslCaps.maxCombinedSamplers()) {
381 GrCapsDebugf(this->caps(), "Program would use too many combined samplers\n");
382 return false;
383 }
384 return true;
385}
386
Brian Salomonf9f45122016-11-29 11:59:17 -0500387bool GrGLSLProgramBuilder::checkImageStorageCounts() {
388 const GrGLSLCaps& glslCaps = *this->glslCaps();
389 if (fNumVertexImageStorages > glslCaps.maxVertexImageStorages()) {
390 GrCapsDebugf(this->caps(), "Program would use too many vertex images\n");
391 return false;
392 }
393 if (fNumGeometryImageStorages > glslCaps.maxGeometryImageStorages()) {
394 GrCapsDebugf(this->caps(), "Program would use too many geometry images\n");
395 return false;
396 }
397 if (fNumFragmentImageStorages > glslCaps.maxFragmentImageStorages()) {
398 GrCapsDebugf(this->caps(), "Program would use too many fragment images\n");
399 return false;
400 }
401 // If the same image is used in two different shaders, it counts as two combined images.
402 int numCombinedImages = fNumVertexImageStorages + fNumGeometryImageStorages +
403 fNumFragmentImageStorages;
404 if (numCombinedImages > glslCaps.maxCombinedImageStorages()) {
405 GrCapsDebugf(this->caps(), "Program would use too many combined images\n");
406 return false;
407 }
408 return true;
409}
410
cdalton87332102016-02-26 12:22:02 -0800411#ifdef SK_DEBUG
egdanielfa896322016-01-13 12:19:30 -0800412void GrGLSLProgramBuilder::verify(const GrPrimitiveProcessor& gp) {
cdalton87332102016-02-26 12:22:02 -0800413 SkASSERT(fFS.usedProcessorFeatures() == gp.requiredFeatures());
egdanielfa896322016-01-13 12:19:30 -0800414}
415
416void GrGLSLProgramBuilder::verify(const GrXferProcessor& xp) {
cdalton87332102016-02-26 12:22:02 -0800417 SkASSERT(fFS.usedProcessorFeatures() == xp.requiredFeatures());
egdanielfa896322016-01-13 12:19:30 -0800418 SkASSERT(fFS.hasReadDstColor() == xp.willReadDstColor());
419}
420
421void GrGLSLProgramBuilder::verify(const GrFragmentProcessor& fp) {
cdalton87332102016-02-26 12:22:02 -0800422 SkASSERT(fFS.usedProcessorFeatures() == fp.requiredFeatures());
egdaniel8dcdedc2015-11-11 06:27:20 -0800423}
cdalton87332102016-02-26 12:22:02 -0800424#endif
egdaniel8dcdedc2015-11-11 06:27:20 -0800425
426void GrGLSLProgramBuilder::nameVariable(SkString* out, char prefix, const char* name, bool mangle) {
427 if ('\0' == prefix) {
428 *out = name;
429 } else {
430 out->printf("%c%s", prefix, name);
431 }
432 if (mangle) {
433 if (out->endsWith('_')) {
434 // Names containing "__" are reserved.
435 out->append("x");
436 }
437 out->appendf("_Stage%d%s", fStageIndex, fFS.getMangleString().c_str());
438 }
439}
440
egdanielfa896322016-01-13 12:19:30 -0800441void GrGLSLProgramBuilder::nameExpression(GrGLSLExpr4* output, const char* baseName) {
442 // create var to hold stage result. If we already have a valid output name, just use that
443 // otherwise create a new mangled one. This name is only valid if we are reordering stages
444 // and have to tell stage exactly where to put its output.
445 SkString outName;
446 if (output->isValid()) {
447 outName = output->c_str();
448 } else {
449 this->nameVariable(&outName, '\0', baseName);
450 }
451 fFS.codeAppendf("vec4 %s;", outName.c_str());
452 *output = outName;
453}
454
cdalton5e58cee2016-02-11 12:49:47 -0800455void GrGLSLProgramBuilder::appendUniformDecls(GrShaderFlags visibility, SkString* out) const {
egdaniel7ea439b2015-12-03 09:20:44 -0800456 this->uniformHandler()->appendUniformDecls(visibility, out);
457}
458
459void GrGLSLProgramBuilder::addRTAdjustmentUniform(GrSLPrecision precision,
460 const char* name,
461 const char** outName) {
462 SkASSERT(!fUniformHandles.fRTAdjustmentUni.isValid());
463 fUniformHandles.fRTAdjustmentUni =
cdalton5e58cee2016-02-11 12:49:47 -0800464 this->uniformHandler()->addUniform(kVertex_GrShaderFlag,
egdaniel7ea439b2015-12-03 09:20:44 -0800465 kVec4f_GrSLType,
466 precision,
467 name,
468 outName);
469}
470
471void GrGLSLProgramBuilder::addRTHeightUniform(const char* name, const char** outName) {
472 SkASSERT(!fUniformHandles.fRTHeightUni.isValid());
473 GrGLSLUniformHandler* uniformHandler = this->uniformHandler();
474 fUniformHandles.fRTHeightUni =
cdalton5e58cee2016-02-11 12:49:47 -0800475 uniformHandler->internalAddUniformArray(kFragment_GrShaderFlag,
egdaniel7ea439b2015-12-03 09:20:44 -0800476 kFloat_GrSLType, kDefault_GrSLPrecision,
477 name, false, 0, outName);
egdaniel8dcdedc2015-11-11 06:27:20 -0800478}
479
egdanielfa896322016-01-13 12:19:30 -0800480void GrGLSLProgramBuilder::cleanupFragmentProcessors() {
481 for (int i = 0; i < fFragmentProcessors.count(); ++i) {
482 delete fFragmentProcessors[i];
483 }
484}
485
egdaniel9f1d4152016-02-10 09:50:38 -0800486void GrGLSLProgramBuilder::finalizeShaders() {
487 this->varyingHandler()->finalize();
cdalton5e58cee2016-02-11 12:49:47 -0800488 fVS.finalize(kVertex_GrShaderFlag);
csmartdalton276cc412016-11-21 11:55:00 -0700489 if (this->primitiveProcessor().willUseGeoShader()) {
490 SkASSERT(this->glslCaps()->geometryShaderSupport());
491 fGS.finalize(kGeometry_GrShaderFlag);
492 }
cdalton5e58cee2016-02-11 12:49:47 -0800493 fFS.finalize(kFragment_GrShaderFlag);
egdaniel9f1d4152016-02-10 09:50:38 -0800494}