Add support for renaming GPU resources (development builds only)

This commit is contained in:
Wojtek Figat
2022-06-22 23:08:39 +02:00
parent 5d34575a91
commit 1dab45f407
7 changed files with 49 additions and 41 deletions

View File

@@ -184,8 +184,9 @@ GPUResource::GPUResource(const SpawnParams& params)
GPUResource::~GPUResource()
{
#if !BUILD_RELEASE
ASSERT(_memoryUsage == 0);
#if !BUILD_RELEASE && GPU_ENABLE_RESOURCE_NAMING
if (_memoryUsage != 0)
LOG(Error, "{0} '{1}' has not been disposed before destruction", ScriptingObject::ToString(), _name);
#endif
}
@@ -203,7 +204,12 @@ uint64 GPUResource::GetMemoryUsage() const
String GPUResource::GetName() const
{
return String::Empty;
return _name;
}
void GPUResource::SetName(const StringView& name)
{
_name = name;
}
#endif
@@ -231,10 +237,10 @@ void GPUResource::OnReleaseGPU()
String GPUResource::ToString() const
{
#if GPU_ENABLE_RESOURCE_NAMING
return GetName();
#else
return TEXT("GPU Resource");
if (_name.HasChars())
return _name;
#endif
return ScriptingObject::ToString();
}
void GPUResource::OnDeleteObject()

View File

@@ -31,6 +31,9 @@ public:
protected:
uint64 _memoryUsage = 0;
#if GPU_ENABLE_RESOURCE_NAMING
String _name;
#endif
public:
NON_COPYABLE(GPUResource);
@@ -77,12 +80,15 @@ public:
API_PROPERTY() uint64 GetMemoryUsage() const;
#if GPU_ENABLE_RESOURCE_NAMING
/// <summary>
/// Gets the resource name.
/// </summary>
virtual String GetName() const;
API_PROPERTY() String GetName() const;
/// <summary>
/// Sets the resource name.
/// </summary>
API_PROPERTY() void SetName(const StringView& name);
#endif
/// <summary>
@@ -119,11 +125,6 @@ class GPUResourceBase : public BaseType
protected:
DeviceType* _device;
private:
#if GPU_ENABLE_RESOURCE_NAMING
String _name;
#endif
public:
/// <summary>
/// Initializes a new instance of the <see cref="GPUResourceBase"/> class.
@@ -132,10 +133,10 @@ public:
/// <param name="name">The resource name.</param>
GPUResourceBase(DeviceType* device, const StringView& name) noexcept
: _device(device)
#if GPU_ENABLE_RESOURCE_NAMING
, _name(name.Get(), name.Length())
#endif
{
#if GPU_ENABLE_RESOURCE_NAMING
GPUResource::_name = name;
#endif
device->Resources.Add(this);
}
@@ -159,12 +160,6 @@ public:
public:
// [GPUResource]
#if GPU_ENABLE_RESOURCE_NAMING
String GetName() const override
{
return _name;
}
#endif
void OnDeviceDispose() override
{
GPUResource::OnDeviceDispose();

View File

@@ -42,7 +42,7 @@ Task* GPUSwapChain::DownloadDataAsync(TextureData& result)
return nullptr;
}
auto texture = GPUDevice::Instance->CreateTexture(String::Empty);
auto texture = GPUDevice::Instance->CreateTexture();
if (texture->Init(GPUTextureDescription::New2D(GetWidth(), GetHeight(), GetFormat(), GPUTextureFlags::None, 1).ToStagingReadback()))
{
LOG(Warning, "Failed to create staging texture for the window swapchain backuffer download.");
@@ -84,3 +84,17 @@ void GPUSwapChain::Present(bool vsync)
// Count amount of present calls
_presentCount++;
}
String GPUSwapChain::ToString() const
{
#if GPU_ENABLE_RESOURCE_NAMING
return String::Format(TEXT("SwapChain {0}x{1}, {2}"), GetWidth(), GetHeight(), GetName());
#else
return TEXT("SwapChain");
#endif
}
GPUResource::ResourceType GPUSwapChain::GetResourceType() const
{
return ResourceType::Texture;
}

View File

@@ -159,14 +159,6 @@ public:
public:
// [GPUResource]
ResourceType GetResourceType() const final override
{
return ResourceType::Texture;
}
#if GPU_ENABLE_RESOURCE_NAMING
String GetName() const override
{
return String::Format(TEXT("RenderOutput {0}x{1}"), GetWidth(), GetHeight());
}
#endif
String ToString() const override;
ResourceType GetResourceType() const final override;
};

View File

@@ -211,12 +211,12 @@ uint32 GetHash(const GPUTextureDescription& key)
GPUTexture* GPUTexture::Spawn(const SpawnParams& params)
{
return GPUDevice::Instance->CreateTexture(String::Empty);
return GPUDevice::Instance->CreateTexture();
}
GPUTexture* GPUTexture::New()
{
return GPUDevice::Instance->CreateTexture(String::Empty);
return GPUDevice::Instance->CreateTexture();
}
GPUTexture::GPUTexture()

View File

@@ -267,7 +267,7 @@ Task* StreamingTexture::UpdateAllocation(int32 residency)
#if GPU_ENABLE_RESOURCE_NAMING
texture = GPUDevice::Instance->CreateTexture(_texture->GetName());
#else
texture = GPUDevice::Instance->CreateTexture(String::Empty);
texture = GPUDevice::Instance->CreateTexture();
#endif
}

View File

@@ -213,18 +213,19 @@ namespace RenderToolsDX
{
HRESULT reason = S_OK;
const RendererType rendererType = GPUDevice::Instance ? GPUDevice::Instance->GetRendererType() : RendererType::Unknown;
void* nativePtr = GPUDevice::Instance ? GPUDevice::Instance->GetNativePtr() : nullptr;
#if GRAPHICS_API_DIRECTX12
if (rendererType == RendererType::DirectX12)
if (rendererType == RendererType::DirectX12 && nativePtr)
{
reason = ((ID3D12Device*)GPUDevice::Instance->GetNativePtr())->GetDeviceRemovedReason();
reason = ((ID3D12Device*)nativePtr)->GetDeviceRemovedReason();
}
#endif
#if GRAPHICS_API_DIRECTX11
if (rendererType == RendererType::DirectX11 ||
if ((rendererType == RendererType::DirectX11 ||
rendererType == RendererType::DirectX10_1 ||
rendererType == RendererType::DirectX10)
rendererType == RendererType::DirectX10) && nativePtr)
{
reason = ((ID3D11Device*)GPUDevice::Instance->GetNativePtr())->GetDeviceRemovedReason();
reason = ((ID3D11Device*)nativePtr)->GetDeviceRemovedReason();
}
#endif
const Char* reasonStr = nullptr;