MarkDirtyResource
on a buffer ifcontents
is called and the returned pointer is not NULL (type should beMTLStorageModeShared
).MarkDirtyResource
on a buffer ifdidModifyRange
is called (type should beMTLStorageModeManaged
).MarkDirtyResource
on a buffer if buffer type isMTLStorageModePrivate
.MTLBuffer
type is only known at creation time.
- How do dirty resources interact with frame referenced resources. Will dirty resources always be output or is it only if they are frame referenced also?
- TODO
- Metal Documentation: Choosing a Resource Storage Mode in macOS
- MTLBuffer Storage Modes
MTLStorageModeShared
: Stored in CPU memory accessible by the GPU.- Application manages synchronisation:
- CPU->GPU:
MTLCommandBuffer::commit
- GPU->CPU: when command buffer completes execution ie.
MTLCommandBuffer::status == MTLCommandBufferStatusCompleted
MTLStorageModeManaged
: Stored in CPU and GPU memory.- Metal driver manages synchronisation:
- CPU->GPU:
MTLBuffer::didModifyRange
- GPU->CPU:
MTLBlitCommandEncoder::synchronizeResource
MTLStorageModePrivate
: Stored in GPU memory.- Only accessible by copying to a
MTLStorageModeShared
buffer.
- Only accessible by copying to a
MTLBuffer
CPU contents locked / submitted to GPU onMTLCommandBuffer:: commit
.- RenderCommandEncoder has APIs to use
MTLBuffer
ie.setVertexBuffer
,setFragmentBuffer
.
- To read buffer data will need to convert
MTLStorageModePrivate
buffers toMTLStorageModeManaged
or use a temporary intermediateMTLStorageModeManaged
buffer. - Mark buffer dirty if
contents
is called and returned pointer is not NULL : type should beMTLStorageModeShared
- Mark buffer dirty if
didModifyRange
is called, type should beMTLStorageModeManaged
- Mark buffer dirty if buffer type is
MTLStorageModePrivate
ie. GPU only - Should
MTLStorageModeManaged
buffers only be tracked ifcontents
is called on them?- In theory yes if the returned pointer is not NULL.
- In practice might be easier to snashot the buffer without explicit tracking.
- Should buffers only be tracked if they can reside in CPU memory?
- GPU only buffers do not need to be tracked for CPU modification. They need to be tracked for GPU modification which is hard to do for all situations.
- In practice easier to snashot the buffer without explicit tracking.
- CPU buffers with explicit synchronization can be handled in the serialization of the synchronization APIs
- In practice might be easier to snashot the buffer without explicit tracking.
- GPU only buffers do not need to be tracked for CPU modification. They need to be tracked for GPU modification which is hard to do for all situations.
- Attach the buffers to the
MTLRenderCommandEncoder
parentMTLCommandBuffer
insetVertexBuffer
,setFragmentBuffer
APIs. - On
MTLCommandBuffer::commit
in active capture serialize attached buffer contents. - On
MTLCommandBuffer::commit
replay de-serialize buffer contents.