Reputation: 6540
Is it possible to have a Metal compute function that processes a texture in-place on iOS? I have noticed that some MPS
image filters support in-place processing, and was wondering if there is a way to accomplish this with custom kernels.
Specifically, I am looking to combine two textures into one using a blend function. I am easily able to do this by making the first texture a render target and using a shader to write the second one on top, but it feels like an overkill since both textures are the same size.
Upvotes: 1
Views: 195
Reputation: 31782
Yes, you can take a texture parameter with the access::read_write
attribute, and read and write it within the same kernel function invocation. You'll need to ensure that the texture is created with both the .read
and .write
usage flags. Additionally, note that writes are not guaranteed to be seen by any subsequent reads by the same thread unless you call the flush()
function after the write.
By the way, MetalPerformanceShaders kernels that are able to operate "in-place" don't necessarily use read_write
textures; it's often the case that they use auxiliary textures and buffers and do their work across multiple passes. Per the documentation, any kernel can fail to operate in-place for any number of reasons, so you should always provide a fallback allocator to handle such cases.
Upvotes: 3