#ifndef _GRAPHICS_PIPELINE_VULKAN_H #define _GRAPHICS_PIPELINE_VULKAN_H #include "graphics-pipeline.hpp" #include #include #include #include #include #define GLM_FORCE_RADIANS #define GLM_FORCE_DEPTH_ZERO_TO_ONE // Since, in Vulkan, the depth range is 0 to 1 instead of -1 to 1 #define GLM_FORCE_RIGHT_HANDED #include #include #include "vulkan-utils.hpp" using namespace glm; // TODO: Maybe change the name of this struct so I can call the list something other than descriptorInfoList struct DescriptorInfo { VkDescriptorType type; VkShaderStageFlags stageFlags; // Only one of the below properties should be set vector* bufferDataList; VkDescriptorImageInfo* imageData; }; // TODO: Use this struct for uniform buffers as well (maybe move it to VulkanUtils) struct StorageBufferSet { vector buffers; vector memory; vector infoSet; }; template class GraphicsPipeline_Vulkan : public GraphicsPipeline { public: string vertShaderFile, fragShaderFile; GraphicsPipeline_Vulkan(); // TODO: swapChainImages is only ever used to get its size. Check how that is determined and, // if it will never change, just pass it in the constructor and save it // If it does change, I could add an updateSwapchainImageCount() function GraphicsPipeline_Vulkan(VkPrimitiveTopology topology, VkPhysicalDevice physicalDevice, VkDevice device, VkRenderPass renderPass, Viewport viewport, vector& swapChainImages, size_t vertexCapacity, size_t indexCapacity, size_t objectCapacity); ~GraphicsPipeline_Vulkan(); size_t getNumVertices(); void updateRenderPass(VkRenderPass renderPass); // Maybe I should rename these to addVertexAttribute (addVaryingAttribute) and addUniformAttribute void addAttribute(VkFormat format, size_t offset); void addStorageDescriptor(VkShaderStageFlags stageFlags); void addDescriptorInfo(VkDescriptorType type, VkShaderStageFlags stageFlags, vector* bufferData); void addDescriptorInfo(VkDescriptorType type, VkShaderStageFlags stageFlags, VkDescriptorImageInfo* imageData); void createPipeline(string vertShaderFile, string fragShaderFile); void createDescriptorSetLayout(); void createDescriptorPool(vector& swapChainImages); void createDescriptorSets(vector& swapChainImages); void createRenderCommands(VkCommandBuffer& commandBuffer, uint32_t currentImage); bool addObject(const vector& vertices, vector indices, SSBOType& ssbo, VkCommandPool commandPool, VkQueue graphicsQueue); void updateObject(size_t objIndex, SSBOType& ssbo); void updateObjectVertices(size_t objIndex, const vector& vertices, VkCommandPool commandPool, VkQueue graphicsQueue); void cleanup(); void cleanupBuffers(); private: VkPrimitiveTopology topology; VkPhysicalDevice physicalDevice; VkDevice device; VkRenderPass renderPass; VkPipeline pipeline; VkPipelineLayout pipelineLayout; VkVertexInputBindingDescription bindingDescription; vector attributeDescriptions; vector descriptorInfoList; VkDescriptorSetLayout descriptorSetLayout; VkDescriptorPool descriptorPool; vector descriptorSets; size_t numVertices; size_t vertexCapacity; VkBuffer vertexBuffer; VkDeviceMemory vertexBufferMemory; size_t numIndices; size_t indexCapacity; VkBuffer indexBuffer; VkDeviceMemory indexBufferMemory; size_t numObjects; size_t objectCapacity; StorageBufferSet storageBufferSet; VkShaderModule createShaderModule(const vector& code); vector readFile(const string& filename); void resizeVertexBuffer(VkCommandPool commandPool, VkQueue graphicsQueue); void resizeIndexBuffer(VkCommandPool commandPool, VkQueue graphicsQueue); void resizeStorageBufferSet(StorageBufferSet& set, VkCommandPool commandPool, VkQueue graphicsQueue); }; /*** PUBLIC METHODS ***/ template GraphicsPipeline_Vulkan::GraphicsPipeline_Vulkan() { } // TODO: Verify that vertex capacity and index capacity are both > 0 // TODO: See if it would be feasible to move code in the createPipeline method // into the constructor. That way, I can also put relevant cleanup code into the destructor template GraphicsPipeline_Vulkan::GraphicsPipeline_Vulkan( VkPrimitiveTopology topology, VkPhysicalDevice physicalDevice, VkDevice device, VkRenderPass renderPass, Viewport viewport, vector& swapChainImages, size_t vertexCapacity, size_t indexCapacity, size_t objectCapacity) { this->topology = topology; this->physicalDevice = physicalDevice; this->device = device; this->renderPass = renderPass; this->viewport = viewport; // Since there is only one array of vertex data, we use binding = 0 // I'll probably do that for the foreseeable future // I can calculate the stride myself given info about all the varying attributes this->bindingDescription.binding = 0; this->bindingDescription.stride = sizeof(VertexType); this->bindingDescription.inputRate = VK_VERTEX_INPUT_RATE_VERTEX; this->numVertices = 0; this->vertexCapacity = vertexCapacity; VulkanUtils::createBuffer(device, physicalDevice, vertexCapacity * sizeof(VertexType), VK_BUFFER_USAGE_TRANSFER_SRC_BIT | VK_BUFFER_USAGE_TRANSFER_DST_BIT | VK_BUFFER_USAGE_VERTEX_BUFFER_BIT, VK_MEMORY_PROPERTY_DEVICE_LOCAL_BIT, vertexBuffer, vertexBufferMemory); this->numIndices = 0; this->indexCapacity = indexCapacity; VulkanUtils::createBuffer(device, physicalDevice, indexCapacity * sizeof(uint16_t), VK_BUFFER_USAGE_TRANSFER_SRC_BIT | VK_BUFFER_USAGE_TRANSFER_DST_BIT | VK_BUFFER_USAGE_INDEX_BUFFER_BIT, VK_MEMORY_PROPERTY_DEVICE_LOCAL_BIT, indexBuffer, indexBufferMemory); this->numObjects = 0; this->objectCapacity = objectCapacity; // Hacky way to allow an SSBO to be optional // Specifying void* as the SSBOType will skip allocating the related buffers if (!is_same_v) { VkDeviceSize bufferSize = objectCapacity * sizeof(SSBOType); cout << "NUM SWAP CHAIN IMAGES: " << swapChainImages.size() << endl; storageBufferSet.buffers.resize(swapChainImages.size()); storageBufferSet.memory.resize(swapChainImages.size()); storageBufferSet.infoSet.resize(swapChainImages.size()); for (size_t i = 0; i < swapChainImages.size(); i++) { VulkanUtils::createBuffer(this->device, this->physicalDevice, bufferSize, VK_BUFFER_USAGE_TRANSFER_SRC_BIT | VK_BUFFER_USAGE_STORAGE_BUFFER_BIT, VK_MEMORY_PROPERTY_HOST_VISIBLE_BIT | VK_MEMORY_PROPERTY_HOST_COHERENT_BIT, storageBufferSet.buffers[i], storageBufferSet.memory[i]); storageBufferSet.infoSet[i].buffer = storageBufferSet.buffers[i]; storageBufferSet.infoSet[i].offset = 0; // This is the offset from the start of the buffer, so always 0 for now storageBufferSet.infoSet[i].range = bufferSize; // Size of the update starting from offset, or VK_WHOLE_SIZE } } } // TODO: Move as much cleanup as I can into the destructor template GraphicsPipeline_Vulkan::~GraphicsPipeline_Vulkan() { } template size_t GraphicsPipeline_Vulkan::getNumVertices() { return numVertices; } template void GraphicsPipeline_Vulkan::updateRenderPass(VkRenderPass renderPass) { this->renderPass = renderPass; } template void GraphicsPipeline_Vulkan::addAttribute(VkFormat format, size_t offset) { VkVertexInputAttributeDescription attributeDesc = {}; attributeDesc.binding = 0; attributeDesc.location = this->attributeDescriptions.size(); attributeDesc.format = format; attributeDesc.offset = offset; this->attributeDescriptions.push_back(attributeDesc); } // TODO: The SSBOType check isn't really needed since I call this function in VulkanGame explicitly template void GraphicsPipeline_Vulkan::addStorageDescriptor(VkShaderStageFlags stageFlags) { if (!is_same_v) { addDescriptorInfo(VK_DESCRIPTOR_TYPE_STORAGE_BUFFER, stageFlags, &storageBufferSet.infoSet); } } template void GraphicsPipeline_Vulkan::addDescriptorInfo(VkDescriptorType type, VkShaderStageFlags stageFlags, vector* bufferData) { this->descriptorInfoList.push_back({ type, stageFlags, bufferData, nullptr }); } template void GraphicsPipeline_Vulkan::addDescriptorInfo(VkDescriptorType type, VkShaderStageFlags stageFlags, VkDescriptorImageInfo* imageData) { this->descriptorInfoList.push_back({ type, stageFlags, nullptr, imageData }); } template void GraphicsPipeline_Vulkan::createPipeline(string vertShaderFile, string fragShaderFile) { this->vertShaderFile = vertShaderFile; this->fragShaderFile = fragShaderFile; vector vertShaderCode = readFile(vertShaderFile); vector fragShaderCode = readFile(fragShaderFile); VkShaderModule vertShaderModule = createShaderModule(vertShaderCode); VkShaderModule fragShaderModule = createShaderModule(fragShaderCode); VkPipelineShaderStageCreateInfo vertShaderStageInfo = {}; vertShaderStageInfo.sType = VK_STRUCTURE_TYPE_PIPELINE_SHADER_STAGE_CREATE_INFO; vertShaderStageInfo.stage = VK_SHADER_STAGE_VERTEX_BIT; vertShaderStageInfo.module = vertShaderModule; vertShaderStageInfo.pName = "main"; VkPipelineShaderStageCreateInfo fragShaderStageInfo = {}; fragShaderStageInfo.sType = VK_STRUCTURE_TYPE_PIPELINE_SHADER_STAGE_CREATE_INFO; fragShaderStageInfo.stage = VK_SHADER_STAGE_FRAGMENT_BIT; fragShaderStageInfo.module = fragShaderModule; fragShaderStageInfo.pName = "main"; VkPipelineShaderStageCreateInfo shaderStages[] = { vertShaderStageInfo, fragShaderStageInfo }; VkPipelineVertexInputStateCreateInfo vertexInputInfo = {}; vertexInputInfo.sType = VK_STRUCTURE_TYPE_PIPELINE_VERTEX_INPUT_STATE_CREATE_INFO; vertexInputInfo.vertexBindingDescriptionCount = 1; vertexInputInfo.vertexAttributeDescriptionCount = static_cast(this->attributeDescriptions.size()); vertexInputInfo.pVertexBindingDescriptions = &this->bindingDescription; vertexInputInfo.pVertexAttributeDescriptions = this->attributeDescriptions.data(); VkPipelineInputAssemblyStateCreateInfo inputAssembly = {}; inputAssembly.sType = VK_STRUCTURE_TYPE_PIPELINE_INPUT_ASSEMBLY_STATE_CREATE_INFO; inputAssembly.topology = this->topology; inputAssembly.primitiveRestartEnable = VK_FALSE; VkViewport viewport = {}; viewport.x = (float)this->viewport.x; viewport.y = (float)this->viewport.y; viewport.width = (float)this->viewport.width; viewport.height = (float)this->viewport.height; viewport.minDepth = 0.0f; viewport.maxDepth = 1.0f; VkRect2D scissor = {}; scissor.offset = { 0, 0 }; scissor.extent = { (uint32_t)this->viewport.width, (uint32_t)this->viewport.height }; VkPipelineViewportStateCreateInfo viewportState = {}; viewportState.sType = VK_STRUCTURE_TYPE_PIPELINE_VIEWPORT_STATE_CREATE_INFO; viewportState.viewportCount = 1; viewportState.pViewports = &viewport; viewportState.scissorCount = 1; viewportState.pScissors = &scissor; VkPipelineRasterizationStateCreateInfo rasterizer = {}; rasterizer.sType = VK_STRUCTURE_TYPE_PIPELINE_RASTERIZATION_STATE_CREATE_INFO; rasterizer.depthClampEnable = VK_FALSE; rasterizer.rasterizerDiscardEnable = VK_FALSE; rasterizer.polygonMode = VK_POLYGON_MODE_FILL; rasterizer.lineWidth = 1.0f; rasterizer.cullMode = VK_CULL_MODE_BACK_BIT; rasterizer.frontFace = VK_FRONT_FACE_COUNTER_CLOCKWISE; rasterizer.depthBiasEnable = VK_FALSE; VkPipelineMultisampleStateCreateInfo multisampling = {}; multisampling.sType = VK_STRUCTURE_TYPE_PIPELINE_MULTISAMPLE_STATE_CREATE_INFO; multisampling.sampleShadingEnable = VK_FALSE; multisampling.rasterizationSamples = VK_SAMPLE_COUNT_1_BIT; VkPipelineColorBlendAttachmentState colorBlendAttachment = {}; colorBlendAttachment.colorWriteMask = VK_COLOR_COMPONENT_R_BIT | VK_COLOR_COMPONENT_G_BIT | VK_COLOR_COMPONENT_B_BIT | VK_COLOR_COMPONENT_A_BIT; colorBlendAttachment.blendEnable = VK_TRUE; colorBlendAttachment.colorBlendOp = VK_BLEND_OP_ADD; colorBlendAttachment.srcColorBlendFactor = VK_BLEND_FACTOR_SRC_ALPHA; colorBlendAttachment.dstColorBlendFactor = VK_BLEND_FACTOR_ONE_MINUS_SRC_ALPHA; colorBlendAttachment.alphaBlendOp = VK_BLEND_OP_ADD; colorBlendAttachment.srcAlphaBlendFactor = VK_BLEND_FACTOR_SRC_ALPHA; colorBlendAttachment.dstAlphaBlendFactor = VK_BLEND_FACTOR_ONE_MINUS_SRC_ALPHA; VkPipelineColorBlendStateCreateInfo colorBlending = {}; colorBlending.sType = VK_STRUCTURE_TYPE_PIPELINE_COLOR_BLEND_STATE_CREATE_INFO; colorBlending.logicOpEnable = VK_FALSE; colorBlending.logicOp = VK_LOGIC_OP_COPY; colorBlending.attachmentCount = 1; colorBlending.pAttachments = &colorBlendAttachment; colorBlending.blendConstants[0] = 0.0f; colorBlending.blendConstants[1] = 0.0f; colorBlending.blendConstants[2] = 0.0f; colorBlending.blendConstants[3] = 0.0f; VkPipelineDepthStencilStateCreateInfo depthStencil = {}; depthStencil.sType = VK_STRUCTURE_TYPE_PIPELINE_DEPTH_STENCIL_STATE_CREATE_INFO; depthStencil.depthTestEnable = VK_TRUE; depthStencil.depthWriteEnable = VK_TRUE; depthStencil.depthCompareOp = VK_COMPARE_OP_LESS; depthStencil.depthBoundsTestEnable = VK_FALSE; depthStencil.minDepthBounds = 0.0f; depthStencil.maxDepthBounds = 1.0f; depthStencil.stencilTestEnable = VK_FALSE; depthStencil.front = {}; depthStencil.back = {}; VkPipelineLayoutCreateInfo pipelineLayoutInfo = {}; pipelineLayoutInfo.sType = VK_STRUCTURE_TYPE_PIPELINE_LAYOUT_CREATE_INFO; pipelineLayoutInfo.setLayoutCount = 1; pipelineLayoutInfo.pSetLayouts = &this->descriptorSetLayout; pipelineLayoutInfo.pushConstantRangeCount = 0; if (vkCreatePipelineLayout(this->device, &pipelineLayoutInfo, nullptr, &this->pipelineLayout) != VK_SUCCESS) { throw runtime_error("failed to create pipeline layout!"); } VkGraphicsPipelineCreateInfo pipelineInfo = {}; pipelineInfo.sType = VK_STRUCTURE_TYPE_GRAPHICS_PIPELINE_CREATE_INFO; pipelineInfo.stageCount = 2; pipelineInfo.pStages = shaderStages; pipelineInfo.pVertexInputState = &vertexInputInfo; pipelineInfo.pInputAssemblyState = &inputAssembly; pipelineInfo.pViewportState = &viewportState; pipelineInfo.pRasterizationState = &rasterizer; pipelineInfo.pMultisampleState = &multisampling; pipelineInfo.pDepthStencilState = &depthStencil; pipelineInfo.pColorBlendState = &colorBlending; pipelineInfo.pDynamicState = nullptr; pipelineInfo.layout = this->pipelineLayout; pipelineInfo.renderPass = this->renderPass; pipelineInfo.subpass = 0; pipelineInfo.basePipelineHandle = VK_NULL_HANDLE; pipelineInfo.basePipelineIndex = -1; if (vkCreateGraphicsPipelines(this->device, VK_NULL_HANDLE, 1, &pipelineInfo, nullptr, &this->pipeline) != VK_SUCCESS) { throw runtime_error("failed to create graphics pipeline!"); } vkDestroyShaderModule(this->device, vertShaderModule, nullptr); vkDestroyShaderModule(this->device, fragShaderModule, nullptr); } template void GraphicsPipeline_Vulkan::createDescriptorSetLayout() { vector bindings(this->descriptorInfoList.size()); for (size_t i = 0; i < bindings.size(); i++) { bindings[i].binding = i; bindings[i].descriptorCount = 1; bindings[i].descriptorType = this->descriptorInfoList[i].type; bindings[i].stageFlags = this->descriptorInfoList[i].stageFlags; bindings[i].pImmutableSamplers = nullptr; } VkDescriptorSetLayoutCreateInfo layoutInfo = {}; layoutInfo.sType = VK_STRUCTURE_TYPE_DESCRIPTOR_SET_LAYOUT_CREATE_INFO; layoutInfo.bindingCount = static_cast(bindings.size()); layoutInfo.pBindings = bindings.data(); if (vkCreateDescriptorSetLayout(this->device, &layoutInfo, nullptr, &this->descriptorSetLayout) != VK_SUCCESS) { throw runtime_error("failed to create descriptor set layout!"); } } template void GraphicsPipeline_Vulkan::createDescriptorPool(vector& swapChainImages) { vector poolSizes(this->descriptorInfoList.size()); for (size_t i = 0; i < poolSizes.size(); i++) { poolSizes[i].type = this->descriptorInfoList[i].type; poolSizes[i].descriptorCount = static_cast(swapChainImages.size()); } VkDescriptorPoolCreateInfo poolInfo = {}; poolInfo.sType = VK_STRUCTURE_TYPE_DESCRIPTOR_POOL_CREATE_INFO; poolInfo.poolSizeCount = static_cast(poolSizes.size()); poolInfo.pPoolSizes = poolSizes.data(); poolInfo.maxSets = static_cast(swapChainImages.size()); if (vkCreateDescriptorPool(this->device, &poolInfo, nullptr, &this->descriptorPool) != VK_SUCCESS) { throw runtime_error("failed to create descriptor pool!"); } } // TODO: Since I only need the size of the swapChainImages array, I should just pass that in instead of the whole array template void GraphicsPipeline_Vulkan::createDescriptorSets(vector& swapChainImages) { vector layouts(swapChainImages.size(), this->descriptorSetLayout); VkDescriptorSetAllocateInfo allocInfo = {}; allocInfo.sType = VK_STRUCTURE_TYPE_DESCRIPTOR_SET_ALLOCATE_INFO; allocInfo.descriptorPool = this->descriptorPool; allocInfo.descriptorSetCount = static_cast(swapChainImages.size()); allocInfo.pSetLayouts = layouts.data(); this->descriptorSets.resize(swapChainImages.size()); if (vkAllocateDescriptorSets(device, &allocInfo, this->descriptorSets.data()) != VK_SUCCESS) { throw runtime_error("failed to allocate descriptor sets!"); } for (size_t i = 0; i < swapChainImages.size(); i++) { vector descriptorWrites(this->descriptorInfoList.size()); for (size_t j = 0; j < descriptorWrites.size(); j++) { descriptorWrites[j].sType = VK_STRUCTURE_TYPE_WRITE_DESCRIPTOR_SET; descriptorWrites[j].dstSet = this->descriptorSets[i]; descriptorWrites[j].dstBinding = j; descriptorWrites[j].dstArrayElement = 0; descriptorWrites[j].descriptorType = this->descriptorInfoList[j].type; descriptorWrites[j].descriptorCount = 1; descriptorWrites[j].pBufferInfo = nullptr; descriptorWrites[j].pImageInfo = nullptr; descriptorWrites[j].pTexelBufferView = nullptr; switch (descriptorWrites[j].descriptorType) { case VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER: case VK_DESCRIPTOR_TYPE_STORAGE_BUFFER: descriptorWrites[j].pBufferInfo = &(*this->descriptorInfoList[j].bufferDataList)[i]; break; case VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER: descriptorWrites[j].pImageInfo = this->descriptorInfoList[j].imageData; break; default: throw runtime_error("Unknown descriptor type: " + to_string(descriptorWrites[j].descriptorType)); } } vkUpdateDescriptorSets(this->device, static_cast(descriptorWrites.size()), descriptorWrites.data(), 0, nullptr); } } template void GraphicsPipeline_Vulkan::createRenderCommands(VkCommandBuffer& commandBuffer, uint32_t currentImage) { vkCmdBindPipeline(commandBuffer, VK_PIPELINE_BIND_POINT_GRAPHICS, pipeline); vkCmdBindDescriptorSets(commandBuffer, VK_PIPELINE_BIND_POINT_GRAPHICS, pipelineLayout, 0, 1, &descriptorSets[currentImage], 0, nullptr); VkBuffer vertexBuffers[] = { vertexBuffer }; VkDeviceSize offsets[] = { 0 }; vkCmdBindVertexBuffers(commandBuffer, 0, 1, vertexBuffers, offsets); vkCmdBindIndexBuffer(commandBuffer, indexBuffer, 0, VK_INDEX_TYPE_UINT16); vkCmdDrawIndexed(commandBuffer, static_cast(numIndices), 1, 0, 0, 0); } template bool GraphicsPipeline_Vulkan::addObject( const vector& vertices, vector indices, SSBOType& ssbo, VkCommandPool commandPool, VkQueue graphicsQueue) { // TODO: When resizing the vertex or index buffer, take deleted objects into account. // Remove their data from the buffer and determine the new size of the bufer based on # of remining objects // If # non-deleted objects > currentCapacity / 2 // - resize and double capacity // else If # non-deleted objects < currentCapacity / 4 // - resize amd halve capacity // else // - don't resize, but rewrite data in the buffer to only have non-deleted objects if (this->numVertices + vertices.size() > this->vertexCapacity) { resizeVertexBuffer(commandPool, graphicsQueue); } VulkanUtils::copyDataToBuffer(this->device, this->physicalDevice, commandPool, vertices, this->vertexBuffer, this->numVertices, graphicsQueue); this->numVertices += vertices.size(); if (this->numIndices + indices.size() > this->indexCapacity) { resizeIndexBuffer(commandPool, graphicsQueue); } VulkanUtils::copyDataToBuffer(this->device, this->physicalDevice, commandPool, indices, this->indexBuffer, this->numIndices, graphicsQueue); this->numIndices += indices.size(); bool resizedStorageBuffer = false; if (!is_same_v) { if (this->numObjects == this->objectCapacity) { resizeStorageBufferSet(storageBufferSet, commandPool, graphicsQueue); cleanup(); // Assume the SSBO is always the 2nd binding this->descriptorInfoList[1].bufferDataList = &storageBufferSet.infoSet; resizedStorageBuffer = true; cout << "SSBO resized, New object capacity: " << this->objectCapacity << endl; // TODO: I'll need to correctly update the descriptor set array instead of appending to it // Then, I'll have to call createDescriptorSets() and finally createCommandBuffers() (from vulkan-game) // This isn't too bad actually, since I have to call createCommandBuffers() every time I add a newobject // anyway. So, in this function, I'll just have to call createDescriptorSets() } updateObject(this->numObjects, ssbo); } this->numObjects++; return resizedStorageBuffer; } template void GraphicsPipeline_Vulkan::updateObject(size_t objIndex, SSBOType& ssbo) { if (!is_same_v) { for (size_t i = 0; i < storageBufferSet.memory.size(); i++) { VulkanUtils::copyDataToMemory(this->device, storageBufferSet.memory[i], objIndex, ssbo); } } } // Should only be used if the number of vertices has not changed template void GraphicsPipeline_Vulkan::updateObjectVertices(size_t objIndex, const vector& vertices, VkCommandPool commandPool, VkQueue graphicsQueue) { VulkanUtils::copyDataToBuffer(this->device, this->physicalDevice, commandPool, vertices, this->vertexBuffer, objIndex * vertices.size(), graphicsQueue); } template void GraphicsPipeline_Vulkan::cleanup() { vkDestroyPipeline(device, pipeline, nullptr); vkDestroyDescriptorPool(device, descriptorPool, nullptr); // TODO: I read that the pipeline layout does not have to be recreated every time // Try only creating it once vkDestroyPipelineLayout(device, pipelineLayout, nullptr); } template void GraphicsPipeline_Vulkan::cleanupBuffers() { vkDestroyDescriptorSetLayout(device, descriptorSetLayout, nullptr); vkDestroyBuffer(device, vertexBuffer, nullptr); vkFreeMemory(device, vertexBufferMemory, nullptr); vkDestroyBuffer(device, indexBuffer, nullptr); vkFreeMemory(device, indexBufferMemory, nullptr); if (!is_same_v) { for (size_t i = 0; i < storageBufferSet.buffers.size(); i++) { vkDestroyBuffer(device, storageBufferSet.buffers[i], nullptr); vkFreeMemory(device, storageBufferSet.memory[i], nullptr); } } } /*** PRIVATE METHODS ***/ template VkShaderModule GraphicsPipeline_Vulkan::createShaderModule(const vector& code) { VkShaderModuleCreateInfo createInfo = {}; createInfo.sType = VK_STRUCTURE_TYPE_SHADER_MODULE_CREATE_INFO; createInfo.codeSize = code.size(); createInfo.pCode = reinterpret_cast(code.data()); VkShaderModule shaderModule; if (vkCreateShaderModule(this->device, &createInfo, nullptr, &shaderModule) != VK_SUCCESS) { throw runtime_error("failed to create shader module!"); } return shaderModule; } template vector GraphicsPipeline_Vulkan::readFile(const string& filename) { ifstream file(filename, ios::ate | ios::binary); if (!file.is_open()) { throw runtime_error("failed to open file!"); } size_t fileSize = (size_t)file.tellg(); vector buffer(fileSize); file.seekg(0); file.read(buffer.data(), fileSize); file.close(); return buffer; } template void GraphicsPipeline_Vulkan::resizeVertexBuffer(VkCommandPool commandPool, VkQueue graphicsQueue) { VkBuffer newVertexBuffer; VkDeviceMemory newVertexBufferMemory; this->vertexCapacity *= 2; VulkanUtils::createBuffer(this->device, this->physicalDevice, this->vertexCapacity * sizeof(VertexType), VK_BUFFER_USAGE_TRANSFER_SRC_BIT | VK_BUFFER_USAGE_TRANSFER_DST_BIT | VK_BUFFER_USAGE_VERTEX_BUFFER_BIT, VK_MEMORY_PROPERTY_DEVICE_LOCAL_BIT, newVertexBuffer, newVertexBufferMemory); VulkanUtils::copyBuffer(this->device, commandPool, vertexBuffer, newVertexBuffer, 0, 0, numVertices * sizeof(VertexType), graphicsQueue); vkDestroyBuffer(this->device, vertexBuffer, nullptr); vkFreeMemory(this->device, vertexBufferMemory, nullptr); vertexBuffer = newVertexBuffer; vertexBufferMemory = newVertexBufferMemory; } template void GraphicsPipeline_Vulkan::resizeIndexBuffer(VkCommandPool commandPool, VkQueue graphicsQueue) { VkBuffer newIndexBuffer; VkDeviceMemory newIndexBufferMemory; this->indexCapacity *= 2; VulkanUtils::createBuffer(this->device, this->physicalDevice, this->indexCapacity * sizeof(uint16_t), VK_BUFFER_USAGE_TRANSFER_SRC_BIT | VK_BUFFER_USAGE_TRANSFER_DST_BIT | VK_BUFFER_USAGE_INDEX_BUFFER_BIT, VK_MEMORY_PROPERTY_DEVICE_LOCAL_BIT, newIndexBuffer, newIndexBufferMemory); VulkanUtils::copyBuffer(this->device, commandPool, indexBuffer, newIndexBuffer, 0, 0, numIndices * sizeof(uint16_t), graphicsQueue); vkDestroyBuffer(this->device, indexBuffer, nullptr); vkFreeMemory(this->device, indexBufferMemory, nullptr); indexBuffer = newIndexBuffer; indexBufferMemory = newIndexBufferMemory; } template void GraphicsPipeline_Vulkan::resizeStorageBufferSet(StorageBufferSet& set, VkCommandPool commandPool, VkQueue graphicsQueue) { this->objectCapacity *= 2; VkDeviceSize bufferSize = objectCapacity * sizeof(SSBOType); for (size_t i = 0; i < set.buffers.size(); i++) { VkBuffer newStorageBuffer; VkDeviceMemory newStorageBufferMemory; VulkanUtils::createBuffer(this->device, this->physicalDevice, bufferSize, VK_BUFFER_USAGE_TRANSFER_SRC_BIT | VK_BUFFER_USAGE_TRANSFER_DST_BIT | VK_BUFFER_USAGE_STORAGE_BUFFER_BIT, VK_MEMORY_PROPERTY_HOST_VISIBLE_BIT | VK_MEMORY_PROPERTY_HOST_COHERENT_BIT, newStorageBuffer, newStorageBufferMemory); VulkanUtils::copyBuffer(this->device, commandPool, set.buffers[i], newStorageBuffer, 0, 0, this->numObjects * sizeof(SSBOType), graphicsQueue); vkDestroyBuffer(this->device, set.buffers[i], nullptr); vkFreeMemory(this->device, set.memory[i], nullptr); set.buffers[i] = newStorageBuffer; set.memory[i] = newStorageBufferMemory; set.infoSet[i].buffer = set.buffers[i]; set.infoSet[i].offset = 0; // This is the offset from the start of the buffer, so always 0 for now set.infoSet[i].range = bufferSize; // Size of the update starting from offset, or VK_WHOLE_SIZE } } #endif // _GRAPHICS_PIPELINE_VULKAN_H