Debian Codegraph @

Sources/nCine/Graphics/RHI/GXM/GxmBufferObject.h

raw
1 #pragma once
2
3 #include "GxmMemory.h"
4 #include "../RhiTypes.h"
5
6 #include <cstddef>
7 #include <cstdint>
8
9 #include <Containers/SmallVector.h>
10 #include <Containers/StringView.h>
11
12 using namespace Death::Containers;
13
14 namespace nCine::RHI::GXM
15 {
16 /**
17 @brief Buffer object of the sceGxm backend (aliased as `RHI::Buffer`)
18
19 A vertex, index or uniform buffer keeps a resizable byte store the pipeline maps and writes into.
20 @ref MapBufferRange() hands back a pointer into that store and @ref BindBufferRange() forwards a
21 sub-range to the device.
22
23 Every store is GPU-visible memory (@ref GxmMemory), so the pipeline's writes land where the hardware
24 can read them with no upload step at all:
25
26 - **Vertex and index** buffers are read by the GPU directly from it.
27 - **Uniform** buffers are read either way depending on the shader. A small block is copied into the
28 draw's reserved default uniform buffer, but a batched instance array is far too large for the
29 SGX's uniform registers, so the compiler places it in a *uniform buffer container* and the range is
30 handed to `sceGxmSetVertexUniformBuffer()` by address instead - which needs it to be GPU-visible.
31
32 The store is grow-only: a @ref BufferData() that fits the existing block reuses it, so the pipeline's
33 per-frame ring buffers allocate once. The memory is uncached, which suits a write-once-per-frame
34 stream; the small per-draw copies read back out of it, which is a handful of bytes.
35 */
36 class GxmBufferObject
37 {
38 public:
39 explicit GxmBufferObject(BufferTarget target);
40 ~GxmBufferObject();
41
42 GxmBufferObject(const GxmBufferObject&) = delete;
43 GxmBufferObject& operator=(const GxmBufferObject&) = delete;
44
45 /** @brief Returns a synthetic handle uniquely identifying the buffer (used by material sort keys) */
46 inline std::uint32_t GetGLHandle() const {
47 return _handle;
48 }
49 /** @brief Returns the binding target of the buffer */
50 inline BufferTarget GetTarget() const {
51 return _target;
52 }
53 /** @brief Returns the size in bytes of the data store */
54 inline std::size_t GetSize() const {
55 return _size;
56 }
57 /** @brief Returns the base pointer of the data store */
58 inline std::uint8_t* HostData() {
59 return _data;
60 }
61 /** @brief Returns the base pointer of the data store */
62 inline const std::uint8_t* HostData() const {
63 return _data;
64 }
65 /**
66 @brief Returns the GPU-visible base address of the store, or `nullptr` if it could not be allocated
67
68 The same pointer @ref HostData() returns, only named for the side that consumes it.
69 */
70 inline const void* GetGpuData() const {
71 return (_gpuBlock.IsValid() ? _data : nullptr);
72 }
73
74 /** @brief Marks the buffer as the currently bound one for its target (always issues the "bind") */
75 bool Bind() const;
76 /** @brief Marks no buffer as bound for this object's target */
77 bool Unbind() const;
78
79 /** @brief (Re)creates the data store with the given size, optional initial data and usage hint */
80 void BufferData(std::size_t size, const void* data, BufferUsage usage);
81 /** @brief Updates a subset of the data store starting at the given byte offset */
82 void BufferSubData(std::size_t offset, std::size_t size, const void* data);
83 /** @brief (Re)creates the data store like @ref BufferData(); there is no immutable storage, so the flags are ignored */
84 void BufferStorage(std::size_t size, const void* data, MapFlags flags);
85
86 /** @brief Binds the whole buffer to a uniform binding point index */
87 void BindBufferBase(std::uint32_t index);
88 /** @brief Binds a byte range of the buffer to a uniform binding point index (forwarded to the device) */
89 void BindBufferRange(std::uint32_t index, std::size_t offset, std::size_t size);
90
91 /** @brief Returns a pointer to a byte range of the store (there is no real mapping) */
92 void* MapBufferRange(std::size_t offset, std::size_t length, MapFlags access);
93 /** @brief Flushes a mapped range (a no-op, the store is uncached GPU-visible memory) */
94 void FlushMappedBufferRange(std::size_t offset, std::size_t length);
95 /** @brief Unmaps the buffer (a no-op, the store is directly addressable) */
96 bool Unmap();
97
98 /** @brief Sets a debug label for the buffer (ignored) */
99 void SetObjectLabel(StringView label);
100
101 // Static bound-handle helpers used by the shared VAO pool. The OpenGL backend keeps a per-target
102 // bound-buffer cache here; this backend has no such state, so these are inert. They mirror the OpenGL
103 // backend's static helpers so `RenderVaoPool` (which tracks a VAO's element array buffer) compiles
104 // unchanged against the `RHI::Buffer` alias.
105 /** @brief Records the buffer handle bound for a target (inert) */
106 inline static void SetBoundHandle(std::uint32_t target, std::uint32_t glHandle) {
107 static_cast<void>(target);
108 static_cast<void>(glHandle);
109 }
110 /** @brief Marks a buffer handle as bound for a target (inert) */
111 inline static bool BindHandle(std::uint32_t target, std::uint32_t glHandle) {
112 static_cast<void>(target);
113 static_cast<void>(glHandle);
114 return true;
115 }
116
117 private:
118 static std::uint32_t _nextHandle;
119
120 std::uint32_t _handle;
121 BufferTarget _target;
122
123 // Base and size of the store, whichever of the two backing kinds below provides it
124 std::uint8_t* _data;
125 std::size_t _size;
126
127 // GPU-visible backing of the store
128 GxmMemory::Block _gpuBlock;
129
130 /** @brief (Re)allocates the store to at least @p size bytes, keeping an already large enough one */
131 void Reserve(std::size_t size);
132 };
133 }