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