Metal API thread safety

Hi,


I'd like to know if all Metal APIs are thread safe.


I can only confirm that the command queues are thread safe according to the documentation.


Can I use one MTLDevice concurrently on multiple threads?

Can I use one MTLTextureLoader concurrently on multiple threads?

How about CVMetalTextureCaches, are they thread-safe?

Replies

Anyone?

Honestly, about the only place I know of where Metal documentation says you can use multiple threads is MTLParallelRenderCommandEncoder https://developer.apple.com/documentation/metal/mtlparallelrendercommandencoder

And given Metal's lightweight nature, I'd assume that nothing else is thread-safe. Where did you find information about command queues being thread-safe? Also, I assume that you mean MTKTextureLoader, not MTLTextureLoader (yeah, confusing naming scheme). It has async functions, and it lets you load multiple textures in async way, so maybe it can internally work concurrently? But I would refrain from calling this object from multiple threads myself.

https://developer.apple.com/library/content/documentation/Miscellaneous/Conceptual/MetalProgrammingGuide/Cmd-Submiss/Cmd-Submiss.html#//apple_ref/doc/uid/TP40014221-CH3-SW14


A command queue accepts an ordered list of command buffers that the GPU will execute. All command buffers sent to a single queue are guaranteed to execute in the order in which the command buffers were enqueued. In general, command queues are thread-safe and allow multiple active command buffers to be encoded simultaneously.

Bumping this. APPLE: This is *important*. What are the threading requirements and implications using the API.


Can I allocate resources from a MTLDevice safely from multiple threads?

My evidence is pointing to no - this NEEDS to be documented. Stop making us have to guess.

You should be able to safely alloc resources from a device. What are you seeing that suggests otherwise?

This is one of the possible bugs caused by Metal synchronization issue.

https://github.com/gfx-rs/gfx/issues/1984

Apart from MTLDevice, other API calls needs clarify as well. Say MTLTexture.replaceRegion. Is it thread-safe?

Vulkan specification take a great deal of effort in documenting threading behaviors, which is very helpful.