blob: 385026a52d6789a869320f544a82a9ceb46da392 [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);
halcanary9d524f22016-03-29 09:03:52 -070065 if (primProc.getPixelLocalStorageState() !=
ethannicholas22793252016-01-30 09:59:10 -080066 GrPixelLocalStorageState::kDraw_GrPixelLocalStorageState) {
halcanary9d524f22016-03-29 09:03:52 -070067 this->emitAndInstallXferProc(this->pipeline().getXferProcessor(), *inputColor,
Brian Salomon8c852be2017-01-04 10:44:42 -050068 *inputCoverage, primProc.getPixelLocalStorageState());
ethannicholas22793252016-01-30 09:59:10 -080069 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
csmartdalton936f81b2017-02-13 15:45:35 -070094 SkASSERT(!fUniformHandles.fRTAdjustmentUni.isValid());
95 GrShaderFlags rtAdjustVisibility = kVertex_GrShaderFlag;
96 if (proc.willUseGeoShader()) {
97 rtAdjustVisibility |= kGeometry_GrShaderFlag;
98 }
99 fUniformHandles.fRTAdjustmentUni = this->uniformHandler()->addUniform(rtAdjustVisibility,
100 kVec4f_GrSLType,
101 kHigh_GrSLPrecision,
102 "rtAdjustment");
103 const char* rtAdjustName =
104 this->uniformHandler()->getUniformCStr(fUniformHandles.fRTAdjustmentUni);
105
egdanielfa896322016-01-13 12:19:30 -0800106 // Enclose custom code in a block to avoid namespace conflicts
107 SkString openBrace;
108 openBrace.printf("{ // Stage %d, %s\n", fStageIndex, proc.name());
109 fFS.codeAppend(openBrace.c_str());
110 fVS.codeAppendf("// Primitive Processor %s\n", proc.name());
111
112 SkASSERT(!fGeometryProcessor);
Brian Salomon94efbf52016-11-29 13:43:05 -0500113 fGeometryProcessor = proc.createGLSLInstance(*this->shaderCaps());
egdanielfa896322016-01-13 12:19:30 -0800114
Brian Salomonf9f45122016-11-29 11:59:17 -0500115 SkSTArray<4, SamplerHandle> texSamplers(proc.numTextureSamplers());
116 SkSTArray<2, SamplerHandle> bufferSamplers(proc.numBuffers());
117 SkSTArray<2, ImageStorageHandle> imageStorages(proc.numImageStorages());
118 this->emitSamplersAndImageStorages(proc, &texSamplers, &bufferSamplers, &imageStorages);
egdanielfa896322016-01-13 12:19:30 -0800119
bsalomona624bf32016-09-20 09:12:47 -0700120 GrGLSLPrimitiveProcessor::FPCoordTransformHandler transformHandler(fPipeline,
121 &fTransformedCoordVars);
egdanielfa896322016-01-13 12:19:30 -0800122 GrGLSLGeometryProcessor::EmitArgs args(&fVS,
csmartdalton276cc412016-11-21 11:55:00 -0700123 proc.willUseGeoShader() ? &fGS : nullptr,
egdanielfa896322016-01-13 12:19:30 -0800124 &fFS,
125 this->varyingHandler(),
126 this->uniformHandler(),
Brian Salomon94efbf52016-11-29 13:43:05 -0500127 this->shaderCaps(),
egdanielfa896322016-01-13 12:19:30 -0800128 proc,
129 outputColor->c_str(),
130 outputCoverage->c_str(),
dvonbeck9b03e7b2016-08-01 11:01:56 -0700131 distanceVectorName,
csmartdalton936f81b2017-02-13 15:45:35 -0700132 rtAdjustName,
egdaniel09aa1fc2016-04-20 07:09:46 -0700133 texSamplers.begin(),
134 bufferSamplers.begin(),
Brian Salomonf9f45122016-11-29 11:59:17 -0500135 imageStorages.begin(),
bsalomona624bf32016-09-20 09:12:47 -0700136 &transformHandler);
egdanielfa896322016-01-13 12:19:30 -0800137 fGeometryProcessor->emitCode(args);
138
139 // We have to check that effects and the code they emit are consistent, ie if an effect
140 // asks for dst color, then the emit code needs to follow suit
cdalton87332102016-02-26 12:22:02 -0800141 SkDEBUGCODE(verify(proc);)
egdanielfa896322016-01-13 12:19:30 -0800142
143 fFS.codeAppend("}");
144}
145
bsalomona624bf32016-09-20 09:12:47 -0700146void GrGLSLProgramBuilder::emitAndInstallFragProcs(GrGLSLExpr4* color, GrGLSLExpr4* coverage) {
147 int transformedCoordVarsIdx = 0;
148 GrGLSLExpr4** inOut = &color;
149 for (int i = 0; i < this->pipeline().numFragmentProcessors(); ++i) {
150 if (i == this->pipeline().numColorFragmentProcessors()) {
151 inOut = &coverage;
152 }
egdanielfa896322016-01-13 12:19:30 -0800153 GrGLSLExpr4 output;
154 const GrFragmentProcessor& fp = this->pipeline().getFragmentProcessor(i);
bsalomona624bf32016-09-20 09:12:47 -0700155 this->emitAndInstallFragProc(fp, i, transformedCoordVarsIdx, **inOut, &output);
156 GrFragmentProcessor::Iter iter(&fp);
157 while (const GrFragmentProcessor* fp = iter.next()) {
158 transformedCoordVarsIdx += fp->numCoordTransforms();
159 }
160 **inOut = output;
egdanielfa896322016-01-13 12:19:30 -0800161 }
162}
163
164// TODO Processors cannot output zeros because an empty string is all 1s
165// the fix is to allow effects to take the GrGLSLExpr4 directly
166void GrGLSLProgramBuilder::emitAndInstallFragProc(const GrFragmentProcessor& fp,
167 int index,
bsalomona624bf32016-09-20 09:12:47 -0700168 int transformedCoordVarsIdx,
egdanielfa896322016-01-13 12:19:30 -0800169 const GrGLSLExpr4& input,
170 GrGLSLExpr4* output) {
171 // Program builders have a bit of state we need to clear with each effect
172 AutoStageAdvance adv(this);
173 this->nameExpression(output, "output");
174
175 // Enclose custom code in a block to avoid namespace conflicts
176 SkString openBrace;
177 openBrace.printf("{ // Stage %d, %s\n", fStageIndex, fp.name());
178 fFS.codeAppend(openBrace.c_str());
179
180 GrGLSLFragmentProcessor* fragProc = fp.createGLSLInstance();
181
Brian Salomon0bbecb22016-11-17 11:38:22 -0500182 SkSTArray<4, SamplerHandle> textureSamplerArray(fp.numTextureSamplers());
bsalomonb58a2b42016-09-26 06:55:02 -0700183 SkSTArray<2, SamplerHandle> bufferSamplerArray(fp.numBuffers());
Brian Salomonf9f45122016-11-29 11:59:17 -0500184 SkSTArray<2, ImageStorageHandle> imageStorageArray(fp.numImageStorages());
bsalomonb58a2b42016-09-26 06:55:02 -0700185 GrFragmentProcessor::Iter iter(&fp);
186 while (const GrFragmentProcessor* subFP = iter.next()) {
Brian Salomonf9f45122016-11-29 11:59:17 -0500187 this->emitSamplersAndImageStorages(*subFP, &textureSamplerArray, &bufferSamplerArray,
188 &imageStorageArray);
bsalomonb58a2b42016-09-26 06:55:02 -0700189 }
egdanielfa896322016-01-13 12:19:30 -0800190
bsalomona624bf32016-09-20 09:12:47 -0700191 const GrShaderVar* coordVars = fTransformedCoordVars.begin() + transformedCoordVarsIdx;
192 GrGLSLFragmentProcessor::TransformedCoordVars coords(&fp, coordVars);
bsalomonb58a2b42016-09-26 06:55:02 -0700193 GrGLSLFragmentProcessor::TextureSamplers textureSamplers(&fp, textureSamplerArray.begin());
194 GrGLSLFragmentProcessor::BufferSamplers bufferSamplers(&fp, bufferSamplerArray.begin());
Brian Salomonf9f45122016-11-29 11:59:17 -0500195 GrGLSLFragmentProcessor::ImageStorages imageStorages(&fp, imageStorageArray.begin());
egdanielfa896322016-01-13 12:19:30 -0800196 GrGLSLFragmentProcessor::EmitArgs args(&fFS,
197 this->uniformHandler(),
Brian Salomon94efbf52016-11-29 13:43:05 -0500198 this->shaderCaps(),
egdanielfa896322016-01-13 12:19:30 -0800199 fp,
200 output->c_str(),
201 input.isOnes() ? nullptr : input.c_str(),
bsalomona624bf32016-09-20 09:12:47 -0700202 coords,
bsalomonb58a2b42016-09-26 06:55:02 -0700203 textureSamplers,
204 bufferSamplers,
Brian Salomonf9f45122016-11-29 11:59:17 -0500205 imageStorages,
dvonbeck9b03e7b2016-08-01 11:01:56 -0700206 this->primitiveProcessor().implementsDistanceVector());
207
egdanielfa896322016-01-13 12:19:30 -0800208 fragProc->emitCode(args);
209
210 // We have to check that effects and the code they emit are consistent, ie if an effect
211 // asks for dst color, then the emit code needs to follow suit
cdalton87332102016-02-26 12:22:02 -0800212 SkDEBUGCODE(verify(fp);)
egdanielfa896322016-01-13 12:19:30 -0800213 fFragmentProcessors.push_back(fragProc);
214
215 fFS.codeAppend("}");
216}
217
218void GrGLSLProgramBuilder::emitAndInstallXferProc(const GrXferProcessor& xp,
219 const GrGLSLExpr4& colorIn,
220 const GrGLSLExpr4& coverageIn,
ethannicholas22793252016-01-30 09:59:10 -0800221 GrPixelLocalStorageState plsState) {
egdanielfa896322016-01-13 12:19:30 -0800222 // Program builders have a bit of state we need to clear with each effect
223 AutoStageAdvance adv(this);
224
225 SkASSERT(!fXferProcessor);
226 fXferProcessor = xp.createGLSLInstance();
227
228 // Enable dual source secondary output if we have one
229 if (xp.hasSecondaryOutput()) {
230 fFS.enableSecondaryOutput();
231 }
232
Brian Salomon94efbf52016-11-29 13:43:05 -0500233 if (this->shaderCaps()->mustDeclareFragmentShaderOutput()) {
egdanielfa896322016-01-13 12:19:30 -0800234 fFS.enableCustomOutput();
235 }
236
237 SkString openBrace;
238 openBrace.printf("{ // Xfer Processor: %s\n", xp.name());
239 fFS.codeAppend(openBrace.c_str());
240
Brian Salomonf9f45122016-11-29 11:59:17 -0500241 SkSTArray<4, SamplerHandle> texSamplers(xp.numTextureSamplers());
242 SkSTArray<2, SamplerHandle> bufferSamplers(xp.numBuffers());
243 SkSTArray<2, ImageStorageHandle> imageStorageArray(xp.numImageStorages());
244 this->emitSamplersAndImageStorages(xp, &texSamplers, &bufferSamplers, &imageStorageArray);
egdanielfa896322016-01-13 12:19:30 -0800245
ethannicholas22793252016-01-30 09:59:10 -0800246 bool usePLSDstRead = (plsState == GrPixelLocalStorageState::kFinish_GrPixelLocalStorageState);
egdanielfa896322016-01-13 12:19:30 -0800247 GrGLSLXferProcessor::EmitArgs args(&fFS,
248 this->uniformHandler(),
Brian Salomon94efbf52016-11-29 13:43:05 -0500249 this->shaderCaps(),
egdanielfa896322016-01-13 12:19:30 -0800250 xp, colorIn.c_str(),
Brian Salomon8c852be2017-01-04 10:44:42 -0500251 coverageIn.c_str(),
egdanielfa896322016-01-13 12:19:30 -0800252 fFS.getPrimaryColorOutputName(),
253 fFS.getSecondaryColorOutputName(),
egdaniel09aa1fc2016-04-20 07:09:46 -0700254 texSamplers.begin(),
255 bufferSamplers.begin(),
Brian Salomonf9f45122016-11-29 11:59:17 -0500256 imageStorageArray.begin(),
ethannicholas22793252016-01-30 09:59:10 -0800257 usePLSDstRead);
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 Salomonf9f45122016-11-29 11:59:17 -0500286 this->emitSampler(samplerType, sampler.texture()->config(), name.c_str(),
287 sampler.visibility(), outTexSamplerHandles);
288
cdalton9c3f1432016-03-11 10:07:37 -0800289 }
cdalton74b8d322016-04-11 14:47:28 -0700290
291 if (int numBuffers = processor.numBuffers()) {
Brian Salomon94efbf52016-11-29 13:43:05 -0500292 SkASSERT(this->shaderCaps()->texelBufferSupport());
cdalton74b8d322016-04-11 14:47:28 -0700293 GrShaderFlags texelBufferVisibility = kNone_GrShaderFlags;
294
295 for (int b = 0; b < numBuffers; ++b) {
Brian Salomonb014cca2016-11-18 11:39:15 -0500296 const GrProcessor::BufferAccess& access = processor.bufferAccess(b);
Brian Salomonf9f45122016-11-29 11:59:17 -0500297 name.printf("BufferSampler_%d", outBufferSamplerHandles->count());
csmartdalton22458032016-11-16 11:28:16 -0700298 this->emitSampler(kBufferSampler_GrSLType, access.texelConfig(), name.c_str(),
Brian Salomonf9f45122016-11-29 11:59:17 -0500299 access.visibility(), outBufferSamplerHandles);
cdalton74b8d322016-04-11 14:47:28 -0700300 texelBufferVisibility |= access.visibility();
301 }
302
Brian Salomon94efbf52016-11-29 13:43:05 -0500303 if (const char* extension = this->shaderCaps()->texelBufferExtensionString()) {
cdalton74b8d322016-04-11 14:47:28 -0700304 this->addFeature(texelBufferVisibility,
305 1 << GrGLSLShaderBuilder::kTexelBuffer_GLSLPrivateFeature,
306 extension);
307 }
308 }
Brian Salomonf9f45122016-11-29 11:59:17 -0500309 int numImageStorages = processor.numImageStorages();
310 for (int i = 0; i < numImageStorages; ++i) {
311 const GrProcessor::ImageStorageAccess& imageStorageAccess = processor.imageStorageAccess(i);
312 name.printf("Image_%d", outImageStorageHandles->count());
313 this->emitImageStorage(imageStorageAccess, name.c_str(), outImageStorageHandles);
314 }
cdalton74b8d322016-04-11 14:47:28 -0700315}
316
317void GrGLSLProgramBuilder::emitSampler(GrSLType samplerType,
318 GrPixelConfig config,
319 const char* name,
320 GrShaderFlags visibility,
Brian Salomonf9f45122016-11-29 11:59:17 -0500321 SkTArray<SamplerHandle>* outSamplerHandles) {
cdalton74b8d322016-04-11 14:47:28 -0700322 if (visibility & kVertex_GrShaderFlag) {
323 ++fNumVertexSamplers;
324 }
325 if (visibility & kGeometry_GrShaderFlag) {
326 SkASSERT(this->primitiveProcessor().willUseGeoShader());
327 ++fNumGeometrySamplers;
328 }
329 if (visibility & kFragment_GrShaderFlag) {
330 ++fNumFragmentSamplers;
331 }
Brian Salomon94efbf52016-11-29 13:43:05 -0500332 GrSLPrecision precision = this->shaderCaps()->samplerPrecision(config, visibility);
333 GrSwizzle swizzle = this->shaderCaps()->configTextureSwizzle(config);
Brian Salomonf9f45122016-11-29 11:59:17 -0500334 outSamplerHandles->emplace_back(this->uniformHandler()->addSampler(visibility,
335 swizzle,
336 samplerType,
337 precision,
338 name));
339}
340
341void GrGLSLProgramBuilder::emitImageStorage(const GrProcessor::ImageStorageAccess& access,
342 const char* name,
343 SkTArray<ImageStorageHandle>* outImageStorageHandles) {
344 if (access.visibility() & kVertex_GrShaderFlag) {
345 ++fNumVertexImageStorages;
346 }
347 if (access.visibility() & kGeometry_GrShaderFlag) {
348 SkASSERT(this->primitiveProcessor().willUseGeoShader());
349 ++fNumGeometryImageStorages;
350 }
351 if (access.visibility() & kFragment_GrShaderFlag) {
352 ++fNumFragmentImageStorages;
353 }
354 GrSLType uniformType = access.texture()->texturePriv().imageStorageType();
355 ImageStorageHandle handle = this->uniformHandler()->addImageStorage(access.visibility(),
356 uniformType, access.format(), access.memoryModel(), access.restrict(), access.ioType(),
357 name);
358 outImageStorageHandles->emplace_back(handle);
cdalton9c3f1432016-03-11 10:07:37 -0800359}
360
egdanielfa896322016-01-13 12:19:30 -0800361void GrGLSLProgramBuilder::emitFSOutputSwizzle(bool hasSecondaryOutput) {
362 // Swizzle the fragment shader outputs if necessary.
363 GrSwizzle swizzle;
Ethan Nicholas38657112017-02-09 17:01:22 -0500364 swizzle.setFromKey(this->desc()->header().fOutputSwizzle);
egdanielfa896322016-01-13 12:19:30 -0800365 if (swizzle != GrSwizzle::RGBA()) {
366 fFS.codeAppendf("%s = %s.%s;", fFS.getPrimaryColorOutputName(),
367 fFS.getPrimaryColorOutputName(),
368 swizzle.c_str());
369 if (hasSecondaryOutput) {
370 fFS.codeAppendf("%s = %s.%s;", fFS.getSecondaryColorOutputName(),
371 fFS.getSecondaryColorOutputName(),
372 swizzle.c_str());
373 }
374 }
375}
376
cdalton9c3f1432016-03-11 10:07:37 -0800377bool GrGLSLProgramBuilder::checkSamplerCounts() {
Brian Salomon1edc5b92016-11-29 13:43:46 -0500378 const GrShaderCaps& shaderCaps = *this->shaderCaps();
379 if (fNumVertexSamplers > shaderCaps.maxVertexSamplers()) {
cdalton9c3f1432016-03-11 10:07:37 -0800380 GrCapsDebugf(this->caps(), "Program would use too many vertex samplers\n");
381 return false;
382 }
Brian Salomon1edc5b92016-11-29 13:43:46 -0500383 if (fNumGeometrySamplers > shaderCaps.maxGeometrySamplers()) {
cdalton9c3f1432016-03-11 10:07:37 -0800384 GrCapsDebugf(this->caps(), "Program would use too many geometry samplers\n");
385 return false;
386 }
Brian Salomon1edc5b92016-11-29 13:43:46 -0500387 if (fNumFragmentSamplers > shaderCaps.maxFragmentSamplers()) {
cdalton9c3f1432016-03-11 10:07:37 -0800388 GrCapsDebugf(this->caps(), "Program would use too many fragment samplers\n");
389 return false;
390 }
391 // If the same sampler is used in two different shaders, it counts as two combined samplers.
392 int numCombinedSamplers = fNumVertexSamplers + fNumGeometrySamplers + fNumFragmentSamplers;
Brian Salomon1edc5b92016-11-29 13:43:46 -0500393 if (numCombinedSamplers > shaderCaps.maxCombinedSamplers()) {
cdalton9c3f1432016-03-11 10:07:37 -0800394 GrCapsDebugf(this->caps(), "Program would use too many combined samplers\n");
395 return false;
396 }
397 return true;
398}
399
Brian Salomonf9f45122016-11-29 11:59:17 -0500400bool GrGLSLProgramBuilder::checkImageStorageCounts() {
Brian Salomon1edc5b92016-11-29 13:43:46 -0500401 const GrShaderCaps& shaderCaps = *this->shaderCaps();
402 if (fNumVertexImageStorages > shaderCaps.maxVertexImageStorages()) {
Brian Salomonf9f45122016-11-29 11:59:17 -0500403 GrCapsDebugf(this->caps(), "Program would use too many vertex images\n");
404 return false;
405 }
Brian Salomon1edc5b92016-11-29 13:43:46 -0500406 if (fNumGeometryImageStorages > shaderCaps.maxGeometryImageStorages()) {
Brian Salomonf9f45122016-11-29 11:59:17 -0500407 GrCapsDebugf(this->caps(), "Program would use too many geometry images\n");
408 return false;
409 }
Brian Salomon1edc5b92016-11-29 13:43:46 -0500410 if (fNumFragmentImageStorages > shaderCaps.maxFragmentImageStorages()) {
Brian Salomonf9f45122016-11-29 11:59:17 -0500411 GrCapsDebugf(this->caps(), "Program would use too many fragment images\n");
412 return false;
413 }
414 // If the same image is used in two different shaders, it counts as two combined images.
415 int numCombinedImages = fNumVertexImageStorages + fNumGeometryImageStorages +
416 fNumFragmentImageStorages;
Brian Salomon1edc5b92016-11-29 13:43:46 -0500417 if (numCombinedImages > shaderCaps.maxCombinedImageStorages()) {
Brian Salomonf9f45122016-11-29 11:59:17 -0500418 GrCapsDebugf(this->caps(), "Program would use too many combined images\n");
419 return false;
420 }
421 return true;
422}
423
cdalton87332102016-02-26 12:22:02 -0800424#ifdef SK_DEBUG
egdanielfa896322016-01-13 12:19:30 -0800425void GrGLSLProgramBuilder::verify(const GrPrimitiveProcessor& gp) {
cdalton87332102016-02-26 12:22:02 -0800426 SkASSERT(fFS.usedProcessorFeatures() == gp.requiredFeatures());
egdanielfa896322016-01-13 12:19:30 -0800427}
428
429void GrGLSLProgramBuilder::verify(const GrXferProcessor& xp) {
cdalton87332102016-02-26 12:22:02 -0800430 SkASSERT(fFS.usedProcessorFeatures() == xp.requiredFeatures());
egdanielfa896322016-01-13 12:19:30 -0800431 SkASSERT(fFS.hasReadDstColor() == xp.willReadDstColor());
432}
433
434void GrGLSLProgramBuilder::verify(const GrFragmentProcessor& fp) {
cdalton87332102016-02-26 12:22:02 -0800435 SkASSERT(fFS.usedProcessorFeatures() == fp.requiredFeatures());
egdaniel8dcdedc2015-11-11 06:27:20 -0800436}
cdalton87332102016-02-26 12:22:02 -0800437#endif
egdaniel8dcdedc2015-11-11 06:27:20 -0800438
439void GrGLSLProgramBuilder::nameVariable(SkString* out, char prefix, const char* name, bool mangle) {
440 if ('\0' == prefix) {
441 *out = name;
442 } else {
443 out->printf("%c%s", prefix, name);
444 }
445 if (mangle) {
446 if (out->endsWith('_')) {
447 // Names containing "__" are reserved.
448 out->append("x");
449 }
450 out->appendf("_Stage%d%s", fStageIndex, fFS.getMangleString().c_str());
451 }
452}
453
egdanielfa896322016-01-13 12:19:30 -0800454void GrGLSLProgramBuilder::nameExpression(GrGLSLExpr4* output, const char* baseName) {
455 // create var to hold stage result. If we already have a valid output name, just use that
456 // otherwise create a new mangled one. This name is only valid if we are reordering stages
457 // and have to tell stage exactly where to put its output.
458 SkString outName;
459 if (output->isValid()) {
460 outName = output->c_str();
461 } else {
462 this->nameVariable(&outName, '\0', baseName);
463 }
464 fFS.codeAppendf("vec4 %s;", outName.c_str());
465 *output = outName;
466}
467
cdalton5e58cee2016-02-11 12:49:47 -0800468void GrGLSLProgramBuilder::appendUniformDecls(GrShaderFlags visibility, SkString* out) const {
egdaniel7ea439b2015-12-03 09:20:44 -0800469 this->uniformHandler()->appendUniformDecls(visibility, out);
470}
471
Ethan Nicholas941e7e22016-12-12 15:33:30 -0500472void GrGLSLProgramBuilder::addRTHeightUniform(const char* name) {
egdaniel7ea439b2015-12-03 09:20:44 -0800473 SkASSERT(!fUniformHandles.fRTHeightUni.isValid());
474 GrGLSLUniformHandler* uniformHandler = this->uniformHandler();
475 fUniformHandles.fRTHeightUni =
cdalton5e58cee2016-02-11 12:49:47 -0800476 uniformHandler->internalAddUniformArray(kFragment_GrShaderFlag,
egdaniel7ea439b2015-12-03 09:20:44 -0800477 kFloat_GrSLType, kDefault_GrSLPrecision,
Ethan Nicholas941e7e22016-12-12 15:33:30 -0500478 name, false, 0, nullptr);
egdaniel8dcdedc2015-11-11 06:27:20 -0800479}
480
egdanielfa896322016-01-13 12:19:30 -0800481void GrGLSLProgramBuilder::cleanupFragmentProcessors() {
482 for (int i = 0; i < fFragmentProcessors.count(); ++i) {
483 delete fFragmentProcessors[i];
484 }
485}
486
egdaniel9f1d4152016-02-10 09:50:38 -0800487void GrGLSLProgramBuilder::finalizeShaders() {
488 this->varyingHandler()->finalize();
cdalton5e58cee2016-02-11 12:49:47 -0800489 fVS.finalize(kVertex_GrShaderFlag);
csmartdalton276cc412016-11-21 11:55:00 -0700490 if (this->primitiveProcessor().willUseGeoShader()) {
Brian Salomon94efbf52016-11-29 13:43:05 -0500491 SkASSERT(this->shaderCaps()->geometryShaderSupport());
csmartdalton276cc412016-11-21 11:55:00 -0700492 fGS.finalize(kGeometry_GrShaderFlag);
493 }
cdalton5e58cee2016-02-11 12:49:47 -0800494 fFS.finalize(kFragment_GrShaderFlag);
egdaniel9f1d4152016-02-10 09:50:38 -0800495}