Improving texture atlas allocation in WebRender
nical.github.io
nical.github.io
You can peek behind the scenes of maps.google.com using WebGL Inspector or similar, it's kinda fun to watch.
https://github.com/magcius/noclip.website/blob/master/src/So...
Compared to shelf, I think it did really well on the input data I had. I didn't even use the Halo Reach tricks for removing border padding when packing subpixel lightmaps!
Font atlas is required to be dynamic because Unicode has too many different codepoints. Statically allocated bitmap fonts no longer work in practice. Based on the pictures in the article, it works better than what WebRender is doing.
The performance is good even on Raspberry Pi. This library https://github.com/Const-me/Vrmac uses 3 of them for 2D graphics: RGBA for sprites, another RGBA for ClearType fonts, and grayscale one for non-ClearType fonts. Here's the only pixel shader that implements all 2D rendering, including text: https://github.com/Const-me/Vrmac/blob/master/Vrmac/Draw/Sha...
> a batch can only reference a fixed set of resources (such as GPU buffers and textures)
Technically that’s true, but not practically. There’s GPU resource type called “texture array”. It’s possible to append new layers into an array without moving data across PCIe and back.
> It’s possible to append new layers into an array without moving data across PCIe and back.
? As far as I know, texture layer dimensions have to be specified up-front. Unless, I suppose, you're OpenGL and the driver lies to you about that with mutable textures, but it's really creating a new texture under and doing a full copy for you behind the scenes.
OpenGL 3.0 and OpenGL ES 3.0: https://pcwalton.github.io/slides/201808-webrender/
They both support texture arrays.
> texture layer dimensions have to be specified up-front
Indeed, but it's possible to create another larger one, and copy layers between arrays within GPU.
It's the same idea as std::vector on CPUs, most heaps can't resize memory either, they instead allocate new blocks and copy.
There're two wins with texture arrays.
1. Removes area limitation. 2D textures are often limited to rather small size, like 2k^2 pixels. Same limitation applies to arrays, but with multiple layers their total area is only limited by VRAM size. That 2D library I've linked above renders arbitrarily complex 2D graphics with only 2 draw calls, regardless on how many sprites or font glyphs are used in the scene. Two because front-to-back opaque followed by back-to-front translucent.
2. These packing algorithms often scale worse than linearly with available area or count of sprites on the atlas. Using multiple layers of the array takes care of that, new sprites are only packed into the last layer, and layer size is never too large.
Back when we were using arrays, we indeed "resized" them as you describe. When they became large we ran in to terrible performance problems allocating and copying such large blocks of memory. We decided it was better to use fixed size textures and accept some batch breaks.
Interesting. I wonder how many of them were bugs of actual OS-supplied GPU driver, as opposed to Angle compatibility layer you seem to be using on Windows? My library uses better designed compatibility layer called diligent engine https://diligentgraphics.com/diligent-engine/
> and accept some batch breaks
How do you solve Z order changes and similar runtime changes?
The example is multiple layers with images. When first rendered they were Z ordered as ABCD, in a next frame reordered as DBCA. It's possible these images are grouped into 2 textures as AB and BC, this means after the reorder you need 1 draw call per image, which is less than ideal.
Happens quite often on the web. Like a forum thread with avatars, as user scrolls, the rendering order of avatars changes randomly.
We only have to break the batch if the avatars are in separate textures and they overlap, so it isn't a huge issue in practice. There are cases where it is, but it's much better than the issues we had "resizing" the texture arrays.
edit: to clarify, yes if images are in separate textures we must use separate batches, but as long as they don't overlap we can still add more images to the old batch. so the total number doesn't grow too large
AFAIK it’s same as on Windows, if you want a reliable GPU API you have to use the native one, Metal or Direct3D respectively.
> and especially on mobile
Right, these can be hit and miss. There’s hope though, new Android seems to be transitioning from GLES to Vulkan, things might improve eventually.
> the issues we had "resizing" the texture arrays.
I wonder did anyone tried to use D3D tiled resources, or Vulkan sparse images, for this problem?