• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
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 "GrCCPathProcessor.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.
19 constexpr 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.
25 static 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 
37 GR_DECLARE_STATIC_UNIQUE_KEY(gVertexBufferKey);
38 
FindVertexBuffer(GrOnFlushResourceProvider * onFlushRP)39 sk_sp<const GrBuffer> GrCCPathProcessor::FindVertexBuffer(GrOnFlushResourceProvider* onFlushRP) {
40     GR_DEFINE_STATIC_UNIQUE_KEY(gVertexBufferKey);
41     return onFlushRP->findOrMakeStaticBuffer(kVertex_GrBufferType, sizeof(kOctoEdgeNorms),
42                                              kOctoEdgeNorms, gVertexBufferKey);
43 }
44 
45 static constexpr uint16_t kRestartStrip = 0xffff;
46 
47 static constexpr uint16_t kOctoIndicesAsStrips[] = {
48     1, 0, 2, 4, 3, kRestartStrip, // First half.
49     5, 4, 6, 0, 7 // Second half.
50 };
51 
52 static constexpr uint16_t kOctoIndicesAsTris[] = {
53     // First half.
54     1, 0, 2,
55     0, 4, 2,
56     2, 4, 3,
57 
58     // Second half.
59     5, 4, 6,
60     4, 0, 6,
61     6, 0, 7,
62 };
63 
64 GR_DECLARE_STATIC_UNIQUE_KEY(gIndexBufferKey);
65 
FindIndexBuffer(GrOnFlushResourceProvider * onFlushRP)66 sk_sp<const GrBuffer> GrCCPathProcessor::FindIndexBuffer(GrOnFlushResourceProvider* onFlushRP) {
67     GR_DEFINE_STATIC_UNIQUE_KEY(gIndexBufferKey);
68     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     }
75 }
76 
NumIndicesPerInstance(const GrCaps & caps)77 int GrCCPathProcessor::NumIndicesPerInstance(const GrCaps& caps) {
78     return caps.usePrimitiveRestart() ? SK_ARRAY_COUNT(kOctoIndicesAsStrips)
79                                       : SK_ARRAY_COUNT(kOctoIndicesAsTris);
80 }
81 
GrCCPathProcessor(GrResourceProvider * resourceProvider,sk_sp<GrTextureProxy> atlas,SkPath::FillType fillType)82 GrCCPathProcessor::GrCCPathProcessor(GrResourceProvider* resourceProvider,
83                                      sk_sp<GrTextureProxy> atlas, SkPath::FillType fillType)
84         : INHERITED(kGrCCPathProcessor_ClassID)
85         , fFillType(fillType)
86         , fAtlasAccess(std::move(atlas), GrSamplerState::Filter::kNearest,
87                        GrSamplerState::WrapMode::kClamp, kFragment_GrShaderFlag) {
88     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);
92     this->addInstanceAttrib("atlas_offset", kShort2_GrVertexAttribType);
93     this->addInstanceAttrib("color", kUByte4_norm_GrVertexAttribType);
94 
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 
111     this->addVertexAttrib("edge_norms", kFloat4_GrVertexAttribType);
112 
113     fAtlasAccess.instantiate(resourceProvider);
114     this->addTextureSampler(&fAtlasAccess);
115 
116     if (resourceProvider->caps()->usePrimitiveRestart()) {
117         this->setWillUsePrimitiveRestart();
118     }
119 }
120 
getGLSLProcessorKey(const GrShaderCaps &,GrProcessorKeyBuilder * b) const121 void GrCCPathProcessor::getGLSLProcessorKey(const GrShaderCaps&, GrProcessorKeyBuilder* b) const {
122     b->add32((fFillType << 16) | this->atlasProxy()->origin());
123 }
124 
125 class GLSLPathProcessor : public GrGLSLGeometryProcessor {
126 public:
127     void onEmitCode(EmitArgs& args, GrGPArgs* gpArgs) override;
128 
129 private:
setData(const GrGLSLProgramDataManager & pdman,const GrPrimitiveProcessor & primProc,FPCoordTransformIter && transformIter)130     void setData(const GrGLSLProgramDataManager& pdman, const GrPrimitiveProcessor& primProc,
131                  FPCoordTransformIter&& transformIter) override {
132         const GrCCPathProcessor& proc = primProc.cast<GrCCPathProcessor>();
133         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 
createGLSLInstance(const GrShaderCaps &) const143 GrGLSLPrimitiveProcessor* GrCCPathProcessor::createGLSLInstance(const GrShaderCaps&) const {
144     return new GLSLPathProcessor();
145 }
146 
onEmitCode(EmitArgs & args,GrGPArgs * gpArgs)147 void GLSLPathProcessor::onEmitCode(EmitArgs& args, GrGPArgs* gpArgs) {
148     using InstanceAttribs = GrCCPathProcessor::InstanceAttribs;
149     const GrCCPathProcessor& proc = args.fGP.cast<GrCCPathProcessor>();
150     GrGLSLUniformHandler* uniHandler = args.fUniformHandler;
151     GrGLSLVaryingHandler* varyingHandler = args.fVaryingHandler;
152 
153     const char* atlasAdjust;
154     fAtlasAdjustUniform = uniHandler->addUniform(
155             kVertex_GrShaderFlag,
156             kFloat2_GrSLType, "atlas_adjust", &atlasAdjust);
157 
158     varyingHandler->emitAttributes(proc);
159 
160     GrGLSLVarying texcoord(kFloat2_GrSLType);
161     GrGLSLVarying color(kHalf4_GrSLType);
162     varyingHandler->addVarying("texcoord", &texcoord);
163     varyingHandler->addFlatPassThroughAttribute(&proc.getInstanceAttrib(InstanceAttribs::kColor),
164                                                 args.fOutputColor);
165 
166     // The vertex shader bloats and intersects the devBounds and devBounds45 rectangles, in order to
167     // find an octagon that circumscribes the (bloated) path.
168     GrGLSLVertexBuilder* v = args.fVertBuilder;
169 
170     // Each vertex is the intersection of one edge from devBounds and one from devBounds45.
171     // 'N' holds the normals to these edges as column vectors.
172     //
173     // NOTE: "float2x2(float4)" is valid and equivalent to "float2x2(float4.xy, float4.zw)",
174     // however Intel compilers crash when we use the former syntax in this shader.
175     v->codeAppendf("float2x2 N = float2x2(%s.xy, %s.zw);",
176                    proc.getEdgeNormsAttrib().fName, proc.getEdgeNormsAttrib().fName);
177 
178     // N[0] is the normal for the edge we are intersecting from the regular bounding box, pointing
179     // out of the octagon.
180     v->codeAppendf("float2 refpt = float2[2](%s.xy, %s.zw)[sk_VertexID >> 2];",
181                    proc.getInstanceAttrib(InstanceAttribs::kDevBounds).fName,
182                    proc.getInstanceAttrib(InstanceAttribs::kDevBounds).fName);
183     v->codeAppendf("refpt += N[0] * %f;", kAABloatRadius); // bloat for AA.
184 
185     // N[1] is the normal for the edge we are intersecting from the 45-degree bounding box, pointing
186     // out of the octagon.
187     v->codeAppendf("float2 refpt45 = float2[2](%s.xy, %s.zw)[((sk_VertexID + 1) >> 2) & 1];",
188                    proc.getInstanceAttrib(InstanceAttribs::kDevBounds45).fName,
189                    proc.getInstanceAttrib(InstanceAttribs::kDevBounds45).fName);
190     v->codeAppendf("refpt45 *= float2x2(.5,.5,-.5,.5);"); // transform back to device space.
191     v->codeAppendf("refpt45 += N[1] * %f;", kAABloatRadius); // bloat for AA.
192 
193     v->codeAppend ("float2 K = float2(dot(N[0], refpt), dot(N[1], refpt45));");
194     v->codeAppendf("float2 octocoord = K * inverse(N);");
195 
196     gpArgs->fPositionVar.set(kFloat2_GrSLType, "octocoord");
197 
198     // Convert to atlas coordinates in order to do our texture lookup.
199     v->codeAppendf("float2 atlascoord = octocoord + float2(%s);",
200                    proc.getInstanceAttrib(InstanceAttribs::kAtlasOffset).fName);
201     if (kTopLeft_GrSurfaceOrigin == proc.atlasProxy()->origin()) {
202         v->codeAppendf("%s = atlascoord * %s;", texcoord.vsOut(), atlasAdjust);
203     } else {
204         SkASSERT(kBottomLeft_GrSurfaceOrigin == proc.atlasProxy()->origin());
205         v->codeAppendf("%s = float2(atlascoord.x * %s.x, 1 - atlascoord.y * %s.y);",
206                        texcoord.vsOut(), atlasAdjust, atlasAdjust);
207     }
208 
209     // Convert to path/local cordinates.
210     v->codeAppendf("float2x2 viewmatrix = float2x2(%s.xy, %s.zw);", // float2x2(float4) busts Intel.
211                    proc.getInstanceAttrib(InstanceAttribs::kViewMatrix).fName,
212                    proc.getInstanceAttrib(InstanceAttribs::kViewMatrix).fName);
213     v->codeAppendf("float2 pathcoord = inverse(viewmatrix) * (octocoord - %s);",
214                    proc.getInstanceAttrib(InstanceAttribs::kViewTranslate).fName);
215 
216     this->emitTransforms(v, varyingHandler, uniHandler, GrShaderVar("pathcoord", kFloat2_GrSLType),
217                          args.fFPCoordTransformHandler);
218 
219     // Fragment shader.
220     GrGLSLPPFragmentBuilder* f = args.fFragBuilder;
221 
222     f->codeAppend ("half coverage_count = ");
223     f->appendTextureLookup(args.fTexSamplers[0], texcoord.fsIn(), kFloat2_GrSLType);
224     f->codeAppend (".a;");
225 
226     if (SkPath::kWinding_FillType == proc.fillType()) {
227         f->codeAppendf("%s = half4(min(abs(coverage_count), 1));", args.fOutputCoverage);
228     } else {
229         SkASSERT(SkPath::kEvenOdd_FillType == proc.fillType());
230         f->codeAppend ("half t = mod(abs(coverage_count), 2);");
231         f->codeAppendf("%s = half4(1 - abs(t - 1));", args.fOutputCoverage);
232     }
233 }
234