From 344898b091867e5450a3fa432a207d75255df77a Mon Sep 17 00:00:00 2001 From: Yong He Date: Wed, 12 Oct 2022 14:51:13 -0700 Subject: Add gfx debug layer trampoline for D3D12 interfaces. (#2445) Co-authored-by: Yong He --- tools/gfx/d3d12/d3d12-transient-heap.cpp | 4 +- tools/gfx/d3d12/d3d12-transient-heap.h | 2 +- tools/gfx/debug-layer/debug-command-buffer.cpp | 33 ++++++++++++++++ tools/gfx/debug-layer/debug-command-buffer.h | 5 ++- tools/gfx/debug-layer/debug-helper-functions.cpp | 2 - tools/gfx/debug-layer/debug-helper-functions.h | 39 +++++++++++++------ tools/gfx/debug-layer/debug-transient-heap.cpp | 48 +++++++++++++++++++++++- tools/gfx/debug-layer/debug-transient-heap.h | 20 +++++++++- tools/gfx/renderer-shared.cpp | 2 +- tools/gfx/renderer-shared.h | 2 +- 10 files changed, 134 insertions(+), 23 deletions(-) (limited to 'tools') diff --git a/tools/gfx/d3d12/d3d12-transient-heap.cpp b/tools/gfx/d3d12/d3d12-transient-heap.cpp index e33aa473c..11b03dc12 100644 --- a/tools/gfx/d3d12/d3d12-transient-heap.cpp +++ b/tools/gfx/d3d12/d3d12-transient-heap.cpp @@ -81,9 +81,9 @@ D3D12DescriptorHeap& TransientResourceHeapImpl::getCurrentSamplerHeap() Result TransientResourceHeapImpl::queryInterface(SlangUUID const& uuid, void** outObject) { - if (uuid == GfxGUID::IID_ID3D12TransientResourceHeap) + if (uuid == GfxGUID::IID_ITransientResourceHeapD3D12) { - *outObject = static_cast(this); + *outObject = static_cast(this); addRef(); return SLANG_OK; } diff --git a/tools/gfx/d3d12/d3d12-transient-heap.h b/tools/gfx/d3d12/d3d12-transient-heap.h index 8d9ac261a..6b05367e8 100644 --- a/tools/gfx/d3d12/d3d12-transient-heap.h +++ b/tools/gfx/d3d12/d3d12-transient-heap.h @@ -12,7 +12,7 @@ using namespace Slang; class TransientResourceHeapImpl : public TransientResourceHeapBaseImpl - , public ID3D12TransientResourceHeap + , public ITransientResourceHeapD3D12 { private: typedef TransientResourceHeapBaseImpl Super; diff --git a/tools/gfx/debug-layer/debug-command-buffer.cpp b/tools/gfx/debug-layer/debug-command-buffer.cpp index aa9c6b8b8..f3c71cb77 100644 --- a/tools/gfx/debug-layer/debug-command-buffer.cpp +++ b/tools/gfx/debug-layer/debug-command-buffer.cpp @@ -22,6 +22,15 @@ DebugCommandBuffer::DebugCommandBuffer() m_rayTracingCommandEncoder.commandBuffer = this; } +ICommandBuffer* DebugCommandBuffer::getInterface(const Slang::Guid& guid) +{ + if (guid == GfxGUID::IID_ICommandBuffer || guid == GfxGUID::IID_ISlangUnknown) + return (DebugObject*)this; + if (guid == GfxGUID::IID_ICommandBufferD3D12) + return static_cast(this); + return nullptr; +} + void DebugCommandBuffer::encodeRenderCommands( IRenderPassLayout* renderPass, IFramebuffer* framebuffer, @@ -127,6 +136,30 @@ Result DebugCommandBuffer::getNativeHandle(InteropHandle* outHandle) return baseObject->getNativeHandle(outHandle); } +void DebugCommandBuffer::invalidateDescriptorHeapBinding() +{ + SLANG_GFX_API_FUNC; + ComPtr cmdBuf; + if (SLANG_FAILED(baseObject->queryInterface(SlangUUID SLANG_UUID_ICommandBufferD3D12, (void**)cmdBuf.writeRef()))) + { + GFX_DIAGNOSE_ERROR("The current command buffer implementation does not provide ICommandBufferD3D12 interface."); + return; + } + return cmdBuf->invalidateDescriptorHeapBinding(); +} + +void DebugCommandBuffer::ensureInternalDescriptorHeapsBound() +{ + SLANG_GFX_API_FUNC; + ComPtr cmdBuf; + if (SLANG_FAILED(baseObject->queryInterface(SlangUUID SLANG_UUID_ICommandBufferD3D12, (void**)cmdBuf.writeRef()))) + { + GFX_DIAGNOSE_ERROR("The current command buffer implementation does not provide ICommandBufferD3D12 interface."); + return; + } + return cmdBuf->ensureInternalDescriptorHeapsBound(); +} + void DebugCommandBuffer::checkEncodersClosedBeforeNewEncoder() { if (m_renderCommandEncoder.isOpen || m_resourceCommandEncoder.isOpen || diff --git a/tools/gfx/debug-layer/debug-command-buffer.h b/tools/gfx/debug-layer/debug-command-buffer.h index d7633960b..33709fb2c 100644 --- a/tools/gfx/debug-layer/debug-command-buffer.h +++ b/tools/gfx/debug-layer/debug-command-buffer.h @@ -12,14 +12,13 @@ using namespace Slang; namespace debug { -class DebugCommandBuffer : public DebugObject +class DebugCommandBuffer : public DebugObject, ICommandBufferD3D12 { public: SLANG_COM_OBJECT_IUNKNOWN_ALL; public: DebugTransientResourceHeap* m_transientHeap; - private: DebugRenderCommandEncoder m_renderCommandEncoder; DebugComputeCommandEncoder m_computeCommandEncoder; @@ -41,6 +40,8 @@ public: encodeRayTracingCommands(IRayTracingCommandEncoder** outEncoder) override; virtual SLANG_NO_THROW void SLANG_MCALL close() override; virtual SLANG_NO_THROW Result SLANG_MCALL getNativeHandle(InteropHandle* outHandle) override; + virtual SLANG_NO_THROW void SLANG_MCALL invalidateDescriptorHeapBinding() override; + virtual SLANG_NO_THROW void SLANG_MCALL ensureInternalDescriptorHeapsBound() override; private: void checkEncodersClosedBeforeNewEncoder(); diff --git a/tools/gfx/debug-layer/debug-helper-functions.cpp b/tools/gfx/debug-layer/debug-helper-functions.cpp index 0d2c1d110..10dcedf2a 100644 --- a/tools/gfx/debug-layer/debug-helper-functions.cpp +++ b/tools/gfx/debug-layer/debug-helper-functions.cpp @@ -13,7 +13,6 @@ thread_local const char* _currentFunctionName = nullptr; SLANG_GFX_DEBUG_GET_INTERFACE_IMPL(Device) SLANG_GFX_DEBUG_GET_INTERFACE_IMPL_PARENT(BufferResource, Resource) SLANG_GFX_DEBUG_GET_INTERFACE_IMPL_PARENT(TextureResource, Resource) -SLANG_GFX_DEBUG_GET_INTERFACE_IMPL(CommandBuffer) SLANG_GFX_DEBUG_GET_INTERFACE_IMPL(CommandQueue) SLANG_GFX_DEBUG_GET_INTERFACE_IMPL(Framebuffer) SLANG_GFX_DEBUG_GET_INTERFACE_IMPL(FramebufferLayout) @@ -25,7 +24,6 @@ SLANG_GFX_DEBUG_GET_INTERFACE_IMPL(SamplerState) SLANG_GFX_DEBUG_GET_INTERFACE_IMPL(ShaderObject) SLANG_GFX_DEBUG_GET_INTERFACE_IMPL(ShaderProgram) SLANG_GFX_DEBUG_GET_INTERFACE_IMPL(Swapchain) -SLANG_GFX_DEBUG_GET_INTERFACE_IMPL(TransientResourceHeap) SLANG_GFX_DEBUG_GET_INTERFACE_IMPL(QueryPool) SLANG_GFX_DEBUG_GET_INTERFACE_IMPL_PARENT(AccelerationStructure, ResourceView) SLANG_GFX_DEBUG_GET_INTERFACE_IMPL(Fence) diff --git a/tools/gfx/debug-layer/debug-helper-functions.h b/tools/gfx/debug-layer/debug-helper-functions.h index 1842cf458..f2edd0d84 100644 --- a/tools/gfx/debug-layer/debug-helper-functions.h +++ b/tools/gfx/debug-layer/debug-helper-functions.h @@ -131,13 +131,30 @@ void _gfxDiagnoseImpl(DebugMessageType type, const char* format, TArgs... args) // Utility conversion functions to get Debug* object or the inner object from a user provided // pointer. -#define SLANG_GFX_DEBUG_GET_OBJ_IMPL(type) \ - inline Debug##type* getDebugObj(I##type* ptr) { return static_cast(ptr); } \ - inline I##type* getInnerObj(I##type* ptr) \ - { \ - if (!ptr) return nullptr; \ - auto debugObj = getDebugObj(ptr); \ - return debugObj->baseObject; \ +#define SLANG_GFX_DEBUG_GET_OBJ_IMPL(type) \ + inline Debug##type* getDebugObj(I##type* ptr) \ + { \ + return static_cast(static_cast*>(ptr)); \ + } \ + inline I##type* getInnerObj(I##type* ptr) \ + { \ + if (!ptr) \ + return nullptr; \ + auto debugObj = getDebugObj(ptr); \ + return debugObj->baseObject; \ + } + +#define SLANG_GFX_DEBUG_GET_OBJ_IMPL_UNOWNED(type) \ + inline Debug##type* getDebugObj(I##type* ptr) \ + { \ + return static_cast(static_cast*>(ptr)); \ + } \ + inline I##type* getInnerObj(I##type* ptr) \ + { \ + if (!ptr) \ + return nullptr; \ + auto debugObj = getDebugObj(ptr); \ + return debugObj->baseObject; \ } SLANG_GFX_DEBUG_GET_OBJ_IMPL(Device) @@ -145,10 +162,10 @@ SLANG_GFX_DEBUG_GET_OBJ_IMPL(BufferResource) SLANG_GFX_DEBUG_GET_OBJ_IMPL(TextureResource) SLANG_GFX_DEBUG_GET_OBJ_IMPL(CommandBuffer) SLANG_GFX_DEBUG_GET_OBJ_IMPL(CommandQueue) -SLANG_GFX_DEBUG_GET_OBJ_IMPL(ComputeCommandEncoder) -SLANG_GFX_DEBUG_GET_OBJ_IMPL(RenderCommandEncoder) -SLANG_GFX_DEBUG_GET_OBJ_IMPL(ResourceCommandEncoder) -SLANG_GFX_DEBUG_GET_OBJ_IMPL(RayTracingCommandEncoder) +SLANG_GFX_DEBUG_GET_OBJ_IMPL_UNOWNED(ComputeCommandEncoder) +SLANG_GFX_DEBUG_GET_OBJ_IMPL_UNOWNED(RenderCommandEncoder) +SLANG_GFX_DEBUG_GET_OBJ_IMPL_UNOWNED(ResourceCommandEncoder) +SLANG_GFX_DEBUG_GET_OBJ_IMPL_UNOWNED(RayTracingCommandEncoder) SLANG_GFX_DEBUG_GET_OBJ_IMPL(Framebuffer) SLANG_GFX_DEBUG_GET_OBJ_IMPL(FramebufferLayout) SLANG_GFX_DEBUG_GET_OBJ_IMPL(InputLayout) diff --git a/tools/gfx/debug-layer/debug-transient-heap.cpp b/tools/gfx/debug-layer/debug-transient-heap.cpp index 91e575742..84861297a 100644 --- a/tools/gfx/debug-layer/debug-transient-heap.cpp +++ b/tools/gfx/debug-layer/debug-transient-heap.cpp @@ -12,6 +12,23 @@ using namespace Slang; namespace debug { +SlangResult DebugTransientResourceHeap::queryInterface(SlangUUID const& uuid, void** outObject) +{ + if (uuid == GfxGUID::IID_ISlangUnknown || uuid == GfxGUID::IID_ITransientResourceHeap) + *outObject = static_cast(this); + if (uuid == GfxGUID::IID_ITransientResourceHeapD3D12) + { + RefPtr result = new DebugTransientResourceHeapD3D12(); + baseObject->queryInterface(uuid, (void**)result->baseObject.writeRef()); + returnComPtr((ITransientResourceHeapD3D12**)outObject, result); + return SLANG_OK; + } + else + { + return baseObject->queryInterface(uuid, outObject); + } +} + Result DebugTransientResourceHeap::synchronizeAndReset() { SLANG_GFX_API_FUNC; @@ -32,9 +49,38 @@ Result DebugTransientResourceHeap::createCommandBuffer(ICommandBuffer** outComma auto result = baseObject->createCommandBuffer(outObject->baseObject.writeRef()); if (SLANG_FAILED(result)) return result; - returnComPtr(outCommandBuffer, outObject); + outObject->queryInterface(SlangUUID SLANG_UUID_ICommandBuffer, (void**)outCommandBuffer); return result; } +SlangResult DebugTransientResourceHeapD3D12::queryInterface(SlangUUID const& uuid, void** outObject) +{ + if (uuid == GfxGUID::IID_ISlangUnknown || uuid == GfxGUID::IID_ITransientResourceHeapD3D12) + *outObject = static_cast(this); + if (uuid == GfxGUID::IID_ITransientResourceHeap) + { + RefPtr result = new DebugTransientResourceHeap(); + baseObject->queryInterface(uuid, (void**)result->baseObject.writeRef()); + returnComPtr((ITransientResourceHeap**)outObject, result); + return SLANG_OK; + } + else + { + return baseObject->queryInterface(uuid, outObject); + } +} + +Result DebugTransientResourceHeapD3D12::allocateTransientDescriptorTable( + DescriptorType type, + GfxCount count, + Offset& outDescriptorOffset, + void** outD3DDescriptorHeapHandle) +{ + SLANG_GFX_API_FUNC; + + return baseObject->allocateTransientDescriptorTable( + type, count, outDescriptorOffset, outD3DDescriptorHeapHandle); +} + } // namespace debug } // namespace gfx diff --git a/tools/gfx/debug-layer/debug-transient-heap.h b/tools/gfx/debug-layer/debug-transient-heap.h index dd89d8a21..60430228d 100644 --- a/tools/gfx/debug-layer/debug-transient-heap.h +++ b/tools/gfx/debug-layer/debug-transient-heap.h @@ -12,15 +12,31 @@ namespace debug class DebugTransientResourceHeap : public DebugObject { public: - SLANG_COM_OBJECT_IUNKNOWN_ALL; + SLANG_COM_OBJECT_IUNKNOWN_ADD_REF; + SLANG_COM_OBJECT_IUNKNOWN_RELEASE; public: - ITransientResourceHeap* getInterface(const Slang::Guid& guid); + virtual SLANG_NO_THROW SlangResult SLANG_MCALL queryInterface(SlangUUID const& uuid, void** outObject) override; + virtual SLANG_NO_THROW Result SLANG_MCALL synchronizeAndReset() override; virtual SLANG_NO_THROW Result SLANG_MCALL finish() override; virtual SLANG_NO_THROW Result SLANG_MCALL createCommandBuffer(ICommandBuffer** outCommandBuffer) override; }; +class DebugTransientResourceHeapD3D12 : public DebugObject +{ +public: + SLANG_COM_OBJECT_IUNKNOWN_ADD_REF; + SLANG_COM_OBJECT_IUNKNOWN_RELEASE; +public: + virtual SLANG_NO_THROW SlangResult SLANG_MCALL queryInterface(SlangUUID const& uuid, void** outObject) override; + virtual SLANG_NO_THROW Result SLANG_MCALL allocateTransientDescriptorTable( + DescriptorType type, + GfxCount count, + Offset& outDescriptorOffset, + void** outD3DDescriptorHeapHandle) override; +}; + } // namespace debug } // namespace gfx diff --git a/tools/gfx/renderer-shared.cpp b/tools/gfx/renderer-shared.cpp index 8ed776776..f76fa2325 100644 --- a/tools/gfx/renderer-shared.cpp +++ b/tools/gfx/renderer-shared.cpp @@ -46,7 +46,7 @@ const Slang::Guid GfxGUID::IID_IAccelerationStructure = SLANG_UUID_IAcceleration const Slang::Guid GfxGUID::IID_IFence = SLANG_UUID_IFence; const Slang::Guid GfxGUID::IID_IShaderTable = SLANG_UUID_IShaderTable; const Slang::Guid GfxGUID::IID_IPipelineCreationAPIDispatcher = SLANG_UUID_IPipelineCreationAPIDispatcher; -const Slang::Guid GfxGUID::IID_ID3D12TransientResourceHeap = SLANG_UUID_ID3D12TransientResourceHeap; +const Slang::Guid GfxGUID::IID_ITransientResourceHeapD3D12 = SLANG_UUID_ITransientResourceHeapD3D12; StageType translateStage(SlangStage slangStage) diff --git a/tools/gfx/renderer-shared.h b/tools/gfx/renderer-shared.h index a753fe017..b140e1f40 100644 --- a/tools/gfx/renderer-shared.h +++ b/tools/gfx/renderer-shared.h @@ -43,7 +43,7 @@ struct GfxGUID static const Slang::Guid IID_IFence; static const Slang::Guid IID_IShaderTable; static const Slang::Guid IID_IPipelineCreationAPIDispatcher; - static const Slang::Guid IID_ID3D12TransientResourceHeap; + static const Slang::Guid IID_ITransientResourceHeapD3D12; }; bool isGfxDebugLayerEnabled(); -- cgit v1.2.3