Staging buffer
This commit is contained in:
parent
c2ee7e8677
commit
7cc871fb48
82
src/main.cpp
82
src/main.cpp
|
@ -578,31 +578,87 @@ class VulkanApp {
|
||||||
}
|
}
|
||||||
|
|
||||||
fn createVertexBuffer() -> void {
|
fn createVertexBuffer() -> void {
|
||||||
|
vk::DeviceSize bufferSize = sizeof(mVertices[0]) * mVertices.size();
|
||||||
|
|
||||||
|
vk::UniqueBuffer stagingBuffer;
|
||||||
|
vk::UniqueDeviceMemory stagingBufferMemory;
|
||||||
|
|
||||||
|
createBuffer(
|
||||||
|
bufferSize,
|
||||||
|
vk::BufferUsageFlagBits::eVertexBuffer,
|
||||||
|
vk::MemoryPropertyFlagBits::eHostVisible | vk::MemoryPropertyFlagBits::eHostCoherent,
|
||||||
|
stagingBuffer,
|
||||||
|
stagingBufferMemory
|
||||||
|
);
|
||||||
|
|
||||||
|
void* data = mDevice->mapMemory(stagingBufferMemory.get(), 0, bufferSize);
|
||||||
|
memcpy(data, mVertices.data(), static_cast<usize>(bufferSize));
|
||||||
|
mDevice->unmapMemory(stagingBufferMemory.get());
|
||||||
|
|
||||||
|
createBuffer(
|
||||||
|
bufferSize,
|
||||||
|
vk::BufferUsageFlagBits::eVertexBuffer | vk::BufferUsageFlagBits::eTransferDst,
|
||||||
|
vk::MemoryPropertyFlagBits::eDeviceLocal,
|
||||||
|
mVertexBuffer,
|
||||||
|
mVertexBufferMemory
|
||||||
|
);
|
||||||
|
|
||||||
|
copyBuffer(stagingBuffer.get(), mVertexBuffer.get(), bufferSize);
|
||||||
|
|
||||||
|
stagingBuffer.reset();
|
||||||
|
stagingBufferMemory.reset();
|
||||||
|
}
|
||||||
|
|
||||||
|
fn createBuffer(
|
||||||
|
vk::DeviceSize deviceSize,
|
||||||
|
vk::BufferUsageFlags bufferUsageFlags,
|
||||||
|
vk::MemoryPropertyFlags memoryPropertyFlags,
|
||||||
|
vk::UniqueBuffer& buffer,
|
||||||
|
vk::UniqueDeviceMemory& bufferMemory
|
||||||
|
) -> void {
|
||||||
vk::BufferCreateInfo bufferInfo {
|
vk::BufferCreateInfo bufferInfo {
|
||||||
.size = sizeof(mVertices[0]) * mVertices.size(),
|
.size = deviceSize,
|
||||||
.usage = vk::BufferUsageFlagBits::eVertexBuffer,
|
.usage = bufferUsageFlags,
|
||||||
.sharingMode = vk::SharingMode::eExclusive,
|
.sharingMode = vk::SharingMode::eExclusive,
|
||||||
};
|
};
|
||||||
|
|
||||||
mVertexBuffer = mDevice->createBufferUnique(bufferInfo, nullptr);
|
buffer = mDevice->createBufferUnique(bufferInfo);
|
||||||
|
|
||||||
vk::MemoryRequirements memRequirements = mDevice->getBufferMemoryRequirements(mVertexBuffer.get());
|
vk::MemoryRequirements memRequirements = mDevice->getBufferMemoryRequirements(buffer.get());
|
||||||
|
|
||||||
vk::MemoryAllocateInfo allocInfo {
|
vk::MemoryAllocateInfo allocInfo {
|
||||||
.allocationSize = memRequirements.size,
|
.allocationSize = memRequirements.size,
|
||||||
.memoryTypeIndex = findMemoryType(
|
.memoryTypeIndex = findMemoryType(memRequirements.memoryTypeBits, memoryPropertyFlags),
|
||||||
memRequirements.memoryTypeBits,
|
|
||||||
vk::MemoryPropertyFlagBits::eHostVisible | vk::MemoryPropertyFlagBits::eHostCoherent
|
|
||||||
),
|
|
||||||
};
|
};
|
||||||
|
|
||||||
mVertexBufferMemory = mDevice->allocateMemoryUnique(allocInfo);
|
bufferMemory = mDevice->allocateMemoryUnique(allocInfo);
|
||||||
|
|
||||||
mDevice->bindBufferMemory(mVertexBuffer.get(), mVertexBufferMemory.get(), 0);
|
mDevice->bindBufferMemory(buffer.get(), bufferMemory.get(), 0);
|
||||||
|
}
|
||||||
|
|
||||||
void* data = mDevice->mapMemory(mVertexBufferMemory.get(), 0, bufferInfo.size);
|
fn copyBuffer(vk::Buffer srcBuffer, vk::Buffer dstBuffer, vk::DeviceSize deviceSize) -> void {
|
||||||
memcpy(data, mVertices.data(), static_cast<usize>(bufferInfo.size));
|
vk::CommandBufferAllocateInfo allocInfo {
|
||||||
mDevice->unmapMemory(mVertexBufferMemory.get());
|
.commandPool = mCommandPool.get(),
|
||||||
|
.level = vk::CommandBufferLevel::ePrimary,
|
||||||
|
.commandBufferCount = 1,
|
||||||
|
};
|
||||||
|
|
||||||
|
vk::UniqueCommandBuffer commandBuffer = std::move(mDevice->allocateCommandBuffersUnique(allocInfo)[0]);
|
||||||
|
|
||||||
|
vk::CommandBufferBeginInfo beginInfo { .flags = vk::CommandBufferUsageFlagBits::eOneTimeSubmit };
|
||||||
|
|
||||||
|
commandBuffer->begin(beginInfo);
|
||||||
|
|
||||||
|
vk::BufferCopy copyRegion { .size = deviceSize };
|
||||||
|
|
||||||
|
commandBuffer->copyBuffer(srcBuffer, dstBuffer, 1, ©Region);
|
||||||
|
|
||||||
|
commandBuffer->end();
|
||||||
|
|
||||||
|
vk::SubmitInfo submitInfo { .commandBufferCount = 1, .pCommandBuffers = &commandBuffer.get() };
|
||||||
|
|
||||||
|
mGraphicsQueue.submit(submitInfo, nullptr);
|
||||||
|
mGraphicsQueue.waitIdle();
|
||||||
}
|
}
|
||||||
|
|
||||||
fn findMemoryType(u32 typeFilter, vk::MemoryPropertyFlags properties) -> u32 {
|
fn findMemoryType(u32 typeFilter, vk::MemoryPropertyFlags properties) -> u32 {
|
||||||
|
|
Loading…
Reference in a new issue