Optimize the XCAA fragment shaders a bit

This commit is contained in:
Patrick Walton 2017-11-10 19:25:47 -08:00
parent c9948a844d
commit fc2ac12fc0
2 changed files with 9 additions and 17 deletions

View File

@ -19,10 +19,10 @@ varying float vWinding;
// x = p0x + t^2 * (p0x - 2*p1x + p2x) + t*(2*p1x - 2*p0x) // x = p0x + t^2 * (p0x - 2*p1x + p2x) + t*(2*p1x - 2*p0x)
// //
// We use the Citardauq Formula to avoid floating point precision issues. // We use the Citardauq Formula to avoid floating point precision issues.
float solveCurveT(float p0x, float p1x, float p2x, float x) { vec2 solveCurveT(float p0x, float p1x, float p2x, vec2 x) {
float a = p0x - 2.0 * p1x + p2x; float a = p0x - 2.0 * p1x + p2x;
float b = 2.0 * p1x - 2.0 * p0x; float b = 2.0 * p1x - 2.0 * p0x;
float c = p0x - x; vec2 c = p0x - x;
return 2.0 * c / (-b - sqrt(b * b - 4.0 * a * c)); return 2.0 * c / (-b - sqrt(b * b - 4.0 * a * c));
} }
@ -33,17 +33,18 @@ void main() {
vec2 cp = vControlPoint; vec2 cp = vControlPoint;
// Compute pixel extents. // Compute pixel extents.
vec4 pixelExtents = center.xxyy + vec4(-0.5, 0.5, -0.5, 0.5); vec2 pixelExtents = center.xx + vec2(-0.5, 0.5);
// Clip the curve to the left and right edges to create a line. // Clip the curve to the left and right edges to create a line.
// //
// TODO(pcwalton): Consider clipping to the bottom and top edges properly too. (I kind of doubt // TODO(pcwalton): Consider clipping to the bottom and top edges properly too. (I kind of doubt
// it's worth it to do this, though, given that the maximum error doing it this way will always // it's worth it to do this, though, given that the maximum error doing it this way will always
// be less than a pixel, and it saves a lot of time.) // be less than a pixel, and it saves a lot of time.)
// vec2 t = solveCurveT(p0.x, cp.x, p1.x, pixelExtents);
// FIXME(pcwalton): Factor out shared terms to avoid computing them multiple times. if (p0.x > pixelExtents.x)
vec2 t = vec2(p0.x < pixelExtents.x ? solveCurveT(p0.x, cp.x, p1.x, pixelExtents.x) : 0.0, t.x = 0.0;
p1.x > pixelExtents.y ? solveCurveT(p0.x, cp.x, p1.x, pixelExtents.y) : 1.0); if (p1.x < pixelExtents.y)
t.y = 1.0;
vec2 clippedP0 = mix(mix(p0, cp, t.x), mix(cp, p1, t.x), t.x); vec2 clippedP0 = mix(mix(p0, cp, t.x), mix(cp, p1, t.x), t.x);
vec2 clippedP1 = mix(mix(p0, cp, t.y), mix(cp, p1, t.y), t.y); vec2 clippedP1 = mix(mix(p0, cp, t.y), mix(cp, p1, t.y), t.y);

View File

@ -18,15 +18,6 @@ void main() {
vec2 center = gl_FragCoord.xy; vec2 center = gl_FragCoord.xy;
vec2 p0 = vEndpoints.xy, p1 = vEndpoints.zw; vec2 p0 = vEndpoints.xy, p1 = vEndpoints.zw;
// Set up Liang-Barsky clipping.
vec4 pixelExtents = center.xxyy + vec4(-0.5, 0.5, -0.5, 0.5);
vec2 dp = p1 - p0;
vec4 q = pixelExtents - p0.xxyy;
// Use Liang-Barsky to clip to the left and right sides of this pixel.
vec2 t = clamp(q.xy / dp.xx, 0.0, 1.0);
vec2 spanP0 = p0 + dp * t.x, spanP1 = p0 + dp * t.y;
// Compute area. // Compute area.
gl_FragColor = vec4(computeCoverage(p0, dp, center, vWinding)); gl_FragColor = vec4(computeCoverage(p0, p1 - p0, center, vWinding));
} }