Changeset adcd252 in opengl-game
- Timestamp:
- Aug 2, 2019, 8:00:34 PM (5 years ago)
- Branches:
- feature/imgui-sdl, master, points-test
- Children:
- bba12e7
- Parents:
- 4f63fa8
- Files:
-
- 2 edited
Legend:
- Unmodified
- Added
- Removed
-
shaders/shader.vert
r4f63fa8 radcd252 8 8 } ubo; 9 9 10 layout(location = 0) in vec 2inPosition;10 layout(location = 0) in vec3 inPosition; 11 11 layout(location = 1) in vec3 inColor; 12 12 layout(location = 2) in vec2 inTexCoord; … … 16 16 17 17 void main() { 18 gl_Position = ubo.proj * ubo.view * ubo.model * vec4(inPosition, 0.0,1.0);18 gl_Position = ubo.proj * ubo.view * ubo.model * vec4(inPosition, 1.0); 19 19 fragColor = inColor; 20 20 fragTexCoord = inTexCoord; -
vulkan-game.cpp
r4f63fa8 radcd252 67 67 68 68 struct Vertex { 69 glm::vec 2pos;69 glm::vec3 pos; 70 70 glm::vec3 color; 71 71 glm::vec2 texCoord; … … 86 86 attributeDescriptions[0].binding = 0; 87 87 attributeDescriptions[0].location = 0; 88 attributeDescriptions[0].format = VK_FORMAT_R32G32 _SFLOAT;88 attributeDescriptions[0].format = VK_FORMAT_R32G32B32_SFLOAT; 89 89 attributeDescriptions[0].offset = offsetof(Vertex, pos); 90 90 … … 110 110 111 111 const vector<Vertex> vertices = { 112 {{-0.5f, -0.5f}, {1.0f, 0.0f, 0.0f}, {0.0f, 1.0f}}, 113 {{ 0.5f, -0.5f}, {0.0f, 1.0f, 0.0f}, {1.0f, 1.0f}}, 114 {{ 0.5f, 0.5f}, {0.0f, 0.0f, 1.0f}, {1.0f, 0.0f}}, 115 {{-0.5f, 0.5f}, {1.0f, 1.0f, 1.0f}, {0.0f, 0.0f}} 112 {{-0.5f, -0.5f, 0.0f}, {1.0f, 0.0f, 0.0f}, {0.0f, 1.0f}}, 113 {{ 0.5f, -0.5f, 0.0f}, {0.0f, 1.0f, 0.0f}, {1.0f, 1.0f}}, 114 {{ 0.5f, 0.5f, 0.0f}, {0.0f, 0.0f, 1.0f}, {1.0f, 0.0f}}, 115 {{-0.5f, 0.5f, 0.0f}, {1.0f, 1.0f, 1.0f}, {0.0f, 0.0f}}, 116 117 {{-0.5f, -0.5f, -0.5f}, {1.0f, 0.0f, 0.0f}, {0.0f, 1.0f}}, 118 {{ 0.5f, -0.5f, -0.5f}, {0.0f, 1.0f, 0.0f}, {1.0f, 1.0f}}, 119 {{ 0.5f, 0.5f, -0.5f}, {0.0f, 0.0f, 1.0f}, {1.0f, 0.0f}}, 120 {{-0.5f, 0.5f, -0.5f}, {1.0f, 1.0f, 1.0f}, {0.0f, 0.0f}} 116 121 }; 117 122 118 123 const vector<uint16_t> indices = { 119 0, 1, 2, 2, 3, 0 124 0, 1, 2, 2, 3, 0, 125 4, 5, 6, 6, 7, 4 120 126 }; 121 127 … … 184 190 185 191 VkCommandPool commandPool; 192 193 VkImage depthImage; 194 VkDeviceMemory depthImageMemory; 195 VkImageView depthImageView; 186 196 187 197 VkImage textureImage; … … 236 246 createDescriptorSetLayout(); 237 247 createGraphicsPipeline(); 248 createCommandPool(); 249 createDepthResources(); 238 250 createFramebuffers(); 239 createCommandPool();240 251 createTextureImage(); 241 252 createTextureImageView(); … … 596 607 597 608 for (size_t i = 0; i < swapChainImages.size(); i++) { 598 swapChainImageViews[i] = createImageView(swapChainImages[i], swapChainImageFormat );609 swapChainImageViews[i] = createImageView(swapChainImages[i], swapChainImageFormat, VK_IMAGE_ASPECT_COLOR_BIT); 599 610 } 600 611 } … … 615 626 colorAttachmentRef.layout = VK_IMAGE_LAYOUT_COLOR_ATTACHMENT_OPTIMAL; 616 627 628 VkAttachmentDescription depthAttachment = {}; 629 depthAttachment.format = findDepthFormat(); 630 depthAttachment.samples = VK_SAMPLE_COUNT_1_BIT; 631 depthAttachment.loadOp = VK_ATTACHMENT_LOAD_OP_CLEAR; 632 depthAttachment.storeOp = VK_ATTACHMENT_STORE_OP_DONT_CARE; 633 depthAttachment.stencilLoadOp = VK_ATTACHMENT_LOAD_OP_DONT_CARE; 634 depthAttachment.stencilStoreOp = VK_ATTACHMENT_STORE_OP_DONT_CARE; 635 depthAttachment.initialLayout = VK_IMAGE_LAYOUT_UNDEFINED; 636 depthAttachment.finalLayout = VK_IMAGE_LAYOUT_DEPTH_STENCIL_ATTACHMENT_OPTIMAL; 637 638 VkAttachmentReference depthAttachmentRef = {}; 639 depthAttachmentRef.attachment = 1; 640 depthAttachmentRef.layout = VK_IMAGE_LAYOUT_DEPTH_STENCIL_ATTACHMENT_OPTIMAL; 641 617 642 VkSubpassDescription subpass = {}; 618 643 subpass.pipelineBindPoint = VK_PIPELINE_BIND_POINT_GRAPHICS; 619 644 subpass.colorAttachmentCount = 1; 620 645 subpass.pColorAttachments = &colorAttachmentRef; 646 subpass.pDepthStencilAttachment = &depthAttachmentRef; 621 647 622 648 VkSubpassDependency dependency = {}; … … 628 654 dependency.dstAccessMask = VK_ACCESS_COLOR_ATTACHMENT_READ_BIT | VK_ACCESS_COLOR_ATTACHMENT_WRITE_BIT; 629 655 656 array<VkAttachmentDescription, 2> attachments = { colorAttachment, depthAttachment }; 630 657 VkRenderPassCreateInfo renderPassInfo = {}; 631 658 renderPassInfo.sType = VK_STRUCTURE_TYPE_RENDER_PASS_CREATE_INFO; 632 renderPassInfo.attachmentCount = 1;633 renderPassInfo.pAttachments = &colorAttachment;659 renderPassInfo.attachmentCount = static_cast<uint32_t>(attachments.size()); 660 renderPassInfo.pAttachments = attachments.data(); 634 661 renderPassInfo.subpassCount = 1; 635 662 renderPassInfo.pSubpasses = &subpass; … … 754 781 colorBlending.blendConstants[3] = 0.0f; 755 782 783 VkPipelineDepthStencilStateCreateInfo depthStencil = {}; 784 depthStencil.sType = VK_STRUCTURE_TYPE_PIPELINE_DEPTH_STENCIL_STATE_CREATE_INFO; 785 depthStencil.depthTestEnable = VK_TRUE; 786 depthStencil.depthWriteEnable = VK_TRUE; 787 depthStencil.depthCompareOp = VK_COMPARE_OP_LESS; 788 depthStencil.depthBoundsTestEnable = VK_FALSE; 789 depthStencil.minDepthBounds = 0.0f; 790 depthStencil.maxDepthBounds = 1.0f; 791 depthStencil.stencilTestEnable = VK_FALSE; 792 depthStencil.front = {}; 793 depthStencil.back = {}; 794 756 795 VkPipelineLayoutCreateInfo pipelineLayoutInfo = {}; 757 796 pipelineLayoutInfo.sType = VK_STRUCTURE_TYPE_PIPELINE_LAYOUT_CREATE_INFO; … … 773 812 pipelineInfo.pRasterizationState = &rasterizer; 774 813 pipelineInfo.pMultisampleState = &multisampling; 775 pipelineInfo.pDepthStencilState = nullptr;814 pipelineInfo.pDepthStencilState = &depthStencil; 776 815 pipelineInfo.pColorBlendState = &colorBlending; 777 816 pipelineInfo.pDynamicState = nullptr; … … 808 847 809 848 for (size_t i = 0; i < swapChainImageViews.size(); i++) { 810 VkImageView attachments[] = { 811 swapChainImageViews[i] 849 array <VkImageView, 2> attachments = { 850 swapChainImageViews[i], 851 depthImageView 812 852 }; 813 853 … … 815 855 framebufferInfo.sType = VK_STRUCTURE_TYPE_FRAMEBUFFER_CREATE_INFO; 816 856 framebufferInfo.renderPass = renderPass; 817 framebufferInfo.attachmentCount = 1;818 framebufferInfo.pAttachments = attachments ;857 framebufferInfo.attachmentCount = static_cast<uint32_t>(attachments.size()); 858 framebufferInfo.pAttachments = attachments.data(); 819 859 framebufferInfo.width = swapChainExtent.width; 820 860 framebufferInfo.height = swapChainExtent.height; … … 870 910 871 911 return indices; 912 } 913 914 void createDepthResources() { 915 VkFormat depthFormat = findDepthFormat(); 916 917 createImage(swapChainExtent.width, swapChainExtent.height, depthFormat, VK_IMAGE_TILING_OPTIMAL, 918 VK_IMAGE_USAGE_DEPTH_STENCIL_ATTACHMENT_BIT, VK_MEMORY_PROPERTY_DEVICE_LOCAL_BIT, depthImage, depthImageMemory); 919 depthImageView = createImageView(depthImage, depthFormat, VK_IMAGE_ASPECT_DEPTH_BIT); 920 921 transitionImageLayout(depthImage, depthFormat, VK_IMAGE_LAYOUT_UNDEFINED, VK_IMAGE_LAYOUT_DEPTH_STENCIL_ATTACHMENT_OPTIMAL); 922 } 923 924 VkFormat findDepthFormat() { 925 return findSupportedFormat( 926 { VK_FORMAT_D32_SFLOAT, VK_FORMAT_D32_SFLOAT_S8_UINT, VK_FORMAT_D24_UNORM_S8_UINT }, 927 VK_IMAGE_TILING_OPTIMAL, 928 VK_FORMAT_FEATURE_DEPTH_STENCIL_ATTACHMENT_BIT 929 ); 930 } 931 932 VkFormat findSupportedFormat(const vector<VkFormat>& candidates, VkImageTiling tiling, 933 VkFormatFeatureFlags features) { 934 for (VkFormat format : candidates) { 935 VkFormatProperties props; 936 vkGetPhysicalDeviceFormatProperties(physicalDevice, format, &props); 937 938 if (tiling == VK_IMAGE_TILING_LINEAR && 939 (props.linearTilingFeatures & features) == features) { 940 return format; 941 } else if (tiling == VK_IMAGE_TILING_OPTIMAL && 942 (props.optimalTilingFeatures & features) == features) { 943 return format; 944 } 945 } 946 947 throw runtime_error("failed to find supported format!"); 948 } 949 950 bool hasStencilComponent(VkFormat format) { 951 return format == VK_FORMAT_D32_SFLOAT_S8_UINT || format == VK_FORMAT_D24_UNORM_S8_UINT; 872 952 } 873 953 … … 955 1035 barrier.dstQueueFamilyIndex = VK_QUEUE_FAMILY_IGNORED; 956 1036 barrier.image = image; 957 barrier.subresourceRange.aspectMask = VK_IMAGE_ASPECT_COLOR_BIT; 1037 1038 if (newLayout == VK_IMAGE_LAYOUT_DEPTH_STENCIL_ATTACHMENT_OPTIMAL) { 1039 barrier.subresourceRange.aspectMask = VK_IMAGE_ASPECT_DEPTH_BIT; 1040 1041 if (hasStencilComponent(format)) { 1042 barrier.subresourceRange.aspectMask |= VK_IMAGE_ASPECT_STENCIL_BIT; 1043 } 1044 } else { 1045 barrier.subresourceRange.aspectMask = VK_IMAGE_ASPECT_COLOR_BIT; 1046 } 1047 958 1048 barrier.subresourceRange.baseMipLevel = 0; 959 1049 barrier.subresourceRange.levelCount = 1; … … 976 1066 sourceStage = VK_PIPELINE_STAGE_TRANSFER_BIT; 977 1067 destinationStage = VK_PIPELINE_STAGE_FRAGMENT_SHADER_BIT; 1068 } else if (oldLayout == VK_IMAGE_LAYOUT_UNDEFINED && newLayout == VK_IMAGE_LAYOUT_DEPTH_STENCIL_ATTACHMENT_OPTIMAL) { 1069 barrier.srcAccessMask = 0; 1070 barrier.dstAccessMask = VK_ACCESS_DEPTH_STENCIL_ATTACHMENT_READ_BIT | VK_ACCESS_DEPTH_STENCIL_ATTACHMENT_WRITE_BIT; 1071 1072 sourceStage = VK_PIPELINE_STAGE_TOP_OF_PIPE_BIT; 1073 destinationStage = VK_PIPELINE_STAGE_EARLY_FRAGMENT_TESTS_BIT; 978 1074 } else { 979 1075 throw invalid_argument("unsupported layout transition!"); … … 1019 1115 1020 1116 void createTextureImageView() { 1021 textureImageView = createImageView(textureImage, VK_FORMAT_R8G8B8A8_UNORM );1022 } 1023 1024 VkImageView createImageView(VkImage image, VkFormat format ) {1117 textureImageView = createImageView(textureImage, VK_FORMAT_R8G8B8A8_UNORM, VK_IMAGE_ASPECT_COLOR_BIT); 1118 } 1119 1120 VkImageView createImageView(VkImage image, VkFormat format, VkImageAspectFlags aspectFlags) { 1025 1121 VkImageViewCreateInfo viewInfo = {}; 1026 1122 viewInfo.sType = VK_STRUCTURE_TYPE_IMAGE_VIEW_CREATE_INFO; … … 1034 1130 viewInfo.components.a = VK_COMPONENT_SWIZZLE_IDENTITY; 1035 1131 1036 viewInfo.subresourceRange.aspectMask = VK_IMAGE_ASPECT_COLOR_BIT;1132 viewInfo.subresourceRange.aspectMask = aspectFlags; 1037 1133 viewInfo.subresourceRange.baseMipLevel = 0; 1038 1134 viewInfo.subresourceRange.levelCount = 1; … … 1314 1410 renderPassInfo.renderArea.extent = swapChainExtent; 1315 1411 1316 VkClearValue clearColor = { 0.0f, 0.0f, 0.0f, 1.0f }; 1317 renderPassInfo.clearValueCount = 1; 1318 renderPassInfo.pClearValues = &clearColor; 1412 array<VkClearValue, 2> clearValues = {}; 1413 clearValues[0].color = { 0.0f, 0.0f, 0.0f, 1.0f }; 1414 clearValues[1].depthStencil = { 1.0f, 0 }; 1415 1416 renderPassInfo.clearValueCount = static_cast<uint32_t>(clearValues.size()); 1417 renderPassInfo.pClearValues = clearValues.data(); 1319 1418 1320 1419 vkCmdBeginRenderPass(commandBuffers[i], &renderPassInfo, VK_SUBPASS_CONTENTS_INLINE); … … 1495 1594 createRenderPass(); 1496 1595 createGraphicsPipeline(); 1596 createDepthResources(); 1497 1597 createFramebuffers(); 1498 1598 createUniformBuffers(); … … 1541 1641 1542 1642 void cleanupSwapChain() { 1643 vkDestroyImageView(device, depthImageView, nullptr); 1644 vkDestroyImage(device, depthImage, nullptr); 1645 vkFreeMemory(device, depthImageMemory, nullptr); 1646 1543 1647 for (auto framebuffer : swapChainFramebuffers) { 1544 1648 vkDestroyFramebuffer(device, framebuffer, nullptr);
Note:
See TracChangeset
for help on using the changeset viewer.