| [7d2b0b9] | 1 | #ifndef _GRAPHICS_PIPELINE_VULKAN_H
|
|---|
| 2 | #define _GRAPHICS_PIPELINE_VULKAN_H
|
|---|
| 3 |
|
|---|
| 4 | #include "graphics-pipeline.hpp"
|
|---|
| 5 |
|
|---|
| [e3bef3a] | 6 | #include <iostream>
|
|---|
| [7d2b0b9] | 7 | #include <vector>
|
|---|
| 8 |
|
|---|
| [771b33a] | 9 | #include <vulkan/vulkan.h>
|
|---|
| 10 |
|
|---|
| [e3bef3a] | 11 | #include "vulkan-utils.hpp"
|
|---|
| 12 |
|
|---|
| 13 | using namespace std;
|
|---|
| 14 |
|
|---|
| 15 | // TODO: Remove any instances of cout and instead throw exceptions
|
|---|
| 16 |
|
|---|
| [b794178] | 17 | // TODO: Maybe change the name of this struct so I can call the list something other than descriptorInfoList
|
|---|
| 18 | struct DescriptorInfo {
|
|---|
| 19 | VkDescriptorType type;
|
|---|
| 20 | VkShaderStageFlags stageFlags;
|
|---|
| 21 |
|
|---|
| 22 | // Only one of the below properties should be set
|
|---|
| 23 | vector<VkDescriptorBufferInfo>* bufferDataList;
|
|---|
| 24 | VkDescriptorImageInfo* imageData;
|
|---|
| 25 | };
|
|---|
| 26 |
|
|---|
| [7d2b0b9] | 27 | class GraphicsPipeline_Vulkan : public GraphicsPipeline {
|
|---|
| 28 | public:
|
|---|
| [87c8f1a] | 29 | GraphicsPipeline_Vulkan(VkPhysicalDevice physicalDevice, VkDevice device, VkRenderPass renderPass,
|
|---|
| 30 | Viewport viewport, int vertexSize);
|
|---|
| [7d2b0b9] | 31 | ~GraphicsPipeline_Vulkan();
|
|---|
| 32 |
|
|---|
| [0ae182f] | 33 | void updateRenderPass(VkRenderPass renderPass);
|
|---|
| 34 |
|
|---|
| [e3bef3a] | 35 | template<class VertexType>
|
|---|
| 36 | void bindData(const vector<VertexType>& vertices, const vector<uint16_t>& indices,
|
|---|
| [87c8f1a] | 37 | VkCommandPool commandPool, VkQueue graphicsQueue);
|
|---|
| 38 |
|
|---|
| 39 | void createVertexBuffer(const void* bufferData, int vertexSize, VkCommandPool commandPool,
|
|---|
| 40 | VkQueue graphicsQueue);
|
|---|
| 41 | void createIndexBuffer(const void* bufferData, int indexSize, VkCommandPool commandPool,
|
|---|
| 42 | VkQueue graphicsQueue);
|
|---|
| 43 |
|
|---|
| [b794178] | 44 | // Maybe I should rename these to addVertexAttribute (addVaryingAttribute) and addUniformAttribute
|
|---|
| 45 |
|
|---|
| [771b33a] | 46 | void addAttribute(VkFormat format, size_t offset);
|
|---|
| [b794178] | 47 |
|
|---|
| 48 | void addDescriptorInfo(VkDescriptorType type, VkShaderStageFlags stageFlags, vector<VkDescriptorBufferInfo>* bufferData);
|
|---|
| 49 | void addDescriptorInfo(VkDescriptorType type, VkShaderStageFlags stageFlags, VkDescriptorImageInfo* imageData);
|
|---|
| 50 |
|
|---|
| [7d2b0b9] | 51 | void createPipeline(string vertShaderFile, string fragShaderFile);
|
|---|
| [b794178] | 52 | void createDescriptorSetLayout();
|
|---|
| 53 | void createDescriptorPool(vector<VkImage>& swapChainImages);
|
|---|
| 54 | void createDescriptorSets(vector<VkImage>& swapChainImages);
|
|---|
| 55 |
|
|---|
| [603b5bc] | 56 | void createRenderCommands(VkCommandBuffer& commandBuffer, uint32_t currentImage);
|
|---|
| 57 |
|
|---|
| [e3bef3a] | 58 | template<class VertexType>
|
|---|
| 59 | bool addObject(const vector<VertexType>& vertices, vector<uint16_t>& indices, VkCommandPool commandPool,
|
|---|
| 60 | VkQueue graphicsQueue);
|
|---|
| 61 |
|
|---|
| [b794178] | 62 | void cleanup();
|
|---|
| 63 | void cleanupBuffers();
|
|---|
| [7d2b0b9] | 64 |
|
|---|
| 65 | private:
|
|---|
| [b794178] | 66 | VkShaderModule createShaderModule(const vector<char>& code);
|
|---|
| 67 | vector<char> readFile(const string& filename);
|
|---|
| 68 |
|
|---|
| [87c8f1a] | 69 | VkPhysicalDevice physicalDevice;
|
|---|
| [7d2b0b9] | 70 | VkDevice device;
|
|---|
| [b794178] | 71 | VkRenderPass renderPass;
|
|---|
| 72 |
|
|---|
| 73 | VkPipeline pipeline;
|
|---|
| 74 | VkPipelineLayout pipelineLayout;
|
|---|
| 75 |
|
|---|
| [771b33a] | 76 | VkVertexInputBindingDescription bindingDescription;
|
|---|
| [b794178] | 77 |
|
|---|
| [771b33a] | 78 | vector<VkVertexInputAttributeDescription> attributeDescriptions;
|
|---|
| [b794178] | 79 | vector<DescriptorInfo> descriptorInfoList;
|
|---|
| [7d2b0b9] | 80 |
|
|---|
| [b794178] | 81 | VkDescriptorSetLayout descriptorSetLayout;
|
|---|
| 82 | VkDescriptorPool descriptorPool;
|
|---|
| 83 | vector<VkDescriptorSet> descriptorSets;
|
|---|
| [87c8f1a] | 84 |
|
|---|
| 85 | size_t numVertices;
|
|---|
| 86 | size_t vertexCapacity;
|
|---|
| 87 | VkBuffer vertexBuffer;
|
|---|
| 88 | VkDeviceMemory vertexBufferMemory;
|
|---|
| 89 |
|
|---|
| 90 | size_t numIndices;
|
|---|
| 91 | size_t indexCapacity;
|
|---|
| 92 | VkBuffer indexBuffer;
|
|---|
| 93 | VkDeviceMemory indexBufferMemory;
|
|---|
| [7d2b0b9] | 94 | };
|
|---|
| 95 |
|
|---|
| [e3bef3a] | 96 | // TODO: Probably better to template the whole class
|
|---|
| 97 | // TODO: Change the index type to uint32_t and check the Vulkan Tutorial loading model section as a reference
|
|---|
| 98 |
|
|---|
| 99 | // TODO: combine this function and the constructor since I call this right after the constructor anyway
|
|---|
| 100 | template<class VertexType>
|
|---|
| 101 | void GraphicsPipeline_Vulkan::bindData(const vector<VertexType>& vertices, const vector<uint16_t>& indices,
|
|---|
| [87c8f1a] | 102 | VkCommandPool commandPool, VkQueue graphicsQueue) {
|
|---|
| 103 | numVertices = vertices.size();
|
|---|
| 104 | vertexCapacity = numVertices * 2;
|
|---|
| 105 | createVertexBuffer(vertices.data(), sizeof(VertexType), commandPool, graphicsQueue);
|
|---|
| 106 |
|
|---|
| 107 | numIndices = indices.size();
|
|---|
| 108 | indexCapacity = numIndices * 2;
|
|---|
| [e3bef3a] | 109 | createIndexBuffer(indices.data(), sizeof(uint16_t), commandPool, graphicsQueue);
|
|---|
| 110 | }
|
|---|
| 111 |
|
|---|
| 112 | template<class VertexType>
|
|---|
| 113 | bool GraphicsPipeline_Vulkan::addObject(const vector<VertexType>& vertices, vector<uint16_t>& indices,
|
|---|
| 114 | VkCommandPool commandPool, VkQueue graphicsQueue) {
|
|---|
| 115 | cout << "Adding object to pipeline..." << endl;
|
|---|
| 116 |
|
|---|
| 117 | if (numVertices + vertices.size() > vertexCapacity) {
|
|---|
| 118 | cout << "ERROR: Need to resize vertex buffers" << endl;
|
|---|
| 119 | } else if (numIndices + indices.size() > indexCapacity) {
|
|---|
| 120 | cout << "ERROR: Need to resize index buffers" << endl;
|
|---|
| 121 | } else {
|
|---|
| 122 | cout << "Added object to scene" << endl;
|
|---|
| 123 |
|
|---|
| 124 | for (uint16_t& idx : indices) {
|
|---|
| 125 | idx += numVertices;
|
|---|
| 126 | }
|
|---|
| 127 |
|
|---|
| 128 | VulkanUtils::copyDataToBuffer(device, physicalDevice, commandPool, vertices, vertexBuffer, numVertices,
|
|---|
| 129 | graphicsQueue);
|
|---|
| 130 | numVertices += vertices.size();
|
|---|
| 131 |
|
|---|
| 132 | VulkanUtils::copyDataToBuffer(device, physicalDevice, commandPool, indices, indexBuffer, numIndices,
|
|---|
| 133 | graphicsQueue);
|
|---|
| 134 | numIndices += indices.size();
|
|---|
| 135 |
|
|---|
| 136 | return true;
|
|---|
| 137 | }
|
|---|
| 138 |
|
|---|
| 139 | return false;
|
|---|
| [87c8f1a] | 140 | }
|
|---|
| 141 |
|
|---|
| [7d2b0b9] | 142 | #endif // _GRAPHICS_PIPELINE_VULKAN_H
|
|---|