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"
|
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
|
|
|
|
{
|
2021-02-17 09:49:59 +00:00
|
|
|
vk::Instance instance = vk::su::createInstance( AppName, EngineName, {}, vk::su::getInstanceExtensions() );
|
2020-04-12 19:49:12 +00:00
|
|
|
#if !defined( NDEBUG )
|
2022-02-28 09:11:04 +00:00
|
|
|
vk::DebugUtilsMessengerEXT debugUtilsMessenger = instance.createDebugUtilsMessengerEXT( vk::su::makeDebugUtilsMessengerCreateInfoEXT() );
|
2019-03-05 07:59:40 +00:00
|
|
|
#endif
|
2018-03-31 08:09:50 +00:00
|
|
|
|
2021-02-17 09:49:59 +00:00
|
|
|
vk::PhysicalDevice physicalDevice = instance.enumeratePhysicalDevices().front();
|
2018-03-31 08:09:50 +00:00
|
|
|
|
2022-02-28 09:11:04 +00:00
|
|
|
std::vector<vk::QueueFamilyProperties> queueFamilyProperties = physicalDevice.getQueueFamilyProperties();
|
|
|
|
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 */
|
|
|
|
|
2021-02-17 09:49:59 +00:00
|
|
|
uint32_t width = 64;
|
|
|
|
uint32_t height = 64;
|
|
|
|
vk::su::WindowData window = vk::su::createWindow( AppName, { width, height } );
|
|
|
|
vk::SurfaceKHR surface;
|
2020-01-28 09:16:10 +00:00
|
|
|
{
|
|
|
|
VkSurfaceKHR _surface;
|
2021-02-17 09:49:59 +00:00
|
|
|
glfwCreateWindowSurface( static_cast<VkInstance>( instance ), window.handle, nullptr, &_surface );
|
|
|
|
surface = vk::SurfaceKHR( _surface );
|
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
|
2022-02-28 09:11:04 +00:00
|
|
|
size_t presentQueueFamilyIndex = physicalDevice.getSurfaceSupportKHR( static_cast<uint32_t>( graphicsQueueFamilyIndex ), surface )
|
2022-08-10 09:19:06 +00:00
|
|
|
? graphicsQueueFamilyIndex
|
|
|
|
: queueFamilyProperties.size();
|
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
|
|
|
// 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 ) &&
|
2021-02-17 09:49:59 +00:00
|
|
|
physicalDevice.getSurfaceSupportKHR( static_cast<uint32_t>( i ), surface ) )
|
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
|
|
|
{
|
2021-02-17 09:49:59 +00:00
|
|
|
if ( physicalDevice.getSurfaceSupportKHR( static_cast<uint32_t>( i ), surface ) )
|
2018-03-31 08:09:50 +00:00
|
|
|
{
|
|
|
|
presentQueueFamilyIndex = i;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2022-02-28 09:11:04 +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
|
2021-02-17 09:49:59 +00:00
|
|
|
vk::Device device = vk::su::createDevice( physicalDevice, graphicsQueueFamilyIndex, vk::su::getDeviceExtensions() );
|
2018-03-31 08:09:50 +00:00
|
|
|
|
|
|
|
// get the supported VkFormats
|
2021-02-17 09:49:59 +00:00
|
|
|
std::vector<vk::SurfaceFormatKHR> formats = physicalDevice.getSurfaceFormatsKHR( surface );
|
2020-04-12 19:49:12 +00:00
|
|
|
assert( !formats.empty() );
|
2022-02-28 09:11:04 +00:00
|
|
|
vk::Format format = ( formats[0].format == vk::Format::eUndefined ) ? vk::Format::eB8G8R8A8Unorm : formats[0].format;
|
2020-04-12 19:49:12 +00:00
|
|
|
|
2021-02-17 09:49:59 +00:00
|
|
|
vk::SurfaceCapabilitiesKHR surfaceCapabilities = physicalDevice.getSurfaceCapabilitiesKHR( surface );
|
2021-12-07 09:39:23 +00:00
|
|
|
vk::Extent2D swapchainExtent;
|
2020-04-12 19:49:12 +00:00
|
|
|
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.
|
2022-02-28 09:11:04 +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;
|
|
|
|
|
2022-02-28 09:11:04 +00:00
|
|
|
vk::SurfaceTransformFlagBitsKHR preTransform = ( surfaceCapabilities.supportedTransforms & vk::SurfaceTransformFlagBitsKHR::eIdentity )
|
2022-08-10 09:19:06 +00:00
|
|
|
? vk::SurfaceTransformFlagBitsKHR::eIdentity
|
|
|
|
: surfaceCapabilities.currentTransform;
|
2018-03-31 08:09:50 +00:00
|
|
|
|
|
|
|
vk::CompositeAlphaFlagBitsKHR compositeAlpha =
|
2022-02-28 09:11:04 +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;
|
2020-04-12 19:49:12 +00:00
|
|
|
|
|
|
|
vk::SwapchainCreateInfoKHR swapChainCreateInfo( vk::SwapchainCreateFlagsKHR(),
|
2021-02-17 09:49:59 +00:00
|
|
|
surface,
|
2020-04-12 19:49:12 +00:00
|
|
|
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 );
|
|
|
|
|
2022-02-28 09:11:04 +00:00
|
|
|
uint32_t queueFamilyIndices[2] = { static_cast<uint32_t>( graphicsQueueFamilyIndex ), static_cast<uint32_t>( presentQueueFamilyIndex ) };
|
2020-04-12 19:49:12 +00:00
|
|
|
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
|
|
|
}
|
|
|
|
|
2021-02-17 09:49:59 +00:00
|
|
|
vk::SwapchainKHR swapChain = device.createSwapchainKHR( swapChainCreateInfo );
|
2018-03-31 08:09:50 +00:00
|
|
|
|
2021-02-17 09:49:59 +00:00
|
|
|
std::vector<vk::Image> swapChainImages = device.getSwapchainImagesKHR( swapChain );
|
2018-03-31 08:09:50 +00:00
|
|
|
|
2021-02-17 09:49:59 +00:00
|
|
|
std::vector<vk::ImageView> imageViews;
|
2020-04-12 19:49:12 +00:00
|
|
|
imageViews.reserve( swapChainImages.size() );
|
2022-02-28 09:11:04 +00:00
|
|
|
vk::ImageViewCreateInfo imageViewCreateInfo( {}, {}, vk::ImageViewType::e2D, format, {}, { vk::ImageAspectFlagBits::eColor, 0, 1, 0, 1 } );
|
2020-04-12 19:49:12 +00:00
|
|
|
for ( auto image : swapChainImages )
|
2018-03-31 08:09:50 +00:00
|
|
|
{
|
2021-11-11 10:49:07 +00:00
|
|
|
imageViewCreateInfo.image = image;
|
2021-02-17 09:49:59 +00:00
|
|
|
imageViews.push_back( device.createImageView( imageViewCreateInfo ) );
|
2018-03-31 08:09:50 +00:00
|
|
|
}
|
|
|
|
|
2021-02-17 09:49:59 +00:00
|
|
|
// destroy the imageViews, the swapChain,and the surface
|
|
|
|
for ( auto & imageView : imageViews )
|
|
|
|
{
|
|
|
|
device.destroyImageView( imageView );
|
|
|
|
}
|
|
|
|
device.destroySwapchainKHR( swapChain );
|
|
|
|
instance.destroySurfaceKHR( surface );
|
2018-03-31 08:09:50 +00:00
|
|
|
|
|
|
|
/* VULKAN_HPP_KEY_END */
|
2021-02-17 09:49:59 +00:00
|
|
|
|
|
|
|
device.destroy();
|
2021-05-25 14:14:56 +00:00
|
|
|
#if !defined( NDEBUG )
|
2021-02-17 09:49:59 +00:00
|
|
|
instance.destroyDebugUtilsMessengerEXT( debugUtilsMessenger );
|
2021-05-25 14:14:56 +00:00
|
|
|
#endif
|
2021-02-17 09:49:59 +00:00
|
|
|
instance.destroy();
|
2018-03-31 08:09:50 +00:00
|
|
|
}
|
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;
|
|
|
|
}
|