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