Changeset d22ae72 in opengl-game for vulkan-game.cpp
- Timestamp:
- Aug 20, 2019, 7:12:21 PM (5 years ago)
- Branches:
- feature/imgui-sdl, master, points-test
- Children:
- 28c13da
- Parents:
- b8b32bd
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
vulkan-game.cpp
rb8b32bd rd22ae72 112 112 113 113 struct GraphicsPipelineInfo { 114 VkPipelineLayout pipelineLayout; 114 115 VkPipeline pipeline; 116 117 VkDescriptorPool descriptorPool; 118 VkDescriptorSetLayout descriptorSetLayout; 119 vector<VkDescriptorSet> descriptorSets; 115 120 116 121 VkBuffer vertexBuffer; … … 187 192 188 193 VkRenderPass renderPass; 189 VkDescriptorSetLayout descriptorSetLayout;190 VkPipelineLayout pipelineLayout;191 VkDescriptorPool descriptorPool;192 vector<VkDescriptorSet> descriptorSets;193 194 194 VkCommandPool commandPool; 195 195 … … 313 313 createImageViews(); 314 314 createRenderPass(); 315 createDescriptorSetLayout(); 316 createGraphicsPipeline("shaders/vert.spv", "shaders/frag.spv", &scenePipeline.pipeline); 317 createGraphicsPipeline("shaders/vert.spv", "shaders/frag.spv", &overlayPipeline.pipeline); 315 316 createDescriptorSetLayout(scenePipeline); 317 createGraphicsPipeline("shaders/vert.spv", "shaders/frag.spv", scenePipeline); 318 319 createDescriptorSetLayout(overlayPipeline); 320 createGraphicsPipeline("shaders/vert.spv", "shaders/frag.spv", overlayPipeline); 321 318 322 createCommandPool(); 319 323 createDepthResources(); … … 358 362 359 363 createUniformBuffers(); 360 createDescriptorPool(); 361 createDescriptorSets(); 364 365 createDescriptorPool(scenePipeline); 366 createDescriptorSets(scenePipeline); 367 368 createDescriptorPool(overlayPipeline); 369 createDescriptorSets(overlayPipeline); 370 362 371 createCommandBuffers(); 363 372 createSyncObjects(); … … 766 775 } 767 776 768 void createDescriptorSetLayout( ) {777 void createDescriptorSetLayout(GraphicsPipelineInfo& info) { 769 778 VkDescriptorSetLayoutBinding uboLayoutBinding = {}; 770 779 uboLayoutBinding.binding = 0; … … 794 803 layoutInfo.pBindings = bindings.data(); 795 804 796 if (vkCreateDescriptorSetLayout(device, &layoutInfo, nullptr, & descriptorSetLayout) != VK_SUCCESS) {805 if (vkCreateDescriptorSetLayout(device, &layoutInfo, nullptr, &info.descriptorSetLayout) != VK_SUCCESS) { 797 806 throw runtime_error("failed to create descriptor set layout!"); 798 807 } 799 808 } 800 809 801 void createGraphicsPipeline(string vertShaderFile, string fragShaderFile, VkPipeline* pipeline) {810 void createGraphicsPipeline(string vertShaderFile, string fragShaderFile, GraphicsPipelineInfo& info) { 802 811 auto vertShaderCode = readFile(vertShaderFile); 803 812 auto fragShaderCode = readFile(fragShaderFile); … … 906 915 pipelineLayoutInfo.sType = VK_STRUCTURE_TYPE_PIPELINE_LAYOUT_CREATE_INFO; 907 916 pipelineLayoutInfo.setLayoutCount = 1; 908 pipelineLayoutInfo.pSetLayouts = & descriptorSetLayout;917 pipelineLayoutInfo.pSetLayouts = &info.descriptorSetLayout; 909 918 pipelineLayoutInfo.pushConstantRangeCount = 0; 910 919 911 if (vkCreatePipelineLayout(device, &pipelineLayoutInfo, nullptr, & pipelineLayout) != VK_SUCCESS) {920 if (vkCreatePipelineLayout(device, &pipelineLayoutInfo, nullptr, &info.pipelineLayout) != VK_SUCCESS) { 912 921 throw runtime_error("failed to create pipeline layout!"); 913 922 } … … 925 934 pipelineInfo.pColorBlendState = &colorBlending; 926 935 pipelineInfo.pDynamicState = nullptr; 927 pipelineInfo.layout = pipelineLayout;936 pipelineInfo.layout = info.pipelineLayout; 928 937 pipelineInfo.renderPass = renderPass; 929 938 pipelineInfo.subpass = 0; … … 931 940 pipelineInfo.basePipelineIndex = -1; 932 941 933 if (vkCreateGraphicsPipelines(device, VK_NULL_HANDLE, 1, &pipelineInfo, nullptr, pipeline) != VK_SUCCESS) {942 if (vkCreateGraphicsPipelines(device, VK_NULL_HANDLE, 1, &pipelineInfo, nullptr, &info.pipeline) != VK_SUCCESS) { 934 943 throw runtime_error("failed to create graphics pipeline!"); 935 944 } … … 1487 1496 } 1488 1497 1489 void createDescriptorPool( ) {1498 void createDescriptorPool(GraphicsPipelineInfo& info) { 1490 1499 array<VkDescriptorPoolSize, 3> poolSizes = {}; 1491 1500 poolSizes[0].type = VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER; … … 1502 1511 poolInfo.maxSets = static_cast<uint32_t>(swapChainImages.size()); 1503 1512 1504 if (vkCreateDescriptorPool(device, &poolInfo, nullptr, & descriptorPool) != VK_SUCCESS) {1513 if (vkCreateDescriptorPool(device, &poolInfo, nullptr, &info.descriptorPool) != VK_SUCCESS) { 1505 1514 throw runtime_error("failed to create descriptor pool!"); 1506 1515 } 1507 1516 } 1508 1517 1509 void createDescriptorSets( ) {1510 vector<VkDescriptorSetLayout> layouts(swapChainImages.size(), descriptorSetLayout);1518 void createDescriptorSets(GraphicsPipelineInfo& info) { 1519 vector<VkDescriptorSetLayout> layouts(swapChainImages.size(), info.descriptorSetLayout); 1511 1520 1512 1521 VkDescriptorSetAllocateInfo allocInfo = {}; 1513 1522 allocInfo.sType = VK_STRUCTURE_TYPE_DESCRIPTOR_SET_ALLOCATE_INFO; 1514 allocInfo.descriptorPool = descriptorPool;1523 allocInfo.descriptorPool = info.descriptorPool; 1515 1524 allocInfo.descriptorSetCount = static_cast<uint32_t>(swapChainImages.size()); 1516 1525 allocInfo.pSetLayouts = layouts.data(); 1517 1526 1518 descriptorSets.resize(swapChainImages.size());1519 if (vkAllocateDescriptorSets(device, &allocInfo, descriptorSets.data()) != VK_SUCCESS) {1527 info.descriptorSets.resize(swapChainImages.size()); 1528 if (vkAllocateDescriptorSets(device, &allocInfo, info.descriptorSets.data()) != VK_SUCCESS) { 1520 1529 throw runtime_error("failed to allocate descriptor sets!"); 1521 1530 } … … 1540 1549 1541 1550 descriptorWrites[0].sType = VK_STRUCTURE_TYPE_WRITE_DESCRIPTOR_SET; 1542 descriptorWrites[0].dstSet = descriptorSets[i];1551 descriptorWrites[0].dstSet = info.descriptorSets[i]; 1543 1552 descriptorWrites[0].dstBinding = 0; 1544 1553 descriptorWrites[0].dstArrayElement = 0; … … 1550 1559 1551 1560 descriptorWrites[1].sType = VK_STRUCTURE_TYPE_WRITE_DESCRIPTOR_SET; 1552 descriptorWrites[1].dstSet = descriptorSets[i];1561 descriptorWrites[1].dstSet = info.descriptorSets[i]; 1553 1562 descriptorWrites[1].dstBinding = 1; 1554 1563 descriptorWrites[1].dstArrayElement = 0; … … 1560 1569 1561 1570 descriptorWrites[2].sType = VK_STRUCTURE_TYPE_WRITE_DESCRIPTOR_SET; 1562 descriptorWrites[2].dstSet = descriptorSets[i];1571 descriptorWrites[2].dstSet = info.descriptorSets[i]; 1563 1572 descriptorWrites[2].dstBinding = 2; 1564 1573 descriptorWrites[2].dstArrayElement = 0; … … 1612 1621 vkCmdBeginRenderPass(commandBuffers[i], &renderPassInfo, VK_SUBPASS_CONTENTS_INLINE); 1613 1622 1614 vkCmdBindPipeline(commandBuffers[i], VK_PIPELINE_BIND_POINT_GRAPHICS, scenePipeline.pipeline); 1615 1616 vkCmdBindDescriptorSets(commandBuffers[i], VK_PIPELINE_BIND_POINT_GRAPHICS, pipelineLayout, 0, 1, &descriptorSets[i], 0, nullptr); 1617 1618 VkBuffer vertexBuffers[] = { scenePipeline.vertexBuffer }; 1619 VkDeviceSize offsets[] = { 0 }; 1620 vkCmdBindVertexBuffers(commandBuffers[i], 0, 1, vertexBuffers, offsets); 1621 1622 vkCmdBindIndexBuffer(commandBuffers[i], scenePipeline.indexBuffer, 0, VK_INDEX_TYPE_UINT16); 1623 1624 vkCmdDrawIndexed(commandBuffers[i], static_cast<uint32_t>(scenePipeline.numIndices), 1, 0, 0, 0); 1625 1626 vkCmdBindPipeline(commandBuffers[i], VK_PIPELINE_BIND_POINT_GRAPHICS, overlayPipeline.pipeline); 1627 1628 VkBuffer vertexBuffersOverlay[] = { overlayPipeline.vertexBuffer }; 1629 vkCmdBindVertexBuffers(commandBuffers[i], 0, 1, vertexBuffersOverlay, offsets); 1630 1631 vkCmdBindIndexBuffer(commandBuffers[i], overlayPipeline.indexBuffer, 0, VK_INDEX_TYPE_UINT16); 1632 1633 vkCmdDrawIndexed(commandBuffers[i], static_cast<uint32_t>(overlayPipeline.numIndices), 1, 0, 0, 0); 1623 createGraphicsPipelineCommands(scenePipeline, i); 1624 createGraphicsPipelineCommands(overlayPipeline, i); 1634 1625 1635 1626 vkCmdEndRenderPass(commandBuffers[i]); … … 1639 1630 } 1640 1631 } 1632 } 1633 1634 void createGraphicsPipelineCommands(GraphicsPipelineInfo& info, size_t imageIdx) { 1635 vkCmdBindPipeline(commandBuffers[imageIdx], VK_PIPELINE_BIND_POINT_GRAPHICS, info.pipeline); 1636 vkCmdBindDescriptorSets(commandBuffers[imageIdx], VK_PIPELINE_BIND_POINT_GRAPHICS, info.pipelineLayout, 0, 1, 1637 &info.descriptorSets[imageIdx], 0, nullptr); 1638 1639 VkBuffer vertexBuffers[] = { info.vertexBuffer }; 1640 VkDeviceSize offsets[] = { 0 }; 1641 vkCmdBindVertexBuffers(commandBuffers[imageIdx], 0, 1, vertexBuffers, offsets); 1642 1643 vkCmdBindIndexBuffer(commandBuffers[imageIdx], info.indexBuffer, 0, VK_INDEX_TYPE_UINT16); 1644 1645 vkCmdDrawIndexed(commandBuffers[imageIdx], static_cast<uint32_t>(info.numIndices), 1, 0, 0, 0); 1641 1646 } 1642 1647 … … 1824 1829 createImageViews(); 1825 1830 createRenderPass(); 1826 createGraphicsPipeline("shaders/vert.spv", "shaders/frag.spv", &scenePipeline.pipeline); 1827 createGraphicsPipeline("shaders/vert.spv", "shaders/frag.spv", &overlayPipeline.pipeline); 1831 1832 createGraphicsPipeline("shaders/vert.spv", "shaders/frag.spv", scenePipeline); 1833 createGraphicsPipeline("shaders/vert.spv", "shaders/frag.spv", overlayPipeline); 1834 1828 1835 createDepthResources(); 1829 1836 createFramebuffers(); 1830 1837 createUniformBuffers(); 1831 createDescriptorPool(); 1832 createDescriptorSets(); 1838 1839 createDescriptorPool(scenePipeline); 1840 createDescriptorSets(scenePipeline); 1841 1842 createDescriptorPool(overlayPipeline); 1843 createDescriptorSets(overlayPipeline); 1844 1833 1845 createCommandBuffers(); 1834 1846 } … … 1851 1863 vkFreeMemory(device, sdlOverlayImageMemory, nullptr); 1852 1864 1853 vkDestroyDescriptorSetLayout(device, descriptorSetLayout, nullptr); 1854 1855 vkDestroyBuffer(device, scenePipeline.vertexBuffer, nullptr); 1856 vkFreeMemory(device, scenePipeline.vertexBufferMemory, nullptr); 1857 vkDestroyBuffer(device, scenePipeline.indexBuffer, nullptr); 1858 vkFreeMemory(device, scenePipeline.indexBufferMemory, nullptr); 1859 1860 vkDestroyBuffer(device, overlayPipeline.vertexBuffer, nullptr); 1861 vkFreeMemory(device, overlayPipeline.vertexBufferMemory, nullptr); 1862 vkDestroyBuffer(device, overlayPipeline.indexBuffer, nullptr); 1863 vkFreeMemory(device, overlayPipeline.indexBufferMemory, nullptr); 1865 cleanupPipelineBuffers(scenePipeline); 1866 cleanupPipelineBuffers(overlayPipeline); 1864 1867 1865 1868 for (size_t i = 0; i < MAX_FRAMES_IN_FLIGHT; i++) { … … 1920 1923 vkFreeCommandBuffers(device, commandPool, static_cast<uint32_t>(commandBuffers.size()), commandBuffers.data()); 1921 1924 1922 vkDestroyPipeline(device, scenePipeline.pipeline, nullptr); 1923 vkDestroyPipeline(device, overlayPipeline.pipeline, nullptr); 1924 1925 vkDestroyPipelineLayout(device, pipelineLayout, nullptr); 1925 cleanupPipeline(scenePipeline); 1926 cleanupPipeline(overlayPipeline); 1927 1926 1928 vkDestroyRenderPass(device, renderPass, nullptr); 1927 1929 … … 1936 1938 vkFreeMemory(device, uniformBuffersMemory[i], nullptr); 1937 1939 } 1938 1939 vkDestroyDescriptorPool(device, descriptorPool, nullptr); 1940 } 1941 1942 void cleanupPipeline(GraphicsPipelineInfo& pipeline) { 1943 vkDestroyPipeline(device, pipeline.pipeline, nullptr); 1944 vkDestroyDescriptorPool(device, pipeline.descriptorPool, nullptr); 1945 vkDestroyPipelineLayout(device, pipeline.pipelineLayout, nullptr); 1946 } 1947 1948 void cleanupPipelineBuffers(GraphicsPipelineInfo& pipeline) { 1949 vkDestroyDescriptorSetLayout(device, pipeline.descriptorSetLayout, nullptr); 1950 1951 vkDestroyBuffer(device, pipeline.vertexBuffer, nullptr); 1952 vkFreeMemory(device, pipeline.vertexBufferMemory, nullptr); 1953 vkDestroyBuffer(device, pipeline.indexBuffer, nullptr); 1954 vkFreeMemory(device, pipeline.indexBufferMemory, nullptr); 1940 1955 } 1941 1956
Note:
See TracChangeset
for help on using the changeset viewer.