50 if (dstOffset + nbytes >
size_)
51 throw Exception(
"GpuBuffer.write: out of range (offset=%llu size=%llu capacity=%llu)",
52 (
unsigned long long)dstOffset, (
unsigned long long)nbytes,
53 (
unsigned long long)
size_);
60#if defined(VKB_ENABLE_VMA)
63 const VkResult result = vmaMapMemory(
device_->vma_allocator, vmaAllocation_, &base);
64 if (result != VK_SUCCESS)
65 throw Exception(
"GpuBuffer.write: VMA map failed: %d",
int(result));
66 std::memcpy(
static_cast<unsigned char *
>(base) + dstOffset, src,
size_t(nbytes));
67 vmaUnmapMemory(
device_->vma_allocator, vmaAllocation_);
71 void *
ptr = (*device_)->mapMemory(
memory_, dstOffset, nbytes, vk::MemoryMapFlags{});
72 std::memcpy(
ptr, src,
size_t(nbytes));
73 (*device_)->unmapMemory(
memory_);
77 using buf = vk::BufferUsageFlagBits;
78 using pfb = vk::MemoryPropertyFlagBits;
79 vkb::GenericBuffer staging(*
device_, buf::eTransferSrc, nbytes,
80 pfb::eHostVisible | pfb::eHostCoherent);
81 staging.updateLocal(vkb::FrameSlot::gpuIdle(), src, nbytes);
82 vkb::executeImmediately(
device_->instance, pool, queue, [&](vk::CommandBuffer cb) {
83 vk::BufferCopy bc{0, vk::DeviceSize(dstOffset), vk::DeviceSize(nbytes)};
84 cb.copyBuffer(staging.buffer,
buffer_, bc);
88void VulkanGpuBuffer::downloadBytes(
void *dst, uint64_t nbytes, uint64_t srcOffset)
const {
89 if (!device_ || !buffer_ || !dst || nbytes == 0)
return;
90 if (srcOffset + nbytes > size_)
91 throw Exception(
"GpuBuffer.read: out of range (offset=%llu size=%llu capacity=%llu)",
92 (
unsigned long long)srcOffset, (
unsigned long long)nbytes,
93 (
unsigned long long)size_);
100#if defined(VKB_ENABLE_VMA)
101 if (vmaAllocation_) {
102 void *base =
nullptr;
103 const VkResult result = vmaMapMemory(device_->vma_allocator, vmaAllocation_, &base);
104 if (result != VK_SUCCESS)
105 throw Exception(
"GpuBuffer.read: VMA map failed: %d",
int(result));
106 std::memcpy(dst,
static_cast<const unsigned char *
>(base) + srcOffset,
size_t(nbytes));
107 vmaUnmapMemory(device_->vma_allocator, vmaAllocation_);
111 void *
ptr = (*device_)->mapMemory(memory_, srcOffset, nbytes, vk::MemoryMapFlags{});
112 std::memcpy(dst,
ptr,
size_t(nbytes));
113 (*device_)->unmapMemory(memory_);
117 using buf = vk::BufferUsageFlagBits;
118 using pfb = vk::MemoryPropertyFlagBits;
119 vkb::GenericBuffer staging(*device_, buf::eTransferDst, nbytes,
120 pfb::eHostVisible | pfb::eHostCoherent);
121 vkb::executeImmediately(device_->instance, pool, queue, [&](vk::CommandBuffer cb) {
122 vk::BufferCopy bc{vk::DeviceSize(srcOffset), 0, vk::DeviceSize(nbytes)};
123 cb.copyBuffer(buffer_, staging.buffer, bc);
125 void *
ptr = staging.map();
126 std::memcpy(dst,
ptr,
size_t(nbytes));