~sircmpwn/xrgears

799fd07f97c023c8d2516679c69baf6e8228b387 — Lubosz Sarnecki 4 years ago 57aacf2
refactor swap chain code.
M vitamin-k/vikSwapchain.hpp => vitamin-k/vikSwapchain.hpp +50 -2
@@ 1,9 1,57 @@
#pragma once

#include <vulkan/vulkan.h>

#include "../vks/vksLog.hpp"

namespace vik {
class SwapChain {
public:
    SwapChain() {}
    ~SwapChain() {}
  SwapChain() {}
  ~SwapChain() {}

  void create_image_view(const VkDevice &device, const VkImage& image,
                         const VkFormat &format, VkImageView *view) {
    VkImageViewCreateInfo view_create_info = {};
    view_create_info.sType = VK_STRUCTURE_TYPE_IMAGE_VIEW_CREATE_INFO;
    view_create_info.pNext = NULL;
    view_create_info.format = format;
    view_create_info.components = {
      VK_COMPONENT_SWIZZLE_R,
      VK_COMPONENT_SWIZZLE_G,
      VK_COMPONENT_SWIZZLE_B,
      VK_COMPONENT_SWIZZLE_A
    };
    view_create_info.subresourceRange.aspectMask = VK_IMAGE_ASPECT_COLOR_BIT;
    view_create_info.subresourceRange.baseMipLevel = 0;
    view_create_info.subresourceRange.levelCount = 1;
    view_create_info.subresourceRange.baseArrayLayer = 0;
    view_create_info.subresourceRange.layerCount = 1;
    view_create_info.viewType = VK_IMAGE_VIEW_TYPE_2D;
    view_create_info.flags = 0;
    view_create_info.image = image;

    vik_log_check(vkCreateImageView(device, &view_create_info, nullptr, view));
  }

  void create_frame_buffer(const VkDevice &device,
                           const VkRenderPass &render_pass, VkImageView *view,
                           uint32_t width, uint32_t height,
                           VkFramebuffer *frame_buffer) {
    VkFramebufferCreateInfo framebufferinfo = {};
    framebufferinfo.sType = VK_STRUCTURE_TYPE_FRAMEBUFFER_CREATE_INFO;
    framebufferinfo.renderPass = render_pass;
    framebufferinfo.attachmentCount = 1;
    framebufferinfo.pAttachments = view;
    framebufferinfo.width = width;
    framebufferinfo.height = height;
    framebufferinfo.layers = 1;

    vkCreateFramebuffer(device,
                        &framebufferinfo,
                        NULL,
                        frame_buffer);
  }

};
}

M vkc/vkcSwapChain.hpp => vkc/vkcSwapChain.hpp +4 -39
@@ 21,47 21,12 @@ public:
  SwapChain() {}
  ~SwapChain() {}

  /*
  void init_buffer(VkDevice device, VkFormat image_format, VkRenderPass render_pass,
                   uint32_t width, uint32_t height, RenderBuffer *b) {

    VkImageSubresourceRange subresource_range;
    subresource_range.aspectMask = VK_IMAGE_ASPECT_COLOR_BIT;
    subresource_range.baseMipLevel = 0;
    subresource_range.levelCount = 1;
    subresource_range.baseArrayLayer = 0;
    subresource_range.layerCount = 1;

    VkImageViewCreateInfo imageviewinfo;
    imageviewinfo.sType = VK_STRUCTURE_TYPE_IMAGE_VIEW_CREATE_INFO;
    imageviewinfo.image = b->image;
    imageviewinfo.viewType = VK_IMAGE_VIEW_TYPE_2D;
    imageviewinfo.format = image_format;
    imageviewinfo.components = {
        VK_COMPONENT_SWIZZLE_R,
        VK_COMPONENT_SWIZZLE_G,
        VK_COMPONENT_SWIZZLE_B,
        VK_COMPONENT_SWIZZLE_A
    };
    imageviewinfo.subresourceRange = subresource_range;

    vkCreateImageView(device,
                      &imageviewinfo,
                      NULL,
                      &b->view);

    VkFramebufferCreateInfo framebufferinfo;
    framebufferinfo.sType = VK_STRUCTURE_TYPE_FRAMEBUFFER_CREATE_INFO;
    framebufferinfo.renderPass = render_pass;
    framebufferinfo.attachmentCount = 1;
    framebufferinfo.pAttachments = &b->view;
    framebufferinfo.width = width;
    framebufferinfo.height = height;
    framebufferinfo.layers = 1;

    vkCreateFramebuffer(device,
                        &framebufferinfo,
                        NULL,
                        &b->framebuffer);
    create_image_view(device, b->image, image_format, &b->view);
    create_frame_buffer(device, render_pass, &b->view, width, height, &b->framebuffer);
  }
  */
};
}

