diff --git a/include/application.hpp b/include/application.hpp index 7d64868..fd5daef 100644 --- a/include/application.hpp +++ b/include/application.hpp @@ -30,7 +30,7 @@ class Instance; class Swapchain; class GraphicsPipeline; class Buffer; -class SimulationBuffer; +class Buffer; class CommandPool; class Image; class ComputePipeline; @@ -64,8 +64,9 @@ private: unique_ptr transferSemaphore; unique_ptr renderFence; unique_ptr computeFence; - unique_ptr transferFence; + unique_ptr computeTransferredFence; std::mutex submitMutex; + std::mutex bufferWriteMutex; unique_ptr swapchain; unique_ptr descriptorPool; @@ -83,11 +84,11 @@ private: void updateConstraintBuffers(VkCommandBuffer commandBuffer); size_t currentDrawVertexBuffer = 0; - unique_ptr vertexBuffers[2]; - unique_ptr faceBuffer; - unique_ptr edgeBuffer; - unique_ptr triangleBuffer; - unique_ptr tetrahedronBuffer; + unique_ptr vertexBuffers[2]; + unique_ptr faceBuffer; + unique_ptr edgeBuffer; + unique_ptr triangleBuffer; + unique_ptr tetrahedronBuffer; ConstraintData constraintData {}; vector> softBodies; diff --git a/include/vulkan/buffer.hpp b/include/vulkan/buffer.hpp index 60a5060..ac46d5c 100644 --- a/include/vulkan/buffer.hpp +++ b/include/vulkan/buffer.hpp @@ -19,10 +19,8 @@ class Buffer { public: Buffer(VkDeviceSize bufferSize, VkBufferUsageFlags bufferUsage, VmaMemoryUsage memoryUsage, VmaAllocationCreateFlags vmaAllocationFlags); - Buffer(VkDeviceSize bufferSize, void* initialData, VkBufferUsageFlags bufferUsage, - VmaMemoryUsage memoryUsage, VmaAllocationCreateFlags vmaAllocationFlags); - static unique_ptr Append(const Buffer& old, void* data, VkDeviceSize size, VkCommandBuffer commandBuffer=VK_NULL_HANDLE); - static unique_ptr Replace(const Buffer& old, void* data, VkDeviceSize size, VkCommandBuffer commandBuffer=VK_NULL_HANDLE); + unique_ptr appended(void* data, VkDeviceSize appendSize, VkCommandBuffer commandBuffer) const; + unique_ptr replaced(void* data, VkDeviceSize replaceSize, VkCommandBuffer commandBuffer) const; virtual ~Buffer(); VkBuffer handle = VK_NULL_HANDLE; VmaAllocation allocation = VK_NULL_HANDLE; @@ -35,7 +33,7 @@ public: } void copyTo(Buffer* buffer) const; void copyTo(Image* image) const; - void setData(void* data, VkDeviceSize offset, VkDeviceSize size, VkCommandBuffer commandBuffer=VK_NULL_HANDLE); + void setData(void* data, VkDeviceSize offset, VkDeviceSize dataSize, VkCommandBuffer commandBuffer=VK_NULL_HANDLE); shared_ptr getStagingBuffer(); Buffer(const Buffer& other) = delete; Buffer& operator =(const Buffer& other) = delete; @@ -45,10 +43,4 @@ private: VkBufferUsageFlags bufferUsageFlags; VmaMemoryUsage memoryUsage; VmaAllocationCreateFlags allocationCreateFlags; -}; - -class SimulationBuffer : public Buffer { -public: - SimulationBuffer(VkDeviceSize bufferSize, VkBufferUsageFlags bufferUsage=0): - Buffer(bufferSize, initialData, bufferUsage | VK_BUFFER_USAGE_STORAGE_BUFFER_BIT, VMA_MEMORY_USAGE_AUTO_PREFER_DEVICE, 0) {} }; \ No newline at end of file diff --git a/src/application.cpp b/src/application.cpp index ca8e415..3f14dd5 100644 --- a/src/application.cpp +++ b/src/application.cpp @@ -81,9 +81,11 @@ Application::Application() { bool grabbing; } initialGrabInformation {}; initialGrabInformation.distanceToFace = 1e20; - grabBuffer = make_unique(sizeof(GrabInformation), &initialGrabInformation, sizeof(GrabInformation), - VK_BUFFER_USAGE_STORAGE_BUFFER_BIT, VMA_MEMORY_USAGE_AUTO_PREFER_DEVICE, 0); + grabBuffer = make_unique(sizeof(GrabInformation), + VK_BUFFER_USAGE_STORAGE_BUFFER_BIT | VK_BUFFER_USAGE_TRANSFER_DST_BIT, + VMA_MEMORY_USAGE_AUTO_PREFER_DEVICE, 0); grabBuffer->setName("Grab"); + grabBuffer->setData(&initialGrabInformation, 0, sizeof(initialGrabInformation)); grabber = make_unique(); sizeInformationBuffer = make_unique( @@ -93,8 +95,9 @@ Application::Application() { VMA_ALLOCATION_CREATE_HOST_ACCESS_SEQUENTIAL_WRITE_BIT | VMA_ALLOCATION_CREATE_MAPPED_BIT); sizeInformationBuffer->setName("Sizes"); sizeInformation = &sizeInformationBuffer->access(); + *sizeInformation = {}; - addSoftBody("models/bunny_medium.ply"); + addSoftBody("models/bunny_medium.ply", 10); SimulationUniformData simulationUniformData { .gravity = {0, -9.81, 0}, @@ -122,13 +125,6 @@ Application::Application() { descriptorPool->bindBuffer(*cameraUniformBuffer, VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER, DescriptorSet::WORLD, 0); descriptorPool->bindImage(*firstImage, VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER, DescriptorSet::WORLD, 2); - descriptorPool->bindBuffer(*vertexBuffers[1 - currentDrawVertexBuffer], VK_DESCRIPTOR_TYPE_STORAGE_BUFFER, DescriptorSet::MESH, 0); - descriptorPool->bindBuffer(*faceBuffer, VK_DESCRIPTOR_TYPE_STORAGE_BUFFER, DescriptorSet::MESH, 1); - descriptorPool->bindBuffer(*edgeBuffer, VK_DESCRIPTOR_TYPE_STORAGE_BUFFER, DescriptorSet::MESH, 2); - // descriptorPool->bindBuffer(*triangleBuffer, VK_DESCRIPTOR_TYPE_STORAGE_BUFFER, DescriptorSet::MESH, 3); - descriptorPool->bindBuffer(*tetrahedronBuffer, VK_DESCRIPTOR_TYPE_STORAGE_BUFFER, DescriptorSet::MESH, 4); - descriptorPool->bindBuffer(*sizeInformationBuffer, VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER, DescriptorSet::MESH, 5); - descriptorPool->bindBuffer(*simulationPropertiesBuffer, VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER, DescriptorSet::SIMULATION, 0); descriptorPool->bindBuffer(*grabBuffer, VK_DESCRIPTOR_TYPE_STORAGE_BUFFER, DescriptorSet::SIMULATION, 1); @@ -179,10 +175,12 @@ void Application::createSyncObjects() { transferSemaphore = make_unique(); renderFence = make_unique(true); computeFence = make_unique(false); - transferFence = make_unique(false); + computeTransferredFence = make_unique(false); } void Application::addSoftBody(const std::string &modelFile, size_t count) { + bufferWriteMutex.lock(); + Mesh mesh(modelFile); // Do SoftBody calculations once in constructor, will be copied from now on @@ -242,25 +240,64 @@ void Application::addSoftBody(const std::string &modelFile, size_t count) { sizeInformation->vertexCount += newVertices.size(); sizeInformation->faceCount += newFaces.size(); - auto commandBuffer = Instance::instance->renderingCommandPool->beginSingleTimeCommandBuffer(); - vertexBuffers[0] = Buffer::Append(*vertexBuffers[0], newVertices.data(), newVertices.size() * sizeof(Vertex), commandBuffer); - vertexBuffers[1] = Buffer::Append(*vertexBuffers[1], newVertices.data(), newVertices.size() * sizeof(Vertex), commandBuffer); - faceBuffer = Buffer::Append(*faceBuffer, newFaces.data(), newFaces.size() * sizeof(Face), commandBuffer); - edgeBuffer = Buffer::Replace(*edgeBuffer, constraintData.edges.data(), constraintData.edges.size() * sizeof(Edge), commandBuffer); - // triangleBuffer = Buffer::Replace(*triangleBuffer, constraintData.triangles.data(), constraintData.triangles.size() * sizeof(Triangle), commandBuffer); - tetrahedronBuffer = Buffer::Replace(*tetrahedronBuffer, constraintData.tetrahedra.data(), constraintData.tetrahedra.size() * sizeof(Tetrahedron), commandBuffer); - - vertexBuffers[0] = make_unique(VK_BUFFER_USAGE_VERTEX_BUFFER_BIT | VK_BUFFER_USAGE_TRANSFER_SRC_BIT | VK_BUFFER_USAGE_TRANSFER_DST_BIT); - vertexBuffers[1] = make_unique(VK_BUFFER_USAGE_VERTEX_BUFFER_BIT | VK_BUFFER_USAGE_TRANSFER_SRC_BIT | VK_BUFFER_USAGE_TRANSFER_DST_BIT); - faceBuffer = make_unique(VK_BUFFER_USAGE_INDEX_BUFFER_BIT); + unique_ptr newVertexBuffers[2]; + unique_ptr newFaceBuffer; + unique_ptr newEdgeBuffer; + unique_ptr newTetrahedronBuffer; + auto commandBuffer = Instance::instance->renderingCommandPool->beginSingleTimeCommandBuffer(); - vertexBuffers[0]->setName("Vertices 0"); - vertexBuffers[1]->setName("Vertices 1"); - faceBuffer->setName("Faces"); + VkBufferUsageFlags bufferUsageFlags = VK_BUFFER_USAGE_STORAGE_BUFFER_BIT | VK_BUFFER_USAGE_TRANSFER_DST_BIT | VK_BUFFER_USAGE_TRANSFER_SRC_BIT; + VmaMemoryUsage memoryUsage = VMA_MEMORY_USAGE_AUTO_PREFER_DEVICE; + + if (vertexBuffers[0] == nullptr){ + newVertexBuffers[0] = make_unique(newVertices.size() * sizeof(Vertex), + bufferUsageFlags | VK_BUFFER_USAGE_VERTEX_BUFFER_BIT, + memoryUsage, 0); + newVertexBuffers[1] = make_unique(newVertices.size() * sizeof(Vertex), + bufferUsageFlags | VK_BUFFER_USAGE_VERTEX_BUFFER_BIT, + memoryUsage, 0); + newFaceBuffer = make_unique(newFaces.size() * sizeof(Face), + bufferUsageFlags | VK_BUFFER_USAGE_INDEX_BUFFER_BIT, + memoryUsage, 0); + newEdgeBuffer = make_unique(constraintData.edges.size() * sizeof(Edge), bufferUsageFlags, memoryUsage, 0); + newTetrahedronBuffer = make_unique(constraintData.tetrahedra.size() * sizeof(Tetrahedron), bufferUsageFlags, memoryUsage, 0); + + newVertexBuffers[0]->setName("Vertices 0"); + newVertexBuffers[1]->setName("Vertices 1"); + newFaceBuffer->setName("Faces"); + newEdgeBuffer->setName("Edges"); + newTetrahedronBuffer->setName("Tetrahedra"); + + newVertexBuffers[0]->setData(newVertices.data(), 0, newVertexBuffers[0]->size, commandBuffer); + newVertexBuffers[1]->setData(newVertices.data(), 0, newVertexBuffers[1]->size, commandBuffer); + newFaceBuffer->setData(newFaces.data(), 0, newFaceBuffer->size, commandBuffer); + newEdgeBuffer->setData(constraintData.edges.data(), 0, newEdgeBuffer->size, commandBuffer); + newTetrahedronBuffer->setData(constraintData.tetrahedra.data(), 0, newTetrahedronBuffer->size, commandBuffer); + } else { + newVertexBuffers[0] = vertexBuffers[0]->appended(newVertices.data(), newVertices.size() * sizeof(Vertex), commandBuffer); + newVertexBuffers[1] = vertexBuffers[1]->appended(newVertices.data(), newVertices.size() * sizeof(Vertex), commandBuffer); + newFaceBuffer = faceBuffer->appended(newFaces.data(), newFaces.size() * sizeof(Face), commandBuffer); + newEdgeBuffer = edgeBuffer->replaced(constraintData.edges.data(), constraintData.edges.size() * sizeof(Edge), commandBuffer); + newTetrahedronBuffer = tetrahedronBuffer->replaced(constraintData.tetrahedra.data(), constraintData.tetrahedra.size() * sizeof(Tetrahedron), commandBuffer); + } VkQueue queue = Instance::instance->graphicsAndPresentQueue; Instance::instance->renderingCommandPool->endSingleTimeCommandBuffer(commandBuffer, queue); + + vertexBuffers[0] = std::move(newVertexBuffers[0]); + vertexBuffers[1] = std::move(newVertexBuffers[1]); + faceBuffer = std::move(newFaceBuffer); + edgeBuffer = std::move(newEdgeBuffer); + tetrahedronBuffer = std::move(newTetrahedronBuffer); + + descriptorPool->bindBuffer(*vertexBuffers[1 - currentDrawVertexBuffer], VK_DESCRIPTOR_TYPE_STORAGE_BUFFER, DescriptorSet::MESH, 0); + descriptorPool->bindBuffer(*faceBuffer, VK_DESCRIPTOR_TYPE_STORAGE_BUFFER, DescriptorSet::MESH, 1); + descriptorPool->bindBuffer(*edgeBuffer, VK_DESCRIPTOR_TYPE_STORAGE_BUFFER, DescriptorSet::MESH, 2); + descriptorPool->bindBuffer(*tetrahedronBuffer, VK_DESCRIPTOR_TYPE_STORAGE_BUFFER, DescriptorSet::MESH, 4); + descriptorPool->bindBuffer(*sizeInformationBuffer, VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER, DescriptorSet::MESH, 5); + + bufferWriteMutex.unlock(); } void Application::removeSoftBody(const unique_ptr &softBody) { @@ -275,13 +312,14 @@ void Application::removeSoftBody(const unique_ptr &softBody) { } void Application::updateConstraintBuffers(VkCommandBuffer commandBuffer) { - edgeBuffer = make_unique(constraintData.edges.size() * sizeof(Edge), constraintData.edges.data()); - // triangleBuffer = make_unique(); - tetrahedronBuffer = make_unique(constraintData.tetrahedra.size() * sizeof(Tetrahedron), constraintData.tetrahedra.data()); + /* + edgeBuffer = make_unique(constraintData.edges.size() * sizeof(Edge), constraintData.edges.data()); + tetrahedronBuffer = make_unique(constraintData.tetrahedra.size() * sizeof(Tetrahedron), constraintData.tetrahedra.data()); edgeBuffer.value()->setName("Edges"); // triangleBuffer->setName("Triangles"); tetrahedronBuffer.value()->setName("Tetrahedra"); + */ } void Application::createComputePipelines() { @@ -365,7 +403,7 @@ void Application::drawFrame(float dt) { VkBufferMemoryBarrier vertexBufferBarrier{}; vertexBufferBarrier.sType = VK_STRUCTURE_TYPE_BUFFER_MEMORY_BARRIER; - vertexBufferBarrier.size = vertexBuffers[currentDrawVertexBuffer]->allocationInfo.size; + vertexBufferBarrier.size = vertexBuffers[currentDrawVertexBuffer]->size; vertexBufferBarrier.offset = 0; vertexBufferBarrier.buffer = vertexBuffers[currentDrawVertexBuffer]->handle; vertexBufferBarrier.srcAccessMask = VK_ACCESS_SHADER_WRITE_BIT; @@ -474,6 +512,8 @@ void Application::recordDrawCommands(VkCommandBuffer commandBuffer) { } void Application::update() { + bufferWriteMutex.lock(); + VkCommandBufferBeginInfo beginInfo {}; beginInfo.sType = VK_STRUCTURE_TYPE_COMMAND_BUFFER_BEGIN_INFO; beginInfo.flags = 0; @@ -507,7 +547,7 @@ void Application::update() { vkBeginCommandBuffer(cmdBuffer, &beginInfo); VkBufferCopy copyRegion {}; - copyRegion.size = vertexBuffers[1 - currentDrawVertexBuffer]->allocationInfo.size; + copyRegion.size = vertexBuffers[1 - currentDrawVertexBuffer]->size; copyRegion.srcOffset = 0; copyRegion.dstOffset = 0; @@ -526,7 +566,7 @@ void Application::update() { submit.pWaitDstStageMask = &waitStage; submitMutex.lock(); - vkQueueSubmit(Instance::instance->computeAndTransferQueue, 1, &submit, transferFence->handle); + vkQueueSubmit(Instance::instance->computeAndTransferQueue, 1, &submit, computeTransferredFence->handle); submitMutex.unlock(); } @@ -535,11 +575,13 @@ void Application::update() { currentDrawVertexBuffer = 1 - currentDrawVertexBuffer; - vkWaitForFences(Instance::GetDevice(), 1, &transferFence->handle, VK_TRUE, UINT64_MAX); - vkResetFences(Instance::GetDevice(), 1, &transferFence->handle); + vkWaitForFences(Instance::GetDevice(), 1, &computeTransferredFence->handle, VK_TRUE, UINT64_MAX); + vkResetFences(Instance::GetDevice(), 1, &computeTransferredFence->handle); currentDrawVertexBuffer = 1 - currentDrawVertexBuffer; // descriptorPool->bindBuffer(*vertexBuffers[1 - currentDrawVertexBuffer], VK_DESCRIPTOR_TYPE_STORAGE_BUFFER, DescriptorSet::MESH, 0); + + bufferWriteMutex.unlock(); } uint32_t Application::GetGroupCount(uint32_t threads, uint32_t blockSize) { @@ -670,7 +712,7 @@ void Application::computePipelineBarrier(VkCommandBuffer commandBuffer) { VkBufferMemoryBarrier bufferMemoryBarrier {}; bufferMemoryBarrier.sType = VK_STRUCTURE_TYPE_BUFFER_MEMORY_BARRIER; bufferMemoryBarrier.buffer = buffer.handle; - bufferMemoryBarrier.size = buffer.allocationInfo.size; + bufferMemoryBarrier.size = buffer.size; bufferMemoryBarrier.srcAccessMask = VK_ACCESS_SHADER_WRITE_BIT; bufferMemoryBarrier.dstAccessMask = VK_ACCESS_SHADER_READ_BIT | VK_ACCESS_SHADER_WRITE_BIT; bufferMemoryBarrier.srcQueueFamilyIndex = VK_QUEUE_FAMILY_IGNORED; diff --git a/src/vulkan/buffer.cpp b/src/vulkan/buffer.cpp index d87aba3..41b56aa 100644 --- a/src/vulkan/buffer.cpp +++ b/src/vulkan/buffer.cpp @@ -22,21 +22,6 @@ Buffer::Buffer(VkDeviceSize bufferSize, VkBufferUsageFlags bufferUsageFlags, Vma vmaCreateBuffer(Instance::GetAllocator(), &bufferCreateInfo, &allocationCreateInfo, &handle, &allocation, &allocationInfo); } -Buffer::Buffer(VkDeviceSize bufferSize, void *initialData, VkBufferUsageFlags bufferUsageFlags, - VmaMemoryUsage memoryUsage, VmaAllocationCreateFlags allocationFlags) - : Buffer(bufferSize, bufferUsageFlags | VK_BUFFER_USAGE_TRANSFER_DST_BIT, memoryUsage, allocationFlags){ - - Buffer stagingBuffer( - bufferSize, - VK_BUFFER_USAGE_TRANSFER_SRC_BIT, - VMA_MEMORY_USAGE_AUTO, - VMA_ALLOCATION_CREATE_MAPPED_BIT | VMA_ALLOCATION_CREATE_HOST_ACCESS_SEQUENTIAL_WRITE_BIT - ); - - memcpy(stagingBuffer.allocationInfo.pMappedData, initialData, bufferSize); - stagingBuffer.copyTo(this); -} - Buffer::~Buffer() { vmaDestroyBuffer(Instance::GetAllocator(), handle, allocation); } @@ -48,7 +33,7 @@ void Buffer::copyTo(Buffer *buffer) const { VkCommandBuffer commandBuffer = commandPool->beginSingleTimeCommandBuffer(); VkBufferCopy copyRegion {}; - copyRegion.size = allocationInfo.size; + copyRegion.size = size; vkCmdCopyBuffer(commandBuffer, handle, buffer->handle, 1, ©Region); commandPool->endSingleTimeCommandBuffer(commandBuffer, queue); @@ -76,15 +61,23 @@ void Buffer::copyTo(Image *image) const { commandPool->endSingleTimeCommandBuffer(commandBuffer, queue); } -void Buffer::setData(void *data, VkDeviceSize offset, VkDeviceSize size, VkCommandBuffer commandBuffer) { +void Buffer::setData(void *data, VkDeviceSize offset, VkDeviceSize dataSize, VkCommandBuffer commandBuffer) { if (allocationInfo.pMappedData){ - memcpy(allocationInfo.pMappedData, reinterpret_cast(data) + offset, size); + memcpy(reinterpret_cast(allocationInfo.pMappedData) + offset, data, dataSize); + VkMemoryPropertyFlags flags; vmaGetAllocationMemoryProperties(Instance::GetAllocator(), allocation, &flags); assert(flags & VK_MEMORY_PROPERTY_HOST_COHERENT_BIT); } else { + bool singleShot = false; + if (!commandBuffer){ + commandBuffer = Instance::instance->renderingCommandPool->beginSingleTimeCommandBuffer(); + singleShot = true; + } + + auto stagingBuffer = getStagingBuffer(); - stagingBuffer->setData(data, offset, size); + stagingBuffer->setData(data, offset, dataSize); VkBufferMemoryBarrier bufferMemoryBarrier {}; bufferMemoryBarrier.sType = VK_STRUCTURE_TYPE_BUFFER_MEMORY_BARRIER; @@ -93,7 +86,7 @@ void Buffer::setData(void *data, VkDeviceSize offset, VkDeviceSize size, VkComma bufferMemoryBarrier.srcQueueFamilyIndex = VK_QUEUE_FAMILY_IGNORED; bufferMemoryBarrier.dstQueueFamilyIndex = VK_QUEUE_FAMILY_IGNORED; bufferMemoryBarrier.buffer = stagingBuffer->handle; - bufferMemoryBarrier.size = size; + bufferMemoryBarrier.size = dataSize; bufferMemoryBarrier.offset = offset; vkCmdPipelineBarrier( @@ -108,9 +101,12 @@ void Buffer::setData(void *data, VkDeviceSize offset, VkDeviceSize size, VkComma VkBufferCopy region {}; region.srcOffset = offset; region.dstOffset = offset; - region.size = size; + region.size = dataSize; vkCmdCopyBuffer(commandBuffer, stagingBuffer->handle, handle, 1, ®ion); + + if (singleShot) + Instance::instance->renderingCommandPool->endSingleTimeCommandBuffer(commandBuffer, Instance::instance->graphicsAndPresentQueue); } } @@ -119,28 +115,32 @@ void Buffer::setName(const std::string &newName) { vmaSetAllocationName(Instance::GetAllocator(), allocation, newName.data()); } -unique_ptr Buffer::Append(const Buffer &old, void *data, VkDeviceSize size, VkCommandBuffer commandBuffer) { - auto buffer = make_unique(old.size + size, - old.bufferUsageFlags | VK_BUFFER_USAGE_TRANSFER_DST_BIT, - old.memoryUsage, - old.allocationCreateFlags); +unique_ptr Buffer::appended(void *data, VkDeviceSize appendSize, VkCommandBuffer commandBuffer) const { + auto buffer = make_unique(size + appendSize, + bufferUsageFlags, + memoryUsage, + allocationCreateFlags); + buffer->setName(name); VkBufferCopy copyRegion {}; - copyRegion.size = old.size; - vkCmdCopyBuffer(commandBuffer, old.handle, buffer->handle, 1, ©Region); + copyRegion.size = size; + copyRegion.srcOffset = 0; + copyRegion.dstOffset = 0; + vkCmdCopyBuffer(commandBuffer, handle, buffer->handle, 1, ©Region); - buffer->setData(data, old.size, size, commandBuffer); + buffer->setData(data, size, appendSize, commandBuffer); return buffer; } -unique_ptr Buffer::Replace(const Buffer &old, void *data, VkDeviceSize size, VkCommandBuffer commandBuffer) { - auto buffer = make_unique(size, - old.bufferUsageFlags | VK_BUFFER_USAGE_TRANSFER_DST_BIT, - old.memoryUsage, - old.allocationCreateFlags); +unique_ptr Buffer::replaced(void *data, VkDeviceSize replaceSize, VkCommandBuffer commandBuffer) const { + auto buffer = make_unique(replaceSize, + bufferUsageFlags, + memoryUsage, + allocationCreateFlags); + buffer->setName(name); - buffer->setData(data, 0, size, commandBuffer); + buffer->setData(data, 0, replaceSize, commandBuffer); return buffer; } @@ -150,10 +150,12 @@ shared_ptr Buffer::getStagingBuffer() { return stagingBufferOptional.value(); stagingBufferOptional = make_shared( - allocationInfo.size, + size, VK_BUFFER_USAGE_TRANSFER_SRC_BIT, VMA_MEMORY_USAGE_AUTO, VMA_ALLOCATION_CREATE_MAPPED_BIT | VMA_ALLOCATION_CREATE_HOST_ACCESS_SEQUENTIAL_WRITE_BIT); + stagingBufferOptional.value()->setName(name + " staging"); + return stagingBufferOptional.value(); } diff --git a/src/vulkan/image.cpp b/src/vulkan/image.cpp index 571ad00..e3bab20 100644 --- a/src/vulkan/image.cpp +++ b/src/vulkan/image.cpp @@ -31,7 +31,9 @@ Image::Image(uint32_t width, uint32_t height, VkFormat format, VkImageTiling til Image::Image(void *initialData, VkDeviceSize size, uint32_t width, uint32_t height, VkFormat format, VkImageTiling tiling, VkImageUsageFlags usage) : Image(width, height, format, tiling, usage){ - Buffer stagingBuffer(size, initialData, size, VK_BUFFER_USAGE_TRANSFER_SRC_BIT, VMA_MEMORY_USAGE_AUTO_PREFER_DEVICE, 0); + Buffer stagingBuffer(size, VK_BUFFER_USAGE_TRANSFER_SRC_BIT, VMA_MEMORY_USAGE_AUTO_PREFER_DEVICE, + VMA_ALLOCATION_CREATE_MAPPED_BIT | VMA_ALLOCATION_CREATE_HOST_ACCESS_SEQUENTIAL_WRITE_BIT); + stagingBuffer.setData(initialData, 0, size); transitionLayout(VK_IMAGE_LAYOUT_UNDEFINED, VK_IMAGE_LAYOUT_TRANSFER_DST_OPTIMAL); stagingBuffer.copyTo(this);