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/GrGLSLShaderBuilder.h"
18#include "src/gpu/glsl/GrGLSLVarying.h"
19
20class GrGLSLFPFragmentBuilder;
21class GrGLSLVertexGeoBuilder;
22class 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 */
36class GrCCCoverageProcessor : public GrGeometryProcessor {
37public:
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 PrimitiveType primitiveType() const { return fPrimitiveType; }
76
77 // Number of bezier points for curves, or 3 for triangles.
78 int numInputPoints() const { return PrimitiveType::kCubics == fPrimitiveType ? 4 : 3; }
79
80 bool isTriangles() const {
81 return PrimitiveType::kTriangles == fPrimitiveType ||
82 PrimitiveType::kWeightedTriangles == fPrimitiveType;
83 }
84
85 int hasInputWeight() const {
86 return PrimitiveType::kWeightedTriangles == fPrimitiveType ||
87 PrimitiveType::kConics == fPrimitiveType;
88 }
89
90 // GrPrimitiveProcessor overrides.
91 const char* name() const override { return PrimitiveTypeName(fPrimitiveType); }
92 void getGLSLProcessorKey(const GrShaderCaps&, GrProcessorKeyBuilder* b) const override {
93 SkDEBUGCODE(this->getDebugBloatKey(b));
94 b->add32((int)fPrimitiveType);
95 }
96 GrGLSLPrimitiveProcessor* createGLSLInstance(const GrShaderCaps&) const final;
97
98#ifdef SK_DEBUG
99 // Increases the 1/2 pixel AA bloat by a factor of debugBloat.
100 void enableDebugBloat(float debugBloat) { fDebugBloat = debugBloat; }
101 bool debugBloatEnabled() const { return fDebugBloat > 0; }
102 float debugBloat() const { SkASSERT(this->debugBloatEnabled()); return fDebugBloat; }
103 void getDebugBloatKey(GrProcessorKeyBuilder* b) const {
104 uint32_t bloatBits;
105 memcpy(&bloatBits, &fDebugBloat, 4);
106 b->add32(bloatBits);
107 }
108#endif
109
110 // The caller uses these methods to actualy draw the coverage PrimitiveTypes. For each
111 // subpassIdx of each PrimitiveType, it calls reset/bind*/drawInstances.
112 virtual int numSubpasses() const = 0;
113 virtual void reset(PrimitiveType, int subpassIdx, GrResourceProvider*) = 0;
114 void bindPipeline(GrOpFlushState*, const GrPipeline&, const SkRect& drawBounds) const;
115 virtual void bindBuffers(GrOpsRenderPass*, sk_sp<const GrBuffer> instanceBuffer) const = 0;
116 virtual void drawInstances(GrOpsRenderPass*, int instanceCount, int baseInstance) const = 0;
117
118 // The Shader provides code to calculate each pixel's coverage in a RenderPass. It also
119 // provides details about shape-specific geometry.
120 class Shader {
121 public:
122 // Returns true if the Impl should not calculate the coverage argument for emitVaryings().
123 // If true, then "coverage" will have a signed magnitude of 1.
124 virtual bool calculatesOwnEdgeCoverage() const { return false; }
125
126 // Called before generating geometry. Subclasses may set up internal member variables during
127 // this time that will be needed during onEmitVaryings (e.g. transformation matrices).
128 //
129 // If the 'outHull4' parameter is provided, and there are not 4 input points, the subclass
130 // is required to fill it with the name of a 4-point hull around which the Impl can generate
131 // its geometry. If it is left unchanged, the Impl will use the regular input points.
132 virtual void emitSetupCode(
133 GrGLSLVertexGeoBuilder*, const char* pts, const char** outHull4 = nullptr) const {
134 SkASSERT(!outHull4);
135 }
136
137 void emitVaryings(
138 GrGLSLVaryingHandler* varyingHandler, GrGLSLVarying::Scope scope, SkString* code,
139 const char* position, const char* coverage, const char* cornerCoverage,
140 const char* wind) {
141 SkASSERT(GrGLSLVarying::Scope::kVertToGeo != scope);
142 this->onEmitVaryings(
143 varyingHandler, scope, code, position, coverage, cornerCoverage, wind);
144 }
145
146 // Writes the signed coverage value at the current pixel to "outputCoverage".
147 virtual void emitFragmentCoverageCode(
148 GrGLSLFPFragmentBuilder*, const char* outputCoverage) const = 0;
149
150 // Assigns the built-in sample mask at the current pixel.
151 virtual void emitSampleMaskCode(GrGLSLFPFragmentBuilder*) const = 0;
152
153 // Calculates the winding direction of the input points (+1, -1, or 0). Wind for extremely
154 // thin triangles gets rounded to zero.
155 static void CalcWind(const GrCCCoverageProcessor&, GrGLSLVertexGeoBuilder*, const char* pts,
156 const char* outputWind);
157
158 // Calculates an edge's coverage at a conservative raster vertex. The edge is defined by two
159 // clockwise-ordered points, 'leftPt' and 'rightPt'. 'rasterVertexDir' is a pair of +/-1
160 // values that point in the direction of conservative raster bloat, starting from an
161 // endpoint.
162 //
163 // Coverage values ramp from -1 (completely outside the edge) to 0 (completely inside).
164 static void CalcEdgeCoverageAtBloatVertex(GrGLSLVertexGeoBuilder*, const char* leftPt,
165 const char* rightPt, const char* rasterVertexDir,
166 const char* outputCoverage);
167
168 // Calculates an edge's coverage at two conservative raster vertices.
169 // (See CalcEdgeCoverageAtBloatVertex).
170 static void CalcEdgeCoveragesAtBloatVertices(GrGLSLVertexGeoBuilder*, const char* leftPt,
171 const char* rightPt, const char* bloatDir1,
172 const char* bloatDir2,
173 const char* outputCoverages);
174
175 // Corner boxes require an additional "attenuation" varying that is multiplied by the
176 // regular (linearly-interpolated) coverage. This function calculates the attenuation value
177 // to use in the single, outermost vertex. The remaining three vertices of the corner box
178 // all use an attenuation value of 1.
179 static void CalcCornerAttenuation(GrGLSLVertexGeoBuilder*, const char* leftDir,
180 const char* rightDir, const char* outputAttenuation);
181
182 virtual ~Shader() {}
183
184 protected:
185 // Here the subclass adds its internal varyings to the handler and produces code to
186 // initialize those varyings from a given position and coverage values.
187 //
188 // NOTE: the coverage values are signed appropriately for wind.
189 // 'coverage' will only be +1 or -1 on curves.
190 virtual void onEmitVaryings(
191 GrGLSLVaryingHandler*, GrGLSLVarying::Scope, SkString* code, const char* position,
192 const char* coverage, const char* cornerCoverage, const char* wind) = 0;
193
194 // Returns the name of a Shader's internal varying at the point where where its value is
195 // assigned. This is intended to work whether called for a vertex or a geometry shader.
196 const char* OutName(const GrGLSLVarying& varying) const {
197 using Scope = GrGLSLVarying::Scope;
198 SkASSERT(Scope::kVertToGeo != varying.scope());
199 return Scope::kGeoToFrag == varying.scope() ? varying.gsOut() : varying.vsOut();
200 }
201
202 // Our friendship with GrGLSLShaderBuilder does not propagate to subclasses.
203 inline static SkString& AccessCodeString(GrGLSLShaderBuilder* s) { return s->code(); }
204 };
205
206protected:
207 // Slightly undershoot a bloat radius of 0.5 so vertices that fall on integer boundaries don't
208 // accidentally bleed into neighbor pixels.
209 static constexpr float kAABloatRadius = 0.491111f;
210
211 GrCCCoverageProcessor(ClassID classID) : INHERITED(classID) {}
212
213 virtual GrPrimitiveType primType() const = 0;
214
215 virtual GrGLSLPrimitiveProcessor* onCreateGLSLInstance(std::unique_ptr<Shader>) const = 0;
216
217 // Our friendship with GrGLSLShaderBuilder does not propagate to subclasses.
218 inline static SkString& AccessCodeString(GrGLSLShaderBuilder* s) { return s->code(); }
219
220 PrimitiveType fPrimitiveType;
221 SkDEBUGCODE(float fDebugBloat = 0);
222
223 class TriangleShader;
224
225 typedef GrGeometryProcessor INHERITED;
226};
227
228inline const char* GrCCCoverageProcessor::PrimitiveTypeName(PrimitiveType type) {
229 switch (type) {
230 case PrimitiveType::kTriangles: return "kTriangles";
231 case PrimitiveType::kWeightedTriangles: return "kWeightedTriangles";
232 case PrimitiveType::kQuadratics: return "kQuadratics";
233 case PrimitiveType::kCubics: return "kCubics";
234 case PrimitiveType::kConics: return "kConics";
235 }
236 SK_ABORT("Invalid PrimitiveType");
237}
238
239inline void GrCCCoverageProcessor::TriPointInstance::set(
240 const SkPoint p[3], const Sk2f& translate, Ordering ordering) {
241 this->set(p[0], p[1], p[2], translate, ordering);
242}
243
244inline void GrCCCoverageProcessor::TriPointInstance::set(
245 const SkPoint& p0, const SkPoint& p1, const SkPoint& p2, const Sk2f& translate,
246 Ordering ordering) {
247 Sk2f P0 = Sk2f::Load(&p0);
248 Sk2f P1 = Sk2f::Load(&p1);
249 Sk2f P2 = Sk2f::Load(&p2);
250 this->set(P0, P1, P2, translate, ordering);
251}
252
253inline void GrCCCoverageProcessor::TriPointInstance::set(
254 const Sk2f& P0, const Sk2f& P1, const Sk2f& P2, const Sk2f& translate, Ordering ordering) {
255 if (Ordering::kXYTransposed == ordering) {
256 Sk2f::Store3(fValues, P0 + translate, P1 + translate, P2 + translate);
257 } else {
258 (P0 + translate).store(fValues);
259 (P1 + translate).store(fValues + 2);
260 (P2 + translate).store(fValues + 4);
261 }
262}
263
264inline void GrCCCoverageProcessor::QuadPointInstance::set(const SkPoint p[4], float dx, float dy) {
265 Sk4f X,Y;
266 Sk4f::Load2(p, &X, &Y);
267 (X + dx).store(&fX);
268 (Y + dy).store(&fY);
269}
270
271inline void GrCCCoverageProcessor::QuadPointInstance::setW(const SkPoint p[3], const Sk2f& trans,
272 float w) {
273 this->setW(p[0], p[1], p[2], trans, w);
274}
275
276inline void GrCCCoverageProcessor::QuadPointInstance::setW(const SkPoint& p0, const SkPoint& p1,
277 const SkPoint& p2, const Sk2f& trans,
278 float w) {
279 Sk2f P0 = Sk2f::Load(&p0);
280 Sk2f P1 = Sk2f::Load(&p1);
281 Sk2f P2 = Sk2f::Load(&p2);
282 this->setW(P0, P1, P2, trans, w);
283}
284
285inline void GrCCCoverageProcessor::QuadPointInstance::setW(const Sk2f& P0, const Sk2f& P1,
286 const Sk2f& P2, const Sk2f& trans,
287 float w) {
288 Sk2f W = Sk2f(w);
289 Sk2f::Store4(this, P0 + trans, P1 + trans, P2 + trans, W);
290}
291
292#endif
293