M vkc/vkcSwapChainDRM.hpp => vkc/vkcSwapChainDRM.hpp +4 -2
@@ 93,8 93,10 @@ public:
                          pitches, offsets, &kms_b->fb, 0);
      vik_log_f_if(ret == -1, "addfb2 failed");

      init_buffer(device, image_format, render_pass,
                  width, height, b);
      create_image_view(device, b->image,
                        image_format, &b->view);
      create_frame_buffer(device, render_pass, &b->view,
                          width, height, &b->framebuffer);
    }
  }


M vkc/vkcSwapChainVK.hpp => vkc/vkcSwapChainVK.hpp +4 -2
@@ 75,8 75,10 @@ public:

    for (uint32_t i = 0; i < image_count; i++) {
      buffers[i].image = swap_chain_images[i];
      init_buffer(device, image_format, render_pass,
                  width, height, &buffers[i]);
      create_image_view(device, buffers[i].image,
                        image_format, &buffers[i].view);
      create_frame_buffer(device, render_pass, &buffers[i].view,
                          width, height, &buffers[i].framebuffer);
    }
  }


M vks/vksSwapChain.hpp => vks/vksSwapChain.hpp +1 -24
@@ 261,29 261,6 @@ class SwapChain : public vik::SwapChain {
    vkDestroySwapchainKHR(device, sc, nullptr);
  }

  void create_image_view(const VkImage& image, VkImageView *view) {
    VkImageViewCreateInfo colorAttachmentView = {};
    colorAttachmentView.sType = VK_STRUCTURE_TYPE_IMAGE_VIEW_CREATE_INFO;
    colorAttachmentView.pNext = NULL;
    colorAttachmentView.format = colorFormat;
    colorAttachmentView.components = {
      VK_COMPONENT_SWIZZLE_R,
      VK_COMPONENT_SWIZZLE_G,
      VK_COMPONENT_SWIZZLE_B,
      VK_COMPONENT_SWIZZLE_A
    };
    colorAttachmentView.subresourceRange.aspectMask = VK_IMAGE_ASPECT_COLOR_BIT;
    colorAttachmentView.subresourceRange.baseMipLevel = 0;
    colorAttachmentView.subresourceRange.levelCount = 1;
    colorAttachmentView.subresourceRange.baseArrayLayer = 0;
    colorAttachmentView.subresourceRange.layerCount = 1;
    colorAttachmentView.viewType = VK_IMAGE_VIEW_TYPE_2D;
    colorAttachmentView.flags = 0;
    colorAttachmentView.image = image;

    vik_log_check(vkCreateImageView(device, &colorAttachmentView, nullptr, view));
  }

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



@@ 295,7 272,7 @@ class SwapChain : public vik::SwapChain {
    buffers.resize(imageCount);
    for (uint32_t i = 0; i < imageCount; i++) {
      buffers[i].image = images[i];
      create_image_view(images[i], &buffers[i].view);
      create_image_view(device, images[i], colorFormat, &buffers[i].view);
    }
  }