Hacker News new | ask | show | jobs
by ben-schaaf 9 days ago
Pegging the CPU while scrolling - a common action that takes a full rerender - is not good enough in this day and age. 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.

Shader compilation basically doesn't matter. The number of shaders needed for a GUI toolkit is tiny, and none are particularly complex. In my experience it doesn't appreciably add to the startup time.

I do agree there should still be CPU rendering for when the GPU doesn't work. It's a particularly useful feature for old or virtualised hardware.

2 comments

Scrolling should not take a full render - you ideally only need to render a small stripe that becomes visible. For example, if you scroll at 720px/sec, at 120 fps you need to render only 6 px of height per frame. Must not be hard.

For example, if you are scrolling a PDF you could theoretically "scroll" (change start y offset) of the window buffer, render a newly visible stripe (those 6px) and re-render the toolbar 6px lower which otherwise would scroll with the window.

It seems nobody can do this nowadays, so another option is to use two layers - a document and a toolbar and offload the compositing to window compositor which might as well use GPU for that. So you have one buffer with a toolbar (never changes), and another buffer with a document, and when scrolling, you only draw the changed part and instruct the compositor to draw the document with an offset.

Surprisingly, it's often faster to do a full render than to copy pixels around in the framebuffer, for two reasons.

1. With discrete GPUs, which have their own VRAM (unlike integrated GPUs), reading the framebuffer is often much slower than reading main memory.

2. If you're copying pixels around in the framebuffer, the best you can do is memcpy speed, which spends half of the memory bus bandwidth on reading memory. But if you're computing fresh pixels to put in the framebuffer instead, you can go about twice that fast. Sometimes more than twice that fast. As a simple example, https://canonical.org/~kragen/sw/dev3/bgraify.c unpacks 1-bit-deep bitmaps with a palette of two 32-bit colors on one core at 26 gigabytes per second (on this Ryzen 5 3500U), while memcpy() on that same core can only reach about 10 gigabytes per second. (memcpy() on two cores reaches more like 18 gigabytes per second, so the naïve extrapolation is that you ought to be able to hit 36 gigabytes per second for something like a solid-color fill.)

I do not see where I suggested to copy the pixels with CPU. With compositors like Wayland, you have a buffer in memory that stores content of a window. Obviously writing a 6-pixel stripe into that buffer (and updating starting Y offset) is faster than over-writing the whole buffer. Now, if that buffer is directly accessibly by GPU, you do not need to copy nothing. And I assume that even poor discrete GPUs can copy that 6 rows with pixels from system RAM to their memory for compositing.

Also your URL shows 404 for me.

To scroll the content you do not need to copy anything. You can just tell the compositor: "first render rows 6-1000 from this buffer and after that rows 0-5". That would look like the content has scrolled 6px up without a single copied byte. This trick was used by game consoles on 80s.

I'm sorry about the URL; I've fixed it now. Let me know if it's still broken.

Yes, you can scroll by telling the GPU to use a different starting address for its framebuffer, but only if you're scrolling the whole screen. If you want to scroll the contents of a browser window but not the browser chrome, though, or one window but not another, you start needing to tell the GPU to composite things from a few different textures, or needing to copy things around in system RAM. If you push this work off on a software compositor like Wayland, you aren't saving any work; you're just changing which process does the work.

> you start needing to tell the GPU to composite things from a few different textures,

Yes, correct, you need to have two buffers - one for the document, another for the toolbar. In this case you need to do compositing. The good thing is that compositing here is just memcpy, and it can be done on CPU and almost on any GPU starting from 90s, no need for Vulkan support or NVIDIA proprietary driver. And compositing (memcpy) is faster than rendering complicated document from scratch. So instead of sending one large buffer to Wayland, you send two smaller buffers ("send" doesn't imply copying here as Wayland uses shared memory).

So in a perfect world, the application updates a small part of a buffer with a document in a shared memory, changes Y offset, and CPU or GPU rebulds the window that was scrolled by combining the document and toolbar buffers. Oh, even better, by copying only the document part into a framebuffer because toolbar did not change.

So for example, if you are scrolling a text document, with my approach you need to render only one line of text (that became visible) and you reuse previously drawn lines. But with "immediate mode" approach, all visible lines must be re-rendered every frame.

URL works now.

> And compositing (memcpy) is faster than rendering complicated document from scratch.

Again, I don't think this is generally true, and that was the specific point I was investigating with bgraify.c. You can potentially render a complicated document from scratch, on the CPU, at speeds closer to memset() than to memcpy(), getting more than a 2× speedup.

And, on the GPU, you can do it at texture-mapping speeds, which are much faster still. (The basic plan is that you put the glyphs of your font into a texture atlas which your fragment shader texture-maps, possibly using SDF thresholding.) rxi's MicroUI https://github.com/rxi/microui is an example of a very small, simple IMGUI library whose standard backend works this way on top of OpenGL.

For anyone who is reading this later, I've concluded that I was basically wrong here: even when you can render pixels faster than you can copy them around the framebuffer, not rendering them is still faster and lower power, as long as there's a framebuffer. See https://news.ycombinator.com/item?id=50640236 and the demo program I elicited from Claude Opus 5.5 to test this: https://canonical.org/~kragen/sw/vriffle/
This saves you some work of drawing glyphs, but you now need to blit that whole buffer. So you've gone from a single color background fill and rendering a full page of text, to a offset framebuffer copy and then a small partial render. In terms of memory bandwidth I don't think you're coming out on top here.
Even without the GPU, you don't have to peg the CPU when scrolling a 4K 120Hz display. 3840 pixels/row × 2160 rows/frame × 120 frames/second × 4 bytes/pixel is 4 gigabytes per second. The laptop I'm typing this on can memcpy() to main memory at 10 gigabytes per second on one core and 17–19 gigabytes per second on two cores. So we're talking about more like a 25% CPU load if we can avoid extra copies, maybe better than that if most of the work is something like colormapping 8-bit grayscale font-glyph pixels.

(If you're mostly memcpying from a font that fits in cache, you should get a higher fill rate than this.)

Using a GPU is of course much better, but not necessary to reach the level of performance you're talking about.

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.

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.
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