~sircmpwn/xrgears

b2dada62c0ecf137788ca06f5af5630db5a6b3e9 — Lubosz Sarnecki 4 years ago ff5d364
refactor vkc swapchain vk.
M vitamin-k/vikSwapChain.hpp => vitamin-k/vikSwapChain.hpp +5 -0
@@ 14,6 14,11 @@ struct RenderBuffer {
  VkFramebuffer framebuffer;
};

struct SwapChainBuffer {
  VkImage image;
  VkImageView view;
};

class SwapChain {
public:
  RenderBuffer buffers[MAX_NUM_IMAGES];

M vitamin-k/vikSwapChainVK.hpp => vitamin-k/vikSwapChainVK.hpp +1 -0
@@ 9,6 9,7 @@ class SwapChainVK : public SwapChain {
public:

  VkSwapchainKHR swap_chain = VK_NULL_HANDLE;
  uint32_t image_count = 0;

  SwapChainVK() {}
  ~SwapChainVK() {}

M vkc/vkcSwapChainVK.hpp => vkc/vkcSwapChainVK.hpp +26 -14
@@ 10,8 10,6 @@ namespace vkc {
class SwapChainVK : public vik::SwapChainVK {

public:
  uint32_t image_count = 0;

  SwapChainVK() {
  }



@@ 20,16 18,12 @@ public:

  void init(VkDevice device, VkPhysicalDevice physical_device, VkSurfaceKHR surface,
            VkFormat image_format, uint32_t width, uint32_t height, VkRenderPass render_pass) {
    VkSurfaceCapabilitiesKHR surface_caps;
    vkGetPhysicalDeviceSurfaceCapabilitiesKHR(physical_device, surface,
                                              &surface_caps);
    assert(surface_caps.supportedCompositeAlpha &
           VK_COMPOSITE_ALPHA_OPAQUE_BIT_KHR);

    VkBool32 supported;
    vkGetPhysicalDeviceSurfaceSupportKHR(physical_device, 0, surface, &supported);
    assert(supported);
    create(device, physical_device, surface, image_format, width, height);
    update_swap_chain_images(device, width, height, render_pass, image_format);
  }

  VkPresentModeKHR select_present_mode(VkPhysicalDevice physical_device,
                                       VkSurfaceKHR surface) {
    uint32_t count;
    vkGetPhysicalDeviceSurfacePresentModesKHR(physical_device, surface,
                                              &count, NULL);


@@ 44,8 38,21 @@ public:
        break;
      }
    }
    return present_mode;
  }

    uint32_t queueFamilyIndices[] { 0 };
  void create(VkDevice device, VkPhysicalDevice physical_device,
              VkSurfaceKHR surface, VkFormat image_format,
              uint32_t width, uint32_t height) {
    VkSurfaceCapabilitiesKHR surface_caps;
    vkGetPhysicalDeviceSurfaceCapabilitiesKHR(physical_device, surface,
                                              &surface_caps);
    assert(surface_caps.supportedCompositeAlpha &
           VK_COMPOSITE_ALPHA_OPAQUE_BIT_KHR);

    VkBool32 supported;
    vkGetPhysicalDeviceSurfaceSupportKHR(physical_device, 0, surface, &supported);
    assert(supported);

    VkSwapchainCreateInfoKHR swapchainfo = {};
    swapchainfo.sType = VK_STRUCTURE_TYPE_SWAPCHAIN_CREATE_INFO_KHR;


@@ 58,13 65,18 @@ public:
    swapchainfo.imageUsage = VK_IMAGE_USAGE_COLOR_ATTACHMENT_BIT;
    swapchainfo.imageSharingMode = VK_SHARING_MODE_EXCLUSIVE;
    swapchainfo.queueFamilyIndexCount = 1;
    swapchainfo.pQueueFamilyIndices = queueFamilyIndices;
    swapchainfo.pQueueFamilyIndices = { 0 };
    swapchainfo.preTransform = VK_SURFACE_TRANSFORM_IDENTITY_BIT_KHR;
    swapchainfo.compositeAlpha = VK_COMPOSITE_ALPHA_OPAQUE_BIT_KHR;
    swapchainfo.presentMode = present_mode;
    swapchainfo.presentMode = select_present_mode(physical_device,
                                                  surface);

    vkCreateSwapchainKHR(device, &swapchainfo, NULL, &swap_chain);
  }

  void update_swap_chain_images(VkDevice device, uint32_t width,
                                uint32_t height, VkRenderPass render_pass,
                                VkFormat image_format) {
    vkGetSwapchainImagesKHR(device, swap_chain, &image_count, NULL);
    assert(image_count > 0);
    vik_log_d("Creating swap chain with %d images.", image_count);

M vks/vksRenderer.cpp => vks/vksRenderer.cpp +3 -3
@@ 123,9 123,9 @@ bool Renderer::checkCommandBuffers() {
void Renderer::createCommandBuffers() {
  // Create one command buffer for each swap chain image and reuse for rendering

  vik_log_d("Swapchain image count %d", swapChain.imageCount);
  vik_log_d("Swapchain image count %d", swapChain.image_count);

  drawCmdBuffers.resize(swapChain.imageCount);
  drawCmdBuffers.resize(swapChain.image_count);

  VkCommandBufferAllocateInfo cmdBufAllocateInfo =
      vks::initializers::commandBufferAllocateInfo(


@@ 479,7 479,7 @@ void Renderer::setupFrameBuffer() {
  frameBufferCreateInfo.layers = 1;

  // Create frame buffers for every swap chain image
  frameBuffers.resize(swapChain.imageCount);
  frameBuffers.resize(swapChain.image_count);
  for (uint32_t i = 0; i < frameBuffers.size(); i++) {
    attachments[0] = swapChain.buffers[i].view;
    vik_log_check(vkCreateFramebuffer(device, &frameBufferCreateInfo, nullptr, &frameBuffers[i]));

M vks/vksSwapChain.hpp => vks/vksSwapChain.hpp +9 -18
@@ 28,11 28,6 @@

namespace vks {

typedef struct _SwapChainBuffers {
  VkImage image;
  VkImageView view;
} SwapChainBuffer;

class SwapChain : public vik::SwapChainVK {
 public:
  VkSurfaceKHR surface;


@@ 47,9 42,8 @@ class SwapChain : public vik::SwapChainVK {
  VkColorSpaceKHR colorSpace;
  /** @brief Handle to the current swap chain, required for recreation */

  uint32_t imageCount;
  std::vector<VkImage> images;
  std::vector<SwapChainBuffer> buffers;
  std::vector<vik::SwapChainBuffer> buffers;
  // Index of the deteced graphics and presenting device queue
  /** @brief Queue family index of the detected graphics and presenting device queue */
  uint32_t queueNodeIndex = UINT32_MAX;


@@ 198,7 192,7 @@ class SwapChain : public vik::SwapChainVK {

    // Get available present modes
    uint32_t presentModeCount;
    vik_log_check(vkGetPhysicalDeviceSurfacePresentModesKHR(physicalDevice,surface, &presentModeCount, NULL));
    vik_log_check(vkGetPhysicalDeviceSurfacePresentModesKHR(physicalDevice, surface, &presentModeCount, NULL));
    assert(presentModeCount > 0);

    std::vector<VkPresentModeKHR> presentModes(presentModeCount);


@@ 256,21 250,21 @@ class SwapChain : public vik::SwapChainVK {
  }

  void destroy_swap_chain(const VkSwapchainKHR &sc) {
    for (uint32_t i = 0; i < imageCount; i++)
    for (uint32_t i = 0; i < image_count; i++)
      vkDestroyImageView(device, buffers[i].view, nullptr);
    vkDestroySwapchainKHR(device, sc, nullptr);
  }

  void update_swap_chain_images() {
    vik_log_check(vkGetSwapchainImagesKHR(device, swap_chain, &imageCount, NULL));
    vik_log_check(vkGetSwapchainImagesKHR(device, swap_chain, &image_count, NULL));

    // Get the swap chain images
    images.resize(imageCount);
    vik_log_check(vkGetSwapchainImagesKHR(device, swap_chain, &imageCount, images.data()));
    images.resize(image_count);
    vik_log_check(vkGetSwapchainImagesKHR(device, swap_chain, &image_count, images.data()));

    // Get the swap chain buffers containing the image and imageview
    buffers.resize(imageCount);
    for (uint32_t i = 0; i < imageCount; i++) {
    buffers.resize(image_count);
    for (uint32_t i = 0; i < image_count; i++) {
      buffers[i].image = images[i];
      create_image_view(device, images[i], colorFormat, &buffers[i].view);
    }


@@ 329,15 323,12 @@ class SwapChain : public vik::SwapChainVK {
    update_swap_chain_images();
  }




  /**
  * Destroy and free Vulkan resources used for the swapchain
  */
  void cleanup() {
    if (swap_chain != VK_NULL_HANDLE) {
      for (uint32_t i = 0; i < imageCount; i++)
      for (uint32_t i = 0; i < image_count; i++)
        vkDestroyImageView(device, buffers[i].view, nullptr);
    }