blob: d0b20cf686b229e3961424800ae7b0f4f47d4a8b [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
8#ifndef GrCCPRCoverageProcessor_DEFINED
9#define GrCCPRCoverageProcessor_DEFINED
10
11#include "GrGeometryProcessor.h"
12#include "glsl/GrGLSLGeometryProcessor.h"
13#include "glsl/GrGLSLVarying.h"
14
15class GrGLSLFragmentBuilder;
16
17/**
18 * This is the geometry processor for the simple convex primitive shapes (triangles and closed curve
19 * segments) from which ccpr paths are composed. The output is a single-channel alpha value,
20 * positive for clockwise primitives and negative for counter-clockwise, that indicates coverage.
21 *
22 * The caller is responsible to render all modes for all applicable primitives into a cleared,
23 * floating point, alpha-only render target using SkBlendMode::kPlus. Once all of a path's
24 * primitives have been drawn, the render target contains a composite coverage count that can then
25 * be used to draw the path (see GrCCPRPathProcessor).
26 *
27 * Caller provides the primitives' (x,y) points in an fp32x2 (RG) texel buffer, and an instance
Chris Daltonc1e59632017-09-05 00:30:07 -060028 * buffer with a single int32x4 attrib (for triangles) or int32x2 (for curves) defined below. There
29 * are no vertex attribs.
Chris Dalton1a325d22017-07-14 15:17:41 -060030 *
31 * Draw calls are instanced, with one vertex per bezier point (3 for triangles). They use the
32 * corresponding GrPrimitiveType as defined below.
33 */
34class GrCCPRCoverageProcessor : public GrGeometryProcessor {
35public:
36 // Use top-left to avoid a uniform access in the fragment shader.
37 static constexpr GrSurfaceOrigin kAtlasOrigin = kTopLeft_GrSurfaceOrigin;
38
39 static constexpr GrPrimitiveType kTrianglesGrPrimitiveType = GrPrimitiveType::kTriangles;
40 static constexpr GrPrimitiveType kQuadraticsGrPrimitiveType = GrPrimitiveType::kTriangles;
41 static constexpr GrPrimitiveType kCubicsGrPrimitiveType = GrPrimitiveType::kLinesAdjacency;
42
Chris Daltonc1e59632017-09-05 00:30:07 -060043 struct TriangleInstance {
44 int32_t fPt0Idx;
45 int32_t fPt1Idx;
46 int32_t fPt2Idx;
Chris Dalton1a325d22017-07-14 15:17:41 -060047 int32_t fPackedAtlasOffset; // (offsetY << 16) | (offsetX & 0xffff)
48 };
49
Chris Daltonc1e59632017-09-05 00:30:07 -060050 GR_STATIC_ASSERT(4 * 4 == sizeof(TriangleInstance));
51
52 struct CurveInstance {
53 int32_t fPtsIdx;
54 int32_t fPackedAtlasOffset; // (offsetY << 16) | (offsetX & 0xffff)
55 };
56
57 GR_STATIC_ASSERT(2 * 4 == sizeof(CurveInstance));
Chris Dalton1a325d22017-07-14 15:17:41 -060058
59 enum class Mode {
60 // Triangles.
61 kTriangleHulls,
62 kTriangleEdges,
63 kCombinedTriangleHullsAndEdges,
64 kTriangleCorners,
65
66 // Quadratics.
67 kQuadraticHulls,
Chris Daltonb072bb62017-08-07 09:00:46 -060068 kQuadraticCorners,
Chris Dalton1a325d22017-07-14 15:17:41 -060069
70 // Cubics.
71 kSerpentineInsets,
72 kSerpentineBorders,
73 kLoopInsets,
74 kLoopBorders
75 };
Chris Daltonc1e59632017-09-05 00:30:07 -060076 static constexpr GrVertexAttribType InstanceArrayFormat(Mode mode) {
77 return mode < Mode::kQuadraticHulls ? kVec4i_GrVertexAttribType : kVec2i_GrVertexAttribType;
78 }
Chris Dalton1a325d22017-07-14 15:17:41 -060079 static const char* GetProcessorName(Mode);
80
81 GrCCPRCoverageProcessor(Mode, GrBuffer* pointsBuffer);
82
83 const char* instanceAttrib() const { return fInstanceAttrib.fName; }
Chris Daltonc1e59632017-09-05 00:30:07 -060084 int atlasOffsetIdx() const {
85 return kVec4i_GrVertexAttribType == InstanceArrayFormat(fMode) ? 3 : 1;
86 }
Chris Dalton1a325d22017-07-14 15:17:41 -060087 const char* name() const override { return GetProcessorName(fMode); }
88 SkString dumpInfo() const override {
89 return SkStringPrintf("%s\n%s", this->name(), this->INHERITED::dumpInfo().c_str());
90 }
91
92 void getGLSLProcessorKey(const GrShaderCaps&, GrProcessorKeyBuilder*) const override;
93 GrGLSLPrimitiveProcessor* createGLSLInstance(const GrShaderCaps&) const override;
94
95#ifdef SK_DEBUG
96 static constexpr float kDebugBloat = 50;
97
98 // Increases the 1/2 pixel AA bloat by a factor of kDebugBloat and outputs color instead of
99 // coverage (coverage=+1 -> green, coverage=0 -> black, coverage=-1 -> red).
100 void enableDebugVisualizations() { fDebugVisualizations = true; }
101 bool debugVisualizations() const { return fDebugVisualizations; }
102
Robert Phillips2890fbf2017-07-26 15:48:41 -0400103 static void Validate(GrRenderTargetProxy* atlasProxy);
Chris Dalton1a325d22017-07-14 15:17:41 -0600104#endif
105
106 class PrimitiveProcessor;
107
108private:
109 const Mode fMode;
110 const Attribute& fInstanceAttrib;
111 BufferAccess fPointsBufferAccess;
112 SkDEBUGCODE(bool fDebugVisualizations = false;)
113
114 typedef GrGeometryProcessor INHERITED;
115};
116
117/**
118 * This class represents the actual SKSL implementation for the various primitives and modes of
119 * GrCCPRCoverageProcessor.
120 */
121class GrCCPRCoverageProcessor::PrimitiveProcessor : public GrGLSLGeometryProcessor {
122protected:
123 // Slightly undershoot a bloat radius of 0.5 so vertices that fall on integer boundaries don't
124 // accidentally bleed into neighbor pixels.
125 static constexpr float kAABloatRadius = 0.491111f;
126
127 // Specifies how the fragment shader should calculate sk_FragColor.a.
128 enum class CoverageType {
129 kOne, // Output +1 all around, modulated by wind.
130 kInterpolated, // Interpolate the coverage values that the geometry shader associates with
131 // each point, modulated by wind.
132 kShader // Call emitShaderCoverage and let the subclass decide, then a modulate by wind.
133 };
134
135 PrimitiveProcessor(CoverageType coverageType)
136 : fCoverageType(coverageType)
Brian Salomon1d816b92017-08-17 11:07:59 -0400137 , fGeomWind("wind", kFloat_GrSLType, GrShaderVar::kNonArray, kLow_GrSLPrecision)
138 , fFragWind(kFloat_GrSLType)
139 , fFragCoverageTimesWind(kFloat_GrSLType) {}
Chris Dalton1a325d22017-07-14 15:17:41 -0600140
141 // Called before generating shader code. Subclass should add its custom varyings to the handler
142 // and update its corresponding internal member variables.
143 virtual void resetVaryings(GrGLSLVaryingHandler*) {}
144
145 // Here the subclass fetches its vertex from the texel buffer, translates by atlasOffset, and
146 // sets "fPositionVar" in the GrGPArgs.
147 virtual void onEmitVertexShader(const GrCCPRCoverageProcessor&, GrGLSLVertexBuilder*,
148 const TexelBufferHandle& pointsBuffer, const char* atlasOffset,
149 const char* rtAdjust, GrGPArgs*) const = 0;
150
151 // Here the subclass determines the winding direction of its primitive. It must write a value of
152 // either -1, 0, or +1 to "outputWind" (e.g. "sign(area)"). Fractional values are not valid.
153 virtual void emitWind(GrGLSLGeometryBuilder*, const char* rtAdjust,
154 const char* outputWind) const = 0;
155
156 // This is where the subclass generates the actual geometry to be rasterized by hardware:
157 //
158 // emitVertexFn(point1, coverage);
159 // emitVertexFn(point2, coverage);
160 // ...
161 // EndPrimitive();
162 //
163 // Generally a subclass will want to use emitHullGeometry and/or emitEdgeGeometry rather than
164 // calling emitVertexFn directly.
165 //
166 // Subclass must also call GrGLSLGeometryBuilder::configure.
167 virtual void onEmitGeometryShader(GrGLSLGeometryBuilder*, const char* emitVertexFn,
168 const char* wind, const char* rtAdjust) const = 0;
169
170 // This is a hook to inject code in the geometry shader's "emitVertex" function. Subclass
171 // should use this to write values to its custom varyings.
172 // NOTE: even flat varyings should be rewritten at each vertex.
173 virtual void emitPerVertexGeometryCode(SkString* fnBody, const char* position,
174 const char* coverage, const char* wind) const {}
175
176 // Called when the subclass has selected CoverageType::kShader. Primitives should produce
177 // coverage values between +0..1. Base class modulates the sign for wind.
178 // TODO: subclasses might have good spots to stuff the winding information without burning a
179 // whole new varying slot. Consider requiring them to generate the correct coverage sign.
180 virtual void emitShaderCoverage(GrGLSLFragmentBuilder*, const char* outputCoverage) const {
Ben Wagnerb4aab9a2017-08-16 10:53:04 -0400181 SK_ABORT("Shader coverage not implemented when using CoverageType::kShader.");
Chris Dalton1a325d22017-07-14 15:17:41 -0600182 }
183
184 // Emits one wedge of the conservative raster hull of a convex polygon. The complete hull has
185 // one wedge for each side of the polygon (i.e. call this N times, generally from different
186 // geometry shader invocations). Coverage is +1 all around.
187 //
188 // Logically, the conservative raster hull is equivalent to the convex hull of pixel-size boxes
189 // centered on the vertices.
190 //
191 // If an optional inset polygon is provided, then this emits a border from the inset to the
192 // hull, rather than the entire hull.
193 //
194 // Geometry shader must be configured to output triangle strips.
195 //
196 // Returns the maximum number of vertices that will be emitted.
197 int emitHullGeometry(GrGLSLGeometryBuilder*, const char* emitVertexFn, const char* polygonPts,
198 int numSides, const char* wedgeIdx, const char* insetPts = nullptr) const;
199
200 // Emits the conservative raster of an edge (i.e. convex hull of two pixel-size boxes centered
201 // on the endpoints). Coverage is -1 on the outside border of the edge geometry and 0 on the
202 // inside. This effectively converts a jagged conservative raster edge into a smooth antialiased
203 // edge when using CoverageType::kInterpolated.
204 //
205 // If the subclass has already called emitEdgeDistanceEquation, then provide the distance
206 // equation. Otherwise this function will call emitEdgeDistanceEquation implicitly.
207 //
208 // Geometry shader must be configured to output triangle strips.
209 //
210 // Returns the maximum number of vertices that will be emitted.
211 int emitEdgeGeometry(GrGLSLGeometryBuilder*, const char* emitVertexFn, const char* leftPt,
212 const char* rightPt, const char* distanceEquation = nullptr) const;
213
Ethan Nicholas5af9ea32017-07-28 15:19:46 -0400214 // Defines an equation ("dot(float3(pt, 1), distance_equation)") that is -1 on the outside
215 // border of a conservative raster edge and 0 on the inside (see emitEdgeGeometry).
Chris Dalton1a325d22017-07-14 15:17:41 -0600216 void emitEdgeDistanceEquation(GrGLSLGeometryBuilder*, const char* leftPt, const char* rightPt,
217 const char* outputDistanceEquation) const;
218
Chris Daltonb072bb62017-08-07 09:00:46 -0600219 // Emits the conservative raster of a single point (i.e. pixel-size box centered on the point).
220 // Coverage is +1 all around.
221 //
222 // Geometry shader must be configured to output triangle strips.
223 //
224 // Returns the number of vertices that were emitted.
225 int emitCornerGeometry(GrGLSLGeometryBuilder*, const char* emitVertexFn, const char* pt) const;
226
Ethan Nicholas5af9ea32017-07-28 15:19:46 -0400227 // Defines a global float2 array that contains MSAA sample locations as offsets from pixel
228 // center. Subclasses can use this for software multisampling.
Chris Dalton1a325d22017-07-14 15:17:41 -0600229 //
230 // Returns the number of samples.
231 int defineSoftSampleLocations(GrGLSLFragmentBuilder*, const char* samplesName) const;
232
233private:
234 void setData(const GrGLSLProgramDataManager& pdman, const GrPrimitiveProcessor&,
235 FPCoordTransformIter&& transformIter) final {
236 this->setTransformDataHelper(SkMatrix::I(), pdman, &transformIter);
237 }
238
239 void onEmitCode(EmitArgs& args, GrGPArgs* gpArgs) final;
240
241 void emitVertexShader(const GrCCPRCoverageProcessor&, GrGLSLVertexBuilder*,
242 const TexelBufferHandle& pointsBuffer, const char* rtAdjust,
243 GrGPArgs* gpArgs) const;
244 void emitGeometryShader(const GrCCPRCoverageProcessor&, GrGLSLGeometryBuilder*,
245 const char* rtAdjust) const;
246 void emitCoverage(const GrCCPRCoverageProcessor&, GrGLSLFragmentBuilder*,
247 const char* outputColor, const char* outputCoverage) const;
248
249 const CoverageType fCoverageType;
250 GrShaderVar fGeomWind;
251 GrGLSLGeoToFrag fFragWind;
252 GrGLSLGeoToFrag fFragCoverageTimesWind;
253
254 typedef GrGLSLGeometryProcessor INHERITED;
255};
256
257#endif