Compare commits

...

2 commits

Author SHA1 Message Date
Mars 7cc871fb48
Staging buffer 2024-10-10 16:04:46 -04:00
Mars c2ee7e8677
update flake 2024-10-10 16:04:38 -04:00
3 changed files with 73 additions and 21 deletions

View file

@ -1,10 +1,6 @@
# This file was generated by nvfetcher, please do not modify it manually.
{ fetchgit, fetchurl, fetchFromGitHub, dockerTools }:
{
fetchgit,
fetchurl,
fetchFromGitHub,
dockerTools,
}: {
fmt = {
pname = "fmt";
version = "11.0.2";

View file

@ -2,11 +2,11 @@
"nodes": {
"nixpkgs": {
"locked": {
"lastModified": 1728001300,
"narHash": "sha256-W7PYJ6s+A91V1WZOTef8y3t1BGQKCfcLtHjOKcP5ScA=",
"lastModified": 1728588307,
"narHash": "sha256-Y1mO+iz3RUNfmu2m2c42BggOLV5q0z2CWyJzf1ypDLA=",
"owner": "NixOS",
"repo": "nixpkgs",
"rev": "e00cc9f2fa7eaac3232e0de9b0b86dfd58b1f43c",
"rev": "45093dff80ae7cdefc55abb77075d2931d6b47ca",
"type": "github"
},
"original": {

View file

@ -578,31 +578,87 @@ class VulkanApp {
}
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 {
.size = sizeof(mVertices[0]) * mVertices.size(),
.usage = vk::BufferUsageFlagBits::eVertexBuffer,
.size = deviceSize,
.usage = bufferUsageFlags,
.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 {
.allocationSize = memRequirements.size,
.memoryTypeIndex = findMemoryType(
memRequirements.memoryTypeBits,
vk::MemoryPropertyFlagBits::eHostVisible | vk::MemoryPropertyFlagBits::eHostCoherent
),
.memoryTypeIndex = findMemoryType(memRequirements.memoryTypeBits, memoryPropertyFlags),
};
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);
memcpy(data, mVertices.data(), static_cast<usize>(bufferInfo.size));
mDevice->unmapMemory(mVertexBufferMemory.get());
fn copyBuffer(vk::Buffer srcBuffer, vk::Buffer dstBuffer, vk::DeviceSize deviceSize) -> void {
vk::CommandBufferAllocateInfo allocInfo {
.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, &copyRegion);
commandBuffer->end();
vk::SubmitInfo submitInfo { .commandBufferCount = 1, .pCommandBuffers = &commandBuffer.get() };
mGraphicsQueue.submit(submitInfo, nullptr);
mGraphicsQueue.waitIdle();
}
fn findMemoryType(u32 typeFilter, vk::MemoryPropertyFlags properties) -> u32 {