servo / pathfinder

A fast, practical GPU rasterizer for fonts and vector graphics
Apache License 2.0
3.59k stars 201 forks source link

Signed distance field tiles #192

Open pcwalton opened 5 years ago

pcwalton commented 5 years ago

Obviously along with #191 it would be desirable to get better scaling then what bilinear filtering of coverage can provide. Although imprecise, SDFs can often provide better results. Of course, generating the SDF is the problem.

Here's a rough sketch of how I think we can generate suitable SDFs during the tiling phase. Observe that if you are not doing shadows, bevels, or other similar effects all you need is the distance to the edge on edge pixels. Thus we do not have to calculate Voronoi regions and can simply use the quads we normally generate. So, we simply write distance to edge instead of coverage on edge pixels. That leaves the sign to determine. We can simply use a second channel (or, more probably, a second framebuffer) to accumulate winding numbers. Combine the winding number with the distance to edge and we should have a suitable SDF.

A potential interesting improvement is to store angle of the edge in a third channel. This would allow for better antialiasing, by taking both distance and angle into account.

Note that this technique doesn't allow accumulation of information about multiple edges in a single pixel. That's the fundamental reason behind the imprecision of SDF and is inherent to the problem.

LifeIsStrange commented 5 years ago

You probably already know it but Valve did some prior work: https://citeseerx.ist.psu.edu/viewdoc/summary?doi=10.1.1.170.9418

Asbtract from Wikipedia SDF page: "Signed distance functions have also recently been used in a method (advanced by Valve Corporation) to render smooth fonts at large sizes (or alternatively at high DPI) using GPU acceleration.[6] Valve's method computed signed distance fields in raster space in order to avoid the computational complexity of solving the problem in the (continuous) vector space. More recently piece-wise approximation solutions have been proposed (which for example approximate a Bézier with arc splines), but even this way the computation can be too slow for real-time rendering, and it has to be assisted by grid-based discretization techniques to approximate (and cull from the computation) the distance to points that are too far away.[7]"

BTW Facebook recently improved the state of the art with http://openaccess.thecvf.com/content_CVPR_2019/html/Park_DeepSDF_Learning_Continuous_Signed_Distance_Functions_for_Shape_Representation_CVPR_2019_paper.html But is probably not suitable for pathfinder.