blob: b7a7b155adaea97b506425d68abf2e04c7baa92d [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
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 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:
Chris Daltonbaae2dd2021-06-25 14:52:49 -060024 DrawAtlasPathShader(const GrTextureProxy* atlasProxy, GrSwizzle swizzle, bool isInverseFill,
Chris Daltona05ccc32021-06-29 19:42:13 -060025 bool usesLocalCoords, const GrShaderCaps& shaderCaps)
Chris Dalton4e998532020-02-10 11:06:42 -070026 : GrGeometryProcessor(kDrawAtlasPathShader_ClassID)
27 , fAtlasAccess(GrSamplerState::Filter::kNearest, atlasProxy->backendFormat(), swizzle)
28 , fAtlasDimensions(atlasProxy->backingStoreDimensions())
Chris Daltonbaae2dd2021-06-25 14:52:49 -060029 , fIsInverseFill(isInverseFill)
Chris Dalton4e998532020-02-10 11:06:42 -070030 , fUsesLocalCoords(usesLocalCoords) {
Chris Daltonbaae2dd2021-06-25 14:52:49 -060031 fAttribs.emplace_back("dev_xywh", kInt4_GrVertexAttribType, kInt4_GrSLType);
32 fAttribs.emplace_back("atlas_xy", kInt2_GrVertexAttribType, kInt2_GrSLType);
33 fAttribs.emplace_back("color", kFloat4_GrVertexAttribType, kHalf4_GrSLType);
34 if (fIsInverseFill) {
35 fAttribs.emplace_back("drawbounds", kFloat4_GrVertexAttribType, kFloat4_GrSLType);
Chris Dalton4e998532020-02-10 11:06:42 -070036 }
Chris Daltonbaae2dd2021-06-25 14:52:49 -060037 if (fUsesLocalCoords) {
38 fAttribs.emplace_back("viewmatrix_scaleskew", kFloat4_GrVertexAttribType,
39 kFloat4_GrSLType);
40 fAttribs.emplace_back("viewmatrix_trans", kFloat2_GrVertexAttribType, kFloat2_GrSLType);
41 }
42 this->setInstanceAttributes(fAttribs.data(), fAttribs.count());
Chris Daltona05ccc32021-06-29 19:42:13 -060043 if (!shaderCaps.vertexIDSupport()) {
44 constexpr static Attribute kUnitCoordAttrib("unit_coord", kFloat2_GrVertexAttribType,
45 kFloat2_GrSLType);
46 this->setVertexAttributes(&kUnitCoordAttrib, 1);
47 }
Chris Dalton4e998532020-02-10 11:06:42 -070048 this->setTextureSamplerCnt(1);
49 }
50
51private:
52 const char* name() const override { return "DrawAtlasPathShader"; }
53 void getGLSLProcessorKey(const GrShaderCaps&, GrProcessorKeyBuilder* b) const override {
54 b->add32(fUsesLocalCoords);
55 }
56 const TextureSampler& onTextureSampler(int) const override { return fAtlasAccess; }
Robert Phillipsf10535f2021-03-23 09:30:45 -040057 GrGLSLGeometryProcessor* createGLSLInstance(const GrShaderCaps&) const override;
Chris Dalton4e998532020-02-10 11:06:42 -070058
59 const TextureSampler fAtlasAccess;
60 const SkISize fAtlasDimensions;
Chris Daltonbaae2dd2021-06-25 14:52:49 -060061 const bool fIsInverseFill;
Chris Dalton4e998532020-02-10 11:06:42 -070062 const bool fUsesLocalCoords;
Chris Daltonbaae2dd2021-06-25 14:52:49 -060063 SkSTArray<6, GrGeometryProcessor::Attribute> fAttribs;
Chris Dalton4e998532020-02-10 11:06:42 -070064
65 class Impl;
66};
67
68class DrawAtlasPathShader::Impl : public GrGLSLGeometryProcessor {
69 void onEmitCode(EmitArgs& args, GrGPArgs* gpArgs) override {
Robert Phillips787fd9d2021-03-22 14:48:09 -040070 const auto& shader = args.fGeomProc.cast<DrawAtlasPathShader>();
Chris Dalton4e998532020-02-10 11:06:42 -070071 args.fVaryingHandler->emitAttributes(shader);
72
73 GrGLSLVarying atlasCoord(kFloat2_GrSLType);
74 args.fVaryingHandler->addVarying("atlascoord", &atlasCoord);
75
76 GrGLSLVarying color(kHalf4_GrSLType);
John Stiles4d7ac492021-03-09 20:16:43 -050077 args.fFragBuilder->codeAppendf("half4 %s;", args.fOutputColor);
Chris Dalton4e998532020-02-10 11:06:42 -070078 args.fVaryingHandler->addPassThroughAttribute(
Chris Daltonbaae2dd2021-06-25 14:52:49 -060079 shader.fAttribs[2], args.fOutputColor,
Chris Dalton4e998532020-02-10 11:06:42 -070080 GrGLSLVaryingHandler::Interpolation::kCanBeFlat);
81
82 const char* atlasAdjust;
83 fAtlasAdjustUniform = args.fUniformHandler->addUniform(
Ethan Nicholas16464c32020-04-06 13:53:05 -040084 nullptr, kVertex_GrShaderFlag, kFloat2_GrSLType, "atlas_adjust", &atlasAdjust);
Chris Dalton4e998532020-02-10 11:06:42 -070085
Chris Daltona05ccc32021-06-29 19:42:13 -060086 if (args.fShaderCaps->vertexIDSupport()) {
87 // If we don't have sk_VertexID support then "unit_coord" already came in as a vertex
88 // attrib.
89 args.fVertBuilder->codeAppendf(R"(
90 float2 unit_coord = float2(sk_VertexID & 1, sk_VertexID >> 1);)");
91 }
92
Chris Dalton4e998532020-02-10 11:06:42 -070093 args.fVertBuilder->codeAppendf(R"(
Chris Daltonbaae2dd2021-06-25 14:52:49 -060094 float2 devtopleft = float2(dev_xywh.xy);)");
95
96 if (shader.fIsInverseFill) {
97 args.fVertBuilder->codeAppendf(R"(
Chris Daltona05ccc32021-06-29 19:42:13 -060098 float2 devcoord = mix(drawbounds.xy, drawbounds.zw, unit_coord);)");
Chris Daltonbaae2dd2021-06-25 14:52:49 -060099 } else {
100 args.fVertBuilder->codeAppendf(R"(
Chris Daltona05ccc32021-06-29 19:42:13 -0600101 float2 devcoord = abs(float2(dev_xywh.zw)) * unit_coord + devtopleft;)");
Chris Daltonbaae2dd2021-06-25 14:52:49 -0600102 }
103
104 args.fVertBuilder->codeAppendf(R"(
105 float2 atlascoord = devcoord - devtopleft;
106 bool transposed = dev_xywh.w < 0; // Negative height means the path is transposed.
107 if (transposed) {
108 atlascoord = atlascoord.yx;
109 }
110 atlascoord += float2(atlas_xy);
111 %s = atlascoord * %s;)",
112 atlasCoord.vsOut(), atlasAdjust);
Chris Dalton4e998532020-02-10 11:06:42 -0700113
114 gpArgs->fPositionVar.set(kFloat2_GrSLType, "devcoord");
115
Chris Dalton4e998532020-02-10 11:06:42 -0700116 if (shader.fUsesLocalCoords) {
117 args.fVertBuilder->codeAppendf(R"(
Chris Daltonbaae2dd2021-06-25 14:52:49 -0600118 float2x2 M = float2x2(viewmatrix_scaleskew);
119 float2 localcoord = inverse(M) * (devcoord - viewmatrix_trans);)");
Michael Ludwig553db622020-06-19 10:47:30 -0400120 gpArgs->fLocalCoordVar.set(kFloat2_GrSLType, "localcoord");
Chris Dalton4e998532020-02-10 11:06:42 -0700121 }
Chris Dalton4e998532020-02-10 11:06:42 -0700122
Chris Daltonbaae2dd2021-06-25 14:52:49 -0600123 if (shader.fIsInverseFill) {
124 GrGLSLVarying atlasBounds(kFloat4_GrSLType);
125 args.fVaryingHandler->addVarying("atlasbounds", &atlasBounds,
126 GrGLSLVaryingHandler::Interpolation::kCanBeFlat);
127 args.fVertBuilder->codeAppendf(R"(
128 int2 atlas_wh = (transposed) ? abs(dev_xywh.wz) : dev_xywh.zw;
129 %s = float4(atlas_xy, atlas_xy + atlas_wh) * %s.xyxy;)", atlasBounds.vsOut(),
130 atlasAdjust);
131
132 args.fFragBuilder->codeAppendf(R"(
133 half coverage = 0;
134 float2 atlascoord = %s;
135 float4 atlasbounds = %s;
136 if (all(greaterThan(atlascoord, atlasbounds.xy)) &&
137 all(lessThan(atlascoord, atlasbounds.zw))) {
138 coverage = )", atlasCoord.fsIn(), atlasBounds.fsIn());
139 args.fFragBuilder->appendTextureLookup(args.fTexSamplers[0], "atlascoord");
140 args.fFragBuilder->codeAppendf(R"(.a;
141 }
142 half4 %s = half4(1 - coverage);)", args.fOutputCoverage);
143 } else {
144 args.fFragBuilder->codeAppendf("half4 %s = ", args.fOutputCoverage);
145 args.fFragBuilder->appendTextureLookup(args.fTexSamplers[0], atlasCoord.fsIn());
146 args.fFragBuilder->codeAppendf(".aaaa;");
147 }
Chris Dalton4e998532020-02-10 11:06:42 -0700148 }
149
Brian Osman609f1592020-07-01 15:14:39 -0400150 void setData(const GrGLSLProgramDataManager& pdman,
Brian Salomon5a328282021-04-14 10:32:25 -0400151 const GrShaderCaps&,
Robert Phillips787fd9d2021-03-22 14:48:09 -0400152 const GrGeometryProcessor& geomProc) override {
153 const SkISize& dimensions = geomProc.cast<DrawAtlasPathShader>().fAtlasDimensions;
Chris Dalton4e998532020-02-10 11:06:42 -0700154 pdman.set2f(fAtlasAdjustUniform, 1.f / dimensions.width(), 1.f / dimensions.height());
Chris Dalton4e998532020-02-10 11:06:42 -0700155 }
156
157 GrGLSLUniformHandler::UniformHandle fAtlasAdjustUniform;
158};
159
Robert Phillipsf10535f2021-03-23 09:30:45 -0400160GrGLSLGeometryProcessor* DrawAtlasPathShader::createGLSLInstance(const GrShaderCaps&) const {
Chris Dalton4e998532020-02-10 11:06:42 -0700161 return new Impl();
162}
163
164} // namespace
165
166GrProcessorSet::Analysis GrDrawAtlasPathOp::finalize(const GrCaps& caps, const GrAppliedClip* clip,
Chris Dalton4e998532020-02-10 11:06:42 -0700167 GrClampType clampType) {
168 const GrProcessorSet::Analysis& analysis = fProcessors.finalize(
Chris Daltonbaae2dd2021-06-25 14:52:49 -0600169 fHeadInstance.fColor, GrProcessorAnalysisCoverage::kSingleChannel, clip,
170 &GrUserStencilSettings::kUnused, caps, clampType, &fHeadInstance.fColor);
Chris Dalton4e998532020-02-10 11:06:42 -0700171 fUsesLocalCoords = analysis.usesLocalCoords();
172 return analysis;
173}
174
175GrOp::CombineResult GrDrawAtlasPathOp::onCombineIfPossible(
Herb Derbye25c3002020-10-27 15:57:27 -0400176 GrOp* op, SkArenaAlloc* alloc, const GrCaps&) {
Chris Dalton4e998532020-02-10 11:06:42 -0700177 auto* that = op->cast<GrDrawAtlasPathOp>();
178 SkASSERT(fAtlasProxy == that->fAtlasProxy);
179 SkASSERT(fEnableHWAA == that->fEnableHWAA);
180
Chris Daltonbaae2dd2021-06-25 14:52:49 -0600181 if (fIsInverseFill != that->fIsInverseFill || fProcessors != that->fProcessors) {
Chris Dalton4e998532020-02-10 11:06:42 -0700182 return CombineResult::kCannotCombine;
183 }
184
185 SkASSERT(fUsesLocalCoords == that->fUsesLocalCoords);
Chris Daltonbaae2dd2021-06-25 14:52:49 -0600186 auto* copy = alloc->make<Instance>(that->fHeadInstance);
187 *fTailInstance = copy;
188 fTailInstance = (!copy->fNext) ? &copy->fNext : that->fTailInstance;
Chris Dalton4e998532020-02-10 11:06:42 -0700189 fInstanceCount += that->fInstanceCount;
190 return CombineResult::kMerged;
191}
192
Chris Daltonbaae2dd2021-06-25 14:52:49 -0600193void GrDrawAtlasPathOp::onPrePrepare(GrRecordingContext*, const GrSurfaceProxyView& writeView,
194 GrAppliedClip*, const GrDstProxyView&,
Greg Daniel42dbca52020-11-20 10:22:43 -0500195 GrXferBarrierFlags renderPassXferBarriers,
Chris Daltonbaae2dd2021-06-25 14:52:49 -0600196 GrLoadOp colorLoadOp) {
197 SK_ABORT("DDL support not implemented for GrDrawAtlasPathOp.");
Chris Dalton4e998532020-02-10 11:06:42 -0700198}
199
Chris Daltona05ccc32021-06-29 19:42:13 -0600200GR_DECLARE_STATIC_UNIQUE_KEY(gUnitQuadBufferKey);
201
Chris Daltonbaae2dd2021-06-25 14:52:49 -0600202void GrDrawAtlasPathOp::onPrepare(GrOpFlushState* state) {
203 SkArenaAlloc* arena = state->allocator();
Chris Dalton4e998532020-02-10 11:06:42 -0700204
205 GrPipeline::InitArgs initArgs;
206 if (fEnableHWAA) {
207 initArgs.fInputFlags |= GrPipeline::InputFlags::kHWAntialias;
208 }
209 initArgs.fCaps = &state->caps();
210 initArgs.fDstProxyView = state->drawOpArgs().dstProxyView();
Adlai Hollere2296f72020-11-19 13:41:26 -0500211 initArgs.fWriteSwizzle = state->drawOpArgs().writeView().swizzle();
Chris Daltonbaae2dd2021-06-25 14:52:49 -0600212 auto pipeline = arena->make<GrPipeline>(initArgs, std::move(fProcessors),
213 state->detachAppliedClip());
Chris Dalton4e998532020-02-10 11:06:42 -0700214 GrSwizzle swizzle = state->caps().getReadSwizzle(fAtlasProxy->backendFormat(),
215 GrColorType::kAlpha_8);
Chris Daltonbaae2dd2021-06-25 14:52:49 -0600216 auto shader = arena->make<DrawAtlasPathShader>(fAtlasProxy.get(), swizzle, fIsInverseFill,
Chris Daltona05ccc32021-06-29 19:42:13 -0600217 fUsesLocalCoords, *state->caps().shaderCaps());
Chris Daltonbaae2dd2021-06-25 14:52:49 -0600218 fProgram = arena->make<GrProgramInfo>(state->writeView(), pipeline,
219 &GrUserStencilSettings::kUnused, shader,
220 GrPrimitiveType::kTriangleStrip, 0,
221 state->renderPassBarriers(), state->colorLoadOp());
Chris Dalton012f8492020-03-05 11:49:15 -0700222
Chris Daltonbaae2dd2021-06-25 14:52:49 -0600223 if (GrVertexWriter instanceWriter = state->makeVertexSpace(
224 shader->instanceStride(), fInstanceCount, &fInstanceBuffer, &fBaseInstance)) {
225 for (const Instance* instance = &fHeadInstance; instance; instance = instance->fNext) {
226 instanceWriter.write(
227 instance->fDevXYWH,
228 instance->fAtlasXY,
229 instance->fColor,
230 GrVertexWriter::If(fIsInverseFill, instance->fDrawBoundsIfInverseFilled),
231 GrVertexWriter::If(fUsesLocalCoords, instance->fViewMatrixIfUsingLocalCoords));
232 }
233 }
Chris Daltona05ccc32021-06-29 19:42:13 -0600234
235 if (!state->caps().shaderCaps()->vertexIDSupport()) {
236 constexpr static SkPoint kUnitQuad[4] = {{0,0}, {0,1}, {1,0}, {1,1}};
237
238 GR_DEFINE_STATIC_UNIQUE_KEY(gUnitQuadBufferKey);
239
240 fVertexBufferIfNoIDSupport = state->resourceProvider()->findOrMakeStaticBuffer(
241 GrGpuBufferType::kVertex, sizeof(kUnitQuad), kUnitQuad, gUnitQuadBufferKey);
242 }
Chris Daltonbaae2dd2021-06-25 14:52:49 -0600243}
Chris Dalton4e998532020-02-10 11:06:42 -0700244
Chris Daltonbaae2dd2021-06-25 14:52:49 -0600245void GrDrawAtlasPathOp::onExecute(GrOpFlushState* state, const SkRect& chainBounds) {
246 SkASSERT(fAtlasProxy->isInstantiated());
247 state->bindPipelineAndScissorClip(*fProgram, this->bounds());
248 state->bindTextures(fProgram->geomProc(), *fAtlasProxy, fProgram->pipeline());
Chris Daltona05ccc32021-06-29 19:42:13 -0600249 state->bindBuffers(nullptr, std::move(fInstanceBuffer), fVertexBufferIfNoIDSupport);
Chris Daltonaa0e45c2020-03-16 10:05:11 -0600250 state->drawInstanced(fInstanceCount, fBaseInstance, 4, 0);
Chris Dalton4e998532020-02-10 11:06:42 -0700251}