[7d2b0b9] | 1 | #ifndef _GRAPHICS_PIPELINE_VULKAN_H
|
---|
| 2 | #define _GRAPHICS_PIPELINE_VULKAN_H
|
---|
| 3 |
|
---|
| 4 | #include "graphics-pipeline.hpp"
|
---|
| 5 |
|
---|
[b8777b7] | 6 | #include <fstream>
|
---|
[860a0da] | 7 | #include <iostream>
|
---|
[cd487fb] | 8 | #include <stdexcept>
|
---|
[7d2b0b9] | 9 | #include <vector>
|
---|
| 10 |
|
---|
[771b33a] | 11 | #include <vulkan/vulkan.h>
|
---|
| 12 |
|
---|
[cd1cb0f] | 13 | #define GLM_FORCE_RADIANS
|
---|
| 14 | #define GLM_FORCE_DEPTH_ZERO_TO_ONE // Since, in Vulkan, the depth range is 0 to 1 instead of -1 to 1
|
---|
| 15 | #define GLM_FORCE_RIGHT_HANDED
|
---|
| 16 |
|
---|
| 17 | #include <glm/glm.hpp>
|
---|
| 18 | #include <glm/gtc/matrix_transform.hpp>
|
---|
| 19 |
|
---|
[e3bef3a] | 20 | #include "vulkan-utils.hpp"
|
---|
| 21 |
|
---|
[cd1cb0f] | 22 | using namespace glm;
|
---|
| 23 |
|
---|
[b794178] | 24 | // TODO: Maybe change the name of this struct so I can call the list something other than descriptorInfoList
|
---|
| 25 | struct DescriptorInfo {
|
---|
| 26 | VkDescriptorType type;
|
---|
| 27 | VkShaderStageFlags stageFlags;
|
---|
| 28 |
|
---|
| 29 | // Only one of the below properties should be set
|
---|
| 30 | vector<VkDescriptorBufferInfo>* bufferDataList;
|
---|
| 31 | VkDescriptorImageInfo* imageData;
|
---|
| 32 | };
|
---|
| 33 |
|
---|
[9d21aac] | 34 | template<class VertexType>
|
---|
[7d2b0b9] | 35 | class GraphicsPipeline_Vulkan : public GraphicsPipeline {
|
---|
| 36 | public:
|
---|
[44f23af] | 37 | string vertShaderFile, fragShaderFile;
|
---|
| 38 |
|
---|
[b8777b7] | 39 | GraphicsPipeline_Vulkan();
|
---|
[860a0da] | 40 |
|
---|
[52a02e6] | 41 | GraphicsPipeline_Vulkan(VkPrimitiveTopology topology, VkPhysicalDevice physicalDevice, VkDevice device,
|
---|
[58453c3] | 42 | VkRenderPass renderPass, Viewport viewport, size_t vertexCapacity, size_t indexCapacity);
|
---|
[7d2b0b9] | 43 | ~GraphicsPipeline_Vulkan();
|
---|
| 44 |
|
---|
[0fe8433] | 45 | size_t getNumVertices();
|
---|
| 46 |
|
---|
[0ae182f] | 47 | void updateRenderPass(VkRenderPass renderPass);
|
---|
| 48 |
|
---|
[b794178] | 49 | // Maybe I should rename these to addVertexAttribute (addVaryingAttribute) and addUniformAttribute
|
---|
| 50 |
|
---|
[771b33a] | 51 | void addAttribute(VkFormat format, size_t offset);
|
---|
[b794178] | 52 |
|
---|
[e8445f0] | 53 | // TODO: I might be able to use a single VkDescriptorBufferInfo here and reuse it when creating the descriptor sets
|
---|
| 54 | void addDescriptorInfo(VkDescriptorType type, VkShaderStageFlags stageFlags,
|
---|
| 55 | vector<VkDescriptorBufferInfo>* bufferData);
|
---|
[b794178] | 56 | void addDescriptorInfo(VkDescriptorType type, VkShaderStageFlags stageFlags, VkDescriptorImageInfo* imageData);
|
---|
| 57 |
|
---|
[58453c3] | 58 | void updateDescriptorInfo(uint32_t index, vector<VkDescriptorBufferInfo>* bufferData, uint32_t size);
|
---|
[9d21aac] | 59 |
|
---|
[7d2b0b9] | 60 | void createPipeline(string vertShaderFile, string fragShaderFile);
|
---|
[b794178] | 61 | void createDescriptorSetLayout();
|
---|
[58453c3] | 62 | void createDescriptorPool(uint32_t size);
|
---|
| 63 | void createDescriptorSets(uint32_t size);
|
---|
[b794178] | 64 |
|
---|
[603b5bc] | 65 | void createRenderCommands(VkCommandBuffer& commandBuffer, uint32_t currentImage);
|
---|
| 66 |
|
---|
[9d21aac] | 67 | void addObject(const vector<VertexType>& vertices, vector<uint16_t> indices, VkCommandPool commandPool,
|
---|
| 68 | VkQueue graphicsQueue);
|
---|
[2da64ef] | 69 |
|
---|
[a52ba87] | 70 | void updateObjectVertices(size_t objIndex, const vector<VertexType>& vertices, VkCommandPool commandPool,
|
---|
[e8445f0] | 71 | VkQueue graphicsQueue);
|
---|
[a52ba87] | 72 |
|
---|
[b794178] | 73 | void cleanup();
|
---|
| 74 | void cleanupBuffers();
|
---|
[7d2b0b9] | 75 |
|
---|
| 76 | private:
|
---|
[52a02e6] | 77 | VkPrimitiveTopology topology;
|
---|
[87c8f1a] | 78 | VkPhysicalDevice physicalDevice;
|
---|
[7d2b0b9] | 79 | VkDevice device;
|
---|
[b794178] | 80 | VkRenderPass renderPass;
|
---|
| 81 |
|
---|
| 82 | VkPipeline pipeline;
|
---|
| 83 | VkPipelineLayout pipelineLayout;
|
---|
| 84 |
|
---|
[771b33a] | 85 | VkVertexInputBindingDescription bindingDescription;
|
---|
[b794178] | 86 |
|
---|
[771b33a] | 87 | vector<VkVertexInputAttributeDescription> attributeDescriptions;
|
---|
[b794178] | 88 | vector<DescriptorInfo> descriptorInfoList;
|
---|
[7d2b0b9] | 89 |
|
---|
[b794178] | 90 | VkDescriptorSetLayout descriptorSetLayout;
|
---|
| 91 | VkDescriptorPool descriptorPool;
|
---|
| 92 | vector<VkDescriptorSet> descriptorSets;
|
---|
[87c8f1a] | 93 |
|
---|
| 94 | size_t numVertices;
|
---|
| 95 | size_t vertexCapacity;
|
---|
| 96 | VkBuffer vertexBuffer;
|
---|
| 97 | VkDeviceMemory vertexBufferMemory;
|
---|
| 98 |
|
---|
| 99 | size_t numIndices;
|
---|
| 100 | size_t indexCapacity;
|
---|
| 101 | VkBuffer indexBuffer;
|
---|
| 102 | VkDeviceMemory indexBufferMemory;
|
---|
[b8777b7] | 103 |
|
---|
| 104 | VkShaderModule createShaderModule(const vector<char>& code);
|
---|
| 105 | vector<char> readFile(const string& filename);
|
---|
| 106 |
|
---|
[5a0242e] | 107 | void resizeVertexBuffer(VkCommandPool commandPool, VkQueue graphicsQueue);
|
---|
| 108 | void resizeIndexBuffer(VkCommandPool commandPool, VkQueue graphicsQueue);
|
---|
[7d2b0b9] | 109 | };
|
---|
| 110 |
|
---|
[b8777b7] | 111 | /*** PUBLIC METHODS ***/
|
---|
| 112 |
|
---|
[9d21aac] | 113 | template<class VertexType>
|
---|
| 114 | GraphicsPipeline_Vulkan<VertexType>::GraphicsPipeline_Vulkan() {
|
---|
[b8777b7] | 115 | }
|
---|
| 116 |
|
---|
[683dd55] | 117 | // TODO: Verify that vertex capacity and index capacity are both > 0
|
---|
[3b84bb6] | 118 | // TODO: See if it would be feasible to move code in the createPipeline method
|
---|
| 119 | // into the constructor. That way, I can also put relevant cleanup code into the destructor
|
---|
[9d21aac] | 120 | template<class VertexType>
|
---|
[a3cefaa] | 121 | GraphicsPipeline_Vulkan<VertexType>::GraphicsPipeline_Vulkan(VkPrimitiveTopology topology,
|
---|
| 122 | VkPhysicalDevice physicalDevice, VkDevice device,
|
---|
| 123 | VkRenderPass renderPass, Viewport viewport,
|
---|
[58453c3] | 124 | size_t vertexCapacity, size_t indexCapacity)
|
---|
[a3cefaa] | 125 | : topology(topology)
|
---|
| 126 | , physicalDevice(physicalDevice)
|
---|
| 127 | , device(device)
|
---|
| 128 | , renderPass(renderPass) {
|
---|
| 129 | // This is a member of the base GraphicsPipeline class
|
---|
| 130 | // It currently is not used for the OpenGL pipeline. Either figure out why (since OpenGL certainly has the concept of
|
---|
| 131 | // viewports) and use it there too and add viewport the the base class constructor, or create a second base class
|
---|
| 132 | // constructor which takes the viewport
|
---|
[b8777b7] | 133 | this->viewport = viewport;
|
---|
| 134 |
|
---|
| 135 | // Since there is only one array of vertex data, we use binding = 0
|
---|
| 136 | // I'll probably do that for the foreseeable future
|
---|
| 137 | // I can calculate the stride myself given info about all the varying attributes
|
---|
| 138 | this->bindingDescription.binding = 0;
|
---|
[5a0242e] | 139 | this->bindingDescription.stride = sizeof(VertexType);
|
---|
[b8777b7] | 140 | this->bindingDescription.inputRate = VK_VERTEX_INPUT_RATE_VERTEX;
|
---|
| 141 |
|
---|
[5a0242e] | 142 | this->numVertices = 0;
|
---|
| 143 | this->vertexCapacity = vertexCapacity;
|
---|
[b8777b7] | 144 |
|
---|
[5a0242e] | 145 | VulkanUtils::createBuffer(device, physicalDevice, vertexCapacity * sizeof(VertexType),
|
---|
| 146 | VK_BUFFER_USAGE_TRANSFER_SRC_BIT | VK_BUFFER_USAGE_TRANSFER_DST_BIT | VK_BUFFER_USAGE_VERTEX_BUFFER_BIT,
|
---|
| 147 | VK_MEMORY_PROPERTY_DEVICE_LOCAL_BIT, vertexBuffer, vertexBufferMemory);
|
---|
[e3bef3a] | 148 |
|
---|
[5a0242e] | 149 | this->numIndices = 0;
|
---|
| 150 | this->indexCapacity = indexCapacity;
|
---|
[87c8f1a] | 151 |
|
---|
[5a0242e] | 152 | VulkanUtils::createBuffer(device, physicalDevice, indexCapacity * sizeof(uint16_t),
|
---|
| 153 | VK_BUFFER_USAGE_TRANSFER_SRC_BIT | VK_BUFFER_USAGE_TRANSFER_DST_BIT | VK_BUFFER_USAGE_INDEX_BUFFER_BIT,
|
---|
| 154 | VK_MEMORY_PROPERTY_DEVICE_LOCAL_BIT, indexBuffer, indexBufferMemory);
|
---|
[e3bef3a] | 155 | }
|
---|
| 156 |
|
---|
[860a0da] | 157 | // TODO: Move as much cleanup as I can into the destructor
|
---|
[9d21aac] | 158 | template<class VertexType>
|
---|
| 159 | GraphicsPipeline_Vulkan<VertexType>::~GraphicsPipeline_Vulkan() {
|
---|
[b8777b7] | 160 | }
|
---|
| 161 |
|
---|
[9d21aac] | 162 | template<class VertexType>
|
---|
| 163 | size_t GraphicsPipeline_Vulkan<VertexType>::getNumVertices() {
|
---|
[0fe8433] | 164 | return numVertices;
|
---|
| 165 | }
|
---|
| 166 |
|
---|
[9d21aac] | 167 | template<class VertexType>
|
---|
| 168 | void GraphicsPipeline_Vulkan<VertexType>::updateRenderPass(VkRenderPass renderPass) {
|
---|
[5a0242e] | 169 | this->renderPass = renderPass;
|
---|
[b8777b7] | 170 | }
|
---|
| 171 |
|
---|
[9d21aac] | 172 | template<class VertexType>
|
---|
| 173 | void GraphicsPipeline_Vulkan<VertexType>::addAttribute(VkFormat format, size_t offset) {
|
---|
[b8777b7] | 174 | VkVertexInputAttributeDescription attributeDesc = {};
|
---|
| 175 |
|
---|
| 176 | attributeDesc.binding = 0;
|
---|
| 177 | attributeDesc.location = this->attributeDescriptions.size();
|
---|
| 178 | attributeDesc.format = format;
|
---|
| 179 | attributeDesc.offset = offset;
|
---|
| 180 |
|
---|
| 181 | this->attributeDescriptions.push_back(attributeDesc);
|
---|
| 182 | }
|
---|
| 183 |
|
---|
[9d21aac] | 184 | template<class VertexType>
|
---|
[58453c3] | 185 | void GraphicsPipeline_Vulkan<VertexType>::addDescriptorInfo(VkDescriptorType type, VkShaderStageFlags stageFlags,
|
---|
| 186 | vector<VkDescriptorBufferInfo>* bufferData) {
|
---|
[b8777b7] | 187 | this->descriptorInfoList.push_back({ type, stageFlags, bufferData, nullptr });
|
---|
| 188 | }
|
---|
| 189 |
|
---|
[9d21aac] | 190 | template<class VertexType>
|
---|
[58453c3] | 191 | void GraphicsPipeline_Vulkan<VertexType>::addDescriptorInfo(VkDescriptorType type, VkShaderStageFlags stageFlags,
|
---|
| 192 | VkDescriptorImageInfo* imageData) {
|
---|
[b8777b7] | 193 | this->descriptorInfoList.push_back({ type, stageFlags, nullptr, imageData });
|
---|
| 194 | }
|
---|
| 195 |
|
---|
[58453c3] | 196 | // TODO: Maybe make an analogous one for updating image info
|
---|
| 197 | // Also, I may want to rewrite this function to call vkUpdateDescriptorSets once and call it once
|
---|
| 198 | // per swapchain image from VulkanGame
|
---|
[9d21aac] | 199 | template<class VertexType>
|
---|
| 200 | void GraphicsPipeline_Vulkan<VertexType>::updateDescriptorInfo(uint32_t index,
|
---|
[a3cefaa] | 201 | vector<VkDescriptorBufferInfo>* bufferData,
|
---|
[58453c3] | 202 | uint32_t size) {
|
---|
[9d21aac] | 203 | this->descriptorInfoList[index].bufferDataList = bufferData;
|
---|
[a3cefaa] | 204 |
|
---|
| 205 | // TODO: This code was mostly copied from createDescriptorSets. I should make some common function they both use
|
---|
| 206 | // for updating descriptor sets
|
---|
[58453c3] | 207 | for (size_t i = 0; i < size; i++) {
|
---|
[a3cefaa] | 208 | VkWriteDescriptorSet descriptorWrite = {};
|
---|
| 209 |
|
---|
| 210 | descriptorWrite.sType = VK_STRUCTURE_TYPE_WRITE_DESCRIPTOR_SET;
|
---|
| 211 | descriptorWrite.dstSet = this->descriptorSets[i];
|
---|
| 212 | descriptorWrite.dstBinding = index;
|
---|
| 213 | descriptorWrite.dstArrayElement = 0;
|
---|
| 214 | descriptorWrite.descriptorType = this->descriptorInfoList[index].type;
|
---|
| 215 | descriptorWrite.descriptorCount = 1;
|
---|
| 216 | descriptorWrite.pBufferInfo = nullptr;
|
---|
| 217 | descriptorWrite.pImageInfo = nullptr;
|
---|
| 218 | descriptorWrite.pTexelBufferView = nullptr;
|
---|
| 219 |
|
---|
| 220 | // This method is only intended for updated descriptor sets of type VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER,
|
---|
| 221 | // but I'm leaving that in here for completeness
|
---|
| 222 | switch (descriptorWrite.descriptorType) {
|
---|
| 223 | case VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER:
|
---|
| 224 | case VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER_DYNAMIC:
|
---|
| 225 | case VK_DESCRIPTOR_TYPE_STORAGE_BUFFER:
|
---|
| 226 | descriptorWrite.pBufferInfo = &(*this->descriptorInfoList[index].bufferDataList)[i];
|
---|
| 227 | break;
|
---|
| 228 | case VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER:
|
---|
| 229 | descriptorWrite.pImageInfo = this->descriptorInfoList[index].imageData;
|
---|
| 230 | break;
|
---|
| 231 | default:
|
---|
| 232 | throw runtime_error("Unknown descriptor type: " + to_string(descriptorWrite.descriptorType));
|
---|
| 233 | }
|
---|
| 234 |
|
---|
[58453c3] | 235 | // TODO: Instead, assert that (bufferData->size() == swapChainImages.size() (now just changed to size)
|
---|
| 236 | if (bufferData->size() != size) {
|
---|
[a3cefaa] | 237 | cout << "ALERT ALERT ALERT: SIZE MISMATCH!!!!!!!" << endl;
|
---|
| 238 | }
|
---|
| 239 |
|
---|
| 240 | vkUpdateDescriptorSets(this->device, 1, &descriptorWrite, 0, nullptr);
|
---|
| 241 | }
|
---|
[9d21aac] | 242 | }
|
---|
| 243 |
|
---|
| 244 | template<class VertexType>
|
---|
| 245 | void GraphicsPipeline_Vulkan<VertexType>::createPipeline(string vertShaderFile, string fragShaderFile) {
|
---|
[44f23af] | 246 | this->vertShaderFile = vertShaderFile;
|
---|
| 247 | this->fragShaderFile = fragShaderFile;
|
---|
| 248 |
|
---|
[b8777b7] | 249 | vector<char> vertShaderCode = readFile(vertShaderFile);
|
---|
| 250 | vector<char> fragShaderCode = readFile(fragShaderFile);
|
---|
| 251 |
|
---|
| 252 | VkShaderModule vertShaderModule = createShaderModule(vertShaderCode);
|
---|
| 253 | VkShaderModule fragShaderModule = createShaderModule(fragShaderCode);
|
---|
| 254 |
|
---|
| 255 | VkPipelineShaderStageCreateInfo vertShaderStageInfo = {};
|
---|
| 256 | vertShaderStageInfo.sType = VK_STRUCTURE_TYPE_PIPELINE_SHADER_STAGE_CREATE_INFO;
|
---|
| 257 | vertShaderStageInfo.stage = VK_SHADER_STAGE_VERTEX_BIT;
|
---|
| 258 | vertShaderStageInfo.module = vertShaderModule;
|
---|
| 259 | vertShaderStageInfo.pName = "main";
|
---|
| 260 |
|
---|
| 261 | VkPipelineShaderStageCreateInfo fragShaderStageInfo = {};
|
---|
| 262 | fragShaderStageInfo.sType = VK_STRUCTURE_TYPE_PIPELINE_SHADER_STAGE_CREATE_INFO;
|
---|
| 263 | fragShaderStageInfo.stage = VK_SHADER_STAGE_FRAGMENT_BIT;
|
---|
| 264 | fragShaderStageInfo.module = fragShaderModule;
|
---|
| 265 | fragShaderStageInfo.pName = "main";
|
---|
| 266 |
|
---|
| 267 | VkPipelineShaderStageCreateInfo shaderStages[] = { vertShaderStageInfo, fragShaderStageInfo };
|
---|
| 268 |
|
---|
| 269 | VkPipelineVertexInputStateCreateInfo vertexInputInfo = {};
|
---|
| 270 | vertexInputInfo.sType = VK_STRUCTURE_TYPE_PIPELINE_VERTEX_INPUT_STATE_CREATE_INFO;
|
---|
| 271 |
|
---|
| 272 | vertexInputInfo.vertexBindingDescriptionCount = 1;
|
---|
| 273 | vertexInputInfo.vertexAttributeDescriptionCount = static_cast<uint32_t>(this->attributeDescriptions.size());
|
---|
| 274 | vertexInputInfo.pVertexBindingDescriptions = &this->bindingDescription;
|
---|
| 275 | vertexInputInfo.pVertexAttributeDescriptions = this->attributeDescriptions.data();
|
---|
| 276 |
|
---|
| 277 | VkPipelineInputAssemblyStateCreateInfo inputAssembly = {};
|
---|
| 278 | inputAssembly.sType = VK_STRUCTURE_TYPE_PIPELINE_INPUT_ASSEMBLY_STATE_CREATE_INFO;
|
---|
[52a02e6] | 279 | inputAssembly.topology = this->topology;
|
---|
[b8777b7] | 280 | inputAssembly.primitiveRestartEnable = VK_FALSE;
|
---|
| 281 |
|
---|
| 282 | VkViewport viewport = {};
|
---|
| 283 | viewport.x = (float)this->viewport.x;
|
---|
| 284 | viewport.y = (float)this->viewport.y;
|
---|
| 285 | viewport.width = (float)this->viewport.width;
|
---|
| 286 | viewport.height = (float)this->viewport.height;
|
---|
| 287 | viewport.minDepth = 0.0f;
|
---|
| 288 | viewport.maxDepth = 1.0f;
|
---|
| 289 |
|
---|
| 290 | VkRect2D scissor = {};
|
---|
| 291 | scissor.offset = { 0, 0 };
|
---|
| 292 | scissor.extent = { (uint32_t)this->viewport.width, (uint32_t)this->viewport.height };
|
---|
| 293 |
|
---|
| 294 | VkPipelineViewportStateCreateInfo viewportState = {};
|
---|
| 295 | viewportState.sType = VK_STRUCTURE_TYPE_PIPELINE_VIEWPORT_STATE_CREATE_INFO;
|
---|
| 296 | viewportState.viewportCount = 1;
|
---|
| 297 | viewportState.pViewports = &viewport;
|
---|
| 298 | viewportState.scissorCount = 1;
|
---|
| 299 | viewportState.pScissors = &scissor;
|
---|
| 300 |
|
---|
| 301 | VkPipelineRasterizationStateCreateInfo rasterizer = {};
|
---|
| 302 | rasterizer.sType = VK_STRUCTURE_TYPE_PIPELINE_RASTERIZATION_STATE_CREATE_INFO;
|
---|
| 303 | rasterizer.depthClampEnable = VK_FALSE;
|
---|
| 304 | rasterizer.rasterizerDiscardEnable = VK_FALSE;
|
---|
| 305 | rasterizer.polygonMode = VK_POLYGON_MODE_FILL;
|
---|
| 306 | rasterizer.lineWidth = 1.0f;
|
---|
| 307 | rasterizer.cullMode = VK_CULL_MODE_BACK_BIT;
|
---|
| 308 | rasterizer.frontFace = VK_FRONT_FACE_COUNTER_CLOCKWISE;
|
---|
| 309 | rasterizer.depthBiasEnable = VK_FALSE;
|
---|
| 310 |
|
---|
| 311 | VkPipelineMultisampleStateCreateInfo multisampling = {};
|
---|
| 312 | multisampling.sType = VK_STRUCTURE_TYPE_PIPELINE_MULTISAMPLE_STATE_CREATE_INFO;
|
---|
| 313 | multisampling.sampleShadingEnable = VK_FALSE;
|
---|
| 314 | multisampling.rasterizationSamples = VK_SAMPLE_COUNT_1_BIT;
|
---|
| 315 |
|
---|
| 316 | VkPipelineColorBlendAttachmentState colorBlendAttachment = {};
|
---|
| 317 | colorBlendAttachment.colorWriteMask = VK_COLOR_COMPONENT_R_BIT | VK_COLOR_COMPONENT_G_BIT | VK_COLOR_COMPONENT_B_BIT | VK_COLOR_COMPONENT_A_BIT;
|
---|
| 318 | colorBlendAttachment.blendEnable = VK_TRUE;
|
---|
| 319 | colorBlendAttachment.colorBlendOp = VK_BLEND_OP_ADD;
|
---|
| 320 | colorBlendAttachment.srcColorBlendFactor = VK_BLEND_FACTOR_SRC_ALPHA;
|
---|
| 321 | colorBlendAttachment.dstColorBlendFactor = VK_BLEND_FACTOR_ONE_MINUS_SRC_ALPHA;
|
---|
| 322 | colorBlendAttachment.alphaBlendOp = VK_BLEND_OP_ADD;
|
---|
| 323 | colorBlendAttachment.srcAlphaBlendFactor = VK_BLEND_FACTOR_SRC_ALPHA;
|
---|
| 324 | colorBlendAttachment.dstAlphaBlendFactor = VK_BLEND_FACTOR_ONE_MINUS_SRC_ALPHA;
|
---|
| 325 |
|
---|
| 326 | VkPipelineColorBlendStateCreateInfo colorBlending = {};
|
---|
| 327 | colorBlending.sType = VK_STRUCTURE_TYPE_PIPELINE_COLOR_BLEND_STATE_CREATE_INFO;
|
---|
| 328 | colorBlending.logicOpEnable = VK_FALSE;
|
---|
| 329 | colorBlending.logicOp = VK_LOGIC_OP_COPY;
|
---|
| 330 | colorBlending.attachmentCount = 1;
|
---|
| 331 | colorBlending.pAttachments = &colorBlendAttachment;
|
---|
| 332 | colorBlending.blendConstants[0] = 0.0f;
|
---|
| 333 | colorBlending.blendConstants[1] = 0.0f;
|
---|
| 334 | colorBlending.blendConstants[2] = 0.0f;
|
---|
| 335 | colorBlending.blendConstants[3] = 0.0f;
|
---|
| 336 |
|
---|
| 337 | VkPipelineDepthStencilStateCreateInfo depthStencil = {};
|
---|
| 338 | depthStencil.sType = VK_STRUCTURE_TYPE_PIPELINE_DEPTH_STENCIL_STATE_CREATE_INFO;
|
---|
| 339 | depthStencil.depthTestEnable = VK_TRUE;
|
---|
| 340 | depthStencil.depthWriteEnable = VK_TRUE;
|
---|
| 341 | depthStencil.depthCompareOp = VK_COMPARE_OP_LESS;
|
---|
| 342 | depthStencil.depthBoundsTestEnable = VK_FALSE;
|
---|
| 343 | depthStencil.minDepthBounds = 0.0f;
|
---|
| 344 | depthStencil.maxDepthBounds = 1.0f;
|
---|
| 345 | depthStencil.stencilTestEnable = VK_FALSE;
|
---|
| 346 | depthStencil.front = {};
|
---|
| 347 | depthStencil.back = {};
|
---|
| 348 |
|
---|
| 349 | VkPipelineLayoutCreateInfo pipelineLayoutInfo = {};
|
---|
| 350 | pipelineLayoutInfo.sType = VK_STRUCTURE_TYPE_PIPELINE_LAYOUT_CREATE_INFO;
|
---|
| 351 | pipelineLayoutInfo.setLayoutCount = 1;
|
---|
| 352 | pipelineLayoutInfo.pSetLayouts = &this->descriptorSetLayout;
|
---|
| 353 | pipelineLayoutInfo.pushConstantRangeCount = 0;
|
---|
| 354 |
|
---|
| 355 | if (vkCreatePipelineLayout(this->device, &pipelineLayoutInfo, nullptr, &this->pipelineLayout) != VK_SUCCESS) {
|
---|
| 356 | throw runtime_error("failed to create pipeline layout!");
|
---|
| 357 | }
|
---|
| 358 |
|
---|
| 359 | VkGraphicsPipelineCreateInfo pipelineInfo = {};
|
---|
| 360 | pipelineInfo.sType = VK_STRUCTURE_TYPE_GRAPHICS_PIPELINE_CREATE_INFO;
|
---|
| 361 | pipelineInfo.stageCount = 2;
|
---|
| 362 | pipelineInfo.pStages = shaderStages;
|
---|
| 363 | pipelineInfo.pVertexInputState = &vertexInputInfo;
|
---|
| 364 | pipelineInfo.pInputAssemblyState = &inputAssembly;
|
---|
| 365 | pipelineInfo.pViewportState = &viewportState;
|
---|
| 366 | pipelineInfo.pRasterizationState = &rasterizer;
|
---|
| 367 | pipelineInfo.pMultisampleState = &multisampling;
|
---|
| 368 | pipelineInfo.pDepthStencilState = &depthStencil;
|
---|
| 369 | pipelineInfo.pColorBlendState = &colorBlending;
|
---|
| 370 | pipelineInfo.pDynamicState = nullptr;
|
---|
| 371 | pipelineInfo.layout = this->pipelineLayout;
|
---|
| 372 | pipelineInfo.renderPass = this->renderPass;
|
---|
| 373 | pipelineInfo.subpass = 0;
|
---|
| 374 | pipelineInfo.basePipelineHandle = VK_NULL_HANDLE;
|
---|
| 375 | pipelineInfo.basePipelineIndex = -1;
|
---|
| 376 |
|
---|
| 377 | if (vkCreateGraphicsPipelines(this->device, VK_NULL_HANDLE, 1, &pipelineInfo, nullptr, &this->pipeline) != VK_SUCCESS) {
|
---|
| 378 | throw runtime_error("failed to create graphics pipeline!");
|
---|
| 379 | }
|
---|
| 380 |
|
---|
| 381 | vkDestroyShaderModule(this->device, vertShaderModule, nullptr);
|
---|
| 382 | vkDestroyShaderModule(this->device, fragShaderModule, nullptr);
|
---|
| 383 | }
|
---|
| 384 |
|
---|
[9d21aac] | 385 | template<class VertexType>
|
---|
| 386 | void GraphicsPipeline_Vulkan<VertexType>::createDescriptorSetLayout() {
|
---|
[b8777b7] | 387 | vector<VkDescriptorSetLayoutBinding> bindings(this->descriptorInfoList.size());
|
---|
| 388 |
|
---|
| 389 | for (size_t i = 0; i < bindings.size(); i++) {
|
---|
| 390 | bindings[i].binding = i;
|
---|
| 391 | bindings[i].descriptorCount = 1;
|
---|
| 392 | bindings[i].descriptorType = this->descriptorInfoList[i].type;
|
---|
| 393 | bindings[i].stageFlags = this->descriptorInfoList[i].stageFlags;
|
---|
| 394 | bindings[i].pImmutableSamplers = nullptr;
|
---|
| 395 | }
|
---|
| 396 |
|
---|
| 397 | VkDescriptorSetLayoutCreateInfo layoutInfo = {};
|
---|
| 398 | layoutInfo.sType = VK_STRUCTURE_TYPE_DESCRIPTOR_SET_LAYOUT_CREATE_INFO;
|
---|
| 399 | layoutInfo.bindingCount = static_cast<uint32_t>(bindings.size());
|
---|
| 400 | layoutInfo.pBindings = bindings.data();
|
---|
| 401 |
|
---|
| 402 | if (vkCreateDescriptorSetLayout(this->device, &layoutInfo, nullptr, &this->descriptorSetLayout) != VK_SUCCESS) {
|
---|
| 403 | throw runtime_error("failed to create descriptor set layout!");
|
---|
| 404 | }
|
---|
| 405 | }
|
---|
| 406 |
|
---|
[9d21aac] | 407 | template<class VertexType>
|
---|
[58453c3] | 408 | void GraphicsPipeline_Vulkan<VertexType>::createDescriptorPool(uint32_t size) {
|
---|
[b8777b7] | 409 | vector<VkDescriptorPoolSize> poolSizes(this->descriptorInfoList.size());
|
---|
| 410 |
|
---|
| 411 | for (size_t i = 0; i < poolSizes.size(); i++) {
|
---|
| 412 | poolSizes[i].type = this->descriptorInfoList[i].type;
|
---|
[58453c3] | 413 | poolSizes[i].descriptorCount = size;
|
---|
[b8777b7] | 414 | }
|
---|
| 415 |
|
---|
| 416 | VkDescriptorPoolCreateInfo poolInfo = {};
|
---|
| 417 | poolInfo.sType = VK_STRUCTURE_TYPE_DESCRIPTOR_POOL_CREATE_INFO;
|
---|
| 418 | poolInfo.poolSizeCount = static_cast<uint32_t>(poolSizes.size());
|
---|
| 419 | poolInfo.pPoolSizes = poolSizes.data();
|
---|
[58453c3] | 420 | poolInfo.maxSets = size;
|
---|
[b8777b7] | 421 |
|
---|
| 422 | if (vkCreateDescriptorPool(this->device, &poolInfo, nullptr, &this->descriptorPool) != VK_SUCCESS) {
|
---|
| 423 | throw runtime_error("failed to create descriptor pool!");
|
---|
| 424 | }
|
---|
| 425 | }
|
---|
| 426 |
|
---|
[9d21aac] | 427 | template<class VertexType>
|
---|
[58453c3] | 428 | void GraphicsPipeline_Vulkan<VertexType>::createDescriptorSets(uint32_t size) {
|
---|
| 429 | vector<VkDescriptorSetLayout> layouts(size, this->descriptorSetLayout);
|
---|
[b8777b7] | 430 |
|
---|
| 431 | VkDescriptorSetAllocateInfo allocInfo = {};
|
---|
| 432 | allocInfo.sType = VK_STRUCTURE_TYPE_DESCRIPTOR_SET_ALLOCATE_INFO;
|
---|
| 433 | allocInfo.descriptorPool = this->descriptorPool;
|
---|
[58453c3] | 434 | allocInfo.descriptorSetCount = size;
|
---|
[b8777b7] | 435 | allocInfo.pSetLayouts = layouts.data();
|
---|
| 436 |
|
---|
[58453c3] | 437 | this->descriptorSets.resize(size);
|
---|
[b8777b7] | 438 | if (vkAllocateDescriptorSets(device, &allocInfo, this->descriptorSets.data()) != VK_SUCCESS) {
|
---|
| 439 | throw runtime_error("failed to allocate descriptor sets!");
|
---|
| 440 | }
|
---|
[e3bef3a] | 441 |
|
---|
[58453c3] | 442 | for (size_t i = 0; i < size; i++) {
|
---|
[b8777b7] | 443 | vector<VkWriteDescriptorSet> descriptorWrites(this->descriptorInfoList.size());
|
---|
| 444 |
|
---|
| 445 | for (size_t j = 0; j < descriptorWrites.size(); j++) {
|
---|
| 446 | descriptorWrites[j].sType = VK_STRUCTURE_TYPE_WRITE_DESCRIPTOR_SET;
|
---|
| 447 | descriptorWrites[j].dstSet = this->descriptorSets[i];
|
---|
| 448 | descriptorWrites[j].dstBinding = j;
|
---|
| 449 | descriptorWrites[j].dstArrayElement = 0;
|
---|
| 450 | descriptorWrites[j].descriptorType = this->descriptorInfoList[j].type;
|
---|
| 451 | descriptorWrites[j].descriptorCount = 1;
|
---|
| 452 | descriptorWrites[j].pBufferInfo = nullptr;
|
---|
| 453 | descriptorWrites[j].pImageInfo = nullptr;
|
---|
| 454 | descriptorWrites[j].pTexelBufferView = nullptr;
|
---|
| 455 |
|
---|
| 456 | switch (descriptorWrites[j].descriptorType) {
|
---|
| 457 | case VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER:
|
---|
[e8445f0] | 458 | case VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER_DYNAMIC:
|
---|
[055750a] | 459 | case VK_DESCRIPTOR_TYPE_STORAGE_BUFFER:
|
---|
[b8777b7] | 460 | descriptorWrites[j].pBufferInfo = &(*this->descriptorInfoList[j].bufferDataList)[i];
|
---|
| 461 | break;
|
---|
| 462 | case VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER:
|
---|
| 463 | descriptorWrites[j].pImageInfo = this->descriptorInfoList[j].imageData;
|
---|
| 464 | break;
|
---|
| 465 | default:
|
---|
| 466 | throw runtime_error("Unknown descriptor type: " + to_string(descriptorWrites[j].descriptorType));
|
---|
| 467 | }
|
---|
[e3bef3a] | 468 | }
|
---|
| 469 |
|
---|
[b8777b7] | 470 | vkUpdateDescriptorSets(this->device, static_cast<uint32_t>(descriptorWrites.size()), descriptorWrites.data(), 0, nullptr);
|
---|
| 471 | }
|
---|
| 472 | }
|
---|
| 473 |
|
---|
[9d21aac] | 474 | template<class VertexType>
|
---|
| 475 | void GraphicsPipeline_Vulkan<VertexType>::createRenderCommands(VkCommandBuffer& commandBuffer, uint32_t currentImage) {
|
---|
[b8777b7] | 476 | vkCmdBindPipeline(commandBuffer, VK_PIPELINE_BIND_POINT_GRAPHICS, pipeline);
|
---|
[e8445f0] | 477 |
|
---|
[b8777b7] | 478 | vkCmdBindDescriptorSets(commandBuffer, VK_PIPELINE_BIND_POINT_GRAPHICS, pipelineLayout, 0, 1,
|
---|
| 479 | &descriptorSets[currentImage], 0, nullptr);
|
---|
| 480 |
|
---|
| 481 | VkBuffer vertexBuffers[] = { vertexBuffer };
|
---|
| 482 | VkDeviceSize offsets[] = { 0 };
|
---|
| 483 | vkCmdBindVertexBuffers(commandBuffer, 0, 1, vertexBuffers, offsets);
|
---|
| 484 |
|
---|
| 485 | vkCmdBindIndexBuffer(commandBuffer, indexBuffer, 0, VK_INDEX_TYPE_UINT16);
|
---|
| 486 |
|
---|
| 487 | vkCmdDrawIndexed(commandBuffer, static_cast<uint32_t>(numIndices), 1, 0, 0, 0);
|
---|
| 488 | }
|
---|
| 489 |
|
---|
[9d21aac] | 490 | template<class VertexType>
|
---|
| 491 | void GraphicsPipeline_Vulkan<VertexType>::addObject(const vector<VertexType>& vertices, vector<uint16_t> indices,
|
---|
| 492 | VkCommandPool commandPool, VkQueue graphicsQueue) {
|
---|
[3b84bb6] | 493 | // TODO: When resizing the vertex or index buffer, take deleted objects into account.
|
---|
| 494 | // Remove their data from the buffer and determine the new size of the bufer based on # of remining objects
|
---|
| 495 |
|
---|
| 496 | // If # non-deleted objects > currentCapacity / 2
|
---|
| 497 | // - resize and double capacity
|
---|
| 498 | // else If # non-deleted objects < currentCapacity / 4
|
---|
| 499 | // - resize amd halve capacity
|
---|
| 500 | // else
|
---|
| 501 | // - don't resize, but rewrite data in the buffer to only have non-deleted objects
|
---|
[b8777b7] | 502 |
|
---|
[a52ba87] | 503 | if (this->numVertices + vertices.size() > this->vertexCapacity) {
|
---|
[5a0242e] | 504 | resizeVertexBuffer(commandPool, graphicsQueue);
|
---|
| 505 | }
|
---|
[a52ba87] | 506 | VulkanUtils::copyDataToBuffer(this->device, this->physicalDevice, commandPool, vertices,
|
---|
| 507 | this->vertexBuffer, this->numVertices, graphicsQueue);
|
---|
| 508 | this->numVertices += vertices.size();
|
---|
[b8777b7] | 509 |
|
---|
[a52ba87] | 510 | if (this->numIndices + indices.size() > this->indexCapacity) {
|
---|
[44f23af] | 511 | resizeIndexBuffer(commandPool, graphicsQueue);
|
---|
| 512 | }
|
---|
[a52ba87] | 513 | VulkanUtils::copyDataToBuffer(this->device, this->physicalDevice, commandPool, indices,
|
---|
| 514 | this->indexBuffer, this->numIndices, graphicsQueue);
|
---|
| 515 | this->numIndices += indices.size();
|
---|
[2da64ef] | 516 | }
|
---|
| 517 |
|
---|
[a52ba87] | 518 | // Should only be used if the number of vertices has not changed
|
---|
[9d21aac] | 519 | template<class VertexType>
|
---|
| 520 | void GraphicsPipeline_Vulkan<VertexType>::updateObjectVertices(size_t objIndex,
|
---|
[a52ba87] | 521 | const vector<VertexType>& vertices, VkCommandPool commandPool, VkQueue graphicsQueue) {
|
---|
| 522 | VulkanUtils::copyDataToBuffer(this->device, this->physicalDevice, commandPool, vertices,
|
---|
| 523 | this->vertexBuffer, objIndex * vertices.size(), graphicsQueue);
|
---|
| 524 | }
|
---|
| 525 |
|
---|
[9d21aac] | 526 | template<class VertexType>
|
---|
| 527 | void GraphicsPipeline_Vulkan<VertexType>::cleanup() {
|
---|
[b8777b7] | 528 | vkDestroyPipeline(device, pipeline, nullptr);
|
---|
| 529 | vkDestroyDescriptorPool(device, descriptorPool, nullptr);
|
---|
[860a0da] | 530 |
|
---|
| 531 | // TODO: I read that the pipeline layout does not have to be recreated every time
|
---|
| 532 | // Try only creating it once
|
---|
[b8777b7] | 533 | vkDestroyPipelineLayout(device, pipelineLayout, nullptr);
|
---|
| 534 | }
|
---|
| 535 |
|
---|
[9d21aac] | 536 | template<class VertexType>
|
---|
| 537 | void GraphicsPipeline_Vulkan<VertexType>::cleanupBuffers() {
|
---|
[b8777b7] | 538 | vkDestroyDescriptorSetLayout(device, descriptorSetLayout, nullptr);
|
---|
| 539 |
|
---|
| 540 | vkDestroyBuffer(device, vertexBuffer, nullptr);
|
---|
| 541 | vkFreeMemory(device, vertexBufferMemory, nullptr);
|
---|
| 542 | vkDestroyBuffer(device, indexBuffer, nullptr);
|
---|
| 543 | vkFreeMemory(device, indexBufferMemory, nullptr);
|
---|
| 544 | }
|
---|
| 545 |
|
---|
| 546 | /*** PRIVATE METHODS ***/
|
---|
| 547 |
|
---|
[9d21aac] | 548 | template<class VertexType>
|
---|
| 549 | VkShaderModule GraphicsPipeline_Vulkan<VertexType>::createShaderModule(const vector<char>& code) {
|
---|
[b8777b7] | 550 | VkShaderModuleCreateInfo createInfo = {};
|
---|
| 551 | createInfo.sType = VK_STRUCTURE_TYPE_SHADER_MODULE_CREATE_INFO;
|
---|
| 552 | createInfo.codeSize = code.size();
|
---|
| 553 | createInfo.pCode = reinterpret_cast<const uint32_t*>(code.data());
|
---|
| 554 |
|
---|
| 555 | VkShaderModule shaderModule;
|
---|
| 556 | if (vkCreateShaderModule(this->device, &createInfo, nullptr, &shaderModule) != VK_SUCCESS) {
|
---|
| 557 | throw runtime_error("failed to create shader module!");
|
---|
| 558 | }
|
---|
| 559 |
|
---|
| 560 | return shaderModule;
|
---|
| 561 | }
|
---|
[e3bef3a] | 562 |
|
---|
[9d21aac] | 563 | template<class VertexType>
|
---|
| 564 | vector<char> GraphicsPipeline_Vulkan<VertexType>::readFile(const string& filename) {
|
---|
[b8777b7] | 565 | ifstream file(filename, ios::ate | ios::binary);
|
---|
[e3bef3a] | 566 |
|
---|
[b8777b7] | 567 | if (!file.is_open()) {
|
---|
| 568 | throw runtime_error("failed to open file!");
|
---|
[e3bef3a] | 569 | }
|
---|
| 570 |
|
---|
[b8777b7] | 571 | size_t fileSize = (size_t)file.tellg();
|
---|
| 572 | vector<char> buffer(fileSize);
|
---|
| 573 |
|
---|
| 574 | file.seekg(0);
|
---|
| 575 | file.read(buffer.data(), fileSize);
|
---|
| 576 |
|
---|
| 577 | file.close();
|
---|
| 578 |
|
---|
| 579 | return buffer;
|
---|
| 580 | }
|
---|
| 581 |
|
---|
[9d21aac] | 582 | template<class VertexType>
|
---|
| 583 | void GraphicsPipeline_Vulkan<VertexType>::resizeVertexBuffer(VkCommandPool commandPool, VkQueue graphicsQueue) {
|
---|
[5a0242e] | 584 | VkBuffer newVertexBuffer;
|
---|
| 585 | VkDeviceMemory newVertexBufferMemory;
|
---|
[860a0da] | 586 | this->vertexCapacity *= 2;
|
---|
[5a0242e] | 587 |
|
---|
[860a0da] | 588 | VulkanUtils::createBuffer(this->device, this->physicalDevice, this->vertexCapacity * sizeof(VertexType),
|
---|
[5a0242e] | 589 | VK_BUFFER_USAGE_TRANSFER_SRC_BIT | VK_BUFFER_USAGE_TRANSFER_DST_BIT | VK_BUFFER_USAGE_VERTEX_BUFFER_BIT,
|
---|
| 590 | VK_MEMORY_PROPERTY_DEVICE_LOCAL_BIT, newVertexBuffer, newVertexBufferMemory);
|
---|
| 591 |
|
---|
[860a0da] | 592 | VulkanUtils::copyBuffer(this->device, commandPool, vertexBuffer, newVertexBuffer, 0, 0, numVertices * sizeof(VertexType), graphicsQueue);
|
---|
[5a0242e] | 593 |
|
---|
[860a0da] | 594 | vkDestroyBuffer(this->device, vertexBuffer, nullptr);
|
---|
| 595 | vkFreeMemory(this->device, vertexBufferMemory, nullptr);
|
---|
[b8777b7] | 596 |
|
---|
[5a0242e] | 597 | vertexBuffer = newVertexBuffer;
|
---|
| 598 | vertexBufferMemory = newVertexBufferMemory;
|
---|
| 599 | }
|
---|
[b8777b7] | 600 |
|
---|
[9d21aac] | 601 | template<class VertexType>
|
---|
| 602 | void GraphicsPipeline_Vulkan<VertexType>::resizeIndexBuffer(VkCommandPool commandPool, VkQueue graphicsQueue) {
|
---|
[5a0242e] | 603 | VkBuffer newIndexBuffer;
|
---|
| 604 | VkDeviceMemory newIndexBufferMemory;
|
---|
[860a0da] | 605 | this->indexCapacity *= 2;
|
---|
[b8777b7] | 606 |
|
---|
[860a0da] | 607 | VulkanUtils::createBuffer(this->device, this->physicalDevice, this->indexCapacity * sizeof(uint16_t),
|
---|
[5a0242e] | 608 | VK_BUFFER_USAGE_TRANSFER_SRC_BIT | VK_BUFFER_USAGE_TRANSFER_DST_BIT | VK_BUFFER_USAGE_INDEX_BUFFER_BIT,
|
---|
| 609 | VK_MEMORY_PROPERTY_DEVICE_LOCAL_BIT, newIndexBuffer, newIndexBufferMemory);
|
---|
| 610 |
|
---|
[860a0da] | 611 | VulkanUtils::copyBuffer(this->device, commandPool, indexBuffer, newIndexBuffer, 0, 0, numIndices * sizeof(uint16_t), graphicsQueue);
|
---|
[5a0242e] | 612 |
|
---|
[860a0da] | 613 | vkDestroyBuffer(this->device, indexBuffer, nullptr);
|
---|
| 614 | vkFreeMemory(this->device, indexBufferMemory, nullptr);
|
---|
[5a0242e] | 615 |
|
---|
| 616 | indexBuffer = newIndexBuffer;
|
---|
| 617 | indexBufferMemory = newIndexBufferMemory;
|
---|
[87c8f1a] | 618 | }
|
---|
| 619 |
|
---|
[e8445f0] | 620 | #endif // _GRAPHICS_PIPELINE_VULKAN_H
|
---|