blob: 2e456f57a21165e6e22f700ba8154003f1620320 [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"
13#include "src/gpu/glsl/GrGLSLFragmentShaderBuilder.h"
14#include "src/gpu/glsl/GrGLSLGeometryProcessor.h"
15#include "src/gpu/glsl/GrGLSLVarying.h"
16#include "src/gpu/glsl/GrGLSLVertexGeoBuilder.h"
17
18namespace {
19
20constexpr static GrGeometryProcessor::Attribute kInstanceAttribs[] = {
Chris Daltond2dc8dd2020-05-19 16:32:02 -060021 {"dev_xywh", kInt4_GrVertexAttribType, kInt4_GrSLType},
22 {"atlas_xy", kInt2_GrVertexAttribType, kInt2_GrSLType},
Chris Dalton4e998532020-02-10 11:06:42 -070023 {"color", kFloat4_GrVertexAttribType, kHalf4_GrSLType},
24 {"viewmatrix_scaleskew", kFloat4_GrVertexAttribType, kFloat4_GrSLType},
25 {"viewmatrix_trans", kFloat2_GrVertexAttribType, kFloat2_GrSLType}};
26
27class DrawAtlasPathShader : public GrGeometryProcessor {
28public:
29 DrawAtlasPathShader(const GrTextureProxy* atlasProxy, GrSwizzle swizzle, bool usesLocalCoords)
30 : GrGeometryProcessor(kDrawAtlasPathShader_ClassID)
31 , fAtlasAccess(GrSamplerState::Filter::kNearest, atlasProxy->backendFormat(), swizzle)
32 , fAtlasDimensions(atlasProxy->backingStoreDimensions())
33 , fUsesLocalCoords(usesLocalCoords) {
34 int numInstanceAttribs = SK_ARRAY_COUNT(kInstanceAttribs);
35 if (!fUsesLocalCoords) {
36 numInstanceAttribs -= 2;
37 }
38 this->setInstanceAttributes(kInstanceAttribs, numInstanceAttribs);
39 this->setTextureSamplerCnt(1);
40 }
41
42private:
43 const char* name() const override { return "DrawAtlasPathShader"; }
44 void getGLSLProcessorKey(const GrShaderCaps&, GrProcessorKeyBuilder* b) const override {
45 b->add32(fUsesLocalCoords);
46 }
47 const TextureSampler& onTextureSampler(int) const override { return fAtlasAccess; }
48 GrGLSLPrimitiveProcessor* createGLSLInstance(const GrShaderCaps&) const override;
49
50 const TextureSampler fAtlasAccess;
51 const SkISize fAtlasDimensions;
52 const bool fUsesLocalCoords;
53
54 class Impl;
55};
56
57class DrawAtlasPathShader::Impl : public GrGLSLGeometryProcessor {
58 void onEmitCode(EmitArgs& args, GrGPArgs* gpArgs) override {
59 const auto& shader = args.fGP.cast<DrawAtlasPathShader>();
60 args.fVaryingHandler->emitAttributes(shader);
61
62 GrGLSLVarying atlasCoord(kFloat2_GrSLType);
63 args.fVaryingHandler->addVarying("atlascoord", &atlasCoord);
64
65 GrGLSLVarying color(kHalf4_GrSLType);
66 args.fVaryingHandler->addPassThroughAttribute(
67 kInstanceAttribs[2], args.fOutputColor,
68 GrGLSLVaryingHandler::Interpolation::kCanBeFlat);
69
70 const char* atlasAdjust;
71 fAtlasAdjustUniform = args.fUniformHandler->addUniform(
Ethan Nicholas16464c32020-04-06 13:53:05 -040072 nullptr, kVertex_GrShaderFlag, kFloat2_GrSLType, "atlas_adjust", &atlasAdjust);
Chris Dalton4e998532020-02-10 11:06:42 -070073
74 args.fVertBuilder->codeAppendf(R"(
75 float2 T = float2(sk_VertexID & 1, sk_VertexID >> 1);
Chris Daltond2dc8dd2020-05-19 16:32:02 -060076 float2 devtopleft = float2(dev_xywh.xy);
77 float2 devcoord = abs(float2(dev_xywh.zw)) * T + devtopleft;
78 float2 atlascoord = devcoord - devtopleft;
79 if (dev_xywh.w < 0) { // Negative height indicates that the path is transposed.
80 atlascoord = atlascoord.yx;
81 }
82 atlascoord += atlas_xy;
Chris Dalton4e998532020-02-10 11:06:42 -070083 %s = atlascoord * %s;)",
84 atlasCoord.vsOut(), atlasAdjust);
85
86 gpArgs->fPositionVar.set(kFloat2_GrSLType, "devcoord");
87
Michael Ludwigce869102020-06-20 00:37:30 +000088 GrShaderVar localCoord = gpArgs->fPositionVar;
Chris Dalton4e998532020-02-10 11:06:42 -070089 if (shader.fUsesLocalCoords) {
90 args.fVertBuilder->codeAppendf(R"(
91 float2x2 M = float2x2(viewmatrix_scaleskew);
92 float2 localcoord = inverse(M) * (devcoord - viewmatrix_trans);)");
Michael Ludwigce869102020-06-20 00:37:30 +000093 localCoord.set(kFloat2_GrSLType, "localcoord");
Chris Dalton4e998532020-02-10 11:06:42 -070094 }
Michael Ludwigce869102020-06-20 00:37:30 +000095 this->emitTransforms(args.fVertBuilder, args.fVaryingHandler, args.fUniformHandler,
96 localCoord, args.fFPCoordTransformHandler);
Chris Dalton4e998532020-02-10 11:06:42 -070097
98 args.fFragBuilder->codeAppendf("%s = ", args.fOutputCoverage);
99 args.fFragBuilder->appendTextureLookup(args.fTexSamplers[0], atlasCoord.fsIn());
100 args.fFragBuilder->codeAppendf(".aaaa;");
101 }
102
103 void setData(const GrGLSLProgramDataManager& pdman, const GrPrimitiveProcessor& primProc,
104 const CoordTransformRange& transformRange) override {
105 const SkISize& dimensions = primProc.cast<DrawAtlasPathShader>().fAtlasDimensions;
106 pdman.set2f(fAtlasAdjustUniform, 1.f / dimensions.width(), 1.f / dimensions.height());
Michael Ludwigce869102020-06-20 00:37:30 +0000107 this->setTransformDataHelper(SkMatrix::I(), pdman, transformRange);
Chris Dalton4e998532020-02-10 11:06:42 -0700108 }
109
110 GrGLSLUniformHandler::UniformHandle fAtlasAdjustUniform;
111};
112
113GrGLSLPrimitiveProcessor* DrawAtlasPathShader::createGLSLInstance(const GrShaderCaps&) const {
114 return new Impl();
115}
116
117} // namespace
118
119GrProcessorSet::Analysis GrDrawAtlasPathOp::finalize(const GrCaps& caps, const GrAppliedClip* clip,
120 bool hasMixedSampledCoverage,
121 GrClampType clampType) {
122 const GrProcessorSet::Analysis& analysis = fProcessors.finalize(
123 fInstanceList.fInstance.fColor, GrProcessorAnalysisCoverage::kSingleChannel, clip,
124 &GrUserStencilSettings::kUnused, hasMixedSampledCoverage, caps, clampType,
125 &fInstanceList.fInstance.fColor);
126 fUsesLocalCoords = analysis.usesLocalCoords();
127 return analysis;
128}
129
130GrOp::CombineResult GrDrawAtlasPathOp::onCombineIfPossible(
131 GrOp* op, GrRecordingContext::Arenas* arenas, const GrCaps&) {
132 auto* that = op->cast<GrDrawAtlasPathOp>();
133 SkASSERT(fAtlasProxy == that->fAtlasProxy);
134 SkASSERT(fEnableHWAA == that->fEnableHWAA);
135
136 if (fProcessors != that->fProcessors) {
137 return CombineResult::kCannotCombine;
138 }
139
140 SkASSERT(fUsesLocalCoords == that->fUsesLocalCoords);
141 auto* copy = arenas->recordTimeAllocator()->make<InstanceList>(that->fInstanceList);
142 *fInstanceTail = copy;
143 fInstanceTail = (!copy->fNext) ? &copy->fNext : that->fInstanceTail;
144 fInstanceCount += that->fInstanceCount;
145 return CombineResult::kMerged;
146}
147
Robert Phillipsc655c3a2020-03-18 13:23:45 -0400148void GrDrawAtlasPathOp::onPrePrepare(GrRecordingContext*,
Brian Salomon8afde5f2020-04-01 16:22:00 -0400149 const GrSurfaceProxyView* writeView,
Robert Phillipsc655c3a2020-03-18 13:23:45 -0400150 GrAppliedClip*,
151 const GrXferProcessor::DstProxyView&) {
152}
153
Chris Dalton4e998532020-02-10 11:06:42 -0700154void GrDrawAtlasPathOp::onPrepare(GrOpFlushState* state) {
155 size_t instanceStride = Instance::Stride(fUsesLocalCoords);
156 if (char* instanceData = (char*)state->makeVertexSpace(
157 instanceStride, fInstanceCount, &fInstanceBuffer, &fBaseInstance)) {
158 SkDEBUGCODE(char* end = instanceData + fInstanceCount * instanceStride);
159 for (const InstanceList* list = &fInstanceList; list; list = list->fNext) {
160 memcpy(instanceData, &list->fInstance, instanceStride);
161 instanceData += instanceStride;
162 }
163 SkASSERT(instanceData == end);
164 }
165}
166
167void GrDrawAtlasPathOp::onExecute(GrOpFlushState* state, const SkRect& chainBounds) {
168 SkASSERT(fAtlasProxy->isInstantiated());
169
170 GrPipeline::InitArgs initArgs;
171 if (fEnableHWAA) {
172 initArgs.fInputFlags |= GrPipeline::InputFlags::kHWAntialias;
173 }
174 initArgs.fCaps = &state->caps();
175 initArgs.fDstProxyView = state->drawOpArgs().dstProxyView();
Brian Salomon982f5462020-03-30 12:52:33 -0400176 initArgs.fWriteSwizzle = state->drawOpArgs().writeSwizzle();
Chris Daltonaa0e45c2020-03-16 10:05:11 -0600177 GrPipeline pipeline(initArgs, std::move(fProcessors), state->detachAppliedClip());
Chris Dalton4e998532020-02-10 11:06:42 -0700178
179 GrSwizzle swizzle = state->caps().getReadSwizzle(fAtlasProxy->backendFormat(),
180 GrColorType::kAlpha_8);
Chris Dalton012f8492020-03-05 11:49:15 -0700181
Chris Dalton4e998532020-02-10 11:06:42 -0700182 DrawAtlasPathShader shader(fAtlasProxy.get(), swizzle, fUsesLocalCoords);
183 SkASSERT(shader.instanceStride() == Instance::Stride(fUsesLocalCoords));
184
185 GrProgramInfo programInfo(state->proxy()->numSamples(), state->proxy()->numStencilSamples(),
Brian Salomon8afde5f2020-04-01 16:22:00 -0400186 state->proxy()->backendFormat(), state->writeView()->origin(),
Chris Dalton304e14d2020-03-17 14:29:06 -0600187 &pipeline, &shader, GrPrimitiveType::kTriangleStrip);
Chris Dalton4e998532020-02-10 11:06:42 -0700188
Chris Daltonaa0e45c2020-03-16 10:05:11 -0600189 state->bindPipelineAndScissorClip(programInfo, this->bounds());
190 state->bindTextures(shader, *fAtlasProxy, pipeline);
191 state->bindBuffers(nullptr, fInstanceBuffer.get(), nullptr);
192 state->drawInstanced(fInstanceCount, fBaseInstance, 4, 0);
Chris Dalton4e998532020-02-10 11:06:42 -0700193}