diff options
author | 2018-04-23 21:14:42 -0600 | |
---|---|---|
committer | 2018-04-24 15:23:06 +0000 | |
commit | 6f5e77a08faf9d967eba8194811cbf2391092c23 (patch) | |
tree | 577ea1030e16bd68c47eaa5cd3c494785c7204e6 /src/gpu/ccpr | |
parent | b97f28b90399c2571712401a30b0299d37b4b52e (diff) |
ccpr: Cull extremely thin triangles
When triangles get too thin it's possible for FP round-off error to
actually give us the wrong winding direction, causing rendering
artifacts. This change also allows us to unblacklist ANGLE.
Bug: skia:7805
Bug: skia:7820
Change-Id: Ibaa0f033eba625d720e3a594c4515d8264cc413d
Reviewed-on: https://skia-review.googlesource.com/123262
Reviewed-by: Brian Osman <brianosman@google.com>
Commit-Queue: Chris Dalton <csmartdalton@google.com>
Diffstat (limited to 'src/gpu/ccpr')
-rw-r--r-- | src/gpu/ccpr/GrCCCoverageProcessor.cpp | 30 | ||||
-rw-r--r-- | src/gpu/ccpr/GrCCCoverageProcessor.h | 10 | ||||
-rw-r--r-- | src/gpu/ccpr/GrCCCoverageProcessor_GSImpl.cpp | 24 | ||||
-rw-r--r-- | src/gpu/ccpr/GrCCCoverageProcessor_VSImpl.cpp | 14 | ||||
-rw-r--r-- | src/gpu/ccpr/GrCCGeometry.cpp | 56 | ||||
-rw-r--r-- | src/gpu/ccpr/GrCCGeometry.h | 4 | ||||
-rw-r--r-- | src/gpu/ccpr/GrCCPathParser.cpp | 5 |
7 files changed, 86 insertions, 57 deletions
diff --git a/src/gpu/ccpr/GrCCCoverageProcessor.cpp b/src/gpu/ccpr/GrCCCoverageProcessor.cpp index d38db27a19..75d0667b09 100644 --- a/src/gpu/ccpr/GrCCCoverageProcessor.cpp +++ b/src/gpu/ccpr/GrCCCoverageProcessor.cpp @@ -44,6 +44,36 @@ class GrCCCoverageProcessor::TriangleShader : public GrCCCoverageProcessor::Shad GrGLSLVarying fCoverages; }; +void GrCCCoverageProcessor::Shader::CalcWind(const GrCCCoverageProcessor& proc, + GrGLSLVertexGeoBuilder* s, const char* pts, + const char* outputWind) { + if (3 == proc.numInputPoints()) { + s->codeAppendf("float2 a = %s[0] - %s[1], " + "b = %s[0] - %s[2];", pts, pts, pts, pts); + } else { + // All inputs are convex, so it's sufficient to just average the middle two input points. + SkASSERT(4 == proc.numInputPoints()); + s->codeAppendf("float2 p12 = (%s[1] + %s[2]) * .5;", pts, pts); + s->codeAppendf("float2 a = %s[0] - p12, " + "b = %s[0] - %s[3];", pts, pts, pts); + } + + s->codeAppend ("float area_x2 = determinant(float2x2(a, b));"); + if (proc.isTriangles()) { + // We cull extremely thin triangles by zeroing wind. When a triangle gets too thin it's + // possible for FP round-off error to actually give us the wrong winding direction, causing + // rendering artifacts. The criteria we choose is "height <~ 1/1024". So we drop a triangle + // if the max effect it can have on any single pixel is <~ 1/1024, or 1/4 of a bit in 8888. + s->codeAppend ("float2 bbox_size = max(abs(a), abs(b));"); + s->codeAppend ("float basewidth = max(bbox_size.x + bbox_size.y, 1);"); + s->codeAppendf("%s = (abs(area_x2 * 1024) > basewidth) ? sign(area_x2) : 0;", outputWind); + } else { + // We already converted nearly-flat curves to lines on the CPU, so no need to worry about + // thin curve hulls at this point. + s->codeAppendf("%s = sign(area_x2);", outputWind); + } +} + void GrCCCoverageProcessor::Shader::EmitEdgeDistanceEquation(GrGLSLVertexGeoBuilder* s, const char* leftPt, const char* rightPt, diff --git a/src/gpu/ccpr/GrCCCoverageProcessor.h b/src/gpu/ccpr/GrCCCoverageProcessor.h index 454e728ae9..e3ea34f8dd 100644 --- a/src/gpu/ccpr/GrCCCoverageProcessor.h +++ b/src/gpu/ccpr/GrCCCoverageProcessor.h @@ -133,6 +133,11 @@ public: void emitFragmentCode(const GrCCCoverageProcessor&, GrGLSLFPFragmentBuilder*, const char* skOutputColor, const char* skOutputCoverage) const; + // Calculates the winding direction of the input points (+1, -1, or 0). Wind for extremely + // thin triangles gets rounded to zero. + static void CalcWind(const GrCCCoverageProcessor&, GrGLSLVertexGeoBuilder*, const char* pts, + const char* outputWind); + // Defines an equation ("dot(float3(pt, 1), distance_equation)") that is -1 on the outside // border of a conservative raster edge and 0 on the inside. 'leftPt' and 'rightPt' must be // ordered clockwise. @@ -207,6 +212,11 @@ private: // Number of bezier points for curves, or 3 for triangles. int numInputPoints() const { return PrimitiveType::kCubics == fPrimitiveType ? 4 : 3; } + bool isTriangles() const { + return PrimitiveType::kTriangles == fPrimitiveType || + PrimitiveType::kWeightedTriangles == fPrimitiveType; + } + int hasInputWeight() const { return PrimitiveType::kWeightedTriangles == fPrimitiveType || PrimitiveType::kConics == fPrimitiveType; diff --git a/src/gpu/ccpr/GrCCCoverageProcessor_GSImpl.cpp b/src/gpu/ccpr/GrCCCoverageProcessor_GSImpl.cpp index b1d886cf8c..61bb4ec7cf 100644 --- a/src/gpu/ccpr/GrCCCoverageProcessor_GSImpl.cpp +++ b/src/gpu/ccpr/GrCCCoverageProcessor_GSImpl.cpp @@ -59,18 +59,11 @@ protected: GrShaderVar wind("wind", kHalf_GrSLType); g->declareGlobal(wind); - if (PrimitiveType::kWeightedTriangles != proc.fPrimitiveType) { - g->codeAppend ("float area_x2 = determinant(float2x2(pts[0] - pts[1], " - "pts[0] - pts[2]));"); - if (4 == numInputPoints) { - g->codeAppend ("area_x2 += determinant(float2x2(pts[0] - pts[2], " - "pts[0] - pts[3]));"); - } - g->codeAppendf("%s = sign(area_x2);", wind.c_str()); - } else { + Shader::CalcWind(proc, g, "pts", wind.c_str()); + if (PrimitiveType::kWeightedTriangles == proc.fPrimitiveType) { SkASSERT(3 == numInputPoints); SkASSERT(kFloat4_GrVertexAttribType == proc.getAttrib(0).fType); - g->codeAppendf("%s = sk_in[0].sk_Position.w;", wind.c_str()); + g->codeAppendf("%s *= sk_in[0].sk_Position.w;", wind.c_str()); } SkString emitVertexFn; @@ -306,10 +299,8 @@ public: const GrShaderVar& wind, const char* emitVertexFn) const override { fShader->emitSetupCode(g, "pts", wind.c_str()); - bool isTriangle = PrimitiveType::kTriangles == proc.fPrimitiveType || - PrimitiveType::kWeightedTriangles == proc.fPrimitiveType; g->codeAppendf("int corneridx = sk_InvocationID;"); - if (!isTriangle) { + if (!proc.isTriangles()) { g->codeAppendf("corneridx *= %i;", proc.numInputPoints() - 1); } @@ -336,7 +327,7 @@ public: Shader::CalcCornerAttenuation(g, "leftdir", "rightdir", "attenuation"); g->codeAppend ("}"); - if (isTriangle) { + if (proc.isTriangles()) { g->codeAppend ("half2 left_coverages; {"); Shader::CalcEdgeCoveragesAtBloatVertices(g, "left", "corner", "-outbloat", "-crossbloat", "left_coverages"); @@ -384,7 +375,7 @@ public: g->codeAppendf("%s(corner + crossbloat * bloat, -1, half2(1));", emitVertexFn); } - g->configure(InputType::kLines, OutputType::kTriangleStrip, 4, isTriangle ? 3 : 2); + g->configure(InputType::kLines, OutputType::kTriangleStrip, 4, proc.isTriangles() ? 3 : 2); } }; @@ -416,8 +407,7 @@ void GrCCCoverageProcessor::appendGSMesh(GrBuffer* instanceBuffer, int instanceC GrGLSLPrimitiveProcessor* GrCCCoverageProcessor::createGSImpl(std::unique_ptr<Shader> shadr) const { if (GSSubpass::kHulls == fGSSubpass) { - return (PrimitiveType::kTriangles == fPrimitiveType || - PrimitiveType::kWeightedTriangles == fPrimitiveType) + return this->isTriangles() ? (GSImpl*) new GSTriangleHullImpl(std::move(shadr)) : (GSImpl*) new GSCurveHullImpl(std::move(shadr)); } diff --git a/src/gpu/ccpr/GrCCCoverageProcessor_VSImpl.cpp b/src/gpu/ccpr/GrCCCoverageProcessor_VSImpl.cpp index dd8da96f82..08b8886aed 100644 --- a/src/gpu/ccpr/GrCCCoverageProcessor_VSImpl.cpp +++ b/src/gpu/ccpr/GrCCCoverageProcessor_VSImpl.cpp @@ -263,18 +263,12 @@ void GrCCCoverageProcessor::VSImpl::onEmitCode(EmitArgs& args, GrGPArgs* gpArgs) inputWidth, inputWidth, proc.getAttrib(kAttribIdx_X).fName, swizzle, proc.getAttrib(kAttribIdx_Y).fName, swizzle); - if (PrimitiveType::kWeightedTriangles != proc.fPrimitiveType) { - v->codeAppend ("float area_x2 = determinant(float2x2(pts[0] - pts[1], " - "pts[0] - pts[2]));"); - if (4 == numInputPoints) { - v->codeAppend ("area_x2 += determinant(float2x2(pts[0] - pts[2], " - "pts[0] - pts[3]));"); - } - v->codeAppend ("half wind = sign(area_x2);"); - } else { + v->codeAppend ("half wind;"); + Shader::CalcWind(proc, v, "pts", "wind"); + if (PrimitiveType::kWeightedTriangles == proc.fPrimitiveType) { SkASSERT(3 == numInputPoints); SkASSERT(kFloat4_GrVertexAttribType == proc.getAttrib(kAttribIdx_X).fType); - v->codeAppendf("half wind = %s.w;", proc.getAttrib(kAttribIdx_X).fName); + v->codeAppendf("wind *= %s.w;", proc.getAttrib(kAttribIdx_X).fName); } float bloat = kAABloatRadius; diff --git a/src/gpu/ccpr/GrCCGeometry.cpp b/src/gpu/ccpr/GrCCGeometry.cpp index 9fbf3e61f8..30d93ad416 100644 --- a/src/gpu/ccpr/GrCCGeometry.cpp +++ b/src/gpu/ccpr/GrCCGeometry.cpp @@ -38,14 +38,20 @@ void GrCCGeometry::beginContour(const SkPoint& pt) { SkDEBUGCODE(fBuildingContour = true); } -void GrCCGeometry::lineTo(const SkPoint& pt) { +void GrCCGeometry::lineTo(const SkPoint P[2]) { SkASSERT(fBuildingContour); - fPoints.push_back(pt); - fVerbs.push_back(Verb::kLineTo); + SkASSERT(P[0] == fPoints.back()); + Sk2f p0 = Sk2f::Load(P); + Sk2f p1 = Sk2f::Load(P+1); + this->appendLine(p0, p1); } -void GrCCGeometry::appendLine(const Sk2f& endpt) { - endpt.store(&fPoints.push_back()); +inline void GrCCGeometry::appendLine(const Sk2f& p0, const Sk2f& p1) { + SkASSERT(fPoints.back() == SkPoint::Make(p0[0], p0[1])); + if ((p0 == p1).allTrue()) { + return; + } + p1.store(&fPoints.push_back()); fVerbs.push_back(Verb::kLineTo); } @@ -142,7 +148,7 @@ void GrCCGeometry::quadraticTo(const SkPoint P[3]) { // Don't crunch on the curve if it is nearly flat (or just very small). Flat curves can break // The monotonic chopping math. if (are_collinear(p0, p1, p2)) { - this->appendLine(p2); + this->appendLine(p0, p2); return; } @@ -190,12 +196,12 @@ inline void GrCCGeometry::appendQuadratics(const Sk2f& p0, const Sk2f& p1, const inline void GrCCGeometry::appendMonotonicQuadratic(const Sk2f& p0, const Sk2f& p1, const Sk2f& p2) { // Don't send curves to the GPU if we know they are nearly flat (or just very small). if (are_collinear(p0, p1, p2)) { - SkASSERT(fPoints.back() == SkPoint::Make(p0[0], p0[1])); - this->appendLine(p2); + this->appendLine(p0, p2); return; } SkASSERT(fPoints.back() == SkPoint::Make(p0[0], p0[1])); + SkASSERT((p0 != p2).anyTrue()); p1.store(&fPoints.push_back()); p2.store(&fPoints.push_back()); fVerbs.push_back(Verb::kMonotonicQuadraticTo); @@ -466,7 +472,9 @@ void GrCCGeometry::cubicTo(const SkPoint P[4], float inflectPad, float loopInter // Don't crunch on the curve or inflate geometry if it is nearly flat (or just very small). // Flat curves can break the math below. if (are_collinear(P)) { - this->lineTo(P[3]); + Sk2f p0 = Sk2f::Load(P); + Sk2f p3 = Sk2f::Load(P+3); + this->appendLine(p0, p3); return; } @@ -570,10 +578,6 @@ void GrCCGeometry::appendCubics(AppendCubicMode mode, const Sk2f& p0, const Sk2f void GrCCGeometry::appendCubics(AppendCubicMode mode, const Sk2f& p0, const Sk2f& p1, const Sk2f& p2, const Sk2f& p3, int maxSubdivisions) { - if ((p0 == p3).allTrue()) { - return; - } - if (SkCubicType::kLoop != fCurrCubicType) { // Serpentines and cusps are always monotonic after chopping around inflection points. SkASSERT(!SkCubicIsDegenerate(fCurrCubicType)); @@ -583,8 +587,7 @@ void GrCCGeometry::appendCubics(AppendCubicMode mode, const Sk2f& p0, const Sk2f // This can cause some curves to feel slightly more flat when inspected rigorously back // and forth against another renderer, but for now this seems acceptable given the // simplicity. - SkASSERT(fPoints.back() == SkPoint::Make(p0[0], p0[1])); - this->appendLine(p3); + this->appendLine(p0, p3); return; } } else { @@ -613,12 +616,12 @@ void GrCCGeometry::appendCubics(AppendCubicMode mode, const Sk2f& p0, const Sk2f // Don't send curves to the GPU if we know they are nearly flat (or just very small). // Since the cubic segment is known to be convex at this point, our flatness check is simple. if (are_collinear(p0, (p1 + p2) * .5f, p3)) { - SkASSERT(fPoints.back() == SkPoint::Make(p0[0], p0[1])); - this->appendLine(p3); + this->appendLine(p0, p3); return; } SkASSERT(fPoints.back() == SkPoint::Make(p0[0], p0[1])); + SkASSERT((p0 != p3).anyTrue()); p1.store(&fPoints.push_back()); p2.store(&fPoints.push_back()); p3.store(&fPoints.push_back()); @@ -686,7 +689,7 @@ inline void GrCCGeometry::chopAndAppendCubicAtMidTangent(AppendCubicMode mode, c // near-flat cubics in cubicTo().) if (!(midT > 0 && midT < 1)) { // The cubic is flat. Otherwise there would be a real midtangent inside T=0..1. - this->appendLine(p3); + this->appendLine(p0, p3); return; } @@ -720,7 +723,7 @@ void GrCCGeometry::conicTo(const SkPoint P[3], float w) { // midtangents.) if (!(midT > 0 && midT < 1)) { // The conic is flat. Otherwise there would be a real midtangent inside T=0..1. - this->appendLine(p2); + this->appendLine(p0, p2); return; } @@ -747,7 +750,6 @@ void GrCCGeometry::conicTo(const SkPoint P[3], float w) { void GrCCGeometry::appendMonotonicConic(const Sk2f& p0, const Sk2f& p1, const Sk2f& p2, float w) { SkASSERT(w >= 0); - SkASSERT(fPoints.back() == SkPoint::Make(p0[0], p0[1])); Sk2f base = p2 - p0; Sk2f baseAbs = base.abs(); @@ -758,24 +760,28 @@ void GrCCGeometry::appendMonotonicConic(const Sk2f& p0, const Sk2f& p1, const Sk float h1 = std::abs(d[1] - d[0]); // Height of p1 above the base. float ht = h1*w, hs = 1 + w; // Height of the conic = ht/hs. - if (ht < (baseWidth*hs) * kFlatnessThreshold) { // i.e. ht/hs < baseWidth * kFlatnessThreshold + // i.e. (ht/hs <= baseWidth * kFlatnessThreshold). Use "<=" in case base == 0. + if (ht <= (baseWidth*hs) * kFlatnessThreshold) { // We are flat. (See rationale in are_collinear.) - this->appendLine(p2); + this->appendLine(p0, p2); return; } - if (w > 1 && h1*hs - ht < baseWidth*hs) { // i.e. w > 1 && h1 - ht/hs < baseWidth + // i.e. (w > 1 && h1 - ht/hs < baseWidth). + if (w > 1 && h1*hs - ht < baseWidth*hs) { // If we get within 1px of p1 when w > 1, we will pick up artifacts from the implicit // function's reflection. Chop at max height (T=.5) and draw a triangle instead. Sk2f p1w = p1*w; Sk2f ab = p0 + p1w; Sk2f bc = p1w + p2; Sk2f highpoint = (ab + bc) / (2*(1 + w)); - this->appendLine(highpoint); - this->appendLine(p2); + this->appendLine(p0, highpoint); + this->appendLine(highpoint, p2); return; } + SkASSERT(fPoints.back() == SkPoint::Make(p0[0], p0[1])); + SkASSERT((p0 != p2).anyTrue()); p1.store(&fPoints.push_back()); p2.store(&fPoints.push_back()); fConicWeights.push_back(w); diff --git a/src/gpu/ccpr/GrCCGeometry.h b/src/gpu/ccpr/GrCCGeometry.h index 96a38e94e7..571b3c0a52 100644 --- a/src/gpu/ccpr/GrCCGeometry.h +++ b/src/gpu/ccpr/GrCCGeometry.h @@ -76,7 +76,7 @@ public: void beginPath(); void beginContour(const SkPoint&); - void lineTo(const SkPoint&); + void lineTo(const SkPoint P[2]); void quadraticTo(const SkPoint[3]); // We pass through inflection points and loop intersections using a line and quadratic(s) @@ -97,7 +97,7 @@ public: PrimitiveTallies endContour(); // Returns the numbers of primitives needed to draw the contour. private: - inline void appendLine(const Sk2f& endpt); + inline void appendLine(const Sk2f& p0, const Sk2f& p1); inline void appendQuadratics(const Sk2f& p0, const Sk2f& p1, const Sk2f& p2); inline void appendMonotonicQuadratic(const Sk2f& p0, const Sk2f& p1, const Sk2f& p2); diff --git a/src/gpu/ccpr/GrCCPathParser.cpp b/src/gpu/ccpr/GrCCPathParser.cpp index 2740569fe6..1629a191a5 100644 --- a/src/gpu/ccpr/GrCCPathParser.cpp +++ b/src/gpu/ccpr/GrCCPathParser.cpp @@ -132,7 +132,7 @@ void GrCCPathParser::parsePath(const SkPath& path, const SkPoint* deviceSpacePts insideContour = false; continue; case SkPath::kLine_Verb: - fGeometry.lineTo(deviceSpacePts[ptsIdx]); + fGeometry.lineTo(&deviceSpacePts[ptsIdx - 1]); ++ptsIdx; continue; case SkPath::kQuad_Verb: @@ -340,8 +340,7 @@ static void emit_tessellated_fan(const GrTessellator::WindingVertex* vertices, i } else { quadPointInstanceData[indices->fWeightedTriangles++].setW( vertices[i].fPos, vertices[i+1].fPos, vertices[i + 2].fPos, atlasOffset, - // Tessellator has opposite winding sense. - -static_cast<float>(vertices[i].fWinding)); + static_cast<float>(abs(vertices[i].fWinding))); } } } |