Changeset 6fc24c7 in opengl-game


Ignore:
Timestamp:
Sep 27, 2019, 7:58:33 PM (5 years ago)
Author:
Dmitry Portnoy <dmitry.portnoy@…>
Branches:
feature/imgui-sdl, master, points-test
Children:
0e09340
Parents:
f94eea9
Message:

In vulkangame, add code to create a render pass

Files:
5 edited

Legend:

Unmodified
Added
Removed
  • vulkan-game.cpp

    rf94eea9 r6fc24c7  
    11#include "vulkan-game.hpp"
    22
     3#include <array>
    34#include <iostream>
    45#include <set>
     
    102103   createSwapChain();
    103104   createImageViews();
     105   createRenderPass();
    104106}
    105107
     
    419421}
    420422
     423void VulkanGame::createRenderPass() {
     424   VkAttachmentDescription colorAttachment = {};
     425   colorAttachment.format = swapChainImageFormat;
     426   colorAttachment.samples = VK_SAMPLE_COUNT_1_BIT;
     427   colorAttachment.loadOp = VK_ATTACHMENT_LOAD_OP_CLEAR;
     428   colorAttachment.storeOp = VK_ATTACHMENT_STORE_OP_STORE;
     429   colorAttachment.stencilLoadOp = VK_ATTACHMENT_LOAD_OP_DONT_CARE;
     430   colorAttachment.stencilStoreOp = VK_ATTACHMENT_STORE_OP_DONT_CARE;
     431   colorAttachment.initialLayout = VK_IMAGE_LAYOUT_UNDEFINED;
     432   colorAttachment.finalLayout = VK_IMAGE_LAYOUT_PRESENT_SRC_KHR;
     433
     434   VkAttachmentReference colorAttachmentRef = {};
     435   colorAttachmentRef.attachment = 0;
     436   colorAttachmentRef.layout = VK_IMAGE_LAYOUT_COLOR_ATTACHMENT_OPTIMAL;
     437
     438   VkAttachmentDescription depthAttachment = {};
     439   depthAttachment.format = findDepthFormat();
     440   depthAttachment.samples = VK_SAMPLE_COUNT_1_BIT;
     441   depthAttachment.loadOp = VK_ATTACHMENT_LOAD_OP_CLEAR;
     442   depthAttachment.storeOp = VK_ATTACHMENT_STORE_OP_DONT_CARE;
     443   depthAttachment.stencilLoadOp = VK_ATTACHMENT_LOAD_OP_DONT_CARE;
     444   depthAttachment.stencilStoreOp = VK_ATTACHMENT_STORE_OP_DONT_CARE;
     445   depthAttachment.initialLayout = VK_IMAGE_LAYOUT_UNDEFINED;
     446   depthAttachment.finalLayout = VK_IMAGE_LAYOUT_DEPTH_STENCIL_ATTACHMENT_OPTIMAL;
     447
     448   VkAttachmentReference depthAttachmentRef = {};
     449   depthAttachmentRef.attachment = 1;
     450   depthAttachmentRef.layout = VK_IMAGE_LAYOUT_DEPTH_STENCIL_ATTACHMENT_OPTIMAL;
     451
     452   VkSubpassDescription subpass = {};
     453   subpass.pipelineBindPoint = VK_PIPELINE_BIND_POINT_GRAPHICS;
     454   subpass.colorAttachmentCount = 1;
     455   subpass.pColorAttachments = &colorAttachmentRef;
     456   subpass.pDepthStencilAttachment = &depthAttachmentRef;
     457
     458   VkSubpassDependency dependency = {};
     459   dependency.srcSubpass = VK_SUBPASS_EXTERNAL;
     460   dependency.dstSubpass = 0;
     461   dependency.srcStageMask = VK_PIPELINE_STAGE_COLOR_ATTACHMENT_OUTPUT_BIT;
     462   dependency.srcAccessMask = 0;
     463   dependency.dstStageMask = VK_PIPELINE_STAGE_COLOR_ATTACHMENT_OUTPUT_BIT;
     464   dependency.dstAccessMask = VK_ACCESS_COLOR_ATTACHMENT_READ_BIT | VK_ACCESS_COLOR_ATTACHMENT_WRITE_BIT;
     465
     466   array<VkAttachmentDescription, 2> attachments = { colorAttachment, depthAttachment };
     467   VkRenderPassCreateInfo renderPassInfo = {};
     468   renderPassInfo.sType = VK_STRUCTURE_TYPE_RENDER_PASS_CREATE_INFO;
     469   renderPassInfo.attachmentCount = static_cast<uint32_t>(attachments.size());
     470   renderPassInfo.pAttachments = attachments.data();
     471   renderPassInfo.subpassCount = 1;
     472   renderPassInfo.pSubpasses = &subpass;
     473   renderPassInfo.dependencyCount = 1;
     474   renderPassInfo.pDependencies = &dependency;
     475
     476   if (vkCreateRenderPass(device, &renderPassInfo, nullptr, &renderPass) != VK_SUCCESS) {
     477      throw runtime_error("failed to create render pass!");
     478   }
     479}
     480
     481VkFormat VulkanGame::findDepthFormat() {
     482   return VulkanUtils::findSupportedFormat(
     483      physicalDevice,
     484      { VK_FORMAT_D32_SFLOAT, VK_FORMAT_D32_SFLOAT_S8_UINT, VK_FORMAT_D24_UNORM_S8_UINT },
     485      VK_IMAGE_TILING_OPTIMAL,
     486      VK_FORMAT_FEATURE_DEPTH_STENCIL_ATTACHMENT_BIT
     487   );
     488}
     489
    421490void VulkanGame::cleanupSwapChain() {
     491   vkDestroyRenderPass(device, renderPass, nullptr);
     492
    422493   for (auto imageView : swapChainImageViews) {
    423494      vkDestroyImageView(device, imageView, nullptr);
  • vulkan-game.hpp

    rf94eea9 r6fc24c7  
    3838      VkExtent2D swapChainExtent;
    3939      vector<VkImageView> swapChainImageViews;
     40      VkRenderPass renderPass;
    4041
    4142      bool initWindow(int width, int height, unsigned char guiFlags);
     
    5758      void createSwapChain();
    5859      void createImageViews();
     60      void createRenderPass();
     61      VkFormat findDepthFormat();
    5962
    6063      void cleanupSwapChain();
  • vulkan-ref.cpp

    rf94eea9 r6fc24c7  
    177177      vector<VkFramebuffer> swapChainFramebuffers;
    178178
     179/*** START OF REFACTORED CODE ***/
    179180      VkRenderPass renderPass;
     181/*** END OF REFACTORED CODE ***/
    180182
    181183      VkCommandPool commandPool;
     
    319321         createSwapChain();
    320322         createImageViews();
     323         createRenderPass();
    321324/*** END OF REFACTORED CODE ***/
    322          createRenderPass();
    323325
    324326         createCommandPool();
     
    734736         }
    735737      }
    736 /*** END OF REFACTORED CODE ***/
    737738
    738739      void createRenderPass() {
     
    793794         }
    794795      }
     796/*** END OF REFACTORED CODE ***/
    795797
    796798      void initGraphicsPipelineInfo(GraphicsPipelineInfo& info,
     
    10801082      }
    10811083
     1084/*** START OF REFACTORED CODE ***/
    10821085      VkFormat findDepthFormat() {
    10831086         return findSupportedFormat(
     
    11051108         throw runtime_error("failed to find supported format!");
    11061109      }
     1110/*** END OF REFACTORED CODE ***/
    11071111
    11081112      bool hasStencilComponent(VkFormat format) {
     
    19481952         cleanupPipeline(overlayPipeline);
    19491953
     1954/*** START OF REFACTORED CODE ***/
    19501955         vkDestroyRenderPass(device, renderPass, nullptr);
    19511956
    1952 /*** START OF REFACTORED CODE ***/
    19531957         for (auto imageView : swapChainImageViews) {
    19541958            vkDestroyImageView(device, imageView, nullptr);
  • vulkan-utils.cpp

    rf94eea9 r6fc24c7  
    192192   return imageView;
    193193}
     194
     195VkFormat VulkanUtils::findSupportedFormat(VkPhysicalDevice physicalDevice, const vector<VkFormat>& candidates,
     196      VkImageTiling tiling, VkFormatFeatureFlags features) {
     197   for (VkFormat format : candidates) {
     198      VkFormatProperties props;
     199      vkGetPhysicalDeviceFormatProperties(physicalDevice, format, &props);
     200
     201      if (tiling == VK_IMAGE_TILING_LINEAR &&
     202            (props.linearTilingFeatures & features) == features) {
     203         return format;
     204      } else if (tiling == VK_IMAGE_TILING_OPTIMAL &&
     205            (props.optimalTilingFeatures & features) == features) {
     206         return format;
     207      }
     208   }
     209
     210   throw runtime_error("failed to find supported format!");
     211}
  • vulkan-utils.hpp

    rf94eea9 r6fc24c7  
    4444      static VkExtent2D chooseSwapExtent(const VkSurfaceCapabilitiesKHR& capabilities, int width, int height);
    4545      static VkImageView createImageView(VkDevice device, VkImage image, VkFormat format, VkImageAspectFlags aspectFlags);
     46      static VkFormat findSupportedFormat(VkPhysicalDevice physicalDevice, const vector<VkFormat>& candidates,
     47            VkImageTiling tiling, VkFormatFeatureFlags features);
    4648};
    4749
Note: See TracChangeset for help on using the changeset viewer.