blob: 40339baa07577bcac681addcc0b2b70d9e2704ba [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
Chris Dalton27059d32018-01-23 14:06:50 -070011#include "GrCaps.h"
Chris Dalton1a325d22017-07-14 15:17:41 -060012#include "GrGeometryProcessor.h"
Chris Dalton8dfc70f2018-03-26 19:15:22 -060013#include "GrPipeline.h"
Chris Dalton90e8fb12017-12-22 02:24:53 -070014#include "GrShaderCaps.h"
Chris Daltona3e92712017-12-04 11:45:51 -070015#include "SkNx.h"
Chris Dalton1a325d22017-07-14 15:17:41 -060016#include "glsl/GrGLSLGeometryProcessor.h"
17#include "glsl/GrGLSLVarying.h"
18
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 Dalton703b4762018-04-06 16:11:48 -060040 kWeightedTriangles, // Triangles (from the tessellator) whose winding magnitude > 1.
Chris Dalton8dfc70f2018-03-26 19:15:22 -060041 kQuadratics,
42 kCubics,
43 };
44 static const char* PrimitiveTypeName(PrimitiveType);
45
Chris Dalton84403d72018-02-13 21:46:17 -050046 // Defines a single primitive shape with 3 input points (i.e. Triangles and Quadratics).
47 // X,Y point values are transposed.
48 struct TriPointInstance {
Chris Daltona3e92712017-12-04 11:45:51 -070049 float fX[3];
50 float fY[3];
Chris Daltonc1e59632017-09-05 00:30:07 -060051
Chris Daltona3e92712017-12-04 11:45:51 -070052 void set(const SkPoint[3], const Sk2f& trans);
53 void set(const SkPoint&, const SkPoint&, const SkPoint&, const Sk2f& trans);
Chris Daltonc1e59632017-09-05 00:30:07 -060054 };
55
Chris Dalton84403d72018-02-13 21:46:17 -050056 // Defines a single primitive shape with 4 input points, or 3 input points plus a W parameter
57 // duplicated in both 4th components (i.e. Cubics or Triangles with a custom winding number).
58 // X,Y point values are transposed.
59 struct QuadPointInstance {
Chris Daltona3e92712017-12-04 11:45:51 -070060 float fX[4];
61 float fY[4];
62
63 void set(const SkPoint[4], float dx, float dy);
Chris Dalton703b4762018-04-06 16:11:48 -060064 void setW(const SkPoint&, const SkPoint&, const SkPoint&, const Sk2f& trans, float w);
Chris Daltona3e92712017-12-04 11:45:51 -070065 };
Chris Daltonbaf3e782018-03-08 15:55:58 +000066
Chris Dalton703b4762018-04-06 16:11:48 -060067 GrCCCoverageProcessor(GrResourceProvider* rp, PrimitiveType type)
Chris Dalton383a2ef2018-01-08 17:21:41 -050068 : INHERITED(kGrCCCoverageProcessor_ClassID)
Chris Dalton8dfc70f2018-03-26 19:15:22 -060069 , fPrimitiveType(type)
Chris Dalton84403d72018-02-13 21:46:17 -050070 , fImpl(rp->caps()->shaderCaps()->geometryShaderSupport() ? Impl::kGeometryShader
71 : Impl::kVertexShader) {
Chris Dalton90e8fb12017-12-22 02:24:53 -070072 if (Impl::kGeometryShader == fImpl) {
73 this->initGS();
74 } else {
Chris Dalton84403d72018-02-13 21:46:17 -050075 this->initVS(rp);
Chris Dalton90e8fb12017-12-22 02:24:53 -070076 }
Chris Daltona3e92712017-12-04 11:45:51 -070077 }
78
Chris Dalton23261772017-12-10 16:41:45 -070079 // GrPrimitiveProcessor overrides.
Chris Dalton8dfc70f2018-03-26 19:15:22 -060080 const char* name() const override { return PrimitiveTypeName(fPrimitiveType); }
Chris Dalton23261772017-12-10 16:41:45 -070081 SkString dumpInfo() const override {
82 return SkStringPrintf("%s\n%s", this->name(), this->INHERITED::dumpInfo().c_str());
83 }
84 void getGLSLProcessorKey(const GrShaderCaps&, GrProcessorKeyBuilder*) const override;
85 GrGLSLPrimitiveProcessor* createGLSLInstance(const GrShaderCaps&) const override;
86
87#ifdef SK_DEBUG
Chris Dalton8d38a7f2018-03-19 14:16:44 -060088 // Increases the 1/2 pixel AA bloat by a factor of debugBloat.
89 void enableDebugBloat(float debugBloat) { fDebugBloat = debugBloat; }
90 bool debugBloatEnabled() const { return fDebugBloat > 0; }
91 float debugBloat() const { SkASSERT(this->debugBloatEnabled()); return fDebugBloat; }
Chris Dalton23261772017-12-10 16:41:45 -070092#endif
93
Chris Dalton8738cf42018-03-09 11:57:40 -070094 // Appends a GrMesh that will draw the provided instances. The instanceBuffer must be an array
95 // of either TriPointInstance or QuadPointInstance, depending on this processor's RendererPass,
96 // with coordinates in the desired shape's final atlas-space position.
97 void appendMesh(GrBuffer* instanceBuffer, int instanceCount, int baseInstance,
98 SkTArray<GrMesh>* out) const {
99 if (Impl::kGeometryShader == fImpl) {
100 this->appendGSMesh(instanceBuffer, instanceCount, baseInstance, out);
101 } else {
102 this->appendVSMesh(instanceBuffer, instanceCount, baseInstance, out);
103 }
104 }
105
Chris Dalton8dfc70f2018-03-26 19:15:22 -0600106 void draw(GrOpFlushState*, const GrPipeline&, const GrMesh[], const GrPipeline::DynamicState[],
107 int meshCount, const SkRect& drawBounds) const;
108
Chris Daltonfe462ef2018-03-08 15:54:01 +0000109 // The Shader provides code to calculate each pixel's coverage in a RenderPass. It also
110 // provides details about shape-specific geometry.
Chris Dalton6a3dbee2017-10-16 10:44:41 -0600111 class Shader {
112 public:
Chris Dalton21ba5512018-03-21 17:20:21 -0600113 // Called before generating geometry. Subclasses may set up internal member variables during
114 // this time that will be needed during onEmitVaryings (e.g. transformation matrices).
Chris Daltonfe462ef2018-03-08 15:54:01 +0000115 //
Chris Dalton21ba5512018-03-21 17:20:21 -0600116 // If the optional 'tighterHull' parameter is not null and gets filled out by the subclass,
117 // the the Impl will generate geometry around those points rather than the input points.
118 virtual void emitSetupCode(GrGLSLVertexGeoBuilder*, const char* pts, const char* wind,
119 const char** tighterHull = nullptr) const {}
Chris Daltonfe462ef2018-03-08 15:54:01 +0000120
121 void emitVaryings(GrGLSLVaryingHandler* varyingHandler, GrGLSLVarying::Scope scope,
Chris Dalton04a1de52018-03-14 02:04:09 -0600122 SkString* code, const char* position, const char* coverage,
Chris Dalton4c239342018-04-05 18:43:40 -0600123 const char* cornerCoverage) {
Chris Daltonfe462ef2018-03-08 15:54:01 +0000124 SkASSERT(GrGLSLVarying::Scope::kVertToGeo != scope);
Chris Dalton4c239342018-04-05 18:43:40 -0600125 this->onEmitVaryings(varyingHandler, scope, code, position, coverage, cornerCoverage);
Chris Daltonfe462ef2018-03-08 15:54:01 +0000126 }
Chris Dalton622650a2018-03-07 17:30:10 -0700127
Chris Dalton0a793812018-03-07 11:18:30 -0700128 void emitFragmentCode(const GrCCCoverageProcessor&, GrGLSLFPFragmentBuilder*,
Chris Dalton6a3dbee2017-10-16 10:44:41 -0600129 const char* skOutputColor, const char* skOutputCoverage) const;
130
Chris Daltonbaf3e782018-03-08 15:55:58 +0000131 // Defines an equation ("dot(float3(pt, 1), distance_equation)") that is -1 on the outside
132 // border of a conservative raster edge and 0 on the inside. 'leftPt' and 'rightPt' must be
133 // ordered clockwise.
134 static void EmitEdgeDistanceEquation(GrGLSLVertexGeoBuilder*, const char* leftPt,
135 const char* rightPt,
136 const char* outputDistanceEquation);
137
Chris Dalton0a793812018-03-07 11:18:30 -0700138 // Calculates an edge's coverage at a conservative raster vertex. The edge is defined by two
139 // clockwise-ordered points, 'leftPt' and 'rightPt'. 'rasterVertexDir' is a pair of +/-1
140 // values that point in the direction of conservative raster bloat, starting from an
141 // endpoint.
142 //
143 // Coverage values ramp from -1 (completely outside the edge) to 0 (completely inside).
144 static void CalcEdgeCoverageAtBloatVertex(GrGLSLVertexGeoBuilder*, const char* leftPt,
145 const char* rightPt, const char* rasterVertexDir,
146 const char* outputCoverage);
147
Chris Dalton8738cf42018-03-09 11:57:40 -0700148 // Calculates an edge's coverage at two conservative raster vertices.
149 // (See CalcEdgeCoverageAtBloatVertex).
150 static void CalcEdgeCoveragesAtBloatVertices(GrGLSLVertexGeoBuilder*, const char* leftPt,
151 const char* rightPt, const char* bloatDir1,
152 const char* bloatDir2,
153 const char* outputCoverages);
154
Chris Dalton04a1de52018-03-14 02:04:09 -0600155 // Corner boxes require an additional "attenuation" varying that is multiplied by the
156 // regular (linearly-interpolated) coverage. This function calculates the attenuation value
157 // to use in the single, outermost vertex. The remaining three vertices of the corner box
158 // all use an attenuation value of 1.
Chris Dalton4c239342018-04-05 18:43:40 -0600159 static void CalcCornerAttenuation(GrGLSLVertexGeoBuilder*, const char* leftDir,
160 const char* rightDir, const char* outputAttenuation);
Chris Dalton04a1de52018-03-14 02:04:09 -0600161
Chris Dalton6a3dbee2017-10-16 10:44:41 -0600162 virtual ~Shader() {}
163
164 protected:
Chris Dalton622650a2018-03-07 17:30:10 -0700165 // Here the subclass adds its internal varyings to the handler and produces code to
Chris Dalton21ba5512018-03-21 17:20:21 -0600166 // initialize those varyings from a given position and coverage values.
Chris Dalton622650a2018-03-07 17:30:10 -0700167 //
Chris Dalton21ba5512018-03-21 17:20:21 -0600168 // NOTE: the coverage values are signed appropriately for wind.
169 // 'coverage' will only be +1 or -1 on curves.
Chris Daltonfe462ef2018-03-08 15:54:01 +0000170 virtual void onEmitVaryings(GrGLSLVaryingHandler*, GrGLSLVarying::Scope, SkString* code,
Chris Dalton04a1de52018-03-14 02:04:09 -0600171 const char* position, const char* coverage,
Chris Dalton4c239342018-04-05 18:43:40 -0600172 const char* cornerCoverage) = 0;
Chris Dalton622650a2018-03-07 17:30:10 -0700173
Chris Daltonfe462ef2018-03-08 15:54:01 +0000174 // Emits the fragment code that calculates a pixel's signed coverage value.
Chris Daltonbaf3e782018-03-08 15:55:58 +0000175 virtual void onEmitFragmentCode(GrGLSLFPFragmentBuilder*,
Chris Daltonfe462ef2018-03-08 15:54:01 +0000176 const char* outputCoverage) const = 0;
Chris Dalton6a3dbee2017-10-16 10:44:41 -0600177
Chris Dalton90e8fb12017-12-22 02:24:53 -0700178 // Returns the name of a Shader's internal varying at the point where where its value is
179 // assigned. This is intended to work whether called for a vertex or a geometry shader.
180 const char* OutName(const GrGLSLVarying& varying) const {
181 using Scope = GrGLSLVarying::Scope;
182 SkASSERT(Scope::kVertToGeo != varying.scope());
183 return Scope::kGeoToFrag == varying.scope() ? varying.gsOut() : varying.vsOut();
184 }
Chris Dalton4c239342018-04-05 18:43:40 -0600185
186 // Our friendship with GrGLSLShaderBuilder does not propogate to subclasses.
187 inline static SkString& AccessCodeString(GrGLSLShaderBuilder* s) { return s->code(); }
Chris Dalton6a3dbee2017-10-16 10:44:41 -0600188 };
189
Chris Dalton4c239342018-04-05 18:43:40 -0600190private:
Chris Dalton6a3dbee2017-10-16 10:44:41 -0600191 class GSImpl;
Chris Dalton21ba5512018-03-21 17:20:21 -0600192 class GSTriangleHullImpl;
193 class GSCurveHullImpl;
194 class GSCornerImpl;
Chris Dalton90e8fb12017-12-22 02:24:53 -0700195 class VSImpl;
Chris Dalton4c239342018-04-05 18:43:40 -0600196 class TriangleShader;
Chris Dalton1a325d22017-07-14 15:17:41 -0600197
Chris Dalton1a325d22017-07-14 15:17:41 -0600198 // Slightly undershoot a bloat radius of 0.5 so vertices that fall on integer boundaries don't
199 // accidentally bleed into neighbor pixels.
200 static constexpr float kAABloatRadius = 0.491111f;
201
Chris Dalton1fbdb612017-12-12 12:48:47 -0700202 // Number of bezier points for curves, or 3 for triangles.
Chris Dalton8dfc70f2018-03-26 19:15:22 -0600203 int numInputPoints() const { return PrimitiveType::kCubics == fPrimitiveType ? 4 : 3; }
Chris Dalton1fbdb612017-12-12 12:48:47 -0700204
Chris Dalton90e8fb12017-12-22 02:24:53 -0700205 enum class Impl : bool {
206 kGeometryShader,
207 kVertexShader
208 };
209
Chris Dalton8dfc70f2018-03-26 19:15:22 -0600210 // Geometry shader backend draws primitives in two subpasses.
211 enum class GSSubpass : bool {
212 kHulls,
213 kCorners
214 };
215
216 GrCCCoverageProcessor(const GrCCCoverageProcessor& proc, GSSubpass subpass)
217 : INHERITED(kGrCCCoverageProcessor_ClassID)
218 , fPrimitiveType(proc.fPrimitiveType)
Chris Dalton8dfc70f2018-03-26 19:15:22 -0600219 , fImpl(Impl::kGeometryShader)
220 SkDEBUGCODE(, fDebugBloat(proc.fDebugBloat))
221 , fGSSubpass(subpass) {
222 SkASSERT(Impl::kGeometryShader == proc.fImpl);
223 this->initGS();
224 }
225
Chris Dalton23261772017-12-10 16:41:45 -0700226 void initGS();
Chris Dalton84403d72018-02-13 21:46:17 -0500227 void initVS(GrResourceProvider*);
Chris Dalton90e8fb12017-12-22 02:24:53 -0700228
Chris Dalton23261772017-12-10 16:41:45 -0700229 void appendGSMesh(GrBuffer* instanceBuffer, int instanceCount, int baseInstance,
Chris Dalton90e8fb12017-12-22 02:24:53 -0700230 SkTArray<GrMesh>* out) const;
231 void appendVSMesh(GrBuffer* instanceBuffer, int instanceCount, int baseInstance,
232 SkTArray<GrMesh>* out) const;
233
Chris Dalton1fbdb612017-12-12 12:48:47 -0700234 GrGLSLPrimitiveProcessor* createGSImpl(std::unique_ptr<Shader>) const;
Chris Dalton90e8fb12017-12-22 02:24:53 -0700235 GrGLSLPrimitiveProcessor* createVSImpl(std::unique_ptr<Shader>) const;
Chris Dalton1a325d22017-07-14 15:17:41 -0600236
Chris Dalton8dfc70f2018-03-26 19:15:22 -0600237 const PrimitiveType fPrimitiveType;
Chris Dalton90e8fb12017-12-22 02:24:53 -0700238 const Impl fImpl;
Chris Dalton383a2ef2018-01-08 17:21:41 -0500239 SkDEBUGCODE(float fDebugBloat = 0);
Chris Dalton1a325d22017-07-14 15:17:41 -0600240
Chris Dalton8dfc70f2018-03-26 19:15:22 -0600241 // Used by GSImpl.
242 const GSSubpass fGSSubpass = GSSubpass::kHulls;
243
Chris Dalton27059d32018-01-23 14:06:50 -0700244 // Used by VSImpl.
Chris Dalton8dfc70f2018-03-26 19:15:22 -0600245 sk_sp<const GrBuffer> fVSVertexBuffer;
246 sk_sp<const GrBuffer> fVSIndexBuffer;
247 int fVSNumIndicesPerInstance;
248 GrPrimitiveType fVSTriangleType;
Chris Dalton27059d32018-01-23 14:06:50 -0700249
Chris Dalton6a3dbee2017-10-16 10:44:41 -0600250 typedef GrGeometryProcessor INHERITED;
Chris Dalton1a325d22017-07-14 15:17:41 -0600251};
252
Chris Dalton8dfc70f2018-03-26 19:15:22 -0600253inline const char* GrCCCoverageProcessor::PrimitiveTypeName(PrimitiveType type) {
254 switch (type) {
255 case PrimitiveType::kTriangles: return "kTriangles";
Chris Dalton703b4762018-04-06 16:11:48 -0600256 case PrimitiveType::kWeightedTriangles: return "kWeightedTriangles";
Chris Dalton8dfc70f2018-03-26 19:15:22 -0600257 case PrimitiveType::kQuadratics: return "kQuadratics";
258 case PrimitiveType::kCubics: return "kCubics";
259 }
260 SK_ABORT("Invalid PrimitiveType");
261 return "";
262}
263
Chris Dalton84403d72018-02-13 21:46:17 -0500264inline void GrCCCoverageProcessor::TriPointInstance::set(const SkPoint p[3], const Sk2f& trans) {
Chris Daltona3e92712017-12-04 11:45:51 -0700265 this->set(p[0], p[1], p[2], trans);
266}
267
Chris Dalton84403d72018-02-13 21:46:17 -0500268inline void GrCCCoverageProcessor::TriPointInstance::set(const SkPoint& p0, const SkPoint& p1,
Chris Dalton383a2ef2018-01-08 17:21:41 -0500269 const SkPoint& p2, const Sk2f& trans) {
Chris Daltona3e92712017-12-04 11:45:51 -0700270 Sk2f P0 = Sk2f::Load(&p0) + trans;
271 Sk2f P1 = Sk2f::Load(&p1) + trans;
272 Sk2f P2 = Sk2f::Load(&p2) + trans;
273 Sk2f::Store3(this, P0, P1, P2);
274}
275
Chris Dalton84403d72018-02-13 21:46:17 -0500276inline void GrCCCoverageProcessor::QuadPointInstance::set(const SkPoint p[4], float dx, float dy) {
Chris Daltona3e92712017-12-04 11:45:51 -0700277 Sk4f X,Y;
278 Sk4f::Load2(p, &X, &Y);
279 (X + dx).store(&fX);
280 (Y + dy).store(&fY);
281}
282
Chris Dalton703b4762018-04-06 16:11:48 -0600283inline void GrCCCoverageProcessor::QuadPointInstance::setW(const SkPoint& p0, const SkPoint& p1,
284 const SkPoint& p2, const Sk2f& trans,
285 float w) {
Chris Dalton84403d72018-02-13 21:46:17 -0500286 Sk2f P0 = Sk2f::Load(&p0) + trans;
287 Sk2f P1 = Sk2f::Load(&p1) + trans;
288 Sk2f P2 = Sk2f::Load(&p2) + trans;
289 Sk2f W = Sk2f(w);
290 Sk2f::Store4(this, P0, P1, P2, W);
291}
292
Chris Dalton1a325d22017-07-14 15:17:41 -0600293#endif