Optimize GPU Resource name to prevent memory allocs when changing name frequently
This commit is contained in:
committed by
Wojtek Figat
parent
70cce0e1ee
commit
67c63f1410
@@ -195,7 +195,11 @@ GPUResource::~GPUResource()
|
|||||||
{
|
{
|
||||||
#if !BUILD_RELEASE && GPU_ENABLE_RESOURCE_NAMING
|
#if !BUILD_RELEASE && GPU_ENABLE_RESOURCE_NAMING
|
||||||
if (_memoryUsage != 0)
|
if (_memoryUsage != 0)
|
||||||
LOG(Error, "{0} '{1}' has not been disposed before destruction", ScriptingObject::ToString(), _name);
|
LOG(Error, "{0} '{1}' has not been disposed before destruction", ScriptingObject::ToString(), GetName());
|
||||||
|
#endif
|
||||||
|
#if GPU_ENABLE_RESOURCE_NAMING
|
||||||
|
if (_namePtr)
|
||||||
|
Platform::Free(_namePtr);
|
||||||
#endif
|
#endif
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -208,14 +212,26 @@ static_assert((GPU_ENABLE_RESOURCE_NAMING) == (!BUILD_RELEASE), "Update build co
|
|||||||
|
|
||||||
#if GPU_ENABLE_RESOURCE_NAMING
|
#if GPU_ENABLE_RESOURCE_NAMING
|
||||||
|
|
||||||
String GPUResource::GetName() const
|
StringView GPUResource::GetName() const
|
||||||
{
|
{
|
||||||
return _name;
|
return StringView(_namePtr, _nameSize);
|
||||||
}
|
}
|
||||||
|
|
||||||
void GPUResource::SetName(const StringView& name)
|
void GPUResource::SetName(const StringView& name)
|
||||||
{
|
{
|
||||||
_name = name;
|
if (_nameCapacity < name.Length() + 1)
|
||||||
|
{
|
||||||
|
if (_namePtr)
|
||||||
|
Platform::Free(_namePtr);
|
||||||
|
_nameCapacity = name.Length() + 1;
|
||||||
|
_namePtr = (Char*)Platform::Allocate(_nameCapacity * sizeof(Char), 16);
|
||||||
|
}
|
||||||
|
_nameSize = name.Length();
|
||||||
|
if (name.HasChars())
|
||||||
|
{
|
||||||
|
Platform::MemoryCopy(_namePtr, name.Get(), _nameSize * sizeof(Char));
|
||||||
|
_namePtr[_nameSize] = 0;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
#endif
|
#endif
|
||||||
@@ -243,8 +259,8 @@ void GPUResource::OnReleaseGPU()
|
|||||||
String GPUResource::ToString() const
|
String GPUResource::ToString() const
|
||||||
{
|
{
|
||||||
#if GPU_ENABLE_RESOURCE_NAMING
|
#if GPU_ENABLE_RESOURCE_NAMING
|
||||||
if (_name.HasChars())
|
if (_namePtr)
|
||||||
return _name;
|
return String(_namePtr, _nameSize);
|
||||||
#endif
|
#endif
|
||||||
return ScriptingObject::ToString();
|
return ScriptingObject::ToString();
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -49,7 +49,8 @@ API_CLASS(Abstract, NoSpawn) class FLAXENGINE_API GPUResource : public Scripting
|
|||||||
protected:
|
protected:
|
||||||
uint64 _memoryUsage = 0;
|
uint64 _memoryUsage = 0;
|
||||||
#if GPU_ENABLE_RESOURCE_NAMING
|
#if GPU_ENABLE_RESOURCE_NAMING
|
||||||
String _name;
|
Char* _namePtr = nullptr;
|
||||||
|
int32 _nameSize = 0, _nameCapacity = 0;
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
public:
|
public:
|
||||||
@@ -95,7 +96,7 @@ public:
|
|||||||
/// <summary>
|
/// <summary>
|
||||||
/// Gets the resource name.
|
/// Gets the resource name.
|
||||||
/// </summary>
|
/// </summary>
|
||||||
API_PROPERTY() String GetName() const;
|
API_PROPERTY() StringView GetName() const;
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// Sets the resource name.
|
/// Sets the resource name.
|
||||||
@@ -149,7 +150,7 @@ public:
|
|||||||
{
|
{
|
||||||
#if GPU_ENABLE_RESOURCE_NAMING
|
#if GPU_ENABLE_RESOURCE_NAMING
|
||||||
if (name.HasChars())
|
if (name.HasChars())
|
||||||
GPUResource::_name = name;
|
GPUResource::SetName(name);
|
||||||
#endif
|
#endif
|
||||||
device->AddResource(this);
|
device->AddResource(this);
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -38,7 +38,7 @@ public:
|
|||||||
GPUSwapChain::GPUSwapChain()
|
GPUSwapChain::GPUSwapChain()
|
||||||
{
|
{
|
||||||
#if GPU_ENABLE_RESOURCE_NAMING
|
#if GPU_ENABLE_RESOURCE_NAMING
|
||||||
_name = TEXT("Swap Chain (backbuffers)");
|
SetName(TEXT("Swap Chain (backbuffers)"));
|
||||||
#endif
|
#endif
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -174,7 +174,7 @@ bool GPUBufferDX12::OnInit()
|
|||||||
if (_desc.Flags & GPUBufferFlags::Counter || _desc.Flags & GPUBufferFlags::Append)
|
if (_desc.Flags & GPUBufferFlags::Counter || _desc.Flags & GPUBufferFlags::Append)
|
||||||
{
|
{
|
||||||
#if GPU_ENABLE_RESOURCE_NAMING
|
#if GPU_ENABLE_RESOURCE_NAMING
|
||||||
String name = GetName() + TEXT(".Counter");
|
String name = String(GetName()) + TEXT(".Counter");
|
||||||
_counter = ::New<GPUBufferDX12>(_device, name);
|
_counter = ::New<GPUBufferDX12>(_device, name);
|
||||||
#else
|
#else
|
||||||
_counter = ::New<GPUBufferDX12>(_device, String::Empty);
|
_counter = ::New<GPUBufferDX12>(_device, String::Empty);
|
||||||
|
|||||||
@@ -165,7 +165,7 @@ bool GPUBufferVulkan::OnInit()
|
|||||||
if (_desc.Flags & GPUBufferFlags::Counter || _desc.Flags & GPUBufferFlags::Append)
|
if (_desc.Flags & GPUBufferFlags::Counter || _desc.Flags & GPUBufferFlags::Append)
|
||||||
{
|
{
|
||||||
#if GPU_ENABLE_RESOURCE_NAMING
|
#if GPU_ENABLE_RESOURCE_NAMING
|
||||||
String name = GetName() + TEXT(".Counter");
|
String name = String(GetName()) + TEXT(".Counter");
|
||||||
Counter = ::New<GPUBufferVulkan>(_device, name);
|
Counter = ::New<GPUBufferVulkan>(_device, name);
|
||||||
#else
|
#else
|
||||||
Counter = ::New<GPUBufferVulkan>(_device, StringView::Empty);
|
Counter = ::New<GPUBufferVulkan>(_device, StringView::Empty);
|
||||||
|
|||||||
Reference in New Issue
Block a user