简体   繁体   中英

How does Metal Shader Language's (MSL) atomic_uint bridge to Swift?

In my Swift iOS Metal App, I would like to have a Metal Shading Language (MSL) kernel like so:

kernel void someKernel(device atomic_uint &counter [[buffer(0)]], ...)
{
    ...
}

I have 2 questions:

  1. How does MSL's atomic_uint bridge to Swift?
  2. What is the size of atomic_uint in memory so that I can decide how many bytes to reserve for the MTLBuffer on the Swift/App side?

Thank your for help

To answer your initial question:

  1. atomic_uint maps to a UInt32 type in Swift.
  2. It's going to be 4 bytes or MemoryLayout<UInt32>.stride to allocate.

And to add: atomics aren't going to be atomic between CPU and GPU. Meaning, you can't change memory atomically on CPU and see this change atomically on GPU and vice versa. So in the end, this is why it maps to just a UInt32 . To guarantee that CPU sees GPU changes you will need to wait for a buffer to complete by either adding a completion handler or by signaling MTLSharedEvent on a GPU timeline and making a listener for that event in CPU code.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM