blob: 81c57dde87d8b2011005e304e7dae25f5a93a84a [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
Chris Dalton383a2ef2018-01-08 17:21:41 -05008#include "GrCCPathProcessor.h"
Chris Dalton1a325d22017-07-14 15:17:41 -06009
Chris Daltond925f2d2018-05-07 19:19:06 -060010#include "GrGpuCommandBuffer.h"
Chris Dalton1a325d22017-07-14 15:17:41 -060011#include "GrOnFlushResourceProvider.h"
12#include "GrTexture.h"
Chris Dalton4da70192018-06-18 09:51:36 -060013#include "ccpr/GrCCPerFlushResources.h"
Chris Dalton1a325d22017-07-14 15:17:41 -060014#include "glsl/GrGLSLFragmentShaderBuilder.h"
15#include "glsl/GrGLSLGeometryProcessor.h"
16#include "glsl/GrGLSLProgramBuilder.h"
17#include "glsl/GrGLSLVarying.h"
18
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
20// from the path's bounding box and one edge from its 45-degree bounding box. The below inputs
21// define a vertex by the two edges that need to be intersected. Normals point out of the octagon,
22// and the bounding boxes are sent in as instance attribs.
23static constexpr float kOctoEdgeNorms[8 * 4] = {
24 // bbox // bbox45
25 -1, 0, -1,+1,
26 -1, 0, -1,-1,
27 0,-1, -1,-1,
28 0,-1, +1,-1,
29 +1, 0, +1,-1,
30 +1, 0, +1,+1,
31 0,+1, +1,+1,
32 0,+1, -1,+1,
33};
34
35GR_DECLARE_STATIC_UNIQUE_KEY(gVertexBufferKey);
36
Chris Dalton383a2ef2018-01-08 17:21:41 -050037sk_sp<const GrBuffer> GrCCPathProcessor::FindVertexBuffer(GrOnFlushResourceProvider* onFlushRP) {
Chris Dalton5d2de082017-12-19 10:40:23 -070038 GR_DEFINE_STATIC_UNIQUE_KEY(gVertexBufferKey);
39 return onFlushRP->findOrMakeStaticBuffer(kVertex_GrBufferType, sizeof(kOctoEdgeNorms),
40 kOctoEdgeNorms, gVertexBufferKey);
41}
42
Chris Dalton27059d32018-01-23 14:06:50 -070043static constexpr uint16_t kRestartStrip = 0xffff;
44
45static constexpr uint16_t kOctoIndicesAsStrips[] = {
46 1, 0, 2, 4, 3, kRestartStrip, // First half.
47 5, 4, 6, 0, 7 // Second half.
48};
49
50static constexpr uint16_t kOctoIndicesAsTris[] = {
51 // First half.
52 1, 0, 2,
Chris Dalton1a325d22017-07-14 15:17:41 -060053 0, 4, 2,
Chris Dalton1a325d22017-07-14 15:17:41 -060054 2, 4, 3,
Chris Dalton27059d32018-01-23 14:06:50 -070055
56 // Second half.
57 5, 4, 6,
58 4, 0, 6,
Chris Dalton1a325d22017-07-14 15:17:41 -060059 6, 0, 7,
60};
61
62GR_DECLARE_STATIC_UNIQUE_KEY(gIndexBufferKey);
63
Brian Salomon92be2f72018-06-19 14:33:47 -040064constexpr GrPrimitiveProcessor::Attribute GrCCPathProcessor::kInstanceAttribs[];
65constexpr GrPrimitiveProcessor::Attribute GrCCPathProcessor::kEdgeNormsAttrib;
66
Chris Dalton383a2ef2018-01-08 17:21:41 -050067sk_sp<const GrBuffer> GrCCPathProcessor::FindIndexBuffer(GrOnFlushResourceProvider* onFlushRP) {
Chris Dalton5d2de082017-12-19 10:40:23 -070068 GR_DEFINE_STATIC_UNIQUE_KEY(gIndexBufferKey);
Chris Dalton27059d32018-01-23 14:06:50 -070069 if (onFlushRP->caps()->usePrimitiveRestart()) {
70 return onFlushRP->findOrMakeStaticBuffer(kIndex_GrBufferType, sizeof(kOctoIndicesAsStrips),
71 kOctoIndicesAsStrips, gIndexBufferKey);
72 } else {
73 return onFlushRP->findOrMakeStaticBuffer(kIndex_GrBufferType, sizeof(kOctoIndicesAsTris),
74 kOctoIndicesAsTris, gIndexBufferKey);
75 }
Chris Dalton5d2de082017-12-19 10:40:23 -070076}
77
Brian Salomon7eae3e02018-08-07 14:02:38 +000078GrCCPathProcessor::GrCCPathProcessor(const GrTextureProxy* atlas,
Chris Dalton1c548942018-05-22 13:09:48 -060079 const SkMatrix& viewMatrixIfUsingLocalCoords)
Chris Dalton383a2ef2018-01-08 17:21:41 -050080 : INHERITED(kGrCCPathProcessor_ClassID)
Brian Salomon7eae3e02018-08-07 14:02:38 +000081 , fAtlasAccess(atlas->textureType(), atlas->config(), GrSamplerState::Filter::kNearest,
Greg Daniel0f70be82018-10-08 17:35:08 +000082 GrSamplerState::WrapMode::kClamp)
Brian Salomon7eae3e02018-08-07 14:02:38 +000083 , fAtlasSize(atlas->isize())
84 , fAtlasOrigin(atlas->origin()) {
85 // TODO: Can we just assert that atlas has GrCCAtlas::kTextureOrigin and remove fAtlasOrigin?
Brian Osmanf04fb3c2018-11-12 15:34:00 -050086 this->setInstanceAttributes(kInstanceAttribs, kNumInstanceAttribs);
87 SkASSERT(this->instanceStride() == sizeof(Instance));
Chris Dalton1a325d22017-07-14 15:17:41 -060088
Brian Osmanf04fb3c2018-11-12 15:34:00 -050089 this->setVertexAttributes(&kEdgeNormsAttrib, 1);
Brian Salomonf7dcd762018-07-30 14:48:15 -040090 this->setTextureSamplerCnt(1);
Chris Dalton27059d32018-01-23 14:06:50 -070091
Chris Dalton1c548942018-05-22 13:09:48 -060092 if (!viewMatrixIfUsingLocalCoords.invert(&fLocalMatrix)) {
93 fLocalMatrix.setIdentity();
Chris Dalton27059d32018-01-23 14:06:50 -070094 }
Chris Dalton1a325d22017-07-14 15:17:41 -060095}
96
Chris Dalton1a325d22017-07-14 15:17:41 -060097class GLSLPathProcessor : public GrGLSLGeometryProcessor {
98public:
99 void onEmitCode(EmitArgs& args, GrGPArgs* gpArgs) override;
100
101private:
102 void setData(const GrGLSLProgramDataManager& pdman, const GrPrimitiveProcessor& primProc,
103 FPCoordTransformIter&& transformIter) override {
Chris Dalton383a2ef2018-01-08 17:21:41 -0500104 const GrCCPathProcessor& proc = primProc.cast<GrCCPathProcessor>();
Brian Salomon7eae3e02018-08-07 14:02:38 +0000105 pdman.set2f(fAtlasAdjustUniform, 1.0f / proc.atlasSize().fWidth,
106 1.0f / proc.atlasSize().fHeight);
Chris Dalton1c548942018-05-22 13:09:48 -0600107 this->setTransformDataHelper(proc.localMatrix(), pdman, &transformIter);
Chris Dalton1a325d22017-07-14 15:17:41 -0600108 }
109
110 GrGLSLUniformHandler::UniformHandle fAtlasAdjustUniform;
111
112 typedef GrGLSLGeometryProcessor INHERITED;
113};
114
Chris Dalton383a2ef2018-01-08 17:21:41 -0500115GrGLSLPrimitiveProcessor* GrCCPathProcessor::createGLSLInstance(const GrShaderCaps&) const {
Chris Dalton1a325d22017-07-14 15:17:41 -0600116 return new GLSLPathProcessor();
117}
118
Chris Daltond925f2d2018-05-07 19:19:06 -0600119void GrCCPathProcessor::drawPaths(GrOpFlushState* flushState, const GrPipeline& pipeline,
Brian Salomon49348902018-06-26 09:12:38 -0400120 const GrPipeline::FixedDynamicState* fixedDynamicState,
Chris Dalton4da70192018-06-18 09:51:36 -0600121 const GrCCPerFlushResources& resources, int baseInstance,
122 int endInstance, const SkRect& bounds) const {
Chris Daltond925f2d2018-05-07 19:19:06 -0600123 const GrCaps& caps = flushState->caps();
124 GrPrimitiveType primitiveType = caps.usePrimitiveRestart()
125 ? GrPrimitiveType::kTriangleStrip
126 : GrPrimitiveType::kTriangles;
127 int numIndicesPerInstance = caps.usePrimitiveRestart()
128 ? SK_ARRAY_COUNT(kOctoIndicesAsStrips)
129 : SK_ARRAY_COUNT(kOctoIndicesAsTris);
130 GrMesh mesh(primitiveType);
Brian Salomon802cb312018-06-08 18:05:20 -0400131 auto enablePrimitiveRestart = GrPrimitiveRestart(flushState->caps().usePrimitiveRestart());
132
Brian Salomon12d22642019-01-29 14:38:50 -0500133 mesh.setIndexedInstanced(resources.refIndexBuffer(), numIndicesPerInstance,
134 resources.refInstanceBuffer(), endInstance - baseInstance,
135 baseInstance, enablePrimitiveRestart);
136 mesh.setVertexData(resources.refVertexBuffer());
Chris Daltond925f2d2018-05-07 19:19:06 -0600137
Brian Salomon49348902018-06-26 09:12:38 -0400138 flushState->rtCommandBuffer()->draw(*this, pipeline, fixedDynamicState, nullptr, &mesh, 1,
139 bounds);
Chris Daltond925f2d2018-05-07 19:19:06 -0600140}
141
Chris Dalton1a325d22017-07-14 15:17:41 -0600142void GLSLPathProcessor::onEmitCode(EmitArgs& args, GrGPArgs* gpArgs) {
Chris Dalton383a2ef2018-01-08 17:21:41 -0500143 using InstanceAttribs = GrCCPathProcessor::InstanceAttribs;
Chris Dalton7b046312018-02-02 11:06:30 -0700144 using Interpolation = GrGLSLVaryingHandler::Interpolation;
145
Chris Dalton383a2ef2018-01-08 17:21:41 -0500146 const GrCCPathProcessor& proc = args.fGP.cast<GrCCPathProcessor>();
Chris Dalton1a325d22017-07-14 15:17:41 -0600147 GrGLSLUniformHandler* uniHandler = args.fUniformHandler;
148 GrGLSLVaryingHandler* varyingHandler = args.fVaryingHandler;
149
150 const char* atlasAdjust;
151 fAtlasAdjustUniform = uniHandler->addUniform(
152 kVertex_GrShaderFlag,
Ethan Nicholas8aa45692017-09-20 11:24:15 -0400153 kFloat2_GrSLType, "atlas_adjust", &atlasAdjust);
Chris Dalton1a325d22017-07-14 15:17:41 -0600154
155 varyingHandler->emitAttributes(proc);
156
Chris Daltondaef06a2018-05-23 17:11:09 -0600157 GrGLSLVarying texcoord(kFloat3_GrSLType);
Chris Dalton27372882017-12-08 13:34:21 -0700158 GrGLSLVarying color(kHalf4_GrSLType);
Chris Daltonfdde34e2017-10-16 14:15:26 -0600159 varyingHandler->addVarying("texcoord", &texcoord);
Brian Salomon92be2f72018-06-19 14:33:47 -0400160 varyingHandler->addPassThroughAttribute(proc.getInstanceAttrib(InstanceAttribs::kColor),
Chris Dalton7b046312018-02-02 11:06:30 -0700161 args.fOutputColor, Interpolation::kCanBeFlat);
Chris Dalton1a325d22017-07-14 15:17:41 -0600162
Chris Daltond0b8d932017-12-21 16:48:52 -0700163 // The vertex shader bloats and intersects the devBounds and devBounds45 rectangles, in order to
164 // find an octagon that circumscribes the (bloated) path.
Chris Dalton1a325d22017-07-14 15:17:41 -0600165 GrGLSLVertexBuilder* v = args.fVertBuilder;
166
Chris Daltond0b8d932017-12-21 16:48:52 -0700167 // Each vertex is the intersection of one edge from devBounds and one from devBounds45.
168 // 'N' holds the normals to these edges as column vectors.
169 //
170 // NOTE: "float2x2(float4)" is valid and equivalent to "float2x2(float4.xy, float4.zw)",
171 // however Intel compilers crash when we use the former syntax in this shader.
Brian Salomon70132d02018-05-29 15:33:06 -0400172 v->codeAppendf("float2x2 N = float2x2(%s.xy, %s.zw);", proc.getEdgeNormsAttrib().name(),
173 proc.getEdgeNormsAttrib().name());
Chris Dalton1a325d22017-07-14 15:17:41 -0600174
175 // N[0] is the normal for the edge we are intersecting from the regular bounding box, pointing
176 // out of the octagon.
Chris Daltondaef06a2018-05-23 17:11:09 -0600177 v->codeAppendf("float4 devbounds = %s;",
Brian Salomon70132d02018-05-29 15:33:06 -0400178 proc.getInstanceAttrib(InstanceAttribs::kDevBounds).name());
Chris Daltondaef06a2018-05-23 17:11:09 -0600179 v->codeAppend ("float2 refpt = (0 == sk_VertexID >> 2)"
180 "? float2(min(devbounds.x, devbounds.z), devbounds.y)"
181 ": float2(max(devbounds.x, devbounds.z), devbounds.w);");
Chris Dalton1a325d22017-07-14 15:17:41 -0600182
183 // N[1] is the normal for the edge we are intersecting from the 45-degree bounding box, pointing
184 // out of the octagon.
Chris Dalton5cd67002018-04-30 11:04:40 -0600185 v->codeAppendf("float2 refpt45 = (0 == ((sk_VertexID + 1) & (1 << 2))) ? %s.xy : %s.zw;",
Brian Salomon70132d02018-05-29 15:33:06 -0400186 proc.getInstanceAttrib(InstanceAttribs::kDevBounds45).name(),
187 proc.getInstanceAttrib(InstanceAttribs::kDevBounds45).name());
Ethan Nicholas8aa45692017-09-20 11:24:15 -0400188 v->codeAppendf("refpt45 *= float2x2(.5,.5,-.5,.5);"); // transform back to device space.
Chris Dalton1a325d22017-07-14 15:17:41 -0600189
Ethan Nicholas8aa45692017-09-20 11:24:15 -0400190 v->codeAppend ("float2 K = float2(dot(N[0], refpt), dot(N[1], refpt45));");
191 v->codeAppendf("float2 octocoord = K * inverse(N);");
Chris Dalton1a325d22017-07-14 15:17:41 -0600192
Chris Dalton51e8b7e2018-10-09 21:18:45 -0600193 // Round the octagon out to ensure we rasterize every pixel the path might touch. (Positive
194 // bloatdir means we should take the "ceil" and negative means to take the "floor".)
195 //
196 // NOTE: If we were just drawing a rect, ceil/floor would be enough. But since there are also
197 // diagonals in the octagon that cross through pixel centers, we need to outset by another
198 // quarter px to ensure those pixels get rasterized.
199 v->codeAppend ("float2 bloatdir = (0 != N[0].x) "
200 "? half2(N[0].x, N[1].y) : half2(N[1].x, N[0].y);");
201 v->codeAppend ("octocoord = (ceil(octocoord * bloatdir - 1e-4) + 0.25) * bloatdir;");
202
Ethan Nicholas8aa45692017-09-20 11:24:15 -0400203 gpArgs->fPositionVar.set(kFloat2_GrSLType, "octocoord");
Chris Dalton1a325d22017-07-14 15:17:41 -0600204
205 // Convert to atlas coordinates in order to do our texture lookup.
Ethan Nicholas8aa45692017-09-20 11:24:15 -0400206 v->codeAppendf("float2 atlascoord = octocoord + float2(%s);",
Chris Dalton9414c962018-06-14 10:14:50 -0600207 proc.getInstanceAttrib(InstanceAttribs::kDevToAtlasOffset).name());
Brian Salomon7eae3e02018-08-07 14:02:38 +0000208 if (kTopLeft_GrSurfaceOrigin == proc.atlasOrigin()) {
Chris Daltondaef06a2018-05-23 17:11:09 -0600209 v->codeAppendf("%s.xy = atlascoord * %s;", texcoord.vsOut(), atlasAdjust);
Chris Dalton1a325d22017-07-14 15:17:41 -0600210 } else {
Brian Salomon7eae3e02018-08-07 14:02:38 +0000211 SkASSERT(kBottomLeft_GrSurfaceOrigin == proc.atlasOrigin());
Chris Daltondaef06a2018-05-23 17:11:09 -0600212 v->codeAppendf("%s.xy = float2(atlascoord.x * %s.x, 1 - atlascoord.y * %s.y);",
Chris Dalton1a325d22017-07-14 15:17:41 -0600213 texcoord.vsOut(), atlasAdjust, atlasAdjust);
214 }
Chris Daltondaef06a2018-05-23 17:11:09 -0600215 // The third texture coordinate is -.5 for even-odd paths and +.5 for winding ones.
216 // ("right < left" indicates even-odd fill type.)
217 v->codeAppendf("%s.z = sign(devbounds.z - devbounds.x) * .5;", texcoord.vsOut());
Chris Dalton1a325d22017-07-14 15:17:41 -0600218
Chris Dalton1c548942018-05-22 13:09:48 -0600219 this->emitTransforms(v, varyingHandler, uniHandler, GrShaderVar("octocoord", kFloat2_GrSLType),
220 proc.localMatrix(), args.fFPCoordTransformHandler);
Chris Dalton1a325d22017-07-14 15:17:41 -0600221
222 // Fragment shader.
Chris Dalton60283612018-02-14 13:38:14 -0700223 GrGLSLFPFragmentBuilder* f = args.fFragBuilder;
Chris Dalton1a325d22017-07-14 15:17:41 -0600224
Chris Daltondaef06a2018-05-23 17:11:09 -0600225 // Look up coverage count in the atlas.
226 f->codeAppend ("half coverage = ");
227 f->appendTextureLookup(args.fTexSamplers[0], SkStringPrintf("%s.xy", texcoord.fsIn()).c_str(),
228 kFloat2_GrSLType);
Chris Dalton1a325d22017-07-14 15:17:41 -0600229 f->codeAppend (".a;");
230
Chris Daltondaef06a2018-05-23 17:11:09 -0600231 // Scale coverage count by .5. Make it negative for even-odd paths and positive for winding
232 // ones. Clamp winding coverage counts at 1.0 (i.e. min(coverage/2, .5)).
233 f->codeAppendf("coverage = min(abs(coverage) * %s.z, .5);", texcoord.fsIn());
234
235 // For negative values, this finishes the even-odd sawtooth function. Since positive (winding)
236 // values were clamped at "coverage/2 = .5", this only undoes the previous multiply by .5.
237 f->codeAppend ("coverage = 1 - abs(fract(coverage) * 2 - 1);");
238
239 f->codeAppendf("%s = half4(coverage);", args.fOutputCoverage);
Chris Dalton1a325d22017-07-14 15:17:41 -0600240}