| 1 |
#include "VulkanBufferObject.h" |
| 2 |
#include "VulkanDevice.h" |
| 3 |
#include "VulkanCommon.h" |
| 4 |
|
| 5 |
#include <cstring> |
| 6 |
|
| 7 |
namespace nCine::RHI::Vulkan |
| 8 |
{ |
| 9 |
std::uint32_t VulkanBufferObject::_nextHandle = 1; |
| 10 |
|
| 11 |
VulkanBufferObject::VulkanBufferObject(BufferTarget target) |
| 12 |
: _handle(_nextHandle++), _target(target), |
| 13 |
_gpuBuffer(0), _gpuMemory(0), _gpuMapped(nullptr), _gpuCapacity(0), _gpuDirty(true) |
| 14 |
{ |
| 15 |
} |
| 16 |
|
| 17 |
VulkanBufferObject::~VulkanBufferObject() |
| 18 |
{ |
| 19 |
ReleaseGpu(); |
| 20 |
} |
| 21 |
|
| 22 |
void VulkanBufferObject::ReleaseGpu() const |
| 23 |
{ |
| 24 |
// Unmapping is a CPU-side operation (it does not disturb an in-flight GPU read of the buffer), so do it now. |
| 25 |
VkDevice device = VkDeviceHandle(); |
| 26 |
if (device != VK_NULL_HANDLE && _gpuMapped != nullptr) { |
| 27 |
vkUnmapMemory(device, reinterpret_cast<VkDeviceMemory>(_gpuMemory)); |
| 28 |
} |
| 29 |
_gpuMapped = nullptr; |
| 30 |
// Defer the buffer / memory frees: an in-flight frame's command buffer may still bind this buffer as a |
| 31 |
// vertex/index source (up to MaxFramesInFlight frames run concurrently), and this also runs on a mid-frame |
| 32 |
// grow-realloc; freeing now would be a GPU use-after-free. The device frees them once every referencing |
| 33 |
// frame has completed. VkEnqueueDestroy no-ops if the device is already gone (teardown). |
| 34 |
VkEnqueueDestroy(VkDeferredResource::Buffer, _gpuBuffer); |
| 35 |
VkEnqueueDestroy(VkDeferredResource::DeviceMemory, _gpuMemory); |
| 36 |
_gpuBuffer = 0; |
| 37 |
_gpuMemory = 0; |
| 38 |
_gpuCapacity = 0; |
| 39 |
} |
| 40 |
|
| 41 |
std::uint64_t VulkanBufferObject::GetVkBuffer() const |
| 42 |
{ |
| 43 |
// Only the geometry (Vertex/Index) targets get a real VkBuffer; uniform data is routed through the |
| 44 |
// device's per-frame device-aligned uniform ring instead (see VulkanDevice draw path). |
| 45 |
if (_target == BufferTarget::Uniform) { |
| 46 |
return 0; |
| 47 |
} |
| 48 |
|
| 49 |
VkDevice device = VkDeviceHandle(); |
| 50 |
if (device == VK_NULL_HANDLE || _storage.empty()) { |
| 51 |
return 0; |
| 52 |
} |
| 53 |
|
| 54 |
// (Re)create when missing or too small (grow-only). Safe mid-frame: the device waits on the previous |
| 55 |
// frame's fence before any draw records, so the old buffer is no longer read by the GPU. |
| 56 |
if (_gpuBuffer == 0 || _gpuCapacity < _storage.size()) { |
| 57 |
ReleaseGpu(); |
| 58 |
|
| 59 |
VkBufferCreateInfo bci = {}; |
| 60 |
bci.sType = VK_STRUCTURE_TYPE_BUFFER_CREATE_INFO; |
| 61 |
bci.size = _storage.size(); |
| 62 |
bci.usage = (_target == BufferTarget::Index) ? VK_BUFFER_USAGE_INDEX_BUFFER_BIT : VK_BUFFER_USAGE_VERTEX_BUFFER_BIT; |
| 63 |
bci.sharingMode = VK_SHARING_MODE_EXCLUSIVE; |
| 64 |
VkBuffer buffer = VK_NULL_HANDLE; |
| 65 |
if (vkCreateBuffer(device, &bci, nullptr, &buffer) != VK_SUCCESS) { |
| 66 |
return 0; |
| 67 |
} |
| 68 |
|
| 69 |
VkMemoryRequirements req; |
| 70 |
vkGetBufferMemoryRequirements(device, buffer, &req); |
| 71 |
const std::uint32_t memType = VkFindMemoryType(req.memoryTypeBits, |
| 72 |
VK_MEMORY_PROPERTY_HOST_VISIBLE_BIT | VK_MEMORY_PROPERTY_HOST_COHERENT_BIT); |
| 73 |
VkMemoryAllocateInfo mai = {}; |
| 74 |
mai.sType = VK_STRUCTURE_TYPE_MEMORY_ALLOCATE_INFO; |
| 75 |
mai.allocationSize = req.size; |
| 76 |
mai.memoryTypeIndex = memType; |
| 77 |
VkDeviceMemory memory = VK_NULL_HANDLE; |
| 78 |
if (memType == UINT32_MAX || vkAllocateMemory(device, &mai, nullptr, &memory) != VK_SUCCESS) { |
| 79 |
vkDestroyBuffer(device, buffer, nullptr); |
| 80 |
return 0; |
| 81 |
} |
| 82 |
vkBindBufferMemory(device, buffer, memory, 0); |
| 83 |
void* mapped = nullptr; |
| 84 |
vkMapMemory(device, memory, 0, VK_WHOLE_SIZE, 0, &mapped); |
| 85 |
|
| 86 |
_gpuBuffer = reinterpret_cast<std::uint64_t>(buffer); |
| 87 |
_gpuMemory = reinterpret_cast<std::uint64_t>(memory); |
| 88 |
_gpuMapped = mapped; |
| 89 |
_gpuCapacity = _storage.size(); |
| 90 |
_gpuDirty = true; |
| 91 |
} |
| 92 |
|
| 93 |
if (_gpuDirty && _gpuMapped != nullptr) { |
| 94 |
std::memcpy(_gpuMapped, _storage.data(), _storage.size()); |
| 95 |
_gpuDirty = false; |
| 96 |
} |
| 97 |
return _gpuBuffer; |
| 98 |
} |
| 99 |
|
| 100 |
bool VulkanBufferObject::Bind() const |
| 101 |
{ |
| 102 |
return true; |
| 103 |
} |
| 104 |
|
| 105 |
bool VulkanBufferObject::Unbind() const |
| 106 |
{ |
| 107 |
return true; |
| 108 |
} |
| 109 |
|
| 110 |
void VulkanBufferObject::BufferData(std::size_t size, const void* data, BufferUsage usage) |
| 111 |
{ |
| 112 |
static_cast<void>(usage); |
| 113 |
_storage.assign(size, std::uint8_t(0)); |
| 114 |
if (data != nullptr && size > 0) { |
| 115 |
std::memcpy(_storage.data(), data, size); |
| 116 |
} |
| 117 |
_gpuDirty = true; |
| 118 |
} |
| 119 |
|
| 120 |
void VulkanBufferObject::BufferSubData(std::size_t offset, std::size_t size, const void* data) |
| 121 |
{ |
| 122 |
if (data == nullptr || size == 0 || offset + size > _storage.size()) { |
| 123 |
return; |
| 124 |
} |
| 125 |
std::memcpy(_storage.data() + offset, data, size); |
| 126 |
_gpuDirty = true; |
| 127 |
} |
| 128 |
|
| 129 |
void VulkanBufferObject::BufferStorage(std::size_t size, const void* data, MapFlags flags) |
| 130 |
{ |
| 131 |
// The host store is a plain resizable buffer, so "immutable storage" is just a (re)allocation |
| 132 |
static_cast<void>(flags); |
| 133 |
_storage.assign(size, std::uint8_t(0)); |
| 134 |
if (data != nullptr && size > 0) { |
| 135 |
std::memcpy(_storage.data(), data, size); |
| 136 |
} |
| 137 |
_gpuDirty = true; |
| 138 |
} |
| 139 |
|
| 140 |
void VulkanBufferObject::BindBufferBase(std::uint32_t index) |
| 141 |
{ |
| 142 |
BindBufferRange(index, 0, _storage.size()); |
| 143 |
} |
| 144 |
|
| 145 |
void VulkanBufferObject::BindBufferRange(std::uint32_t index, std::size_t offset, std::size_t size) |
| 146 |
{ |
| 147 |
if (offset > _storage.size()) { |
| 148 |
return; |
| 149 |
} |
| 150 |
if (offset + size > _storage.size()) { |
| 151 |
size = _storage.size() - offset; |
| 152 |
} |
| 153 |
VulkanDevice::BindUniformRange(index, _storage.data() + offset, std::uint32_t(size)); |
| 154 |
} |
| 155 |
|
| 156 |
void* VulkanBufferObject::MapBufferRange(std::size_t offset, std::size_t length, MapFlags access) |
| 157 |
{ |
| 158 |
static_cast<void>(length); |
| 159 |
static_cast<void>(access); |
| 160 |
if (offset > _storage.size()) { |
| 161 |
return nullptr; |
| 162 |
} |
| 163 |
return _storage.data() + offset; |
| 164 |
} |
| 165 |
|
| 166 |
void VulkanBufferObject::FlushMappedBufferRange(std::size_t offset, std::size_t length) |
| 167 |
{ |
| 168 |
static_cast<void>(offset); |
| 169 |
static_cast<void>(length); |
| 170 |
_gpuDirty = true; |
| 171 |
} |
| 172 |
|
| 173 |
bool VulkanBufferObject::Unmap() |
| 174 |
{ |
| 175 |
_gpuDirty = true; |
| 176 |
return true; |
| 177 |
} |
| 178 |
|
| 179 |
void VulkanBufferObject::SetObjectLabel(StringView label) |
| 180 |
{ |
| 181 |
static_cast<void>(label); |
| 182 |
} |
| 183 |
} |