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