| 1 |
#include "RsxBufferObject.h" |
| 2 |
#include "RsxDevice.h" |
| 3 |
|
| 4 |
#include "../../../../Main.h" |
| 5 |
|
| 6 |
#include <cstring> |
| 7 |
|
| 8 |
namespace nCine::RHI::RSX |
| 9 |
{ |
| 10 |
namespace |
| 11 |
{ |
| 12 |
/** |
| 13 |
@brief Alignment every buffer store is allocated with |
| 14 |
|
| 15 |
128 rather than the 64 the hardware demands of an offset it fetches from: an index array and a |
| 16 |
vertex stream are both read through the GPU's 128-byte memory bursts, so starting a store on that |
| 17 |
boundary means the first burst of a draw carries only data the draw asked for. |
| 18 |
*/ |
| 19 |
constexpr std::uint32_t StoreAlignment = 128; |
| 20 |
} |
| 21 |
|
| 22 |
std::uint32_t RsxBufferObject::_nextHandle = 0; |
| 23 |
|
| 24 |
RsxBufferObject::RsxBufferObject(BufferTarget target) |
| 25 |
: _handle(++_nextHandle), _target(target), _data(nullptr), _size(0) |
| 26 |
{ |
| 27 |
} |
| 28 |
|
| 29 |
RsxBufferObject::~RsxBufferObject() |
| 30 |
{ |
| 31 |
RsxVram::Free(_gpuBlock); |
| 32 |
_data = nullptr; |
| 33 |
_size = 0; |
| 34 |
} |
| 35 |
|
| 36 |
bool RsxBufferObject::Bind() const |
| 37 |
{ |
| 38 |
// There is no per-target binding point on the RSX: a vertex stream's offset is handed to |
| 39 |
// `rsxBindVertexArrayAttrib()` and an index buffer's to `rsxDrawIndexArray()`, both resolved from |
| 40 |
// the program's bound vertex format at draw time |
| 41 |
return true; |
| 42 |
} |
| 43 |
|
| 44 |
bool RsxBufferObject::Unbind() const |
| 45 |
{ |
| 46 |
return true; |
| 47 |
} |
| 48 |
|
| 49 |
void RsxBufferObject::Reserve(std::size_t size) |
| 50 |
{ |
| 51 |
if (size <= _size && _data != nullptr) { |
| 52 |
return; |
| 53 |
} |
| 54 |
|
| 55 |
// A store the GPU reads has to live in memory it can address by offset. Growing means a fresh block: |
| 56 |
// the pipeline's ring buffers size themselves once at startup, so this is not a per-frame path. |
| 57 |
// Main memory rather than local: the PPE rewrites these every frame (see the header). |
| 58 |
RsxVram::Free(_gpuBlock); |
| 59 |
_gpuBlock = RsxVram::Alloc(std::uint32_t(size), StoreAlignment, RsxVram::Location::Main); |
| 60 |
if (!_gpuBlock.IsValid()) { |
| 61 |
LOGE("Failed to allocate {} bytes of GPU-visible memory for a buffer object", size); |
| 62 |
_data = nullptr; |
| 63 |
_size = 0; |
| 64 |
return; |
| 65 |
} |
| 66 |
|
| 67 |
_data = static_cast<std::uint8_t*>(_gpuBlock.Base); |
| 68 |
_size = size; |
| 69 |
} |
| 70 |
|
| 71 |
void RsxBufferObject::BufferData(std::size_t size, const void* data, BufferUsage usage) |
| 72 |
{ |
| 73 |
static_cast<void>(usage); |
| 74 |
|
| 75 |
Reserve(size); |
| 76 |
if (_data == nullptr) { |
| 77 |
return; |
| 78 |
} |
| 79 |
if (data != nullptr) { |
| 80 |
std::memcpy(_data, data, size); |
| 81 |
} else { |
| 82 |
std::memset(_data, 0, size); |
| 83 |
} |
| 84 |
} |
| 85 |
|
| 86 |
void RsxBufferObject::BufferSubData(std::size_t offset, std::size_t size, const void* data) |
| 87 |
{ |
| 88 |
if (data == nullptr || _data == nullptr || offset + size > _size) { |
| 89 |
return; |
| 90 |
} |
| 91 |
std::memcpy(_data + offset, data, size); |
| 92 |
} |
| 93 |
|
| 94 |
void RsxBufferObject::BufferStorage(std::size_t size, const void* data, MapFlags flags) |
| 95 |
{ |
| 96 |
static_cast<void>(flags); |
| 97 |
BufferData(size, data, BufferUsage::StaticDraw); |
| 98 |
} |
| 99 |
|
| 100 |
void RsxBufferObject::BindBufferBase(std::uint32_t index) |
| 101 |
{ |
| 102 |
BindBufferRange(index, 0, _size); |
| 103 |
} |
| 104 |
|
| 105 |
void RsxBufferObject::BindBufferRange(std::uint32_t index, std::size_t offset, std::size_t size) |
| 106 |
{ |
| 107 |
if (_data == nullptr || offset + size > _size) { |
| 108 |
return; |
| 109 |
} |
| 110 |
RsxDevice::BindUniformRange(index, _data + offset, std::uint32_t(size)); |
| 111 |
} |
| 112 |
|
| 113 |
void* RsxBufferObject::MapBufferRange(std::size_t offset, std::size_t length, MapFlags access) |
| 114 |
{ |
| 115 |
static_cast<void>(access); |
| 116 |
if (_data == nullptr || offset + length > _size) { |
| 117 |
return nullptr; |
| 118 |
} |
| 119 |
return _data + offset; |
| 120 |
} |
| 121 |
|
| 122 |
void RsxBufferObject::FlushMappedBufferRange(std::size_t offset, std::size_t length) |
| 123 |
{ |
| 124 |
static_cast<void>(offset); |
| 125 |
static_cast<void>(length); |
| 126 |
} |
| 127 |
|
| 128 |
bool RsxBufferObject::Unmap() |
| 129 |
{ |
| 130 |
return true; |
| 131 |
} |
| 132 |
|
| 133 |
void RsxBufferObject::SetObjectLabel(StringView label) |
| 134 |
{ |
| 135 |
static_cast<void>(label); |
| 136 |
} |
| 137 |
} |