Updating of a Metal texture in every frame

Hi,

We need to update some managed texture from system memory to GPU memory in every frame. Metal.

Problem: it seems that a texture still can be busy for rendering of queued frames and when we update it for next frame a previous frame can be rendered incorrectly (texture looks wrong).

How I can guess when a texture is being completely processed and can be re-used again? I don't know how long a queue of frames in Metal rendering.

It seems that DirectX can automatically create internal copies of a texture which we sent for rendering and we can start to modify the same texture for a next frame. How to do the same in Metal?
I thought the same thing...
Is "MTLTexture.replaceRegion" asynchronous or synchronous, or if it is asynchronous, is there a delegate?
but...
Due to the machine's convenience, I thought I had to wait until the next drawing timing, so I did it with a double buffer.
We call replaceRegion:mipmapLevel:withBytes:bytesPerRow:

Documentation: "This method immediately copies the pixel data into the texture. It does not synchronize against any GPU accesses to this texture. For example, if a command buffer includes read or write operations on a given texture, you must ensure that these operations complete before calling the replaceRegion:mipmapLevel:withBytes:bytesPerRow: method on the given texture. You can use the addCompletedHandler: method, waitUntilCompleted method, or custom semaphores to signal that a command buffer has completed execution."

According Apple's documentation, this function is synchronous. In a current frame we don't have anothr command buffer which has other operations with the same texture.

But we have previous frames which still may be in a queue for rendering and this texture can be busy (potentially).
But how we should guess that this texture is "busy"?
I would grateful for any advise.
Usually, you want to double- or triple- buffer so you're not waiting for the memory region to become available again. ie, make 3 textures and rotate between them so one is free to be updated, one is being enqueued, and one is being used for rendering at any given time.
Hi Ceylo, darknoon,

Thanks for your replies!

I think that double/tripple buffering could be a real explanation of this issue.

Also an article "Synchronizing Events Between a GPU and the CPU" also is a good idea. It's difficult to understand if we really should use these sync. events. I need to think more.

Right now, our app works fine with 3 textures in a series.
Updating of a Metal texture in every frame
 
 
Q