blob: e6adfffe140c461cd5bf86a4e03796062fc44119 [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 Dalton90e8fb12017-12-22 02:24:53 -070013#include "GrShaderCaps.h"
Chris Daltona3e92712017-12-04 11:45:51 -070014#include "SkNx.h"
Chris Dalton1a325d22017-07-14 15:17:41 -060015#include "glsl/GrGLSLGeometryProcessor.h"
16#include "glsl/GrGLSLVarying.h"
17
Robert Phillips7f861922018-01-30 13:13:42 +000018class GrGLSLPPFragmentBuilder;
Chris Dalton1fbdb612017-12-12 12:48:47 -070019class GrGLSLVertexGeoBuilder;
Chris Dalton23261772017-12-10 16:41:45 -070020class GrMesh;
Chris Dalton1a325d22017-07-14 15:17:41 -060021
22/**
Chris Dalton1fbdb612017-12-12 12:48:47 -070023 * This is the geometry processor for the simple convex primitive shapes (triangles and closed,
24 * convex bezier curves) from which ccpr paths are composed. The output is a single-channel alpha
25 * value, positive for clockwise shapes and negative for counter-clockwise, that indicates coverage.
Chris Dalton1a325d22017-07-14 15:17:41 -060026 *
Chris Dalton6a3dbee2017-10-16 10:44:41 -060027 * The caller is responsible to execute all render passes for all applicable primitives into a
28 * cleared, floating point, alpha-only render target using SkBlendMode::kPlus (see RenderPass
29 * below). Once all of a path's primitives have been drawn, the render target contains a composite
Chris Dalton383a2ef2018-01-08 17:21:41 -050030 * coverage count that can then be used to draw the path (see GrCCPathProcessor).
Chris Dalton1a325d22017-07-14 15:17:41 -060031 *
Chris Dalton23261772017-12-10 16:41:45 -070032 * To draw a renderer pass, see appendMesh below.
Chris Dalton1a325d22017-07-14 15:17:41 -060033 */
Chris Dalton383a2ef2018-01-08 17:21:41 -050034class GrCCCoverageProcessor : public GrGeometryProcessor {
Chris Dalton1a325d22017-07-14 15:17:41 -060035public:
Chris Dalton84403d72018-02-13 21:46:17 -050036 // Defines a single primitive shape with 3 input points (i.e. Triangles and Quadratics).
37 // X,Y point values are transposed.
38 struct TriPointInstance {
Chris Daltona3e92712017-12-04 11:45:51 -070039 float fX[3];
40 float fY[3];
Chris Daltonc1e59632017-09-05 00:30:07 -060041
Chris Daltona3e92712017-12-04 11:45:51 -070042 void set(const SkPoint[3], const Sk2f& trans);
43 void set(const SkPoint&, const SkPoint&, const SkPoint&, const Sk2f& trans);
Chris Daltonc1e59632017-09-05 00:30:07 -060044 };
45
Chris Dalton84403d72018-02-13 21:46:17 -050046 // Defines a single primitive shape with 4 input points, or 3 input points plus a W parameter
47 // duplicated in both 4th components (i.e. Cubics or Triangles with a custom winding number).
48 // X,Y point values are transposed.
49 struct QuadPointInstance {
Chris Daltona3e92712017-12-04 11:45:51 -070050 float fX[4];
51 float fY[4];
52
53 void set(const SkPoint[4], float dx, float dy);
Chris Dalton84403d72018-02-13 21:46:17 -050054 void set(const SkPoint&, const SkPoint&, const SkPoint&, const Sk2f& trans, float w);
Chris Daltona3e92712017-12-04 11:45:51 -070055 };
Chris Dalton1a325d22017-07-14 15:17:41 -060056
Chris Dalton1fbdb612017-12-12 12:48:47 -070057 // All primitive shapes (triangles and closed, convex bezier curves) require more than one
Chris Dalton23261772017-12-10 16:41:45 -070058 // render pass. Here we enumerate every render pass needed in order to produce a complete
59 // coverage count mask. This is an exhaustive list of all ccpr coverage shaders.
Chris Dalton1fbdb612017-12-12 12:48:47 -070060 //
Chris Dalton90e8fb12017-12-22 02:24:53 -070061 // During a render pass, the "Impl" (GSImpl or VSimpl) generates conservative geometry for
Chris Dalton1fbdb612017-12-12 12:48:47 -070062 // rasterization, and the Shader decides the coverage value at each pixel.
Chris Dalton6a3dbee2017-10-16 10:44:41 -060063 enum class RenderPass {
Chris Dalton1fbdb612017-12-12 12:48:47 -070064 // For a Hull, the Impl generates a "conservative raster hull" around the input points. This
65 // is the geometry that causes a pixel to be rasterized if it is touched anywhere by the
Chris Daltonf510e262018-01-30 16:42:37 -070066 // input polygon. The input coverage values sent to the Shader at each vertex are either
Chris Dalton90e8fb12017-12-22 02:24:53 -070067 // null, or +1 all around if the Impl combines this pass with kTriangleEdges. Logically,
68 // the conservative raster hull is equivalent to the convex hull of pixel size boxes
69 // centered on each input point.
Chris Dalton1a325d22017-07-14 15:17:41 -060070 kTriangleHulls,
Chris Dalton1a325d22017-07-14 15:17:41 -060071 kQuadraticHulls,
Chris Daltonbe4ffab2017-12-08 10:59:58 -070072 kCubicHulls,
Chris Dalton1fbdb612017-12-12 12:48:47 -070073
74 // For Edges, the Impl generates conservative rasters around every input edge (i.e. convex
Chris Daltonf510e262018-01-30 16:42:37 -070075 // hulls of two pixel-size boxes centered on both of the edge's endpoints). The input
Chris Dalton1fbdb612017-12-12 12:48:47 -070076 // coverage values sent to the Shader at each vertex are -1 on the outside border of the
77 // edge geometry and 0 on the inside. This is the only geometry type that associates
78 // coverage values with the output vertices. Interpolated, these coverage values convert
79 // jagged conservative raster edges into a smooth antialiased edge.
Chris Dalton90e8fb12017-12-22 02:24:53 -070080 //
81 // NOTE: The Impl may combine this pass with kTriangleHulls, in which case DoesRenderPass()
82 // will be false for kTriangleEdges and it must not be used.
Chris Dalton1fbdb612017-12-12 12:48:47 -070083 kTriangleEdges,
84
85 // For Corners, the Impl Generates the conservative rasters of corner points (i.e.
86 // pixel-size boxes). It generates 3 corner boxes for triangles and 2 for curves. The Shader
Chris Daltonf510e262018-01-30 16:42:37 -070087 // specifies which corners. Input coverage values sent to the Shader will be null.
Chris Dalton1fbdb612017-12-12 12:48:47 -070088 kTriangleCorners,
89 kQuadraticCorners,
Chris Daltonbe4ffab2017-12-08 10:59:58 -070090 kCubicCorners
Chris Dalton1a325d22017-07-14 15:17:41 -060091 };
Chris Dalton23261772017-12-10 16:41:45 -070092 static bool RenderPassIsCubic(RenderPass);
93 static const char* RenderPassName(RenderPass);
Chris Dalton1a325d22017-07-14 15:17:41 -060094
Chris Dalton27059d32018-01-23 14:06:50 -070095 constexpr static bool DoesRenderPass(RenderPass renderPass, const GrCaps& caps) {
96 return RenderPass::kTriangleEdges != renderPass ||
97 caps.shaderCaps()->geometryShaderSupport();
Chris Dalton90e8fb12017-12-22 02:24:53 -070098 }
99
Chris Dalton84403d72018-02-13 21:46:17 -0500100 enum class WindMethod : bool {
101 kCrossProduct, // Calculate wind = +/-1 by sign of the cross product.
102 kInstanceData // Instance data provides custom, signed wind values of any magnitude.
103 // (For tightly-wound tessellated triangles.)
104 };
105
106 GrCCCoverageProcessor(GrResourceProvider* rp, RenderPass pass, WindMethod windMethod)
Chris Dalton383a2ef2018-01-08 17:21:41 -0500107 : INHERITED(kGrCCCoverageProcessor_ClassID)
Chris Dalton90e8fb12017-12-22 02:24:53 -0700108 , fRenderPass(pass)
Chris Dalton84403d72018-02-13 21:46:17 -0500109 , fWindMethod(windMethod)
110 , fImpl(rp->caps()->shaderCaps()->geometryShaderSupport() ? Impl::kGeometryShader
111 : Impl::kVertexShader) {
112 SkASSERT(DoesRenderPass(pass, *rp->caps()));
Chris Dalton90e8fb12017-12-22 02:24:53 -0700113 if (Impl::kGeometryShader == fImpl) {
114 this->initGS();
115 } else {
Chris Dalton84403d72018-02-13 21:46:17 -0500116 this->initVS(rp);
Chris Dalton90e8fb12017-12-22 02:24:53 -0700117 }
Chris Daltona3e92712017-12-04 11:45:51 -0700118 }
119
Chris Dalton23261772017-12-10 16:41:45 -0700120 // Appends a GrMesh that will draw the provided instances. The instanceBuffer must be an array
Chris Dalton84403d72018-02-13 21:46:17 -0500121 // of either TriPointInstance or QuadPointInstance, depending on this processor's RendererPass,
122 // with coordinates in the desired shape's final atlas-space position.
Chris Dalton23261772017-12-10 16:41:45 -0700123 void appendMesh(GrBuffer* instanceBuffer, int instanceCount, int baseInstance,
Chris Dalton90e8fb12017-12-22 02:24:53 -0700124 SkTArray<GrMesh>* out) {
125 if (Impl::kGeometryShader == fImpl) {
126 this->appendGSMesh(instanceBuffer, instanceCount, baseInstance, out);
127 } else {
128 this->appendVSMesh(instanceBuffer, instanceCount, baseInstance, out);
129 }
Chris Daltonbe4ffab2017-12-08 10:59:58 -0700130 }
Chris Dalton6a3dbee2017-10-16 10:44:41 -0600131
Chris Dalton23261772017-12-10 16:41:45 -0700132 // GrPrimitiveProcessor overrides.
133 const char* name() const override { return RenderPassName(fRenderPass); }
134 SkString dumpInfo() const override {
135 return SkStringPrintf("%s\n%s", this->name(), this->INHERITED::dumpInfo().c_str());
136 }
137 void getGLSLProcessorKey(const GrShaderCaps&, GrProcessorKeyBuilder*) const override;
138 GrGLSLPrimitiveProcessor* createGLSLInstance(const GrShaderCaps&) const override;
139
140#ifdef SK_DEBUG
141 // Increases the 1/2 pixel AA bloat by a factor of debugBloat and outputs color instead of
142 // coverage (coverage=+1 -> green, coverage=0 -> black, coverage=-1 -> red).
143 void enableDebugVisualizations(float debugBloat) { fDebugBloat = debugBloat; }
144 bool debugVisualizationsEnabled() const { return fDebugBloat > 0; }
145 float debugBloat() const { SkASSERT(this->debugVisualizationsEnabled()); return fDebugBloat; }
146#endif
147
Chris Dalton1fbdb612017-12-12 12:48:47 -0700148 // The Shader provides code to calculate each pixel's coverage in a RenderPass. It also
149 // provides details about shape-specific geometry.
Chris Dalton6a3dbee2017-10-16 10:44:41 -0600150 class Shader {
151 public:
Chris Dalton6a3dbee2017-10-16 10:44:41 -0600152 union GeometryVars {
153 struct {
154 const char* fAlternatePoints; // floatNx2 (if left null, will use input points).
Chris Dalton6a3dbee2017-10-16 10:44:41 -0600155 } fHullVars;
156
157 struct {
158 const char* fPoint; // float2
159 } fCornerVars;
160
161 GeometryVars() { memset(this, 0, sizeof(*this)); }
162 };
163
164 // Called before generating geometry. Subclasses must fill out the applicable fields in
165 // GeometryVars (if any), and may also use this opportunity to setup internal member
166 // variables that will be needed during onEmitVaryings (e.g. transformation matrices).
Chris Dalton1fbdb612017-12-12 12:48:47 -0700167 //
168 // repetitionID is a 0-based index and indicates which edge or corner is being generated.
169 // It will be null when generating a hull.
170 virtual void emitSetupCode(GrGLSLVertexGeoBuilder*, const char* pts,
171 const char* repetitionID, const char* wind,
172 GeometryVars*) const {}
Chris Dalton6a3dbee2017-10-16 10:44:41 -0600173
Chris Daltonf510e262018-01-30 16:42:37 -0700174 void emitVaryings(GrGLSLVaryingHandler* varyingHandler, GrGLSLVarying::Scope scope,
175 SkString* code, const char* position, const char* inputCoverage,
176 const char* wind) {
177 SkASSERT(GrGLSLVarying::Scope::kVertToGeo != scope);
178 this->onEmitVaryings(varyingHandler, scope, code, position, inputCoverage, wind);
179 }
Chris Dalton6a3dbee2017-10-16 10:44:41 -0600180
Robert Phillips7f861922018-01-30 13:13:42 +0000181 void emitFragmentCode(const GrCCCoverageProcessor& proc, GrGLSLPPFragmentBuilder*,
Chris Dalton6a3dbee2017-10-16 10:44:41 -0600182 const char* skOutputColor, const char* skOutputCoverage) const;
183
184 // Defines an equation ("dot(float3(pt, 1), distance_equation)") that is -1 on the outside
Chris Daltoncc0ab7e2017-10-24 14:16:52 -0600185 // border of a conservative raster edge and 0 on the inside. 'leftPt' and 'rightPt' must be
186 // ordered clockwise.
Chris Dalton1fbdb612017-12-12 12:48:47 -0700187 static void EmitEdgeDistanceEquation(GrGLSLVertexGeoBuilder*, const char* leftPt,
Chris Dalton6a3dbee2017-10-16 10:44:41 -0600188 const char* rightPt,
189 const char* outputDistanceEquation);
190
Chris Dalton6a3dbee2017-10-16 10:44:41 -0600191 virtual ~Shader() {}
192
193 protected:
Chris Dalton6a3dbee2017-10-16 10:44:41 -0600194 // Here the subclass adds its internal varyings to the handler and produces code to
Chris Daltonf510e262018-01-30 16:42:37 -0700195 // initialize those varyings from a given position, input coverage value, and wind.
Chris Dalton6a3dbee2017-10-16 10:44:41 -0600196 //
Chris Daltonf510e262018-01-30 16:42:37 -0700197 // NOTE: the coverage input is only relevant for edges (see comments in RenderPass).
Chris Dalton6a3dbee2017-10-16 10:44:41 -0600198 // Otherwise it is +1 all around.
Chris Daltonf510e262018-01-30 16:42:37 -0700199 virtual void onEmitVaryings(GrGLSLVaryingHandler*, GrGLSLVarying::Scope, SkString* code,
200 const char* position, const char* inputCoverage,
201 const char* wind) = 0;
Chris Dalton6a3dbee2017-10-16 10:44:41 -0600202
Chris Daltonf510e262018-01-30 16:42:37 -0700203 // Emits the fragment code that calculates a pixel's signed coverage value.
Robert Phillips7f861922018-01-30 13:13:42 +0000204 virtual void onEmitFragmentCode(GrGLSLPPFragmentBuilder*,
Chris Dalton6a3dbee2017-10-16 10:44:41 -0600205 const char* outputCoverage) const = 0;
206
Chris Dalton90e8fb12017-12-22 02:24:53 -0700207 // Returns the name of a Shader's internal varying at the point where where its value is
208 // assigned. This is intended to work whether called for a vertex or a geometry shader.
209 const char* OutName(const GrGLSLVarying& varying) const {
210 using Scope = GrGLSLVarying::Scope;
211 SkASSERT(Scope::kVertToGeo != varying.scope());
212 return Scope::kGeoToFrag == varying.scope() ? varying.gsOut() : varying.vsOut();
213 }
214
Chris Dalton1fbdb612017-12-12 12:48:47 -0700215 // Defines a global float2 array that contains MSAA sample locations as offsets from pixel
216 // center. Subclasses can use this for software multisampling.
217 //
218 // Returns the number of samples.
Robert Phillips7f861922018-01-30 13:13:42 +0000219 static int DefineSoftSampleLocations(GrGLSLPPFragmentBuilder* f, const char* samplesName);
Chris Dalton6a3dbee2017-10-16 10:44:41 -0600220 };
221
Chris Dalton6a3dbee2017-10-16 10:44:41 -0600222 class GSImpl;
Chris Dalton90e8fb12017-12-22 02:24:53 -0700223 class VSImpl;
Chris Dalton1a325d22017-07-14 15:17:41 -0600224
225private:
Chris Dalton1a325d22017-07-14 15:17:41 -0600226 // Slightly undershoot a bloat radius of 0.5 so vertices that fall on integer boundaries don't
227 // accidentally bleed into neighbor pixels.
228 static constexpr float kAABloatRadius = 0.491111f;
229
Chris Dalton1fbdb612017-12-12 12:48:47 -0700230 // Number of bezier points for curves, or 3 for triangles.
231 int numInputPoints() const { return RenderPassIsCubic(fRenderPass) ? 4 : 3; }
232
Chris Dalton90e8fb12017-12-22 02:24:53 -0700233 enum class Impl : bool {
234 kGeometryShader,
235 kVertexShader
236 };
237
Chris Dalton23261772017-12-10 16:41:45 -0700238 void initGS();
Chris Dalton84403d72018-02-13 21:46:17 -0500239 void initVS(GrResourceProvider*);
Chris Dalton90e8fb12017-12-22 02:24:53 -0700240
Chris Dalton23261772017-12-10 16:41:45 -0700241 void appendGSMesh(GrBuffer* instanceBuffer, int instanceCount, int baseInstance,
Chris Dalton90e8fb12017-12-22 02:24:53 -0700242 SkTArray<GrMesh>* out) const;
243 void appendVSMesh(GrBuffer* instanceBuffer, int instanceCount, int baseInstance,
244 SkTArray<GrMesh>* out) const;
245
Chris Dalton1fbdb612017-12-12 12:48:47 -0700246 GrGLSLPrimitiveProcessor* createGSImpl(std::unique_ptr<Shader>) const;
Chris Dalton90e8fb12017-12-22 02:24:53 -0700247 GrGLSLPrimitiveProcessor* createVSImpl(std::unique_ptr<Shader>) const;
Chris Dalton1a325d22017-07-14 15:17:41 -0600248
Chris Daltona3e92712017-12-04 11:45:51 -0700249 const RenderPass fRenderPass;
Chris Dalton84403d72018-02-13 21:46:17 -0500250 const WindMethod fWindMethod;
Chris Dalton90e8fb12017-12-22 02:24:53 -0700251 const Impl fImpl;
Chris Dalton383a2ef2018-01-08 17:21:41 -0500252 SkDEBUGCODE(float fDebugBloat = 0);
Chris Dalton1a325d22017-07-14 15:17:41 -0600253
Chris Dalton27059d32018-01-23 14:06:50 -0700254 // Used by VSImpl.
255 sk_sp<const GrBuffer> fVertexBuffer;
256 sk_sp<const GrBuffer> fIndexBuffer;
257 int fNumIndicesPerInstance;
258 GrPrimitiveType fPrimitiveType;
259
Chris Dalton6a3dbee2017-10-16 10:44:41 -0600260 typedef GrGeometryProcessor INHERITED;
Chris Dalton1a325d22017-07-14 15:17:41 -0600261};
262
Chris Dalton84403d72018-02-13 21:46:17 -0500263inline void GrCCCoverageProcessor::TriPointInstance::set(const SkPoint p[3], const Sk2f& trans) {
Chris Daltona3e92712017-12-04 11:45:51 -0700264 this->set(p[0], p[1], p[2], trans);
265}
266
Chris Dalton84403d72018-02-13 21:46:17 -0500267inline void GrCCCoverageProcessor::TriPointInstance::set(const SkPoint& p0, const SkPoint& p1,
Chris Dalton383a2ef2018-01-08 17:21:41 -0500268 const SkPoint& p2, const Sk2f& trans) {
Chris Daltona3e92712017-12-04 11:45:51 -0700269 Sk2f P0 = Sk2f::Load(&p0) + trans;
270 Sk2f P1 = Sk2f::Load(&p1) + trans;
271 Sk2f P2 = Sk2f::Load(&p2) + trans;
272 Sk2f::Store3(this, P0, P1, P2);
273}
274
Chris Dalton84403d72018-02-13 21:46:17 -0500275inline void GrCCCoverageProcessor::QuadPointInstance::set(const SkPoint p[4], float dx, float dy) {
Chris Daltona3e92712017-12-04 11:45:51 -0700276 Sk4f X,Y;
277 Sk4f::Load2(p, &X, &Y);
278 (X + dx).store(&fX);
279 (Y + dy).store(&fY);
280}
281
Chris Dalton84403d72018-02-13 21:46:17 -0500282inline void GrCCCoverageProcessor::QuadPointInstance::set(const SkPoint& p0, const SkPoint& p1,
283 const SkPoint& p2, const Sk2f& trans,
284 float w) {
285 Sk2f P0 = Sk2f::Load(&p0) + trans;
286 Sk2f P1 = Sk2f::Load(&p1) + trans;
287 Sk2f P2 = Sk2f::Load(&p2) + trans;
288 Sk2f W = Sk2f(w);
289 Sk2f::Store4(this, P0, P1, P2, W);
290}
291
Chris Dalton383a2ef2018-01-08 17:21:41 -0500292inline bool GrCCCoverageProcessor::RenderPassIsCubic(RenderPass pass) {
Chris Dalton23261772017-12-10 16:41:45 -0700293 switch (pass) {
294 case RenderPass::kTriangleHulls:
295 case RenderPass::kTriangleEdges:
296 case RenderPass::kTriangleCorners:
297 case RenderPass::kQuadraticHulls:
298 case RenderPass::kQuadraticCorners:
299 return false;
300 case RenderPass::kCubicHulls:
301 case RenderPass::kCubicCorners:
302 return true;
303 }
Chris Dalton1fbdb612017-12-12 12:48:47 -0700304 SK_ABORT("Invalid RenderPass");
Chris Dalton23261772017-12-10 16:41:45 -0700305 return false;
306}
307
Chris Dalton383a2ef2018-01-08 17:21:41 -0500308inline const char* GrCCCoverageProcessor::RenderPassName(RenderPass pass) {
Chris Dalton23261772017-12-10 16:41:45 -0700309 switch (pass) {
310 case RenderPass::kTriangleHulls: return "kTriangleHulls";
311 case RenderPass::kTriangleEdges: return "kTriangleEdges";
312 case RenderPass::kTriangleCorners: return "kTriangleCorners";
313 case RenderPass::kQuadraticHulls: return "kQuadraticHulls";
314 case RenderPass::kQuadraticCorners: return "kQuadraticCorners";
315 case RenderPass::kCubicHulls: return "kCubicHulls";
316 case RenderPass::kCubicCorners: return "kCubicCorners";
317 }
Chris Dalton1fbdb612017-12-12 12:48:47 -0700318 SK_ABORT("Invalid RenderPass");
Chris Dalton23261772017-12-10 16:41:45 -0700319 return "";
320}
321
Chris Dalton1a325d22017-07-14 15:17:41 -0600322#endif