summaryrefslogtreecommitdiff
path: root/src/vulkan_swapchain.cpp
blob: 651b5585c6275ad87c94fb037c765b5dc5df6574 (plain) (blame)
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
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
#include "vulkan_swapchain.h"

#include "GLFW/glfw3.h"
#include "imgui_impl_glfw.h"
#include "imgui_impl_vulkan.h"
#include "vulkan/vulkan_core.h"

#include <cstdint>
#include <cstdlib>
#include <vector>
#include <iostream>
#include <string>

std::vector<std::string> get_glfw_instance_extensions() {
    uint32_t extension_count = 0;
    const char **extensions = glfwGetRequiredInstanceExtensions(&extension_count);

    std::vector<std::string> result;
    result.reserve(extension_count);
    for (uint32_t i = 0; i < extension_count; i++) {
        result.push_back(extensions[i]);
    }
    return result;
}

namespace iris {

Swapchain::Swapchain(
    GLFWwindow *window,
    iris::Device device) : device(device), window(window)
{
    if (!glfwVulkanSupported()) {
        std::cerr << "GLFW failed to find Vulkan support" << std::endl;
        // TODO throw an exception
        abort();
    }

    // Create the surface
    CHECK_VULKAN(glfwCreateWindowSurface(
        device.instance,
        window,
        VK_NULL_HANDLE,
        &surface));

    // Optionally check surface capabilities
    {
        VkBool32 supported = VK_FALSE;
        CHECK_VULKAN(vkGetPhysicalDeviceSurfaceSupportKHR(
            device.physical_device,
            device.main_queue_family_index,
            surface,
            &supported));
        if (supported != VK_TRUE) {
            // TODO throw an exception
            std::cerr << "Surface does not support presentation" << std::endl;
            abort();
        }  

        VkSurfaceCapabilitiesKHR surface_capabilities;
        CHECK_VULKAN(vkGetPhysicalDeviceSurfaceCapabilitiesKHR(
            device.physical_device,
            surface,
            &surface_capabilities));

        if (surface_capabilities.maxImageCount < SWAPCHAIN_IMAGE_COUNT) {
            // TODO throw an exception
            std::cerr << "Surface does not support enough images" << std::endl;
            abort();
        }
    }
    
    // Create render pass
    VkAttachmentDescription2 attachment = {
        .sType = VK_STRUCTURE_TYPE_ATTACHMENT_DESCRIPTION_2,
        .format = VK_FORMAT_B8G8R8A8_UNORM,
        .samples = VK_SAMPLE_COUNT_1_BIT,
        .loadOp = VK_ATTACHMENT_LOAD_OP_LOAD,
        .storeOp = VK_ATTACHMENT_STORE_OP_STORE,
        .stencilLoadOp = VK_ATTACHMENT_LOAD_OP_DONT_CARE,
        .stencilStoreOp = VK_ATTACHMENT_STORE_OP_DONT_CARE,
        .initialLayout = VK_IMAGE_LAYOUT_COLOR_ATTACHMENT_OPTIMAL,
        .finalLayout = VK_IMAGE_LAYOUT_PRESENT_SRC_KHR,
    };
    VkAttachmentReference2 color_attachment = {
        .sType = VK_STRUCTURE_TYPE_ATTACHMENT_REFERENCE_2,
        .attachment = 0,
        .layout = VK_IMAGE_LAYOUT_COLOR_ATTACHMENT_OPTIMAL,
    };
    VkSubpassDescription2 subpass = {
        .sType = VK_STRUCTURE_TYPE_SUBPASS_DESCRIPTION_2,
        .pipelineBindPoint = VK_PIPELINE_BIND_POINT_GRAPHICS,
        .colorAttachmentCount = 1,
        .pColorAttachments = &color_attachment,
    };
    VkSubpassDependency2 dependencies[2] = {
        {
            .sType = VK_STRUCTURE_TYPE_SUBPASS_DEPENDENCY_2,
            .srcSubpass = VK_SUBPASS_EXTERNAL,
            .dstSubpass = 0,
            .srcStageMask = VK_PIPELINE_STAGE_TRANSFER_BIT | VK_PIPELINE_STAGE_COLOR_ATTACHMENT_OUTPUT_BIT,
            .dstStageMask = VK_PIPELINE_STAGE_COLOR_ATTACHMENT_OUTPUT_BIT,
            .srcAccessMask = VK_ACCESS_MEMORY_WRITE_BIT,
            .dstAccessMask = VK_ACCESS_MEMORY_READ_BIT | VK_ACCESS_MEMORY_WRITE_BIT,
        },
        {
            .sType = VK_STRUCTURE_TYPE_SUBPASS_DEPENDENCY_2,
            .srcSubpass = 0,
            .dstSubpass = VK_SUBPASS_EXTERNAL,
            .srcStageMask = VK_PIPELINE_STAGE_COLOR_ATTACHMENT_OUTPUT_BIT,
            .dstStageMask = VK_PIPELINE_STAGE_TRANSFER_BIT | VK_PIPELINE_STAGE_COLOR_ATTACHMENT_OUTPUT_BIT,
            .srcAccessMask = VK_ACCESS_MEMORY_WRITE_BIT,
            .dstAccessMask = VK_ACCESS_MEMORY_READ_BIT | VK_ACCESS_MEMORY_WRITE_BIT,
        },
    };

    VkRenderPassCreateInfo2 render_pass_create_info = {
        .sType = VK_STRUCTURE_TYPE_RENDER_PASS_CREATE_INFO_2,
        .attachmentCount = 1,
        .pAttachments = &attachment,
        .subpassCount = 1,
        .pSubpasses = &subpass,
        .dependencyCount = 2,
        .pDependencies = dependencies,
    };
    CHECK_VULKAN(vkCreateRenderPass2(
        device.device,
        &render_pass_create_info,
        nullptr,
        &render_pass));

    // Create swapchain and image/imageview/framebuffers
    // TODO: move this into `resize` to support resizing
    {
        // swapchain
        int i_width, i_height;
        glfwGetFramebufferSize(window, &i_width, &i_height);
        width = (uint32_t) i_width;
        height = (uint32_t) i_height;

        VkSwapchainCreateInfoKHR swapchain_create_info = {
            .sType = VK_STRUCTURE_TYPE_SWAPCHAIN_CREATE_INFO_KHR,
            .surface = surface,
            .minImageCount = SWAPCHAIN_IMAGE_COUNT,
            .imageFormat = VK_FORMAT_B8G8R8A8_UNORM,
            .imageColorSpace = VK_COLOR_SPACE_SRGB_NONLINEAR_KHR,
            .imageExtent = {width, height},
            .imageArrayLayers = 1,
            .imageUsage = VK_IMAGE_USAGE_COLOR_ATTACHMENT_BIT | VK_IMAGE_USAGE_TRANSFER_DST_BIT,
            .imageSharingMode = VK_SHARING_MODE_EXCLUSIVE,
            .preTransform = VK_SURFACE_TRANSFORM_IDENTITY_BIT_KHR,
            .compositeAlpha = VK_COMPOSITE_ALPHA_OPAQUE_BIT_KHR,
            .presentMode = VK_PRESENT_MODE_IMMEDIATE_KHR,
            .clipped = VK_TRUE,
            .oldSwapchain = VK_NULL_HANDLE,
        };
        CHECK_VULKAN(vkCreateSwapchainKHR(
            device.device,
            &swapchain_create_info,
            nullptr,
            &swapchain));

        // images
        uint32_t image_count = 0;
        CHECK_VULKAN(vkGetSwapchainImagesKHR(
            device.device,
            swapchain,
            &image_count,
            nullptr));
        if (image_count > SWAPCHAIN_IMAGE_COUNT) {
            // TODO throw an exception
            std::cerr << "Swapchain image count is greater than expected" << std::endl;
            abort();
        }
        CHECK_VULKAN(vkGetSwapchainImagesKHR(
            device.device,
            swapchain,
            &image_count,
            swapchain_images));

        // image views
        for (uint32_t i = 0; i < image_count; i++) {
            VkImageViewCreateInfo image_view_create_info = {
                .sType = VK_STRUCTURE_TYPE_IMAGE_VIEW_CREATE_INFO,
                .image = swapchain_images[i],
                .viewType = VK_IMAGE_VIEW_TYPE_2D,
                .format = VK_FORMAT_B8G8R8A8_UNORM,
                .components = {
                    .r = VK_COMPONENT_SWIZZLE_IDENTITY,
                    .g = VK_COMPONENT_SWIZZLE_IDENTITY,
                    .b = VK_COMPONENT_SWIZZLE_IDENTITY,
                    .a = VK_COMPONENT_SWIZZLE_IDENTITY,
                },
                .subresourceRange = {
                    .aspectMask = VK_IMAGE_ASPECT_COLOR_BIT,
                    .baseMipLevel = 0,
                    .levelCount = 1,
                    .baseArrayLayer = 0,
                    .layerCount = 1,
                },
            };

            CHECK_VULKAN(vkCreateImageView(
                device.device,
                &image_view_create_info,
                nullptr,
                &swapchain_image_views[i]));
        }

        // framebuffers
        for (uint32_t i = 0; i < image_count; i++) {
            VkFramebufferCreateInfo frame_buffer_create_info = {
                .sType = VK_STRUCTURE_TYPE_FRAMEBUFFER_CREATE_INFO,
                .renderPass = render_pass,
                .attachmentCount = 1,
                .pAttachments = &swapchain_image_views[i],
                .width = width,
                .height = height,
                .layers = 1,
            };
            CHECK_VULKAN(vkCreateFramebuffer(
                device.device,
                &frame_buffer_create_info,
                nullptr,
                &framebuffers[i]));
        }
    }


    // Create descriptor pool
    VkDescriptorPoolSize pool_size = {
        .type = VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER,
        .descriptorCount = 1,
    };
    VkDescriptorPoolCreateInfo pool_info = {
        .sType = VK_STRUCTURE_TYPE_DESCRIPTOR_POOL_CREATE_INFO,
        .maxSets = 1,
        .poolSizeCount = 1,
        .pPoolSizes = &pool_size,
    };
    CHECK_VULKAN(vkCreateDescriptorPool(
        device.device,
        &pool_info,
        nullptr,
        &descriptor_pool));

    // initialize ImGui
    ImGui_ImplGlfw_InitForVulkan(window, true);
    ImGui_ImplVulkan_InitInfo init_info = {
        .Instance = device.instance,
        .PhysicalDevice = device.physical_device,
        .Device = device.device,
        .QueueFamily = device.main_queue_family_index,
        .Queue = device.graphics_queue,
        .DescriptorPool = descriptor_pool,
        .MinImageCount = SWAPCHAIN_IMAGE_COUNT,
        .ImageCount = SWAPCHAIN_IMAGE_COUNT,
        .MSAASamples = VK_SAMPLE_COUNT_1_BIT,

        // optional 
        .CheckVkResultFn = [](const VkResult err) { CHECK_VULKAN(err); },
    };
    ImGui_ImplVulkan_Init(&init_info);
}

Swapchain::~Swapchain() {
    ImGui_ImplVulkan_Shutdown();

    for (uint32_t i = 0; i < SWAPCHAIN_IMAGE_COUNT; i++) {
        vkDestroyFramebuffer(device.device, framebuffers[i], nullptr);
        vkDestroyImageView(device.device, swapchain_image_views[i], nullptr);
    }

    vkDestroyDescriptorPool(device.device, descriptor_pool, nullptr);
    vkDestroySwapchainKHR(device.device, swapchain, nullptr);
    vkDestroyRenderPass(device.device, render_pass, nullptr);
    vkDestroySurfaceKHR(device.instance, surface, nullptr);
}

} // namespace iris