egdaniel | 8dcdedc | 2015-11-11 06:27:20 -0800 | [diff] [blame] | 1 | /* |
| 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 | |
egdaniel | fa89632 | 2016-01-13 12:19:30 -0800 | [diff] [blame] | 10 | #include "GrPipeline.h" |
Brian Salomon | 739c5bf | 2016-11-07 09:53:44 -0500 | [diff] [blame] | 11 | #include "GrTexturePriv.h" |
egdaniel | fa89632 | 2016-01-13 12:19:30 -0800 | [diff] [blame] | 12 | #include "glsl/GrGLSLFragmentProcessor.h" |
| 13 | #include "glsl/GrGLSLGeometryProcessor.h" |
egdaniel | 9f1d415 | 2016-02-10 09:50:38 -0800 | [diff] [blame] | 14 | #include "glsl/GrGLSLVarying.h" |
egdaniel | fa89632 | 2016-01-13 12:19:30 -0800 | [diff] [blame] | 15 | #include "glsl/GrGLSLXferProcessor.h" |
| 16 | |
egdaniel | 8dcdedc | 2015-11-11 06:27:20 -0800 | [diff] [blame] | 17 | const int GrGLSLProgramBuilder::kVarsPerBlock = 8; |
| 18 | |
egdaniel | 0e1853c | 2016-03-17 11:35:45 -0700 | [diff] [blame] | 19 | GrGLSLProgramBuilder::GrGLSLProgramBuilder(const GrPipeline& pipeline, |
| 20 | const GrPrimitiveProcessor& primProc, |
| 21 | const GrProgramDesc& desc) |
egdaniel | 8dcdedc | 2015-11-11 06:27:20 -0800 | [diff] [blame] | 22 | : fVS(this) |
| 23 | , fGS(this) |
cdalton | 28f45b9 | 2016-03-07 13:58:26 -0800 | [diff] [blame] | 24 | , fFS(this) |
egdaniel | 8dcdedc | 2015-11-11 06:27:20 -0800 | [diff] [blame] | 25 | , fStageIndex(-1) |
egdaniel | 0e1853c | 2016-03-17 11:35:45 -0700 | [diff] [blame] | 26 | , fPipeline(pipeline) |
| 27 | , fPrimProc(primProc) |
| 28 | , fDesc(desc) |
egdaniel | fa89632 | 2016-01-13 12:19:30 -0800 | [diff] [blame] | 29 | , fGeometryProcessor(nullptr) |
cdalton | 9c3f143 | 2016-03-11 10:07:37 -0800 | [diff] [blame] | 30 | , fXferProcessor(nullptr) |
cdalton | 9c3f143 | 2016-03-11 10:07:37 -0800 | [diff] [blame] | 31 | , fNumVertexSamplers(0) |
| 32 | , fNumGeometrySamplers(0) |
| 33 | , fNumFragmentSamplers(0) { |
| 34 | } |
| 35 | |
| 36 | void GrGLSLProgramBuilder::addFeature(GrShaderFlags shaders, |
| 37 | uint32_t featureBit, |
| 38 | const char* extensionName) { |
| 39 | if (shaders & kVertex_GrShaderFlag) { |
| 40 | fVS.addFeature(featureBit, extensionName); |
| 41 | } |
| 42 | if (shaders & kGeometry_GrShaderFlag) { |
| 43 | SkASSERT(this->glslCaps()->geometryShaderSupport()); |
| 44 | fGS.addFeature(featureBit, extensionName); |
| 45 | } |
| 46 | if (shaders & kFragment_GrShaderFlag) { |
| 47 | fFS.addFeature(featureBit, extensionName); |
| 48 | } |
egdaniel | fa89632 | 2016-01-13 12:19:30 -0800 | [diff] [blame] | 49 | } |
| 50 | |
| 51 | bool GrGLSLProgramBuilder::emitAndInstallProcs(GrGLSLExpr4* inputColor, |
cdalton | 9c3f143 | 2016-03-11 10:07:37 -0800 | [diff] [blame] | 52 | GrGLSLExpr4* inputCoverage) { |
egdaniel | fa89632 | 2016-01-13 12:19:30 -0800 | [diff] [blame] | 53 | // First we loop over all of the installed processors and collect coord transforms. These will |
| 54 | // be sent to the GrGLSLPrimitiveProcessor in its emitCode function |
| 55 | const GrPrimitiveProcessor& primProc = this->primitiveProcessor(); |
egdaniel | fa89632 | 2016-01-13 12:19:30 -0800 | [diff] [blame] | 56 | |
egdaniel | fa89632 | 2016-01-13 12:19:30 -0800 | [diff] [blame] | 57 | this->emitAndInstallPrimProc(primProc, inputColor, inputCoverage); |
| 58 | |
bsalomon | a624bf3 | 2016-09-20 09:12:47 -0700 | [diff] [blame] | 59 | this->emitAndInstallFragProcs(inputColor, inputCoverage); |
halcanary | 9d524f2 | 2016-03-29 09:03:52 -0700 | [diff] [blame] | 60 | if (primProc.getPixelLocalStorageState() != |
ethannicholas | 2279325 | 2016-01-30 09:59:10 -0800 | [diff] [blame] | 61 | GrPixelLocalStorageState::kDraw_GrPixelLocalStorageState) { |
halcanary | 9d524f2 | 2016-03-29 09:03:52 -0700 | [diff] [blame] | 62 | this->emitAndInstallXferProc(this->pipeline().getXferProcessor(), *inputColor, |
ethannicholas | 2279325 | 2016-01-30 09:59:10 -0800 | [diff] [blame] | 63 | *inputCoverage, this->pipeline().ignoresCoverage(), |
| 64 | primProc.getPixelLocalStorageState()); |
| 65 | this->emitFSOutputSwizzle(this->pipeline().getXferProcessor().hasSecondaryOutput()); |
| 66 | } |
cdalton | 9c3f143 | 2016-03-11 10:07:37 -0800 | [diff] [blame] | 67 | |
| 68 | return this->checkSamplerCounts(); |
egdaniel | fa89632 | 2016-01-13 12:19:30 -0800 | [diff] [blame] | 69 | } |
| 70 | |
| 71 | void GrGLSLProgramBuilder::emitAndInstallPrimProc(const GrPrimitiveProcessor& proc, |
| 72 | GrGLSLExpr4* outputColor, |
| 73 | GrGLSLExpr4* outputCoverage) { |
| 74 | // Program builders have a bit of state we need to clear with each effect |
| 75 | AutoStageAdvance adv(this); |
| 76 | this->nameExpression(outputColor, "outputColor"); |
| 77 | this->nameExpression(outputCoverage, "outputCoverage"); |
| 78 | |
dvonbeck | 9b03e7b | 2016-08-01 11:01:56 -0700 | [diff] [blame] | 79 | const char* distanceVectorName = nullptr; |
| 80 | if (this->fPipeline.usesDistanceVectorField() && proc.implementsDistanceVector()) { |
robertphillips | 05a4cf5 | 2016-09-08 09:02:43 -0700 | [diff] [blame] | 81 | // 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. |
dvonbeck | 9b03e7b | 2016-08-01 11:01:56 -0700 | [diff] [blame] | 84 | distanceVectorName = fFS.distanceVectorName(); |
dvonbeck | ee92063 | 2016-08-11 14:17:59 -0700 | [diff] [blame] | 85 | fFS.codeAppend( "// Normalized vector to the closest geometric edge (in device space)\n"); |
dvonbeck | 84bca78 | 2016-08-08 11:47:12 -0700 | [diff] [blame] | 86 | fFS.codeAppend( "// Distance to the edge encoded in the z-component\n"); |
jvanverth | 6c177a1 | 2016-08-17 07:59:41 -0700 | [diff] [blame] | 87 | fFS.codeAppendf("vec4 %s;", distanceVectorName); |
dvonbeck | 9b03e7b | 2016-08-01 11:01:56 -0700 | [diff] [blame] | 88 | } |
| 89 | |
egdaniel | fa89632 | 2016-01-13 12:19:30 -0800 | [diff] [blame] | 90 | // Enclose custom code in a block to avoid namespace conflicts |
| 91 | SkString openBrace; |
| 92 | openBrace.printf("{ // Stage %d, %s\n", fStageIndex, proc.name()); |
| 93 | fFS.codeAppend(openBrace.c_str()); |
| 94 | fVS.codeAppendf("// Primitive Processor %s\n", proc.name()); |
| 95 | |
| 96 | SkASSERT(!fGeometryProcessor); |
| 97 | fGeometryProcessor = proc.createGLSLInstance(*this->glslCaps()); |
| 98 | |
egdaniel | 09aa1fc | 2016-04-20 07:09:46 -0700 | [diff] [blame] | 99 | SkSTArray<4, SamplerHandle> texSamplers(proc.numTextures()); |
| 100 | SkSTArray<2, SamplerHandle> bufferSamplers(proc.numBuffers()); |
cdalton | 74b8d32 | 2016-04-11 14:47:28 -0700 | [diff] [blame] | 101 | this->emitSamplers(proc, &texSamplers, &bufferSamplers); |
egdaniel | fa89632 | 2016-01-13 12:19:30 -0800 | [diff] [blame] | 102 | |
bsalomon | a624bf3 | 2016-09-20 09:12:47 -0700 | [diff] [blame] | 103 | GrGLSLPrimitiveProcessor::FPCoordTransformHandler transformHandler(fPipeline, |
| 104 | &fTransformedCoordVars); |
egdaniel | fa89632 | 2016-01-13 12:19:30 -0800 | [diff] [blame] | 105 | GrGLSLGeometryProcessor::EmitArgs args(&fVS, |
| 106 | &fFS, |
| 107 | this->varyingHandler(), |
| 108 | this->uniformHandler(), |
| 109 | this->glslCaps(), |
| 110 | proc, |
| 111 | outputColor->c_str(), |
| 112 | outputCoverage->c_str(), |
dvonbeck | 9b03e7b | 2016-08-01 11:01:56 -0700 | [diff] [blame] | 113 | distanceVectorName, |
egdaniel | 09aa1fc | 2016-04-20 07:09:46 -0700 | [diff] [blame] | 114 | texSamplers.begin(), |
| 115 | bufferSamplers.begin(), |
bsalomon | a624bf3 | 2016-09-20 09:12:47 -0700 | [diff] [blame] | 116 | &transformHandler); |
egdaniel | fa89632 | 2016-01-13 12:19:30 -0800 | [diff] [blame] | 117 | fGeometryProcessor->emitCode(args); |
| 118 | |
| 119 | // We have to check that effects and the code they emit are consistent, ie if an effect |
| 120 | // asks for dst color, then the emit code needs to follow suit |
cdalton | 8733210 | 2016-02-26 12:22:02 -0800 | [diff] [blame] | 121 | SkDEBUGCODE(verify(proc);) |
egdaniel | fa89632 | 2016-01-13 12:19:30 -0800 | [diff] [blame] | 122 | |
| 123 | fFS.codeAppend("}"); |
| 124 | } |
| 125 | |
bsalomon | a624bf3 | 2016-09-20 09:12:47 -0700 | [diff] [blame] | 126 | void GrGLSLProgramBuilder::emitAndInstallFragProcs(GrGLSLExpr4* color, GrGLSLExpr4* coverage) { |
| 127 | int transformedCoordVarsIdx = 0; |
| 128 | GrGLSLExpr4** inOut = &color; |
| 129 | for (int i = 0; i < this->pipeline().numFragmentProcessors(); ++i) { |
| 130 | if (i == this->pipeline().numColorFragmentProcessors()) { |
| 131 | inOut = &coverage; |
| 132 | } |
egdaniel | fa89632 | 2016-01-13 12:19:30 -0800 | [diff] [blame] | 133 | GrGLSLExpr4 output; |
| 134 | const GrFragmentProcessor& fp = this->pipeline().getFragmentProcessor(i); |
bsalomon | a624bf3 | 2016-09-20 09:12:47 -0700 | [diff] [blame] | 135 | this->emitAndInstallFragProc(fp, i, transformedCoordVarsIdx, **inOut, &output); |
| 136 | GrFragmentProcessor::Iter iter(&fp); |
| 137 | while (const GrFragmentProcessor* fp = iter.next()) { |
| 138 | transformedCoordVarsIdx += fp->numCoordTransforms(); |
| 139 | } |
| 140 | **inOut = output; |
egdaniel | fa89632 | 2016-01-13 12:19:30 -0800 | [diff] [blame] | 141 | } |
| 142 | } |
| 143 | |
| 144 | // TODO Processors cannot output zeros because an empty string is all 1s |
| 145 | // the fix is to allow effects to take the GrGLSLExpr4 directly |
| 146 | void GrGLSLProgramBuilder::emitAndInstallFragProc(const GrFragmentProcessor& fp, |
| 147 | int index, |
bsalomon | a624bf3 | 2016-09-20 09:12:47 -0700 | [diff] [blame] | 148 | int transformedCoordVarsIdx, |
egdaniel | fa89632 | 2016-01-13 12:19:30 -0800 | [diff] [blame] | 149 | const GrGLSLExpr4& input, |
| 150 | GrGLSLExpr4* output) { |
| 151 | // Program builders have a bit of state we need to clear with each effect |
| 152 | AutoStageAdvance adv(this); |
| 153 | this->nameExpression(output, "output"); |
| 154 | |
| 155 | // Enclose custom code in a block to avoid namespace conflicts |
| 156 | SkString openBrace; |
| 157 | openBrace.printf("{ // Stage %d, %s\n", fStageIndex, fp.name()); |
| 158 | fFS.codeAppend(openBrace.c_str()); |
| 159 | |
| 160 | GrGLSLFragmentProcessor* fragProc = fp.createGLSLInstance(); |
| 161 | |
bsalomon | b58a2b4 | 2016-09-26 06:55:02 -0700 | [diff] [blame] | 162 | SkSTArray<4, SamplerHandle> textureSamplerArray(fp.numTextures()); |
| 163 | SkSTArray<2, SamplerHandle> bufferSamplerArray(fp.numBuffers()); |
| 164 | GrFragmentProcessor::Iter iter(&fp); |
| 165 | while (const GrFragmentProcessor* subFP = iter.next()) { |
| 166 | this->emitSamplers(*subFP, &textureSamplerArray, &bufferSamplerArray); |
| 167 | } |
egdaniel | fa89632 | 2016-01-13 12:19:30 -0800 | [diff] [blame] | 168 | |
bsalomon | a624bf3 | 2016-09-20 09:12:47 -0700 | [diff] [blame] | 169 | const GrShaderVar* coordVars = fTransformedCoordVars.begin() + transformedCoordVarsIdx; |
| 170 | GrGLSLFragmentProcessor::TransformedCoordVars coords(&fp, coordVars); |
bsalomon | b58a2b4 | 2016-09-26 06:55:02 -0700 | [diff] [blame] | 171 | GrGLSLFragmentProcessor::TextureSamplers textureSamplers(&fp, textureSamplerArray.begin()); |
| 172 | GrGLSLFragmentProcessor::BufferSamplers bufferSamplers(&fp, bufferSamplerArray.begin()); |
egdaniel | fa89632 | 2016-01-13 12:19:30 -0800 | [diff] [blame] | 173 | GrGLSLFragmentProcessor::EmitArgs args(&fFS, |
| 174 | this->uniformHandler(), |
| 175 | this->glslCaps(), |
| 176 | fp, |
| 177 | output->c_str(), |
| 178 | input.isOnes() ? nullptr : input.c_str(), |
bsalomon | a624bf3 | 2016-09-20 09:12:47 -0700 | [diff] [blame] | 179 | coords, |
bsalomon | b58a2b4 | 2016-09-26 06:55:02 -0700 | [diff] [blame] | 180 | textureSamplers, |
| 181 | bufferSamplers, |
dvonbeck | 9b03e7b | 2016-08-01 11:01:56 -0700 | [diff] [blame] | 182 | this->primitiveProcessor().implementsDistanceVector()); |
| 183 | |
egdaniel | fa89632 | 2016-01-13 12:19:30 -0800 | [diff] [blame] | 184 | fragProc->emitCode(args); |
| 185 | |
| 186 | // We have to check that effects and the code they emit are consistent, ie if an effect |
| 187 | // asks for dst color, then the emit code needs to follow suit |
cdalton | 8733210 | 2016-02-26 12:22:02 -0800 | [diff] [blame] | 188 | SkDEBUGCODE(verify(fp);) |
egdaniel | fa89632 | 2016-01-13 12:19:30 -0800 | [diff] [blame] | 189 | fFragmentProcessors.push_back(fragProc); |
| 190 | |
| 191 | fFS.codeAppend("}"); |
| 192 | } |
| 193 | |
| 194 | void GrGLSLProgramBuilder::emitAndInstallXferProc(const GrXferProcessor& xp, |
| 195 | const GrGLSLExpr4& colorIn, |
| 196 | const GrGLSLExpr4& coverageIn, |
ethannicholas | 2279325 | 2016-01-30 09:59:10 -0800 | [diff] [blame] | 197 | bool ignoresCoverage, |
| 198 | GrPixelLocalStorageState plsState) { |
egdaniel | fa89632 | 2016-01-13 12:19:30 -0800 | [diff] [blame] | 199 | // Program builders have a bit of state we need to clear with each effect |
| 200 | AutoStageAdvance adv(this); |
| 201 | |
| 202 | SkASSERT(!fXferProcessor); |
| 203 | fXferProcessor = xp.createGLSLInstance(); |
| 204 | |
| 205 | // Enable dual source secondary output if we have one |
| 206 | if (xp.hasSecondaryOutput()) { |
| 207 | fFS.enableSecondaryOutput(); |
| 208 | } |
| 209 | |
| 210 | if (this->glslCaps()->mustDeclareFragmentShaderOutput()) { |
| 211 | fFS.enableCustomOutput(); |
| 212 | } |
| 213 | |
| 214 | SkString openBrace; |
| 215 | openBrace.printf("{ // Xfer Processor: %s\n", xp.name()); |
| 216 | fFS.codeAppend(openBrace.c_str()); |
| 217 | |
egdaniel | 09aa1fc | 2016-04-20 07:09:46 -0700 | [diff] [blame] | 218 | SkSTArray<4, SamplerHandle> texSamplers(xp.numTextures()); |
| 219 | SkSTArray<2, SamplerHandle> bufferSamplers(xp.numBuffers()); |
cdalton | 74b8d32 | 2016-04-11 14:47:28 -0700 | [diff] [blame] | 220 | this->emitSamplers(xp, &texSamplers, &bufferSamplers); |
egdaniel | fa89632 | 2016-01-13 12:19:30 -0800 | [diff] [blame] | 221 | |
ethannicholas | 2279325 | 2016-01-30 09:59:10 -0800 | [diff] [blame] | 222 | bool usePLSDstRead = (plsState == GrPixelLocalStorageState::kFinish_GrPixelLocalStorageState); |
egdaniel | fa89632 | 2016-01-13 12:19:30 -0800 | [diff] [blame] | 223 | GrGLSLXferProcessor::EmitArgs args(&fFS, |
| 224 | this->uniformHandler(), |
| 225 | this->glslCaps(), |
| 226 | xp, colorIn.c_str(), |
| 227 | ignoresCoverage ? nullptr : coverageIn.c_str(), |
| 228 | fFS.getPrimaryColorOutputName(), |
| 229 | fFS.getSecondaryColorOutputName(), |
egdaniel | 09aa1fc | 2016-04-20 07:09:46 -0700 | [diff] [blame] | 230 | texSamplers.begin(), |
| 231 | bufferSamplers.begin(), |
ethannicholas | 2279325 | 2016-01-30 09:59:10 -0800 | [diff] [blame] | 232 | usePLSDstRead); |
egdaniel | fa89632 | 2016-01-13 12:19:30 -0800 | [diff] [blame] | 233 | fXferProcessor->emitCode(args); |
| 234 | |
| 235 | // We have to check that effects and the code they emit are consistent, ie if an effect |
| 236 | // asks for dst color, then the emit code needs to follow suit |
cdalton | 8733210 | 2016-02-26 12:22:02 -0800 | [diff] [blame] | 237 | SkDEBUGCODE(verify(xp);) |
egdaniel | fa89632 | 2016-01-13 12:19:30 -0800 | [diff] [blame] | 238 | fFS.codeAppend("}"); |
| 239 | } |
| 240 | |
cdalton | 9c3f143 | 2016-03-11 10:07:37 -0800 | [diff] [blame] | 241 | void GrGLSLProgramBuilder::emitSamplers(const GrProcessor& processor, |
egdaniel | 09aa1fc | 2016-04-20 07:09:46 -0700 | [diff] [blame] | 242 | SkTArray<SamplerHandle>* outTexSamplers, |
| 243 | SkTArray<SamplerHandle>* outBufferSamplers) { |
cdalton | 9c3f143 | 2016-03-11 10:07:37 -0800 | [diff] [blame] | 244 | SkString name; |
cdalton | 74b8d32 | 2016-04-11 14:47:28 -0700 | [diff] [blame] | 245 | int numTextures = processor.numTextures(); |
cdalton | 9c3f143 | 2016-03-11 10:07:37 -0800 | [diff] [blame] | 246 | for (int t = 0; t < numTextures; ++t) { |
| 247 | const GrTextureAccess& access = processor.textureAccess(t); |
Brian Salomon | 739c5bf | 2016-11-07 09:53:44 -0500 | [diff] [blame] | 248 | GrSLType samplerType = access.getTexture()->texturePriv().samplerType(); |
egdaniel | 990dbc8 | 2016-07-13 14:09:30 -0700 | [diff] [blame] | 249 | if (kTextureExternalSampler_GrSLType == samplerType) { |
cdalton | 9c3f143 | 2016-03-11 10:07:37 -0800 | [diff] [blame] | 250 | const char* externalFeatureString = this->glslCaps()->externalTextureExtensionString(); |
| 251 | // We shouldn't ever create a GrGLTexture that requires external sampler type |
| 252 | SkASSERT(externalFeatureString); |
cdalton | 74b8d32 | 2016-04-11 14:47:28 -0700 | [diff] [blame] | 253 | this->addFeature(access.getVisibility(), |
cdalton | 9c3f143 | 2016-03-11 10:07:37 -0800 | [diff] [blame] | 254 | 1 << GrGLSLShaderBuilder::kExternalTexture_GLSLPrivateFeature, |
| 255 | externalFeatureString); |
| 256 | } |
bsalomon | b58a2b4 | 2016-09-26 06:55:02 -0700 | [diff] [blame] | 257 | name.printf("TextureSampler_%d", outTexSamplers->count()); |
cdalton | 74b8d32 | 2016-04-11 14:47:28 -0700 | [diff] [blame] | 258 | this->emitSampler(samplerType, access.getTexture()->config(), |
| 259 | name.c_str(), access.getVisibility(), outTexSamplers); |
cdalton | 9c3f143 | 2016-03-11 10:07:37 -0800 | [diff] [blame] | 260 | } |
cdalton | 74b8d32 | 2016-04-11 14:47:28 -0700 | [diff] [blame] | 261 | |
| 262 | if (int numBuffers = processor.numBuffers()) { |
| 263 | SkASSERT(this->glslCaps()->texelBufferSupport()); |
| 264 | GrShaderFlags texelBufferVisibility = kNone_GrShaderFlags; |
| 265 | |
| 266 | for (int b = 0; b < numBuffers; ++b) { |
| 267 | const GrBufferAccess& access = processor.bufferAccess(b); |
bsalomon | b58a2b4 | 2016-09-26 06:55:02 -0700 | [diff] [blame] | 268 | name.printf("BufferSampler_%d", outBufferSamplers->count()); |
csmartdalton | 2245803 | 2016-11-16 11:28:16 -0700 | [diff] [blame^] | 269 | this->emitSampler(kBufferSampler_GrSLType, access.texelConfig(), name.c_str(), |
cdalton | 74b8d32 | 2016-04-11 14:47:28 -0700 | [diff] [blame] | 270 | access.visibility(), outBufferSamplers); |
| 271 | texelBufferVisibility |= access.visibility(); |
| 272 | } |
| 273 | |
| 274 | if (const char* extension = this->glslCaps()->texelBufferExtensionString()) { |
| 275 | this->addFeature(texelBufferVisibility, |
| 276 | 1 << GrGLSLShaderBuilder::kTexelBuffer_GLSLPrivateFeature, |
| 277 | extension); |
| 278 | } |
| 279 | } |
| 280 | } |
| 281 | |
| 282 | void GrGLSLProgramBuilder::emitSampler(GrSLType samplerType, |
| 283 | GrPixelConfig config, |
| 284 | const char* name, |
| 285 | GrShaderFlags visibility, |
egdaniel | 09aa1fc | 2016-04-20 07:09:46 -0700 | [diff] [blame] | 286 | SkTArray<SamplerHandle>* outSamplers) { |
cdalton | 74b8d32 | 2016-04-11 14:47:28 -0700 | [diff] [blame] | 287 | if (visibility & kVertex_GrShaderFlag) { |
| 288 | ++fNumVertexSamplers; |
| 289 | } |
| 290 | if (visibility & kGeometry_GrShaderFlag) { |
| 291 | SkASSERT(this->primitiveProcessor().willUseGeoShader()); |
| 292 | ++fNumGeometrySamplers; |
| 293 | } |
| 294 | if (visibility & kFragment_GrShaderFlag) { |
| 295 | ++fNumFragmentSamplers; |
| 296 | } |
| 297 | GrSLPrecision precision = this->glslCaps()->samplerPrecision(config, visibility); |
egdaniel | 09aa1fc | 2016-04-20 07:09:46 -0700 | [diff] [blame] | 298 | SamplerHandle handle = this->uniformHandler()->addSampler(visibility, |
| 299 | config, |
| 300 | samplerType, |
| 301 | precision, |
| 302 | name); |
| 303 | outSamplers->emplace_back(handle); |
cdalton | 9c3f143 | 2016-03-11 10:07:37 -0800 | [diff] [blame] | 304 | } |
| 305 | |
egdaniel | fa89632 | 2016-01-13 12:19:30 -0800 | [diff] [blame] | 306 | void GrGLSLProgramBuilder::emitFSOutputSwizzle(bool hasSecondaryOutput) { |
| 307 | // Swizzle the fragment shader outputs if necessary. |
| 308 | GrSwizzle swizzle; |
| 309 | swizzle.setFromKey(this->desc().header().fOutputSwizzle); |
| 310 | if (swizzle != GrSwizzle::RGBA()) { |
| 311 | fFS.codeAppendf("%s = %s.%s;", fFS.getPrimaryColorOutputName(), |
| 312 | fFS.getPrimaryColorOutputName(), |
| 313 | swizzle.c_str()); |
| 314 | if (hasSecondaryOutput) { |
| 315 | fFS.codeAppendf("%s = %s.%s;", fFS.getSecondaryColorOutputName(), |
| 316 | fFS.getSecondaryColorOutputName(), |
| 317 | swizzle.c_str()); |
| 318 | } |
| 319 | } |
| 320 | } |
| 321 | |
cdalton | 9c3f143 | 2016-03-11 10:07:37 -0800 | [diff] [blame] | 322 | bool GrGLSLProgramBuilder::checkSamplerCounts() { |
| 323 | const GrGLSLCaps& glslCaps = *this->glslCaps(); |
| 324 | if (fNumVertexSamplers > glslCaps.maxVertexSamplers()) { |
| 325 | GrCapsDebugf(this->caps(), "Program would use too many vertex samplers\n"); |
| 326 | return false; |
| 327 | } |
| 328 | if (fNumGeometrySamplers > glslCaps.maxGeometrySamplers()) { |
| 329 | GrCapsDebugf(this->caps(), "Program would use too many geometry samplers\n"); |
| 330 | return false; |
| 331 | } |
| 332 | if (fNumFragmentSamplers > glslCaps.maxFragmentSamplers()) { |
| 333 | GrCapsDebugf(this->caps(), "Program would use too many fragment samplers\n"); |
| 334 | return false; |
| 335 | } |
| 336 | // If the same sampler is used in two different shaders, it counts as two combined samplers. |
| 337 | int numCombinedSamplers = fNumVertexSamplers + fNumGeometrySamplers + fNumFragmentSamplers; |
| 338 | if (numCombinedSamplers > glslCaps.maxCombinedSamplers()) { |
| 339 | GrCapsDebugf(this->caps(), "Program would use too many combined samplers\n"); |
| 340 | return false; |
| 341 | } |
| 342 | return true; |
| 343 | } |
| 344 | |
cdalton | 8733210 | 2016-02-26 12:22:02 -0800 | [diff] [blame] | 345 | #ifdef SK_DEBUG |
egdaniel | fa89632 | 2016-01-13 12:19:30 -0800 | [diff] [blame] | 346 | void GrGLSLProgramBuilder::verify(const GrPrimitiveProcessor& gp) { |
cdalton | 8733210 | 2016-02-26 12:22:02 -0800 | [diff] [blame] | 347 | SkASSERT(fFS.usedProcessorFeatures() == gp.requiredFeatures()); |
egdaniel | fa89632 | 2016-01-13 12:19:30 -0800 | [diff] [blame] | 348 | } |
| 349 | |
| 350 | void GrGLSLProgramBuilder::verify(const GrXferProcessor& xp) { |
cdalton | 8733210 | 2016-02-26 12:22:02 -0800 | [diff] [blame] | 351 | SkASSERT(fFS.usedProcessorFeatures() == xp.requiredFeatures()); |
egdaniel | fa89632 | 2016-01-13 12:19:30 -0800 | [diff] [blame] | 352 | SkASSERT(fFS.hasReadDstColor() == xp.willReadDstColor()); |
| 353 | } |
| 354 | |
| 355 | void GrGLSLProgramBuilder::verify(const GrFragmentProcessor& fp) { |
cdalton | 8733210 | 2016-02-26 12:22:02 -0800 | [diff] [blame] | 356 | SkASSERT(fFS.usedProcessorFeatures() == fp.requiredFeatures()); |
egdaniel | 8dcdedc | 2015-11-11 06:27:20 -0800 | [diff] [blame] | 357 | } |
cdalton | 8733210 | 2016-02-26 12:22:02 -0800 | [diff] [blame] | 358 | #endif |
egdaniel | 8dcdedc | 2015-11-11 06:27:20 -0800 | [diff] [blame] | 359 | |
| 360 | void GrGLSLProgramBuilder::nameVariable(SkString* out, char prefix, const char* name, bool mangle) { |
| 361 | if ('\0' == prefix) { |
| 362 | *out = name; |
| 363 | } else { |
| 364 | out->printf("%c%s", prefix, name); |
| 365 | } |
| 366 | if (mangle) { |
| 367 | if (out->endsWith('_')) { |
| 368 | // Names containing "__" are reserved. |
| 369 | out->append("x"); |
| 370 | } |
| 371 | out->appendf("_Stage%d%s", fStageIndex, fFS.getMangleString().c_str()); |
| 372 | } |
| 373 | } |
| 374 | |
egdaniel | fa89632 | 2016-01-13 12:19:30 -0800 | [diff] [blame] | 375 | void GrGLSLProgramBuilder::nameExpression(GrGLSLExpr4* output, const char* baseName) { |
| 376 | // create var to hold stage result. If we already have a valid output name, just use that |
| 377 | // otherwise create a new mangled one. This name is only valid if we are reordering stages |
| 378 | // and have to tell stage exactly where to put its output. |
| 379 | SkString outName; |
| 380 | if (output->isValid()) { |
| 381 | outName = output->c_str(); |
| 382 | } else { |
| 383 | this->nameVariable(&outName, '\0', baseName); |
| 384 | } |
| 385 | fFS.codeAppendf("vec4 %s;", outName.c_str()); |
| 386 | *output = outName; |
| 387 | } |
| 388 | |
cdalton | 5e58cee | 2016-02-11 12:49:47 -0800 | [diff] [blame] | 389 | void GrGLSLProgramBuilder::appendUniformDecls(GrShaderFlags visibility, SkString* out) const { |
egdaniel | 7ea439b | 2015-12-03 09:20:44 -0800 | [diff] [blame] | 390 | this->uniformHandler()->appendUniformDecls(visibility, out); |
| 391 | } |
| 392 | |
egdaniel | 09aa1fc | 2016-04-20 07:09:46 -0700 | [diff] [blame] | 393 | const GrGLSLSampler& GrGLSLProgramBuilder::getSampler(SamplerHandle handle) const { |
| 394 | return this->uniformHandler()->getSampler(handle); |
| 395 | } |
| 396 | |
egdaniel | 7ea439b | 2015-12-03 09:20:44 -0800 | [diff] [blame] | 397 | void GrGLSLProgramBuilder::addRTAdjustmentUniform(GrSLPrecision precision, |
| 398 | const char* name, |
| 399 | const char** outName) { |
| 400 | SkASSERT(!fUniformHandles.fRTAdjustmentUni.isValid()); |
| 401 | fUniformHandles.fRTAdjustmentUni = |
cdalton | 5e58cee | 2016-02-11 12:49:47 -0800 | [diff] [blame] | 402 | this->uniformHandler()->addUniform(kVertex_GrShaderFlag, |
egdaniel | 7ea439b | 2015-12-03 09:20:44 -0800 | [diff] [blame] | 403 | kVec4f_GrSLType, |
| 404 | precision, |
| 405 | name, |
| 406 | outName); |
| 407 | } |
| 408 | |
| 409 | void GrGLSLProgramBuilder::addRTHeightUniform(const char* name, const char** outName) { |
| 410 | SkASSERT(!fUniformHandles.fRTHeightUni.isValid()); |
| 411 | GrGLSLUniformHandler* uniformHandler = this->uniformHandler(); |
| 412 | fUniformHandles.fRTHeightUni = |
cdalton | 5e58cee | 2016-02-11 12:49:47 -0800 | [diff] [blame] | 413 | uniformHandler->internalAddUniformArray(kFragment_GrShaderFlag, |
egdaniel | 7ea439b | 2015-12-03 09:20:44 -0800 | [diff] [blame] | 414 | kFloat_GrSLType, kDefault_GrSLPrecision, |
| 415 | name, false, 0, outName); |
egdaniel | 8dcdedc | 2015-11-11 06:27:20 -0800 | [diff] [blame] | 416 | } |
| 417 | |
egdaniel | fa89632 | 2016-01-13 12:19:30 -0800 | [diff] [blame] | 418 | void GrGLSLProgramBuilder::cleanupFragmentProcessors() { |
| 419 | for (int i = 0; i < fFragmentProcessors.count(); ++i) { |
| 420 | delete fFragmentProcessors[i]; |
| 421 | } |
| 422 | } |
| 423 | |
egdaniel | 9f1d415 | 2016-02-10 09:50:38 -0800 | [diff] [blame] | 424 | void GrGLSLProgramBuilder::finalizeShaders() { |
| 425 | this->varyingHandler()->finalize(); |
cdalton | 5e58cee | 2016-02-11 12:49:47 -0800 | [diff] [blame] | 426 | fVS.finalize(kVertex_GrShaderFlag); |
| 427 | fFS.finalize(kFragment_GrShaderFlag); |
egdaniel | 9f1d415 | 2016-02-10 09:50:38 -0800 | [diff] [blame] | 428 | } |