Synchronize the contents of a managed resource for the CPU or GPU.
Resources with a MTLStorageMode.managed storage mode are accessible to both the CPU and the GPU. Metal optimizes managed resources for each processor, but requires that you explicitly synchronize a given resource after modifying any part of its contents. After you synchronize a managed resource, the CPU and the GPU both have access to the updated resource data.
After you modify any part of a managed resource with the CPU or the GPU, you must synchronize the resource before you modify any part of it with the other processor. You must follow this rule even if the CPU-modified part and the GPU-modified part of the resource don’t overlap. When you synchronize a managed resource, Metal may synchronize more than the specified part if it needs to. Additionally, Metal may automatically copy resource data between the CPU and the GPU if your app or system is constrained by memory.
Synchronize a Managed Buffer
First, create a managed buffer.
Next, modify the buffer's data with the CPU.
After completing a CPU modification, call the didModifyRange(_:) method. This method updates a specific range of data and keeps the buffer synchronized. You must call this method to update the buffer’s data for the GPU; otherwise, the data is undefined for the GPU.
After encoding a GPU modification, encode a synchronize(resource:) command. This command updates the entire buffer and keeps it synchronized. You must execute this command to update the buffer’s data for the CPU; otherwise, the data is undefined for the CPU.
After encoding a GPU modification, encode a synchronize(resource:) command. This command updates the entire texture and keeps it synchronized. To update a specific texture slice or mipmap level, encode the synchronize(texture:slice:level:) command instead. You must execute one of these commands to update the texture’s data for the CPU; otherwise, the data is undefined for the CPU.