| ▲ | SDF vs. MSDF vs. Slug: GPU Text Rendering(alphapixeldev.com) | ||||||||||||||||||||||||||||||||||||||||||||||
| 86 points by ibobev 5 hours ago | 35 comments | |||||||||||||||||||||||||||||||||||||||||||||||
| ▲ | GuB-42 3 hours ago | parent | next [-] | ||||||||||||||||||||||||||||||||||||||||||||||
I once implemented SDF text rendering. To me, the thing I liked the most about this technique it how easy it is to add effects on top. With a few lines of shader code, I had outlines and softening of the edges (antialiasing). I didn't try the MSDF variant, as I didn't mind corners not being sharp when scaled up, so I don't know if these effects break on MSDF. Slug doesn't seem to support any of these, it is just "for a given point, am I in or am I out?", but it doesn't tell you by how much, which is great on really high resolution displays and large sizes, but you would lose the ability to do the kind of effects you can do with SDFs, and have to deal with antialiasing separately. | |||||||||||||||||||||||||||||||||||||||||||||||
| |||||||||||||||||||||||||||||||||||||||||||||||
| ▲ | jdanford 4 hours ago | parent | prev | next [-] | ||||||||||||||||||||||||||||||||||||||||||||||
Man, I am getting incredibly tired of reading LLM-generated writing | |||||||||||||||||||||||||||||||||||||||||||||||
| |||||||||||||||||||||||||||||||||||||||||||||||
| ▲ | Const-me 2 hours ago | parent | prev | next [-] | ||||||||||||||||||||||||||||||||||||||||||||||
“What makes glyphs hard” Another reason is hinting. Traditional text renderers like FreeType are aware of the pixel grid and they adjust the curves slightly, snapping them to that grid. For all methods in the article quite hard to do on GPUs. “Chinese, Japanese, and Korean have tens of thousands of glyphs, and baking all of them at several sizes is a memory disaster” One possible solution is dynamic atlas built on CPU for visible glyphs only. “The distance-field panels notch, where interpolating between stored samples no longer matches the true curve” Can’t it be fixed in the shader, using screen-space derivatives of the SDF? I think in theory, SDF value for pixel center combined with screen-space gradient vector of that number delivers enough data to compute partial coverage for the pixels on the edge. | |||||||||||||||||||||||||||||||||||||||||||||||
| ▲ | mattdesl 4 hours ago | parent | prev | next [-] | ||||||||||||||||||||||||||||||||||||||||||||||
I've also been working on a GPU curve renderer, Windfoil, based on a formulation that Fable 5 originally proposed to me during a directed search [1]. It is similar in some ways to Slug, not always as fast, but uses less shader storage (single band instead of two) and produces higher quality anti-aliasing i.e. closer to a box-filtered ground truth. It may be of interest to some game/graphics devs here... | |||||||||||||||||||||||||||||||||||||||||||||||
| |||||||||||||||||||||||||||||||||||||||||||||||
| ▲ | flohofwoe 4 hours ago | parent | prev | next [-] | ||||||||||||||||||||||||||||||||||||||||||||||
Here's a simple Slug rendering example on top of sokol_gfx.h: via WebGPU backend: https://floooh.github.io/sokol-webgpu/slug-sapp.html via WebGL2 backend: https://floooh.github.io/sokol-html5/slug-sapp.html There's quite a bit of helper code plus stb_truetype.h and stb_ds.h under the hood to parse TTF files and crunch the TTF curve data into the runtime format expected by the Slug shader (this stuff should better go into an offline asset pipeline tool): https://github.com/floooh/sokol-samples/blob/master/libs/slu... ...the actual text rendering code is also taking a couple of shortcuts, e.g. no kerning, no right-to-left, and also no text shaping. There's also a new and complete text rendering stack by Mikko Mononen called Skribidi (AFAIK not based on Slug though): https://github.com/memononen/Skribidi ...the list of external dependencies is a bit scary for a small self-contained sample though (Harfbuzz, SheenBidi, libunibreak, etc...), but that basically shows that proper international text rendering is really damn hard, even when trying to simplify the code as much as possible. | |||||||||||||||||||||||||||||||||||||||||||||||
| |||||||||||||||||||||||||||||||||||||||||||||||
| ▲ | seanw265 3 hours ago | parent | prev | next [-] | ||||||||||||||||||||||||||||||||||||||||||||||
Interesting read. Text rendering techniques have always fascinated me. I'm a bit confused because at some points it seems like the author is conflating "tessellation" and "Rive". Are they the same thing? As an uneducated reader, my understanding would be that Rive is an implementation of a renderer using the tessellation approach. But surely a generic tessellation approach could support perfect arbitrary transformations even if Rive doesn't? Maybe there's something I'm missing. Also, a nitpick: in the "head to head" section, the author highlights Slug's better performance in green for the entries that it wins (or ties). For the sake of fairness, shouldn't we highlight the winners in every category? Surely Rive's "low" memory usage beats Slug's "moderate"? | |||||||||||||||||||||||||||||||||||||||||||||||
| ▲ | jayd16 3 hours ago | parent | prev | next [-] | ||||||||||||||||||||||||||||||||||||||||||||||
Is there a runtime comparison of the shaders needed? Seems like you might have to do a lot of texture sampling if you have to walk the ray through the data but maybe there's a trick? MSDF is pretty much just the target texel in question plus the surrounding samples in a way the GPU can do entirely upfront before the math starts. (M)SDF glyphs also play nicely with mip mapping and I would think this Slug algorithm needs uncompressed data. Maybe that doesn't matter because you just don't scale the data ever? | |||||||||||||||||||||||||||||||||||||||||||||||
| |||||||||||||||||||||||||||||||||||||||||||||||
| ▲ | pavlov 3 hours ago | parent | prev | next [-] | ||||||||||||||||||||||||||||||||||||||||||||||
> "In 2017 Eric Lengyel published an algorithm, called Slug, that stopped dodging. It renders glyphs directly from their outlines in the fragment shader, with no texture atlas and no per-frame tessellation. Lengyel patented it in 2019, and on March 17, 2026 he dedicated that patent to the public domain." It's nice that he gave the patent to public domain, but this is not how patents are supposed to work. You can't patent something two years after it was already published. I'm guessing he actually filed for a patent before publishing, and the article should read: "Lengyel was granted a patent for it in 2019" It's an AI-written article, so maybe it's not reasonable to expect it to be consistent on this level... | |||||||||||||||||||||||||||||||||||||||||||||||
| |||||||||||||||||||||||||||||||||||||||||||||||
| ▲ | sirwhinesalot 3 hours ago | parent | prev | next [-] | ||||||||||||||||||||||||||||||||||||||||||||||
There's another GPU text rendering algorithm missing from the comparison: Rook & Possum's Scanline Sweeper: https://rookandpossum.com/posts/scanline-sweeper/ Sean Barret (creator of the stb public domain libraries) independently invented a CPU-based implementation of the same idea, used in stb_truetype. | |||||||||||||||||||||||||||||||||||||||||||||||
| ▲ | rezmason 4 hours ago | parent | prev | next [-] | ||||||||||||||||||||||||||||||||||||||||||||||
Slug looks impressive! The project I'm most known for is basically an MSDF shader with a bloom pass. It serves my needs 100%, though if I expand to support arbitrary text, I may reach for Slug. The one issue with MSDF I want to raise is, it seems everybody uses the same msdfgen texture creation program from Viktor Chlumský's master's thesis 11 years ago. I wish there were other implementations. Who ever heard of a graphics technique that was only ever programmed once, and then used everywhere without substantial iteration? We need to de-XKCD-2347 MSDFs for everyone's sake, including and especially Chlumský. | |||||||||||||||||||||||||||||||||||||||||||||||
| |||||||||||||||||||||||||||||||||||||||||||||||
| ▲ | hncbw02z5a 4 hours ago | parent | prev | next [-] | ||||||||||||||||||||||||||||||||||||||||||||||
MSDF corners gave me trouble til I bumped the distance range at small sizes. | |||||||||||||||||||||||||||||||||||||||||||||||
| ▲ | bel8 4 hours ago | parent | prev | next [-] | ||||||||||||||||||||||||||||||||||||||||||||||
MSDF looks better than Slug for me in the first example. But slug wins in perspective in my eyes. I use MSDF to render crisp text in my webgl hobby game. Hope to publish it with source code when I get the time. thanks for sharing the article. I'll take a deeper look at it later. | |||||||||||||||||||||||||||||||||||||||||||||||
| |||||||||||||||||||||||||||||||||||||||||||||||
| ▲ | logdahl 3 hours ago | parent | prev | next [-] | ||||||||||||||||||||||||||||||||||||||||||||||
I knew the second Eric posted about releasing Slug to the public domain we'd get 100s of "OpenSlug" slopped-up. Will be interesting to see which implementation will win or if Slug will keep being a thing. | |||||||||||||||||||||||||||||||||||||||||||||||
| |||||||||||||||||||||||||||||||||||||||||||||||
| ▲ | reactordev 4 hours ago | parent | prev [-] | ||||||||||||||||||||||||||||||||||||||||||||||
Excellent write up. The cited references are on point too. Eric Lengyel is a legend. | |||||||||||||||||||||||||||||||||||||||||||||||