blob: 55cbcaba6e82435f66eb168bf5a236afb42a2c8a [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
10#include "GrOnFlushResourceProvider.h"
11#include "GrTexture.h"
12#include "glsl/GrGLSLFragmentShaderBuilder.h"
13#include "glsl/GrGLSLGeometryProcessor.h"
14#include "glsl/GrGLSLProgramBuilder.h"
15#include "glsl/GrGLSLVarying.h"
16
17// Slightly undershoot an AA bloat radius of 0.5 so vertices that fall on integer boundaries don't
18// accidentally reach into neighboring path masks within the atlas.
19constexpr float kAABloatRadius = 0.491111f;
20
21// Paths are drawn as octagons. Each point on the octagon is the intersection of two lines: one edge
22// from the path's bounding box and one edge from its 45-degree bounding box. The below inputs
23// define a vertex by the two edges that need to be intersected. Normals point out of the octagon,
24// and the bounding boxes are sent in as instance attribs.
25static constexpr float kOctoEdgeNorms[8 * 4] = {
26 // bbox // bbox45
27 -1, 0, -1,+1,
28 -1, 0, -1,-1,
29 0,-1, -1,-1,
30 0,-1, +1,-1,
31 +1, 0, +1,-1,
32 +1, 0, +1,+1,
33 0,+1, +1,+1,
34 0,+1, -1,+1,
35};
36
37GR_DECLARE_STATIC_UNIQUE_KEY(gVertexBufferKey);
38
Chris Dalton383a2ef2018-01-08 17:21:41 -050039sk_sp<const GrBuffer> GrCCPathProcessor::FindVertexBuffer(GrOnFlushResourceProvider* onFlushRP) {
Chris Dalton5d2de082017-12-19 10:40:23 -070040 GR_DEFINE_STATIC_UNIQUE_KEY(gVertexBufferKey);
41 return onFlushRP->findOrMakeStaticBuffer(kVertex_GrBufferType, sizeof(kOctoEdgeNorms),
42 kOctoEdgeNorms, gVertexBufferKey);
43}
44
Chris Dalton27059d32018-01-23 14:06:50 -070045static constexpr uint16_t kRestartStrip = 0xffff;
46
47static constexpr uint16_t kOctoIndicesAsStrips[] = {
48 1, 0, 2, 4, 3, kRestartStrip, // First half.
49 5, 4, 6, 0, 7 // Second half.
50};
51
52static constexpr uint16_t kOctoIndicesAsTris[] = {
53 // First half.
54 1, 0, 2,
Chris Dalton1a325d22017-07-14 15:17:41 -060055 0, 4, 2,
Chris Dalton1a325d22017-07-14 15:17:41 -060056 2, 4, 3,
Chris Dalton27059d32018-01-23 14:06:50 -070057
58 // Second half.
59 5, 4, 6,
60 4, 0, 6,
Chris Dalton1a325d22017-07-14 15:17:41 -060061 6, 0, 7,
62};
63
64GR_DECLARE_STATIC_UNIQUE_KEY(gIndexBufferKey);
65
Chris Dalton383a2ef2018-01-08 17:21:41 -050066sk_sp<const GrBuffer> GrCCPathProcessor::FindIndexBuffer(GrOnFlushResourceProvider* onFlushRP) {
Chris Dalton5d2de082017-12-19 10:40:23 -070067 GR_DEFINE_STATIC_UNIQUE_KEY(gIndexBufferKey);
Chris Dalton27059d32018-01-23 14:06:50 -070068 if (onFlushRP->caps()->usePrimitiveRestart()) {
69 return onFlushRP->findOrMakeStaticBuffer(kIndex_GrBufferType, sizeof(kOctoIndicesAsStrips),
70 kOctoIndicesAsStrips, gIndexBufferKey);
71 } else {
72 return onFlushRP->findOrMakeStaticBuffer(kIndex_GrBufferType, sizeof(kOctoIndicesAsTris),
73 kOctoIndicesAsTris, gIndexBufferKey);
74 }
Chris Dalton5d2de082017-12-19 10:40:23 -070075}
76
Chris Dalton27059d32018-01-23 14:06:50 -070077int GrCCPathProcessor::NumIndicesPerInstance(const GrCaps& caps) {
78 return caps.usePrimitiveRestart() ? SK_ARRAY_COUNT(kOctoIndicesAsStrips)
79 : SK_ARRAY_COUNT(kOctoIndicesAsTris);
80}
81
82GrCCPathProcessor::GrCCPathProcessor(GrResourceProvider* resourceProvider,
83 sk_sp<GrTextureProxy> atlas, SkPath::FillType fillType)
Chris Dalton383a2ef2018-01-08 17:21:41 -050084 : INHERITED(kGrCCPathProcessor_ClassID)
Chris Daltona3e92712017-12-04 11:45:51 -070085 , fFillType(fillType)
86 , fAtlasAccess(std::move(atlas), GrSamplerState::Filter::kNearest,
87 GrSamplerState::WrapMode::kClamp, kFragment_GrShaderFlag) {
Ethan Nicholasfa7ee242017-09-25 09:52:04 -040088 this->addInstanceAttrib("devbounds", kFloat4_GrVertexAttribType);
89 this->addInstanceAttrib("devbounds45", kFloat4_GrVertexAttribType);
90 this->addInstanceAttrib("view_matrix", kFloat4_GrVertexAttribType);
91 this->addInstanceAttrib("view_translate", kFloat2_GrVertexAttribType);
Chris Daltona045eea2017-10-24 13:22:10 -060092 this->addInstanceAttrib("atlas_offset", kShort2_GrVertexAttribType);
Ethan Nicholasfa7ee242017-09-25 09:52:04 -040093 this->addInstanceAttrib("color", kUByte4_norm_GrVertexAttribType);
Chris Dalton1a325d22017-07-14 15:17:41 -060094
95 SkASSERT(offsetof(Instance, fDevBounds) ==
96 this->getInstanceAttrib(InstanceAttribs::kDevBounds).fOffsetInRecord);
97 SkASSERT(offsetof(Instance, fDevBounds45) ==
98 this->getInstanceAttrib(InstanceAttribs::kDevBounds45).fOffsetInRecord);
99 SkASSERT(offsetof(Instance, fViewMatrix) ==
100 this->getInstanceAttrib(InstanceAttribs::kViewMatrix).fOffsetInRecord);
101 SkASSERT(offsetof(Instance, fViewTranslate) ==
102 this->getInstanceAttrib(InstanceAttribs::kViewTranslate).fOffsetInRecord);
103 SkASSERT(offsetof(Instance, fAtlasOffset) ==
104 this->getInstanceAttrib(InstanceAttribs::kAtlasOffset).fOffsetInRecord);
105 SkASSERT(offsetof(Instance, fColor) ==
106 this->getInstanceAttrib(InstanceAttribs::kColor).fOffsetInRecord);
107 SkASSERT(sizeof(Instance) == this->getInstanceStride());
108
109 GR_STATIC_ASSERT(6 == kNumInstanceAttribs);
110
Ethan Nicholasfa7ee242017-09-25 09:52:04 -0400111 this->addVertexAttrib("edge_norms", kFloat4_GrVertexAttribType);
Chris Dalton1a325d22017-07-14 15:17:41 -0600112
Chris Dalton27059d32018-01-23 14:06:50 -0700113 fAtlasAccess.instantiate(resourceProvider);
Chris Dalton1a325d22017-07-14 15:17:41 -0600114 this->addTextureSampler(&fAtlasAccess);
Chris Dalton27059d32018-01-23 14:06:50 -0700115
116 if (resourceProvider->caps()->usePrimitiveRestart()) {
117 this->setWillUsePrimitiveRestart();
118 }
Chris Dalton1a325d22017-07-14 15:17:41 -0600119}
120
Chris Dalton383a2ef2018-01-08 17:21:41 -0500121void GrCCPathProcessor::getGLSLProcessorKey(const GrShaderCaps&, GrProcessorKeyBuilder* b) const {
Robert Phillipse44ef102017-07-21 15:37:19 -0400122 b->add32((fFillType << 16) | this->atlasProxy()->origin());
Chris Dalton1a325d22017-07-14 15:17:41 -0600123}
124
125class GLSLPathProcessor : public GrGLSLGeometryProcessor {
126public:
127 void onEmitCode(EmitArgs& args, GrGPArgs* gpArgs) override;
128
129private:
130 void setData(const GrGLSLProgramDataManager& pdman, const GrPrimitiveProcessor& primProc,
131 FPCoordTransformIter&& transformIter) override {
Chris Dalton383a2ef2018-01-08 17:21:41 -0500132 const GrCCPathProcessor& proc = primProc.cast<GrCCPathProcessor>();
Chris Dalton1a325d22017-07-14 15:17:41 -0600133 pdman.set2f(fAtlasAdjustUniform, 1.0f / proc.atlas()->width(),
134 1.0f / proc.atlas()->height());
135 this->setTransformDataHelper(SkMatrix::I(), pdman, &transformIter);
136 }
137
138 GrGLSLUniformHandler::UniformHandle fAtlasAdjustUniform;
139
140 typedef GrGLSLGeometryProcessor INHERITED;
141};
142
Chris Dalton383a2ef2018-01-08 17:21:41 -0500143GrGLSLPrimitiveProcessor* GrCCPathProcessor::createGLSLInstance(const GrShaderCaps&) const {
Chris Dalton1a325d22017-07-14 15:17:41 -0600144 return new GLSLPathProcessor();
145}
146
147void GLSLPathProcessor::onEmitCode(EmitArgs& args, GrGPArgs* gpArgs) {
Chris Dalton383a2ef2018-01-08 17:21:41 -0500148 using InstanceAttribs = GrCCPathProcessor::InstanceAttribs;
Chris Dalton7b046312018-02-02 11:06:30 -0700149 using Interpolation = GrGLSLVaryingHandler::Interpolation;
150
Chris Dalton383a2ef2018-01-08 17:21:41 -0500151 const GrCCPathProcessor& proc = args.fGP.cast<GrCCPathProcessor>();
Chris Dalton1a325d22017-07-14 15:17:41 -0600152 GrGLSLUniformHandler* uniHandler = args.fUniformHandler;
153 GrGLSLVaryingHandler* varyingHandler = args.fVaryingHandler;
154
155 const char* atlasAdjust;
156 fAtlasAdjustUniform = uniHandler->addUniform(
157 kVertex_GrShaderFlag,
Ethan Nicholas8aa45692017-09-20 11:24:15 -0400158 kFloat2_GrSLType, "atlas_adjust", &atlasAdjust);
Chris Dalton1a325d22017-07-14 15:17:41 -0600159
160 varyingHandler->emitAttributes(proc);
161
Chris Dalton27372882017-12-08 13:34:21 -0700162 GrGLSLVarying texcoord(kFloat2_GrSLType);
163 GrGLSLVarying color(kHalf4_GrSLType);
Chris Daltonfdde34e2017-10-16 14:15:26 -0600164 varyingHandler->addVarying("texcoord", &texcoord);
Chris Dalton7b046312018-02-02 11:06:30 -0700165 varyingHandler->addPassThroughAttribute(&proc.getInstanceAttrib(InstanceAttribs::kColor),
166 args.fOutputColor, Interpolation::kCanBeFlat);
Chris Dalton1a325d22017-07-14 15:17:41 -0600167
Chris Daltond0b8d932017-12-21 16:48:52 -0700168 // The vertex shader bloats and intersects the devBounds and devBounds45 rectangles, in order to
169 // find an octagon that circumscribes the (bloated) path.
Chris Dalton1a325d22017-07-14 15:17:41 -0600170 GrGLSLVertexBuilder* v = args.fVertBuilder;
171
Chris Daltond0b8d932017-12-21 16:48:52 -0700172 // Each vertex is the intersection of one edge from devBounds and one from devBounds45.
173 // 'N' holds the normals to these edges as column vectors.
174 //
175 // NOTE: "float2x2(float4)" is valid and equivalent to "float2x2(float4.xy, float4.zw)",
176 // however Intel compilers crash when we use the former syntax in this shader.
177 v->codeAppendf("float2x2 N = float2x2(%s.xy, %s.zw);",
178 proc.getEdgeNormsAttrib().fName, proc.getEdgeNormsAttrib().fName);
Chris Dalton1a325d22017-07-14 15:17:41 -0600179
180 // N[0] is the normal for the edge we are intersecting from the regular bounding box, pointing
181 // out of the octagon.
Chris Dalton5cd67002018-04-30 11:04:40 -0600182 v->codeAppendf("float2 refpt = (0 == sk_VertexID >> 2) ? %s.xy : %s.zw;",
Chris Dalton1a325d22017-07-14 15:17:41 -0600183 proc.getInstanceAttrib(InstanceAttribs::kDevBounds).fName,
184 proc.getInstanceAttrib(InstanceAttribs::kDevBounds).fName);
185 v->codeAppendf("refpt += N[0] * %f;", kAABloatRadius); // bloat for AA.
186
187 // N[1] is the normal for the edge we are intersecting from the 45-degree bounding box, pointing
188 // out of the octagon.
Chris Dalton5cd67002018-04-30 11:04:40 -0600189 v->codeAppendf("float2 refpt45 = (0 == ((sk_VertexID + 1) & (1 << 2))) ? %s.xy : %s.zw;",
Chris Dalton1a325d22017-07-14 15:17:41 -0600190 proc.getInstanceAttrib(InstanceAttribs::kDevBounds45).fName,
191 proc.getInstanceAttrib(InstanceAttribs::kDevBounds45).fName);
Ethan Nicholas8aa45692017-09-20 11:24:15 -0400192 v->codeAppendf("refpt45 *= float2x2(.5,.5,-.5,.5);"); // transform back to device space.
Chris Dalton1a325d22017-07-14 15:17:41 -0600193 v->codeAppendf("refpt45 += N[1] * %f;", kAABloatRadius); // bloat for AA.
194
Ethan Nicholas8aa45692017-09-20 11:24:15 -0400195 v->codeAppend ("float2 K = float2(dot(N[0], refpt), dot(N[1], refpt45));");
196 v->codeAppendf("float2 octocoord = K * inverse(N);");
Chris Dalton1a325d22017-07-14 15:17:41 -0600197
Ethan Nicholas8aa45692017-09-20 11:24:15 -0400198 gpArgs->fPositionVar.set(kFloat2_GrSLType, "octocoord");
Chris Dalton1a325d22017-07-14 15:17:41 -0600199
200 // Convert to atlas coordinates in order to do our texture lookup.
Ethan Nicholas8aa45692017-09-20 11:24:15 -0400201 v->codeAppendf("float2 atlascoord = octocoord + float2(%s);",
Chris Dalton1a325d22017-07-14 15:17:41 -0600202 proc.getInstanceAttrib(InstanceAttribs::kAtlasOffset).fName);
Robert Phillipse44ef102017-07-21 15:37:19 -0400203 if (kTopLeft_GrSurfaceOrigin == proc.atlasProxy()->origin()) {
Chris Dalton1a325d22017-07-14 15:17:41 -0600204 v->codeAppendf("%s = atlascoord * %s;", texcoord.vsOut(), atlasAdjust);
205 } else {
Robert Phillipse44ef102017-07-21 15:37:19 -0400206 SkASSERT(kBottomLeft_GrSurfaceOrigin == proc.atlasProxy()->origin());
Ethan Nicholas8aa45692017-09-20 11:24:15 -0400207 v->codeAppendf("%s = float2(atlascoord.x * %s.x, 1 - atlascoord.y * %s.y);",
Chris Dalton1a325d22017-07-14 15:17:41 -0600208 texcoord.vsOut(), atlasAdjust, atlasAdjust);
209 }
210
Chris Daltond0b8d932017-12-21 16:48:52 -0700211 // Convert to path/local cordinates.
212 v->codeAppendf("float2x2 viewmatrix = float2x2(%s.xy, %s.zw);", // float2x2(float4) busts Intel.
Chris Dalton1a325d22017-07-14 15:17:41 -0600213 proc.getInstanceAttrib(InstanceAttribs::kViewMatrix).fName,
Chris Daltond0b8d932017-12-21 16:48:52 -0700214 proc.getInstanceAttrib(InstanceAttribs::kViewMatrix).fName);
215 v->codeAppendf("float2 pathcoord = inverse(viewmatrix) * (octocoord - %s);",
Chris Dalton1a325d22017-07-14 15:17:41 -0600216 proc.getInstanceAttrib(InstanceAttribs::kViewTranslate).fName);
217
Brian Salomon04460cc2017-12-06 14:47:42 -0500218 this->emitTransforms(v, varyingHandler, uniHandler, GrShaderVar("pathcoord", kFloat2_GrSLType),
Chris Dalton1a325d22017-07-14 15:17:41 -0600219 args.fFPCoordTransformHandler);
220
221 // Fragment shader.
Chris Dalton60283612018-02-14 13:38:14 -0700222 GrGLSLFPFragmentBuilder* f = args.fFragBuilder;
Chris Dalton1a325d22017-07-14 15:17:41 -0600223
Ethan Nicholasf7b88202017-09-18 14:10:39 -0400224 f->codeAppend ("half coverage_count = ");
Ethan Nicholas8aa45692017-09-20 11:24:15 -0400225 f->appendTextureLookup(args.fTexSamplers[0], texcoord.fsIn(), kFloat2_GrSLType);
Chris Dalton1a325d22017-07-14 15:17:41 -0600226 f->codeAppend (".a;");
227
228 if (SkPath::kWinding_FillType == proc.fillType()) {
Ethan Nicholasf7b88202017-09-18 14:10:39 -0400229 f->codeAppendf("%s = half4(min(abs(coverage_count), 1));", args.fOutputCoverage);
Chris Dalton1a325d22017-07-14 15:17:41 -0600230 } else {
231 SkASSERT(SkPath::kEvenOdd_FillType == proc.fillType());
Ethan Nicholasf7b88202017-09-18 14:10:39 -0400232 f->codeAppend ("half t = mod(abs(coverage_count), 2);");
233 f->codeAppendf("%s = half4(1 - abs(t - 1));", args.fOutputCoverage);
Chris Dalton1a325d22017-07-14 15:17:41 -0600234 }
235}