2019-03-05 07:59:40 +00:00
|
|
|
// Copyright(c) 2018-2019, NVIDIA CORPORATION. All rights reserved.
|
2018-03-31 08:09:50 +00:00
|
|
|
//
|
|
|
|
// Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
// you may not use this file except in compliance with the License.
|
|
|
|
// You may obtain a copy of the License at
|
|
|
|
//
|
|
|
|
// http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
//
|
|
|
|
// Unless required by applicable law or agreed to in writing, software
|
|
|
|
// distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
// See the License for the specific language governing permissions and
|
|
|
|
// limitations under the License.
|
|
|
|
//
|
|
|
|
// VulkanHpp Samples : 05_InitSwapchain
|
|
|
|
// Initialize a swapchain
|
|
|
|
|
2019-03-19 14:35:08 +00:00
|
|
|
#include "../utils/utils.hpp"
|
2018-03-31 08:09:50 +00:00
|
|
|
#include "vulkan/vulkan.hpp"
|
2020-04-12 19:49:12 +00:00
|
|
|
|
2018-03-31 08:09:50 +00:00
|
|
|
#include <iostream>
|
|
|
|
|
2020-04-12 19:49:12 +00:00
|
|
|
static char const * AppName = "05_InitSwapchain";
|
|
|
|
static char const * EngineName = "Vulkan.hpp";
|
2018-03-31 08:09:50 +00:00
|
|
|
|
2020-04-12 19:49:12 +00:00
|
|
|
int main( int /*argc*/, char ** /*argv*/ )
|
2018-03-31 08:09:50 +00:00
|
|
|
{
|
|
|
|
try
|
|
|
|
{
|
2020-04-12 19:49:12 +00:00
|
|
|
vk::UniqueInstance instance = vk::su::createInstance( AppName, EngineName, {}, vk::su::getInstanceExtensions() );
|
|
|
|
#if !defined( NDEBUG )
|
|
|
|
vk::UniqueDebugUtilsMessengerEXT debugUtilsMessenger = vk::su::createDebugUtilsMessenger( instance );
|
2019-03-05 07:59:40 +00:00
|
|
|
#endif
|
2018-03-31 08:09:50 +00:00
|
|
|
|
2019-06-25 07:47:27 +00:00
|
|
|
vk::PhysicalDevice physicalDevice = instance->enumeratePhysicalDevices().front();
|
2018-03-31 08:09:50 +00:00
|
|
|
|
2019-06-25 07:47:27 +00:00
|
|
|
std::vector<vk::QueueFamilyProperties> queueFamilyProperties = physicalDevice.getQueueFamilyProperties();
|
2020-04-12 19:49:12 +00:00
|
|
|
uint32_t graphicsQueueFamilyIndex = vk::su::findGraphicsQueueFamilyIndex( queueFamilyProperties );
|
2019-03-05 07:59:40 +00:00
|
|
|
|
2018-03-31 08:09:50 +00:00
|
|
|
/* VULKAN_HPP_KEY_START */
|
|
|
|
|
2020-04-12 19:49:12 +00:00
|
|
|
uint32_t width = 64;
|
|
|
|
uint32_t height = 64;
|
|
|
|
vk::su::WindowData window = vk::su::createWindow( AppName, { width, height } );
|
2020-01-28 09:16:10 +00:00
|
|
|
vk::UniqueSurfaceKHR surface;
|
|
|
|
{
|
|
|
|
VkSurfaceKHR _surface;
|
2020-04-12 19:49:12 +00:00
|
|
|
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 );
|
2020-01-28 09:16:10 +00:00
|
|
|
}
|
2018-03-31 08:09:50 +00:00
|
|
|
|
|
|
|
// determine a queueFamilyIndex that suports present
|
|
|
|
// first check if the graphicsQueueFamiliyIndex is good enough
|
2020-04-12 19:49:12 +00:00
|
|
|
size_t presentQueueFamilyIndex =
|
|
|
|
physicalDevice.getSurfaceSupportKHR( static_cast<uint32_t>( graphicsQueueFamilyIndex ), surface.get() )
|
|
|
|
? graphicsQueueFamilyIndex
|
|
|
|
: queueFamilyProperties.size();
|
|
|
|
if ( presentQueueFamilyIndex == queueFamilyProperties.size() )
|
2018-03-31 08:09:50 +00:00
|
|
|
{
|
2020-04-12 19:49:12 +00:00
|
|
|
// 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++ )
|
2018-03-31 08:09:50 +00:00
|
|
|
{
|
2020-04-12 19:49:12 +00:00
|
|
|
if ( ( queueFamilyProperties[i].queueFlags & vk::QueueFlagBits::eGraphics ) &&
|
|
|
|
physicalDevice.getSurfaceSupportKHR( static_cast<uint32_t>( i ), surface.get() ) )
|
2018-03-31 08:09:50 +00:00
|
|
|
{
|
2020-04-12 19:49:12 +00:00
|
|
|
graphicsQueueFamilyIndex = vk::su::checked_cast<uint32_t>( i );
|
|
|
|
presentQueueFamilyIndex = i;
|
2018-03-31 08:09:50 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
2020-04-12 19:49:12 +00:00
|
|
|
if ( presentQueueFamilyIndex == queueFamilyProperties.size() )
|
2018-03-31 08:09:50 +00:00
|
|
|
{
|
2020-04-12 19:49:12 +00:00
|
|
|
// 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++ )
|
2018-03-31 08:09:50 +00:00
|
|
|
{
|
2020-04-12 19:49:12 +00:00
|
|
|
if ( physicalDevice.getSurfaceSupportKHR( static_cast<uint32_t>( i ), surface.get() ) )
|
2018-03-31 08:09:50 +00:00
|
|
|
{
|
|
|
|
presentQueueFamilyIndex = i;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2020-04-12 19:49:12 +00:00
|
|
|
if ( ( graphicsQueueFamilyIndex == queueFamilyProperties.size() ) ||
|
|
|
|
( presentQueueFamilyIndex == queueFamilyProperties.size() ) )
|
2018-03-31 08:09:50 +00:00
|
|
|
{
|
2020-04-12 19:49:12 +00:00
|
|
|
throw std::runtime_error( "Could not find a queue for graphics or present -> terminating" );
|
2018-03-31 08:09:50 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// create a device
|
2020-04-12 19:49:12 +00:00
|
|
|
vk::UniqueDevice device =
|
|
|
|
vk::su::createDevice( physicalDevice, graphicsQueueFamilyIndex, vk::su::getDeviceExtensions() );
|
2018-03-31 08:09:50 +00:00
|
|
|
|
|
|
|
// get the supported VkFormats
|
2020-04-12 19:49:12 +00:00
|
|
|
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() )
|
2018-03-31 08:09:50 +00:00
|
|
|
{
|
|
|
|
// If the surface size is undefined, the size is set to the size of the images requested.
|
2020-04-12 19:49:12 +00:00
|
|
|
swapchainExtent.width =
|
|
|
|
vk::su::clamp( width, surfaceCapabilities.minImageExtent.width, surfaceCapabilities.maxImageExtent.width );
|
|
|
|
swapchainExtent.height =
|
|
|
|
vk::su::clamp( height, surfaceCapabilities.minImageExtent.height, surfaceCapabilities.maxImageExtent.height );
|
2018-03-31 08:09:50 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
// If the surface size is defined, the swap chain size must match
|
|
|
|
swapchainExtent = surfaceCapabilities.currentExtent;
|
|
|
|
}
|
|
|
|
|
|
|
|
// The FIFO present mode is guaranteed by the spec to be supported
|
|
|
|
vk::PresentModeKHR swapchainPresentMode = vk::PresentModeKHR::eFifo;
|
|
|
|
|
2020-04-12 19:49:12 +00:00
|
|
|
vk::SurfaceTransformFlagBitsKHR preTransform =
|
|
|
|
( surfaceCapabilities.supportedTransforms & vk::SurfaceTransformFlagBitsKHR::eIdentity )
|
|
|
|
? vk::SurfaceTransformFlagBitsKHR::eIdentity
|
|
|
|
: surfaceCapabilities.currentTransform;
|
2018-03-31 08:09:50 +00:00
|
|
|
|
|
|
|
vk::CompositeAlphaFlagBitsKHR compositeAlpha =
|
2020-04-12 19:49:12 +00:00
|
|
|
( 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,
|
2020-07-08 08:58:37 +00:00
|
|
|
{},
|
2020-04-12 19:49:12 +00:00
|
|
|
preTransform,
|
|
|
|
compositeAlpha,
|
|
|
|
swapchainPresentMode,
|
|
|
|
true,
|
|
|
|
nullptr );
|
|
|
|
|
|
|
|
uint32_t queueFamilyIndices[2] = { static_cast<uint32_t>( graphicsQueueFamilyIndex ),
|
|
|
|
static_cast<uint32_t>( presentQueueFamilyIndex ) };
|
|
|
|
if ( graphicsQueueFamilyIndex != presentQueueFamilyIndex )
|
2018-03-31 08:09:50 +00:00
|
|
|
{
|
2020-04-12 19:49:12 +00:00
|
|
|
// 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;
|
2018-03-31 08:09:50 +00:00
|
|
|
swapChainCreateInfo.queueFamilyIndexCount = 2;
|
2020-04-12 19:49:12 +00:00
|
|
|
swapChainCreateInfo.pQueueFamilyIndices = queueFamilyIndices;
|
2018-03-31 08:09:50 +00:00
|
|
|
}
|
|
|
|
|
2020-04-12 19:49:12 +00:00
|
|
|
vk::UniqueSwapchainKHR swapChain = device->createSwapchainKHRUnique( swapChainCreateInfo );
|
2018-03-31 08:09:50 +00:00
|
|
|
|
2020-04-12 19:49:12 +00:00
|
|
|
std::vector<vk::Image> swapChainImages = device->getSwapchainImagesKHR( swapChain.get() );
|
2018-03-31 08:09:50 +00:00
|
|
|
|
|
|
|
std::vector<vk::UniqueImageView> imageViews;
|
2020-04-12 19:49:12 +00:00
|
|
|
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 )
|
2018-03-31 08:09:50 +00:00
|
|
|
{
|
2020-04-12 19:49:12 +00:00
|
|
|
vk::ImageViewCreateInfo imageViewCreateInfo(
|
|
|
|
vk::ImageViewCreateFlags(), image, vk::ImageViewType::e2D, format, componentMapping, subResourceRange );
|
|
|
|
imageViews.push_back( device->createImageViewUnique( imageViewCreateInfo ) );
|
2018-03-31 08:09:50 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Note: No need to explicitly destroy the ImageViews or the swapChain, as the corresponding destroy
|
|
|
|
// functions are called by the destructor of the UniqueImageView and the UniqueSwapChainKHR on leaving this scope.
|
|
|
|
|
|
|
|
/* VULKAN_HPP_KEY_END */
|
|
|
|
}
|
2020-04-12 19:49:12 +00:00
|
|
|
catch ( vk::SystemError & err )
|
2018-03-31 08:09:50 +00:00
|
|
|
{
|
|
|
|
std::cout << "vk::SystemError: " << err.what() << std::endl;
|
2020-04-12 19:49:12 +00:00
|
|
|
exit( -1 );
|
2018-03-31 08:09:50 +00:00
|
|
|
}
|
2020-07-08 08:58:37 +00:00
|
|
|
catch ( std::exception & err )
|
2018-03-31 08:09:50 +00:00
|
|
|
{
|
2020-07-08 08:58:37 +00:00
|
|
|
std::cout << "std::exception: " << err.what() << std::endl;
|
2020-04-12 19:49:12 +00:00
|
|
|
exit( -1 );
|
2018-03-31 08:09:50 +00:00
|
|
|
}
|
2020-04-12 19:49:12 +00:00
|
|
|
catch ( ... )
|
2018-03-31 08:09:50 +00:00
|
|
|
{
|
|
|
|
std::cout << "unknown error\n";
|
2020-04-12 19:49:12 +00:00
|
|
|
exit( -1 );
|
2018-03-31 08:09:50 +00:00
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|