Add GPUResourceUsage.Staging for both CPU read/write access

This commit is contained in:
Wojtek Figat
2025-01-30 20:39:04 +01:00
parent f11306af24
commit 44fae3838e
12 changed files with 89 additions and 21 deletions

View File

@@ -308,6 +308,17 @@ API_ENUM() enum class GPUResourceUsage
/// - Any resources read or accessed randomly on host, e.g. CPU-side copy of vertex buffer used as source of transfer, but also used for collision detection. /// - Any resources read or accessed randomly on host, e.g. CPU-side copy of vertex buffer used as source of transfer, but also used for collision detection.
/// </remarks> /// </remarks>
StagingReadback = 3, StagingReadback = 3,
/// <summary>
/// A resource that supports both read and write from the CPU.
/// This is likely to be the common choice for read-write buffers to transfer data between GPU compute buffers and CPU memory.
/// It usually means CPU (system) memory.
/// </summary>
/// <remarks>
/// Usage:
/// - Staging memory to upload to GPU for compute and gather results back after processing.
/// </remarks>
Staging = 4,
}; };
/// <summary> /// <summary>

View File

@@ -71,6 +71,15 @@ GPUBufferDescription GPUBufferDescription::ToStagingReadback() const
return desc; return desc;
} }
GPUBufferDescription GPUBufferDescription::ToStaging() const
{
auto desc = *this;
desc.Usage = GPUResourceUsage::Staging;
desc.Flags = GPUBufferFlags::None;
desc.InitData = nullptr;
return desc;
}
bool GPUBufferDescription::Equals(const GPUBufferDescription& other) const bool GPUBufferDescription::Equals(const GPUBufferDescription& other) const
{ {
return Size == other.Size return Size == other.Size
@@ -123,6 +132,16 @@ GPUBuffer::GPUBuffer()
_desc.Size = 0; _desc.Size = 0;
} }
bool GPUBuffer::IsStaging() const
{
return _desc.Usage == GPUResourceUsage::StagingReadback || _desc.Usage == GPUResourceUsage::StagingUpload || _desc.Usage == GPUResourceUsage::Staging;
}
bool GPUBuffer::IsDynamic() const
{
return _desc.Usage == GPUResourceUsage::Dynamic;
}
bool GPUBuffer::Init(const GPUBufferDescription& desc) bool GPUBuffer::Init(const GPUBufferDescription& desc)
{ {
ASSERT(Math::IsInRange<uint32>(desc.Size, 1, MAX_int32) ASSERT(Math::IsInRange<uint32>(desc.Size, 1, MAX_int32)
@@ -215,7 +234,7 @@ bool GPUBuffer::DownloadData(BytesContainer& result)
LOG(Warning, "Cannot download GPU buffer data from an empty buffer."); LOG(Warning, "Cannot download GPU buffer data from an empty buffer.");
return true; return true;
} }
if (_desc.Usage == GPUResourceUsage::StagingReadback || _desc.Usage == GPUResourceUsage::Dynamic) if (_desc.Usage == GPUResourceUsage::StagingReadback || _desc.Usage == GPUResourceUsage::Dynamic || _desc.Usage == GPUResourceUsage::Staging)
{ {
// Use faster path for staging resources // Use faster path for staging resources
return GetData(result); return GetData(result);

View File

@@ -86,20 +86,14 @@ public:
} }
/// <summary> /// <summary>
/// Checks if buffer is a staging buffer (supports CPU readback). /// Checks if buffer is a staging buffer (supports CPU access).
/// </summary> /// </summary>
API_PROPERTY() FORCE_INLINE bool IsStaging() const API_PROPERTY() bool IsStaging() const;
{
return _desc.Usage == GPUResourceUsage::StagingReadback || _desc.Usage == GPUResourceUsage::StagingUpload;
}
/// <summary> /// <summary>
/// Checks if buffer is a staging buffer (supports CPU readback). /// Checks if buffer is a dynamic buffer.
/// </summary> /// </summary>
API_PROPERTY() FORCE_INLINE bool IsDynamic() const API_PROPERTY() bool IsDynamic() const;
{
return _desc.Usage == GPUResourceUsage::Dynamic;
}
/// <summary> /// <summary>
/// Gets a value indicating whether this buffer is a shader resource. /// Gets a value indicating whether this buffer is a shader resource.
@@ -173,7 +167,7 @@ public:
Task* DownloadDataAsync(BytesContainer& result); Task* DownloadDataAsync(BytesContainer& result);
/// <summary> /// <summary>
/// Gets the buffer data via map/memcpy/unmap sequence. Always supported for dynamic and staging readback buffers (other types support depends on graphics backend implementation). /// Gets the buffer data via map/memcpy/unmap sequence. Always supported for dynamic and staging buffers (other types support depends on graphics backend implementation).
/// </summary> /// </summary>
/// <param name="output">The output data container.</param> /// <param name="output">The output data container.</param>
/// <returns>True if failed, otherwise false.</returns> /// <returns>True if failed, otherwise false.</returns>

View File

@@ -271,7 +271,7 @@ namespace FlaxEngine
} }
/// <summary> /// <summary>
/// Gets the staging upload description for this instance. /// Gets the staging upload (CPU write) description for this instance.
/// </summary> /// </summary>
/// <returns>A staging buffer description</returns> /// <returns>A staging buffer description</returns>
public GPUBufferDescription ToStagingUpload() public GPUBufferDescription ToStagingUpload()
@@ -284,7 +284,7 @@ namespace FlaxEngine
} }
/// <summary> /// <summary>
/// Gets the staging readback description for this instance. /// Gets the staging readback (CPU read) description for this instance.
/// </summary> /// </summary>
/// <returns>A staging buffer description</returns> /// <returns>A staging buffer description</returns>
public GPUBufferDescription ToStagingReadback() public GPUBufferDescription ToStagingReadback()
@@ -296,6 +296,19 @@ namespace FlaxEngine
return desc; return desc;
} }
/// <summary>
/// Gets the staging (CPU read/write) description for this instance.
/// </summary>
/// <returns>A staging buffer description</returns>
public GPUBufferDescription ToStaging()
{
var desc = this;
desc.Usage = GPUResourceUsage.Staging;
desc.Flags = GPUBufferFlags.None;
desc.InitData = IntPtr.Zero;
return desc;
}
/// <inheritdoc /> /// <inheritdoc />
public bool Equals(GPUBufferDescription other) public bool Equals(GPUBufferDescription other)
{ {

View File

@@ -335,6 +335,7 @@ public:
void Clear(); void Clear();
GPUBufferDescription ToStagingUpload() const; GPUBufferDescription ToStagingUpload() const;
GPUBufferDescription ToStagingReadback() const; GPUBufferDescription ToStagingReadback() const;
GPUBufferDescription ToStaging() const;
bool Equals(const GPUBufferDescription& other) const; bool Equals(const GPUBufferDescription& other) const;
String ToString() const; String ToString() const;

View File

@@ -142,6 +142,14 @@ GPUTextureDescription GPUTextureDescription::ToStagingReadback() const
return copy; return copy;
} }
GPUTextureDescription GPUTextureDescription::ToStaging() const
{
auto copy = *this;
copy.Flags = GPUTextureFlags::None;
copy.Usage = GPUResourceUsage::Staging;
return copy;
}
bool GPUTextureDescription::Equals(const GPUTextureDescription& other) const bool GPUTextureDescription::Equals(const GPUTextureDescription& other) const
{ {
return Dimensions == other.Dimensions return Dimensions == other.Dimensions
@@ -208,6 +216,11 @@ GPUTexture::GPUTexture()
_desc.Clear(); _desc.Clear();
} }
bool GPUTexture::IsStaging() const
{
return _desc.Usage == GPUResourceUsage::StagingUpload || _desc.Usage == GPUResourceUsage::StagingReadback || _desc.Usage == GPUResourceUsage::Staging;
}
Float2 GPUTexture::Size() const Float2 GPUTexture::Size() const
{ {
return Float2(static_cast<float>(_desc.Width), static_cast<float>(_desc.Height)); return Float2(static_cast<float>(_desc.Width), static_cast<float>(_desc.Height));

View File

@@ -294,10 +294,7 @@ public:
/// <summary> /// <summary>
/// Checks if texture is a staging buffer (supports direct CPU access). /// Checks if texture is a staging buffer (supports direct CPU access).
/// </summary> /// </summary>
FORCE_INLINE bool IsStaging() const bool IsStaging() const;
{
return _desc.Usage == GPUResourceUsage::StagingUpload || _desc.Usage == GPUResourceUsage::StagingReadback;
}
/// <summary> /// <summary>
/// Gets a boolean indicating whether this <see cref="GPUTexture"/> is a using a block compress format (BC1, BC2, BC3, BC4, BC5, BC6H, BC7, etc.). /// Gets a boolean indicating whether this <see cref="GPUTexture"/> is a using a block compress format (BC1, BC2, BC3, BC4, BC5, BC6H, BC7, etc.).

View File

@@ -300,7 +300,7 @@ namespace FlaxEngine
} }
/// <summary> /// <summary>
/// Gets the staging description for this instance. /// Gets the staging upload (CPU write) description for this instance.
/// </summary> /// </summary>
/// <returns>A staging texture description</returns> /// <returns>A staging texture description</returns>
public GPUTextureDescription ToStagingUpload() public GPUTextureDescription ToStagingUpload()
@@ -312,7 +312,7 @@ namespace FlaxEngine
} }
/// <summary> /// <summary>
/// Gets the staging description for this instance. /// Gets the staging readback (CPU read) description for this instance.
/// </summary> /// </summary>
/// <returns>A staging texture description</returns> /// <returns>A staging texture description</returns>
public GPUTextureDescription ToStagingReadback() public GPUTextureDescription ToStagingReadback()
@@ -323,6 +323,18 @@ namespace FlaxEngine
return desc; return desc;
} }
/// <summary>
/// Gets the staging (CPU read/write) description for this instance.
/// </summary>
/// <returns>A staging texture description</returns>
public GPUTextureDescription ToStaging()
{
var desc = this;
desc.Flags = GPUTextureFlags.None;
desc.Usage = GPUResourceUsage.Staging;
return desc;
}
/// <inheritdoc /> /// <inheritdoc />
public override string ToString() public override string ToString()
{ {

View File

@@ -397,6 +397,7 @@ public:
void Clear(); void Clear();
GPUTextureDescription ToStagingUpload() const; GPUTextureDescription ToStagingUpload() const;
GPUTextureDescription ToStagingReadback() const; GPUTextureDescription ToStagingReadback() const;
GPUTextureDescription ToStaging() const;
bool Equals(const GPUTextureDescription& other) const; bool Equals(const GPUTextureDescription& other) const;
String ToString() const; String ToString() const;

View File

@@ -120,6 +120,7 @@ bool GPUBufferDX12::OnInit()
switch (_desc.Usage) switch (_desc.Usage)
{ {
case GPUResourceUsage::StagingUpload: case GPUResourceUsage::StagingUpload:
case GPUResourceUsage::Staging:
heapProperties.Type = D3D12_HEAP_TYPE_UPLOAD; heapProperties.Type = D3D12_HEAP_TYPE_UPLOAD;
break; break;
case GPUResourceUsage::StagingReadback: case GPUResourceUsage::StagingReadback:
@@ -152,7 +153,7 @@ bool GPUBufferDX12::OnInit()
// But if we are doing it during update or from the other thread we have to register resource data upload job. // But if we are doing it during update or from the other thread we have to register resource data upload job.
// In both cases options.InitData data have to exist for a few next frames. // In both cases options.InitData data have to exist for a few next frames.
if (_desc.Usage == GPUResourceUsage::StagingUpload) if (_desc.Usage == GPUResourceUsage::StagingUpload || _desc.Usage == GPUResourceUsage::Staging)
{ {
// Modify staging resource data now // Modify staging resource data now
SetData(_desc.InitData, _desc.Size); SetData(_desc.InitData, _desc.Size);

View File

@@ -28,6 +28,7 @@ namespace RenderToolsDX
{ {
case GPUResourceUsage::Dynamic: case GPUResourceUsage::Dynamic:
return D3D11_USAGE_DYNAMIC; return D3D11_USAGE_DYNAMIC;
case GPUResourceUsage::Staging:
case GPUResourceUsage::StagingUpload: case GPUResourceUsage::StagingUpload:
case GPUResourceUsage::StagingReadback: case GPUResourceUsage::StagingReadback:
return D3D11_USAGE_STAGING; return D3D11_USAGE_STAGING;
@@ -47,6 +48,8 @@ namespace RenderToolsDX
{ {
case GPUResourceUsage::Dynamic: case GPUResourceUsage::Dynamic:
return D3D11_CPU_ACCESS_WRITE; return D3D11_CPU_ACCESS_WRITE;
case GPUResourceUsage::Staging:
return D3D11_CPU_ACCESS_READ | D3D11_CPU_ACCESS_WRITE;
case GPUResourceUsage::StagingReadback: case GPUResourceUsage::StagingReadback:
return D3D11_CPU_ACCESS_READ; return D3D11_CPU_ACCESS_READ;
case GPUResourceUsage::StagingUpload: case GPUResourceUsage::StagingUpload:

View File

@@ -128,6 +128,9 @@ bool GPUBufferVulkan::OnInit()
case GPUResourceUsage::StagingReadback: case GPUResourceUsage::StagingReadback:
allocInfo.usage = VMA_MEMORY_USAGE_GPU_TO_CPU; allocInfo.usage = VMA_MEMORY_USAGE_GPU_TO_CPU;
break; break;
case GPUResourceUsage::Staging:
allocInfo.usage = VMA_MEMORY_USAGE_CPU_COPY;
break;
default: default:
allocInfo.usage = VMA_MEMORY_USAGE_GPU_ONLY; allocInfo.usage = VMA_MEMORY_USAGE_GPU_ONLY;
} }