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" |
| 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 | 383a2ef | 2018-01-08 17:21:41 -0500 | [diff] [blame] | 18 | void GrCCCoverageProcessor::Shader::emitFragmentCode(const GrCCCoverageProcessor& proc, |
Chris Dalton | 6028361 | 2018-02-14 13:38:14 -0700 | [diff] [blame] | 19 | GrGLSLFPFragmentBuilder* f, |
Chris Dalton | 383a2ef | 2018-01-08 17:21:41 -0500 | [diff] [blame] | 20 | const char* skOutputColor, |
| 21 | const char* skOutputCoverage) const { |
Chris Dalton | 6a3dbee | 2017-10-16 10:44:41 -0600 | [diff] [blame] | 22 | f->codeAppendf("half coverage = 0;"); |
| 23 | this->onEmitFragmentCode(f, "coverage"); |
Chris Dalton | f510e26 | 2018-01-30 16:42:37 -0700 | [diff] [blame] | 24 | f->codeAppendf("%s.a = coverage;", skOutputColor); |
Chris Dalton | 6a3dbee | 2017-10-16 10:44:41 -0600 | [diff] [blame] | 25 | f->codeAppendf("%s = half4(1);", skOutputCoverage); |
Chris Dalton | 1a325d2 | 2017-07-14 15:17:41 -0600 | [diff] [blame] | 26 | #ifdef SK_DEBUG |
Chris Dalton | a640c49 | 2017-09-11 22:04:03 -0700 | [diff] [blame] | 27 | if (proc.debugVisualizationsEnabled()) { |
Chris Dalton | 6a3dbee | 2017-10-16 10:44:41 -0600 | [diff] [blame] | 28 | f->codeAppendf("%s = half4(-%s.a, %s.a, 0, 1);", |
| 29 | skOutputColor, skOutputColor, skOutputColor); |
Eric Boren | d6365e5 | 2017-10-16 12:31:14 +0000 | [diff] [blame] | 30 | } |
| 31 | #endif |
Eric Boren | d6365e5 | 2017-10-16 12:31:14 +0000 | [diff] [blame] | 32 | } |
| 33 | |
Chris Dalton | 383a2ef | 2018-01-08 17:21:41 -0500 | [diff] [blame] | 34 | void GrCCCoverageProcessor::Shader::EmitEdgeDistanceEquation(GrGLSLVertexGeoBuilder* s, |
| 35 | const char* leftPt, |
| 36 | const char* rightPt, |
| 37 | const char* outputDistanceEquation) { |
Chris Dalton | 6a3dbee | 2017-10-16 10:44:41 -0600 | [diff] [blame] | 38 | s->codeAppendf("float2 n = float2(%s.y - %s.y, %s.x - %s.x);", |
Eric Boren | d6365e5 | 2017-10-16 12:31:14 +0000 | [diff] [blame] | 39 | rightPt, leftPt, leftPt, rightPt); |
Chris Dalton | cc0ab7e | 2017-10-24 14:16:52 -0600 | [diff] [blame] | 40 | s->codeAppend ("float nwidth = (abs(n.x) + abs(n.y)) * (bloat * 2);"); |
| 41 | // When nwidth=0, wind must also be 0 (and coverage * wind = 0). So it doesn't matter what we |
| 42 | // come up with here as long as it isn't NaN or Inf. |
| 43 | s->codeAppend ("n /= (0 != nwidth) ? nwidth : 1;"); |
| 44 | s->codeAppendf("%s = float3(-n, dot(n, %s) - .5);", outputDistanceEquation, leftPt); |
Eric Boren | d6365e5 | 2017-10-16 12:31:14 +0000 | [diff] [blame] | 45 | } |
| 46 | |
Chris Dalton | 0a79381 | 2018-03-07 11:18:30 -0700 | [diff] [blame^] | 47 | void GrCCCoverageProcessor::Shader::CalcEdgeCoverageAtBloatVertex(GrGLSLVertexGeoBuilder* s, |
| 48 | const char* leftPt, |
| 49 | const char* rightPt, |
| 50 | const char* rasterVertexDir, |
| 51 | const char* outputCoverage) { |
| 52 | // Here we find an edge's coverage at one corner of a conservative raster bloat box whose center |
| 53 | // falls on the edge in question. (A bloat box is axis-aligned and the size of one pixel.) We |
| 54 | // always set up coverage so it is -1 at the outermost corner, 0 at the innermost, and -.5 at |
| 55 | // the center. Interpolated, these coverage values convert jagged conservative raster edges into |
| 56 | // smooth antialiased edges. |
| 57 | // |
| 58 | // d1 == (P + sign(n) * bloat) dot n (Distance at the bloat box vertex whose |
| 59 | // == P dot n + (abs(n.x) + abs(n.y)) * bloatSize coverage=-1, where the bloat box is |
| 60 | // centered on P.) |
| 61 | // |
| 62 | // d0 == (P - sign(n) * bloat) dot n (Distance at the bloat box vertex whose |
| 63 | // == P dot n - (abs(n.x) + abs(n.y)) * bloatSize coverage=0, where the bloat box is |
| 64 | // centered on P.) |
| 65 | // |
| 66 | // d == (P + rasterVertexDir * bloatSize) dot n (Distance at the bloat box vertex whose |
| 67 | // == P dot n + (rasterVertexDir dot n) * bloatSize coverage we wish to calculate.) |
| 68 | // |
| 69 | // coverage == -(d - d0) / (d1 - d0) (coverage=-1 at d=d1; coverage=0 at d=d0) |
| 70 | // |
| 71 | // == (rasterVertexDir dot n) / (abs(n.x) + abs(n.y)) * -.5 - .5 |
| 72 | // |
| 73 | s->codeAppendf("float2 n = float2(%s.y - %s.y, %s.x - %s.x);", |
| 74 | rightPt, leftPt, leftPt, rightPt); |
| 75 | s->codeAppend ("float nwidth = abs(n.x) + abs(n.y);"); |
| 76 | s->codeAppendf("float t = dot(%s, n);", rasterVertexDir); |
| 77 | // The below conditional guarantees we get exactly 1 on the divide when nwidth=t (in case the |
| 78 | // GPU divides by multiplying by the reciprocal?) It also guards against NaN when nwidth=0. |
| 79 | s->codeAppendf("%s = (abs(t) != nwidth ? t / nwidth : sign(t)) * -.5 - .5;", outputCoverage); |
| 80 | } |
| 81 | |
Chris Dalton | 6028361 | 2018-02-14 13:38:14 -0700 | [diff] [blame] | 82 | int GrCCCoverageProcessor::Shader::DefineSoftSampleLocations(GrGLSLFPFragmentBuilder* f, |
Chris Dalton | 383a2ef | 2018-01-08 17:21:41 -0500 | [diff] [blame] | 83 | const char* samplesName) { |
Chris Dalton | 1a325d2 | 2017-07-14 15:17:41 -0600 | [diff] [blame] | 84 | // Standard DX11 sample locations. |
| 85 | #if defined(SK_BUILD_FOR_ANDROID) || defined(SK_BUILD_FOR_IOS) |
Ethan Nicholas | 8aa4569 | 2017-09-20 11:24:15 -0400 | [diff] [blame] | 86 | f->defineConstant("float2[8]", samplesName, "float2[8](" |
| 87 | "float2(+1, -3)/16, float2(-1, +3)/16, float2(+5, +1)/16, float2(-3, -5)/16, " |
| 88 | "float2(-5, +5)/16, float2(-7, -1)/16, float2(+3, +7)/16, float2(+7, -7)/16." |
Chris Dalton | 1a325d2 | 2017-07-14 15:17:41 -0600 | [diff] [blame] | 89 | ")"); |
| 90 | return 8; |
| 91 | #else |
Ethan Nicholas | 8aa4569 | 2017-09-20 11:24:15 -0400 | [diff] [blame] | 92 | f->defineConstant("float2[16]", samplesName, "float2[16](" |
| 93 | "float2(+1, +1)/16, float2(-1, -3)/16, float2(-3, +2)/16, float2(+4, -1)/16, " |
| 94 | "float2(-5, -2)/16, float2(+2, +5)/16, float2(+5, +3)/16, float2(+3, -5)/16, " |
| 95 | "float2(-2, +6)/16, float2( 0, -7)/16, float2(-4, -6)/16, float2(-6, +4)/16, " |
| 96 | "float2(-8, 0)/16, float2(+7, -4)/16, float2(+6, +7)/16, float2(-7, -8)/16." |
Chris Dalton | 1a325d2 | 2017-07-14 15:17:41 -0600 | [diff] [blame] | 97 | ")"); |
| 98 | return 16; |
| 99 | #endif |
| 100 | } |
| 101 | |
Chris Dalton | 383a2ef | 2018-01-08 17:21:41 -0500 | [diff] [blame] | 102 | void GrCCCoverageProcessor::getGLSLProcessorKey(const GrShaderCaps&, |
| 103 | GrProcessorKeyBuilder* b) const { |
Chris Dalton | 84403d7 | 2018-02-13 21:46:17 -0500 | [diff] [blame] | 104 | int key = (int)fRenderPass << 2; |
| 105 | if (WindMethod::kInstanceData == fWindMethod) { |
| 106 | key |= 2; |
| 107 | } |
| 108 | if (Impl::kVertexShader == fImpl) { |
Chris Dalton | 90e8fb1 | 2017-12-22 02:24:53 -0700 | [diff] [blame] | 109 | key |= 1; |
| 110 | } |
| 111 | #ifdef SK_DEBUG |
| 112 | uint32_t bloatBits; |
| 113 | memcpy(&bloatBits, &fDebugBloat, 4); |
| 114 | b->add32(bloatBits); |
| 115 | #endif |
| 116 | b->add32(key); |
Chris Dalton | 6a3dbee | 2017-10-16 10:44:41 -0600 | [diff] [blame] | 117 | } |
| 118 | |
Chris Dalton | 383a2ef | 2018-01-08 17:21:41 -0500 | [diff] [blame] | 119 | GrGLSLPrimitiveProcessor* GrCCCoverageProcessor::createGLSLInstance(const GrShaderCaps&) const { |
Chris Dalton | 6a3dbee | 2017-10-16 10:44:41 -0600 | [diff] [blame] | 120 | std::unique_ptr<Shader> shader; |
| 121 | switch (fRenderPass) { |
Chris Dalton | 6a3dbee | 2017-10-16 10:44:41 -0600 | [diff] [blame] | 122 | case RenderPass::kTriangleHulls: |
Chris Dalton | 6a3dbee | 2017-10-16 10:44:41 -0600 | [diff] [blame] | 123 | case RenderPass::kTriangleEdges: |
Chris Dalton | 383a2ef | 2018-01-08 17:21:41 -0500 | [diff] [blame] | 124 | shader = skstd::make_unique<GrCCTriangleShader>(); |
Chris Dalton | 6a3dbee | 2017-10-16 10:44:41 -0600 | [diff] [blame] | 125 | break; |
| 126 | case RenderPass::kTriangleCorners: |
Chris Dalton | 383a2ef | 2018-01-08 17:21:41 -0500 | [diff] [blame] | 127 | shader = skstd::make_unique<GrCCTriangleCornerShader>(); |
Chris Dalton | 6a3dbee | 2017-10-16 10:44:41 -0600 | [diff] [blame] | 128 | break; |
| 129 | case RenderPass::kQuadraticHulls: |
Chris Dalton | 383a2ef | 2018-01-08 17:21:41 -0500 | [diff] [blame] | 130 | shader = skstd::make_unique<GrCCQuadraticHullShader>(); |
Chris Dalton | 6a3dbee | 2017-10-16 10:44:41 -0600 | [diff] [blame] | 131 | break; |
| 132 | case RenderPass::kQuadraticCorners: |
Chris Dalton | 383a2ef | 2018-01-08 17:21:41 -0500 | [diff] [blame] | 133 | shader = skstd::make_unique<GrCCQuadraticCornerShader>(); |
Chris Dalton | 6a3dbee | 2017-10-16 10:44:41 -0600 | [diff] [blame] | 134 | break; |
Chris Dalton | be4ffab | 2017-12-08 10:59:58 -0700 | [diff] [blame] | 135 | case RenderPass::kCubicHulls: |
Chris Dalton | 383a2ef | 2018-01-08 17:21:41 -0500 | [diff] [blame] | 136 | shader = skstd::make_unique<GrCCCubicHullShader>(); |
Chris Dalton | 6a3dbee | 2017-10-16 10:44:41 -0600 | [diff] [blame] | 137 | break; |
Chris Dalton | be4ffab | 2017-12-08 10:59:58 -0700 | [diff] [blame] | 138 | case RenderPass::kCubicCorners: |
Chris Dalton | 383a2ef | 2018-01-08 17:21:41 -0500 | [diff] [blame] | 139 | shader = skstd::make_unique<GrCCCubicCornerShader>(); |
Chris Dalton | 6a3dbee | 2017-10-16 10:44:41 -0600 | [diff] [blame] | 140 | break; |
| 141 | } |
Chris Dalton | 90e8fb1 | 2017-12-22 02:24:53 -0700 | [diff] [blame] | 142 | return Impl::kGeometryShader == fImpl ? this->createGSImpl(std::move(shader)) |
| 143 | : this->createVSImpl(std::move(shader)); |
Chris Dalton | 6a3dbee | 2017-10-16 10:44:41 -0600 | [diff] [blame] | 144 | } |