blob: 1296e089dc175804c6dc337833dc42f6b1c86393 [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 Dalton23261772017-12-10 16:41:45 -070036 // Defines a single triangle or closed quadratic bezier, with transposed x,y point values.
37 struct TriangleInstance {
Chris Daltona3e92712017-12-04 11:45:51 -070038 float fX[3];
39 float fY[3];
Chris Daltonc1e59632017-09-05 00:30:07 -060040
Chris Daltona3e92712017-12-04 11:45:51 -070041 void set(const SkPoint[3], const Sk2f& trans);
42 void set(const SkPoint&, const SkPoint&, const SkPoint&, const Sk2f& trans);
Chris Daltonc1e59632017-09-05 00:30:07 -060043 };
44
Chris Dalton23261772017-12-10 16:41:45 -070045 // Defines a single closed cubic bezier, with transposed x,y point values.
Chris Daltona3e92712017-12-04 11:45:51 -070046 struct CubicInstance {
47 float fX[4];
48 float fY[4];
49
50 void set(const SkPoint[4], float dx, float dy);
51 };
Chris Dalton1a325d22017-07-14 15:17:41 -060052
Chris Dalton1fbdb612017-12-12 12:48:47 -070053 // All primitive shapes (triangles and closed, convex bezier curves) require more than one
Chris Dalton23261772017-12-10 16:41:45 -070054 // render pass. Here we enumerate every render pass needed in order to produce a complete
55 // coverage count mask. This is an exhaustive list of all ccpr coverage shaders.
Chris Dalton1fbdb612017-12-12 12:48:47 -070056 //
Chris Dalton90e8fb12017-12-22 02:24:53 -070057 // During a render pass, the "Impl" (GSImpl or VSimpl) generates conservative geometry for
Chris Dalton1fbdb612017-12-12 12:48:47 -070058 // rasterization, and the Shader decides the coverage value at each pixel.
Chris Dalton6a3dbee2017-10-16 10:44:41 -060059 enum class RenderPass {
Chris Dalton1fbdb612017-12-12 12:48:47 -070060 // For a Hull, the Impl generates a "conservative raster hull" around the input points. This
61 // is the geometry that causes a pixel to be rasterized if it is touched anywhere by the
Chris Daltonf510e262018-01-30 16:42:37 -070062 // input polygon. The input coverage values sent to the Shader at each vertex are either
Chris Dalton90e8fb12017-12-22 02:24:53 -070063 // null, or +1 all around if the Impl combines this pass with kTriangleEdges. Logically,
64 // the conservative raster hull is equivalent to the convex hull of pixel size boxes
65 // centered on each input point.
Chris Dalton1a325d22017-07-14 15:17:41 -060066 kTriangleHulls,
Chris Dalton1a325d22017-07-14 15:17:41 -060067 kQuadraticHulls,
Chris Daltonbe4ffab2017-12-08 10:59:58 -070068 kCubicHulls,
Chris Dalton1fbdb612017-12-12 12:48:47 -070069
70 // For Edges, the Impl generates conservative rasters around every input edge (i.e. convex
Chris Daltonf510e262018-01-30 16:42:37 -070071 // hulls of two pixel-size boxes centered on both of the edge's endpoints). The input
Chris Dalton1fbdb612017-12-12 12:48:47 -070072 // coverage values sent to the Shader at each vertex are -1 on the outside border of the
73 // edge geometry and 0 on the inside. This is the only geometry type that associates
74 // coverage values with the output vertices. Interpolated, these coverage values convert
75 // jagged conservative raster edges into a smooth antialiased edge.
Chris Dalton90e8fb12017-12-22 02:24:53 -070076 //
77 // NOTE: The Impl may combine this pass with kTriangleHulls, in which case DoesRenderPass()
78 // will be false for kTriangleEdges and it must not be used.
Chris Dalton1fbdb612017-12-12 12:48:47 -070079 kTriangleEdges,
80
81 // For Corners, the Impl Generates the conservative rasters of corner points (i.e.
82 // pixel-size boxes). It generates 3 corner boxes for triangles and 2 for curves. The Shader
Chris Daltonf510e262018-01-30 16:42:37 -070083 // specifies which corners. Input coverage values sent to the Shader will be null.
Chris Dalton1fbdb612017-12-12 12:48:47 -070084 kTriangleCorners,
85 kQuadraticCorners,
Chris Daltonbe4ffab2017-12-08 10:59:58 -070086 kCubicCorners
Chris Dalton1a325d22017-07-14 15:17:41 -060087 };
Chris Dalton23261772017-12-10 16:41:45 -070088 static bool RenderPassIsCubic(RenderPass);
89 static const char* RenderPassName(RenderPass);
Chris Dalton1a325d22017-07-14 15:17:41 -060090
Chris Dalton27059d32018-01-23 14:06:50 -070091 constexpr static bool DoesRenderPass(RenderPass renderPass, const GrCaps& caps) {
92 return RenderPass::kTriangleEdges != renderPass ||
93 caps.shaderCaps()->geometryShaderSupport();
Chris Dalton90e8fb12017-12-22 02:24:53 -070094 }
95
Chris Dalton27059d32018-01-23 14:06:50 -070096 GrCCCoverageProcessor(GrResourceProvider* rp, RenderPass pass, const GrCaps& caps)
Chris Dalton383a2ef2018-01-08 17:21:41 -050097 : INHERITED(kGrCCCoverageProcessor_ClassID)
Chris Dalton90e8fb12017-12-22 02:24:53 -070098 , fRenderPass(pass)
Chris Dalton27059d32018-01-23 14:06:50 -070099 , fImpl(caps.shaderCaps()->geometryShaderSupport() ? Impl::kGeometryShader
100 : Impl::kVertexShader) {
Chris Dalton90e8fb12017-12-22 02:24:53 -0700101 SkASSERT(DoesRenderPass(pass, caps));
102 if (Impl::kGeometryShader == fImpl) {
103 this->initGS();
104 } else {
Chris Dalton27059d32018-01-23 14:06:50 -0700105 this->initVS(rp, caps);
Chris Dalton90e8fb12017-12-22 02:24:53 -0700106 }
Chris Daltona3e92712017-12-04 11:45:51 -0700107 }
108
Chris Dalton23261772017-12-10 16:41:45 -0700109 // Appends a GrMesh that will draw the provided instances. The instanceBuffer must be an array
110 // of either TriangleInstance or CubicInstance, depending on this processor's RendererPass, with
111 // coordinates in the desired shape's final atlas-space position.
112 //
113 // NOTE: Quadratics use TriangleInstance since both have 3 points.
114 void appendMesh(GrBuffer* instanceBuffer, int instanceCount, int baseInstance,
Chris Dalton90e8fb12017-12-22 02:24:53 -0700115 SkTArray<GrMesh>* out) {
116 if (Impl::kGeometryShader == fImpl) {
117 this->appendGSMesh(instanceBuffer, instanceCount, baseInstance, out);
118 } else {
119 this->appendVSMesh(instanceBuffer, instanceCount, baseInstance, out);
120 }
Chris Daltonbe4ffab2017-12-08 10:59:58 -0700121 }
Chris Dalton6a3dbee2017-10-16 10:44:41 -0600122
Chris Dalton23261772017-12-10 16:41:45 -0700123 // GrPrimitiveProcessor overrides.
124 const char* name() const override { return RenderPassName(fRenderPass); }
125 SkString dumpInfo() const override {
126 return SkStringPrintf("%s\n%s", this->name(), this->INHERITED::dumpInfo().c_str());
127 }
128 void getGLSLProcessorKey(const GrShaderCaps&, GrProcessorKeyBuilder*) const override;
129 GrGLSLPrimitiveProcessor* createGLSLInstance(const GrShaderCaps&) const override;
130
131#ifdef SK_DEBUG
132 // Increases the 1/2 pixel AA bloat by a factor of debugBloat and outputs color instead of
133 // coverage (coverage=+1 -> green, coverage=0 -> black, coverage=-1 -> red).
134 void enableDebugVisualizations(float debugBloat) { fDebugBloat = debugBloat; }
135 bool debugVisualizationsEnabled() const { return fDebugBloat > 0; }
136 float debugBloat() const { SkASSERT(this->debugVisualizationsEnabled()); return fDebugBloat; }
137#endif
138
Chris Dalton1fbdb612017-12-12 12:48:47 -0700139 // The Shader provides code to calculate each pixel's coverage in a RenderPass. It also
140 // provides details about shape-specific geometry.
Chris Dalton6a3dbee2017-10-16 10:44:41 -0600141 class Shader {
142 public:
Chris Dalton6a3dbee2017-10-16 10:44:41 -0600143 union GeometryVars {
144 struct {
145 const char* fAlternatePoints; // floatNx2 (if left null, will use input points).
Chris Dalton6a3dbee2017-10-16 10:44:41 -0600146 } fHullVars;
147
148 struct {
149 const char* fPoint; // float2
150 } fCornerVars;
151
152 GeometryVars() { memset(this, 0, sizeof(*this)); }
153 };
154
155 // Called before generating geometry. Subclasses must fill out the applicable fields in
156 // GeometryVars (if any), and may also use this opportunity to setup internal member
157 // variables that will be needed during onEmitVaryings (e.g. transformation matrices).
Chris Dalton1fbdb612017-12-12 12:48:47 -0700158 //
159 // repetitionID is a 0-based index and indicates which edge or corner is being generated.
160 // It will be null when generating a hull.
161 virtual void emitSetupCode(GrGLSLVertexGeoBuilder*, const char* pts,
162 const char* repetitionID, const char* wind,
163 GeometryVars*) const {}
Chris Dalton6a3dbee2017-10-16 10:44:41 -0600164
Chris Daltonf510e262018-01-30 16:42:37 -0700165 void emitVaryings(GrGLSLVaryingHandler* varyingHandler, GrGLSLVarying::Scope scope,
166 SkString* code, const char* position, const char* inputCoverage,
167 const char* wind) {
168 SkASSERT(GrGLSLVarying::Scope::kVertToGeo != scope);
169 this->onEmitVaryings(varyingHandler, scope, code, position, inputCoverage, wind);
170 }
Chris Dalton6a3dbee2017-10-16 10:44:41 -0600171
Robert Phillips7f861922018-01-30 13:13:42 +0000172 void emitFragmentCode(const GrCCCoverageProcessor& proc, GrGLSLPPFragmentBuilder*,
Chris Dalton6a3dbee2017-10-16 10:44:41 -0600173 const char* skOutputColor, const char* skOutputCoverage) const;
174
175 // Defines an equation ("dot(float3(pt, 1), distance_equation)") that is -1 on the outside
Chris Daltoncc0ab7e2017-10-24 14:16:52 -0600176 // border of a conservative raster edge and 0 on the inside. 'leftPt' and 'rightPt' must be
177 // ordered clockwise.
Chris Dalton1fbdb612017-12-12 12:48:47 -0700178 static void EmitEdgeDistanceEquation(GrGLSLVertexGeoBuilder*, const char* leftPt,
Chris Dalton6a3dbee2017-10-16 10:44:41 -0600179 const char* rightPt,
180 const char* outputDistanceEquation);
181
Chris Dalton6a3dbee2017-10-16 10:44:41 -0600182 virtual ~Shader() {}
183
184 protected:
Chris Dalton6a3dbee2017-10-16 10:44:41 -0600185 // Here the subclass adds its internal varyings to the handler and produces code to
Chris Daltonf510e262018-01-30 16:42:37 -0700186 // initialize those varyings from a given position, input coverage value, and wind.
Chris Dalton6a3dbee2017-10-16 10:44:41 -0600187 //
Chris Daltonf510e262018-01-30 16:42:37 -0700188 // NOTE: the coverage input is only relevant for edges (see comments in RenderPass).
Chris Dalton6a3dbee2017-10-16 10:44:41 -0600189 // Otherwise it is +1 all around.
Chris Daltonf510e262018-01-30 16:42:37 -0700190 virtual void onEmitVaryings(GrGLSLVaryingHandler*, GrGLSLVarying::Scope, SkString* code,
191 const char* position, const char* inputCoverage,
192 const char* wind) = 0;
Chris Dalton6a3dbee2017-10-16 10:44:41 -0600193
Chris Daltonf510e262018-01-30 16:42:37 -0700194 // Emits the fragment code that calculates a pixel's signed coverage value.
Robert Phillips7f861922018-01-30 13:13:42 +0000195 virtual void onEmitFragmentCode(GrGLSLPPFragmentBuilder*,
Chris Dalton6a3dbee2017-10-16 10:44:41 -0600196 const char* outputCoverage) const = 0;
197
Chris Dalton90e8fb12017-12-22 02:24:53 -0700198 // Returns the name of a Shader's internal varying at the point where where its value is
199 // assigned. This is intended to work whether called for a vertex or a geometry shader.
200 const char* OutName(const GrGLSLVarying& varying) const {
201 using Scope = GrGLSLVarying::Scope;
202 SkASSERT(Scope::kVertToGeo != varying.scope());
203 return Scope::kGeoToFrag == varying.scope() ? varying.gsOut() : varying.vsOut();
204 }
205
Chris Dalton1fbdb612017-12-12 12:48:47 -0700206 // Defines a global float2 array that contains MSAA sample locations as offsets from pixel
207 // center. Subclasses can use this for software multisampling.
208 //
209 // Returns the number of samples.
Robert Phillips7f861922018-01-30 13:13:42 +0000210 static int DefineSoftSampleLocations(GrGLSLPPFragmentBuilder* f, const char* samplesName);
Chris Dalton6a3dbee2017-10-16 10:44:41 -0600211 };
212
Chris Dalton6a3dbee2017-10-16 10:44:41 -0600213 class GSImpl;
Chris Dalton90e8fb12017-12-22 02:24:53 -0700214 class VSImpl;
Chris Dalton1a325d22017-07-14 15:17:41 -0600215
216private:
Chris Dalton1a325d22017-07-14 15:17:41 -0600217 // Slightly undershoot a bloat radius of 0.5 so vertices that fall on integer boundaries don't
218 // accidentally bleed into neighbor pixels.
219 static constexpr float kAABloatRadius = 0.491111f;
220
Chris Dalton1fbdb612017-12-12 12:48:47 -0700221 // Number of bezier points for curves, or 3 for triangles.
222 int numInputPoints() const { return RenderPassIsCubic(fRenderPass) ? 4 : 3; }
223
Chris Dalton90e8fb12017-12-22 02:24:53 -0700224 enum class Impl : bool {
225 kGeometryShader,
226 kVertexShader
227 };
228
Chris Dalton23261772017-12-10 16:41:45 -0700229 void initGS();
Chris Dalton27059d32018-01-23 14:06:50 -0700230 void initVS(GrResourceProvider*, const GrCaps&);
Chris Dalton90e8fb12017-12-22 02:24:53 -0700231
Chris Dalton23261772017-12-10 16:41:45 -0700232 void appendGSMesh(GrBuffer* instanceBuffer, int instanceCount, int baseInstance,
Chris Dalton90e8fb12017-12-22 02:24:53 -0700233 SkTArray<GrMesh>* out) const;
234 void appendVSMesh(GrBuffer* instanceBuffer, int instanceCount, int baseInstance,
235 SkTArray<GrMesh>* out) const;
236
Chris Dalton1fbdb612017-12-12 12:48:47 -0700237 GrGLSLPrimitiveProcessor* createGSImpl(std::unique_ptr<Shader>) const;
Chris Dalton90e8fb12017-12-22 02:24:53 -0700238 GrGLSLPrimitiveProcessor* createVSImpl(std::unique_ptr<Shader>) const;
Chris Dalton1a325d22017-07-14 15:17:41 -0600239
Chris Daltona3e92712017-12-04 11:45:51 -0700240 const RenderPass fRenderPass;
Chris Dalton90e8fb12017-12-22 02:24:53 -0700241 const Impl fImpl;
Chris Dalton383a2ef2018-01-08 17:21:41 -0500242 SkDEBUGCODE(float fDebugBloat = 0);
Chris Dalton1a325d22017-07-14 15:17:41 -0600243
Chris Dalton27059d32018-01-23 14:06:50 -0700244 // Used by VSImpl.
245 sk_sp<const GrBuffer> fVertexBuffer;
246 sk_sp<const GrBuffer> fIndexBuffer;
247 int fNumIndicesPerInstance;
248 GrPrimitiveType fPrimitiveType;
249
Chris Dalton6a3dbee2017-10-16 10:44:41 -0600250 typedef GrGeometryProcessor INHERITED;
Chris Dalton1a325d22017-07-14 15:17:41 -0600251};
252
Chris Dalton383a2ef2018-01-08 17:21:41 -0500253inline void GrCCCoverageProcessor::TriangleInstance::set(const SkPoint p[3], const Sk2f& trans) {
Chris Daltona3e92712017-12-04 11:45:51 -0700254 this->set(p[0], p[1], p[2], trans);
255}
256
Chris Dalton383a2ef2018-01-08 17:21:41 -0500257inline void GrCCCoverageProcessor::TriangleInstance::set(const SkPoint& p0, const SkPoint& p1,
258 const SkPoint& p2, const Sk2f& trans) {
Chris Daltona3e92712017-12-04 11:45:51 -0700259 Sk2f P0 = Sk2f::Load(&p0) + trans;
260 Sk2f P1 = Sk2f::Load(&p1) + trans;
261 Sk2f P2 = Sk2f::Load(&p2) + trans;
262 Sk2f::Store3(this, P0, P1, P2);
263}
264
Chris Dalton383a2ef2018-01-08 17:21:41 -0500265inline void GrCCCoverageProcessor::CubicInstance::set(const SkPoint p[4], float dx, float dy) {
Chris Daltona3e92712017-12-04 11:45:51 -0700266 Sk4f X,Y;
267 Sk4f::Load2(p, &X, &Y);
268 (X + dx).store(&fX);
269 (Y + dy).store(&fY);
270}
271
Chris Dalton383a2ef2018-01-08 17:21:41 -0500272inline bool GrCCCoverageProcessor::RenderPassIsCubic(RenderPass pass) {
Chris Dalton23261772017-12-10 16:41:45 -0700273 switch (pass) {
274 case RenderPass::kTriangleHulls:
275 case RenderPass::kTriangleEdges:
276 case RenderPass::kTriangleCorners:
277 case RenderPass::kQuadraticHulls:
278 case RenderPass::kQuadraticCorners:
279 return false;
280 case RenderPass::kCubicHulls:
281 case RenderPass::kCubicCorners:
282 return true;
283 }
Chris Dalton1fbdb612017-12-12 12:48:47 -0700284 SK_ABORT("Invalid RenderPass");
Chris Dalton23261772017-12-10 16:41:45 -0700285 return false;
286}
287
Chris Dalton383a2ef2018-01-08 17:21:41 -0500288inline const char* GrCCCoverageProcessor::RenderPassName(RenderPass pass) {
Chris Dalton23261772017-12-10 16:41:45 -0700289 switch (pass) {
290 case RenderPass::kTriangleHulls: return "kTriangleHulls";
291 case RenderPass::kTriangleEdges: return "kTriangleEdges";
292 case RenderPass::kTriangleCorners: return "kTriangleCorners";
293 case RenderPass::kQuadraticHulls: return "kQuadraticHulls";
294 case RenderPass::kQuadraticCorners: return "kQuadraticCorners";
295 case RenderPass::kCubicHulls: return "kCubicHulls";
296 case RenderPass::kCubicCorners: return "kCubicCorners";
297 }
Chris Dalton1fbdb612017-12-12 12:48:47 -0700298 SK_ABORT("Invalid RenderPass");
Chris Dalton23261772017-12-10 16:41:45 -0700299 return "";
300}
301
Chris Dalton1a325d22017-07-14 15:17:41 -0600302#endif