~sircmpwn/xrgears

ref: 1357a7691f8d6c04ac96499740f8db714a8bb54f xrgears/vkc/vkcSwapChainVK.hpp -rw-r--r-- 3.7 KiB
1357a769 — Lubosz Sarnecki rename Swapchain to SwapChain 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
95
96
97
98
99
100
101
102
103
104
105
106
#pragma once

#include <vulkan/vulkan.h>

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

namespace vkc {

class SwapChainVK : public vik::SwapChainVK {

public:
  VkSwapchainKHR swap_chain;
  uint32_t image_count = 0;
  uint32_t present_index = 0;

  SwapChainVK() {
  }

  ~SwapChainVK() {
  }

  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);

    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;
      }
    }

    uint32_t queueFamilyIndices[] { 0 };

    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.pQueueFamilyIndices = queueFamilyIndices;
    swapchainfo.preTransform = VK_SURFACE_TRANSFORM_IDENTITY_BIT_KHR;
    swapchainfo.compositeAlpha = VK_COMPOSITE_ALPHA_OPAQUE_BIT_KHR;
    swapchainfo.presentMode = present_mode;

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

    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);

    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);
      create_frame_buffer(device, render_pass, &buffers[i].view,
                          width, height, &buffers[i].framebuffer);
    }
  }

  VkResult aquire_next_image(VkDevice device, VkSemaphore semaphore) {
    return vkAcquireNextImageKHR(device, swap_chain, 60,
                                 semaphore, VK_NULL_HANDLE, &present_index);
  }

  void present(VkQueue queue) {
    VkSwapchainKHR swapChains[] = { swap_chain, };
    uint32_t indices[] = { present_index, };

    VkPresentInfoKHR presentInfo = {};
    presentInfo.sType = VK_STRUCTURE_TYPE_PRESENT_INFO_KHR;
    presentInfo.swapchainCount = 1;
    presentInfo.pSwapchains = swapChains;
    presentInfo.pImageIndices = indices;
    //presentInfo.pResults = &result,

    VkResult result = vkQueuePresentKHR(queue, &presentInfo);
    vik_log_f_if(result != VK_SUCCESS, "vkQueuePresentKHR failed.");
  }

};
}