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 #ifndef GrCCCoverageProcessor_DEFINED
9 #define GrCCCoverageProcessor_DEFINED
10
11 #include "include/private/SkNx.h"
12 #include "src/gpu/GrCaps.h"
13 #include "src/gpu/GrGeometryProcessor.h"
14 #include "src/gpu/GrPipeline.h"
15 #include "src/gpu/GrShaderCaps.h"
16 #include "src/gpu/glsl/GrGLSLGeometryProcessor.h"
17 #include "src/gpu/glsl/GrGLSLVarying.h"
18
19 class GrGLSLFPFragmentBuilder;
20 class GrGLSLVertexGeoBuilder;
21 class GrMesh;
22 class GrOpFlushState;
23
24 /**
25 * This is the geometry processor for the simple convex primitive shapes (triangles and closed,
26 * convex bezier curves) from which ccpr paths are composed. The output is a single-channel alpha
27 * value, positive for clockwise shapes and negative for counter-clockwise, that indicates coverage.
28 *
29 * The caller is responsible to draw all primitives as produced by GrCCGeometry into a cleared,
30 * floating point, alpha-only render target using SkBlendMode::kPlus. Once all of a path's
31 * primitives have been drawn, the render target contains a composite coverage count that can then
32 * be used to draw the path (see GrCCPathProcessor).
33 *
34 * To draw primitives, use appendMesh() and draw() (defined below).
35 */
36 class GrCCCoverageProcessor : public GrGeometryProcessor {
37 public:
38 enum class PrimitiveType {
39 kTriangles,
40 kWeightedTriangles, // Triangles (from the tessellator) whose winding magnitude > 1.
41 kQuadratics,
42 kCubics,
43 kConics
44 };
45 static const char* PrimitiveTypeName(PrimitiveType);
46
47 // Defines a single primitive shape with 3 input points (i.e. Triangles and Quadratics).
48 // X,Y point values are transposed.
49 struct TriPointInstance {
50 float fValues[6];
51
52 enum class Ordering : bool {
53 kXYTransposed,
54 kXYInterleaved,
55 };
56
57 void set(const SkPoint[3], const Sk2f& translate, Ordering);
58 void set(const SkPoint&, const SkPoint&, const SkPoint&, const Sk2f& translate, Ordering);
59 void set(const Sk2f& P0, const Sk2f& P1, const Sk2f& P2, const Sk2f& translate, Ordering);
60 };
61
62 // Defines a single primitive shape with 4 input points, or 3 input points plus a "weight"
63 // parameter duplicated in both lanes of the 4th input (i.e. Cubics, Conics, and Triangles with
64 // a weighted winding number). X,Y point values are transposed.
65 struct QuadPointInstance {
66 float fX[4];
67 float fY[4];
68
69 void set(const SkPoint[4], float dx, float dy);
70 void setW(const SkPoint[3], const Sk2f& trans, float w);
71 void setW(const SkPoint&, const SkPoint&, const SkPoint&, const Sk2f& trans, float w);
72 void setW(const Sk2f& P0, const Sk2f& P1, const Sk2f& P2, const Sk2f& trans, float w);
73 };
74
75 virtual void reset(PrimitiveType, GrResourceProvider*) = 0;
76
primitiveType()77 PrimitiveType primitiveType() const { return fPrimitiveType; }
78
79 // Number of bezier points for curves, or 3 for triangles.
numInputPoints()80 int numInputPoints() const { return PrimitiveType::kCubics == fPrimitiveType ? 4 : 3; }
81
isTriangles()82 bool isTriangles() const {
83 return PrimitiveType::kTriangles == fPrimitiveType ||
84 PrimitiveType::kWeightedTriangles == fPrimitiveType;
85 }
86
hasInputWeight()87 int hasInputWeight() const {
88 return PrimitiveType::kWeightedTriangles == fPrimitiveType ||
89 PrimitiveType::kConics == fPrimitiveType;
90 }
91
92 // GrPrimitiveProcessor overrides.
name()93 const char* name() const override { return PrimitiveTypeName(fPrimitiveType); }
94 #ifdef SK_DEBUG
dumpInfo()95 SkString dumpInfo() const override {
96 return SkStringPrintf("%s\n%s", this->name(), this->INHERITED::dumpInfo().c_str());
97 }
98 #endif
getGLSLProcessorKey(const GrShaderCaps &,GrProcessorKeyBuilder * b)99 void getGLSLProcessorKey(const GrShaderCaps&, GrProcessorKeyBuilder* b) const override {
100 SkDEBUGCODE(this->getDebugBloatKey(b));
101 b->add32((int)fPrimitiveType);
102 }
103 GrGLSLPrimitiveProcessor* createGLSLInstance(const GrShaderCaps&) const final;
104
105 #ifdef SK_DEBUG
106 // Increases the 1/2 pixel AA bloat by a factor of debugBloat.
enableDebugBloat(float debugBloat)107 void enableDebugBloat(float debugBloat) { fDebugBloat = debugBloat; }
debugBloatEnabled()108 bool debugBloatEnabled() const { return fDebugBloat > 0; }
debugBloat()109 float debugBloat() const { SkASSERT(this->debugBloatEnabled()); return fDebugBloat; }
getDebugBloatKey(GrProcessorKeyBuilder * b)110 void getDebugBloatKey(GrProcessorKeyBuilder* b) const {
111 uint32_t bloatBits;
112 memcpy(&bloatBits, &fDebugBloat, 4);
113 b->add32(bloatBits);
114 }
115 #endif
116
117 // Appends a GrMesh that will draw the provided instances. The instanceBuffer must be an array
118 // of either TriPointInstance or QuadPointInstance, depending on this processor's RendererPass,
119 // with coordinates in the desired shape's final atlas-space position.
120 virtual void appendMesh(sk_sp<const GrGpuBuffer> instanceBuffer, int instanceCount,
121 int baseInstance, SkTArray<GrMesh>* out) const = 0;
122
123 virtual void draw(GrOpFlushState*, const GrPipeline&, const SkIRect scissorRects[],
124 const GrMesh[], int meshCount, const SkRect& drawBounds) const;
125
126 // The Shader provides code to calculate each pixel's coverage in a RenderPass. It also
127 // provides details about shape-specific geometry.
128 class Shader {
129 public:
130 // Returns true if the Impl should not calculate the coverage argument for emitVaryings().
131 // If true, then "coverage" will have a signed magnitude of 1.
calculatesOwnEdgeCoverage()132 virtual bool calculatesOwnEdgeCoverage() const { return false; }
133
134 // Called before generating geometry. Subclasses may set up internal member variables during
135 // this time that will be needed during onEmitVaryings (e.g. transformation matrices).
136 //
137 // If the 'outHull4' parameter is provided, and there are not 4 input points, the subclass
138 // is required to fill it with the name of a 4-point hull around which the Impl can generate
139 // its geometry. If it is left unchanged, the Impl will use the regular input points.
140 virtual void emitSetupCode(
141 GrGLSLVertexGeoBuilder*, const char* pts, const char** outHull4 = nullptr) const {
142 SkASSERT(!outHull4);
143 }
144
emitVaryings(GrGLSLVaryingHandler * varyingHandler,GrGLSLVarying::Scope scope,SkString * code,const char * position,const char * coverage,const char * cornerCoverage,const char * wind)145 void emitVaryings(
146 GrGLSLVaryingHandler* varyingHandler, GrGLSLVarying::Scope scope, SkString* code,
147 const char* position, const char* coverage, const char* cornerCoverage,
148 const char* wind) {
149 SkASSERT(GrGLSLVarying::Scope::kVertToGeo != scope);
150 this->onEmitVaryings(
151 varyingHandler, scope, code, position, coverage, cornerCoverage, wind);
152 }
153
154 // Writes the signed coverage value at the current pixel to "outputCoverage".
155 virtual void emitFragmentCoverageCode(
156 GrGLSLFPFragmentBuilder*, const char* outputCoverage) const = 0;
157
158 // Assigns the built-in sample mask at the current pixel.
159 virtual void emitSampleMaskCode(GrGLSLFPFragmentBuilder*) const = 0;
160
161 // Calculates the winding direction of the input points (+1, -1, or 0). Wind for extremely
162 // thin triangles gets rounded to zero.
163 static void CalcWind(const GrCCCoverageProcessor&, GrGLSLVertexGeoBuilder*, const char* pts,
164 const char* outputWind);
165
166 // Calculates an edge's coverage at a conservative raster vertex. The edge is defined by two
167 // clockwise-ordered points, 'leftPt' and 'rightPt'. 'rasterVertexDir' is a pair of +/-1
168 // values that point in the direction of conservative raster bloat, starting from an
169 // endpoint.
170 //
171 // Coverage values ramp from -1 (completely outside the edge) to 0 (completely inside).
172 static void CalcEdgeCoverageAtBloatVertex(GrGLSLVertexGeoBuilder*, const char* leftPt,
173 const char* rightPt, const char* rasterVertexDir,
174 const char* outputCoverage);
175
176 // Calculates an edge's coverage at two conservative raster vertices.
177 // (See CalcEdgeCoverageAtBloatVertex).
178 static void CalcEdgeCoveragesAtBloatVertices(GrGLSLVertexGeoBuilder*, const char* leftPt,
179 const char* rightPt, const char* bloatDir1,
180 const char* bloatDir2,
181 const char* outputCoverages);
182
183 // Corner boxes require an additional "attenuation" varying that is multiplied by the
184 // regular (linearly-interpolated) coverage. This function calculates the attenuation value
185 // to use in the single, outermost vertex. The remaining three vertices of the corner box
186 // all use an attenuation value of 1.
187 static void CalcCornerAttenuation(GrGLSLVertexGeoBuilder*, const char* leftDir,
188 const char* rightDir, const char* outputAttenuation);
189
~Shader()190 virtual ~Shader() {}
191
192 protected:
193 // Here the subclass adds its internal varyings to the handler and produces code to
194 // initialize those varyings from a given position and coverage values.
195 //
196 // NOTE: the coverage values are signed appropriately for wind.
197 // 'coverage' will only be +1 or -1 on curves.
198 virtual void onEmitVaryings(
199 GrGLSLVaryingHandler*, GrGLSLVarying::Scope, SkString* code, const char* position,
200 const char* coverage, const char* cornerCoverage, const char* wind) = 0;
201
202 // Returns the name of a Shader's internal varying at the point where where its value is
203 // assigned. This is intended to work whether called for a vertex or a geometry shader.
OutName(const GrGLSLVarying & varying)204 const char* OutName(const GrGLSLVarying& varying) const {
205 using Scope = GrGLSLVarying::Scope;
206 SkASSERT(Scope::kVertToGeo != varying.scope());
207 return Scope::kGeoToFrag == varying.scope() ? varying.gsOut() : varying.vsOut();
208 }
209
210 // Our friendship with GrGLSLShaderBuilder does not propagate to subclasses.
AccessCodeString(GrGLSLShaderBuilder * s)211 inline static SkString& AccessCodeString(GrGLSLShaderBuilder* s) { return s->code(); }
212 };
213
214 protected:
215 // Slightly undershoot a bloat radius of 0.5 so vertices that fall on integer boundaries don't
216 // accidentally bleed into neighbor pixels.
217 static constexpr float kAABloatRadius = 0.491111f;
218
GrCCCoverageProcessor(ClassID classID)219 GrCCCoverageProcessor(ClassID classID) : INHERITED(classID) {}
220
221 virtual GrGLSLPrimitiveProcessor* onCreateGLSLInstance(std::unique_ptr<Shader>) const = 0;
222
223 // Our friendship with GrGLSLShaderBuilder does not propagate to subclasses.
AccessCodeString(GrGLSLShaderBuilder * s)224 inline static SkString& AccessCodeString(GrGLSLShaderBuilder* s) { return s->code(); }
225
226 PrimitiveType fPrimitiveType;
227 SkDEBUGCODE(float fDebugBloat = 0);
228
229 class TriangleShader;
230
231 typedef GrGeometryProcessor INHERITED;
232 };
233
PrimitiveTypeName(PrimitiveType type)234 inline const char* GrCCCoverageProcessor::PrimitiveTypeName(PrimitiveType type) {
235 switch (type) {
236 case PrimitiveType::kTriangles: return "kTriangles";
237 case PrimitiveType::kWeightedTriangles: return "kWeightedTriangles";
238 case PrimitiveType::kQuadratics: return "kQuadratics";
239 case PrimitiveType::kCubics: return "kCubics";
240 case PrimitiveType::kConics: return "kConics";
241 }
242 SK_ABORT("Invalid PrimitiveType");
243 }
244
set(const SkPoint p[3],const Sk2f & translate,Ordering ordering)245 inline void GrCCCoverageProcessor::TriPointInstance::set(
246 const SkPoint p[3], const Sk2f& translate, Ordering ordering) {
247 this->set(p[0], p[1], p[2], translate, ordering);
248 }
249
set(const SkPoint & p0,const SkPoint & p1,const SkPoint & p2,const Sk2f & translate,Ordering ordering)250 inline void GrCCCoverageProcessor::TriPointInstance::set(
251 const SkPoint& p0, const SkPoint& p1, const SkPoint& p2, const Sk2f& translate,
252 Ordering ordering) {
253 Sk2f P0 = Sk2f::Load(&p0);
254 Sk2f P1 = Sk2f::Load(&p1);
255 Sk2f P2 = Sk2f::Load(&p2);
256 this->set(P0, P1, P2, translate, ordering);
257 }
258
set(const Sk2f & P0,const Sk2f & P1,const Sk2f & P2,const Sk2f & translate,Ordering ordering)259 inline void GrCCCoverageProcessor::TriPointInstance::set(
260 const Sk2f& P0, const Sk2f& P1, const Sk2f& P2, const Sk2f& translate, Ordering ordering) {
261 if (Ordering::kXYTransposed == ordering) {
262 Sk2f::Store3(fValues, P0 + translate, P1 + translate, P2 + translate);
263 } else {
264 (P0 + translate).store(fValues);
265 (P1 + translate).store(fValues + 2);
266 (P2 + translate).store(fValues + 4);
267 }
268 }
269
set(const SkPoint p[4],float dx,float dy)270 inline void GrCCCoverageProcessor::QuadPointInstance::set(const SkPoint p[4], float dx, float dy) {
271 Sk4f X,Y;
272 Sk4f::Load2(p, &X, &Y);
273 (X + dx).store(&fX);
274 (Y + dy).store(&fY);
275 }
276
setW(const SkPoint p[3],const Sk2f & trans,float w)277 inline void GrCCCoverageProcessor::QuadPointInstance::setW(const SkPoint p[3], const Sk2f& trans,
278 float w) {
279 this->setW(p[0], p[1], p[2], trans, w);
280 }
281
setW(const SkPoint & p0,const SkPoint & p1,const SkPoint & p2,const Sk2f & trans,float w)282 inline void GrCCCoverageProcessor::QuadPointInstance::setW(const SkPoint& p0, const SkPoint& p1,
283 const SkPoint& p2, const Sk2f& trans,
284 float w) {
285 Sk2f P0 = Sk2f::Load(&p0);
286 Sk2f P1 = Sk2f::Load(&p1);
287 Sk2f P2 = Sk2f::Load(&p2);
288 this->setW(P0, P1, P2, trans, w);
289 }
290
setW(const Sk2f & P0,const Sk2f & P1,const Sk2f & P2,const Sk2f & trans,float w)291 inline void GrCCCoverageProcessor::QuadPointInstance::setW(const Sk2f& P0, const Sk2f& P1,
292 const Sk2f& P2, const Sk2f& trans,
293 float w) {
294 Sk2f W = Sk2f(w);
295 Sk2f::Store4(this, P0 + trans, P1 + trans, P2 + trans, W);
296 }
297
298 #endif
299