#include "GsBuffer.h" #include "GsDevice.h" #include namespace nCine::RHI::GS { std::uint32_t GsBuffer::_nextHandle = 1; GsBuffer::GsBuffer(BufferTarget target) : _handle(_nextHandle++), _target(target) { } bool GsBuffer::Bind() const { // No bound-buffer cache; the draw dispatch reads from the ranges forwarded by BindBufferRange(), // so this only reports that a bind "was issued" return true; } bool GsBuffer::Unbind() const { return true; } void GsBuffer::BufferData(std::size_t size, const void* data, BufferUsage usage) { static_cast(usage); if (data == nullptr && size == _storage.size()) { // Same-size orphaning (BufferData with no data is how the streaming buffers discard last // frame's contents) - the contract leaves the contents undefined, so re-zeroing the whole // store every frame is nothing but wasted memory traffic return; } _storage.assign(size, std::uint8_t(0)); if (data != nullptr && size > 0) { std::memcpy(_storage.data(), data, size); } } void GsBuffer::BufferSubData(std::size_t offset, std::size_t size, const void* data) { if (data == nullptr || size == 0 || offset + size > _storage.size()) { return; } std::memcpy(_storage.data() + offset, data, size); } void GsBuffer::BufferStorage(std::size_t size, const void* data, MapFlags flags) { // Everything lives in a resizable host store, so "immutable storage" is just a plain // (re)allocation; the storage/mapping flags do not apply static_cast(flags); _storage.assign(size, std::uint8_t(0)); if (data != nullptr && size > 0) { std::memcpy(_storage.data(), data, size); } } void GsBuffer::BindBufferBase(std::uint32_t index) { BindBufferRange(index, 0, _storage.size()); } void GsBuffer::BindBufferRange(std::uint32_t index, std::size_t offset, std::size_t size) { if (offset > _storage.size()) { return; } if (offset + size > _storage.size()) { size = _storage.size() - offset; } GsDevice::BindUniformRange(index, _storage.data() + offset, std::uint32_t(size)); } void* GsBuffer::MapBufferRange(std::size_t offset, std::size_t length, MapFlags access) { static_cast(length); static_cast(access); if (offset > _storage.size()) { return nullptr; } return _storage.data() + offset; } void GsBuffer::FlushMappedBufferRange(std::size_t offset, std::size_t length) { static_cast(offset); static_cast(length); } bool GsBuffer::Unmap() { return true; } void GsBuffer::SetObjectLabel(StringView label) { static_cast(label); } }