| 1 |
#pragma once |
| 2 |
|
| 3 |
#include "../RhiTypes.h" |
| 4 |
|
| 5 |
#include <cstddef> |
| 6 |
#include <cstdint> |
| 7 |
#include <vector> |
| 8 |
|
| 9 |
#include <Containers/StringView.h> |
| 10 |
|
| 11 |
using namespace Death::Containers; |
| 12 |
|
| 13 |
namespace nCine::RHI::GU |
| 14 |
{ |
| 15 |
/** |
| 16 |
@brief Host-memory buffer object of the GU backend (aliased as `RHI::Buffer`) |
| 17 |
|
| 18 |
The GU backend consumes vertices and uniform blocks from host memory (GE draws are fed through a |
| 19 |
display list the CPU writes into, so there is no persistent device-side vertex store to upload |
| 20 |
into), which makes this class a plain resizable byte store exactly like the software backend's: @ref |
| 21 |
MapBufferRange() hands back a pointer into the store, and @ref BindBufferRange() forwards a |
| 22 |
sub-range to the device so a bound uniform block can be decoded by the draw dispatch. |
| 23 |
*/ |
| 24 |
class GuBuffer |
| 25 |
{ |
| 26 |
public: |
| 27 |
explicit GuBuffer(BufferTarget target); |
| 28 |
~GuBuffer() = default; |
| 29 |
|
| 30 |
GuBuffer(const GuBuffer&) = delete; |
| 31 |
GuBuffer& operator=(const GuBuffer&) = delete; |
| 32 |
|
| 33 |
/** @brief Returns a synthetic handle uniquely identifying the buffer (used by material sort keys) */ |
| 34 |
inline std::uint32_t GetGLHandle() const { |
| 35 |
return _handle; |
| 36 |
} |
| 37 |
/** @brief Returns the binding target of the buffer */ |
| 38 |
inline BufferTarget GetTarget() const { |
| 39 |
return _target; |
| 40 |
} |
| 41 |
/** @brief Returns the size in bytes of the data store */ |
| 42 |
inline std::size_t GetSize() const { |
| 43 |
return _storage.size(); |
| 44 |
} |
| 45 |
/** @brief Returns the base pointer of the host-side data store */ |
| 46 |
inline std::uint8_t* HostData() { |
| 47 |
return _storage.data(); |
| 48 |
} |
| 49 |
/** @brief Returns the base pointer of the host-side data store */ |
| 50 |
inline const std::uint8_t* HostData() const { |
| 51 |
return _storage.data(); |
| 52 |
} |
| 53 |
|
| 54 |
/** @brief Marks the buffer as the currently bound one for its target (always issues the "bind") */ |
| 55 |
bool Bind() const; |
| 56 |
/** @brief Marks no buffer as bound for this object's target */ |
| 57 |
bool Unbind() const; |
| 58 |
|
| 59 |
/** @brief (Re)creates the data store with the given size, optional initial data and usage hint */ |
| 60 |
void BufferData(std::size_t size, const void* data, BufferUsage usage); |
| 61 |
/** @brief Updates a subset of the data store starting at the given byte offset */ |
| 62 |
void BufferSubData(std::size_t offset, std::size_t size, const void* data); |
| 63 |
/** @brief (Re)creates the data store like @ref BufferData(); there is no immutable storage, so the flags are ignored */ |
| 64 |
void BufferStorage(std::size_t size, const void* data, MapFlags flags); |
| 65 |
|
| 66 |
/** @brief Binds the whole buffer to a uniform binding point index */ |
| 67 |
void BindBufferBase(std::uint32_t index); |
| 68 |
/** @brief Binds a byte range of the buffer to a uniform binding point index (forwarded to the device) */ |
| 69 |
void BindBufferRange(std::uint32_t index, std::size_t offset, std::size_t size); |
| 70 |
|
| 71 |
/** @brief Returns a host pointer to a byte range of the store (there is no real mapping) */ |
| 72 |
void* MapBufferRange(std::size_t offset, std::size_t length, MapFlags access); |
| 73 |
/** @brief Flushes a mapped range (a no-op, the store is host memory) */ |
| 74 |
void FlushMappedBufferRange(std::size_t offset, std::size_t length); |
| 75 |
/** @brief Unmaps the buffer (a no-op, the store is host memory) */ |
| 76 |
bool Unmap(); |
| 77 |
|
| 78 |
/** @brief Sets a debug label for the buffer (ignored by the GU backend) */ |
| 79 |
void SetObjectLabel(StringView label); |
| 80 |
|
| 81 |
// Static bound-handle helpers used by the shared VAO pool. Inert here for the same reason as the |
| 82 |
// software backend's: there is no bound-buffer cache, the effects read the forwarded ranges. |
| 83 |
/** @brief Records the buffer handle bound for a target (inert for the GU backend) */ |
| 84 |
inline static void SetBoundHandle(std::uint32_t target, std::uint32_t glHandle) { |
| 85 |
static_cast<void>(target); |
| 86 |
static_cast<void>(glHandle); |
| 87 |
} |
| 88 |
/** @brief Marks a buffer handle as bound for a target (inert for the GU backend) */ |
| 89 |
inline static bool BindHandle(std::uint32_t target, std::uint32_t glHandle) { |
| 90 |
static_cast<void>(target); |
| 91 |
static_cast<void>(glHandle); |
| 92 |
return true; |
| 93 |
} |
| 94 |
|
| 95 |
private: |
| 96 |
static std::uint32_t _nextHandle; |
| 97 |
|
| 98 |
std::uint32_t _handle; |
| 99 |
BufferTarget _target; |
| 100 |
std::vector<std::uint8_t> _storage; |
| 101 |
}; |
| 102 |
} |