blob: 0d87c4f5a2c5174f880656dcb0352e2f652d0a6b [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
Ethan Nicholas2983f402017-05-08 09:36:08 -040056bool GrGLSLProgramBuilder::emitAndInstallProcs() {
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
Ethan Nicholas2983f402017-05-08 09:36:08 -040061 SkString inputColor;
62 SkString inputCoverage;
63 this->emitAndInstallPrimProc(primProc, &inputColor, &inputCoverage);
64 this->emitAndInstallFragProcs(&inputColor, &inputCoverage);
65 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,
Ethan Nicholas2983f402017-05-08 09:36:08 -040072 SkString* outputColor,
73 SkString* outputCoverage) {
egdanielfa896322016-01-13 12:19:30 -080074 // 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
csmartdalton936f81b2017-02-13 15:45:35 -070079 SkASSERT(!fUniformHandles.fRTAdjustmentUni.isValid());
80 GrShaderFlags rtAdjustVisibility = kVertex_GrShaderFlag;
81 if (proc.willUseGeoShader()) {
82 rtAdjustVisibility |= kGeometry_GrShaderFlag;
83 }
84 fUniformHandles.fRTAdjustmentUni = this->uniformHandler()->addUniform(rtAdjustVisibility,
85 kVec4f_GrSLType,
86 kHigh_GrSLPrecision,
87 "rtAdjustment");
88 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);
Brian Salomon94efbf52016-11-29 13:43:05 -050098 fGeometryProcessor = 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 Salomonf9f45122016-11-29 11:59:17 -0500102 SkSTArray<2, ImageStorageHandle> imageStorages(proc.numImageStorages());
Greg Danielbc5d4d72017-05-05 10:28:42 -0400103 this->emitSamplersAndImageStorages(proc, &texSamplers, &texelBuffers, &imageStorages);
egdanielfa896322016-01-13 12:19:30 -0800104
bsalomona624bf32016-09-20 09:12:47 -0700105 GrGLSLPrimitiveProcessor::FPCoordTransformHandler transformHandler(fPipeline,
106 &fTransformedCoordVars);
egdanielfa896322016-01-13 12:19:30 -0800107 GrGLSLGeometryProcessor::EmitArgs args(&fVS,
csmartdalton276cc412016-11-21 11:55:00 -0700108 proc.willUseGeoShader() ? &fGS : nullptr,
egdanielfa896322016-01-13 12:19:30 -0800109 &fFS,
110 this->varyingHandler(),
111 this->uniformHandler(),
Brian Salomon94efbf52016-11-29 13:43:05 -0500112 this->shaderCaps(),
egdanielfa896322016-01-13 12:19:30 -0800113 proc,
114 outputColor->c_str(),
115 outputCoverage->c_str(),
csmartdalton936f81b2017-02-13 15:45:35 -0700116 rtAdjustName,
egdaniel09aa1fc2016-04-20 07:09:46 -0700117 texSamplers.begin(),
Greg Danielbc5d4d72017-05-05 10:28:42 -0400118 texelBuffers.begin(),
Brian Salomonf9f45122016-11-29 11:59:17 -0500119 imageStorages.begin(),
bsalomona624bf32016-09-20 09:12:47 -0700120 &transformHandler);
egdanielfa896322016-01-13 12:19:30 -0800121 fGeometryProcessor->emitCode(args);
122
123 // We have to check that effects and the code they emit are consistent, ie if an effect
124 // asks for dst color, then the emit code needs to follow suit
cdalton87332102016-02-26 12:22:02 -0800125 SkDEBUGCODE(verify(proc);)
egdanielfa896322016-01-13 12:19:30 -0800126
127 fFS.codeAppend("}");
128}
129
Ethan Nicholas2983f402017-05-08 09:36:08 -0400130void GrGLSLProgramBuilder::emitAndInstallFragProcs(SkString* color, SkString* coverage) {
bsalomona624bf32016-09-20 09:12:47 -0700131 int transformedCoordVarsIdx = 0;
Ethan Nicholas2983f402017-05-08 09:36:08 -0400132 SkString** inOut = &color;
bsalomona624bf32016-09-20 09:12:47 -0700133 for (int i = 0; i < this->pipeline().numFragmentProcessors(); ++i) {
134 if (i == this->pipeline().numColorFragmentProcessors()) {
135 inOut = &coverage;
136 }
Ethan Nicholas2983f402017-05-08 09:36:08 -0400137 SkString output;
egdanielfa896322016-01-13 12:19:30 -0800138 const GrFragmentProcessor& fp = this->pipeline().getFragmentProcessor(i);
Ethan Nicholas2983f402017-05-08 09:36:08 -0400139 output = this->emitAndInstallFragProc(fp, i, transformedCoordVarsIdx, **inOut, output);
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 }
146}
147
148// TODO Processors cannot output zeros because an empty string is all 1s
Ethan Nicholas2983f402017-05-08 09:36:08 -0400149// the fix is to allow effects to take the SkString directly
150SkString GrGLSLProgramBuilder::emitAndInstallFragProc(const GrFragmentProcessor& fp,
151 int index,
152 int transformedCoordVarsIdx,
153 const SkString& input,
154 SkString output) {
155 SkASSERT(input.size());
egdanielfa896322016-01-13 12:19:30 -0800156 // Program builders have a bit of state we need to clear with each effect
157 AutoStageAdvance adv(this);
Ethan Nicholas2983f402017-05-08 09:36:08 -0400158 this->nameExpression(&output, "output");
egdanielfa896322016-01-13 12:19:30 -0800159
160 // Enclose custom code in a block to avoid namespace conflicts
161 SkString openBrace;
162 openBrace.printf("{ // Stage %d, %s\n", fStageIndex, fp.name());
163 fFS.codeAppend(openBrace.c_str());
164
165 GrGLSLFragmentProcessor* fragProc = fp.createGLSLInstance();
166
Brian Salomon0bbecb22016-11-17 11:38:22 -0500167 SkSTArray<4, SamplerHandle> textureSamplerArray(fp.numTextureSamplers());
Greg Danielbc5d4d72017-05-05 10:28:42 -0400168 SkSTArray<2, TexelBufferHandle> texelBufferArray(fp.numBuffers());
Brian Salomonf9f45122016-11-29 11:59:17 -0500169 SkSTArray<2, ImageStorageHandle> imageStorageArray(fp.numImageStorages());
bsalomonb58a2b42016-09-26 06:55:02 -0700170 GrFragmentProcessor::Iter iter(&fp);
171 while (const GrFragmentProcessor* subFP = iter.next()) {
Greg Danielbc5d4d72017-05-05 10:28:42 -0400172 this->emitSamplersAndImageStorages(*subFP, &textureSamplerArray, &texelBufferArray,
Brian Salomonf9f45122016-11-29 11:59:17 -0500173 &imageStorageArray);
bsalomonb58a2b42016-09-26 06:55:02 -0700174 }
egdanielfa896322016-01-13 12:19:30 -0800175
bsalomona624bf32016-09-20 09:12:47 -0700176 const GrShaderVar* coordVars = fTransformedCoordVars.begin() + transformedCoordVarsIdx;
177 GrGLSLFragmentProcessor::TransformedCoordVars coords(&fp, coordVars);
bsalomonb58a2b42016-09-26 06:55:02 -0700178 GrGLSLFragmentProcessor::TextureSamplers textureSamplers(&fp, textureSamplerArray.begin());
Greg Danielbc5d4d72017-05-05 10:28:42 -0400179 GrGLSLFragmentProcessor::TexelBuffers texelBuffers(&fp, texelBufferArray.begin());
Brian Salomonf9f45122016-11-29 11:59:17 -0500180 GrGLSLFragmentProcessor::ImageStorages imageStorages(&fp, imageStorageArray.begin());
egdanielfa896322016-01-13 12:19:30 -0800181 GrGLSLFragmentProcessor::EmitArgs args(&fFS,
182 this->uniformHandler(),
Brian Salomon94efbf52016-11-29 13:43:05 -0500183 this->shaderCaps(),
egdanielfa896322016-01-13 12:19:30 -0800184 fp,
Ethan Nicholas2983f402017-05-08 09:36:08 -0400185 output.c_str(),
186 input.c_str(),
bsalomona624bf32016-09-20 09:12:47 -0700187 coords,
bsalomonb58a2b42016-09-26 06:55:02 -0700188 textureSamplers,
Greg Danielbc5d4d72017-05-05 10:28:42 -0400189 texelBuffers,
Brian Salomone23bffd2017-06-02 11:01:10 -0400190 imageStorages);
dvonbeck9b03e7b2016-08-01 11:01:56 -0700191
egdanielfa896322016-01-13 12:19:30 -0800192 fragProc->emitCode(args);
193
194 // We have to check that effects and the code they emit are consistent, ie if an effect
195 // asks for dst color, then the emit code needs to follow suit
cdalton87332102016-02-26 12:22:02 -0800196 SkDEBUGCODE(verify(fp);)
egdanielfa896322016-01-13 12:19:30 -0800197 fFragmentProcessors.push_back(fragProc);
198
199 fFS.codeAppend("}");
Ethan Nicholas2983f402017-05-08 09:36:08 -0400200 return output;
egdanielfa896322016-01-13 12:19:30 -0800201}
202
Ethan Nicholas2983f402017-05-08 09:36:08 -0400203void GrGLSLProgramBuilder::emitAndInstallXferProc(const SkString& colorIn,
204 const SkString& coverageIn) {
egdanielfa896322016-01-13 12:19:30 -0800205 // Program builders have a bit of state we need to clear with each effect
206 AutoStageAdvance adv(this);
207
208 SkASSERT(!fXferProcessor);
Brian Salomon18dfa982017-04-03 16:57:43 -0400209 const GrXferProcessor& xp = fPipeline.getXferProcessor();
egdanielfa896322016-01-13 12:19:30 -0800210 fXferProcessor = xp.createGLSLInstance();
211
212 // Enable dual source secondary output if we have one
213 if (xp.hasSecondaryOutput()) {
214 fFS.enableSecondaryOutput();
215 }
216
Brian Salomon94efbf52016-11-29 13:43:05 -0500217 if (this->shaderCaps()->mustDeclareFragmentShaderOutput()) {
egdanielfa896322016-01-13 12:19:30 -0800218 fFS.enableCustomOutput();
219 }
220
221 SkString openBrace;
222 openBrace.printf("{ // Xfer Processor: %s\n", xp.name());
223 fFS.codeAppend(openBrace.c_str());
224
Brian Salomon18dfa982017-04-03 16:57:43 -0400225 SamplerHandle dstTextureSamplerHandle;
226 GrSurfaceOrigin dstTextureOrigin = kTopLeft_GrSurfaceOrigin;
Robert Phillipsbb581ce2017-05-29 15:05:15 -0400227
228 if (GrTexture* dstTexture = fPipeline.peekDstTexture()) {
Brian Salomon18dfa982017-04-03 16:57:43 -0400229 // GrProcessor::TextureSampler sampler(dstTexture);
230 SkString name("DstTextureSampler");
231 dstTextureSamplerHandle =
232 this->emitSampler(dstTexture->texturePriv().samplerType(), dstTexture->config(),
233 "DstTextureSampler", kFragment_GrShaderFlag);
234 dstTextureOrigin = dstTexture->origin();
235 SkASSERT(kTextureExternalSampler_GrSLType != dstTexture->texturePriv().samplerType());
236 }
egdanielfa896322016-01-13 12:19:30 -0800237
238 GrGLSLXferProcessor::EmitArgs args(&fFS,
239 this->uniformHandler(),
Brian Salomon94efbf52016-11-29 13:43:05 -0500240 this->shaderCaps(),
Brian Salomon18dfa982017-04-03 16:57:43 -0400241 xp,
Ethan Nicholas2983f402017-05-08 09:36:08 -0400242 colorIn.size() ? colorIn.c_str() : "vec4(1)",
243 coverageIn.size() ? coverageIn.c_str() : "vec4(1)",
egdanielfa896322016-01-13 12:19:30 -0800244 fFS.getPrimaryColorOutputName(),
245 fFS.getSecondaryColorOutputName(),
Brian Salomon18dfa982017-04-03 16:57:43 -0400246 dstTextureSamplerHandle,
247 dstTextureOrigin);
egdanielfa896322016-01-13 12:19:30 -0800248 fXferProcessor->emitCode(args);
249
250 // We have to check that effects and the code they emit are consistent, ie if an effect
251 // asks for dst color, then the emit code needs to follow suit
cdalton87332102016-02-26 12:22:02 -0800252 SkDEBUGCODE(verify(xp);)
egdanielfa896322016-01-13 12:19:30 -0800253 fFS.codeAppend("}");
254}
255
Brian Salomonf9f45122016-11-29 11:59:17 -0500256void GrGLSLProgramBuilder::emitSamplersAndImageStorages(
Brian Salomonab015ef2017-04-04 10:15:51 -0400257 const GrResourceIOProcessor& processor,
Brian Salomonf9f45122016-11-29 11:59:17 -0500258 SkTArray<SamplerHandle>* outTexSamplerHandles,
Greg Danielbc5d4d72017-05-05 10:28:42 -0400259 SkTArray<TexelBufferHandle>* outTexelBufferHandles,
Brian Salomonf9f45122016-11-29 11:59:17 -0500260 SkTArray<ImageStorageHandle>* outImageStorageHandles) {
cdalton9c3f1432016-03-11 10:07:37 -0800261 SkString name;
Brian Salomon0bbecb22016-11-17 11:38:22 -0500262 int numTextureSamplers = processor.numTextureSamplers();
263 for (int t = 0; t < numTextureSamplers; ++t) {
Brian Salomonab015ef2017-04-04 10:15:51 -0400264 const GrResourceIOProcessor::TextureSampler& sampler = processor.textureSampler(t);
Brian Salomonf9f45122016-11-29 11:59:17 -0500265 name.printf("TextureSampler_%d", outTexSamplerHandles->count());
Robert Phillips9bee2e52017-05-29 12:37:20 -0400266 GrSLType samplerType = sampler.peekTexture()->texturePriv().samplerType();
egdaniel990dbc82016-07-13 14:09:30 -0700267 if (kTextureExternalSampler_GrSLType == samplerType) {
Brian Salomon94efbf52016-11-29 13:43:05 -0500268 const char* externalFeatureString =
269 this->shaderCaps()->externalTextureExtensionString();
cdalton9c3f1432016-03-11 10:07:37 -0800270 // We shouldn't ever create a GrGLTexture that requires external sampler type
271 SkASSERT(externalFeatureString);
Brian Salomondb4183d2016-11-17 12:48:40 -0500272 this->addFeature(sampler.visibility(),
cdalton9c3f1432016-03-11 10:07:37 -0800273 1 << GrGLSLShaderBuilder::kExternalTexture_GLSLPrivateFeature,
274 externalFeatureString);
275 }
Brian Salomon18dfa982017-04-03 16:57:43 -0400276 outTexSamplerHandles->emplace_back(this->emitSampler(
Robert Phillips9bee2e52017-05-29 12:37:20 -0400277 samplerType, sampler.peekTexture()->config(), name.c_str(), sampler.visibility()));
cdalton9c3f1432016-03-11 10:07:37 -0800278 }
cdalton74b8d322016-04-11 14:47:28 -0700279 if (int numBuffers = processor.numBuffers()) {
Brian Salomon94efbf52016-11-29 13:43:05 -0500280 SkASSERT(this->shaderCaps()->texelBufferSupport());
cdalton74b8d322016-04-11 14:47:28 -0700281 GrShaderFlags texelBufferVisibility = kNone_GrShaderFlags;
282
283 for (int b = 0; b < numBuffers; ++b) {
Brian Salomonab015ef2017-04-04 10:15:51 -0400284 const GrResourceIOProcessor::BufferAccess& access = processor.bufferAccess(b);
Greg Danielbc5d4d72017-05-05 10:28:42 -0400285 name.printf("TexelBuffer_%d", outTexelBufferHandles->count());
286 outTexelBufferHandles->emplace_back(
287 this->emitTexelBuffer(access.texelConfig(), name.c_str(), access.visibility()));
cdalton74b8d322016-04-11 14:47:28 -0700288 texelBufferVisibility |= access.visibility();
289 }
290
Brian Salomon94efbf52016-11-29 13:43:05 -0500291 if (const char* extension = this->shaderCaps()->texelBufferExtensionString()) {
cdalton74b8d322016-04-11 14:47:28 -0700292 this->addFeature(texelBufferVisibility,
293 1 << GrGLSLShaderBuilder::kTexelBuffer_GLSLPrivateFeature,
294 extension);
295 }
296 }
Brian Salomonf9f45122016-11-29 11:59:17 -0500297 int numImageStorages = processor.numImageStorages();
298 for (int i = 0; i < numImageStorages; ++i) {
Brian Salomonab015ef2017-04-04 10:15:51 -0400299 const GrResourceIOProcessor::ImageStorageAccess& imageStorageAccess =
300 processor.imageStorageAccess(i);
Brian Salomonf9f45122016-11-29 11:59:17 -0500301 name.printf("Image_%d", outImageStorageHandles->count());
Brian Salomon18dfa982017-04-03 16:57:43 -0400302 outImageStorageHandles->emplace_back(
303 this->emitImageStorage(imageStorageAccess, name.c_str()));
Brian Salomonf9f45122016-11-29 11:59:17 -0500304 }
cdalton74b8d322016-04-11 14:47:28 -0700305}
306
Greg Danielbc5d4d72017-05-05 10:28:42 -0400307void GrGLSLProgramBuilder::updateSamplerCounts(GrShaderFlags visibility) {
cdalton74b8d322016-04-11 14:47:28 -0700308 if (visibility & kVertex_GrShaderFlag) {
309 ++fNumVertexSamplers;
310 }
311 if (visibility & kGeometry_GrShaderFlag) {
312 SkASSERT(this->primitiveProcessor().willUseGeoShader());
313 ++fNumGeometrySamplers;
314 }
315 if (visibility & kFragment_GrShaderFlag) {
316 ++fNumFragmentSamplers;
317 }
Greg Danielbc5d4d72017-05-05 10:28:42 -0400318}
319
320GrGLSLProgramBuilder::SamplerHandle GrGLSLProgramBuilder::emitSampler(GrSLType samplerType,
321 GrPixelConfig config,
322 const char* name,
323 GrShaderFlags visibility) {
324 this->updateSamplerCounts(visibility);
Brian Salomon94efbf52016-11-29 13:43:05 -0500325 GrSLPrecision precision = this->shaderCaps()->samplerPrecision(config, visibility);
326 GrSwizzle swizzle = this->shaderCaps()->configTextureSwizzle(config);
Brian Salomon18dfa982017-04-03 16:57:43 -0400327 return this->uniformHandler()->addSampler(visibility, swizzle, samplerType, precision, name);
Brian Salomonf9f45122016-11-29 11:59:17 -0500328}
329
Greg Danielbc5d4d72017-05-05 10:28:42 -0400330GrGLSLProgramBuilder::TexelBufferHandle GrGLSLProgramBuilder::emitTexelBuffer(
331 GrPixelConfig config, const char* name, GrShaderFlags visibility) {
332 this->updateSamplerCounts(visibility);
333 GrSLPrecision precision = this->shaderCaps()->samplerPrecision(config, visibility);
334 return this->uniformHandler()->addTexelBuffer(visibility, precision, name);
335}
336
Brian Salomon18dfa982017-04-03 16:57:43 -0400337GrGLSLProgramBuilder::ImageStorageHandle GrGLSLProgramBuilder::emitImageStorage(
Brian Salomonab015ef2017-04-04 10:15:51 -0400338 const GrResourceIOProcessor::ImageStorageAccess& access, const char* name) {
Brian Salomonf9f45122016-11-29 11:59:17 -0500339 if (access.visibility() & kVertex_GrShaderFlag) {
340 ++fNumVertexImageStorages;
341 }
342 if (access.visibility() & kGeometry_GrShaderFlag) {
343 SkASSERT(this->primitiveProcessor().willUseGeoShader());
344 ++fNumGeometryImageStorages;
345 }
346 if (access.visibility() & kFragment_GrShaderFlag) {
347 ++fNumFragmentImageStorages;
348 }
Robert Phillips8a02f652017-05-12 14:49:16 -0400349 GrSLType uniformType = access.proxy()->imageStorageType();
Brian Salomon18dfa982017-04-03 16:57:43 -0400350 return this->uniformHandler()->addImageStorage(access.visibility(), uniformType,
351 access.format(), access.memoryModel(),
352 access.restrict(), access.ioType(), name);
cdalton9c3f1432016-03-11 10:07:37 -0800353}
354
egdanielfa896322016-01-13 12:19:30 -0800355void GrGLSLProgramBuilder::emitFSOutputSwizzle(bool hasSecondaryOutput) {
356 // Swizzle the fragment shader outputs if necessary.
357 GrSwizzle swizzle;
Ethan Nicholas38657112017-02-09 17:01:22 -0500358 swizzle.setFromKey(this->desc()->header().fOutputSwizzle);
egdanielfa896322016-01-13 12:19:30 -0800359 if (swizzle != GrSwizzle::RGBA()) {
360 fFS.codeAppendf("%s = %s.%s;", fFS.getPrimaryColorOutputName(),
361 fFS.getPrimaryColorOutputName(),
362 swizzle.c_str());
363 if (hasSecondaryOutput) {
364 fFS.codeAppendf("%s = %s.%s;", fFS.getSecondaryColorOutputName(),
365 fFS.getSecondaryColorOutputName(),
366 swizzle.c_str());
367 }
368 }
369}
370
cdalton9c3f1432016-03-11 10:07:37 -0800371bool GrGLSLProgramBuilder::checkSamplerCounts() {
Brian Salomon1edc5b92016-11-29 13:43:46 -0500372 const GrShaderCaps& shaderCaps = *this->shaderCaps();
373 if (fNumVertexSamplers > shaderCaps.maxVertexSamplers()) {
cdalton9c3f1432016-03-11 10:07:37 -0800374 GrCapsDebugf(this->caps(), "Program would use too many vertex samplers\n");
375 return false;
376 }
Brian Salomon1edc5b92016-11-29 13:43:46 -0500377 if (fNumGeometrySamplers > shaderCaps.maxGeometrySamplers()) {
cdalton9c3f1432016-03-11 10:07:37 -0800378 GrCapsDebugf(this->caps(), "Program would use too many geometry samplers\n");
379 return false;
380 }
Brian Salomon1edc5b92016-11-29 13:43:46 -0500381 if (fNumFragmentSamplers > shaderCaps.maxFragmentSamplers()) {
cdalton9c3f1432016-03-11 10:07:37 -0800382 GrCapsDebugf(this->caps(), "Program would use too many fragment samplers\n");
383 return false;
384 }
385 // If the same sampler is used in two different shaders, it counts as two combined samplers.
386 int numCombinedSamplers = fNumVertexSamplers + fNumGeometrySamplers + fNumFragmentSamplers;
Brian Salomon1edc5b92016-11-29 13:43:46 -0500387 if (numCombinedSamplers > shaderCaps.maxCombinedSamplers()) {
cdalton9c3f1432016-03-11 10:07:37 -0800388 GrCapsDebugf(this->caps(), "Program would use too many combined samplers\n");
389 return false;
390 }
391 return true;
392}
393
Brian Salomonf9f45122016-11-29 11:59:17 -0500394bool GrGLSLProgramBuilder::checkImageStorageCounts() {
Brian Salomon1edc5b92016-11-29 13:43:46 -0500395 const GrShaderCaps& shaderCaps = *this->shaderCaps();
396 if (fNumVertexImageStorages > shaderCaps.maxVertexImageStorages()) {
Brian Salomonf9f45122016-11-29 11:59:17 -0500397 GrCapsDebugf(this->caps(), "Program would use too many vertex images\n");
398 return false;
399 }
Brian Salomon1edc5b92016-11-29 13:43:46 -0500400 if (fNumGeometryImageStorages > shaderCaps.maxGeometryImageStorages()) {
Brian Salomonf9f45122016-11-29 11:59:17 -0500401 GrCapsDebugf(this->caps(), "Program would use too many geometry images\n");
402 return false;
403 }
Brian Salomon1edc5b92016-11-29 13:43:46 -0500404 if (fNumFragmentImageStorages > shaderCaps.maxFragmentImageStorages()) {
Brian Salomonf9f45122016-11-29 11:59:17 -0500405 GrCapsDebugf(this->caps(), "Program would use too many fragment images\n");
406 return false;
407 }
408 // If the same image is used in two different shaders, it counts as two combined images.
409 int numCombinedImages = fNumVertexImageStorages + fNumGeometryImageStorages +
410 fNumFragmentImageStorages;
Brian Salomon1edc5b92016-11-29 13:43:46 -0500411 if (numCombinedImages > shaderCaps.maxCombinedImageStorages()) {
Brian Salomonf9f45122016-11-29 11:59:17 -0500412 GrCapsDebugf(this->caps(), "Program would use too many combined images\n");
413 return false;
414 }
415 return true;
416}
417
cdalton87332102016-02-26 12:22:02 -0800418#ifdef SK_DEBUG
egdanielfa896322016-01-13 12:19:30 -0800419void GrGLSLProgramBuilder::verify(const GrPrimitiveProcessor& gp) {
cdalton87332102016-02-26 12:22:02 -0800420 SkASSERT(fFS.usedProcessorFeatures() == gp.requiredFeatures());
egdanielfa896322016-01-13 12:19:30 -0800421}
422
423void GrGLSLProgramBuilder::verify(const GrXferProcessor& xp) {
cdalton87332102016-02-26 12:22:02 -0800424 SkASSERT(fFS.usedProcessorFeatures() == xp.requiredFeatures());
egdanielfa896322016-01-13 12:19:30 -0800425 SkASSERT(fFS.hasReadDstColor() == xp.willReadDstColor());
426}
427
428void GrGLSLProgramBuilder::verify(const GrFragmentProcessor& fp) {
cdalton87332102016-02-26 12:22:02 -0800429 SkASSERT(fFS.usedProcessorFeatures() == fp.requiredFeatures());
egdaniel8dcdedc2015-11-11 06:27:20 -0800430}
cdalton87332102016-02-26 12:22:02 -0800431#endif
egdaniel8dcdedc2015-11-11 06:27:20 -0800432
433void GrGLSLProgramBuilder::nameVariable(SkString* out, char prefix, const char* name, bool mangle) {
434 if ('\0' == prefix) {
435 *out = name;
436 } else {
437 out->printf("%c%s", prefix, name);
438 }
439 if (mangle) {
440 if (out->endsWith('_')) {
441 // Names containing "__" are reserved.
442 out->append("x");
443 }
444 out->appendf("_Stage%d%s", fStageIndex, fFS.getMangleString().c_str());
445 }
446}
447
Ethan Nicholas2983f402017-05-08 09:36:08 -0400448void GrGLSLProgramBuilder::nameExpression(SkString* output, const char* baseName) {
egdanielfa896322016-01-13 12:19:30 -0800449 // create var to hold stage result. If we already have a valid output name, just use that
450 // otherwise create a new mangled one. This name is only valid if we are reordering stages
451 // and have to tell stage exactly where to put its output.
452 SkString outName;
Ethan Nicholas2983f402017-05-08 09:36:08 -0400453 if (output->size()) {
egdanielfa896322016-01-13 12:19:30 -0800454 outName = output->c_str();
455 } else {
456 this->nameVariable(&outName, '\0', baseName);
457 }
458 fFS.codeAppendf("vec4 %s;", outName.c_str());
459 *output = outName;
460}
461
cdalton5e58cee2016-02-11 12:49:47 -0800462void GrGLSLProgramBuilder::appendUniformDecls(GrShaderFlags visibility, SkString* out) const {
egdaniel7ea439b2015-12-03 09:20:44 -0800463 this->uniformHandler()->appendUniformDecls(visibility, out);
464}
465
Ethan Nicholas941e7e22016-12-12 15:33:30 -0500466void GrGLSLProgramBuilder::addRTHeightUniform(const char* name) {
egdaniel7ea439b2015-12-03 09:20:44 -0800467 SkASSERT(!fUniformHandles.fRTHeightUni.isValid());
468 GrGLSLUniformHandler* uniformHandler = this->uniformHandler();
469 fUniformHandles.fRTHeightUni =
cdalton5e58cee2016-02-11 12:49:47 -0800470 uniformHandler->internalAddUniformArray(kFragment_GrShaderFlag,
egdaniel7ea439b2015-12-03 09:20:44 -0800471 kFloat_GrSLType, kDefault_GrSLPrecision,
Ethan Nicholas941e7e22016-12-12 15:33:30 -0500472 name, false, 0, nullptr);
egdaniel8dcdedc2015-11-11 06:27:20 -0800473}
474
egdanielfa896322016-01-13 12:19:30 -0800475void GrGLSLProgramBuilder::cleanupFragmentProcessors() {
476 for (int i = 0; i < fFragmentProcessors.count(); ++i) {
477 delete fFragmentProcessors[i];
478 }
479}
480
egdaniel9f1d4152016-02-10 09:50:38 -0800481void GrGLSLProgramBuilder::finalizeShaders() {
482 this->varyingHandler()->finalize();
cdalton5e58cee2016-02-11 12:49:47 -0800483 fVS.finalize(kVertex_GrShaderFlag);
csmartdalton276cc412016-11-21 11:55:00 -0700484 if (this->primitiveProcessor().willUseGeoShader()) {
Brian Salomon94efbf52016-11-29 13:43:05 -0500485 SkASSERT(this->shaderCaps()->geometryShaderSupport());
csmartdalton276cc412016-11-21 11:55:00 -0700486 fGS.finalize(kGeometry_GrShaderFlag);
487 }
cdalton5e58cee2016-02-11 12:49:47 -0800488 fFS.finalize(kFragment_GrShaderFlag);
egdaniel9f1d4152016-02-10 09:50:38 -0800489}