Ethan Nicholas | 420f156 | 2017-07-14 13:11:38 -0400 | [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 | |
Ethan Nicholas | aae47c8 | 2017-11-10 15:34:03 -0500 | [diff] [blame] | 8 | layout(key) in GrClipEdgeType edgeType; |
Chris Dalton | 47c8ed3 | 2017-11-15 18:27:09 -0700 | [diff] [blame] | 9 | in float2 center; |
| 10 | in float2 radii; |
Ethan Nicholas | 420f156 | 2017-07-14 13:11:38 -0400 | [diff] [blame] | 11 | |
Chris Dalton | 47c8ed3 | 2017-11-15 18:27:09 -0700 | [diff] [blame] | 12 | float2 prevCenter; |
| 13 | float2 prevRadii = float2(-1); |
Ethan Nicholas | 420f156 | 2017-07-14 13:11:38 -0400 | [diff] [blame] | 14 | // The ellipse uniform is (center.x, center.y, 1 / rx^2, 1 / ry^2) |
Chris Dalton | 47c8ed3 | 2017-11-15 18:27:09 -0700 | [diff] [blame] | 15 | // The last two terms can underflow when float != fp32, so we also provide a workaround. |
Ethan Nicholas | 8aa4569 | 2017-09-20 11:24:15 -0400 | [diff] [blame] | 16 | uniform float4 ellipse; |
Ethan Nicholas | 420f156 | 2017-07-14 13:11:38 -0400 | [diff] [blame] | 17 | |
Chris Dalton | 47c8ed3 | 2017-11-15 18:27:09 -0700 | [diff] [blame] | 18 | bool useScale = !sk_Caps.floatIs32Bits; |
| 19 | layout(when=useScale) uniform float2 scale; |
Ethan Nicholas | 420f156 | 2017-07-14 13:11:38 -0400 | [diff] [blame] | 20 | |
| 21 | @optimizationFlags { kCompatibleWithCoverageAsAlpha_OptimizationFlag } |
| 22 | |
| 23 | @setData(pdman) { |
| 24 | if (radii != prevRadii || center != prevCenter) { |
| 25 | float invRXSqd; |
| 26 | float invRYSqd; |
| 27 | // If we're using a scale factor to work around precision issues, choose the larger radius |
| 28 | // as the scale factor. The inv radii need to be pre-adjusted by the scale factor. |
| 29 | if (scale.isValid()) { |
| 30 | if (radii.fX > radii.fY) { |
| 31 | invRXSqd = 1.f; |
| 32 | invRYSqd = (radii.fX * radii.fX) / |
| 33 | (radii.fY * radii.fY); |
| 34 | pdman.set2f(scale, radii.fX, 1.f / radii.fX); |
| 35 | } else { |
| 36 | invRXSqd = (radii.fY * radii.fY) / |
| 37 | (radii.fX * radii.fX); |
| 38 | invRYSqd = 1.f; |
| 39 | pdman.set2f(scale, radii.fY, 1.f / radii.fY); |
| 40 | } |
| 41 | } else { |
| 42 | invRXSqd = 1.f / (radii.fX * radii.fX); |
| 43 | invRYSqd = 1.f / (radii.fY * radii.fY); |
| 44 | } |
| 45 | pdman.set4f(ellipse, center.fX, center.fY, invRXSqd, invRYSqd); |
| 46 | prevCenter = center; |
| 47 | prevRadii = radii; |
| 48 | } |
| 49 | } |
| 50 | |
| 51 | void main() { |
| 52 | // d is the offset to the ellipse center |
Chris Dalton | 47c8ed3 | 2017-11-15 18:27:09 -0700 | [diff] [blame] | 53 | float2 d = sk_FragCoord.xy - ellipse.xy; |
Ethan Nicholas | 420f156 | 2017-07-14 13:11:38 -0400 | [diff] [blame] | 54 | // If we're on a device with a "real" mediump then we'll do the distance computation in a space |
| 55 | // that is normalized by the larger radius. The scale uniform will be scale, 1/scale. The |
| 56 | // inverse squared radii uniform values are already in this normalized space. The center is |
| 57 | // not. |
| 58 | @if (useScale) { |
| 59 | d *= scale.y; |
| 60 | } |
Chris Dalton | 47c8ed3 | 2017-11-15 18:27:09 -0700 | [diff] [blame] | 61 | float2 Z = d * ellipse.zw; |
Ethan Nicholas | 420f156 | 2017-07-14 13:11:38 -0400 | [diff] [blame] | 62 | // implicit is the evaluation of (x/rx)^2 + (y/ry)^2 - 1. |
Chris Dalton | 47c8ed3 | 2017-11-15 18:27:09 -0700 | [diff] [blame] | 63 | float implicit = dot(Z, d) - 1; |
Ethan Nicholas | 420f156 | 2017-07-14 13:11:38 -0400 | [diff] [blame] | 64 | // grad_dot is the squared length of the gradient of the implicit. |
Chris Dalton | 47c8ed3 | 2017-11-15 18:27:09 -0700 | [diff] [blame] | 65 | float grad_dot = 4 * dot(Z, Z); |
Ethan Nicholas | 420f156 | 2017-07-14 13:11:38 -0400 | [diff] [blame] | 66 | // Avoid calling inversesqrt on zero. |
| 67 | grad_dot = max(grad_dot, 1e-4); |
Chris Dalton | 47c8ed3 | 2017-11-15 18:27:09 -0700 | [diff] [blame] | 68 | float approx_dist = implicit * inversesqrt(grad_dot); |
Ethan Nicholas | 420f156 | 2017-07-14 13:11:38 -0400 | [diff] [blame] | 69 | @if (useScale) { |
| 70 | approx_dist *= scale.x; |
| 71 | } |
| 72 | |
Ethan Nicholas | f7b8820 | 2017-09-18 14:10:39 -0400 | [diff] [blame] | 73 | half alpha; |
Ethan Nicholas | 420f156 | 2017-07-14 13:11:38 -0400 | [diff] [blame] | 74 | @switch (edgeType) { |
Ethan Nicholas | aae47c8 | 2017-11-10 15:34:03 -0500 | [diff] [blame] | 75 | case GrClipEdgeType::kFillBW: |
Ethan Nicholas | 420f156 | 2017-07-14 13:11:38 -0400 | [diff] [blame] | 76 | alpha = approx_dist > 0.0 ? 0.0 : 1.0; |
| 77 | break; |
Ethan Nicholas | aae47c8 | 2017-11-10 15:34:03 -0500 | [diff] [blame] | 78 | case GrClipEdgeType::kFillAA: |
Ethan Nicholas | 420f156 | 2017-07-14 13:11:38 -0400 | [diff] [blame] | 79 | alpha = clamp(0.5 - approx_dist, 0.0, 1.0); |
| 80 | break; |
Ethan Nicholas | aae47c8 | 2017-11-10 15:34:03 -0500 | [diff] [blame] | 81 | case GrClipEdgeType::kInverseFillBW: |
Ethan Nicholas | 420f156 | 2017-07-14 13:11:38 -0400 | [diff] [blame] | 82 | alpha = approx_dist > 0.0 ? 1.0 : 0.0; |
| 83 | break; |
Ethan Nicholas | aae47c8 | 2017-11-10 15:34:03 -0500 | [diff] [blame] | 84 | case GrClipEdgeType::kInverseFillAA: |
Ethan Nicholas | 420f156 | 2017-07-14 13:11:38 -0400 | [diff] [blame] | 85 | alpha = clamp(0.5 + approx_dist, 0.0, 1.0); |
| 86 | break; |
| 87 | default: |
| 88 | // hairline not supported |
| 89 | discard; |
| 90 | } |
| 91 | sk_OutColor = sk_InColor * alpha; |
| 92 | } |
| 93 | |
| 94 | @test(testData) { |
| 95 | SkPoint center; |
| 96 | center.fX = testData->fRandom->nextRangeScalar(0.f, 1000.f); |
| 97 | center.fY = testData->fRandom->nextRangeScalar(0.f, 1000.f); |
| 98 | SkScalar rx = testData->fRandom->nextRangeF(0.f, 1000.f); |
| 99 | SkScalar ry = testData->fRandom->nextRangeF(0.f, 1000.f); |
Ethan Nicholas | 0f3c732 | 2017-11-09 14:51:17 -0500 | [diff] [blame] | 100 | GrClipEdgeType et; |
Ethan Nicholas | 420f156 | 2017-07-14 13:11:38 -0400 | [diff] [blame] | 101 | do { |
Ethan Nicholas | 1706f84 | 2017-11-10 11:58:19 -0500 | [diff] [blame] | 102 | et = (GrClipEdgeType) testData->fRandom->nextULessThan(kGrClipEdgeTypeCnt); |
| 103 | } while (GrClipEdgeType::kHairlineAA == et); |
Ethan Nicholas | aae47c8 | 2017-11-10 15:34:03 -0500 | [diff] [blame] | 104 | return GrEllipseEffect::Make(et, center, SkPoint::Make(rx, ry)); |
Chris Dalton | 47c8ed3 | 2017-11-15 18:27:09 -0700 | [diff] [blame] | 105 | } |