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" |
Brian Salomon | 4cfae3b | 2020-07-23 10:33:24 -0400 | [diff] [blame] | 14 | #include "src/gpu/GrTexture.h" |
Mike Klein | c0bd9f9 | 2019-04-23 12:05:21 -0500 | [diff] [blame] | 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, |
Stephen White | b185785 | 2020-02-07 15:33:23 +0000 | [diff] [blame] | 24 | const GrProgramDesc& desc, |
| 25 | const GrProgramInfo& programInfo) |
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) |
Austin Eng | 77fdf66 | 2020-02-07 01:26:16 +0000 | [diff] [blame] | 31 | , fDesc(desc) |
Stephen White | b185785 | 2020-02-07 15:33:23 +0000 | [diff] [blame] | 32 | , fProgramInfo(programInfo) |
Brian Salomon | ff168d9 | 2018-06-23 15:17:27 -0400 | [diff] [blame] | 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); |
Ethan Nicholas | 5843012 | 2020-04-14 09:54:02 -0400 | [diff] [blame] | 60 | fGeometryProcessor->emitTransformCode(&fVS, this->uniformHandler()); |
cdalton | 9c3f143 | 2016-03-11 10:07:37 -0800 | [diff] [blame] | 61 | |
Brian Salomon | 559f556 | 2017-11-15 14:28:33 -0500 | [diff] [blame] | 62 | return this->checkSamplerCounts(); |
egdaniel | fa89632 | 2016-01-13 12:19:30 -0800 | [diff] [blame] | 63 | } |
| 64 | |
Chris Dalton | 1b1b0d5 | 2020-03-03 12:00:59 -0700 | [diff] [blame] | 65 | void GrGLSLProgramBuilder::emitAndInstallPrimProc(SkString* outputColor, 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 | |
egdaniel | fa89632 | 2016-01-13 12:19:30 -0800 | [diff] [blame] | 68 | // Program builders have a bit of state we need to clear with each effect |
| 69 | AutoStageAdvance adv(this); |
| 70 | this->nameExpression(outputColor, "outputColor"); |
| 71 | this->nameExpression(outputCoverage, "outputCoverage"); |
| 72 | |
csmartdalton | 936f81b | 2017-02-13 15:45:35 -0700 | [diff] [blame] | 73 | SkASSERT(!fUniformHandles.fRTAdjustmentUni.isValid()); |
Robert Phillips | fe8da17 | 2018-01-24 14:52:02 +0000 | [diff] [blame] | 74 | GrShaderFlags rtAdjustVisibility; |
csmartdalton | 936f81b | 2017-02-13 15:45:35 -0700 | [diff] [blame] | 75 | if (proc.willUseGeoShader()) { |
Robert Phillips | fe8da17 | 2018-01-24 14:52:02 +0000 | [diff] [blame] | 76 | rtAdjustVisibility = kGeometry_GrShaderFlag; |
Chris Dalton | 5a2f962 | 2019-12-27 14:56:38 -0700 | [diff] [blame] | 77 | } else if (proc.willUseTessellationShaders()) { |
| 78 | rtAdjustVisibility = kTessEvaluation_GrShaderFlag; |
Robert Phillips | fe8da17 | 2018-01-24 14:52:02 +0000 | [diff] [blame] | 79 | } else { |
| 80 | rtAdjustVisibility = kVertex_GrShaderFlag; |
csmartdalton | 936f81b | 2017-02-13 15:45:35 -0700 | [diff] [blame] | 81 | } |
Robert Phillips | fe8da17 | 2018-01-24 14:52:02 +0000 | [diff] [blame] | 82 | fUniformHandles.fRTAdjustmentUni = this->uniformHandler()->addUniform( |
Ethan Nicholas | 16464c3 | 2020-04-06 13:53:05 -0400 | [diff] [blame] | 83 | nullptr, rtAdjustVisibility, kFloat4_GrSLType, SkSL::Compiler::RTADJUST_NAME); |
csmartdalton | 936f81b | 2017-02-13 15:45:35 -0700 | [diff] [blame] | 84 | const char* rtAdjustName = |
| 85 | this->uniformHandler()->getUniformCStr(fUniformHandles.fRTAdjustmentUni); |
| 86 | |
egdaniel | fa89632 | 2016-01-13 12:19:30 -0800 | [diff] [blame] | 87 | // Enclose custom code in a block to avoid namespace conflicts |
| 88 | SkString openBrace; |
| 89 | openBrace.printf("{ // Stage %d, %s\n", fStageIndex, proc.name()); |
| 90 | fFS.codeAppend(openBrace.c_str()); |
| 91 | fVS.codeAppendf("// Primitive Processor %s\n", proc.name()); |
| 92 | |
| 93 | SkASSERT(!fGeometryProcessor); |
Robert Phillips | 369e8b7 | 2017-08-01 16:13:04 -0400 | [diff] [blame] | 94 | fGeometryProcessor.reset(proc.createGLSLInstance(*this->shaderCaps())); |
egdaniel | fa89632 | 2016-01-13 12:19:30 -0800 | [diff] [blame] | 95 | |
Brian Salomon | e782f84 | 2018-07-31 13:53:11 -0400 | [diff] [blame] | 96 | SkAutoSTMalloc<4, SamplerHandle> texSamplers(proc.numTextureSamplers()); |
| 97 | for (int i = 0; i < proc.numTextureSamplers(); ++i) { |
| 98 | SkString name; |
| 99 | name.printf("TextureSampler_%d", i); |
| 100 | const auto& sampler = proc.textureSampler(i); |
Chris Dalton | 1b1b0d5 | 2020-03-03 12:00:59 -0700 | [diff] [blame] | 101 | texSamplers[i] = this->emitSampler(proc.textureSampler(i).backendFormat(), |
Greg Daniel | 9a51a86 | 2018-11-30 10:18:14 -0500 | [diff] [blame] | 102 | sampler.samplerState(), |
Greg Daniel | 2c3398d | 2019-06-19 11:58:01 -0400 | [diff] [blame] | 103 | sampler.swizzle(), |
Greg Daniel | 9a51a86 | 2018-11-30 10:18:14 -0500 | [diff] [blame] | 104 | name.c_str()); |
Brian Salomon | e782f84 | 2018-07-31 13:53:11 -0400 | [diff] [blame] | 105 | } |
egdaniel | fa89632 | 2016-01-13 12:19:30 -0800 | [diff] [blame] | 106 | |
Robert Phillips | 901aff0 | 2019-10-08 12:32:56 -0400 | [diff] [blame] | 107 | GrGLSLPrimitiveProcessor::FPCoordTransformHandler transformHandler(this->pipeline(), |
bsalomon | a624bf3 | 2016-09-20 09:12:47 -0700 | [diff] [blame] | 108 | &fTransformedCoordVars); |
egdaniel | fa89632 | 2016-01-13 12:19:30 -0800 | [diff] [blame] | 109 | GrGLSLGeometryProcessor::EmitArgs args(&fVS, |
csmartdalton | 276cc41 | 2016-11-21 11:55:00 -0700 | [diff] [blame] | 110 | proc.willUseGeoShader() ? &fGS : nullptr, |
egdaniel | fa89632 | 2016-01-13 12:19:30 -0800 | [diff] [blame] | 111 | &fFS, |
| 112 | this->varyingHandler(), |
| 113 | this->uniformHandler(), |
Brian Salomon | 94efbf5 | 2016-11-29 13:43:05 -0500 | [diff] [blame] | 114 | this->shaderCaps(), |
egdaniel | fa89632 | 2016-01-13 12:19:30 -0800 | [diff] [blame] | 115 | proc, |
| 116 | outputColor->c_str(), |
| 117 | outputCoverage->c_str(), |
csmartdalton | 936f81b | 2017-02-13 15:45:35 -0700 | [diff] [blame] | 118 | rtAdjustName, |
Brian Salomon | e782f84 | 2018-07-31 13:53:11 -0400 | [diff] [blame] | 119 | texSamplers.get(), |
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; |
Brian Salomon | d90b3d3 | 2020-07-09 12:04:31 -0400 | [diff] [blame] | 132 | int fpCount = this->pipeline().numFragmentProcessors(); |
| 133 | fFragmentProcessors.reset(new std::unique_ptr<GrGLSLFragmentProcessor>[fpCount]); |
| 134 | for (int i = 0; i < fpCount; ++i) { |
John Stiles | d3feb6f | 2020-07-23 18:18:12 -0400 | [diff] [blame^] | 135 | SkString* inOut = this->pipeline().isColorFragmentProcessor(i) ? color : coverage; |
Ethan Nicholas | 2983f40 | 2017-05-08 09:36:08 -0400 | [diff] [blame] | 136 | SkString output; |
egdaniel | fa89632 | 2016-01-13 12:19:30 -0800 | [diff] [blame] | 137 | const GrFragmentProcessor& fp = this->pipeline().getFragmentProcessor(i); |
Brian Salomon | d90b3d3 | 2020-07-09 12:04:31 -0400 | [diff] [blame] | 138 | fFragmentProcessors[i] = std::unique_ptr<GrGLSLFragmentProcessor>(fp.createGLSLInstance()); |
John Stiles | d3feb6f | 2020-07-23 18:18:12 -0400 | [diff] [blame^] | 139 | output = this->emitFragProc(fp, *fFragmentProcessors[i], transformedCoordVarsIdx, *inOut, |
Brian Salomon | d90b3d3 | 2020-07-09 12:04:31 -0400 | [diff] [blame] | 140 | output); |
Brian Salomon | 87f4d29 | 2020-07-09 12:48:38 -0400 | [diff] [blame] | 141 | for (const auto& subFP : GrFragmentProcessor::FPRange(fp)) { |
Brian Osman | 9cf98dc | 2020-07-01 17:21:27 -0400 | [diff] [blame] | 142 | transformedCoordVarsIdx += subFP.numVaryingCoordsUsed(); |
bsalomon | a624bf3 | 2016-09-20 09:12:47 -0700 | [diff] [blame] | 143 | } |
John Stiles | d3feb6f | 2020-07-23 18:18:12 -0400 | [diff] [blame^] | 144 | *inOut = std::move(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 |
Brian Salomon | d90b3d3 | 2020-07-09 12:04:31 -0400 | [diff] [blame] | 150 | SkString GrGLSLProgramBuilder::emitFragProc(const GrFragmentProcessor& fp, |
| 151 | GrGLSLFragmentProcessor& glslFP, |
| 152 | int transformedCoordVarsIdx, |
| 153 | const SkString& input, |
| 154 | SkString output) { |
Ethan Nicholas | 2983f40 | 2017-05-08 09:36:08 -0400 | [diff] [blame] | 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 | |
Brian Salomon | e782f84 | 2018-07-31 13:53:11 -0400 | [diff] [blame] | 160 | int samplerIdx = 0; |
Brian Salomon | d90b3d3 | 2020-07-09 12:04:31 -0400 | [diff] [blame] | 161 | for (auto [subFP, subGLSLFP] : GrGLSLFragmentProcessor::ParallelRange(fp, glslFP)) { |
| 162 | if (auto* te = subFP.asTextureEffect()) { |
Brian Salomon | e782f84 | 2018-07-31 13:53:11 -0400 | [diff] [blame] | 163 | SkString name; |
| 164 | name.printf("TextureSampler_%d", samplerIdx++); |
Brian Salomon | d90b3d3 | 2020-07-09 12:04:31 -0400 | [diff] [blame] | 165 | |
| 166 | GrSamplerState samplerState = te->samplerState(); |
| 167 | const GrBackendFormat& format = te->view().proxy()->backendFormat(); |
| 168 | GrSwizzle swizzle = te->view().swizzle(); |
| 169 | SamplerHandle handle = this->emitSampler(format, samplerState, swizzle, name.c_str()); |
| 170 | static_cast<GrTextureEffect::Impl&>(subGLSLFP).setSamplerHandle(handle); |
Brian Salomon | e782f84 | 2018-07-31 13:53:11 -0400 | [diff] [blame] | 171 | } |
bsalomon | b58a2b4 | 2016-09-26 06:55:02 -0700 | [diff] [blame] | 172 | } |
Brian Osman | 9cf98dc | 2020-07-01 17:21:27 -0400 | [diff] [blame] | 173 | const GrShaderVar* coordVars = fTransformedCoordVars.begin() + transformedCoordVarsIdx; |
bsalomon | a624bf3 | 2016-09-20 09:12:47 -0700 | [diff] [blame] | 174 | GrGLSLFragmentProcessor::TransformedCoordVars coords(&fp, coordVars); |
egdaniel | fa89632 | 2016-01-13 12:19:30 -0800 | [diff] [blame] | 175 | GrGLSLFragmentProcessor::EmitArgs args(&fFS, |
| 176 | this->uniformHandler(), |
Brian Salomon | 94efbf5 | 2016-11-29 13:43:05 -0500 | [diff] [blame] | 177 | this->shaderCaps(), |
egdaniel | fa89632 | 2016-01-13 12:19:30 -0800 | [diff] [blame] | 178 | fp, |
Brian Salomon | 7a96c2a | 2020-07-09 17:58:20 -0400 | [diff] [blame] | 179 | "_output", |
| 180 | "_input", |
Michael Ludwig | e88320b | 2020-06-24 09:04:56 -0400 | [diff] [blame] | 181 | "_coords", |
Brian Salomon | d90b3d3 | 2020-07-09 12:04:31 -0400 | [diff] [blame] | 182 | coords); |
Brian Salomon | 7a96c2a | 2020-07-09 17:58:20 -0400 | [diff] [blame] | 183 | auto name = fFS.writeProcessorFunction(&glslFP, args); |
| 184 | fFS.codeAppendf("%s = %s(%s);", output.c_str(), name.c_str(), input.c_str()); |
egdaniel | fa89632 | 2016-01-13 12:19:30 -0800 | [diff] [blame] | 185 | |
| 186 | // We have to check that effects and the code they emit are consistent, ie if an effect |
| 187 | // asks for dst color, then the emit code needs to follow suit |
cdalton | 8733210 | 2016-02-26 12:22:02 -0800 | [diff] [blame] | 188 | SkDEBUGCODE(verify(fp);) |
egdaniel | fa89632 | 2016-01-13 12:19:30 -0800 | [diff] [blame] | 189 | |
Ethan Nicholas | 2983f40 | 2017-05-08 09:36:08 -0400 | [diff] [blame] | 190 | return output; |
egdaniel | fa89632 | 2016-01-13 12:19:30 -0800 | [diff] [blame] | 191 | } |
| 192 | |
Ethan Nicholas | 2983f40 | 2017-05-08 09:36:08 -0400 | [diff] [blame] | 193 | void GrGLSLProgramBuilder::emitAndInstallXferProc(const SkString& colorIn, |
| 194 | const SkString& coverageIn) { |
egdaniel | fa89632 | 2016-01-13 12:19:30 -0800 | [diff] [blame] | 195 | // Program builders have a bit of state we need to clear with each effect |
| 196 | AutoStageAdvance adv(this); |
| 197 | |
| 198 | SkASSERT(!fXferProcessor); |
Robert Phillips | 901aff0 | 2019-10-08 12:32:56 -0400 | [diff] [blame] | 199 | const GrXferProcessor& xp = this->pipeline().getXferProcessor(); |
Robert Phillips | 369e8b7 | 2017-08-01 16:13:04 -0400 | [diff] [blame] | 200 | fXferProcessor.reset(xp.createGLSLInstance()); |
egdaniel | fa89632 | 2016-01-13 12:19:30 -0800 | [diff] [blame] | 201 | |
| 202 | // Enable dual source secondary output if we have one |
| 203 | if (xp.hasSecondaryOutput()) { |
| 204 | fFS.enableSecondaryOutput(); |
| 205 | } |
| 206 | |
Brian Salomon | 94efbf5 | 2016-11-29 13:43:05 -0500 | [diff] [blame] | 207 | if (this->shaderCaps()->mustDeclareFragmentShaderOutput()) { |
egdaniel | fa89632 | 2016-01-13 12:19:30 -0800 | [diff] [blame] | 208 | fFS.enableCustomOutput(); |
| 209 | } |
| 210 | |
| 211 | SkString openBrace; |
| 212 | openBrace.printf("{ // Xfer Processor: %s\n", xp.name()); |
| 213 | fFS.codeAppend(openBrace.c_str()); |
| 214 | |
Brian Salomon | 18dfa98 | 2017-04-03 16:57:43 -0400 | [diff] [blame] | 215 | SamplerHandle dstTextureSamplerHandle; |
| 216 | GrSurfaceOrigin dstTextureOrigin = kTopLeft_GrSurfaceOrigin; |
Robert Phillips | bb581ce | 2017-05-29 15:05:15 -0400 | [diff] [blame] | 217 | |
Greg Daniel | 524e28b | 2019-11-01 11:48:53 -0400 | [diff] [blame] | 218 | const GrSurfaceProxyView& dstView = this->pipeline().dstProxyView(); |
| 219 | if (GrTextureProxy* dstTextureProxy = dstView.asTextureProxy()) { |
Brian Salomon | 18dfa98 | 2017-04-03 16:57:43 -0400 | [diff] [blame] | 220 | // GrProcessor::TextureSampler sampler(dstTexture); |
Greg Daniel | 524e28b | 2019-11-01 11:48:53 -0400 | [diff] [blame] | 221 | const GrSwizzle& swizzle = dstView.swizzle(); |
Chris Dalton | 1b1b0d5 | 2020-03-03 12:00:59 -0700 | [diff] [blame] | 222 | dstTextureSamplerHandle = this->emitSampler(dstTextureProxy->backendFormat(), |
| 223 | GrSamplerState(), swizzle, "DstTextureSampler"); |
Greg Daniel | 524e28b | 2019-11-01 11:48:53 -0400 | [diff] [blame] | 224 | dstTextureOrigin = dstView.origin(); |
Robert Phillips | 66ae304 | 2019-10-10 12:57:58 -0400 | [diff] [blame] | 225 | SkASSERT(dstTextureProxy->textureType() != GrTextureType::kExternal); |
Brian Salomon | 18dfa98 | 2017-04-03 16:57:43 -0400 | [diff] [blame] | 226 | } |
egdaniel | fa89632 | 2016-01-13 12:19:30 -0800 | [diff] [blame] | 227 | |
Brian Salomon | f19f9ca | 2019-09-18 15:54:26 -0400 | [diff] [blame] | 228 | SkString finalInColor = colorIn.size() ? colorIn : SkString("float4(1)"); |
Brian Osman | 65cdd61 | 2019-03-14 17:01:57 -0400 | [diff] [blame] | 229 | |
egdaniel | fa89632 | 2016-01-13 12:19:30 -0800 | [diff] [blame] | 230 | GrGLSLXferProcessor::EmitArgs args(&fFS, |
| 231 | this->uniformHandler(), |
Brian Salomon | 94efbf5 | 2016-11-29 13:43:05 -0500 | [diff] [blame] | 232 | this->shaderCaps(), |
Brian Salomon | 18dfa98 | 2017-04-03 16:57:43 -0400 | [diff] [blame] | 233 | xp, |
Brian Osman | 65cdd61 | 2019-03-14 17:01:57 -0400 | [diff] [blame] | 234 | finalInColor.c_str(), |
Ethan Nicholas | 5af9ea3 | 2017-07-28 15:19:46 -0400 | [diff] [blame] | 235 | coverageIn.size() ? coverageIn.c_str() : "float4(1)", |
egdaniel | fa89632 | 2016-01-13 12:19:30 -0800 | [diff] [blame] | 236 | fFS.getPrimaryColorOutputName(), |
| 237 | fFS.getSecondaryColorOutputName(), |
Brian Salomon | 18dfa98 | 2017-04-03 16:57:43 -0400 | [diff] [blame] | 238 | dstTextureSamplerHandle, |
Chris Dalton | 6b76df0 | 2019-04-08 11:01:34 -0600 | [diff] [blame] | 239 | dstTextureOrigin, |
Brian Salomon | 982f546 | 2020-03-30 12:52:33 -0400 | [diff] [blame] | 240 | this->pipeline().writeSwizzle()); |
egdaniel | fa89632 | 2016-01-13 12:19:30 -0800 | [diff] [blame] | 241 | fXferProcessor->emitCode(args); |
| 242 | |
| 243 | // We have to check that effects and the code they emit are consistent, ie if an effect |
| 244 | // asks for dst color, then the emit code needs to follow suit |
cdalton | 8733210 | 2016-02-26 12:22:02 -0800 | [diff] [blame] | 245 | SkDEBUGCODE(verify(xp);) |
egdaniel | fa89632 | 2016-01-13 12:19:30 -0800 | [diff] [blame] | 246 | fFS.codeAppend("}"); |
| 247 | } |
| 248 | |
Chris Dalton | 1b1b0d5 | 2020-03-03 12:00:59 -0700 | [diff] [blame] | 249 | GrGLSLProgramBuilder::SamplerHandle GrGLSLProgramBuilder::emitSampler( |
| 250 | const GrBackendFormat& backendFormat, GrSamplerState state, const GrSwizzle& swizzle, |
| 251 | const char* name) { |
Greg Daniel | 0f70be8 | 2018-10-08 17:35:08 +0000 | [diff] [blame] | 252 | ++fNumFragmentSamplers; |
Chris Dalton | 1b1b0d5 | 2020-03-03 12:00:59 -0700 | [diff] [blame] | 253 | return this->uniformHandler()->addSampler(backendFormat, state, swizzle, name, |
| 254 | this->shaderCaps()); |
Brian Salomon | f9f4512 | 2016-11-29 11:59:17 -0500 | [diff] [blame] | 255 | } |
| 256 | |
cdalton | 9c3f143 | 2016-03-11 10:07:37 -0800 | [diff] [blame] | 257 | bool GrGLSLProgramBuilder::checkSamplerCounts() { |
Brian Salomon | 1edc5b9 | 2016-11-29 13:43:46 -0500 | [diff] [blame] | 258 | const GrShaderCaps& shaderCaps = *this->shaderCaps(); |
Brian Salomon | 1edc5b9 | 2016-11-29 13:43:46 -0500 | [diff] [blame] | 259 | if (fNumFragmentSamplers > shaderCaps.maxFragmentSamplers()) { |
cdalton | 9c3f143 | 2016-03-11 10:07:37 -0800 | [diff] [blame] | 260 | GrCapsDebugf(this->caps(), "Program would use too many fragment samplers\n"); |
| 261 | return false; |
| 262 | } |
cdalton | 9c3f143 | 2016-03-11 10:07:37 -0800 | [diff] [blame] | 263 | return true; |
| 264 | } |
| 265 | |
cdalton | 8733210 | 2016-02-26 12:22:02 -0800 | [diff] [blame] | 266 | #ifdef SK_DEBUG |
egdaniel | fa89632 | 2016-01-13 12:19:30 -0800 | [diff] [blame] | 267 | void GrGLSLProgramBuilder::verify(const GrPrimitiveProcessor& gp) { |
Chris Dalton | d7291ba | 2019-03-07 14:17:03 -0700 | [diff] [blame] | 268 | SkASSERT(!fFS.fHasReadDstColorThisStage_DebugOnly); |
| 269 | SkASSERT(fFS.fUsedProcessorFeaturesThisStage_DebugOnly == gp.requestedFeatures()); |
egdaniel | fa89632 | 2016-01-13 12:19:30 -0800 | [diff] [blame] | 270 | } |
| 271 | |
| 272 | void GrGLSLProgramBuilder::verify(const GrFragmentProcessor& fp) { |
Chris Dalton | d7291ba | 2019-03-07 14:17:03 -0700 | [diff] [blame] | 273 | SkASSERT(!fFS.fHasReadDstColorThisStage_DebugOnly); |
| 274 | SkASSERT(fFS.fUsedProcessorFeaturesThisStage_DebugOnly == fp.requestedFeatures()); |
| 275 | } |
| 276 | |
| 277 | void GrGLSLProgramBuilder::verify(const GrXferProcessor& xp) { |
| 278 | SkASSERT(xp.willReadDstColor() == fFS.fHasReadDstColorThisStage_DebugOnly); |
| 279 | SkASSERT(fFS.fUsedProcessorFeaturesThisStage_DebugOnly == xp.requestedFeatures()); |
egdaniel | 8dcdedc | 2015-11-11 06:27:20 -0800 | [diff] [blame] | 280 | } |
cdalton | 8733210 | 2016-02-26 12:22:02 -0800 | [diff] [blame] | 281 | #endif |
egdaniel | 8dcdedc | 2015-11-11 06:27:20 -0800 | [diff] [blame] | 282 | |
| 283 | void GrGLSLProgramBuilder::nameVariable(SkString* out, char prefix, const char* name, bool mangle) { |
| 284 | if ('\0' == prefix) { |
| 285 | *out = name; |
| 286 | } else { |
| 287 | out->printf("%c%s", prefix, name); |
| 288 | } |
| 289 | if (mangle) { |
| 290 | if (out->endsWith('_')) { |
| 291 | // Names containing "__" are reserved. |
| 292 | out->append("x"); |
| 293 | } |
| 294 | out->appendf("_Stage%d%s", fStageIndex, fFS.getMangleString().c_str()); |
| 295 | } |
| 296 | } |
| 297 | |
Ethan Nicholas | 2983f40 | 2017-05-08 09:36:08 -0400 | [diff] [blame] | 298 | void GrGLSLProgramBuilder::nameExpression(SkString* output, const char* baseName) { |
egdaniel | fa89632 | 2016-01-13 12:19:30 -0800 | [diff] [blame] | 299 | // create var to hold stage result. If we already have a valid output name, just use that |
| 300 | // otherwise create a new mangled one. This name is only valid if we are reordering stages |
| 301 | // and have to tell stage exactly where to put its output. |
| 302 | SkString outName; |
Ethan Nicholas | 2983f40 | 2017-05-08 09:36:08 -0400 | [diff] [blame] | 303 | if (output->size()) { |
egdaniel | fa89632 | 2016-01-13 12:19:30 -0800 | [diff] [blame] | 304 | outName = output->c_str(); |
| 305 | } else { |
| 306 | this->nameVariable(&outName, '\0', baseName); |
| 307 | } |
Ethan Nicholas | f7b8820 | 2017-09-18 14:10:39 -0400 | [diff] [blame] | 308 | fFS.codeAppendf("half4 %s;", outName.c_str()); |
egdaniel | fa89632 | 2016-01-13 12:19:30 -0800 | [diff] [blame] | 309 | *output = outName; |
| 310 | } |
| 311 | |
cdalton | 5e58cee | 2016-02-11 12:49:47 -0800 | [diff] [blame] | 312 | void GrGLSLProgramBuilder::appendUniformDecls(GrShaderFlags visibility, SkString* out) const { |
egdaniel | 7ea439b | 2015-12-03 09:20:44 -0800 | [diff] [blame] | 313 | this->uniformHandler()->appendUniformDecls(visibility, out); |
| 314 | } |
| 315 | |
Ethan Nicholas | cd700e9 | 2018-08-24 16:43:57 -0400 | [diff] [blame] | 316 | void GrGLSLProgramBuilder::addRTWidthUniform(const char* name) { |
Brian Osman | f792445 | 2019-09-24 10:44:37 -0400 | [diff] [blame] | 317 | SkASSERT(!fUniformHandles.fRTWidthUni.isValid()); |
| 318 | GrGLSLUniformHandler* uniformHandler = this->uniformHandler(); |
| 319 | fUniformHandles.fRTWidthUni = |
Ethan Nicholas | 16464c3 | 2020-04-06 13:53:05 -0400 | [diff] [blame] | 320 | uniformHandler->internalAddUniformArray(nullptr, kFragment_GrShaderFlag, kHalf_GrSLType, |
| 321 | name, false, 0, nullptr); |
Ethan Nicholas | cd700e9 | 2018-08-24 16:43:57 -0400 | [diff] [blame] | 322 | } |
| 323 | |
Greg Daniel | e6ab998 | 2018-08-22 13:56:32 +0000 | [diff] [blame] | 324 | void GrGLSLProgramBuilder::addRTHeightUniform(const char* name) { |
Brian Osman | f792445 | 2019-09-24 10:44:37 -0400 | [diff] [blame] | 325 | SkASSERT(!fUniformHandles.fRTHeightUni.isValid()); |
| 326 | GrGLSLUniformHandler* uniformHandler = this->uniformHandler(); |
| 327 | fUniformHandles.fRTHeightUni = |
Ethan Nicholas | 16464c3 | 2020-04-06 13:53:05 -0400 | [diff] [blame] | 328 | uniformHandler->internalAddUniformArray(nullptr, kFragment_GrShaderFlag, kHalf_GrSLType, |
| 329 | name, false, 0, nullptr); |
egdaniel | 8dcdedc | 2015-11-11 06:27:20 -0800 | [diff] [blame] | 330 | } |
| 331 | |
egdaniel | 9f1d415 | 2016-02-10 09:50:38 -0800 | [diff] [blame] | 332 | void GrGLSLProgramBuilder::finalizeShaders() { |
| 333 | this->varyingHandler()->finalize(); |
cdalton | 5e58cee | 2016-02-11 12:49:47 -0800 | [diff] [blame] | 334 | fVS.finalize(kVertex_GrShaderFlag); |
csmartdalton | 276cc41 | 2016-11-21 11:55:00 -0700 | [diff] [blame] | 335 | if (this->primitiveProcessor().willUseGeoShader()) { |
Brian Salomon | 94efbf5 | 2016-11-29 13:43:05 -0500 | [diff] [blame] | 336 | SkASSERT(this->shaderCaps()->geometryShaderSupport()); |
csmartdalton | 276cc41 | 2016-11-21 11:55:00 -0700 | [diff] [blame] | 337 | fGS.finalize(kGeometry_GrShaderFlag); |
| 338 | } |
cdalton | 5e58cee | 2016-02-11 12:49:47 -0800 | [diff] [blame] | 339 | fFS.finalize(kFragment_GrShaderFlag); |
egdaniel | 9f1d415 | 2016-02-10 09:50:38 -0800 | [diff] [blame] | 340 | } |