diff --git a/include/application.hpp b/include/application.hpp index fd5daef..b551bfa 100644 --- a/include/application.hpp +++ b/include/application.hpp @@ -26,19 +26,33 @@ using std::vector; using std::optional; class SoftBody; + class Instance; + class Swapchain; + class GraphicsPipeline; + class Buffer; + class Buffer; + class CommandPool; + class Image; + class ComputePipeline; + class Fence; + class Semaphore; + class Camera; + class DescriptorPool; + class Grabber; + struct SizesUniformData; class Application { @@ -79,8 +93,8 @@ private: unique_ptr grabber; unique_ptr grabBuffer; - void addSoftBody(const std::string& modelFile, size_t count=1); - void removeSoftBody(const unique_ptr& softBody); + void addSoftBody(const std::string &modelFile, size_t count = 1); + void removeSoftBody(const unique_ptr &softBody); void updateConstraintBuffers(VkCommandBuffer commandBuffer); size_t currentDrawVertexBuffer = 0; diff --git a/include/camera.hpp b/include/camera.hpp index c96c4f7..f0238c3 100644 --- a/include/camera.hpp +++ b/include/camera.hpp @@ -18,14 +18,14 @@ private: float near = 0.1; float far = 100; - VkExtent2D& extent; + VkExtent2D &extent; public: - explicit Camera(VkExtent2D& extent); + explicit Camera(VkExtent2D &extent); void update(float dt); glm::mat4 view() const; glm::mat4 projection() const; glm::vec2 viewport() const; - glm::vec3 localToWorld(const glm::vec3& direction) const; + glm::vec3 localToWorld(const glm::vec3 &direction) const; protected: void mouseMoved(const glm::vec2 &delta) override; }; \ No newline at end of file diff --git a/include/constraints.hpp b/include/constraints.hpp index 5d57df7..fe111a5 100644 --- a/include/constraints.hpp +++ b/include/constraints.hpp @@ -51,7 +51,7 @@ struct ConstraintData { void recordNewPartition(); void writePartitionInformation(); - void insert(const ConstraintData& other); + void insert(const ConstraintData &other); private: uint32_t prePartitionEdgeCount; uint32_t prePartitionTetrahedronCount; @@ -62,20 +62,24 @@ private: struct Constraint { virtual ~Constraint() {} - virtual void writeData(ConstraintData& dataLists) const {}; + + virtual void writeData(ConstraintData &dataLists) const {}; }; struct DistanceConstraint : Edge, Constraint { explicit DistanceConstraint(const Edge &edge) : Edge(edge) {} + void writeData(ConstraintData &dataLists) const override; }; struct AreaConstraint : Triangle, Constraint { - explicit AreaConstraint(const Triangle& triangle) : Triangle(triangle) {} + explicit AreaConstraint(const Triangle &triangle) : Triangle(triangle) {} + void writeData(ConstraintData &dataLists) const override; }; struct VolumeConstraint : Tetrahedron, Constraint { - explicit VolumeConstraint(const Tetrahedron& tetrahedron) : Tetrahedron(tetrahedron) {} + explicit VolumeConstraint(const Tetrahedron &tetrahedron) : Tetrahedron(tetrahedron) {} + void writeData(ConstraintData &dataLists) const override; }; diff --git a/include/input.hpp b/include/input.hpp index bd865d4..f56acef 100644 --- a/include/input.hpp +++ b/include/input.hpp @@ -6,27 +6,28 @@ #include "glm/vec2.hpp" class Listener; + class MouseListener; class Input { friend Listener; friend MouseListener; public: - explicit Input(GLFWwindow* window); - static Input* instance; + explicit Input(GLFWwindow *window); + static Input *instance; static std::map KeyIsDown; static std::map MouseButtonIsDown; glm::vec2 currentCursorPosition {}; private: - GLFWwindow* window; - std::set listeners; + GLFWwindow *window; + std::set listeners; void keyPressed(int key); void keyReleased(int key); void mouseButtonPressed(int button); void mouseButtonReleased(int button); - void mouseMoved(const glm::vec2& newCursorPosition); + void mouseMoved(const glm::vec2 &newCursorPosition); }; class Listener { @@ -41,7 +42,9 @@ class KeyListener : Listener { class MouseListener : public Listener { public: - virtual void mouseMoved(const glm::vec2& delta) {}; + virtual void mouseMoved(const glm::vec2 &delta) {}; + virtual void mouseButtonPressed(int button) {}; + virtual void mouseButtonReleased(int button) {}; }; \ No newline at end of file diff --git a/include/soft_body.hpp b/include/soft_body.hpp index 77e28ca..8dc8e94 100644 --- a/include/soft_body.hpp +++ b/include/soft_body.hpp @@ -14,13 +14,13 @@ using std::vector; struct Vertex; -typedef unordered_map> Graph; +typedef unordered_map> Graph; class Mesh; class SoftBody { 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; int32_t vertexOffset = 0; @@ -28,9 +28,9 @@ public: vector faces; 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: void splitConstraints(); void reorderConstraintIndices(const vector> &partitions); diff --git a/include/vulkan/buffer.hpp b/include/vulkan/buffer.hpp index 683cc15..7f36f1f 100644 --- a/include/vulkan/buffer.hpp +++ b/include/vulkan/buffer.hpp @@ -18,26 +18,28 @@ class Image; class Buffer { public: Buffer(VkDeviceSize bufferSize, VkBufferUsageFlags bufferUsage, - VmaMemoryUsage memoryUsage, VmaAllocationCreateFlags vmaAllocationFlags, - const std::string& name=""); - unique_ptr appended(void* data, VkDeviceSize appendSize, VkCommandBuffer commandBuffer) const; - unique_ptr replaced(void* data, VkDeviceSize replaceSize, VkCommandBuffer commandBuffer) const; + VmaMemoryUsage memoryUsage, VmaAllocationCreateFlags vmaAllocationFlags, + const std::string &name = ""); + 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; VmaAllocationInfo allocationInfo {}; VkDeviceSize size; - void setName(const std::string& newName); - template - T& access(){ - return *reinterpret_cast(allocationInfo.pMappedData); + void setName(const std::string &newName); + + template + T &access() { + return *reinterpret_cast(allocationInfo.pMappedData); } - void copyTo(Buffer* buffer) const; - void copyTo(Image* image) const; - void setData(void* data, VkDeviceSize offset, VkDeviceSize dataSize, VkCommandBuffer commandBuffer=VK_NULL_HANDLE); + + void copyTo(Buffer *buffer) const; + void copyTo(Image *image) const; + 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; + Buffer(const Buffer &other) = delete; + Buffer &operator=(const Buffer &other) = delete; private: optional> stagingBufferOptional; std::string name; diff --git a/include/vulkan/descriptor_pool.hpp b/include/vulkan/descriptor_pool.hpp index fe159ff..b68dce1 100644 --- a/include/vulkan/descriptor_pool.hpp +++ b/include/vulkan/descriptor_pool.hpp @@ -5,6 +5,7 @@ #include class Buffer; + class Image; enum class DescriptorSet { @@ -18,8 +19,8 @@ public: DescriptorPool(); ~DescriptorPool(); - 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 bindBuffer(const Buffer &buffer, VkDescriptorType type, DescriptorSet set, uint32_t binding); + void bindImage(const Image &image, VkDescriptorType type, DescriptorSet set, uint32_t binding); std::map sets; std::map layouts; VkDescriptorPool handle = VK_NULL_HANDLE; diff --git a/include/vulkan/image.hpp b/include/vulkan/image.hpp index 33e82da..1c79e95 100644 --- a/include/vulkan/image.hpp +++ b/include/vulkan/image.hpp @@ -8,8 +8,8 @@ class Instance; class Image { public: Image(uint32_t width, uint32_t height, VkFormat format, VkImageTiling tiling, - VkImageUsageFlags usage); - Image(void* initialData, VkDeviceSize size, uint32_t width, uint32_t height, VkFormat format, VkImageTiling tiling, + VkImageUsageFlags usage); + Image(void *initialData, VkDeviceSize size, uint32_t width, uint32_t height, VkFormat format, VkImageTiling tiling, VkImageUsageFlags usage); ~Image(); diff --git a/include/vulkan/instance.hpp b/include/vulkan/instance.hpp index c67c09b..c5385bb 100644 --- a/include/vulkan/instance.hpp +++ b/include/vulkan/instance.hpp @@ -14,6 +14,7 @@ using std::optional, std::vector; class CommandPool; + class Swapchain; void printVmaStats(); @@ -29,39 +30,26 @@ public: bool windowResized = false; - CommandPool* renderingCommandPool = nullptr; - CommandPool* computeCommandPool = nullptr; + CommandPool *renderingCommandPool = nullptr; + CommandPool *computeCommandPool = nullptr; + - struct QueueFamilyIndices { vector graphicsAndPresent; vector computeAndTransfer; - std::set uniqueQueueFamilies(){ - std::set unique; - unique.insert(graphicsAndPresent.begin(), graphicsAndPresent.end()); - 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(); - } + std::set uniqueQueueFamilies(); + uint32_t tryComputeAndTransferDedicated(); + bool isEnough(); }; + QueueFamilyIndices indices {}; - static Instance* instance; + static Instance *instance; static VkDevice GetDevice(); static VkPhysicalDevice GetPhysicalDevice(); static VmaAllocator GetAllocator(); static VkSurfaceKHR GetSurface(); - void initImGui(const Swapchain& swapchain); + void initImGui(const Swapchain &swapchain); private: VkPhysicalDevice physicalDevice = VK_NULL_HANDLE; VkDevice device = VK_NULL_HANDLE; diff --git a/include/vulkan/pipeline.hpp b/include/vulkan/pipeline.hpp index 908bcee..b3959e1 100644 --- a/include/vulkan/pipeline.hpp +++ b/include/vulkan/pipeline.hpp @@ -20,15 +20,15 @@ protected: class GraphicsPipeline : public Pipeline { public: - explicit GraphicsPipeline(const std::string& vertexShaderPath, const std::string& fragmentShaderPath, + explicit GraphicsPipeline(const std::string &vertexShaderPath, const std::string &fragmentShaderPath, VkRenderPass renderPass, - const std::vector &descriptorSetLayouts={}, - const std::vector &pushConstantRanges={}); + const std::vector &descriptorSetLayouts = {}, + const std::vector &pushConstantRanges = {}); }; class ComputePipeline : public Pipeline { public: - explicit ComputePipeline(const std::string& shaderFile, - const std::vector &descriptorSetLayouts={}, - const std::vector &pushConstantRanges={}); + explicit ComputePipeline(const std::string &shaderFile, + const std::vector &descriptorSetLayouts = {}, + const std::vector &pushConstantRanges = {}); }; \ No newline at end of file diff --git a/include/vulkan/swapchain.hpp b/include/vulkan/swapchain.hpp index ea7c70b..912052d 100644 --- a/include/vulkan/swapchain.hpp +++ b/include/vulkan/swapchain.hpp @@ -4,6 +4,7 @@ #include class Image; + class Instance; struct SwapchainSupportDetails { @@ -34,15 +35,15 @@ private: void createRenderpass(); - Image* depthImage = nullptr; + Image *depthImage = nullptr; void createDepthResources(); void createFramebuffers(); - static VkSurfaceFormatKHR chooseSwapSurfaceFormat(const std::vector& availableFormats); - static VkPresentModeKHR chooseSwapPresentMode(const std::vector& availablePresentModes); + static VkSurfaceFormatKHR chooseSwapSurfaceFormat(const std::vector &availableFormats); + static VkPresentModeKHR chooseSwapPresentMode(const std::vector &availablePresentModes); - VkExtent2D chooseSwapExtent(const VkSurfaceCapabilitiesKHR& capabilities); + VkExtent2D chooseSwapExtent(const VkSurfaceCapabilitiesKHR &capabilities); void createSwapchain(); }; \ No newline at end of file diff --git a/src/application.cpp b/src/application.cpp index e020378..6c2e71f 100644 --- a/src/application.cpp +++ b/src/application.cpp @@ -273,17 +273,23 @@ void Application::addSoftBody(const std::string &modelFile, size_t count) { newVertexBuffers[0] = make_unique( newVertices.size() * sizeof(Vertex), bufferUsageFlags | VK_BUFFER_USAGE_VERTEX_BUFFER_BIT, - memoryUsage, 0, "Vertices 0" + memoryUsage, + 0, + "Vertices 0" ); newVertexBuffers[1] = make_unique( newVertices.size() * sizeof(Vertex), bufferUsageFlags | VK_BUFFER_USAGE_VERTEX_BUFFER_BIT, - memoryUsage, 0, "Vertices 1" + memoryUsage, + 0, + "Vertices 1" ); newFaceBuffer = make_unique( newFaces.size() * sizeof(Face), bufferUsageFlags | VK_BUFFER_USAGE_INDEX_BUFFER_BIT, - memoryUsage, 0, "Faces" + memoryUsage, + 0, + "Faces" ); newEdgeBuffer = make_unique( constraintData.edges.size() * sizeof(Edge), @@ -293,7 +299,10 @@ void Application::addSoftBody(const std::string &modelFile, size_t count) { "Edges" ); newTetrahedronBuffer = make_unique( - constraintData.tetrahedra.size() * sizeof(Tetrahedron), bufferUsageFlags, memoryUsage, 0, + constraintData.tetrahedra.size() * sizeof(Tetrahedron), + bufferUsageFlags, + memoryUsage, + 0, "Tetrahedra" ); @@ -304,17 +313,29 @@ void Application::addSoftBody(const std::string &modelFile, size_t count) { newTetrahedronBuffer->setData(constraintData.tetrahedra.data(), 0, newTetrahedronBuffer->size, commandBuffer); } else { newVertexBuffers[0] = vertexBuffers[0]->appended( - newVertices.data(), newVertices.size() * sizeof(Vertex), commandBuffer + newVertices.data(), + newVertices.size() * sizeof(Vertex), + commandBuffer ); 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( - constraintData.edges.data(), constraintData.edges.size() * sizeof(Edge), commandBuffer + constraintData.edges.data(), + constraintData.edges.size() * sizeof(Edge), + commandBuffer ); 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); tetrahedronBuffer = std::move(newTetrahedronBuffer); - descriptorPool->bindBuffer( - *vertexBuffers[1 - currentDrawVertexBuffer], VK_DESCRIPTOR_TYPE_STORAGE_BUFFER, DescriptorSet::MESH, 0 - ); + 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); diff --git a/src/vulkan/instance.cpp b/src/vulkan/instance.cpp index 0855013..8529318 100644 --- a/src/vulkan/instance.cpp +++ b/src/vulkan/instance.cpp @@ -355,3 +355,22 @@ void Instance::initImGui(const Swapchain &swapchain) { ImGui_ImplVulkan_Init(&initInfo); } + + +std::set Instance::QueueFamilyIndices::uniqueQueueFamilies() { + std::set 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(); +}