Foundation
Loading...
Searching...
No Matches
StagingBuffer.hpp
Go to the documentation of this file.
1#pragma once
2#include <Core/Allocator.hpp>
4
6{
7 using namespace RHI;
8 using namespace Core;
9 using namespace RenderCore;
10 // [Source Buffer (staging), Dest Buffer, CopyRegion]
18 {
22 void* mMapped;
23 size_t mSize;
24 size_t mOffset = 0;
25
26 public:
32 size_t Write(Span<const char> data, size_t alignment);
36 void Seek(size_t offset, size_t alignment);
41 [[nodiscard]] size_t Tell() const { return mOffset; }
42 [[nodiscard]] size_t Size() const { return mSize; }
43 [[nodiscard]] size_t FreeSize() const { return mSize - mOffset; }
44 void Reset();
45
46 [[nodiscard]] RHIBuffer* GetBuffer() const { return mBuffer.Get(); }
47 };
71 class StagedBuffer : public RHIObject /* pinned */
72 {
73 public:
74 enum class State
75 {
76 Idle,
78 };
79
80 private:
81 RHIDevice* mDevice{nullptr};
83
85 // Destination buffer
87
90
93
94 public:
108 RHIBuffer* GetBuffer() { return mBuffer.Get(); }
115 void BeginTransfer();
130 void Transfer(size_t dst_offset, Span<const char> data, size_t alignment = 4);
134 void EndTransfer();
138 bool HasUpdates() { return mClearValue.has_value() || !mBufferStagings.empty(); }
151 };
153 ENUM_NAME(Idle)
156} // namespace Foundation::Rendering
#define ENUM_NAME_CONV_BEGIN(T)
Defines convince to_string() method and format_as() [fmt] for the respective enum class Example usage...
Definition Enums.hpp:84
#define ENUM_NAME(E)
Definition Enums.hpp:95
General Purpose Allocator (GPA) interface.
Definition Allocator.hpp:24
Definition Resource.hpp:30
Definition Command.hpp:34
Definition Device.hpp:131
Base class for all RHI objects.
Definition Details.hpp:16
Helper class for GPU buffer updates.
Definition StagingBuffer.hpp:72
StagingBuffer mStagingBuffer
Definition StagingBuffer.hpp:84
void EndTransfer()
Ends the transfer state, and pushes optimized copy commands to the given command list.
Definition StagingBuffer.cpp:116
BufferStagingList mBufferStagings
Definition StagingBuffer.hpp:89
RHIBuffer * GetBuffer()
Gets the GPU-only backing buffer.
Definition StagingBuffer.hpp:108
State
Definition StagingBuffer.hpp:75
bool HasUpdates()
Check if there are any pending updates to be performed.
Definition StagingBuffer.hpp:138
RHIDevice * mDevice
Definition StagingBuffer.hpp:81
RHIDeviceScopedObjectHandle< RHIBuffer > mBuffer
Definition StagingBuffer.hpp:86
void Update(RHICommandList *cmd)
Push the scheduled uploads onto the command list.
Definition StagingBuffer.cpp:122
Optional< uint32_t > mClearValue
Definition StagingBuffer.hpp:92
StagingBuffer * GetStagingBuffer()
Gets the current staging buffer for data uploads.
Definition StagingBuffer.cpp:92
RHIDeviceIdleGuard mIdleGuard
Definition StagingBuffer.hpp:91
Allocator * mAllocator
Definition StagingBuffer.hpp:82
void BeginTransfer()
Resets the staging buffer and aborts all pending data updates.
Definition StagingBuffer.cpp:85
State mState
Definition StagingBuffer.hpp:88
Bump-only allocation buffer used for staging data to be transferred to GPU.
Definition StagingBuffer.hpp:18
size_t Write(Span< const char > data, size_t alignment)
Writes data to the staging buffer, returning the offset of the data in the buffer.
Definition StagingBuffer.cpp:53
Allocator * mAllocator
Definition StagingBuffer.hpp:19
void Reset()
Definition StagingBuffer.cpp:67
void Seek(size_t offset, size_t alignment)
Seeks the current offset to the given offset, aligned to the given alignment.
Definition StagingBuffer.cpp:61
size_t mSize
Definition StagingBuffer.hpp:23
size_t Size() const
Definition StagingBuffer.hpp:42
RHIBuffer * GetBuffer() const
Definition StagingBuffer.hpp:46
size_t Tell() const
Resets the staging buffer, allowing it to be reused. No GPU-side data transfer is performed.
Definition StagingBuffer.hpp:41
void * mMapped
Definition StagingBuffer.hpp:22
RHIDevice * mDevice
Definition StagingBuffer.hpp:20
RHIDeviceScopedObjectHandle< RHIBuffer > mBuffer
Definition StagingBuffer.hpp:21
size_t FreeSize() const
Definition StagingBuffer.hpp:43
size_t mOffset
Definition StagingBuffer.hpp:24
std::unique_ptr< T, StlDeleter< T > > UniquePtr
std::unique_ptr with custom deleter that uses a Foundation::Core::Allocator to deallocate memory.
Definition Allocator.hpp:161
Transfer
Definition Common.hpp:201
static constexpr size_t kFullSize
Definition Common.hpp:8
Reference implementations of real-time rendering routines.
Definition Application.hpp:18
Vector< RHICommandList::CopyBufferRegion > BufferCopyList
Definition StagingBuffer.hpp:13
Vector< BufferStagingItem > BufferStagingList
Definition StagingBuffer.hpp:12
Tuple< RHIBuffer *, RHIBuffer *, RHICommandList::CopyBufferRegion > BufferStagingItem
Definition StagingBuffer.hpp:11
BufferStagingList & CoalesceBufferStaging(BufferStagingList &res)
Two-pointers in-place coalescing of copy regions.
Definition StagingBuffer.cpp:6
Definition Resource.hpp:21
RAII guard to wait for device idle on destruction.
Definition Device.hpp:205