blob: 8120136fad69c315c83b13086163fed0251a187f [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.
Chris Dalton7f578bf2017-09-05 16:46:48 -060071 kSerpentineHulls,
72 kLoopHulls,
73 kSerpentineCorners,
74 kLoopCorners
Chris Dalton1a325d22017-07-14 15:17:41 -060075 };
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
Chris Dalton7f578bf2017-09-05 16:46:48 -060095#ifdef SK_DEBUG
Chris Daltona640c492017-09-11 22:04:03 -070096 // Increases the 1/2 pixel AA bloat by a factor of debugBloat and outputs color instead of
Chris Dalton1a325d22017-07-14 15:17:41 -060097 // coverage (coverage=+1 -> green, coverage=0 -> black, coverage=-1 -> red).
Chris Daltona640c492017-09-11 22:04:03 -070098 void enableDebugVisualizations(float debugBloat) { fDebugBloat = debugBloat; }
99 bool debugVisualizationsEnabled() const { return fDebugBloat > 0; }
100 float debugBloat() const { SkASSERT(this->debugVisualizationsEnabled()); return fDebugBloat; }
Chris Dalton1a325d22017-07-14 15:17:41 -0600101
Robert Phillips2890fbf2017-07-26 15:48:41 -0400102 static void Validate(GrRenderTargetProxy* atlasProxy);
Chris Dalton1a325d22017-07-14 15:17:41 -0600103#endif
104
105 class PrimitiveProcessor;
106
107private:
Chris Daltona640c492017-09-11 22:04:03 -0700108 const Mode fMode;
109 const Attribute& fInstanceAttrib;
110 BufferAccess fPointsBufferAccess;
111 SkDEBUGCODE(float fDebugBloat = false;)
Chris Dalton1a325d22017-07-14 15:17:41 -0600112
113 typedef GrGeometryProcessor INHERITED;
114};
115
116/**
117 * This class represents the actual SKSL implementation for the various primitives and modes of
118 * GrCCPRCoverageProcessor.
119 */
120class GrCCPRCoverageProcessor::PrimitiveProcessor : public GrGLSLGeometryProcessor {
121protected:
122 // Slightly undershoot a bloat radius of 0.5 so vertices that fall on integer boundaries don't
123 // accidentally bleed into neighbor pixels.
124 static constexpr float kAABloatRadius = 0.491111f;
125
126 // Specifies how the fragment shader should calculate sk_FragColor.a.
127 enum class CoverageType {
128 kOne, // Output +1 all around, modulated by wind.
129 kInterpolated, // Interpolate the coverage values that the geometry shader associates with
130 // each point, modulated by wind.
131 kShader // Call emitShaderCoverage and let the subclass decide, then a modulate by wind.
132 };
133
134 PrimitiveProcessor(CoverageType coverageType)
135 : fCoverageType(coverageType)
Ethan Nicholasf7b88202017-09-18 14:10:39 -0400136 , fGeomWind("wind", kHalf_GrSLType, GrShaderVar::kNonArray, kLow_GrSLPrecision)
137 , fFragWind(kHalf_GrSLType)
138 , fFragCoverageTimesWind(kHalf_GrSLType) {}
Chris Dalton1a325d22017-07-14 15:17:41 -0600139
140 // Called before generating shader code. Subclass should add its custom varyings to the handler
141 // and update its corresponding internal member variables.
142 virtual void resetVaryings(GrGLSLVaryingHandler*) {}
143
144 // Here the subclass fetches its vertex from the texel buffer, translates by atlasOffset, and
145 // sets "fPositionVar" in the GrGPArgs.
146 virtual void onEmitVertexShader(const GrCCPRCoverageProcessor&, GrGLSLVertexBuilder*,
147 const TexelBufferHandle& pointsBuffer, const char* atlasOffset,
148 const char* rtAdjust, GrGPArgs*) const = 0;
149
150 // Here the subclass determines the winding direction of its primitive. It must write a value of
151 // either -1, 0, or +1 to "outputWind" (e.g. "sign(area)"). Fractional values are not valid.
152 virtual void emitWind(GrGLSLGeometryBuilder*, const char* rtAdjust,
153 const char* outputWind) const = 0;
154
155 // This is where the subclass generates the actual geometry to be rasterized by hardware:
156 //
157 // emitVertexFn(point1, coverage);
158 // emitVertexFn(point2, coverage);
159 // ...
160 // EndPrimitive();
161 //
162 // Generally a subclass will want to use emitHullGeometry and/or emitEdgeGeometry rather than
163 // calling emitVertexFn directly.
164 //
165 // Subclass must also call GrGLSLGeometryBuilder::configure.
166 virtual void onEmitGeometryShader(GrGLSLGeometryBuilder*, const char* emitVertexFn,
167 const char* wind, const char* rtAdjust) const = 0;
168
169 // This is a hook to inject code in the geometry shader's "emitVertex" function. Subclass
170 // should use this to write values to its custom varyings.
171 // NOTE: even flat varyings should be rewritten at each vertex.
172 virtual void emitPerVertexGeometryCode(SkString* fnBody, const char* position,
173 const char* coverage, const char* wind) const {}
174
175 // Called when the subclass has selected CoverageType::kShader. Primitives should produce
176 // coverage values between +0..1. Base class modulates the sign for wind.
177 // TODO: subclasses might have good spots to stuff the winding information without burning a
178 // whole new varying slot. Consider requiring them to generate the correct coverage sign.
179 virtual void emitShaderCoverage(GrGLSLFragmentBuilder*, const char* outputCoverage) const {
Ben Wagnerb4aab9a2017-08-16 10:53:04 -0400180 SK_ABORT("Shader coverage not implemented when using CoverageType::kShader.");
Chris Dalton1a325d22017-07-14 15:17:41 -0600181 }
182
183 // Emits one wedge of the conservative raster hull of a convex polygon. The complete hull has
184 // one wedge for each side of the polygon (i.e. call this N times, generally from different
185 // geometry shader invocations). Coverage is +1 all around.
186 //
187 // Logically, the conservative raster hull is equivalent to the convex hull of pixel-size boxes
188 // centered on the vertices.
189 //
Chris Dalton1a325d22017-07-14 15:17:41 -0600190 // Geometry shader must be configured to output triangle strips.
191 //
192 // Returns the maximum number of vertices that will be emitted.
193 int emitHullGeometry(GrGLSLGeometryBuilder*, const char* emitVertexFn, const char* polygonPts,
Chris Dalton7f578bf2017-09-05 16:46:48 -0600194 int numSides, const char* wedgeIdx, const char* midpoint = nullptr) const;
Chris Dalton1a325d22017-07-14 15:17:41 -0600195
196 // Emits the conservative raster of an edge (i.e. convex hull of two pixel-size boxes centered
197 // on the endpoints). Coverage is -1 on the outside border of the edge geometry and 0 on the
198 // inside. This effectively converts a jagged conservative raster edge into a smooth antialiased
199 // edge when using CoverageType::kInterpolated.
200 //
201 // If the subclass has already called emitEdgeDistanceEquation, then provide the distance
202 // equation. Otherwise this function will call emitEdgeDistanceEquation implicitly.
203 //
204 // Geometry shader must be configured to output triangle strips.
205 //
206 // Returns the maximum number of vertices that will be emitted.
207 int emitEdgeGeometry(GrGLSLGeometryBuilder*, const char* emitVertexFn, const char* leftPt,
208 const char* rightPt, const char* distanceEquation = nullptr) const;
209
Ethan Nicholas5af9ea32017-07-28 15:19:46 -0400210 // Defines an equation ("dot(float3(pt, 1), distance_equation)") that is -1 on the outside
211 // border of a conservative raster edge and 0 on the inside (see emitEdgeGeometry).
Chris Dalton1a325d22017-07-14 15:17:41 -0600212 void emitEdgeDistanceEquation(GrGLSLGeometryBuilder*, const char* leftPt, const char* rightPt,
213 const char* outputDistanceEquation) const;
214
Chris Daltonb072bb62017-08-07 09:00:46 -0600215 // Emits the conservative raster of a single point (i.e. pixel-size box centered on the point).
216 // Coverage is +1 all around.
217 //
218 // Geometry shader must be configured to output triangle strips.
219 //
220 // Returns the number of vertices that were emitted.
221 int emitCornerGeometry(GrGLSLGeometryBuilder*, const char* emitVertexFn, const char* pt) const;
222
Ethan Nicholas5af9ea32017-07-28 15:19:46 -0400223 // Defines a global float2 array that contains MSAA sample locations as offsets from pixel
224 // center. Subclasses can use this for software multisampling.
Chris Dalton1a325d22017-07-14 15:17:41 -0600225 //
226 // Returns the number of samples.
227 int defineSoftSampleLocations(GrGLSLFragmentBuilder*, const char* samplesName) const;
228
229private:
230 void setData(const GrGLSLProgramDataManager& pdman, const GrPrimitiveProcessor&,
231 FPCoordTransformIter&& transformIter) final {
232 this->setTransformDataHelper(SkMatrix::I(), pdman, &transformIter);
233 }
234
235 void onEmitCode(EmitArgs& args, GrGPArgs* gpArgs) final;
236
237 void emitVertexShader(const GrCCPRCoverageProcessor&, GrGLSLVertexBuilder*,
238 const TexelBufferHandle& pointsBuffer, const char* rtAdjust,
239 GrGPArgs* gpArgs) const;
240 void emitGeometryShader(const GrCCPRCoverageProcessor&, GrGLSLGeometryBuilder*,
241 const char* rtAdjust) const;
242 void emitCoverage(const GrCCPRCoverageProcessor&, GrGLSLFragmentBuilder*,
243 const char* outputColor, const char* outputCoverage) const;
244
245 const CoverageType fCoverageType;
246 GrShaderVar fGeomWind;
247 GrGLSLGeoToFrag fFragWind;
248 GrGLSLGeoToFrag fFragCoverageTimesWind;
249
250 typedef GrGLSLGeometryProcessor INHERITED;
251};
252
253#endif