9,867 karma · joined Aug 20, 2015 (11y ago) · 5,528 submissions
comment 2d ago · link · parent
First, the data is sent to the monitor from a framebuffer. With compositors like Wayland, every window has its own buffer, and when it is damaged (updated), the compositors copies (memcpy) the damaged part from window buffer into the framebuffer using CPU or GPU. As I understand, this part cannot be skipped and the application renders window content into a window buffer so there always will be a memcpy stage to have it actually displayed.
For example, if the button changed its color, the program updates button image in window buffer, reports the damaged rectangle and the compositor copies that change into the framebuffer to become visible.
Actually compositors use two buffers to prevent tearing, and switch between them on every update so the things might be more complicated.
So with this model in mind, updating one line of text in a window buffer is faster than updating the whole buffer.
Furthermore, to render the text you need to memcpy the glyphs from glyph cache into their locations. So rendering whole window of text boils down to lot of small memcpys.
Also, you mention AVX-512 but I don't think my CPU has it. And when doing lot of 256-bit SIMD the CPU starts throttling to 1GHz-level speed. Also, you mentioned display lists but that requires extra memory compared to my approach.
Also, I decided to try to actually benchmark rendering text from a glyph atlas. Luckily, LLMs can write such code in matter of minutes, so I wrote it: https://pastebin.com/7baP4tWv
Results in a VM without a GPU:
Buffer 3840 x 2160 px, grid 295 cols x 90 rows = 26550 letters/frame, 10 runs, avg per frame: 7.832 ms, time per letter: 295.0 ns, letters/second: 3390088, pixels/second: 1059.086 Mpx/s.
I also godbolt'ed the hot loop in case you are curious about the assembly - it seems to use 128-bit SIMD and unrolled the single glyph rendering loop: https://godbolt.org/z/1Gh1McKE8
comment 2d ago · link · parent
And as I have written above, you can offload compositing (memcpy) to any GPU from 90s while rendering letters is a harder task. Even with glyph cache you need to manage it, upload updates to GPU etc. So much easier to not use GPU for this.
comment 2d ago · link · parent
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.
comment 2d ago · link · parent
Because writing good code is harder than bad code.
Let me give an example. I vide-coded a program that renders white and gray frames and it can do it faster than 60fps on one core in a VM with no GPU. However, VS Code in that VM feels like it is running at 5 fps. The scrolling is slow and laggy. Why is that so? Probably because writing good code is harder. And probably they were thinking like you, that redrawing the whole screen is fast enough except in fact it is not.
Every time someone says that their framework is fast, they should test it in a VM without a GPU.
comment 2d ago · link · parent
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.
comment 2d ago · link · parent
Only in a hello world application. Every professional editor like GIMP has tons of tiny widgets. And redrawing one tiny widget on mouse over is faster than running the render function.
Also, you are assuming that running render function costs nothing while in real-world apps there will be layout calculations (because you don't want to position widgets manually), array iteration (for example, to display a sum and average of values from a 100K element array), text characters layout, calling helper functions, generating SVG for a 10K points graph, some genius might add reading from a file, etc.
Yes, writing good code is harder than bad code, especially for a LLM.
And if you are on the stage where you need widgets and layout calculation, why not add a dirty flag and damage rectangles as well, I cannot understand.
comment 2d ago · link · parent
The article you are referring to is interesting, but it looks like adding a hack instead of rewriting the code. They also did not make measurements for an older system without a working GPU.
> If rebuilding the UI takes a millisecond at most, how much should we care?
Because user runs multiple programs, and rendering UI doesn't always take milliseconds, and battery capacity is not infinite. Also programs using GPU typically use lot of extra memory, require slow shader compilation, GPUs often have buggy drivers.
Also it would be better if every program used same UI widgets (for example, GTK) rather than have their own set of widgets with missing features like IME support, different hot keys etc.
Also, what is the advantage of immediate mode UI over proper GUI reactive framework with convenient declarative syntax? I see no advantages.
comment 2d ago · link · parent
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.
comment 2d ago · link · parent
- first, GPU requires shaders compilation which is slow so you need CPU renderer anyway
- second, not all GPUs are supported, not all have drivers, some are buggy and many machines, like VM, do not have a GPU
- third, it probably uses lot of memory
You don't need a GPU to add one letter to a text field.
I did some tests in a GPU-less VM, using one x86-64 core. The test simply rendered black and white 4K frames. Rendering to memory gave numbers like 360 fps (and ~8GiB/s of RAM writes). Rendering to X server via SHM produced something about 80 fps, the X server used 50% of the core and the program used other 50%. But many users have much smaller screens, and you typically do not need to redraw whole window every frame.
So you can render everything on a CPU and, if the user has a GPU, use it to accelerate drawing rounded rectangles which make probably 90% of GUI.
comment 5d ago · link · parent
comment 5d ago · link · parent