Hacker News new | ask | show | jobs
by ben-schaaf 12 days ago
Real applications don't use 8 bit grayscale font glyphs; there's colour glyphs and subpixel Antialiasing, and subpixel positioning. Commonly you're storing 3 channel alpha (or color), and 6 copies of each glyphs at different subpixel positions. You're not blitting these glyphs, you're blending them with the background. Font atlases do not fit in cache, especially at 4k. You also need to not just write the framebuffer, but also send it to the GPU. On some systems that can be done quickly, on others you're going over PCIe. And on top of all that, you have the data structures that are telling you what text to render where, which further fill the cache.

All that to say, your example of blitting a single color is very, very far away from the practical reality of rendering a GUI.

1 comments

I elicited a demo application from Claude last night to test this. As a placeholder name, it's called Riffle. On the CPU, it does LCD subpixel antialiasing, subpixel positioning, kerning, the full-paragraph-optimization Knuth–Plass line breaking algorithm, Liang hyphenation, full text justification, Unicode, continuously variable font size, and continuously variable font weight, and it blends the glyphs with an animated procedural texture background, per color channel, i.e., with LCD subpixel antialiasing.

If you're not familiar with some of these algorithms, just know that, for the most part, they're significantly more demanding than the layout systems used by Real Applications (if a browser is a Real Application, anyway) and produce much better results.

On my 1080p display (¼ of 4K) Riffle runs in the range 120–300 frames per second on one of my four cores, at roughly 1 gigapixel per second. However, my LCD is only 60 fps.

I tried to record a video with OBS, but not only did the extra load slow Riffle down to only about 70fps, but OBS started missing most of the screen frames I was seeing when Riffle updated the screen rapidly, adding serious jank to the recording that wasn't there in real life. Worse, my attempts to compress the resulting file with ffmpeg to anything resembling a reasonable size made complete garbage out of the text. So you'll have to run it yourself if you want to see it.

On one core of the Xeon it was first tested on, it draws roughly 3.5 gigapixels per second, which is about 60% faster than memcpy and would be about 400 frames per second at 4K resolution. On my Ryzen 5 3500U laptop, it manages about 1.1 gigapixels per second, which is 55% slower than memcpy and barely over 120 frames per second. I'm hoping I can improve that today.

Although most of the algorithms I'm using produce much higher-quality output than Real Applications, I did do two things that compromise output quality slightly to get speed:

1. Compositing the dark antialiased text onto the light background is done with elementwise minimum rather than standard α-multiplication. This is an imperceptible difference visually, but roughly doubles the fill rate even without LCD subpixel compositing, which it provides for free.

2. The glyph atlas for each frame is generated from a piecewise-linear sampled approximation of the font SDF, which is generated at startup. (Initially, I was just rendering the frame directly from the SDF, but on the Xeon that was only hitting about 1 gigapixel per second.) At large font sizes this produces visible artifacts. However, it also provides continuously variable font size, weight, and outlining.

Again, I don't want to claim that rendering the UI on the CPU is better than doing it on the GPU. Doing it on the GPU is better! But doing it on the CPU is totally tolerable, very far from "pegging the CPU while scrolling [on] a 4k120hz display".

If you want to try it out, it's at https://canonical.org/~kragen/sw/riffle-packed.tar.gz.

It's a neat demo, but not applicable for a UI toolkit. Sure, if you render text poorly you can achieve much greater speeds. You don't need to look hard to see artifacts even at regular font sizes with this approach.

Do the same demo with proper font atlases and proper blending and you won't be seeing that same level of performance.

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.

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.

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.