Debian Codegraph @

Sources/nCine/Graphics/RHI/GS/GsBuffer.h

raw
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::GS
14 {
15 /**
16 @brief Host-memory buffer object of the GS backend (aliased as `RHI::Buffer`)
17
18 The GS backend consumes vertices and uniform blocks from host memory: a draw is a GIF packet the EE
19 builds in main RAM and hands to the DMA controller, so there is no persistent device-side vertex store
20 to upload into. That makes this class a plain resizable byte store exactly like the PVR and software
21 backends': @ref MapBufferRange() hands back a pointer into the store, and @ref BindBufferRange()
22 forwards a sub-range to the device so a bound uniform block can be decoded by the draw dispatch.
23 */
24 class GsBuffer
25 {
26 public:
27 explicit GsBuffer(BufferTarget target);
28 ~GsBuffer() = default;
29
30 GsBuffer(const GsBuffer&) = delete;
31 GsBuffer& operator=(const GsBuffer&) = 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 GS 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 // PVR 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 GS 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 GS 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 }