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