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. 26
      include/vulkan/buffer.hpp
  7. 5
      include/vulkan/descriptor_pool.hpp
  8. 2
      include/vulkan/image.hpp
  9. 30
      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;
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> grabber;
unique_ptr<Buffer> grabBuffer;
void addSoftBody(const std::string& modelFile, size_t count=1);
void removeSoftBody(const unique_ptr<SoftBody>& softBody);
void addSoftBody(const std::string &modelFile, size_t count = 1);
void removeSoftBody(const unique_ptr<SoftBody> &softBody);
void updateConstraintBuffers(VkCommandBuffer commandBuffer);
size_t currentDrawVertexBuffer = 0;

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

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

@ -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<int, bool> KeyIsDown;
static std::map<int, bool> MouseButtonIsDown;
glm::vec2 currentCursorPosition {};
private:
GLFWwindow* window;
std::set<Listener*> listeners;
GLFWwindow *window;
std::set<Listener *> 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) {};
};

@ -14,13 +14,13 @@ using std::vector;
struct Vertex;
typedef unordered_map<const Constraint*, vector<const Constraint *>> Graph;
typedef unordered_map<const Constraint *, vector<const Constraint *>> 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<Face> 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<unordered_set<const Constraint *>> &partitions);

@ -19,25 +19,27 @@ class Buffer {
public:
Buffer(VkDeviceSize bufferSize, VkBufferUsageFlags bufferUsage,
VmaMemoryUsage memoryUsage, VmaAllocationCreateFlags vmaAllocationFlags,
const std::string& name="");
unique_ptr<Buffer> appended(void* data, VkDeviceSize appendSize, VkCommandBuffer commandBuffer) const;
unique_ptr<Buffer> replaced(void* data, VkDeviceSize replaceSize, VkCommandBuffer commandBuffer) const;
const std::string &name = "");
unique_ptr<Buffer> appended(void *data, VkDeviceSize appendSize, VkCommandBuffer commandBuffer) const;
unique_ptr<Buffer> 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 <typename T>
T& access(){
return *reinterpret_cast<T*>(allocationInfo.pMappedData);
void setName(const std::string &newName);
template<typename T>
T &access() {
return *reinterpret_cast<T *>(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<Buffer> 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<shared_ptr<Buffer>> stagingBufferOptional;
std::string name;

@ -5,6 +5,7 @@
#include <map>
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<DescriptorSet, VkDescriptorSet> sets;
std::map<DescriptorSet, VkDescriptorSetLayout> layouts;
VkDescriptorPool handle = VK_NULL_HANDLE;

@ -9,7 +9,7 @@ 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,
Image(void *initialData, VkDeviceSize size, uint32_t width, uint32_t height, VkFormat format, VkImageTiling tiling,
VkImageUsageFlags usage);
~Image();

@ -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<uint32_t> graphicsAndPresent;
vector<uint32_t> computeAndTransfer;
std::set<uint32_t> uniqueQueueFamilies(){
std::set<uint32_t> 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<uint32_t> 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;

@ -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<VkDescriptorSetLayout> &descriptorSetLayouts={},
const std::vector<VkPushConstantRange> &pushConstantRanges={});
const std::vector<VkDescriptorSetLayout> &descriptorSetLayouts = {},
const std::vector<VkPushConstantRange> &pushConstantRanges = {});
};
class ComputePipeline : public Pipeline {
public:
explicit ComputePipeline(const std::string& shaderFile,
const std::vector<VkDescriptorSetLayout> &descriptorSetLayouts={},
const std::vector<VkPushConstantRange> &pushConstantRanges={});
explicit ComputePipeline(const std::string &shaderFile,
const std::vector<VkDescriptorSetLayout> &descriptorSetLayouts = {},
const std::vector<VkPushConstantRange> &pushConstantRanges = {});
};

@ -4,6 +4,7 @@
#include <vector>
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<VkSurfaceFormatKHR>& availableFormats);
static VkPresentModeKHR chooseSwapPresentMode(const std::vector<VkPresentModeKHR>& availablePresentModes);
static VkSurfaceFormatKHR chooseSwapSurfaceFormat(const std::vector<VkSurfaceFormatKHR> &availableFormats);
static VkPresentModeKHR chooseSwapPresentMode(const std::vector<VkPresentModeKHR> &availablePresentModes);
VkExtent2D chooseSwapExtent(const VkSurfaceCapabilitiesKHR& capabilities);
VkExtent2D chooseSwapExtent(const VkSurfaceCapabilitiesKHR &capabilities);
void createSwapchain();
};

@ -273,17 +273,23 @@ void Application::addSoftBody(const std::string &modelFile, size_t count) {
newVertexBuffers[0] = make_unique<Buffer>(
newVertices.size() * sizeof(Vertex),
bufferUsageFlags | VK_BUFFER_USAGE_VERTEX_BUFFER_BIT,
memoryUsage, 0, "Vertices 0"
memoryUsage,
0,
"Vertices 0"
);
newVertexBuffers[1] = make_unique<Buffer>(
newVertices.size() * sizeof(Vertex),
bufferUsageFlags | VK_BUFFER_USAGE_VERTEX_BUFFER_BIT,
memoryUsage, 0, "Vertices 1"
memoryUsage,
0,
"Vertices 1"
);
newFaceBuffer = make_unique<Buffer>(
newFaces.size() * sizeof(Face),
bufferUsageFlags | VK_BUFFER_USAGE_INDEX_BUFFER_BIT,
memoryUsage, 0, "Faces"
memoryUsage,
0,
"Faces"
);
newEdgeBuffer = make_unique<Buffer>(
constraintData.edges.size() * sizeof(Edge),
@ -293,7 +299,10 @@ void Application::addSoftBody(const std::string &modelFile, size_t count) {
"Edges"
);
newTetrahedronBuffer = make_unique<Buffer>(
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);

@ -355,3 +355,22 @@ void Instance::initImGui(const Swapchain &swapchain) {
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