Debian Codegraph @

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

raw
1 #include "D3D11BufferObject.h"
2 #include "D3D11Device.h"
3
4 #include <cstring>
5
6 #include <d3d11.h>
7
8 namespace nCine::RHI::D3D11
9 {
10 std::uint32_t D3D11BufferObject::_nextHandle = 1;
11
12 D3D11BufferObject::D3D11BufferObject(BufferTarget target)
13 : _handle(_nextHandle++), _target(target), _gpuBuffer(nullptr), _gpuBufferCapacity(0), _gpuDirty(true)
14 {
15 }
16
17 D3D11BufferObject::~D3D11BufferObject()
18 {
19 if (_gpuBuffer != nullptr) {
20 _gpuBuffer->Release();
21 _gpuBuffer = nullptr;
22 }
23 }
24
25 ID3D11Buffer* D3D11BufferObject::GetD3DBuffer() const
26 {
27 ID3D11Device* device = D3D11Device::GetD3DDevice();
28 ID3D11DeviceContext* context = D3D11Device::GetD3DContext();
29 if (device == nullptr || context == nullptr || _storage.empty()) {
30 return nullptr;
31 }
32
33 // (Re)create the buffer if missing or too small (grow-only), then refresh its contents from the store
34 if (_gpuBuffer == nullptr || _gpuBufferCapacity < _storage.size()) {
35 if (_gpuBuffer != nullptr) {
36 _gpuBuffer->Release();
37 _gpuBuffer = nullptr;
38 }
39 D3D11_BUFFER_DESC desc = {};
40 desc.ByteWidth = static_cast<UINT>((_storage.size() + 15u) & ~std::size_t(15u));
41 desc.Usage = D3D11_USAGE_DYNAMIC;
42 desc.BindFlags = (_target == BufferTarget::Index) ? D3D11_BIND_INDEX_BUFFER : D3D11_BIND_VERTEX_BUFFER;
43 desc.CPUAccessFlags = D3D11_CPU_ACCESS_WRITE;
44 if (FAILED(device->CreateBuffer(&desc, nullptr, &_gpuBuffer))) {
45 _gpuBuffer = nullptr;
46 return nullptr;
47 }
48 _gpuBufferCapacity = desc.ByteWidth;
49 _gpuDirty = true;
50 }
51
52 if (_gpuDirty) {
53 D3D11_MAPPED_SUBRESOURCE mapped;
54 if (SUCCEEDED(context->Map(_gpuBuffer, 0, D3D11_MAP_WRITE_DISCARD, 0, &mapped))) {
55 std::memcpy(mapped.pData, _storage.data(), _storage.size());
56 context->Unmap(_gpuBuffer, 0);
57 }
58 _gpuDirty = false;
59 }
60 return _gpuBuffer;
61 }
62
63 bool D3D11BufferObject::Bind() const
64 {
65 // No bound-buffer cache is consulted; the ranges are forwarded by BindBufferRange(),
66 // so this only reports that a bind "was issued"
67 return true;
68 }
69
70 bool D3D11BufferObject::Unbind() const
71 {
72 return true;
73 }
74
75 void D3D11BufferObject::BufferData(std::size_t size, const void* data, BufferUsage usage)
76 {
77 static_cast<void>(usage);
78 _storage.assign(size, std::uint8_t(0));
79 if (data != nullptr && size > 0) {
80 std::memcpy(_storage.data(), data, size);
81 }
82 _gpuDirty = true;
83 }
84
85 void D3D11BufferObject::BufferSubData(std::size_t offset, std::size_t size, const void* data)
86 {
87 if (data == nullptr || size == 0 || offset + size > _storage.size()) {
88 return;
89 }
90 std::memcpy(_storage.data() + offset, data, size);
91 _gpuDirty = true;
92 }
93
94 void D3D11BufferObject::BufferStorage(std::size_t size, const void* data, MapFlags flags)
95 {
96 // The host store is a plain resizable buffer, so "immutable storage" is just a (re)allocation;
97 // the storage/mapping flags do not apply
98 static_cast<void>(flags);
99 _storage.assign(size, std::uint8_t(0));
100 if (data != nullptr && size > 0) {
101 std::memcpy(_storage.data(), data, size);
102 }
103 _gpuDirty = true;
104 }
105
106 void D3D11BufferObject::BindBufferBase(std::uint32_t index)
107 {
108 BindBufferRange(index, 0, _storage.size());
109 }
110
111 void D3D11BufferObject::BindBufferRange(std::uint32_t index, std::size_t offset, std::size_t size)
112 {
113 if (offset > _storage.size()) {
114 return;
115 }
116 if (offset + size > _storage.size()) {
117 size = _storage.size() - offset;
118 }
119 D3D11Device::BindUniformRange(index, _storage.data() + offset, std::uint32_t(size));
120 }
121
122 void* D3D11BufferObject::MapBufferRange(std::size_t offset, std::size_t length, MapFlags access)
123 {
124 static_cast<void>(length);
125 static_cast<void>(access);
126 if (offset > _storage.size()) {
127 return nullptr;
128 }
129 return _storage.data() + offset;
130 }
131
132 void D3D11BufferObject::FlushMappedBufferRange(std::size_t offset, std::size_t length)
133 {
134 static_cast<void>(offset);
135 static_cast<void>(length);
136 _gpuDirty = true;
137 }
138
139 bool D3D11BufferObject::Unmap()
140 {
141 _gpuDirty = true;
142 return true;
143 }
144
145 void D3D11BufferObject::SetObjectLabel(StringView label)
146 {
147 static_cast<void>(label);
148 }
149 }