code cleanup

main
Benjamin Kraft 3 months ago
parent c3ad1f7009
commit 3e45f2eefd
  1. 18
      include/application.hpp
  2. 6
      include/camera.hpp
  3. 12
      include/constraints.hpp
  4. 15
      include/input.hpp
  5. 8
      include/soft_body.hpp
  6. 28
      include/vulkan/buffer.hpp
  7. 5
      include/vulkan/descriptor_pool.hpp
  8. 4
      include/vulkan/image.hpp
  9. 32
      include/vulkan/instance.hpp
  10. 12
      include/vulkan/pipeline.hpp
  11. 9
      include/vulkan/swapchain.hpp
  12. 43
      src/application.cpp
  13. 19
      src/vulkan/instance.cpp

@ -26,19 +26,33 @@ using std::vector;
using std::optional; using std::optional;
class SoftBody; class SoftBody;
class Instance; class Instance;
class Swapchain; class Swapchain;
class GraphicsPipeline; class GraphicsPipeline;
class Buffer; class Buffer;
class Buffer; class Buffer;
class CommandPool; class CommandPool;
class Image; class Image;
class ComputePipeline; class ComputePipeline;
class Fence; class Fence;
class Semaphore; class Semaphore;
class Camera; class Camera;
class DescriptorPool; class DescriptorPool;
class Grabber; class Grabber;
struct SizesUniformData; struct SizesUniformData;
class Application { class Application {
@ -79,8 +93,8 @@ private:
unique_ptr<Grabber> grabber; unique_ptr<Grabber> grabber;
unique_ptr<Buffer> grabBuffer; unique_ptr<Buffer> grabBuffer;
void addSoftBody(const std::string& modelFile, size_t count=1); void addSoftBody(const std::string &modelFile, size_t count = 1);
void removeSoftBody(const unique_ptr<SoftBody>& softBody); void removeSoftBody(const unique_ptr<SoftBody> &softBody);
void updateConstraintBuffers(VkCommandBuffer commandBuffer); void updateConstraintBuffers(VkCommandBuffer commandBuffer);
size_t currentDrawVertexBuffer = 0; size_t currentDrawVertexBuffer = 0;

@ -18,14 +18,14 @@ private:
float near = 0.1; float near = 0.1;
float far = 100; float far = 100;
VkExtent2D& extent; VkExtent2D &extent;
public: public:
explicit Camera(VkExtent2D& extent); explicit Camera(VkExtent2D &extent);
void update(float dt); void update(float dt);
glm::mat4 view() const; glm::mat4 view() const;
glm::mat4 projection() const; glm::mat4 projection() const;
glm::vec2 viewport() const; glm::vec2 viewport() const;
glm::vec3 localToWorld(const glm::vec3& direction) const; glm::vec3 localToWorld(const glm::vec3 &direction) const;
protected: protected:
void mouseMoved(const glm::vec2 &delta) override; void mouseMoved(const glm::vec2 &delta) override;
}; };

