Chris Dalton | 1a325d2 | 2017-07-14 15:17:41 -0600 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2017 Google Inc. |
| 3 | * |
| 4 | * Use of this source code is governed by a BSD-style license that can be |
| 5 | * found in the LICENSE file. |
| 6 | */ |
| 7 | |
Chris Dalton | 383a2ef | 2018-01-08 17:21:41 -0500 | [diff] [blame] | 8 | #include "GrCCCoverageProcessor.h" |
Chris Dalton | 1a325d2 | 2017-07-14 15:17:41 -0600 | [diff] [blame] | 9 | |
Chris Dalton | 6a3dbee | 2017-10-16 10:44:41 -0600 | [diff] [blame] | 10 | #include "SkMakeUnique.h" |
Chris Dalton | 383a2ef | 2018-01-08 17:21:41 -0500 | [diff] [blame] | 11 | #include "ccpr/GrCCCubicShader.h" |
| 12 | #include "ccpr/GrCCQuadraticShader.h" |
Chris Dalton | fe462ef | 2018-03-08 15:54:01 +0000 | [diff] [blame^] | 13 | #include "ccpr/GrCCTriangleShader.h" |
Chris Dalton | 90e8fb1 | 2017-12-22 02:24:53 -0700 | [diff] [blame] | 14 | #include "glsl/GrGLSLVertexGeoBuilder.h" |
Chris Dalton | 1a325d2 | 2017-07-14 15:17:41 -0600 | [diff] [blame] | 15 | #include "glsl/GrGLSLFragmentShaderBuilder.h" |
Chris Dalton | 1fbdb61 | 2017-12-12 12:48:47 -0700 | [diff] [blame] | 16 | #include "glsl/GrGLSLVertexGeoBuilder.h" |
Chris Dalton | 1a325d2 | 2017-07-14 15:17:41 -0600 | [diff] [blame] | 17 | |
Chris Dalton | df04ce2 | 2018-03-07 17:28:07 -0700 | [diff] [blame] | 18 | void GrCCCoverageProcessor::getGLSLProcessorKey(const GrShaderCaps&, |
| 19 | GrProcessorKeyBuilder* b) const { |
Chris Dalton | fe462ef | 2018-03-08 15:54:01 +0000 | [diff] [blame^] | 20 | int key = (int)fRenderPass << 2; |
Chris Dalton | df04ce2 | 2018-03-07 17:28:07 -0700 | [diff] [blame] | 21 | if (WindMethod::kInstanceData == fWindMethod) { |
| 22 | key |= 2; |
| 23 | } |
| 24 | if (Impl::kVertexShader == fImpl) { |
| 25 | key |= 1; |
| 26 | } |
| 27 | #ifdef SK_DEBUG |
| 28 | uint32_t bloatBits; |
| 29 | memcpy(&bloatBits, &fDebugBloat, 4); |
| 30 | b->add32(bloatBits); |
| 31 | #endif |
| 32 | b->add32(key); |
| 33 | } |
| 34 | |
| 35 | GrGLSLPrimitiveProcessor* GrCCCoverageProcessor::createGLSLInstance(const GrShaderCaps&) const { |
| 36 | std::unique_ptr<Shader> shader; |
| 37 | switch (fRenderPass) { |
| 38 | case RenderPass::kTriangles: |
Chris Dalton | fe462ef | 2018-03-08 15:54:01 +0000 | [diff] [blame^] | 39 | shader = skstd::make_unique<GrCCTriangleShader>(); |
| 40 | break; |
| 41 | case RenderPass::kTriangleCorners: |
| 42 | shader = skstd::make_unique<GrCCTriangleCornerShader>(); |
Chris Dalton | df04ce2 | 2018-03-07 17:28:07 -0700 | [diff] [blame] | 43 | break; |
| 44 | case RenderPass::kQuadratics: |
| 45 | shader = skstd::make_unique<GrCCQuadraticShader>(); |
| 46 | break; |
| 47 | case RenderPass::kCubics: |
| 48 | shader = skstd::make_unique<GrCCCubicShader>(); |
| 49 | break; |
| 50 | } |
| 51 | return Impl::kGeometryShader == fImpl ? this->createGSImpl(std::move(shader)) |
| 52 | : this->createVSImpl(std::move(shader)); |
| 53 | } |
| 54 | |
Chris Dalton | 383a2ef | 2018-01-08 17:21:41 -0500 | [diff] [blame] | 55 | void GrCCCoverageProcessor::Shader::emitFragmentCode(const GrCCCoverageProcessor& proc, |
Chris Dalton | 6028361 | 2018-02-14 13:38:14 -0700 | [diff] [blame] | 56 | GrGLSLFPFragmentBuilder* f, |
Chris Dalton | 383a2ef | 2018-01-08 17:21:41 -0500 | [diff] [blame] | 57 | const char* skOutputColor, |
| 58 | const char* skOutputCoverage) const { |
Chris Dalton | fe462ef | 2018-03-08 15:54:01 +0000 | [diff] [blame^] | 59 | f->codeAppendf("half coverage = 0;"); |
Chris Dalton | df04ce2 | 2018-03-07 17:28:07 -0700 | [diff] [blame] | 60 | this->onEmitFragmentCode(proc, f, "coverage"); |
Chris Dalton | f510e26 | 2018-01-30 16:42:37 -0700 | [diff] [blame] | 61 | f->codeAppendf("%s.a = coverage;", skOutputColor); |
Chris Dalton | 6a3dbee | 2017-10-16 10:44:41 -0600 | [diff] [blame] | 62 | f->codeAppendf("%s = half4(1);", skOutputCoverage); |
Chris Dalton | 1a325d2 | 2017-07-14 15:17:41 -0600 | [diff] [blame] | 63 | #ifdef SK_DEBUG |
Chris Dalton | a640c49 | 2017-09-11 22:04:03 -0700 | [diff] [blame] | 64 | if (proc.debugVisualizationsEnabled()) { |
Chris Dalton | df04ce2 | 2018-03-07 17:28:07 -0700 | [diff] [blame] | 65 | f->codeAppendf("%s = half4(-%s.a, %s.a, 0, abs(%s.a));", |
| 66 | skOutputColor, skOutputColor, skOutputColor, skOutputColor); |
Eric Boren | d6365e5 | 2017-10-16 12:31:14 +0000 | [diff] [blame] | 67 | } |
| 68 | #endif |
Eric Boren | d6365e5 | 2017-10-16 12:31:14 +0000 | [diff] [blame] | 69 | } |
| 70 | |
Chris Dalton | 0a79381 | 2018-03-07 11:18:30 -0700 | [diff] [blame] | 71 | void GrCCCoverageProcessor::Shader::CalcEdgeCoverageAtBloatVertex(GrGLSLVertexGeoBuilder* s, |
| 72 | const char* leftPt, |
| 73 | const char* rightPt, |
| 74 | const char* rasterVertexDir, |
| 75 | const char* outputCoverage) { |
| 76 | // Here we find an edge's coverage at one corner of a conservative raster bloat box whose center |
| 77 | // falls on the edge in question. (A bloat box is axis-aligned and the size of one pixel.) We |
| 78 | // always set up coverage so it is -1 at the outermost corner, 0 at the innermost, and -.5 at |
| 79 | // the center. Interpolated, these coverage values convert jagged conservative raster edges into |
| 80 | // smooth antialiased edges. |
| 81 | // |
| 82 | // d1 == (P + sign(n) * bloat) dot n (Distance at the bloat box vertex whose |
| 83 | // == P dot n + (abs(n.x) + abs(n.y)) * bloatSize coverage=-1, where the bloat box is |
| 84 | // centered on P.) |
| 85 | // |
| 86 | // d0 == (P - sign(n) * bloat) dot n (Distance at the bloat box vertex whose |
| 87 | // == P dot n - (abs(n.x) + abs(n.y)) * bloatSize coverage=0, where the bloat box is |
| 88 | // centered on P.) |
| 89 | // |
| 90 | // d == (P + rasterVertexDir * bloatSize) dot n (Distance at the bloat box vertex whose |
| 91 | // == P dot n + (rasterVertexDir dot n) * bloatSize coverage we wish to calculate.) |
| 92 | // |
| 93 | // coverage == -(d - d0) / (d1 - d0) (coverage=-1 at d=d1; coverage=0 at d=d0) |
| 94 | // |
| 95 | // == (rasterVertexDir dot n) / (abs(n.x) + abs(n.y)) * -.5 - .5 |
| 96 | // |
| 97 | s->codeAppendf("float2 n = float2(%s.y - %s.y, %s.x - %s.x);", |
| 98 | rightPt, leftPt, leftPt, rightPt); |
| 99 | s->codeAppend ("float nwidth = abs(n.x) + abs(n.y);"); |
| 100 | s->codeAppendf("float t = dot(%s, n);", rasterVertexDir); |
| 101 | // The below conditional guarantees we get exactly 1 on the divide when nwidth=t (in case the |
| 102 | // GPU divides by multiplying by the reciprocal?) It also guards against NaN when nwidth=0. |
| 103 | s->codeAppendf("%s = (abs(t) != nwidth ? t / nwidth : sign(t)) * -.5 - .5;", outputCoverage); |
| 104 | } |