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