Cleanup on Samples (#295)
- introduced samples/utils functions in namespace vk::su (vulkan sample utils) - introduced usage of debugReportCallback
This commit is contained in:
committed by
Markus Tavenrath
parent
018ebdd87f
commit
f0dc8d6f38
@@ -1,4 +1,4 @@
|
||||
// Copyright(c) 2018, NVIDIA CORPORATION. All rights reserved.
|
||||
// Copyright(c) 2018-2019, NVIDIA CORPORATION. All rights reserved.
|
||||
//
|
||||
// Licensed under the Apache License, Version 2.0 (the "License");
|
||||
// you may not use this file except in compliance with the License.
|
||||
@@ -15,147 +15,40 @@
|
||||
// VulkanHpp Samples : 05_InitSwapchain
|
||||
// Initialize a swapchain
|
||||
|
||||
#include "..\utils\utils.hpp"
|
||||
#include "vulkan/vulkan.hpp"
|
||||
#include <iostream>
|
||||
|
||||
static char const* AppName = "05_InitSwapchain";
|
||||
static char const* EngineName = "Vulkan.hpp";
|
||||
|
||||
template<class T, class Compare>
|
||||
constexpr const T& clamp(const T& v, const T& lo, const T& hi, Compare comp)
|
||||
{
|
||||
return assert(!comp(hi, lo)),
|
||||
comp(v, lo) ? lo : comp(hi, v) ? hi : v;
|
||||
}
|
||||
|
||||
template<class T>
|
||||
constexpr const T& clamp(const T& v, const T& lo, const T& hi)
|
||||
{
|
||||
return clamp(v, lo, hi, std::less<>());
|
||||
}
|
||||
|
||||
static std::vector<char const*> getDeviceExtensions()
|
||||
{
|
||||
std::vector<char const*> extensions;
|
||||
|
||||
extensions.push_back(VK_KHR_SWAPCHAIN_EXTENSION_NAME);
|
||||
|
||||
return extensions;
|
||||
}
|
||||
|
||||
static std::vector<char const*> getInstanceExtensions()
|
||||
{
|
||||
std::vector<char const*> extensions;
|
||||
|
||||
extensions.push_back(VK_KHR_SURFACE_EXTENSION_NAME);
|
||||
#if defined(VK_USE_PLATFORM_ANDROID_KHR)
|
||||
extensions.push_back(VK_KHR_ANDROID_SURFACE_EXTENSION_NAME);
|
||||
#elif defined(VK_USE_PLATFORM_IOS_MVK)
|
||||
extensions.push_back(VK_MVK_IOS_SURFACE_EXTENSION_NAME);
|
||||
#elif defined(VK_USE_PLATFORM_MACOS_MVK)
|
||||
extensions.push_back(VK_MVK_MACOS_SURFACE_EXTENSION_NAME);
|
||||
#elif defined(VK_USE_PLATFORM_MIR_KHR)
|
||||
extensions.push_back(VK_KHR_MIR_SURFACE_EXTENSION_NAME);
|
||||
#elif defined(VK_USE_PLATFORM_VI_NN)
|
||||
extensions.push_back(VK_NN_VI_SURFACE_EXTENSION_NAME);
|
||||
#elif defined(VK_USE_PLATFORM_WAYLAND_KHR)
|
||||
extensions.push_back(VK_KHR_WAYLAND_SURFACE_EXTENSION_NAME);
|
||||
#elif defined(VK_USE_PLATFORM_WIN32_KHR)
|
||||
extensions.push_back(VK_KHR_WIN32_SURFACE_EXTENSION_NAME);
|
||||
#elif defined(VK_USE_PLATFORM_XCB_KHR)
|
||||
extensions.push_back(VK_KHR_XCB_SURFACE_EXTENSION_NAME);
|
||||
#elif defined(VK_USE_PLATFORM_XLIB_KHR)
|
||||
extensions.push_back(VK_KHR_XLIB_SURFACE_EXTENSION_NAME);
|
||||
#elif defined(VK_USE_PLATFORM_XLIB_XRANDR_EXT)
|
||||
extensions.push_back(VK_EXT_ACQUIRE_XLIB_DISPLAY_EXTENSION_NAME);
|
||||
#endif
|
||||
|
||||
return extensions;
|
||||
}
|
||||
|
||||
#if defined(VK_USE_PLATFORM_WIN32_KHR)
|
||||
LRESULT CALLBACK WindowProc(HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam)
|
||||
{
|
||||
switch (uMsg)
|
||||
{
|
||||
case WM_CLOSE:
|
||||
PostQuitMessage(0);
|
||||
break;
|
||||
default:
|
||||
break;
|
||||
}
|
||||
return (DefWindowProc(hWnd, uMsg, wParam, lParam));
|
||||
}
|
||||
|
||||
HWND initializeWindow(std::string const& className, std::string const& windowName, LONG width, LONG height)
|
||||
{
|
||||
WNDCLASSEX windowClass;
|
||||
memset(&windowClass, 0, sizeof(WNDCLASSEX));
|
||||
|
||||
HINSTANCE instance = GetModuleHandle(nullptr);
|
||||
windowClass.cbSize = sizeof(WNDCLASSEX);
|
||||
windowClass.style = CS_HREDRAW | CS_VREDRAW;
|
||||
windowClass.lpfnWndProc = WindowProc;
|
||||
windowClass.hInstance = instance;
|
||||
windowClass.hIcon = LoadIcon(NULL, IDI_APPLICATION);
|
||||
windowClass.hCursor = LoadCursor(NULL, IDC_ARROW);
|
||||
windowClass.hbrBackground = (HBRUSH)GetStockObject(WHITE_BRUSH);
|
||||
windowClass.lpszClassName = className.c_str();
|
||||
windowClass.hIconSm = LoadIcon(NULL, IDI_WINLOGO);
|
||||
|
||||
if (!RegisterClassEx(&windowClass))
|
||||
{
|
||||
throw std::runtime_error("Failed to register WNDCLASSEX -> terminating");
|
||||
}
|
||||
|
||||
RECT windowRect = { 0, 0, width, height };
|
||||
AdjustWindowRect(&windowRect, WS_OVERLAPPEDWINDOW, FALSE);
|
||||
|
||||
HWND window = CreateWindowEx(0, className.c_str(), windowName.c_str(), WS_OVERLAPPEDWINDOW | WS_VISIBLE | WS_SYSMENU, 100, 100, windowRect.right - windowRect.left,
|
||||
windowRect.bottom - windowRect.top, nullptr, nullptr, instance, nullptr);
|
||||
if (!window)
|
||||
{
|
||||
throw std::runtime_error("Failed to create window -> terminating");
|
||||
}
|
||||
|
||||
return window;
|
||||
}
|
||||
#else
|
||||
#pragma error "unhandled platform"
|
||||
#endif
|
||||
|
||||
|
||||
int main(int /*argc*/, char * /*argv[]*/)
|
||||
{
|
||||
try
|
||||
{
|
||||
vk::ApplicationInfo appInfo(AppName, 1, EngineName, 1, VK_API_VERSION_1_1);
|
||||
std::vector<char const*> instanceExtensions = getInstanceExtensions();
|
||||
vk::InstanceCreateInfo instanceCreateInfo({}, &appInfo, 0, nullptr, static_cast<uint32_t>(instanceExtensions.size()), instanceExtensions.data());
|
||||
vk::UniqueInstance instance = vk::createInstanceUnique(instanceCreateInfo);
|
||||
vk::UniqueInstance instance = vk::su::createInstance(AppName, EngineName, vk::su::getInstanceExtensions());
|
||||
#if !defined(NDEBUG)
|
||||
vk::UniqueDebugReportCallbackEXT debugReportCallback = vk::su::createDebugReportCallback(instance);
|
||||
#endif
|
||||
|
||||
std::vector<vk::PhysicalDevice> physicalDevices = instance->enumeratePhysicalDevices();
|
||||
assert(!physicalDevices.empty());
|
||||
|
||||
std::vector<vk::QueueFamilyProperties> queueFamilyProperties = physicalDevices[0].getQueueFamilyProperties();
|
||||
uint32_t graphicsQueueFamilyIndex = vk::su::findGraphicsQueueFamilyIndex(queueFamilyProperties);
|
||||
|
||||
/* VULKAN_HPP_KEY_START */
|
||||
|
||||
uint32_t width = 50;
|
||||
uint32_t height = 50;
|
||||
uint32_t width = 64;
|
||||
uint32_t height = 64;
|
||||
#if defined(VK_USE_PLATFORM_WIN32_KHR)
|
||||
HWND window = initializeWindow(AppName, "Sample", width, height);
|
||||
HWND window = vk::su::initializeWindow(AppName, AppName, width, height);
|
||||
|
||||
vk::UniqueSurfaceKHR surface = instance->createWin32SurfaceKHRUnique(vk::Win32SurfaceCreateInfoKHR(vk::Win32SurfaceCreateFlagsKHR(), GetModuleHandle(nullptr), window));
|
||||
#else
|
||||
#pragma error "unhandled platform"
|
||||
#endif
|
||||
|
||||
// determine a queueFamilyIndex that supports graphics
|
||||
std::vector<vk::QueueFamilyProperties> queueFamilyProperties = physicalDevices[0].getQueueFamilyProperties();
|
||||
size_t graphicsQueueFamilyIndex = std::distance(queueFamilyProperties.begin(),
|
||||
std::find_if(queueFamilyProperties.begin(),
|
||||
queueFamilyProperties.end(),
|
||||
[](vk::QueueFamilyProperties const& qfp) { return qfp.queueFlags & vk::QueueFlagBits::eGraphics; }));
|
||||
|
||||
// determine a queueFamilyIndex that suports present
|
||||
// first check if the graphicsQueueFamiliyIndex is good enough
|
||||
size_t presentQueueFamilyIndex = physicalDevices[0].getSurfaceSupportKHR(static_cast<uint32_t>(graphicsQueueFamilyIndex), surface.get()) ? graphicsQueueFamilyIndex : queueFamilyProperties.size();
|
||||
@@ -166,7 +59,7 @@ int main(int /*argc*/, char * /*argv[]*/)
|
||||
{
|
||||
if ((queueFamilyProperties[i].queueFlags & vk::QueueFlagBits::eGraphics) && physicalDevices[0].getSurfaceSupportKHR(static_cast<uint32_t>(i), surface.get()))
|
||||
{
|
||||
graphicsQueueFamilyIndex = i;
|
||||
graphicsQueueFamilyIndex = vk::su::checked_cast<uint32_t>(i);
|
||||
presentQueueFamilyIndex = i;
|
||||
break;
|
||||
}
|
||||
@@ -190,10 +83,7 @@ int main(int /*argc*/, char * /*argv[]*/)
|
||||
}
|
||||
|
||||
// create a device
|
||||
float queuePriority = 0.0f;
|
||||
vk::DeviceQueueCreateInfo deviceQueueCreateInfo({}, static_cast<uint32_t>(graphicsQueueFamilyIndex), 1, &queuePriority);
|
||||
std::vector<char const*> deviceExtensionNames = getDeviceExtensions();
|
||||
vk::UniqueDevice device = physicalDevices[0].createDeviceUnique(vk::DeviceCreateInfo({}, 1, &deviceQueueCreateInfo, 0, nullptr, static_cast<uint32_t>(deviceExtensionNames.size()), deviceExtensionNames.data()));
|
||||
vk::UniqueDevice device = vk::su::createDevice(physicalDevices[0], graphicsQueueFamilyIndex, vk::su::getDeviceExtensions());
|
||||
|
||||
// get the supported VkFormats
|
||||
std::vector<vk::SurfaceFormatKHR> formats = physicalDevices[0].getSurfaceFormatsKHR(surface.get());
|
||||
@@ -201,13 +91,12 @@ int main(int /*argc*/, char * /*argv[]*/)
|
||||
vk::Format format = (formats[0].format == vk::Format::eUndefined) ? vk::Format::eB8G8R8A8Unorm : formats[0].format;
|
||||
|
||||
vk::SurfaceCapabilitiesKHR surfaceCapabilities = physicalDevices[0].getSurfaceCapabilitiesKHR(surface.get());
|
||||
|
||||
VkExtent2D swapchainExtent;
|
||||
if (surfaceCapabilities.currentExtent.width == std::numeric_limits<uint32_t>::max())
|
||||
{
|
||||
// If the surface size is undefined, the size is set to the size of the images requested.
|
||||
swapchainExtent.width = clamp(width, surfaceCapabilities.minImageExtent.width, surfaceCapabilities.maxImageExtent.width);
|
||||
swapchainExtent.height = clamp(height, surfaceCapabilities.minImageExtent.height, surfaceCapabilities.maxImageExtent.height);
|
||||
swapchainExtent.width = vk::su::clamp(width, surfaceCapabilities.minImageExtent.width, surfaceCapabilities.maxImageExtent.width);
|
||||
swapchainExtent.height = vk::su::clamp(height, surfaceCapabilities.minImageExtent.height, surfaceCapabilities.maxImageExtent.height);
|
||||
}
|
||||
else
|
||||
{
|
||||
@@ -244,10 +133,10 @@ int main(int /*argc*/, char * /*argv[]*/)
|
||||
|
||||
std::vector<vk::UniqueImageView> imageViews;
|
||||
imageViews.reserve(swapChainImages.size());
|
||||
vk::ComponentMapping componentMapping(vk::ComponentSwizzle::eR, vk::ComponentSwizzle::eG, vk::ComponentSwizzle::eB, vk::ComponentSwizzle::eA);
|
||||
vk::ImageSubresourceRange subResourceRange(vk::ImageAspectFlagBits::eColor, 0, 1, 0, 1);
|
||||
for (auto image : swapChainImages)
|
||||
{
|
||||
vk::ComponentMapping componentMapping(vk::ComponentSwizzle::eR, vk::ComponentSwizzle::eG, vk::ComponentSwizzle::eB, vk::ComponentSwizzle::eA);
|
||||
vk::ImageSubresourceRange subResourceRange(vk::ImageAspectFlagBits::eColor, 0, 1, 0, 1);
|
||||
vk::ImageViewCreateInfo imageViewCreateInfo(vk::ImageViewCreateFlags(), image, vk::ImageViewType::e2D, format, componentMapping, subResourceRange);
|
||||
imageViews.push_back(device->createImageViewUnique(imageViewCreateInfo));
|
||||
}
|
||||
|
||||
@@ -1,4 +1,4 @@
|
||||
# Copyright(c) 2018, NVIDIA CORPORATION. All rights reserved.
|
||||
# Copyright(c) 2018-2019, NVIDIA CORPORATION. All rights reserved.
|
||||
#
|
||||
# Licensed under the Apache License, Version 2.0 (the "License");
|
||||
# you may not use this file except in compliance with the License.
|
||||
@@ -17,10 +17,12 @@ cmake_minimum_required(VERSION 3.2)
|
||||
project(05_InitSwapchain)
|
||||
|
||||
set(HEADERS
|
||||
../utils/utils.hpp
|
||||
)
|
||||
|
||||
set(SOURCES
|
||||
05_InitSwapchain.cpp
|
||||
../utils/utils.cpp
|
||||
)
|
||||
|
||||
source_group(headers FILES ${HEADERS})
|
||||
|
||||
Reference in New Issue
Block a user