Debian Codegraph @

Sources/nCine/Graphics/RHI/GXM/GxmBufferObject.cpp

raw
1 #include "GxmBufferObject.h"
2 #include "GxmDevice.h"
3
4 #include "../../../../Main.h"
5
6 #include <cstring>
7
8 namespace nCine::RHI::GXM
9 {
10 std::uint32_t GxmBufferObject::_nextHandle = 0;
11
12 GxmBufferObject::GxmBufferObject(BufferTarget target)
13 : _handle(++_nextHandle), _target(target), _data(nullptr), _size(0)
14 {
15 }
16
17 GxmBufferObject::~GxmBufferObject()
18 {
19 GxmMemory::Free(_gpuBlock);
20 _data = nullptr;
21 _size = 0;
22 }
23
24 bool GxmBufferObject::Bind() const
25 {
26 // There is no per-target binding point in sceGxm: a vertex stream's address is handed to the draw call
27 // and an index buffer's to `sceGxmDraw()`, both resolved from the program's bound vertex format
28 return true;
29 }
30
31 bool GxmBufferObject::Unbind() const
32 {
33 return true;
34 }
35
36 void GxmBufferObject::Reserve(std::size_t size)
37 {
38 if (size <= _size && _data != nullptr) {
39 return;
40 }
41
42 // A store the GPU reads has to be a mapped memory block. Growing means a fresh block: the pipeline's
43 // ring buffers size themselves once at startup, so this is not a per-frame path
44 GxmMemory::Free(_gpuBlock);
45 const char* name = (_target == BufferTarget::Index ? "Jazz2:IndexBuffer"
46 : (_target == BufferTarget::Uniform ? "Jazz2:UniformBuffer" : "Jazz2:VertexBuffer"));
47 _gpuBlock = GxmMemory::Alloc(name, std::uint32_t(size), SCE_GXM_MEMORY_ATTRIB_READ);
48 if (!_gpuBlock.IsValid()) {
49 LOGE("Failed to allocate {} bytes of GPU-visible memory for a buffer object", size);
50 _data = nullptr;
51 _size = 0;
52 return;
53 }
54
55 _data = static_cast<std::uint8_t*>(_gpuBlock.Base);
56 _size = size;
57 }
58
59 void GxmBufferObject::BufferData(std::size_t size, const void* data, BufferUsage usage)
60 {
61 static_cast<void>(usage);
62
63 Reserve(size);
64 if (_data == nullptr) {
65 return;
66 }
67 if (data != nullptr) {
68 std::memcpy(_data, data, size);
69 } else {
70 std::memset(_data, 0, size);
71 }
72 }
73
74 void GxmBufferObject::BufferSubData(std::size_t offset, std::size_t size, const void* data)
75 {
76 if (data == nullptr || _data == nullptr || offset + size > _size) {
77 return;
78 }
79 std::memcpy(_data + offset, data, size);
80 }
81
82 void GxmBufferObject::BufferStorage(std::size_t size, const void* data, MapFlags flags)
83 {
84 static_cast<void>(flags);
85 BufferData(size, data, BufferUsage::StaticDraw);
86 }
87
88 void GxmBufferObject::BindBufferBase(std::uint32_t index)
89 {
90 BindBufferRange(index, 0, _size);
91 }
92
93 void GxmBufferObject::BindBufferRange(std::uint32_t index, std::size_t offset, std::size_t size)
94 {
95 if (_data == nullptr || offset + size > _size) {
96 return;
97 }
98 GxmDevice::BindUniformRange(index, _data + offset, std::uint32_t(size));
99 }
100
101 void* GxmBufferObject::MapBufferRange(std::size_t offset, std::size_t length, MapFlags access)
102 {
103 static_cast<void>(access);
104 if (_data == nullptr || offset + length > _size) {
105 return nullptr;
106 }
107 return _data + offset;
108 }
109
110 void GxmBufferObject::FlushMappedBufferRange(std::size_t offset, std::size_t length)
111 {
112 static_cast<void>(offset);
113 static_cast<void>(length);
114 }
115
116 bool GxmBufferObject::Unmap()
117 {
118 return true;
119 }
120
121 void GxmBufferObject::SetObjectLabel(StringView label)
122 {
123 static_cast<void>(label);
124 }
125 }