• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 "GrCaps.h"
12 #include "GrGeometryProcessor.h"
13 #include "GrShaderCaps.h"
14 #include "SkNx.h"
15 #include "glsl/GrGLSLGeometryProcessor.h"
16 #include "glsl/GrGLSLVarying.h"
17 
18 class GrGLSLPPFragmentBuilder;
19 class GrGLSLVertexGeoBuilder;
20 class GrMesh;
21 
22 /**
23  * 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.
26  *
27  * 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
30  * coverage count that can then be used to draw the path (see GrCCPathProcessor).
31  *
32  * To draw a renderer pass, see appendMesh below.
33  */
34 class GrCCCoverageProcessor : public GrGeometryProcessor {
35 public:
36     // Defines a single triangle or closed quadratic bezier, with transposed x,y point values.
37     struct TriangleInstance {
38         float fX[3];
39         float fY[3];
40 
41         void set(const SkPoint[3], const Sk2f& trans);
42         void set(const SkPoint&, const SkPoint&, const SkPoint&, const Sk2f& trans);
43     };
44 
45     // Defines a single closed cubic bezier, with transposed x,y point values.
46     struct CubicInstance {
47         float fX[4];
48         float fY[4];
49 
50         void set(const SkPoint[4], float dx, float dy);
51     };
52 
53     // All primitive shapes (triangles and closed, convex bezier curves) require more than one
54     // 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.
56     //
57     // During a render pass, the "Impl" (GSImpl or VSimpl) generates conservative geometry for
58     // rasterization, and the Shader decides the coverage value at each pixel.
59     enum class RenderPass {
60         // 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
62         // input polygon. The input coverage values sent to the Shader at each vertex are either
63         // 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.
66         kTriangleHulls,
67         kQuadraticHulls,
68         kCubicHulls,
69 
70         // For Edges, the Impl generates conservative rasters around every input edge (i.e. convex
71         // hulls of two pixel-size boxes centered on both of the edge's endpoints). The input
72         // 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.
76         //
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.
79         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
83         // specifies which corners. Input coverage values sent to the Shader will be null.
84         kTriangleCorners,
85         kQuadraticCorners,
86         kCubicCorners
87     };
88     static bool RenderPassIsCubic(RenderPass);
89     static const char* RenderPassName(RenderPass);
90 
DoesRenderPass(RenderPass renderPass,const GrCaps & caps)91     constexpr static bool DoesRenderPass(RenderPass renderPass, const GrCaps& caps) {
92         return RenderPass::kTriangleEdges != renderPass ||
93                caps.shaderCaps()->geometryShaderSupport();
94     }
95 
GrCCCoverageProcessor(GrResourceProvider * rp,RenderPass pass,const GrCaps & caps)96     GrCCCoverageProcessor(GrResourceProvider* rp, RenderPass pass, const GrCaps& caps)
97             : INHERITED(kGrCCCoverageProcessor_ClassID)
98             , fRenderPass(pass)
99             , fImpl(caps.shaderCaps()->geometryShaderSupport() ? Impl::kGeometryShader
100                                                                : Impl::kVertexShader) {
101         SkASSERT(DoesRenderPass(pass, caps));
102         if (Impl::kGeometryShader == fImpl) {
103             this->initGS();
104         } else {
105             this->initVS(rp, caps);
106         }
107     }
108 
109     // 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.
appendMesh(GrBuffer * instanceBuffer,int instanceCount,int baseInstance,SkTArray<GrMesh> * out)114     void appendMesh(GrBuffer* instanceBuffer, int instanceCount, int baseInstance,
115                     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         }
121     }
122 
123     // GrPrimitiveProcessor overrides.
name()124     const char* name() const override { return RenderPassName(fRenderPass); }
dumpInfo()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).
enableDebugVisualizations(float debugBloat)134     void enableDebugVisualizations(float debugBloat) { fDebugBloat = debugBloat; }
debugVisualizationsEnabled()135     bool debugVisualizationsEnabled() const { return fDebugBloat > 0; }
debugBloat()136     float debugBloat() const { SkASSERT(this->debugVisualizationsEnabled()); return fDebugBloat; }
137 #endif
138 
139     // The Shader provides code to calculate each pixel's coverage in a RenderPass. It also
140     // provides details about shape-specific geometry.
141     class Shader {
142     public:
143         union GeometryVars {
144             struct {
145                 const char* fAlternatePoints; // floatNx2 (if left null, will use input points).
146             } fHullVars;
147 
148             struct {
149                 const char* fPoint; // float2
150             } fCornerVars;
151 
GeometryVars()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).
158         //
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.
emitSetupCode(GrGLSLVertexGeoBuilder *,const char * pts,const char * repetitionID,const char * wind,GeometryVars *)161         virtual void emitSetupCode(GrGLSLVertexGeoBuilder*, const char* pts,
162                                    const char* repetitionID, const char* wind,
163                                    GeometryVars*) const {}
164 
emitVaryings(GrGLSLVaryingHandler * varyingHandler,GrGLSLVarying::Scope scope,SkString * code,const char * position,const char * inputCoverage,const char * wind)165         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         }
171 
172         void emitFragmentCode(const GrCCCoverageProcessor& proc, GrGLSLPPFragmentBuilder*,
173                               const char* skOutputColor, const char* skOutputCoverage) const;
174 
175         // Defines an equation ("dot(float3(pt, 1), distance_equation)") that is -1 on the outside
176         // border of a conservative raster edge and 0 on the inside. 'leftPt' and 'rightPt' must be
177         // ordered clockwise.
178         static void EmitEdgeDistanceEquation(GrGLSLVertexGeoBuilder*, const char* leftPt,
179                                              const char* rightPt,
180                                              const char* outputDistanceEquation);
181 
~Shader()182         virtual ~Shader() {}
183 
184     protected:
185         // Here the subclass adds its internal varyings to the handler and produces code to
186         // initialize those varyings from a given position, input coverage value, and wind.
187         //
188         // NOTE: the coverage input is only relevant for edges (see comments in RenderPass).
189         // Otherwise it is +1 all around.
190         virtual void onEmitVaryings(GrGLSLVaryingHandler*, GrGLSLVarying::Scope, SkString* code,
191                                     const char* position, const char* inputCoverage,
192                                     const char* wind) = 0;
193 
194         // Emits the fragment code that calculates a pixel's signed coverage value.
195         virtual void onEmitFragmentCode(GrGLSLPPFragmentBuilder*,
196                                         const char* outputCoverage) const = 0;
197 
198         // 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.
OutName(const GrGLSLVarying & varying)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 
206         // 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.
210         static int DefineSoftSampleLocations(GrGLSLPPFragmentBuilder* f, const char* samplesName);
211     };
212 
213     class GSImpl;
214     class VSImpl;
215 
216 private:
217     // 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 
221     // Number of bezier points for curves, or 3 for triangles.
numInputPoints()222     int numInputPoints() const { return RenderPassIsCubic(fRenderPass) ? 4 : 3; }
223 
224     enum class Impl : bool {
225         kGeometryShader,
226         kVertexShader
227     };
228 
229     void initGS();
230     void initVS(GrResourceProvider*, const GrCaps&);
231 
232     void appendGSMesh(GrBuffer* instanceBuffer, int instanceCount, int baseInstance,
233                       SkTArray<GrMesh>* out) const;
234     void appendVSMesh(GrBuffer* instanceBuffer, int instanceCount, int baseInstance,
235                       SkTArray<GrMesh>* out) const;
236 
237     GrGLSLPrimitiveProcessor* createGSImpl(std::unique_ptr<Shader>) const;
238     GrGLSLPrimitiveProcessor* createVSImpl(std::unique_ptr<Shader>) const;
239 
240     const RenderPass fRenderPass;
241     const Impl fImpl;
242     SkDEBUGCODE(float fDebugBloat = 0);
243 
244     // Used by VSImpl.
245     sk_sp<const GrBuffer> fVertexBuffer;
246     sk_sp<const GrBuffer> fIndexBuffer;
247     int fNumIndicesPerInstance;
248     GrPrimitiveType fPrimitiveType;
249 
250     typedef GrGeometryProcessor INHERITED;
251 };
252 
set(const SkPoint p[3],const Sk2f & trans)253 inline void GrCCCoverageProcessor::TriangleInstance::set(const SkPoint p[3], const Sk2f& trans) {
254     this->set(p[0], p[1], p[2], trans);
255 }
256 
set(const SkPoint & p0,const SkPoint & p1,const SkPoint & p2,const Sk2f & trans)257 inline void GrCCCoverageProcessor::TriangleInstance::set(const SkPoint& p0, const SkPoint& p1,
258                                                          const SkPoint& p2, const Sk2f& trans) {
259     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 
set(const SkPoint p[4],float dx,float dy)265 inline void GrCCCoverageProcessor::CubicInstance::set(const SkPoint p[4], float dx, float dy) {
266     Sk4f X,Y;
267     Sk4f::Load2(p, &X, &Y);
268     (X + dx).store(&fX);
269     (Y + dy).store(&fY);
270 }
271 
RenderPassIsCubic(RenderPass pass)272 inline bool GrCCCoverageProcessor::RenderPassIsCubic(RenderPass pass) {
273     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     }
284     SK_ABORT("Invalid RenderPass");
285     return false;
286 }
287 
RenderPassName(RenderPass pass)288 inline const char* GrCCCoverageProcessor::RenderPassName(RenderPass pass) {
289     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     }
298     SK_ABORT("Invalid RenderPass");
299     return "";
300 }
301 
302 #endif
303