blob: fc4c2fb33343d032a9fca016067b9b2e374b493e [file] [log] [blame]
Chris Dalton4e998532020-02-10 11:06:42 -07001/*
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
Chris Daltonc3176002021-07-23 15:33:09 -06008#include "src/gpu/ops/GrDrawAtlasPathOp.h"
Chris Dalton4e998532020-02-10 11:06:42 -07009
10#include "src/gpu/GrOpFlushState.h"
11#include "src/gpu/GrOpsRenderPass.h"
12#include "src/gpu/GrProgramInfo.h"
Robert Phillips1a82a4e2021-07-01 10:27:44 -040013#include "src/gpu/GrResourceProvider.h"
Chris Daltonbaae2dd2021-06-25 14:52:49 -060014#include "src/gpu/GrVertexWriter.h"
Chris Dalton4e998532020-02-10 11:06:42 -070015#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
20namespace {
21
Chris Dalton4e998532020-02-10 11:06:42 -070022class DrawAtlasPathShader : public GrGeometryProcessor {
23public:
Brian Salomonbab2d112021-08-11 09:59:56 -040024 DrawAtlasPathShader(bool usesLocalCoords,
25 const GrAtlasInstancedHelper* atlasHelper,
Chris Daltonb1fd64e2021-07-08 15:38:51 -060026 const GrShaderCaps& shaderCaps)
Chris Dalton4e998532020-02-10 11:06:42 -070027 : GrGeometryProcessor(kDrawAtlasPathShader_ClassID)
Chris Daltonb1fd64e2021-07-08 15:38:51 -060028 , fUsesLocalCoords(usesLocalCoords)
29 , fAtlasHelper(atlasHelper)
Brian Salomonbab2d112021-08-11 09:59:56 -040030 , fAtlasAccess(GrSamplerState::Filter::kNearest,
31 fAtlasHelper->proxy()->backendFormat(),
Chris Daltonb1fd64e2021-07-08 15:38:51 -060032 fAtlasHelper->atlasSwizzle()) {
Chris Daltona05ccc32021-06-29 19:42:13 -060033 if (!shaderCaps.vertexIDSupport()) {
Brian Salomonbab2d112021-08-11 09:59:56 -040034 constexpr static Attribute kUnitCoordAttrib(
35 "unitCoord", kFloat2_GrVertexAttribType, kFloat2_GrSLType);
Chris Daltona05ccc32021-06-29 19:42:13 -060036 this->setVertexAttributes(&kUnitCoordAttrib, 1);
37 }
Chris Daltonee40d5a2021-07-07 16:34:36 -060038 fAttribs.emplace_back("fillBounds", kFloat4_GrVertexAttribType, kFloat4_GrSLType);
39 if (fUsesLocalCoords) {
40 fAttribs.emplace_back("affineMatrix", kFloat4_GrVertexAttribType, kFloat4_GrSLType);
41 fAttribs.emplace_back("translate", kFloat2_GrVertexAttribType, kFloat2_GrSLType);
42 }
43 SkASSERT(fAttribs.count() == this->colorAttribIdx());
44 fAttribs.emplace_back("color", kFloat4_GrVertexAttribType, kHalf4_GrSLType);
Chris Daltonb1fd64e2021-07-08 15:38:51 -060045 fAtlasHelper->appendInstanceAttribs(&fAttribs);
46 SkASSERT(fAttribs.count() <= kMaxInstanceAttribs);
Chris Daltonee40d5a2021-07-07 16:34:36 -060047 this->setInstanceAttributes(fAttribs.data(), fAttribs.count());
Chris Dalton4e998532020-02-10 11:06:42 -070048 this->setTextureSamplerCnt(1);
49 }
50
51private:
Brian Salomonbab2d112021-08-11 09:59:56 -040052 class Impl;
53
Chris Daltonee40d5a2021-07-07 16:34:36 -060054 int colorAttribIdx() const { return fUsesLocalCoords ? 3 : 1; }
Chris Dalton4e998532020-02-10 11:06:42 -070055 const char* name() const override { return "DrawAtlasPathShader"; }
Brian Salomon13b28732021-08-06 15:33:58 -040056 void addToKey(const GrShaderCaps&, GrProcessorKeyBuilder* b) const override {
Chris Daltonb1fd64e2021-07-08 15:38:51 -060057 b->addBits(1, fUsesLocalCoords, "localCoords");
58 fAtlasHelper->getKeyBits(b);
Chris Dalton4e998532020-02-10 11:06:42 -070059 }
60 const TextureSampler& onTextureSampler(int) const override { return fAtlasAccess; }
Brian Salomonf95940b2021-08-09 15:56:24 -040061 std::unique_ptr<ProgramImpl> makeProgramImpl(const GrShaderCaps&) const override;
Chris Dalton4e998532020-02-10 11:06:42 -070062
Chris Dalton4e998532020-02-10 11:06:42 -070063 const bool fUsesLocalCoords;
Chris Daltonb1fd64e2021-07-08 15:38:51 -060064 const GrAtlasInstancedHelper* const fAtlasHelper;
65 TextureSampler fAtlasAccess;
66 constexpr static int kMaxInstanceAttribs = 6;
67 SkSTArray<kMaxInstanceAttribs, GrGeometryProcessor::Attribute> fAttribs;
Chris Dalton4e998532020-02-10 11:06:42 -070068};
69
Brian Salomonf95940b2021-08-09 15:56:24 -040070class DrawAtlasPathShader::Impl : public ProgramImpl {
Brian Salomonbab2d112021-08-11 09:59:56 -040071public:
72 void setData(const GrGLSLProgramDataManager& pdman,
73 const GrShaderCaps&,
74 const GrGeometryProcessor& geomProc) override {
75 auto* atlasHelper = geomProc.cast<DrawAtlasPathShader>().fAtlasHelper;
76 atlasHelper->setUniformData(pdman, fAtlasAdjustUniform);
77 }
78
79private:
Chris Dalton4e998532020-02-10 11:06:42 -070080 void onEmitCode(EmitArgs& args, GrGPArgs* gpArgs) override {
Robert Phillips787fd9d2021-03-22 14:48:09 -040081 const auto& shader = args.fGeomProc.cast<DrawAtlasPathShader>();
Chris Dalton4e998532020-02-10 11:06:42 -070082 args.fVaryingHandler->emitAttributes(shader);
83
Chris Daltona05ccc32021-06-29 19:42:13 -060084 if (args.fShaderCaps->vertexIDSupport()) {
Chris Daltonee40d5a2021-07-07 16:34:36 -060085 // If we don't have sk_VertexID support then "unitCoord" already came in as a vertex
Chris Daltona05ccc32021-06-29 19:42:13 -060086 // attrib.
87 args.fVertBuilder->codeAppendf(R"(
Chris Daltonee40d5a2021-07-07 16:34:36 -060088 float2 unitCoord = float2(sk_VertexID & 1, sk_VertexID >> 1);)");
Chris Daltona05ccc32021-06-29 19:42:13 -060089 }
90
Chris Dalton4e998532020-02-10 11:06:42 -070091 args.fVertBuilder->codeAppendf(R"(
Chris Daltonee40d5a2021-07-07 16:34:36 -060092 float2 devCoord = mix(fillBounds.xy, fillBounds.zw, unitCoord);)");
93 gpArgs->fPositionVar.set(kFloat2_GrSLType, "devCoord");
Chris Dalton4e998532020-02-10 11:06:42 -070094
Chris Dalton4e998532020-02-10 11:06:42 -070095 if (shader.fUsesLocalCoords) {
96 args.fVertBuilder->codeAppendf(R"(
Chris Daltonee40d5a2021-07-07 16:34:36 -060097 float2x2 M = float2x2(affineMatrix);
98 float2 localCoord = inverse(M) * (devCoord - translate);)");
99 gpArgs->fLocalCoordVar.set(kFloat2_GrSLType, "localCoord");
Chris Dalton4e998532020-02-10 11:06:42 -0700100 }
Chris Dalton4e998532020-02-10 11:06:42 -0700101
Chris Daltonb1fd64e2021-07-08 15:38:51 -0600102 args.fFragBuilder->codeAppendf("half4 %s = half4(1);", args.fOutputCoverage);
103 shader.fAtlasHelper->injectShaderCode(args, gpArgs->fPositionVar, &fAtlasAdjustUniform);
Chris Daltonee40d5a2021-07-07 16:34:36 -0600104
105 args.fFragBuilder->codeAppendf("half4 %s;", args.fOutputColor);
106 args.fVaryingHandler->addPassThroughAttribute(
107 shader.fAttribs[shader.colorAttribIdx()], args.fOutputColor,
108 GrGLSLVaryingHandler::Interpolation::kCanBeFlat);
Chris Dalton4e998532020-02-10 11:06:42 -0700109 }
110
Chris Dalton4e998532020-02-10 11:06:42 -0700111 GrGLSLUniformHandler::UniformHandle fAtlasAdjustUniform;
112};
113
Brian Salomonf95940b2021-08-09 15:56:24 -0400114std::unique_ptr<GrGeometryProcessor::ProgramImpl> DrawAtlasPathShader::makeProgramImpl(
115 const GrShaderCaps&) const {
116 return std::make_unique<Impl>();
Chris Dalton4e998532020-02-10 11:06:42 -0700117}
118
119} // namespace
120
121GrProcessorSet::Analysis GrDrawAtlasPathOp::finalize(const GrCaps& caps, const GrAppliedClip* clip,
Chris Dalton4e998532020-02-10 11:06:42 -0700122 GrClampType clampType) {
123 const GrProcessorSet::Analysis& analysis = fProcessors.finalize(
Chris Daltonee40d5a2021-07-07 16:34:36 -0600124 fHeadInstance->fColor, GrProcessorAnalysisCoverage::kSingleChannel, clip,
125 &GrUserStencilSettings::kUnused, caps, clampType, &fHeadInstance->fColor);
Chris Dalton4e998532020-02-10 11:06:42 -0700126 fUsesLocalCoords = analysis.usesLocalCoords();
127 return analysis;
128}
129
Chris Daltonb1fd64e2021-07-08 15:38:51 -0600130GrOp::CombineResult GrDrawAtlasPathOp::onCombineIfPossible(GrOp* op, SkArenaAlloc*, const GrCaps&) {
Chris Dalton4e998532020-02-10 11:06:42 -0700131 auto* that = op->cast<GrDrawAtlasPathOp>();
Chris Dalton4e998532020-02-10 11:06:42 -0700132
Chris Daltonb1fd64e2021-07-08 15:38:51 -0600133 if (!fAtlasHelper.isCompatible(that->fAtlasHelper) ||
134 fProcessors != that->fProcessors) {
Chris Dalton4e998532020-02-10 11:06:42 -0700135 return CombineResult::kCannotCombine;
136 }
137
138 SkASSERT(fUsesLocalCoords == that->fUsesLocalCoords);
Chris Daltonee40d5a2021-07-07 16:34:36 -0600139 *fTailInstance = that->fHeadInstance;
140 fTailInstance = that->fTailInstance;
Chris Dalton4e998532020-02-10 11:06:42 -0700141 fInstanceCount += that->fInstanceCount;
142 return CombineResult::kMerged;
143}
144
Chris Daltonee40d5a2021-07-07 16:34:36 -0600145void GrDrawAtlasPathOp::prepareProgram(const GrCaps& caps, SkArenaAlloc* arena,
Chris Daltoncc29a392021-07-12 15:16:29 -0600146 const GrSurfaceProxyView& writeView, bool usesMSAASurface,
Chris Daltonee40d5a2021-07-07 16:34:36 -0600147 GrAppliedClip&& appliedClip,
148 const GrDstProxyView& dstProxyView,
149 GrXferBarrierFlags renderPassXferBarriers,
150 GrLoadOp colorLoadOp) {
151 SkASSERT(!fProgram);
Chris Dalton4e998532020-02-10 11:06:42 -0700152 GrPipeline::InitArgs initArgs;
Chris Daltoncc29a392021-07-12 15:16:29 -0600153 if (usesMSAASurface) {
Chris Dalton4e998532020-02-10 11:06:42 -0700154 initArgs.fInputFlags |= GrPipeline::InputFlags::kHWAntialias;
155 }
Chris Daltonee40d5a2021-07-07 16:34:36 -0600156 initArgs.fCaps = &caps;
157 initArgs.fDstProxyView = dstProxyView;
158 initArgs.fWriteSwizzle = writeView.swizzle();
Chris Daltonbaae2dd2021-06-25 14:52:49 -0600159 auto pipeline = arena->make<GrPipeline>(initArgs, std::move(fProcessors),
Chris Daltonee40d5a2021-07-07 16:34:36 -0600160 std::move(appliedClip));
Chris Daltonb1fd64e2021-07-08 15:38:51 -0600161 auto shader = arena->make<DrawAtlasPathShader>(fUsesLocalCoords, &fAtlasHelper,
162 *caps.shaderCaps());
Chris Daltonee40d5a2021-07-07 16:34:36 -0600163 fProgram = arena->make<GrProgramInfo>(writeView, pipeline, &GrUserStencilSettings::kUnused,
164 shader, GrPrimitiveType::kTriangleStrip, 0,
165 renderPassXferBarriers, colorLoadOp);
166}
Chris Dalton012f8492020-03-05 11:49:15 -0700167
Chris Daltonee40d5a2021-07-07 16:34:36 -0600168void GrDrawAtlasPathOp::onPrePrepare(GrRecordingContext* rContext,
169 const GrSurfaceProxyView& writeView,
170 GrAppliedClip* appliedClip, const GrDstProxyView& dstProxyView,
171 GrXferBarrierFlags renderPassXferBarriers,
172 GrLoadOp colorLoadOp) {
173 this->prepareProgram(*rContext->priv().caps(), rContext->priv().recordTimeAllocator(),
Chris Daltoncc29a392021-07-12 15:16:29 -0600174 writeView, writeView.asRenderTargetProxy()->numSamples() > 1,
175 std::move(*appliedClip), dstProxyView, renderPassXferBarriers,
Chris Daltonee40d5a2021-07-07 16:34:36 -0600176 colorLoadOp);
177 SkASSERT(fProgram);
178 rContext->priv().recordProgramInfo(fProgram);
179}
180
181GR_DECLARE_STATIC_UNIQUE_KEY(gUnitQuadBufferKey);
182
183void GrDrawAtlasPathOp::onPrepare(GrOpFlushState* flushState) {
184 if (!fProgram) {
185 this->prepareProgram(flushState->caps(), flushState->allocator(), flushState->writeView(),
Chris Daltoncc29a392021-07-12 15:16:29 -0600186 flushState->usesMSAASurface(), flushState->detachAppliedClip(),
187 flushState->dstProxyView(), flushState->renderPassBarriers(),
188 flushState->colorLoadOp());
Chris Daltonee40d5a2021-07-07 16:34:36 -0600189 SkASSERT(fProgram);
190 }
191
Chris Daltoncc29a392021-07-12 15:16:29 -0600192 // FIXME(skbug.com/12201): Our draw's MSAA state should match the render target, but DDL doesn't
193 // yet communicate DMSAA state to onPrePrepare.
194 SkASSERT(fProgram->pipeline().isHWAntialiasState() == flushState->usesMSAASurface());
195
Chris Daltonee40d5a2021-07-07 16:34:36 -0600196 if (GrVertexWriter instanceWriter = flushState->makeVertexSpace(
197 fProgram->geomProc().instanceStride(), fInstanceCount, &fInstanceBuffer,
198 &fBaseInstance)) {
Chris Daltonb1fd64e2021-07-08 15:38:51 -0600199 for (const Instance* i = fHeadInstance; i; i = i->fNext) {
Chris Daltonbaae2dd2021-06-25 14:52:49 -0600200 instanceWriter.write(
Chris Daltonb1fd64e2021-07-08 15:38:51 -0600201 SkRect::Make(i->fFillBounds),
Chris Daltonee40d5a2021-07-07 16:34:36 -0600202 GrVertexWriter::If(fUsesLocalCoords,
Chris Daltonb1fd64e2021-07-08 15:38:51 -0600203 i->fLocalToDeviceIfUsingLocalCoords),
204 i->fColor);
205 fAtlasHelper.writeInstanceData(&instanceWriter, &i->fAtlasInstance);
Chris Daltonbaae2dd2021-06-25 14:52:49 -0600206 }
207 }
Chris Daltona05ccc32021-06-29 19:42:13 -0600208
Chris Daltonee40d5a2021-07-07 16:34:36 -0600209 if (!flushState->caps().shaderCaps()->vertexIDSupport()) {
Chris Daltona05ccc32021-06-29 19:42:13 -0600210 constexpr static SkPoint kUnitQuad[4] = {{0,0}, {0,1}, {1,0}, {1,1}};
211
212 GR_DEFINE_STATIC_UNIQUE_KEY(gUnitQuadBufferKey);
213
Chris Daltonee40d5a2021-07-07 16:34:36 -0600214 fVertexBufferIfNoIDSupport = flushState->resourceProvider()->findOrMakeStaticBuffer(
Chris Daltona05ccc32021-06-29 19:42:13 -0600215 GrGpuBufferType::kVertex, sizeof(kUnitQuad), kUnitQuad, gUnitQuadBufferKey);
216 }
Chris Daltonbaae2dd2021-06-25 14:52:49 -0600217}
Chris Dalton4e998532020-02-10 11:06:42 -0700218
Chris Daltonee40d5a2021-07-07 16:34:36 -0600219void GrDrawAtlasPathOp::onExecute(GrOpFlushState* flushState, const SkRect& chainBounds) {
Chris Daltonee40d5a2021-07-07 16:34:36 -0600220 flushState->bindPipelineAndScissorClip(*fProgram, this->bounds());
Chris Daltonb1fd64e2021-07-08 15:38:51 -0600221 flushState->bindTextures(fProgram->geomProc(), *fAtlasHelper.proxy(), fProgram->pipeline());
Chris Daltonee40d5a2021-07-07 16:34:36 -0600222 flushState->bindBuffers(nullptr, std::move(fInstanceBuffer), fVertexBufferIfNoIDSupport);
223 flushState->drawInstanced(fInstanceCount, fBaseInstance, 4, 0);
Chris Dalton4e998532020-02-10 11:06:42 -0700224}