|
|
@ -29,6 +29,12 @@ struct CameraUniformData { |
|
|
|
glm::vec2 viewport; |
|
|
|
glm::vec2 viewport; |
|
|
|
}; |
|
|
|
}; |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
struct SimulationUniformData { |
|
|
|
|
|
|
|
glm::vec3 gravity; |
|
|
|
|
|
|
|
float dt; |
|
|
|
|
|
|
|
uint32_t k; |
|
|
|
|
|
|
|
}; |
|
|
|
|
|
|
|
|
|
|
|
struct GrabPushData { |
|
|
|
struct GrabPushData { |
|
|
|
uint32_t state; |
|
|
|
uint32_t state; |
|
|
|
alignas(8) glm::vec2 screenPosition; |
|
|
|
alignas(8) glm::vec2 screenPosition; |
|
|
@ -49,14 +55,13 @@ Application::Application() { |
|
|
|
swapchain->renderPass, |
|
|
|
swapchain->renderPass, |
|
|
|
{descriptorPool->layouts[DescriptorSet::WORLD]})); |
|
|
|
{descriptorPool->layouts[DescriptorSet::WORLD]})); |
|
|
|
|
|
|
|
|
|
|
|
VkDeviceSize bufferSize = sizeof(CameraUniformData); |
|
|
|
cameraUniformBuffer = make_unique<Buffer>(sizeof(CameraUniformData), |
|
|
|
|
|
|
|
|
|
|
|
cameraUniformBuffer = make_unique<Buffer>(bufferSize, |
|
|
|
|
|
|
|
VK_BUFFER_USAGE_UNIFORM_BUFFER_BIT, |
|
|
|
VK_BUFFER_USAGE_UNIFORM_BUFFER_BIT, |
|
|
|
VMA_MEMORY_USAGE_AUTO_PREFER_DEVICE, |
|
|
|
VMA_MEMORY_USAGE_AUTO_PREFER_DEVICE, |
|
|
|
VMA_ALLOCATION_CREATE_HOST_ACCESS_SEQUENTIAL_WRITE_BIT | VMA_ALLOCATION_CREATE_MAPPED_BIT); |
|
|
|
VMA_ALLOCATION_CREATE_HOST_ACCESS_SEQUENTIAL_WRITE_BIT | VMA_ALLOCATION_CREATE_MAPPED_BIT); |
|
|
|
cameraUniformBuffer->setName("Camera"); |
|
|
|
cameraUniformBuffer->setName("Camera"); |
|
|
|
camera = make_unique<Camera>(swapchain->extent); |
|
|
|
camera = make_unique<Camera>(swapchain->extent); |
|
|
|
|
|
|
|
updateCameraBuffer(); |
|
|
|
|
|
|
|
|
|
|
|
struct GrabInformation { |
|
|
|
struct GrabInformation { |
|
|
|
float originalInverseMass; |
|
|
|
float originalInverseMass; |
|
|
@ -82,16 +87,19 @@ Application::Application() { |
|
|
|
VMA_MEMORY_USAGE_AUTO_PREFER_DEVICE, 0); |
|
|
|
VMA_MEMORY_USAGE_AUTO_PREFER_DEVICE, 0); |
|
|
|
sizeInformationBuffer->setName("Sizes"); |
|
|
|
sizeInformationBuffer->setName("Sizes"); |
|
|
|
|
|
|
|
|
|
|
|
simulationUniformData.gravity = {0, -9.81, 0}; |
|
|
|
SimulationUniformData simulationUniformData { |
|
|
|
simulationUniformData.k = 10; |
|
|
|
.gravity = {0, -9.81, 0}, |
|
|
|
simulationUniformData.dt = 1.f / 60.f; |
|
|
|
.dt = 1.f / 60.f, |
|
|
|
|
|
|
|
.k = 10, |
|
|
|
|
|
|
|
}; |
|
|
|
|
|
|
|
|
|
|
|
propertiesBuffer = make_unique<Buffer>( |
|
|
|
simulationPropertiesBuffer = make_unique<Buffer>( |
|
|
|
sizeof(SimulationUniformData), &simulationUniformData, sizeof(simulationUniformData), |
|
|
|
sizeof(SimulationUniformData), |
|
|
|
VK_BUFFER_USAGE_UNIFORM_BUFFER_BIT, |
|
|
|
VK_BUFFER_USAGE_UNIFORM_BUFFER_BIT, |
|
|
|
VMA_MEMORY_USAGE_AUTO_PREFER_DEVICE, 0); |
|
|
|
VMA_MEMORY_USAGE_AUTO_PREFER_DEVICE, |
|
|
|
propertiesBuffer->setName("Simulation properties"); |
|
|
|
VMA_ALLOCATION_CREATE_HOST_ACCESS_SEQUENTIAL_WRITE_BIT | VMA_ALLOCATION_CREATE_MAPPED_BIT); |
|
|
|
|
|
|
|
simulationPropertiesBuffer->setName("Simulation properties"); |
|
|
|
|
|
|
|
simulationPropertiesBuffer->access<SimulationUniformData>() = simulationUniformData; |
|
|
|
|
|
|
|
|
|
|
|
descriptorPool->bindBuffer(*cameraUniformBuffer, VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER, DescriptorSet::WORLD, 0); |
|
|
|
descriptorPool->bindBuffer(*cameraUniformBuffer, VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER, DescriptorSet::WORLD, 0); |
|
|
|
|
|
|
|
|
|
|
@ -102,7 +110,7 @@ Application::Application() { |
|
|
|
descriptorPool->bindBuffer(*tetrahedronBuffer, VK_DESCRIPTOR_TYPE_STORAGE_BUFFER, DescriptorSet::MESH, 4); |
|
|
|
descriptorPool->bindBuffer(*tetrahedronBuffer, VK_DESCRIPTOR_TYPE_STORAGE_BUFFER, DescriptorSet::MESH, 4); |
|
|
|
descriptorPool->bindBuffer(*sizeInformationBuffer, VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER, DescriptorSet::MESH, 5); |
|
|
|
descriptorPool->bindBuffer(*sizeInformationBuffer, VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER, DescriptorSet::MESH, 5); |
|
|
|
|
|
|
|
|
|
|
|
descriptorPool->bindBuffer(*propertiesBuffer, VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER, DescriptorSet::SIMULATION, 0); |
|
|
|
descriptorPool->bindBuffer(*simulationPropertiesBuffer, VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER, DescriptorSet::SIMULATION, 0); |
|
|
|
descriptorPool->bindBuffer(*grabBuffer, VK_DESCRIPTOR_TYPE_STORAGE_BUFFER, DescriptorSet::SIMULATION, 1); |
|
|
|
descriptorPool->bindBuffer(*grabBuffer, VK_DESCRIPTOR_TYPE_STORAGE_BUFFER, DescriptorSet::SIMULATION, 1); |
|
|
|
|
|
|
|
|
|
|
|
createComputePipelines(); |
|
|
|
createComputePipelines(); |
|
|
@ -124,16 +132,15 @@ void Application::mainLoop() { |
|
|
|
auto t2 = system_clock::now(); |
|
|
|
auto t2 = system_clock::now(); |
|
|
|
|
|
|
|
|
|
|
|
auto measuredUpdateDuration = duration<float>(t2 - t1); |
|
|
|
auto measuredUpdateDuration = duration<float>(t2 - t1); |
|
|
|
auto requestedUpdateDuration = duration<float>(simulationUniformData.dt); |
|
|
|
auto requestedUpdateDuration = duration<float>(simulationPropertiesBuffer->access<SimulationUniformData>().dt); |
|
|
|
std::this_thread::sleep_for(requestedUpdateDuration - measuredUpdateDuration); |
|
|
|
std::this_thread::sleep_for(requestedUpdateDuration - measuredUpdateDuration); |
|
|
|
} |
|
|
|
} |
|
|
|
}); |
|
|
|
}); |
|
|
|
|
|
|
|
|
|
|
|
auto t2 = system_clock::now(); |
|
|
|
|
|
|
|
auto t1 = system_clock::now(); |
|
|
|
auto t1 = system_clock::now(); |
|
|
|
while (!glfwWindowShouldClose(Instance::instance->window)){ |
|
|
|
while (!glfwWindowShouldClose(Instance::instance->window)){ |
|
|
|
glfwPollEvents(); |
|
|
|
glfwPollEvents(); |
|
|
|
t2 = system_clock::now(); |
|
|
|
auto t2 = system_clock::now(); |
|
|
|
float seconds = duration<float>(t2 - t1).count(); |
|
|
|
float seconds = duration<float>(t2 - t1).count(); |
|
|
|
t1 = system_clock::now(); |
|
|
|
t1 = system_clock::now(); |
|
|
|
drawFrame(seconds); |
|
|
|
drawFrame(seconds); |
|
|
@ -326,13 +333,11 @@ void Application::createComputePipelines() { |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
void Application::updateCameraBuffer() { |
|
|
|
void Application::updateCameraBuffer() { |
|
|
|
CameraUniformData ubo {}; |
|
|
|
CameraUniformData& data = cameraUniformBuffer->access<CameraUniformData>(); |
|
|
|
ubo.view = camera->view(); |
|
|
|
data.view = camera->view(); |
|
|
|
ubo.projection = camera->projection(); |
|
|
|
data.projection = camera->projection(); |
|
|
|
ubo.projection[1][1] *= -1; |
|
|
|
data.projection[1][1] *= -1; |
|
|
|
ubo.viewport = camera->viewport(); |
|
|
|
data.viewport = camera->viewport(); |
|
|
|
|
|
|
|
|
|
|
|
memcpy(cameraUniformBuffer->allocationInfo.pMappedData, &ubo, sizeof(CameraUniformData)); |
|
|
|
|
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
void Application::drawFrame(float dt) { |
|
|
|
void Application::drawFrame(float dt) { |
|
|
@ -605,7 +610,9 @@ void Application::recordPBDCommands(VkCommandBuffer cmdBuffer) { |
|
|
|
|
|
|
|
|
|
|
|
uint32_t state; |
|
|
|
uint32_t state; |
|
|
|
|
|
|
|
|
|
|
|
for (size_t i = 0; i < simulationUniformData.k; i++){ |
|
|
|
uint32_t k = simulationPropertiesBuffer->access<SimulationUniformData>().k; |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
for (size_t i = 0; i < k; i++){ |
|
|
|
state = 0; |
|
|
|
state = 0; |
|
|
|
vkCmdPushConstants(cmdBuffer, pbdPipeline->layout, VK_SHADER_STAGE_COMPUTE_BIT, 0, sizeof(uint32_t), &state); |
|
|
|
vkCmdPushConstants(cmdBuffer, pbdPipeline->layout, VK_SHADER_STAGE_COMPUTE_BIT, 0, sizeof(uint32_t), &state); |
|
|
|
vkCmdDispatch(cmdBuffer, vertexGroupCount, 1, 1); |
|
|
|
vkCmdDispatch(cmdBuffer, vertexGroupCount, 1, 1); |
|
|
|