Hacker News new | ask | show | jobs
by kragen 11 days ago
I don't think it's rendering text poorly, although this may be a question of taste. Look at the screenshots yourself: https://canonical.org/~kragen/sw/vriffle/

Despite the artifacts at regular font sizes I see with the current version of vriffle, its text rendering is dramatically superior to what mainstream UI toolkits provide, at least to my eye. Mainstream UI toolkits do not provide Knuth–Plass paragraph wrapping, any hyphenation at all, or any justification, and many of them do not even provide subpixel glyph positioning. Consequently, their text rendering quality more closely resembles that of an amateur desktop publishing flyer printed from a Macintosh SE than that of a properly typeset book. vriffle is closer to a properly typeset book, unless you set the text to be blurry or outlined or something.

The slight artifacts in its letterforms are smaller than the variability in inking you see in a book printed from hot lead. Mostly they have the effect of making DejaVu Serif look slightly more like a slab-serif font, which improves its readability at small point sizes.

Moreover, vriffle's approach provides continuously variable font sizes and font weights, which mainstream UI toolkits do not.

Finally, on my laptop, it's rendering 1080p frames in 3–5 milliseconds on one of the laptop's four cores, so at 60fps it's only using about 25% of one core, which is 6% of the CPU. This is a long way from pegging the CPU, and that's redoing the layout and rasterization from scratch on every frame, which you don't have to do if you're just rerendering to scroll. The compositing step is more like 1–2 milliseconds, but I think that's only because of a performance bug; with luck I'll be able to speed that up 4× today.

But, you might ask, what about the 4K display at 120fps we were originally talking about? That's 4 times the pixels twice as often, but that would still only be 50% of my CPU. And, as I understand it, this laptop's HDMI port isn't physically capable of more than 1080p output at 144Hz. If you were building a machine to drive a bigger display, I imagine you'd use a higher-end CPU to do it.

2 comments

The artifacts (that look like extra pixels at the bottom of letters) might be related to "signed distance fields" rendering.
Yes, and in particular the piecewise-planar approximation I'm using to the SDF, which is discontinuous, giving rise to a bit of spikiness in the level sets of the approximation. I have four attacks to try on this problem in vriffle:

1. Enjoy it. It slightly improves readability, and it's an aesthetically interesting effect that can be exaggerated if desired.

2. Sample the SDF at higher resolution. If I increase TPE_X (texels per em in X) from 24 to 96 and TPE_Y from 12 to 48, there are no longer any visible font rendering artifacts either at normal text sizes or at very large text sizes, and, despite the larger working set, the frame time with an animated procedural background and no inter-frame glyph caching increases from about 4.3 milliseconds to 4.7 milliseconds. Of course, it doesn't affect the size of the antialiased rasterized glyphs, so if I cache those between frames by pressing 'c' it's just as fast, 2.9–3.0ms. But it takes 1.1 seconds at startup to precompute the SDFs for all of the several thousand glyphs in DejaVu Serif, so I'd have to do that lazily. And there's some kind of bug in the SDF sampling which gives badly distressed rasterizations at extremely small font sizes with the higher-resolution SDF, when it should only improve the rendering.

3. Improve the SDF approximation by changing the piecewise-planar approximation, for example, rotating the gradient angle to a better angle. These spikes typically happen near sharp corners, where the gradient of the actual SDF rotates dramatically over a short distance, and a better approximation might be possible at the same resolution.

4. Use a different representation entirely.

I don't know which one will work best.

So, I ended up increasing the SDF sampling resolution, but only to 48×24, and also using a different approximation for the SDF, computed as the least-squares plane across the nearest-neighbor region for each texel. And fixed a couple of bugs. It looks pretty great now.
It's wild that you see artifacting due to your poor approach to drawing glyphs, and then say it's better because of layouting. Those are two entirely separate concerns.

Your response is pretty clearly generated, and I'm not about to engage in an argument with a meat proxy. Goodbye

I wrote every letter of that response with my own meat fingers, actually. I don't know how I would even go about getting an LLM to say things like "more closely resembles that of an amateur desktop publishing flyer printed from a Macintosh SE". If I sounded like Claude, it's probably mostly because I was talking to Claude all day, and partly because LLMs are trained largely on things written by people like me.

I'm disappointed that you're responding to a comment about aesthetic taste and algorithmic optimization with something that sounds suspiciously like personal attacks, and mistaken ones at that. I am hoping you have more to contribute to the conversation.

I think I've already comprehensively disproven your objections about subpixel antialiasing, subpixel positioning, blending with the background, subpixel blending, the cache footprint of font atlases, and the cache footprint of display lists, as well as an objection you could have raised about the time needed for constraint-optimization layout.

So, suppose, hypothetically, those font rendering artifacts weren't there, and rerendering the screen from scratch at a high frame rate still took only a fraction of the CPU. In that case, would you agree that your initial thesis was mistaken? I'll remind you that it was:

> Your best case scenario still doesn't achieve the minimum for a 4k120hz display. Take into account text with subpixel-antialiasing, and that resolutions scale to ok now and you'll quickly find CPU rendering unusably slow.

