blob: 5fdb488ea5a286a953c452d5ad7957df87f47765 [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
Chris Dalton383a2ef2018-01-08 17:21:41 -05008#ifndef GrCCCoverageProcessor_DEFINED
9#define GrCCCoverageProcessor_DEFINED
Chris Dalton1a325d22017-07-14 15:17:41 -060010
Mike Kleinc0bd9f92019-04-23 12:05:21 -050011#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"
Chris Dalton1a325d22017-07-14 15:17:41 -060018
Chris Dalton60283612018-02-14 13:38:14 -070019class GrGLSLFPFragmentBuilder;
Chris Dalton1fbdb612017-12-12 12:48:47 -070020class GrGLSLVertexGeoBuilder;
Chris Dalton23261772017-12-10 16:41:45 -070021class GrMesh;
Chris Dalton8dfc70f2018-03-26 19:15:22 -060022class GrOpFlushState;
Chris Dalton1a325d22017-07-14 15:17:41 -060023
24/**
Chris Dalton1fbdb612017-12-12 12:48:47 -070025 * 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.
Chris Dalton1a325d22017-07-14 15:17:41 -060028 *
Chris Dalton8dfc70f2018-03-26 19:15:22 -060029 * 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).
Chris Dalton1a325d22017-07-14 15:17:41 -060033 *
Chris Dalton8dfc70f2018-03-26 19:15:22 -060034 * To draw primitives, use appendMesh() and draw() (defined below).
Chris Dalton1a325d22017-07-14 15:17:41 -060035 */
Chris Dalton383a2ef2018-01-08 17:21:41 -050036class GrCCCoverageProcessor : public GrGeometryProcessor {
Chris Dalton1a325d22017-07-14 15:17:41 -060037public:
Chris Dalton8dfc70f2018-03-26 19:15:22 -060038 enum class PrimitiveType {
39 kTriangles,
Chris Dalton2c5e0112019-03-29 13:14:18 -050040 kWeightedTriangles, // Triangles (from the tessellator) whose winding magnitude > 1.
Chris Dalton8dfc70f2018-03-26 19:15:22 -060041 kQuadratics,
42 kCubics,
Chris Dalton9f2dab02018-04-18 14:07:03 -060043 kConics
Chris Dalton8dfc70f2018-03-26 19:15:22 -060044 };
45 static const char* PrimitiveTypeName(PrimitiveType);
46
Chris Dalton84403d72018-02-13 21:46:17 -050047 // Defines a single primitive shape with 3 input points (i.e. Triangles and Quadratics).
48 // X,Y point values are transposed.
49 struct TriPointInstance {
Chris Daltonc3318f02019-07-19 14:20:53 -060050 float fValues[6];
Chris Daltonc1e59632017-09-05 00:30:07 -060051
Chris Daltonc3318f02019-07-19 14:20:53 -060052 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);
Chris Daltonc1e59632017-09-05 00:30:07 -060060 };
61
Chris Dalton9f2dab02018-04-18 14:07:03 -060062 // 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.
Chris Dalton84403d72018-02-13 21:46:17 -050065 struct QuadPointInstance {
Chris Daltona3e92712017-12-04 11:45:51 -070066 float fX[4];
67 float fY[4];
68
69 void set(const SkPoint[4], float dx, float dy);
Chris Dalton9f2dab02018-04-18 14:07:03 -060070 void setW(const SkPoint[3], const Sk2f& trans, float w);
Chris Dalton703b4762018-04-06 16:11:48 -060071 void setW(const SkPoint&, const SkPoint&, const SkPoint&, const Sk2f& trans, float w);
Chris Dalton09a7bb22018-08-31 19:53:15 +080072 void setW(const Sk2f& P0, const Sk2f& P1, const Sk2f& P2, const Sk2f& trans, float w);
Chris Daltona3e92712017-12-04 11:45:51 -070073 };
Chris Daltonbaf3e782018-03-08 15:55:58 +000074
Chris Dalton2c5e0112019-03-29 13:14:18 -050075 virtual void reset(PrimitiveType, GrResourceProvider*) = 0;
76
77 PrimitiveType primitiveType() const { return fPrimitiveType; }
78
79 // Number of bezier points for curves, or 3 for triangles.
80 int numInputPoints() const { return PrimitiveType::kCubics == fPrimitiveType ? 4 : 3; }
81
82 bool isTriangles() const {
83 return PrimitiveType::kTriangles == fPrimitiveType ||
84 PrimitiveType::kWeightedTriangles == fPrimitiveType;
85 }
86
87 int hasInputWeight() const {
88 return PrimitiveType::kWeightedTriangles == fPrimitiveType ||
89 PrimitiveType::kConics == fPrimitiveType;
Chris Daltona3e92712017-12-04 11:45:51 -070090 }
91
Chris Dalton23261772017-12-10 16:41:45 -070092 // GrPrimitiveProcessor overrides.
Chris Dalton8dfc70f2018-03-26 19:15:22 -060093 const char* name() const override { return PrimitiveTypeName(fPrimitiveType); }
Brian Osman9a390ac2018-11-12 09:47:48 -050094#ifdef SK_DEBUG
Chris Dalton23261772017-12-10 16:41:45 -070095 SkString dumpInfo() const override {
96 return SkStringPrintf("%s\n%s", this->name(), this->INHERITED::dumpInfo().c_str());
97 }
Brian Osman9a390ac2018-11-12 09:47:48 -050098#endif
Chris Dalton2c5e0112019-03-29 13:14:18 -050099 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;
Chris Dalton23261772017-12-10 16:41:45 -0700104
105#ifdef SK_DEBUG
Chris Dalton8d38a7f2018-03-19 14:16:44 -0600106 // Increases the 1/2 pixel AA bloat by a factor of debugBloat.
107 void enableDebugBloat(float debugBloat) { fDebugBloat = debugBloat; }
108 bool debugBloatEnabled() const { return fDebugBloat > 0; }
109 float debugBloat() const { SkASSERT(this->debugBloatEnabled()); return fDebugBloat; }
Chris Dalton2c5e0112019-03-29 13:14:18 -0500110 void getDebugBloatKey(GrProcessorKeyBuilder* b) const {
111 uint32_t bloatBits;
112 memcpy(&bloatBits, &fDebugBloat, 4);
113 b->add32(bloatBits);
114 }
Chris Dalton23261772017-12-10 16:41:45 -0700115#endif
116
Chris Dalton8738cf42018-03-09 11:57:40 -0700117 // 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.
Chris Dalton2c5e0112019-03-29 13:14:18 -0500120 virtual void appendMesh(sk_sp<const GrGpuBuffer> instanceBuffer, int instanceCount,
121 int baseInstance, SkTArray<GrMesh>* out) const = 0;
Chris Dalton8738cf42018-03-09 11:57:40 -0700122
Chris Dalton2c5e0112019-03-29 13:14:18 -0500123 virtual void draw(GrOpFlushState*, const GrPipeline&, const SkIRect scissorRects[],
124 const GrMesh[], int meshCount, const SkRect& drawBounds) const;
Chris Dalton8dfc70f2018-03-26 19:15:22 -0600125
Chris Daltonfe462ef2018-03-08 15:54:01 +0000126 // The Shader provides code to calculate each pixel's coverage in a RenderPass. It also
127 // provides details about shape-specific geometry.
Chris Dalton6a3dbee2017-10-16 10:44:41 -0600128 class Shader {
129 public:
Chris Dalton84d36cd2019-04-17 14:47:17 -0600130 // 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.
132 virtual bool calculatesOwnEdgeCoverage() const { return false; }
133
Chris Dalton21ba5512018-03-21 17:20:21 -0600134 // 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).
Chris Daltonfe462ef2018-03-08 15:54:01 +0000136 //
Chris Dalton9713dcf2018-04-10 00:48:41 -0600137 // 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.
Chris Dalton84d36cd2019-04-17 14:47:17 -0600140 virtual void emitSetupCode(
141 GrGLSLVertexGeoBuilder*, const char* pts, const char** outHull4 = nullptr) const {
Chris Dalton9713dcf2018-04-10 00:48:41 -0600142 SkASSERT(!outHull4);
143 }
Chris Daltonfe462ef2018-03-08 15:54:01 +0000144
Chris Dalton84d36cd2019-04-17 14:47:17 -0600145 void emitVaryings(
146 GrGLSLVaryingHandler* varyingHandler, GrGLSLVarying::Scope scope, SkString* code,
147 const char* position, const char* coverage, const char* cornerCoverage,
148 const char* wind) {
Chris Daltonfe462ef2018-03-08 15:54:01 +0000149 SkASSERT(GrGLSLVarying::Scope::kVertToGeo != scope);
Chris Dalton84d36cd2019-04-17 14:47:17 -0600150 this->onEmitVaryings(
151 varyingHandler, scope, code, position, coverage, cornerCoverage, wind);
Chris Daltonfe462ef2018-03-08 15:54:01 +0000152 }
Chris Dalton622650a2018-03-07 17:30:10 -0700153
Chris Daltonc3318f02019-07-19 14:20:53 -0600154 // 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;
Chris Dalton6a3dbee2017-10-16 10:44:41 -0600160
Chris Dalton6f5e77a2018-04-23 21:14:42 -0600161 // 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
Chris Dalton0a793812018-03-07 11:18:30 -0700166 // 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
Chris Dalton8738cf42018-03-09 11:57:40 -0700176 // 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
Chris Dalton04a1de52018-03-14 02:04:09 -0600183 // 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.
Chris Dalton4c239342018-04-05 18:43:40 -0600187 static void CalcCornerAttenuation(GrGLSLVertexGeoBuilder*, const char* leftDir,
188 const char* rightDir, const char* outputAttenuation);
Chris Dalton04a1de52018-03-14 02:04:09 -0600189
Chris Dalton6a3dbee2017-10-16 10:44:41 -0600190 virtual ~Shader() {}
191
192 protected:
Chris Dalton622650a2018-03-07 17:30:10 -0700193 // Here the subclass adds its internal varyings to the handler and produces code to
Chris Dalton21ba5512018-03-21 17:20:21 -0600194 // initialize those varyings from a given position and coverage values.
Chris Dalton622650a2018-03-07 17:30:10 -0700195 //
Chris Dalton21ba5512018-03-21 17:20:21 -0600196 // NOTE: the coverage values are signed appropriately for wind.
197 // 'coverage' will only be +1 or -1 on curves.
Chris Dalton84d36cd2019-04-17 14:47:17 -0600198 virtual void onEmitVaryings(
199 GrGLSLVaryingHandler*, GrGLSLVarying::Scope, SkString* code, const char* position,
200 const char* coverage, const char* cornerCoverage, const char* wind) = 0;
Chris Dalton622650a2018-03-07 17:30:10 -0700201
Chris Dalton90e8fb12017-12-22 02:24:53 -0700202 // 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.
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 }
Chris Dalton4c239342018-04-05 18:43:40 -0600209
Chris Dalton2c5e0112019-03-29 13:14:18 -0500210 // Our friendship with GrGLSLShaderBuilder does not propagate to subclasses.
Chris Dalton4c239342018-04-05 18:43:40 -0600211 inline static SkString& AccessCodeString(GrGLSLShaderBuilder* s) { return s->code(); }
Chris Dalton6a3dbee2017-10-16 10:44:41 -0600212 };
213
Chris Dalton2c5e0112019-03-29 13:14:18 -0500214protected:
Chris Dalton1a325d22017-07-14 15:17:41 -0600215 // 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
Chris Dalton2c5e0112019-03-29 13:14:18 -0500219 GrCCCoverageProcessor(ClassID classID) : INHERITED(classID) {}
Chris Dalton1fbdb612017-12-12 12:48:47 -0700220
Chris Dalton2c5e0112019-03-29 13:14:18 -0500221 virtual GrGLSLPrimitiveProcessor* onCreateGLSLInstance(std::unique_ptr<Shader>) const = 0;
Chris Dalton6f5e77a2018-04-23 21:14:42 -0600222
Chris Dalton2c5e0112019-03-29 13:14:18 -0500223 // Our friendship with GrGLSLShaderBuilder does not propagate to subclasses.
224 inline static SkString& AccessCodeString(GrGLSLShaderBuilder* s) { return s->code(); }
Chris Dalton9f2dab02018-04-18 14:07:03 -0600225
Chris Dalton2c5e0112019-03-29 13:14:18 -0500226 PrimitiveType fPrimitiveType;
Chris Dalton383a2ef2018-01-08 17:21:41 -0500227 SkDEBUGCODE(float fDebugBloat = 0);
Chris Dalton1a325d22017-07-14 15:17:41 -0600228
Chris Dalton2c5e0112019-03-29 13:14:18 -0500229 class TriangleShader;
Chris Dalton27059d32018-01-23 14:06:50 -0700230
Chris Dalton6a3dbee2017-10-16 10:44:41 -0600231 typedef GrGeometryProcessor INHERITED;
Chris Dalton1a325d22017-07-14 15:17:41 -0600232};
233
Chris Dalton8dfc70f2018-03-26 19:15:22 -0600234inline const char* GrCCCoverageProcessor::PrimitiveTypeName(PrimitiveType type) {
235 switch (type) {
236 case PrimitiveType::kTriangles: return "kTriangles";
Chris Dalton703b4762018-04-06 16:11:48 -0600237 case PrimitiveType::kWeightedTriangles: return "kWeightedTriangles";
Chris Dalton8dfc70f2018-03-26 19:15:22 -0600238 case PrimitiveType::kQuadratics: return "kQuadratics";
239 case PrimitiveType::kCubics: return "kCubics";
Chris Dalton9f2dab02018-04-18 14:07:03 -0600240 case PrimitiveType::kConics: return "kConics";
Chris Dalton8dfc70f2018-03-26 19:15:22 -0600241 }
242 SK_ABORT("Invalid PrimitiveType");
Chris Dalton8dfc70f2018-03-26 19:15:22 -0600243}
244
Chris Daltonc3318f02019-07-19 14:20:53 -0600245inline 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);
Chris Daltona3e92712017-12-04 11:45:51 -0700248}
249
Chris Daltonc3318f02019-07-19 14:20:53 -0600250inline void GrCCCoverageProcessor::TriPointInstance::set(
251 const SkPoint& p0, const SkPoint& p1, const SkPoint& p2, const Sk2f& translate,
252 Ordering ordering) {
Chris Dalton09a7bb22018-08-31 19:53:15 +0800253 Sk2f P0 = Sk2f::Load(&p0);
254 Sk2f P1 = Sk2f::Load(&p1);
255 Sk2f P2 = Sk2f::Load(&p2);
Chris Daltonc3318f02019-07-19 14:20:53 -0600256 this->set(P0, P1, P2, translate, ordering);
Chris Dalton09a7bb22018-08-31 19:53:15 +0800257}
258
Chris Daltonc3318f02019-07-19 14:20:53 -0600259inline 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 }
Chris Daltona3e92712017-12-04 11:45:51 -0700268}
269
Chris Dalton84403d72018-02-13 21:46:17 -0500270inline void GrCCCoverageProcessor::QuadPointInstance::set(const SkPoint p[4], float dx, float dy) {
Chris Daltona3e92712017-12-04 11:45:51 -0700271 Sk4f X,Y;
272 Sk4f::Load2(p, &X, &Y);
273 (X + dx).store(&fX);
274 (Y + dy).store(&fY);
275}
276
Chris Dalton9f2dab02018-04-18 14:07:03 -0600277inline 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
Chris Dalton703b4762018-04-06 16:11:48 -0600282inline void GrCCCoverageProcessor::QuadPointInstance::setW(const SkPoint& p0, const SkPoint& p1,
283 const SkPoint& p2, const Sk2f& trans,
284 float w) {
Chris Dalton09a7bb22018-08-31 19:53:15 +0800285 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
291inline void GrCCCoverageProcessor::QuadPointInstance::setW(const Sk2f& P0, const Sk2f& P1,
292 const Sk2f& P2, const Sk2f& trans,
293 float w) {
Chris Dalton84403d72018-02-13 21:46:17 -0500294 Sk2f W = Sk2f(w);
Chris Dalton09a7bb22018-08-31 19:53:15 +0800295 Sk2f::Store4(this, P0 + trans, P1 + trans, P2 + trans, W);
Chris Dalton84403d72018-02-13 21:46:17 -0500296}
297
Chris Dalton1a325d22017-07-14 15:17:41 -0600298#endif