Debian Codegraph @

Sources/nCine/Graphics/RHI/RSX/RsxBufferObject.h

raw
1 #pragma once
2
3 #include "RsxVram.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::RSX
15 {
16 /**
17 @brief Buffer object of the RSX 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 RsxVram), so the pipeline's writes land where the hardware
24 can read them with no upload step at all - and every store is allocated in **main** memory rather
25 than in the GPU's local GDDR3. That is the opposite of where a texture goes, for the opposite reason:
26 these buffers are rewritten by the PPE every frame, and writing them into local memory would trade a
27 cheap cached write for an uncached one across the bus. The GPU reads them over FlexIO instead, which
28 is exactly the trade a streamed vertex buffer wants (see @ref RsxVram).
29
30 - **Vertex and index** buffers are read by the GPU directly from it, by the offset the block carries.
31 - **Uniform** buffers are read either way depending on the shader. A small block is written straight
32 into the vertex program's constant registers by the draw; a batched instance array is repacked and
33 written into them element by element, because the RSX has no uniform buffer to bind at all
34 (see `RsxDevice::UploadInstanceArray()`).
35
36 The store is grow-only: a @ref BufferData() that fits the existing block reuses it, so the pipeline's
37 per-frame ring buffers allocate once.
38 */
39 class RsxBufferObject
40 {
41 public:
42 explicit RsxBufferObject(BufferTarget target);
43 ~RsxBufferObject();
44
45 RsxBufferObject(const RsxBufferObject&) = delete;
46 RsxBufferObject& operator=(const RsxBufferObject&) = delete;
47
48 /** @brief Returns a synthetic handle uniquely identifying the buffer (used by material sort keys) */
49 inline std::uint32_t GetGLHandle() const {
50 return _handle;
51 }
52 /** @brief Returns the binding target of the buffer */
53 inline BufferTarget GetTarget() const {
54 return _target;
55 }
56 /** @brief Returns the size in bytes of the data store */
57 inline std::size_t GetSize() const {
58 return _size;
59 }
60 /** @brief Returns the base pointer of the data store */
61 inline std::uint8_t* HostData() {
62 return _data;
63 }
64 /** @brief Returns the base pointer of the data store */
65 inline const std::uint8_t* HostData() const {
66 return _data;
67 }
68 /**
69 @brief Returns the GPU-visible base address of the store, or `nullptr` if it could not be allocated
70
71 The same pointer @ref HostData() returns, only named for the side that consumes it.
72 */
73 inline const void* GetGpuData() const {
74 return (_gpuBlock.IsValid() ? _data : nullptr);
75 }
76
77 /** @brief Marks the buffer as the currently bound one for its target (always issues the "bind") */
78 bool Bind() const;
79 /** @brief Marks no buffer as bound for this object's target */
80 bool Unbind() const;
81
82 /** @brief (Re)creates the data store with the given size, optional initial data and usage hint */
83 void BufferData(std::size_t size, const void* data, BufferUsage usage);
84 /** @brief Updates a subset of the data store starting at the given byte offset */
85 void BufferSubData(std::size_t offset, std::size_t size, const void* data);
86 /** @brief (Re)creates the data store like @ref BufferData(); there is no immutable storage, so the flags are ignored */
87 void BufferStorage(std::size_t size, const void* data, MapFlags flags);
88
89 /** @brief Binds the whole buffer to a uniform binding point index */
90 void BindBufferBase(std::uint32_t index);
91 /** @brief Binds a byte range of the buffer to a uniform binding point index (forwarded to the device) */
92 void BindBufferRange(std::uint32_t index, std::size_t offset, std::size_t size);
93
94 /** @brief Returns a pointer to a byte range of the store (there is no real mapping) */
95 void* MapBufferRange(std::size_t offset, std::size_t length, MapFlags access);
96 /** @brief Flushes a mapped range (a no-op, the store is uncached GPU-visible memory) */
97 void FlushMappedBufferRange(std::size_t offset, std::size_t length);
98 /** @brief Unmaps the buffer (a no-op, the store is directly addressable) */
99 bool Unmap();
100
101 /** @brief Sets a debug label for the buffer (ignored) */
102 void SetObjectLabel(StringView label);
103
104 // Static bound-handle helpers used by the shared VAO pool. The OpenGL backend keeps a per-target
105 // bound-buffer cache here; this backend has no such state, so these are inert. They mirror the OpenGL
106 // backend's static helpers so `RenderVaoPool` (which tracks a VAO's element array buffer) compiles
107 // unchanged against the `RHI::Buffer` alias.
108 /** @brief Records the buffer handle bound for a target (inert) */
109 inline static void SetBoundHandle(std::uint32_t target, std::uint32_t glHandle) {
110 static_cast<void>(target);
111 static_cast<void>(glHandle);
112 }
113 /** @brief Marks a buffer handle as bound for a target (inert) */
114 inline static bool BindHandle(std::uint32_t target, std::uint32_t glHandle) {
115 static_cast<void>(target);
116 static_cast<void>(glHandle);
117 return true;
118 }
119
120 private:
121 static std::uint32_t _nextHandle;
122
123 std::uint32_t _handle;
124 BufferTarget _target;
125
126 // Base and size of the store, whichever of the two backing kinds below provides it
127 std::uint8_t* _data;
128 std::size_t _size;
129
130 // GPU-visible backing of the store
131 RsxVram::Block _gpuBlock;
132
133 /** @brief (Re)allocates the store to at least @p size bytes, keeping an already large enough one */
134 void Reserve(std::size_t size);
135 };
136 }