ethannicholas | 2279325 | 2016-01-30 09:59:10 -0800 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2015 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 | #include "GrPLSPathRenderer.h" |
| 9 | |
| 10 | #include "SkChunkAlloc.h" |
| 11 | #include "SkGeometry.h" |
| 12 | #include "SkPathPriv.h" |
| 13 | #include "SkString.h" |
| 14 | #include "SkTSort.h" |
| 15 | #include "SkTraceEvent.h" |
| 16 | #include "GrBatchFlushState.h" |
| 17 | #include "GrBatchTest.h" |
| 18 | #include "GrCaps.h" |
| 19 | #include "GrContext.h" |
| 20 | #include "GrDefaultGeoProcFactory.h" |
| 21 | #include "GrPLSGeometryProcessor.h" |
| 22 | #include "GrInvariantOutput.h" |
| 23 | #include "GrPathUtils.h" |
| 24 | #include "GrProcessor.h" |
bsalomon | bb24383 | 2016-07-22 07:10:19 -0700 | [diff] [blame] | 25 | #include "GrPipelineBuilder.h" |
bsalomon | 6663acf | 2016-05-10 09:14:17 -0700 | [diff] [blame] | 26 | #include "GrStyle.h" |
ethannicholas | 2279325 | 2016-01-30 09:59:10 -0800 | [diff] [blame] | 27 | #include "GrTessellator.h" |
| 28 | #include "batches/GrVertexBatch.h" |
| 29 | #include "glsl/GrGLSLGeometryProcessor.h" |
| 30 | #include "gl/builders/GrGLProgramBuilder.h" |
| 31 | #include "glsl/GrGLSLPLSPathRendering.h" |
| 32 | |
| 33 | GrPLSPathRenderer::GrPLSPathRenderer() { |
| 34 | } |
| 35 | |
| 36 | struct PLSVertex { |
| 37 | SkPoint fPos; |
| 38 | // for triangles, these are the three triangle vertices |
halcanary | 9d524f2 | 2016-03-29 09:03:52 -0700 | [diff] [blame] | 39 | // for quads, vert1 is the texture UV coords, and vert2 and vert3 are the line segment |
ethannicholas | 2279325 | 2016-01-30 09:59:10 -0800 | [diff] [blame] | 40 | // comprising the flat edge of the quad |
| 41 | SkPoint fVert1; |
| 42 | SkPoint fVert2; |
| 43 | SkPoint fVert3; |
| 44 | int fWinding; |
| 45 | }; |
| 46 | typedef SkTArray<PLSVertex, true> PLSVertices; |
| 47 | |
| 48 | typedef SkTArray<SkPoint, true> FinishVertices; |
| 49 | |
| 50 | static const float kCubicTolerance = 0.5f; |
| 51 | static const float kConicTolerance = 0.5f; |
| 52 | |
| 53 | static const float kBloatSize = 1.0f; |
| 54 | |
| 55 | static const float kBloatLimit = 640000.0f; |
| 56 | |
| 57 | #define kQuadNumVertices 5 |
| 58 | static void add_quad(SkPoint pts[3], PLSVertices& vertices) { |
halcanary | 9d524f2 | 2016-03-29 09:03:52 -0700 | [diff] [blame] | 59 | SkPoint normal = SkPoint::Make(pts[0].fY - pts[2].fY, |
ethannicholas | 2279325 | 2016-01-30 09:59:10 -0800 | [diff] [blame] | 60 | pts[2].fX - pts[0].fX); |
| 61 | normal.setLength(kBloatSize); |
| 62 | SkScalar cross = (pts[1] - pts[0]).cross(pts[2] - pts[0]); |
| 63 | if (cross < 0) { |
| 64 | normal = -normal; |
| 65 | } |
| 66 | PLSVertex quad[kQuadNumVertices]; |
| 67 | quad[0].fPos = pts[0] + normal; |
| 68 | quad[1].fPos = pts[0] - normal; |
| 69 | quad[2].fPos = pts[1] - normal; |
| 70 | quad[3].fPos = pts[2] - normal; |
| 71 | quad[4].fPos = pts[2] + normal; |
| 72 | for (int i = 0; i < kQuadNumVertices; i++) { |
| 73 | quad[i].fWinding = cross < 0 ? 1 : -1; |
| 74 | if (cross > 0.0) { |
| 75 | quad[i].fVert2 = pts[0]; |
| 76 | quad[i].fVert3 = pts[2]; |
| 77 | } |
| 78 | else { |
| 79 | quad[i].fVert2 = pts[2]; |
| 80 | quad[i].fVert3 = pts[0]; |
| 81 | } |
| 82 | } |
| 83 | GrPathUtils::QuadUVMatrix DevToUV(pts); |
| 84 | DevToUV.apply<kQuadNumVertices, sizeof(PLSVertex), sizeof(SkPoint)>(quad); |
| 85 | for (int i = 2; i < kQuadNumVertices; i++) { |
| 86 | vertices.push_back(quad[0]); |
| 87 | vertices.push_back(quad[i - 1]); |
| 88 | vertices.push_back(quad[i]); |
| 89 | } |
| 90 | } |
| 91 | |
| 92 | /* Used by bloat_tri; outsets a single point. */ |
| 93 | static bool outset(SkPoint* p1, SkPoint line1, SkPoint line2) { |
| 94 | // rotate the two line vectors 90 degrees to form the normals, and compute |
| 95 | // the dot product of the normals |
| 96 | SkScalar dotProd = line1.fY * line2.fY + line1.fX * line2.fX; |
| 97 | SkScalar lengthSq = 1.0f / ((1.0f - dotProd) / 2.0f); |
| 98 | if (lengthSq > kBloatLimit) { |
| 99 | return false; |
| 100 | } |
| 101 | SkPoint bisector = line1 + line2; |
| 102 | bisector.setLength(SkScalarSqrt(lengthSq) * kBloatSize); |
| 103 | *p1 += bisector; |
| 104 | return true; |
| 105 | } |
| 106 | |
| 107 | /* Bloats a triangle so as to create a border kBloatSize pixels wide all around it. */ |
| 108 | static bool bloat_tri(SkPoint pts[3]) { |
| 109 | SkPoint line1 = pts[0] - pts[1]; |
| 110 | line1.normalize(); |
| 111 | SkPoint line2 = pts[0] - pts[2]; |
| 112 | line2.normalize(); |
| 113 | SkPoint line3 = pts[1] - pts[2]; |
| 114 | line3.normalize(); |
| 115 | |
| 116 | SkPoint result[3]; |
| 117 | result[0] = pts[0]; |
| 118 | if (!outset(&result[0], line1, line2)) { |
| 119 | return false; |
| 120 | } |
| 121 | result[1] = pts[1]; |
| 122 | if (!outset(&result[1], -line1, line3)) { |
| 123 | return false; |
| 124 | } |
| 125 | result[2] = pts[2]; |
| 126 | if (!outset(&result[2], -line3, -line2)) { |
| 127 | return false; |
| 128 | } |
| 129 | pts[0] = result[0]; |
| 130 | pts[1] = result[1]; |
| 131 | pts[2] = result[2]; |
| 132 | return true; |
| 133 | } |
| 134 | |
halcanary | 9d524f2 | 2016-03-29 09:03:52 -0700 | [diff] [blame] | 135 | static bool get_geometry(const SkPath& path, const SkMatrix& m, PLSVertices& triVertices, |
ethannicholas | 2279325 | 2016-01-30 09:59:10 -0800 | [diff] [blame] | 136 | PLSVertices& quadVertices, GrResourceProvider* resourceProvider, |
| 137 | SkRect bounds) { |
| 138 | SkScalar screenSpaceTol = GrPathUtils::kDefaultTolerance; |
| 139 | SkScalar tol = GrPathUtils::scaleToleranceToSrc(screenSpaceTol, m, bounds); |
| 140 | int contourCnt; |
| 141 | int maxPts = GrPathUtils::worstCasePointCount(path, &contourCnt, tol); |
| 142 | if (maxPts <= 0) { |
| 143 | return 0; |
| 144 | } |
| 145 | SkPath linesOnlyPath; |
| 146 | linesOnlyPath.setFillType(path.getFillType()); |
| 147 | SkSTArray<15, SkPoint, true> quadPoints; |
ethannicholas | 2279325 | 2016-01-30 09:59:10 -0800 | [diff] [blame] | 148 | SkPath::Iter iter(path, true); |
| 149 | bool done = false; |
| 150 | while (!done) { |
| 151 | SkPoint pts[4]; |
| 152 | SkPath::Verb verb = iter.next(pts); |
| 153 | switch (verb) { |
| 154 | case SkPath::kMove_Verb: |
| 155 | SkASSERT(quadPoints.count() % 3 == 0); |
| 156 | for (int i = 0; i < quadPoints.count(); i += 3) { |
| 157 | add_quad(&quadPoints[i], quadVertices); |
| 158 | } |
| 159 | quadPoints.reset(); |
| 160 | m.mapPoints(&pts[0], 1); |
| 161 | linesOnlyPath.moveTo(pts[0]); |
| 162 | break; |
| 163 | case SkPath::kLine_Verb: |
| 164 | m.mapPoints(&pts[1], 1); |
| 165 | linesOnlyPath.lineTo(pts[1]); |
| 166 | break; |
| 167 | case SkPath::kQuad_Verb: |
| 168 | m.mapPoints(pts, 3); |
| 169 | linesOnlyPath.lineTo(pts[2]); |
| 170 | quadPoints.push_back(pts[0]); |
| 171 | quadPoints.push_back(pts[1]); |
| 172 | quadPoints.push_back(pts[2]); |
| 173 | break; |
| 174 | case SkPath::kCubic_Verb: { |
| 175 | m.mapPoints(pts, 4); |
| 176 | SkSTArray<15, SkPoint, true> quads; |
bsalomon | 18fab30 | 2016-02-16 08:00:05 -0800 | [diff] [blame] | 177 | GrPathUtils::convertCubicToQuads(pts, kCubicTolerance, &quads); |
ethannicholas | 2279325 | 2016-01-30 09:59:10 -0800 | [diff] [blame] | 178 | int count = quads.count(); |
| 179 | for (int q = 0; q < count; q += 3) { |
| 180 | linesOnlyPath.lineTo(quads[q + 2]); |
| 181 | quadPoints.push_back(quads[q]); |
| 182 | quadPoints.push_back(quads[q + 1]); |
| 183 | quadPoints.push_back(quads[q + 2]); |
| 184 | } |
| 185 | break; |
| 186 | } |
| 187 | case SkPath::kConic_Verb: { |
| 188 | m.mapPoints(pts, 3); |
| 189 | SkScalar weight = iter.conicWeight(); |
| 190 | SkAutoConicToQuads converter; |
| 191 | const SkPoint* quads = converter.computeQuads(pts, weight, kConicTolerance); |
| 192 | int count = converter.countQuads(); |
| 193 | for (int i = 0; i < count; ++i) { |
| 194 | linesOnlyPath.lineTo(quads[2 * i + 2]); |
| 195 | quadPoints.push_back(quads[2 * i]); |
| 196 | quadPoints.push_back(quads[2 * i + 1]); |
| 197 | quadPoints.push_back(quads[2 * i + 2]); |
| 198 | } |
| 199 | break; |
| 200 | } |
| 201 | case SkPath::kClose_Verb: |
| 202 | linesOnlyPath.close(); |
| 203 | break; |
| 204 | case SkPath::kDone_Verb: |
| 205 | done = true; |
| 206 | break; |
| 207 | default: SkASSERT(false); |
| 208 | } |
| 209 | } |
| 210 | SkASSERT(quadPoints.count() % 3 == 0); |
| 211 | for (int i = 0; i < quadPoints.count(); i += 3) { |
| 212 | add_quad(&quadPoints[i], quadVertices); |
| 213 | } |
| 214 | |
| 215 | static const GrUniqueKey::Domain kDomain = GrUniqueKey::GenerateDomain(); |
| 216 | GrUniqueKey key; |
| 217 | GrUniqueKey::Builder builder(&key, kDomain, 2); |
| 218 | builder[0] = path.getGenerationID(); |
| 219 | builder[1] = path.getFillType(); |
| 220 | builder.finish(); |
| 221 | GrTessellator::WindingVertex* windingVertices; |
| 222 | int triVertexCount = GrTessellator::PathToVertices(linesOnlyPath, 0, bounds, &windingVertices); |
| 223 | if (triVertexCount > 0) { |
| 224 | for (int i = 0; i < triVertexCount; i += 3) { |
| 225 | SkPoint p1 = windingVertices[i].fPos; |
| 226 | SkPoint p2 = windingVertices[i + 1].fPos; |
| 227 | SkPoint p3 = windingVertices[i + 2].fPos; |
| 228 | int winding = windingVertices[i].fWinding; |
| 229 | SkASSERT(windingVertices[i + 1].fWinding == winding); |
| 230 | SkASSERT(windingVertices[i + 2].fWinding == winding); |
| 231 | SkScalar cross = (p2 - p1).cross(p3 - p1); |
| 232 | SkPoint bloated[3] = { p1, p2, p3 }; |
| 233 | if (cross < 0.0f) { |
| 234 | SkTSwap(p1, p3); |
| 235 | } |
| 236 | if (bloat_tri(bloated)) { |
| 237 | triVertices.push_back({ bloated[0], p1, p2, p3, winding }); |
| 238 | triVertices.push_back({ bloated[1], p1, p2, p3, winding }); |
| 239 | triVertices.push_back({ bloated[2], p1, p2, p3, winding }); |
| 240 | } |
| 241 | else { |
| 242 | SkScalar minX = SkTMin(p1.fX, SkTMin(p2.fX, p3.fX)) - 1.0f; |
| 243 | SkScalar minY = SkTMin(p1.fY, SkTMin(p2.fY, p3.fY)) - 1.0f; |
| 244 | SkScalar maxX = SkTMax(p1.fX, SkTMax(p2.fX, p3.fX)) + 1.0f; |
| 245 | SkScalar maxY = SkTMax(p1.fY, SkTMax(p2.fY, p3.fY)) + 1.0f; |
| 246 | triVertices.push_back({ { minX, minY }, p1, p2, p3, winding }); |
| 247 | triVertices.push_back({ { maxX, minY }, p1, p2, p3, winding }); |
| 248 | triVertices.push_back({ { minX, maxY }, p1, p2, p3, winding }); |
| 249 | triVertices.push_back({ { maxX, minY }, p1, p2, p3, winding }); |
| 250 | triVertices.push_back({ { maxX, maxY }, p1, p2, p3, winding }); |
| 251 | triVertices.push_back({ { minX, maxY }, p1, p2, p3, winding }); |
| 252 | } |
| 253 | } |
| 254 | delete[] windingVertices; |
| 255 | } |
| 256 | return triVertexCount > 0 || quadVertices.count() > 0; |
| 257 | } |
| 258 | |
| 259 | class PLSAATriangleEffect : public GrPLSGeometryProcessor { |
| 260 | public: |
| 261 | |
| 262 | static GrPLSGeometryProcessor* Create(const SkMatrix& localMatrix, |
| 263 | bool usesLocalCoords) { |
| 264 | return new PLSAATriangleEffect(localMatrix, usesLocalCoords); |
| 265 | } |
| 266 | |
| 267 | virtual ~PLSAATriangleEffect() {} |
| 268 | |
| 269 | const char* name() const override { return "PLSAATriangle"; } |
| 270 | |
| 271 | const Attribute* inPosition() const { return fInPosition; } |
| 272 | const Attribute* inVertex1() const { return fInVertex1; } |
| 273 | const Attribute* inVertex2() const { return fInVertex2; } |
| 274 | const Attribute* inVertex3() const { return fInVertex3; } |
| 275 | const Attribute* inWindings() const { return fInWindings; } |
| 276 | const SkMatrix& localMatrix() const { return fLocalMatrix; } |
| 277 | bool usesLocalCoords() const { return fUsesLocalCoords; } |
| 278 | |
| 279 | class GLSLProcessor : public GrGLSLGeometryProcessor { |
| 280 | public: |
| 281 | GLSLProcessor(const GrGeometryProcessor&) {} |
| 282 | |
| 283 | void onEmitCode(EmitArgs& args, GrGPArgs* gpArgs) override { |
| 284 | const PLSAATriangleEffect& te = args.fGP.cast<PLSAATriangleEffect>(); |
| 285 | GrGLSLVertexBuilder* vsBuilder = args.fVertBuilder; |
| 286 | GrGLSLVaryingHandler* varyingHandler = args.fVaryingHandler; |
| 287 | GrGLSLUniformHandler* uniformHandler = args.fUniformHandler; |
| 288 | |
| 289 | varyingHandler->emitAttributes(te); |
| 290 | |
| 291 | this->setupPosition(vsBuilder, gpArgs, te.inPosition()->fName); |
| 292 | |
| 293 | GrGLSLVertToFrag v1(kVec2f_GrSLType); |
| 294 | varyingHandler->addVarying("Vertex1", &v1, kHigh_GrSLPrecision); |
halcanary | 9d524f2 | 2016-03-29 09:03:52 -0700 | [diff] [blame] | 295 | vsBuilder->codeAppendf("%s = vec2(%s.x, %s.y);", |
ethannicholas | 2279325 | 2016-01-30 09:59:10 -0800 | [diff] [blame] | 296 | v1.vsOut(), |
halcanary | 9d524f2 | 2016-03-29 09:03:52 -0700 | [diff] [blame] | 297 | te.inVertex1()->fName, |
ethannicholas | 2279325 | 2016-01-30 09:59:10 -0800 | [diff] [blame] | 298 | te.inVertex1()->fName); |
| 299 | |
| 300 | GrGLSLVertToFrag v2(kVec2f_GrSLType); |
| 301 | varyingHandler->addVarying("Vertex2", &v2, kHigh_GrSLPrecision); |
halcanary | 9d524f2 | 2016-03-29 09:03:52 -0700 | [diff] [blame] | 302 | vsBuilder->codeAppendf("%s = vec2(%s.x, %s.y);", |
ethannicholas | 2279325 | 2016-01-30 09:59:10 -0800 | [diff] [blame] | 303 | v2.vsOut(), |
halcanary | 9d524f2 | 2016-03-29 09:03:52 -0700 | [diff] [blame] | 304 | te.inVertex2()->fName, |
ethannicholas | 2279325 | 2016-01-30 09:59:10 -0800 | [diff] [blame] | 305 | te.inVertex2()->fName); |
| 306 | |
| 307 | GrGLSLVertToFrag v3(kVec2f_GrSLType); |
| 308 | varyingHandler->addVarying("Vertex3", &v3, kHigh_GrSLPrecision); |
halcanary | 9d524f2 | 2016-03-29 09:03:52 -0700 | [diff] [blame] | 309 | vsBuilder->codeAppendf("%s = vec2(%s.x, %s.y);", |
ethannicholas | 2279325 | 2016-01-30 09:59:10 -0800 | [diff] [blame] | 310 | v3.vsOut(), |
halcanary | 9d524f2 | 2016-03-29 09:03:52 -0700 | [diff] [blame] | 311 | te.inVertex3()->fName, |
ethannicholas | 2279325 | 2016-01-30 09:59:10 -0800 | [diff] [blame] | 312 | te.inVertex3()->fName); |
| 313 | |
| 314 | GrGLSLVertToFrag delta1(kVec2f_GrSLType); |
| 315 | varyingHandler->addVarying("delta1", &delta1, kHigh_GrSLPrecision); |
halcanary | 9d524f2 | 2016-03-29 09:03:52 -0700 | [diff] [blame] | 316 | vsBuilder->codeAppendf("%s = vec2(%s.x - %s.x, %s.y - %s.y) * 0.5;", |
ethannicholas | 2279325 | 2016-01-30 09:59:10 -0800 | [diff] [blame] | 317 | delta1.vsOut(), v1.vsOut(), v2.vsOut(), v2.vsOut(), v1.vsOut()); |
| 318 | |
| 319 | GrGLSLVertToFrag delta2(kVec2f_GrSLType); |
| 320 | varyingHandler->addVarying("delta2", &delta2, kHigh_GrSLPrecision); |
halcanary | 9d524f2 | 2016-03-29 09:03:52 -0700 | [diff] [blame] | 321 | vsBuilder->codeAppendf("%s = vec2(%s.x - %s.x, %s.y - %s.y) * 0.5;", |
ethannicholas | 2279325 | 2016-01-30 09:59:10 -0800 | [diff] [blame] | 322 | delta2.vsOut(), v2.vsOut(), v3.vsOut(), v3.vsOut(), v2.vsOut()); |
| 323 | |
| 324 | GrGLSLVertToFrag delta3(kVec2f_GrSLType); |
| 325 | varyingHandler->addVarying("delta3", &delta3, kHigh_GrSLPrecision); |
halcanary | 9d524f2 | 2016-03-29 09:03:52 -0700 | [diff] [blame] | 326 | vsBuilder->codeAppendf("%s = vec2(%s.x - %s.x, %s.y - %s.y) * 0.5;", |
ethannicholas | 2279325 | 2016-01-30 09:59:10 -0800 | [diff] [blame] | 327 | delta3.vsOut(), v3.vsOut(), v1.vsOut(), v1.vsOut(), v3.vsOut()); |
| 328 | |
| 329 | GrGLSLVertToFrag windings(kInt_GrSLType); |
cdalton | c08f196 | 2016-02-12 12:14:06 -0800 | [diff] [blame] | 330 | varyingHandler->addFlatVarying("windings", &windings, kLow_GrSLPrecision); |
halcanary | 9d524f2 | 2016-03-29 09:03:52 -0700 | [diff] [blame] | 331 | vsBuilder->codeAppendf("%s = %s;", |
ethannicholas | 2279325 | 2016-01-30 09:59:10 -0800 | [diff] [blame] | 332 | windings.vsOut(), te.inWindings()->fName); |
| 333 | |
| 334 | // emit transforms |
halcanary | 9d524f2 | 2016-03-29 09:03:52 -0700 | [diff] [blame] | 335 | this->emitTransforms(vsBuilder, varyingHandler, uniformHandler, gpArgs->fPositionVar, |
bsalomon | a624bf3 | 2016-09-20 09:12:47 -0700 | [diff] [blame] | 336 | te.inPosition()->fName, te.localMatrix(), |
| 337 | args.fFPCoordTransformHandler); |
ethannicholas | 2279325 | 2016-01-30 09:59:10 -0800 | [diff] [blame] | 338 | |
cdalton | 8528541 | 2016-02-18 12:37:07 -0800 | [diff] [blame] | 339 | GrGLSLPPFragmentBuilder* fsBuilder = args.fFragBuilder; |
ethannicholas | 2279325 | 2016-01-30 09:59:10 -0800 | [diff] [blame] | 340 | SkAssertResult(fsBuilder->enableFeature( |
| 341 | GrGLSLFragmentShaderBuilder::kPixelLocalStorage_GLSLFeature)); |
ethannicholas | 2279325 | 2016-01-30 09:59:10 -0800 | [diff] [blame] | 342 | fsBuilder->declAppendf(GR_GL_PLS_PATH_DATA_DECL); |
halcanary | 9d524f2 | 2016-03-29 09:03:52 -0700 | [diff] [blame] | 343 | // Compute four subsamples, each shifted a quarter pixel along x and y from |
| 344 | // gl_FragCoord. The oriented box positioning of the subsamples is of course not |
ethannicholas | 2279325 | 2016-01-30 09:59:10 -0800 | [diff] [blame] | 345 | // optimal, but it greatly simplifies the math and this simplification is necessary for |
| 346 | // performance reasons. |
halcanary | 9d524f2 | 2016-03-29 09:03:52 -0700 | [diff] [blame] | 347 | fsBuilder->codeAppendf("highp vec2 firstSample = %s.xy - vec2(0.25);", |
ethannicholas | 2279325 | 2016-01-30 09:59:10 -0800 | [diff] [blame] | 348 | fsBuilder->fragmentPosition()); |
| 349 | fsBuilder->codeAppendf("highp vec2 delta1 = %s;", delta1.fsIn()); |
| 350 | fsBuilder->codeAppendf("highp vec2 delta2 = %s;", delta2.fsIn()); |
| 351 | fsBuilder->codeAppendf("highp vec2 delta3 = %s;", delta3.fsIn()); |
| 352 | // Check whether first sample is inside the triangle by computing three dot products. If |
| 353 | // all are < 0, we're inside. The first vector in each case is half of what it is |
| 354 | // "supposed" to be, because we re-use them later as adjustment factors for which half |
| 355 | // is the correct value, so we multiply the dots by two to compensate. |
halcanary | 9d524f2 | 2016-03-29 09:03:52 -0700 | [diff] [blame] | 356 | fsBuilder->codeAppendf("highp float d1 = dot(delta1, (firstSample - %s).yx) * 2.0;", |
ethannicholas | 2279325 | 2016-01-30 09:59:10 -0800 | [diff] [blame] | 357 | v1.fsIn()); |
halcanary | 9d524f2 | 2016-03-29 09:03:52 -0700 | [diff] [blame] | 358 | fsBuilder->codeAppendf("highp float d2 = dot(delta2, (firstSample - %s).yx) * 2.0;", |
ethannicholas | 2279325 | 2016-01-30 09:59:10 -0800 | [diff] [blame] | 359 | v2.fsIn()); |
halcanary | 9d524f2 | 2016-03-29 09:03:52 -0700 | [diff] [blame] | 360 | fsBuilder->codeAppendf("highp float d3 = dot(delta3, (firstSample - %s).yx) * 2.0;", |
ethannicholas | 2279325 | 2016-01-30 09:59:10 -0800 | [diff] [blame] | 361 | v3.fsIn()); |
| 362 | fsBuilder->codeAppend("highp float dmax = max(d1, max(d2, d3));"); |
| 363 | fsBuilder->codeAppendf("pls.windings[0] += (dmax <= 0.0) ? %s : 0;", windings.fsIn()); |
| 364 | // for subsequent samples, we don't recalculate the entire dot product -- just adjust it |
| 365 | // to the value it would have if we did recompute it. |
| 366 | fsBuilder->codeAppend("d1 += delta1.x;"); |
| 367 | fsBuilder->codeAppend("d2 += delta2.x;"); |
| 368 | fsBuilder->codeAppend("d3 += delta3.x;"); |
| 369 | fsBuilder->codeAppend("dmax = max(d1, max(d2, d3));"); |
| 370 | fsBuilder->codeAppendf("pls.windings[1] += (dmax <= 0.0) ? %s : 0;", windings.fsIn()); |
| 371 | fsBuilder->codeAppend("d1 += delta1.y;"); |
| 372 | fsBuilder->codeAppend("d2 += delta2.y;"); |
| 373 | fsBuilder->codeAppend("d3 += delta3.y;"); |
| 374 | fsBuilder->codeAppend("dmax = max(d1, max(d2, d3));"); |
| 375 | fsBuilder->codeAppendf("pls.windings[2] += (dmax <= 0.0) ? %s : 0;", windings.fsIn()); |
| 376 | fsBuilder->codeAppend("d1 -= delta1.x;"); |
| 377 | fsBuilder->codeAppend("d2 -= delta2.x;"); |
| 378 | fsBuilder->codeAppend("d3 -= delta3.x;"); |
| 379 | fsBuilder->codeAppend("dmax = max(d1, max(d2, d3));"); |
| 380 | fsBuilder->codeAppendf("pls.windings[3] += (dmax <= 0.0) ? %s : 0;", windings.fsIn()); |
| 381 | } |
| 382 | |
| 383 | static inline void GenKey(const GrGeometryProcessor& gp, |
| 384 | const GrGLSLCaps&, |
| 385 | GrProcessorKeyBuilder* b) { |
| 386 | const PLSAATriangleEffect& te = gp.cast<PLSAATriangleEffect>(); |
| 387 | uint32_t key = 0; |
| 388 | key |= te.localMatrix().hasPerspective() ? 0x1 : 0x0; |
| 389 | b->add32(key); |
| 390 | } |
| 391 | |
bsalomon | a624bf3 | 2016-09-20 09:12:47 -0700 | [diff] [blame] | 392 | void setData(const GrGLSLProgramDataManager& pdman, const GrPrimitiveProcessor& gp, |
| 393 | FPCoordTransformIter&& transformIter) override { |
| 394 | this->setTransformDataHelper(gp.cast<PLSAATriangleEffect>().fLocalMatrix, pdman, |
| 395 | &transformIter); |
ethannicholas | 2279325 | 2016-01-30 09:59:10 -0800 | [diff] [blame] | 396 | } |
| 397 | |
| 398 | private: |
| 399 | typedef GrGLSLGeometryProcessor INHERITED; |
| 400 | }; |
| 401 | |
| 402 | virtual void getGLSLProcessorKey(const GrGLSLCaps& caps, |
| 403 | GrProcessorKeyBuilder* b) const override { |
| 404 | GLSLProcessor::GenKey(*this, caps, b); |
| 405 | } |
| 406 | |
| 407 | virtual GrGLSLPrimitiveProcessor* createGLSLInstance(const GrGLSLCaps&) const override { |
| 408 | return new GLSLProcessor(*this); |
| 409 | } |
| 410 | |
| 411 | private: |
| 412 | PLSAATriangleEffect(const SkMatrix& localMatrix, bool usesLocalCoords) |
| 413 | : fLocalMatrix(localMatrix) |
| 414 | , fUsesLocalCoords(usesLocalCoords) { |
| 415 | this->initClassID<PLSAATriangleEffect>(); |
bsalomon | 6cb807b | 2016-08-17 11:33:39 -0700 | [diff] [blame] | 416 | fInPosition = &this->addVertexAttrib("inPosition", kVec2f_GrVertexAttribType, |
| 417 | kHigh_GrSLPrecision); |
| 418 | fInVertex1 = &this->addVertexAttrib("inVertex1", kVec2f_GrVertexAttribType, |
| 419 | kHigh_GrSLPrecision); |
| 420 | fInVertex2 = &this->addVertexAttrib("inVertex2", kVec2f_GrVertexAttribType, |
| 421 | kHigh_GrSLPrecision); |
| 422 | fInVertex3 = &this->addVertexAttrib("inVertex3", kVec2f_GrVertexAttribType, |
| 423 | kHigh_GrSLPrecision); |
| 424 | fInWindings = &this->addVertexAttrib("inWindings", kInt_GrVertexAttribType, |
| 425 | kLow_GrSLPrecision); |
ethannicholas | 2279325 | 2016-01-30 09:59:10 -0800 | [diff] [blame] | 426 | this->setWillReadFragmentPosition(); |
| 427 | } |
| 428 | |
| 429 | const Attribute* fInPosition; |
| 430 | const Attribute* fInVertex1; |
| 431 | const Attribute* fInVertex2; |
| 432 | const Attribute* fInVertex3; |
| 433 | const Attribute* fInWindings; |
| 434 | SkMatrix fLocalMatrix; |
| 435 | bool fUsesLocalCoords; |
halcanary | 9d524f2 | 2016-03-29 09:03:52 -0700 | [diff] [blame] | 436 | |
ethannicholas | 2279325 | 2016-01-30 09:59:10 -0800 | [diff] [blame] | 437 | GR_DECLARE_GEOMETRY_PROCESSOR_TEST; |
| 438 | |
| 439 | typedef GrGeometryProcessor INHERITED; |
| 440 | }; |
| 441 | |
| 442 | /////////////////////////////////////////////////////////////////////////////// |
| 443 | |
| 444 | /* |
| 445 | * Quadratic specified by 0=u^2-v canonical coords. u and v are the first |
| 446 | * two components of the vertex attribute. Coverage is based on signed |
| 447 | * distance with negative being inside, positive outside. The edge is specified in |
| 448 | * window space (y-down). If either the third or fourth component of the interpolated |
| 449 | * vertex coord is > 0 then the pixel is considered outside the edge. This is used to |
| 450 | * attempt to trim to a portion of the infinite quad. |
| 451 | * Requires shader derivative instruction support. |
| 452 | */ |
| 453 | |
| 454 | class PLSQuadEdgeEffect : public GrPLSGeometryProcessor { |
| 455 | public: |
| 456 | |
| 457 | static GrPLSGeometryProcessor* Create(const SkMatrix& localMatrix, |
| 458 | bool usesLocalCoords) { |
| 459 | return new PLSQuadEdgeEffect(localMatrix, usesLocalCoords); |
| 460 | } |
| 461 | |
| 462 | virtual ~PLSQuadEdgeEffect() {} |
| 463 | |
| 464 | const char* name() const override { return "PLSQuadEdge"; } |
| 465 | |
| 466 | const Attribute* inPosition() const { return fInPosition; } |
| 467 | const Attribute* inUV() const { return fInUV; } |
| 468 | const Attribute* inEndpoint1() const { return fInEndpoint1; } |
| 469 | const Attribute* inEndpoint2() const { return fInEndpoint2; } |
| 470 | const Attribute* inWindings() const { return fInWindings; } |
| 471 | const SkMatrix& localMatrix() const { return fLocalMatrix; } |
| 472 | bool usesLocalCoords() const { return fUsesLocalCoords; } |
| 473 | |
| 474 | class GLSLProcessor : public GrGLSLGeometryProcessor { |
| 475 | public: |
| 476 | GLSLProcessor(const GrGeometryProcessor&) {} |
| 477 | |
| 478 | void onEmitCode(EmitArgs& args, GrGPArgs* gpArgs) override { |
| 479 | const PLSQuadEdgeEffect& qe = args.fGP.cast<PLSQuadEdgeEffect>(); |
| 480 | GrGLSLVertexBuilder* vsBuilder = args.fVertBuilder; |
| 481 | GrGLSLVaryingHandler* varyingHandler = args.fVaryingHandler; |
| 482 | GrGLSLUniformHandler* uniformHandler = args.fUniformHandler; |
| 483 | |
| 484 | // emit attributes |
| 485 | varyingHandler->emitAttributes(qe); |
| 486 | |
| 487 | GrGLSLVertToFrag uv(kVec2f_GrSLType); |
| 488 | varyingHandler->addVarying("uv", &uv, kHigh_GrSLPrecision); |
| 489 | vsBuilder->codeAppendf("%s = %s;", uv.vsOut(), qe.inUV()->fName); |
| 490 | |
| 491 | GrGLSLVertToFrag ep1(kVec2f_GrSLType); |
| 492 | varyingHandler->addVarying("endpoint1", &ep1, kHigh_GrSLPrecision); |
halcanary | 9d524f2 | 2016-03-29 09:03:52 -0700 | [diff] [blame] | 493 | vsBuilder->codeAppendf("%s = vec2(%s.x, %s.y);", ep1.vsOut(), |
ethannicholas | 2279325 | 2016-01-30 09:59:10 -0800 | [diff] [blame] | 494 | qe.inEndpoint1()->fName, qe.inEndpoint1()->fName); |
| 495 | |
| 496 | GrGLSLVertToFrag ep2(kVec2f_GrSLType); |
| 497 | varyingHandler->addVarying("endpoint2", &ep2, kHigh_GrSLPrecision); |
halcanary | 9d524f2 | 2016-03-29 09:03:52 -0700 | [diff] [blame] | 498 | vsBuilder->codeAppendf("%s = vec2(%s.x, %s.y);", ep2.vsOut(), |
ethannicholas | 2279325 | 2016-01-30 09:59:10 -0800 | [diff] [blame] | 499 | qe.inEndpoint2()->fName, qe.inEndpoint2()->fName); |
| 500 | |
| 501 | GrGLSLVertToFrag delta(kVec2f_GrSLType); |
| 502 | varyingHandler->addVarying("delta", &delta, kHigh_GrSLPrecision); |
halcanary | 9d524f2 | 2016-03-29 09:03:52 -0700 | [diff] [blame] | 503 | vsBuilder->codeAppendf("%s = vec2(%s.x - %s.x, %s.y - %s.y) * 0.5;", |
| 504 | delta.vsOut(), ep1.vsOut(), ep2.vsOut(), ep2.vsOut(), |
ethannicholas | 2279325 | 2016-01-30 09:59:10 -0800 | [diff] [blame] | 505 | ep1.vsOut()); |
| 506 | |
| 507 | GrGLSLVertToFrag windings(kInt_GrSLType); |
cdalton | c08f196 | 2016-02-12 12:14:06 -0800 | [diff] [blame] | 508 | varyingHandler->addFlatVarying("windings", &windings, kLow_GrSLPrecision); |
halcanary | 9d524f2 | 2016-03-29 09:03:52 -0700 | [diff] [blame] | 509 | vsBuilder->codeAppendf("%s = %s;", |
ethannicholas | 2279325 | 2016-01-30 09:59:10 -0800 | [diff] [blame] | 510 | windings.vsOut(), qe.inWindings()->fName); |
| 511 | |
| 512 | // Setup position |
| 513 | this->setupPosition(vsBuilder, gpArgs, qe.inPosition()->fName); |
| 514 | |
| 515 | // emit transforms |
halcanary | 9d524f2 | 2016-03-29 09:03:52 -0700 | [diff] [blame] | 516 | this->emitTransforms(vsBuilder, varyingHandler, uniformHandler, gpArgs->fPositionVar, |
bsalomon | a624bf3 | 2016-09-20 09:12:47 -0700 | [diff] [blame] | 517 | qe.inPosition()->fName, qe.localMatrix(), |
| 518 | args.fFPCoordTransformHandler); |
ethannicholas | 2279325 | 2016-01-30 09:59:10 -0800 | [diff] [blame] | 519 | |
cdalton | 8528541 | 2016-02-18 12:37:07 -0800 | [diff] [blame] | 520 | GrGLSLPPFragmentBuilder* fsBuilder = args.fFragBuilder; |
ethannicholas | 2279325 | 2016-01-30 09:59:10 -0800 | [diff] [blame] | 521 | SkAssertResult(fsBuilder->enableFeature( |
| 522 | GrGLSLFragmentShaderBuilder::kPixelLocalStorage_GLSLFeature)); |
ethannicholas | 2279325 | 2016-01-30 09:59:10 -0800 | [diff] [blame] | 523 | static const int QUAD_ARGS = 2; |
| 524 | GrGLSLShaderVar inQuadArgs[QUAD_ARGS] = { |
| 525 | GrGLSLShaderVar("dot", kFloat_GrSLType, 0, kHigh_GrSLPrecision), |
| 526 | GrGLSLShaderVar("uv", kVec2f_GrSLType, 0, kHigh_GrSLPrecision) |
| 527 | }; |
| 528 | SkString inQuadName; |
| 529 | |
| 530 | const char* inQuadCode = "if (uv.x * uv.x <= uv.y) {" |
| 531 | "return dot >= 0.0;" |
| 532 | "} else {" |
| 533 | "return false;" |
| 534 | "}"; |
halcanary | 9d524f2 | 2016-03-29 09:03:52 -0700 | [diff] [blame] | 535 | fsBuilder->emitFunction(kBool_GrSLType, "in_quad", QUAD_ARGS, inQuadArgs, inQuadCode, |
ethannicholas | 2279325 | 2016-01-30 09:59:10 -0800 | [diff] [blame] | 536 | &inQuadName); |
| 537 | fsBuilder->declAppendf(GR_GL_PLS_PATH_DATA_DECL); |
| 538 | // keep the derivative instructions outside the conditional |
| 539 | fsBuilder->codeAppendf("highp vec2 uvdX = dFdx(%s);", uv.fsIn()); |
| 540 | fsBuilder->codeAppendf("highp vec2 uvdY = dFdy(%s);", uv.fsIn()); |
| 541 | fsBuilder->codeAppend("highp vec2 uvIncX = uvdX * 0.45 + uvdY * -0.1;"); |
| 542 | fsBuilder->codeAppend("highp vec2 uvIncY = uvdX * 0.1 + uvdY * 0.55;"); |
halcanary | 9d524f2 | 2016-03-29 09:03:52 -0700 | [diff] [blame] | 543 | fsBuilder->codeAppendf("highp vec2 uv = %s.xy - uvdX * 0.35 - uvdY * 0.25;", |
ethannicholas | 2279325 | 2016-01-30 09:59:10 -0800 | [diff] [blame] | 544 | uv.fsIn()); |
| 545 | fsBuilder->codeAppendf("highp vec2 firstSample = %s.xy - vec2(0.25);", |
| 546 | fsBuilder->fragmentPosition()); |
halcanary | 9d524f2 | 2016-03-29 09:03:52 -0700 | [diff] [blame] | 547 | fsBuilder->codeAppendf("highp float d = dot(%s, (firstSample - %s).yx) * 2.0;", |
ethannicholas | 2279325 | 2016-01-30 09:59:10 -0800 | [diff] [blame] | 548 | delta.fsIn(), ep1.fsIn()); |
halcanary | 9d524f2 | 2016-03-29 09:03:52 -0700 | [diff] [blame] | 549 | fsBuilder->codeAppendf("pls.windings[0] += %s(d, uv) ? %s : 0;", inQuadName.c_str(), |
ethannicholas | 2279325 | 2016-01-30 09:59:10 -0800 | [diff] [blame] | 550 | windings.fsIn()); |
| 551 | fsBuilder->codeAppend("uv += uvIncX;"); |
| 552 | fsBuilder->codeAppendf("d += %s.x;", delta.fsIn()); |
halcanary | 9d524f2 | 2016-03-29 09:03:52 -0700 | [diff] [blame] | 553 | fsBuilder->codeAppendf("pls.windings[1] += %s(d, uv) ? %s : 0;", inQuadName.c_str(), |
ethannicholas | 2279325 | 2016-01-30 09:59:10 -0800 | [diff] [blame] | 554 | windings.fsIn()); |
| 555 | fsBuilder->codeAppend("uv += uvIncY;"); |
| 556 | fsBuilder->codeAppendf("d += %s.y;", delta.fsIn()); |
halcanary | 9d524f2 | 2016-03-29 09:03:52 -0700 | [diff] [blame] | 557 | fsBuilder->codeAppendf("pls.windings[2] += %s(d, uv) ? %s : 0;", inQuadName.c_str(), |
ethannicholas | 2279325 | 2016-01-30 09:59:10 -0800 | [diff] [blame] | 558 | windings.fsIn()); |
| 559 | fsBuilder->codeAppend("uv -= uvIncX;"); |
| 560 | fsBuilder->codeAppendf("d -= %s.x;", delta.fsIn()); |
halcanary | 9d524f2 | 2016-03-29 09:03:52 -0700 | [diff] [blame] | 561 | fsBuilder->codeAppendf("pls.windings[3] += %s(d, uv) ? %s : 0;", inQuadName.c_str(), |
ethannicholas | 2279325 | 2016-01-30 09:59:10 -0800 | [diff] [blame] | 562 | windings.fsIn()); |
| 563 | } |
| 564 | |
| 565 | static inline void GenKey(const GrGeometryProcessor& gp, |
| 566 | const GrGLSLCaps&, |
| 567 | GrProcessorKeyBuilder* b) { |
| 568 | const PLSQuadEdgeEffect& qee = gp.cast<PLSQuadEdgeEffect>(); |
| 569 | uint32_t key = 0; |
| 570 | key |= qee.usesLocalCoords() && qee.localMatrix().hasPerspective() ? 0x1 : 0x0; |
| 571 | b->add32(key); |
| 572 | } |
| 573 | |
bsalomon | a624bf3 | 2016-09-20 09:12:47 -0700 | [diff] [blame] | 574 | void setData(const GrGLSLProgramDataManager& pdman, const GrPrimitiveProcessor& gp, |
| 575 | FPCoordTransformIter&& transformIter) override { |
| 576 | this->setTransformDataHelper(gp.cast<PLSQuadEdgeEffect>().fLocalMatrix, pdman, |
| 577 | &transformIter); |
ethannicholas | 2279325 | 2016-01-30 09:59:10 -0800 | [diff] [blame] | 578 | } |
| 579 | |
| 580 | private: |
| 581 | typedef GrGLSLGeometryProcessor INHERITED; |
| 582 | }; |
| 583 | |
| 584 | virtual void getGLSLProcessorKey(const GrGLSLCaps& caps, |
| 585 | GrProcessorKeyBuilder* b) const override { |
| 586 | GLSLProcessor::GenKey(*this, caps, b); |
| 587 | } |
| 588 | |
| 589 | virtual GrGLSLPrimitiveProcessor* createGLSLInstance(const GrGLSLCaps&) const override { |
| 590 | return new GLSLProcessor(*this); |
| 591 | } |
| 592 | |
| 593 | private: |
| 594 | PLSQuadEdgeEffect(const SkMatrix& localMatrix, bool usesLocalCoords) |
| 595 | : fLocalMatrix(localMatrix) |
| 596 | , fUsesLocalCoords(usesLocalCoords) { |
| 597 | this->initClassID<PLSQuadEdgeEffect>(); |
bsalomon | 6cb807b | 2016-08-17 11:33:39 -0700 | [diff] [blame] | 598 | fInPosition = &this->addVertexAttrib("inPosition", kVec2f_GrVertexAttribType, |
| 599 | kHigh_GrSLPrecision); |
| 600 | fInUV = &this->addVertexAttrib("inUV", kVec2f_GrVertexAttribType, kHigh_GrSLPrecision); |
| 601 | fInEndpoint1 = &this->addVertexAttrib("inEndpoint1", kVec2f_GrVertexAttribType, |
| 602 | kHigh_GrSLPrecision); |
| 603 | fInEndpoint2 = &this->addVertexAttrib("inEndpoint2", kVec2f_GrVertexAttribType, |
| 604 | kHigh_GrSLPrecision); |
| 605 | fInWindings = &this->addVertexAttrib("inWindings", kInt_GrVertexAttribType, |
| 606 | kLow_GrSLPrecision); |
ethannicholas | 2279325 | 2016-01-30 09:59:10 -0800 | [diff] [blame] | 607 | this->setWillReadFragmentPosition(); |
| 608 | } |
| 609 | |
| 610 | const Attribute* fInPosition; |
| 611 | const Attribute* fInUV; |
| 612 | const Attribute* fInEndpoint1; |
| 613 | const Attribute* fInEndpoint2; |
| 614 | const Attribute* fInWindings; |
| 615 | SkMatrix fLocalMatrix; |
| 616 | bool fUsesLocalCoords; |
halcanary | 9d524f2 | 2016-03-29 09:03:52 -0700 | [diff] [blame] | 617 | |
ethannicholas | 2279325 | 2016-01-30 09:59:10 -0800 | [diff] [blame] | 618 | GR_DECLARE_GEOMETRY_PROCESSOR_TEST; |
| 619 | |
| 620 | typedef GrGeometryProcessor INHERITED; |
| 621 | }; |
| 622 | |
| 623 | class PLSFinishEffect : public GrGeometryProcessor { |
| 624 | public: |
| 625 | |
| 626 | static GrGeometryProcessor* Create(GrColor color, bool useEvenOdd, const SkMatrix& localMatrix, |
| 627 | bool usesLocalCoords) { |
| 628 | return new PLSFinishEffect(color, useEvenOdd, localMatrix, usesLocalCoords); |
| 629 | } |
| 630 | |
| 631 | virtual ~PLSFinishEffect() {} |
| 632 | |
| 633 | const char* name() const override { return "PLSFinish"; } |
| 634 | |
| 635 | const Attribute* inPosition() const { return fInPosition; } |
| 636 | GrColor color() const { return fColor; } |
| 637 | bool colorIgnored() const { return GrColor_ILLEGAL == fColor; } |
| 638 | const SkMatrix& localMatrix() const { return fLocalMatrix; } |
| 639 | bool usesLocalCoords() const { return fUsesLocalCoords; } |
halcanary | 9d524f2 | 2016-03-29 09:03:52 -0700 | [diff] [blame] | 640 | |
| 641 | GrPixelLocalStorageState getPixelLocalStorageState() const override { |
ethannicholas | 2279325 | 2016-01-30 09:59:10 -0800 | [diff] [blame] | 642 | return GrPixelLocalStorageState::kFinish_GrPixelLocalStorageState; |
| 643 | } |
| 644 | |
| 645 | const char* getDestColorOverride() const override { |
halcanary | 9d524f2 | 2016-03-29 09:03:52 -0700 | [diff] [blame] | 646 | return GR_GL_PLS_DSTCOLOR_NAME; |
ethannicholas | 2279325 | 2016-01-30 09:59:10 -0800 | [diff] [blame] | 647 | } |
| 648 | |
| 649 | class GLSLProcessor : public GrGLSLGeometryProcessor { |
| 650 | public: |
| 651 | GLSLProcessor(const GrGeometryProcessor&) {} |
| 652 | |
| 653 | void onEmitCode(EmitArgs& args, GrGPArgs* gpArgs) override { |
| 654 | const PLSFinishEffect& fe = args.fGP.cast<PLSFinishEffect>(); |
| 655 | GrGLSLVertexBuilder* vsBuilder = args.fVertBuilder; |
| 656 | GrGLSLVaryingHandler* varyingHandler = args.fVaryingHandler; |
| 657 | GrGLSLUniformHandler* uniformHandler = args.fUniformHandler; |
| 658 | |
cdalton | 5e58cee | 2016-02-11 12:49:47 -0800 | [diff] [blame] | 659 | fUseEvenOdd = uniformHandler->addUniform(kFragment_GrShaderFlag, |
halcanary | 9d524f2 | 2016-03-29 09:03:52 -0700 | [diff] [blame] | 660 | kFloat_GrSLType, kLow_GrSLPrecision, |
ethannicholas | 2279325 | 2016-01-30 09:59:10 -0800 | [diff] [blame] | 661 | "useEvenOdd"); |
| 662 | const char* useEvenOdd = uniformHandler->getUniformCStr(fUseEvenOdd); |
| 663 | |
| 664 | varyingHandler->emitAttributes(fe); |
| 665 | this->setupPosition(vsBuilder, gpArgs, fe.inPosition()->fName); |
halcanary | 9d524f2 | 2016-03-29 09:03:52 -0700 | [diff] [blame] | 666 | this->emitTransforms(vsBuilder, varyingHandler, uniformHandler, gpArgs->fPositionVar, |
bsalomon | a624bf3 | 2016-09-20 09:12:47 -0700 | [diff] [blame] | 667 | fe.inPosition()->fName, fe.localMatrix(), |
| 668 | args.fFPCoordTransformHandler); |
ethannicholas | 2279325 | 2016-01-30 09:59:10 -0800 | [diff] [blame] | 669 | |
cdalton | 8528541 | 2016-02-18 12:37:07 -0800 | [diff] [blame] | 670 | GrGLSLPPFragmentBuilder* fsBuilder = args.fFragBuilder; |
ethannicholas | 2279325 | 2016-01-30 09:59:10 -0800 | [diff] [blame] | 671 | SkAssertResult(fsBuilder->enableFeature( |
| 672 | GrGLSLFragmentShaderBuilder::kPixelLocalStorage_GLSLFeature)); |
| 673 | fsBuilder->declAppendf(GR_GL_PLS_PATH_DATA_DECL); |
| 674 | fsBuilder->codeAppend("float coverage;"); |
| 675 | fsBuilder->codeAppendf("if (%s != 0.0) {", useEvenOdd); |
| 676 | fsBuilder->codeAppend("coverage = float(abs(pls.windings[0]) % 2) * 0.25;"); |
| 677 | fsBuilder->codeAppend("coverage += float(abs(pls.windings[1]) % 2) * 0.25;"); |
| 678 | fsBuilder->codeAppend("coverage += float(abs(pls.windings[2]) % 2) * 0.25;"); |
| 679 | fsBuilder->codeAppend("coverage += float(abs(pls.windings[3]) % 2) * 0.25;"); |
| 680 | fsBuilder->codeAppend("} else {"); |
| 681 | fsBuilder->codeAppend("coverage = pls.windings[0] != 0 ? 0.25 : 0.0;"); |
| 682 | fsBuilder->codeAppend("coverage += pls.windings[1] != 0 ? 0.25 : 0.0;"); |
| 683 | fsBuilder->codeAppend("coverage += pls.windings[2] != 0 ? 0.25 : 0.0;"); |
| 684 | fsBuilder->codeAppend("coverage += pls.windings[3] != 0 ? 0.25 : 0.0;"); |
| 685 | fsBuilder->codeAppend("}"); |
| 686 | if (!fe.colorIgnored()) { |
halcanary | 9d524f2 | 2016-03-29 09:03:52 -0700 | [diff] [blame] | 687 | this->setupUniformColor(fsBuilder, uniformHandler, args.fOutputColor, |
ethannicholas | 2279325 | 2016-01-30 09:59:10 -0800 | [diff] [blame] | 688 | &fColorUniform); |
| 689 | } |
| 690 | fsBuilder->codeAppendf("%s = vec4(coverage);", args.fOutputCoverage); |
| 691 | fsBuilder->codeAppendf("%s = vec4(1.0, 0.0, 1.0, 1.0);", args.fOutputColor); |
| 692 | } |
| 693 | |
| 694 | static inline void GenKey(const GrGeometryProcessor& gp, |
| 695 | const GrGLSLCaps&, |
| 696 | GrProcessorKeyBuilder* b) { |
| 697 | const PLSFinishEffect& fe = gp.cast<PLSFinishEffect>(); |
| 698 | uint32_t key = 0; |
| 699 | key |= fe.usesLocalCoords() && fe.localMatrix().hasPerspective() ? 0x1 : 0x0; |
| 700 | b->add32(key); |
| 701 | } |
| 702 | |
bsalomon | a624bf3 | 2016-09-20 09:12:47 -0700 | [diff] [blame] | 703 | void setData(const GrGLSLProgramDataManager& pdman, const GrPrimitiveProcessor& gp, |
| 704 | FPCoordTransformIter&& transformIter) override { |
ethannicholas | 2279325 | 2016-01-30 09:59:10 -0800 | [diff] [blame] | 705 | const PLSFinishEffect& fe = gp.cast<PLSFinishEffect>(); |
| 706 | pdman.set1f(fUseEvenOdd, fe.fUseEvenOdd); |
| 707 | if (fe.color() != fColor && !fe.colorIgnored()) { |
| 708 | GrGLfloat c[4]; |
| 709 | GrColorToRGBAFloat(fe.color(), c); |
| 710 | pdman.set4fv(fColorUniform, 1, c); |
| 711 | fColor = fe.color(); |
| 712 | } |
bsalomon | a624bf3 | 2016-09-20 09:12:47 -0700 | [diff] [blame] | 713 | this->setTransformDataHelper(fe.fLocalMatrix, pdman, &transformIter); |
ethannicholas | 2279325 | 2016-01-30 09:59:10 -0800 | [diff] [blame] | 714 | } |
| 715 | |
| 716 | private: |
| 717 | GrColor fColor; |
| 718 | UniformHandle fColorUniform; |
| 719 | UniformHandle fUseEvenOdd; |
| 720 | |
| 721 | typedef GrGLSLGeometryProcessor INHERITED; |
| 722 | }; |
| 723 | |
| 724 | virtual void getGLSLProcessorKey(const GrGLSLCaps& caps, |
| 725 | GrProcessorKeyBuilder* b) const override { |
| 726 | GLSLProcessor::GenKey(*this, caps, b); |
| 727 | } |
| 728 | |
| 729 | virtual GrGLSLPrimitiveProcessor* createGLSLInstance(const GrGLSLCaps&) const override { |
| 730 | return new GLSLProcessor(*this); |
| 731 | } |
| 732 | |
| 733 | private: |
halcanary | 9d524f2 | 2016-03-29 09:03:52 -0700 | [diff] [blame] | 734 | PLSFinishEffect(GrColor color, bool useEvenOdd, const SkMatrix& localMatrix, |
ethannicholas | 2279325 | 2016-01-30 09:59:10 -0800 | [diff] [blame] | 735 | bool usesLocalCoords) |
| 736 | : fColor(color) |
| 737 | , fUseEvenOdd(useEvenOdd) |
| 738 | , fLocalMatrix(localMatrix) |
| 739 | , fUsesLocalCoords(usesLocalCoords) { |
| 740 | this->initClassID<PLSFinishEffect>(); |
bsalomon | 6cb807b | 2016-08-17 11:33:39 -0700 | [diff] [blame] | 741 | fInPosition = &this->addVertexAttrib("inPosition", kVec2f_GrVertexAttribType, |
| 742 | kHigh_GrSLPrecision); |
ethannicholas | 2279325 | 2016-01-30 09:59:10 -0800 | [diff] [blame] | 743 | } |
| 744 | |
| 745 | const Attribute* fInPosition; |
| 746 | GrColor fColor; |
| 747 | bool fUseEvenOdd; |
| 748 | SkMatrix fLocalMatrix; |
| 749 | bool fUsesLocalCoords; |
| 750 | |
| 751 | typedef GrGeometryProcessor INHERITED; |
| 752 | }; |
| 753 | |
| 754 | /////////////////////////////////////////////////////////////////////////////// |
| 755 | |
| 756 | bool GrPLSPathRenderer::onCanDrawPath(const CanDrawPathArgs& args) const { |
| 757 | // We have support for even-odd rendering, but are having some troublesome |
| 758 | // seams. Disable in the presence of even-odd for now. |
bsalomon | 8acedde | 2016-06-24 10:42:16 -0700 | [diff] [blame] | 759 | SkPath path; |
| 760 | args.fShape->asPath(&path); |
ethannicholas | 2279325 | 2016-01-30 09:59:10 -0800 | [diff] [blame] | 761 | return args.fShaderCaps->shaderDerivativeSupport() && args.fAntiAlias && |
bsalomon | 8acedde | 2016-06-24 10:42:16 -0700 | [diff] [blame] | 762 | args.fShape->style().isSimpleFill() && !path.isInverseFillType() && |
| 763 | path.getFillType() == SkPath::FillType::kWinding_FillType; |
ethannicholas | 2279325 | 2016-01-30 09:59:10 -0800 | [diff] [blame] | 764 | } |
| 765 | |
| 766 | class PLSPathBatch : public GrVertexBatch { |
| 767 | public: |
| 768 | DEFINE_BATCH_CLASS_ID |
bsalomon | 065c8cf | 2016-06-30 20:45:38 -0700 | [diff] [blame] | 769 | PLSPathBatch(GrColor color, const SkPath& path, const SkMatrix& viewMatrix) |
| 770 | : INHERITED(ClassID()) |
| 771 | , fColor(color) |
| 772 | , fPath(path) |
| 773 | , fViewMatrix(viewMatrix) { |
| 774 | // compute bounds |
bsalomon | 88cf17d | 2016-07-08 06:40:56 -0700 | [diff] [blame] | 775 | this->setTransformedBounds(path.getBounds(), fViewMatrix, HasAABloat::kYes, |
| 776 | IsZeroArea::kNo); |
ethannicholas | 2279325 | 2016-01-30 09:59:10 -0800 | [diff] [blame] | 777 | } |
| 778 | |
| 779 | const char* name() const override { return "PLSBatch"; } |
| 780 | |
halcanary | 9d524f2 | 2016-03-29 09:03:52 -0700 | [diff] [blame] | 781 | void computePipelineOptimizations(GrInitInvariantOutput* color, |
ethannicholas | 2279325 | 2016-01-30 09:59:10 -0800 | [diff] [blame] | 782 | GrInitInvariantOutput* coverage, |
| 783 | GrBatchToXPOverrides* overrides) const override { |
| 784 | // When this is called on a batch, there is only one geometry bundle |
bsalomon | 065c8cf | 2016-06-30 20:45:38 -0700 | [diff] [blame] | 785 | color->setKnownFourComponents(fColor); |
ethannicholas | 2279325 | 2016-01-30 09:59:10 -0800 | [diff] [blame] | 786 | coverage->setUnknownSingleComponent(); |
| 787 | overrides->fUsePLSDstRead = true; |
| 788 | } |
| 789 | |
| 790 | void initBatchTracker(const GrXPOverridesForBatch& overrides) override { |
| 791 | // Handle any color overrides |
| 792 | if (!overrides.readsColor()) { |
bsalomon | 065c8cf | 2016-06-30 20:45:38 -0700 | [diff] [blame] | 793 | fColor = GrColor_ILLEGAL; |
ethannicholas | 2279325 | 2016-01-30 09:59:10 -0800 | [diff] [blame] | 794 | } |
bsalomon | 065c8cf | 2016-06-30 20:45:38 -0700 | [diff] [blame] | 795 | overrides.getOverrideColorIfSet(&fColor); |
ethannicholas | 2279325 | 2016-01-30 09:59:10 -0800 | [diff] [blame] | 796 | |
| 797 | // setup batch properties |
bsalomon | 065c8cf | 2016-06-30 20:45:38 -0700 | [diff] [blame] | 798 | fUsesLocalCoords = overrides.readsLocalCoords(); |
ethannicholas | 2279325 | 2016-01-30 09:59:10 -0800 | [diff] [blame] | 799 | } |
| 800 | |
| 801 | void onPrepareDraws(Target* target) const override { |
ethannicholas | 2279325 | 2016-01-30 09:59:10 -0800 | [diff] [blame] | 802 | |
| 803 | SkMatrix invert; |
bsalomon | 065c8cf | 2016-06-30 20:45:38 -0700 | [diff] [blame] | 804 | if (fUsesLocalCoords && !fViewMatrix.invert(&invert)) { |
ethannicholas | 2279325 | 2016-01-30 09:59:10 -0800 | [diff] [blame] | 805 | SkDebugf("Could not invert viewmatrix\n"); |
| 806 | return; |
| 807 | } |
| 808 | |
| 809 | // Setup GrGeometryProcessors |
Hal Canary | 144caf5 | 2016-11-07 17:57:18 -0500 | [diff] [blame] | 810 | sk_sp<GrPLSGeometryProcessor> triangleProcessor( |
bsalomon | 065c8cf | 2016-06-30 20:45:38 -0700 | [diff] [blame] | 811 | PLSAATriangleEffect::Create(invert, fUsesLocalCoords)); |
Hal Canary | 144caf5 | 2016-11-07 17:57:18 -0500 | [diff] [blame] | 812 | sk_sp<GrPLSGeometryProcessor> quadProcessor( |
bsalomon | 065c8cf | 2016-06-30 20:45:38 -0700 | [diff] [blame] | 813 | PLSQuadEdgeEffect::Create(invert, fUsesLocalCoords)); |
ethannicholas | 2279325 | 2016-01-30 09:59:10 -0800 | [diff] [blame] | 814 | |
| 815 | GrResourceProvider* rp = target->resourceProvider(); |
bsalomon | 065c8cf | 2016-06-30 20:45:38 -0700 | [diff] [blame] | 816 | SkRect bounds; |
| 817 | this->bounds().roundOut(&bounds); |
| 818 | triangleProcessor->setBounds(bounds); |
| 819 | quadProcessor->setBounds(bounds); |
ethannicholas | 2279325 | 2016-01-30 09:59:10 -0800 | [diff] [blame] | 820 | |
bsalomon | 065c8cf | 2016-06-30 20:45:38 -0700 | [diff] [blame] | 821 | // We use the fact that SkPath::transform path does subdivision based on |
| 822 | // perspective. Otherwise, we apply the view matrix when copying to the |
| 823 | // segment representation. |
| 824 | const SkMatrix* viewMatrix = &fViewMatrix; |
ethannicholas | 2279325 | 2016-01-30 09:59:10 -0800 | [diff] [blame] | 825 | |
bsalomon | 065c8cf | 2016-06-30 20:45:38 -0700 | [diff] [blame] | 826 | // We avoid initializing the path unless we have to |
| 827 | const SkPath* pathPtr = &fPath; |
| 828 | SkTLazy<SkPath> tmpPath; |
| 829 | if (viewMatrix->hasPerspective()) { |
| 830 | SkPath* tmpPathPtr = tmpPath.init(*pathPtr); |
| 831 | tmpPathPtr->setIsVolatile(true); |
| 832 | tmpPathPtr->transform(*viewMatrix); |
| 833 | viewMatrix = &SkMatrix::I(); |
| 834 | pathPtr = tmpPathPtr; |
| 835 | } |
ethannicholas | 2279325 | 2016-01-30 09:59:10 -0800 | [diff] [blame] | 836 | |
bsalomon | 065c8cf | 2016-06-30 20:45:38 -0700 | [diff] [blame] | 837 | GrMesh mesh; |
ethannicholas | 2279325 | 2016-01-30 09:59:10 -0800 | [diff] [blame] | 838 | |
bsalomon | 065c8cf | 2016-06-30 20:45:38 -0700 | [diff] [blame] | 839 | PLSVertices triVertices; |
| 840 | PLSVertices quadVertices; |
| 841 | if (!get_geometry(*pathPtr, *viewMatrix, triVertices, quadVertices, rp, bounds)) { |
| 842 | return; |
| 843 | } |
ethannicholas | 2279325 | 2016-01-30 09:59:10 -0800 | [diff] [blame] | 844 | |
bsalomon | 065c8cf | 2016-06-30 20:45:38 -0700 | [diff] [blame] | 845 | if (triVertices.count()) { |
| 846 | const GrBuffer* triVertexBuffer; |
| 847 | int firstTriVertex; |
| 848 | size_t triStride = triangleProcessor->getVertexStride(); |
| 849 | PLSVertex* triVerts = reinterpret_cast<PLSVertex*>(target->makeVertexSpace( |
| 850 | triStride, triVertices.count(), &triVertexBuffer, &firstTriVertex)); |
| 851 | if (!triVerts) { |
ethannicholas | 2279325 | 2016-01-30 09:59:10 -0800 | [diff] [blame] | 852 | SkDebugf("Could not allocate vertices\n"); |
| 853 | return; |
| 854 | } |
bsalomon | 065c8cf | 2016-06-30 20:45:38 -0700 | [diff] [blame] | 855 | for (int i = 0; i < triVertices.count(); ++i) { |
| 856 | triVerts[i] = triVertices[i]; |
| 857 | } |
| 858 | mesh.init(kTriangles_GrPrimitiveType, triVertexBuffer, firstTriVertex, |
| 859 | triVertices.count()); |
Hal Canary | 144caf5 | 2016-11-07 17:57:18 -0500 | [diff] [blame] | 860 | target->draw(triangleProcessor.get(), mesh); |
ethannicholas | 2279325 | 2016-01-30 09:59:10 -0800 | [diff] [blame] | 861 | } |
ethannicholas | 2279325 | 2016-01-30 09:59:10 -0800 | [diff] [blame] | 862 | |
bsalomon | 065c8cf | 2016-06-30 20:45:38 -0700 | [diff] [blame] | 863 | if (quadVertices.count()) { |
| 864 | const GrBuffer* quadVertexBuffer; |
| 865 | int firstQuadVertex; |
| 866 | size_t quadStride = quadProcessor->getVertexStride(); |
| 867 | PLSVertex* quadVerts = reinterpret_cast<PLSVertex*>(target->makeVertexSpace( |
| 868 | quadStride, quadVertices.count(), &quadVertexBuffer, &firstQuadVertex)); |
| 869 | if (!quadVerts) { |
| 870 | SkDebugf("Could not allocate vertices\n"); |
| 871 | return; |
| 872 | } |
| 873 | for (int i = 0; i < quadVertices.count(); ++i) { |
| 874 | quadVerts[i] = quadVertices[i]; |
| 875 | } |
| 876 | mesh.init(kTriangles_GrPrimitiveType, quadVertexBuffer, firstQuadVertex, |
| 877 | quadVertices.count()); |
Hal Canary | 144caf5 | 2016-11-07 17:57:18 -0500 | [diff] [blame] | 878 | target->draw(quadProcessor.get(), mesh); |
bsalomon | 065c8cf | 2016-06-30 20:45:38 -0700 | [diff] [blame] | 879 | } |
| 880 | |
Hal Canary | 144caf5 | 2016-11-07 17:57:18 -0500 | [diff] [blame] | 881 | sk_sp<GrGeometryProcessor> finishProcessor( |
bsalomon | 065c8cf | 2016-06-30 20:45:38 -0700 | [diff] [blame] | 882 | PLSFinishEffect::Create(fColor, |
| 883 | pathPtr->getFillType() == |
| 884 | SkPath::FillType::kEvenOdd_FillType, |
| 885 | invert, |
| 886 | fUsesLocalCoords)); |
| 887 | const GrBuffer* rectVertexBuffer; |
| 888 | size_t finishStride = finishProcessor->getVertexStride(); |
| 889 | int firstRectVertex; |
| 890 | static const int kRectVertexCount = 6; |
| 891 | SkPoint* rectVerts = reinterpret_cast<SkPoint*>(target->makeVertexSpace( |
| 892 | finishStride, kRectVertexCount, &rectVertexBuffer, &firstRectVertex)); |
| 893 | if (!rectVerts) { |
| 894 | SkDebugf("Could not allocate vertices\n"); |
| 895 | return; |
| 896 | } |
| 897 | rectVerts[0] = { bounds.fLeft, bounds.fTop }; |
| 898 | rectVerts[1] = { bounds.fLeft, bounds.fBottom }; |
| 899 | rectVerts[2] = { bounds.fRight, bounds.fBottom }; |
| 900 | rectVerts[3] = { bounds.fLeft, bounds.fTop }; |
| 901 | rectVerts[4] = { bounds.fRight, bounds.fTop }; |
| 902 | rectVerts[5] = { bounds.fRight, bounds.fBottom }; |
| 903 | |
| 904 | mesh.init(kTriangles_GrPrimitiveType, rectVertexBuffer, firstRectVertex, |
| 905 | kRectVertexCount); |
Hal Canary | 144caf5 | 2016-11-07 17:57:18 -0500 | [diff] [blame] | 906 | target->draw(finishProcessor.get(), mesh); |
bsalomon | 065c8cf | 2016-06-30 20:45:38 -0700 | [diff] [blame] | 907 | } |
ethannicholas | 2279325 | 2016-01-30 09:59:10 -0800 | [diff] [blame] | 908 | |
| 909 | private: |
ethannicholas | 2279325 | 2016-01-30 09:59:10 -0800 | [diff] [blame] | 910 | bool onCombineIfPossible(GrBatch* t, const GrCaps& caps) override { |
| 911 | return false; |
| 912 | } |
| 913 | |
bsalomon | 065c8cf | 2016-06-30 20:45:38 -0700 | [diff] [blame] | 914 | bool fUsesLocalCoords; |
ethannicholas | 2279325 | 2016-01-30 09:59:10 -0800 | [diff] [blame] | 915 | |
bsalomon | 065c8cf | 2016-06-30 20:45:38 -0700 | [diff] [blame] | 916 | GrColor fColor; |
| 917 | SkPath fPath; |
| 918 | SkMatrix fViewMatrix; |
ethannicholas | 2279325 | 2016-01-30 09:59:10 -0800 | [diff] [blame] | 919 | typedef GrVertexBatch INHERITED; |
| 920 | }; |
| 921 | |
| 922 | SkDEBUGCODE(bool inPLSDraw = false;) |
| 923 | bool GrPLSPathRenderer::onDrawPath(const DrawPathArgs& args) { |
caryclark | d656200 | 2016-07-27 12:02:07 -0700 | [diff] [blame] | 924 | SkASSERT(!args.fShape->isEmpty()); |
ethannicholas | 2279325 | 2016-01-30 09:59:10 -0800 | [diff] [blame] | 925 | SkASSERT(!inPLSDraw); |
| 926 | SkDEBUGCODE(inPLSDraw = true;) |
bsalomon | 065c8cf | 2016-06-30 20:45:38 -0700 | [diff] [blame] | 927 | SkPath path; |
| 928 | args.fShape->asPath(&path); |
ethannicholas | 2279325 | 2016-01-30 09:59:10 -0800 | [diff] [blame] | 929 | |
Hal Canary | 144caf5 | 2016-11-07 17:57:18 -0500 | [diff] [blame] | 930 | sk_sp<GrDrawBatch> batch(new PLSPathBatch(args.fPaint->getColor(), |
| 931 | path, *args.fViewMatrix)); |
robertphillips | 976f5f0 | 2016-06-03 10:59:20 -0700 | [diff] [blame] | 932 | |
Brian Osman | 1105224 | 2016-10-27 14:47:55 -0400 | [diff] [blame] | 933 | GrPipelineBuilder pipelineBuilder(*args.fPaint, |
| 934 | args.fRenderTargetContext->mustUseHWAA(*args.fPaint)); |
bsalomon | bb24383 | 2016-07-22 07:10:19 -0700 | [diff] [blame] | 935 | pipelineBuilder.setUserStencil(args.fUserStencilSettings); |
| 936 | |
Hal Canary | 144caf5 | 2016-11-07 17:57:18 -0500 | [diff] [blame] | 937 | args.fRenderTargetContext->drawBatch(pipelineBuilder, *args.fClip, batch.get()); |
ethannicholas | 2279325 | 2016-01-30 09:59:10 -0800 | [diff] [blame] | 938 | |
| 939 | SkDEBUGCODE(inPLSDraw = false;) |
| 940 | return true; |
| 941 | |
| 942 | } |
| 943 | |
| 944 | /////////////////////////////////////////////////////////////////////////////////////////////////// |
| 945 | |
| 946 | #ifdef GR_TEST_UTILS |
| 947 | |
| 948 | DRAW_BATCH_TEST_DEFINE(PLSPathBatch) { |
bsalomon | 065c8cf | 2016-06-30 20:45:38 -0700 | [diff] [blame] | 949 | GrColor color = GrRandomColor(random); |
| 950 | SkMatrix vm = GrTest::TestMatrixInvertible(random); |
| 951 | SkPath path = GrTest::TestPathConvex(random); |
ethannicholas | 2279325 | 2016-01-30 09:59:10 -0800 | [diff] [blame] | 952 | |
bsalomon | 065c8cf | 2016-06-30 20:45:38 -0700 | [diff] [blame] | 953 | return new PLSPathBatch(color, path, vm); |
ethannicholas | 2279325 | 2016-01-30 09:59:10 -0800 | [diff] [blame] | 954 | } |
| 955 | |
| 956 | #endif |