0

I am aware of MPSImageHistogram, but I'd like to implement the algorithm myself to understand Metal better. However, I run into thread synchronization problem when trying to accumulate pixel information into histogram bins, and I got no clue how to solve it. On iOS, I think I have a couple of considerable options including programmable blending and thread group sharing. Unfortunately, those are not available on macOS.

I appreciate any general tip/direction to approach the problem on macOS, either thread synchronization or image histogram.

AQUArch
  • 1
  • 1
  • 2

1 Answers1

1

The simplest approach is probably to use atomic variables (e.g. atomic_uint) and atomic operations (e.g. atomic_fetch_add_explicit()) to increment the counts. You may get a lot of contention which may cause performance issues, but that's sort of another question.

Ken Thomases
  • 88,520
  • 7
  • 116
  • 154
  • @KenThomasese I thought of atomic variables, but I don't think I can create an array of `atomic_uint` to manage all of histogram bins. That would be a lot of counter input arguments to pass into the shaders. – AQUArch Jun 25 '18 at 19:32
  • Yes, you can treat a buffer as an array of `atomic_uint`. `device atomic_uint* bins[[buffer(0)]]` and then pass `&bins[i]` into one of the atomic functions. – Ken Thomases Jun 25 '18 at 19:59
  • I didn't think that would work at all. I am going to test it out. Thanks a lot @ken. – AQUArch Jun 25 '18 at 20:33