summaryrefslogtreecommitdiff
path: root/src/vk/swapchain.cpp
blob: 678279e187ff2631acbdbba2d2266604eb17870a (plain)
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
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
/*
 * Copyright 2022 Frederico de Oliveira Linhares
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

#include "swapchain.hpp"

#include "../core.hpp"

#include <vector>

namespace
{

void
load_swapchain(void *obj)
{
  auto self = static_cast<VK::Swapchain*>(obj);

  // Surface formats.
  uint32_t vk_surface_format_count;
  std::vector<VkSurfaceFormatKHR> vk_surface_formats;
  vkGetPhysicalDeviceSurfaceFormatsKHR(
    cg_core.vk_device_with_swapchain->physical_device, cg_core.window_surface,
    &vk_surface_format_count, nullptr);
  vk_surface_formats.resize(vk_surface_format_count);
  vkGetPhysicalDeviceSurfaceFormatsKHR(
    cg_core.vk_device_with_swapchain->physical_device, cg_core.window_surface,
    &vk_surface_format_count, vk_surface_formats.data());

  VkSwapchainCreateInfoKHR swapchain_create_info = {};
  swapchain_create_info.sType = VK_STRUCTURE_TYPE_SWAPCHAIN_CREATE_INFO_KHR;
  swapchain_create_info.pNext = nullptr;
  swapchain_create_info.flags = 0;
  swapchain_create_info.surface = cg_core.window_surface;
  swapchain_create_info.minImageCount = 3; // triple buffering.

  self->image_format = vk_surface_formats[0].format;
  swapchain_create_info.imageFormat = self->image_format;
  swapchain_create_info.imageColorSpace = vk_surface_formats[0].colorSpace;

  swapchain_create_info.imageExtent = {
    cg_core.screen_width, cg_core.screen_height};
  swapchain_create_info.imageArrayLayers = 1;
  swapchain_create_info.imageUsage = VK_IMAGE_USAGE_COLOR_ATTACHMENT_BIT;
  swapchain_create_info.imageSharingMode = VK_SHARING_MODE_EXCLUSIVE;
  swapchain_create_info.queueFamilyIndexCount = 0;
  swapchain_create_info.pQueueFamilyIndices = nullptr;
  swapchain_create_info.preTransform = VK_SURFACE_TRANSFORM_IDENTITY_BIT_KHR;
  swapchain_create_info.compositeAlpha = VK_COMPOSITE_ALPHA_OPAQUE_BIT_KHR;
  swapchain_create_info.presentMode = VK_PRESENT_MODE_FIFO_KHR;
  swapchain_create_info.clipped = VK_FALSE;
  swapchain_create_info.oldSwapchain = VK_NULL_HANDLE;

  if(vkCreateSwapchainKHR(
       cg_core.vk_device_with_swapchain->device, &swapchain_create_info,
       nullptr, &self->swapchain) != VK_SUCCESS)
    throw CommandError{"Vulkan failed to create swapchain."};

  vkGetSwapchainImagesKHR(
    cg_core.vk_device_with_swapchain->device, self->swapchain,
    &self->images_count, nullptr);
  self->images = new VkImage[self->images_count];
  vkGetSwapchainImagesKHR(
    cg_core.vk_device_with_swapchain->device, self->swapchain,
    &self->images_count, self->images);
}

void
unload_swapchain(void *obj)
{
  auto self = static_cast<VK::Swapchain*>(obj);

  delete[] self->images;
  vkDestroySwapchainKHR(
    cg_core.vk_device_with_swapchain->device, self->swapchain, nullptr);
}

void
load_image_view(void *obj)
{
  auto self = static_cast<VK::Swapchain*>(obj);

  self->image_views = new VkImageView[self->images_count];
  for(auto i{0}; i < self->images_count; i++)
  {
    VkImageViewCreateInfo create_info = {};
    create_info.sType = VK_STRUCTURE_TYPE_IMAGE_VIEW_CREATE_INFO;
    create_info.image = self->images[i];
    create_info.viewType = VK_IMAGE_VIEW_TYPE_2D;
    create_info.format = self->image_format;
    create_info.components.r = VK_COMPONENT_SWIZZLE_IDENTITY;
    create_info.components.g = VK_COMPONENT_SWIZZLE_IDENTITY;
    create_info.components.b = VK_COMPONENT_SWIZZLE_IDENTITY;
    create_info.components.a = VK_COMPONENT_SWIZZLE_IDENTITY;
    create_info.subresourceRange.aspectMask = VK_IMAGE_ASPECT_COLOR_BIT;
    create_info.subresourceRange.baseMipLevel = 0;
    create_info.subresourceRange.levelCount = 1;
    create_info.subresourceRange.baseArrayLayer = 0;
    create_info.subresourceRange.layerCount = 1;

    if(vkCreateImageView(
	 cg_core.vk_device_with_swapchain->device, &create_info, nullptr,
	 &self->image_views[i]))
      throw CommandError{"Could no create Image View for swapchain."};
  }
}

void
unload_image_view(void *obj)
{
  auto self = static_cast<VK::Swapchain*>(obj);

  for(auto i{0}; i < self->images_count; i++)
    vkDestroyImageView(
      cg_core.vk_device_with_swapchain->device, self->image_views[i], nullptr);
}

const CommandChain loader{
  {&load_swapchain, &unload_swapchain},
  {&load_image_view, &unload_image_view}
};

}

namespace VK
{

Swapchain::Swapchain()
{
  loader.execute(this);
}

Swapchain::~Swapchain()
{
  loader.revert(this);
}

}