Search for max value coordinate in a MTLTexture

I struggle with the implementation of a compute kernel to solve a relative simple search problem. I want to find the coordinate with the highest pixel value within a MTLTexture.
I have already an implementation where I copy the content of the MTLTexture into CPU memory and apply some min/max commands from the accelerate framework. But maybe there is also a simple solution for this task using Metal which I haven't found yet.



Replies

This can certainly be done with a Metal compute kernel although kernels aren't especially good at scanning through lots of data to pick something out. The CPU is better suited for that type of work. If the value will be used for subsequent CPU work, using vImage as you are currently doing would probably the best option. However, if your app uses the computed value for subsequent GPU work, you probably should create a kernel to do this since waiting for the GPU the pixel data to the CPU and then pushing the result back to the GPU will incur lots of overhead.

If you decide that using the GPU to do the work makes sense, MPS provides the MPSImageAreaMax class which generate an appropriate compute kernel.