[7d2b0b9] | 1 | #include "graphics-pipeline_vulkan.hpp"
|
---|
| 2 |
|
---|
| 3 | #include <fstream>
|
---|
| 4 | #include <stdexcept>
|
---|
| 5 |
|
---|
[b794178] | 6 | using namespace std;
|
---|
| 7 |
|
---|
[87c8f1a] | 8 | GraphicsPipeline_Vulkan::GraphicsPipeline_Vulkan(VkPhysicalDevice physicalDevice, VkDevice device,
|
---|
| 9 | VkRenderPass renderPass, Viewport viewport, int vertexSize) {
|
---|
| 10 | this->physicalDevice = physicalDevice;
|
---|
[7d2b0b9] | 11 | this->device = device;
|
---|
[b794178] | 12 | this->renderPass = renderPass;
|
---|
[771b33a] | 13 | this->viewport = viewport;
|
---|
| 14 |
|
---|
[87c8f1a] | 15 | // Since there is only one array of vertex data, we use binding = 0
|
---|
| 16 | // I'll probably do that for the foreseeable future
|
---|
| 17 | // I can calculate the stride myself given info about all the varying attributes
|
---|
[771b33a] | 18 | this->bindingDescription.binding = 0;
|
---|
| 19 | this->bindingDescription.stride = vertexSize;
|
---|
| 20 | this->bindingDescription.inputRate = VK_VERTEX_INPUT_RATE_VERTEX;
|
---|
[7d2b0b9] | 21 | }
|
---|
| 22 |
|
---|
| 23 | GraphicsPipeline_Vulkan::~GraphicsPipeline_Vulkan() {
|
---|
| 24 | }
|
---|
| 25 |
|
---|
[0ae182f] | 26 | void GraphicsPipeline_Vulkan::updateRenderPass(VkRenderPass renderPass) {
|
---|
| 27 | this->renderPass = renderPass;
|
---|
| 28 | }
|
---|
| 29 |
|
---|
[87c8f1a] | 30 | void GraphicsPipeline_Vulkan::createVertexBuffer(const void* bufferData, int vertexSize,
|
---|
| 31 | VkCommandPool commandPool, VkQueue graphicsQueue) {
|
---|
| 32 | VkDeviceSize bufferSize = numVertices * vertexSize;
|
---|
| 33 | VkDeviceSize bufferCapacity = vertexCapacity * vertexSize;
|
---|
| 34 |
|
---|
| 35 | VkBuffer stagingBuffer;
|
---|
| 36 | VkDeviceMemory stagingBufferMemory;
|
---|
| 37 | VulkanUtils::createBuffer(device, physicalDevice, bufferSize, VK_BUFFER_USAGE_TRANSFER_SRC_BIT,
|
---|
| 38 | VK_MEMORY_PROPERTY_HOST_VISIBLE_BIT | VK_MEMORY_PROPERTY_HOST_COHERENT_BIT,
|
---|
| 39 | stagingBuffer, stagingBufferMemory);
|
---|
| 40 |
|
---|
| 41 | void* data;
|
---|
| 42 | vkMapMemory(device, stagingBufferMemory, 0, bufferSize, 0, &data);
|
---|
| 43 | memcpy(data, bufferData, (size_t) bufferSize);
|
---|
| 44 | vkUnmapMemory(device, stagingBufferMemory);
|
---|
| 45 |
|
---|
| 46 | VulkanUtils::createBuffer(device, physicalDevice, bufferCapacity,
|
---|
| 47 | VK_BUFFER_USAGE_TRANSFER_DST_BIT | VK_BUFFER_USAGE_VERTEX_BUFFER_BIT,
|
---|
| 48 | VK_MEMORY_PROPERTY_DEVICE_LOCAL_BIT, vertexBuffer, vertexBufferMemory);
|
---|
| 49 |
|
---|
| 50 | VulkanUtils::copyBuffer(device, commandPool, stagingBuffer, vertexBuffer, 0, 0, bufferSize, graphicsQueue);
|
---|
| 51 |
|
---|
| 52 | vkDestroyBuffer(device, stagingBuffer, nullptr);
|
---|
| 53 | vkFreeMemory(device, stagingBufferMemory, nullptr);
|
---|
| 54 | }
|
---|
| 55 |
|
---|
| 56 | void GraphicsPipeline_Vulkan::createIndexBuffer(const void* bufferData, int indexSize,
|
---|
| 57 | VkCommandPool commandPool, VkQueue graphicsQueue) {
|
---|
| 58 | VkDeviceSize bufferSize = numIndices * indexSize;
|
---|
| 59 | VkDeviceSize bufferCapacity = indexCapacity * indexSize;
|
---|
| 60 |
|
---|
| 61 | VkBuffer stagingBuffer;
|
---|
| 62 | VkDeviceMemory stagingBufferMemory;
|
---|
| 63 | VulkanUtils::createBuffer(device, physicalDevice, bufferSize, VK_BUFFER_USAGE_TRANSFER_SRC_BIT,
|
---|
| 64 | VK_MEMORY_PROPERTY_HOST_VISIBLE_BIT | VK_MEMORY_PROPERTY_HOST_COHERENT_BIT,
|
---|
| 65 | stagingBuffer, stagingBufferMemory);
|
---|
| 66 |
|
---|
| 67 | void* data;
|
---|
| 68 | vkMapMemory(device, stagingBufferMemory, 0, bufferSize, 0, &data);
|
---|
| 69 | memcpy(data, bufferData, (size_t) bufferSize);
|
---|
| 70 | vkUnmapMemory(device, stagingBufferMemory);
|
---|
| 71 |
|
---|
| 72 | VulkanUtils::createBuffer(device, physicalDevice, bufferCapacity,
|
---|
| 73 | VK_BUFFER_USAGE_TRANSFER_DST_BIT | VK_BUFFER_USAGE_INDEX_BUFFER_BIT,
|
---|
| 74 | VK_MEMORY_PROPERTY_DEVICE_LOCAL_BIT, indexBuffer, indexBufferMemory);
|
---|
| 75 |
|
---|
| 76 | VulkanUtils::copyBuffer(device, commandPool, stagingBuffer, indexBuffer, 0, 0, bufferSize, graphicsQueue);
|
---|
| 77 |
|
---|
| 78 | vkDestroyBuffer(device, stagingBuffer, nullptr);
|
---|
| 79 | vkFreeMemory(device, stagingBufferMemory, nullptr);
|
---|
| 80 | }
|
---|
| 81 |
|
---|
[771b33a] | 82 | void GraphicsPipeline_Vulkan::addAttribute(VkFormat format, size_t offset) {
|
---|
| 83 | VkVertexInputAttributeDescription attributeDesc = {};
|
---|
| 84 |
|
---|
| 85 | attributeDesc.binding = 0;
|
---|
| 86 | attributeDesc.location = this->attributeDescriptions.size();
|
---|
| 87 | attributeDesc.format = format;
|
---|
| 88 | attributeDesc.offset = offset;
|
---|
| 89 |
|
---|
| 90 | this->attributeDescriptions.push_back(attributeDesc);
|
---|
| 91 | }
|
---|
| 92 |
|
---|
[b794178] | 93 | void GraphicsPipeline_Vulkan::addDescriptorInfo(VkDescriptorType type, VkShaderStageFlags stageFlags, vector<VkDescriptorBufferInfo>* bufferData) {
|
---|
| 94 | this->descriptorInfoList.push_back({ type, stageFlags, bufferData, nullptr });
|
---|
| 95 | }
|
---|
| 96 |
|
---|
| 97 | void GraphicsPipeline_Vulkan::addDescriptorInfo(VkDescriptorType type, VkShaderStageFlags stageFlags, VkDescriptorImageInfo* imageData) {
|
---|
| 98 | this->descriptorInfoList.push_back({ type, stageFlags, nullptr, imageData });
|
---|
| 99 | }
|
---|
| 100 |
|
---|
[7d2b0b9] | 101 | void GraphicsPipeline_Vulkan::createPipeline(string vertShaderFile, string fragShaderFile) {
|
---|
| 102 | vector<char> vertShaderCode = readFile(vertShaderFile);
|
---|
| 103 | vector<char> fragShaderCode = readFile(fragShaderFile);
|
---|
| 104 |
|
---|
| 105 | VkShaderModule vertShaderModule = createShaderModule(vertShaderCode);
|
---|
| 106 | VkShaderModule fragShaderModule = createShaderModule(fragShaderCode);
|
---|
| 107 |
|
---|
| 108 | VkPipelineShaderStageCreateInfo vertShaderStageInfo = {};
|
---|
| 109 | vertShaderStageInfo.sType = VK_STRUCTURE_TYPE_PIPELINE_SHADER_STAGE_CREATE_INFO;
|
---|
| 110 | vertShaderStageInfo.stage = VK_SHADER_STAGE_VERTEX_BIT;
|
---|
| 111 | vertShaderStageInfo.module = vertShaderModule;
|
---|
| 112 | vertShaderStageInfo.pName = "main";
|
---|
| 113 |
|
---|
| 114 | VkPipelineShaderStageCreateInfo fragShaderStageInfo = {};
|
---|
| 115 | fragShaderStageInfo.sType = VK_STRUCTURE_TYPE_PIPELINE_SHADER_STAGE_CREATE_INFO;
|
---|
| 116 | fragShaderStageInfo.stage = VK_SHADER_STAGE_FRAGMENT_BIT;
|
---|
| 117 | fragShaderStageInfo.module = fragShaderModule;
|
---|
| 118 | fragShaderStageInfo.pName = "main";
|
---|
| 119 |
|
---|
| 120 | VkPipelineShaderStageCreateInfo shaderStages[] = { vertShaderStageInfo, fragShaderStageInfo };
|
---|
| 121 |
|
---|
| 122 | VkPipelineVertexInputStateCreateInfo vertexInputInfo = {};
|
---|
| 123 | vertexInputInfo.sType = VK_STRUCTURE_TYPE_PIPELINE_VERTEX_INPUT_STATE_CREATE_INFO;
|
---|
| 124 |
|
---|
[771b33a] | 125 | vertexInputInfo.vertexBindingDescriptionCount = 1;
|
---|
| 126 | vertexInputInfo.vertexAttributeDescriptionCount = static_cast<uint32_t>(this->attributeDescriptions.size());
|
---|
| 127 | vertexInputInfo.pVertexBindingDescriptions = &this->bindingDescription;
|
---|
| 128 | vertexInputInfo.pVertexAttributeDescriptions = this->attributeDescriptions.data();
|
---|
| 129 |
|
---|
| 130 | VkPipelineInputAssemblyStateCreateInfo inputAssembly = {};
|
---|
| 131 | inputAssembly.sType = VK_STRUCTURE_TYPE_PIPELINE_INPUT_ASSEMBLY_STATE_CREATE_INFO;
|
---|
| 132 | inputAssembly.topology = VK_PRIMITIVE_TOPOLOGY_TRIANGLE_LIST;
|
---|
| 133 | inputAssembly.primitiveRestartEnable = VK_FALSE;
|
---|
| 134 |
|
---|
| 135 | VkViewport viewport = {};
|
---|
| 136 | viewport.x = (float)this->viewport.x;
|
---|
| 137 | viewport.y = (float)this->viewport.y;
|
---|
| 138 | viewport.width = (float)this->viewport.width;
|
---|
| 139 | viewport.height = (float)this->viewport.height;
|
---|
| 140 | viewport.minDepth = 0.0f;
|
---|
| 141 | viewport.maxDepth = 1.0f;
|
---|
| 142 |
|
---|
| 143 | VkRect2D scissor = {};
|
---|
| 144 | scissor.offset = { 0, 0 };
|
---|
| 145 | scissor.extent = { (uint32_t)this->viewport.width, (uint32_t)this->viewport.height };
|
---|
| 146 |
|
---|
| 147 | VkPipelineViewportStateCreateInfo viewportState = {};
|
---|
| 148 | viewportState.sType = VK_STRUCTURE_TYPE_PIPELINE_VIEWPORT_STATE_CREATE_INFO;
|
---|
| 149 | viewportState.viewportCount = 1;
|
---|
| 150 | viewportState.pViewports = &viewport;
|
---|
| 151 | viewportState.scissorCount = 1;
|
---|
| 152 | viewportState.pScissors = &scissor;
|
---|
| 153 |
|
---|
| 154 | VkPipelineRasterizationStateCreateInfo rasterizer = {};
|
---|
| 155 | rasterizer.sType = VK_STRUCTURE_TYPE_PIPELINE_RASTERIZATION_STATE_CREATE_INFO;
|
---|
| 156 | rasterizer.depthClampEnable = VK_FALSE;
|
---|
| 157 | rasterizer.rasterizerDiscardEnable = VK_FALSE;
|
---|
| 158 | rasterizer.polygonMode = VK_POLYGON_MODE_FILL;
|
---|
| 159 | rasterizer.lineWidth = 1.0f;
|
---|
| 160 | rasterizer.cullMode = VK_CULL_MODE_BACK_BIT;
|
---|
| 161 | rasterizer.frontFace = VK_FRONT_FACE_COUNTER_CLOCKWISE;
|
---|
| 162 | rasterizer.depthBiasEnable = VK_FALSE;
|
---|
| 163 |
|
---|
| 164 | VkPipelineMultisampleStateCreateInfo multisampling = {};
|
---|
| 165 | multisampling.sType = VK_STRUCTURE_TYPE_PIPELINE_MULTISAMPLE_STATE_CREATE_INFO;
|
---|
| 166 | multisampling.sampleShadingEnable = VK_FALSE;
|
---|
| 167 | multisampling.rasterizationSamples = VK_SAMPLE_COUNT_1_BIT;
|
---|
| 168 |
|
---|
| 169 | VkPipelineColorBlendAttachmentState colorBlendAttachment = {};
|
---|
| 170 | colorBlendAttachment.colorWriteMask = VK_COLOR_COMPONENT_R_BIT | VK_COLOR_COMPONENT_G_BIT | VK_COLOR_COMPONENT_B_BIT | VK_COLOR_COMPONENT_A_BIT;
|
---|
| 171 | colorBlendAttachment.blendEnable = VK_TRUE;
|
---|
| 172 | colorBlendAttachment.colorBlendOp = VK_BLEND_OP_ADD;
|
---|
| 173 | colorBlendAttachment.srcColorBlendFactor = VK_BLEND_FACTOR_SRC_ALPHA;
|
---|
| 174 | colorBlendAttachment.dstColorBlendFactor = VK_BLEND_FACTOR_ONE_MINUS_SRC_ALPHA;
|
---|
| 175 | colorBlendAttachment.alphaBlendOp = VK_BLEND_OP_ADD;
|
---|
| 176 | colorBlendAttachment.srcAlphaBlendFactor = VK_BLEND_FACTOR_SRC_ALPHA;
|
---|
| 177 | colorBlendAttachment.dstAlphaBlendFactor = VK_BLEND_FACTOR_ONE_MINUS_SRC_ALPHA;
|
---|
| 178 |
|
---|
| 179 | VkPipelineColorBlendStateCreateInfo colorBlending = {};
|
---|
| 180 | colorBlending.sType = VK_STRUCTURE_TYPE_PIPELINE_COLOR_BLEND_STATE_CREATE_INFO;
|
---|
| 181 | colorBlending.logicOpEnable = VK_FALSE;
|
---|
| 182 | colorBlending.logicOp = VK_LOGIC_OP_COPY;
|
---|
| 183 | colorBlending.attachmentCount = 1;
|
---|
| 184 | colorBlending.pAttachments = &colorBlendAttachment;
|
---|
| 185 | colorBlending.blendConstants[0] = 0.0f;
|
---|
| 186 | colorBlending.blendConstants[1] = 0.0f;
|
---|
| 187 | colorBlending.blendConstants[2] = 0.0f;
|
---|
| 188 | colorBlending.blendConstants[3] = 0.0f;
|
---|
| 189 |
|
---|
| 190 | VkPipelineDepthStencilStateCreateInfo depthStencil = {};
|
---|
| 191 | depthStencil.sType = VK_STRUCTURE_TYPE_PIPELINE_DEPTH_STENCIL_STATE_CREATE_INFO;
|
---|
| 192 | depthStencil.depthTestEnable = VK_TRUE;
|
---|
| 193 | depthStencil.depthWriteEnable = VK_TRUE;
|
---|
| 194 | depthStencil.depthCompareOp = VK_COMPARE_OP_LESS;
|
---|
| 195 | depthStencil.depthBoundsTestEnable = VK_FALSE;
|
---|
| 196 | depthStencil.minDepthBounds = 0.0f;
|
---|
| 197 | depthStencil.maxDepthBounds = 1.0f;
|
---|
| 198 | depthStencil.stencilTestEnable = VK_FALSE;
|
---|
| 199 | depthStencil.front = {};
|
---|
| 200 | depthStencil.back = {};
|
---|
| 201 |
|
---|
| 202 | VkPipelineLayoutCreateInfo pipelineLayoutInfo = {};
|
---|
| 203 | pipelineLayoutInfo.sType = VK_STRUCTURE_TYPE_PIPELINE_LAYOUT_CREATE_INFO;
|
---|
| 204 | pipelineLayoutInfo.setLayoutCount = 1;
|
---|
[b794178] | 205 | pipelineLayoutInfo.pSetLayouts = &this->descriptorSetLayout;
|
---|
| 206 | pipelineLayoutInfo.pushConstantRangeCount = 0;
|
---|
| 207 |
|
---|
| 208 | if (vkCreatePipelineLayout(this->device, &pipelineLayoutInfo, nullptr, &this->pipelineLayout) != VK_SUCCESS) {
|
---|
| 209 | throw runtime_error("failed to create pipeline layout!");
|
---|
| 210 | }
|
---|
| 211 |
|
---|
| 212 | VkGraphicsPipelineCreateInfo pipelineInfo = {};
|
---|
| 213 | pipelineInfo.sType = VK_STRUCTURE_TYPE_GRAPHICS_PIPELINE_CREATE_INFO;
|
---|
| 214 | pipelineInfo.stageCount = 2;
|
---|
| 215 | pipelineInfo.pStages = shaderStages;
|
---|
| 216 | pipelineInfo.pVertexInputState = &vertexInputInfo;
|
---|
| 217 | pipelineInfo.pInputAssemblyState = &inputAssembly;
|
---|
| 218 | pipelineInfo.pViewportState = &viewportState;
|
---|
| 219 | pipelineInfo.pRasterizationState = &rasterizer;
|
---|
| 220 | pipelineInfo.pMultisampleState = &multisampling;
|
---|
| 221 | pipelineInfo.pDepthStencilState = &depthStencil;
|
---|
| 222 | pipelineInfo.pColorBlendState = &colorBlending;
|
---|
| 223 | pipelineInfo.pDynamicState = nullptr;
|
---|
| 224 | pipelineInfo.layout = this->pipelineLayout;
|
---|
| 225 | pipelineInfo.renderPass = this->renderPass;
|
---|
| 226 | pipelineInfo.subpass = 0;
|
---|
| 227 | pipelineInfo.basePipelineHandle = VK_NULL_HANDLE;
|
---|
| 228 | pipelineInfo.basePipelineIndex = -1;
|
---|
| 229 |
|
---|
| 230 | if (vkCreateGraphicsPipelines(this->device, VK_NULL_HANDLE, 1, &pipelineInfo, nullptr, &this->pipeline) != VK_SUCCESS) {
|
---|
| 231 | throw runtime_error("failed to create graphics pipeline!");
|
---|
| 232 | }
|
---|
[771b33a] | 233 |
|
---|
[b794178] | 234 | vkDestroyShaderModule(this->device, vertShaderModule, nullptr);
|
---|
| 235 | vkDestroyShaderModule(this->device, fragShaderModule, nullptr);
|
---|
| 236 | }
|
---|
| 237 |
|
---|
| 238 | void GraphicsPipeline_Vulkan::createDescriptorSetLayout() {
|
---|
| 239 | vector<VkDescriptorSetLayoutBinding> bindings(this->descriptorInfoList.size());
|
---|
| 240 |
|
---|
| 241 | for (size_t i = 0; i < bindings.size(); i++) {
|
---|
| 242 | bindings[i].binding = i;
|
---|
| 243 | bindings[i].descriptorCount = 1;
|
---|
| 244 | bindings[i].descriptorType = this->descriptorInfoList[i].type;
|
---|
| 245 | bindings[i].stageFlags = this->descriptorInfoList[i].stageFlags;
|
---|
| 246 | bindings[i].pImmutableSamplers = nullptr;
|
---|
| 247 | }
|
---|
| 248 |
|
---|
| 249 | VkDescriptorSetLayoutCreateInfo layoutInfo = {};
|
---|
| 250 | layoutInfo.sType = VK_STRUCTURE_TYPE_DESCRIPTOR_SET_LAYOUT_CREATE_INFO;
|
---|
| 251 | layoutInfo.bindingCount = static_cast<uint32_t>(bindings.size());
|
---|
| 252 | layoutInfo.pBindings = bindings.data();
|
---|
| 253 |
|
---|
| 254 | if (vkCreateDescriptorSetLayout(this->device, &layoutInfo, nullptr, &this->descriptorSetLayout) != VK_SUCCESS) {
|
---|
| 255 | throw runtime_error("failed to create descriptor set layout!");
|
---|
| 256 | }
|
---|
| 257 | }
|
---|
| 258 |
|
---|
| 259 | void GraphicsPipeline_Vulkan::createDescriptorPool(vector<VkImage>& swapChainImages) {
|
---|
| 260 | vector<VkDescriptorPoolSize> poolSizes(this->descriptorInfoList.size());
|
---|
| 261 |
|
---|
| 262 | for (size_t i = 0; i < poolSizes.size(); i++) {
|
---|
| 263 | poolSizes[i].type = this->descriptorInfoList[i].type;
|
---|
| 264 | poolSizes[i].descriptorCount = static_cast<uint32_t>(swapChainImages.size());
|
---|
| 265 | }
|
---|
| 266 |
|
---|
| 267 | VkDescriptorPoolCreateInfo poolInfo = {};
|
---|
| 268 | poolInfo.sType = VK_STRUCTURE_TYPE_DESCRIPTOR_POOL_CREATE_INFO;
|
---|
| 269 | poolInfo.poolSizeCount = static_cast<uint32_t>(poolSizes.size());
|
---|
| 270 | poolInfo.pPoolSizes = poolSizes.data();
|
---|
| 271 | poolInfo.maxSets = static_cast<uint32_t>(swapChainImages.size());
|
---|
| 272 |
|
---|
| 273 | if (vkCreateDescriptorPool(this->device, &poolInfo, nullptr, &this->descriptorPool) != VK_SUCCESS) {
|
---|
| 274 | throw runtime_error("failed to create descriptor pool!");
|
---|
| 275 | }
|
---|
| 276 | }
|
---|
| 277 |
|
---|
| 278 | void GraphicsPipeline_Vulkan::createDescriptorSets(vector<VkImage>& swapChainImages) {
|
---|
| 279 | vector<VkDescriptorSetLayout> layouts(swapChainImages.size(), this->descriptorSetLayout);
|
---|
| 280 |
|
---|
| 281 | VkDescriptorSetAllocateInfo allocInfo = {};
|
---|
| 282 | allocInfo.sType = VK_STRUCTURE_TYPE_DESCRIPTOR_SET_ALLOCATE_INFO;
|
---|
| 283 | allocInfo.descriptorPool = this->descriptorPool;
|
---|
| 284 | allocInfo.descriptorSetCount = static_cast<uint32_t>(swapChainImages.size());
|
---|
| 285 | allocInfo.pSetLayouts = layouts.data();
|
---|
| 286 |
|
---|
| 287 | this->descriptorSets.resize(swapChainImages.size());
|
---|
| 288 | if (vkAllocateDescriptorSets(device, &allocInfo, this->descriptorSets.data()) != VK_SUCCESS) {
|
---|
| 289 | throw runtime_error("failed to allocate descriptor sets!");
|
---|
| 290 | }
|
---|
| 291 |
|
---|
| 292 | for (size_t i = 0; i < swapChainImages.size(); i++) {
|
---|
| 293 | vector<VkWriteDescriptorSet> descriptorWrites(this->descriptorInfoList.size());
|
---|
| 294 |
|
---|
| 295 | for (size_t j = 0; j < descriptorWrites.size(); j++) {
|
---|
| 296 | descriptorWrites[j].sType = VK_STRUCTURE_TYPE_WRITE_DESCRIPTOR_SET;
|
---|
| 297 | descriptorWrites[j].dstSet = this->descriptorSets[i];
|
---|
| 298 | descriptorWrites[j].dstBinding = j;
|
---|
| 299 | descriptorWrites[j].dstArrayElement = 0;
|
---|
| 300 | descriptorWrites[j].descriptorType = this->descriptorInfoList[j].type;
|
---|
| 301 | descriptorWrites[j].descriptorCount = 1;
|
---|
| 302 | descriptorWrites[j].pBufferInfo = nullptr;
|
---|
| 303 | descriptorWrites[j].pImageInfo = nullptr;
|
---|
| 304 | descriptorWrites[j].pTexelBufferView = nullptr;
|
---|
| 305 |
|
---|
| 306 | switch (descriptorWrites[j].descriptorType) {
|
---|
| 307 | case VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER:
|
---|
| 308 | descriptorWrites[j].pBufferInfo = &(*this->descriptorInfoList[j].bufferDataList)[i];
|
---|
| 309 | break;
|
---|
| 310 | case VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER:
|
---|
| 311 | descriptorWrites[j].pImageInfo = this->descriptorInfoList[j].imageData;
|
---|
| 312 | break;
|
---|
| 313 | default:
|
---|
[cd487fb] | 314 | throw runtime_error("Unknown descriptor type: " + to_string(descriptorWrites[j].descriptorType));
|
---|
[b794178] | 315 | }
|
---|
| 316 | }
|
---|
| 317 |
|
---|
| 318 | vkUpdateDescriptorSets(this->device, static_cast<uint32_t>(descriptorWrites.size()), descriptorWrites.data(), 0, nullptr);
|
---|
| 319 | }
|
---|
[7d2b0b9] | 320 | }
|
---|
| 321 |
|
---|
[603b5bc] | 322 | void GraphicsPipeline_Vulkan::createRenderCommands(VkCommandBuffer& commandBuffer, uint32_t currentImage) {
|
---|
| 323 | vkCmdBindPipeline(commandBuffer, VK_PIPELINE_BIND_POINT_GRAPHICS, pipeline);
|
---|
| 324 | vkCmdBindDescriptorSets(commandBuffer, VK_PIPELINE_BIND_POINT_GRAPHICS, pipelineLayout, 0, 1,
|
---|
| 325 | &descriptorSets[currentImage], 0, nullptr);
|
---|
| 326 |
|
---|
[d2d9286] | 327 | VkBuffer vertexBuffers[] = { vertexBuffer };
|
---|
[603b5bc] | 328 | VkDeviceSize offsets[] = { 0 };
|
---|
[d2d9286] | 329 | vkCmdBindVertexBuffers(commandBuffer, 0, 1, vertexBuffers, offsets);
|
---|
[603b5bc] | 330 |
|
---|
[d2d9286] | 331 | vkCmdBindIndexBuffer(commandBuffer, indexBuffer, 0, VK_INDEX_TYPE_UINT16);
|
---|
[603b5bc] | 332 |
|
---|
[d2d9286] | 333 | vkCmdDrawIndexed(commandBuffer, static_cast<uint32_t>(numIndices), 1, 0, 0, 0);
|
---|
[603b5bc] | 334 | }
|
---|
| 335 |
|
---|
[7d2b0b9] | 336 | VkShaderModule GraphicsPipeline_Vulkan::createShaderModule(const vector<char>& code) {
|
---|
| 337 | VkShaderModuleCreateInfo createInfo = {};
|
---|
| 338 | createInfo.sType = VK_STRUCTURE_TYPE_SHADER_MODULE_CREATE_INFO;
|
---|
| 339 | createInfo.codeSize = code.size();
|
---|
| 340 | createInfo.pCode = reinterpret_cast<const uint32_t*>(code.data());
|
---|
| 341 |
|
---|
| 342 | VkShaderModule shaderModule;
|
---|
[b794178] | 343 | if (vkCreateShaderModule(this->device, &createInfo, nullptr, &shaderModule) != VK_SUCCESS) {
|
---|
[7d2b0b9] | 344 | throw runtime_error("failed to create shader module!");
|
---|
| 345 | }
|
---|
| 346 |
|
---|
| 347 | return shaderModule;
|
---|
| 348 | }
|
---|
| 349 |
|
---|
| 350 | vector<char> GraphicsPipeline_Vulkan::readFile(const string& filename) {
|
---|
| 351 | ifstream file(filename, ios::ate | ios::binary);
|
---|
| 352 |
|
---|
| 353 | if (!file.is_open()) {
|
---|
| 354 | throw runtime_error("failed to open file!");
|
---|
| 355 | }
|
---|
| 356 |
|
---|
| 357 | size_t fileSize = (size_t)file.tellg();
|
---|
| 358 | vector<char> buffer(fileSize);
|
---|
| 359 |
|
---|
| 360 | file.seekg(0);
|
---|
| 361 | file.read(buffer.data(), fileSize);
|
---|
| 362 |
|
---|
| 363 | file.close();
|
---|
| 364 |
|
---|
| 365 | return buffer;
|
---|
[b794178] | 366 | }
|
---|
| 367 |
|
---|
| 368 | void GraphicsPipeline_Vulkan::cleanup() {
|
---|
[87c8f1a] | 369 | vkDestroyPipeline(device, pipeline, nullptr);
|
---|
| 370 | vkDestroyDescriptorPool(device, descriptorPool, nullptr);
|
---|
| 371 | vkDestroyPipelineLayout(device, pipelineLayout, nullptr);
|
---|
[b794178] | 372 | }
|
---|
| 373 |
|
---|
| 374 | void GraphicsPipeline_Vulkan::cleanupBuffers() {
|
---|
[87c8f1a] | 375 | vkDestroyDescriptorSetLayout(device, descriptorSetLayout, nullptr);
|
---|
| 376 |
|
---|
| 377 | vkDestroyBuffer(device, vertexBuffer, nullptr);
|
---|
| 378 | vkFreeMemory(device, vertexBufferMemory, nullptr);
|
---|
| 379 | vkDestroyBuffer(device, indexBuffer, nullptr);
|
---|
| 380 | vkFreeMemory(device, indexBufferMemory, nullptr);
|
---|
[7d2b0b9] | 381 | }
|
---|