blob: 5ea975556c9f2d760fb6eb003fde32b533c2b5ff [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 "include/gpu/GrTexture.h"
11#include "src/gpu/GrGpuCommandBuffer.h"
12#include "src/gpu/GrOnFlushResourceProvider.h"
13#include "src/gpu/ccpr/GrCCPerFlushResources.h"
14#include "src/gpu/glsl/GrGLSLFragmentShaderBuilder.h"
15#include "src/gpu/glsl/GrGLSLGeometryProcessor.h"
16#include "src/gpu/glsl/GrGLSLProgramBuilder.h"
17#include "src/gpu/glsl/GrGLSLVarying.h"
Chris Dalton1a325d22017-07-14 15:17:41 -060018
Chris Dalton1a325d22017-07-14 15:17:41 -060019// 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 -060020// from the path's bounding box and one edge from its 45-degree bounding box. The selectors
21// below indicate one corner from the bounding box, paired with a corner from the 45-degree bounding
22// box. The octagon vertex is the point that lies between these two corners, found by intersecting
23// their edges.
24static constexpr float kOctoEdgeNorms[8*4] = {
Chris Dalton1a325d22017-07-14 15:17:41 -060025 // bbox // bbox45
Chris Dalton377b18b2019-04-18 13:33:50 -060026 0,0, 0,0,
27 0,0, 1,0,
28 1,0, 1,0,
29 1,0, 1,1,
30 1,1, 1,1,
31 1,1, 0,1,
32 0,1, 0,1,
33 0,1, 0,0,
Chris Dalton1a325d22017-07-14 15:17:41 -060034};
35
36GR_DECLARE_STATIC_UNIQUE_KEY(gVertexBufferKey);
37
Brian Salomondbf70722019-02-07 11:31:24 -050038sk_sp<const GrGpuBuffer> GrCCPathProcessor::FindVertexBuffer(GrOnFlushResourceProvider* onFlushRP) {
Chris Dalton5d2de082017-12-19 10:40:23 -070039 GR_DEFINE_STATIC_UNIQUE_KEY(gVertexBufferKey);
Brian Salomonae64c192019-02-05 09:41:37 -050040 return onFlushRP->findOrMakeStaticBuffer(GrGpuBufferType::kVertex, sizeof(kOctoEdgeNorms),
Chris Dalton5d2de082017-12-19 10:40:23 -070041 kOctoEdgeNorms, gVertexBufferKey);
42}
43
Chris Dalton27059d32018-01-23 14:06:50 -070044static constexpr uint16_t kRestartStrip = 0xffff;
45
46static constexpr uint16_t kOctoIndicesAsStrips[] = {
Chris Dalton377b18b2019-04-18 13:33:50 -060047 3, 4, 2, 0, 1, kRestartStrip, // First half.
48 7, 0, 6, 4, 5 // Second half.
Chris Dalton27059d32018-01-23 14:06:50 -070049};
50
51static constexpr uint16_t kOctoIndicesAsTris[] = {
52 // First half.
Chris Dalton377b18b2019-04-18 13:33:50 -060053 3, 4, 2,
54 4, 0, 2,
55 2, 0, 1,
Chris Dalton27059d32018-01-23 14:06:50 -070056
57 // Second half.
Chris Dalton377b18b2019-04-18 13:33:50 -060058 7, 0, 6,
59 0, 4, 6,
60 6, 4, 5,
Chris Dalton1a325d22017-07-14 15:17:41 -060061};
62
63GR_DECLARE_STATIC_UNIQUE_KEY(gIndexBufferKey);
64
Brian Salomon92be2f72018-06-19 14:33:47 -040065constexpr GrPrimitiveProcessor::Attribute GrCCPathProcessor::kInstanceAttribs[];
Chris Dalton377b18b2019-04-18 13:33:50 -060066constexpr GrPrimitiveProcessor::Attribute GrCCPathProcessor::kCornersAttrib;
Brian Salomon92be2f72018-06-19 14:33:47 -040067
Brian Salomondbf70722019-02-07 11:31:24 -050068sk_sp<const GrGpuBuffer> GrCCPathProcessor::FindIndexBuffer(GrOnFlushResourceProvider* onFlushRP) {
Chris Dalton5d2de082017-12-19 10:40:23 -070069 GR_DEFINE_STATIC_UNIQUE_KEY(gIndexBufferKey);
Chris Dalton27059d32018-01-23 14:06:50 -070070 if (onFlushRP->caps()->usePrimitiveRestart()) {
Brian Salomonae64c192019-02-05 09:41:37 -050071 return onFlushRP->findOrMakeStaticBuffer(GrGpuBufferType::kIndex,
72 sizeof(kOctoIndicesAsStrips), kOctoIndicesAsStrips,
73 gIndexBufferKey);
Chris Dalton27059d32018-01-23 14:06:50 -070074 } else {
Brian Salomonae64c192019-02-05 09:41:37 -050075 return onFlushRP->findOrMakeStaticBuffer(GrGpuBufferType::kIndex,
76 sizeof(kOctoIndicesAsTris), kOctoIndicesAsTris,
77 gIndexBufferKey);
Chris Dalton27059d32018-01-23 14:06:50 -070078 }
Chris Dalton5d2de082017-12-19 10:40:23 -070079}
80
Brian Salomon7eae3e02018-08-07 14:02:38 +000081GrCCPathProcessor::GrCCPathProcessor(const GrTextureProxy* atlas,
Chris Dalton1c548942018-05-22 13:09:48 -060082 const SkMatrix& viewMatrixIfUsingLocalCoords)
Chris Dalton383a2ef2018-01-08 17:21:41 -050083 : INHERITED(kGrCCPathProcessor_ClassID)
Brian Salomon7eae3e02018-08-07 14:02:38 +000084 , fAtlasAccess(atlas->textureType(), atlas->config(), GrSamplerState::Filter::kNearest,
Greg Daniel0f70be82018-10-08 17:35:08 +000085 GrSamplerState::WrapMode::kClamp)
Brian Salomon7eae3e02018-08-07 14:02:38 +000086 , fAtlasSize(atlas->isize())
87 , fAtlasOrigin(atlas->origin()) {
88 // TODO: Can we just assert that atlas has GrCCAtlas::kTextureOrigin and remove fAtlasOrigin?
Chris Dalton377b18b2019-04-18 13:33:50 -060089 this->setInstanceAttributes(kInstanceAttribs, SK_ARRAY_COUNT(kInstanceAttribs));
Brian Osmanf04fb3c2018-11-12 15:34:00 -050090 SkASSERT(this->instanceStride() == sizeof(Instance));
Chris Dalton1a325d22017-07-14 15:17:41 -060091
Chris Dalton377b18b2019-04-18 13:33:50 -060092 this->setVertexAttributes(&kCornersAttrib, 1);
Brian Salomonf7dcd762018-07-30 14:48:15 -040093 this->setTextureSamplerCnt(1);
Chris Dalton27059d32018-01-23 14:06:50 -070094
Chris Dalton1c548942018-05-22 13:09:48 -060095 if (!viewMatrixIfUsingLocalCoords.invert(&fLocalMatrix)) {
96 fLocalMatrix.setIdentity();
Chris Dalton27059d32018-01-23 14:06:50 -070097 }
Chris Dalton1a325d22017-07-14 15:17:41 -060098}
99
Chris Dalton377b18b2019-04-18 13:33:50 -0600100class GrCCPathProcessor::Impl : public GrGLSLGeometryProcessor {
Chris Dalton1a325d22017-07-14 15:17:41 -0600101public:
102 void onEmitCode(EmitArgs& args, GrGPArgs* gpArgs) override;
103
104private:
105 void setData(const GrGLSLProgramDataManager& pdman, const GrPrimitiveProcessor& primProc,
106 FPCoordTransformIter&& transformIter) override {
Chris Dalton383a2ef2018-01-08 17:21:41 -0500107 const GrCCPathProcessor& proc = primProc.cast<GrCCPathProcessor>();
Brian Salomon7eae3e02018-08-07 14:02:38 +0000108 pdman.set2f(fAtlasAdjustUniform, 1.0f / proc.atlasSize().fWidth,
109 1.0f / proc.atlasSize().fHeight);
Chris Dalton1c548942018-05-22 13:09:48 -0600110 this->setTransformDataHelper(proc.localMatrix(), pdman, &transformIter);
Chris Dalton1a325d22017-07-14 15:17:41 -0600111 }
112
113 GrGLSLUniformHandler::UniformHandle fAtlasAdjustUniform;
114
115 typedef GrGLSLGeometryProcessor INHERITED;
116};
117
Chris Dalton383a2ef2018-01-08 17:21:41 -0500118GrGLSLPrimitiveProcessor* GrCCPathProcessor::createGLSLInstance(const GrShaderCaps&) const {
Chris Dalton377b18b2019-04-18 13:33:50 -0600119 return new Impl();
Chris Dalton1a325d22017-07-14 15:17:41 -0600120}
121
Chris Daltond925f2d2018-05-07 19:19:06 -0600122void GrCCPathProcessor::drawPaths(GrOpFlushState* flushState, const GrPipeline& pipeline,
Brian Salomon49348902018-06-26 09:12:38 -0400123 const GrPipeline::FixedDynamicState* fixedDynamicState,
Chris Dalton4da70192018-06-18 09:51:36 -0600124 const GrCCPerFlushResources& resources, int baseInstance,
125 int endInstance, const SkRect& bounds) const {
Chris Daltond925f2d2018-05-07 19:19:06 -0600126 const GrCaps& caps = flushState->caps();
127 GrPrimitiveType primitiveType = caps.usePrimitiveRestart()
128 ? GrPrimitiveType::kTriangleStrip
129 : GrPrimitiveType::kTriangles;
130 int numIndicesPerInstance = caps.usePrimitiveRestart()
131 ? SK_ARRAY_COUNT(kOctoIndicesAsStrips)
132 : SK_ARRAY_COUNT(kOctoIndicesAsTris);
133 GrMesh mesh(primitiveType);
Brian Salomon802cb312018-06-08 18:05:20 -0400134 auto enablePrimitiveRestart = GrPrimitiveRestart(flushState->caps().usePrimitiveRestart());
135
Brian Salomon12d22642019-01-29 14:38:50 -0500136 mesh.setIndexedInstanced(resources.refIndexBuffer(), numIndicesPerInstance,
137 resources.refInstanceBuffer(), endInstance - baseInstance,
138 baseInstance, enablePrimitiveRestart);
139 mesh.setVertexData(resources.refVertexBuffer());
Chris Daltond925f2d2018-05-07 19:19:06 -0600140
Brian Salomon49348902018-06-26 09:12:38 -0400141 flushState->rtCommandBuffer()->draw(*this, pipeline, fixedDynamicState, nullptr, &mesh, 1,
142 bounds);
Chris Daltond925f2d2018-05-07 19:19:06 -0600143}
144
Chris Dalton377b18b2019-04-18 13:33:50 -0600145void GrCCPathProcessor::Impl::onEmitCode(EmitArgs& args, GrGPArgs* gpArgs) {
Chris Dalton7b046312018-02-02 11:06:30 -0700146 using Interpolation = GrGLSLVaryingHandler::Interpolation;
147
Chris Dalton383a2ef2018-01-08 17:21:41 -0500148 const GrCCPathProcessor& proc = args.fGP.cast<GrCCPathProcessor>();
Chris Dalton1a325d22017-07-14 15:17:41 -0600149 GrGLSLUniformHandler* uniHandler = args.fUniformHandler;
150 GrGLSLVaryingHandler* varyingHandler = args.fVaryingHandler;
151
152 const char* atlasAdjust;
153 fAtlasAdjustUniform = uniHandler->addUniform(
Chris Dalton377b18b2019-04-18 13:33:50 -0600154 kVertex_GrShaderFlag, kFloat2_GrSLType, "atlas_adjust", &atlasAdjust);
Chris Dalton1a325d22017-07-14 15:17:41 -0600155
156 varyingHandler->emitAttributes(proc);
157
Chris Daltondaef06a2018-05-23 17:11:09 -0600158 GrGLSLVarying texcoord(kFloat3_GrSLType);
Chris Dalton27372882017-12-08 13:34:21 -0700159 GrGLSLVarying color(kHalf4_GrSLType);
Chris Daltonfdde34e2017-10-16 14:15:26 -0600160 varyingHandler->addVarying("texcoord", &texcoord);
Chris Dalton377b18b2019-04-18 13:33:50 -0600161 varyingHandler->addPassThroughAttribute(
162 kInstanceAttribs[kColorAttribIdx], args.fOutputColor, Interpolation::kCanBeFlat);
Chris Dalton1a325d22017-07-14 15:17:41 -0600163
Chris Daltond0b8d932017-12-21 16:48:52 -0700164 // The vertex shader bloats and intersects the devBounds and devBounds45 rectangles, in order to
165 // find an octagon that circumscribes the (bloated) path.
Chris Dalton1a325d22017-07-14 15:17:41 -0600166 GrGLSLVertexBuilder* v = args.fVertBuilder;
167
Chris Dalton377b18b2019-04-18 13:33:50 -0600168 // Are we clockwise (positive wind, nonzero fill), or counter-clockwise (negative wind,
169 // even/odd fill)?
170 v->codeAppendf("float wind = sign(devbounds.z - devbounds.x);");
Chris Dalton1a325d22017-07-14 15:17:41 -0600171
Chris Dalton377b18b2019-04-18 13:33:50 -0600172 // Find our reference corner from the device-space bounding box.
173 v->codeAppendf("float2 refpt = mix(devbounds.xy, devbounds.zw, corners.xy);");
Chris Dalton1a325d22017-07-14 15:17:41 -0600174
Chris Dalton377b18b2019-04-18 13:33:50 -0600175 // Find our reference corner from the 45-degree bounding box.
176 v->codeAppendf("float2 refpt45 = mix(devbounds45.xy, devbounds45.zw, corners.zw);");
177 // Transform back to device space.
178 v->codeAppendf("refpt45 *= float2x2(+1, +1, -wind, +wind) * .5;");
Chris Dalton1a325d22017-07-14 15:17:41 -0600179
Chris Dalton377b18b2019-04-18 13:33:50 -0600180 // Find the normals to each edge, then intersect them to find our octagon vertex.
181 v->codeAppendf("float2x2 N = float2x2("
182 "corners.z + corners.w - 1, corners.w - corners.z, "
183 "corners.xy*2 - 1);");
184 v->codeAppendf("N = float2x2(wind, 0, 0, 1) * N;");
185 v->codeAppendf("float2 K = float2(dot(N[0], refpt), dot(N[1], refpt45));");
Ethan Nicholas8aa45692017-09-20 11:24:15 -0400186 v->codeAppendf("float2 octocoord = K * inverse(N);");
Chris Dalton1a325d22017-07-14 15:17:41 -0600187
Chris Dalton51e8b7e2018-10-09 21:18:45 -0600188 // Round the octagon out to ensure we rasterize every pixel the path might touch. (Positive
189 // bloatdir means we should take the "ceil" and negative means to take the "floor".)
190 //
191 // NOTE: If we were just drawing a rect, ceil/floor would be enough. But since there are also
192 // diagonals in the octagon that cross through pixel centers, we need to outset by another
193 // quarter px to ensure those pixels get rasterized.
Chris Dalton377b18b2019-04-18 13:33:50 -0600194 v->codeAppendf("float2 bloatdir = (0 != N[0].x) "
195 "? float2(N[0].x, N[1].y)"
196 ": float2(N[1].x, N[0].y);");
197 v->codeAppendf("octocoord = (ceil(octocoord * bloatdir - 1e-4) + 0.25) * bloatdir;");
Chris Dalton1a325d22017-07-14 15:17:41 -0600198
199 // Convert to atlas coordinates in order to do our texture lookup.
Chris Dalton377b18b2019-04-18 13:33:50 -0600200 v->codeAppendf("float2 atlascoord = octocoord + float2(dev_to_atlas_offset);");
Brian Salomon7eae3e02018-08-07 14:02:38 +0000201 if (kTopLeft_GrSurfaceOrigin == proc.atlasOrigin()) {
Chris Daltondaef06a2018-05-23 17:11:09 -0600202 v->codeAppendf("%s.xy = atlascoord * %s;", texcoord.vsOut(), atlasAdjust);
Chris Dalton1a325d22017-07-14 15:17:41 -0600203 } else {
Brian Salomon7eae3e02018-08-07 14:02:38 +0000204 SkASSERT(kBottomLeft_GrSurfaceOrigin == proc.atlasOrigin());
Chris Daltondaef06a2018-05-23 17:11:09 -0600205 v->codeAppendf("%s.xy = float2(atlascoord.x * %s.x, 1 - atlascoord.y * %s.y);",
Chris Dalton1a325d22017-07-14 15:17:41 -0600206 texcoord.vsOut(), atlasAdjust, atlasAdjust);
207 }
Chris Dalton377b18b2019-04-18 13:33:50 -0600208 v->codeAppendf("%s.z = wind * .5;", texcoord.vsOut());
Chris Dalton1a325d22017-07-14 15:17:41 -0600209
Chris Dalton377b18b2019-04-18 13:33:50 -0600210 gpArgs->fPositionVar.set(kFloat2_GrSLType, "octocoord");
211 this->emitTransforms(v, varyingHandler, uniHandler, gpArgs->fPositionVar, proc.localMatrix(),
212 args.fFPCoordTransformHandler);
Chris Dalton1a325d22017-07-14 15:17:41 -0600213
214 // Fragment shader.
Chris Dalton60283612018-02-14 13:38:14 -0700215 GrGLSLFPFragmentBuilder* f = args.fFragBuilder;
Chris Dalton1a325d22017-07-14 15:17:41 -0600216
Chris Daltondaef06a2018-05-23 17:11:09 -0600217 // Look up coverage count in the atlas.
218 f->codeAppend ("half coverage = ");
219 f->appendTextureLookup(args.fTexSamplers[0], SkStringPrintf("%s.xy", texcoord.fsIn()).c_str(),
220 kFloat2_GrSLType);
Chris Dalton1a325d22017-07-14 15:17:41 -0600221 f->codeAppend (".a;");
222
Chris Daltondaef06a2018-05-23 17:11:09 -0600223 // Scale coverage count by .5. Make it negative for even-odd paths and positive for winding
224 // ones. Clamp winding coverage counts at 1.0 (i.e. min(coverage/2, .5)).
Ethan Nicholase1f55022019-02-05 17:17:40 -0500225 f->codeAppendf("coverage = min(abs(coverage) * half(%s.z), .5);", texcoord.fsIn());
Chris Daltondaef06a2018-05-23 17:11:09 -0600226
227 // For negative values, this finishes the even-odd sawtooth function. Since positive (winding)
228 // values were clamped at "coverage/2 = .5", this only undoes the previous multiply by .5.
229 f->codeAppend ("coverage = 1 - abs(fract(coverage) * 2 - 1);");
230
231 f->codeAppendf("%s = half4(coverage);", args.fOutputCoverage);
Chris Dalton1a325d22017-07-14 15:17:41 -0600232}