Changeset e5d4aca in opengl-game
- Timestamp:
- Aug 22, 2019, 2:19:56 AM (5 years ago)
- Branches:
- feature/imgui-sdl, master, points-test
- Children:
- 6544020
- Parents:
- c8c6da8
- Files:
-
- 1 added
- 4 edited
Legend:
- Unmodified
- Added
- Removed
-
TODO.txt
rc8c6da8 re5d4aca 18 18 -Maybe separate out the camera controls 19 19 -Turn the game into more of an overhead view RTS where you can use the mouse to select and move ships 20 21 Apparently a new pipeline is required for every type of shader 22 However, it seems like I should only need one swap chain 23 Seems like I'll have to create a new set of framebuffers and a new set of command buffers as well 24 25 I will definitely have to create new command buffers, since that's where I specify the vertex buffers and 26 index buffer for the draw operation 27 28 We'll need to have a separate command buffer for every framebuffer in the swapchain 29 30 Actually, I could probably just have multiple vkCmdDraw calls in each command buffer and rebind to a different 31 pipeline between each one -
shaders/overlay.frag
rc8c6da8 re5d4aca 2 2 #extension GL_ARB_separate_shader_objects : enable 3 3 4 layout(binding = 1) uniform sampler2D texSampler; 5 layout(binding = 2) uniform sampler2D uiTexSampler; 4 layout(binding = 0) uniform sampler2D uiTexSampler; 6 5 7 6 layout(location = 0) in vec3 fragColor; … … 12 11 13 12 void main() { 13 outColor = texture(uiTexSampler, fragTexCoord); 14 14 15 if (isOverlay == 1) { 15 16 outColor = texture(uiTexSampler, fragTexCoord); 16 17 } else { 17 outColor = vec4(fragColor * texture( texSampler, fragTexCoord).rgb, 1.0);18 outColor = vec4(fragColor * texture(uiTexSampler, fragTexCoord).rgb, 1.0); 18 19 } 19 20 } -
shaders/overlay.vert
rc8c6da8 re5d4aca 1 1 #version 450 2 2 #extension GL_ARB_separate_shader_objects : enable 3 4 layout (binding = 0) uniform UniformBufferObject {5 mat4 model;6 mat4 view;7 mat4 proj;8 } ubo;9 3 10 4 layout(location = 0) in vec3 inPosition; … … 17 11 18 12 void main() { 19 if (gl_VertexIndex < 8) { 20 gl_Position = ubo.proj * ubo.view * ubo.model * vec4(inPosition, 1.0); 13 if (gl_VertexIndex < 0) { 21 14 fragColor = inColor; 22 15 isOverlay = 0; 23 16 } else { 24 gl_Position = vec4(inPosition, 1.0); 25 fragColor = vec3(1.0, 1.0, 1.0); 17 fragColor = inColor; 26 18 isOverlay = 1; 27 19 } 28 20 29 21 fragTexCoord = inTexCoord; 22 gl_Position = vec4(inPosition, 1.0); 30 23 } -
vulkan-game.cpp
rc8c6da8 re5d4aca 332 332 createRenderPass(); 333 333 334 createDescriptorSetLayout(scenePipeline);335 createGraphicsPipeline("shaders/vert.spv", "shaders/frag.spv", scenePipeline);336 337 createDescriptorSetLayout(overlayPipeline);338 createGraphicsPipeline("shaders/vert.spv", "shaders/frag.spv", overlayPipeline);339 340 334 createCommandPool(); 341 createDepthResources();342 createFramebuffers();343 335 344 336 createImageResources("textures/texture.jpg", textureImage, textureImageMemory, textureImageView); 345 337 createImageResourcesFromSDLTexture(uiOverlay, sdlOverlayImage, sdlOverlayImageMemory, sdlOverlayImageView); 346 338 createTextureSampler(); 339 340 createSceneDescriptorSetLayout(scenePipeline); 341 createOverlayDescriptorSetLayout(overlayPipeline); 347 342 348 343 createShaderBuffers(scenePipeline, { … … 357 352 {{-0.5f, 0.5f, 0.0f}, {1.0f, 1.0f, 1.0f}, {0.0f, 0.0f}} 358 353 }, { 359 0, 1, 2, 2, 3,0,360 4, 5, 6, 6, 7,4354 0, 1, 2, 2, 3, 0, 355 4, 5, 6, 6, 7, 4 361 356 }); 362 357 363 358 createShaderBuffers(overlayPipeline, { 364 // Filler vertices to increase the overlay vertex indices to at least 8 365 {{-1.0f, -1.0f, 3.0f}, {1.0f, 1.0f, 1.0f}, {0.0f, 0.0f}}, 366 {{-1.0f, -1.0f, 3.0f}, {1.0f, 1.0f, 1.0f}, {0.0f, 0.0f}}, 367 {{-1.0f, -1.0f, 3.0f}, {1.0f, 1.0f, 1.0f}, {0.0f, 0.0f}}, 368 {{-1.0f, -1.0f, 3.0f}, {1.0f, 1.0f, 1.0f}, {0.0f, 0.0f}}, 369 {{-1.0f, -1.0f, 3.0f}, {1.0f, 1.0f, 1.0f}, {0.0f, 0.0f}}, 370 {{-1.0f, -1.0f, 3.0f}, {1.0f, 1.0f, 1.0f}, {0.0f, 0.0f}}, 371 {{-1.0f, -1.0f, 3.0f}, {1.0f, 1.0f, 1.0f}, {0.0f, 0.0f}}, 372 {{-1.0f, -1.0f, 3.0f}, {1.0f, 1.0f, 1.0f}, {0.0f, 0.0f}}, 373 374 {{-1.0f, 1.0f, 0.0f}, {1.0f, 0.0f, 0.0f}, {0.0f, 1.0f}}, 375 {{ 1.0f, 1.0f, 0.0f}, {0.0f, 1.0f, 0.0f}, {1.0f, 1.0f}}, 376 {{ 1.0f, -1.0f, 0.0f}, {0.0f, 0.0f, 1.0f}, {1.0f, 0.0f}}, 377 {{-1.0f, -1.0f, 0.0f}, {1.0f, 1.0f, 1.0f}, {0.0f, 0.0f}} 359 {{-1.0f, 1.0f, 0.0f}, {0.0f, 0.0f, 0.0f}, {0.0f, 1.0f}}, 360 {{ 1.0f, 1.0f, 0.0f}, {0.0f, 0.0f, 0.0f}, {1.0f, 1.0f}}, 361 {{ 1.0f, -1.0f, 0.0f}, {0.0f, 0.0f, 0.0f}, {1.0f, 0.0f}}, 362 {{-1.0f, -1.0f, 0.0f}, {0.0f, 0.0f, 0.0f}, {0.0f, 0.0f}} 378 363 }, { 379 8, 9, 10, 10, 11, 8364 0, 1, 2, 2,3, 0 380 365 }); 381 366 382 createUniformBuffers(); 383 384 createDescriptorPool(scenePipeline); 385 createDescriptorSets(scenePipeline); 386 387 createDescriptorPool(overlayPipeline); 388 createDescriptorSets(overlayPipeline); 389 390 createCommandBuffers(); 367 createBufferResources(); 368 391 369 createSyncObjects(); 392 370 } … … 794 772 } 795 773 796 void create DescriptorSetLayout(GraphicsPipelineInfo& info) {774 void createSceneDescriptorSetLayout(GraphicsPipelineInfo& info) { 797 775 VkDescriptorSetLayoutBinding uboLayoutBinding = {}; 798 776 uboLayoutBinding.binding = 0; … … 817 795 818 796 array<VkDescriptorSetLayoutBinding, 3> bindings = { uboLayoutBinding, samplerLayoutBinding, overlaySamplerLayoutBinding }; 797 VkDescriptorSetLayoutCreateInfo layoutInfo = {}; 798 layoutInfo.sType = VK_STRUCTURE_TYPE_DESCRIPTOR_SET_LAYOUT_CREATE_INFO; 799 layoutInfo.bindingCount = static_cast<uint32_t>(bindings.size()); 800 layoutInfo.pBindings = bindings.data(); 801 802 if (vkCreateDescriptorSetLayout(device, &layoutInfo, nullptr, &info.descriptorSetLayout) != VK_SUCCESS) { 803 throw runtime_error("failed to create descriptor set layout!"); 804 } 805 } 806 807 void createOverlayDescriptorSetLayout(GraphicsPipelineInfo& info) { 808 VkDescriptorSetLayoutBinding samplerLayoutBinding = {}; 809 samplerLayoutBinding.binding = 0; 810 samplerLayoutBinding.descriptorCount = 1; 811 samplerLayoutBinding.descriptorType = VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER; 812 samplerLayoutBinding.pImmutableSamplers = nullptr; 813 samplerLayoutBinding.stageFlags = VK_SHADER_STAGE_FRAGMENT_BIT; 814 815 array<VkDescriptorSetLayoutBinding, 1> bindings = { samplerLayoutBinding }; 819 816 VkDescriptorSetLayoutCreateInfo layoutInfo = {}; 820 817 layoutInfo.sType = VK_STRUCTURE_TYPE_DESCRIPTOR_SET_LAYOUT_CREATE_INFO; … … 1512 1509 } 1513 1510 1514 void create DescriptorPool(GraphicsPipelineInfo& info) {1511 void createSceneDescriptorPool(GraphicsPipelineInfo& info) { 1515 1512 array<VkDescriptorPoolSize, 3> poolSizes = {}; 1516 1513 poolSizes[0].type = VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER; … … 1532 1529 } 1533 1530 1534 void create DescriptorSets(GraphicsPipelineInfo& info) {1531 void createSceneDescriptorSets(GraphicsPipelineInfo& info) { 1535 1532 vector<VkDescriptorSetLayout> layouts(swapChainImages.size(), info.descriptorSetLayout); 1536 1533 … … 1593 1590 descriptorWrites[2].pImageInfo = &overlayImageInfo; 1594 1591 descriptorWrites[2].pTexelBufferView = nullptr; 1592 1593 vkUpdateDescriptorSets(device, static_cast<uint32_t>(descriptorWrites.size()), descriptorWrites.data(), 0, nullptr); 1594 } 1595 } 1596 1597 void createOverlayDescriptorPool(GraphicsPipelineInfo& info) { 1598 array<VkDescriptorPoolSize, 1> poolSizes = {}; 1599 poolSizes[0].type = VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER; 1600 poolSizes[0].descriptorCount = static_cast<uint32_t>(swapChainImages.size()); 1601 1602 VkDescriptorPoolCreateInfo poolInfo = {}; 1603 poolInfo.sType = VK_STRUCTURE_TYPE_DESCRIPTOR_POOL_CREATE_INFO; 1604 poolInfo.poolSizeCount = static_cast<uint32_t>(poolSizes.size()); 1605 poolInfo.pPoolSizes = poolSizes.data(); 1606 poolInfo.maxSets = static_cast<uint32_t>(swapChainImages.size()); 1607 1608 if (vkCreateDescriptorPool(device, &poolInfo, nullptr, &info.descriptorPool) != VK_SUCCESS) { 1609 throw runtime_error("failed to create descriptor pool!"); 1610 } 1611 } 1612 1613 void createOverlayDescriptorSets(GraphicsPipelineInfo& info) { 1614 vector<VkDescriptorSetLayout> layouts(swapChainImages.size(), info.descriptorSetLayout); 1615 1616 VkDescriptorSetAllocateInfo allocInfo = {}; 1617 allocInfo.sType = VK_STRUCTURE_TYPE_DESCRIPTOR_SET_ALLOCATE_INFO; 1618 allocInfo.descriptorPool = info.descriptorPool; 1619 allocInfo.descriptorSetCount = static_cast<uint32_t>(swapChainImages.size()); 1620 allocInfo.pSetLayouts = layouts.data(); 1621 1622 info.descriptorSets.resize(swapChainImages.size()); 1623 if (vkAllocateDescriptorSets(device, &allocInfo, info.descriptorSets.data()) != VK_SUCCESS) { 1624 throw runtime_error("failed to allocate descriptor sets!"); 1625 } 1626 1627 for (size_t i = 0; i < swapChainImages.size(); i++) { 1628 VkDescriptorImageInfo imageInfo = {}; 1629 imageInfo.imageLayout = VK_IMAGE_LAYOUT_SHADER_READ_ONLY_OPTIMAL; 1630 imageInfo.imageView = sdlOverlayImageView; 1631 imageInfo.sampler = textureSampler; 1632 1633 array<VkWriteDescriptorSet, 1> descriptorWrites = {}; 1634 1635 descriptorWrites[0].sType = VK_STRUCTURE_TYPE_WRITE_DESCRIPTOR_SET; 1636 descriptorWrites[0].dstSet = info.descriptorSets[i]; 1637 descriptorWrites[0].dstBinding = 0; 1638 descriptorWrites[0].dstArrayElement = 0; 1639 descriptorWrites[0].descriptorType = VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER; 1640 descriptorWrites[0].descriptorCount = 1; 1641 descriptorWrites[0].pBufferInfo = nullptr; 1642 descriptorWrites[0].pImageInfo = &imageInfo; 1643 descriptorWrites[0].pTexelBufferView = nullptr; 1595 1644 1596 1645 vkUpdateDescriptorSets(device, static_cast<uint32_t>(descriptorWrites.size()), descriptorWrites.data(), 0, nullptr); … … 1845 1894 createRenderPass(); 1846 1895 1847 createGraphicsPipeline("shaders/scene-vert.spv", "shaders/scene-frag.spv", scenePipeline); 1848 createGraphicsPipeline("shaders/overlay-vert.spv", "shaders/overlay-frag.spv", overlayPipeline); 1849 1896 createBufferResources(); 1897 } 1898 1899 void createBufferResources() { 1850 1900 createDepthResources(); 1851 1901 createFramebuffers(); 1852 1902 createUniformBuffers(); 1853 1903 1854 createDescriptorPool(scenePipeline); 1855 createDescriptorSets(scenePipeline); 1856 1857 createDescriptorPool(overlayPipeline); 1858 createDescriptorSets(overlayPipeline); 1904 createGraphicsPipeline("shaders/scene-vert.spv", "shaders/scene-frag.spv", scenePipeline); 1905 createSceneDescriptorPool(scenePipeline); 1906 createSceneDescriptorSets(scenePipeline); 1907 1908 createGraphicsPipeline("shaders/overlay-vert.spv", "shaders/overlay-frag.spv", overlayPipeline); 1909 createOverlayDescriptorPool(overlayPipeline); 1910 createOverlayDescriptorSets(overlayPipeline); 1859 1911 1860 1912 createCommandBuffers();
Note:
See TracChangeset
for help on using the changeset viewer.