Changes in vulkan-game.cpp [adcd252:4f63fa8] in opengl-game


Ignore:
File:
1 edited

Legend:

Unmodified
Added
Removed
  • vulkan-game.cpp

    radcd252 r4f63fa8  
    6767
    6868struct Vertex {
    69    glm::vec3 pos;
     69   glm::vec2 pos;
    7070   glm::vec3 color;
    7171   glm::vec2 texCoord;
     
    8686      attributeDescriptions[0].binding = 0;
    8787      attributeDescriptions[0].location = 0;
    88       attributeDescriptions[0].format = VK_FORMAT_R32G32B32_SFLOAT;
     88      attributeDescriptions[0].format = VK_FORMAT_R32G32_SFLOAT;
    8989      attributeDescriptions[0].offset = offsetof(Vertex, pos);
    9090
     
    110110
    111111const 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}}
    121116};
    122117
    123118const 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
    126120};
    127121
     
    190184
    191185      VkCommandPool commandPool;
    192 
    193       VkImage depthImage;
    194       VkDeviceMemory depthImageMemory;
    195       VkImageView depthImageView;
    196186
    197187      VkImage textureImage;
     
    246236         createDescriptorSetLayout();
    247237         createGraphicsPipeline();
     238         createFramebuffers();
    248239         createCommandPool();
    249          createDepthResources();
    250          createFramebuffers();
    251240         createTextureImage();
    252241         createTextureImageView();
     
    607596
    608597         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);
    610599         }
    611600      }
     
    626615         colorAttachmentRef.layout = VK_IMAGE_LAYOUT_COLOR_ATTACHMENT_OPTIMAL;
    627616
    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 
    642617         VkSubpassDescription subpass = {};
    643618         subpass.pipelineBindPoint = VK_PIPELINE_BIND_POINT_GRAPHICS;
    644619         subpass.colorAttachmentCount = 1;
    645620         subpass.pColorAttachments = &colorAttachmentRef;
    646          subpass.pDepthStencilAttachment = &depthAttachmentRef;
    647621
    648622         VkSubpassDependency dependency = {};
     
    654628         dependency.dstAccessMask = VK_ACCESS_COLOR_ATTACHMENT_READ_BIT | VK_ACCESS_COLOR_ATTACHMENT_WRITE_BIT;
    655629
    656          array<VkAttachmentDescription, 2> attachments = { colorAttachment, depthAttachment };
    657630         VkRenderPassCreateInfo renderPassInfo = {};
    658631         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;
    661634         renderPassInfo.subpassCount = 1;
    662635         renderPassInfo.pSubpasses = &subpass;
     
    781754         colorBlending.blendConstants[3] = 0.0f;
    782755
    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 
    795756         VkPipelineLayoutCreateInfo pipelineLayoutInfo = {};
    796757         pipelineLayoutInfo.sType = VK_STRUCTURE_TYPE_PIPELINE_LAYOUT_CREATE_INFO;
     
    812773         pipelineInfo.pRasterizationState = &rasterizer;
    813774         pipelineInfo.pMultisampleState = &multisampling;
    814          pipelineInfo.pDepthStencilState = &depthStencil;
     775         pipelineInfo.pDepthStencilState = nullptr;
    815776         pipelineInfo.pColorBlendState = &colorBlending;
    816777         pipelineInfo.pDynamicState = nullptr;
     
    847808
    848809         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]
    852812            };
    853813
     
    855815            framebufferInfo.sType = VK_STRUCTURE_TYPE_FRAMEBUFFER_CREATE_INFO;
    856816            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;
    859819            framebufferInfo.width = swapChainExtent.width;
    860820            framebufferInfo.height = swapChainExtent.height;
     
    910870
    911871         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;
    952872      }
    953873
     
    1035955         barrier.dstQueueFamilyIndex = VK_QUEUE_FAMILY_IGNORED;
    1036956         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;
    1048958         barrier.subresourceRange.baseMipLevel = 0;
    1049959         barrier.subresourceRange.levelCount = 1;
     
    1066976            sourceStage = VK_PIPELINE_STAGE_TRANSFER_BIT;
    1067977            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;
    1074978         } else {
    1075979            throw invalid_argument("unsupported layout transition!");
     
    11151019
    11161020      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) {
    11211025         VkImageViewCreateInfo viewInfo = {};
    11221026         viewInfo.sType = VK_STRUCTURE_TYPE_IMAGE_VIEW_CREATE_INFO;
     
    11301034         viewInfo.components.a = VK_COMPONENT_SWIZZLE_IDENTITY;
    11311035
    1132          viewInfo.subresourceRange.aspectMask = aspectFlags;
     1036         viewInfo.subresourceRange.aspectMask = VK_IMAGE_ASPECT_COLOR_BIT;
    11331037         viewInfo.subresourceRange.baseMipLevel = 0;
    11341038         viewInfo.subresourceRange.levelCount = 1;
     
    14101314            renderPassInfo.renderArea.extent = swapChainExtent;
    14111315
    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;
    14181319
    14191320            vkCmdBeginRenderPass(commandBuffers[i], &renderPassInfo, VK_SUBPASS_CONTENTS_INLINE);
     
    15941495         createRenderPass();
    15951496         createGraphicsPipeline();
    1596          createDepthResources();
    15971497         createFramebuffers();
    15981498         createUniformBuffers();
     
    16411541
    16421542      void cleanupSwapChain() {
    1643          vkDestroyImageView(device, depthImageView, nullptr);
    1644          vkDestroyImage(device, depthImage, nullptr);
    1645          vkFreeMemory(device, depthImageMemory, nullptr);
    1646 
    16471543         for (auto framebuffer : swapChainFramebuffers) {
    16481544            vkDestroyFramebuffer(device, framebuffer, nullptr);
Note: See TracChangeset for help on using the changeset viewer.