-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathvkc_context.cpp
101 lines (80 loc) · 2.46 KB
/
vkc_context.cpp
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
#include "vkc_context.h"
/**
* Initialize with empty fields.
*/
VkcContext::VkcContext()
{
surface = VK_NULL_HANDLE;
instance = VK_NULL_HANDLE;
}
/**
* Create the context.
*/
VkcContext::VkcContext(uint32_t id, const VkcDevice *device, const VkInstance instance) : VkcContext()
{
this->instance = instance;
this->device = device;
createSurface(id);
getCommandChains();
swapchain = new VkcSwapchain(surface, device);
pipeline = new VkcPipeline(swapchain, device);
}
/**
* Destroy the context.
*/
VkcContext::~VkcContext()
{
if (pipeline != nullptr)
delete pipeline;
if (swapchain != nullptr)
delete swapchain;
if (surface != VK_NULL_HANDLE)
vkDestroySurfaceKHR(instance, surface, nullptr);
}
/**
* Creates the surface on which Vulkan will draw to.
*/
void VkcContext::createSurface(uint64_t id)
{
// Fill surface info.
VkWin32SurfaceCreateInfoKHR surfaceInfo =
{
VK_STRUCTURE_TYPE_WIN32_SURFACE_CREATE_INFO_KHR, // VkStructureType sType;
nullptr, // const void* pNext;
0, // VkWin32SurfaceCreateFlagsKHR flags;
nullptr, // HINSTANCE hinstance;
(HWND)id // HWND hwnd;
};
// Create surface.
vkCreateWin32SurfaceKHR(instance, &surfaceInfo, nullptr, &surface);
}
/**
* Get the queue and command buffer pairs compatible with the surface.
*/
void VkcContext::getCommandChains()
{
for (int i = 0; i < device->queueFamilies.size(); i++)
{
VkcQueueFamily family = device->queueFamilies[i];
VkBool32 ok;
vkGetPhysicalDeviceSurfaceSupportKHR(device->physical, family.index, surface, &ok);
if (ok)
for (int j = 0; j < family.queues.size(); j++)
commandChain.append({family.queues[j], family.commandBuffers[j]});
}
}
/**
* Resize the swapchain.
*/
void VkcContext::resize()
{
if (swapchain != nullptr)
{
// Destroy swapchain.
VkRenderPass oldRenderPass = swapchain->renderPass;
swapchain->renderPass = VK_NULL_HANDLE;
delete swapchain;
// Recreate swapchain using old render pass.
swapchain = new VkcSwapchain(oldRenderPass, surface, device);
}
}