Chris Dalton | 4e99853 | 2020-02-10 11:06:42 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2020 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 "src/gpu/tessellate/GrDrawAtlasPathOp.h" |
| 9 | |
| 10 | #include "src/gpu/GrOpFlushState.h" |
| 11 | #include "src/gpu/GrOpsRenderPass.h" |
| 12 | #include "src/gpu/GrProgramInfo.h" |
Robert Phillips | 1a82a4e | 2021-07-01 10:27:44 -0400 | [diff] [blame] | 13 | #include "src/gpu/GrResourceProvider.h" |
Chris Dalton | baae2dd | 2021-06-25 14:52:49 -0600 | [diff] [blame] | 14 | #include "src/gpu/GrVertexWriter.h" |
Chris Dalton | 4e99853 | 2020-02-10 11:06:42 -0700 | [diff] [blame] | 15 | #include "src/gpu/glsl/GrGLSLFragmentShaderBuilder.h" |
| 16 | #include "src/gpu/glsl/GrGLSLGeometryProcessor.h" |
| 17 | #include "src/gpu/glsl/GrGLSLVarying.h" |
| 18 | #include "src/gpu/glsl/GrGLSLVertexGeoBuilder.h" |
| 19 | |
| 20 | namespace { |
| 21 | |
Chris Dalton | 4e99853 | 2020-02-10 11:06:42 -0700 | [diff] [blame] | 22 | class DrawAtlasPathShader : public GrGeometryProcessor { |
| 23 | public: |
Chris Dalton | b1fd64e | 2021-07-08 15:38:51 -0600 | [diff] [blame^] | 24 | DrawAtlasPathShader(bool usesLocalCoords, const GrAtlasInstancedHelper* atlasHelper, |
| 25 | const GrShaderCaps& shaderCaps) |
Chris Dalton | 4e99853 | 2020-02-10 11:06:42 -0700 | [diff] [blame] | 26 | : GrGeometryProcessor(kDrawAtlasPathShader_ClassID) |
Chris Dalton | b1fd64e | 2021-07-08 15:38:51 -0600 | [diff] [blame^] | 27 | , fUsesLocalCoords(usesLocalCoords) |
| 28 | , fAtlasHelper(atlasHelper) |
| 29 | , fAtlasAccess(GrSamplerState::Filter::kNearest, fAtlasHelper->proxy()->backendFormat(), |
| 30 | fAtlasHelper->atlasSwizzle()) { |
Chris Dalton | a05ccc3 | 2021-06-29 19:42:13 -0600 | [diff] [blame] | 31 | if (!shaderCaps.vertexIDSupport()) { |
Chris Dalton | ee40d5a | 2021-07-07 16:34:36 -0600 | [diff] [blame] | 32 | constexpr static Attribute kUnitCoordAttrib("unitCoord", kFloat2_GrVertexAttribType, |
Chris Dalton | a05ccc3 | 2021-06-29 19:42:13 -0600 | [diff] [blame] | 33 | kFloat2_GrSLType); |
| 34 | this->setVertexAttributes(&kUnitCoordAttrib, 1); |
| 35 | } |
Chris Dalton | ee40d5a | 2021-07-07 16:34:36 -0600 | [diff] [blame] | 36 | fAttribs.emplace_back("fillBounds", kFloat4_GrVertexAttribType, kFloat4_GrSLType); |
| 37 | if (fUsesLocalCoords) { |
| 38 | fAttribs.emplace_back("affineMatrix", kFloat4_GrVertexAttribType, kFloat4_GrSLType); |
| 39 | fAttribs.emplace_back("translate", kFloat2_GrVertexAttribType, kFloat2_GrSLType); |
| 40 | } |
| 41 | SkASSERT(fAttribs.count() == this->colorAttribIdx()); |
| 42 | fAttribs.emplace_back("color", kFloat4_GrVertexAttribType, kHalf4_GrSLType); |
Chris Dalton | b1fd64e | 2021-07-08 15:38:51 -0600 | [diff] [blame^] | 43 | fAtlasHelper->appendInstanceAttribs(&fAttribs); |
| 44 | SkASSERT(fAttribs.count() <= kMaxInstanceAttribs); |
Chris Dalton | ee40d5a | 2021-07-07 16:34:36 -0600 | [diff] [blame] | 45 | this->setInstanceAttributes(fAttribs.data(), fAttribs.count()); |
Chris Dalton | 4e99853 | 2020-02-10 11:06:42 -0700 | [diff] [blame] | 46 | this->setTextureSamplerCnt(1); |
| 47 | } |
| 48 | |
| 49 | private: |
Chris Dalton | ee40d5a | 2021-07-07 16:34:36 -0600 | [diff] [blame] | 50 | int colorAttribIdx() const { return fUsesLocalCoords ? 3 : 1; } |
Chris Dalton | 4e99853 | 2020-02-10 11:06:42 -0700 | [diff] [blame] | 51 | const char* name() const override { return "DrawAtlasPathShader"; } |
| 52 | void getGLSLProcessorKey(const GrShaderCaps&, GrProcessorKeyBuilder* b) const override { |
Chris Dalton | b1fd64e | 2021-07-08 15:38:51 -0600 | [diff] [blame^] | 53 | b->addBits(1, fUsesLocalCoords, "localCoords"); |
| 54 | fAtlasHelper->getKeyBits(b); |
Chris Dalton | 4e99853 | 2020-02-10 11:06:42 -0700 | [diff] [blame] | 55 | } |
| 56 | const TextureSampler& onTextureSampler(int) const override { return fAtlasAccess; } |
Robert Phillips | f10535f | 2021-03-23 09:30:45 -0400 | [diff] [blame] | 57 | GrGLSLGeometryProcessor* createGLSLInstance(const GrShaderCaps&) const override; |
Chris Dalton | 4e99853 | 2020-02-10 11:06:42 -0700 | [diff] [blame] | 58 | |
Chris Dalton | 4e99853 | 2020-02-10 11:06:42 -0700 | [diff] [blame] | 59 | const bool fUsesLocalCoords; |
Chris Dalton | b1fd64e | 2021-07-08 15:38:51 -0600 | [diff] [blame^] | 60 | const GrAtlasInstancedHelper* const fAtlasHelper; |
| 61 | TextureSampler fAtlasAccess; |
| 62 | constexpr static int kMaxInstanceAttribs = 6; |
| 63 | SkSTArray<kMaxInstanceAttribs, GrGeometryProcessor::Attribute> fAttribs; |
Chris Dalton | 4e99853 | 2020-02-10 11:06:42 -0700 | [diff] [blame] | 64 | |
| 65 | class Impl; |
| 66 | }; |
| 67 | |
| 68 | class DrawAtlasPathShader::Impl : public GrGLSLGeometryProcessor { |
| 69 | void onEmitCode(EmitArgs& args, GrGPArgs* gpArgs) override { |
Robert Phillips | 787fd9d | 2021-03-22 14:48:09 -0400 | [diff] [blame] | 70 | const auto& shader = args.fGeomProc.cast<DrawAtlasPathShader>(); |
Chris Dalton | 4e99853 | 2020-02-10 11:06:42 -0700 | [diff] [blame] | 71 | args.fVaryingHandler->emitAttributes(shader); |
| 72 | |
Chris Dalton | a05ccc3 | 2021-06-29 19:42:13 -0600 | [diff] [blame] | 73 | if (args.fShaderCaps->vertexIDSupport()) { |
Chris Dalton | ee40d5a | 2021-07-07 16:34:36 -0600 | [diff] [blame] | 74 | // If we don't have sk_VertexID support then "unitCoord" already came in as a vertex |
Chris Dalton | a05ccc3 | 2021-06-29 19:42:13 -0600 | [diff] [blame] | 75 | // attrib. |
| 76 | args.fVertBuilder->codeAppendf(R"( |
Chris Dalton | ee40d5a | 2021-07-07 16:34:36 -0600 | [diff] [blame] | 77 | float2 unitCoord = float2(sk_VertexID & 1, sk_VertexID >> 1);)"); |
Chris Dalton | a05ccc3 | 2021-06-29 19:42:13 -0600 | [diff] [blame] | 78 | } |
| 79 | |
Chris Dalton | 4e99853 | 2020-02-10 11:06:42 -0700 | [diff] [blame] | 80 | args.fVertBuilder->codeAppendf(R"( |
Chris Dalton | ee40d5a | 2021-07-07 16:34:36 -0600 | [diff] [blame] | 81 | float2 devCoord = mix(fillBounds.xy, fillBounds.zw, unitCoord);)"); |
| 82 | gpArgs->fPositionVar.set(kFloat2_GrSLType, "devCoord"); |
Chris Dalton | 4e99853 | 2020-02-10 11:06:42 -0700 | [diff] [blame] | 83 | |
Chris Dalton | 4e99853 | 2020-02-10 11:06:42 -0700 | [diff] [blame] | 84 | if (shader.fUsesLocalCoords) { |
| 85 | args.fVertBuilder->codeAppendf(R"( |
Chris Dalton | ee40d5a | 2021-07-07 16:34:36 -0600 | [diff] [blame] | 86 | float2x2 M = float2x2(affineMatrix); |
| 87 | float2 localCoord = inverse(M) * (devCoord - translate);)"); |
| 88 | gpArgs->fLocalCoordVar.set(kFloat2_GrSLType, "localCoord"); |
Chris Dalton | 4e99853 | 2020-02-10 11:06:42 -0700 | [diff] [blame] | 89 | } |
Chris Dalton | 4e99853 | 2020-02-10 11:06:42 -0700 | [diff] [blame] | 90 | |
Chris Dalton | b1fd64e | 2021-07-08 15:38:51 -0600 | [diff] [blame^] | 91 | args.fFragBuilder->codeAppendf("half4 %s = half4(1);", args.fOutputCoverage); |
| 92 | shader.fAtlasHelper->injectShaderCode(args, gpArgs->fPositionVar, &fAtlasAdjustUniform); |
Chris Dalton | ee40d5a | 2021-07-07 16:34:36 -0600 | [diff] [blame] | 93 | |
| 94 | args.fFragBuilder->codeAppendf("half4 %s;", args.fOutputColor); |
| 95 | args.fVaryingHandler->addPassThroughAttribute( |
| 96 | shader.fAttribs[shader.colorAttribIdx()], args.fOutputColor, |
| 97 | GrGLSLVaryingHandler::Interpolation::kCanBeFlat); |
Chris Dalton | 4e99853 | 2020-02-10 11:06:42 -0700 | [diff] [blame] | 98 | } |
| 99 | |
Brian Osman | 609f159 | 2020-07-01 15:14:39 -0400 | [diff] [blame] | 100 | void setData(const GrGLSLProgramDataManager& pdman, |
Brian Salomon | 5a32828 | 2021-04-14 10:32:25 -0400 | [diff] [blame] | 101 | const GrShaderCaps&, |
Robert Phillips | 787fd9d | 2021-03-22 14:48:09 -0400 | [diff] [blame] | 102 | const GrGeometryProcessor& geomProc) override { |
Chris Dalton | b1fd64e | 2021-07-08 15:38:51 -0600 | [diff] [blame^] | 103 | auto* atlasHelper = geomProc.cast<DrawAtlasPathShader>().fAtlasHelper; |
| 104 | atlasHelper->setUniformData(pdman, fAtlasAdjustUniform); |
Chris Dalton | 4e99853 | 2020-02-10 11:06:42 -0700 | [diff] [blame] | 105 | } |
| 106 | |
| 107 | GrGLSLUniformHandler::UniformHandle fAtlasAdjustUniform; |
| 108 | }; |
| 109 | |
Robert Phillips | f10535f | 2021-03-23 09:30:45 -0400 | [diff] [blame] | 110 | GrGLSLGeometryProcessor* DrawAtlasPathShader::createGLSLInstance(const GrShaderCaps&) const { |
Chris Dalton | 4e99853 | 2020-02-10 11:06:42 -0700 | [diff] [blame] | 111 | return new Impl(); |
| 112 | } |
| 113 | |
| 114 | } // namespace |
| 115 | |
| 116 | GrProcessorSet::Analysis GrDrawAtlasPathOp::finalize(const GrCaps& caps, const GrAppliedClip* clip, |
Chris Dalton | 4e99853 | 2020-02-10 11:06:42 -0700 | [diff] [blame] | 117 | GrClampType clampType) { |
| 118 | const GrProcessorSet::Analysis& analysis = fProcessors.finalize( |
Chris Dalton | ee40d5a | 2021-07-07 16:34:36 -0600 | [diff] [blame] | 119 | fHeadInstance->fColor, GrProcessorAnalysisCoverage::kSingleChannel, clip, |
| 120 | &GrUserStencilSettings::kUnused, caps, clampType, &fHeadInstance->fColor); |
Chris Dalton | 4e99853 | 2020-02-10 11:06:42 -0700 | [diff] [blame] | 121 | fUsesLocalCoords = analysis.usesLocalCoords(); |
| 122 | return analysis; |
| 123 | } |
| 124 | |
Chris Dalton | b1fd64e | 2021-07-08 15:38:51 -0600 | [diff] [blame^] | 125 | GrOp::CombineResult GrDrawAtlasPathOp::onCombineIfPossible(GrOp* op, SkArenaAlloc*, const GrCaps&) { |
Chris Dalton | 4e99853 | 2020-02-10 11:06:42 -0700 | [diff] [blame] | 126 | auto* that = op->cast<GrDrawAtlasPathOp>(); |
Chris Dalton | 4e99853 | 2020-02-10 11:06:42 -0700 | [diff] [blame] | 127 | SkASSERT(fEnableHWAA == that->fEnableHWAA); |
| 128 | |
Chris Dalton | b1fd64e | 2021-07-08 15:38:51 -0600 | [diff] [blame^] | 129 | if (!fAtlasHelper.isCompatible(that->fAtlasHelper) || |
| 130 | fProcessors != that->fProcessors) { |
Chris Dalton | 4e99853 | 2020-02-10 11:06:42 -0700 | [diff] [blame] | 131 | return CombineResult::kCannotCombine; |
| 132 | } |
| 133 | |
| 134 | SkASSERT(fUsesLocalCoords == that->fUsesLocalCoords); |
Chris Dalton | ee40d5a | 2021-07-07 16:34:36 -0600 | [diff] [blame] | 135 | *fTailInstance = that->fHeadInstance; |
| 136 | fTailInstance = that->fTailInstance; |
Chris Dalton | 4e99853 | 2020-02-10 11:06:42 -0700 | [diff] [blame] | 137 | fInstanceCount += that->fInstanceCount; |
| 138 | return CombineResult::kMerged; |
| 139 | } |
| 140 | |
Chris Dalton | ee40d5a | 2021-07-07 16:34:36 -0600 | [diff] [blame] | 141 | void GrDrawAtlasPathOp::prepareProgram(const GrCaps& caps, SkArenaAlloc* arena, |
| 142 | const GrSurfaceProxyView& writeView, |
| 143 | GrAppliedClip&& appliedClip, |
| 144 | const GrDstProxyView& dstProxyView, |
| 145 | GrXferBarrierFlags renderPassXferBarriers, |
| 146 | GrLoadOp colorLoadOp) { |
| 147 | SkASSERT(!fProgram); |
Chris Dalton | 4e99853 | 2020-02-10 11:06:42 -0700 | [diff] [blame] | 148 | GrPipeline::InitArgs initArgs; |
| 149 | if (fEnableHWAA) { |
| 150 | initArgs.fInputFlags |= GrPipeline::InputFlags::kHWAntialias; |
| 151 | } |
Chris Dalton | ee40d5a | 2021-07-07 16:34:36 -0600 | [diff] [blame] | 152 | initArgs.fCaps = ∩︀ |
| 153 | initArgs.fDstProxyView = dstProxyView; |
| 154 | initArgs.fWriteSwizzle = writeView.swizzle(); |
Chris Dalton | baae2dd | 2021-06-25 14:52:49 -0600 | [diff] [blame] | 155 | auto pipeline = arena->make<GrPipeline>(initArgs, std::move(fProcessors), |
Chris Dalton | ee40d5a | 2021-07-07 16:34:36 -0600 | [diff] [blame] | 156 | std::move(appliedClip)); |
Chris Dalton | b1fd64e | 2021-07-08 15:38:51 -0600 | [diff] [blame^] | 157 | auto shader = arena->make<DrawAtlasPathShader>(fUsesLocalCoords, &fAtlasHelper, |
| 158 | *caps.shaderCaps()); |
Chris Dalton | ee40d5a | 2021-07-07 16:34:36 -0600 | [diff] [blame] | 159 | fProgram = arena->make<GrProgramInfo>(writeView, pipeline, &GrUserStencilSettings::kUnused, |
| 160 | shader, GrPrimitiveType::kTriangleStrip, 0, |
| 161 | renderPassXferBarriers, colorLoadOp); |
| 162 | } |
Chris Dalton | 012f849 | 2020-03-05 11:49:15 -0700 | [diff] [blame] | 163 | |
Chris Dalton | ee40d5a | 2021-07-07 16:34:36 -0600 | [diff] [blame] | 164 | void GrDrawAtlasPathOp::onPrePrepare(GrRecordingContext* rContext, |
| 165 | const GrSurfaceProxyView& writeView, |
| 166 | GrAppliedClip* appliedClip, const GrDstProxyView& dstProxyView, |
| 167 | GrXferBarrierFlags renderPassXferBarriers, |
| 168 | GrLoadOp colorLoadOp) { |
| 169 | this->prepareProgram(*rContext->priv().caps(), rContext->priv().recordTimeAllocator(), |
| 170 | writeView, std::move(*appliedClip), dstProxyView, renderPassXferBarriers, |
| 171 | colorLoadOp); |
| 172 | SkASSERT(fProgram); |
| 173 | rContext->priv().recordProgramInfo(fProgram); |
| 174 | } |
| 175 | |
| 176 | GR_DECLARE_STATIC_UNIQUE_KEY(gUnitQuadBufferKey); |
| 177 | |
| 178 | void GrDrawAtlasPathOp::onPrepare(GrOpFlushState* flushState) { |
| 179 | if (!fProgram) { |
| 180 | this->prepareProgram(flushState->caps(), flushState->allocator(), flushState->writeView(), |
| 181 | flushState->detachAppliedClip(), flushState->dstProxyView(), |
| 182 | flushState->renderPassBarriers(), flushState->colorLoadOp()); |
| 183 | SkASSERT(fProgram); |
| 184 | } |
| 185 | |
| 186 | if (GrVertexWriter instanceWriter = flushState->makeVertexSpace( |
| 187 | fProgram->geomProc().instanceStride(), fInstanceCount, &fInstanceBuffer, |
| 188 | &fBaseInstance)) { |
Chris Dalton | b1fd64e | 2021-07-08 15:38:51 -0600 | [diff] [blame^] | 189 | for (const Instance* i = fHeadInstance; i; i = i->fNext) { |
Chris Dalton | baae2dd | 2021-06-25 14:52:49 -0600 | [diff] [blame] | 190 | instanceWriter.write( |
Chris Dalton | b1fd64e | 2021-07-08 15:38:51 -0600 | [diff] [blame^] | 191 | SkRect::Make(i->fFillBounds), |
Chris Dalton | ee40d5a | 2021-07-07 16:34:36 -0600 | [diff] [blame] | 192 | GrVertexWriter::If(fUsesLocalCoords, |
Chris Dalton | b1fd64e | 2021-07-08 15:38:51 -0600 | [diff] [blame^] | 193 | i->fLocalToDeviceIfUsingLocalCoords), |
| 194 | i->fColor); |
| 195 | fAtlasHelper.writeInstanceData(&instanceWriter, &i->fAtlasInstance); |
Chris Dalton | baae2dd | 2021-06-25 14:52:49 -0600 | [diff] [blame] | 196 | } |
| 197 | } |
Chris Dalton | a05ccc3 | 2021-06-29 19:42:13 -0600 | [diff] [blame] | 198 | |
Chris Dalton | ee40d5a | 2021-07-07 16:34:36 -0600 | [diff] [blame] | 199 | if (!flushState->caps().shaderCaps()->vertexIDSupport()) { |
Chris Dalton | a05ccc3 | 2021-06-29 19:42:13 -0600 | [diff] [blame] | 200 | constexpr static SkPoint kUnitQuad[4] = {{0,0}, {0,1}, {1,0}, {1,1}}; |
| 201 | |
| 202 | GR_DEFINE_STATIC_UNIQUE_KEY(gUnitQuadBufferKey); |
| 203 | |
Chris Dalton | ee40d5a | 2021-07-07 16:34:36 -0600 | [diff] [blame] | 204 | fVertexBufferIfNoIDSupport = flushState->resourceProvider()->findOrMakeStaticBuffer( |
Chris Dalton | a05ccc3 | 2021-06-29 19:42:13 -0600 | [diff] [blame] | 205 | GrGpuBufferType::kVertex, sizeof(kUnitQuad), kUnitQuad, gUnitQuadBufferKey); |
| 206 | } |
Chris Dalton | baae2dd | 2021-06-25 14:52:49 -0600 | [diff] [blame] | 207 | } |
Chris Dalton | 4e99853 | 2020-02-10 11:06:42 -0700 | [diff] [blame] | 208 | |
Chris Dalton | ee40d5a | 2021-07-07 16:34:36 -0600 | [diff] [blame] | 209 | void GrDrawAtlasPathOp::onExecute(GrOpFlushState* flushState, const SkRect& chainBounds) { |
Chris Dalton | ee40d5a | 2021-07-07 16:34:36 -0600 | [diff] [blame] | 210 | flushState->bindPipelineAndScissorClip(*fProgram, this->bounds()); |
Chris Dalton | b1fd64e | 2021-07-08 15:38:51 -0600 | [diff] [blame^] | 211 | flushState->bindTextures(fProgram->geomProc(), *fAtlasHelper.proxy(), fProgram->pipeline()); |
Chris Dalton | ee40d5a | 2021-07-07 16:34:36 -0600 | [diff] [blame] | 212 | flushState->bindBuffers(nullptr, std::move(fInstanceBuffer), fVertexBufferIfNoIDSupport); |
| 213 | flushState->drawInstanced(fInstanceCount, fBaseInstance, 4, 0); |
Chris Dalton | 4e99853 | 2020-02-10 11:06:42 -0700 | [diff] [blame] | 214 | } |