Debian Codegraph @

Sources/nCine/Graphics/RHI/Software/SwBuffer.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::Software
14 {
15 /**
16 @brief Host-memory buffer object of the software backend (aliased as `RHI::Buffer`)
17
18 Backs a vertex, index or uniform buffer with a plain resizable byte store. Because the software
19 backend reads everything from host memory there is no device-side upload: @ref MapBufferRange()
20 simply hands back a pointer into the store, and @ref BindBufferRange() forwards a sub-range to the
21 device so a bound uniform block can be sampled by the C++ effects at draw time.
22 */
23 class SwBuffer
24 {
25 public:
26 explicit SwBuffer(BufferTarget target);
27 ~SwBuffer() = default;
28
29 SwBuffer(const SwBuffer&) = delete;
30 SwBuffer& operator=(const SwBuffer&) = delete;
31
32 /** @brief Returns a synthetic handle uniquely identifying the buffer (used by material sort keys) */
33 inline std::uint32_t GetGLHandle() const {
34 return _handle;
35 }
36 /** @brief Returns the binding target of the buffer */
37 inline BufferTarget GetTarget() const {
38 return _target;
39 }
40 /** @brief Returns the size in bytes of the data store */
41 inline std::size_t GetSize() const {
42 return _storage.size();
43 }
44 /** @brief Returns the base pointer of the host-side data store */
45 inline std::uint8_t* HostData() {
46 return _storage.data();
47 }
48 /** @brief Returns the base pointer of the host-side data store */
49 inline const std::uint8_t* HostData() const {
50 return _storage.data();
51 }
52
53 /** @brief Marks the buffer as the currently bound one for its target (always issues the "bind") */
54 bool Bind() const;
55 /** @brief Marks no buffer as bound for this object's target */
56 bool Unbind() const;
57
58 /** @brief (Re)creates the data store with the given size, optional initial data and usage hint */
59 void BufferData(std::size_t size, const void* data, BufferUsage usage);
60 /** @brief Updates a subset of the data store starting at the given byte offset */
61 void BufferSubData(std::size_t offset, std::size_t size, const void* data);
62 /** @brief (Re)creates the data store like @ref BufferData(); the software backend has no immutable storage, so the flags are ignored */
63 void BufferStorage(std::size_t size, const void* data, MapFlags flags);
64
65 /** @brief Binds the whole buffer to a uniform binding point index */
66 void BindBufferBase(std::uint32_t index);
67 /** @brief Binds a byte range of the buffer to a uniform binding point index (forwarded to the device) */
68 void BindBufferRange(std::uint32_t index, std::size_t offset, std::size_t size);
69
70 /** @brief Returns a host pointer to a byte range of the store (there is no real mapping) */
71 void* MapBufferRange(std::size_t offset, std::size_t length, MapFlags access);
72 /** @brief Flushes a mapped range (a no-op, the store is host memory) */
73 void FlushMappedBufferRange(std::size_t offset, std::size_t length);
74 /** @brief Unmaps the buffer (a no-op, the store is host memory) */
75 bool Unmap();
76
77 /** @brief Sets a debug label for the buffer (ignored by the software backend) */
78 void SetObjectLabel(StringView label);
79
80 // Static bound-handle helpers used by the shared VAO pool. The OpenGL backend keeps a per-target
81 // bound-buffer cache here; the software backend has no such state, so these are inert. They mirror
82 // the OpenGL backend's static helpers so `RenderVaoPool` (which tracks a VAO's element array buffer)
83 // compiles unchanged against the `RHI::Buffer` alias.
84 /** @brief Records the buffer handle bound for a target (inert for the software backend) */
85 inline static void SetBoundHandle(std::uint32_t target, std::uint32_t glHandle) {
86 static_cast<void>(target);
87 static_cast<void>(glHandle);
88 }
89 /** @brief Marks a buffer handle as bound for a target (inert for the software backend) */
90 inline static bool BindHandle(std::uint32_t target, std::uint32_t glHandle) {
91 static_cast<void>(target);
92 static_cast<void>(glHandle);
93 return true;
94 }
95
96 private:
97 static std::uint32_t _nextHandle;
98
99 std::uint32_t _handle;
100 BufferTarget _target;
101 std::vector<std::uint8_t> _storage;
102 };
103 }