Does this thesis now stand or fall, in your eyes, on these little slab serifs?

> I'm disappointed that you're responding to a comment about aesthetic taste and algorithmic optimization with something that sounds suspiciously like personal attacks

It was not just a comment about aesthetics, but also a complete dismissal of a point I've made 3 times now: You are not rendering glyphs correctly. And so any conclusion about performance you're drawing from that is irrelevant.

> I think I've already comprehensively disproven your objections about subpixel antialiasing, subpixel positioning, blending with the background, subpixel blending, the cache footprint of font atlases, and the cache footprint of display lists, as well as an objection you could have raised about the time needed for constraint-optimization layout.

I don't understand how you can seriously claim that you've proven anything while you simultaneously admit to taking shortcuts that produce rendering artifacts.

> So, suppose, hypothetically, those font rendering artifacts weren't there, and rerendering the screen from scratch at a high frame rate still took only a fraction of the CPU. In that case, would you agree that your initial thesis was mistaken?

Yes? Of course, if you're able to render glyphs the same as Windows or Linux at a much faster rate than they are able to, I'd love to see it. But that's rather a moot point when you haven't done so.

I fixed it. Those font rendering artifacts aren't there anymore, and it's still just as fast, except that now it takes an extra 200 milliseconds to start. It's using the same SDF rendering algorithm, it's just at a higher sample frequency and using a better approximation of the SDF. So, now, will you agree that you initial thesis was mistaken? That is, I will remind you, "Your [codedokode's] best case scenario [of on-CPU rendering] still doesn't achieve the minimum for a 4k120hz display. Take into account text with subpixel-antialiasing, and that resolutions scale to ok now and you'll quickly find CPU rendering unusably slow."

Also, I think there are a number of fundamental misconceptions behind your comment, and I think it's worthwhile to try to correct them.

— ⁂ —

First, you say, "if you're able to render glyphs the same as [Microsoft] Windows or Linux", but Microsoft Windows and [the font rendering machinery generally used on] Linux don't render glyphs the same as one another. MacOS doesn't render them the same as either one of them. So your proposed criterion for minimal usability would reject MacOS.

— ⁂ —

Second, you complain that I am completely dismissing your point that "[I am] not rendering glyphs correctly." I'm dismissing your point because it is based on a basic misunderstanding. TrueType and similar outline-font formats define infinitely precise, mathematically-abstract letterforms made out of splines and straight lines. Computer screens, by contrast, can only display sampled approximations of those shapes. Worse, computer screens are fairly low-resolution (often only ≈150dpi, an order of magnitude worse than a garden-variety 600dpi laser printer and two orders of magnitude worse than high-quality 2400dpi printing), and they don't enjoy the high-quality analog antialiasing filtering that audio sampled systems virtually always employ. Furthermore, they are invariably run at nearly their maximum dynamic range, leading to clipping when they try to display signals with sharp steps in them, such as letterforms.

Consequently, rendering letterforms on computer screens is always an exercise in balancing different kinds of imperfections. You can't sinc-filter those sharp edges because the Gibbs phenomenon demands that you go outside the dynamic range of the screen, unless you reduce the contrast ratio, significantly impairing readability. If you exploit subpixel antialiasing fully, you get color fringing. You need some kind of antialiasing filter in the digital domain, even if it's just a box filter, but that unavoidably reduces sharpness.

Alpha compositing (Porter–Duff over), which is commonly used for blending text with a background, isn't correct either. It's an efficiency compromise which produces visible display artifacts, such as the so-called "conflation artifacts", under common circumstances. It's a good enough compromise for many situations, and it's much faster than, for example, supersampling antialiasing, but it's just an aesthetically acceptable compromise. It's not a standard of correctness.

There is no such thing as correct rendering of letterforms on a computer display. There's only a choice of compromises. That's why macOS renders text differently from Microsoft Windows.

It's not a question of correctness. It's a question of aesthetics.

— ⁂ —

Third, you said, "I don't understand how you can seriously claim that you've proven anything while you simultaneously admit to taking shortcuts that produce rendering artifacts." I am not sure if you were serious about this, but I am going to assume good faith and assume that you were, and explain the thing you claimed not to understand.

What I said I had proven was that you were mistaken in "your objections about subpixel antialiasing, subpixel positioning, blending with the background, subpixel blending, the cache footprint of font atlases, and the cache footprint of display lists." Specifically, you brought these up as reasons that on-CPU rendering "still doesn't achieve the minimum for a 4k120hz display".

The reason I was confident in claiming that, when I still had visible font rendering artifacts, was that none of those things have anything to do with letterform rasterization!

The artifacts weren't in display list cache footprints or in text layout with subpixel positioning. So those parts of vriffle didn't need to change to fix the artifacts.

— ⁂ —

On my laptop, though, it's still slower than memcpy, taking about 4 milliseconds on one core for a 1080p frame filling the screen with text at a readable size. That's only about 25% of the speed of memcpy. But it's still not close to pegging the CPU.

Updated screenshots and source code at https://canonical.org/~kragen/sw/vriffle/