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
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
|
// vk-swap-chain.cpp
#include "vk-swap-chain.h"
#include "../apple/cocoa-util.h"
#include "vk-util.h"
namespace gfx
{
using namespace Slang;
namespace vk
{
ISwapchain* SwapchainImpl::getInterface(const Guid& guid)
{
if (guid == GfxGUID::IID_ISlangUnknown || guid == GfxGUID::IID_ISwapchain)
return static_cast<ISwapchain*>(this);
return nullptr;
}
void SwapchainImpl::destroySwapchainAndImages()
{
m_api->vkQueueWaitIdle(m_queue->m_queue);
if (m_swapChain != VK_NULL_HANDLE)
{
m_api->vkDestroySwapchainKHR(m_api->m_device, m_swapChain, nullptr);
m_swapChain = VK_NULL_HANDLE;
}
// Mark that it is no longer used
m_images.clear();
}
void SwapchainImpl::getWindowSize(int* widthOut, int* heightOut) const
{
#if SLANG_WINDOWS_FAMILY
RECT rc;
::GetClientRect((HWND)m_windowHandle.handleValues[0], &rc);
*widthOut = rc.right - rc.left;
*heightOut = rc.bottom - rc.top;
#elif SLANG_APPLE_FAMILY
CocoaUtil::getNSWindowContentSize((void*)m_windowHandle.handleValues[0], widthOut, heightOut);
#elif defined(SLANG_ENABLE_XLIB)
XWindowAttributes winAttr = {};
XGetWindowAttributes(
(Display*)m_windowHandle.handleValues[0],
(Window)m_windowHandle.handleValues[1],
&winAttr);
*widthOut = winAttr.width;
*heightOut = winAttr.height;
#else
*widthOut = 0;
*heightOut = 0;
#endif
}
Result SwapchainImpl::createSwapchainAndImages()
{
int width, height;
getWindowSize(&width, &height);
VkExtent2D imageExtent = {};
imageExtent.width = width;
imageExtent.height = height;
m_desc.width = width;
m_desc.height = height;
// catch this before throwing error
if (width == 0 || height == 0)
{
return SLANG_FAIL;
}
// It is necessary to query the caps -> otherwise the LunarG verification layer will
// issue an error
{
VkSurfaceCapabilitiesKHR surfaceCaps;
SLANG_VK_RETURN_ON_FAIL(m_api->vkGetPhysicalDeviceSurfaceCapabilitiesKHR(
m_api->m_physicalDevice,
m_surface,
&surfaceCaps));
}
VkPresentModeKHR presentMode;
List<VkPresentModeKHR> presentModes;
uint32_t numPresentModes = 0;
m_api->vkGetPhysicalDeviceSurfacePresentModesKHR(
m_api->m_physicalDevice,
m_surface,
&numPresentModes,
nullptr);
presentModes.setCount(numPresentModes);
m_api->vkGetPhysicalDeviceSurfacePresentModesKHR(
m_api->m_physicalDevice,
m_surface,
&numPresentModes,
presentModes.getBuffer());
{
int numCheckPresentOptions = 3;
VkPresentModeKHR presentOptions[] = {
VK_PRESENT_MODE_IMMEDIATE_KHR,
VK_PRESENT_MODE_MAILBOX_KHR,
VK_PRESENT_MODE_FIFO_KHR};
if (m_desc.enableVSync)
{
presentOptions[0] = VK_PRESENT_MODE_FIFO_KHR;
presentOptions[1] = VK_PRESENT_MODE_IMMEDIATE_KHR;
presentOptions[2] = VK_PRESENT_MODE_MAILBOX_KHR;
}
presentMode = VK_PRESENT_MODE_MAX_ENUM_KHR; // Invalid
// Find the first option that's available on the device
for (int j = 0; j < numCheckPresentOptions; j++)
{
if (presentModes.indexOf(presentOptions[j]) != Index(-1))
{
presentMode = presentOptions[j];
break;
}
}
if (presentMode == VK_PRESENT_MODE_MAX_ENUM_KHR)
{
return SLANG_FAIL;
}
}
VkSwapchainKHR oldSwapchain = VK_NULL_HANDLE;
VkSwapchainCreateInfoKHR swapchainDesc = {};
swapchainDesc.sType = VK_STRUCTURE_TYPE_SWAPCHAIN_CREATE_INFO_KHR;
swapchainDesc.surface = m_surface;
swapchainDesc.minImageCount = m_desc.imageCount;
swapchainDesc.imageFormat = m_vkformat;
swapchainDesc.imageColorSpace = VK_COLOR_SPACE_SRGB_NONLINEAR_KHR;
swapchainDesc.imageExtent = imageExtent;
swapchainDesc.imageArrayLayers = 1;
swapchainDesc.imageUsage =
VK_IMAGE_USAGE_COLOR_ATTACHMENT_BIT | VK_IMAGE_USAGE_TRANSFER_DST_BIT;
swapchainDesc.imageSharingMode = VK_SHARING_MODE_EXCLUSIVE;
swapchainDesc.preTransform = VK_SURFACE_TRANSFORM_IDENTITY_BIT_KHR;
swapchainDesc.compositeAlpha = VK_COMPOSITE_ALPHA_OPAQUE_BIT_KHR;
swapchainDesc.presentMode = presentMode;
swapchainDesc.clipped = VK_TRUE;
swapchainDesc.oldSwapchain = oldSwapchain;
SLANG_VK_RETURN_ON_FAIL(
m_api->vkCreateSwapchainKHR(m_api->m_device, &swapchainDesc, nullptr, &m_swapChain));
uint32_t numSwapChainImages = 0;
m_api->vkGetSwapchainImagesKHR(m_api->m_device, m_swapChain, &numSwapChainImages, nullptr);
List<VkImage> vkImages;
{
vkImages.setCount(numSwapChainImages);
m_api->vkGetSwapchainImagesKHR(
m_api->m_device,
m_swapChain,
&numSwapChainImages,
vkImages.getBuffer());
}
for (GfxIndex i = 0; i < m_desc.imageCount; i++)
{
ITextureResource::Desc imageDesc = {};
imageDesc.allowedStates = ResourceStateSet(
ResourceState::Present,
ResourceState::RenderTarget,
ResourceState::CopyDestination);
imageDesc.type = IResource::Type::Texture2D;
imageDesc.arraySize = 0;
imageDesc.format = m_desc.format;
imageDesc.size.width = m_desc.width;
imageDesc.size.height = m_desc.height;
imageDesc.size.depth = 1;
imageDesc.numMipLevels = 1;
imageDesc.defaultState = ResourceState::Present;
RefPtr<TextureResourceImpl> image = new TextureResourceImpl(imageDesc, m_renderer);
image->m_image = vkImages[i];
image->m_imageMemory = 0;
image->m_vkformat = m_vkformat;
image->m_isWeakImageReference = true;
m_images.add(image);
}
return SLANG_OK;
}
SwapchainImpl::~SwapchainImpl()
{
destroySwapchainAndImages();
if (m_surface)
{
m_api->vkDestroySurfaceKHR(m_api->m_instance, m_surface, nullptr);
m_surface = VK_NULL_HANDLE;
}
m_renderer->m_api.vkDestroySemaphore(m_renderer->m_api.m_device, m_nextImageSemaphore, nullptr);
#if SLANG_APPLE_FAMILY
CocoaUtil::destroyMetalLayer(m_metalLayer);
#endif
}
Index SwapchainImpl::_indexOfFormat(List<VkSurfaceFormatKHR>& formatsIn, VkFormat format)
{
const Index numFormats = formatsIn.getCount();
const VkSurfaceFormatKHR* formats = formatsIn.getBuffer();
for (Index i = 0; i < numFormats; ++i)
{
if (formats[i].format == format)
{
return i;
}
}
return -1;
}
Result SwapchainImpl::init(DeviceImpl* renderer, const ISwapchain::Desc& desc, WindowHandle window)
{
m_desc = desc;
m_renderer = renderer;
m_api = &renderer->m_api;
m_queue = static_cast<CommandQueueImpl*>(desc.queue);
m_windowHandle = window;
VkSemaphoreCreateInfo semaphoreCreateInfo = {};
semaphoreCreateInfo.sType = VK_STRUCTURE_TYPE_SEMAPHORE_CREATE_INFO;
SLANG_VK_RETURN_ON_FAIL(renderer->m_api.vkCreateSemaphore(
renderer->m_api.m_device,
&semaphoreCreateInfo,
nullptr,
&m_nextImageSemaphore));
m_queue = static_cast<CommandQueueImpl*>(desc.queue);
// Make sure it's not set initially
m_vkformat = VK_FORMAT_UNDEFINED;
#if SLANG_WINDOWS_FAMILY
VkWin32SurfaceCreateInfoKHR surfaceCreateInfo = {};
surfaceCreateInfo.sType = VK_STRUCTURE_TYPE_WIN32_SURFACE_CREATE_INFO_KHR;
surfaceCreateInfo.hinstance = ::GetModuleHandle(nullptr);
surfaceCreateInfo.hwnd = (HWND)window.handleValues[0];
SLANG_VK_RETURN_ON_FAIL(
m_api->vkCreateWin32SurfaceKHR(m_api->m_instance, &surfaceCreateInfo, nullptr, &m_surface));
#elif SLANG_APPLE_FAMILY
m_metalLayer = CocoaUtil::createMetalLayer((void*)window.handleValues[0]);
VkMetalSurfaceCreateInfoEXT surfaceCreateInfo = {};
surfaceCreateInfo.sType = VK_STRUCTURE_TYPE_METAL_SURFACE_CREATE_INFO_EXT;
surfaceCreateInfo.pLayer = (CAMetalLayer*)m_metalLayer;
SLANG_VK_RETURN_ON_FAIL(
m_api->vkCreateMetalSurfaceEXT(m_api->m_instance, &surfaceCreateInfo, nullptr, &m_surface));
#elif SLANG_ENABLE_XLIB
VkXlibSurfaceCreateInfoKHR surfaceCreateInfo = {};
surfaceCreateInfo.sType = VK_STRUCTURE_TYPE_XLIB_SURFACE_CREATE_INFO_KHR;
surfaceCreateInfo.dpy = (Display*)window.handleValues[0];
surfaceCreateInfo.window = (Window)window.handleValues[1];
SLANG_VK_RETURN_ON_FAIL(
m_api->vkCreateXlibSurfaceKHR(m_api->m_instance, &surfaceCreateInfo, nullptr, &m_surface));
#else
return SLANG_E_NOT_AVAILABLE;
#endif
VkBool32 supported = false;
m_api->vkGetPhysicalDeviceSurfaceSupportKHR(
m_api->m_physicalDevice,
renderer->m_queueFamilyIndex,
m_surface,
&supported);
uint32_t numSurfaceFormats = 0;
List<VkSurfaceFormatKHR> surfaceFormats;
m_api->vkGetPhysicalDeviceSurfaceFormatsKHR(
m_api->m_physicalDevice,
m_surface,
&numSurfaceFormats,
nullptr);
surfaceFormats.setCount(int(numSurfaceFormats));
m_api->vkGetPhysicalDeviceSurfaceFormatsKHR(
m_api->m_physicalDevice,
m_surface,
&numSurfaceFormats,
surfaceFormats.getBuffer());
// Look for a suitable format
List<VkFormat> formats;
formats.add(VulkanUtil::getVkFormat(desc.format));
// HACK! To check for a different format if couldn't be found
if (desc.format == Format::R8G8B8A8_UNORM)
{
formats.add(VK_FORMAT_B8G8R8A8_UNORM);
}
for (Index i = 0; i < formats.getCount(); ++i)
{
VkFormat format = formats[i];
if (_indexOfFormat(surfaceFormats, format) >= 0)
{
m_vkformat = format;
}
}
if (m_vkformat == VK_FORMAT_UNDEFINED)
{
return SLANG_FAIL;
}
// Save the desc
m_desc = desc;
if (m_desc.format == Format::R8G8B8A8_UNORM && m_vkformat == VK_FORMAT_B8G8R8A8_UNORM)
{
m_desc.format = Format::B8G8R8A8_UNORM;
}
createSwapchainAndImages();
return SLANG_OK;
}
Result SwapchainImpl::getImage(GfxIndex index, ITextureResource** outResource)
{
if (m_images.getCount() <= (Index)index)
return SLANG_FAIL;
returnComPtr(outResource, m_images[index]);
return SLANG_OK;
}
Result SwapchainImpl::resize(GfxCount width, GfxCount height)
{
SLANG_UNUSED(width);
SLANG_UNUSED(height);
destroySwapchainAndImages();
return createSwapchainAndImages();
}
Result SwapchainImpl::present()
{
// If there are pending fence wait operations, flush them as an
// empty vkQueueSubmit.
if (m_queue->m_pendingWaitFences.getCount() != 0)
{
m_queue->queueSubmitImpl(0, nullptr, nullptr, 0);
}
uint32_t swapChainIndices[] = {uint32_t(m_currentImageIndex)};
VkPresentInfoKHR presentInfo = {};
presentInfo.sType = VK_STRUCTURE_TYPE_PRESENT_INFO_KHR;
presentInfo.swapchainCount = 1;
presentInfo.pSwapchains = &m_swapChain;
presentInfo.pImageIndices = swapChainIndices;
Array<VkSemaphore, 2> waitSemaphores;
for (auto s : m_queue->m_pendingWaitSemaphores)
{
if (s != VK_NULL_HANDLE)
{
waitSemaphores.add(s);
}
}
m_queue->m_pendingWaitSemaphores[0] = VK_NULL_HANDLE;
m_queue->m_pendingWaitSemaphores[1] = VK_NULL_HANDLE;
presentInfo.waitSemaphoreCount = (uint32_t)waitSemaphores.getCount();
if (presentInfo.waitSemaphoreCount)
{
presentInfo.pWaitSemaphores = waitSemaphores.getBuffer();
}
if (m_currentImageIndex != -1)
m_api->vkQueuePresentKHR(m_queue->m_queue, &presentInfo);
return SLANG_OK;
}
int SwapchainImpl::acquireNextImage()
{
if (!m_images.getCount())
{
m_queue->m_pendingWaitSemaphores[1] = VK_NULL_HANDLE;
return -1;
}
m_currentImageIndex = -1;
VkResult result = m_api->vkAcquireNextImageKHR(
m_api->m_device,
m_swapChain,
UINT64_MAX,
m_nextImageSemaphore,
VK_NULL_HANDLE,
(uint32_t*)&m_currentImageIndex);
if (result != VK_SUCCESS
#if SLANG_APPLE_FAMILY
&& result != VK_SUBOPTIMAL_KHR
#endif
)
{
m_currentImageIndex = -1;
destroySwapchainAndImages();
return m_currentImageIndex;
}
// Make the queue's next submit wait on `m_nextImageSemaphore`.
m_queue->m_pendingWaitSemaphores[1] = m_nextImageSemaphore;
return m_currentImageIndex;
}
Result SwapchainImpl::setFullScreenMode(bool mode)
{
return SLANG_FAIL;
}
} // namespace vk
} // namespace gfx
|