blob: e1a508619ac86c27fd6210d5bf7e03a141e24ed3 [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
19// Slightly undershoot an AA bloat radius of 0.5 so vertices that fall on integer boundaries don't
20// accidentally reach into neighboring path masks within the atlas.
21constexpr float kAABloatRadius = 0.491111f;
22
23// Paths are drawn as octagons. Each point on the octagon is the intersection of two lines: one edge
24// from the path's bounding box and one edge from its 45-degree bounding box. The below inputs
25// define a vertex by the two edges that need to be intersected. Normals point out of the octagon,
26// and the bounding boxes are sent in as instance attribs.
27static constexpr float kOctoEdgeNorms[8 * 4] = {
28 // bbox // bbox45
29 -1, 0, -1,+1,
30 -1, 0, -1,-1,
31 0,-1, -1,-1,
32 0,-1, +1,-1,
33 +1, 0, +1,-1,
34 +1, 0, +1,+1,
35 0,+1, +1,+1,
36 0,+1, -1,+1,
37};
38
39GR_DECLARE_STATIC_UNIQUE_KEY(gVertexBufferKey);
40
Chris Dalton383a2ef2018-01-08 17:21:41 -050041sk_sp<const GrBuffer> GrCCPathProcessor::FindVertexBuffer(GrOnFlushResourceProvider* onFlushRP) {
Chris Dalton5d2de082017-12-19 10:40:23 -070042 GR_DEFINE_STATIC_UNIQUE_KEY(gVertexBufferKey);
43 return onFlushRP->findOrMakeStaticBuffer(kVertex_GrBufferType, sizeof(kOctoEdgeNorms),
44 kOctoEdgeNorms, gVertexBufferKey);
45}
46
Chris Dalton27059d32018-01-23 14:06:50 -070047static constexpr uint16_t kRestartStrip = 0xffff;
48
49static constexpr uint16_t kOctoIndicesAsStrips[] = {
50 1, 0, 2, 4, 3, kRestartStrip, // First half.
51 5, 4, 6, 0, 7 // Second half.
52};
53
54static constexpr uint16_t kOctoIndicesAsTris[] = {
55 // First half.
56 1, 0, 2,
Chris Dalton1a325d22017-07-14 15:17:41 -060057 0, 4, 2,
Chris Dalton1a325d22017-07-14 15:17:41 -060058 2, 4, 3,
Chris Dalton27059d32018-01-23 14:06:50 -070059
60 // Second half.
61 5, 4, 6,
62 4, 0, 6,
Chris Dalton1a325d22017-07-14 15:17:41 -060063 6, 0, 7,
64};
65
66GR_DECLARE_STATIC_UNIQUE_KEY(gIndexBufferKey);
67
Brian Salomon92be2f72018-06-19 14:33:47 -040068constexpr GrPrimitiveProcessor::Attribute GrCCPathProcessor::kInstanceAttribs[];
69constexpr GrPrimitiveProcessor::Attribute GrCCPathProcessor::kEdgeNormsAttrib;
70
Chris Dalton383a2ef2018-01-08 17:21:41 -050071sk_sp<const GrBuffer> GrCCPathProcessor::FindIndexBuffer(GrOnFlushResourceProvider* onFlushRP) {
Chris Dalton5d2de082017-12-19 10:40:23 -070072 GR_DEFINE_STATIC_UNIQUE_KEY(gIndexBufferKey);
Chris Dalton27059d32018-01-23 14:06:50 -070073 if (onFlushRP->caps()->usePrimitiveRestart()) {
74 return onFlushRP->findOrMakeStaticBuffer(kIndex_GrBufferType, sizeof(kOctoIndicesAsStrips),
75 kOctoIndicesAsStrips, gIndexBufferKey);
76 } else {
77 return onFlushRP->findOrMakeStaticBuffer(kIndex_GrBufferType, sizeof(kOctoIndicesAsTris),
78 kOctoIndicesAsTris, gIndexBufferKey);
79 }
Chris Dalton5d2de082017-12-19 10:40:23 -070080}
81
Chris Dalton27059d32018-01-23 14:06:50 -070082GrCCPathProcessor::GrCCPathProcessor(GrResourceProvider* resourceProvider,
Chris Daltondaef06a2018-05-23 17:11:09 -060083 sk_sp<GrTextureProxy> atlas,
Chris Dalton1c548942018-05-22 13:09:48 -060084 const SkMatrix& viewMatrixIfUsingLocalCoords)
Chris Dalton383a2ef2018-01-08 17:21:41 -050085 : INHERITED(kGrCCPathProcessor_ClassID)
Chris Daltona3e92712017-12-04 11:45:51 -070086 , fAtlasAccess(std::move(atlas), GrSamplerState::Filter::kNearest,
87 GrSamplerState::WrapMode::kClamp, kFragment_GrShaderFlag) {
Brian Salomon92be2f72018-06-19 14:33:47 -040088 this->setInstanceAttributeCnt(kNumInstanceAttribs);
89 // Check that instance attributes exactly match Instance struct layout.
90 SkASSERT(!strcmp(this->instanceAttribute(0).name(), "devbounds"));
91 SkASSERT(!strcmp(this->instanceAttribute(1).name(), "devbounds45"));
92 SkASSERT(!strcmp(this->instanceAttribute(2).name(), "dev_to_atlas_offset"));
93 SkASSERT(!strcmp(this->instanceAttribute(3).name(), "color"));
94 SkASSERT(this->debugOnly_instanceAttributeOffset(0) == offsetof(Instance, fDevBounds));
95 SkASSERT(this->debugOnly_instanceAttributeOffset(1) == offsetof(Instance, fDevBounds45));
96 SkASSERT(this->debugOnly_instanceAttributeOffset(2) == offsetof(Instance, fDevToAtlasOffset));
97 SkASSERT(this->debugOnly_instanceAttributeOffset(3) == offsetof(Instance, fColor));
98 SkASSERT(this->debugOnly_instanceStride() == sizeof(Instance));
Chris Dalton1a325d22017-07-14 15:17:41 -060099
Brian Salomon92be2f72018-06-19 14:33:47 -0400100 this->setVertexAttributeCnt(1);
Chris Dalton1a325d22017-07-14 15:17:41 -0600101
Chris Dalton27059d32018-01-23 14:06:50 -0700102 fAtlasAccess.instantiate(resourceProvider);
Chris Dalton1a325d22017-07-14 15:17:41 -0600103 this->addTextureSampler(&fAtlasAccess);
Chris Dalton27059d32018-01-23 14:06:50 -0700104
Chris Dalton1c548942018-05-22 13:09:48 -0600105 if (!viewMatrixIfUsingLocalCoords.invert(&fLocalMatrix)) {
106 fLocalMatrix.setIdentity();
Chris Dalton27059d32018-01-23 14:06:50 -0700107 }
Chris Dalton1a325d22017-07-14 15:17:41 -0600108}
109
Chris Dalton1a325d22017-07-14 15:17:41 -0600110class GLSLPathProcessor : public GrGLSLGeometryProcessor {
111public:
112 void onEmitCode(EmitArgs& args, GrGPArgs* gpArgs) override;
113
114private:
115 void setData(const GrGLSLProgramDataManager& pdman, const GrPrimitiveProcessor& primProc,
116 FPCoordTransformIter&& transformIter) override {
Chris Dalton383a2ef2018-01-08 17:21:41 -0500117 const GrCCPathProcessor& proc = primProc.cast<GrCCPathProcessor>();
Chris Dalton1a325d22017-07-14 15:17:41 -0600118 pdman.set2f(fAtlasAdjustUniform, 1.0f / proc.atlas()->width(),
119 1.0f / proc.atlas()->height());
Chris Dalton1c548942018-05-22 13:09:48 -0600120 this->setTransformDataHelper(proc.localMatrix(), pdman, &transformIter);
Chris Dalton1a325d22017-07-14 15:17:41 -0600121 }
122
123 GrGLSLUniformHandler::UniformHandle fAtlasAdjustUniform;
124
125 typedef GrGLSLGeometryProcessor INHERITED;
126};
127
Chris Dalton383a2ef2018-01-08 17:21:41 -0500128GrGLSLPrimitiveProcessor* GrCCPathProcessor::createGLSLInstance(const GrShaderCaps&) const {
Chris Dalton1a325d22017-07-14 15:17:41 -0600129 return new GLSLPathProcessor();
130}
131
Chris Daltond925f2d2018-05-07 19:19:06 -0600132void GrCCPathProcessor::drawPaths(GrOpFlushState* flushState, const GrPipeline& pipeline,
Chris Dalton4da70192018-06-18 09:51:36 -0600133 const GrCCPerFlushResources& resources, int baseInstance,
134 int endInstance, const SkRect& bounds) const {
Chris Daltond925f2d2018-05-07 19:19:06 -0600135 const GrCaps& caps = flushState->caps();
136 GrPrimitiveType primitiveType = caps.usePrimitiveRestart()
137 ? GrPrimitiveType::kTriangleStrip
138 : GrPrimitiveType::kTriangles;
139 int numIndicesPerInstance = caps.usePrimitiveRestart()
140 ? SK_ARRAY_COUNT(kOctoIndicesAsStrips)
141 : SK_ARRAY_COUNT(kOctoIndicesAsTris);
142 GrMesh mesh(primitiveType);
Brian Salomon802cb312018-06-08 18:05:20 -0400143 auto enablePrimitiveRestart = GrPrimitiveRestart(flushState->caps().usePrimitiveRestart());
144
Chris Dalton4da70192018-06-18 09:51:36 -0600145 mesh.setIndexedInstanced(resources.indexBuffer(), numIndicesPerInstance,
146 resources.instanceBuffer(), endInstance - baseInstance, baseInstance,
147 enablePrimitiveRestart);
148 mesh.setVertexData(resources.vertexBuffer());
Chris Daltond925f2d2018-05-07 19:19:06 -0600149
Brian Salomonff168d92018-06-23 15:17:27 -0400150 flushState->rtCommandBuffer()->draw(*this, pipeline, &mesh, nullptr, 1, bounds);
Chris Daltond925f2d2018-05-07 19:19:06 -0600151}
152
Chris Dalton1a325d22017-07-14 15:17:41 -0600153void GLSLPathProcessor::onEmitCode(EmitArgs& args, GrGPArgs* gpArgs) {
Chris Dalton383a2ef2018-01-08 17:21:41 -0500154 using InstanceAttribs = GrCCPathProcessor::InstanceAttribs;
Chris Dalton7b046312018-02-02 11:06:30 -0700155 using Interpolation = GrGLSLVaryingHandler::Interpolation;
156
Chris Dalton383a2ef2018-01-08 17:21:41 -0500157 const GrCCPathProcessor& proc = args.fGP.cast<GrCCPathProcessor>();
Chris Dalton1a325d22017-07-14 15:17:41 -0600158 GrGLSLUniformHandler* uniHandler = args.fUniformHandler;
159 GrGLSLVaryingHandler* varyingHandler = args.fVaryingHandler;
160
161 const char* atlasAdjust;
162 fAtlasAdjustUniform = uniHandler->addUniform(
163 kVertex_GrShaderFlag,
Ethan Nicholas8aa45692017-09-20 11:24:15 -0400164 kFloat2_GrSLType, "atlas_adjust", &atlasAdjust);
Chris Dalton1a325d22017-07-14 15:17:41 -0600165
166 varyingHandler->emitAttributes(proc);
167
Chris Daltondaef06a2018-05-23 17:11:09 -0600168 GrGLSLVarying texcoord(kFloat3_GrSLType);
Chris Dalton27372882017-12-08 13:34:21 -0700169 GrGLSLVarying color(kHalf4_GrSLType);
Chris Daltonfdde34e2017-10-16 14:15:26 -0600170 varyingHandler->addVarying("texcoord", &texcoord);
Brian Salomon92be2f72018-06-19 14:33:47 -0400171 varyingHandler->addPassThroughAttribute(proc.getInstanceAttrib(InstanceAttribs::kColor),
Chris Dalton7b046312018-02-02 11:06:30 -0700172 args.fOutputColor, Interpolation::kCanBeFlat);
Chris Dalton1a325d22017-07-14 15:17:41 -0600173
Chris Daltond0b8d932017-12-21 16:48:52 -0700174 // The vertex shader bloats and intersects the devBounds and devBounds45 rectangles, in order to
175 // find an octagon that circumscribes the (bloated) path.
Chris Dalton1a325d22017-07-14 15:17:41 -0600176 GrGLSLVertexBuilder* v = args.fVertBuilder;
177
Chris Daltond0b8d932017-12-21 16:48:52 -0700178 // Each vertex is the intersection of one edge from devBounds and one from devBounds45.
179 // 'N' holds the normals to these edges as column vectors.
180 //
181 // NOTE: "float2x2(float4)" is valid and equivalent to "float2x2(float4.xy, float4.zw)",
182 // however Intel compilers crash when we use the former syntax in this shader.
Brian Salomon70132d02018-05-29 15:33:06 -0400183 v->codeAppendf("float2x2 N = float2x2(%s.xy, %s.zw);", proc.getEdgeNormsAttrib().name(),
184 proc.getEdgeNormsAttrib().name());
Chris Dalton1a325d22017-07-14 15:17:41 -0600185
186 // N[0] is the normal for the edge we are intersecting from the regular bounding box, pointing
187 // out of the octagon.
Chris Daltondaef06a2018-05-23 17:11:09 -0600188 v->codeAppendf("float4 devbounds = %s;",
Brian Salomon70132d02018-05-29 15:33:06 -0400189 proc.getInstanceAttrib(InstanceAttribs::kDevBounds).name());
Chris Daltondaef06a2018-05-23 17:11:09 -0600190 v->codeAppend ("float2 refpt = (0 == sk_VertexID >> 2)"
191 "? float2(min(devbounds.x, devbounds.z), devbounds.y)"
192 ": float2(max(devbounds.x, devbounds.z), devbounds.w);");
Chris Dalton1a325d22017-07-14 15:17:41 -0600193 v->codeAppendf("refpt += N[0] * %f;", kAABloatRadius); // bloat for AA.
194
195 // N[1] is the normal for the edge we are intersecting from the 45-degree bounding box, pointing
196 // out of the octagon.
Chris Dalton5cd67002018-04-30 11:04:40 -0600197 v->codeAppendf("float2 refpt45 = (0 == ((sk_VertexID + 1) & (1 << 2))) ? %s.xy : %s.zw;",
Brian Salomon70132d02018-05-29 15:33:06 -0400198 proc.getInstanceAttrib(InstanceAttribs::kDevBounds45).name(),
199 proc.getInstanceAttrib(InstanceAttribs::kDevBounds45).name());
Ethan Nicholas8aa45692017-09-20 11:24:15 -0400200 v->codeAppendf("refpt45 *= float2x2(.5,.5,-.5,.5);"); // transform back to device space.
Chris Dalton1a325d22017-07-14 15:17:41 -0600201 v->codeAppendf("refpt45 += N[1] * %f;", kAABloatRadius); // bloat for AA.
202
Ethan Nicholas8aa45692017-09-20 11:24:15 -0400203 v->codeAppend ("float2 K = float2(dot(N[0], refpt), dot(N[1], refpt45));");
204 v->codeAppendf("float2 octocoord = K * inverse(N);");
Chris Dalton1a325d22017-07-14 15:17:41 -0600205
Ethan Nicholas8aa45692017-09-20 11:24:15 -0400206 gpArgs->fPositionVar.set(kFloat2_GrSLType, "octocoord");
Chris Dalton1a325d22017-07-14 15:17:41 -0600207
208 // Convert to atlas coordinates in order to do our texture lookup.
Ethan Nicholas8aa45692017-09-20 11:24:15 -0400209 v->codeAppendf("float2 atlascoord = octocoord + float2(%s);",
Chris Dalton9414c962018-06-14 10:14:50 -0600210 proc.getInstanceAttrib(InstanceAttribs::kDevToAtlasOffset).name());
Robert Phillipse44ef102017-07-21 15:37:19 -0400211 if (kTopLeft_GrSurfaceOrigin == proc.atlasProxy()->origin()) {
Chris Daltondaef06a2018-05-23 17:11:09 -0600212 v->codeAppendf("%s.xy = atlascoord * %s;", texcoord.vsOut(), atlasAdjust);
Chris Dalton1a325d22017-07-14 15:17:41 -0600213 } else {
Robert Phillipse44ef102017-07-21 15:37:19 -0400214 SkASSERT(kBottomLeft_GrSurfaceOrigin == proc.atlasProxy()->origin());
Chris Daltondaef06a2018-05-23 17:11:09 -0600215 v->codeAppendf("%s.xy = float2(atlascoord.x * %s.x, 1 - atlascoord.y * %s.y);",
Chris Dalton1a325d22017-07-14 15:17:41 -0600216 texcoord.vsOut(), atlasAdjust, atlasAdjust);
217 }
Chris Daltondaef06a2018-05-23 17:11:09 -0600218 // The third texture coordinate is -.5 for even-odd paths and +.5 for winding ones.
219 // ("right < left" indicates even-odd fill type.)
220 v->codeAppendf("%s.z = sign(devbounds.z - devbounds.x) * .5;", texcoord.vsOut());
Chris Dalton1a325d22017-07-14 15:17:41 -0600221
Chris Dalton1c548942018-05-22 13:09:48 -0600222 this->emitTransforms(v, varyingHandler, uniHandler, GrShaderVar("octocoord", kFloat2_GrSLType),
223 proc.localMatrix(), args.fFPCoordTransformHandler);
Chris Dalton1a325d22017-07-14 15:17:41 -0600224
225 // Fragment shader.
Chris Dalton60283612018-02-14 13:38:14 -0700226 GrGLSLFPFragmentBuilder* f = args.fFragBuilder;
Chris Dalton1a325d22017-07-14 15:17:41 -0600227
Chris Daltondaef06a2018-05-23 17:11:09 -0600228 // Look up coverage count in the atlas.
229 f->codeAppend ("half coverage = ");
230 f->appendTextureLookup(args.fTexSamplers[0], SkStringPrintf("%s.xy", texcoord.fsIn()).c_str(),
231 kFloat2_GrSLType);
Chris Dalton1a325d22017-07-14 15:17:41 -0600232 f->codeAppend (".a;");
233
Chris Daltondaef06a2018-05-23 17:11:09 -0600234 // Scale coverage count by .5. Make it negative for even-odd paths and positive for winding
235 // ones. Clamp winding coverage counts at 1.0 (i.e. min(coverage/2, .5)).
236 f->codeAppendf("coverage = min(abs(coverage) * %s.z, .5);", texcoord.fsIn());
237
238 // For negative values, this finishes the even-odd sawtooth function. Since positive (winding)
239 // values were clamped at "coverage/2 = .5", this only undoes the previous multiply by .5.
240 f->codeAppend ("coverage = 1 - abs(fract(coverage) * 2 - 1);");
241
242 f->codeAppendf("%s = half4(coverage);", args.fOutputCoverage);
Chris Dalton1a325d22017-07-14 15:17:41 -0600243}