Skip to content

Commit 5c786e7

Browse files
committed
Code cleanup
Use designated initializers
1 parent 3b0cbc1 commit 5c786e7

2 files changed

Lines changed: 79 additions & 108 deletions

File tree

base/VulkanSwapChain.cpp

Lines changed: 60 additions & 88 deletions
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ void VulkanSwapChain::initSurface(screen_context_t screen_context, screen_window
3535

3636
// Create the os-specific surface
3737
#if defined(VK_USE_PLATFORM_WIN32_KHR)
38-
VkWin32SurfaceCreateInfoKHR surfaceCreateInfo = {};
38+
VkWin32SurfaceCreateInfoKHR surfaceCreateInfo{};
3939
surfaceCreateInfo.sType = VK_STRUCTURE_TYPE_WIN32_SURFACE_CREATE_INFO_KHR;
4040
surfaceCreateInfo.hinstance = (HINSTANCE)platformHandle;
4141
surfaceCreateInfo.hwnd = (HWND)platformWindow;
@@ -110,9 +110,8 @@ void VulkanSwapChain::initSurface(screen_context_t screen_context, screen_window
110110

111111
// Get available queue family properties
112112
uint32_t queueCount;
113-
vkGetPhysicalDeviceQueueFamilyProperties(physicalDevice, &queueCount, NULL);
113+
vkGetPhysicalDeviceQueueFamilyProperties(physicalDevice, &queueCount, nullptr);
114114
assert(queueCount >= 1);
115-
116115
std::vector<VkQueueFamilyProperties> queueProps(queueCount);
117116
vkGetPhysicalDeviceQueueFamilyProperties(physicalDevice, &queueCount, queueProps.data());
118117

@@ -129,17 +128,12 @@ void VulkanSwapChain::initSurface(screen_context_t screen_context, screen_window
129128
// families, try to find one that supports both
130129
uint32_t graphicsQueueNodeIndex = UINT32_MAX;
131130
uint32_t presentQueueNodeIndex = UINT32_MAX;
132-
for (uint32_t i = 0; i < queueCount; i++)
133-
{
134-
if ((queueProps[i].queueFlags & VK_QUEUE_GRAPHICS_BIT) != 0)
135-
{
136-
if (graphicsQueueNodeIndex == UINT32_MAX)
137-
{
131+
for (uint32_t i = 0; i < queueCount; i++) {
132+
if ((queueProps[i].queueFlags & VK_QUEUE_GRAPHICS_BIT) != 0) {
133+
if (graphicsQueueNodeIndex == UINT32_MAX) {
138134
graphicsQueueNodeIndex = i;
139135
}
140-
141-
if (supportsPresent[i] == VK_TRUE)
142-
{
136+
if (supportsPresent[i] == VK_TRUE) {
143137
graphicsQueueNodeIndex = i;
144138
presentQueueNodeIndex = i;
145139
break;
@@ -150,34 +144,27 @@ void VulkanSwapChain::initSurface(screen_context_t screen_context, screen_window
150144
{
151145
// If there's no queue that supports both present and graphics
152146
// try to find a separate present queue
153-
for (uint32_t i = 0; i < queueCount; ++i)
154-
{
155-
if (supportsPresent[i] == VK_TRUE)
156-
{
147+
for (uint32_t i = 0; i < queueCount; ++i) {
148+
if (supportsPresent[i] == VK_TRUE) {
157149
presentQueueNodeIndex = i;
158150
break;
159151
}
160152
}
161153
}
162154

163155
// Exit if either a graphics or a presenting queue hasn't been found
164-
if (graphicsQueueNodeIndex == UINT32_MAX || presentQueueNodeIndex == UINT32_MAX)
165-
{
156+
if (graphicsQueueNodeIndex == UINT32_MAX || presentQueueNodeIndex == UINT32_MAX) {
166157
vks::tools::exitFatal("Could not find a graphics and/or presenting queue!", -1);
167158
}
168-
169-
if (graphicsQueueNodeIndex != presentQueueNodeIndex)
170-
{
159+
if (graphicsQueueNodeIndex != presentQueueNodeIndex) {
171160
vks::tools::exitFatal("Separate graphics and presenting queues are not supported yet!", -1);
172161
}
173-
174162
queueNodeIndex = graphicsQueueNodeIndex;
175163

176164
// Get list of supported surface formats
177165
uint32_t formatCount;
178166
VK_CHECK_RESULT(vkGetPhysicalDeviceSurfaceFormatsKHR(physicalDevice, surface, &formatCount, NULL));
179167
assert(formatCount > 0);
180-
181168
std::vector<VkSurfaceFormatKHR> surfaceFormats(formatCount);
182169
VK_CHECK_RESULT(vkGetPhysicalDeviceSurfaceFormatsKHR(physicalDevice, surface, &formatCount, surfaceFormats.data()));
183170

@@ -189,7 +176,6 @@ void VulkanSwapChain::initSurface(screen_context_t screen_context, screen_window
189176
VK_FORMAT_R8G8B8A8_UNORM,
190177
VK_FORMAT_A8B8G8R8_UNORM_PACK32
191178
};
192-
193179
for (auto& availableFormat : surfaceFormats) {
194180
if (std::find(preferredImageFormats.begin(), preferredImageFormats.end(), availableFormat.format) != preferredImageFormats.end()) {
195181
selectedFormat = availableFormat;
@@ -218,29 +204,26 @@ void VulkanSwapChain::create(uint32_t& width, uint32_t& height, bool vsync, bool
218204
VkSwapchainKHR oldSwapchain = swapChain;
219205

220206
// Get physical device surface properties and formats
221-
VkSurfaceCapabilitiesKHR surfCaps;
222-
VK_CHECK_RESULT(vkGetPhysicalDeviceSurfaceCapabilitiesKHR(physicalDevice, surface, &surfCaps));
207+
VkSurfaceCapabilitiesKHR surfaceCaps;
208+
VK_CHECK_RESULT(vkGetPhysicalDeviceSurfaceCapabilitiesKHR(physicalDevice, surface, &surfaceCaps));
223209

224210
VkExtent2D swapchainExtent = {};
225211
// If width (and height) equals the special value 0xFFFFFFFF, the size of the surface will be set by the swapchain
226-
if (surfCaps.currentExtent.width == (uint32_t)-1)
227-
{
212+
if (surfaceCaps.currentExtent.width == (uint32_t)-1) {
228213
// If the surface size is undefined, the size is set to the size of the images requested
229214
swapchainExtent.width = width;
230215
swapchainExtent.height = height;
231-
}
232-
else
233-
{
216+
} else {
234217
// If the surface size is defined, the swap chain size must match
235-
swapchainExtent = surfCaps.currentExtent;
236-
width = surfCaps.currentExtent.width;
237-
height = surfCaps.currentExtent.height;
218+
swapchainExtent = surfaceCaps.currentExtent;
219+
width = surfaceCaps.currentExtent.width;
220+
height = surfaceCaps.currentExtent.height;
238221
}
239222

240223

241224
// Select a present mode for the swapchain
242225
uint32_t presentModeCount;
243-
VK_CHECK_RESULT(vkGetPhysicalDeviceSurfacePresentModesKHR(physicalDevice, surface, &presentModeCount, NULL));
226+
VK_CHECK_RESULT(vkGetPhysicalDeviceSurfacePresentModesKHR(physicalDevice, surface, &presentModeCount, nullptr));
244227
assert(presentModeCount > 0);
245228

246229
std::vector<VkPresentModeKHR> presentModes(presentModeCount);
@@ -269,22 +252,18 @@ void VulkanSwapChain::create(uint32_t& width, uint32_t& height, bool vsync, bool
269252
}
270253

271254
// Determine the number of images
272-
uint32_t desiredNumberOfSwapchainImages = surfCaps.minImageCount + 1;
273-
if ((surfCaps.maxImageCount > 0) && (desiredNumberOfSwapchainImages > surfCaps.maxImageCount))
274-
{
275-
desiredNumberOfSwapchainImages = surfCaps.maxImageCount;
255+
uint32_t desiredNumberOfSwapchainImages = surfaceCaps.minImageCount + 1;
256+
if ((surfaceCaps.maxImageCount > 0) && (desiredNumberOfSwapchainImages > surfaceCaps.maxImageCount)) {
257+
desiredNumberOfSwapchainImages = surfaceCaps.maxImageCount;
276258
}
277259

278260
// Find the transformation of the surface
279261
VkSurfaceTransformFlagsKHR preTransform;
280-
if (surfCaps.supportedTransforms & VK_SURFACE_TRANSFORM_IDENTITY_BIT_KHR)
281-
{
262+
if (surfaceCaps.supportedTransforms & VK_SURFACE_TRANSFORM_IDENTITY_BIT_KHR) {
282263
// We prefer a non-rotated transform
283264
preTransform = VK_SURFACE_TRANSFORM_IDENTITY_BIT_KHR;
284-
}
285-
else
286-
{
287-
preTransform = surfCaps.currentTransform;
265+
} else {
266+
preTransform = surfaceCaps.currentTransform;
288267
}
289268

290269
// Find a supported composite alpha format (not all devices support alpha opaque)
@@ -297,41 +276,39 @@ void VulkanSwapChain::create(uint32_t& width, uint32_t& height, bool vsync, bool
297276
VK_COMPOSITE_ALPHA_INHERIT_BIT_KHR,
298277
};
299278
for (auto& compositeAlphaFlag : compositeAlphaFlags) {
300-
if (surfCaps.supportedCompositeAlpha & compositeAlphaFlag) {
279+
if (surfaceCaps.supportedCompositeAlpha & compositeAlphaFlag) {
301280
compositeAlpha = compositeAlphaFlag;
302281
break;
303282
};
304283
}
305284

306-
VkSwapchainCreateInfoKHR swapchainCI = {};
307-
swapchainCI.sType = VK_STRUCTURE_TYPE_SWAPCHAIN_CREATE_INFO_KHR;
308-
swapchainCI.surface = surface;
309-
swapchainCI.minImageCount = desiredNumberOfSwapchainImages;
310-
swapchainCI.imageFormat = colorFormat;
311-
swapchainCI.imageColorSpace = colorSpace;
312-
swapchainCI.imageExtent = { swapchainExtent.width, swapchainExtent.height };
313-
swapchainCI.imageUsage = VK_IMAGE_USAGE_COLOR_ATTACHMENT_BIT;
314-
swapchainCI.preTransform = (VkSurfaceTransformFlagBitsKHR)preTransform;
315-
swapchainCI.imageArrayLayers = 1;
316-
swapchainCI.imageSharingMode = VK_SHARING_MODE_EXCLUSIVE;
317-
swapchainCI.queueFamilyIndexCount = 0;
318-
swapchainCI.presentMode = swapchainPresentMode;
319-
// Setting oldSwapChain to the saved handle of the previous swapchain aids in resource reuse and makes sure that we can still present already acquired images
320-
swapchainCI.oldSwapchain = oldSwapchain;
321-
// Setting clipped to VK_TRUE allows the implementation to discard rendering outside of the surface area
322-
swapchainCI.clipped = VK_TRUE;
323-
swapchainCI.compositeAlpha = compositeAlpha;
324-
285+
VkSwapchainCreateInfoKHR swapchainCI{
286+
.sType = VK_STRUCTURE_TYPE_SWAPCHAIN_CREATE_INFO_KHR,
287+
.surface = surface,
288+
.minImageCount = desiredNumberOfSwapchainImages,
289+
.imageFormat = colorFormat,
290+
.imageColorSpace = colorSpace,
291+
.imageExtent = { swapchainExtent.width, swapchainExtent.height },
292+
.imageArrayLayers = 1,
293+
.imageUsage = VK_IMAGE_USAGE_COLOR_ATTACHMENT_BIT,
294+
.imageSharingMode = VK_SHARING_MODE_EXCLUSIVE,
295+
.queueFamilyIndexCount = 0,
296+
.preTransform = (VkSurfaceTransformFlagBitsKHR)preTransform,
297+
.compositeAlpha = compositeAlpha,
298+
.presentMode = swapchainPresentMode,
299+
// Setting clipped to VK_TRUE allows the implementation to discard rendering outside of the surface area
300+
.clipped = VK_TRUE,
301+
// Setting oldSwapChain to the saved handle of the previous swapchain aids in resource reuse and makes sure that we can still present already acquired images
302+
.oldSwapchain = oldSwapchain,
303+
};
325304
// Enable transfer source on swap chain images if supported
326-
if (surfCaps.supportedUsageFlags & VK_IMAGE_USAGE_TRANSFER_SRC_BIT) {
305+
if (surfaceCaps.supportedUsageFlags & VK_IMAGE_USAGE_TRANSFER_SRC_BIT) {
327306
swapchainCI.imageUsage |= VK_IMAGE_USAGE_TRANSFER_SRC_BIT;
328307
}
329-
330308
// Enable transfer destination on swap chain images if supported
331-
if (surfCaps.supportedUsageFlags & VK_IMAGE_USAGE_TRANSFER_DST_BIT) {
309+
if (surfaceCaps.supportedUsageFlags & VK_IMAGE_USAGE_TRANSFER_DST_BIT) {
332310
swapchainCI.imageUsage |= VK_IMAGE_USAGE_TRANSFER_DST_BIT;
333311
}
334-
335312
VK_CHECK_RESULT(vkCreateSwapchainKHR(device, &swapchainCI, nullptr, &swapChain));
336313

337314
// If an existing swap chain is re-created, destroy the old swap chain and the ressources owned by the application (image views, images are owned by the swap chain)
@@ -341,34 +318,29 @@ void VulkanSwapChain::create(uint32_t& width, uint32_t& height, bool vsync, bool
341318
}
342319
vkDestroySwapchainKHR(device, oldSwapchain, nullptr);
343320
}
321+
// Get the (new) swap chain images
344322
VK_CHECK_RESULT(vkGetSwapchainImagesKHR(device, swapChain, &imageCount, nullptr));
345-
346-
// Get the swap chain images
347323
images.resize(imageCount);
348324
VK_CHECK_RESULT(vkGetSwapchainImagesKHR(device, swapChain, &imageCount, images.data()));
349325

350326
// Get the swap chain buffers containing the image and imageview
351327
imageViews.resize(imageCount);
352328
for (auto i = 0; i < images.size(); i++)
353329
{
354-
VkImageViewCreateInfo colorAttachmentView = {};
355-
colorAttachmentView.sType = VK_STRUCTURE_TYPE_IMAGE_VIEW_CREATE_INFO;
356-
colorAttachmentView.pNext = NULL;
357-
colorAttachmentView.format = colorFormat;
358-
colorAttachmentView.components = {
359-
VK_COMPONENT_SWIZZLE_R,
360-
VK_COMPONENT_SWIZZLE_G,
361-
VK_COMPONENT_SWIZZLE_B,
362-
VK_COMPONENT_SWIZZLE_A
330+
VkImageViewCreateInfo colorAttachmentView{
331+
.sType = VK_STRUCTURE_TYPE_IMAGE_VIEW_CREATE_INFO,
332+
.image = images[i],
333+
.viewType = VK_IMAGE_VIEW_TYPE_2D,
334+
.format = colorFormat,
335+
.components = { VK_COMPONENT_SWIZZLE_R, VK_COMPONENT_SWIZZLE_G, VK_COMPONENT_SWIZZLE_B, VK_COMPONENT_SWIZZLE_A },
336+
.subresourceRange = {
337+
.aspectMask = VK_IMAGE_ASPECT_COLOR_BIT,
338+
.baseMipLevel = 0,
339+
.levelCount = 1,
340+
.baseArrayLayer = 0,
341+
.layerCount = 1
342+
},
363343
};
364-
colorAttachmentView.subresourceRange.aspectMask = VK_IMAGE_ASPECT_COLOR_BIT;
365-
colorAttachmentView.subresourceRange.baseMipLevel = 0;
366-
colorAttachmentView.subresourceRange.levelCount = 1;
367-
colorAttachmentView.subresourceRange.baseArrayLayer = 0;
368-
colorAttachmentView.subresourceRange.layerCount = 1;
369-
colorAttachmentView.viewType = VK_IMAGE_VIEW_TYPE_2D;
370-
colorAttachmentView.flags = 0;
371-
colorAttachmentView.image = images[i];
372344
VK_CHECK_RESULT(vkCreateImageView(device, &colorAttachmentView, nullptr, &imageViews[i]));
373345
}
374346
}

base/vulkanexamplebase.cpp

Lines changed: 19 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1128,20 +1128,20 @@ HWND VulkanExampleBase::setupWindow(HINSTANCE hinstance, WNDPROC wndproc)
11281128
{
11291129
this->windowInstance = hinstance;
11301130

1131-
WNDCLASSEX wndClass{};
1132-
1133-
wndClass.cbSize = sizeof(WNDCLASSEX);
1134-
wndClass.style = CS_HREDRAW | CS_VREDRAW;
1135-
wndClass.lpfnWndProc = wndproc;
1136-
wndClass.cbClsExtra = 0;
1137-
wndClass.cbWndExtra = 0;
1138-
wndClass.hInstance = hinstance;
1139-
wndClass.hIcon = LoadIcon(NULL, IDI_APPLICATION);
1140-
wndClass.hCursor = LoadCursor(NULL, IDC_ARROW);
1141-
wndClass.hbrBackground = (HBRUSH)GetStockObject(BLACK_BRUSH);
1142-
wndClass.lpszMenuName = NULL;
1143-
wndClass.lpszClassName = name.c_str();
1144-
wndClass.hIconSm = LoadIcon(NULL, IDI_WINLOGO);
1131+
WNDCLASSEX wndClass{
1132+
.cbSize = sizeof(WNDCLASSEX),
1133+
.style = CS_HREDRAW | CS_VREDRAW,
1134+
.lpfnWndProc = wndproc,
1135+
.cbClsExtra = 0,
1136+
.cbWndExtra = 0,
1137+
.hInstance = hinstance,
1138+
.hIcon = LoadIcon(NULL, IDI_APPLICATION),
1139+
.hCursor = LoadCursor(NULL, IDC_ARROW),
1140+
.hbrBackground = (HBRUSH)GetStockObject(BLACK_BRUSH),
1141+
.lpszMenuName = NULL,
1142+
.lpszClassName = name.c_str(),
1143+
.hIconSm = LoadIcon(NULL, IDI_WINLOGO),
1144+
};
11451145

11461146
if (!RegisterClassEx(&wndClass))
11471147
{
@@ -1195,13 +1195,12 @@ HWND VulkanExampleBase::setupWindow(HINSTANCE hinstance, WNDPROC wndproc)
11951195
dwStyle = WS_OVERLAPPEDWINDOW | WS_CLIPSIBLINGS | WS_CLIPCHILDREN;
11961196
}
11971197

1198-
RECT windowRect = {
1199-
0L,
1200-
0L,
1201-
settings.fullscreen ? (long)screenWidth : (long)width,
1202-
settings.fullscreen ? (long)screenHeight : (long)height
1198+
RECT windowRect{
1199+
.left = 0L,
1200+
.top = 0L,
1201+
.right = settings.fullscreen ? (long)screenWidth : (long)width,
1202+
.bottom = settings.fullscreen ? (long)screenHeight : (long)height
12031203
};
1204-
12051204
AdjustWindowRectEx(&windowRect, dwStyle, FALSE, dwExStyle);
12061205

12071206
std::string windowTitle = getWindowTitle();

0 commit comments

Comments
 (0)