1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
|
/*
* Copyright 2017 Google Inc.
*
* Use of this source code is governed by a BSD-style license that can be
* found in the LICENSE file.
*/
#include "GrCCPathProcessor.h"
#include "GrGpuCommandBuffer.h"
#include "GrOnFlushResourceProvider.h"
#include "GrTexture.h"
#include "ccpr/GrCCPerFlushResources.h"
#include "glsl/GrGLSLFragmentShaderBuilder.h"
#include "glsl/GrGLSLGeometryProcessor.h"
#include "glsl/GrGLSLProgramBuilder.h"
#include "glsl/GrGLSLVarying.h"
// Slightly undershoot an AA bloat radius of 0.5 so vertices that fall on integer boundaries don't
// accidentally reach into neighboring path masks within the atlas.
constexpr float kAABloatRadius = 0.491111f;
// Paths are drawn as octagons. Each point on the octagon is the intersection of two lines: one edge
// from the path's bounding box and one edge from its 45-degree bounding box. The below inputs
// define a vertex by the two edges that need to be intersected. Normals point out of the octagon,
// and the bounding boxes are sent in as instance attribs.
static constexpr float kOctoEdgeNorms[8 * 4] = {
// bbox // bbox45
-1, 0, -1,+1,
-1, 0, -1,-1,
0,-1, -1,-1,
0,-1, +1,-1,
+1, 0, +1,-1,
+1, 0, +1,+1,
0,+1, +1,+1,
0,+1, -1,+1,
};
GR_DECLARE_STATIC_UNIQUE_KEY(gVertexBufferKey);
sk_sp<const GrBuffer> GrCCPathProcessor::FindVertexBuffer(GrOnFlushResourceProvider* onFlushRP) {
GR_DEFINE_STATIC_UNIQUE_KEY(gVertexBufferKey);
return onFlushRP->findOrMakeStaticBuffer(kVertex_GrBufferType, sizeof(kOctoEdgeNorms),
kOctoEdgeNorms, gVertexBufferKey);
}
static constexpr uint16_t kRestartStrip = 0xffff;
static constexpr uint16_t kOctoIndicesAsStrips[] = {
1, 0, 2, 4, 3, kRestartStrip, // First half.
5, 4, 6, 0, 7 // Second half.
};
static constexpr uint16_t kOctoIndicesAsTris[] = {
// First half.
1, 0, 2,
0, 4, 2,
2, 4, 3,
// Second half.
5, 4, 6,
4, 0, 6,
6, 0, 7,
};
GR_DECLARE_STATIC_UNIQUE_KEY(gIndexBufferKey);
sk_sp<const GrBuffer> GrCCPathProcessor::FindIndexBuffer(GrOnFlushResourceProvider* onFlushRP) {
GR_DEFINE_STATIC_UNIQUE_KEY(gIndexBufferKey);
if (onFlushRP->caps()->usePrimitiveRestart()) {
return onFlushRP->findOrMakeStaticBuffer(kIndex_GrBufferType, sizeof(kOctoIndicesAsStrips),
kOctoIndicesAsStrips, gIndexBufferKey);
} else {
return onFlushRP->findOrMakeStaticBuffer(kIndex_GrBufferType, sizeof(kOctoIndicesAsTris),
kOctoIndicesAsTris, gIndexBufferKey);
}
}
GrCCPathProcessor::GrCCPathProcessor(GrResourceProvider* resourceProvider,
sk_sp<GrTextureProxy> atlas,
const SkMatrix& viewMatrixIfUsingLocalCoords)
: INHERITED(kGrCCPathProcessor_ClassID)
, fAtlasAccess(std::move(atlas), GrSamplerState::Filter::kNearest,
GrSamplerState::WrapMode::kClamp, kFragment_GrShaderFlag) {
this->addInstanceAttrib("devbounds", kFloat4_GrVertexAttribType);
this->addInstanceAttrib("devbounds45", kFloat4_GrVertexAttribType);
this->addInstanceAttrib("dev_to_atlas_offset", kInt2_GrVertexAttribType);
this->addInstanceAttrib("color", kUByte4_norm_GrVertexAttribType);
SkASSERT(offsetof(Instance, fDevBounds) ==
this->getInstanceAttrib(InstanceAttribs::kDevBounds).offsetInRecord());
SkASSERT(offsetof(Instance, fDevBounds45) ==
this->getInstanceAttrib(InstanceAttribs::kDevBounds45).offsetInRecord());
SkASSERT(offsetof(Instance, fDevToAtlasOffset) ==
this->getInstanceAttrib(InstanceAttribs::kDevToAtlasOffset).offsetInRecord());
SkASSERT(offsetof(Instance, fColor) ==
this->getInstanceAttrib(InstanceAttribs::kColor).offsetInRecord());
SkASSERT(sizeof(Instance) == this->getInstanceStride());
GR_STATIC_ASSERT(4 == kNumInstanceAttribs);
this->addVertexAttrib("edge_norms", kFloat4_GrVertexAttribType);
fAtlasAccess.instantiate(resourceProvider);
this->addTextureSampler(&fAtlasAccess);
if (!viewMatrixIfUsingLocalCoords.invert(&fLocalMatrix)) {
fLocalMatrix.setIdentity();
}
}
class GLSLPathProcessor : public GrGLSLGeometryProcessor {
public:
void onEmitCode(EmitArgs& args, GrGPArgs* gpArgs) override;
private:
void setData(const GrGLSLProgramDataManager& pdman, const GrPrimitiveProcessor& primProc,
FPCoordTransformIter&& transformIter) override {
const GrCCPathProcessor& proc = primProc.cast<GrCCPathProcessor>();
pdman.set2f(fAtlasAdjustUniform, 1.0f / proc.atlas()->width(),
1.0f / proc.atlas()->height());
this->setTransformDataHelper(proc.localMatrix(), pdman, &transformIter);
}
GrGLSLUniformHandler::UniformHandle fAtlasAdjustUniform;
typedef GrGLSLGeometryProcessor INHERITED;
};
GrGLSLPrimitiveProcessor* GrCCPathProcessor::createGLSLInstance(const GrShaderCaps&) const {
return new GLSLPathProcessor();
}
void GrCCPathProcessor::drawPaths(GrOpFlushState* flushState, const GrPipeline& pipeline,
const GrCCPerFlushResources& resources, int baseInstance,
int endInstance, const SkRect& bounds) const {
const GrCaps& caps = flushState->caps();
GrPrimitiveType primitiveType = caps.usePrimitiveRestart()
? GrPrimitiveType::kTriangleStrip
: GrPrimitiveType::kTriangles;
int numIndicesPerInstance = caps.usePrimitiveRestart()
? SK_ARRAY_COUNT(kOctoIndicesAsStrips)
: SK_ARRAY_COUNT(kOctoIndicesAsTris);
GrMesh mesh(primitiveType);
auto enablePrimitiveRestart = GrPrimitiveRestart(flushState->caps().usePrimitiveRestart());
mesh.setIndexedInstanced(resources.indexBuffer(), numIndicesPerInstance,
resources.instanceBuffer(), endInstance - baseInstance, baseInstance,
enablePrimitiveRestart);
mesh.setVertexData(resources.vertexBuffer());
flushState->rtCommandBuffer()->draw(pipeline, *this, &mesh, nullptr, 1, bounds);
}
void GLSLPathProcessor::onEmitCode(EmitArgs& args, GrGPArgs* gpArgs) {
using InstanceAttribs = GrCCPathProcessor::InstanceAttribs;
using Interpolation = GrGLSLVaryingHandler::Interpolation;
const GrCCPathProcessor& proc = args.fGP.cast<GrCCPathProcessor>();
GrGLSLUniformHandler* uniHandler = args.fUniformHandler;
GrGLSLVaryingHandler* varyingHandler = args.fVaryingHandler;
const char* atlasAdjust;
fAtlasAdjustUniform = uniHandler->addUniform(
kVertex_GrShaderFlag,
kFloat2_GrSLType, "atlas_adjust", &atlasAdjust);
varyingHandler->emitAttributes(proc);
GrGLSLVarying texcoord(kFloat3_GrSLType);
GrGLSLVarying color(kHalf4_GrSLType);
varyingHandler->addVarying("texcoord", &texcoord);
varyingHandler->addPassThroughAttribute(&proc.getInstanceAttrib(InstanceAttribs::kColor),
args.fOutputColor, Interpolation::kCanBeFlat);
// The vertex shader bloats and intersects the devBounds and devBounds45 rectangles, in order to
// find an octagon that circumscribes the (bloated) path.
GrGLSLVertexBuilder* v = args.fVertBuilder;
// Each vertex is the intersection of one edge from devBounds and one from devBounds45.
// 'N' holds the normals to these edges as column vectors.
//
// NOTE: "float2x2(float4)" is valid and equivalent to "float2x2(float4.xy, float4.zw)",
// however Intel compilers crash when we use the former syntax in this shader.
v->codeAppendf("float2x2 N = float2x2(%s.xy, %s.zw);", proc.getEdgeNormsAttrib().name(),
proc.getEdgeNormsAttrib().name());
// N[0] is the normal for the edge we are intersecting from the regular bounding box, pointing
// out of the octagon.
v->codeAppendf("float4 devbounds = %s;",
proc.getInstanceAttrib(InstanceAttribs::kDevBounds).name());
v->codeAppend ("float2 refpt = (0 == sk_VertexID >> 2)"
"? float2(min(devbounds.x, devbounds.z), devbounds.y)"
": float2(max(devbounds.x, devbounds.z), devbounds.w);");
v->codeAppendf("refpt += N[0] * %f;", kAABloatRadius); // bloat for AA.
// N[1] is the normal for the edge we are intersecting from the 45-degree bounding box, pointing
// out of the octagon.
v->codeAppendf("float2 refpt45 = (0 == ((sk_VertexID + 1) & (1 << 2))) ? %s.xy : %s.zw;",
proc.getInstanceAttrib(InstanceAttribs::kDevBounds45).name(),
proc.getInstanceAttrib(InstanceAttribs::kDevBounds45).name());
v->codeAppendf("refpt45 *= float2x2(.5,.5,-.5,.5);"); // transform back to device space.
v->codeAppendf("refpt45 += N[1] * %f;", kAABloatRadius); // bloat for AA.
v->codeAppend ("float2 K = float2(dot(N[0], refpt), dot(N[1], refpt45));");
v->codeAppendf("float2 octocoord = K * inverse(N);");
gpArgs->fPositionVar.set(kFloat2_GrSLType, "octocoord");
// Convert to atlas coordinates in order to do our texture lookup.
v->codeAppendf("float2 atlascoord = octocoord + float2(%s);",
proc.getInstanceAttrib(InstanceAttribs::kDevToAtlasOffset).name());
if (kTopLeft_GrSurfaceOrigin == proc.atlasProxy()->origin()) {
v->codeAppendf("%s.xy = atlascoord * %s;", texcoord.vsOut(), atlasAdjust);
} else {
SkASSERT(kBottomLeft_GrSurfaceOrigin == proc.atlasProxy()->origin());
v->codeAppendf("%s.xy = float2(atlascoord.x * %s.x, 1 - atlascoord.y * %s.y);",
texcoord.vsOut(), atlasAdjust, atlasAdjust);
}
// The third texture coordinate is -.5 for even-odd paths and +.5 for winding ones.
// ("right < left" indicates even-odd fill type.)
v->codeAppendf("%s.z = sign(devbounds.z - devbounds.x) * .5;", texcoord.vsOut());
this->emitTransforms(v, varyingHandler, uniHandler, GrShaderVar("octocoord", kFloat2_GrSLType),
proc.localMatrix(), args.fFPCoordTransformHandler);
// Fragment shader.
GrGLSLFPFragmentBuilder* f = args.fFragBuilder;
// Look up coverage count in the atlas.
f->codeAppend ("half coverage = ");
f->appendTextureLookup(args.fTexSamplers[0], SkStringPrintf("%s.xy", texcoord.fsIn()).c_str(),
kFloat2_GrSLType);
f->codeAppend (".a;");
// Scale coverage count by .5. Make it negative for even-odd paths and positive for winding
// ones. Clamp winding coverage counts at 1.0 (i.e. min(coverage/2, .5)).
f->codeAppendf("coverage = min(abs(coverage) * %s.z, .5);", texcoord.fsIn());
// For negative values, this finishes the even-odd sawtooth function. Since positive (winding)
// values were clamped at "coverage/2 = .5", this only undoes the previous multiply by .5.
f->codeAppend ("coverage = 1 - abs(fract(coverage) * 2 - 1);");
f->codeAppendf("%s = half4(coverage);", args.fOutputCoverage);
}
|