better buffers and naming

feature/softbody-runtime-control
Benjamin Kraft 3 months ago
parent c8217b1c20
commit 997223c330
  1. 9
      include/application.hpp
  2. 12
      include/vulkan/buffer.hpp
  3. 53
      src/application.cpp
  4. 22
      src/vulkan/buffer.cpp

@ -75,14 +75,7 @@ private:
unique_ptr<Buffer> sizeInformationBuffer; unique_ptr<Buffer> sizeInformationBuffer;
struct SimulationUniformData { unique_ptr<Buffer> simulationPropertiesBuffer;
glm::vec3 gravity;
// Delta time in seconds
float dt;
uint32_t k;
};
unique_ptr<Buffer> propertiesBuffer;
SimulationUniformData simulationUniformData {};
void createComputePipelines(); void createComputePipelines();
unique_ptr<ComputePipeline> grabPipeline; unique_ptr<ComputePipeline> grabPipeline;

@ -7,16 +7,20 @@
class Buffer { class Buffer {
public: public:
explicit Buffer(VkDeviceSize size, VkBufferUsageFlags bufferUsage, explicit Buffer(VkDeviceSize bufferSize, VkBufferUsageFlags bufferUsage,
VmaMemoryUsage memoryUsage, VmaAllocationCreateFlags flags); VmaMemoryUsage memoryUsage, VmaAllocationCreateFlags vmaAllocationFlags);
explicit Buffer(VkDeviceSize size, void* data, VkDeviceSize dataSize, VkBufferUsageFlags bufferUsage, explicit Buffer(VkDeviceSize bufferSize, void* initialData, VkDeviceSize initialDataSize, VkBufferUsageFlags bufferUsage,
VmaMemoryUsage memoryUsage, VmaAllocationCreateFlags flags); VmaMemoryUsage memoryUsage, VmaAllocationCreateFlags vmaAllocationFlags);
~Buffer(); ~Buffer();
VkBuffer handle = VK_NULL_HANDLE; VkBuffer handle = VK_NULL_HANDLE;
VmaAllocation allocation = VK_NULL_HANDLE; VmaAllocation allocation = VK_NULL_HANDLE;
VmaAllocationInfo allocationInfo {}; VmaAllocationInfo allocationInfo {};
VkDeviceSize size; VkDeviceSize size;
void setName(const std::string& name); void setName(const std::string& name);
template <typename T>
T& access(){
return *reinterpret_cast<T*>(allocationInfo.pMappedData);
}
Buffer(const Buffer& other) = delete; Buffer(const Buffer& other) = delete;
Buffer& operator =(const Buffer& other) = delete; Buffer& operator =(const Buffer& other) = delete;
private: private:

@ -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);

@ -6,32 +6,32 @@
#include "vk_mem_alloc.h" #include "vk_mem_alloc.h"
Buffer::Buffer(VkDeviceSize size, VkBufferUsageFlags bufferUsage, VmaMemoryUsage memoryUsage, VmaAllocationCreateFlags flags) : size(size) { Buffer::Buffer(VkDeviceSize bufferSize, VkBufferUsageFlags bufferUsage, VmaMemoryUsage memoryUsage, VmaAllocationCreateFlags vmaAllocationFlags) : size(bufferSize) {
VkBufferCreateInfo bufferCreateInfo {}; VkBufferCreateInfo bufferCreateInfo {};
bufferCreateInfo.sType = VK_STRUCTURE_TYPE_BUFFER_CREATE_INFO; bufferCreateInfo.sType = VK_STRUCTURE_TYPE_BUFFER_CREATE_INFO;
bufferCreateInfo.size = size; bufferCreateInfo.size = bufferSize;
bufferCreateInfo.usage = bufferUsage; bufferCreateInfo.usage = bufferUsage;
bufferCreateInfo.sharingMode = VK_SHARING_MODE_EXCLUSIVE; bufferCreateInfo.sharingMode = VK_SHARING_MODE_EXCLUSIVE;
VmaAllocationCreateInfo allocationCreateInfo {}; VmaAllocationCreateInfo allocationCreateInfo {};
allocationCreateInfo.usage = memoryUsage; allocationCreateInfo.usage = memoryUsage;
allocationCreateInfo.flags = flags; allocationCreateInfo.flags = vmaAllocationFlags;
vmaCreateBuffer(Instance::GetAllocator(), &bufferCreateInfo, &allocationCreateInfo, &handle, &allocation, &allocationInfo); vmaCreateBuffer(Instance::GetAllocator(), &bufferCreateInfo, &allocationCreateInfo, &handle, &allocation, &allocationInfo);
} }
Buffer::Buffer(VkDeviceSize size, void *data, VkDeviceSize dataSize, VkBufferUsageFlags bufferUsage, Buffer::Buffer(VkDeviceSize bufferSize, void *initialData, VkDeviceSize initialDataSize, VkBufferUsageFlags bufferUsage,
VmaMemoryUsage memoryUsage, VmaAllocationCreateFlags flags) VmaMemoryUsage memoryUsage, VmaAllocationCreateFlags vmaAllocationFlags)
: Buffer(size, bufferUsage | VK_BUFFER_USAGE_TRANSFER_DST_BIT, memoryUsage, flags){ : Buffer(bufferSize, bufferUsage | VK_BUFFER_USAGE_TRANSFER_DST_BIT, memoryUsage, vmaAllocationFlags){
Buffer stagingBuffer( Buffer stagingBuffer(
size, bufferSize,
VK_BUFFER_USAGE_TRANSFER_SRC_BIT, VK_BUFFER_USAGE_TRANSFER_SRC_BIT,
VMA_MEMORY_USAGE_AUTO, VMA_MEMORY_USAGE_AUTO,
VMA_ALLOCATION_CREATE_MAPPED_BIT | VMA_ALLOCATION_CREATE_HOST_ACCESS_SEQUENTIAL_WRITE_BIT VMA_ALLOCATION_CREATE_MAPPED_BIT | VMA_ALLOCATION_CREATE_HOST_ACCESS_SEQUENTIAL_WRITE_BIT
); );
memcpy(stagingBuffer.allocationInfo.pMappedData, data, dataSize); memcpy(stagingBuffer.allocationInfo.pMappedData, initialData, initialDataSize);
stagingBuffer.copyTo(this); stagingBuffer.copyTo(this);
} }
@ -43,7 +43,7 @@ void Buffer::copyTo(Buffer *dst) {
VkCommandBufferAllocateInfo allocateInfo {}; VkCommandBufferAllocateInfo allocateInfo {};
allocateInfo.sType = VK_STRUCTURE_TYPE_COMMAND_BUFFER_ALLOCATE_INFO; allocateInfo.sType = VK_STRUCTURE_TYPE_COMMAND_BUFFER_ALLOCATE_INFO;
allocateInfo.level = VK_COMMAND_BUFFER_LEVEL_PRIMARY; allocateInfo.level = VK_COMMAND_BUFFER_LEVEL_PRIMARY;
allocateInfo.commandPool = Instance::instance->renderingCommandPool->handle; allocateInfo.commandPool = Instance::instance->computeCommandPool->handle;
allocateInfo.commandBufferCount = 1; allocateInfo.commandBufferCount = 1;
VkCommandBuffer commandBuffer; VkCommandBuffer commandBuffer;
@ -64,10 +64,10 @@ void Buffer::copyTo(Buffer *dst) {
submitInfo.commandBufferCount = 1; submitInfo.commandBufferCount = 1;
submitInfo.pCommandBuffers = &commandBuffer; submitInfo.pCommandBuffers = &commandBuffer;
vkQueueSubmit(Instance::instance->graphicsAndPresentQueue, 1, &submitInfo, VK_NULL_HANDLE); vkQueueSubmit(Instance::instance->computeAndTransferQueue, 1, &submitInfo, VK_NULL_HANDLE);
vkDeviceWaitIdle(Instance::GetDevice()); vkDeviceWaitIdle(Instance::GetDevice());
vkFreeCommandBuffers(Instance::GetDevice(), Instance::instance->renderingCommandPool->handle, 1, &commandBuffer); vkFreeCommandBuffers(Instance::GetDevice(), Instance::instance->computeCommandPool->handle, 1, &commandBuffer);
} }
void Buffer::setName(const std::string &name) { void Buffer::setName(const std::string &name) {

Loading…
Cancel
Save