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