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" |
Robert Phillips | fe8da17 | 2018-01-24 14:52:02 +0000 | [diff] [blame] | 18 | #include "SkSLCompiler.h" |
egdaniel | fa89632 | 2016-01-13 12:19:30 -0800 | [diff] [blame] | 19 | |
egdaniel | 8dcdedc | 2015-11-11 06:27:20 -0800 | [diff] [blame] | 20 | const int GrGLSLProgramBuilder::kVarsPerBlock = 8; |
| 21 | |
Brian Salomon | ff168d9 | 2018-06-23 15:17:27 -0400 | [diff] [blame] | 22 | GrGLSLProgramBuilder::GrGLSLProgramBuilder(const GrPrimitiveProcessor& primProc, |
| 23 | const GrPipeline& pipeline, |
Ethan Nicholas | 3865711 | 2017-02-09 17:01:22 -0500 | [diff] [blame] | 24 | GrProgramDesc* desc) |
Brian Salomon | ff168d9 | 2018-06-23 15:17:27 -0400 | [diff] [blame] | 25 | : fVS(this) |
| 26 | , fGS(this) |
| 27 | , fFS(this) |
| 28 | , fStageIndex(-1) |
| 29 | , fPipeline(pipeline) |
| 30 | , fPrimProc(primProc) |
| 31 | , fDesc(desc) |
| 32 | , fGeometryProcessor(nullptr) |
| 33 | , fXferProcessor(nullptr) |
| 34 | , fNumVertexSamplers(0) |
| 35 | , fNumGeometrySamplers(0) |
| 36 | , fNumFragmentSamplers(0) {} |
cdalton | 9c3f143 | 2016-03-11 10:07:37 -0800 | [diff] [blame] | 37 | |
| 38 | void GrGLSLProgramBuilder::addFeature(GrShaderFlags shaders, |
| 39 | uint32_t featureBit, |
| 40 | const char* extensionName) { |
| 41 | if (shaders & kVertex_GrShaderFlag) { |
| 42 | fVS.addFeature(featureBit, extensionName); |
| 43 | } |
| 44 | if (shaders & kGeometry_GrShaderFlag) { |
csmartdalton | 276cc41 | 2016-11-21 11:55:00 -0700 | [diff] [blame] | 45 | SkASSERT(this->primitiveProcessor().willUseGeoShader()); |
cdalton | 9c3f143 | 2016-03-11 10:07:37 -0800 | [diff] [blame] | 46 | fGS.addFeature(featureBit, extensionName); |
| 47 | } |
| 48 | if (shaders & kFragment_GrShaderFlag) { |
| 49 | fFS.addFeature(featureBit, extensionName); |
| 50 | } |
egdaniel | fa89632 | 2016-01-13 12:19:30 -0800 | [diff] [blame] | 51 | } |
| 52 | |
Ethan Nicholas | 2983f40 | 2017-05-08 09:36:08 -0400 | [diff] [blame] | 53 | bool GrGLSLProgramBuilder::emitAndInstallProcs() { |
egdaniel | fa89632 | 2016-01-13 12:19:30 -0800 | [diff] [blame] | 54 | // First we loop over all of the installed processors and collect coord transforms. These will |
| 55 | // be sent to the GrGLSLPrimitiveProcessor in its emitCode function |
| 56 | const GrPrimitiveProcessor& primProc = this->primitiveProcessor(); |
egdaniel | fa89632 | 2016-01-13 12:19:30 -0800 | [diff] [blame] | 57 | |
Ethan Nicholas | 2983f40 | 2017-05-08 09:36:08 -0400 | [diff] [blame] | 58 | SkString inputColor; |
| 59 | SkString inputCoverage; |
| 60 | this->emitAndInstallPrimProc(primProc, &inputColor, &inputCoverage); |
| 61 | this->emitAndInstallFragProcs(&inputColor, &inputCoverage); |
| 62 | this->emitAndInstallXferProc(inputColor, inputCoverage); |
Brian Salomon | 42c456f | 2017-03-06 11:29:48 -0500 | [diff] [blame] | 63 | this->emitFSOutputSwizzle(this->pipeline().getXferProcessor().hasSecondaryOutput()); |
cdalton | 9c3f143 | 2016-03-11 10:07:37 -0800 | [diff] [blame] | 64 | |
Brian Salomon | 559f556 | 2017-11-15 14:28:33 -0500 | [diff] [blame] | 65 | return this->checkSamplerCounts(); |
egdaniel | fa89632 | 2016-01-13 12:19:30 -0800 | [diff] [blame] | 66 | } |
| 67 | |
| 68 | void GrGLSLProgramBuilder::emitAndInstallPrimProc(const GrPrimitiveProcessor& proc, |
Ethan Nicholas | 2983f40 | 2017-05-08 09:36:08 -0400 | [diff] [blame] | 69 | SkString* outputColor, |
| 70 | SkString* outputCoverage) { |
egdaniel | fa89632 | 2016-01-13 12:19:30 -0800 | [diff] [blame] | 71 | // Program builders have a bit of state we need to clear with each effect |
| 72 | AutoStageAdvance adv(this); |
| 73 | this->nameExpression(outputColor, "outputColor"); |
| 74 | this->nameExpression(outputCoverage, "outputCoverage"); |
| 75 | |
csmartdalton | 936f81b | 2017-02-13 15:45:35 -0700 | [diff] [blame] | 76 | SkASSERT(!fUniformHandles.fRTAdjustmentUni.isValid()); |
Robert Phillips | fe8da17 | 2018-01-24 14:52:02 +0000 | [diff] [blame] | 77 | GrShaderFlags rtAdjustVisibility; |
csmartdalton | 936f81b | 2017-02-13 15:45:35 -0700 | [diff] [blame] | 78 | if (proc.willUseGeoShader()) { |
Robert Phillips | fe8da17 | 2018-01-24 14:52:02 +0000 | [diff] [blame] | 79 | rtAdjustVisibility = kGeometry_GrShaderFlag; |
| 80 | } else { |
| 81 | rtAdjustVisibility = kVertex_GrShaderFlag; |
csmartdalton | 936f81b | 2017-02-13 15:45:35 -0700 | [diff] [blame] | 82 | } |
Robert Phillips | fe8da17 | 2018-01-24 14:52:02 +0000 | [diff] [blame] | 83 | fUniformHandles.fRTAdjustmentUni = this->uniformHandler()->addUniform( |
| 84 | rtAdjustVisibility, |
| 85 | kFloat4_GrSLType, |
| 86 | SkSL::Compiler::RTADJUST_NAME); |
csmartdalton | 936f81b | 2017-02-13 15:45:35 -0700 | [diff] [blame] | 87 | const char* rtAdjustName = |
| 88 | this->uniformHandler()->getUniformCStr(fUniformHandles.fRTAdjustmentUni); |
| 89 | |
egdaniel | fa89632 | 2016-01-13 12:19:30 -0800 | [diff] [blame] | 90 | // Enclose custom code in a block to avoid namespace conflicts |
| 91 | SkString openBrace; |
| 92 | openBrace.printf("{ // Stage %d, %s\n", fStageIndex, proc.name()); |
| 93 | fFS.codeAppend(openBrace.c_str()); |
| 94 | fVS.codeAppendf("// Primitive Processor %s\n", proc.name()); |
| 95 | |
| 96 | SkASSERT(!fGeometryProcessor); |
Robert Phillips | 369e8b7 | 2017-08-01 16:13:04 -0400 | [diff] [blame] | 97 | fGeometryProcessor.reset(proc.createGLSLInstance(*this->shaderCaps())); |
egdaniel | fa89632 | 2016-01-13 12:19:30 -0800 | [diff] [blame] | 98 | |
Brian Salomon | e782f84 | 2018-07-31 13:53:11 -0400 | [diff] [blame] | 99 | SkAutoSTMalloc<4, SamplerHandle> texSamplers(proc.numTextureSamplers()); |
| 100 | for (int i = 0; i < proc.numTextureSamplers(); ++i) { |
| 101 | SkString name; |
| 102 | name.printf("TextureSampler_%d", i); |
| 103 | const auto& sampler = proc.textureSampler(i); |
Brian Salomon | 7eae3e0 | 2018-08-07 14:02:38 +0000 | [diff] [blame] | 104 | texSamplers[i] = this->emitSampler(sampler.textureType(), sampler.config(), name.c_str(), |
| 105 | sampler.visibility()); |
Brian Salomon | e782f84 | 2018-07-31 13:53:11 -0400 | [diff] [blame] | 106 | } |
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(), |
Brian Salomon | 94efbf5 | 2016-11-29 13:43:05 -0500 | [diff] [blame] | 115 | this->shaderCaps(), |
egdaniel | fa89632 | 2016-01-13 12:19:30 -0800 | [diff] [blame] | 116 | proc, |
| 117 | outputColor->c_str(), |
| 118 | outputCoverage->c_str(), |
csmartdalton | 936f81b | 2017-02-13 15:45:35 -0700 | [diff] [blame] | 119 | rtAdjustName, |
Brian Salomon | e782f84 | 2018-07-31 13:53:11 -0400 | [diff] [blame] | 120 | texSamplers.get(), |
bsalomon | a624bf3 | 2016-09-20 09:12:47 -0700 | [diff] [blame] | 121 | &transformHandler); |
egdaniel | fa89632 | 2016-01-13 12:19:30 -0800 | [diff] [blame] | 122 | fGeometryProcessor->emitCode(args); |
| 123 | |
| 124 | // We have to check that effects and the code they emit are consistent, ie if an effect |
| 125 | // asks for dst color, then the emit code needs to follow suit |
cdalton | 8733210 | 2016-02-26 12:22:02 -0800 | [diff] [blame] | 126 | SkDEBUGCODE(verify(proc);) |
egdaniel | fa89632 | 2016-01-13 12:19:30 -0800 | [diff] [blame] | 127 | |
| 128 | fFS.codeAppend("}"); |
| 129 | } |
| 130 | |
Ethan Nicholas | 2983f40 | 2017-05-08 09:36:08 -0400 | [diff] [blame] | 131 | void GrGLSLProgramBuilder::emitAndInstallFragProcs(SkString* color, SkString* coverage) { |
bsalomon | a624bf3 | 2016-09-20 09:12:47 -0700 | [diff] [blame] | 132 | int transformedCoordVarsIdx = 0; |
Ethan Nicholas | 2983f40 | 2017-05-08 09:36:08 -0400 | [diff] [blame] | 133 | SkString** inOut = &color; |
Brian Salomon | 4d3f517 | 2018-06-07 14:42:52 -0400 | [diff] [blame] | 134 | SkSTArray<8, std::unique_ptr<GrGLSLFragmentProcessor>> glslFragmentProcessors; |
bsalomon | a624bf3 | 2016-09-20 09:12:47 -0700 | [diff] [blame] | 135 | for (int i = 0; i < this->pipeline().numFragmentProcessors(); ++i) { |
| 136 | if (i == this->pipeline().numColorFragmentProcessors()) { |
| 137 | inOut = &coverage; |
| 138 | } |
Ethan Nicholas | 2983f40 | 2017-05-08 09:36:08 -0400 | [diff] [blame] | 139 | SkString output; |
egdaniel | fa89632 | 2016-01-13 12:19:30 -0800 | [diff] [blame] | 140 | const GrFragmentProcessor& fp = this->pipeline().getFragmentProcessor(i); |
Brian Salomon | 4d3f517 | 2018-06-07 14:42:52 -0400 | [diff] [blame] | 141 | output = this->emitAndInstallFragProc(fp, i, transformedCoordVarsIdx, **inOut, output, |
| 142 | &glslFragmentProcessors); |
bsalomon | a624bf3 | 2016-09-20 09:12:47 -0700 | [diff] [blame] | 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 | } |
Brian Salomon | 4d3f517 | 2018-06-07 14:42:52 -0400 | [diff] [blame] | 149 | fFragmentProcessorCnt = glslFragmentProcessors.count(); |
| 150 | fFragmentProcessors.reset(new std::unique_ptr<GrGLSLFragmentProcessor>[fFragmentProcessorCnt]); |
| 151 | for (int i = 0; i < fFragmentProcessorCnt; ++i) { |
| 152 | fFragmentProcessors[i] = std::move(glslFragmentProcessors[i]); |
| 153 | } |
egdaniel | fa89632 | 2016-01-13 12:19:30 -0800 | [diff] [blame] | 154 | } |
| 155 | |
| 156 | // TODO Processors cannot output zeros because an empty string is all 1s |
Ethan Nicholas | 2983f40 | 2017-05-08 09:36:08 -0400 | [diff] [blame] | 157 | // the fix is to allow effects to take the SkString directly |
Brian Salomon | 4d3f517 | 2018-06-07 14:42:52 -0400 | [diff] [blame] | 158 | SkString GrGLSLProgramBuilder::emitAndInstallFragProc( |
| 159 | const GrFragmentProcessor& fp, |
| 160 | int index, |
| 161 | int transformedCoordVarsIdx, |
| 162 | const SkString& input, |
| 163 | SkString output, |
| 164 | SkTArray<std::unique_ptr<GrGLSLFragmentProcessor>>* glslFragmentProcessors) { |
Ethan Nicholas | 2983f40 | 2017-05-08 09:36:08 -0400 | [diff] [blame] | 165 | SkASSERT(input.size()); |
egdaniel | fa89632 | 2016-01-13 12:19:30 -0800 | [diff] [blame] | 166 | // Program builders have a bit of state we need to clear with each effect |
| 167 | AutoStageAdvance adv(this); |
Ethan Nicholas | 2983f40 | 2017-05-08 09:36:08 -0400 | [diff] [blame] | 168 | this->nameExpression(&output, "output"); |
egdaniel | fa89632 | 2016-01-13 12:19:30 -0800 | [diff] [blame] | 169 | |
| 170 | // Enclose custom code in a block to avoid namespace conflicts |
| 171 | SkString openBrace; |
| 172 | openBrace.printf("{ // Stage %d, %s\n", fStageIndex, fp.name()); |
| 173 | fFS.codeAppend(openBrace.c_str()); |
| 174 | |
| 175 | GrGLSLFragmentProcessor* fragProc = fp.createGLSLInstance(); |
| 176 | |
Brian Salomon | e782f84 | 2018-07-31 13:53:11 -0400 | [diff] [blame] | 177 | SkSTArray<4, SamplerHandle> texSamplers; |
| 178 | GrFragmentProcessor::Iter fpIter(&fp); |
| 179 | int samplerIdx = 0; |
| 180 | while (const auto* subFP = fpIter.next()) { |
| 181 | for (int i = 0; i < subFP->numTextureSamplers(); ++i) { |
| 182 | SkString name; |
| 183 | name.printf("TextureSampler_%d", samplerIdx++); |
| 184 | const auto& sampler = subFP->textureSampler(i); |
| 185 | GrTextureType textureType = sampler.peekTexture()->texturePriv().textureType(); |
| 186 | texSamplers.emplace_back(this->emitSampler(textureType, sampler.peekTexture()->config(), |
| 187 | name.c_str(), kFragment_GrShaderFlag)); |
| 188 | } |
bsalomon | b58a2b4 | 2016-09-26 06:55:02 -0700 | [diff] [blame] | 189 | } |
egdaniel | fa89632 | 2016-01-13 12:19:30 -0800 | [diff] [blame] | 190 | |
bsalomon | a624bf3 | 2016-09-20 09:12:47 -0700 | [diff] [blame] | 191 | const GrShaderVar* coordVars = fTransformedCoordVars.begin() + transformedCoordVarsIdx; |
| 192 | GrGLSLFragmentProcessor::TransformedCoordVars coords(&fp, coordVars); |
Brian Salomon | e782f84 | 2018-07-31 13:53:11 -0400 | [diff] [blame] | 193 | GrGLSLFragmentProcessor::TextureSamplers textureSamplers(&fp, texSamplers.begin()); |
egdaniel | fa89632 | 2016-01-13 12:19:30 -0800 | [diff] [blame] | 194 | GrGLSLFragmentProcessor::EmitArgs args(&fFS, |
| 195 | this->uniformHandler(), |
Brian Salomon | 94efbf5 | 2016-11-29 13:43:05 -0500 | [diff] [blame] | 196 | this->shaderCaps(), |
egdaniel | fa89632 | 2016-01-13 12:19:30 -0800 | [diff] [blame] | 197 | fp, |
Ethan Nicholas | 2983f40 | 2017-05-08 09:36:08 -0400 | [diff] [blame] | 198 | output.c_str(), |
| 199 | input.c_str(), |
bsalomon | a624bf3 | 2016-09-20 09:12:47 -0700 | [diff] [blame] | 200 | coords, |
Brian Salomon | 662ea4b | 2018-07-12 14:53:49 -0400 | [diff] [blame] | 201 | textureSamplers); |
dvonbeck | 9b03e7b | 2016-08-01 11:01:56 -0700 | [diff] [blame] | 202 | |
egdaniel | fa89632 | 2016-01-13 12:19:30 -0800 | [diff] [blame] | 203 | fragProc->emitCode(args); |
| 204 | |
| 205 | // We have to check that effects and the code they emit are consistent, ie if an effect |
| 206 | // asks for dst color, then the emit code needs to follow suit |
cdalton | 8733210 | 2016-02-26 12:22:02 -0800 | [diff] [blame] | 207 | SkDEBUGCODE(verify(fp);) |
Brian Salomon | 4d3f517 | 2018-06-07 14:42:52 -0400 | [diff] [blame] | 208 | glslFragmentProcessors->emplace_back(fragProc); |
egdaniel | fa89632 | 2016-01-13 12:19:30 -0800 | [diff] [blame] | 209 | |
| 210 | fFS.codeAppend("}"); |
Ethan Nicholas | 2983f40 | 2017-05-08 09:36:08 -0400 | [diff] [blame] | 211 | return output; |
egdaniel | fa89632 | 2016-01-13 12:19:30 -0800 | [diff] [blame] | 212 | } |
| 213 | |
Ethan Nicholas | 2983f40 | 2017-05-08 09:36:08 -0400 | [diff] [blame] | 214 | void GrGLSLProgramBuilder::emitAndInstallXferProc(const SkString& colorIn, |
| 215 | const SkString& coverageIn) { |
egdaniel | fa89632 | 2016-01-13 12:19:30 -0800 | [diff] [blame] | 216 | // Program builders have a bit of state we need to clear with each effect |
| 217 | AutoStageAdvance adv(this); |
| 218 | |
| 219 | SkASSERT(!fXferProcessor); |
Brian Salomon | 18dfa98 | 2017-04-03 16:57:43 -0400 | [diff] [blame] | 220 | const GrXferProcessor& xp = fPipeline.getXferProcessor(); |
Robert Phillips | 369e8b7 | 2017-08-01 16:13:04 -0400 | [diff] [blame] | 221 | fXferProcessor.reset(xp.createGLSLInstance()); |
egdaniel | fa89632 | 2016-01-13 12:19:30 -0800 | [diff] [blame] | 222 | |
| 223 | // Enable dual source secondary output if we have one |
| 224 | if (xp.hasSecondaryOutput()) { |
| 225 | fFS.enableSecondaryOutput(); |
| 226 | } |
| 227 | |
Brian Salomon | 94efbf5 | 2016-11-29 13:43:05 -0500 | [diff] [blame] | 228 | if (this->shaderCaps()->mustDeclareFragmentShaderOutput()) { |
egdaniel | fa89632 | 2016-01-13 12:19:30 -0800 | [diff] [blame] | 229 | fFS.enableCustomOutput(); |
| 230 | } |
| 231 | |
| 232 | SkString openBrace; |
| 233 | openBrace.printf("{ // Xfer Processor: %s\n", xp.name()); |
| 234 | fFS.codeAppend(openBrace.c_str()); |
| 235 | |
Brian Salomon | 18dfa98 | 2017-04-03 16:57:43 -0400 | [diff] [blame] | 236 | SamplerHandle dstTextureSamplerHandle; |
| 237 | GrSurfaceOrigin dstTextureOrigin = kTopLeft_GrSurfaceOrigin; |
Robert Phillips | bb581ce | 2017-05-29 15:05:15 -0400 | [diff] [blame] | 238 | |
| 239 | if (GrTexture* dstTexture = fPipeline.peekDstTexture()) { |
Brian Salomon | 18dfa98 | 2017-04-03 16:57:43 -0400 | [diff] [blame] | 240 | // GrProcessor::TextureSampler sampler(dstTexture); |
| 241 | SkString name("DstTextureSampler"); |
| 242 | dstTextureSamplerHandle = |
Brian Salomon | 60dd8c7 | 2018-07-30 10:24:13 -0400 | [diff] [blame] | 243 | this->emitSampler(dstTexture->texturePriv().textureType(), dstTexture->config(), |
Brian Salomon | 18dfa98 | 2017-04-03 16:57:43 -0400 | [diff] [blame] | 244 | "DstTextureSampler", kFragment_GrShaderFlag); |
Robert Phillips | 16d8ec6 | 2017-07-27 16:16:25 -0400 | [diff] [blame] | 245 | dstTextureOrigin = fPipeline.dstTextureProxy()->origin(); |
Brian Salomon | 60dd8c7 | 2018-07-30 10:24:13 -0400 | [diff] [blame] | 246 | SkASSERT(dstTexture->texturePriv().textureType() != GrTextureType::kExternal); |
Brian Salomon | 18dfa98 | 2017-04-03 16:57:43 -0400 | [diff] [blame] | 247 | } |
egdaniel | fa89632 | 2016-01-13 12:19:30 -0800 | [diff] [blame] | 248 | |
| 249 | GrGLSLXferProcessor::EmitArgs args(&fFS, |
| 250 | this->uniformHandler(), |
Brian Salomon | 94efbf5 | 2016-11-29 13:43:05 -0500 | [diff] [blame] | 251 | this->shaderCaps(), |
Brian Salomon | 18dfa98 | 2017-04-03 16:57:43 -0400 | [diff] [blame] | 252 | xp, |
Ethan Nicholas | 5af9ea3 | 2017-07-28 15:19:46 -0400 | [diff] [blame] | 253 | colorIn.size() ? colorIn.c_str() : "float4(1)", |
| 254 | coverageIn.size() ? coverageIn.c_str() : "float4(1)", |
egdaniel | fa89632 | 2016-01-13 12:19:30 -0800 | [diff] [blame] | 255 | fFS.getPrimaryColorOutputName(), |
| 256 | fFS.getSecondaryColorOutputName(), |
Brian Salomon | 18dfa98 | 2017-04-03 16:57:43 -0400 | [diff] [blame] | 257 | dstTextureSamplerHandle, |
| 258 | dstTextureOrigin); |
egdaniel | fa89632 | 2016-01-13 12:19:30 -0800 | [diff] [blame] | 259 | fXferProcessor->emitCode(args); |
| 260 | |
| 261 | // We have to check that effects and the code they emit are consistent, ie if an effect |
| 262 | // asks for dst color, then the emit code needs to follow suit |
cdalton | 8733210 | 2016-02-26 12:22:02 -0800 | [diff] [blame] | 263 | SkDEBUGCODE(verify(xp);) |
egdaniel | fa89632 | 2016-01-13 12:19:30 -0800 | [diff] [blame] | 264 | fFS.codeAppend("}"); |
| 265 | } |
| 266 | |
Greg Daniel | bc5d4d7 | 2017-05-05 10:28:42 -0400 | [diff] [blame] | 267 | void GrGLSLProgramBuilder::updateSamplerCounts(GrShaderFlags visibility) { |
cdalton | 74b8d32 | 2016-04-11 14:47:28 -0700 | [diff] [blame] | 268 | if (visibility & kVertex_GrShaderFlag) { |
| 269 | ++fNumVertexSamplers; |
| 270 | } |
| 271 | if (visibility & kGeometry_GrShaderFlag) { |
| 272 | SkASSERT(this->primitiveProcessor().willUseGeoShader()); |
| 273 | ++fNumGeometrySamplers; |
| 274 | } |
| 275 | if (visibility & kFragment_GrShaderFlag) { |
| 276 | ++fNumFragmentSamplers; |
| 277 | } |
Greg Daniel | bc5d4d7 | 2017-05-05 10:28:42 -0400 | [diff] [blame] | 278 | } |
| 279 | |
Brian Salomon | 60dd8c7 | 2018-07-30 10:24:13 -0400 | [diff] [blame] | 280 | GrGLSLProgramBuilder::SamplerHandle GrGLSLProgramBuilder::emitSampler(GrTextureType textureType, |
Greg Daniel | bc5d4d7 | 2017-05-05 10:28:42 -0400 | [diff] [blame] | 281 | GrPixelConfig config, |
| 282 | const char* name, |
| 283 | GrShaderFlags visibility) { |
| 284 | this->updateSamplerCounts(visibility); |
Chris Dalton | 47c8ed3 | 2017-11-15 18:27:09 -0700 | [diff] [blame] | 285 | GrSLPrecision precision = GrSLSamplerPrecision(config); |
Brian Salomon | 94efbf5 | 2016-11-29 13:43:05 -0500 | [diff] [blame] | 286 | GrSwizzle swizzle = this->shaderCaps()->configTextureSwizzle(config); |
Brian Salomon | 60dd8c7 | 2018-07-30 10:24:13 -0400 | [diff] [blame] | 287 | return this->uniformHandler()->addSampler(visibility, swizzle, textureType, precision, name); |
Brian Salomon | f9f4512 | 2016-11-29 11:59:17 -0500 | [diff] [blame] | 288 | } |
| 289 | |
egdaniel | fa89632 | 2016-01-13 12:19:30 -0800 | [diff] [blame] | 290 | void GrGLSLProgramBuilder::emitFSOutputSwizzle(bool hasSecondaryOutput) { |
| 291 | // Swizzle the fragment shader outputs if necessary. |
| 292 | GrSwizzle swizzle; |
Ethan Nicholas | 3865711 | 2017-02-09 17:01:22 -0500 | [diff] [blame] | 293 | swizzle.setFromKey(this->desc()->header().fOutputSwizzle); |
egdaniel | fa89632 | 2016-01-13 12:19:30 -0800 | [diff] [blame] | 294 | if (swizzle != GrSwizzle::RGBA()) { |
| 295 | fFS.codeAppendf("%s = %s.%s;", fFS.getPrimaryColorOutputName(), |
| 296 | fFS.getPrimaryColorOutputName(), |
| 297 | swizzle.c_str()); |
| 298 | if (hasSecondaryOutput) { |
| 299 | fFS.codeAppendf("%s = %s.%s;", fFS.getSecondaryColorOutputName(), |
| 300 | fFS.getSecondaryColorOutputName(), |
| 301 | swizzle.c_str()); |
| 302 | } |
| 303 | } |
| 304 | } |
| 305 | |
cdalton | 9c3f143 | 2016-03-11 10:07:37 -0800 | [diff] [blame] | 306 | bool GrGLSLProgramBuilder::checkSamplerCounts() { |
Brian Salomon | 1edc5b9 | 2016-11-29 13:43:46 -0500 | [diff] [blame] | 307 | const GrShaderCaps& shaderCaps = *this->shaderCaps(); |
| 308 | if (fNumVertexSamplers > shaderCaps.maxVertexSamplers()) { |
cdalton | 9c3f143 | 2016-03-11 10:07:37 -0800 | [diff] [blame] | 309 | GrCapsDebugf(this->caps(), "Program would use too many vertex samplers\n"); |
| 310 | return false; |
| 311 | } |
Brian Salomon | 1edc5b9 | 2016-11-29 13:43:46 -0500 | [diff] [blame] | 312 | if (fNumGeometrySamplers > shaderCaps.maxGeometrySamplers()) { |
cdalton | 9c3f143 | 2016-03-11 10:07:37 -0800 | [diff] [blame] | 313 | GrCapsDebugf(this->caps(), "Program would use too many geometry samplers\n"); |
| 314 | return false; |
| 315 | } |
Brian Salomon | 1edc5b9 | 2016-11-29 13:43:46 -0500 | [diff] [blame] | 316 | if (fNumFragmentSamplers > shaderCaps.maxFragmentSamplers()) { |
cdalton | 9c3f143 | 2016-03-11 10:07:37 -0800 | [diff] [blame] | 317 | GrCapsDebugf(this->caps(), "Program would use too many fragment samplers\n"); |
| 318 | return false; |
| 319 | } |
| 320 | // If the same sampler is used in two different shaders, it counts as two combined samplers. |
| 321 | int numCombinedSamplers = fNumVertexSamplers + fNumGeometrySamplers + fNumFragmentSamplers; |
Brian Salomon | 1edc5b9 | 2016-11-29 13:43:46 -0500 | [diff] [blame] | 322 | if (numCombinedSamplers > shaderCaps.maxCombinedSamplers()) { |
cdalton | 9c3f143 | 2016-03-11 10:07:37 -0800 | [diff] [blame] | 323 | GrCapsDebugf(this->caps(), "Program would use too many combined samplers\n"); |
| 324 | return false; |
| 325 | } |
| 326 | return true; |
| 327 | } |
| 328 | |
cdalton | 8733210 | 2016-02-26 12:22:02 -0800 | [diff] [blame] | 329 | #ifdef SK_DEBUG |
egdaniel | fa89632 | 2016-01-13 12:19:30 -0800 | [diff] [blame] | 330 | void GrGLSLProgramBuilder::verify(const GrPrimitiveProcessor& gp) { |
egdaniel | fa89632 | 2016-01-13 12:19:30 -0800 | [diff] [blame] | 331 | } |
| 332 | |
| 333 | void GrGLSLProgramBuilder::verify(const GrXferProcessor& xp) { |
| 334 | SkASSERT(fFS.hasReadDstColor() == xp.willReadDstColor()); |
| 335 | } |
| 336 | |
| 337 | void GrGLSLProgramBuilder::verify(const GrFragmentProcessor& fp) { |
egdaniel | 8dcdedc | 2015-11-11 06:27:20 -0800 | [diff] [blame] | 338 | } |
cdalton | 8733210 | 2016-02-26 12:22:02 -0800 | [diff] [blame] | 339 | #endif |
egdaniel | 8dcdedc | 2015-11-11 06:27:20 -0800 | [diff] [blame] | 340 | |
| 341 | void GrGLSLProgramBuilder::nameVariable(SkString* out, char prefix, const char* name, bool mangle) { |
| 342 | if ('\0' == prefix) { |
| 343 | *out = name; |
| 344 | } else { |
| 345 | out->printf("%c%s", prefix, name); |
| 346 | } |
| 347 | if (mangle) { |
| 348 | if (out->endsWith('_')) { |
| 349 | // Names containing "__" are reserved. |
| 350 | out->append("x"); |
| 351 | } |
| 352 | out->appendf("_Stage%d%s", fStageIndex, fFS.getMangleString().c_str()); |
| 353 | } |
| 354 | } |
| 355 | |
Ethan Nicholas | 2983f40 | 2017-05-08 09:36:08 -0400 | [diff] [blame] | 356 | void GrGLSLProgramBuilder::nameExpression(SkString* output, const char* baseName) { |
egdaniel | fa89632 | 2016-01-13 12:19:30 -0800 | [diff] [blame] | 357 | // create var to hold stage result. If we already have a valid output name, just use that |
| 358 | // otherwise create a new mangled one. This name is only valid if we are reordering stages |
| 359 | // and have to tell stage exactly where to put its output. |
| 360 | SkString outName; |
Ethan Nicholas | 2983f40 | 2017-05-08 09:36:08 -0400 | [diff] [blame] | 361 | if (output->size()) { |
egdaniel | fa89632 | 2016-01-13 12:19:30 -0800 | [diff] [blame] | 362 | outName = output->c_str(); |
| 363 | } else { |
| 364 | this->nameVariable(&outName, '\0', baseName); |
| 365 | } |
Ethan Nicholas | f7b8820 | 2017-09-18 14:10:39 -0400 | [diff] [blame] | 366 | fFS.codeAppendf("half4 %s;", outName.c_str()); |
egdaniel | fa89632 | 2016-01-13 12:19:30 -0800 | [diff] [blame] | 367 | *output = outName; |
| 368 | } |
| 369 | |
cdalton | 5e58cee | 2016-02-11 12:49:47 -0800 | [diff] [blame] | 370 | void GrGLSLProgramBuilder::appendUniformDecls(GrShaderFlags visibility, SkString* out) const { |
egdaniel | 7ea439b | 2015-12-03 09:20:44 -0800 | [diff] [blame] | 371 | this->uniformHandler()->appendUniformDecls(visibility, out); |
| 372 | } |
| 373 | |
Ethan Nicholas | cd700e9 | 2018-08-24 16:43:57 -0400 | [diff] [blame] | 374 | void GrGLSLProgramBuilder::addRTWidthUniform(const char* name) { |
| 375 | SkASSERT(!fUniformHandles.fRTWidthUni.isValid()); |
| 376 | GrGLSLUniformHandler* uniformHandler = this->uniformHandler(); |
| 377 | fUniformHandles.fRTWidthUni = |
| 378 | uniformHandler->internalAddUniformArray(kFragment_GrShaderFlag, |
| 379 | kHalf_GrSLType, kDefault_GrSLPrecision, |
| 380 | name, false, 0, nullptr); |
| 381 | } |
| 382 | |
Greg Daniel | e6ab998 | 2018-08-22 13:56:32 +0000 | [diff] [blame] | 383 | void GrGLSLProgramBuilder::addRTHeightUniform(const char* name) { |
| 384 | SkASSERT(!fUniformHandles.fRTHeightUni.isValid()); |
egdaniel | 7ea439b | 2015-12-03 09:20:44 -0800 | [diff] [blame] | 385 | GrGLSLUniformHandler* uniformHandler = this->uniformHandler(); |
Greg Daniel | e6ab998 | 2018-08-22 13:56:32 +0000 | [diff] [blame] | 386 | fUniformHandles.fRTHeightUni = |
cdalton | 5e58cee | 2016-02-11 12:49:47 -0800 | [diff] [blame] | 387 | uniformHandler->internalAddUniformArray(kFragment_GrShaderFlag, |
Greg Daniel | e6ab998 | 2018-08-22 13:56:32 +0000 | [diff] [blame] | 388 | kHalf_GrSLType, kDefault_GrSLPrecision, |
Ethan Nicholas | 941e7e2 | 2016-12-12 15:33:30 -0500 | [diff] [blame] | 389 | name, false, 0, nullptr); |
egdaniel | 8dcdedc | 2015-11-11 06:27:20 -0800 | [diff] [blame] | 390 | } |
| 391 | |
egdaniel | 9f1d415 | 2016-02-10 09:50:38 -0800 | [diff] [blame] | 392 | void GrGLSLProgramBuilder::finalizeShaders() { |
| 393 | this->varyingHandler()->finalize(); |
cdalton | 5e58cee | 2016-02-11 12:49:47 -0800 | [diff] [blame] | 394 | fVS.finalize(kVertex_GrShaderFlag); |
csmartdalton | 276cc41 | 2016-11-21 11:55:00 -0700 | [diff] [blame] | 395 | if (this->primitiveProcessor().willUseGeoShader()) { |
Brian Salomon | 94efbf5 | 2016-11-29 13:43:05 -0500 | [diff] [blame] | 396 | SkASSERT(this->shaderCaps()->geometryShaderSupport()); |
csmartdalton | 276cc41 | 2016-11-21 11:55:00 -0700 | [diff] [blame] | 397 | fGS.finalize(kGeometry_GrShaderFlag); |
| 398 | } |
cdalton | 5e58cee | 2016-02-11 12:49:47 -0800 | [diff] [blame] | 399 | fFS.finalize(kFragment_GrShaderFlag); |
egdaniel | 9f1d415 | 2016-02-10 09:50:38 -0800 | [diff] [blame] | 400 | } |