Debian Codegraph @

Sources/nCine/Graphics/RHI/GU/GuBuffer.cpp

raw
1 #include "GuBuffer.h"
2 #include "GuDevice.h"
3
4 #include <cstring>
5
6 namespace nCine::RHI::GU
7 {
8 std::uint32_t GuBuffer::_nextHandle = 1;
9
10 GuBuffer::GuBuffer(BufferTarget target)
11 : _handle(_nextHandle++), _target(target)
12 {
13 }
14
15 bool GuBuffer::Bind() const
16 {
17 // No bound-buffer cache; the draw dispatch reads from the ranges forwarded by BindBufferRange(),
18 // so this only reports that a bind "was issued"
19 return true;
20 }
21
22 bool GuBuffer::Unbind() const
23 {
24 return true;
25 }
26
27 void GuBuffer::BufferData(std::size_t size, const void* data, BufferUsage usage)
28 {
29 static_cast<void>(usage);
30 if (data == nullptr && size == _storage.size()) {
31 // Same-size orphaning (BufferData with no data is how the streaming buffers discard last
32 // frame's contents) - the contract leaves the contents undefined, so re-zeroing the whole
33 // store every frame is nothing but wasted memory traffic
34 return;
35 }
36 _storage.assign(size, std::uint8_t(0));
37 if (data != nullptr && size > 0) {
38 std::memcpy(_storage.data(), data, size);
39 }
40 }
41
42 void GuBuffer::BufferSubData(std::size_t offset, std::size_t size, const void* data)
43 {
44 if (data == nullptr || size == 0 || offset + size > _storage.size()) {
45 return;
46 }
47 std::memcpy(_storage.data() + offset, data, size);
48 }
49
50 void GuBuffer::BufferStorage(std::size_t size, const void* data, MapFlags flags)
51 {
52 // Everything lives in a resizable host store, so "immutable storage" is just a plain
53 // (re)allocation; the storage/mapping flags do not apply
54 static_cast<void>(flags);
55 _storage.assign(size, std::uint8_t(0));
56 if (data != nullptr && size > 0) {
57 std::memcpy(_storage.data(), data, size);
58 }
59 }
60
61 void GuBuffer::BindBufferBase(std::uint32_t index)
62 {
63 BindBufferRange(index, 0, _storage.size());
64 }
65
66 void GuBuffer::BindBufferRange(std::uint32_t index, std::size_t offset, std::size_t size)
67 {
68 if (offset > _storage.size()) {
69 return;
70 }
71 if (offset + size > _storage.size()) {
72 size = _storage.size() - offset;
73 }
74 GuDevice::BindUniformRange(index, _storage.data() + offset, std::uint32_t(size));
75 }
76
77 void* GuBuffer::MapBufferRange(std::size_t offset, std::size_t length, MapFlags access)
78 {
79 static_cast<void>(length);
80 static_cast<void>(access);
81 if (offset > _storage.size()) {
82 return nullptr;
83 }
84 return _storage.data() + offset;
85 }
86
87 void GuBuffer::FlushMappedBufferRange(std::size_t offset, std::size_t length)
88 {
89 static_cast<void>(offset);
90 static_cast<void>(length);
91 }
92
93 bool GuBuffer::Unmap()
94 {
95 return true;
96 }
97
98 void GuBuffer::SetObjectLabel(StringView label)
99 {
100 static_cast<void>(label);
101 }
102 }