blob: 96b3359d4e268ebe6ffe7199c427661bdecba41e [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
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());
Greg Danielbc5d4d72017-05-05 10:28:42 -0400112 SkSTArray<2, TexelBufferHandle> texelBuffers(proc.numBuffers());
Brian Salomonf9f45122016-11-29 11:59:17 -0500113 SkSTArray<2, ImageStorageHandle> imageStorages(proc.numImageStorages());
Greg Danielbc5d4d72017-05-05 10:28:42 -0400114 this->emitSamplersAndImageStorages(proc, &texSamplers, &texelBuffers, &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(),
Greg Danielbc5d4d72017-05-05 10:28:42 -0400130 texelBuffers.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
Ethan Nicholas2983f402017-05-08 09:36:08 -0400142void GrGLSLProgramBuilder::emitAndInstallFragProcs(SkString* color, SkString* coverage) {
bsalomona624bf32016-09-20 09:12:47 -0700143 int transformedCoordVarsIdx = 0;
Ethan Nicholas2983f402017-05-08 09:36:08 -0400144 SkString** inOut = &color;
bsalomona624bf32016-09-20 09:12:47 -0700145 for (int i = 0; i < this->pipeline().numFragmentProcessors(); ++i) {
146 if (i == this->pipeline().numColorFragmentProcessors()) {
147 inOut = &coverage;
148 }
Ethan Nicholas2983f402017-05-08 09:36:08 -0400149 SkString output;
egdanielfa896322016-01-13 12:19:30 -0800150 const GrFragmentProcessor& fp = this->pipeline().getFragmentProcessor(i);
Ethan Nicholas2983f402017-05-08 09:36:08 -0400151 output = this->emitAndInstallFragProc(fp, i, transformedCoordVarsIdx, **inOut, output);
bsalomona624bf32016-09-20 09:12:47 -0700152 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
Ethan Nicholas2983f402017-05-08 09:36:08 -0400161// the fix is to allow effects to take the SkString directly
162SkString GrGLSLProgramBuilder::emitAndInstallFragProc(const GrFragmentProcessor& fp,
163 int index,
164 int transformedCoordVarsIdx,
165 const SkString& input,
166 SkString output) {
167 SkASSERT(input.size());
egdanielfa896322016-01-13 12:19:30 -0800168 // Program builders have a bit of state we need to clear with each effect
169 AutoStageAdvance adv(this);
Ethan Nicholas2983f402017-05-08 09:36:08 -0400170 this->nameExpression(&output, "output");
egdanielfa896322016-01-13 12:19:30 -0800171
172 // Enclose custom code in a block to avoid namespace conflicts
173 SkString openBrace;
174 openBrace.printf("{ // Stage %d, %s\n", fStageIndex, fp.name());
175 fFS.codeAppend(openBrace.c_str());
176
177 GrGLSLFragmentProcessor* fragProc = fp.createGLSLInstance();
178
Brian Salomon0bbecb22016-11-17 11:38:22 -0500179 SkSTArray<4, SamplerHandle> textureSamplerArray(fp.numTextureSamplers());
Greg Danielbc5d4d72017-05-05 10:28:42 -0400180 SkSTArray<2, TexelBufferHandle> texelBufferArray(fp.numBuffers());
Brian Salomonf9f45122016-11-29 11:59:17 -0500181 SkSTArray<2, ImageStorageHandle> imageStorageArray(fp.numImageStorages());
bsalomonb58a2b42016-09-26 06:55:02 -0700182 GrFragmentProcessor::Iter iter(&fp);
183 while (const GrFragmentProcessor* subFP = iter.next()) {
Greg Danielbc5d4d72017-05-05 10:28:42 -0400184 this->emitSamplersAndImageStorages(*subFP, &textureSamplerArray, &texelBufferArray,
Brian Salomonf9f45122016-11-29 11:59:17 -0500185 &imageStorageArray);
bsalomonb58a2b42016-09-26 06:55:02 -0700186 }
egdanielfa896322016-01-13 12:19:30 -0800187
bsalomona624bf32016-09-20 09:12:47 -0700188 const GrShaderVar* coordVars = fTransformedCoordVars.begin() + transformedCoordVarsIdx;
189 GrGLSLFragmentProcessor::TransformedCoordVars coords(&fp, coordVars);
bsalomonb58a2b42016-09-26 06:55:02 -0700190 GrGLSLFragmentProcessor::TextureSamplers textureSamplers(&fp, textureSamplerArray.begin());
Greg Danielbc5d4d72017-05-05 10:28:42 -0400191 GrGLSLFragmentProcessor::TexelBuffers texelBuffers(&fp, texelBufferArray.begin());
Brian Salomonf9f45122016-11-29 11:59:17 -0500192 GrGLSLFragmentProcessor::ImageStorages imageStorages(&fp, imageStorageArray.begin());
egdanielfa896322016-01-13 12:19:30 -0800193 GrGLSLFragmentProcessor::EmitArgs args(&fFS,
194 this->uniformHandler(),
Brian Salomon94efbf52016-11-29 13:43:05 -0500195 this->shaderCaps(),
egdanielfa896322016-01-13 12:19:30 -0800196 fp,
Ethan Nicholas2983f402017-05-08 09:36:08 -0400197 output.c_str(),
198 input.c_str(),
bsalomona624bf32016-09-20 09:12:47 -0700199 coords,
bsalomonb58a2b42016-09-26 06:55:02 -0700200 textureSamplers,
Greg Danielbc5d4d72017-05-05 10:28:42 -0400201 texelBuffers,
Brian Salomonf9f45122016-11-29 11:59:17 -0500202 imageStorages,
dvonbeck9b03e7b2016-08-01 11:01:56 -0700203 this->primitiveProcessor().implementsDistanceVector());
204
egdanielfa896322016-01-13 12:19:30 -0800205 fragProc->emitCode(args);
206
207 // We have to check that effects and the code they emit are consistent, ie if an effect
208 // asks for dst color, then the emit code needs to follow suit
cdalton87332102016-02-26 12:22:02 -0800209 SkDEBUGCODE(verify(fp);)
egdanielfa896322016-01-13 12:19:30 -0800210 fFragmentProcessors.push_back(fragProc);
211
212 fFS.codeAppend("}");
Ethan Nicholas2983f402017-05-08 09:36:08 -0400213 return output;
egdanielfa896322016-01-13 12:19:30 -0800214}
215
Ethan Nicholas2983f402017-05-08 09:36:08 -0400216void GrGLSLProgramBuilder::emitAndInstallXferProc(const SkString& colorIn,
217 const SkString& coverageIn) {
egdanielfa896322016-01-13 12:19:30 -0800218 // Program builders have a bit of state we need to clear with each effect
219 AutoStageAdvance adv(this);
220
221 SkASSERT(!fXferProcessor);
Brian Salomon18dfa982017-04-03 16:57:43 -0400222 const GrXferProcessor& xp = fPipeline.getXferProcessor();
egdanielfa896322016-01-13 12:19:30 -0800223 fXferProcessor = xp.createGLSLInstance();
224
225 // Enable dual source secondary output if we have one
226 if (xp.hasSecondaryOutput()) {
227 fFS.enableSecondaryOutput();
228 }
229
Brian Salomon94efbf52016-11-29 13:43:05 -0500230 if (this->shaderCaps()->mustDeclareFragmentShaderOutput()) {
egdanielfa896322016-01-13 12:19:30 -0800231 fFS.enableCustomOutput();
232 }
233
234 SkString openBrace;
235 openBrace.printf("{ // Xfer Processor: %s\n", xp.name());
236 fFS.codeAppend(openBrace.c_str());
237
Brian Salomon18dfa982017-04-03 16:57:43 -0400238 SamplerHandle dstTextureSamplerHandle;
239 GrSurfaceOrigin dstTextureOrigin = kTopLeft_GrSurfaceOrigin;
240 if (GrTexture* dstTexture = fPipeline.dstTexture()) {
241 // GrProcessor::TextureSampler sampler(dstTexture);
242 SkString name("DstTextureSampler");
243 dstTextureSamplerHandle =
244 this->emitSampler(dstTexture->texturePriv().samplerType(), dstTexture->config(),
245 "DstTextureSampler", kFragment_GrShaderFlag);
246 dstTextureOrigin = dstTexture->origin();
247 SkASSERT(kTextureExternalSampler_GrSLType != dstTexture->texturePriv().samplerType());
248 }
egdanielfa896322016-01-13 12:19:30 -0800249
250 GrGLSLXferProcessor::EmitArgs args(&fFS,
251 this->uniformHandler(),
Brian Salomon94efbf52016-11-29 13:43:05 -0500252 this->shaderCaps(),
Brian Salomon18dfa982017-04-03 16:57:43 -0400253 xp,
Ethan Nicholas2983f402017-05-08 09:36:08 -0400254 colorIn.size() ? colorIn.c_str() : "vec4(1)",
255 coverageIn.size() ? coverageIn.c_str() : "vec4(1)",
egdanielfa896322016-01-13 12:19:30 -0800256 fFS.getPrimaryColorOutputName(),
257 fFS.getSecondaryColorOutputName(),
Brian Salomon18dfa982017-04-03 16:57:43 -0400258 dstTextureSamplerHandle,
259 dstTextureOrigin);
egdanielfa896322016-01-13 12:19:30 -0800260 fXferProcessor->emitCode(args);
261
262 // We have to check that effects and the code they emit are consistent, ie if an effect
263 // asks for dst color, then the emit code needs to follow suit
cdalton87332102016-02-26 12:22:02 -0800264 SkDEBUGCODE(verify(xp);)
egdanielfa896322016-01-13 12:19:30 -0800265 fFS.codeAppend("}");
266}
267
Brian Salomonf9f45122016-11-29 11:59:17 -0500268void GrGLSLProgramBuilder::emitSamplersAndImageStorages(
Brian Salomonab015ef2017-04-04 10:15:51 -0400269 const GrResourceIOProcessor& processor,
Brian Salomonf9f45122016-11-29 11:59:17 -0500270 SkTArray<SamplerHandle>* outTexSamplerHandles,
Greg Danielbc5d4d72017-05-05 10:28:42 -0400271 SkTArray<TexelBufferHandle>* outTexelBufferHandles,
Brian Salomonf9f45122016-11-29 11:59:17 -0500272 SkTArray<ImageStorageHandle>* outImageStorageHandles) {
cdalton9c3f1432016-03-11 10:07:37 -0800273 SkString name;
Brian Salomon0bbecb22016-11-17 11:38:22 -0500274 int numTextureSamplers = processor.numTextureSamplers();
275 for (int t = 0; t < numTextureSamplers; ++t) {
Brian Salomonab015ef2017-04-04 10:15:51 -0400276 const GrResourceIOProcessor::TextureSampler& sampler = processor.textureSampler(t);
Brian Salomonf9f45122016-11-29 11:59:17 -0500277 name.printf("TextureSampler_%d", outTexSamplerHandles->count());
Brian Salomondb4183d2016-11-17 12:48:40 -0500278 GrSLType samplerType = sampler.texture()->texturePriv().samplerType();
egdaniel990dbc82016-07-13 14:09:30 -0700279 if (kTextureExternalSampler_GrSLType == samplerType) {
Brian Salomon94efbf52016-11-29 13:43:05 -0500280 const char* externalFeatureString =
281 this->shaderCaps()->externalTextureExtensionString();
cdalton9c3f1432016-03-11 10:07:37 -0800282 // We shouldn't ever create a GrGLTexture that requires external sampler type
283 SkASSERT(externalFeatureString);
Brian Salomondb4183d2016-11-17 12:48:40 -0500284 this->addFeature(sampler.visibility(),
cdalton9c3f1432016-03-11 10:07:37 -0800285 1 << GrGLSLShaderBuilder::kExternalTexture_GLSLPrivateFeature,
286 externalFeatureString);
287 }
Brian Salomon18dfa982017-04-03 16:57:43 -0400288 outTexSamplerHandles->emplace_back(this->emitSampler(
289 samplerType, sampler.texture()->config(), name.c_str(), sampler.visibility()));
cdalton9c3f1432016-03-11 10:07:37 -0800290 }
cdalton74b8d322016-04-11 14:47:28 -0700291 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 Salomonab015ef2017-04-04 10:15:51 -0400296 const GrResourceIOProcessor::BufferAccess& access = processor.bufferAccess(b);
Greg Danielbc5d4d72017-05-05 10:28:42 -0400297 name.printf("TexelBuffer_%d", outTexelBufferHandles->count());
298 outTexelBufferHandles->emplace_back(
299 this->emitTexelBuffer(access.texelConfig(), name.c_str(), access.visibility()));
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) {
Brian Salomonab015ef2017-04-04 10:15:51 -0400311 const GrResourceIOProcessor::ImageStorageAccess& imageStorageAccess =
312 processor.imageStorageAccess(i);
Brian Salomonf9f45122016-11-29 11:59:17 -0500313 name.printf("Image_%d", outImageStorageHandles->count());
Brian Salomon18dfa982017-04-03 16:57:43 -0400314 outImageStorageHandles->emplace_back(
315 this->emitImageStorage(imageStorageAccess, name.c_str()));
Brian Salomonf9f45122016-11-29 11:59:17 -0500316 }
cdalton74b8d322016-04-11 14:47:28 -0700317}
318
Greg Danielbc5d4d72017-05-05 10:28:42 -0400319void GrGLSLProgramBuilder::updateSamplerCounts(GrShaderFlags visibility) {
cdalton74b8d322016-04-11 14:47:28 -0700320 if (visibility & kVertex_GrShaderFlag) {
321 ++fNumVertexSamplers;
322 }
323 if (visibility & kGeometry_GrShaderFlag) {
324 SkASSERT(this->primitiveProcessor().willUseGeoShader());
325 ++fNumGeometrySamplers;
326 }
327 if (visibility & kFragment_GrShaderFlag) {
328 ++fNumFragmentSamplers;
329 }
Greg Danielbc5d4d72017-05-05 10:28:42 -0400330}
331
332GrGLSLProgramBuilder::SamplerHandle GrGLSLProgramBuilder::emitSampler(GrSLType samplerType,
333 GrPixelConfig config,
334 const char* name,
335 GrShaderFlags visibility) {
336 this->updateSamplerCounts(visibility);
Brian Salomon94efbf52016-11-29 13:43:05 -0500337 GrSLPrecision precision = this->shaderCaps()->samplerPrecision(config, visibility);
338 GrSwizzle swizzle = this->shaderCaps()->configTextureSwizzle(config);
Brian Salomon18dfa982017-04-03 16:57:43 -0400339 return this->uniformHandler()->addSampler(visibility, swizzle, samplerType, precision, name);
Brian Salomonf9f45122016-11-29 11:59:17 -0500340}
341
Greg Danielbc5d4d72017-05-05 10:28:42 -0400342GrGLSLProgramBuilder::TexelBufferHandle GrGLSLProgramBuilder::emitTexelBuffer(
343 GrPixelConfig config, const char* name, GrShaderFlags visibility) {
344 this->updateSamplerCounts(visibility);
345 GrSLPrecision precision = this->shaderCaps()->samplerPrecision(config, visibility);
346 return this->uniformHandler()->addTexelBuffer(visibility, precision, name);
347}
348
Brian Salomon18dfa982017-04-03 16:57:43 -0400349GrGLSLProgramBuilder::ImageStorageHandle GrGLSLProgramBuilder::emitImageStorage(
Brian Salomonab015ef2017-04-04 10:15:51 -0400350 const GrResourceIOProcessor::ImageStorageAccess& access, const char* name) {
Brian Salomonf9f45122016-11-29 11:59:17 -0500351 if (access.visibility() & kVertex_GrShaderFlag) {
352 ++fNumVertexImageStorages;
353 }
354 if (access.visibility() & kGeometry_GrShaderFlag) {
355 SkASSERT(this->primitiveProcessor().willUseGeoShader());
356 ++fNumGeometryImageStorages;
357 }
358 if (access.visibility() & kFragment_GrShaderFlag) {
359 ++fNumFragmentImageStorages;
360 }
Robert Phillips8a02f652017-05-12 14:49:16 -0400361 GrSLType uniformType = access.proxy()->imageStorageType();
Brian Salomon18dfa982017-04-03 16:57:43 -0400362 return this->uniformHandler()->addImageStorage(access.visibility(), uniformType,
363 access.format(), access.memoryModel(),
364 access.restrict(), access.ioType(), name);
cdalton9c3f1432016-03-11 10:07:37 -0800365}
366
egdanielfa896322016-01-13 12:19:30 -0800367void GrGLSLProgramBuilder::emitFSOutputSwizzle(bool hasSecondaryOutput) {
368 // Swizzle the fragment shader outputs if necessary.
369 GrSwizzle swizzle;
Ethan Nicholas38657112017-02-09 17:01:22 -0500370 swizzle.setFromKey(this->desc()->header().fOutputSwizzle);
egdanielfa896322016-01-13 12:19:30 -0800371 if (swizzle != GrSwizzle::RGBA()) {
372 fFS.codeAppendf("%s = %s.%s;", fFS.getPrimaryColorOutputName(),
373 fFS.getPrimaryColorOutputName(),
374 swizzle.c_str());
375 if (hasSecondaryOutput) {
376 fFS.codeAppendf("%s = %s.%s;", fFS.getSecondaryColorOutputName(),
377 fFS.getSecondaryColorOutputName(),
378 swizzle.c_str());
379 }
380 }
381}
382
cdalton9c3f1432016-03-11 10:07:37 -0800383bool GrGLSLProgramBuilder::checkSamplerCounts() {
Brian Salomon1edc5b92016-11-29 13:43:46 -0500384 const GrShaderCaps& shaderCaps = *this->shaderCaps();
385 if (fNumVertexSamplers > shaderCaps.maxVertexSamplers()) {
cdalton9c3f1432016-03-11 10:07:37 -0800386 GrCapsDebugf(this->caps(), "Program would use too many vertex samplers\n");
387 return false;
388 }
Brian Salomon1edc5b92016-11-29 13:43:46 -0500389 if (fNumGeometrySamplers > shaderCaps.maxGeometrySamplers()) {
cdalton9c3f1432016-03-11 10:07:37 -0800390 GrCapsDebugf(this->caps(), "Program would use too many geometry samplers\n");
391 return false;
392 }
Brian Salomon1edc5b92016-11-29 13:43:46 -0500393 if (fNumFragmentSamplers > shaderCaps.maxFragmentSamplers()) {
cdalton9c3f1432016-03-11 10:07:37 -0800394 GrCapsDebugf(this->caps(), "Program would use too many fragment samplers\n");
395 return false;
396 }
397 // If the same sampler is used in two different shaders, it counts as two combined samplers.
398 int numCombinedSamplers = fNumVertexSamplers + fNumGeometrySamplers + fNumFragmentSamplers;
Brian Salomon1edc5b92016-11-29 13:43:46 -0500399 if (numCombinedSamplers > shaderCaps.maxCombinedSamplers()) {
cdalton9c3f1432016-03-11 10:07:37 -0800400 GrCapsDebugf(this->caps(), "Program would use too many combined samplers\n");
401 return false;
402 }
403 return true;
404}
405
Brian Salomonf9f45122016-11-29 11:59:17 -0500406bool GrGLSLProgramBuilder::checkImageStorageCounts() {
Brian Salomon1edc5b92016-11-29 13:43:46 -0500407 const GrShaderCaps& shaderCaps = *this->shaderCaps();
408 if (fNumVertexImageStorages > shaderCaps.maxVertexImageStorages()) {
Brian Salomonf9f45122016-11-29 11:59:17 -0500409 GrCapsDebugf(this->caps(), "Program would use too many vertex images\n");
410 return false;
411 }
Brian Salomon1edc5b92016-11-29 13:43:46 -0500412 if (fNumGeometryImageStorages > shaderCaps.maxGeometryImageStorages()) {
Brian Salomonf9f45122016-11-29 11:59:17 -0500413 GrCapsDebugf(this->caps(), "Program would use too many geometry images\n");
414 return false;
415 }
Brian Salomon1edc5b92016-11-29 13:43:46 -0500416 if (fNumFragmentImageStorages > shaderCaps.maxFragmentImageStorages()) {
Brian Salomonf9f45122016-11-29 11:59:17 -0500417 GrCapsDebugf(this->caps(), "Program would use too many fragment images\n");
418 return false;
419 }
420 // If the same image is used in two different shaders, it counts as two combined images.
421 int numCombinedImages = fNumVertexImageStorages + fNumGeometryImageStorages +
422 fNumFragmentImageStorages;
Brian Salomon1edc5b92016-11-29 13:43:46 -0500423 if (numCombinedImages > shaderCaps.maxCombinedImageStorages()) {
Brian Salomonf9f45122016-11-29 11:59:17 -0500424 GrCapsDebugf(this->caps(), "Program would use too many combined images\n");
425 return false;
426 }
427 return true;
428}
429
cdalton87332102016-02-26 12:22:02 -0800430#ifdef SK_DEBUG
egdanielfa896322016-01-13 12:19:30 -0800431void GrGLSLProgramBuilder::verify(const GrPrimitiveProcessor& gp) {
cdalton87332102016-02-26 12:22:02 -0800432 SkASSERT(fFS.usedProcessorFeatures() == gp.requiredFeatures());
egdanielfa896322016-01-13 12:19:30 -0800433}
434
435void GrGLSLProgramBuilder::verify(const GrXferProcessor& xp) {
cdalton87332102016-02-26 12:22:02 -0800436 SkASSERT(fFS.usedProcessorFeatures() == xp.requiredFeatures());
egdanielfa896322016-01-13 12:19:30 -0800437 SkASSERT(fFS.hasReadDstColor() == xp.willReadDstColor());
438}
439
440void GrGLSLProgramBuilder::verify(const GrFragmentProcessor& fp) {
cdalton87332102016-02-26 12:22:02 -0800441 SkASSERT(fFS.usedProcessorFeatures() == fp.requiredFeatures());
egdaniel8dcdedc2015-11-11 06:27:20 -0800442}
cdalton87332102016-02-26 12:22:02 -0800443#endif
egdaniel8dcdedc2015-11-11 06:27:20 -0800444
445void GrGLSLProgramBuilder::nameVariable(SkString* out, char prefix, const char* name, bool mangle) {
446 if ('\0' == prefix) {
447 *out = name;
448 } else {
449 out->printf("%c%s", prefix, name);
450 }
451 if (mangle) {
452 if (out->endsWith('_')) {
453 // Names containing "__" are reserved.
454 out->append("x");
455 }
456 out->appendf("_Stage%d%s", fStageIndex, fFS.getMangleString().c_str());
457 }
458}
459
Ethan Nicholas2983f402017-05-08 09:36:08 -0400460void GrGLSLProgramBuilder::nameExpression(SkString* output, const char* baseName) {
egdanielfa896322016-01-13 12:19:30 -0800461 // create var to hold stage result. If we already have a valid output name, just use that
462 // otherwise create a new mangled one. This name is only valid if we are reordering stages
463 // and have to tell stage exactly where to put its output.
464 SkString outName;
Ethan Nicholas2983f402017-05-08 09:36:08 -0400465 if (output->size()) {
egdanielfa896322016-01-13 12:19:30 -0800466 outName = output->c_str();
467 } else {
468 this->nameVariable(&outName, '\0', baseName);
469 }
470 fFS.codeAppendf("vec4 %s;", outName.c_str());
471 *output = outName;
472}
473
cdalton5e58cee2016-02-11 12:49:47 -0800474void GrGLSLProgramBuilder::appendUniformDecls(GrShaderFlags visibility, SkString* out) const {
egdaniel7ea439b2015-12-03 09:20:44 -0800475 this->uniformHandler()->appendUniformDecls(visibility, out);
476}
477
Ethan Nicholas941e7e22016-12-12 15:33:30 -0500478void GrGLSLProgramBuilder::addRTHeightUniform(const char* name) {
egdaniel7ea439b2015-12-03 09:20:44 -0800479 SkASSERT(!fUniformHandles.fRTHeightUni.isValid());
480 GrGLSLUniformHandler* uniformHandler = this->uniformHandler();
481 fUniformHandles.fRTHeightUni =
cdalton5e58cee2016-02-11 12:49:47 -0800482 uniformHandler->internalAddUniformArray(kFragment_GrShaderFlag,
egdaniel7ea439b2015-12-03 09:20:44 -0800483 kFloat_GrSLType, kDefault_GrSLPrecision,
Ethan Nicholas941e7e22016-12-12 15:33:30 -0500484 name, false, 0, nullptr);
egdaniel8dcdedc2015-11-11 06:27:20 -0800485}
486
egdanielfa896322016-01-13 12:19:30 -0800487void GrGLSLProgramBuilder::cleanupFragmentProcessors() {
488 for (int i = 0; i < fFragmentProcessors.count(); ++i) {
489 delete fFragmentProcessors[i];
490 }
491}
492
egdaniel9f1d4152016-02-10 09:50:38 -0800493void GrGLSLProgramBuilder::finalizeShaders() {
494 this->varyingHandler()->finalize();
cdalton5e58cee2016-02-11 12:49:47 -0800495 fVS.finalize(kVertex_GrShaderFlag);
csmartdalton276cc412016-11-21 11:55:00 -0700496 if (this->primitiveProcessor().willUseGeoShader()) {
Brian Salomon94efbf52016-11-29 13:43:05 -0500497 SkASSERT(this->shaderCaps()->geometryShaderSupport());
csmartdalton276cc412016-11-21 11:55:00 -0700498 fGS.finalize(kGeometry_GrShaderFlag);
499 }
cdalton5e58cee2016-02-11 12:49:47 -0800500 fFS.finalize(kFragment_GrShaderFlag);
egdaniel9f1d4152016-02-10 09:50:38 -0800501}