Debian Codegraph @

Sources/nCine/Graphics/RHI/Vulkan/VulkanBufferObject.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::Vulkan
14 {
15 /**
16 @brief Buffer object of the Vulkan backend (aliased as `RHI::Buffer`)
17
18 A vertex, index or uniform buffer keeps a resizable host byte store the pipeline maps and writes into.
19 @ref MapBufferRange() hands back a pointer into the store and @ref BindBufferRange() forwards a
20 sub-range to the device. It additionally backs the Vertex/Index targets with a real host-visible
21 `VkBuffer` (@ref GetVkBuffer()), refreshed from the host store when dirty — the device binds it with
22 `vkCmdBindVertexBuffers`/`vkCmdBindIndexBuffer`. Uniform-target buffers stay host-only: their bound
23 ranges are copied into the device's per-frame device-aligned uniform ring at draw time instead.
24 */
25 class VulkanBufferObject
26 {
27 public:
28 explicit VulkanBufferObject(BufferTarget target);
29 ~VulkanBufferObject();
30
31 VulkanBufferObject(const VulkanBufferObject&) = delete;
32 VulkanBufferObject& operator=(const VulkanBufferObject&) = delete;
33
34 /** @brief Returns the `VkBuffer` mirroring the host store (Vertex/Index targets), refreshing it if dirty; `VK_NULL_HANDLE` for Uniform targets or before any data. Returned as `std::uint64_t` so the contract header stays free of `vulkan.h`. */
35 std::uint64_t GetVkBuffer() const;
36
37 /** @brief Returns a synthetic handle uniquely identifying the buffer (used by material sort keys) */
38 inline std::uint32_t GetGLHandle() const {
39 return _handle;
40 }
41 /** @brief Returns the binding target of the buffer */
42 inline BufferTarget GetTarget() const {
43 return _target;
44 }
45 /** @brief Returns the size in bytes of the data store */
46 inline std::size_t GetSize() const {
47 return _storage.size();
48 }
49 /** @brief Returns the base pointer of the host-side data store */
50 inline std::uint8_t* HostData() {
51 return _storage.data();
52 }
53 /** @brief Returns the base pointer of the host-side data store */
54 inline const std::uint8_t* HostData() const {
55 return _storage.data();
56 }
57
58 /** @brief Marks the buffer as the currently bound one for its target (always issues the "bind") */
59 bool Bind() const;
60 /** @brief Marks no buffer as bound for this object's target */
61 bool Unbind() const;
62
63 /** @brief (Re)creates the data store with the given size, optional initial data and usage hint */
64 void BufferData(std::size_t size, const void* data, BufferUsage usage);
65 /** @brief Updates a subset of the data store starting at the given byte offset */
66 void BufferSubData(std::size_t offset, std::size_t size, const void* data);
67 /** @brief (Re)creates the data store like @ref BufferData(); there is no immutable storage, so the flags are ignored */
68 void BufferStorage(std::size_t size, const void* data, MapFlags flags);
69
70 /** @brief Binds the whole buffer to a uniform binding point index */
71 void BindBufferBase(std::uint32_t index);
72 /** @brief Binds a byte range of the buffer to a uniform binding point index (forwarded to the device) */
73 void BindBufferRange(std::uint32_t index, std::size_t offset, std::size_t size);
74
75 /** @brief Returns a host pointer to a byte range of the store (there is no real mapping) */
76 void* MapBufferRange(std::size_t offset, std::size_t length, MapFlags access);
77 /** @brief Flushes a mapped range (a no-op, the store is host memory) */
78 void FlushMappedBufferRange(std::size_t offset, std::size_t length);
79 /** @brief Unmaps the buffer (a no-op, the store is host memory) */
80 bool Unmap();
81
82 /** @brief Sets a debug label for the buffer (ignored) */
83 void SetObjectLabel(StringView label);
84
85 // Static bound-handle helpers used by the shared VAO pool. The OpenGL backend keeps a per-target
86 // bound-buffer cache here; this backend has no such state, so these are inert. They mirror the OpenGL
87 // backend's static helpers so `RenderVaoPool` (which tracks a VAO's element array buffer) compiles
88 // unchanged against the `RHI::Buffer` alias.
89 /** @brief Records the buffer handle bound for a target (inert) */
90 inline static void SetBoundHandle(std::uint32_t target, std::uint32_t glHandle) {
91 static_cast<void>(target);
92 static_cast<void>(glHandle);
93 }
94 /** @brief Marks a buffer handle as bound for a target (inert) */
95 inline static bool BindHandle(std::uint32_t target, std::uint32_t glHandle) {
96 static_cast<void>(target);
97 static_cast<void>(glHandle);
98 return true;
99 }
100
101 private:
102 static std::uint32_t _nextHandle;
103
104 std::uint32_t _handle;
105 BufferTarget _target;
106 std::vector<std::uint8_t> _storage;
107
108 // Host-visible GPU buffer mirroring the host store for the Vertex/Index targets (Uniform targets stay
109 // host-only). Kept as opaque integers so this contract header does not need <vulkan/vulkan.h>; the .cpp
110 // reinterprets them to VkBuffer/VkDeviceMemory. Refreshed lazily from the store when _gpuDirty is set.
111 mutable std::uint64_t _gpuBuffer;
112 mutable std::uint64_t _gpuMemory;
113 mutable void* _gpuMapped;
114 mutable std::size_t _gpuCapacity;
115 mutable bool _gpuDirty;
116
117 void ReleaseGpu() const;
118 };
119 }