~sircmpwn/xrgears

ref: f162555056b018a3805f96df49158518ec63f94c xrgears/vitamin-k/render/vikSwapChain.hpp -rw-r--r-- 2.4 KiB
f1625550 — Lubosz Sarnecki vik: Use struct and array initializers. 2 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
/*
 * vitamin-k
 *
 * Copyright 2016 Sascha Willems - www.saschawillems.de
 * Copyright 2017-2018 Collabora Ltd.
 *
 * Authors: Lubosz Sarnecki <lubosz.sarnecki@collabora.com>
 * SPDX-License-Identifier: MIT
 *
 * Based on Vulkan Examples written by Sascha Willems
 */

#pragma once

#include <vulkan/vulkan.h>

#include <vector>
#include <functional>

#include "../system/vikLog.hpp"
#include "../system/vikSettings.hpp"

namespace vik {

struct SwapChainBuffer {
  VkImage image;
  VkImageView view;
};

class SwapChain {
 public:
  VkPhysicalDevice physical_device;
  VkDevice device;
  VkInstance instance;
  std::vector<SwapChainBuffer> buffers;

  uint32_t image_count = 0;
  VkSurfaceFormatKHR surface_format;

  Settings *settings;

  std::function<void(uint32_t index)> render_cb;

  SwapChain() {}
  ~SwapChain() {}

  virtual void cleanup() = 0;

  virtual uint32_t get_queue_index() {
    return 0;
  }

  virtual void create(uint32_t width, uint32_t height) = 0;

  void set_settings(Settings *s) {
    settings = s;
  }

  void set_render_cb(std::function<void(uint32_t index)> cb) {
    render_cb = cb;
  }

  /**
  * Set instance, physical and logical device to use for the swapchain and get all required function pointers
  *
  * @param instance Vulkan instance to use
  * @param physicalDevice Physical device used to query properties and formats relevant to the swapchain
  * @param device Logical representation of the device to create the swapchain for
  *
  */
  void set_context(VkInstance i, VkPhysicalDevice p, VkDevice d) {
    instance = i;
    physical_device = p;
    device = d;
  }

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

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