Metal backend doesn't appropriately flush CommandBuffer on many staging buffers

25 views
Skip to first unread message

BerstanioDev

unread,
Dec 8, 2025, 12:59:23 PM (9 days ago) Dec 8
to angleproject
I'm not well versed in graphics programming and ANGLE, but I think I identified the following issue (on iOS):

When calling `glTexImage2D` a lot of times in a single frame (e.g. during game launch preparing textures), I noticed the memory consumption steadily increase up to 1.2GB and then iOS killed the application. I identified the following allocating path:
- rx::(anonymous namespace)::UploadTextureContents
- rx::(anonymous namespace)::UploadTextureContentsWithStagingBuffer
- rx::(anonymous namespace)::CopyTextureContentsToStagingBuffer
- rx::mtl::Buffer::Buffer
- rx::mtl::Buffer::reset
- rx::mtl::ContextDevice::newBufferWithLength

After digging a bit through the code, I noticed the following:
`getBlitCommandEncoderWithoutEndingRenderEncoder` is called, but when it does, it will imidiatly return `return &mBlitEncoder;`. This leads to `flushCommandBufferIfNeeded` never being called, which should track that the attached resources to a command buffer don't exceed 400mb.
Adding `flushCommandBufferIfNeeded();` at the start of `getBlitCommandEncoderWithoutEndingRenderEncoder` fixed the issue for me.

I'm not sure, if that is really the root cause or whether there are better ways to fix this.
If necessary, I can try to provide a minimal reproducer.

Shahbaz Youssefi

unread,
Dec 8, 2025, 1:06:16 PM (9 days ago) Dec 8
to angleproject
Hi,

While I'm not familiar with the metal backend, a generic advice I have for you is to avoid mutable textures (`glTexImage2D`) and use immutable textures instead (`glTexStorage2D` + `glTexSubImage2D`). There are lots of optimizations that can be done (whether currently done in the metal backend or not) when the texture is known to be immutable. For example, with mutable textures, ANGLE typically keeps such data uploads in a buffer until the texture is fully specified. With immutable textures, some backends can apply the texture upload right away and avoid the increase in memory.

BerstanioDev

unread,
Dec 10, 2025, 7:34:46 AM (7 days ago) Dec 10
to angleproject
Hi,

thank you for the reply. Unfortunately, an underlying framework I'm using does the `glTexImage2D` calls and I can't rewrite it easily.
Reply all
Reply to author
Forward
0 new messages