From ffbf8f0fd938b77b31a83d58d3f7b3493e3e54f8 Mon Sep 17 00:00:00 2001
From: Philip Rebohle <philip.rebohle@tu-dortmund.de>
Date: Tue, 7 Jan 2025 14:55:36 +0100
Subject: [PATCH] [d3d11] Support debug names for buffers and textures

Co-authored-by: Aaron Leiby <aleiby@gmail.com>
---
 src/d3d11/d3d11_buffer.cpp     | 13 ++++++++++
 src/d3d11/d3d11_buffer.h       |  2 ++
 src/d3d11/d3d11_device_child.h | 24 +++++++++++------
 src/d3d11/d3d11_texture.cpp    | 47 +++++++++++++++++++++++++++++++---
 src/d3d11/d3d11_texture.h      | 14 ++++++++++
 5 files changed, 88 insertions(+), 12 deletions(-)

diff --git a/src/d3d11/d3d11_buffer.cpp b/src/d3d11/d3d11_buffer.cpp
index fad81782e..22aa6d715 100644
--- a/src/d3d11/d3d11_buffer.cpp
+++ b/src/d3d11/d3d11_buffer.cpp
@@ -1,5 +1,6 @@
 #include "d3d11_buffer.h"
 #include "d3d11_context.h"
