1
1
#include " SwapchainInfoVk.h"
2
2
3
3
#include " config/CemuConfig.h"
4
+ #include " gui/guiWrapper.h"
4
5
#include " Cafe/HW/Latte/Core/Latte.h"
5
6
#include " Cafe/HW/Latte/Core/LatteTiming.h"
6
7
#include " Cafe/HW/Latte/Renderer/Vulkan/VulkanAPI.h"
8
+ #include " Cafe/HW/Latte/Renderer/Vulkan/VulkanRenderer.h"
7
9
8
- void SwapchainInfoVk::Create (VkPhysicalDevice physicalDevice, VkDevice logicalDevice )
10
+ SwapchainInfoVk::SwapchainInfoVk ( bool mainWindow, Vector2i size) : mainWindow(mainWindow), m_desiredExtent(size )
9
11
{
10
- m_physicalDevice = physicalDevice;
11
- m_logicalDevice = logicalDevice;
12
- const auto details = QuerySwapchainSupport (surface, physicalDevice);
12
+ auto & windowHandleInfo = mainWindow ? gui_getWindowInfo ().canvas_main : gui_getWindowInfo ().canvas_pad ;
13
+ auto renderer = VulkanRenderer::GetInstance ();
14
+ m_instance = renderer->GetVkInstance ();
15
+ m_logicalDevice = renderer->GetLogicalDevice ();
16
+ m_physicalDevice = renderer->GetPhysicalDevice ();
17
+
18
+ m_surface = renderer->CreateFramebufferSurface (m_instance, windowHandleInfo);
19
+ }
20
+
21
+
22
+ SwapchainInfoVk::~SwapchainInfoVk ()
23
+ {
24
+ Cleanup ();
25
+ if (m_surface != VK_NULL_HANDLE)
26
+ vkDestroySurfaceKHR (m_instance, m_surface, nullptr );
27
+ }
28
+
29
+ void SwapchainInfoVk::Create ()
30
+ {
31
+ const auto details = QuerySwapchainSupport (m_surface, m_physicalDevice);
13
32
m_surfaceFormat = ChooseSurfaceFormat (details.formats );
14
33
m_actualExtent = ChooseSwapExtent (details.capabilities );
15
34
@@ -20,28 +39,28 @@ void SwapchainInfoVk::Create(VkPhysicalDevice physicalDevice, VkDevice logicalDe
20
39
if (image_count < 2 )
21
40
cemuLog_log (LogType::Force, " Vulkan: Swapchain image count less than 2 may cause problems" );
22
41
23
- VkSwapchainCreateInfoKHR create_info = CreateSwapchainCreateInfo (surface , details, m_surfaceFormat, image_count, m_actualExtent);
42
+ VkSwapchainCreateInfoKHR create_info = CreateSwapchainCreateInfo (m_surface , details, m_surfaceFormat, image_count, m_actualExtent);
24
43
create_info.oldSwapchain = nullptr ;
25
44
create_info.imageUsage = VK_IMAGE_USAGE_COLOR_ATTACHMENT_BIT | VK_IMAGE_USAGE_TRANSFER_DST_BIT;
26
45
27
- VkResult result = vkCreateSwapchainKHR (logicalDevice , &create_info, nullptr , &swapchain );
46
+ VkResult result = vkCreateSwapchainKHR (m_logicalDevice , &create_info, nullptr , &m_swapchain );
28
47
if (result != VK_SUCCESS)
29
48
UnrecoverableError (" Error attempting to create a swapchain" );
30
49
31
- result = vkGetSwapchainImagesKHR (logicalDevice, swapchain , &image_count, nullptr );
50
+ result = vkGetSwapchainImagesKHR (m_logicalDevice, m_swapchain , &image_count, nullptr );
32
51
if (result != VK_SUCCESS)
33
52
UnrecoverableError (" Error attempting to retrieve the count of swapchain images" );
34
53
35
54
36
55
m_swapchainImages.resize (image_count);
37
- result = vkGetSwapchainImagesKHR (logicalDevice, swapchain , &image_count, m_swapchainImages.data ());
56
+ result = vkGetSwapchainImagesKHR (m_logicalDevice, m_swapchain , &image_count, m_swapchainImages.data ());
38
57
if (result != VK_SUCCESS)
39
58
UnrecoverableError (" Error attempting to retrieve swapchain images" );
40
59
// create default renderpass
41
60
VkAttachmentDescription colorAttachment = {};
42
61
colorAttachment.format = m_surfaceFormat.format ;
43
62
colorAttachment.samples = VK_SAMPLE_COUNT_1_BIT;
44
- colorAttachment.loadOp = VK_ATTACHMENT_LOAD_OP_DONT_CARE ;
63
+ colorAttachment.loadOp = VK_ATTACHMENT_LOAD_OP_LOAD ;
45
64
colorAttachment.storeOp = VK_ATTACHMENT_STORE_OP_STORE;
46
65
colorAttachment.stencilLoadOp = VK_ATTACHMENT_LOAD_OP_DONT_CARE;
47
66
colorAttachment.stencilStoreOp = VK_ATTACHMENT_STORE_OP_DONT_CARE;
@@ -62,7 +81,7 @@ void SwapchainInfoVk::Create(VkPhysicalDevice physicalDevice, VkDevice logicalDe
62
81
renderPassInfo.pAttachments = &colorAttachment;
63
82
renderPassInfo.subpassCount = 1 ;
64
83
renderPassInfo.pSubpasses = &subpass;
65
- result = vkCreateRenderPass (logicalDevice , &renderPassInfo, nullptr , &m_swapchainRenderPass);
84
+ result = vkCreateRenderPass (m_logicalDevice , &renderPassInfo, nullptr , &m_swapchainRenderPass);
66
85
if (result != VK_SUCCESS)
67
86
UnrecoverableError (" Failed to create renderpass for swapchain" );
68
87
@@ -84,7 +103,7 @@ void SwapchainInfoVk::Create(VkPhysicalDevice physicalDevice, VkDevice logicalDe
84
103
createInfo.subresourceRange .levelCount = 1 ;
85
104
createInfo.subresourceRange .baseArrayLayer = 0 ;
86
105
createInfo.subresourceRange .layerCount = 1 ;
87
- result = vkCreateImageView (logicalDevice , &createInfo, nullptr , &m_swapchainImageViews[i]);
106
+ result = vkCreateImageView (m_logicalDevice , &createInfo, nullptr , &m_swapchainImageViews[i]);
88
107
if (result != VK_SUCCESS)
89
108
UnrecoverableError (" Failed to create imageviews for swapchain" );
90
109
}
@@ -104,7 +123,7 @@ void SwapchainInfoVk::Create(VkPhysicalDevice physicalDevice, VkDevice logicalDe
104
123
framebufferInfo.width = m_actualExtent.width ;
105
124
framebufferInfo.height = m_actualExtent.height ;
106
125
framebufferInfo.layers = 1 ;
107
- result = vkCreateFramebuffer (logicalDevice , &framebufferInfo, nullptr , &m_swapchainFramebuffers[i]);
126
+ result = vkCreateFramebuffer (m_logicalDevice , &framebufferInfo, nullptr , &m_swapchainFramebuffers[i]);
108
127
if (result != VK_SUCCESS)
109
128
UnrecoverableError (" Failed to create framebuffer for swapchain" );
110
129
}
@@ -114,7 +133,7 @@ void SwapchainInfoVk::Create(VkPhysicalDevice physicalDevice, VkDevice logicalDe
114
133
VkSemaphoreCreateInfo info = {};
115
134
info.sType = VK_STRUCTURE_TYPE_SEMAPHORE_CREATE_INFO;
116
135
for (auto & semaphore : m_presentSemaphores){
117
- if (vkCreateSemaphore (logicalDevice , &info, nullptr , &semaphore) != VK_SUCCESS)
136
+ if (vkCreateSemaphore (m_logicalDevice , &info, nullptr , &semaphore) != VK_SUCCESS)
118
137
UnrecoverableError (" Failed to create semaphore for swapchain present" );
119
138
}
120
139
@@ -123,14 +142,14 @@ void SwapchainInfoVk::Create(VkPhysicalDevice physicalDevice, VkDevice logicalDe
123
142
info = {};
124
143
info.sType = VK_STRUCTURE_TYPE_SEMAPHORE_CREATE_INFO;
125
144
for (auto & semaphore : m_acquireSemaphores){
126
- if (vkCreateSemaphore (logicalDevice , &info, nullptr , &semaphore) != VK_SUCCESS)
145
+ if (vkCreateSemaphore (m_logicalDevice , &info, nullptr , &semaphore) != VK_SUCCESS)
127
146
UnrecoverableError (" Failed to create semaphore for swapchain acquire" );
128
147
}
129
148
130
149
VkFenceCreateInfo fenceInfo = {};
131
150
fenceInfo.sType = VK_STRUCTURE_TYPE_FENCE_CREATE_INFO;
132
151
fenceInfo.flags = VK_FENCE_CREATE_SIGNALED_BIT;
133
- result = vkCreateFence (logicalDevice , &fenceInfo, nullptr , &m_imageAvailableFence);
152
+ result = vkCreateFence (m_logicalDevice , &fenceInfo, nullptr , &m_imageAvailableFence);
134
153
if (result != VK_SUCCESS)
135
154
UnrecoverableError (" Failed to create fence for swapchain" );
136
155
@@ -167,19 +186,20 @@ void SwapchainInfoVk::Cleanup()
167
186
168
187
if (m_imageAvailableFence)
169
188
{
189
+ WaitAvailableFence ();
170
190
vkDestroyFence (m_logicalDevice, m_imageAvailableFence, nullptr );
171
191
m_imageAvailableFence = nullptr ;
172
192
}
173
- if (swapchain )
193
+ if (m_swapchain )
174
194
{
175
- vkDestroySwapchainKHR (m_logicalDevice, swapchain , nullptr );
176
- swapchain = VK_NULL_HANDLE;
195
+ vkDestroySwapchainKHR (m_logicalDevice, m_swapchain , nullptr );
196
+ m_swapchain = VK_NULL_HANDLE;
177
197
}
178
198
}
179
199
180
200
bool SwapchainInfoVk::IsValid () const
181
201
{
182
- return swapchain && !m_acquireSemaphores.empty ();
202
+ return m_swapchain && !m_acquireSemaphores.empty ();
183
203
}
184
204
185
205
void SwapchainInfoVk::WaitAvailableFence ()
@@ -207,7 +227,7 @@ bool SwapchainInfoVk::AcquireImage(uint64 timeout)
207
227
ResetAvailableFence ();
208
228
209
229
VkSemaphore acquireSemaphore = m_acquireSemaphores[m_acquireIndex];
210
- VkResult result = vkAcquireNextImageKHR (m_logicalDevice, swapchain , timeout, acquireSemaphore, m_imageAvailableFence, &swapchainImageIndex);
230
+ VkResult result = vkAcquireNextImageKHR (m_logicalDevice, m_swapchain , timeout, acquireSemaphore, m_imageAvailableFence, &swapchainImageIndex);
211
231
if (result == VK_ERROR_OUT_OF_DATE_KHR || result == VK_SUBOPTIMAL_KHR)
212
232
m_shouldRecreate = true ;
213
233
if (result < 0 )
@@ -231,35 +251,6 @@ void SwapchainInfoVk::UnrecoverableError(const char* errMsg)
231
251
throw std::runtime_error (errMsg);
232
252
}
233
253
234
- SwapchainInfoVk::QueueFamilyIndices SwapchainInfoVk::FindQueueFamilies (VkSurfaceKHR surface, VkPhysicalDevice device)
235
- {
236
- uint32_t queueFamilyCount = 0 ;
237
- vkGetPhysicalDeviceQueueFamilyProperties (device, &queueFamilyCount, nullptr );
238
-
239
- std::vector<VkQueueFamilyProperties> queueFamilies (queueFamilyCount);
240
- vkGetPhysicalDeviceQueueFamilyProperties (device, &queueFamilyCount, queueFamilies.data ());
241
-
242
- QueueFamilyIndices indices;
243
- for (int i = 0 ; i < (int )queueFamilies.size (); ++i)
244
- {
245
- const auto & queueFamily = queueFamilies[i];
246
- if (queueFamily.queueCount > 0 && queueFamily.queueFlags & VK_QUEUE_GRAPHICS_BIT)
247
- indices.graphicsFamily = i;
248
-
249
- VkBool32 presentSupport = false ;
250
- const VkResult result = vkGetPhysicalDeviceSurfaceSupportKHR (device, i, surface, &presentSupport);
251
- if (result != VK_SUCCESS)
252
- throw std::runtime_error (fmt::format (" Error while attempting to check if a surface supports presentation: {}" , result));
253
-
254
- if (queueFamily.queueCount > 0 && presentSupport)
255
- indices.presentFamily = i;
256
-
257
- if (indices.IsComplete ())
258
- break ;
259
- }
260
-
261
- return indices;
262
- }
263
254
264
255
SwapchainInfoVk::SwapchainSupportDetails SwapchainInfoVk::QuerySwapchainSupport (VkSurfaceKHR surface, const VkPhysicalDevice& device)
265
256
{
@@ -391,7 +382,7 @@ VkSwapchainCreateInfoKHR SwapchainInfoVk::CreateSwapchainCreateInfo(VkSurfaceKHR
391
382
createInfo.imageArrayLayers = 1 ;
392
383
createInfo.imageUsage = VK_IMAGE_USAGE_COLOR_ATTACHMENT_BIT;
393
384
394
- const QueueFamilyIndices indices = FindQueueFamilies (surface, m_physicalDevice);
385
+ const VulkanRenderer:: QueueFamilyIndices indices = VulkanRenderer::GetInstance ()-> FindQueueFamilies (surface, m_physicalDevice);
395
386
m_swapchainQueueFamilyIndices = { (uint32)indices.graphicsFamily , (uint32)indices.presentFamily };
396
387
if (indices.graphicsFamily != indices.presentFamily )
397
388
{
0 commit comments