blob: e5c99c01a78ba78c5961ebdff67af89e08502d3b [file] [log] [blame]
Chris Dalton1a325d22017-07-14 15:17:41 -06001/*
2 * Copyright 2017 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
Mike Kleinc0bd9f92019-04-23 12:05:21 -05008#include "src/gpu/ccpr/GrCCPathProcessor.h"
Chris Dalton1a325d22017-07-14 15:17:41 -06009
Mike Kleinc0bd9f92019-04-23 12:05:21 -050010#include "src/gpu/GrOnFlushResourceProvider.h"
Greg Daniel2d41d0d2019-08-26 11:08:51 -040011#include "src/gpu/GrOpsRenderPass.h"
Greg Daniel456f9b52020-03-05 19:14:18 +000012#include "src/gpu/GrTexture.h"
Chris Daltonf91b7552019-04-29 16:21:18 -060013#include "src/gpu/GrTexturePriv.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050014#include "src/gpu/ccpr/GrCCPerFlushResources.h"
15#include "src/gpu/glsl/GrGLSLFragmentShaderBuilder.h"
16#include "src/gpu/glsl/GrGLSLGeometryProcessor.h"
17#include "src/gpu/glsl/GrGLSLProgramBuilder.h"
18#include "src/gpu/glsl/GrGLSLVarying.h"
Chris Dalton1a325d22017-07-14 15:17:41 -060019
Chris Dalton1a325d22017-07-14 15:17:41 -060020// Paths are drawn as octagons. Each point on the octagon is the intersection of two lines: one edge
Chris Dalton377b18b2019-04-18 13:33:50 -060021// from the path's bounding box and one edge from its 45-degree bounding box. The selectors
22// below indicate one corner from the bounding box, paired with a corner from the 45-degree bounding
23// box. The octagon vertex is the point that lies between these two corners, found by intersecting
24// their edges.
25static constexpr float kOctoEdgeNorms[8*4] = {
Chris Dalton1a325d22017-07-14 15:17:41 -060026 // bbox // bbox45
Chris Dalton377b18b2019-04-18 13:33:50 -060027 0,0, 0,0,
28 0,0, 1,0,
29 1,0, 1,0,
30 1,0, 1,1,
31 1,1, 1,1,
32 1,1, 0,1,
33 0,1, 0,1,
34 0,1, 0,0,
Chris Dalton1a325d22017-07-14 15:17:41 -060035};
36
37GR_DECLARE_STATIC_UNIQUE_KEY(gVertexBufferKey);
38
Brian Salomondbf70722019-02-07 11:31:24 -050039sk_sp<const GrGpuBuffer> GrCCPathProcessor::FindVertexBuffer(GrOnFlushResourceProvider* onFlushRP) {
Chris Dalton5d2de082017-12-19 10:40:23 -070040 GR_DEFINE_STATIC_UNIQUE_KEY(gVertexBufferKey);
Brian Salomonae64c192019-02-05 09:41:37 -050041 return onFlushRP->findOrMakeStaticBuffer(GrGpuBufferType::kVertex, sizeof(kOctoEdgeNorms),
Chris Dalton5d2de082017-12-19 10:40:23 -070042 kOctoEdgeNorms, gVertexBufferKey);
43}
44
Chris Dalton27059d32018-01-23 14:06:50 -070045static constexpr uint16_t kRestartStrip = 0xffff;
46
47static constexpr uint16_t kOctoIndicesAsStrips[] = {
Chris Dalton377b18b2019-04-18 13:33:50 -060048 3, 4, 2, 0, 1, kRestartStrip, // First half.
49 7, 0, 6, 4, 5 // Second half.
Chris Dalton27059d32018-01-23 14:06:50 -070050};
51
52static constexpr uint16_t kOctoIndicesAsTris[] = {
53 // First half.
Chris Dalton377b18b2019-04-18 13:33:50 -060054 3, 4, 2,
55 4, 0, 2,
56 2, 0, 1,
Chris Dalton27059d32018-01-23 14:06:50 -070057
58 // Second half.
Chris Dalton377b18b2019-04-18 13:33:50 -060059 7, 0, 6,
60 0, 4, 6,
61 6, 4, 5,
Chris Dalton1a325d22017-07-14 15:17:41 -060062};
63
64GR_DECLARE_STATIC_UNIQUE_KEY(gIndexBufferKey);
65
Brian Salomon92be2f72018-06-19 14:33:47 -040066constexpr GrPrimitiveProcessor::Attribute GrCCPathProcessor::kInstanceAttribs[];
Chris Dalton377b18b2019-04-18 13:33:50 -060067constexpr GrPrimitiveProcessor::Attribute GrCCPathProcessor::kCornersAttrib;
Brian Salomon92be2f72018-06-19 14:33:47 -040068
Brian Salomondbf70722019-02-07 11:31:24 -050069sk_sp<const GrGpuBuffer> GrCCPathProcessor::FindIndexBuffer(GrOnFlushResourceProvider* onFlushRP) {
Chris Dalton5d2de082017-12-19 10:40:23 -070070 GR_DEFINE_STATIC_UNIQUE_KEY(gIndexBufferKey);
Chris Dalton27059d32018-01-23 14:06:50 -070071 if (onFlushRP->caps()->usePrimitiveRestart()) {
Brian Salomonae64c192019-02-05 09:41:37 -050072 return onFlushRP->findOrMakeStaticBuffer(GrGpuBufferType::kIndex,
73 sizeof(kOctoIndicesAsStrips), kOctoIndicesAsStrips,
74 gIndexBufferKey);
Chris Dalton27059d32018-01-23 14:06:50 -070075 } else {
Brian Salomonae64c192019-02-05 09:41:37 -050076 return onFlushRP->findOrMakeStaticBuffer(GrGpuBufferType::kIndex,
77 sizeof(kOctoIndicesAsTris), kOctoIndicesAsTris,
78 gIndexBufferKey);
Chris Dalton27059d32018-01-23 14:06:50 -070079 }
Chris Dalton5d2de082017-12-19 10:40:23 -070080}
81
Chris Dalton6a5317a2019-07-12 09:55:52 -060082GrCCPathProcessor::GrCCPathProcessor(CoverageMode coverageMode, const GrTexture* atlasTexture,
83 const GrSwizzle& swizzle, GrSurfaceOrigin atlasOrigin,
Chris Dalton1c548942018-05-22 13:09:48 -060084 const SkMatrix& viewMatrixIfUsingLocalCoords)
Chris Dalton383a2ef2018-01-08 17:21:41 -050085 : INHERITED(kGrCCPathProcessor_ClassID)
Chris Dalton6a5317a2019-07-12 09:55:52 -060086 , fCoverageMode(coverageMode)
Brian Salomonccb61422020-01-09 10:46:36 -050087 , fAtlasAccess(GrSamplerState::Filter::kNearest, atlasTexture->backendFormat(), swizzle)
Brian Salomon2638f3d2019-10-22 12:20:37 -040088 , fAtlasDimensions(atlasTexture->dimensions())
Chris Daltonf91b7552019-04-29 16:21:18 -060089 , fAtlasOrigin(atlasOrigin) {
Brian Salomon7eae3e02018-08-07 14:02:38 +000090 // TODO: Can we just assert that atlas has GrCCAtlas::kTextureOrigin and remove fAtlasOrigin?
Chris Dalton377b18b2019-04-18 13:33:50 -060091 this->setInstanceAttributes(kInstanceAttribs, SK_ARRAY_COUNT(kInstanceAttribs));
Brian Osmanf04fb3c2018-11-12 15:34:00 -050092 SkASSERT(this->instanceStride() == sizeof(Instance));
Chris Dalton1a325d22017-07-14 15:17:41 -060093
Chris Dalton377b18b2019-04-18 13:33:50 -060094 this->setVertexAttributes(&kCornersAttrib, 1);
Brian Salomonf7dcd762018-07-30 14:48:15 -040095 this->setTextureSamplerCnt(1);
Chris Dalton27059d32018-01-23 14:06:50 -070096
Chris Dalton1c548942018-05-22 13:09:48 -060097 if (!viewMatrixIfUsingLocalCoords.invert(&fLocalMatrix)) {
98 fLocalMatrix.setIdentity();
Chris Dalton27059d32018-01-23 14:06:50 -070099 }
Chris Dalton1a325d22017-07-14 15:17:41 -0600100}
101
Chris Dalton377b18b2019-04-18 13:33:50 -0600102class GrCCPathProcessor::Impl : public GrGLSLGeometryProcessor {
Chris Dalton1a325d22017-07-14 15:17:41 -0600103public:
104 void onEmitCode(EmitArgs& args, GrGPArgs* gpArgs) override;
105
Michael Ludwig553db622020-06-19 10:47:30 -0400106 static void GenKey(const GrCCPathProcessor& cc, GrProcessorKeyBuilder* b) {
107 b->add32(AddMatrixKeys((uint32_t) cc.fCoverageMode, SkMatrix::I(), cc.fLocalMatrix));
108 }
109
Chris Dalton1a325d22017-07-14 15:17:41 -0600110private:
111 void setData(const GrGLSLProgramDataManager& pdman, const GrPrimitiveProcessor& primProc,
Brian Salomonc241b582019-11-27 08:57:17 -0500112 const CoordTransformRange& transformRange) override {
Chris Daltonf91b7552019-04-29 16:21:18 -0600113 const auto& proc = primProc.cast<GrCCPathProcessor>();
Brian Salomon2638f3d2019-10-22 12:20:37 -0400114 pdman.set2f(fAtlasAdjustUniform,
115 1.0f / proc.fAtlasDimensions.fWidth,
116 1.0f / proc.fAtlasDimensions.fHeight);
Michael Ludwig553db622020-06-19 10:47:30 -0400117 this->setTransformDataHelper(pdman, transformRange);
118 this->setTransform(pdman, fLocalMatrixUni, proc.fLocalMatrix, &fLocalMatrix);
Chris Dalton1a325d22017-07-14 15:17:41 -0600119 }
120
121 GrGLSLUniformHandler::UniformHandle fAtlasAdjustUniform;
Michael Ludwig553db622020-06-19 10:47:30 -0400122 GrGLSLUniformHandler::UniformHandle fLocalMatrixUni;
123 SkMatrix fLocalMatrix = SkMatrix::InvalidMatrix();
Chris Dalton1a325d22017-07-14 15:17:41 -0600124
125 typedef GrGLSLGeometryProcessor INHERITED;
126};
127
Michael Ludwig553db622020-06-19 10:47:30 -0400128void GrCCPathProcessor::getGLSLProcessorKey(const GrShaderCaps&, GrProcessorKeyBuilder* b) const {
129 GrCCPathProcessor::Impl::GenKey(*this, b);
130}
131
Chris Dalton383a2ef2018-01-08 17:21:41 -0500132GrGLSLPrimitiveProcessor* GrCCPathProcessor::createGLSLInstance(const GrShaderCaps&) const {
Chris Dalton377b18b2019-04-18 13:33:50 -0600133 return new Impl();
Chris Dalton1a325d22017-07-14 15:17:41 -0600134}
135
Chris Daltond925f2d2018-05-07 19:19:06 -0600136void GrCCPathProcessor::drawPaths(GrOpFlushState* flushState, const GrPipeline& pipeline,
Chris Dalton39ca9732020-03-10 10:34:17 -0600137 const GrSurfaceProxy& atlasProxy,
Chris Dalton4da70192018-06-18 09:51:36 -0600138 const GrCCPerFlushResources& resources, int baseInstance,
139 int endInstance, const SkRect& bounds) const {
Chris Daltond925f2d2018-05-07 19:19:06 -0600140 const GrCaps& caps = flushState->caps();
141 GrPrimitiveType primitiveType = caps.usePrimitiveRestart()
142 ? GrPrimitiveType::kTriangleStrip
143 : GrPrimitiveType::kTriangles;
144 int numIndicesPerInstance = caps.usePrimitiveRestart()
145 ? SK_ARRAY_COUNT(kOctoIndicesAsStrips)
146 : SK_ARRAY_COUNT(kOctoIndicesAsTris);
Brian Salomon802cb312018-06-08 18:05:20 -0400147 auto enablePrimitiveRestart = GrPrimitiveRestart(flushState->caps().usePrimitiveRestart());
148
Chris Dalton39ca9732020-03-10 10:34:17 -0600149 GrRenderTargetProxy* rtProxy = flushState->proxy();
150 GrProgramInfo programInfo(rtProxy->numSamples(), rtProxy->numStencilSamples(),
Brian Salomon8afde5f2020-04-01 16:22:00 -0400151 rtProxy->backendFormat(), flushState->writeView()->origin(),
Chris Dalton304e14d2020-03-17 14:29:06 -0600152 &pipeline, this, primitiveType);
Chris Daltond925f2d2018-05-07 19:19:06 -0600153
Chris Daltonaa0e45c2020-03-16 10:05:11 -0600154 flushState->bindPipelineAndScissorClip(programInfo, bounds);
155 flushState->bindTextures(*this, atlasProxy, pipeline);
156 flushState->bindBuffers(resources.indexBuffer(), resources.instanceBuffer(),
Chris Dalton39ca9732020-03-10 10:34:17 -0600157 resources.vertexBuffer(), enablePrimitiveRestart);
Chris Daltonaa0e45c2020-03-16 10:05:11 -0600158 flushState->drawIndexedInstanced(numIndicesPerInstance, 0, endInstance - baseInstance,
Chris Dalton39ca9732020-03-10 10:34:17 -0600159 baseInstance, 0);
Chris Daltond925f2d2018-05-07 19:19:06 -0600160}
161
Chris Dalton377b18b2019-04-18 13:33:50 -0600162void GrCCPathProcessor::Impl::onEmitCode(EmitArgs& args, GrGPArgs* gpArgs) {
Chris Dalton7b046312018-02-02 11:06:30 -0700163 using Interpolation = GrGLSLVaryingHandler::Interpolation;
164
Chris Dalton383a2ef2018-01-08 17:21:41 -0500165 const GrCCPathProcessor& proc = args.fGP.cast<GrCCPathProcessor>();
Chris Dalton1a325d22017-07-14 15:17:41 -0600166 GrGLSLUniformHandler* uniHandler = args.fUniformHandler;
167 GrGLSLVaryingHandler* varyingHandler = args.fVaryingHandler;
Chris Dalton6a5317a2019-07-12 09:55:52 -0600168 bool isCoverageCount = (CoverageMode::kCoverageCount == proc.fCoverageMode);
Chris Dalton1a325d22017-07-14 15:17:41 -0600169
170 const char* atlasAdjust;
171 fAtlasAdjustUniform = uniHandler->addUniform(
Ethan Nicholas16464c32020-04-06 13:53:05 -0400172 nullptr, kVertex_GrShaderFlag, kFloat2_GrSLType, "atlas_adjust", &atlasAdjust);
Chris Dalton1a325d22017-07-14 15:17:41 -0600173
174 varyingHandler->emitAttributes(proc);
175
Chris Dalton6a5317a2019-07-12 09:55:52 -0600176 GrGLSLVarying texcoord((isCoverageCount) ? kFloat3_GrSLType : kFloat2_GrSLType);
Chris Daltonfdde34e2017-10-16 14:15:26 -0600177 varyingHandler->addVarying("texcoord", &texcoord);
Chris Dalton6a5317a2019-07-12 09:55:52 -0600178
179 GrGLSLVarying color(kHalf4_GrSLType);
Chris Dalton377b18b2019-04-18 13:33:50 -0600180 varyingHandler->addPassThroughAttribute(
181 kInstanceAttribs[kColorAttribIdx], args.fOutputColor, Interpolation::kCanBeFlat);
Chris Dalton1a325d22017-07-14 15:17:41 -0600182
Chris Daltond0b8d932017-12-21 16:48:52 -0700183 // The vertex shader bloats and intersects the devBounds and devBounds45 rectangles, in order to
184 // find an octagon that circumscribes the (bloated) path.
Chris Dalton1a325d22017-07-14 15:17:41 -0600185 GrGLSLVertexBuilder* v = args.fVertBuilder;
186
Chris Dalton5b5403e2019-06-05 11:54:39 -0600187 // Are we clockwise? (Positive wind => nonzero fill rule.)
188 // Or counter-clockwise? (negative wind => even/odd fill rule.)
Chris Dalton377b18b2019-04-18 13:33:50 -0600189 v->codeAppendf("float wind = sign(devbounds.z - devbounds.x);");
Chris Dalton1a325d22017-07-14 15:17:41 -0600190
Chris Dalton377b18b2019-04-18 13:33:50 -0600191 // Find our reference corner from the device-space bounding box.
192 v->codeAppendf("float2 refpt = mix(devbounds.xy, devbounds.zw, corners.xy);");
Chris Dalton1a325d22017-07-14 15:17:41 -0600193
Chris Dalton377b18b2019-04-18 13:33:50 -0600194 // Find our reference corner from the 45-degree bounding box.
195 v->codeAppendf("float2 refpt45 = mix(devbounds45.xy, devbounds45.zw, corners.zw);");
196 // Transform back to device space.
197 v->codeAppendf("refpt45 *= float2x2(+1, +1, -wind, +wind) * .5;");
Chris Dalton1a325d22017-07-14 15:17:41 -0600198
Chris Dalton377b18b2019-04-18 13:33:50 -0600199 // Find the normals to each edge, then intersect them to find our octagon vertex.
200 v->codeAppendf("float2x2 N = float2x2("
201 "corners.z + corners.w - 1, corners.w - corners.z, "
202 "corners.xy*2 - 1);");
203 v->codeAppendf("N = float2x2(wind, 0, 0, 1) * N;");
204 v->codeAppendf("float2 K = float2(dot(N[0], refpt), dot(N[1], refpt45));");
Ethan Nicholas8aa45692017-09-20 11:24:15 -0400205 v->codeAppendf("float2 octocoord = K * inverse(N);");
Chris Dalton1a325d22017-07-14 15:17:41 -0600206
Chris Dalton51e8b7e2018-10-09 21:18:45 -0600207 // Round the octagon out to ensure we rasterize every pixel the path might touch. (Positive
208 // bloatdir means we should take the "ceil" and negative means to take the "floor".)
209 //
210 // NOTE: If we were just drawing a rect, ceil/floor would be enough. But since there are also
211 // diagonals in the octagon that cross through pixel centers, we need to outset by another
212 // quarter px to ensure those pixels get rasterized.
Chris Dalton377b18b2019-04-18 13:33:50 -0600213 v->codeAppendf("float2 bloatdir = (0 != N[0].x) "
214 "? float2(N[0].x, N[1].y)"
215 ": float2(N[1].x, N[0].y);");
216 v->codeAppendf("octocoord = (ceil(octocoord * bloatdir - 1e-4) + 0.25) * bloatdir;");
Chris Dalton6a5317a2019-07-12 09:55:52 -0600217 v->codeAppendf("float2 atlascoord = octocoord + float2(dev_to_atlas_offset);");
Chris Dalton1a325d22017-07-14 15:17:41 -0600218
219 // Convert to atlas coordinates in order to do our texture lookup.
Chris Daltonf91b7552019-04-29 16:21:18 -0600220 if (kTopLeft_GrSurfaceOrigin == proc.fAtlasOrigin) {
Chris Daltondaef06a2018-05-23 17:11:09 -0600221 v->codeAppendf("%s.xy = atlascoord * %s;", texcoord.vsOut(), atlasAdjust);
Chris Dalton1a325d22017-07-14 15:17:41 -0600222 } else {
Chris Daltonf91b7552019-04-29 16:21:18 -0600223 SkASSERT(kBottomLeft_GrSurfaceOrigin == proc.fAtlasOrigin);
Chris Daltondaef06a2018-05-23 17:11:09 -0600224 v->codeAppendf("%s.xy = float2(atlascoord.x * %s.x, 1 - atlascoord.y * %s.y);",
Chris Dalton1a325d22017-07-14 15:17:41 -0600225 texcoord.vsOut(), atlasAdjust, atlasAdjust);
226 }
Chris Dalton6a5317a2019-07-12 09:55:52 -0600227 if (isCoverageCount) {
228 v->codeAppendf("%s.z = wind * .5;", texcoord.vsOut());
229 }
Chris Dalton1a325d22017-07-14 15:17:41 -0600230
Chris Dalton377b18b2019-04-18 13:33:50 -0600231 gpArgs->fPositionVar.set(kFloat2_GrSLType, "octocoord");
Michael Ludwig553db622020-06-19 10:47:30 -0400232 this->writeLocalCoord(v, args.fUniformHandler, gpArgs, gpArgs->fPositionVar, proc.fLocalMatrix,
233 &fLocalMatrixUni);
Chris Dalton1a325d22017-07-14 15:17:41 -0600234
235 // Fragment shader.
Chris Dalton60283612018-02-14 13:38:14 -0700236 GrGLSLFPFragmentBuilder* f = args.fFragBuilder;
Chris Dalton1a325d22017-07-14 15:17:41 -0600237
Chris Dalton6a5317a2019-07-12 09:55:52 -0600238 // Look up coverage in the atlas.
239 f->codeAppendf("half coverage = ");
Brian Salomond19cd762020-01-06 13:16:31 -0500240 f->appendTextureLookup(args.fTexSamplers[0], SkStringPrintf("%s.xy", texcoord.fsIn()).c_str());
Chris Dalton6a5317a2019-07-12 09:55:52 -0600241 f->codeAppendf(".a;");
Chris Dalton1a325d22017-07-14 15:17:41 -0600242
Chris Dalton6a5317a2019-07-12 09:55:52 -0600243 if (isCoverageCount) {
244 f->codeAppendf("coverage = abs(coverage);");
Chris Daltondaef06a2018-05-23 17:11:09 -0600245
Chris Dalton6a5317a2019-07-12 09:55:52 -0600246 // Scale coverage count by .5. Make it negative for even-odd paths and positive for
247 // winding ones. Clamp winding coverage counts at 1.0 (i.e. min(coverage/2, .5)).
248 f->codeAppendf("coverage = min(abs(coverage) * half(%s.z), .5);", texcoord.fsIn());
249
250 // For negative values, this finishes the even-odd sawtooth function. Since positive
251 // (winding) values were clamped at "coverage/2 = .5", this only undoes the previous
252 // multiply by .5.
253 f->codeAppend ("coverage = 1 - abs(fract(coverage) * 2 - 1);");
254 }
Chris Daltondaef06a2018-05-23 17:11:09 -0600255
256 f->codeAppendf("%s = half4(coverage);", args.fOutputCoverage);
Chris Dalton1a325d22017-07-14 15:17:41 -0600257}