Introduce usage of clang-format to format vulkan.hpp and the other sources.

This commit is contained in:
asuessenbach
2020-04-12 21:49:12 +02:00
parent ce9fd81bd9
commit f5e59484a6
68 changed files with 56064 additions and 35586 deletions

View File

@@ -17,59 +17,66 @@
#include "../utils/utils.hpp"
#include "vulkan/vulkan.hpp"
#include <iostream>
static char const* AppName = "05_InitSwapchain";
static char const* EngineName = "Vulkan.hpp";
static char const * AppName = "05_InitSwapchain";
static char const * EngineName = "Vulkan.hpp";
int main(int /*argc*/, char ** /*argv*/)
int main( int /*argc*/, char ** /*argv*/ )
{
try
{
vk::UniqueInstance instance = vk::su::createInstance(AppName, EngineName, {}, vk::su::getInstanceExtensions());
#if !defined(NDEBUG)
vk::UniqueDebugUtilsMessengerEXT debugUtilsMessenger = vk::su::createDebugUtilsMessenger(instance);
vk::UniqueInstance instance = vk::su::createInstance( AppName, EngineName, {}, vk::su::getInstanceExtensions() );
#if !defined( NDEBUG )
vk::UniqueDebugUtilsMessengerEXT debugUtilsMessenger = vk::su::createDebugUtilsMessenger( instance );
#endif
vk::PhysicalDevice physicalDevice = instance->enumeratePhysicalDevices().front();
std::vector<vk::QueueFamilyProperties> queueFamilyProperties = physicalDevice.getQueueFamilyProperties();
uint32_t graphicsQueueFamilyIndex = vk::su::findGraphicsQueueFamilyIndex(queueFamilyProperties);
uint32_t graphicsQueueFamilyIndex = vk::su::findGraphicsQueueFamilyIndex( queueFamilyProperties );
/* VULKAN_HPP_KEY_START */
uint32_t width = 64;
uint32_t height = 64;
vk::su::WindowData window = vk::su::createWindow(AppName, {width, height});
uint32_t width = 64;
uint32_t height = 64;
vk::su::WindowData window = vk::su::createWindow( AppName, { width, height } );
vk::UniqueSurfaceKHR surface;
{
VkSurfaceKHR _surface;
glfwCreateWindowSurface(VkInstance(instance.get()), window.handle, nullptr, &_surface);
vk::ObjectDestroy<vk::Instance, VULKAN_HPP_DEFAULT_DISPATCHER_TYPE> _deleter(instance.get());
surface = vk::UniqueSurfaceKHR(vk::SurfaceKHR(_surface), _deleter);
glfwCreateWindowSurface( VkInstance( instance.get() ), window.handle, nullptr, &_surface );
vk::ObjectDestroy<vk::Instance, VULKAN_HPP_DEFAULT_DISPATCHER_TYPE> _deleter( instance.get() );
surface = vk::UniqueSurfaceKHR( vk::SurfaceKHR( _surface ), _deleter );
}
// determine a queueFamilyIndex that suports present
// first check if the graphicsQueueFamiliyIndex is good enough
size_t presentQueueFamilyIndex = physicalDevice.getSurfaceSupportKHR(static_cast<uint32_t>(graphicsQueueFamilyIndex), surface.get()) ? graphicsQueueFamilyIndex : queueFamilyProperties.size();
if (presentQueueFamilyIndex == queueFamilyProperties.size())
size_t presentQueueFamilyIndex =
physicalDevice.getSurfaceSupportKHR( static_cast<uint32_t>( graphicsQueueFamilyIndex ), surface.get() )
? graphicsQueueFamilyIndex
: queueFamilyProperties.size();
if ( presentQueueFamilyIndex == queueFamilyProperties.size() )
{
// the graphicsQueueFamilyIndex doesn't support present -> look for an other family index that supports both graphics and present
for (size_t i = 0; i < queueFamilyProperties.size(); i++)
// the graphicsQueueFamilyIndex doesn't support present -> look for an other family index that supports both
// graphics and present
for ( size_t i = 0; i < queueFamilyProperties.size(); i++ )
{
if ((queueFamilyProperties[i].queueFlags & vk::QueueFlagBits::eGraphics) && physicalDevice.getSurfaceSupportKHR(static_cast<uint32_t>(i), surface.get()))
if ( ( queueFamilyProperties[i].queueFlags & vk::QueueFlagBits::eGraphics ) &&
physicalDevice.getSurfaceSupportKHR( static_cast<uint32_t>( i ), surface.get() ) )
{
graphicsQueueFamilyIndex = vk::su::checked_cast<uint32_t>(i);
presentQueueFamilyIndex = i;
graphicsQueueFamilyIndex = vk::su::checked_cast<uint32_t>( i );
presentQueueFamilyIndex = i;
break;
}
}
if (presentQueueFamilyIndex == queueFamilyProperties.size())
if ( presentQueueFamilyIndex == queueFamilyProperties.size() )
{
// there's nothing like a single family index that supports both graphics and present -> look for an other family index that supports present
for (size_t i = 0; i < queueFamilyProperties.size(); i++)
// there's nothing like a single family index that supports both graphics and present -> look for an other
// family index that supports present
for ( size_t i = 0; i < queueFamilyProperties.size(); i++ )
{
if (physicalDevice.getSurfaceSupportKHR(static_cast<uint32_t>(i), surface.get()))
if ( physicalDevice.getSurfaceSupportKHR( static_cast<uint32_t>( i ), surface.get() ) )
{
presentQueueFamilyIndex = i;
break;
@@ -77,26 +84,31 @@ int main(int /*argc*/, char ** /*argv*/)
}
}
}
if ((graphicsQueueFamilyIndex == queueFamilyProperties.size()) || (presentQueueFamilyIndex == queueFamilyProperties.size()))
if ( ( graphicsQueueFamilyIndex == queueFamilyProperties.size() ) ||
( presentQueueFamilyIndex == queueFamilyProperties.size() ) )
{
throw std::runtime_error("Could not find a queue for graphics or present -> terminating");
throw std::runtime_error( "Could not find a queue for graphics or present -> terminating" );
}
// create a device
vk::UniqueDevice device = vk::su::createDevice(physicalDevice, graphicsQueueFamilyIndex, vk::su::getDeviceExtensions());
vk::UniqueDevice device =
vk::su::createDevice( physicalDevice, graphicsQueueFamilyIndex, vk::su::getDeviceExtensions() );
// get the supported VkFormats
std::vector<vk::SurfaceFormatKHR> formats = physicalDevice.getSurfaceFormatsKHR(surface.get());
assert(!formats.empty());
vk::Format format = (formats[0].format == vk::Format::eUndefined) ? vk::Format::eB8G8R8A8Unorm : formats[0].format;
std::vector<vk::SurfaceFormatKHR> formats = physicalDevice.getSurfaceFormatsKHR( surface.get() );
assert( !formats.empty() );
vk::Format format =
( formats[0].format == vk::Format::eUndefined ) ? vk::Format::eB8G8R8A8Unorm : formats[0].format;
vk::SurfaceCapabilitiesKHR surfaceCapabilities = physicalDevice.getSurfaceCapabilitiesKHR(surface.get());
VkExtent2D swapchainExtent;
if (surfaceCapabilities.currentExtent.width == std::numeric_limits<uint32_t>::max())
vk::SurfaceCapabilitiesKHR surfaceCapabilities = physicalDevice.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 = vk::su::clamp(width, surfaceCapabilities.minImageExtent.width, surfaceCapabilities.maxImageExtent.width);
swapchainExtent.height = vk::su::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
{
@@ -107,38 +119,63 @@ int main(int /*argc*/, char ** /*argv*/)
// The FIFO present mode is guaranteed by the spec to be supported
vk::PresentModeKHR swapchainPresentMode = vk::PresentModeKHR::eFifo;
vk::SurfaceTransformFlagBitsKHR preTransform = (surfaceCapabilities.supportedTransforms & vk::SurfaceTransformFlagBitsKHR::eIdentity) ? vk::SurfaceTransformFlagBitsKHR::eIdentity : surfaceCapabilities.currentTransform;
vk::SurfaceTransformFlagBitsKHR preTransform =
( surfaceCapabilities.supportedTransforms & vk::SurfaceTransformFlagBitsKHR::eIdentity )
? vk::SurfaceTransformFlagBitsKHR::eIdentity
: surfaceCapabilities.currentTransform;
vk::CompositeAlphaFlagBitsKHR compositeAlpha =
(surfaceCapabilities.supportedCompositeAlpha & vk::CompositeAlphaFlagBitsKHR::ePreMultiplied) ? vk::CompositeAlphaFlagBitsKHR::ePreMultiplied :
(surfaceCapabilities.supportedCompositeAlpha & vk::CompositeAlphaFlagBitsKHR::ePostMultiplied) ? vk::CompositeAlphaFlagBitsKHR::ePostMultiplied :
(surfaceCapabilities.supportedCompositeAlpha & vk::CompositeAlphaFlagBitsKHR::eInherit) ? vk::CompositeAlphaFlagBitsKHR::eInherit : vk::CompositeAlphaFlagBitsKHR::eOpaque;
( surfaceCapabilities.supportedCompositeAlpha & vk::CompositeAlphaFlagBitsKHR::ePreMultiplied )
? vk::CompositeAlphaFlagBitsKHR::ePreMultiplied
: ( surfaceCapabilities.supportedCompositeAlpha & vk::CompositeAlphaFlagBitsKHR::ePostMultiplied )
? vk::CompositeAlphaFlagBitsKHR::ePostMultiplied
: ( surfaceCapabilities.supportedCompositeAlpha & vk::CompositeAlphaFlagBitsKHR::eInherit )
? vk::CompositeAlphaFlagBitsKHR::eInherit
: vk::CompositeAlphaFlagBitsKHR::eOpaque;
vk::SwapchainCreateInfoKHR swapChainCreateInfo(vk::SwapchainCreateFlagsKHR(), surface.get(), surfaceCapabilities.minImageCount, format, vk::ColorSpaceKHR::eSrgbNonlinear,
swapchainExtent, 1, vk::ImageUsageFlagBits::eColorAttachment, vk::SharingMode::eExclusive, 0, nullptr, preTransform, compositeAlpha, swapchainPresentMode, true, nullptr);
vk::SwapchainCreateInfoKHR swapChainCreateInfo( vk::SwapchainCreateFlagsKHR(),
surface.get(),
surfaceCapabilities.minImageCount,
format,
vk::ColorSpaceKHR::eSrgbNonlinear,
swapchainExtent,
1,
vk::ImageUsageFlagBits::eColorAttachment,
vk::SharingMode::eExclusive,
0,
nullptr,
preTransform,
compositeAlpha,
swapchainPresentMode,
true,
nullptr );
uint32_t queueFamilyIndices[2] = { static_cast<uint32_t>(graphicsQueueFamilyIndex), static_cast<uint32_t>(presentQueueFamilyIndex) };
if (graphicsQueueFamilyIndex != presentQueueFamilyIndex)
uint32_t queueFamilyIndices[2] = { static_cast<uint32_t>( graphicsQueueFamilyIndex ),
static_cast<uint32_t>( presentQueueFamilyIndex ) };
if ( graphicsQueueFamilyIndex != presentQueueFamilyIndex )
{
// If the graphics and present queues are from different queue families, we either have to explicitly transfer ownership of images between
// the queues, or we have to create the swapchain with imageSharingMode as VK_SHARING_MODE_CONCURRENT
swapChainCreateInfo.imageSharingMode = vk::SharingMode::eConcurrent;
// If the graphics and present queues are from different queue families, we either have to explicitly transfer
// ownership of images between the queues, or we have to create the swapchain with imageSharingMode as
// VK_SHARING_MODE_CONCURRENT
swapChainCreateInfo.imageSharingMode = vk::SharingMode::eConcurrent;
swapChainCreateInfo.queueFamilyIndexCount = 2;
swapChainCreateInfo.pQueueFamilyIndices = queueFamilyIndices;
swapChainCreateInfo.pQueueFamilyIndices = queueFamilyIndices;
}
vk::UniqueSwapchainKHR swapChain = device->createSwapchainKHRUnique(swapChainCreateInfo);
vk::UniqueSwapchainKHR swapChain = device->createSwapchainKHRUnique( swapChainCreateInfo );
std::vector<vk::Image> swapChainImages = device->getSwapchainImagesKHR(swapChain.get());
std::vector<vk::Image> swapChainImages = device->getSwapchainImagesKHR( swapChain.get() );
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)
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::ImageViewCreateInfo imageViewCreateInfo(vk::ImageViewCreateFlags(), image, vk::ImageViewType::e2D, format, componentMapping, subResourceRange);
imageViews.push_back(device->createImageViewUnique(imageViewCreateInfo));
vk::ImageViewCreateInfo imageViewCreateInfo(
vk::ImageViewCreateFlags(), image, vk::ImageViewType::e2D, format, componentMapping, subResourceRange );
imageViews.push_back( device->createImageViewUnique( imageViewCreateInfo ) );
}
// Note: No need to explicitly destroy the ImageViews or the swapChain, as the corresponding destroy
@@ -146,20 +183,20 @@ int main(int /*argc*/, char ** /*argv*/)
/* VULKAN_HPP_KEY_END */
}
catch (vk::SystemError& err)
catch ( vk::SystemError & err )
{
std::cout << "vk::SystemError: " << err.what() << std::endl;
exit(-1);
exit( -1 );
}
catch (std::runtime_error& err)
catch ( std::runtime_error & err )
{
std::cout << "std::runtime_error: " << err.what() << std::endl;
exit(-1);
exit( -1 );
}
catch (...)
catch ( ... )
{
std::cout << "unknown error\n";
exit(-1);
exit( -1 );
}
return 0;
}