Implement the line antialiasing shader, untested as of yet

This commit is contained in:
Patrick Walton 2017-08-18 11:44:17 -07:00
parent ccf5000e1c
commit a9eeec39fe
5 changed files with 147 additions and 6 deletions

View File

@ -48,14 +48,18 @@ const SHADER_URLS: ShaderMap<ShaderProgramURLs> = {
vertex: "/glsl/gles2/ecaa-edge-detect.vs.glsl",
fragment: "/glsl/gles2/ecaa-edge-detect.fs.glsl",
},
ecaaResolve: {
vertex: "/glsl/gles2/ecaa-resolve.vs.glsl",
fragment: "/glsl/gles2/ecaa-resolve.fs.glsl",
},
ecaaCover: {
vertex: "/glsl/gles2/ecaa-cover.vs.glsl",
fragment: "/glsl/gles2/ecaa-cover.fs.glsl",
},
ecaaLine: {
vertex: "/glsl/gles2/ecaa-line.vs.glsl",
fragment: "/glsl/gles2/ecaa-line.fs.glsl",
},
ecaaResolve: {
vertex: "/glsl/gles2/ecaa-resolve.vs.glsl",
fragment: "/glsl/gles2/ecaa-resolve.fs.glsl",
},
};
interface UnlinkedShaderProgram {
@ -85,8 +89,9 @@ interface ShaderMap<T> {
directCurve: T;
directInterior: T;
ecaaEdgeDetect: T;
ecaaResolve: T;
ecaaCover: T;
ecaaLine: T;
ecaaResolve: T;
}
interface UniformMap {

View File

@ -9,6 +9,8 @@
#define MAX_PATHS 65536
#define EPSILON 0.001
precision highp float;
// https://stackoverflow.com/a/36078859
@ -65,3 +67,11 @@ vec2 packPathID(int pathID) {
int unpackPathID(vec2 packedPathID) {
return unpackUInt16(packedPathID);
}
bool xor(bool a, bool b) {
return (a && !b) || (!a && b);
}
float det2(vec2 a, vec2 b) {
return a.x * b.y - b.x * a.y;
}

View File

@ -58,7 +58,10 @@ void main() {
int pathID = fetchUInt16Data(uBVertexPathID, pointIndices.x, uBVertexPathIDDimensions);
vec2 position = mix(roundedExtents.xy, roundedExtents.zw, (aQuadPosition + 1.0) / 2.0);
// FIXME(pcwalton): Use a separate VBO for this.
vec2 quadPosition = (aQuadPosition + 1.0) * 0.5;
vec2 position = mix(roundedExtents.xy, roundedExtents.zw, quadPosition);
position = convertScreenToClipSpace(position, uFramebufferSize);
float depth = convertPathIndexToDepthValue(pathID);
gl_Position = vec4(position, depth, 1.0);

View File

@ -0,0 +1,67 @@
// pathfinder/shaders/gles2/ecaa-line.vs.glsl
//
// Copyright (c) 2017 Mozilla Foundation
precision highp float;
uniform bool uLowerPart;
varying vec4 vEndpoints;
void main() {
// Unpack.
vec2 center = gl_FragCoord.xy;
vec2 p0 = vEndpoints.xy, p1 = vEndpoints.zw;
bool slopeNegative = p0.y > p1.y;
// Set up Liang-Barsky clipping.
vec4 pixelExtents = center.xxyy + vec4(-0.5, 0.5, -0.5, 0.5);
vec4 p = (p1 - p0).xxyy, q = pixelExtents - p0.xxyy;
// Use Liang-Barsky to clip to the left and right sides of this pixel.
vec2 t = clamp(q.xy / p.xy, 0.0, 1.0);
vec2 spanP0 = p0 + p.yw * t.x, spanP1 = p0 + p.yw * t.y;
// ...and to the bottom and top.
if (p.z != 0.0) {
vec2 tVertical = q.zw / p.zw;
if (slopeNegative)
tVertical.xy = tVertical.yx; // FIXME(pcwalton): Can this be removed?
t = vec2(max(t.x, tVertical.x), min(t.y, tVertical.y));
}
// If the line doesn't pass through this pixel, detect that and bail.
if (t.x >= t.y) {
bool fill = uLowerPart ? spanP0.y < pixelExtents.z : spanP0.y > pixelExtents.w;
gl_FragColor = vec4(fill ? spanP1.x - spanP0.x : 0.0);
return;
}
// Calculate A2.x.
float a2x;
if (xor(uLowerPart, slopeNegative)) {
a2x = spanP0.x;
t.xy = t.yx;
} else {
a2x = spanP1.x;
}
// Calculate A3.y.
float a3y = uLowerPart ? pixelExtents.w : pixelExtents.z;
// Calculate A0-A5.
vec2 a0 = p0 + p.yw * t.x;
vec2 a1 = p0 + p.yw * t.y;
vec2 a2 = vec2(a2x, a1.y);
vec2 a3 = vec2(a2x, a3y);
vec2 a4 = vec2(a0.x, a3y);
// Calculate area with the shoelace formula.
float area = 0.5 * (det2(a0, a1) + det2(a1, a2) + det2(a2, a3) + det2(a3, a4) + det2(a4, a0));
if (!slopeNegative)
area = -area;
// Done!
gl_FragColor = vec4(area);
}

View File

@ -0,0 +1,56 @@
// pathfinder/shaders/gles2/ecaa-line.vs.glsl
//
// Copyright (c) 2017 Mozilla Foundation
precision highp float;
uniform mat4 uTransform;
uniform ivec2 uFramebufferSize;
uniform ivec2 uBVertexPositionDimensions;
uniform ivec2 uBVertexPathIDDimensions;
uniform sampler2D uBVertexPosition;
uniform sampler2D uBVertexPathID;
uniform bool uLowerPart;
attribute vec2 aQuadPosition;
attribute vec4 aLineIndices;
varying vec4 vEndpoints;
void main() {
// Fetch B-vertex positions.
ivec2 pointIndices = ivec2(unpackUInt32Attribute(aLineIndices.xy),
unpackUInt32Attribute(aLineIndices.zw));
vec2 leftPosition = fetchFloat2Data(uBVertexPosition,
pointIndices.x,
uBVertexPositionDimensions);
vec2 rightPosition = fetchFloat2Data(uBVertexPosition,
pointIndices.y,
uBVertexPositionDimensions);
vec2 position;
if (abs(leftPosition.x - rightPosition.x) > EPSILON) {
leftPosition = transformVertexPosition(leftPosition, uTransform);
rightPosition = transformVertexPosition(rightPosition, uTransform);
vec2 verticalExtents = vec2(min(leftPosition.y, rightPosition.y),
max(leftPosition.y, rightPosition.y));
vec4 roundedExtents = vec4(floor(vec2(leftPosition.x, verticalExtents.x)),
ceil(vec2(rightPosition.x, verticalExtents.y)));
// FIXME(pcwalton): Use a separate VBO for this.
vec2 quadPosition = (aQuadPosition + 1.0) * 0.5;
position = mix(roundedExtents.xy, roundedExtents.zw, quadPosition);
position = convertScreenToClipSpace(position, uFramebufferSize);
} else {
position = vec2(0.0);
}
int pathID = fetchUInt16Data(uBVertexPathID, pointIndices.x, uBVertexPathIDDimensions);
float depth = convertPathIndexToDepthValue(pathID);
gl_Position = vec4(position, depth, 1.0);
vEndpoints = vec4(leftPosition, rightPosition);
}