From 5e286ad9b86cb9c3e82cc1fdcd645378c367f933 Mon Sep 17 00:00:00 2001 From: Jake Turner Date: Tue, 6 Jan 2026 13:03:31 +1300 Subject: [PATCH] Original ID/Live ID fixups for Metal backend --- .../driver/metal/metal_command_buffer.cpp | 8 ++--- .../driver/metal/metal_command_queue.cpp | 5 +-- renderdoc/driver/metal/metal_core.cpp | 2 +- renderdoc/driver/metal/metal_device.cpp | 36 +++++++++---------- renderdoc/driver/metal/metal_library.cpp | 6 ++-- renderdoc/driver/metal/metal_manager.h | 22 +++++++----- renderdoc/driver/metal/metal_serialise.cpp | 6 ++-- 7 files changed, 44 insertions(+), 41 deletions(-) diff --git a/renderdoc/driver/metal/metal_command_buffer.cpp b/renderdoc/driver/metal/metal_command_buffer.cpp index e416e9dbe..6d029522d 100644 --- a/renderdoc/driver/metal/metal_command_buffer.cpp +++ b/renderdoc/driver/metal/metal_command_buffer.cpp @@ -59,8 +59,8 @@ WrappedMTLBlitCommandEncoder *WrappedMTLCommandBuffer::blitCommandEncoder() MTL::BlitCommandEncoder *realMTLBlitCommandEncoder; SERIALISE_TIME_CALL(realMTLBlitCommandEncoder = Unwrap(this)->blitCommandEncoder()); WrappedMTLBlitCommandEncoder *wrappedMTLBlitCommandEncoder; - ResourceId id = - GetResourceManager()->WrapResource(realMTLBlitCommandEncoder, wrappedMTLBlitCommandEncoder); + ResourceId id = GetResourceManager()->WrapResource(ResourceId(), realMTLBlitCommandEncoder, + wrappedMTLBlitCommandEncoder); wrappedMTLBlitCommandEncoder->SetCommandBuffer(this); if(IsCaptureMode(m_State)) { @@ -113,7 +113,7 @@ WrappedMTLRenderCommandEncoder *WrappedMTLCommandBuffer::renderCommandEncoderWit Unwrap(this)->renderCommandEncoder(mtlDescriptor)); mtlDescriptor->release(); WrappedMTLRenderCommandEncoder *wrappedMTLRenderCommandEncoder; - ResourceId id = GetResourceManager()->WrapResource(realMTLRenderCommandEncoder, + ResourceId id = GetResourceManager()->WrapResource(ResourceId(), realMTLRenderCommandEncoder, wrappedMTLRenderCommandEncoder); wrappedMTLRenderCommandEncoder->SetCommandBuffer(this); if(IsCaptureMode(m_State)) @@ -165,7 +165,7 @@ bool WrappedMTLCommandBuffer::Serialise_presentDrawable(SerialiserType &ser, AddEvent(); ActionDescription action; - ResourceId presentedImageId = GetResourceManager()->GetOriginalID(GetResID(presentedImage)); + ResourceId presentedImageId = GetResID(presentedImage); action.customName = StringFormat::Fmt("presentDrawable(%s)", ToStr(presentedImageId).c_str()); action.flags |= ActionFlags::Present; action.copyDestination = presentedImageId; diff --git a/renderdoc/driver/metal/metal_command_queue.cpp b/renderdoc/driver/metal/metal_command_queue.cpp index a9b126cce..02143db96 100644 --- a/renderdoc/driver/metal/metal_command_queue.cpp +++ b/renderdoc/driver/metal/metal_command_queue.cpp @@ -55,7 +55,8 @@ WrappedMTLCommandBuffer *WrappedMTLCommandQueue::commandBuffer() MTL::CommandBuffer *realMTLCommandBuffer; SERIALISE_TIME_CALL(realMTLCommandBuffer = Unwrap(this)->commandBuffer()); WrappedMTLCommandBuffer *wrappedMTLCommandBuffer; - ResourceId id = GetResourceManager()->WrapResource(realMTLCommandBuffer, wrappedMTLCommandBuffer); + ResourceId id = GetResourceManager()->WrapResource(ResourceId(), realMTLCommandBuffer, + wrappedMTLCommandBuffer); wrappedMTLCommandBuffer->SetCommandQueue(this); if(IsCaptureMode(m_State)) @@ -75,7 +76,7 @@ WrappedMTLCommandBuffer *WrappedMTLCommandQueue::commandBuffer() else { // TODO: implement RD MTL replay - GetResourceManager()->AddLiveResource(id, wrappedMTLCommandBuffer); + GetResourceManager()->AddResource(id, wrappedMTLCommandBuffer); } return wrappedMTLCommandBuffer; diff --git a/renderdoc/driver/metal/metal_core.cpp b/renderdoc/driver/metal/metal_core.cpp index de19c9c32..b4aea8b9b 100644 --- a/renderdoc/driver/metal/metal_core.cpp +++ b/renderdoc/driver/metal/metal_core.cpp @@ -487,7 +487,7 @@ void WrappedMTLDevice::AddResource(ResourceId id, ResourceType type, const char void WrappedMTLDevice::DerivedResource(ResourceId parentLive, ResourceId child) { - ResourceId parentId = GetResourceManager()->GetOriginalID(parentLive); + ResourceId parentId = parentLive; GetReplay()->GetResourceDesc(parentId).derivedResources.push_back(child); GetReplay()->GetResourceDesc(child).parentResources.push_back(parentId); diff --git a/renderdoc/driver/metal/metal_device.cpp b/renderdoc/driver/metal/metal_device.cpp index 470acf0f2..110a3ad03 100644 --- a/renderdoc/driver/metal/metal_device.cpp +++ b/renderdoc/driver/metal/metal_device.cpp @@ -66,7 +66,7 @@ WrappedMTLDevice::WrappedMTLDevice(MTL::Device *realMTLDevice, ResourceId objId) } RDCASSERT(m_Device == this); - GetResourceManager()->AddCurrentResource(objId, this); + GetResourceManager()->AddResource(objId, this); if(IsCaptureMode(m_State)) { @@ -184,8 +184,7 @@ bool WrappedMTLDevice::Serialise_newCommandQueue(SerialiserType &ser, WrappedMTL { MTL::CommandQueue *realMTLCommandQueue = Unwrap(this)->newCommandQueue(); WrappedMTLCommandQueue *wrappedMTLCommandQueue; - GetResourceManager()->WrapResource(realMTLCommandQueue, wrappedMTLCommandQueue); - GetResourceManager()->AddLiveResource(CommandQueue, wrappedMTLCommandQueue); + GetResourceManager()->WrapResource(CommandQueue, realMTLCommandQueue, wrappedMTLCommandQueue); AddResource(CommandQueue, ResourceType::Queue, "Queue"); DerivedResource(this, CommandQueue); @@ -198,7 +197,8 @@ WrappedMTLCommandQueue *WrappedMTLDevice::newCommandQueue() MTL::CommandQueue *realMTLCommandQueue; SERIALISE_TIME_CALL(realMTLCommandQueue = Unwrap(this)->newCommandQueue()); WrappedMTLCommandQueue *wrappedMTLCommandQueue; - ResourceId id = GetResourceManager()->WrapResource(realMTLCommandQueue, wrappedMTLCommandQueue); + ResourceId id = + GetResourceManager()->WrapResource(ResourceId(), realMTLCommandQueue, wrappedMTLCommandQueue); if(IsCaptureMode(m_State)) { Chunk *chunk = NULL; @@ -256,8 +256,7 @@ bool WrappedMTLDevice::Serialise_newDefaultLibrary(SerialiserType &ser, WrappedM dispatch_release(dispatchData); WrappedMTLLibrary *wrappedMTLLibrary; - GetResourceManager()->WrapResource(realMTLLibrary, wrappedMTLLibrary); - GetResourceManager()->AddLiveResource(Library, wrappedMTLLibrary); + GetResourceManager()->WrapResource(Library, realMTLLibrary, wrappedMTLLibrary); AddResource(Library, ResourceType::Pool, "Library"); DerivedResource(this, Library); } @@ -270,7 +269,7 @@ WrappedMTLLibrary *WrappedMTLDevice::newDefaultLibrary() SERIALISE_TIME_CALL(realMTLLibrary = Unwrap(this)->newDefaultLibrary()); WrappedMTLLibrary *wrappedMTLLibrary; - ResourceId id = GetResourceManager()->WrapResource(realMTLLibrary, wrappedMTLLibrary); + ResourceId id = GetResourceManager()->WrapResource(ResourceId(), realMTLLibrary, wrappedMTLLibrary); if(IsCaptureMode(m_State)) { Chunk *chunk = NULL; @@ -309,8 +308,7 @@ bool WrappedMTLDevice::Serialise_newLibraryWithSource(SerialiserType &ser, NS::Error *compileErrors = NULL; MTL::Library *realMTLLibrary = Unwrap(this)->newLibrary(source, options, &compileErrors); WrappedMTLLibrary *wrappedMTLLibrary; - GetResourceManager()->WrapResource(realMTLLibrary, wrappedMTLLibrary); - GetResourceManager()->AddLiveResource(Library, wrappedMTLLibrary); + GetResourceManager()->WrapResource(Library, realMTLLibrary, wrappedMTLLibrary); AddResource(Library, ResourceType::Pool, "Library"); DerivedResource(this, Library); } @@ -324,7 +322,7 @@ WrappedMTLLibrary *WrappedMTLDevice::newLibraryWithSource(NS::String *source, MTL::Library *realMTLLibrary; SERIALISE_TIME_CALL(realMTLLibrary = Unwrap(this)->newLibrary(source, options, error)); WrappedMTLLibrary *wrappedMTLLibrary; - ResourceId id = GetResourceManager()->WrapResource(realMTLLibrary, wrappedMTLLibrary); + ResourceId id = GetResourceManager()->WrapResource(ResourceId(), realMTLLibrary, wrappedMTLLibrary); if(IsCaptureMode(m_State)) { Chunk *chunk = NULL; @@ -377,8 +375,7 @@ bool WrappedMTLDevice::Serialise_newBufferWithBytes(SerialiserType &ser, Wrapped realMTLBuffer = Unwrap(this)->newBuffer(initialData.data(), initialData.size(), options); } WrappedMTLBuffer *wrappedMTLBuffer; - GetResourceManager()->WrapResource(realMTLBuffer, wrappedMTLBuffer); - GetResourceManager()->AddLiveResource(Buffer, wrappedMTLBuffer); + GetResourceManager()->WrapResource(Buffer, realMTLBuffer, wrappedMTLBuffer); AddResource(Buffer, ResourceType::Buffer, "Buffer"); DerivedResource(this, Buffer); @@ -419,9 +416,8 @@ bool WrappedMTLDevice::Serialise_newRenderPipelineStateWithDescriptor( Unwrap(this)->newRenderPipelineState(mtlDescriptor, error); mtlDescriptor->release(); WrappedMTLRenderPipelineState *wrappedMTLRenderPipelineState; - liveID = GetResourceManager()->WrapResource(realMTLRenderPipelineState, + liveID = GetResourceManager()->WrapResource(RenderPipelineState, realMTLRenderPipelineState, wrappedMTLRenderPipelineState); - GetResourceManager()->AddLiveResource(RenderPipelineState, wrappedMTLRenderPipelineState); AddResource(RenderPipelineState, ResourceType::PipelineState, "Pipeline State"); DerivedResource(this, RenderPipelineState); } @@ -438,8 +434,8 @@ WrappedMTLRenderPipelineState *WrappedMTLDevice::newRenderPipelineStateWithDescr realDescriptor->release(); WrappedMTLRenderPipelineState *wrappedMTLRenderPipelineState; - ResourceId id = - GetResourceManager()->WrapResource(realMTLRenderPipelineState, wrappedMTLRenderPipelineState); + ResourceId id = GetResourceManager()->WrapResource(ResourceId(), realMTLRenderPipelineState, + wrappedMTLRenderPipelineState); if(IsCaptureMode(m_State)) { Chunk *chunk = NULL; @@ -492,8 +488,8 @@ bool WrappedMTLDevice::Serialise_newTextureWithDescriptor(SerialiserType &ser, MTL::Texture *realMTLTexture = Unwrap(this)->newTexture(mtlDescriptor); mtlDescriptor->release(); WrappedMTLTexture *wrappedMTLTexture; - ResourceId liveID = GetResourceManager()->WrapResource(realMTLTexture, wrappedMTLTexture); - GetResourceManager()->AddLiveResource(Texture, wrappedMTLTexture); + ResourceId liveID = + GetResourceManager()->WrapResource(Texture, realMTLTexture, wrappedMTLTexture); AddResource(Texture, ResourceType::Texture, "Texture"); DerivedResource(this, Texture); @@ -676,7 +672,7 @@ WrappedMTLTexture *WrappedMTLDevice::Common_NewTexture(RDMTL::TextureDescriptor realDescriptor, iosurface, plane)); realDescriptor->release(); WrappedMTLTexture *wrappedMTLTexture; - ResourceId id = GetResourceManager()->WrapResource(realMTLTexture, wrappedMTLTexture); + ResourceId id = GetResourceManager()->WrapResource(ResourceId(), realMTLTexture, wrappedMTLTexture); if(IsCaptureMode(m_State)) { RDMTL::TextureDescriptor rdDescriptor(descriptor); @@ -712,7 +708,7 @@ WrappedMTLBuffer *WrappedMTLDevice::Common_NewBuffer(bool withBytes, const void : Unwrap(this)->newBuffer(length, options)); WrappedMTLBuffer *wrappedMTLBuffer; - ResourceId id = GetResourceManager()->WrapResource(realMTLBuffer, wrappedMTLBuffer); + ResourceId id = GetResourceManager()->WrapResource(ResourceId(), realMTLBuffer, wrappedMTLBuffer); if(IsCaptureMode(m_State)) { Chunk *chunk = NULL; diff --git a/renderdoc/driver/metal/metal_library.cpp b/renderdoc/driver/metal/metal_library.cpp index 67ff119fe..3880f1915 100644 --- a/renderdoc/driver/metal/metal_library.cpp +++ b/renderdoc/driver/metal/metal_library.cpp @@ -50,8 +50,7 @@ bool WrappedMTLLibrary::Serialise_newFunctionWithName(SerialiserType &ser, { MTL::Function *realMTLFunction = Unwrap(Library)->newFunction(FunctionName); WrappedMTLFunction *wrappedMTLFunction; - GetResourceManager()->WrapResource(realMTLFunction, wrappedMTLFunction); - GetResourceManager()->AddLiveResource(Function, wrappedMTLFunction); + GetResourceManager()->WrapResource(ResourceId(), realMTLFunction, wrappedMTLFunction); m_Device->AddResource(Function, ResourceType::Shader, "Function"); m_Device->DerivedResource(Library, Function); } @@ -64,7 +63,8 @@ WrappedMTLFunction *WrappedMTLLibrary::newFunctionWithName(NS::String *functionN SERIALISE_TIME_CALL(realMTLFunction = Unwrap(this)->newFunction(functionName)); WrappedMTLFunction *wrappedMTLFunction; - ResourceId id = GetResourceManager()->WrapResource(realMTLFunction, wrappedMTLFunction); + ResourceId id = + GetResourceManager()->WrapResource(ResourceId(), realMTLFunction, wrappedMTLFunction); if(IsCaptureMode(m_State)) { diff --git a/renderdoc/driver/metal/metal_manager.h b/renderdoc/driver/metal/metal_manager.h index 769062b15..052d2823a 100644 --- a/renderdoc/driver/metal/metal_manager.h +++ b/renderdoc/driver/metal/metal_manager.h @@ -83,16 +83,14 @@ public: // if any objects leaked past, it's no longer safe to delete them as we would // be calling Shutdown() after the device that owns them is destroyed. Instead // we just have to leak ourselves. - RDCASSERT(m_LiveResourceMap.empty()); RDCASSERT(m_InitialContents.empty()); RDCASSERT(m_ResourceRecords.empty()); - RDCASSERT(m_CurrentResourceMap.empty()); + RDCASSERT(m_ResourceMap.empty()); RDCASSERT(m_WrapperMap.empty()); - m_LiveResourceMap.clear(); m_InitialContents.clear(); m_ResourceRecords.clear(); - m_CurrentResourceMap.clear(); + m_ResourceMap.clear(); m_WrapperMap.clear(); } @@ -107,16 +105,24 @@ public: // ResourceManager interface template - ResourceId WrapResource(realtype obj, typename UnwrapHelper::Outer *&wrapped) + ResourceId WrapResource(ResourceId id, realtype obj, + typename UnwrapHelper::Outer *&wrapped) { RDCASSERT(obj != NULL); RDCASSERT(m_Device != NULL); - ResourceId id = ResourceIDGen::GetNewUniqueID(); + // on replay, we provide an ID for replayed versions of capture-time resources. For + // replay-only/internal resources, we auto-gen a resource id. + // during capture we should always be auto-gen'ing a resource id for obvious reasons. + if(id == ResourceId()) + id = ResourceIDGen::GetNewUniqueID(); + else + RDCASSERT(IsReplayMode(m_State)); + using WrappedType = typename UnwrapHelper::Outer; wrapped = new WrappedType(obj, id, m_Device); wrapped->m_Real = obj; - AddCurrentResource(id, wrapped); + AddResource(id, wrapped); // TODO: implement RD MTL replay // if(IsReplayMode(m_State)) @@ -131,7 +137,7 @@ public: // TODO: implement RD MTL replay - ResourceManager::ReleaseCurrentResource(id); + ResourceManager::ReleaseResource(id); MetalResourceRecord *record = GetRecord(wrapped); if(record) { diff --git a/renderdoc/driver/metal/metal_serialise.cpp b/renderdoc/driver/metal/metal_serialise.cpp index 422da8bb2..712fdce17 100644 --- a/renderdoc/driver/metal/metal_serialise.cpp +++ b/renderdoc/driver/metal/metal_serialise.cpp @@ -47,7 +47,7 @@ void DoSerialiseViaResourceId(SerialiserType &ser, type &el) if(ser.IsWriting() && rm) id = GetResID(el); if(ser.IsStructurising() && rm) - id = rm->GetOriginalID(GetResID(el)); + id = GetResID(el); DoSerialise(ser, id); @@ -59,10 +59,10 @@ void DoSerialiseViaResourceId(SerialiserType &ser, type &el) { if(id != ResourceId() && rm) { - if(rm->HasLiveResource(id)) + if(rm->HasResource(id)) { // we leave this wrapped. - el = (type)rm->GetLiveResource(id); + el = (type)rm->GetResource(id); } } }