|
|
|
|
|
by codedokode
10 days ago
|
|
Let's say you scroll text. I believe rendering one line of text (that became visible) on each frame, and reusing previously rendered lines is faster than rendering all text on every frame. Text rendering is not an easy task. 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. |
|
Text rendering can be made arbitrarily complex, but in most cases it is not. In Russian, most Spanish, most Japanese, Ukrainian, or English, code points map 1:1 to glyphs, and figuring out where each glyph belongs on a line is a matter of running a prefix sum over their advances. AVX-512 prefix sum on 32-element vectors is about 2 nanoseconds per 32-element vector, so you need about 5 nanoseconds of layout time per line and about 200 nanoseconds for the whole screen, say 250ns to include the time to check for exceptional cases and figure out where each line belongs on the screen. This gives you a display list of some 3000 glyphs at various places on the screen.
If you rerender 2 megapixels from that display list at, say, 5 gigapixels per second on the CPU, it's going to take 400μs, which is 400,000ns. (I think the evidence I've shown above strongly suggests that this is doable on my laptop here, although it isn't a proof.)
If, instead, you copy 2 megapixels from one place to another in RAM, you can only do that at about 2.5 gigapixels per second, so reusing previously rendered lines will take more like 800μs.
That is slower than 400μs.
It is not faster than 400μs.
If you, instead, pass the display list to the GPU to texture-map from the font atlas, well, this GPU has a theoretical peak texture fill rate of 38.4 gigatexels per second, so it will take more like 52 μs. On the GPU, you can't really reuse the previously rendered lines.
Again, 800μs is slower than 52 μs. It's not faster.
The GPU doesn't really mind rendering 3000 quads instead of two or three. The difference is down in the range of [correction!] microseconds; this GPU can in theory do one triangle per clock at 1.2GHz, so each quad takes about 1.7 nanoseconds, and you can do 3000 of them in 5μs, about 10% of the total. But this is running concurrently with the fill rate, so it doesn't increase the 52μs.
The key thing to note, though, is that even the 800μs you would get by foolishly reusing previously rendered lines is still pretty small compared to the frame time, which at 60fps (what my laptop can actually run at) is 16667μs, 20 times longer. If you parcel that out among the 3000 or so glyphs on the screen, you have almost 5.6μs to do all the text layout calculations for each glyph: kerning, ligatures, shaping, whatever.
It's true that you need to manage the texture atlas, glyph cache, etc., although multichannel SDF fonts can provide reasonable antialiased results at a fairly wide range of pixel sizes, which can reduce this load quite a lot.