blob: bb0ecc9e225396fb2ae33867f8b115587781f471 [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
8#include "GrCCPRPathProcessor.h"
9
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
39// Index buffer for the octagon defined above.
40static uint16_t kOctoIndices[GrCCPRPathProcessor::kPerInstanceIndexCount] = {
41 0, 4, 2,
42 0, 6, 4,
43 0, 2, 1,
44 2, 4, 3,
45 4, 6, 5,
46 6, 0, 7,
47};
48
49GR_DECLARE_STATIC_UNIQUE_KEY(gIndexBufferKey);
50
51GrCCPRPathProcessor::GrCCPRPathProcessor(GrResourceProvider* rp, sk_sp<GrTextureProxy> atlas,
52 SkPath::FillType fillType, const GrShaderCaps& shaderCaps)
53 : fFillType(fillType) {
54 this->addInstanceAttrib("devbounds", kVec4f_GrVertexAttribType, kHigh_GrSLPrecision);
55 this->addInstanceAttrib("devbounds45", kVec4f_GrVertexAttribType, kHigh_GrSLPrecision);
56 this->addInstanceAttrib("view_matrix", kVec4f_GrVertexAttribType, kHigh_GrSLPrecision);
57 this->addInstanceAttrib("view_translate", kVec2f_GrVertexAttribType, kHigh_GrSLPrecision);
58 // FIXME: this could be a vector of two shorts if it were supported by Ganesh.
Robert Phillips8296e752017-08-25 08:45:21 -040059 // Note: this should be doable now with kVec2us_uint_GrVertexAttribType
Chris Dalton1a325d22017-07-14 15:17:41 -060060 this->addInstanceAttrib("atlas_offset", kVec2i_GrVertexAttribType, kHigh_GrSLPrecision);
61 this->addInstanceAttrib("color", kVec4ub_GrVertexAttribType, kLow_GrSLPrecision);
62
63 SkASSERT(offsetof(Instance, fDevBounds) ==
64 this->getInstanceAttrib(InstanceAttribs::kDevBounds).fOffsetInRecord);
65 SkASSERT(offsetof(Instance, fDevBounds45) ==
66 this->getInstanceAttrib(InstanceAttribs::kDevBounds45).fOffsetInRecord);
67 SkASSERT(offsetof(Instance, fViewMatrix) ==
68 this->getInstanceAttrib(InstanceAttribs::kViewMatrix).fOffsetInRecord);
69 SkASSERT(offsetof(Instance, fViewTranslate) ==
70 this->getInstanceAttrib(InstanceAttribs::kViewTranslate).fOffsetInRecord);
71 SkASSERT(offsetof(Instance, fAtlasOffset) ==
72 this->getInstanceAttrib(InstanceAttribs::kAtlasOffset).fOffsetInRecord);
73 SkASSERT(offsetof(Instance, fColor) ==
74 this->getInstanceAttrib(InstanceAttribs::kColor).fOffsetInRecord);
75 SkASSERT(sizeof(Instance) == this->getInstanceStride());
76
77 GR_STATIC_ASSERT(6 == kNumInstanceAttribs);
78
79 this->addVertexAttrib("edge_norms", kVec4f_GrVertexAttribType, kHigh_GrSLPrecision);
80
Brian Salomon2bbdcc42017-09-07 12:36:34 -040081 fAtlasAccess.reset(std::move(atlas), GrSamplerState::Filter::kNearest,
82 GrSamplerState::WrapMode::kClamp, kFragment_GrShaderFlag);
Chris Dalton1a325d22017-07-14 15:17:41 -060083 fAtlasAccess.instantiate(rp);
84 this->addTextureSampler(&fAtlasAccess);
85
86 this->initClassID<GrCCPRPathProcessor>();
87}
88
89void GrCCPRPathProcessor::getGLSLProcessorKey(const GrShaderCaps&, GrProcessorKeyBuilder* b) const {
Robert Phillipse44ef102017-07-21 15:37:19 -040090 b->add32((fFillType << 16) | this->atlasProxy()->origin());
Chris Dalton1a325d22017-07-14 15:17:41 -060091}
92
93class GLSLPathProcessor : public GrGLSLGeometryProcessor {
94public:
95 void onEmitCode(EmitArgs& args, GrGPArgs* gpArgs) override;
96
97private:
98 void setData(const GrGLSLProgramDataManager& pdman, const GrPrimitiveProcessor& primProc,
99 FPCoordTransformIter&& transformIter) override {
100 const GrCCPRPathProcessor& proc = primProc.cast<GrCCPRPathProcessor>();
101 pdman.set2f(fAtlasAdjustUniform, 1.0f / proc.atlas()->width(),
102 1.0f / proc.atlas()->height());
103 this->setTransformDataHelper(SkMatrix::I(), pdman, &transformIter);
104 }
105
106 GrGLSLUniformHandler::UniformHandle fAtlasAdjustUniform;
107
108 typedef GrGLSLGeometryProcessor INHERITED;
109};
110
111GrGLSLPrimitiveProcessor* GrCCPRPathProcessor::createGLSLInstance(const GrShaderCaps&) const {
112 return new GLSLPathProcessor();
113}
114
115void GLSLPathProcessor::onEmitCode(EmitArgs& args, GrGPArgs* gpArgs) {
116 using InstanceAttribs = GrCCPRPathProcessor::InstanceAttribs;
117 const GrCCPRPathProcessor& proc = args.fGP.cast<GrCCPRPathProcessor>();
118 GrGLSLUniformHandler* uniHandler = args.fUniformHandler;
119 GrGLSLVaryingHandler* varyingHandler = args.fVaryingHandler;
120
121 const char* atlasAdjust;
122 fAtlasAdjustUniform = uniHandler->addUniform(
123 kVertex_GrShaderFlag,
Ethan Nicholasf7b88202017-09-18 14:10:39 -0400124 kHighFloat2_GrSLType, "atlas_adjust", &atlasAdjust);
Chris Dalton1a325d22017-07-14 15:17:41 -0600125
126 varyingHandler->emitAttributes(proc);
127
Ethan Nicholasf7b88202017-09-18 14:10:39 -0400128 GrGLSLVertToFrag texcoord(kHighFloat2_GrSLType);
129 GrGLSLVertToFrag color(kHalf4_GrSLType);
Chris Dalton1a325d22017-07-14 15:17:41 -0600130 varyingHandler->addVarying("texcoord", &texcoord, kHigh_GrSLPrecision);
131 varyingHandler->addFlatPassThroughAttribute(&proc.getInstanceAttrib(InstanceAttribs::kColor),
132 args.fOutputColor, kLow_GrSLPrecision);
133
134 // Vertex shader.
135 GrGLSLVertexBuilder* v = args.fVertBuilder;
136
137 // Find the intersections of (bloated) devBounds and devBounds45 in order to come up with an
138 // octagon that circumscribes the (bloated) path. A vertex is the intersection of two lines:
139 // one edge from the path's bounding box and one edge from its 45-degree bounding box.
Ethan Nicholasf7b88202017-09-18 14:10:39 -0400140 v->codeAppendf("highfloat2x2 N = highfloat2x2(%s);", proc.getEdgeNormsAttrib().fName);
Chris Dalton1a325d22017-07-14 15:17:41 -0600141
142 // N[0] is the normal for the edge we are intersecting from the regular bounding box, pointing
143 // out of the octagon.
Ethan Nicholasf7b88202017-09-18 14:10:39 -0400144 v->codeAppendf("highfloat2 refpt = (min(N[0].x, N[0].y) < 0) ? %s.xy : %s.zw;",
Chris Dalton1a325d22017-07-14 15:17:41 -0600145 proc.getInstanceAttrib(InstanceAttribs::kDevBounds).fName,
146 proc.getInstanceAttrib(InstanceAttribs::kDevBounds).fName);
147 v->codeAppendf("refpt += N[0] * %f;", kAABloatRadius); // bloat for AA.
148
149 // N[1] is the normal for the edge we are intersecting from the 45-degree bounding box, pointing
150 // out of the octagon.
Ethan Nicholasf7b88202017-09-18 14:10:39 -0400151 v->codeAppendf("highfloat2 refpt45 = (N[1].x < 0) ? %s.xy : %s.zw;",
Chris Dalton1a325d22017-07-14 15:17:41 -0600152 proc.getInstanceAttrib(InstanceAttribs::kDevBounds45).fName,
153 proc.getInstanceAttrib(InstanceAttribs::kDevBounds45).fName);
Ethan Nicholasf7b88202017-09-18 14:10:39 -0400154 v->codeAppendf("refpt45 *= highfloat2x2(.5,.5,-.5,.5);"); // transform back to device space.
Chris Dalton1a325d22017-07-14 15:17:41 -0600155 v->codeAppendf("refpt45 += N[1] * %f;", kAABloatRadius); // bloat for AA.
156
Ethan Nicholasf7b88202017-09-18 14:10:39 -0400157 v->codeAppend ("highfloat2 K = highfloat2(dot(N[0], refpt), dot(N[1], refpt45));");
158 v->codeAppendf("highfloat2 octocoord = K * inverse(N);");
Chris Dalton1a325d22017-07-14 15:17:41 -0600159
Ethan Nicholasf7b88202017-09-18 14:10:39 -0400160 gpArgs->fPositionVar.set(kHighFloat2_GrSLType, "octocoord");
Chris Dalton1a325d22017-07-14 15:17:41 -0600161
162 // Convert to atlas coordinates in order to do our texture lookup.
Ethan Nicholasf7b88202017-09-18 14:10:39 -0400163 v->codeAppendf("highfloat2 atlascoord = octocoord + highfloat2(%s);",
Chris Dalton1a325d22017-07-14 15:17:41 -0600164 proc.getInstanceAttrib(InstanceAttribs::kAtlasOffset).fName);
Robert Phillipse44ef102017-07-21 15:37:19 -0400165 if (kTopLeft_GrSurfaceOrigin == proc.atlasProxy()->origin()) {
Chris Dalton1a325d22017-07-14 15:17:41 -0600166 v->codeAppendf("%s = atlascoord * %s;", texcoord.vsOut(), atlasAdjust);
167 } else {
Robert Phillipse44ef102017-07-21 15:37:19 -0400168 SkASSERT(kBottomLeft_GrSurfaceOrigin == proc.atlasProxy()->origin());
Ethan Nicholasf7b88202017-09-18 14:10:39 -0400169 v->codeAppendf("%s = highfloat2(atlascoord.x * %s.x, 1 - atlascoord.y * %s.y);",
Chris Dalton1a325d22017-07-14 15:17:41 -0600170 texcoord.vsOut(), atlasAdjust, atlasAdjust);
171 }
172
173 // Convert to (local) path cordinates.
Ethan Nicholasf7b88202017-09-18 14:10:39 -0400174 v->codeAppendf("highfloat2 pathcoord = inverse(highfloat2x2(%s)) * (octocoord - %s);",
Chris Dalton1a325d22017-07-14 15:17:41 -0600175 proc.getInstanceAttrib(InstanceAttribs::kViewMatrix).fName,
176 proc.getInstanceAttrib(InstanceAttribs::kViewTranslate).fName);
177
178 this->emitTransforms(v, varyingHandler, uniHandler, gpArgs->fPositionVar, "pathcoord",
179 args.fFPCoordTransformHandler);
180
181 // Fragment shader.
182 GrGLSLPPFragmentBuilder* f = args.fFragBuilder;
183
Ethan Nicholasf7b88202017-09-18 14:10:39 -0400184 f->codeAppend ("half coverage_count = ");
185 f->appendTextureLookup(args.fTexSamplers[0], texcoord.fsIn(), kHighFloat2_GrSLType);
Chris Dalton1a325d22017-07-14 15:17:41 -0600186 f->codeAppend (".a;");
187
188 if (SkPath::kWinding_FillType == proc.fillType()) {
Ethan Nicholasf7b88202017-09-18 14:10:39 -0400189 f->codeAppendf("%s = half4(min(abs(coverage_count), 1));", args.fOutputCoverage);
Chris Dalton1a325d22017-07-14 15:17:41 -0600190 } else {
191 SkASSERT(SkPath::kEvenOdd_FillType == proc.fillType());
Ethan Nicholasf7b88202017-09-18 14:10:39 -0400192 f->codeAppend ("half t = mod(abs(coverage_count), 2);");
193 f->codeAppendf("%s = half4(1 - abs(t - 1));", args.fOutputCoverage);
Chris Dalton1a325d22017-07-14 15:17:41 -0600194 }
195}
196
197sk_sp<GrBuffer> GrCCPRPathProcessor::FindOrMakeIndexBuffer(GrOnFlushResourceProvider* onFlushRP) {
198 GR_DEFINE_STATIC_UNIQUE_KEY(gIndexBufferKey);
199 return onFlushRP->findOrMakeStaticBuffer(gIndexBufferKey, kIndex_GrBufferType,
200 sizeof(kOctoIndices), kOctoIndices);
201}
202
203sk_sp<GrBuffer> GrCCPRPathProcessor::FindOrMakeVertexBuffer(GrOnFlushResourceProvider* onFlushRP) {
204 GR_DEFINE_STATIC_UNIQUE_KEY(gVertexBufferKey);
205 return onFlushRP->findOrMakeStaticBuffer(gVertexBufferKey, kVertex_GrBufferType,
206 sizeof(kOctoEdgeNorms), kOctoEdgeNorms);
207}