blob: eb5b74b3d68e827a734da59b7b7869327fcc3506 [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
Brian Salomon48959462021-08-11 13:01:06 -040010#include "src/gpu/GrGeometryProcessor.h"
Chris Dalton4e998532020-02-10 11:06:42 -070011#include "src/gpu/GrOpFlushState.h"
12#include "src/gpu/GrOpsRenderPass.h"
13#include "src/gpu/GrProgramInfo.h"
Robert Phillips1a82a4e2021-07-01 10:27:44 -040014#include "src/gpu/GrResourceProvider.h"
Chris Daltonbaae2dd2021-06-25 14:52:49 -060015#include "src/gpu/GrVertexWriter.h"
Chris Dalton4e998532020-02-10 11:06:42 -070016#include "src/gpu/glsl/GrGLSLFragmentShaderBuilder.h"
Chris Dalton4e998532020-02-10 11:06:42 -070017#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(
Brian Salomon48959462021-08-11 13:01:06 -0400107 shader.fAttribs[shader.colorAttribIdx()].asShaderVar(),
108 args.fOutputColor,
Chris Daltonee40d5a2021-07-07 16:34:36 -0600109 GrGLSLVaryingHandler::Interpolation::kCanBeFlat);
Chris Dalton4e998532020-02-10 11:06:42 -0700110 }
111
Chris Dalton4e998532020-02-10 11:06:42 -0700112 GrGLSLUniformHandler::UniformHandle fAtlasAdjustUniform;
113};
114
Brian Salomonf95940b2021-08-09 15:56:24 -0400115std::unique_ptr<GrGeometryProcessor::ProgramImpl> DrawAtlasPathShader::makeProgramImpl(
116 const GrShaderCaps&) const {
117 return std::make_unique<Impl>();
Chris Dalton4e998532020-02-10 11:06:42 -0700118}
119
120} // namespace
121
122GrProcessorSet::Analysis GrDrawAtlasPathOp::finalize(const GrCaps& caps, const GrAppliedClip* clip,
Chris Dalton4e998532020-02-10 11:06:42 -0700123 GrClampType clampType) {
124 const GrProcessorSet::Analysis& analysis = fProcessors.finalize(
Chris Daltonee40d5a2021-07-07 16:34:36 -0600125 fHeadInstance->fColor, GrProcessorAnalysisCoverage::kSingleChannel, clip,
126 &GrUserStencilSettings::kUnused, caps, clampType, &fHeadInstance->fColor);
Chris Dalton4e998532020-02-10 11:06:42 -0700127 fUsesLocalCoords = analysis.usesLocalCoords();
128 return analysis;
129}
130
Chris Daltonb1fd64e2021-07-08 15:38:51 -0600131GrOp::CombineResult GrDrawAtlasPathOp::onCombineIfPossible(GrOp* op, SkArenaAlloc*, const GrCaps&) {
Chris Dalton4e998532020-02-10 11:06:42 -0700132 auto* that = op->cast<GrDrawAtlasPathOp>();
Chris Dalton4e998532020-02-10 11:06:42 -0700133
Chris Daltonb1fd64e2021-07-08 15:38:51 -0600134 if (!fAtlasHelper.isCompatible(that->fAtlasHelper) ||
135 fProcessors != that->fProcessors) {
Chris Dalton4e998532020-02-10 11:06:42 -0700136 return CombineResult::kCannotCombine;
137 }
138
139 SkASSERT(fUsesLocalCoords == that->fUsesLocalCoords);
Chris Daltonee40d5a2021-07-07 16:34:36 -0600140 *fTailInstance = that->fHeadInstance;
141 fTailInstance = that->fTailInstance;
Chris Dalton4e998532020-02-10 11:06:42 -0700142 fInstanceCount += that->fInstanceCount;
143 return CombineResult::kMerged;
144}
145
Chris Daltonee40d5a2021-07-07 16:34:36 -0600146void GrDrawAtlasPathOp::prepareProgram(const GrCaps& caps, SkArenaAlloc* arena,
Chris Daltoncc29a392021-07-12 15:16:29 -0600147 const GrSurfaceProxyView& writeView, bool usesMSAASurface,
Chris Daltonee40d5a2021-07-07 16:34:36 -0600148 GrAppliedClip&& appliedClip,
149 const GrDstProxyView& dstProxyView,
150 GrXferBarrierFlags renderPassXferBarriers,
151 GrLoadOp colorLoadOp) {
152 SkASSERT(!fProgram);
Chris Dalton4e998532020-02-10 11:06:42 -0700153 GrPipeline::InitArgs initArgs;
Chris Daltoncc29a392021-07-12 15:16:29 -0600154 if (usesMSAASurface) {
Chris Dalton4e998532020-02-10 11:06:42 -0700155 initArgs.fInputFlags |= GrPipeline::InputFlags::kHWAntialias;
156 }
Chris Daltonee40d5a2021-07-07 16:34:36 -0600157 initArgs.fCaps = &caps;
158 initArgs.fDstProxyView = dstProxyView;
159 initArgs.fWriteSwizzle = writeView.swizzle();
Chris Daltonbaae2dd2021-06-25 14:52:49 -0600160 auto pipeline = arena->make<GrPipeline>(initArgs, std::move(fProcessors),
Chris Daltonee40d5a2021-07-07 16:34:36 -0600161 std::move(appliedClip));
Chris Daltonb1fd64e2021-07-08 15:38:51 -0600162 auto shader = arena->make<DrawAtlasPathShader>(fUsesLocalCoords, &fAtlasHelper,
163 *caps.shaderCaps());
Chris Daltonee40d5a2021-07-07 16:34:36 -0600164 fProgram = arena->make<GrProgramInfo>(writeView, pipeline, &GrUserStencilSettings::kUnused,
165 shader, GrPrimitiveType::kTriangleStrip, 0,
166 renderPassXferBarriers, colorLoadOp);
167}
Chris Dalton012f8492020-03-05 11:49:15 -0700168
Chris Daltonee40d5a2021-07-07 16:34:36 -0600169void GrDrawAtlasPathOp::onPrePrepare(GrRecordingContext* rContext,
170 const GrSurfaceProxyView& writeView,
171 GrAppliedClip* appliedClip, const GrDstProxyView& dstProxyView,
172 GrXferBarrierFlags renderPassXferBarriers,
173 GrLoadOp colorLoadOp) {
174 this->prepareProgram(*rContext->priv().caps(), rContext->priv().recordTimeAllocator(),
Chris Daltoncc29a392021-07-12 15:16:29 -0600175 writeView, writeView.asRenderTargetProxy()->numSamples() > 1,
176 std::move(*appliedClip), dstProxyView, renderPassXferBarriers,
Chris Daltonee40d5a2021-07-07 16:34:36 -0600177 colorLoadOp);
178 SkASSERT(fProgram);
179 rContext->priv().recordProgramInfo(fProgram);
180}
181
182GR_DECLARE_STATIC_UNIQUE_KEY(gUnitQuadBufferKey);
183
184void GrDrawAtlasPathOp::onPrepare(GrOpFlushState* flushState) {
185 if (!fProgram) {
186 this->prepareProgram(flushState->caps(), flushState->allocator(), flushState->writeView(),
Chris Daltoncc29a392021-07-12 15:16:29 -0600187 flushState->usesMSAASurface(), flushState->detachAppliedClip(),
188 flushState->dstProxyView(), flushState->renderPassBarriers(),
189 flushState->colorLoadOp());
Chris Daltonee40d5a2021-07-07 16:34:36 -0600190 SkASSERT(fProgram);
191 }
192
Chris Daltoncc29a392021-07-12 15:16:29 -0600193 // FIXME(skbug.com/12201): Our draw's MSAA state should match the render target, but DDL doesn't
194 // yet communicate DMSAA state to onPrePrepare.
195 SkASSERT(fProgram->pipeline().isHWAntialiasState() == flushState->usesMSAASurface());
196
Chris Daltonee40d5a2021-07-07 16:34:36 -0600197 if (GrVertexWriter instanceWriter = flushState->makeVertexSpace(
198 fProgram->geomProc().instanceStride(), fInstanceCount, &fInstanceBuffer,
199 &fBaseInstance)) {
Chris Daltonb1fd64e2021-07-08 15:38:51 -0600200 for (const Instance* i = fHeadInstance; i; i = i->fNext) {
Chris Daltonbaae2dd2021-06-25 14:52:49 -0600201 instanceWriter.write(
Chris Daltonb1fd64e2021-07-08 15:38:51 -0600202 SkRect::Make(i->fFillBounds),
Chris Daltonee40d5a2021-07-07 16:34:36 -0600203 GrVertexWriter::If(fUsesLocalCoords,
Chris Daltonb1fd64e2021-07-08 15:38:51 -0600204 i->fLocalToDeviceIfUsingLocalCoords),
205 i->fColor);
206 fAtlasHelper.writeInstanceData(&instanceWriter, &i->fAtlasInstance);
Chris Daltonbaae2dd2021-06-25 14:52:49 -0600207 }
208 }
Chris Daltona05ccc32021-06-29 19:42:13 -0600209
Chris Daltonee40d5a2021-07-07 16:34:36 -0600210 if (!flushState->caps().shaderCaps()->vertexIDSupport()) {
Chris Daltona05ccc32021-06-29 19:42:13 -0600211 constexpr static SkPoint kUnitQuad[4] = {{0,0}, {0,1}, {1,0}, {1,1}};
212
213 GR_DEFINE_STATIC_UNIQUE_KEY(gUnitQuadBufferKey);
214
Chris Daltonee40d5a2021-07-07 16:34:36 -0600215 fVertexBufferIfNoIDSupport = flushState->resourceProvider()->findOrMakeStaticBuffer(
Chris Daltona05ccc32021-06-29 19:42:13 -0600216 GrGpuBufferType::kVertex, sizeof(kUnitQuad), kUnitQuad, gUnitQuadBufferKey);
217 }
Chris Daltonbaae2dd2021-06-25 14:52:49 -0600218}
Chris Dalton4e998532020-02-10 11:06:42 -0700219
Chris Daltonee40d5a2021-07-07 16:34:36 -0600220void GrDrawAtlasPathOp::onExecute(GrOpFlushState* flushState, const SkRect& chainBounds) {
Chris Daltonee40d5a2021-07-07 16:34:36 -0600221 flushState->bindPipelineAndScissorClip(*fProgram, this->bounds());
Chris Daltonb1fd64e2021-07-08 15:38:51 -0600222 flushState->bindTextures(fProgram->geomProc(), *fAtlasHelper.proxy(), fProgram->pipeline());
Chris Daltonee40d5a2021-07-07 16:34:36 -0600223 flushState->bindBuffers(nullptr, std::move(fInstanceBuffer), fVertexBufferIfNoIDSupport);
224 flushState->drawInstanced(fInstanceCount, fBaseInstance, 4, 0);
Chris Dalton4e998532020-02-10 11:06:42 -0700225}