@ -51,7 +51,7 @@ struct ConstraintData {
void recordNewPartition(); void recordNewPartition();
void writePartitionInformation(); void writePartitionInformation();
void insert(const ConstraintData& other); void insert(const ConstraintData &other);
private: private:
uint32_t prePartitionEdgeCount; uint32_t prePartitionEdgeCount;
uint32_t prePartitionTetrahedronCount; uint32_t prePartitionTetrahedronCount;
@ -62,20 +62,24 @@ private:
struct Constraint { struct Constraint {
virtual ~Constraint() {} virtual ~Constraint() {}
virtual void writeData(ConstraintData& dataLists) const {};
virtual void writeData(ConstraintData &dataLists) const {};
}; };
struct DistanceConstraint : Edge, Constraint { struct DistanceConstraint : Edge, Constraint {
explicit DistanceConstraint(const Edge &edge) : Edge(edge) {} explicit DistanceConstraint(const Edge &edge) : Edge(edge) {}
void writeData(ConstraintData &dataLists) const override; void writeData(ConstraintData &dataLists) const override;
}; };
struct AreaConstraint : Triangle, Constraint { struct AreaConstraint : Triangle, Constraint {
explicit AreaConstraint(const Triangle& triangle) : Triangle(triangle) {} explicit AreaConstraint(const Triangle &triangle) : Triangle(triangle) {}
void writeData(ConstraintData &dataLists) const override; void writeData(ConstraintData &dataLists) const override;
}; };
struct VolumeConstraint : Tetrahedron, Constraint { struct VolumeConstraint : Tetrahedron, Constraint {
explicit VolumeConstraint(const Tetrahedron& tetrahedron) : Tetrahedron(tetrahedron) {} explicit VolumeConstraint(const Tetrahedron &tetrahedron) : Tetrahedron(tetrahedron) {}
void writeData(ConstraintData &dataLists) const override; void writeData(ConstraintData &dataLists) const override;
}; };

@ -6,27 +6,28 @@
#include "glm/vec2.hpp" #include "glm/vec2.hpp"
class Listener; class Listener;
class MouseListener; class MouseListener;
class Input { class Input {
friend Listener; friend Listener;
friend MouseListener; friend MouseListener;
public: public:
explicit Input(GLFWwindow* window); explicit Input(GLFWwindow *window);
static Input* instance; static Input *instance;
static std::map<int, bool> KeyIsDown; static std::map<int, bool> KeyIsDown;
static std::map<int, bool> MouseButtonIsDown; static std::map<int, bool> MouseButtonIsDown;
glm::vec2 currentCursorPosition {}; glm::vec2 currentCursorPosition {};
private: private:
GLFWwindow* window; GLFWwindow *window;
std::set<Listener*> listeners; std::set<Listener *> listeners;
void keyPressed(int key); void keyPressed(int key);
void keyReleased(int key); void keyReleased(int key);
void mouseButtonPressed(int button); void mouseButtonPressed(int button);
void mouseButtonReleased(int button); void mouseButtonReleased(int button);
void mouseMoved(const glm::vec2& newCursorPosition); void mouseMoved(const glm::vec2 &newCursorPosition);
}; };
class Listener { class Listener {
@ -41,7 +42,9 @@ class KeyListener : Listener {
class MouseListener : public Listener { class MouseListener : public Listener {
public: public:
virtual void mouseMoved(const glm::vec2& delta) {}; virtual void mouseMoved(const glm::vec2 &delta) {};
virtual void mouseButtonPressed(int button) {}; virtual void mouseButtonPressed(int button) {};
virtual void mouseButtonReleased(int button) {}; virtual void mouseButtonReleased(int button) {};
}; };

@ -14,13 +14,13 @@ using std::vector;
struct Vertex; struct Vertex;
typedef unordered_map<const Constraint*, vector<const Constraint *>> Graph; typedef unordered_map<const Constraint *, vector<const Constraint *>> Graph;
class Mesh; class Mesh;
class SoftBody { class SoftBody {
public: public:
explicit SoftBody(Mesh* mesh, float edgeCompliance, float triangleCompliance=0, float tetrahedronCompliance=0); explicit SoftBody(Mesh *mesh, float edgeCompliance, float triangleCompliance = 0, float tetrahedronCompliance = 0);
uint32_t firstIndex = 0; uint32_t firstIndex = 0;
int32_t vertexOffset = 0; int32_t vertexOffset = 0;
@ -28,9 +28,9 @@ public:
vector<Face> faces; vector<Face> faces;
ConstraintData constraintData; ConstraintData constraintData;
void applyVertexWorldOffset(const glm::vec3& offset); void applyVertexWorldOffset(const glm::vec3 &offset);
SoftBody& operator =(const SoftBody& other) = delete; SoftBody &operator=(const SoftBody &other) = delete;
private: private:
void splitConstraints(); void splitConstraints();
void reorderConstraintIndices(const vector<unordered_set<const Constraint *>> &partitions); void reorderConstraintIndices(const vector<unordered_set<const Constraint *>> &partitions);

@ -18,26 +18,28 @@ class Image;
class Buffer { class Buffer {
public: public:
Buffer(VkDeviceSize bufferSize, VkBufferUsageFlags bufferUsage, Buffer(VkDeviceSize bufferSize, VkBufferUsageFlags bufferUsage,
VmaMemoryUsage memoryUsage, VmaAllocationCreateFlags vmaAllocationFlags, VmaMemoryUsage memoryUsage, VmaAllocationCreateFlags vmaAllocationFlags,
const std::string& name=""); const std::string &name = "");
unique_ptr<Buffer> appended(void* data, VkDeviceSize appendSize, VkCommandBuffer commandBuffer) const; unique_ptr<Buffer> appended(void *data, VkDeviceSize appendSize, VkCommandBuffer commandBuffer) const;
unique_ptr<Buffer> replaced(void* data, VkDeviceSize replaceSize, VkCommandBuffer commandBuffer) const; unique_ptr<Buffer> replaced(void *data, VkDeviceSize replaceSize, VkCommandBuffer commandBuffer) const;
virtual ~Buffer(); virtual ~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& newName); void setName(const std::string &newName);
template <typename T>
T& access(){ template<typename T>
return *reinterpret_cast<T*>(allocationInfo.pMappedData); T &access() {
return *reinterpret_cast<T *>(allocationInfo.pMappedData);
} }
void copyTo(Buffer* buffer) const;
void copyTo(Image* image) const; void copyTo(Buffer *buffer) const;
void setData(void* data, VkDeviceSize offset, VkDeviceSize dataSize, VkCommandBuffer commandBuffer=VK_NULL_HANDLE); void copyTo(Image *image) const;
void setData(void *data, VkDeviceSize offset, VkDeviceSize dataSize, VkCommandBuffer commandBuffer = VK_NULL_HANDLE);
shared_ptr<Buffer> getStagingBuffer(); shared_ptr<Buffer> getStagingBuffer();
Buffer(const Buffer& other) = delete; Buffer(const Buffer &other) = delete;
Buffer& operator =(const Buffer& other) = delete; Buffer &operator=(const Buffer &other) = delete;
private: private:
optional<shared_ptr<Buffer>> stagingBufferOptional; optional<shared_ptr<Buffer>> stagingBufferOptional;
std::string name; std::string name;

@ -5,6 +5,7 @@
#include <map> #include <map>
class Buffer; class Buffer;
class Image; class Image;
enum class DescriptorSet { enum class DescriptorSet {
@ -18,8 +19,8 @@ public:
DescriptorPool(); DescriptorPool();
~DescriptorPool(); ~DescriptorPool();
void bindBuffer(const Buffer& buffer, VkDescriptorType type, DescriptorSet set, uint32_t binding); void bindBuffer(const Buffer &buffer, VkDescriptorType type, DescriptorSet set, uint32_t binding);
void bindImage(const Image& image, VkDescriptorType type, DescriptorSet set, uint32_t binding); void bindImage(const Image &image, VkDescriptorType type, DescriptorSet set, uint32_t binding);
std::map<DescriptorSet, VkDescriptorSet> sets; std::map<DescriptorSet, VkDescriptorSet> sets;
std::map<DescriptorSet, VkDescriptorSetLayout> layouts; std::map<DescriptorSet, VkDescriptorSetLayout> layouts;
VkDescriptorPool handle = VK_NULL_HANDLE; VkDescriptorPool handle = VK_NULL_HANDLE;

@ -8,8 +8,8 @@ class Instance;
class Image { class Image {
public: public:
Image(uint32_t width, uint32_t height, VkFormat format, VkImageTiling tiling, Image(uint32_t width, uint32_t height, VkFormat format, VkImageTiling tiling,
VkImageUsageFlags usage); VkImageUsageFlags usage);
Image(void* initialData, VkDeviceSize size, uint32_t width, uint32_t height, VkFormat format, VkImageTiling tiling, Image(void *initialData, VkDeviceSize size, uint32_t width, uint32_t height, VkFormat format, VkImageTiling tiling,
VkImageUsageFlags usage); VkImageUsageFlags usage);
~Image(); ~Image();

@ -14,6 +14,7 @@
using std::optional, std::vector; using std::optional, std::vector;
class CommandPool; class CommandPool;
class Swapchain; class Swapchain;
void printVmaStats(); void printVmaStats();
@ -29,39 +30,26 @@ public:
bool windowResized = false; bool windowResized = false;
CommandPool* renderingCommandPool = nullptr; CommandPool *renderingCommandPool = nullptr;
CommandPool* computeCommandPool = nullptr; CommandPool *computeCommandPool = nullptr;
struct QueueFamilyIndices { struct QueueFamilyIndices {
vector<uint32_t> graphicsAndPresent; vector<uint32_t> graphicsAndPresent;
vector<uint32_t> computeAndTransfer; vector<uint32_t> computeAndTransfer;
std::set<uint32_t> uniqueQueueFamilies(){ std::set<uint32_t> uniqueQueueFamilies();
std::set<uint32_t> unique; uint32_t tryComputeAndTransferDedicated();
unique.insert(graphicsAndPresent.begin(), graphicsAndPresent.end()); bool isEnough();
unique.insert(computeAndTransfer.begin(), computeAndTransfer.end());
return unique;
}
uint32_t tryComputeAndTransferDedicated(){
for (uint32_t family : computeAndTransfer){
if (std::find(graphicsAndPresent.begin(), graphicsAndPresent.end(), family) == graphicsAndPresent.end()){
return family;
}
}
return computeAndTransfer[0];
}
bool isEnough(){
return !graphicsAndPresent.empty() && !computeAndTransfer.empty();
}
}; };
QueueFamilyIndices indices {}; QueueFamilyIndices indices {};
static Instance* instance; static Instance *instance;
static VkDevice GetDevice(); static VkDevice GetDevice();
static VkPhysicalDevice GetPhysicalDevice(); static VkPhysicalDevice GetPhysicalDevice();
static VmaAllocator GetAllocator(); static VmaAllocator GetAllocator();
static VkSurfaceKHR GetSurface(); static VkSurfaceKHR GetSurface();
void initImGui(const Swapchain& swapchain); void initImGui(const Swapchain &swapchain);
private: private:
VkPhysicalDevice physicalDevice = VK_NULL_HANDLE; VkPhysicalDevice physicalDevice = VK_NULL_HANDLE;
VkDevice device = VK_NULL_HANDLE; VkDevice device = VK_NULL_HANDLE;

@ -20,15 +20,15 @@ protected:
class GraphicsPipeline : public Pipeline { class GraphicsPipeline : public Pipeline {
public: public:
explicit GraphicsPipeline(const std::string& vertexShaderPath, const std::string& fragmentShaderPath, explicit GraphicsPipeline(const std::string &vertexShaderPath, const std::string &fragmentShaderPath,
VkRenderPass renderPass, VkRenderPass renderPass,
const std::vector<VkDescriptorSetLayout> &descriptorSetLayouts={}, const std::vector<VkDescriptorSetLayout> &descriptorSetLayouts = {},
const std::vector<VkPushConstantRange> &pushConstantRanges={}); const std::vector<VkPushConstantRange> &pushConstantRanges = {});
}; };
class ComputePipeline : public Pipeline { class ComputePipeline : public Pipeline {
public: public:
explicit ComputePipeline(const std::string& shaderFile, explicit ComputePipeline(const std::string &shaderFile,
const std::vector<VkDescriptorSetLayout> &descriptorSetLayouts={}, const std::vector<VkDescriptorSetLayout> &descriptorSetLayouts = {},
const std::vector<VkPushConstantRange> &pushConstantRanges={}); const std::vector<VkPushConstantRange> &pushConstantRanges = {});
}; };

@ -4,6 +4,7 @@
#include <vector> #include <vector>
class Image; class Image;
class Instance; class Instance;
struct SwapchainSupportDetails { struct SwapchainSupportDetails {
@ -34,15 +35,15 @@ private:
void createRenderpass(); void createRenderpass();
Image* depthImage = nullptr; Image *depthImage = nullptr;
void createDepthResources(); void createDepthResources();
void createFramebuffers(); void createFramebuffers();
static VkSurfaceFormatKHR chooseSwapSurfaceFormat(const std::vector<VkSurfaceFormatKHR>& availableFormats); static VkSurfaceFormatKHR chooseSwapSurfaceFormat(const std::vector<VkSurfaceFormatKHR> &availableFormats);
static VkPresentModeKHR chooseSwapPresentMode(const std::vector<VkPresentModeKHR>& availablePresentModes); static VkPresentModeKHR chooseSwapPresentMode(const std::vector<VkPresentModeKHR> &availablePresentModes);
VkExtent2D chooseSwapExtent(const VkSurfaceCapabilitiesKHR& capabilities); VkExtent2D chooseSwapExtent(const VkSurfaceCapabilitiesKHR &capabilities);
void createSwapchain(); void createSwapchain();
}; };

@ -273,17 +273,23 @@ void Application::addSoftBody(const std::string &modelFile, size_t count) {
newVertexBuffers[0] = make_unique<Buffer>( newVertexBuffers[0] = make_unique<Buffer>(
newVertices.size() * sizeof(Vertex), newVertices.size() * sizeof(Vertex),
bufferUsageFlags | VK_BUFFER_USAGE_VERTEX_BUFFER_BIT, bufferUsageFlags | VK_BUFFER_USAGE_VERTEX_BUFFER_BIT,
memoryUsage, 0, "Vertices 0" memoryUsage,
0,
"Vertices 0"
); );
newVertexBuffers[1] = make_unique<Buffer>( newVertexBuffers[1] = make_unique<Buffer>(
newVertices.size() * sizeof(Vertex), newVertices.size() * sizeof(Vertex),
bufferUsageFlags | VK_BUFFER_USAGE_VERTEX_BUFFER_BIT, bufferUsageFlags | VK_BUFFER_USAGE_VERTEX_BUFFER_BIT,
memoryUsage, 0, "Vertices 1" memoryUsage,
0,
"Vertices 1"
); );
newFaceBuffer = make_unique<Buffer>( newFaceBuffer = make_unique<Buffer>(
newFaces.size() * sizeof(Face), newFaces.size() * sizeof(Face),
bufferUsageFlags | VK_BUFFER_USAGE_INDEX_BUFFER_BIT, bufferUsageFlags | VK_BUFFER_USAGE_INDEX_BUFFER_BIT,
memoryUsage, 0, "Faces" memoryUsage,
0,
"Faces"
); );
newEdgeBuffer = make_unique<Buffer>( newEdgeBuffer = make_unique<Buffer>(
constraintData.edges.size() * sizeof(Edge), constraintData.edges.size() * sizeof(Edge),
@ -293,7 +299,10 @@ void Application::addSoftBody(const std::string &modelFile, size_t count) {
"Edges" "Edges"
); );
newTetrahedronBuffer = make_unique<Buffer>( newTetrahedronBuffer = make_unique<Buffer>(
constraintData.tetrahedra.size() * sizeof(Tetrahedron), bufferUsageFlags, memoryUsage, 0, constraintData.tetrahedra.size() * sizeof(Tetrahedron),
bufferUsageFlags,
memoryUsage,
0,
"Tetrahedra" "Tetrahedra"
); );
@ -304,17 +313,29 @@ void Application::addSoftBody(const std::string &modelFile, size_t count) {
newTetrahedronBuffer->setData(constraintData.tetrahedra.data(), 0, newTetrahedronBuffer->size, commandBuffer); newTetrahedronBuffer->setData(constraintData.tetrahedra.data(), 0, newTetrahedronBuffer->size, commandBuffer);
} else { } else {
newVertexBuffers[0] = vertexBuffers[0]->appended( newVertexBuffers[0] = vertexBuffers[0]->appended(
newVertices.data(), newVertices.size() * sizeof(Vertex), commandBuffer newVertices.data(),
newVertices.size() * sizeof(Vertex),
commandBuffer
); );
newVertexBuffers[1] = vertexBuffers[1]->appended( newVertexBuffers[1] = vertexBuffers[1]->appended(
newVertices.data(), newVertices.size() * sizeof(Vertex), commandBuffer newVertices.data(),
newVertices.size() * sizeof(Vertex),
commandBuffer
);
newFaceBuffer = faceBuffer->appended(
newFaces.data(),
newFaces.size() * sizeof(Face),
commandBuffer
); );
newFaceBuffer = faceBuffer->appended(newFaces.data(), newFaces.size() * sizeof(Face), commandBuffer);
newEdgeBuffer = edgeBuffer->replaced( newEdgeBuffer = edgeBuffer->replaced(
constraintData.edges.data(), constraintData.edges.size() * sizeof(Edge), commandBuffer constraintData.edges.data(),
constraintData.edges.size() * sizeof(Edge),
commandBuffer
); );
newTetrahedronBuffer = tetrahedronBuffer->replaced( newTetrahedronBuffer = tetrahedronBuffer->replaced(
constraintData.tetrahedra.data(), constraintData.tetrahedra.size() * sizeof(Tetrahedron), commandBuffer constraintData.tetrahedra.data(),
constraintData.tetrahedra.size() * sizeof(Tetrahedron),
commandBuffer
); );
} }
@ -327,9 +348,7 @@ void Application::addSoftBody(const std::string &modelFile, size_t count) {
edgeBuffer = std::move(newEdgeBuffer); edgeBuffer = std::move(newEdgeBuffer);
tetrahedronBuffer = std::move(newTetrahedronBuffer); tetrahedronBuffer = std::move(newTetrahedronBuffer);
descriptorPool->bindBuffer( descriptorPool->bindBuffer(*vertexBuffers[1 - currentDrawVertexBuffer], VK_DESCRIPTOR_TYPE_STORAGE_BUFFER, DescriptorSet::MESH, 0);
*vertexBuffers[1 - currentDrawVertexBuffer], VK_DESCRIPTOR_TYPE_STORAGE_BUFFER, DescriptorSet::MESH, 0
);
descriptorPool->bindBuffer(*faceBuffer, VK_DESCRIPTOR_TYPE_STORAGE_BUFFER, DescriptorSet::MESH, 1); descriptorPool->bindBuffer(*faceBuffer, VK_DESCRIPTOR_TYPE_STORAGE_BUFFER, DescriptorSet::MESH, 1);
descriptorPool->bindBuffer(*edgeBuffer, VK_DESCRIPTOR_TYPE_STORAGE_BUFFER, DescriptorSet::MESH, 2); descriptorPool->bindBuffer(*edgeBuffer, VK_DESCRIPTOR_TYPE_STORAGE_BUFFER, DescriptorSet::MESH, 2);
descriptorPool->bindBuffer(*tetrahedronBuffer, VK_DESCRIPTOR_TYPE_STORAGE_BUFFER, DescriptorSet::MESH, 4); descriptorPool->bindBuffer(*tetrahedronBuffer, VK_DESCRIPTOR_TYPE_STORAGE_BUFFER, DescriptorSet::MESH, 4);

@ -355,3 +355,22 @@ void Instance::initImGui(const Swapchain &swapchain) {
ImGui_ImplVulkan_Init(&initInfo); ImGui_ImplVulkan_Init(&initInfo);
} }
std::set<uint32_t> Instance::QueueFamilyIndices::uniqueQueueFamilies() {
std::set<uint32_t> unique;
unique.insert(graphicsAndPresent.begin(), graphicsAndPresent.end());
unique.insert(computeAndTransfer.begin(), computeAndTransfer.end());
return unique;
}
uint32_t Instance::QueueFamilyIndices::tryComputeAndTransferDedicated() {
for (uint32_t family: computeAndTransfer)
if (std::find(graphicsAndPresent.begin(), graphicsAndPresent.end(), family) == graphicsAndPresent.end())
return family;
return computeAndTransfer[0];
}
bool Instance::QueueFamilyIndices::isEnough() {
return !graphicsAndPresent.empty() && !computeAndTransfer.empty();
}

Loading…
Cancel
Save