~sircmpwn/xrgears

ref: a63ec76c08695cbc0735573656741035adc9fe4d xrgears/vkc/vkcSwapChainVK.hpp -rw-r--r-- 3.1 KiB
a63ec76c — Lubosz Sarnecki vkc: move frame buffers to renderer. 4 years ago
                                                                                
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
#pragma once

#include <vulkan/vulkan.h>

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

namespace vkc {

class SwapChainVK : public vik::SwapChainVK {

public:
  SwapChainVK() {
  }

  ~SwapChainVK() {
  }

  VkPresentModeKHR select_present_mode(VkPhysicalDevice physical_device,
                                       VkSurfaceKHR surface) {
    uint32_t count;
    vkGetPhysicalDeviceSurfacePresentModesKHR(physical_device, surface,
                                              &count, NULL);
    VkPresentModeKHR present_modes[count];
    vkGetPhysicalDeviceSurfacePresentModesKHR(physical_device, surface,
                                              &count, present_modes);
    int i;
    VkPresentModeKHR present_mode = VK_PRESENT_MODE_MAILBOX_KHR;
    for (i = 0; i < count; i++) {
      if (present_modes[i] == VK_PRESENT_MODE_FIFO_KHR) {
        present_mode = VK_PRESENT_MODE_FIFO_KHR;
        break;
      }
    }
    return present_mode;
  }

  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;
    swapchainfo.surface = surface;
    swapchainfo.minImageCount = 2;
    swapchainfo.imageFormat = image_format;
    swapchainfo.imageColorSpace = VK_COLORSPACE_SRGB_NONLINEAR_KHR;
    swapchainfo.imageExtent = { width, height };
    swapchainfo.imageArrayLayers = 1;
    swapchainfo.imageUsage = VK_IMAGE_USAGE_COLOR_ATTACHMENT_BIT;
    swapchainfo.imageSharingMode = VK_SHARING_MODE_EXCLUSIVE;
    swapchainfo.queueFamilyIndexCount = 1;
    swapchainfo.preTransform = VK_SURFACE_TRANSFORM_IDENTITY_BIT_KHR;
    swapchainfo.compositeAlpha = VK_COMPOSITE_ALPHA_OPAQUE_BIT_KHR;
    swapchainfo.presentMode = select_present_mode(physical_device,
                                                  surface);

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

  void destroy(VkDevice device) {
    if (image_count > 0) {
      vkDestroySwapchainKHR(device, swap_chain, NULL);
      image_count = 0;
    }
  }

  void update_images(VkDevice device, 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);
    VkImage swap_chain_images[image_count];
    vkGetSwapchainImagesKHR(device, swap_chain, &image_count, swap_chain_images);

    buffers.resize(image_count);

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

};
}