Debian Codegraph @

Sources/nCine/Graphics/RHI/D3D11/D3D11BufferObject.h

raw
1 #pragma once
2
3 #include "../RhiTypes.h"
4
5 #include <cstddef>
6 #include <cstdint>
7
8 #include <Containers/SmallVector.h>
9 #include <Containers/StringView.h>
10
11 using namespace Death::Containers;
12
13 // Direct3D 11 interface referenced only as an opaque pointer here (definition pulled in by the .cpp)
14 struct ID3D11Buffer;
15
16 namespace nCine::RHI::D3D11
17 {
18 /**
19 @brief Buffer object of the Direct3D 11 backend (aliased as `RHI::Buffer`)
20
21 A vertex, index or uniform buffer keeps a resizable host byte store the pipeline maps and writes into.
22 @ref MapBufferRange() hands back a pointer into the store and @ref BindBufferRange() forwards a
23 sub-range to the device. Real `ID3D11Buffer` objects (with `Map`/`Unmap` for the streaming uniform
24 ring) are created from this same surface.
25 */
26 class D3D11BufferObject
27 {
28 public:
29 explicit D3D11BufferObject(BufferTarget target);
30 ~D3D11BufferObject();
31
32 D3D11BufferObject(const D3D11BufferObject&) = delete;
33 D3D11BufferObject& operator=(const D3D11BufferObject&) = delete;
34
35 /** @brief Returns a GPU buffer (vertex/index) whose contents are refreshed from the host store when dirty */
36 ID3D11Buffer* GetD3DBuffer() const;
37
38 /** @brief Returns a synthetic handle uniquely identifying the buffer (used by material sort keys) */
39 inline std::uint32_t GetGLHandle() const {
40 return _handle;
41 }
42 /** @brief Returns the binding target of the buffer */
43 inline BufferTarget GetTarget() const {
44 return _target;
45 }
46 /** @brief Returns the size in bytes of the data store */
47 inline std::size_t GetSize() const {
48 return _storage.size();
49 }
50 /** @brief Returns the base pointer of the host-side data store */
51 inline std::uint8_t* HostData() {
52 return _storage.data();
53 }
54 /** @brief Returns the base pointer of the host-side data store */
55 inline const std::uint8_t* HostData() const {
56 return _storage.data();
57 }
58
59 /** @brief Marks the buffer as the currently bound one for its target (always issues the "bind") */
60 bool Bind() const;
61 /** @brief Marks no buffer as bound for this object's target */
62 bool Unbind() const;
63
64 /** @brief (Re)creates the data store with the given size, optional initial data and usage hint */
65 void BufferData(std::size_t size, const void* data, BufferUsage usage);
66 /** @brief Updates a subset of the data store starting at the given byte offset */
67 void BufferSubData(std::size_t offset, std::size_t size, const void* data);
68 /** @brief (Re)creates the data store like @ref BufferData(); there is no immutable storage, so the flags are ignored */
69 void BufferStorage(std::size_t size, const void* data, MapFlags flags);
70
71 /** @brief Binds the whole buffer to a uniform binding point index */
72 void BindBufferBase(std::uint32_t index);
73 /** @brief Binds a byte range of the buffer to a uniform binding point index (forwarded to the device) */
74 void BindBufferRange(std::uint32_t index, std::size_t offset, std::size_t size);
75
76 /** @brief Returns a host pointer to a byte range of the store (there is no real mapping) */
77 void* MapBufferRange(std::size_t offset, std::size_t length, MapFlags access);
78 /** @brief Flushes a mapped range (a no-op, the store is host memory) */
79 void FlushMappedBufferRange(std::size_t offset, std::size_t length);
80 /** @brief Unmaps the buffer (a no-op, the store is host memory) */
81 bool Unmap();
82
83 /** @brief Sets a debug label for the buffer (ignored) */
84 void SetObjectLabel(StringView label);
85
86 // Static bound-handle helpers used by the shared VAO pool. The OpenGL backend keeps a per-target
87 // bound-buffer cache here; this backend has no such state, so these are inert. They mirror the OpenGL
88 // backend's static helpers so `RenderVaoPool` (which tracks a VAO's element array buffer) compiles
89 // unchanged against the `RHI::Buffer` alias.
90 /** @brief Records the buffer handle bound for a target (inert) */
91 inline static void SetBoundHandle(std::uint32_t target, std::uint32_t glHandle) {
92 static_cast<void>(target);
93 static_cast<void>(glHandle);
94 }
95 /** @brief Marks a buffer handle as bound for a target (inert) */
96 inline static bool BindHandle(std::uint32_t target, std::uint32_t glHandle) {
97 static_cast<void>(target);
98 static_cast<void>(glHandle);
99 return true;
100 }
101
102 private:
103 static std::uint32_t _nextHandle;
104
105 std::uint32_t _handle;
106 BufferTarget _target;
107 SmallVector<std::uint8_t, 0> _storage;
108
109 // GPU buffer mirroring the host store for the vertex/index targets (uniform buffers stay host-only and
110 // are forwarded as ranges). Refreshed lazily from the store when @ref _gpuDirty is set.
111 mutable ID3D11Buffer* _gpuBuffer;
112 mutable std::size_t _gpuBufferCapacity;
113 mutable bool _gpuDirty;
114 };
115 }