From 2d9bce32b50f77cf4f7e1d43f2c7dff9b099a977 Mon Sep 17 00:00:00 2001 From: Benjamin Kraft Date: Mon, 7 Oct 2024 20:47:50 +0200 Subject: [PATCH] refactoring --- include/application.hpp | 27 ++--------------- src/application.cpp | 64 +++++++++++++++++++++++++++-------------- 2 files changed, 44 insertions(+), 47 deletions(-) diff --git a/include/application.hpp b/include/application.hpp index cfa71a8..2f017e8 100644 --- a/include/application.hpp +++ b/include/application.hpp @@ -57,11 +57,6 @@ private: unique_ptr descriptorPool; unique_ptr graphicsPipeline; - struct CameraUniformData { - glm::mat4 view; - glm::mat4 projection; - glm::vec2 viewport; - }; unique_ptr cameraUniformBuffer; unique_ptr camera; @@ -78,29 +73,16 @@ private: ConstraintData constraintData {}; vector> softBodies; - struct SizeInformation { - uint32_t vertexCount; - uint32_t faceCount; - uint32_t edgeCount; - uint32_t triangleCount; - uint32_t tetrahedronCount; - }; unique_ptr sizeInformationBuffer; - struct SimulationProperties { + struct SimulationUniformData { glm::vec3 gravity; // Delta time in seconds float dt; uint32_t k; }; unique_ptr propertiesBuffer; - SimulationProperties simulationProperties {}; - - struct GrabPushConstants { - uint32_t state; - alignas(8) glm::vec2 screenPosition; - glm::vec2 screenDelta; - }; + SimulationUniformData simulationUniformData {}; void createComputePipelines(); unique_ptr grabPipeline; @@ -114,11 +96,6 @@ private: void recordGrabCommands(VkCommandBuffer cmdBuffer); void recordPBDCommands(VkCommandBuffer cmdBuffer); void recordNormalCommands(VkCommandBuffer cmdBuffer); - struct PBDPushData { - uint32_t state; - ConstraintData::Partition edgePartition; - ConstraintData::Partition tetrahedronPartition; - }; void update(); diff --git a/src/application.cpp b/src/application.cpp index 583dc6b..b0e5994 100644 --- a/src/application.cpp +++ b/src/application.cpp @@ -15,6 +15,32 @@ #include "timer.hpp" #include "grabber.hpp" +struct SizesUniformData { + uint32_t vertexCount; + uint32_t faceCount; + uint32_t edgeCount; + uint32_t triangleCount; + uint32_t tetrahedronCount; +}; + +struct CameraUniformData { + glm::mat4 view; + glm::mat4 projection; + glm::vec2 viewport; +}; + +struct GrabPushData { + uint32_t state; + alignas(8) glm::vec2 screenPosition; + glm::vec2 screenDelta; +}; + +struct PBDPushData { + uint32_t state; + ConstraintData::Partition edgePartition; + ConstraintData::Partition tetrahedronPartition; +}; + Application::Application() { createSyncObjects(); swapchain = make_unique(); @@ -46,22 +72,22 @@ Application::Application() { createMeshBuffers(); - SizeInformation sizeInformation {}; + SizesUniformData sizeInformation {}; sizeInformation.vertexCount = vertexBuffers[0]->size / sizeof(Vertex); sizeInformation.faceCount = faceBuffer->size / sizeof(Face); sizeInformationBuffer = make_unique( - sizeof(SizeInformation), &sizeInformation, sizeof(sizeInformation), + sizeof(SizesUniformData), &sizeInformation, sizeof(sizeInformation), VK_BUFFER_USAGE_UNIFORM_BUFFER_BIT, VMA_MEMORY_USAGE_AUTO_PREFER_DEVICE, 0); sizeInformationBuffer->setName("Sizes"); - simulationProperties.gravity = {0, -9.81, 0}; - simulationProperties.k = 10; - simulationProperties.dt = 1.f / 60.f; + simulationUniformData.gravity = {0, -9.81, 0}; + simulationUniformData.k = 10; + simulationUniformData.dt = 1.f / 60.f; propertiesBuffer = make_unique( - sizeof(SimulationProperties), &simulationProperties, sizeof(simulationProperties), + sizeof(SimulationUniformData), &simulationUniformData, sizeof(simulationUniformData), VK_BUFFER_USAGE_UNIFORM_BUFFER_BIT, VMA_MEMORY_USAGE_AUTO_PREFER_DEVICE, 0); propertiesBuffer->setName("Simulation properties"); @@ -97,9 +123,9 @@ void Application::mainLoop() { update(); auto t2 = system_clock::now(); - auto updateDuration = duration(t2 - t1); - auto sleepDuration = duration(simulationProperties.dt); - std::this_thread::sleep_for(sleepDuration - updateDuration); + auto measuredUpdateDuration = duration(t2 - t1); + auto requestedUpdateDuration = duration(simulationUniformData.dt); + std::this_thread::sleep_for(requestedUpdateDuration - measuredUpdateDuration); } }); @@ -265,7 +291,7 @@ void Application::createComputePipelines() { pushRanges.push_back({ .stageFlags = VK_SHADER_STAGE_COMPUTE_BIT, .offset = 0, - .size = sizeof(GrabPushConstants) + .size = sizeof(GrabPushData) }); grabPipeline = unique_ptr(new ComputePipeline("shaders/grab.spv", layouts, pushRanges)); } @@ -299,7 +325,6 @@ void Application::createComputePipelines() { } - void Application::updateCameraBuffer() { CameraUniformData ubo {}; ubo.view = camera->view(); @@ -308,11 +333,6 @@ void Application::updateCameraBuffer() { ubo.viewport = camera->viewport(); memcpy(cameraUniformBuffer->allocationInfo.pMappedData, &ubo, sizeof(CameraUniformData)); - - vmaFlushAllocation(Instance::GetAllocator(), - cameraUniformBuffer->allocation, - cameraUniformBuffer->allocationInfo.offset, - cameraUniformBuffer->allocationInfo.size); } void Application::drawFrame(float dt) { @@ -544,12 +564,12 @@ void Application::recordGrabCommands(VkCommandBuffer cmdBuffer) { vkCmdBindDescriptorSets(cmdBuffer, VK_PIPELINE_BIND_POINT_COMPUTE, grabPipeline->layout, 1, 1, &descriptorPool->sets[DescriptorSet::WORLD], 0, nullptr); vkCmdBindDescriptorSets(cmdBuffer, VK_PIPELINE_BIND_POINT_COMPUTE, grabPipeline->layout, 2, 1, &descriptorPool->sets[DescriptorSet::SIMULATION], 0, nullptr); - GrabPushConstants pushConstants {}; + GrabPushData pushConstants {}; if (grabber->started()){ pushConstants.state = 0; pushConstants.screenPosition = grabber->previousCursorPosition; - vkCmdPushConstants(cmdBuffer, grabPipeline->layout, VK_SHADER_STAGE_COMPUTE_BIT, 0, sizeof(GrabPushConstants), &pushConstants); + vkCmdPushConstants(cmdBuffer, grabPipeline->layout, VK_SHADER_STAGE_COMPUTE_BIT, 0, sizeof(GrabPushData), &pushConstants); uint32_t faceInvocations = GetGroupCount(faceBuffer->size / sizeof(Face), BLOCK_SIZE_GRAB); @@ -559,7 +579,7 @@ void Application::recordGrabCommands(VkCommandBuffer cmdBuffer) { 0, nullptr, 0, nullptr); pushConstants.state = 1; - vkCmdPushConstants(cmdBuffer, grabPipeline->layout, VK_SHADER_STAGE_COMPUTE_BIT, 0, sizeof(GrabPushConstants), &pushConstants); + vkCmdPushConstants(cmdBuffer, grabPipeline->layout, VK_SHADER_STAGE_COMPUTE_BIT, 0, sizeof(GrabPushData), &pushConstants); vkCmdDispatch(cmdBuffer, 1, 1, 1); } @@ -570,7 +590,7 @@ void Application::recordGrabCommands(VkCommandBuffer cmdBuffer) { if (grabber->moved(screenDelta)){ pushConstants.state = 2; pushConstants.screenDelta = screenDelta; - vkCmdPushConstants(cmdBuffer, grabPipeline->layout, VK_SHADER_STAGE_COMPUTE_BIT, 0, sizeof(GrabPushConstants), &pushConstants); + vkCmdPushConstants(cmdBuffer, grabPipeline->layout, VK_SHADER_STAGE_COMPUTE_BIT, 0, sizeof(GrabPushData), &pushConstants); vkCmdDispatch(cmdBuffer, 1, 1, 1); } @@ -579,7 +599,7 @@ void Application::recordGrabCommands(VkCommandBuffer cmdBuffer) { if (grabber->stopped()){ pushConstants.state = 3; - vkCmdPushConstants(cmdBuffer, grabPipeline->layout, VK_SHADER_STAGE_COMPUTE_BIT, 0, sizeof(GrabPushConstants), &pushConstants); + vkCmdPushConstants(cmdBuffer, grabPipeline->layout, VK_SHADER_STAGE_COMPUTE_BIT, 0, sizeof(GrabPushData), &pushConstants); vkCmdDispatch(cmdBuffer, 1, 1, 1); } @@ -601,7 +621,7 @@ void Application::recordPBDCommands(VkCommandBuffer cmdBuffer) { uint32_t state; - for (size_t i = 0; i < simulationProperties.k; i++){ + for (size_t i = 0; i < simulationUniformData.k; i++){ state = 0; vkCmdPushConstants(cmdBuffer, pbdPipeline->layout, VK_SHADER_STAGE_COMPUTE_BIT, 0, sizeof(uint32_t), &state); vkCmdDispatch(cmdBuffer, vertexGroupCount, 1, 1);