+#include "d3d11_context_imm.h"
 #include "d3d11_device.h"
 
 namespace dxvk {
@@ -211,6 +212,18 @@ namespace dxvk {
   }
 
 
+  void D3D11Buffer::SetDebugName(const char* pName) {
+    if (m_buffer) {
+      m_parent->GetContext()->InjectCs([
+        cBuffer = m_buffer,
+        cName   = std::string(pName ? pName : "")
+      ] (DxvkContext* ctx) {
+        ctx->setDebugName(cBuffer, cName.c_str());
+      });
+    }
+  }
+
+
   HRESULT D3D11Buffer::NormalizeBufferProperties(D3D11_BUFFER_DESC* pDesc) {
     // Zero-sized buffers are illegal
     if (!pDesc->ByteWidth && !(pDesc->MiscFlags & D3D11_RESOURCE_MISC_TILE_POOL))
diff --git a/src/d3d11/d3d11_buffer.h b/src/d3d11/d3d11_buffer.h
index b65df66a6..af71a5d0f 100644
--- a/src/d3d11/d3d11_buffer.h
+++ b/src/d3d11/d3d11_buffer.h
@@ -61,6 +61,8 @@ namespace dxvk {
     void STDMETHODCALLTYPE GetDesc(
             D3D11_BUFFER_DESC *pDesc) final;
     
+    void STDMETHODCALLTYPE SetDebugName(const char* pName) final;
+
     bool CheckViewCompatibility(
             UINT                BindFlags,
             DXGI_FORMAT         Format) const;
diff --git a/src/d3d11/d3d11_device_child.h b/src/d3d11/d3d11_device_child.h
index 60eff07af..0b14f684e 100644
--- a/src/d3d11/d3d11_device_child.h
+++ b/src/d3d11/d3d11_device_child.h
@@ -19,24 +19,28 @@ namespace dxvk {
     }
     
     HRESULT STDMETHODCALLTYPE GetPrivateData(
-            REFGUID guid,
-            UINT    *pDataSize,
-            void    *pData) final {
+            REFGUID               guid,
+            UINT*                 pDataSize,
+            void*                 pData) final {
       return m_privateData.getData(
         guid, pDataSize, pData);
     }
     
     HRESULT STDMETHODCALLTYPE SetPrivateData(
-            REFGUID guid,
-            UINT    DataSize,
-      const void    *pData) final {
+            REFGUID               guid,
+            UINT                  DataSize,
+      const void*                 pData) final {
+      // WKPDID_D3DDebugObjectName, can't use directly due to MSVC link errors
+      if (guid == GUID{0x429b8c22,0x9188,0x4b0c,0x87,0x42,0xac,0xb0,0xbf,0x85,0xc2,0x00})
+        SetDebugName(static_cast<const char*>(pData));
+
       return m_privateData.setData(
         guid, DataSize, pData);
     }
     
     HRESULT STDMETHODCALLTYPE SetPrivateDataInterface(
-            REFGUID  guid,
-      const IUnknown *pUnknown) final {
+            REFGUID               guid,
+      const IUnknown*             pUnknown) final {
       return m_privateData.setInterface(
         guid, pUnknown);
     }
@@ -46,6 +50,10 @@ namespace dxvk {
       *ppDevice = ref(GetParentInterface());
     }
 
+    virtual void STDMETHODCALLTYPE SetDebugName(const char* pName) {
+      // No-op by default
+    }
+
   protected:
 
     ID3D11Device* GetParentInterface() const {
diff --git a/src/d3d11/d3d11_texture.cpp b/src/d3d11/d3d11_texture.cpp
index 653c78cec..781c25960 100644
--- a/src/d3d11/d3d11_texture.cpp
+++ b/src/d3d11/d3d11_texture.cpp
@@ -1,4 +1,5 @@
 #include "d3d11_device.h"
+#include "d3d11_context_imm.h"
 #include "d3d11_gdi.h"
 #include "d3d11_texture.h"
 
@@ -372,8 +373,31 @@ namespace dxvk {
       return viewFormat.Format == baseFormat.Format && planeCount == 1;
     }
   }
-  
-  
+
+
+  void D3D11CommonTexture::SetDebugName(const char* pName) {
+    if (m_image) {
+      m_device->GetContext()->InjectCs([
+        cImage  = m_image,
+        cName   = std::string(pName ? pName : "")
+      ] (DxvkContext* ctx) {
+        ctx->setDebugName(cImage, cName.c_str());
+      });
+    }
+
+    if (m_mapMode == D3D11_COMMON_TEXTURE_MAP_MODE_STAGING) {
+      for (uint32_t i = 0; i < m_buffers.size(); i++) {
+        m_device->GetContext()->InjectCs([
+          cBuffer = m_buffers[i].buffer,
+          cName   = std::string(pName ? pName : "")
+        ] (DxvkContext* ctx) {
+          ctx->setDebugName(cBuffer, cName.c_str());
+        });
+      }
+    }
+  }
+
+
   HRESULT D3D11CommonTexture::NormalizeTextureProperties(D3D11_COMMON_TEXTURE_DESC* pDesc) {
     if (pDesc->Width == 0 || pDesc->Height == 0 || pDesc->Depth == 0 || pDesc->ArraySize == 0)
       return E_INVALIDARG;
@@ -1194,8 +1218,13 @@ namespace dxvk {
     pDesc->CPUAccessFlags = m_texture.Desc()->CPUAccessFlags;
     pDesc->MiscFlags      = m_texture.Desc()->MiscFlags;
   }
-  
-  
+
+
+  void STDMETHODCALLTYPE D3D11Texture1D::SetDebugName(const char* pName) {
+    m_texture.SetDebugName(pName);
+  }
+
+
   ///////////////////////////////////////////
   //      D 3 D 1 1 T E X T U R E 2 D
   D3D11Texture2D::D3D11Texture2D(
@@ -1377,6 +1406,11 @@ namespace dxvk {
   }
   
   
+  void STDMETHODCALLTYPE D3D11Texture2D::SetDebugName(const char* pName) {
+    m_texture.SetDebugName(pName);
+  }
+
+
   ///////////////////////////////////////////
   //      D 3 D 1 1 T E X T U R E 3 D
   D3D11Texture3D::D3D11Texture3D(
@@ -1488,6 +1522,11 @@ namespace dxvk {
   }
   
   
+  void STDMETHODCALLTYPE D3D11Texture3D::SetDebugName(const char* pName) {
+    m_texture.SetDebugName(pName);
+  }
+
+
   D3D11CommonTexture* GetCommonTexture(ID3D11Resource* pResource) {
     D3D11_RESOURCE_DIMENSION dimension = D3D11_RESOURCE_DIMENSION_UNKNOWN;
     pResource->GetType(&dimension);
diff --git a/src/d3d11/d3d11_texture.h b/src/d3d11/d3d11_texture.h
index 5e799afc1..4b28253d9 100644
--- a/src/d3d11/d3d11_texture.h
+++ b/src/d3d11/d3d11_texture.h
@@ -525,6 +525,14 @@ namespace dxvk {
       return m_11on12;
     }
 
+    /**
+     * \brief Sets debug name for texture
+     *
+     * Passes the given name to the backing image or buffer.
+     * \param [in] name Debug name
+     */
+    void SetDebugName(const char* pName);
+
     /**
      * \brief Normalizes and validates texture description
      * 
@@ -759,6 +767,8 @@ namespace dxvk {
     void STDMETHODCALLTYPE GetDesc(
             D3D11_TEXTURE1D_DESC *pDesc) final;
     
+    void STDMETHODCALLTYPE SetDebugName(const char* pName) final;
+
     D3D11CommonTexture* GetCommonTexture() {
       return &m_texture;
     }
@@ -825,6 +835,8 @@ namespace dxvk {
     void STDMETHODCALLTYPE GetDesc1(
             D3D11_TEXTURE2D_DESC1* pDesc) final;
     
+    void STDMETHODCALLTYPE SetDebugName(const char* pName) final;
+
     D3D11CommonTexture* GetCommonTexture() {
       return &m_texture;
     }
@@ -875,6 +887,8 @@ namespace dxvk {
     void STDMETHODCALLTYPE GetDesc1(
             D3D11_TEXTURE3D_DESC1* pDesc) final;
     
+    void STDMETHODCALLTYPE SetDebugName(const char* pName) final;
+
     D3D11CommonTexture* GetCommonTexture() {
       return &m_texture;
     }