Changes in vulkan-game.cpp [adcd252:4f63fa8] in opengl-game
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
vulkan-game.cpp
radcd252 r4f63fa8 67 67 68 68 struct Vertex { 69 glm::vec 3pos;69 glm::vec2 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 B32_SFLOAT;88 attributeDescriptions[0].format = VK_FORMAT_R32G32_SFLOAT; 89 89 attributeDescriptions[0].offset = offsetof(Vertex, pos); 90 90 … … 110 110 111 111 const vector<Vertex> vertices = { 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}} 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}} 121 116 }; 122 117 123 118 const vector<uint16_t> indices = { 124 0, 1, 2, 2, 3, 0, 125 4, 5, 6, 6, 7, 4 119 0, 1, 2, 2, 3, 0 126 120 }; 127 121 … … 190 184 191 185 VkCommandPool commandPool; 192 193 VkImage depthImage;194 VkDeviceMemory depthImageMemory;195 VkImageView depthImageView;196 186 197 187 VkImage textureImage; … … 246 236 createDescriptorSetLayout(); 247 237 createGraphicsPipeline(); 238 createFramebuffers(); 248 239 createCommandPool(); 249 createDepthResources();250 createFramebuffers();251 240 createTextureImage(); 252 241 createTextureImageView(); … … 607 596 608 597 for (size_t i = 0; i < swapChainImages.size(); i++) { 609 swapChainImageViews[i] = createImageView(swapChainImages[i], swapChainImageFormat , VK_IMAGE_ASPECT_COLOR_BIT);598 swapChainImageViews[i] = createImageView(swapChainImages[i], swapChainImageFormat); 610 599 } 611 600 } … … 626 615 colorAttachmentRef.layout = VK_IMAGE_LAYOUT_COLOR_ATTACHMENT_OPTIMAL; 627 616 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 642 617 VkSubpassDescription subpass = {}; 643 618 subpass.pipelineBindPoint = VK_PIPELINE_BIND_POINT_GRAPHICS; 644 619 subpass.colorAttachmentCount = 1; 645 620 subpass.pColorAttachments = &colorAttachmentRef; 646 subpass.pDepthStencilAttachment = &depthAttachmentRef;647 621 648 622 VkSubpassDependency dependency = {}; … … 654 628 dependency.dstAccessMask = VK_ACCESS_COLOR_ATTACHMENT_READ_BIT | VK_ACCESS_COLOR_ATTACHMENT_WRITE_BIT; 655 629 656 array<VkAttachmentDescription, 2> attachments = { colorAttachment, depthAttachment };657 630 VkRenderPassCreateInfo renderPassInfo = {}; 658 631 renderPassInfo.sType = VK_STRUCTURE_TYPE_RENDER_PASS_CREATE_INFO; 659 renderPassInfo.attachmentCount = static_cast<uint32_t>(attachments.size());660 renderPassInfo.pAttachments = attachments.data();632 renderPassInfo.attachmentCount = 1; 633 renderPassInfo.pAttachments = &colorAttachment; 661 634 renderPassInfo.subpassCount = 1; 662 635 renderPassInfo.pSubpasses = &subpass; … … 781 754 colorBlending.blendConstants[3] = 0.0f; 782 755 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 795 756 VkPipelineLayoutCreateInfo pipelineLayoutInfo = {}; 796 757 pipelineLayoutInfo.sType = VK_STRUCTURE_TYPE_PIPELINE_LAYOUT_CREATE_INFO; … … 812 773 pipelineInfo.pRasterizationState = &rasterizer; 813 774 pipelineInfo.pMultisampleState = &multisampling; 814 pipelineInfo.pDepthStencilState = &depthStencil;775 pipelineInfo.pDepthStencilState = nullptr; 815 776 pipelineInfo.pColorBlendState = &colorBlending; 816 777 pipelineInfo.pDynamicState = nullptr; … … 847 808 848 809 for (size_t i = 0; i < swapChainImageViews.size(); i++) { 849 array <VkImageView, 2> attachments = { 850 swapChainImageViews[i], 851 depthImageView 810 VkImageView attachments[] = { 811 swapChainImageViews[i] 852 812 }; 853 813 … … 855 815 framebufferInfo.sType = VK_STRUCTURE_TYPE_FRAMEBUFFER_CREATE_INFO; 856 816 framebufferInfo.renderPass = renderPass; 857 framebufferInfo.attachmentCount = static_cast<uint32_t>(attachments.size());858 framebufferInfo.pAttachments = attachments .data();817 framebufferInfo.attachmentCount = 1; 818 framebufferInfo.pAttachments = attachments; 859 819 framebufferInfo.width = swapChainExtent.width; 860 820 framebufferInfo.height = swapChainExtent.height; … … 910 870 911 871 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_BIT929 );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;952 872 } 953 873 … … 1035 955 barrier.dstQueueFamilyIndex = VK_QUEUE_FAMILY_IGNORED; 1036 956 barrier.image = image; 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 957 barrier.subresourceRange.aspectMask = VK_IMAGE_ASPECT_COLOR_BIT; 1048 958 barrier.subresourceRange.baseMipLevel = 0; 1049 959 barrier.subresourceRange.levelCount = 1; … … 1066 976 sourceStage = VK_PIPELINE_STAGE_TRANSFER_BIT; 1067 977 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;1074 978 } else { 1075 979 throw invalid_argument("unsupported layout transition!"); … … 1115 1019 1116 1020 void createTextureImageView() { 1117 textureImageView = createImageView(textureImage, VK_FORMAT_R8G8B8A8_UNORM , VK_IMAGE_ASPECT_COLOR_BIT);1118 } 1119 1120 VkImageView createImageView(VkImage image, VkFormat format , VkImageAspectFlags aspectFlags) {1021 textureImageView = createImageView(textureImage, VK_FORMAT_R8G8B8A8_UNORM); 1022 } 1023 1024 VkImageView createImageView(VkImage image, VkFormat format) { 1121 1025 VkImageViewCreateInfo viewInfo = {}; 1122 1026 viewInfo.sType = VK_STRUCTURE_TYPE_IMAGE_VIEW_CREATE_INFO; … … 1130 1034 viewInfo.components.a = VK_COMPONENT_SWIZZLE_IDENTITY; 1131 1035 1132 viewInfo.subresourceRange.aspectMask = aspectFlags;1036 viewInfo.subresourceRange.aspectMask = VK_IMAGE_ASPECT_COLOR_BIT; 1133 1037 viewInfo.subresourceRange.baseMipLevel = 0; 1134 1038 viewInfo.subresourceRange.levelCount = 1; … … 1410 1314 renderPassInfo.renderArea.extent = swapChainExtent; 1411 1315 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(); 1316 VkClearValue clearColor = { 0.0f, 0.0f, 0.0f, 1.0f }; 1317 renderPassInfo.clearValueCount = 1; 1318 renderPassInfo.pClearValues = &clearColor; 1418 1319 1419 1320 vkCmdBeginRenderPass(commandBuffers[i], &renderPassInfo, VK_SUBPASS_CONTENTS_INLINE); … … 1594 1495 createRenderPass(); 1595 1496 createGraphicsPipeline(); 1596 createDepthResources();1597 1497 createFramebuffers(); 1598 1498 createUniformBuffers(); … … 1641 1541 1642 1542 void cleanupSwapChain() { 1643 vkDestroyImageView(device, depthImageView, nullptr);1644 vkDestroyImage(device, depthImage, nullptr);1645 vkFreeMemory(device, depthImageMemory, nullptr);1646 1647 1543 for (auto framebuffer : swapChainFramebuffers) { 1648 1544 vkDestroyFramebuffer(device, framebuffer, nullptr);
Note:
See TracChangeset
for help on using the changeset viewer.