Refactor RAII-samples to use std::unique_ptr only if needed.

This commit is contained in:
asuessenbach 2021-06-21 11:32:43 +02:00
parent da2ecae452
commit 69e8e5bf84
52 changed files with 2171 additions and 2437 deletions

View File

@ -15,8 +15,9 @@
// VulkanHpp Samples : 01_InitInstanceRAII // VulkanHpp Samples : 01_InitInstanceRAII
// Create and destroy a vk::UniqueInstance // Create and destroy a vk::UniqueInstance
#include "vulkan/vulkan_raii.hpp"
#include "../utils/utils.hpp" #include "../utils/utils.hpp"
#include "vulkan/vulkan_raii.hpp"
#include <iostream> #include <iostream>
static std::string AppName = "01_InitInstanceRAII"; static std::string AppName = "01_InitInstanceRAII";
@ -29,7 +30,7 @@ int main( int /*argc*/, char ** /*argv*/ )
try try
{ {
// the very beginning: instantiate a context // the very beginning: instantiate a context
std::unique_ptr<vk::raii::Context> context = vk::raii::su::make_unique<vk::raii::Context>(); vk::raii::Context context;
// initialize the vk::ApplicationInfo structure // initialize the vk::ApplicationInfo structure
vk::ApplicationInfo applicationInfo( AppName.c_str(), 1, EngineName.c_str(), 1, VK_API_VERSION_1_1 ); vk::ApplicationInfo applicationInfo( AppName.c_str(), 1, EngineName.c_str(), 1, VK_API_VERSION_1_1 );
@ -38,7 +39,7 @@ int main( int /*argc*/, char ** /*argv*/ )
vk::InstanceCreateInfo instanceCreateInfo( {}, &applicationInfo ); vk::InstanceCreateInfo instanceCreateInfo( {}, &applicationInfo );
// create an Instance // create an Instance
std::unique_ptr<vk::raii::Instance> instance = vk::raii::su::make_unique<vk::raii::Instance>( *context, instanceCreateInfo ); vk::raii::Instance instance( context, instanceCreateInfo );
} }
catch ( vk::SystemError & err ) catch ( vk::SystemError & err )
{ {

View File

@ -36,17 +36,16 @@ int main( int /*argc*/, char ** /*argv*/ )
{ {
try try
{ {
std::unique_ptr<vk::raii::Context> context = vk::raii::su::make_unique<vk::raii::Context>(); vk::raii::Context context;
std::unique_ptr<vk::raii::Instance> instance = vk::raii::su::makeUniqueInstance( *context, AppName, EngineName ); vk::raii::Instance instance = vk::raii::su::makeInstance( context, AppName, EngineName );
#if !defined( NDEBUG ) #if !defined( NDEBUG )
std::unique_ptr<vk::raii::DebugUtilsMessengerEXT> debugUtilsMessenger = vk::raii::DebugUtilsMessengerEXT debugUtilsMessenger( instance, vk::su::makeDebugUtilsMessengerCreateInfoEXT() );
vk::raii::su::makeUniqueDebugUtilsMessengerEXT( *instance );
#endif #endif
/* VULKAN_HPP_KEY_START */ /* VULKAN_HPP_KEY_START */
// enumerate the physicalDevices // enumerate the physicalDevices
vk::raii::PhysicalDevices physicalDevices( *instance ); vk::raii::PhysicalDevices physicalDevices( instance );
/* VULKAN_HPP_KEY_END */ /* VULKAN_HPP_KEY_END */
} }

View File

@ -26,26 +26,25 @@ int main( int /*argc*/, char ** /*argv*/ )
{ {
try try
{ {
std::unique_ptr<vk::raii::Context> context = vk::raii::su::make_unique<vk::raii::Context>(); vk::raii::Context context;
std::unique_ptr<vk::raii::Instance> instance = vk::raii::su::makeUniqueInstance( *context, AppName, EngineName ); vk::raii::Instance instance = vk::raii::su::makeInstance( context, AppName, EngineName );
#if !defined( NDEBUG ) #if !defined( NDEBUG )
std::unique_ptr<vk::raii::DebugUtilsMessengerEXT> debugUtilsMessenger = vk::raii::DebugUtilsMessengerEXT debugUtilsMessenger( instance, vk::su::makeDebugUtilsMessengerCreateInfoEXT() );
vk::raii::su::makeUniqueDebugUtilsMessengerEXT( *instance );
#endif #endif
std::unique_ptr<vk::raii::PhysicalDevice> physicalDevice = vk::raii::su::makeUniquePhysicalDevice( *instance ); vk::raii::PhysicalDevice physicalDevice = std::move( vk::raii::PhysicalDevices( instance ).front() );
/* VULKAN_HPP_KEY_START */ /* VULKAN_HPP_KEY_START */
// find the index of the first queue family that supports graphics // find the index of the first queue family that supports graphics
uint32_t graphicsQueueFamilyIndex = uint32_t graphicsQueueFamilyIndex =
vk::su::findGraphicsQueueFamilyIndex( physicalDevice->getQueueFamilyProperties() ); vk::su::findGraphicsQueueFamilyIndex( physicalDevice.getQueueFamilyProperties() );
// create a Device // create a Device
float queuePriority = 0.0f; float queuePriority = 0.0f;
vk::DeviceQueueCreateInfo deviceQueueCreateInfo( {}, graphicsQueueFamilyIndex, 1, &queuePriority ); vk::DeviceQueueCreateInfo deviceQueueCreateInfo( {}, graphicsQueueFamilyIndex, 1, &queuePriority );
vk::DeviceCreateInfo deviceCreateInfo( {}, deviceQueueCreateInfo ); vk::DeviceCreateInfo deviceCreateInfo( {}, deviceQueueCreateInfo );
std::unique_ptr<vk::raii::Device> device = vk::raii::su::make_unique<vk::raii::Device>( *physicalDevice, deviceCreateInfo ); vk::raii::Device device( physicalDevice, deviceCreateInfo );
/* VULKAN_HPP_KEY_END */ /* VULKAN_HPP_KEY_END */
} }

View File

@ -26,29 +26,27 @@ int main( int /*argc*/, char ** /*argv*/ )
{ {
try try
{ {
std::unique_ptr<vk::raii::Context> context = vk::raii::su::make_unique<vk::raii::Context>(); vk::raii::Context context;
std::unique_ptr<vk::raii::Instance> instance = vk::raii::su::makeUniqueInstance( *context, AppName, EngineName ); vk::raii::Instance instance = vk::raii::su::makeInstance( context, AppName, EngineName );
#if !defined( NDEBUG ) #if !defined( NDEBUG )
std::unique_ptr<vk::raii::DebugUtilsMessengerEXT> debugUtilsMessenger = vk::raii::DebugUtilsMessengerEXT debugUtilsMessenger( instance, vk::su::makeDebugUtilsMessengerCreateInfoEXT() );
vk::raii::su::makeUniqueDebugUtilsMessengerEXT( *instance );
#endif #endif
std::unique_ptr<vk::raii::PhysicalDevice> physicalDevice = vk::raii::su::makeUniquePhysicalDevice( *instance ); vk::raii::PhysicalDevice physicalDevice = std::move( vk::raii::PhysicalDevices( instance ).front() );
uint32_t graphicsQueueFamilyIndex =
vk::su::findGraphicsQueueFamilyIndex( physicalDevice->getQueueFamilyProperties() ); uint32_t graphicsQueueFamilyIndex =
std::unique_ptr<vk::raii::Device> device = vk::su::findGraphicsQueueFamilyIndex( physicalDevice.getQueueFamilyProperties() );
vk::raii::su::makeUniqueDevice( *physicalDevice, graphicsQueueFamilyIndex ); vk::raii::Device device = vk::raii::su::makeDevice( physicalDevice, graphicsQueueFamilyIndex );
/* VULKAN_HPP_KEY_START */ /* VULKAN_HPP_KEY_START */
// create a CommandPool to allocate a CommandBuffer from // create a CommandPool to allocate a CommandBuffer from
vk::CommandPoolCreateInfo commandPoolCreateInfo( {}, graphicsQueueFamilyIndex ); vk::CommandPoolCreateInfo commandPoolCreateInfo( {}, graphicsQueueFamilyIndex );
std::unique_ptr<vk::raii::CommandPool> commandPool = vk::raii::CommandPool commandPool( device, commandPoolCreateInfo );
vk::raii::su::make_unique<vk::raii::CommandPool>( *device, commandPoolCreateInfo );
// allocate a CommandBuffer from the CommandPool // allocate a CommandBuffer from the CommandPool
vk::CommandBufferAllocateInfo commandBufferAllocateInfo( **commandPool, vk::CommandBufferLevel::ePrimary, 1 ); vk::CommandBufferAllocateInfo commandBufferAllocateInfo( *commandPool, vk::CommandBufferLevel::ePrimary, 1 );
std::unique_ptr<vk::raii::CommandBuffer> commandBuffer = vk::raii::su::make_unique<vk::raii::CommandBuffer>( vk::raii::CommandBuffer commandBuffer =
std::move( vk::raii::CommandBuffers( *device, commandBufferAllocateInfo ).front() ) ); std::move( vk::raii::CommandBuffers( device, commandBufferAllocateInfo ).front() );
/* VULKAN_HPP_KEY_END */ /* VULKAN_HPP_KEY_END */
} }

View File

@ -26,15 +26,15 @@ int main( int /*argc*/, char ** /*argv*/ )
{ {
try try
{ {
std::unique_ptr<vk::raii::Context> context = vk::raii::su::make_unique<vk::raii::Context>(); vk::raii::Context context;
std::unique_ptr<vk::raii::Instance> instance = vk::raii::Instance instance =
vk::raii::su::makeUniqueInstance( *context, AppName, EngineName, {}, vk::su::getInstanceExtensions() ); vk::raii::su::makeInstance( context, AppName, EngineName, {}, vk::su::getInstanceExtensions() );
#if !defined( NDEBUG ) #if !defined( NDEBUG )
std::unique_ptr<vk::raii::DebugUtilsMessengerEXT> debugUtilsMessenger = vk::raii::DebugUtilsMessengerEXT debugUtilsMessenger( instance, vk::su::makeDebugUtilsMessengerCreateInfoEXT() );
vk::raii::su::makeUniqueDebugUtilsMessengerEXT( *instance );
#endif #endif
std::unique_ptr<vk::raii::PhysicalDevice> physicalDevice = vk::raii::su::makeUniquePhysicalDevice( *instance ); vk::raii::PhysicalDevice physicalDevice = std::move( vk::raii::PhysicalDevices( instance ).front() );
std::vector<vk::QueueFamilyProperties> queueFamilyProperties = physicalDevice->getQueueFamilyProperties();
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 */ /* VULKAN_HPP_KEY_START */
@ -43,12 +43,12 @@ int main( int /*argc*/, char ** /*argv*/ )
uint32_t height = 64; uint32_t height = 64;
vk::su::WindowData window = vk::su::createWindow( AppName, { width, height } ); vk::su::WindowData window = vk::su::createWindow( AppName, { width, height } );
VkSurfaceKHR _surface; VkSurfaceKHR _surface;
glfwCreateWindowSurface( static_cast<VkInstance>( **instance ), window.handle, nullptr, &_surface ); glfwCreateWindowSurface( static_cast<VkInstance>( *instance ), window.handle, nullptr, &_surface );
std::unique_ptr<vk::raii::SurfaceKHR> surface = vk::raii::su::make_unique<vk::raii::SurfaceKHR>( *instance, _surface ); vk::raii::SurfaceKHR surface( instance, _surface );
// determine a queueFamilyIndex that suports present // determine a queueFamilyIndex that suports present
// first check if the graphicsQueueFamiliyIndex is good enough // first check if the graphicsQueueFamiliyIndex is good enough
uint32_t presentQueueFamilyIndex = physicalDevice->getSurfaceSupportKHR( graphicsQueueFamilyIndex, **surface ) uint32_t presentQueueFamilyIndex = physicalDevice.getSurfaceSupportKHR( graphicsQueueFamilyIndex, *surface )
? graphicsQueueFamilyIndex ? graphicsQueueFamilyIndex
: vk::su::checked_cast<uint32_t>( queueFamilyProperties.size() ); : vk::su::checked_cast<uint32_t>( queueFamilyProperties.size() );
if ( presentQueueFamilyIndex == queueFamilyProperties.size() ) if ( presentQueueFamilyIndex == queueFamilyProperties.size() )
@ -58,7 +58,7 @@ int main( int /*argc*/, char ** /*argv*/ )
for ( size_t i = 0; i < queueFamilyProperties.size(); i++ ) for ( size_t i = 0; i < queueFamilyProperties.size(); i++ )
{ {
if ( ( queueFamilyProperties[i].queueFlags & vk::QueueFlagBits::eGraphics ) && if ( ( queueFamilyProperties[i].queueFlags & vk::QueueFlagBits::eGraphics ) &&
physicalDevice->getSurfaceSupportKHR( vk::su::checked_cast<uint32_t>( i ), **surface ) ) physicalDevice.getSurfaceSupportKHR( vk::su::checked_cast<uint32_t>( i ), *surface ) )
{ {
graphicsQueueFamilyIndex = vk::su::checked_cast<uint32_t>( i ); graphicsQueueFamilyIndex = vk::su::checked_cast<uint32_t>( i );
presentQueueFamilyIndex = graphicsQueueFamilyIndex; presentQueueFamilyIndex = graphicsQueueFamilyIndex;
@ -71,7 +71,7 @@ int main( int /*argc*/, char ** /*argv*/ )
// family index that supports present // family index that supports present
for ( size_t i = 0; i < queueFamilyProperties.size(); i++ ) for ( size_t i = 0; i < queueFamilyProperties.size(); i++ )
{ {
if ( physicalDevice->getSurfaceSupportKHR( vk::su::checked_cast<uint32_t>( i ), **surface ) ) if ( physicalDevice.getSurfaceSupportKHR( vk::su::checked_cast<uint32_t>( i ), *surface ) )
{ {
presentQueueFamilyIndex = vk::su::checked_cast<uint32_t>( i ); presentQueueFamilyIndex = vk::su::checked_cast<uint32_t>( i );
break; break;
@ -86,16 +86,16 @@ int main( int /*argc*/, char ** /*argv*/ )
} }
// create a device // create a device
std::unique_ptr<vk::raii::Device> device = vk::raii::Device device =
vk::raii::su::makeUniqueDevice( *physicalDevice, graphicsQueueFamilyIndex, vk::su::getDeviceExtensions() ); vk::raii::su::makeDevice( physicalDevice, graphicsQueueFamilyIndex, vk::su::getDeviceExtensions() );
// get the supported VkFormats // get the supported VkFormats
std::vector<vk::SurfaceFormatKHR> formats = physicalDevice->getSurfaceFormatsKHR( **surface ); std::vector<vk::SurfaceFormatKHR> formats = physicalDevice.getSurfaceFormatsKHR( *surface );
assert( !formats.empty() ); assert( !formats.empty() );
vk::Format format = vk::Format format =
( formats[0].format == vk::Format::eUndefined ) ? vk::Format::eB8G8R8A8Unorm : formats[0].format; ( formats[0].format == vk::Format::eUndefined ) ? vk::Format::eB8G8R8A8Unorm : formats[0].format;
vk::SurfaceCapabilitiesKHR surfaceCapabilities = physicalDevice->getSurfaceCapabilitiesKHR( **surface ); vk::SurfaceCapabilitiesKHR surfaceCapabilities = physicalDevice.getSurfaceCapabilitiesKHR( *surface );
VkExtent2D swapchainExtent; VkExtent2D swapchainExtent;
if ( surfaceCapabilities.currentExtent.width == std::numeric_limits<uint32_t>::max() ) if ( surfaceCapabilities.currentExtent.width == std::numeric_limits<uint32_t>::max() )
{ {
@ -129,7 +129,7 @@ int main( int /*argc*/, char ** /*argv*/ )
: vk::CompositeAlphaFlagBitsKHR::eOpaque; : vk::CompositeAlphaFlagBitsKHR::eOpaque;
vk::SwapchainCreateInfoKHR swapChainCreateInfo( vk::SwapchainCreateFlagsKHR(), vk::SwapchainCreateInfoKHR swapChainCreateInfo( vk::SwapchainCreateFlagsKHR(),
**surface, *surface,
surfaceCapabilities.minImageCount, surfaceCapabilities.minImageCount,
format, format,
vk::ColorSpaceKHR::eSrgbNonlinear, vk::ColorSpaceKHR::eSrgbNonlinear,
@ -155,11 +155,10 @@ int main( int /*argc*/, char ** /*argv*/ )
swapChainCreateInfo.pQueueFamilyIndices = queueFamilyIndices.data(); swapChainCreateInfo.pQueueFamilyIndices = queueFamilyIndices.data();
} }
std::unique_ptr<vk::raii::SwapchainKHR> swapChain = vk::raii::SwapchainKHR swapChain( device, swapChainCreateInfo );
vk::raii::su::make_unique<vk::raii::SwapchainKHR>( *device, swapChainCreateInfo ); std::vector<VkImage> swapChainImages = swapChain.getImages();
std::vector<VkImage> swapChainImages = swapChain->getImages();
std::vector<std::unique_ptr<vk::raii::ImageView>> imageViews; std::vector<vk::raii::ImageView> imageViews;
imageViews.reserve( swapChainImages.size() ); imageViews.reserve( swapChainImages.size() );
vk::ComponentMapping componentMapping( vk::ComponentMapping componentMapping(
vk::ComponentSwizzle::eR, vk::ComponentSwizzle::eG, vk::ComponentSwizzle::eB, vk::ComponentSwizzle::eA ); vk::ComponentSwizzle::eR, vk::ComponentSwizzle::eG, vk::ComponentSwizzle::eB, vk::ComponentSwizzle::eA );
@ -168,7 +167,7 @@ int main( int /*argc*/, char ** /*argv*/ )
{ {
vk::ImageViewCreateInfo imageViewCreateInfo( vk::ImageViewCreateInfo imageViewCreateInfo(
{}, static_cast<vk::Image>( image ), vk::ImageViewType::e2D, format, componentMapping, subResourceRange ); {}, static_cast<vk::Image>( image ), vk::ImageViewType::e2D, format, componentMapping, subResourceRange );
imageViews.push_back( vk::raii::su::make_unique<vk::raii::ImageView>( *device, imageViewCreateInfo ) ); imageViews.push_back( { device, imageViewCreateInfo } );
} }
/* VULKAN_HPP_KEY_END */ /* VULKAN_HPP_KEY_END */

View File

@ -26,26 +26,25 @@ int main( int /*argc*/, char ** /*argv*/ )
{ {
try try
{ {
std::unique_ptr<vk::raii::Context> context = vk::raii::su::make_unique<vk::raii::Context>(); vk::raii::Context context;
std::unique_ptr<vk::raii::Instance> instance = vk::raii::Instance instance =
vk::raii::su::makeUniqueInstance( *context, AppName, EngineName, {}, vk::su::getInstanceExtensions() ); vk::raii::su::makeInstance( context, AppName, EngineName, {}, vk::su::getInstanceExtensions() );
#if !defined( NDEBUG ) #if !defined( NDEBUG )
std::unique_ptr<vk::raii::DebugUtilsMessengerEXT> debugUtilsMessenger = vk::raii::DebugUtilsMessengerEXT debugUtilsMessenger( instance, vk::su::makeDebugUtilsMessengerCreateInfoEXT() );
vk::raii::su::makeUniqueDebugUtilsMessengerEXT( *instance );
#endif #endif
std::unique_ptr<vk::raii::PhysicalDevice> physicalDevice = vk::raii::su::makeUniquePhysicalDevice( *instance ); vk::raii::PhysicalDevice physicalDevice = std::move( vk::raii::PhysicalDevices( instance ).front() );
vk::raii::su::SurfaceData surfaceData( *instance, AppName, vk::Extent2D( 500, 500 ) ); vk::raii::su::SurfaceData surfaceData( instance, AppName, vk::Extent2D( 500, 500 ) );
std::pair<uint32_t, uint32_t> graphicsAndPresentQueueFamilyIndex = std::pair<uint32_t, uint32_t> graphicsAndPresentQueueFamilyIndex =
vk::raii::su::findGraphicsAndPresentQueueFamilyIndex( *physicalDevice, *surfaceData.surface ); vk::raii::su::findGraphicsAndPresentQueueFamilyIndex( physicalDevice, *surfaceData.pSurface );
std::unique_ptr<vk::raii::Device> device = vk::raii::su::makeUniqueDevice( vk::raii::Device device = vk::raii::su::makeDevice(
*physicalDevice, graphicsAndPresentQueueFamilyIndex.first, vk::su::getDeviceExtensions() ); physicalDevice, graphicsAndPresentQueueFamilyIndex.first, vk::su::getDeviceExtensions() );
/* VULKAN_HPP_KEY_START */ /* VULKAN_HPP_KEY_START */
const vk::Format depthFormat = vk::Format::eD16Unorm; const vk::Format depthFormat = vk::Format::eD16Unorm;
vk::FormatProperties formatProperties = physicalDevice->getFormatProperties( depthFormat ); vk::FormatProperties formatProperties = physicalDevice.getFormatProperties( depthFormat );
vk::ImageTiling tiling; vk::ImageTiling tiling;
if ( formatProperties.linearTilingFeatures & vk::FormatFeatureFlagBits::eDepthStencilAttachment ) if ( formatProperties.linearTilingFeatures & vk::FormatFeatureFlagBits::eDepthStencilAttachment )
@ -60,7 +59,7 @@ int main( int /*argc*/, char ** /*argv*/ )
{ {
throw std::runtime_error( "DepthStencilAttachment is not supported for D16Unorm depth format." ); throw std::runtime_error( "DepthStencilAttachment is not supported for D16Unorm depth format." );
} }
vk::ImageCreateInfo imageCreateInfo( {}, vk::ImageCreateInfo imageCreateInfo( {},
vk::ImageType::e2D, vk::ImageType::e2D,
depthFormat, depthFormat,
vk::Extent3D( surfaceData.extent, 1 ), vk::Extent3D( surfaceData.extent, 1 ),
@ -69,10 +68,10 @@ int main( int /*argc*/, char ** /*argv*/ )
vk::SampleCountFlagBits::e1, vk::SampleCountFlagBits::e1,
tiling, tiling,
vk::ImageUsageFlagBits::eDepthStencilAttachment ); vk::ImageUsageFlagBits::eDepthStencilAttachment );
std::unique_ptr<vk::raii::Image> depthImage = vk::raii::su::make_unique<vk::raii::Image>( *device, imageCreateInfo ); vk::raii::Image depthImage( device, imageCreateInfo );
vk::PhysicalDeviceMemoryProperties memoryProperties = physicalDevice->getMemoryProperties(); vk::PhysicalDeviceMemoryProperties memoryProperties = physicalDevice.getMemoryProperties();
vk::MemoryRequirements memoryRequirements = depthImage->getMemoryRequirements(); vk::MemoryRequirements memoryRequirements = depthImage.getMemoryRequirements();
uint32_t typeBits = memoryRequirements.memoryTypeBits; uint32_t typeBits = memoryRequirements.memoryTypeBits;
uint32_t typeIndex = uint32_t( ~0 ); uint32_t typeIndex = uint32_t( ~0 );
@ -89,18 +88,16 @@ int main( int /*argc*/, char ** /*argv*/ )
} }
assert( typeIndex != uint32_t( ~0 ) ); assert( typeIndex != uint32_t( ~0 ) );
vk::MemoryAllocateInfo memoryAllocateInfo( memoryRequirements.size, typeIndex ); vk::MemoryAllocateInfo memoryAllocateInfo( memoryRequirements.size, typeIndex );
std::unique_ptr<vk::raii::DeviceMemory> depthMemory = vk::raii::DeviceMemory depthMemory( device, memoryAllocateInfo );
vk::raii::su::make_unique<vk::raii::DeviceMemory>( *device, memoryAllocateInfo ); depthImage.bindMemory( *depthMemory, 0 );
depthImage->bindMemory( **depthMemory, 0 );
vk::ComponentMapping componentMapping( vk::ComponentMapping componentMapping(
vk::ComponentSwizzle::eR, vk::ComponentSwizzle::eG, vk::ComponentSwizzle::eB, vk::ComponentSwizzle::eA ); vk::ComponentSwizzle::eR, vk::ComponentSwizzle::eG, vk::ComponentSwizzle::eB, vk::ComponentSwizzle::eA );
vk::ImageSubresourceRange subResourceRange( vk::ImageAspectFlagBits::eDepth, 0, 1, 0, 1 ); vk::ImageSubresourceRange subResourceRange( vk::ImageAspectFlagBits::eDepth, 0, 1, 0, 1 );
vk::ImageViewCreateInfo imageViewCreateInfo( vk::ImageViewCreateInfo imageViewCreateInfo(
{}, **depthImage, vk::ImageViewType::e2D, depthFormat, componentMapping, subResourceRange ); {}, *depthImage, vk::ImageViewType::e2D, depthFormat, componentMapping, subResourceRange );
std::unique_ptr<vk::raii::ImageView> depthView = vk::raii::ImageView depthView( device, imageViewCreateInfo );
vk::raii::su::make_unique<vk::raii::ImageView>( *device, imageViewCreateInfo );
/* VULKAN_HPP_KEY_END */ /* VULKAN_HPP_KEY_END */
} }

View File

@ -39,18 +39,17 @@ int main( int /*argc*/, char ** /*argv*/ )
{ {
try try
{ {
std::unique_ptr<vk::raii::Context> context = vk::raii::su::make_unique<vk::raii::Context>(); vk::raii::Context context;
std::unique_ptr<vk::raii::Instance> instance = vk::raii::Instance instance =
vk::raii::su::makeUniqueInstance( *context, AppName, EngineName, {}, vk::su::getInstanceExtensions() ); vk::raii::su::makeInstance( context, AppName, EngineName, {}, vk::su::getInstanceExtensions() );
#if !defined( NDEBUG ) #if !defined( NDEBUG )
std::unique_ptr<vk::raii::DebugUtilsMessengerEXT> debugUtilsMessenger = vk::raii::DebugUtilsMessengerEXT debugUtilsMessenger( instance, vk::su::makeDebugUtilsMessengerCreateInfoEXT() );
vk::raii::su::makeUniqueDebugUtilsMessengerEXT( *instance );
#endif #endif
std::unique_ptr<vk::raii::PhysicalDevice> physicalDevice = vk::raii::su::makeUniquePhysicalDevice( *instance ); vk::raii::PhysicalDevice physicalDevice = std::move( vk::raii::PhysicalDevices( instance ).front() );
uint32_t graphicsQueueFamilyIndex =
vk::su::findGraphicsQueueFamilyIndex( physicalDevice->getQueueFamilyProperties() ); uint32_t graphicsQueueFamilyIndex =
std::unique_ptr<vk::raii::Device> device = vk::su::findGraphicsQueueFamilyIndex( physicalDevice.getQueueFamilyProperties() );
vk::raii::su::makeUniqueDevice( *physicalDevice, graphicsQueueFamilyIndex ); vk::raii::Device device = vk::raii::su::makeDevice( physicalDevice, graphicsQueueFamilyIndex );
/* VULKAN_HPP_KEY_START */ /* VULKAN_HPP_KEY_START */
@ -66,24 +65,22 @@ int main( int /*argc*/, char ** /*argv*/ )
// clang-format on // clang-format on
glm::mat4x4 mvpc = clip * projection * view * model; glm::mat4x4 mvpc = clip * projection * view * model;
vk::BufferCreateInfo bufferCreateInfo( {}, sizeof( mvpc ), vk::BufferUsageFlagBits::eUniformBuffer ); vk::BufferCreateInfo bufferCreateInfo( {}, sizeof( mvpc ), vk::BufferUsageFlagBits::eUniformBuffer );
std::unique_ptr<vk::raii::Buffer> uniformDataBuffer = vk::raii::Buffer uniformDataBuffer( device, bufferCreateInfo );
vk::raii::su::make_unique<vk::raii::Buffer>( *device, bufferCreateInfo ); vk::MemoryRequirements memoryRequirements = uniformDataBuffer.getMemoryRequirements();
vk::MemoryRequirements memoryRequirements = uniformDataBuffer->getMemoryRequirements();
uint32_t typeIndex = uint32_t typeIndex =
vk::su::findMemoryType( physicalDevice->getMemoryProperties(), vk::su::findMemoryType( physicalDevice.getMemoryProperties(),
memoryRequirements.memoryTypeBits, memoryRequirements.memoryTypeBits,
vk::MemoryPropertyFlagBits::eHostVisible | vk::MemoryPropertyFlagBits::eHostCoherent ); vk::MemoryPropertyFlagBits::eHostVisible | vk::MemoryPropertyFlagBits::eHostCoherent );
vk::MemoryAllocateInfo memoryAllocateInfo( memoryRequirements.size, typeIndex ); vk::MemoryAllocateInfo memoryAllocateInfo( memoryRequirements.size, typeIndex );
std::unique_ptr<vk::raii::DeviceMemory> uniformDataMemory = vk::raii::DeviceMemory uniformDataMemory( device, memoryAllocateInfo );
vk::raii::su::make_unique<vk::raii::DeviceMemory>( *device, memoryAllocateInfo );
uint8_t * pData = static_cast<uint8_t *>( uniformDataMemory->mapMemory( 0, memoryRequirements.size ) ); uint8_t * pData = static_cast<uint8_t *>( uniformDataMemory.mapMemory( 0, memoryRequirements.size ) );
memcpy( pData, &mvpc, sizeof( mvpc ) ); memcpy( pData, &mvpc, sizeof( mvpc ) );
uniformDataMemory->unmapMemory(); uniformDataMemory.unmapMemory();
uniformDataBuffer->bindMemory( **uniformDataMemory, 0 ); uniformDataBuffer.bindMemory( *uniformDataMemory, 0 );
/* VULKAN_HPP_KEY_END */ /* VULKAN_HPP_KEY_END */
} }

View File

@ -26,32 +26,29 @@ int main( int /*argc*/, char ** /*argv*/ )
{ {
try try
{ {
std::unique_ptr<vk::raii::Context> context = vk::raii::su::make_unique<vk::raii::Context>(); vk::raii::Context context;
std::unique_ptr<vk::raii::Instance> instance = vk::raii::Instance instance =
vk::raii::su::makeUniqueInstance( *context, AppName, EngineName, {}, vk::su::getInstanceExtensions() ); vk::raii::su::makeInstance( context, AppName, EngineName, {}, vk::su::getInstanceExtensions() );
#if !defined( NDEBUG ) #if !defined( NDEBUG )
std::unique_ptr<vk::raii::DebugUtilsMessengerEXT> debugUtilsMessenger = vk::raii::DebugUtilsMessengerEXT debugUtilsMessenger( instance, vk::su::makeDebugUtilsMessengerCreateInfoEXT() );
vk::raii::su::makeUniqueDebugUtilsMessengerEXT( *instance );
#endif #endif
std::unique_ptr<vk::raii::PhysicalDevice> physicalDevice = vk::raii::su::makeUniquePhysicalDevice( *instance ); vk::raii::PhysicalDevice physicalDevice = std::move( vk::raii::PhysicalDevices( instance ).front() );
uint32_t graphicsQueueFamilyIndex =
vk::su::findGraphicsQueueFamilyIndex( physicalDevice->getQueueFamilyProperties() ); uint32_t graphicsQueueFamilyIndex =
std::unique_ptr<vk::raii::Device> device = vk::su::findGraphicsQueueFamilyIndex( physicalDevice.getQueueFamilyProperties() );
vk::raii::su::makeUniqueDevice( *physicalDevice, graphicsQueueFamilyIndex ); vk::raii::Device device = vk::raii::su::makeDevice( physicalDevice, graphicsQueueFamilyIndex );
/* VULKAN_HPP_KEY_START */ /* VULKAN_HPP_KEY_START */
// create a DescriptorSetLayout // create a DescriptorSetLayout
vk::DescriptorSetLayoutBinding descriptorSetLayoutBinding( vk::DescriptorSetLayoutBinding descriptorSetLayoutBinding(
0, vk::DescriptorType::eUniformBuffer, 1, vk::ShaderStageFlagBits::eVertex ); 0, vk::DescriptorType::eUniformBuffer, 1, vk::ShaderStageFlagBits::eVertex );
vk::DescriptorSetLayoutCreateInfo descriptorSetLayoutCreateInfo( {}, descriptorSetLayoutBinding ); vk::DescriptorSetLayoutCreateInfo descriptorSetLayoutCreateInfo( {}, descriptorSetLayoutBinding );
std::unique_ptr<vk::raii::DescriptorSetLayout> descriptorSetLayout = vk::raii::DescriptorSetLayout descriptorSetLayout( device, descriptorSetLayoutCreateInfo );
vk::raii::su::make_unique<vk::raii::DescriptorSetLayout>( *device, descriptorSetLayoutCreateInfo );
// create a PipelineLayout using that DescriptorSetLayout // create a PipelineLayout using that DescriptorSetLayout
vk::PipelineLayoutCreateInfo pipelineLayoutCreateInfo( {}, **descriptorSetLayout ); vk::PipelineLayoutCreateInfo pipelineLayoutCreateInfo( {}, *descriptorSetLayout );
std::unique_ptr<vk::raii::PipelineLayout> pipelineLayout = vk::raii::PipelineLayout pipelineLayout( device, pipelineLayoutCreateInfo );
vk::raii::su::make_unique<vk::raii::PipelineLayout>( *device, pipelineLayoutCreateInfo );
/* VULKAN_HPP_KEY_END */ /* VULKAN_HPP_KEY_END */
} }

View File

@ -39,26 +39,25 @@ int main( int /*argc*/, char ** /*argv*/ )
{ {
try try
{ {
std::unique_ptr<vk::raii::Context> context = vk::raii::su::make_unique<vk::raii::Context>(); vk::raii::Context context;
std::unique_ptr<vk::raii::Instance> instance = vk::raii::Instance instance =
vk::raii::su::makeUniqueInstance( *context, AppName, EngineName, {}, vk::su::getInstanceExtensions() ); vk::raii::su::makeInstance( context, AppName, EngineName, {}, vk::su::getInstanceExtensions() );
#if !defined( NDEBUG ) #if !defined( NDEBUG )
std::unique_ptr<vk::raii::DebugUtilsMessengerEXT> debugUtilsMessenger = vk::raii::DebugUtilsMessengerEXT debugUtilsMessenger( instance, vk::su::makeDebugUtilsMessengerCreateInfoEXT() );
vk::raii::su::makeUniqueDebugUtilsMessengerEXT( *instance );
#endif #endif
std::unique_ptr<vk::raii::PhysicalDevice> physicalDevice = vk::raii::su::makeUniquePhysicalDevice( *instance ); vk::raii::PhysicalDevice physicalDevice = std::move( vk::raii::PhysicalDevices( instance ).front() );
uint32_t graphicsQueueFamilyIndex =
vk::su::findGraphicsQueueFamilyIndex( physicalDevice->getQueueFamilyProperties() ); uint32_t graphicsQueueFamilyIndex =
std::unique_ptr<vk::raii::Device> device = vk::su::findGraphicsQueueFamilyIndex( physicalDevice.getQueueFamilyProperties() );
vk::raii::su::makeUniqueDevice( *physicalDevice, graphicsQueueFamilyIndex ); vk::raii::Device device = vk::raii::su::makeDevice( physicalDevice, graphicsQueueFamilyIndex );
vk::raii::su::BufferData uniformBufferData( vk::raii::su::BufferData uniformBufferData(
*physicalDevice, *device, sizeof( glm::mat4x4 ), vk::BufferUsageFlagBits::eUniformBuffer ); physicalDevice, device, sizeof( glm::mat4x4 ), vk::BufferUsageFlagBits::eUniformBuffer );
glm::mat4x4 mvpcMatrix = vk::su::createModelViewProjectionClipMatrix( vk::Extent2D( 0, 0 ) ); glm::mat4x4 mvpcMatrix = vk::su::createModelViewProjectionClipMatrix( vk::Extent2D( 0, 0 ) );
vk::raii::su::copyToDevice( *uniformBufferData.deviceMemory, mvpcMatrix ); vk::raii::su::copyToDevice( uniformBufferData.deviceMemory, mvpcMatrix );
std::unique_ptr<vk::raii::DescriptorSetLayout> descriptorSetLayout = vk::raii::su::makeUniqueDescriptorSetLayout( vk::raii::DescriptorSetLayout descriptorSetLayout = vk::raii::su::makeDescriptorSetLayout(
*device, { { vk::DescriptorType::eUniformBuffer, 1, vk::ShaderStageFlagBits::eVertex } } ); device, { { vk::DescriptorType::eUniformBuffer, 1, vk::ShaderStageFlagBits::eVertex } } );
/* VULKAN_HPP_KEY_START */ /* VULKAN_HPP_KEY_START */
@ -66,18 +65,17 @@ int main( int /*argc*/, char ** /*argv*/ )
vk::DescriptorPoolSize poolSize( vk::DescriptorType::eUniformBuffer, 1 ); vk::DescriptorPoolSize poolSize( vk::DescriptorType::eUniformBuffer, 1 );
vk::DescriptorPoolCreateInfo descriptorPoolCreateInfo( vk::DescriptorPoolCreateInfo descriptorPoolCreateInfo(
vk::DescriptorPoolCreateFlagBits::eFreeDescriptorSet, 1, poolSize ); vk::DescriptorPoolCreateFlagBits::eFreeDescriptorSet, 1, poolSize );
std::unique_ptr<vk::raii::DescriptorPool> descriptorPool = vk::raii::DescriptorPool descriptorPool( device, descriptorPoolCreateInfo );
vk::raii::su::make_unique<vk::raii::DescriptorPool>( *device, descriptorPoolCreateInfo );
// allocate a descriptor set // allocate a descriptor set
vk::DescriptorSetAllocateInfo descriptorSetAllocateInfo( **descriptorPool, **descriptorSetLayout ); vk::DescriptorSetAllocateInfo descriptorSetAllocateInfo( *descriptorPool, *descriptorSetLayout );
std::unique_ptr<vk::raii::DescriptorSet> descriptorSet = vk::raii::su::make_unique<vk::raii::DescriptorSet>( vk::raii::DescriptorSet descriptorSet =
std::move( vk::raii::DescriptorSets( *device, descriptorSetAllocateInfo ).front() ) ); std::move( vk::raii::DescriptorSets( device, descriptorSetAllocateInfo ).front() );
vk::DescriptorBufferInfo descriptorBufferInfo( **uniformBufferData.buffer, 0, sizeof( glm::mat4x4 ) ); vk::DescriptorBufferInfo descriptorBufferInfo( *uniformBufferData.buffer, 0, sizeof( glm::mat4x4 ) );
vk::WriteDescriptorSet writeDescriptorSet( vk::WriteDescriptorSet writeDescriptorSet(
**descriptorSet, 0, 0, vk::DescriptorType::eUniformBuffer, {}, descriptorBufferInfo ); *descriptorSet, 0, 0, vk::DescriptorType::eUniformBuffer, {}, descriptorBufferInfo );
device->updateDescriptorSets( writeDescriptorSet, nullptr ); device.updateDescriptorSets( writeDescriptorSet, nullptr );
/* VULKAN_HPP_KEY_END */ /* VULKAN_HPP_KEY_END */
} }

View File

@ -38,24 +38,23 @@ int main( int /*argc*/, char ** /*argv*/ )
{ {
try try
{ {
std::unique_ptr<vk::raii::Context> context = vk::raii::su::make_unique<vk::raii::Context>(); vk::raii::Context context;
std::unique_ptr<vk::raii::Instance> instance = vk::raii::Instance instance =
vk::raii::su::makeUniqueInstance( *context, AppName, EngineName, {}, vk::su::getInstanceExtensions() ); vk::raii::su::makeInstance( context, AppName, EngineName, {}, vk::su::getInstanceExtensions() );
#if !defined( NDEBUG ) #if !defined( NDEBUG )
std::unique_ptr<vk::raii::DebugUtilsMessengerEXT> debugUtilsMessenger = vk::raii::DebugUtilsMessengerEXT debugUtilsMessenger( instance, vk::su::makeDebugUtilsMessengerCreateInfoEXT() );
vk::raii::su::makeUniqueDebugUtilsMessengerEXT( *instance );
#endif #endif
std::unique_ptr<vk::raii::PhysicalDevice> physicalDevice = vk::raii::su::makeUniquePhysicalDevice( *instance ); vk::raii::PhysicalDevice physicalDevice = std::move( vk::raii::PhysicalDevices( instance ).front() );
vk::raii::su::SurfaceData surfaceData( *instance, AppName, vk::Extent2D( 500, 500 ) ); vk::raii::su::SurfaceData surfaceData( instance, AppName, vk::Extent2D( 500, 500 ) );
std::pair<uint32_t, uint32_t> graphicsAndPresentQueueFamilyIndex = std::pair<uint32_t, uint32_t> graphicsAndPresentQueueFamilyIndex =
vk::raii::su::findGraphicsAndPresentQueueFamilyIndex( *physicalDevice, *surfaceData.surface ); vk::raii::su::findGraphicsAndPresentQueueFamilyIndex( physicalDevice, *surfaceData.pSurface );
std::unique_ptr<vk::raii::Device> device = vk::raii::su::makeUniqueDevice( vk::raii::Device device = vk::raii::su::makeDevice(
*physicalDevice, graphicsAndPresentQueueFamilyIndex.first, vk::su::getDeviceExtensions() ); physicalDevice, graphicsAndPresentQueueFamilyIndex.first, vk::su::getDeviceExtensions() );
vk::Format colorFormat = vk::Format colorFormat =
vk::su::pickSurfaceFormat( physicalDevice->getSurfaceFormatsKHR( **surfaceData.surface ) ).format; vk::su::pickSurfaceFormat( physicalDevice.getSurfaceFormatsKHR( **surfaceData.pSurface ) ).format;
vk::Format depthFormat = vk::Format::eD16Unorm; vk::Format depthFormat = vk::Format::eD16Unorm;
/* VULKAN_HPP_KEY_START */ /* VULKAN_HPP_KEY_START */
@ -84,9 +83,8 @@ int main( int /*argc*/, char ** /*argv*/ )
vk::AttachmentReference depthReference( 1, vk::ImageLayout::eDepthStencilAttachmentOptimal ); vk::AttachmentReference depthReference( 1, vk::ImageLayout::eDepthStencilAttachmentOptimal );
vk::SubpassDescription subpass( {}, vk::PipelineBindPoint::eGraphics, {}, colorReference, {}, &depthReference ); vk::SubpassDescription subpass( {}, vk::PipelineBindPoint::eGraphics, {}, colorReference, {}, &depthReference );
vk::RenderPassCreateInfo renderPassCreateInfo( {}, attachmentDescriptions, subpass ); vk::RenderPassCreateInfo renderPassCreateInfo( {}, attachmentDescriptions, subpass );
std::unique_ptr<vk::raii::RenderPass> renderPass = vk::raii::RenderPass renderPass( device, renderPassCreateInfo );
vk::raii::su::make_unique<vk::raii::RenderPass>( *device, renderPassCreateInfo );
/* VULKAN_HPP_KEY_END */ /* VULKAN_HPP_KEY_END */
} }

View File

@ -28,18 +28,17 @@ int main( int /*argc*/, char ** /*argv*/ )
{ {
try try
{ {
std::unique_ptr<vk::raii::Context> context = vk::raii::su::make_unique<vk::raii::Context>(); vk::raii::Context context;
std::unique_ptr<vk::raii::Instance> instance = vk::raii::Instance instance =
vk::raii::su::makeUniqueInstance( *context, AppName, EngineName, {}, vk::su::getInstanceExtensions() ); vk::raii::su::makeInstance( context, AppName, EngineName, {}, vk::su::getInstanceExtensions() );
#if !defined( NDEBUG ) #if !defined( NDEBUG )
std::unique_ptr<vk::raii::DebugUtilsMessengerEXT> debugUtilsMessenger = vk::raii::DebugUtilsMessengerEXT debugUtilsMessenger( instance, vk::su::makeDebugUtilsMessengerCreateInfoEXT() );
vk::raii::su::makeUniqueDebugUtilsMessengerEXT( *instance );
#endif #endif
std::unique_ptr<vk::raii::PhysicalDevice> physicalDevice = vk::raii::su::makeUniquePhysicalDevice( *instance ); vk::raii::PhysicalDevice physicalDevice = std::move( vk::raii::PhysicalDevices( instance ).front() );
uint32_t graphicsQueueFamilyIndex =
vk::su::findGraphicsQueueFamilyIndex( physicalDevice->getQueueFamilyProperties() ); uint32_t graphicsQueueFamilyIndex =
std::unique_ptr<vk::raii::Device> device = vk::su::findGraphicsQueueFamilyIndex( physicalDevice.getQueueFamilyProperties() );
vk::raii::su::makeUniqueDevice( *physicalDevice, graphicsQueueFamilyIndex ); vk::raii::Device device = vk::raii::su::makeDevice( physicalDevice, graphicsQueueFamilyIndex );
/* VULKAN_HPP_KEY_START */ /* VULKAN_HPP_KEY_START */
@ -49,17 +48,15 @@ int main( int /*argc*/, char ** /*argv*/ )
bool ok = vk::su::GLSLtoSPV( vk::ShaderStageFlagBits::eVertex, vertexShaderText_PC_C, vertexShaderSPV ); bool ok = vk::su::GLSLtoSPV( vk::ShaderStageFlagBits::eVertex, vertexShaderText_PC_C, vertexShaderSPV );
assert( ok ); assert( ok );
vk::ShaderModuleCreateInfo vertexShaderModuleCreateInfo( {}, vertexShaderSPV ); vk::ShaderModuleCreateInfo vertexShaderModuleCreateInfo( {}, vertexShaderSPV );
std::unique_ptr<vk::raii::ShaderModule> vertexShaderModule = vk::raii::ShaderModule vertexShaderModule( device, vertexShaderModuleCreateInfo );
vk::raii::su::make_unique<vk::raii::ShaderModule>( *device, vertexShaderModuleCreateInfo );
std::vector<unsigned int> fragmentShaderSPV; std::vector<unsigned int> fragmentShaderSPV;
ok = vk::su::GLSLtoSPV( vk::ShaderStageFlagBits::eFragment, fragmentShaderText_C_C, fragmentShaderSPV ); ok = vk::su::GLSLtoSPV( vk::ShaderStageFlagBits::eFragment, fragmentShaderText_C_C, fragmentShaderSPV );
assert( ok ); assert( ok );
vk::ShaderModuleCreateInfo fragmentShaderModuleCreateInfo( {}, fragmentShaderSPV ); vk::ShaderModuleCreateInfo fragmentShaderModuleCreateInfo( {}, fragmentShaderSPV );
std::unique_ptr<vk::raii::ShaderModule> fragmentShaderModule = vk::raii::ShaderModule fragmentShaderModule( device, fragmentShaderModuleCreateInfo );
vk::raii::su::make_unique<vk::raii::ShaderModule>( *device, fragmentShaderModuleCreateInfo );
glslang::FinalizeProcess(); glslang::FinalizeProcess();

View File

@ -26,25 +26,24 @@ int main( int /*argc*/, char ** /*argv*/ )
{ {
try try
{ {
std::unique_ptr<vk::raii::Context> context = vk::raii::su::make_unique<vk::raii::Context>(); vk::raii::Context context;
std::unique_ptr<vk::raii::Instance> instance = vk::raii::Instance instance =
vk::raii::su::makeUniqueInstance( *context, AppName, EngineName, {}, vk::su::getInstanceExtensions() ); vk::raii::su::makeInstance( context, AppName, EngineName, {}, vk::su::getInstanceExtensions() );
#if !defined( NDEBUG ) #if !defined( NDEBUG )
std::unique_ptr<vk::raii::DebugUtilsMessengerEXT> debugUtilsMessenger = vk::raii::DebugUtilsMessengerEXT debugUtilsMessenger( instance, vk::su::makeDebugUtilsMessengerCreateInfoEXT() );
vk::raii::su::makeUniqueDebugUtilsMessengerEXT( *instance );
#endif #endif
std::unique_ptr<vk::raii::PhysicalDevice> physicalDevice = vk::raii::su::makeUniquePhysicalDevice( *instance ); vk::raii::PhysicalDevice physicalDevice = std::move( vk::raii::PhysicalDevices( instance ).front() );
vk::raii::su::SurfaceData surfaceData( *instance, AppName, vk::Extent2D( 64, 64 ) ); vk::raii::su::SurfaceData surfaceData( instance, AppName, vk::Extent2D( 64, 64 ) );
std::pair<uint32_t, uint32_t> graphicsAndPresentQueueFamilyIndex = std::pair<uint32_t, uint32_t> graphicsAndPresentQueueFamilyIndex =
vk::raii::su::findGraphicsAndPresentQueueFamilyIndex( *physicalDevice, *surfaceData.surface ); vk::raii::su::findGraphicsAndPresentQueueFamilyIndex( physicalDevice, *surfaceData.pSurface );
std::unique_ptr<vk::raii::Device> device = vk::raii::su::makeUniqueDevice( vk::raii::Device device = vk::raii::su::makeDevice(
*physicalDevice, graphicsAndPresentQueueFamilyIndex.first, vk::su::getDeviceExtensions() ); physicalDevice, graphicsAndPresentQueueFamilyIndex.first, vk::su::getDeviceExtensions() );
vk::raii::su::SwapChainData swapChainData( *physicalDevice, vk::raii::su::SwapChainData swapChainData( physicalDevice,
*device, device,
*surfaceData.surface, *surfaceData.pSurface,
surfaceData.extent, surfaceData.extent,
vk::ImageUsageFlagBits::eColorAttachment | vk::ImageUsageFlagBits::eColorAttachment |
vk::ImageUsageFlagBits::eTransferSrc, vk::ImageUsageFlagBits::eTransferSrc,
@ -52,25 +51,24 @@ int main( int /*argc*/, char ** /*argv*/ )
graphicsAndPresentQueueFamilyIndex.first, graphicsAndPresentQueueFamilyIndex.first,
graphicsAndPresentQueueFamilyIndex.second ); graphicsAndPresentQueueFamilyIndex.second );
vk::raii::su::DepthBufferData depthBufferData( vk::raii::su::DepthBufferData depthBufferData( physicalDevice, device, vk::Format::eD16Unorm, surfaceData.extent );
*physicalDevice, *device, vk::Format::eD16Unorm, surfaceData.extent );
std::unique_ptr<vk::raii::RenderPass> renderPass = vk::raii::RenderPass renderPass =
vk::raii::su::makeUniqueRenderPass( *device, swapChainData.colorFormat, depthBufferData.format ); vk::raii::su::makeRenderPass( device, swapChainData.colorFormat, depthBufferData.format );
/* VULKAN_KEY_START */ /* VULKAN_KEY_START */
std::array<vk::ImageView, 2> attachments; std::array<vk::ImageView, 2> attachments;
attachments[1] = **depthBufferData.imageView; attachments[1] = **depthBufferData.pImageView;
std::vector<std::unique_ptr<vk::raii::Framebuffer>> framebuffers; std::vector<vk::raii::Framebuffer> framebuffers;
framebuffers.reserve( swapChainData.imageViews.size() ); framebuffers.reserve( swapChainData.imageViews.size() );
for ( auto const & view : swapChainData.imageViews ) for ( auto const & view : swapChainData.imageViews )
{ {
attachments[0] = *view; attachments[0] = *view;
vk::FramebufferCreateInfo framebufferCreateInfo( vk::FramebufferCreateInfo framebufferCreateInfo(
{}, **renderPass, attachments, surfaceData.extent.width, surfaceData.extent.height, 1 ); {}, *renderPass, attachments, surfaceData.extent.width, surfaceData.extent.height, 1 );
framebuffers.push_back( vk::raii::su::make_unique<vk::raii::Framebuffer>( *device, framebufferCreateInfo ) ); framebuffers.push_back( vk::raii::Framebuffer( device, framebufferCreateInfo ) );
} }
/* VULKAN_KEY_END */ /* VULKAN_KEY_END */

View File

@ -37,33 +37,30 @@ int main( int /*argc*/, char ** /*argv*/ )
{ {
try try
{ {
std::unique_ptr<vk::raii::Context> context = vk::raii::su::make_unique<vk::raii::Context>(); vk::raii::Context context;
std::unique_ptr<vk::raii::Instance> instance = vk::raii::Instance instance =
vk::raii::su::makeUniqueInstance( *context, AppName, EngineName, {}, vk::su::getInstanceExtensions() ); vk::raii::su::makeInstance( context, AppName, EngineName, {}, vk::su::getInstanceExtensions() );
#if !defined( NDEBUG ) #if !defined( NDEBUG )
std::unique_ptr<vk::raii::DebugUtilsMessengerEXT> debugUtilsMessenger = vk::raii::DebugUtilsMessengerEXT debugUtilsMessenger( instance, vk::su::makeDebugUtilsMessengerCreateInfoEXT() );
vk::raii::su::makeUniqueDebugUtilsMessengerEXT( *instance );
#endif #endif
std::unique_ptr<vk::raii::PhysicalDevice> physicalDevice = vk::raii::su::makeUniquePhysicalDevice( *instance ); vk::raii::PhysicalDevice physicalDevice = std::move( vk::raii::PhysicalDevices( instance ).front() );
vk::raii::su::SurfaceData surfaceData( *instance, AppName, vk::Extent2D( 64, 64 ) ); vk::raii::su::SurfaceData surfaceData( instance, AppName, vk::Extent2D( 64, 64 ) );
std::pair<uint32_t, uint32_t> graphicsAndPresentQueueFamilyIndex = std::pair<uint32_t, uint32_t> graphicsAndPresentQueueFamilyIndex =
vk::raii::su::findGraphicsAndPresentQueueFamilyIndex( *physicalDevice, *surfaceData.surface ); vk::raii::su::findGraphicsAndPresentQueueFamilyIndex( physicalDevice, *surfaceData.pSurface );
std::unique_ptr<vk::raii::Device> device = vk::raii::su::makeUniqueDevice( vk::raii::Device device = vk::raii::su::makeDevice(
*physicalDevice, graphicsAndPresentQueueFamilyIndex.first, vk::su::getDeviceExtensions() ); physicalDevice, graphicsAndPresentQueueFamilyIndex.first, vk::su::getDeviceExtensions() );
std::unique_ptr<vk::raii::CommandPool> commandPool = vk::raii::CommandPool commandPool = vk::raii::CommandPool(
vk::raii::su::makeUniqueCommandPool( *device, graphicsAndPresentQueueFamilyIndex.first ); device, { vk::CommandPoolCreateFlagBits::eResetCommandBuffer, graphicsAndPresentQueueFamilyIndex.first } );
std::unique_ptr<vk::raii::CommandBuffer> commandBuffer = vk::raii::CommandBuffer commandBuffer = vk::raii::su::makeCommandBuffer( device, commandPool );
vk::raii::su::makeUniqueCommandBuffer( *device, *commandPool );
std::unique_ptr<vk::raii::Queue> graphicsQueue = vk::raii::Queue graphicsQueue( device, graphicsAndPresentQueueFamilyIndex.first, 0 );
vk::raii::su::make_unique<vk::raii::Queue>( *device, graphicsAndPresentQueueFamilyIndex.first, 0 );
vk::raii::su::SwapChainData swapChainData( *physicalDevice, vk::raii::su::SwapChainData swapChainData( physicalDevice,
*device, device,
*surfaceData.surface, *surfaceData.pSurface,
surfaceData.extent, surfaceData.extent,
vk::ImageUsageFlagBits::eColorAttachment | vk::ImageUsageFlagBits::eColorAttachment |
vk::ImageUsageFlagBits::eTransferSrc, vk::ImageUsageFlagBits::eTransferSrc,
@ -71,46 +68,43 @@ int main( int /*argc*/, char ** /*argv*/ )
graphicsAndPresentQueueFamilyIndex.first, graphicsAndPresentQueueFamilyIndex.first,
graphicsAndPresentQueueFamilyIndex.second ); graphicsAndPresentQueueFamilyIndex.second );
vk::raii::su::DepthBufferData depthBufferData( vk::raii::su::DepthBufferData depthBufferData( physicalDevice, device, vk::Format::eD16Unorm, surfaceData.extent );
*physicalDevice, *device, vk::Format::eD16Unorm, surfaceData.extent );
std::unique_ptr<vk::raii::RenderPass> renderPass = vk::raii::RenderPass renderPass =
vk::raii::su::makeUniqueRenderPass( *device, swapChainData.colorFormat, depthBufferData.format ); vk::raii::su::makeRenderPass( device, swapChainData.colorFormat, depthBufferData.format );
std::vector<std::unique_ptr<vk::raii::Framebuffer>> framebuffers = vk::raii::su::makeUniqueFramebuffers( std::vector<vk::raii::Framebuffer> framebuffers = vk::raii::su::makeFramebuffers(
*device, *renderPass, swapChainData.imageViews, depthBufferData.imageView, surfaceData.extent ); device, renderPass, swapChainData.imageViews, &*depthBufferData.pImageView, surfaceData.extent );
/* VULKAN_KEY_START */ /* VULKAN_KEY_START */
// create a vertex buffer for some vertex and color data // create a vertex buffer for some vertex and color data
vk::BufferCreateInfo bufferCreateInfo( {}, sizeof( coloredCubeData ), vk::BufferUsageFlagBits::eVertexBuffer ); vk::BufferCreateInfo bufferCreateInfo( {}, sizeof( coloredCubeData ), vk::BufferUsageFlagBits::eVertexBuffer );
std::unique_ptr<vk::raii::Buffer> vertexBuffer = vk::raii::su::make_unique<vk::raii::Buffer>( *device, bufferCreateInfo ); vk::raii::Buffer vertexBuffer( device, bufferCreateInfo );
// allocate device memory for that buffer // allocate device memory for that buffer
vk::MemoryRequirements memoryRequirements = vertexBuffer->getMemoryRequirements(); vk::MemoryRequirements memoryRequirements = vertexBuffer.getMemoryRequirements();
uint32_t memoryTypeIndex = uint32_t memoryTypeIndex =
vk::su::findMemoryType( physicalDevice->getMemoryProperties(), vk::su::findMemoryType( physicalDevice.getMemoryProperties(),
memoryRequirements.memoryTypeBits, memoryRequirements.memoryTypeBits,
vk::MemoryPropertyFlagBits::eHostVisible | vk::MemoryPropertyFlagBits::eHostCoherent ); vk::MemoryPropertyFlagBits::eHostVisible | vk::MemoryPropertyFlagBits::eHostCoherent );
vk::MemoryAllocateInfo memoryAllocateInfo( memoryRequirements.size, memoryTypeIndex ); vk::MemoryAllocateInfo memoryAllocateInfo( memoryRequirements.size, memoryTypeIndex );
std::unique_ptr<vk::raii::DeviceMemory> deviceMemory = vk::raii::DeviceMemory deviceMemory( device, memoryAllocateInfo );
vk::raii::su::make_unique<vk::raii::DeviceMemory>( *device, memoryAllocateInfo );
// copy the vertex and color data into that device memory // copy the vertex and color data into that device memory
uint8_t * pData = static_cast<uint8_t *>( deviceMemory->mapMemory( 0, memoryRequirements.size ) ); uint8_t * pData = static_cast<uint8_t *>( deviceMemory.mapMemory( 0, memoryRequirements.size ) );
memcpy( pData, coloredCubeData, sizeof( coloredCubeData ) ); memcpy( pData, coloredCubeData, sizeof( coloredCubeData ) );
deviceMemory->unmapMemory(); deviceMemory.unmapMemory();
// and bind the device memory to the vertex buffer // and bind the device memory to the vertex buffer
vertexBuffer->bindMemory( **deviceMemory, 0 ); vertexBuffer.bindMemory( *deviceMemory, 0 );
std::unique_ptr<vk::raii::Semaphore> imageAcquiredSemaphore = vk::raii::Semaphore imageAcquiredSemaphore( device, vk::SemaphoreCreateInfo() );
vk::raii::su::make_unique<vk::raii::Semaphore>( *device, vk::SemaphoreCreateInfo() );
vk::Result result; vk::Result result;
uint32_t imageIndex; uint32_t imageIndex;
std::tie( result, imageIndex ) = std::tie( result, imageIndex ) =
swapChainData.swapChain->acquireNextImage( vk::su::FenceTimeout, **imageAcquiredSemaphore ); swapChainData.pSwapChain->acquireNextImage( vk::su::FenceTimeout, *imageAcquiredSemaphore );
assert( result == vk::Result::eSuccess ); assert( result == vk::Result::eSuccess );
assert( imageIndex < swapChainData.images.size() ); assert( imageIndex < swapChainData.images.size() );
@ -118,17 +112,17 @@ int main( int /*argc*/, char ** /*argv*/ )
clearValues[0].color = vk::ClearColorValue( std::array<float, 4>( { { 0.2f, 0.2f, 0.2f, 0.2f } } ) ); clearValues[0].color = vk::ClearColorValue( std::array<float, 4>( { { 0.2f, 0.2f, 0.2f, 0.2f } } ) );
clearValues[1].depthStencil = vk::ClearDepthStencilValue( 1.0f, 0 ); clearValues[1].depthStencil = vk::ClearDepthStencilValue( 1.0f, 0 );
commandBuffer->begin( {} ); commandBuffer.begin( {} );
vk::RenderPassBeginInfo renderPassBeginInfo( vk::RenderPassBeginInfo renderPassBeginInfo(
**renderPass, **framebuffers[imageIndex], vk::Rect2D( vk::Offset2D( 0, 0 ), surfaceData.extent ), clearValues ); *renderPass, *framebuffers[imageIndex], vk::Rect2D( vk::Offset2D( 0, 0 ), surfaceData.extent ), clearValues );
commandBuffer->beginRenderPass( renderPassBeginInfo, vk::SubpassContents::eInline ); commandBuffer.beginRenderPass( renderPassBeginInfo, vk::SubpassContents::eInline );
commandBuffer->bindVertexBuffers( 0, { **vertexBuffer }, { 0 } ); commandBuffer.bindVertexBuffers( 0, { *vertexBuffer }, { 0 } );
commandBuffer->endRenderPass(); commandBuffer.endRenderPass();
commandBuffer->end(); commandBuffer.end();
vk::raii::su::submitAndWait( *device, *graphicsQueue, *commandBuffer ); vk::raii::su::submitAndWait( device, graphicsQueue, commandBuffer );
/* VULKAN_KEY_END */ /* VULKAN_KEY_END */
} }

View File

@ -40,44 +40,41 @@ int main( int /*argc*/, char ** /*argv*/ )
{ {
try try
{ {
std::unique_ptr<vk::raii::Context> context = vk::raii::su::make_unique<vk::raii::Context>(); vk::raii::Context context;
std::unique_ptr<vk::raii::Instance> instance = vk::raii::Instance instance =
vk::raii::su::makeUniqueInstance( *context, AppName, EngineName, {}, vk::su::getInstanceExtensions() ); vk::raii::su::makeInstance( context, AppName, EngineName, {}, vk::su::getInstanceExtensions() );
#if !defined( NDEBUG ) #if !defined( NDEBUG )
std::unique_ptr<vk::raii::DebugUtilsMessengerEXT> debugUtilsMessenger = vk::raii::DebugUtilsMessengerEXT debugUtilsMessenger( instance, vk::su::makeDebugUtilsMessengerCreateInfoEXT() );
vk::raii::su::makeUniqueDebugUtilsMessengerEXT( *instance );
#endif #endif
std::unique_ptr<vk::raii::PhysicalDevice> physicalDevice = vk::raii::su::makeUniquePhysicalDevice( *instance ); vk::raii::PhysicalDevice physicalDevice = std::move( vk::raii::PhysicalDevices( instance ).front() );
vk::raii::su::SurfaceData surfaceData( *instance, AppName, vk::Extent2D( 500, 500 ) ); vk::raii::su::SurfaceData surfaceData( instance, AppName, vk::Extent2D( 500, 500 ) );
std::pair<uint32_t, uint32_t> graphicsAndPresentQueueFamilyIndex = std::pair<uint32_t, uint32_t> graphicsAndPresentQueueFamilyIndex =
vk::raii::su::findGraphicsAndPresentQueueFamilyIndex( *physicalDevice, *surfaceData.surface ); vk::raii::su::findGraphicsAndPresentQueueFamilyIndex( physicalDevice, *surfaceData.pSurface );
std::unique_ptr<vk::raii::Device> device = vk::raii::su::makeUniqueDevice( vk::raii::Device device = vk::raii::su::makeDevice(
*physicalDevice, graphicsAndPresentQueueFamilyIndex.first, vk::su::getDeviceExtensions() ); physicalDevice, graphicsAndPresentQueueFamilyIndex.first, vk::su::getDeviceExtensions() );
vk::Format colorFormat = vk::Format colorFormat =
vk::su::pickSurfaceFormat( physicalDevice->getSurfaceFormatsKHR( **surfaceData.surface ) ).format; vk::su::pickSurfaceFormat( physicalDevice.getSurfaceFormatsKHR( **surfaceData.pSurface ) ).format;
std::unique_ptr<vk::raii::RenderPass> renderPass = vk::raii::RenderPass renderPass = vk::raii::su::makeRenderPass( device, colorFormat, vk::Format::eD16Unorm );
vk::raii::su::makeUniqueRenderPass( *device, colorFormat, vk::Format::eD16Unorm );
std::unique_ptr<vk::raii::DescriptorSetLayout> descriptorSetLayout = vk::raii::su::makeUniqueDescriptorSetLayout( vk::raii::DescriptorSetLayout descriptorSetLayout = vk::raii::su::makeDescriptorSetLayout(
*device, { { vk::DescriptorType::eUniformBuffer, 1, vk::ShaderStageFlagBits::eVertex } } ); device, { { vk::DescriptorType::eUniformBuffer, 1, vk::ShaderStageFlagBits::eVertex } } );
std::unique_ptr<vk::raii::PipelineLayout> pipelineLayout = vk::raii::PipelineLayout pipelineLayout( device, { {}, *descriptorSetLayout } );
vk::raii::su::makeUniquePipelineLayout( *device, *descriptorSetLayout );
glslang::InitializeProcess(); glslang::InitializeProcess();
std::unique_ptr<vk::raii::ShaderModule> vertexShaderModule = vk::raii::ShaderModule vertexShaderModule =
vk::raii::su::makeUniqueShaderModule( *device, vk::ShaderStageFlagBits::eVertex, vertexShaderText_PC_C ); vk::raii::su::makeShaderModule( device, vk::ShaderStageFlagBits::eVertex, vertexShaderText_PC_C );
std::unique_ptr<vk::raii::ShaderModule> fragmentShaderModule = vk::raii::ShaderModule fragmentShaderModule =
vk::raii::su::makeUniqueShaderModule( *device, vk::ShaderStageFlagBits::eFragment, fragmentShaderText_C_C ); vk::raii::su::makeShaderModule( device, vk::ShaderStageFlagBits::eFragment, fragmentShaderText_C_C );
glslang::FinalizeProcess(); glslang::FinalizeProcess();
/* VULKAN_KEY_START */ /* VULKAN_KEY_START */
std::array<vk::PipelineShaderStageCreateInfo, 2> pipelineShaderStageCreateInfos = { std::array<vk::PipelineShaderStageCreateInfo, 2> pipelineShaderStageCreateInfos = {
vk::PipelineShaderStageCreateInfo( {}, vk::ShaderStageFlagBits::eVertex, **vertexShaderModule, "main" ), vk::PipelineShaderStageCreateInfo( {}, vk::ShaderStageFlagBits::eVertex, *vertexShaderModule, "main" ),
vk::PipelineShaderStageCreateInfo( {}, vk::ShaderStageFlagBits::eFragment, **fragmentShaderModule, "main" ) vk::PipelineShaderStageCreateInfo( {}, vk::ShaderStageFlagBits::eFragment, *fragmentShaderModule, "main" )
}; };
vk::VertexInputBindingDescription vertexInputBindingDescription( 0, sizeof( coloredCubeData[0] ) ); vk::VertexInputBindingDescription vertexInputBindingDescription( 0, sizeof( coloredCubeData[0] ) );
@ -164,13 +161,12 @@ int main( int /*argc*/, char ** /*argv*/ )
&pipelineDepthStencilStateCreateInfo, // pDepthStencilState &pipelineDepthStencilStateCreateInfo, // pDepthStencilState
&pipelineColorBlendStateCreateInfo, // pColorBlendState &pipelineColorBlendStateCreateInfo, // pColorBlendState
&pipelineDynamicStateCreateInfo, // pDynamicState &pipelineDynamicStateCreateInfo, // pDynamicState
**pipelineLayout, // layout *pipelineLayout, // layout
**renderPass // renderPass *renderPass // renderPass
); );
std::shared_ptr<vk::raii::Pipeline> pipeline = vk::raii::Pipeline pipeline( device, nullptr, graphicsPipelineCreateInfo );
vk::raii::su::make_unique<vk::raii::Pipeline>( *device, nullptr, graphicsPipelineCreateInfo ); switch ( pipeline.getConstructorSuccessCode() )
switch ( pipeline->getConstructorSuccessCode() )
{ {
case vk::Result::eSuccess: break; case vk::Result::eSuccess: break;
case vk::Result::ePipelineCompileRequiredEXT: case vk::Result::ePipelineCompileRequiredEXT:

View File

@ -41,35 +41,31 @@ int main( int /*argc*/, char ** /*argv*/ )
{ {
try try
{ {
std::unique_ptr<vk::raii::Context> context = vk::raii::su::make_unique<vk::raii::Context>(); vk::raii::Context context;
std::unique_ptr<vk::raii::Instance> instance = vk::raii::Instance instance =
vk::raii::su::makeUniqueInstance( *context, AppName, EngineName, {}, vk::su::getInstanceExtensions() ); vk::raii::su::makeInstance( context, AppName, EngineName, {}, vk::su::getInstanceExtensions() );
#if !defined( NDEBUG ) #if !defined( NDEBUG )
std::unique_ptr<vk::raii::DebugUtilsMessengerEXT> debugUtilsMessenger = vk::raii::DebugUtilsMessengerEXT debugUtilsMessenger( instance, vk::su::makeDebugUtilsMessengerCreateInfoEXT() );
vk::raii::su::makeUniqueDebugUtilsMessengerEXT( *instance );
#endif #endif
std::unique_ptr<vk::raii::PhysicalDevice> physicalDevice = vk::raii::su::makeUniquePhysicalDevice( *instance ); vk::raii::PhysicalDevice physicalDevice = std::move( vk::raii::PhysicalDevices( instance ).front() );
vk::raii::su::SurfaceData surfaceData( *instance, AppName, vk::Extent2D( 500, 500 ) ); vk::raii::su::SurfaceData surfaceData( instance, AppName, vk::Extent2D( 500, 500 ) );
std::pair<uint32_t, uint32_t> graphicsAndPresentQueueFamilyIndex = std::pair<uint32_t, uint32_t> graphicsAndPresentQueueFamilyIndex =
vk::raii::su::findGraphicsAndPresentQueueFamilyIndex( *physicalDevice, *surfaceData.surface ); vk::raii::su::findGraphicsAndPresentQueueFamilyIndex( physicalDevice, *surfaceData.pSurface );
std::unique_ptr<vk::raii::Device> device = vk::raii::su::makeUniqueDevice( vk::raii::Device device = vk::raii::su::makeDevice(
*physicalDevice, graphicsAndPresentQueueFamilyIndex.first, vk::su::getDeviceExtensions() ); physicalDevice, graphicsAndPresentQueueFamilyIndex.first, vk::su::getDeviceExtensions() );
std::unique_ptr<vk::raii::CommandPool> commandPool = vk::raii::CommandPool commandPool = vk::raii::CommandPool(
vk::raii::su::makeUniqueCommandPool( *device, graphicsAndPresentQueueFamilyIndex.first ); device, { vk::CommandPoolCreateFlagBits::eResetCommandBuffer, graphicsAndPresentQueueFamilyIndex.first } );
std::unique_ptr<vk::raii::CommandBuffer> commandBuffer = vk::raii::CommandBuffer commandBuffer = vk::raii::su::makeCommandBuffer( device, commandPool );
vk::raii::su::makeUniqueCommandBuffer( *device, *commandPool );
std::unique_ptr<vk::raii::Queue> graphicsQueue = vk::raii::Queue graphicsQueue( device, graphicsAndPresentQueueFamilyIndex.first, 0 );
vk::raii::su::make_unique<vk::raii::Queue>( *device, graphicsAndPresentQueueFamilyIndex.first, 0 ); vk::raii::Queue presentQueue( device, graphicsAndPresentQueueFamilyIndex.second, 0 );
std::unique_ptr<vk::raii::Queue> presentQueue =
vk::raii::su::make_unique<vk::raii::Queue>( *device, graphicsAndPresentQueueFamilyIndex.second, 0 );
vk::raii::su::SwapChainData swapChainData( *physicalDevice, vk::raii::su::SwapChainData swapChainData( physicalDevice,
*device, device,
*surfaceData.surface, *surfaceData.pSurface,
surfaceData.extent, surfaceData.extent,
vk::ImageUsageFlagBits::eColorAttachment | vk::ImageUsageFlagBits::eColorAttachment |
vk::ImageUsageFlagBits::eTransferSrc, vk::ImageUsageFlagBits::eTransferSrc,
@ -77,112 +73,107 @@ int main( int /*argc*/, char ** /*argv*/ )
graphicsAndPresentQueueFamilyIndex.first, graphicsAndPresentQueueFamilyIndex.first,
graphicsAndPresentQueueFamilyIndex.second ); graphicsAndPresentQueueFamilyIndex.second );
vk::raii::su::DepthBufferData depthBufferData( vk::raii::su::DepthBufferData depthBufferData( physicalDevice, device, vk::Format::eD16Unorm, surfaceData.extent );
*physicalDevice, *device, vk::Format::eD16Unorm, surfaceData.extent );
vk::raii::su::BufferData uniformBufferData( vk::raii::su::BufferData uniformBufferData(
*physicalDevice, *device, sizeof( glm::mat4x4 ), vk::BufferUsageFlagBits::eUniformBuffer ); physicalDevice, device, sizeof( glm::mat4x4 ), vk::BufferUsageFlagBits::eUniformBuffer );
glm::mat4x4 mvpcMatrix = vk::su::createModelViewProjectionClipMatrix( surfaceData.extent ); glm::mat4x4 mvpcMatrix = vk::su::createModelViewProjectionClipMatrix( surfaceData.extent );
vk::raii::su::copyToDevice( *uniformBufferData.deviceMemory, mvpcMatrix ); vk::raii::su::copyToDevice( uniformBufferData.deviceMemory, mvpcMatrix );
std::unique_ptr<vk::raii::DescriptorSetLayout> descriptorSetLayout = vk::raii::su::makeUniqueDescriptorSetLayout( vk::raii::DescriptorSetLayout descriptorSetLayout = vk::raii::su::makeDescriptorSetLayout(
*device, { { vk::DescriptorType::eUniformBuffer, 1, vk::ShaderStageFlagBits::eVertex } } ); device, { { vk::DescriptorType::eUniformBuffer, 1, vk::ShaderStageFlagBits::eVertex } } );
std::unique_ptr<vk::raii::PipelineLayout> pipelineLayout = vk::raii::PipelineLayout pipelineLayout( device, { {}, *descriptorSetLayout } );
vk::raii::su::makeUniquePipelineLayout( *device, *descriptorSetLayout );
vk::Format colorFormat = vk::Format colorFormat =
vk::su::pickSurfaceFormat( physicalDevice->getSurfaceFormatsKHR( **surfaceData.surface ) ).format; vk::su::pickSurfaceFormat( physicalDevice.getSurfaceFormatsKHR( **surfaceData.pSurface ) ).format;
std::unique_ptr<vk::raii::RenderPass> renderPass = vk::raii::RenderPass renderPass = vk::raii::su::makeRenderPass( device, colorFormat, depthBufferData.format );
vk::raii::su::makeUniqueRenderPass( *device, colorFormat, depthBufferData.format );
glslang::InitializeProcess(); glslang::InitializeProcess();
std::unique_ptr<vk::raii::ShaderModule> vertexShaderModule = vk::raii::ShaderModule vertexShaderModule =
vk::raii::su::makeUniqueShaderModule( *device, vk::ShaderStageFlagBits::eVertex, vertexShaderText_PC_C ); vk::raii::su::makeShaderModule( device, vk::ShaderStageFlagBits::eVertex, vertexShaderText_PC_C );
std::unique_ptr<vk::raii::ShaderModule> fragmentShaderModule = vk::raii::ShaderModule fragmentShaderModule =
vk::raii::su::makeUniqueShaderModule( *device, vk::ShaderStageFlagBits::eFragment, fragmentShaderText_C_C ); vk::raii::su::makeShaderModule( device, vk::ShaderStageFlagBits::eFragment, fragmentShaderText_C_C );
glslang::FinalizeProcess(); glslang::FinalizeProcess();
std::vector<std::unique_ptr<vk::raii::Framebuffer>> framebuffers = vk::raii::su::makeUniqueFramebuffers( std::vector<vk::raii::Framebuffer> framebuffers = vk::raii::su::makeFramebuffers(
*device, *renderPass, swapChainData.imageViews, depthBufferData.imageView, surfaceData.extent ); device, renderPass, swapChainData.imageViews, &*depthBufferData.pImageView, surfaceData.extent );
vk::raii::su::BufferData vertexBufferData( vk::raii::su::BufferData vertexBufferData(
*physicalDevice, *device, sizeof( coloredCubeData ), vk::BufferUsageFlagBits::eVertexBuffer ); physicalDevice, device, sizeof( coloredCubeData ), vk::BufferUsageFlagBits::eVertexBuffer );
vk::raii::su::copyToDevice( vk::raii::su::copyToDevice(
*vertexBufferData.deviceMemory, coloredCubeData, sizeof( coloredCubeData ) / sizeof( coloredCubeData[0] ) ); vertexBufferData.deviceMemory, coloredCubeData, sizeof( coloredCubeData ) / sizeof( coloredCubeData[0] ) );
std::unique_ptr<vk::raii::DescriptorPool> descriptorPool = vk::raii::DescriptorPool descriptorPool =
vk::raii::su::makeUniqueDescriptorPool( *device, { { vk::DescriptorType::eUniformBuffer, 1 } } ); vk::raii::su::makeDescriptorPool( device, { { vk::DescriptorType::eUniformBuffer, 1 } } );
std::unique_ptr<vk::raii::DescriptorSet> descriptorSet = vk::raii::DescriptorSet descriptorSet =
vk::raii::su::makeUniqueDescriptorSet( *device, *descriptorPool, *descriptorSetLayout ); std::move( vk::raii::DescriptorSets( device, { *descriptorPool, *descriptorSetLayout } ).front() );
vk::raii::su::updateDescriptorSets( vk::raii::su::updateDescriptorSets(
*device, *descriptorSet, { { vk::DescriptorType::eUniformBuffer, *uniformBufferData.buffer, nullptr } }, {} ); device, descriptorSet, { { vk::DescriptorType::eUniformBuffer, uniformBufferData.buffer, nullptr } }, {} );
std::unique_ptr<vk::raii::PipelineCache> pipelineCache = vk::raii::PipelineCache pipelineCache( device, vk::PipelineCacheCreateInfo() );
vk::raii::su::make_unique<vk::raii::PipelineCache>( *device, vk::PipelineCacheCreateInfo() ); vk::raii::Pipeline graphicsPipeline = vk::raii::su::makeGraphicsPipeline(
std::unique_ptr<vk::raii::Pipeline> graphicsPipeline = vk::raii::su::makeUniqueGraphicsPipeline( device,
*device, pipelineCache,
*pipelineCache, vertexShaderModule,
*vertexShaderModule,
nullptr, nullptr,
*fragmentShaderModule, fragmentShaderModule,
nullptr, nullptr,
vk::su::checked_cast<uint32_t>( sizeof( coloredCubeData[0] ) ), vk::su::checked_cast<uint32_t>( sizeof( coloredCubeData[0] ) ),
{ { vk::Format::eR32G32B32A32Sfloat, 0 }, { vk::Format::eR32G32B32A32Sfloat, 16 } }, { { vk::Format::eR32G32B32A32Sfloat, 0 }, { vk::Format::eR32G32B32A32Sfloat, 16 } },
vk::FrontFace::eClockwise, vk::FrontFace::eClockwise,
true, true,
*pipelineLayout, pipelineLayout,
*renderPass ); renderPass );
/* VULKAN_KEY_START */ /* VULKAN_KEY_START */
// Get the index of the next available swapchain image: // Get the index of the next available swapchain image:
std::unique_ptr<vk::raii::Semaphore> imageAcquiredSemaphore = vk::raii::Semaphore imageAcquiredSemaphore( device, vk::SemaphoreCreateInfo() );
vk::raii::su::make_unique<vk::raii::Semaphore>( *device, vk::SemaphoreCreateInfo() );
vk::Result result; vk::Result result;
uint32_t imageIndex; uint32_t imageIndex;
std::tie( result, imageIndex ) = std::tie( result, imageIndex ) =
swapChainData.swapChain->acquireNextImage( vk::su::FenceTimeout, **imageAcquiredSemaphore ); swapChainData.pSwapChain->acquireNextImage( vk::su::FenceTimeout, *imageAcquiredSemaphore );
assert( result == vk::Result::eSuccess ); assert( result == vk::Result::eSuccess );
assert( imageIndex < swapChainData.images.size() ); assert( imageIndex < swapChainData.images.size() );
commandBuffer->begin( {} ); commandBuffer.begin( {} );
std::array<vk::ClearValue, 2> clearValues; std::array<vk::ClearValue, 2> clearValues;
clearValues[0].color = vk::ClearColorValue( std::array<float, 4>( { { 0.2f, 0.2f, 0.2f, 0.2f } } ) ); clearValues[0].color = vk::ClearColorValue( std::array<float, 4>( { { 0.2f, 0.2f, 0.2f, 0.2f } } ) );
clearValues[1].depthStencil = vk::ClearDepthStencilValue( 1.0f, 0 ); clearValues[1].depthStencil = vk::ClearDepthStencilValue( 1.0f, 0 );
vk::RenderPassBeginInfo renderPassBeginInfo( vk::RenderPassBeginInfo renderPassBeginInfo(
**renderPass, **framebuffers[imageIndex], vk::Rect2D( vk::Offset2D( 0, 0 ), surfaceData.extent ), clearValues ); *renderPass, *framebuffers[imageIndex], vk::Rect2D( vk::Offset2D( 0, 0 ), surfaceData.extent ), clearValues );
commandBuffer->beginRenderPass( renderPassBeginInfo, vk::SubpassContents::eInline ); commandBuffer.beginRenderPass( renderPassBeginInfo, vk::SubpassContents::eInline );
commandBuffer->bindPipeline( vk::PipelineBindPoint::eGraphics, **graphicsPipeline ); commandBuffer.bindPipeline( vk::PipelineBindPoint::eGraphics, *graphicsPipeline );
commandBuffer->bindDescriptorSets( commandBuffer.bindDescriptorSets(
vk::PipelineBindPoint::eGraphics, **pipelineLayout, 0, { **descriptorSet }, nullptr ); vk::PipelineBindPoint::eGraphics, *pipelineLayout, 0, { *descriptorSet }, nullptr );
commandBuffer->bindVertexBuffers( 0, { **vertexBufferData.buffer }, { 0 } ); commandBuffer.bindVertexBuffers( 0, { *vertexBufferData.buffer }, { 0 } );
commandBuffer->setViewport( 0, commandBuffer.setViewport( 0,
vk::Viewport( 0.0f, vk::Viewport( 0.0f,
0.0f, 0.0f,
static_cast<float>( surfaceData.extent.width ), static_cast<float>( surfaceData.extent.width ),
static_cast<float>( surfaceData.extent.height ), static_cast<float>( surfaceData.extent.height ),
0.0f, 0.0f,
1.0f ) ); 1.0f ) );
commandBuffer->setScissor( 0, vk::Rect2D( vk::Offset2D( 0, 0 ), surfaceData.extent ) ); commandBuffer.setScissor( 0, vk::Rect2D( vk::Offset2D( 0, 0 ), surfaceData.extent ) );
commandBuffer->draw( 12 * 3, 1, 0, 0 ); commandBuffer.draw( 12 * 3, 1, 0, 0 );
commandBuffer->endRenderPass(); commandBuffer.endRenderPass();
commandBuffer->end(); commandBuffer.end();
std::unique_ptr<vk::raii::Fence> drawFence = vk::raii::su::make_unique<vk::raii::Fence>( *device, vk::FenceCreateInfo() ); vk::raii::Fence drawFence( device, vk::FenceCreateInfo() );
vk::PipelineStageFlags waitDestinationStageMask( vk::PipelineStageFlagBits::eColorAttachmentOutput ); vk::PipelineStageFlags waitDestinationStageMask( vk::PipelineStageFlagBits::eColorAttachmentOutput );
vk::SubmitInfo submitInfo( **imageAcquiredSemaphore, waitDestinationStageMask, **commandBuffer ); vk::SubmitInfo submitInfo( *imageAcquiredSemaphore, waitDestinationStageMask, *commandBuffer );
graphicsQueue->submit( submitInfo, **drawFence ); graphicsQueue.submit( submitInfo, *drawFence );
while ( vk::Result::eTimeout == device->waitForFences( { **drawFence }, VK_TRUE, vk::su::FenceTimeout ) ) while ( vk::Result::eTimeout == device.waitForFences( { *drawFence }, VK_TRUE, vk::su::FenceTimeout ) )
; ;
vk::PresentInfoKHR presentInfoKHR( nullptr, **swapChainData.swapChain, imageIndex ); vk::PresentInfoKHR presentInfoKHR( nullptr, **swapChainData.pSwapChain, imageIndex );
result = presentQueue->presentKHR( presentInfoKHR ); result = presentQueue.presentKHR( presentInfoKHR );
switch ( result ) switch ( result )
{ {
case vk::Result::eSuccess: break; case vk::Result::eSuccess: break;
@ -195,7 +186,7 @@ int main( int /*argc*/, char ** /*argv*/ )
/* VULKAN_KEY_END */ /* VULKAN_KEY_END */
device->waitIdle(); device.waitIdle();
} }
catch ( vk::SystemError & err ) catch ( vk::SystemError & err )
{ {

View File

@ -41,10 +41,10 @@ int main( int /*argc*/, char ** /*argv*/ )
desiredVersionString += std::to_string( desiredMinorVersion ); desiredVersionString += std::to_string( desiredMinorVersion );
// initialize the vulkan context // initialize the vulkan context
std::unique_ptr<vk::raii::Context> context = vk::raii::su::make_unique<vk::raii::Context>(); vk::raii::Context context;
// Determine what API version is available // Determine what API version is available
uint32_t apiVersion = context->enumerateInstanceVersion(); uint32_t apiVersion = context.enumerateInstanceVersion();
// Translate the version into major/minor for easier comparison // Translate the version into major/minor for easier comparison
uint32_t loader_major_version = VK_VERSION_MAJOR( apiVersion ); uint32_t loader_major_version = VK_VERSION_MAJOR( apiVersion );
@ -57,24 +57,23 @@ int main( int /*argc*/, char ** /*argv*/ )
( loader_major_version == desiredMajorVersion && loader_minor_version >= desiredMinorVersion ) ) ( loader_major_version == desiredMajorVersion && loader_minor_version >= desiredMinorVersion ) )
{ {
// Create the instance // Create the instance
std::unique_ptr<vk::raii::Instance> instance = vk::raii::Instance instance =
vk::raii::su::makeUniqueInstance( *context, AppName, EngineName, {}, vk::su::getInstanceExtensions() ); vk::raii::su::makeInstance( context, AppName, EngineName, {}, vk::su::getInstanceExtensions() );
#if !defined( NDEBUG ) #if !defined( NDEBUG )
std::unique_ptr<vk::raii::DebugUtilsMessengerEXT> debugUtilsMessenger = vk::raii::DebugUtilsMessengerEXT debugUtilsMessenger( instance, vk::su::makeDebugUtilsMessengerCreateInfoEXT() );
vk::raii::su::make_unique<vk::raii::DebugUtilsMessengerEXT>( *instance, vk::su::makeDebugUtilsMessengerCreateInfoEXT() );
#endif #endif
// Get the list of physical devices // Get the list of physical devices
vk::raii::PhysicalDevices physicalDevices( *instance ); vk::raii::PhysicalDevices physicalDevices( instance );
// Go through the list of physical devices and select only those that are capable of running the API version we // Go through the list of physical devices and select only those that are capable of running the API version we
// want. // want.
std::vector<std::unique_ptr<vk::raii::PhysicalDevice>> desiredPhysicalDevices; std::vector<vk::raii::PhysicalDevice> desiredPhysicalDevices;
for ( auto & pdh : physicalDevices ) for ( auto & pdh : physicalDevices )
{ {
if ( desiredVersion <= pdh.getProperties().apiVersion ) if ( desiredVersion <= pdh.getProperties().apiVersion )
{ {
desiredPhysicalDevices.push_back( vk::raii::su::make_unique<vk::raii::PhysicalDevice>( std::move( pdh ) ) ); desiredPhysicalDevices.push_back( std::move( pdh ) );
} }
} }

View File

@ -26,18 +26,17 @@ int main( int /*argc*/, char ** /*argv*/ )
{ {
try try
{ {
std::unique_ptr<vk::raii::Context> context = vk::raii::su::make_unique<vk::raii::Context>(); vk::raii::Context context;
std::unique_ptr<vk::raii::Instance> instance = vk::raii::Instance instance =
vk::raii::su::makeUniqueInstance( *context, AppName, EngineName, {}, vk::su::getInstanceExtensions() ); vk::raii::su::makeInstance( context, AppName, EngineName, {}, vk::su::getInstanceExtensions() );
#if !defined( NDEBUG ) #if !defined( NDEBUG )
std::unique_ptr<vk::raii::DebugUtilsMessengerEXT> debugUtilsMessenger = vk::raii::DebugUtilsMessengerEXT debugUtilsMessenger( instance, vk::su::makeDebugUtilsMessengerCreateInfoEXT() );
vk::raii::su::makeUniqueDebugUtilsMessengerEXT( *instance );
#endif #endif
std::unique_ptr<vk::raii::PhysicalDevice> physicalDevice = vk::raii::su::makeUniquePhysicalDevice( *instance ); vk::raii::PhysicalDevice physicalDevice = std::move( vk::raii::PhysicalDevices( instance ).front() );
vk::raii::su::SurfaceData surfaceData( *instance, AppName, vk::Extent2D( 640, 640 ) ); vk::raii::su::SurfaceData surfaceData( instance, AppName, vk::Extent2D( 640, 640 ) );
vk::SurfaceCapabilitiesKHR surfaceCapabilities = physicalDevice->getSurfaceCapabilitiesKHR( **surfaceData.surface ); vk::SurfaceCapabilitiesKHR surfaceCapabilities = physicalDevice.getSurfaceCapabilitiesKHR( **surfaceData.pSurface );
if ( !( surfaceCapabilities.supportedUsageFlags & vk::ImageUsageFlagBits::eTransferDst ) ) if ( !( surfaceCapabilities.supportedUsageFlags & vk::ImageUsageFlagBits::eTransferDst ) )
{ {
std::cout << "Surface cannot be destination of blit - abort \n"; std::cout << "Surface cannot be destination of blit - abort \n";
@ -45,23 +44,20 @@ int main( int /*argc*/, char ** /*argv*/ )
} }
std::pair<uint32_t, uint32_t> graphicsAndPresentQueueFamilyIndex = std::pair<uint32_t, uint32_t> graphicsAndPresentQueueFamilyIndex =
vk::raii::su::findGraphicsAndPresentQueueFamilyIndex( *physicalDevice, *surfaceData.surface ); vk::raii::su::findGraphicsAndPresentQueueFamilyIndex( physicalDevice, *surfaceData.pSurface );
std::unique_ptr<vk::raii::Device> device = vk::raii::su::makeUniqueDevice( vk::raii::Device device = vk::raii::su::makeDevice(
*physicalDevice, graphicsAndPresentQueueFamilyIndex.first, vk::su::getDeviceExtensions() ); physicalDevice, graphicsAndPresentQueueFamilyIndex.first, vk::su::getDeviceExtensions() );
std::unique_ptr<vk::raii::CommandPool> commandPool = vk::raii::CommandPool commandPool = vk::raii::CommandPool(
vk::raii::su::makeUniqueCommandPool( *device, graphicsAndPresentQueueFamilyIndex.first ); device, { vk::CommandPoolCreateFlagBits::eResetCommandBuffer, graphicsAndPresentQueueFamilyIndex.first } );
std::unique_ptr<vk::raii::CommandBuffer> commandBuffer = vk::raii::CommandBuffer commandBuffer = vk::raii::su::makeCommandBuffer( device, commandPool );
vk::raii::su::makeUniqueCommandBuffer( *device, *commandPool );
std::unique_ptr<vk::raii::Queue> graphicsQueue = vk::raii::Queue graphicsQueue( device, graphicsAndPresentQueueFamilyIndex.first, 0 );
vk::raii::su::make_unique<vk::raii::Queue>( *device, graphicsAndPresentQueueFamilyIndex.first, 0 ); vk::raii::Queue presentQueue( device, graphicsAndPresentQueueFamilyIndex.second, 0 );
std::unique_ptr<vk::raii::Queue> presentQueue =
vk::raii::su::make_unique<vk::raii::Queue>( *device, graphicsAndPresentQueueFamilyIndex.second, 0 );
vk::raii::su::SwapChainData swapChainData( *physicalDevice, vk::raii::su::SwapChainData swapChainData( physicalDevice,
*device, device,
*surfaceData.surface, *surfaceData.pSurface,
surfaceData.extent, surfaceData.extent,
vk::ImageUsageFlagBits::eColorAttachment | vk::ImageUsageFlagBits::eColorAttachment |
vk::ImageUsageFlagBits::eTransferSrc | vk::ImageUsageFlagBits::eTransferSrc |
@ -72,30 +68,29 @@ int main( int /*argc*/, char ** /*argv*/ )
/* VULKAN_KEY_START */ /* VULKAN_KEY_START */
vk::FormatProperties formatProperties = physicalDevice->getFormatProperties( swapChainData.colorFormat ); vk::FormatProperties formatProperties = physicalDevice.getFormatProperties( swapChainData.colorFormat );
assert( ( formatProperties.linearTilingFeatures & vk::FormatFeatureFlagBits::eBlitSrc ) && assert( ( formatProperties.linearTilingFeatures & vk::FormatFeatureFlagBits::eBlitSrc ) &&
"Format cannot be used as transfer source" ); "Format cannot be used as transfer source" );
std::unique_ptr<vk::raii::Semaphore> imageAcquiredSemaphore = vk::raii::Semaphore imageAcquiredSemaphore( device, vk::SemaphoreCreateInfo() );
vk::raii::su::make_unique<vk::raii::Semaphore>( *device, vk::SemaphoreCreateInfo() );
// Get the index of the next available swapchain image: // Get the index of the next available swapchain image:
vk::Result result; vk::Result result;
uint32_t imageIndex; uint32_t imageIndex;
std::tie( result, imageIndex ) = std::tie( result, imageIndex ) =
swapChainData.swapChain->acquireNextImage( vk::su::FenceTimeout, **imageAcquiredSemaphore ); swapChainData.pSwapChain->acquireNextImage( vk::su::FenceTimeout, *imageAcquiredSemaphore );
assert( result == vk::Result::eSuccess ); assert( result == vk::Result::eSuccess );
assert( imageIndex < swapChainData.images.size() ); assert( imageIndex < swapChainData.images.size() );
commandBuffer->begin( vk::CommandBufferBeginInfo() ); commandBuffer.begin( vk::CommandBufferBeginInfo() );
vk::raii::su::setImageLayout( *commandBuffer, vk::raii::su::setImageLayout( commandBuffer,
static_cast<vk::Image>( swapChainData.images[imageIndex] ), static_cast<vk::Image>( swapChainData.images[imageIndex] ),
swapChainData.colorFormat, swapChainData.colorFormat,
vk::ImageLayout::eUndefined, vk::ImageLayout::eUndefined,
vk::ImageLayout::eTransferDstOptimal ); vk::ImageLayout::eTransferDstOptimal );
// Create an image, map it, and write some values to the image // Create an image, map it, and write some values to the image
vk::ImageCreateInfo imageCreateInfo( {}, vk::ImageCreateInfo imageCreateInfo( {},
vk::ImageType::e2D, vk::ImageType::e2D,
swapChainData.colorFormat, swapChainData.colorFormat,
vk::Extent3D( surfaceData.extent, 1 ), vk::Extent3D( surfaceData.extent, 1 ),
@ -104,38 +99,36 @@ int main( int /*argc*/, char ** /*argv*/ )
vk::SampleCountFlagBits::e1, vk::SampleCountFlagBits::e1,
vk::ImageTiling::eLinear, vk::ImageTiling::eLinear,
vk::ImageUsageFlagBits::eTransferSrc ); vk::ImageUsageFlagBits::eTransferSrc );
std::unique_ptr<vk::raii::Image> blitSourceImage = vk::raii::su::make_unique<vk::raii::Image>( *device, imageCreateInfo ); vk::raii::Image blitSourceImage( device, imageCreateInfo );
vk::PhysicalDeviceMemoryProperties memoryProperties = physicalDevice->getMemoryProperties(); vk::PhysicalDeviceMemoryProperties memoryProperties = physicalDevice.getMemoryProperties();
vk::MemoryRequirements memoryRequirements = blitSourceImage->getMemoryRequirements(); vk::MemoryRequirements memoryRequirements = blitSourceImage.getMemoryRequirements();
uint32_t memoryTypeIndex = vk::su::findMemoryType( uint32_t memoryTypeIndex = vk::su::findMemoryType(
memoryProperties, memoryRequirements.memoryTypeBits, vk::MemoryPropertyFlagBits::eHostVisible ); memoryProperties, memoryRequirements.memoryTypeBits, vk::MemoryPropertyFlagBits::eHostVisible );
vk::MemoryAllocateInfo memoryAllocateInfo( memoryRequirements.size, memoryTypeIndex ); vk::MemoryAllocateInfo memoryAllocateInfo( memoryRequirements.size, memoryTypeIndex );
std::unique_ptr<vk::raii::DeviceMemory> deviceMemory = vk::raii::DeviceMemory deviceMemory( device, memoryAllocateInfo );
vk::raii::su::make_unique<vk::raii::DeviceMemory>( *device, memoryAllocateInfo ); blitSourceImage.bindMemory( *deviceMemory, 0 );
blitSourceImage->bindMemory( **deviceMemory, 0 );
vk::raii::su::setImageLayout( *commandBuffer, vk::raii::su::setImageLayout( commandBuffer,
**blitSourceImage, *blitSourceImage,
swapChainData.colorFormat, swapChainData.colorFormat,
vk::ImageLayout::eUndefined, vk::ImageLayout::eUndefined,
vk::ImageLayout::eGeneral ); vk::ImageLayout::eGeneral );
commandBuffer->end(); commandBuffer.end();
/* Queue the command buffer for execution */ /* Queue the command buffer for execution */
std::unique_ptr<vk::raii::Fence> commandFence = vk::raii::su::make_unique<vk::raii::Fence>( *device, vk::FenceCreateInfo() ); vk::raii::Fence commandFence( device, vk::FenceCreateInfo() );
vk::PipelineStageFlags waitDestinationStageMask( vk::PipelineStageFlagBits::eColorAttachmentOutput ); vk::PipelineStageFlags waitDestinationStageMask( vk::PipelineStageFlagBits::eColorAttachmentOutput );
vk::SubmitInfo submitInfo( **imageAcquiredSemaphore, waitDestinationStageMask, **commandBuffer ); vk::SubmitInfo submitInfo( *imageAcquiredSemaphore, waitDestinationStageMask, *commandBuffer );
graphicsQueue->submit( submitInfo, **commandFence ); graphicsQueue.submit( submitInfo, *commandFence );
/* Make sure command buffer is finished before mapping */ /* Make sure command buffer is finished before mapping */
while ( device->waitForFences( { **commandFence }, true, vk::su::FenceTimeout ) == vk::Result::eTimeout ) while ( device.waitForFences( { *commandFence }, true, vk::su::FenceTimeout ) == vk::Result::eTimeout )
; ;
unsigned char * pImageMemory = unsigned char * pImageMemory = static_cast<unsigned char *>( deviceMemory.mapMemory( 0, memoryRequirements.size ) );
static_cast<unsigned char *>( deviceMemory->mapMemory( 0, memoryRequirements.size ) );
// Checkerboard of 8x8 pixel squares // Checkerboard of 8x8 pixel squares
for ( uint32_t row = 0; row < surfaceData.extent.height; row++ ) for ( uint32_t row = 0; row < surfaceData.extent.height; row++ )
@ -152,16 +145,16 @@ int main( int /*argc*/, char ** /*argv*/ )
} }
// Flush the mapped memory and then unmap it. Assume it isn't coherent since we didn't really confirm // Flush the mapped memory and then unmap it. Assume it isn't coherent since we didn't really confirm
vk::MappedMemoryRange mappedMemoryRange( **deviceMemory, 0, memoryRequirements.size ); vk::MappedMemoryRange mappedMemoryRange( *deviceMemory, 0, memoryRequirements.size );
device->flushMappedMemoryRanges( mappedMemoryRange ); device.flushMappedMemoryRanges( mappedMemoryRange );
deviceMemory->unmapMemory(); deviceMemory.unmapMemory();
commandBuffer->reset( {} ); commandBuffer.reset( {} );
commandBuffer->begin( vk::CommandBufferBeginInfo() ); commandBuffer.begin( vk::CommandBufferBeginInfo() );
// Intend to blit from this image, set the layout accordingly // Intend to blit from this image, set the layout accordingly
vk::raii::su::setImageLayout( *commandBuffer, vk::raii::su::setImageLayout( commandBuffer,
**blitSourceImage, *blitSourceImage,
swapChainData.colorFormat, swapChainData.colorFormat,
vk::ImageLayout::eGeneral, vk::ImageLayout::eGeneral,
vk::ImageLayout::eTransferSrcOptimal ); vk::ImageLayout::eTransferSrcOptimal );
@ -175,12 +168,12 @@ int main( int /*argc*/, char ** /*argv*/ )
{ { vk::Offset3D( 0, 0, 0 ), vk::Offset3D( 32, 32, 1 ) } }, { { vk::Offset3D( 0, 0, 0 ), vk::Offset3D( 32, 32, 1 ) } },
imageSubresourceLayers, imageSubresourceLayers,
{ { vk::Offset3D( 0, 0, 0 ), vk::Offset3D( surfaceData.extent.width, surfaceData.extent.height, 1 ) } } ); { { vk::Offset3D( 0, 0, 0 ), vk::Offset3D( surfaceData.extent.width, surfaceData.extent.height, 1 ) } } );
commandBuffer->blitImage( **blitSourceImage, commandBuffer.blitImage( *blitSourceImage,
vk::ImageLayout::eTransferSrcOptimal, vk::ImageLayout::eTransferSrcOptimal,
blitDestinationImage, blitDestinationImage,
vk::ImageLayout::eTransferDstOptimal, vk::ImageLayout::eTransferDstOptimal,
imageBlit, imageBlit,
vk::Filter::eLinear ); vk::Filter::eLinear );
// Use a barrier to make sure the blit is finished before the copy starts // Use a barrier to make sure the blit is finished before the copy starts
vk::ImageMemoryBarrier memoryBarrier( vk::AccessFlagBits::eTransferWrite, vk::ImageMemoryBarrier memoryBarrier( vk::AccessFlagBits::eTransferWrite,
@ -191,7 +184,7 @@ int main( int /*argc*/, char ** /*argv*/ )
VK_QUEUE_FAMILY_IGNORED, VK_QUEUE_FAMILY_IGNORED,
blitDestinationImage, blitDestinationImage,
vk::ImageSubresourceRange( vk::ImageAspectFlagBits::eColor, 0, 1, 0, 1 ) ); vk::ImageSubresourceRange( vk::ImageAspectFlagBits::eColor, 0, 1, 0, 1 ) );
commandBuffer->pipelineBarrier( commandBuffer.pipelineBarrier(
vk::PipelineStageFlagBits::eTransfer, vk::PipelineStageFlagBits::eTransfer, {}, nullptr, nullptr, memoryBarrier ); vk::PipelineStageFlagBits::eTransfer, vk::PipelineStageFlagBits::eTransfer, {}, nullptr, nullptr, memoryBarrier );
// Do a image copy to part of the dst image - checks should stay small // Do a image copy to part of the dst image - checks should stay small
@ -200,11 +193,11 @@ int main( int /*argc*/, char ** /*argv*/ )
imageSubresourceLayers, imageSubresourceLayers,
vk::Offset3D( 256, 256, 0 ), vk::Offset3D( 256, 256, 0 ),
vk::Extent3D( 128, 128, 1 ) ); vk::Extent3D( 128, 128, 1 ) );
commandBuffer->copyImage( **blitSourceImage, commandBuffer.copyImage( *blitSourceImage,
vk::ImageLayout::eTransferSrcOptimal, vk::ImageLayout::eTransferSrcOptimal,
blitDestinationImage, blitDestinationImage,
vk::ImageLayout::eTransferDstOptimal, vk::ImageLayout::eTransferDstOptimal,
imageCopy ); imageCopy );
vk::ImageMemoryBarrier prePresentBarrier( vk::ImageMemoryBarrier prePresentBarrier(
vk::AccessFlagBits::eTransferWrite, vk::AccessFlagBits::eTransferWrite,
@ -215,26 +208,26 @@ int main( int /*argc*/, char ** /*argv*/ )
VK_QUEUE_FAMILY_IGNORED, VK_QUEUE_FAMILY_IGNORED,
blitDestinationImage, blitDestinationImage,
vk::ImageSubresourceRange( vk::ImageAspectFlagBits::eColor, 0, 1, 0, 1 ) ); vk::ImageSubresourceRange( vk::ImageAspectFlagBits::eColor, 0, 1, 0, 1 ) );
commandBuffer->pipelineBarrier( vk::PipelineStageFlagBits::eTransfer, commandBuffer.pipelineBarrier( vk::PipelineStageFlagBits::eTransfer,
vk::PipelineStageFlagBits::eTopOfPipe, vk::PipelineStageFlagBits::eTopOfPipe,
{}, {},
nullptr, nullptr,
nullptr, nullptr,
prePresentBarrier ); prePresentBarrier );
commandBuffer->end(); commandBuffer.end();
std::unique_ptr<vk::raii::Fence> drawFence = vk::raii::su::make_unique<vk::raii::Fence>( *device, vk::FenceCreateInfo() ); vk::raii::Fence drawFence( device, vk::FenceCreateInfo() );
submitInfo = vk::SubmitInfo( {}, {}, **commandBuffer ); submitInfo = vk::SubmitInfo( {}, {}, *commandBuffer );
graphicsQueue->submit( submitInfo, **drawFence ); graphicsQueue.submit( submitInfo, *drawFence );
graphicsQueue->waitIdle(); graphicsQueue.waitIdle();
/* Make sure command buffer is finished before presenting */ /* Make sure command buffer is finished before presenting */
while ( device->waitForFences( { **drawFence }, true, vk::su::FenceTimeout ) == vk::Result::eTimeout ) while ( device.waitForFences( { *drawFence }, true, vk::su::FenceTimeout ) == vk::Result::eTimeout )
; ;
/* Now present the image in the window */ /* Now present the image in the window */
vk::PresentInfoKHR presentInfoKHR( nullptr, **swapChainData.swapChain, imageIndex ); vk::PresentInfoKHR presentInfoKHR( nullptr, **swapChainData.pSwapChain, imageIndex );
result = presentQueue->presentKHR( presentInfoKHR ); result = presentQueue.presentKHR( presentInfoKHR );
switch ( result ) switch ( result )
{ {
case vk::Result::eSuccess: break; case vk::Result::eSuccess: break;

View File

@ -92,15 +92,16 @@ int main( int /*argc*/, char ** /*argv*/ )
{ {
try try
{ {
std::unique_ptr<vk::raii::Context> context = vk::raii::su::make_unique<vk::raii::Context>(); vk::raii::Context context;
/* VULKAN_KEY_START */ /* VULKAN_KEY_START */
std::vector<vk::ExtensionProperties> props = context->enumerateInstanceExtensionProperties(); std::vector<vk::ExtensionProperties> props = context.enumerateInstanceExtensionProperties();
auto propsIterator = std::find_if( props.begin(), props.end(), []( vk::ExtensionProperties const & ep ) { auto propsIterator = std::find_if( props.begin(),
return strcmp( ep.extensionName, VK_EXT_DEBUG_UTILS_EXTENSION_NAME ) == 0; props.end(),
} ); []( vk::ExtensionProperties const & ep )
{ return strcmp( ep.extensionName, VK_EXT_DEBUG_UTILS_EXTENSION_NAME ) == 0; } );
if ( propsIterator == props.end() ) if ( propsIterator == props.end() )
{ {
std::cout << "Something went very wrong, cannot find " << VK_EXT_DEBUG_UTILS_EXTENSION_NAME << " extension" std::cout << "Something went very wrong, cannot find " << VK_EXT_DEBUG_UTILS_EXTENSION_NAME << " extension"
@ -108,10 +109,10 @@ int main( int /*argc*/, char ** /*argv*/ )
exit( 1 ); exit( 1 );
} }
vk::ApplicationInfo applicationInfo( AppName, 1, EngineName, 1, VK_API_VERSION_1_1 ); vk::ApplicationInfo applicationInfo( AppName, 1, EngineName, 1, VK_API_VERSION_1_1 );
const char * extensionName = VK_EXT_DEBUG_UTILS_EXTENSION_NAME; const char * extensionName = VK_EXT_DEBUG_UTILS_EXTENSION_NAME;
vk::InstanceCreateInfo instanceCreateInfo( {}, &applicationInfo, {}, extensionName ); vk::InstanceCreateInfo instanceCreateInfo( {}, &applicationInfo, {}, extensionName );
std::unique_ptr<vk::raii::Instance> instance = vk::raii::su::make_unique<vk::raii::Instance>( *context, instanceCreateInfo ); vk::raii::Instance instance( context, instanceCreateInfo );
vk::DebugUtilsMessageSeverityFlagsEXT severityFlags( vk::DebugUtilsMessageSeverityFlagBitsEXT::eWarning | vk::DebugUtilsMessageSeverityFlagsEXT severityFlags( vk::DebugUtilsMessageSeverityFlagBitsEXT::eWarning |
vk::DebugUtilsMessageSeverityFlagBitsEXT::eError ); vk::DebugUtilsMessageSeverityFlagBitsEXT::eError );
@ -120,8 +121,7 @@ int main( int /*argc*/, char ** /*argv*/ )
vk::DebugUtilsMessageTypeFlagBitsEXT::eValidation ); vk::DebugUtilsMessageTypeFlagBitsEXT::eValidation );
vk::DebugUtilsMessengerCreateInfoEXT debugUtilsMessengerCreateInfoEXT( vk::DebugUtilsMessengerCreateInfoEXT debugUtilsMessengerCreateInfoEXT(
{}, severityFlags, messageTypeFlags, &debugMessageFunc ); {}, severityFlags, messageTypeFlags, &debugMessageFunc );
std::unique_ptr<vk::raii::DebugUtilsMessengerEXT> debugUtilsMessenger = vk::raii::DebugUtilsMessengerEXT debugUtilsMessenger( instance, debugUtilsMessengerCreateInfoEXT );
vk::raii::su::make_unique<vk::raii::DebugUtilsMessengerEXT>( *instance, debugUtilsMessengerCreateInfoEXT );
/* VULKAN_KEY_END */ /* VULKAN_KEY_END */
} }

View File

@ -30,29 +30,28 @@ int main( int /*argc*/, char ** /*argv*/ )
{ {
try try
{ {
std::unique_ptr<vk::raii::Context> context = vk::raii::su::make_unique<vk::raii::Context>(); vk::raii::Context context;
std::unique_ptr<vk::raii::Instance> instance = vk::raii::Instance instance =
vk::raii::su::makeUniqueInstance( *context, AppName, EngineName, {}, vk::su::getInstanceExtensions() ); vk::raii::su::makeInstance( context, AppName, EngineName, {}, vk::su::getInstanceExtensions() );
#if !defined( NDEBUG ) #if !defined( NDEBUG )
std::unique_ptr<vk::raii::DebugUtilsMessengerEXT> debugUtilsMessenger = vk::raii::DebugUtilsMessengerEXT debugUtilsMessenger( instance, vk::su::makeDebugUtilsMessengerCreateInfoEXT() );
vk::raii::su::makeUniqueDebugUtilsMessengerEXT( *instance );
#endif #endif
vk::raii::PhysicalDevices physicalDevices( *instance );
vk::raii::PhysicalDevices physicalDevices( instance );
assert( !physicalDevices.empty() ); assert( !physicalDevices.empty() );
uint32_t graphicsQueueFamilyIndex = uint32_t graphicsQueueFamilyIndex =
vk::su::findGraphicsQueueFamilyIndex( physicalDevices[0].getQueueFamilyProperties() ); vk::su::findGraphicsQueueFamilyIndex( physicalDevices[0].getQueueFamilyProperties() );
std::unique_ptr<vk::raii::Device> device = vk::raii::Device device = vk::raii::su::makeDevice( physicalDevices[0], graphicsQueueFamilyIndex );
vk::raii::su::makeUniqueDevice( physicalDevices[0], graphicsQueueFamilyIndex );
// create an image // create an image
std::unique_ptr<vk::raii::Image> image = vk::raii::su::makeUniqueImage( *device ); vk::raii::Image image = vk::raii::su::makeImage( device );
/* VULKAN_KEY_START */ /* VULKAN_KEY_START */
vk::DebugUtilsObjectNameInfoEXT debugUtilsObjectNameInfo( vk::DebugUtilsObjectNameInfoEXT debugUtilsObjectNameInfo(
vk::ObjectType::eImage, NON_DISPATCHABLE_HANDLE_TO_UINT64_CAST( VkImage, **image ), "Image name" ); vk::ObjectType::eImage, NON_DISPATCHABLE_HANDLE_TO_UINT64_CAST( VkImage, *image ), "Image name" );
device->setDebugUtilsObjectNameEXT( debugUtilsObjectNameInfo ); device.setDebugUtilsObjectNameEXT( debugUtilsObjectNameInfo );
/* VULKAN_KEY_END */ /* VULKAN_KEY_END */
} }

View File

@ -32,35 +32,31 @@ int main( int /*argc*/, char ** /*argv*/ )
{ {
try try
{ {
std::unique_ptr<vk::raii::Context> context = vk::raii::su::make_unique<vk::raii::Context>(); vk::raii::Context context;
std::unique_ptr<vk::raii::Instance> instance = vk::raii::Instance instance =
vk::raii::su::makeUniqueInstance( *context, AppName, EngineName, {}, vk::su::getInstanceExtensions() ); vk::raii::su::makeInstance( context, AppName, EngineName, {}, vk::su::getInstanceExtensions() );
#if !defined( NDEBUG ) #if !defined( NDEBUG )
std::unique_ptr<vk::raii::DebugUtilsMessengerEXT> debugUtilsMessenger = vk::raii::DebugUtilsMessengerEXT debugUtilsMessenger( instance, vk::su::makeDebugUtilsMessengerCreateInfoEXT() );
vk::raii::su::makeUniqueDebugUtilsMessengerEXT( *instance );
#endif #endif
std::unique_ptr<vk::raii::PhysicalDevice> physicalDevice = vk::raii::su::makeUniquePhysicalDevice( *instance ); vk::raii::PhysicalDevice physicalDevice = std::move( vk::raii::PhysicalDevices( instance ).front() );
vk::raii::su::SurfaceData surfaceData( *instance, AppName, vk::Extent2D( 500, 500 ) ); vk::raii::su::SurfaceData surfaceData( instance, AppName, vk::Extent2D( 500, 500 ) );
std::pair<uint32_t, uint32_t> graphicsAndPresentQueueFamilyIndex = std::pair<uint32_t, uint32_t> graphicsAndPresentQueueFamilyIndex =
vk::raii::su::findGraphicsAndPresentQueueFamilyIndex( *physicalDevice, *surfaceData.surface ); vk::raii::su::findGraphicsAndPresentQueueFamilyIndex( physicalDevice, *surfaceData.pSurface );
std::unique_ptr<vk::raii::Device> device = vk::raii::su::makeUniqueDevice( vk::raii::Device device = vk::raii::su::makeDevice(
*physicalDevice, graphicsAndPresentQueueFamilyIndex.first, vk::su::getDeviceExtensions() ); physicalDevice, graphicsAndPresentQueueFamilyIndex.first, vk::su::getDeviceExtensions() );
std::unique_ptr<vk::raii::CommandPool> commandPool = vk::raii::CommandPool commandPool = vk::raii::CommandPool(
vk::raii::su::makeUniqueCommandPool( *device, graphicsAndPresentQueueFamilyIndex.first ); device, { vk::CommandPoolCreateFlagBits::eResetCommandBuffer, graphicsAndPresentQueueFamilyIndex.first } );
std::unique_ptr<vk::raii::CommandBuffer> commandBuffer = vk::raii::CommandBuffer commandBuffer = vk::raii::su::makeCommandBuffer( device, commandPool );
vk::raii::su::makeUniqueCommandBuffer( *device, *commandPool );
std::unique_ptr<vk::raii::Queue> graphicsQueue = vk::raii::Queue graphicsQueue( device, graphicsAndPresentQueueFamilyIndex.first, 0 );
vk::raii::su::make_unique<vk::raii::Queue>( *device, graphicsAndPresentQueueFamilyIndex.first, 0 ); vk::raii::Queue presentQueue( device, graphicsAndPresentQueueFamilyIndex.second, 0 );
std::unique_ptr<vk::raii::Queue> presentQueue =
vk::raii::su::make_unique<vk::raii::Queue>( *device, graphicsAndPresentQueueFamilyIndex.second, 0 );
vk::raii::su::SwapChainData swapChainData( *physicalDevice, vk::raii::su::SwapChainData swapChainData( physicalDevice,
*device, device,
*surfaceData.surface, *surfaceData.pSurface,
surfaceData.extent, surfaceData.extent,
vk::ImageUsageFlagBits::eColorAttachment | vk::ImageUsageFlagBits::eColorAttachment |
vk::ImageUsageFlagBits::eTransferSrc, vk::ImageUsageFlagBits::eTransferSrc,
@ -68,121 +64,117 @@ int main( int /*argc*/, char ** /*argv*/ )
graphicsAndPresentQueueFamilyIndex.first, graphicsAndPresentQueueFamilyIndex.first,
graphicsAndPresentQueueFamilyIndex.second ); graphicsAndPresentQueueFamilyIndex.second );
vk::raii::su::DepthBufferData depthBufferData( vk::raii::su::DepthBufferData depthBufferData( physicalDevice, device, vk::Format::eD16Unorm, surfaceData.extent );
*physicalDevice, *device, vk::Format::eD16Unorm, surfaceData.extent );
vk::raii::su::TextureData textureData( *physicalDevice, *device ); vk::raii::su::TextureData textureData( physicalDevice, device );
commandBuffer->begin( vk::CommandBufferBeginInfo() ); commandBuffer.begin( vk::CommandBufferBeginInfo() );
textureData.setImage( *commandBuffer, vk::su::CheckerboardImageGenerator() ); textureData.setImage( commandBuffer, vk::su::CheckerboardImageGenerator() );
vk::raii::su::BufferData uniformBufferData( vk::raii::su::BufferData uniformBufferData(
*physicalDevice, *device, sizeof( glm::mat4x4 ), vk::BufferUsageFlagBits::eUniformBuffer ); physicalDevice, device, sizeof( glm::mat4x4 ), vk::BufferUsageFlagBits::eUniformBuffer );
glm::mat4x4 mvpcMatrix = vk::su::createModelViewProjectionClipMatrix( surfaceData.extent ); glm::mat4x4 mvpcMatrix = vk::su::createModelViewProjectionClipMatrix( surfaceData.extent );
vk::raii::su::copyToDevice( *uniformBufferData.deviceMemory, mvpcMatrix ); vk::raii::su::copyToDevice( uniformBufferData.deviceMemory, mvpcMatrix );
std::unique_ptr<vk::raii::DescriptorSetLayout> descriptorSetLayout = vk::raii::su::makeUniqueDescriptorSetLayout( vk::raii::DescriptorSetLayout descriptorSetLayout = vk::raii::su::makeDescriptorSetLayout(
*device, device,
{ { vk::DescriptorType::eUniformBuffer, 1, vk::ShaderStageFlagBits::eVertex }, { { vk::DescriptorType::eUniformBuffer, 1, vk::ShaderStageFlagBits::eVertex },
{ vk::DescriptorType::eCombinedImageSampler, 1, vk::ShaderStageFlagBits::eFragment } } ); { vk::DescriptorType::eCombinedImageSampler, 1, vk::ShaderStageFlagBits::eFragment } } );
std::unique_ptr<vk::raii::PipelineLayout> pipelineLayout = vk::raii::PipelineLayout pipelineLayout( device, { {}, *descriptorSetLayout } );
vk::raii::su::makeUniquePipelineLayout( *device, *descriptorSetLayout );
vk::Format colorFormat = vk::Format colorFormat =
vk::su::pickSurfaceFormat( physicalDevice->getSurfaceFormatsKHR( **surfaceData.surface ) ).format; vk::su::pickSurfaceFormat( physicalDevice.getSurfaceFormatsKHR( **surfaceData.pSurface ) ).format;
std::unique_ptr<vk::raii::RenderPass> renderPass = vk::raii::RenderPass renderPass = vk::raii::su::makeRenderPass( device, colorFormat, depthBufferData.format );
vk::raii::su::makeUniqueRenderPass( *device, colorFormat, depthBufferData.format );
glslang::InitializeProcess(); glslang::InitializeProcess();
std::unique_ptr<vk::raii::ShaderModule> vertexShaderModule = vk::raii::ShaderModule vertexShaderModule =
vk::raii::su::makeUniqueShaderModule( *device, vk::ShaderStageFlagBits::eVertex, vertexShaderText_PT_T ); vk::raii::su::makeShaderModule( device, vk::ShaderStageFlagBits::eVertex, vertexShaderText_PT_T );
std::unique_ptr<vk::raii::ShaderModule> fragmentShaderModule = vk::raii::ShaderModule fragmentShaderModule =
vk::raii::su::makeUniqueShaderModule( *device, vk::ShaderStageFlagBits::eFragment, fragmentShaderText_T_C ); vk::raii::su::makeShaderModule( device, vk::ShaderStageFlagBits::eFragment, fragmentShaderText_T_C );
glslang::FinalizeProcess(); glslang::FinalizeProcess();
std::vector<std::unique_ptr<vk::raii::Framebuffer>> framebuffers = vk::raii::su::makeUniqueFramebuffers( std::vector<vk::raii::Framebuffer> framebuffers = vk::raii::su::makeFramebuffers(
*device, *renderPass, swapChainData.imageViews, depthBufferData.imageView, surfaceData.extent ); device, renderPass, swapChainData.imageViews, &*depthBufferData.pImageView, surfaceData.extent );
vk::raii::su::BufferData vertexBufferData( vk::raii::su::BufferData vertexBufferData(
*physicalDevice, *device, sizeof( texturedCubeData ), vk::BufferUsageFlagBits::eVertexBuffer ); physicalDevice, device, sizeof( texturedCubeData ), vk::BufferUsageFlagBits::eVertexBuffer );
vk::raii::su::copyToDevice( vk::raii::su::copyToDevice(
*vertexBufferData.deviceMemory, texturedCubeData, sizeof( texturedCubeData ) / sizeof( texturedCubeData[0] ) ); vertexBufferData.deviceMemory, texturedCubeData, sizeof( texturedCubeData ) / sizeof( texturedCubeData[0] ) );
std::unique_ptr<vk::raii::DescriptorPool> descriptorPool = vk::raii::su::makeUniqueDescriptorPool( vk::raii::DescriptorPool descriptorPool = vk::raii::su::makeDescriptorPool(
*device, { { vk::DescriptorType::eUniformBuffer, 1 }, { vk::DescriptorType::eCombinedImageSampler, 1 } } ); device, { { vk::DescriptorType::eUniformBuffer, 1 }, { vk::DescriptorType::eCombinedImageSampler, 1 } } );
std::unique_ptr<vk::raii::DescriptorSet> descriptorSet = vk::raii::DescriptorSet descriptorSet =
vk::raii::su::makeUniqueDescriptorSet( *device, *descriptorPool, *descriptorSetLayout ); std::move( vk::raii::DescriptorSets( device, { *descriptorPool, *descriptorSetLayout } ).front() );
vk::raii::su::updateDescriptorSets( *device, vk::raii::su::updateDescriptorSets( device,
*descriptorSet, descriptorSet,
{ { vk::DescriptorType::eUniformBuffer, *uniformBufferData.buffer, nullptr } }, { { vk::DescriptorType::eUniformBuffer, uniformBufferData.buffer, nullptr } },
{ textureData } ); { textureData } );
std::unique_ptr<vk::raii::PipelineCache> pipelineCache = vk::raii::PipelineCache pipelineCache( device, vk::PipelineCacheCreateInfo() );
vk::raii::su::make_unique<vk::raii::PipelineCache>( *device, vk::PipelineCacheCreateInfo() ); vk::raii::Pipeline graphicsPipeline =
std::unique_ptr<vk::raii::Pipeline> graphicsPipeline = vk::raii::su::makeUniqueGraphicsPipeline( vk::raii::su::makeGraphicsPipeline( device,
*device, pipelineCache,
*pipelineCache, vertexShaderModule,
*vertexShaderModule, nullptr,
nullptr, fragmentShaderModule,
*fragmentShaderModule, nullptr,
nullptr, sizeof( texturedCubeData[0] ),
sizeof( texturedCubeData[0] ), { { vk::Format::eR32G32B32A32Sfloat, 0 }, { vk::Format::eR32G32Sfloat, 16 } },
{ { vk::Format::eR32G32B32A32Sfloat, 0 }, { vk::Format::eR32G32Sfloat, 16 } }, vk::FrontFace::eClockwise,
vk::FrontFace::eClockwise, true,
true, pipelineLayout,
*pipelineLayout, renderPass );
*renderPass );
/* VULKAN_KEY_START */ /* VULKAN_KEY_START */
// Get the index of the next available swapchain image: // Get the index of the next available swapchain image:
std::unique_ptr<vk::raii::Semaphore> imageAcquiredSemaphore = vk::raii::Semaphore imageAcquiredSemaphore( device, vk::SemaphoreCreateInfo() );
vk::raii::su::make_unique<vk::raii::Semaphore>( *device, vk::SemaphoreCreateInfo() ); vk::Result result;
vk::Result result; uint32_t imageIndex;
uint32_t imageIndex;
std::tie( result, imageIndex ) = std::tie( result, imageIndex ) =
swapChainData.swapChain->acquireNextImage( vk::su::FenceTimeout, **imageAcquiredSemaphore ); swapChainData.pSwapChain->acquireNextImage( vk::su::FenceTimeout, *imageAcquiredSemaphore );
assert( result == vk::Result::eSuccess ); assert( result == vk::Result::eSuccess );
assert( imageIndex < swapChainData.images.size() ); assert( imageIndex < swapChainData.images.size() );
// commandBuffer->begin() has already been called above! // commandBuffer.begin() has already been called above!
std::array<vk::ClearValue, 2> clearValues; std::array<vk::ClearValue, 2> clearValues;
clearValues[0].color = vk::ClearColorValue( std::array<float, 4>( { { 0.2f, 0.2f, 0.2f, 0.2f } } ) ); clearValues[0].color = vk::ClearColorValue( std::array<float, 4>( { { 0.2f, 0.2f, 0.2f, 0.2f } } ) );
clearValues[1].depthStencil = vk::ClearDepthStencilValue( 1.0f, 0 ); clearValues[1].depthStencil = vk::ClearDepthStencilValue( 1.0f, 0 );
vk::RenderPassBeginInfo renderPassBeginInfo( vk::RenderPassBeginInfo renderPassBeginInfo(
**renderPass, **framebuffers[imageIndex], vk::Rect2D( vk::Offset2D( 0, 0 ), surfaceData.extent ), clearValues ); *renderPass, *framebuffers[imageIndex], vk::Rect2D( vk::Offset2D( 0, 0 ), surfaceData.extent ), clearValues );
commandBuffer->beginRenderPass( renderPassBeginInfo, vk::SubpassContents::eInline );
commandBuffer->bindPipeline( vk::PipelineBindPoint::eGraphics, **graphicsPipeline );
commandBuffer->bindDescriptorSets(
vk::PipelineBindPoint::eGraphics, **pipelineLayout, 0, { **descriptorSet }, nullptr );
commandBuffer->bindVertexBuffers( 0, { **vertexBufferData.buffer }, { 0 } ); commandBuffer.beginRenderPass( renderPassBeginInfo, vk::SubpassContents::eInline );
commandBuffer->setViewport( 0, commandBuffer.bindPipeline( vk::PipelineBindPoint::eGraphics, *graphicsPipeline );
vk::Viewport( 0.0f, commandBuffer.bindDescriptorSets(
0.0f, vk::PipelineBindPoint::eGraphics, *pipelineLayout, 0, { *descriptorSet }, nullptr );
static_cast<float>( surfaceData.extent.width ),
static_cast<float>( surfaceData.extent.height ),
0.0f,
1.0f ) );
commandBuffer->setScissor( 0, vk::Rect2D( vk::Offset2D( 0, 0 ), surfaceData.extent ) );
commandBuffer->draw( 12 * 3, 1, 0, 0 ); commandBuffer.bindVertexBuffers( 0, { *vertexBufferData.buffer }, { 0 } );
commandBuffer->endRenderPass(); commandBuffer.setViewport( 0,
commandBuffer->end(); vk::Viewport( 0.0f,
0.0f,
static_cast<float>( surfaceData.extent.width ),
static_cast<float>( surfaceData.extent.height ),
0.0f,
1.0f ) );
commandBuffer.setScissor( 0, vk::Rect2D( vk::Offset2D( 0, 0 ), surfaceData.extent ) );
std::unique_ptr<vk::raii::Fence> drawFence = vk::raii::su::make_unique<vk::raii::Fence>( *device, vk::FenceCreateInfo() ); commandBuffer.draw( 12 * 3, 1, 0, 0 );
commandBuffer.endRenderPass();
commandBuffer.end();
vk::raii::Fence drawFence( device, vk::FenceCreateInfo() );
vk::PipelineStageFlags waitDestinationStageMask( vk::PipelineStageFlagBits::eColorAttachmentOutput ); vk::PipelineStageFlags waitDestinationStageMask( vk::PipelineStageFlagBits::eColorAttachmentOutput );
vk::SubmitInfo submitInfo( **imageAcquiredSemaphore, waitDestinationStageMask, **commandBuffer ); vk::SubmitInfo submitInfo( *imageAcquiredSemaphore, waitDestinationStageMask, *commandBuffer );
graphicsQueue->submit( submitInfo, **drawFence ); graphicsQueue.submit( submitInfo, *drawFence );
while ( vk::Result::eTimeout == device->waitForFences( { **drawFence }, VK_TRUE, vk::su::FenceTimeout ) ) while ( vk::Result::eTimeout == device.waitForFences( { *drawFence }, VK_TRUE, vk::su::FenceTimeout ) )
; ;
vk::PresentInfoKHR presentInfoKHR( nullptr, **swapChainData.swapChain, imageIndex ); vk::PresentInfoKHR presentInfoKHR( nullptr, **swapChainData.pSwapChain, imageIndex );
result = presentQueue->presentKHR( presentInfoKHR ); result = presentQueue.presentKHR( presentInfoKHR );
switch ( result ) switch ( result )
{ {
case vk::Result::eSuccess: break; case vk::Result::eSuccess: break;

View File

@ -36,35 +36,31 @@ int main( int /*argc*/, char ** /*argv*/ )
{ {
try try
{ {
std::unique_ptr<vk::raii::Context> context = vk::raii::su::make_unique<vk::raii::Context>(); vk::raii::Context context;
std::unique_ptr<vk::raii::Instance> instance = vk::raii::Instance instance =
vk::raii::su::makeUniqueInstance( *context, AppName, EngineName, {}, vk::su::getInstanceExtensions() ); vk::raii::su::makeInstance( context, AppName, EngineName, {}, vk::su::getInstanceExtensions() );
#if !defined( NDEBUG ) #if !defined( NDEBUG )
std::unique_ptr<vk::raii::DebugUtilsMessengerEXT> debugUtilsMessenger = vk::raii::DebugUtilsMessengerEXT debugUtilsMessenger( instance, vk::su::makeDebugUtilsMessengerCreateInfoEXT() );
vk::raii::su::makeUniqueDebugUtilsMessengerEXT( *instance );
#endif #endif
std::unique_ptr<vk::raii::PhysicalDevice> physicalDevice = vk::raii::su::makeUniquePhysicalDevice( *instance ); vk::raii::PhysicalDevice physicalDevice = std::move( vk::raii::PhysicalDevices( instance ).front() );
vk::raii::su::SurfaceData surfaceData( *instance, AppName, vk::Extent2D( 500, 500 ) ); vk::raii::su::SurfaceData surfaceData( instance, AppName, vk::Extent2D( 500, 500 ) );
std::pair<uint32_t, uint32_t> graphicsAndPresentQueueFamilyIndex = std::pair<uint32_t, uint32_t> graphicsAndPresentQueueFamilyIndex =
vk::raii::su::findGraphicsAndPresentQueueFamilyIndex( *physicalDevice, *surfaceData.surface ); vk::raii::su::findGraphicsAndPresentQueueFamilyIndex( physicalDevice, *surfaceData.pSurface );
std::unique_ptr<vk::raii::Device> device = vk::raii::su::makeUniqueDevice( vk::raii::Device device = vk::raii::su::makeDevice(
*physicalDevice, graphicsAndPresentQueueFamilyIndex.first, vk::su::getDeviceExtensions() ); physicalDevice, graphicsAndPresentQueueFamilyIndex.first, vk::su::getDeviceExtensions() );
std::unique_ptr<vk::raii::CommandPool> commandPool = vk::raii::CommandPool commandPool = vk::raii::CommandPool(
vk::raii::su::makeUniqueCommandPool( *device, graphicsAndPresentQueueFamilyIndex.first ); device, { vk::CommandPoolCreateFlagBits::eResetCommandBuffer, graphicsAndPresentQueueFamilyIndex.first } );
std::unique_ptr<vk::raii::CommandBuffer> commandBuffer = vk::raii::CommandBuffer commandBuffer = vk::raii::su::makeCommandBuffer( device, commandPool );
vk::raii::su::makeUniqueCommandBuffer( *device, *commandPool );
std::unique_ptr<vk::raii::Queue> graphicsQueue = vk::raii::Queue graphicsQueue( device, graphicsAndPresentQueueFamilyIndex.first, 0 );
vk::raii::su::make_unique<vk::raii::Queue>( *device, graphicsAndPresentQueueFamilyIndex.first, 0 ); vk::raii::Queue presentQueue( device, graphicsAndPresentQueueFamilyIndex.second, 0 );
std::unique_ptr<vk::raii::Queue> presentQueue =
vk::raii::su::make_unique<vk::raii::Queue>( *device, graphicsAndPresentQueueFamilyIndex.second, 0 );
vk::raii::su::SwapChainData swapChainData( *physicalDevice, vk::raii::su::SwapChainData swapChainData( physicalDevice,
*device, device,
*surfaceData.surface, *surfaceData.pSurface,
surfaceData.extent, surfaceData.extent,
vk::ImageUsageFlagBits::eColorAttachment | vk::ImageUsageFlagBits::eColorAttachment |
vk::ImageUsageFlagBits::eTransferSrc, vk::ImageUsageFlagBits::eTransferSrc,
@ -72,32 +68,30 @@ int main( int /*argc*/, char ** /*argv*/ )
graphicsAndPresentQueueFamilyIndex.first, graphicsAndPresentQueueFamilyIndex.first,
graphicsAndPresentQueueFamilyIndex.second ); graphicsAndPresentQueueFamilyIndex.second );
vk::raii::su::DepthBufferData depthBufferData( vk::raii::su::DepthBufferData depthBufferData( physicalDevice, device, vk::Format::eD16Unorm, surfaceData.extent );
*physicalDevice, *device, vk::Format::eD16Unorm, surfaceData.extent );
vk::Format colorFormat = vk::Format colorFormat =
vk::su::pickSurfaceFormat( physicalDevice->getSurfaceFormatsKHR( **surfaceData.surface ) ).format; vk::su::pickSurfaceFormat( physicalDevice.getSurfaceFormatsKHR( **surfaceData.pSurface ) ).format;
std::unique_ptr<vk::raii::RenderPass> renderPass = vk::raii::RenderPass renderPass = vk::raii::su::makeRenderPass( device, colorFormat, depthBufferData.format );
vk::raii::su::makeUniqueRenderPass( *device, colorFormat, depthBufferData.format );
glslang::InitializeProcess(); glslang::InitializeProcess();
std::unique_ptr<vk::raii::ShaderModule> vertexShaderModule = vk::raii::ShaderModule vertexShaderModule =
vk::raii::su::makeUniqueShaderModule( *device, vk::ShaderStageFlagBits::eVertex, vertexShaderText_PC_C ); vk::raii::su::makeShaderModule( device, vk::ShaderStageFlagBits::eVertex, vertexShaderText_PC_C );
std::unique_ptr<vk::raii::ShaderModule> fragmentShaderModule = vk::raii::ShaderModule fragmentShaderModule =
vk::raii::su::makeUniqueShaderModule( *device, vk::ShaderStageFlagBits::eFragment, fragmentShaderText_C_C ); vk::raii::su::makeShaderModule( device, vk::ShaderStageFlagBits::eFragment, fragmentShaderText_C_C );
glslang::FinalizeProcess(); glslang::FinalizeProcess();
std::vector<std::unique_ptr<vk::raii::Framebuffer>> framebuffers = vk::raii::su::makeUniqueFramebuffers( std::vector<vk::raii::Framebuffer> framebuffers = vk::raii::su::makeFramebuffers(
*device, *renderPass, swapChainData.imageViews, depthBufferData.imageView, surfaceData.extent ); device, renderPass, swapChainData.imageViews, &*depthBufferData.pImageView, surfaceData.extent );
vk::raii::su::BufferData vertexBufferData( vk::raii::su::BufferData vertexBufferData(
*physicalDevice, *device, sizeof( coloredCubeData ), vk::BufferUsageFlagBits::eVertexBuffer ); physicalDevice, device, sizeof( coloredCubeData ), vk::BufferUsageFlagBits::eVertexBuffer );
vk::raii::su::copyToDevice( vk::raii::su::copyToDevice(
*vertexBufferData.deviceMemory, coloredCubeData, sizeof( coloredCubeData ) / sizeof( coloredCubeData[0] ) ); vertexBufferData.deviceMemory, coloredCubeData, sizeof( coloredCubeData ) / sizeof( coloredCubeData[0] ) );
/* VULKAN_KEY_START */ /* VULKAN_KEY_START */
vk::PhysicalDeviceLimits limits = physicalDevice->getProperties().limits; vk::PhysicalDeviceLimits limits = physicalDevice.getProperties().limits;
if ( limits.maxDescriptorSetUniformBuffersDynamic < 1 ) if ( limits.maxDescriptorSetUniformBuffersDynamic < 1 )
{ {
std::cout << "No dynamic uniform buffers supported\n"; std::cout << "No dynamic uniform buffers supported\n";
@ -129,100 +123,94 @@ int main( int /*argc*/, char ** /*argv*/ )
} }
vk::raii::su::BufferData uniformBufferData( vk::raii::su::BufferData uniformBufferData(
*physicalDevice, *device, 2 * bufferSize, vk::BufferUsageFlagBits::eUniformBuffer ); physicalDevice, device, 2 * bufferSize, vk::BufferUsageFlagBits::eUniformBuffer );
vk::raii::su::copyToDevice( *uniformBufferData.deviceMemory, mvpcs, 2, bufferSize ); vk::raii::su::copyToDevice( uniformBufferData.deviceMemory, mvpcs, 2, bufferSize );
// create a DescriptorSetLayout with vk::DescriptorType::eUniformBufferDynamic // create a DescriptorSetLayout with vk::DescriptorType::eUniformBufferDynamic
std::unique_ptr<vk::raii::DescriptorSetLayout> descriptorSetLayout = vk::raii::su::makeUniqueDescriptorSetLayout( vk::raii::DescriptorSetLayout descriptorSetLayout = vk::raii::su::makeDescriptorSetLayout(
*device, { { vk::DescriptorType::eUniformBufferDynamic, 1, vk::ShaderStageFlagBits::eVertex } } ); device, { { vk::DescriptorType::eUniformBufferDynamic, 1, vk::ShaderStageFlagBits::eVertex } } );
std::unique_ptr<vk::raii::PipelineLayout> pipelineLayout = vk::raii::PipelineLayout pipelineLayout( device, { {}, *descriptorSetLayout } );
vk::raii::su::makeUniquePipelineLayout( *device, *descriptorSetLayout );
// create a DescriptorPool with vk::DescriptorType::eUniformBufferDynamic // create a DescriptorPool with vk::DescriptorType::eUniformBufferDynamic
std::unique_ptr<vk::raii::DescriptorPool> descriptorPool = vk::raii::DescriptorPool descriptorPool =
vk::raii::su::makeUniqueDescriptorPool( *device, { { vk::DescriptorType::eUniformBufferDynamic, 1 } } ); vk::raii::su::makeDescriptorPool( device, { { vk::DescriptorType::eUniformBufferDynamic, 1 } } );
std::unique_ptr<vk::raii::DescriptorSet> descriptorSet = vk::raii::DescriptorSet descriptorSet =
vk::raii::su::makeUniqueDescriptorSet( *device, *descriptorPool, *descriptorSetLayout ); std::move( vk::raii::DescriptorSets( device, { *descriptorPool, *descriptorSetLayout } ).front() );
vk::raii::su::updateDescriptorSets( vk::raii::su::updateDescriptorSets(
*device, device, descriptorSet, { { vk::DescriptorType::eUniformBufferDynamic, uniformBufferData.buffer, nullptr } }, {} );
*descriptorSet,
{ { vk::DescriptorType::eUniformBufferDynamic, *uniformBufferData.buffer, nullptr } },
{} );
std::unique_ptr<vk::raii::PipelineCache> pipelineCache = vk::raii::PipelineCache pipelineCache( device, vk::PipelineCacheCreateInfo() );
vk::raii::su::make_unique<vk::raii::PipelineCache>( *device, vk::PipelineCacheCreateInfo() ); vk::raii::Pipeline graphicsPipeline =
std::unique_ptr<vk::raii::Pipeline> graphicsPipeline = vk::raii::su::makeUniqueGraphicsPipeline( vk::raii::su::makeGraphicsPipeline( device,
*device, pipelineCache,
*pipelineCache, vertexShaderModule,
*vertexShaderModule, nullptr,
nullptr, fragmentShaderModule,
*fragmentShaderModule, nullptr,
nullptr, sizeof( coloredCubeData[0] ),
sizeof( coloredCubeData[0] ), { { vk::Format::eR32G32B32A32Sfloat, 0 }, { vk::Format::eR32G32Sfloat, 16 } },
{ { vk::Format::eR32G32B32A32Sfloat, 0 }, { vk::Format::eR32G32Sfloat, 16 } }, vk::FrontFace::eClockwise,
vk::FrontFace::eClockwise, true,
true, pipelineLayout,
*pipelineLayout, renderPass );
*renderPass );
// Get the index of the next available swapchain image: // Get the index of the next available swapchain image:
std::unique_ptr<vk::raii::Semaphore> imageAcquiredSemaphore = vk::raii::Semaphore imageAcquiredSemaphore( device, vk::SemaphoreCreateInfo() );
vk::raii::su::make_unique<vk::raii::Semaphore>( *device, vk::SemaphoreCreateInfo() ); vk::Result result;
vk::Result result; uint32_t imageIndex;
uint32_t imageIndex;
std::tie( result, imageIndex ) = std::tie( result, imageIndex ) =
swapChainData.swapChain->acquireNextImage( vk::su::FenceTimeout, **imageAcquiredSemaphore ); swapChainData.pSwapChain->acquireNextImage( vk::su::FenceTimeout, *imageAcquiredSemaphore );
assert( result == vk::Result::eSuccess ); assert( result == vk::Result::eSuccess );
assert( imageIndex < swapChainData.images.size() ); assert( imageIndex < swapChainData.images.size() );
commandBuffer->begin( {} ); commandBuffer.begin( {} );
std::array<vk::ClearValue, 2> clearValues; std::array<vk::ClearValue, 2> clearValues;
clearValues[0].color = vk::ClearColorValue( std::array<float, 4>( { { 0.2f, 0.2f, 0.2f, 0.2f } } ) ); clearValues[0].color = vk::ClearColorValue( std::array<float, 4>( { { 0.2f, 0.2f, 0.2f, 0.2f } } ) );
clearValues[1].depthStencil = vk::ClearDepthStencilValue( 1.0f, 0 ); clearValues[1].depthStencil = vk::ClearDepthStencilValue( 1.0f, 0 );
vk::RenderPassBeginInfo renderPassBeginInfo( vk::RenderPassBeginInfo renderPassBeginInfo(
**renderPass, **framebuffers[imageIndex], vk::Rect2D( vk::Offset2D( 0, 0 ), surfaceData.extent ), clearValues ); *renderPass, *framebuffers[imageIndex], vk::Rect2D( vk::Offset2D( 0, 0 ), surfaceData.extent ), clearValues );
commandBuffer->beginRenderPass( renderPassBeginInfo, vk::SubpassContents::eInline ); commandBuffer.beginRenderPass( renderPassBeginInfo, vk::SubpassContents::eInline );
commandBuffer->bindPipeline( vk::PipelineBindPoint::eGraphics, **graphicsPipeline ); commandBuffer.bindPipeline( vk::PipelineBindPoint::eGraphics, *graphicsPipeline );
commandBuffer->setViewport( 0, commandBuffer.setViewport( 0,
vk::Viewport( 0.0f, vk::Viewport( 0.0f,
0.0f, 0.0f,
static_cast<float>( surfaceData.extent.width ), static_cast<float>( surfaceData.extent.width ),
static_cast<float>( surfaceData.extent.height ), static_cast<float>( surfaceData.extent.height ),
0.0f, 0.0f,
1.0f ) ); 1.0f ) );
commandBuffer->setScissor( 0, vk::Rect2D( vk::Offset2D( 0, 0 ), surfaceData.extent ) ); commandBuffer.setScissor( 0, vk::Rect2D( vk::Offset2D( 0, 0 ), surfaceData.extent ) );
/* The first draw should use the first matrix in the buffer */ /* The first draw should use the first matrix in the buffer */
uint32_t dynamicOffset = 0; uint32_t dynamicOffset = 0;
commandBuffer->bindDescriptorSets( commandBuffer.bindDescriptorSets(
vk::PipelineBindPoint::eGraphics, **pipelineLayout, 0, { **descriptorSet }, dynamicOffset ); vk::PipelineBindPoint::eGraphics, *pipelineLayout, 0, { *descriptorSet }, dynamicOffset );
commandBuffer->bindVertexBuffers( 0, { **vertexBufferData.buffer }, { 0 } ); commandBuffer.bindVertexBuffers( 0, { *vertexBufferData.buffer }, { 0 } );
commandBuffer->draw( 12 * 3, 1, 0, 0 ); commandBuffer.draw( 12 * 3, 1, 0, 0 );
// the second draw should use the second matrix in the buffer; // the second draw should use the second matrix in the buffer;
dynamicOffset = (uint32_t)bufferSize; dynamicOffset = (uint32_t)bufferSize;
commandBuffer->bindDescriptorSets( commandBuffer.bindDescriptorSets(
vk::PipelineBindPoint::eGraphics, **pipelineLayout, 0, { **descriptorSet }, dynamicOffset ); vk::PipelineBindPoint::eGraphics, *pipelineLayout, 0, { *descriptorSet }, dynamicOffset );
commandBuffer->draw( 12 * 3, 1, 0, 0 ); commandBuffer.draw( 12 * 3, 1, 0, 0 );
commandBuffer->endRenderPass(); commandBuffer.endRenderPass();
commandBuffer->end(); commandBuffer.end();
std::unique_ptr<vk::raii::Fence> drawFence = vk::raii::su::make_unique<vk::raii::Fence>( *device, vk::FenceCreateInfo() ); vk::raii::Fence drawFence( device, vk::FenceCreateInfo() );
vk::PipelineStageFlags waitDestinationStageMask( vk::PipelineStageFlagBits::eColorAttachmentOutput ); vk::PipelineStageFlags waitDestinationStageMask( vk::PipelineStageFlagBits::eColorAttachmentOutput );
vk::SubmitInfo submitInfo( **imageAcquiredSemaphore, waitDestinationStageMask, **commandBuffer ); vk::SubmitInfo submitInfo( *imageAcquiredSemaphore, waitDestinationStageMask, *commandBuffer );
graphicsQueue->submit( submitInfo, **drawFence ); graphicsQueue.submit( submitInfo, *drawFence );
while ( vk::Result::eTimeout == device->waitForFences( { **drawFence }, VK_TRUE, vk::su::FenceTimeout ) ) while ( vk::Result::eTimeout == device.waitForFences( { *drawFence }, VK_TRUE, vk::su::FenceTimeout ) )
; ;
vk::PresentInfoKHR presentInfoKHR( nullptr, **swapChainData.swapChain, imageIndex ); vk::PresentInfoKHR presentInfoKHR( nullptr, **swapChainData.pSwapChain, imageIndex );
result = presentQueue->presentKHR( presentInfoKHR ); result = presentQueue.presentKHR( presentInfoKHR );
switch ( result ) switch ( result )
{ {
case vk::Result::eSuccess: break; case vk::Result::eSuccess: break;

View File

@ -109,20 +109,24 @@ VKAPI_ATTR VkBool32 VKAPI_CALL debugMessageFunc( VkDebugUtilsMessageSeverityFlag
bool checkLayers( std::vector<char const *> const & layers, std::vector<vk::LayerProperties> const & properties ) bool checkLayers( std::vector<char const *> const & layers, std::vector<vk::LayerProperties> const & properties )
{ {
// return true if all layers are listed in the properties // return true if all layers are listed in the properties
return std::all_of( layers.begin(), layers.end(), [&properties]( char const * name ) { return std::all_of( layers.begin(),
return std::find_if( properties.begin(), properties.end(), [&name]( vk::LayerProperties const & property ) { layers.end(),
return strcmp( property.layerName, name ) == 0; [&properties]( char const * name )
} ) != properties.end(); {
} ); return std::find_if( properties.begin(),
properties.end(),
[&name]( vk::LayerProperties const & property )
{ return strcmp( property.layerName, name ) == 0; } ) != properties.end();
} );
} }
int main( int /*argc*/, char ** /*argv*/ ) int main( int /*argc*/, char ** /*argv*/ )
{ {
try try
{ {
std::unique_ptr<vk::raii::Context> context = vk::raii::su::make_unique<vk::raii::Context>(); vk::raii::Context context;
std::vector<vk::LayerProperties> instanceLayerProperties = context->enumerateInstanceLayerProperties(); std::vector<vk::LayerProperties> instanceLayerProperties = context.enumerateInstanceLayerProperties();
/* VULKAN_KEY_START */ /* VULKAN_KEY_START */
@ -141,10 +145,10 @@ int main( int /*argc*/, char ** /*argv*/ )
vk::ApplicationInfo applicationInfo( AppName, 1, EngineName, 1, VK_API_VERSION_1_1 ); vk::ApplicationInfo applicationInfo( AppName, 1, EngineName, 1, VK_API_VERSION_1_1 );
vk::InstanceCreateInfo instanceCreateInfo( {}, &applicationInfo, instanceLayerNames, instanceExtensionNames ); vk::InstanceCreateInfo instanceCreateInfo( {}, &applicationInfo, instanceLayerNames, instanceExtensionNames );
std::unique_ptr<vk::raii::Instance> instance = vk::raii::su::make_unique<vk::raii::Instance>( *context, instanceCreateInfo ); vk::raii::Instance instance( context, instanceCreateInfo );
pfnVkCreateDebugUtilsMessengerEXT = pfnVkCreateDebugUtilsMessengerEXT =
reinterpret_cast<PFN_vkCreateDebugUtilsMessengerEXT>( instance->getProcAddr( "vkCreateDebugUtilsMessengerEXT" ) ); reinterpret_cast<PFN_vkCreateDebugUtilsMessengerEXT>( instance.getProcAddr( "vkCreateDebugUtilsMessengerEXT" ) );
if ( !pfnVkCreateDebugUtilsMessengerEXT ) if ( !pfnVkCreateDebugUtilsMessengerEXT )
{ {
std::cout << "GetInstanceProcAddr: Unable to find pfnVkCreateDebugUtilsMessengerEXT function." << std::endl; std::cout << "GetInstanceProcAddr: Unable to find pfnVkCreateDebugUtilsMessengerEXT function." << std::endl;
@ -152,7 +156,7 @@ int main( int /*argc*/, char ** /*argv*/ )
} }
pfnVkDestroyDebugUtilsMessengerEXT = reinterpret_cast<PFN_vkDestroyDebugUtilsMessengerEXT>( pfnVkDestroyDebugUtilsMessengerEXT = reinterpret_cast<PFN_vkDestroyDebugUtilsMessengerEXT>(
instance->getProcAddr( "vkDestroyDebugUtilsMessengerEXT" ) ); instance.getProcAddr( "vkDestroyDebugUtilsMessengerEXT" ) );
if ( !pfnVkDestroyDebugUtilsMessengerEXT ) if ( !pfnVkDestroyDebugUtilsMessengerEXT )
{ {
std::cout << "GetInstanceProcAddr: Unable to find pfnVkDestroyDebugUtilsMessengerEXT function." << std::endl; std::cout << "GetInstanceProcAddr: Unable to find pfnVkDestroyDebugUtilsMessengerEXT function." << std::endl;
@ -166,24 +170,23 @@ int main( int /*argc*/, char ** /*argv*/ )
vk::DebugUtilsMessageTypeFlagBitsEXT::eValidation ); vk::DebugUtilsMessageTypeFlagBitsEXT::eValidation );
vk::DebugUtilsMessengerCreateInfoEXT debugUtilsMessengerCreateInfoEXT( vk::DebugUtilsMessengerCreateInfoEXT debugUtilsMessengerCreateInfoEXT(
{}, severityFlags, messageTypeFlags, &debugMessageFunc ); {}, severityFlags, messageTypeFlags, &debugMessageFunc );
std::unique_ptr<vk::raii::DebugUtilsMessengerEXT> debugUtilsMessenger = vk::raii::DebugUtilsMessengerEXT debugUtilsMessenger( instance, debugUtilsMessengerCreateInfoEXT );
vk::raii::su::make_unique<vk::raii::DebugUtilsMessengerEXT>( *instance, debugUtilsMessengerCreateInfoEXT );
std::unique_ptr<vk::raii::PhysicalDevice> physicalDevice = vk::raii::su::makeUniquePhysicalDevice( *instance ); vk::raii::PhysicalDevice physicalDevice = std::move( vk::raii::PhysicalDevices( instance ).front() );
// get the index of the first queue family that supports graphics // get the index of the first queue family that supports graphics
uint32_t graphicsQueueFamilyIndex = uint32_t graphicsQueueFamilyIndex =
vk::su::findGraphicsQueueFamilyIndex( physicalDevice->getQueueFamilyProperties() ); vk::su::findGraphicsQueueFamilyIndex( physicalDevice.getQueueFamilyProperties() );
float queuePriority = 0.0f; float queuePriority = 0.0f;
vk::DeviceQueueCreateInfo deviceQueueCreateInfo( {}, graphicsQueueFamilyIndex, 1, &queuePriority ); vk::DeviceQueueCreateInfo deviceQueueCreateInfo( {}, graphicsQueueFamilyIndex, 1, &queuePriority );
vk::DeviceCreateInfo deviceCreateInfo( {}, deviceQueueCreateInfo ); vk::DeviceCreateInfo deviceCreateInfo( {}, deviceQueueCreateInfo );
std::unique_ptr<vk::raii::Device> device = vk::raii::su::make_unique<vk::raii::Device>( *physicalDevice, deviceCreateInfo ); vk::raii::Device device( physicalDevice, deviceCreateInfo );
// Create a vk::CommandPool (not a vk::raii::CommandPool, for testing purposes!) // Create a vk::CommandPool (not a vk::raii::CommandPool, for testing purposes!)
vk::CommandPoolCreateInfo commandPoolCreateInfo( {}, graphicsQueueFamilyIndex ); vk::CommandPoolCreateInfo commandPoolCreateInfo( {}, graphicsQueueFamilyIndex );
vk::CommandPool commandPool = vk::CommandPool commandPool =
( **device ).createCommandPool( commandPoolCreateInfo, nullptr, *device->getDispatcher() ); ( *device ).createCommandPool( commandPoolCreateInfo, nullptr, *device.getDispatcher() );
// The commandPool is not destroyed automatically (as it's not a UniqueCommandPool. // The commandPool is not destroyed automatically (as it's not a UniqueCommandPool.
// That is, the device is destroyed before the commmand pool and will trigger a validation error. // That is, the device is destroyed before the commmand pool and will trigger a validation error.

View File

@ -28,18 +28,17 @@ int main( int /*argc*/, char ** /*argv*/ )
{ {
try try
{ {
std::unique_ptr<vk::raii::Context> context = vk::raii::su::make_unique<vk::raii::Context>(); vk::raii::Context context;
std::unique_ptr<vk::raii::Instance> instance = vk::raii::Instance instance =
vk::raii::su::makeUniqueInstance( *context, AppName, EngineName, {}, vk::su::getInstanceExtensions() ); vk::raii::su::makeInstance( context, AppName, EngineName, {}, vk::su::getInstanceExtensions() );
#if !defined( NDEBUG ) #if !defined( NDEBUG )
std::unique_ptr<vk::raii::DebugUtilsMessengerEXT> debugUtilsMessenger = vk::raii::DebugUtilsMessengerEXT debugUtilsMessenger( instance, vk::su::makeDebugUtilsMessengerCreateInfoEXT() );
vk::raii::su::makeUniqueDebugUtilsMessengerEXT( *instance );
#endif #endif
/* VULKAN_HPP_KEY_START */ /* VULKAN_HPP_KEY_START */
// enumerate the physicalDevices // enumerate the physicalDevices
vk::raii::PhysicalDevices physicalDevices( *instance ); vk::raii::PhysicalDevices physicalDevices( instance );
for ( auto const & pdh : physicalDevices ) for ( auto const & pdh : physicalDevices )
{ {

View File

@ -27,46 +27,43 @@ int main( int /*argc*/, char ** /*argv*/ )
{ {
try try
{ {
std::unique_ptr<vk::raii::Context> context = vk::raii::su::make_unique<vk::raii::Context>(); vk::raii::Context context;
std::unique_ptr<vk::raii::Instance> instance = vk::raii::Instance instance =
vk::raii::su::makeUniqueInstance( *context, AppName, EngineName, {}, vk::su::getInstanceExtensions() ); vk::raii::su::makeInstance( context, AppName, EngineName, {}, vk::su::getInstanceExtensions() );
#if !defined( NDEBUG ) #if !defined( NDEBUG )
std::unique_ptr<vk::raii::DebugUtilsMessengerEXT> debugUtilsMessenger = vk::raii::DebugUtilsMessengerEXT debugUtilsMessenger( instance, vk::su::makeDebugUtilsMessengerCreateInfoEXT() );
vk::raii::su::makeUniqueDebugUtilsMessengerEXT( *instance );
#endif #endif
std::unique_ptr<vk::raii::PhysicalDevice> physicalDevice = vk::raii::su::makeUniquePhysicalDevice( *instance ); vk::raii::PhysicalDevice physicalDevice = std::move( vk::raii::PhysicalDevices( instance ).front() );
uint32_t graphicsQueueFamilyIndex = uint32_t graphicsQueueFamilyIndex =
vk::su::findGraphicsQueueFamilyIndex( physicalDevice->getQueueFamilyProperties() ); vk::su::findGraphicsQueueFamilyIndex( physicalDevice.getQueueFamilyProperties() );
std::unique_ptr<vk::raii::Device> device = vk::raii::Device device =
vk::raii::su::makeUniqueDevice( *physicalDevice, graphicsQueueFamilyIndex, vk::su::getDeviceExtensions() ); vk::raii::su::makeDevice( physicalDevice, graphicsQueueFamilyIndex, vk::su::getDeviceExtensions() );
std::unique_ptr<vk::raii::CommandPool> commandPool = vk::raii::CommandPool commandPool =
vk::raii::su::makeUniqueCommandPool( *device, graphicsQueueFamilyIndex ); vk::raii::CommandPool( device, { vk::CommandPoolCreateFlagBits::eResetCommandBuffer, graphicsQueueFamilyIndex } );
std::unique_ptr<vk::raii::CommandBuffer> commandBuffer = vk::raii::CommandBuffer commandBuffer = vk::raii::su::makeCommandBuffer( device, commandPool );
vk::raii::su::makeUniqueCommandBuffer( *device, *commandPool );
std::unique_ptr<vk::raii::Queue> graphicsQueue = vk::raii::Queue graphicsQueue( device, graphicsQueueFamilyIndex, 0 );
vk::raii::su::make_unique<vk::raii::Queue>( *device, graphicsQueueFamilyIndex, 0 );
/* VULKAN_KEY_START */ /* VULKAN_KEY_START */
// Start with a trivial command buffer and make sure fence wait doesn't time out // Start with a trivial command buffer and make sure fence wait doesn't time out
commandBuffer->begin( {} ); commandBuffer.begin( {} );
commandBuffer->setViewport( 0, vk::Viewport( 0.0f, 0.0f, 10.0f, 10.0f, 0.0f, 1.0f ) ); commandBuffer.setViewport( 0, vk::Viewport( 0.0f, 0.0f, 10.0f, 10.0f, 0.0f, 1.0f ) );
commandBuffer->end(); commandBuffer.end();
std::unique_ptr<vk::raii::Fence> fence = vk::raii::su::make_unique<vk::raii::Fence>( *device, vk::FenceCreateInfo() ); vk::raii::Fence fence( device, vk::FenceCreateInfo() );
vk::SubmitInfo submitInfo( {}, {}, **commandBuffer ); vk::SubmitInfo submitInfo( {}, {}, *commandBuffer );
graphicsQueue->submit( submitInfo, **fence ); graphicsQueue.submit( submitInfo, *fence );
// Make sure timeout is long enough for a simple command buffer without waiting for an event // Make sure timeout is long enough for a simple command buffer without waiting for an event
vk::Result result; vk::Result result;
int timeouts = -1; int timeouts = -1;
do do
{ {
result = device->waitForFences( { **fence }, true, vk::su::FenceTimeout ); result = device.waitForFences( { *fence }, true, vk::su::FenceTimeout );
timeouts++; timeouts++;
} while ( result == vk::Result::eTimeout ); } while ( result == vk::Result::eTimeout );
assert( result == vk::Result::eSuccess ); assert( result == vk::Result::eSuccess );
@ -77,25 +74,25 @@ int main( int /*argc*/, char ** /*argv*/ )
} }
// Now create an event and wait for it on the GPU // Now create an event and wait for it on the GPU
std::unique_ptr<vk::raii::Event> event = vk::raii::su::make_unique<vk::raii::Event>( *device, vk::EventCreateInfo() ); vk::raii::Event event( device, vk::EventCreateInfo() );
commandBuffer->reset( vk::CommandBufferResetFlags() ); commandBuffer.reset( vk::CommandBufferResetFlags() );
commandBuffer->begin( vk::CommandBufferBeginInfo() ); commandBuffer.begin( vk::CommandBufferBeginInfo() );
commandBuffer->waitEvents( { **event }, commandBuffer.waitEvents( { *event },
vk::PipelineStageFlagBits::eHost, vk::PipelineStageFlagBits::eHost,
vk::PipelineStageFlagBits::eBottomOfPipe, vk::PipelineStageFlagBits::eBottomOfPipe,
nullptr, nullptr,
nullptr, nullptr,
nullptr ); nullptr );
commandBuffer->end(); commandBuffer.end();
device->resetFences( { **fence } ); device.resetFences( { *fence } );
// Note that stepping through this code in the debugger is a bad idea because the GPU can TDR waiting for the event. // Note that stepping through this code in the debugger is a bad idea because the GPU can TDR waiting for the event.
// Execute the code from vk::Queue::submit() through vk::Device::setEvent() without breakpoints // Execute the code from vk::Queue::submit() through vk::Device::setEvent() without breakpoints
graphicsQueue->submit( submitInfo, **fence ); graphicsQueue.submit( submitInfo, *fence );
// We should timeout waiting for the fence because the GPU should be waiting on the event // We should timeout waiting for the fence because the GPU should be waiting on the event
result = device->waitForFences( { **fence }, true, vk::su::FenceTimeout ); result = device.waitForFences( { *fence }, true, vk::su::FenceTimeout );
if ( result != vk::Result::eTimeout ) if ( result != vk::Result::eTimeout )
{ {
std::cout << "Didn't get expected timeout in vk::Device::waitForFences, exiting\n"; std::cout << "Didn't get expected timeout in vk::Device::waitForFences, exiting\n";
@ -104,40 +101,40 @@ int main( int /*argc*/, char ** /*argv*/ )
// Set the event from the CPU and wait for the fence. // Set the event from the CPU and wait for the fence.
// This should succeed since we set the event // This should succeed since we set the event
event->set(); event.set();
do do
{ {
result = device->waitForFences( { **fence }, true, vk::su::FenceTimeout ); result = device.waitForFences( { *fence }, true, vk::su::FenceTimeout );
} while ( result == vk::Result::eTimeout ); } while ( result == vk::Result::eTimeout );
assert( result == vk::Result::eSuccess ); assert( result == vk::Result::eSuccess );
commandBuffer->reset( {} ); commandBuffer.reset( {} );
device->resetFences( { **fence } ); device.resetFences( { *fence } );
event->reset(); event.reset();
// Now set the event from the GPU and wait on the CPU // Now set the event from the GPU and wait on the CPU
commandBuffer->begin( vk::CommandBufferBeginInfo() ); commandBuffer.begin( vk::CommandBufferBeginInfo() );
commandBuffer->setEvent( **event, vk::PipelineStageFlagBits::eBottomOfPipe ); commandBuffer.setEvent( *event, vk::PipelineStageFlagBits::eBottomOfPipe );
commandBuffer->end(); commandBuffer.end();
// Look for the event on the CPU. It should be vk::Result::eEventReset since we haven't sent the command buffer yet. // Look for the event on the CPU. It should be vk::Result::eEventReset since we haven't sent the command buffer yet.
result = event->getStatus(); result = event.getStatus();
assert( result == vk::Result::eEventReset ); assert( result == vk::Result::eEventReset );
// Send the command buffer and loop waiting for the event // Send the command buffer and loop waiting for the event
graphicsQueue->submit( submitInfo, **fence ); graphicsQueue.submit( submitInfo, *fence );
int polls = 0; int polls = 0;
do do
{ {
result = event->getStatus(); result = event.getStatus();
polls++; polls++;
} while ( result != vk::Result::eEventSet ); } while ( result != vk::Result::eEventSet );
printf( "%d polls to find the event set\n", polls ); printf( "%d polls to find the event set\n", polls );
do do
{ {
result = device->waitForFences( { **fence }, true, vk::su::FenceTimeout ); result = device.waitForFences( { *fence }, true, vk::su::FenceTimeout );
} while ( result == vk::Result::eTimeout ); } while ( result == vk::Result::eTimeout );
assert( result == vk::Result::eSuccess ); assert( result == vk::Result::eSuccess );

View File

@ -44,35 +44,31 @@ int main( int /*argc*/, char ** /*argv*/ )
{ {
try try
{ {
std::unique_ptr<vk::raii::Context> context = vk::raii::su::make_unique<vk::raii::Context>(); vk::raii::Context context;
std::unique_ptr<vk::raii::Instance> instance = vk::raii::Instance instance =
vk::raii::su::makeUniqueInstance( *context, AppName, EngineName, {}, vk::su::getInstanceExtensions() ); vk::raii::su::makeInstance( context, AppName, EngineName, {}, vk::su::getInstanceExtensions() );
#if !defined( NDEBUG ) #if !defined( NDEBUG )
std::unique_ptr<vk::raii::DebugUtilsMessengerEXT> debugUtilsMessenger = vk::raii::DebugUtilsMessengerEXT debugUtilsMessenger( instance, vk::su::makeDebugUtilsMessengerCreateInfoEXT() );
vk::raii::su::makeUniqueDebugUtilsMessengerEXT( *instance );
#endif #endif
std::unique_ptr<vk::raii::PhysicalDevice> physicalDevice = vk::raii::su::makeUniquePhysicalDevice( *instance ); vk::raii::PhysicalDevice physicalDevice = std::move( vk::raii::PhysicalDevices( instance ).front() );
vk::raii::su::SurfaceData surfaceData( *instance, AppName, vk::Extent2D( 500, 500 ) ); vk::raii::su::SurfaceData surfaceData( instance, AppName, vk::Extent2D( 500, 500 ) );
std::pair<uint32_t, uint32_t> graphicsAndPresentQueueFamilyIndex = std::pair<uint32_t, uint32_t> graphicsAndPresentQueueFamilyIndex =
vk::raii::su::findGraphicsAndPresentQueueFamilyIndex( *physicalDevice, *surfaceData.surface ); vk::raii::su::findGraphicsAndPresentQueueFamilyIndex( physicalDevice, *surfaceData.pSurface );
std::unique_ptr<vk::raii::Device> device = vk::raii::su::makeUniqueDevice( vk::raii::Device device = vk::raii::su::makeDevice(
*physicalDevice, graphicsAndPresentQueueFamilyIndex.first, vk::su::getDeviceExtensions() ); physicalDevice, graphicsAndPresentQueueFamilyIndex.first, vk::su::getDeviceExtensions() );
std::unique_ptr<vk::raii::CommandPool> commandPool = vk::raii::CommandPool commandPool = vk::raii::CommandPool(
vk::raii::su::makeUniqueCommandPool( *device, graphicsAndPresentQueueFamilyIndex.first ); device, { vk::CommandPoolCreateFlagBits::eResetCommandBuffer, graphicsAndPresentQueueFamilyIndex.first } );
std::unique_ptr<vk::raii::CommandBuffer> commandBuffer = vk::raii::CommandBuffer commandBuffer = vk::raii::su::makeCommandBuffer( device, commandPool );
vk::raii::su::makeUniqueCommandBuffer( *device, *commandPool );
std::unique_ptr<vk::raii::Queue> graphicsQueue = vk::raii::Queue graphicsQueue( device, graphicsAndPresentQueueFamilyIndex.first, 0 );
vk::raii::su::make_unique<vk::raii::Queue>( *device, graphicsAndPresentQueueFamilyIndex.first, 0 ); vk::raii::Queue presentQueue( device, graphicsAndPresentQueueFamilyIndex.second, 0 );
std::unique_ptr<vk::raii::Queue> presentQueue =
vk::raii::su::make_unique<vk::raii::Queue>( *device, graphicsAndPresentQueueFamilyIndex.second, 0 );
vk::raii::su::SwapChainData swapChainData( *physicalDevice, vk::raii::su::SwapChainData swapChainData( physicalDevice,
*device, device,
*surfaceData.surface, *surfaceData.pSurface,
surfaceData.extent, surfaceData.extent,
vk::ImageUsageFlagBits::eColorAttachment | vk::ImageUsageFlagBits::eColorAttachment |
vk::ImageUsageFlagBits::eTransferSrc, vk::ImageUsageFlagBits::eTransferSrc,
@ -80,54 +76,50 @@ int main( int /*argc*/, char ** /*argv*/ )
graphicsAndPresentQueueFamilyIndex.first, graphicsAndPresentQueueFamilyIndex.first,
graphicsAndPresentQueueFamilyIndex.second ); graphicsAndPresentQueueFamilyIndex.second );
vk::raii::su::DepthBufferData depthBufferData( vk::raii::su::DepthBufferData depthBufferData( physicalDevice, device, vk::Format::eD16Unorm, surfaceData.extent );
*physicalDevice, *device, vk::Format::eD16Unorm, surfaceData.extent );
vk::raii::su::BufferData uniformBufferData( vk::raii::su::BufferData uniformBufferData(
*physicalDevice, *device, sizeof( glm::mat4x4 ), vk::BufferUsageFlagBits::eUniformBuffer ); physicalDevice, device, sizeof( glm::mat4x4 ), vk::BufferUsageFlagBits::eUniformBuffer );
glm::mat4x4 mvpcMatrix = vk::su::createModelViewProjectionClipMatrix( surfaceData.extent ); glm::mat4x4 mvpcMatrix = vk::su::createModelViewProjectionClipMatrix( surfaceData.extent );
vk::raii::su::copyToDevice( *uniformBufferData.deviceMemory, mvpcMatrix ); vk::raii::su::copyToDevice( uniformBufferData.deviceMemory, mvpcMatrix );
vk::Format colorFormat = vk::Format colorFormat =
vk::su::pickSurfaceFormat( physicalDevice->getSurfaceFormatsKHR( **surfaceData.surface ) ).format; vk::su::pickSurfaceFormat( physicalDevice.getSurfaceFormatsKHR( **surfaceData.pSurface ) ).format;
std::unique_ptr<vk::raii::RenderPass> renderPass = vk::raii::RenderPass renderPass = vk::raii::su::makeRenderPass( device, colorFormat, depthBufferData.format );
vk::raii::su::makeUniqueRenderPass( *device, colorFormat, depthBufferData.format );
glslang::InitializeProcess(); glslang::InitializeProcess();
std::unique_ptr<vk::raii::ShaderModule> vertexShaderModule = vk::raii::ShaderModule vertexShaderModule =
vk::raii::su::makeUniqueShaderModule( *device, vk::ShaderStageFlagBits::eVertex, vertexShaderText_PT_T ); vk::raii::su::makeShaderModule( device, vk::ShaderStageFlagBits::eVertex, vertexShaderText_PT_T );
std::unique_ptr<vk::raii::ShaderModule> fragmentShaderModule = vk::raii::ShaderModule fragmentShaderModule =
vk::raii::su::makeUniqueShaderModule( *device, vk::ShaderStageFlagBits::eFragment, fragmentShaderText_T_C ); vk::raii::su::makeShaderModule( device, vk::ShaderStageFlagBits::eFragment, fragmentShaderText_T_C );
glslang::FinalizeProcess(); glslang::FinalizeProcess();
std::vector<std::unique_ptr<vk::raii::Framebuffer>> framebuffers = vk::raii::su::makeUniqueFramebuffers( std::vector<vk::raii::Framebuffer> framebuffers = vk::raii::su::makeFramebuffers(
*device, *renderPass, swapChainData.imageViews, depthBufferData.imageView, surfaceData.extent ); device, renderPass, swapChainData.imageViews, &*depthBufferData.pImageView, surfaceData.extent );
vk::raii::su::BufferData vertexBufferData( vk::raii::su::BufferData vertexBufferData(
*physicalDevice, *device, sizeof( texturedCubeData ), vk::BufferUsageFlagBits::eVertexBuffer ); physicalDevice, device, sizeof( texturedCubeData ), vk::BufferUsageFlagBits::eVertexBuffer );
vk::raii::su::copyToDevice( vk::raii::su::copyToDevice(
*vertexBufferData.deviceMemory, texturedCubeData, sizeof( texturedCubeData ) / sizeof( texturedCubeData[0] ) ); vertexBufferData.deviceMemory, texturedCubeData, sizeof( texturedCubeData ) / sizeof( texturedCubeData[0] ) );
/* VULKAN_KEY_START */ /* VULKAN_KEY_START */
vk::raii::su::TextureData textureData( *physicalDevice, *device ); vk::raii::su::TextureData textureData( physicalDevice, device );
commandBuffer->begin( vk::CommandBufferBeginInfo() ); commandBuffer.begin( vk::CommandBufferBeginInfo() );
textureData.setImage( *commandBuffer, vk::su::CheckerboardImageGenerator() ); textureData.setImage( commandBuffer, vk::su::CheckerboardImageGenerator() );
std::array<vk::DescriptorSetLayoutBinding, 2> bindings = { std::array<vk::DescriptorSetLayoutBinding, 2> bindings = {
vk::DescriptorSetLayoutBinding( 0, vk::DescriptorType::eUniformBuffer, 1, vk::ShaderStageFlagBits::eVertex ), vk::DescriptorSetLayoutBinding( 0, vk::DescriptorType::eUniformBuffer, 1, vk::ShaderStageFlagBits::eVertex ),
vk::DescriptorSetLayoutBinding( vk::DescriptorSetLayoutBinding(
1, vk::DescriptorType::eCombinedImageSampler, vk::ShaderStageFlagBits::eFragment, **textureData.sampler ) 1, vk::DescriptorType::eCombinedImageSampler, vk::ShaderStageFlagBits::eFragment, *textureData.sampler )
}; };
vk::DescriptorSetLayoutCreateInfo descriptorSetLayoutCreateInfo( {}, bindings ); vk::DescriptorSetLayoutCreateInfo descriptorSetLayoutCreateInfo( {}, bindings );
std::unique_ptr<vk::raii::DescriptorSetLayout> descriptorSetLayout = vk::raii::DescriptorSetLayout descriptorSetLayout( device, descriptorSetLayoutCreateInfo );
vk::raii::su::make_unique<vk::raii::DescriptorSetLayout>( *device, descriptorSetLayoutCreateInfo );
// Create pipeline layout // Create pipeline layout
vk::PipelineLayoutCreateInfo pipelineLayoutCreateInfo( {}, **descriptorSetLayout ); vk::PipelineLayoutCreateInfo pipelineLayoutCreateInfo( {}, *descriptorSetLayout );
std::unique_ptr<vk::raii::PipelineLayout> pipelineLayout = vk::raii::PipelineLayout pipelineLayout( device, pipelineLayoutCreateInfo );
vk::raii::su::make_unique<vk::raii::PipelineLayout>( *device, pipelineLayoutCreateInfo );
// Create a single pool to contain data for our descriptor set // Create a single pool to contain data for our descriptor set
std::array<vk::DescriptorPoolSize, 2> poolSizes = { vk::DescriptorPoolSize( vk::DescriptorType::eUniformBuffer, 1 ), std::array<vk::DescriptorPoolSize, 2> poolSizes = { vk::DescriptorPoolSize( vk::DescriptorType::eUniformBuffer, 1 ),
@ -135,47 +127,44 @@ int main( int /*argc*/, char ** /*argv*/ )
vk::DescriptorType::eCombinedImageSampler, 1 ) }; vk::DescriptorType::eCombinedImageSampler, 1 ) };
vk::DescriptorPoolCreateInfo descriptorPoolCreateInfo( vk::DescriptorPoolCreateInfo descriptorPoolCreateInfo(
vk::DescriptorPoolCreateFlagBits::eFreeDescriptorSet, 1, poolSizes ); vk::DescriptorPoolCreateFlagBits::eFreeDescriptorSet, 1, poolSizes );
std::unique_ptr<vk::raii::DescriptorPool> descriptorPool = vk::raii::DescriptorPool descriptorPool( device, descriptorPoolCreateInfo );
vk::raii::su::make_unique<vk::raii::DescriptorPool>( *device, descriptorPoolCreateInfo );
// Populate descriptor sets // Populate descriptor sets
vk::DescriptorSetAllocateInfo descriptorSetAllocateInfo( **descriptorPool, **descriptorSetLayout ); vk::DescriptorSetAllocateInfo descriptorSetAllocateInfo( *descriptorPool, *descriptorSetLayout );
std::unique_ptr<vk::raii::DescriptorSet> descriptorSet = vk::raii::su::make_unique<vk::raii::DescriptorSet>( vk::raii::DescriptorSet descriptorSet =
std::move( vk::raii::DescriptorSets( *device, descriptorSetAllocateInfo ).front() ) ); std::move( vk::raii::DescriptorSets( device, descriptorSetAllocateInfo ).front() );
vk::DescriptorBufferInfo bufferInfo( **uniformBufferData.buffer, 0, sizeof( glm::mat4x4 ) ); vk::DescriptorBufferInfo bufferInfo( *uniformBufferData.buffer, 0, sizeof( glm::mat4x4 ) );
vk::DescriptorImageInfo imageInfo( vk::DescriptorImageInfo imageInfo(
**textureData.sampler, **textureData.imageData->imageView, vk::ImageLayout::eShaderReadOnlyOptimal ); *textureData.sampler, **textureData.pImageData->pImageView, vk::ImageLayout::eShaderReadOnlyOptimal );
std::array<vk::WriteDescriptorSet, 2> writeDescriptorSets = { std::array<vk::WriteDescriptorSet, 2> writeDescriptorSets = {
vk::WriteDescriptorSet( **descriptorSet, 0, 0, vk::DescriptorType::eUniformBuffer, {}, bufferInfo ), vk::WriteDescriptorSet( *descriptorSet, 0, 0, vk::DescriptorType::eUniformBuffer, {}, bufferInfo ),
vk::WriteDescriptorSet( **descriptorSet, 1, 0, vk::DescriptorType::eCombinedImageSampler, imageInfo ) vk::WriteDescriptorSet( *descriptorSet, 1, 0, vk::DescriptorType::eCombinedImageSampler, imageInfo )
}; };
device->updateDescriptorSets( writeDescriptorSets, nullptr ); device.updateDescriptorSets( writeDescriptorSets, nullptr );
/* VULKAN_KEY_END */ /* VULKAN_KEY_END */
std::unique_ptr<vk::raii::PipelineCache> pipelineCache = vk::raii::PipelineCache pipelineCache( device, vk::PipelineCacheCreateInfo() );
vk::raii::su::make_unique<vk::raii::PipelineCache>( *device, vk::PipelineCacheCreateInfo() ); vk::raii::Pipeline graphicsPipeline =
std::unique_ptr<vk::raii::Pipeline> graphicsPipeline = vk::raii::su::makeUniqueGraphicsPipeline( vk::raii::su::makeGraphicsPipeline( device,
*device, pipelineCache,
*pipelineCache, vertexShaderModule,
*vertexShaderModule, nullptr,
nullptr, fragmentShaderModule,
*fragmentShaderModule, nullptr,
nullptr, sizeof( texturedCubeData[0] ),
sizeof( texturedCubeData[0] ), { { vk::Format::eR32G32B32A32Sfloat, 0 }, { vk::Format::eR32G32Sfloat, 16 } },
{ { vk::Format::eR32G32B32A32Sfloat, 0 }, { vk::Format::eR32G32Sfloat, 16 } }, vk::FrontFace::eClockwise,
vk::FrontFace::eClockwise, true,
true, pipelineLayout,
*pipelineLayout, renderPass );
*renderPass );
std::unique_ptr<vk::raii::Semaphore> imageAcquiredSemaphore = vk::raii::Semaphore imageAcquiredSemaphore( device, vk::SemaphoreCreateInfo() );
vk::raii::su::make_unique<vk::raii::Semaphore>( *device, vk::SemaphoreCreateInfo() ); vk::Result result;
vk::Result result; uint32_t imageIndex;
uint32_t imageIndex;
std::tie( result, imageIndex ) = std::tie( result, imageIndex ) =
swapChainData.swapChain->acquireNextImage( vk::su::FenceTimeout, **imageAcquiredSemaphore ); swapChainData.pSwapChain->acquireNextImage( vk::su::FenceTimeout, *imageAcquiredSemaphore );
assert( result == vk::Result::eSuccess ); assert( result == vk::Result::eSuccess );
assert( imageIndex < swapChainData.images.size() ); assert( imageIndex < swapChainData.images.size() );
@ -183,30 +172,30 @@ int main( int /*argc*/, char ** /*argv*/ )
clearValues[0].color = vk::ClearColorValue( std::array<float, 4>( { { 0.2f, 0.2f, 0.2f, 0.2f } } ) ); clearValues[0].color = vk::ClearColorValue( std::array<float, 4>( { { 0.2f, 0.2f, 0.2f, 0.2f } } ) );
clearValues[1].depthStencil = vk::ClearDepthStencilValue( 1.0f, 0 ); clearValues[1].depthStencil = vk::ClearDepthStencilValue( 1.0f, 0 );
vk::RenderPassBeginInfo renderPassBeginInfo( vk::RenderPassBeginInfo renderPassBeginInfo(
**renderPass, **framebuffers[imageIndex], vk::Rect2D( vk::Offset2D( 0, 0 ), surfaceData.extent ), clearValues ); *renderPass, *framebuffers[imageIndex], vk::Rect2D( vk::Offset2D( 0, 0 ), surfaceData.extent ), clearValues );
commandBuffer->beginRenderPass( renderPassBeginInfo, vk::SubpassContents::eInline ); commandBuffer.beginRenderPass( renderPassBeginInfo, vk::SubpassContents::eInline );
commandBuffer->bindPipeline( vk::PipelineBindPoint::eGraphics, **graphicsPipeline ); commandBuffer.bindPipeline( vk::PipelineBindPoint::eGraphics, *graphicsPipeline );
commandBuffer->bindDescriptorSets( commandBuffer.bindDescriptorSets(
vk::PipelineBindPoint::eGraphics, **pipelineLayout, 0, { **descriptorSet }, nullptr ); vk::PipelineBindPoint::eGraphics, *pipelineLayout, 0, { *descriptorSet }, nullptr );
commandBuffer->bindVertexBuffers( 0, { **vertexBufferData.buffer }, { 0 } ); commandBuffer.bindVertexBuffers( 0, { *vertexBufferData.buffer }, { 0 } );
commandBuffer->setViewport( 0, commandBuffer.setViewport( 0,
vk::Viewport( 0.0f, vk::Viewport( 0.0f,
0.0f, 0.0f,
static_cast<float>( surfaceData.extent.width ), static_cast<float>( surfaceData.extent.width ),
static_cast<float>( surfaceData.extent.height ), static_cast<float>( surfaceData.extent.height ),
0.0f, 0.0f,
1.0f ) ); 1.0f ) );
commandBuffer->setScissor( 0, vk::Rect2D( vk::Offset2D( 0, 0 ), surfaceData.extent ) ); commandBuffer.setScissor( 0, vk::Rect2D( vk::Offset2D( 0, 0 ), surfaceData.extent ) );
commandBuffer->draw( 12 * 3, 1, 0, 0 ); commandBuffer.draw( 12 * 3, 1, 0, 0 );
commandBuffer->endRenderPass(); commandBuffer.endRenderPass();
commandBuffer->end(); commandBuffer.end();
vk::raii::su::submitAndWait( *device, *graphicsQueue, *commandBuffer ); vk::raii::su::submitAndWait( device, graphicsQueue, commandBuffer );
vk::PresentInfoKHR presentInfoKHR( nullptr, **swapChainData.swapChain, imageIndex ); vk::PresentInfoKHR presentInfoKHR( nullptr, **swapChainData.pSwapChain, imageIndex );
result = presentQueue->presentKHR( presentInfoKHR ); result = presentQueue.presentKHR( presentInfoKHR );
switch ( result ) switch ( result )
{ {
case vk::Result::eSuccess: break; case vk::Result::eSuccess: break;
@ -217,7 +206,7 @@ int main( int /*argc*/, char ** /*argv*/ )
} }
std::this_thread::sleep_for( std::chrono::milliseconds( 1000 ) ); std::this_thread::sleep_for( std::chrono::milliseconds( 1000 ) );
device->waitIdle(); device.waitIdle();
} }
catch ( vk::SystemError & err ) catch ( vk::SystemError & err )
{ {

View File

@ -41,36 +41,32 @@ int main( int /*argc*/, char ** /*argv*/ )
{ {
try try
{ {
std::unique_ptr<vk::raii::Context> context = vk::raii::su::make_unique<vk::raii::Context>(); vk::raii::Context context;
std::unique_ptr<vk::raii::Instance> instance = vk::raii::Instance instance =
vk::raii::su::makeUniqueInstance( *context, AppName, EngineName, {}, vk::su::getInstanceExtensions() ); vk::raii::su::makeInstance( context, AppName, EngineName, {}, vk::su::getInstanceExtensions() );
#if !defined( NDEBUG ) #if !defined( NDEBUG )
std::unique_ptr<vk::raii::DebugUtilsMessengerEXT> debugUtilsMessenger = vk::raii::DebugUtilsMessengerEXT debugUtilsMessenger( instance, vk::su::makeDebugUtilsMessengerCreateInfoEXT() );
vk::raii::su::makeUniqueDebugUtilsMessengerEXT( *instance );
#endif #endif
std::unique_ptr<vk::raii::PhysicalDevice> physicalDevice = vk::raii::su::makeUniquePhysicalDevice( *instance ); vk::raii::PhysicalDevice physicalDevice = std::move( vk::raii::PhysicalDevices( instance ).front() );
vk::raii::su::SurfaceData surfaceData( *instance, AppName, vk::Extent2D( 50, 50 ) ); vk::raii::su::SurfaceData surfaceData( instance, AppName, vk::Extent2D( 50, 50 ) );
std::pair<uint32_t, uint32_t> graphicsAndPresentQueueFamilyIndex = std::pair<uint32_t, uint32_t> graphicsAndPresentQueueFamilyIndex =
vk::raii::su::findGraphicsAndPresentQueueFamilyIndex( *physicalDevice, *surfaceData.surface ); vk::raii::su::findGraphicsAndPresentQueueFamilyIndex( physicalDevice, *surfaceData.pSurface );
std::unique_ptr<vk::raii::Device> device = vk::raii::su::makeUniqueDevice( vk::raii::Device device = vk::raii::su::makeDevice(
*physicalDevice, graphicsAndPresentQueueFamilyIndex.first, vk::su::getDeviceExtensions() ); physicalDevice, graphicsAndPresentQueueFamilyIndex.first, vk::su::getDeviceExtensions() );
std::unique_ptr<vk::raii::CommandPool> commandPool = vk::raii::CommandPool commandPool = vk::raii::CommandPool(
vk::raii::su::makeUniqueCommandPool( *device, graphicsAndPresentQueueFamilyIndex.first ); device, { vk::CommandPoolCreateFlagBits::eResetCommandBuffer, graphicsAndPresentQueueFamilyIndex.first } );
std::unique_ptr<vk::raii::CommandBuffer> commandBuffer = vk::raii::CommandBuffer commandBuffer = vk::raii::su::makeCommandBuffer( device, commandPool );
vk::raii::su::makeUniqueCommandBuffer( *device, *commandPool );
std::unique_ptr<vk::raii::Queue> graphicsQueue = vk::raii::Queue graphicsQueue( device, graphicsAndPresentQueueFamilyIndex.first, 0 );
vk::raii::su::make_unique<vk::raii::Queue>( *device, graphicsAndPresentQueueFamilyIndex.first, 0 ); vk::raii::Queue presentQueue( device, graphicsAndPresentQueueFamilyIndex.second, 0 );
std::unique_ptr<vk::raii::Queue> presentQueue =
vk::raii::su::make_unique<vk::raii::Queue>( *device, graphicsAndPresentQueueFamilyIndex.second, 0 );
/* VULKAN_KEY_START */ /* VULKAN_KEY_START */
vk::Format format = vk::Format::eR8G8B8A8Unorm; vk::Format format = vk::Format::eR8G8B8A8Unorm;
vk::FormatProperties formatProperties = physicalDevice->getFormatProperties( format ); vk::FormatProperties formatProperties = physicalDevice.getFormatProperties( format );
// See if we can use a linear tiled image for a texture, if not, we will need a staging buffer for the texture data // See if we can use a linear tiled image for a texture, if not, we will need a staging buffer for the texture data
bool needsStaging = !( formatProperties.linearTilingFeatures & vk::FormatFeatureFlagBits::eSampledImage ); bool needsStaging = !( formatProperties.linearTilingFeatures & vk::FormatFeatureFlagBits::eSampledImage );
@ -89,53 +85,52 @@ int main( int /*argc*/, char ** /*argv*/ )
vk::SharingMode::eExclusive, vk::SharingMode::eExclusive,
{}, {},
needsStaging ? vk::ImageLayout::eUndefined : vk::ImageLayout::ePreinitialized ); needsStaging ? vk::ImageLayout::eUndefined : vk::ImageLayout::ePreinitialized );
std::unique_ptr<vk::raii::Image> image = vk::raii::su::make_unique<vk::raii::Image>( *device, imageCreateInfo ); vk::raii::Image image( device, imageCreateInfo );
vk::MemoryRequirements memoryRequirements = image->getMemoryRequirements(); vk::MemoryRequirements memoryRequirements = image.getMemoryRequirements();
uint32_t memoryTypeIndex = vk::su::findMemoryType( uint32_t memoryTypeIndex = vk::su::findMemoryType(
physicalDevice->getMemoryProperties(), physicalDevice.getMemoryProperties(),
memoryRequirements.memoryTypeBits, memoryRequirements.memoryTypeBits,
needsStaging ? vk::MemoryPropertyFlags() needsStaging ? vk::MemoryPropertyFlags()
: ( vk::MemoryPropertyFlagBits::eHostVisible | vk::MemoryPropertyFlagBits::eHostCoherent ) ); : ( vk::MemoryPropertyFlagBits::eHostVisible | vk::MemoryPropertyFlagBits::eHostCoherent ) );
// allocate memory // allocate memory
vk::MemoryAllocateInfo memoryAllocateInfo( memoryRequirements.size, memoryTypeIndex ); vk::MemoryAllocateInfo memoryAllocateInfo( memoryRequirements.size, memoryTypeIndex );
std::unique_ptr<vk::raii::DeviceMemory> imageMemory = vk::raii::DeviceMemory imageMemory( device, memoryAllocateInfo );
vk::raii::su::make_unique<vk::raii::DeviceMemory>( *device, memoryAllocateInfo );
// bind memory // bind memory
image->bindMemory( **imageMemory, 0 ); image.bindMemory( *imageMemory, 0 );
std::unique_ptr<vk::raii::Buffer> textureBuffer; std::unique_ptr<vk::raii::Buffer> pTextureBuffer;
std::unique_ptr<vk::raii::DeviceMemory> textureBufferMemory; std::unique_ptr<vk::raii::DeviceMemory> pTextureBufferMemory;
if ( needsStaging ) if ( needsStaging )
{ {
// Need a staging buffer to map and copy texture into // Need a staging buffer to map and copy texture into
vk::BufferCreateInfo bufferCreateInfo( vk::BufferCreateInfo bufferCreateInfo(
{}, surfaceData.extent.width * surfaceData.extent.height * 4, vk::BufferUsageFlagBits::eTransferSrc ); {}, surfaceData.extent.width * surfaceData.extent.height * 4, vk::BufferUsageFlagBits::eTransferSrc );
textureBuffer = vk::raii::su::make_unique<vk::raii::Buffer>( *device, bufferCreateInfo ); pTextureBuffer = vk::raii::su::make_unique<vk::raii::Buffer>( device, bufferCreateInfo );
memoryRequirements = textureBuffer->getMemoryRequirements(); memoryRequirements = pTextureBuffer->getMemoryRequirements();
memoryTypeIndex = memoryTypeIndex =
vk::su::findMemoryType( physicalDevice->getMemoryProperties(), vk::su::findMemoryType( physicalDevice.getMemoryProperties(),
memoryRequirements.memoryTypeBits, memoryRequirements.memoryTypeBits,
vk::MemoryPropertyFlagBits::eHostVisible | vk::MemoryPropertyFlagBits::eHostCoherent ); vk::MemoryPropertyFlagBits::eHostVisible | vk::MemoryPropertyFlagBits::eHostCoherent );
// allocate memory // allocate memory
memoryAllocateInfo = vk::MemoryAllocateInfo( memoryRequirements.size, memoryTypeIndex ); memoryAllocateInfo = vk::MemoryAllocateInfo( memoryRequirements.size, memoryTypeIndex );
textureBufferMemory = vk::raii::su::make_unique<vk::raii::DeviceMemory>( *device, memoryAllocateInfo ); pTextureBufferMemory = vk::raii::su::make_unique<vk::raii::DeviceMemory>( device, memoryAllocateInfo );
// bind memory // bind memory
textureBuffer->bindMemory( **textureBufferMemory, 0 ); pTextureBuffer->bindMemory( **pTextureBufferMemory, 0 );
} }
else else
{ {
vk::SubresourceLayout subresourceLayout = vk::SubresourceLayout subresourceLayout =
image->getSubresourceLayout( vk::ImageSubresource( vk::ImageAspectFlagBits::eColor ) ); image.getSubresourceLayout( vk::ImageSubresource( vk::ImageAspectFlagBits::eColor ) );
} }
void * data = needsStaging ? textureBufferMemory->mapMemory( 0, memoryRequirements.size, vk::MemoryMapFlags() ) void * data = needsStaging ? pTextureBufferMemory->mapMemory( 0, memoryRequirements.size, vk::MemoryMapFlags() )
: imageMemory->mapMemory( 0, memoryRequirements.size, vk::MemoryMapFlags() ); : imageMemory.mapMemory( 0, memoryRequirements.size, vk::MemoryMapFlags() );
// Checkerboard of 16x16 pixel squares // Checkerboard of 16x16 pixel squares
unsigned char * pImageMemory = static_cast<unsigned char *>( data ); unsigned char * pImageMemory = static_cast<unsigned char *>( data );
@ -152,39 +147,36 @@ int main( int /*argc*/, char ** /*argv*/ )
} }
} }
needsStaging ? textureBufferMemory->unmapMemory() : imageMemory->unmapMemory(); needsStaging ? pTextureBufferMemory->unmapMemory() : imageMemory.unmapMemory();
commandBuffer->begin( vk::CommandBufferBeginInfo() ); commandBuffer.begin( vk::CommandBufferBeginInfo() );
if ( needsStaging ) if ( needsStaging )
{ {
// Since we're going to blit to the texture image, set its layout to eTransferDstOptimal // Since we're going to blit to the texture image, set its layout to eTransferDstOptimal
vk::raii::su::setImageLayout( vk::raii::su::setImageLayout(
*commandBuffer, **image, format, vk::ImageLayout::eUndefined, vk::ImageLayout::eTransferDstOptimal ); commandBuffer, *image, format, vk::ImageLayout::eUndefined, vk::ImageLayout::eTransferDstOptimal );
vk::BufferImageCopy copyRegion( 0, vk::BufferImageCopy copyRegion( 0,
surfaceData.extent.width, surfaceData.extent.width,
surfaceData.extent.height, surfaceData.extent.height,
vk::ImageSubresourceLayers( vk::ImageAspectFlagBits::eColor, 0, 0, 1 ), vk::ImageSubresourceLayers( vk::ImageAspectFlagBits::eColor, 0, 0, 1 ),
vk::Offset3D( 0, 0, 0 ), vk::Offset3D( 0, 0, 0 ),
vk::Extent3D( surfaceData.extent, 1 ) ); vk::Extent3D( surfaceData.extent, 1 ) );
commandBuffer->copyBufferToImage( **textureBuffer, **image, vk::ImageLayout::eTransferDstOptimal, copyRegion ); commandBuffer.copyBufferToImage( **pTextureBuffer, *image, vk::ImageLayout::eTransferDstOptimal, copyRegion );
// Set the layout for the texture image from eTransferDstOptimal to SHADER_READ_ONLY // Set the layout for the texture image from eTransferDstOptimal to SHADER_READ_ONLY
vk::raii::su::setImageLayout( *commandBuffer, vk::raii::su::setImageLayout(
**image, commandBuffer, *image, format, vk::ImageLayout::eTransferDstOptimal, vk::ImageLayout::eShaderReadOnlyOptimal );
format,
vk::ImageLayout::eTransferDstOptimal,
vk::ImageLayout::eShaderReadOnlyOptimal );
} }
else else
{ {
// If we can use the linear tiled image as a texture, just do it // If we can use the linear tiled image as a texture, just do it
vk::raii::su::setImageLayout( vk::raii::su::setImageLayout(
*commandBuffer, **image, format, vk::ImageLayout::ePreinitialized, vk::ImageLayout::eShaderReadOnlyOptimal ); commandBuffer, *image, format, vk::ImageLayout::ePreinitialized, vk::ImageLayout::eShaderReadOnlyOptimal );
} }
commandBuffer->end(); commandBuffer.end();
vk::raii::su::submitAndWait( *device, *graphicsQueue, *commandBuffer ); vk::raii::su::submitAndWait( device, graphicsQueue, commandBuffer );
vk::SamplerCreateInfo samplerCreateInfo( {}, vk::SamplerCreateInfo samplerCreateInfo( {},
vk::Filter::eNearest, vk::Filter::eNearest,
vk::Filter::eNearest, vk::Filter::eNearest,
vk::SamplerMipmapMode::eNearest, vk::SamplerMipmapMode::eNearest,
@ -199,15 +191,14 @@ int main( int /*argc*/, char ** /*argv*/ )
0.0f, 0.0f,
0.0f, 0.0f,
vk::BorderColor::eFloatOpaqueWhite ); vk::BorderColor::eFloatOpaqueWhite );
std::unique_ptr<vk::raii::Sampler> sampler = vk::raii::su::make_unique<vk::raii::Sampler>( *device, samplerCreateInfo ); vk::raii::Sampler sampler( device, samplerCreateInfo );
vk::ComponentMapping componentMapping( vk::ComponentMapping componentMapping(
vk::ComponentSwizzle::eR, vk::ComponentSwizzle::eG, vk::ComponentSwizzle::eB, vk::ComponentSwizzle::eA ); vk::ComponentSwizzle::eR, vk::ComponentSwizzle::eG, vk::ComponentSwizzle::eB, vk::ComponentSwizzle::eA );
vk::ImageSubresourceRange imageSubresourceRange( vk::ImageAspectFlagBits::eColor, 0, 1, 0, 1 ); vk::ImageSubresourceRange imageSubresourceRange( vk::ImageAspectFlagBits::eColor, 0, 1, 0, 1 );
vk::ImageViewCreateInfo imageViewCreateInfo( vk::ImageViewCreateInfo imageViewCreateInfo(
{}, **image, vk::ImageViewType::e2D, format, componentMapping, imageSubresourceRange ); {}, *image, vk::ImageViewType::e2D, format, componentMapping, imageSubresourceRange );
std::unique_ptr<vk::raii::ImageView> imageView = vk::raii::ImageView imageView( device, imageViewCreateInfo );
vk::raii::su::make_unique<vk::raii::ImageView>( *device, imageViewCreateInfo );
/* VULKAN_KEY_END */ /* VULKAN_KEY_END */
} }

View File

@ -70,42 +70,38 @@ int main( int /*argc*/, char ** /*argv*/ )
{ {
try try
{ {
std::unique_ptr<vk::raii::Context> context = vk::raii::su::make_unique<vk::raii::Context>(); vk::raii::Context context;
std::unique_ptr<vk::raii::Instance> instance = vk::raii::Instance instance =
vk::raii::su::makeUniqueInstance( *context, AppName, EngineName, {}, vk::su::getInstanceExtensions() ); vk::raii::su::makeInstance( context, AppName, EngineName, {}, vk::su::getInstanceExtensions() );
#if !defined( NDEBUG ) #if !defined( NDEBUG )
std::unique_ptr<vk::raii::DebugUtilsMessengerEXT> debugUtilsMessenger = vk::raii::DebugUtilsMessengerEXT debugUtilsMessenger( instance, vk::su::makeDebugUtilsMessengerCreateInfoEXT() );
vk::raii::su::makeUniqueDebugUtilsMessengerEXT( *instance );
#endif #endif
std::unique_ptr<vk::raii::PhysicalDevice> physicalDevice = vk::raii::su::makeUniquePhysicalDevice( *instance ); vk::raii::PhysicalDevice physicalDevice = std::move( vk::raii::PhysicalDevices( instance ).front() );
vk::FormatProperties formatProperties = physicalDevice->getFormatProperties( vk::Format::eR8G8B8A8Unorm ); vk::FormatProperties formatProperties = physicalDevice.getFormatProperties( vk::Format::eR8G8B8A8Unorm );
if ( !( formatProperties.optimalTilingFeatures & vk::FormatFeatureFlagBits::eColorAttachment ) ) if ( !( formatProperties.optimalTilingFeatures & vk::FormatFeatureFlagBits::eColorAttachment ) )
{ {
std::cout << "vk::Format::eR8G8B8A8Unorm format unsupported for input attachment\n"; std::cout << "vk::Format::eR8G8B8A8Unorm format unsupported for input attachment\n";
exit( -1 ); exit( -1 );
} }
vk::raii::su::SurfaceData surfaceData( *instance, AppName, vk::Extent2D( 500, 500 ) ); vk::raii::su::SurfaceData surfaceData( instance, AppName, vk::Extent2D( 500, 500 ) );
std::pair<uint32_t, uint32_t> graphicsAndPresentQueueFamilyIndex = std::pair<uint32_t, uint32_t> graphicsAndPresentQueueFamilyIndex =
vk::raii::su::findGraphicsAndPresentQueueFamilyIndex( *physicalDevice, *surfaceData.surface ); vk::raii::su::findGraphicsAndPresentQueueFamilyIndex( physicalDevice, *surfaceData.pSurface );
std::unique_ptr<vk::raii::Device> device = vk::raii::su::makeUniqueDevice( vk::raii::Device device = vk::raii::su::makeDevice(
*physicalDevice, graphicsAndPresentQueueFamilyIndex.first, vk::su::getDeviceExtensions() ); physicalDevice, graphicsAndPresentQueueFamilyIndex.first, vk::su::getDeviceExtensions() );
std::unique_ptr<vk::raii::CommandPool> commandPool = vk::raii::CommandPool commandPool = vk::raii::CommandPool(
vk::raii::su::makeUniqueCommandPool( *device, graphicsAndPresentQueueFamilyIndex.first ); device, { vk::CommandPoolCreateFlagBits::eResetCommandBuffer, graphicsAndPresentQueueFamilyIndex.first } );
std::unique_ptr<vk::raii::CommandBuffer> commandBuffer = vk::raii::CommandBuffer commandBuffer = vk::raii::su::makeCommandBuffer( device, commandPool );
vk::raii::su::makeUniqueCommandBuffer( *device, *commandPool );
std::unique_ptr<vk::raii::Queue> graphicsQueue = vk::raii::Queue graphicsQueue( device, graphicsAndPresentQueueFamilyIndex.first, 0 );
vk::raii::su::make_unique<vk::raii::Queue>( *device, graphicsAndPresentQueueFamilyIndex.first, 0 ); vk::raii::Queue presentQueue( device, graphicsAndPresentQueueFamilyIndex.second, 0 );
std::unique_ptr<vk::raii::Queue> presentQueue =
vk::raii::su::make_unique<vk::raii::Queue>( *device, graphicsAndPresentQueueFamilyIndex.second, 0 );
vk::raii::su::SwapChainData swapChainData( *physicalDevice, vk::raii::su::SwapChainData swapChainData( physicalDevice,
*device, device,
*surfaceData.surface, *surfaceData.pSurface,
surfaceData.extent, surfaceData.extent,
vk::ImageUsageFlagBits::eColorAttachment | vk::ImageUsageFlagBits::eColorAttachment |
vk::ImageUsageFlagBits::eTransferSrc, vk::ImageUsageFlagBits::eTransferSrc,
@ -131,20 +127,19 @@ int main( int /*argc*/, char ** /*argv*/ )
vk::ImageTiling::eOptimal, vk::ImageTiling::eOptimal,
vk::ImageUsageFlagBits::eInputAttachment | vk::ImageUsageFlagBits::eInputAttachment |
vk::ImageUsageFlagBits::eTransferDst ); vk::ImageUsageFlagBits::eTransferDst );
std::unique_ptr<vk::raii::Image> inputImage = vk::raii::su::make_unique<vk::raii::Image>( *device, imageCreateInfo ); vk::raii::Image inputImage( device, imageCreateInfo );
vk::MemoryRequirements memoryRequirements = inputImage->getMemoryRequirements(); vk::MemoryRequirements memoryRequirements = inputImage.getMemoryRequirements();
uint32_t memoryTypeIndex = uint32_t memoryTypeIndex =
vk::su::findMemoryType( physicalDevice->getMemoryProperties(), memoryRequirements.memoryTypeBits, {} ); vk::su::findMemoryType( physicalDevice.getMemoryProperties(), memoryRequirements.memoryTypeBits, {} );
vk::MemoryAllocateInfo memoryAllocateInfo( memoryRequirements.size, memoryTypeIndex ); vk::MemoryAllocateInfo memoryAllocateInfo( memoryRequirements.size, memoryTypeIndex );
std::unique_ptr<vk::raii::DeviceMemory> inputMemory = vk::raii::DeviceMemory inputMemory( device, memoryAllocateInfo );
vk::raii::su::make_unique<vk::raii::DeviceMemory>( *device, memoryAllocateInfo ); inputImage.bindMemory( *inputMemory, 0 );
inputImage->bindMemory( **inputMemory, 0 );
// Set the image layout to TRANSFER_DST_OPTIMAL to be ready for clear // Set the image layout to TRANSFER_DST_OPTIMAL to be ready for clear
commandBuffer->begin( vk::CommandBufferBeginInfo() ); commandBuffer.begin( vk::CommandBufferBeginInfo() );
vk::raii::su::setImageLayout( *commandBuffer, vk::raii::su::setImageLayout( commandBuffer,
**inputImage, *inputImage,
swapChainData.colorFormat, swapChainData.colorFormat,
vk::ImageLayout::eUndefined, vk::ImageLayout::eUndefined,
vk::ImageLayout::eTransferDstOptimal ); vk::ImageLayout::eTransferDstOptimal );
@ -152,12 +147,12 @@ int main( int /*argc*/, char ** /*argv*/ )
vk::ClearColorValue clearColorValue( std::array<float, 4>( { { 1.0f, 1.0f, 0.0f, 0.0f } } ) ); vk::ClearColorValue clearColorValue( std::array<float, 4>( { { 1.0f, 1.0f, 0.0f, 0.0f } } ) );
vk::ImageSubresourceRange imageSubresourceRange( vk::ImageSubresourceRange imageSubresourceRange(
vk::ImageAspectFlagBits::eColor, 0, VK_REMAINING_MIP_LEVELS, 0, VK_REMAINING_ARRAY_LAYERS ); vk::ImageAspectFlagBits::eColor, 0, VK_REMAINING_MIP_LEVELS, 0, VK_REMAINING_ARRAY_LAYERS );
commandBuffer->clearColorImage( commandBuffer.clearColorImage(
**inputImage, vk::ImageLayout::eTransferDstOptimal, clearColorValue, imageSubresourceRange ); *inputImage, vk::ImageLayout::eTransferDstOptimal, clearColorValue, imageSubresourceRange );
// Set the image layout to SHADER_READONLY_OPTIMAL for use by the shaders // Set the image layout to SHADER_READONLY_OPTIMAL for use by the shaders
vk::raii::su::setImageLayout( *commandBuffer, vk::raii::su::setImageLayout( commandBuffer,
**inputImage, *inputImage,
swapChainData.colorFormat, swapChainData.colorFormat,
vk::ImageLayout::eTransferDstOptimal, vk::ImageLayout::eTransferDstOptimal,
vk::ImageLayout::eShaderReadOnlyOptimal ); vk::ImageLayout::eShaderReadOnlyOptimal );
@ -166,19 +161,16 @@ int main( int /*argc*/, char ** /*argv*/ )
vk::ComponentSwizzle::eR, vk::ComponentSwizzle::eG, vk::ComponentSwizzle::eB, vk::ComponentSwizzle::eA ); vk::ComponentSwizzle::eR, vk::ComponentSwizzle::eG, vk::ComponentSwizzle::eB, vk::ComponentSwizzle::eA );
imageSubresourceRange = vk::ImageSubresourceRange( vk::ImageAspectFlagBits::eColor, 0, 1, 0, 1 ); imageSubresourceRange = vk::ImageSubresourceRange( vk::ImageAspectFlagBits::eColor, 0, 1, 0, 1 );
vk::ImageViewCreateInfo imageViewCreateInfo( vk::ImageViewCreateInfo imageViewCreateInfo(
{}, **inputImage, vk::ImageViewType::e2D, swapChainData.colorFormat, componentMapping, imageSubresourceRange ); {}, *inputImage, vk::ImageViewType::e2D, swapChainData.colorFormat, componentMapping, imageSubresourceRange );
std::unique_ptr<vk::raii::ImageView> inputAttachmentView = vk::raii::ImageView inputAttachmentView( device, imageViewCreateInfo );
vk::raii::su::make_unique<vk::raii::ImageView>( *device, imageViewCreateInfo );
vk::DescriptorSetLayoutBinding layoutBinding( vk::DescriptorSetLayoutBinding layoutBinding(
0, vk::DescriptorType::eInputAttachment, 1, vk::ShaderStageFlagBits::eFragment ); 0, vk::DescriptorType::eInputAttachment, 1, vk::ShaderStageFlagBits::eFragment );
vk::DescriptorSetLayoutCreateInfo descriptorSetLayoutCreateInfo( {}, layoutBinding ); vk::DescriptorSetLayoutCreateInfo descriptorSetLayoutCreateInfo( {}, layoutBinding );
std::unique_ptr<vk::raii::DescriptorSetLayout> descriptorSetLayout = vk::raii::DescriptorSetLayout descriptorSetLayout( device, descriptorSetLayoutCreateInfo );
vk::raii::su::make_unique<vk::raii::DescriptorSetLayout>( *device, descriptorSetLayoutCreateInfo );
vk::PipelineLayoutCreateInfo pipelineLayoutCreateInfo( {}, **descriptorSetLayout ); vk::PipelineLayoutCreateInfo pipelineLayoutCreateInfo( {}, *descriptorSetLayout );
std::unique_ptr<vk::raii::PipelineLayout> pipelineLayout = vk::raii::PipelineLayout pipelineLayout( device, pipelineLayoutCreateInfo );
vk::raii::su::make_unique<vk::raii::PipelineLayout>( *device, pipelineLayoutCreateInfo );
std::array<vk::AttachmentDescription, 2> attachments = { std::array<vk::AttachmentDescription, 2> attachments = {
// First attachment is the color attachment - clear at the beginning of the renderpass and transition layout to // First attachment is the color attachment - clear at the beginning of the renderpass and transition layout to
@ -209,87 +201,82 @@ int main( int /*argc*/, char ** /*argv*/ )
vk::AttachmentReference inputReference( 1, vk::ImageLayout::eShaderReadOnlyOptimal ); vk::AttachmentReference inputReference( 1, vk::ImageLayout::eShaderReadOnlyOptimal );
vk::SubpassDescription subPass( {}, vk::PipelineBindPoint::eGraphics, inputReference, colorReference ); vk::SubpassDescription subPass( {}, vk::PipelineBindPoint::eGraphics, inputReference, colorReference );
vk::RenderPassCreateInfo renderPassCreateInfo( {}, attachments, subPass ); vk::RenderPassCreateInfo renderPassCreateInfo( {}, attachments, subPass );
std::unique_ptr<vk::raii::RenderPass> renderPass = vk::raii::RenderPass renderPass( device, renderPassCreateInfo );
vk::raii::su::make_unique<vk::raii::RenderPass>( *device, renderPassCreateInfo );
glslang::InitializeProcess(); glslang::InitializeProcess();
std::unique_ptr<vk::raii::ShaderModule> vertexShaderModule = vk::raii::ShaderModule vertexShaderModule =
vk::raii::su::makeUniqueShaderModule( *device, vk::ShaderStageFlagBits::eVertex, vertexShaderText ); vk::raii::su::makeShaderModule( device, vk::ShaderStageFlagBits::eVertex, vertexShaderText );
std::unique_ptr<vk::raii::ShaderModule> fragmentShaderModule = vk::raii::ShaderModule fragmentShaderModule =
vk::raii::su::makeUniqueShaderModule( *device, vk::ShaderStageFlagBits::eFragment, fragmentShaderText ); vk::raii::su::makeShaderModule( device, vk::ShaderStageFlagBits::eFragment, fragmentShaderText );
glslang::FinalizeProcess(); glslang::FinalizeProcess();
std::vector<std::unique_ptr<vk::raii::Framebuffer>> framebuffers = vk::raii::su::makeUniqueFramebuffers( std::vector<vk::raii::Framebuffer> framebuffers = vk::raii::su::makeFramebuffers(
*device, *renderPass, swapChainData.imageViews, inputAttachmentView, surfaceData.extent ); device, renderPass, swapChainData.imageViews, &inputAttachmentView, surfaceData.extent );
vk::DescriptorPoolSize poolSize( vk::DescriptorType::eInputAttachment, 1 ); vk::DescriptorPoolSize poolSize( vk::DescriptorType::eInputAttachment, 1 );
vk::DescriptorPoolCreateInfo descriptorPoolCreateInfo( vk::DescriptorPoolCreateInfo descriptorPoolCreateInfo(
vk::DescriptorPoolCreateFlagBits::eFreeDescriptorSet, 1, poolSize ); vk::DescriptorPoolCreateFlagBits::eFreeDescriptorSet, 1, poolSize );
std::unique_ptr<vk::raii::DescriptorPool> descriptorPool = vk::raii::DescriptorPool descriptorPool( device, descriptorPoolCreateInfo );
vk::raii::su::make_unique<vk::raii::DescriptorPool>( *device, descriptorPoolCreateInfo );
vk::DescriptorSetAllocateInfo descriptorSetAllocateInfo( **descriptorPool, **descriptorSetLayout ); vk::DescriptorSetAllocateInfo descriptorSetAllocateInfo( *descriptorPool, *descriptorSetLayout );
std::unique_ptr<vk::raii::DescriptorSet> descriptorSet = vk::raii::su::make_unique<vk::raii::DescriptorSet>( vk::raii::DescriptorSet descriptorSet = std::move( vk::raii::DescriptorSets( device, descriptorSetAllocateInfo ).front() );
std::move( vk::raii::DescriptorSets( *device, descriptorSetAllocateInfo ).front() ) );
vk::DescriptorImageInfo inputImageInfo( nullptr, **inputAttachmentView, vk::ImageLayout::eShaderReadOnlyOptimal ); vk::DescriptorImageInfo inputImageInfo( nullptr, *inputAttachmentView, vk::ImageLayout::eShaderReadOnlyOptimal );
vk::WriteDescriptorSet writeDescriptorSet( vk::WriteDescriptorSet writeDescriptorSet(
**descriptorSet, 0, 0, vk::DescriptorType::eInputAttachment, inputImageInfo ); *descriptorSet, 0, 0, vk::DescriptorType::eInputAttachment, inputImageInfo );
device->updateDescriptorSets( writeDescriptorSet, nullptr ); device.updateDescriptorSets( writeDescriptorSet, nullptr );
std::unique_ptr<vk::raii::PipelineCache> pipelineCache = vk::raii::PipelineCache pipelineCache( device, vk::PipelineCacheCreateInfo() );
vk::raii::su::make_unique<vk::raii::PipelineCache>( *device, vk::PipelineCacheCreateInfo() ); vk::raii::Pipeline graphicsPipeline =
std::unique_ptr<vk::raii::Pipeline> graphicsPipeline = vk::raii::su::makeGraphicsPipeline( device,
vk::raii::su::makeUniqueGraphicsPipeline( *device, pipelineCache,
*pipelineCache, vertexShaderModule,
*vertexShaderModule,
nullptr, nullptr,
*fragmentShaderModule, fragmentShaderModule,
nullptr, nullptr,
0, 0,
{}, {},
vk::FrontFace::eClockwise, vk::FrontFace::eClockwise,
false, false,
*pipelineLayout, pipelineLayout,
*renderPass ); renderPass );
std::unique_ptr<vk::raii::Semaphore> imageAcquiredSemaphore = vk::raii::Semaphore imageAcquiredSemaphore( device, vk::SemaphoreCreateInfo() );
vk::raii::su::make_unique<vk::raii::Semaphore>( *device, vk::SemaphoreCreateInfo() );
vk::Result result; vk::Result result;
uint32_t imageIndex; uint32_t imageIndex;
std::tie( result, imageIndex ) = std::tie( result, imageIndex ) =
swapChainData.swapChain->acquireNextImage( vk::su::FenceTimeout, **imageAcquiredSemaphore ); swapChainData.pSwapChain->acquireNextImage( vk::su::FenceTimeout, *imageAcquiredSemaphore );
assert( result == vk::Result::eSuccess ); assert( result == vk::Result::eSuccess );
assert( imageIndex < swapChainData.images.size() ); assert( imageIndex < swapChainData.images.size() );
vk::ClearValue clearValue; vk::ClearValue clearValue;
clearValue.color = vk::ClearColorValue( std::array<float, 4>( { { 0.2f, 0.2f, 0.2f, 0.2f } } ) ); clearValue.color = vk::ClearColorValue( std::array<float, 4>( { { 0.2f, 0.2f, 0.2f, 0.2f } } ) );
vk::RenderPassBeginInfo renderPassBeginInfo( vk::RenderPassBeginInfo renderPassBeginInfo(
**renderPass, **framebuffers[imageIndex], vk::Rect2D( vk::Offset2D( 0, 0 ), surfaceData.extent ), clearValue ); *renderPass, *framebuffers[imageIndex], vk::Rect2D( vk::Offset2D( 0, 0 ), surfaceData.extent ), clearValue );
commandBuffer->beginRenderPass( renderPassBeginInfo, vk::SubpassContents::eInline ); commandBuffer.beginRenderPass( renderPassBeginInfo, vk::SubpassContents::eInline );
commandBuffer->bindPipeline( vk::PipelineBindPoint::eGraphics, **graphicsPipeline ); commandBuffer.bindPipeline( vk::PipelineBindPoint::eGraphics, *graphicsPipeline );
commandBuffer->bindDescriptorSets( commandBuffer.bindDescriptorSets(
vk::PipelineBindPoint::eGraphics, **pipelineLayout, 0, { **descriptorSet }, nullptr ); vk::PipelineBindPoint::eGraphics, *pipelineLayout, 0, { *descriptorSet }, nullptr );
commandBuffer->setViewport( 0, commandBuffer.setViewport( 0,
vk::Viewport( 0.0f, vk::Viewport( 0.0f,
0.0f, 0.0f,
static_cast<float>( surfaceData.extent.width ), static_cast<float>( surfaceData.extent.width ),
static_cast<float>( surfaceData.extent.height ), static_cast<float>( surfaceData.extent.height ),
0.0f, 0.0f,
1.0f ) ); 1.0f ) );
commandBuffer->setScissor( 0, vk::Rect2D( vk::Offset2D( 0, 0 ), surfaceData.extent ) ); commandBuffer.setScissor( 0, vk::Rect2D( vk::Offset2D( 0, 0 ), surfaceData.extent ) );
commandBuffer->draw( 3, 1, 0, 0 ); commandBuffer.draw( 3, 1, 0, 0 );
commandBuffer->endRenderPass(); commandBuffer.endRenderPass();
commandBuffer->end(); commandBuffer.end();
/* VULKAN_KEY_END */ /* VULKAN_KEY_END */
vk::raii::su::submitAndWait( *device, *graphicsQueue, *commandBuffer ); vk::raii::su::submitAndWait( device, graphicsQueue, commandBuffer );
vk::PresentInfoKHR presentInfoKHR( nullptr, **swapChainData.swapChain, imageIndex ); vk::PresentInfoKHR presentInfoKHR( nullptr, **swapChainData.pSwapChain, imageIndex );
result = presentQueue->presentKHR( presentInfoKHR ); result = presentQueue.presentKHR( presentInfoKHR );
switch ( result ) switch ( result )
{ {
case vk::Result::eSuccess: break; case vk::Result::eSuccess: break;

View File

@ -25,19 +25,18 @@ int main( int /*argc*/, char ** /*argv*/ )
{ {
try try
{ {
std::unique_ptr<vk::raii::Context> context = vk::raii::su::make_unique<vk::raii::Context>(); vk::raii::Context context;
/* VULKAN_KEY_START */ /* VULKAN_KEY_START */
std::vector<vk::ExtensionProperties> extensionProperties = context->enumerateInstanceExtensionProperties(); std::vector<vk::ExtensionProperties> extensionProperties = context.enumerateInstanceExtensionProperties();
// sort the extensions alphabetically // sort the extensions alphabetically
std::sort( extensionProperties.begin(), std::sort( extensionProperties.begin(),
extensionProperties.end(), extensionProperties.end(),
[]( vk::ExtensionProperties const & a, vk::ExtensionProperties const & b ) { []( vk::ExtensionProperties const & a, vk::ExtensionProperties const & b )
return strcmp( a.extensionName, b.extensionName ) < 0; { return strcmp( a.extensionName, b.extensionName ) < 0; } );
} );
std::cout << "Instance Extensions:" << std::endl; std::cout << "Instance Extensions:" << std::endl;
for ( auto const & ep : extensionProperties ) for ( auto const & ep : extensionProperties )

View File

@ -36,8 +36,8 @@ int main( int /*argc*/, char ** /*argv*/ )
{ {
try try
{ {
std::unique_ptr<vk::raii::Context> context = vk::raii::su::make_unique<vk::raii::Context>(); vk::raii::Context context;
std::vector<vk::LayerProperties> layerProperties = context->enumerateInstanceLayerProperties(); std::vector<vk::LayerProperties> layerProperties = context.enumerateInstanceLayerProperties();
/* VULKAN_KEY_START */ /* VULKAN_KEY_START */
@ -47,7 +47,7 @@ int main( int /*argc*/, char ** /*argv*/ )
for ( auto const & layerProperty : layerProperties ) for ( auto const & layerProperty : layerProperties )
{ {
std::vector<vk::ExtensionProperties> extensionProperties = std::vector<vk::ExtensionProperties> extensionProperties =
context->enumerateInstanceExtensionProperties( vk::Optional<const std::string>( layerProperty.layerName ) ); context.enumerateInstanceExtensionProperties( vk::Optional<const std::string>( layerProperty.layerName ) );
propertyData.emplace_back( layerProperty, extensionProperties ); propertyData.emplace_back( layerProperty, extensionProperties );
} }

View File

@ -25,11 +25,11 @@ int main( int /*argc*/, char ** /*argv*/ )
{ {
try try
{ {
std::unique_ptr<vk::raii::Context> context = vk::raii::su::make_unique<vk::raii::Context>(); vk::raii::Context context;
/* VULKAN_KEY_START */ /* VULKAN_KEY_START */
std::vector<vk::LayerProperties> layerProperties = context->enumerateInstanceLayerProperties(); std::vector<vk::LayerProperties> layerProperties = context.enumerateInstanceLayerProperties();
std::cout << "Instance Layers:" << std::endl; std::cout << "Instance Layers:" << std::endl;
if ( layerProperties.empty() ) if ( layerProperties.empty() )

View File

@ -30,11 +30,11 @@ int main( int /*argc*/, char ** /*argv*/ )
{ {
try try
{ {
std::unique_ptr<vk::raii::Context> context = vk::raii::su::make_unique<vk::raii::Context>(); vk::raii::Context context;
/* VULKAN_KEY_START */ /* VULKAN_KEY_START */
uint32_t apiVersion = context->enumerateInstanceVersion(); uint32_t apiVersion = context.enumerateInstanceVersion();
std::cout << "APIVersion = " << decodeAPIVersion( apiVersion ) << std::endl; std::cout << "APIVersion = " << decodeAPIVersion( apiVersion ) << std::endl;
/* VULKAN_KEY_END */ /* VULKAN_KEY_END */

View File

@ -94,35 +94,38 @@ int main( int /*argc*/, char ** /*argv*/ )
{ {
try try
{ {
std::unique_ptr<vk::raii::Context> context = vk::raii::su::make_unique<vk::raii::Context>(); vk::raii::Context context;
std::unique_ptr<vk::raii::Instance> instance = vk::raii::Instance instance =
vk::raii::su::makeUniqueInstance( *context, AppName, EngineName, {}, vk::su::getInstanceExtensions() ); vk::raii::su::makeInstance( context, AppName, EngineName, {}, vk::su::getInstanceExtensions() );
#if !defined( NDEBUG ) #if !defined( NDEBUG )
std::unique_ptr<vk::raii::DebugUtilsMessengerEXT> debugUtilsMessenger = vk::raii::DebugUtilsMessengerEXT debugUtilsMessenger( instance, vk::su::makeDebugUtilsMessengerCreateInfoEXT() );
vk::raii::su::makeUniqueDebugUtilsMessengerEXT( *instance );
#endif #endif
std::unique_ptr<vk::raii::PhysicalDevice> physicalDevice = vk::raii::su::makeUniquePhysicalDevice( *instance ); vk::raii::PhysicalDevice physicalDevice = std::move( vk::raii::PhysicalDevices( instance ).front() );
vk::raii::su::SurfaceData surfaceData( *instance, AppName, vk::Extent2D( 500, 500 ) ); vk::FormatProperties formatProperties = physicalDevice.getFormatProperties( vk::Format::eR8G8B8A8Unorm );
if ( !( formatProperties.optimalTilingFeatures & vk::FormatFeatureFlagBits::eColorAttachment ) )
{
std::cout << "vk::Format::eR8G8B8A8Unorm format unsupported for input attachment\n";
exit( -1 );
}
vk::raii::su::SurfaceData surfaceData( instance, AppName, vk::Extent2D( 500, 500 ) );
std::pair<uint32_t, uint32_t> graphicsAndPresentQueueFamilyIndex = std::pair<uint32_t, uint32_t> graphicsAndPresentQueueFamilyIndex =
vk::raii::su::findGraphicsAndPresentQueueFamilyIndex( *physicalDevice, *surfaceData.surface ); vk::raii::su::findGraphicsAndPresentQueueFamilyIndex( physicalDevice, *surfaceData.pSurface );
std::unique_ptr<vk::raii::Device> device = vk::raii::su::makeUniqueDevice( vk::raii::Device device = vk::raii::su::makeDevice(
*physicalDevice, graphicsAndPresentQueueFamilyIndex.first, vk::su::getDeviceExtensions() ); physicalDevice, graphicsAndPresentQueueFamilyIndex.first, vk::su::getDeviceExtensions() );
std::unique_ptr<vk::raii::CommandPool> commandPool = vk::raii::CommandPool commandPool = vk::raii::CommandPool(
vk::raii::su::makeUniqueCommandPool( *device, graphicsAndPresentQueueFamilyIndex.first ); device, { vk::CommandPoolCreateFlagBits::eResetCommandBuffer, graphicsAndPresentQueueFamilyIndex.first } );
std::unique_ptr<vk::raii::CommandBuffer> commandBuffer = vk::raii::CommandBuffer commandBuffer = vk::raii::su::makeCommandBuffer( device, commandPool );
vk::raii::su::makeUniqueCommandBuffer( *device, *commandPool );
std::unique_ptr<vk::raii::Queue> graphicsQueue = vk::raii::Queue graphicsQueue( device, graphicsAndPresentQueueFamilyIndex.first, 0 );
vk::raii::su::make_unique<vk::raii::Queue>( *device, graphicsAndPresentQueueFamilyIndex.first, 0 ); vk::raii::Queue presentQueue( device, graphicsAndPresentQueueFamilyIndex.second, 0 );
std::unique_ptr<vk::raii::Queue> presentQueue =
vk::raii::su::make_unique<vk::raii::Queue>( *device, graphicsAndPresentQueueFamilyIndex.second, 0 );
vk::raii::su::SwapChainData swapChainData( *physicalDevice, vk::raii::su::SwapChainData swapChainData( physicalDevice,
*device, device,
*surfaceData.surface, *surfaceData.pSurface,
surfaceData.extent, surfaceData.extent,
vk::ImageUsageFlagBits::eColorAttachment | vk::ImageUsageFlagBits::eColorAttachment |
vk::ImageUsageFlagBits::eTransferSrc, vk::ImageUsageFlagBits::eTransferSrc,
@ -130,60 +133,55 @@ int main( int /*argc*/, char ** /*argv*/ )
graphicsAndPresentQueueFamilyIndex.first, graphicsAndPresentQueueFamilyIndex.first,
graphicsAndPresentQueueFamilyIndex.second ); graphicsAndPresentQueueFamilyIndex.second );
vk::raii::su::DepthBufferData depthBufferData( vk::raii::su::DepthBufferData depthBufferData( physicalDevice, device, vk::Format::eD16Unorm, surfaceData.extent );
*physicalDevice, *device, vk::Format::eD16Unorm, surfaceData.extent );
vk::raii::su::TextureData textureData( *physicalDevice, *device ); vk::raii::su::TextureData textureData( physicalDevice, device );
commandBuffer->begin( vk::CommandBufferBeginInfo() ); commandBuffer.begin( vk::CommandBufferBeginInfo() );
textureData.setImage( *commandBuffer, vk::su::MonochromeImageGenerator( { 118, 185, 0 } ) ); textureData.setImage( commandBuffer, vk::su::MonochromeImageGenerator( { 118, 185, 0 } ) );
vk::raii::su::BufferData uniformBufferData( vk::raii::su::BufferData uniformBufferData(
*physicalDevice, *device, sizeof( glm::mat4x4 ), vk::BufferUsageFlagBits::eUniformBuffer ); physicalDevice, device, sizeof( glm::mat4x4 ), vk::BufferUsageFlagBits::eUniformBuffer );
glm::mat4x4 mvpcMatrix = vk::su::createModelViewProjectionClipMatrix( surfaceData.extent ); glm::mat4x4 mvpcMatrix = vk::su::createModelViewProjectionClipMatrix( surfaceData.extent );
vk::raii::su::copyToDevice( *uniformBufferData.deviceMemory, mvpcMatrix ); vk::raii::su::copyToDevice( uniformBufferData.deviceMemory, mvpcMatrix );
vk::Format colorFormat = vk::Format colorFormat =
vk::su::pickSurfaceFormat( physicalDevice->getSurfaceFormatsKHR( **surfaceData.surface ) ).format; vk::su::pickSurfaceFormat( physicalDevice.getSurfaceFormatsKHR( **surfaceData.pSurface ) ).format;
std::unique_ptr<vk::raii::RenderPass> renderPass = vk::raii::RenderPass renderPass = vk::raii::su::makeRenderPass( device, colorFormat, depthBufferData.format );
vk::raii::su::makeUniqueRenderPass( *device, colorFormat, depthBufferData.format );
glslang::InitializeProcess(); glslang::InitializeProcess();
std::unique_ptr<vk::raii::ShaderModule> vertexShaderModule = vk::raii::ShaderModule vertexShaderModule =
vk::raii::su::makeUniqueShaderModule( *device, vk::ShaderStageFlagBits::eVertex, vertexShaderText ); vk::raii::su::makeShaderModule( device, vk::ShaderStageFlagBits::eVertex, vertexShaderText );
std::unique_ptr<vk::raii::ShaderModule> fragmentShaderModule = vk::raii::ShaderModule fragmentShaderModule =
vk::raii::su::makeUniqueShaderModule( *device, vk::ShaderStageFlagBits::eFragment, fragmentShaderText ); vk::raii::su::makeShaderModule( device, vk::ShaderStageFlagBits::eFragment, fragmentShaderText );
glslang::FinalizeProcess(); glslang::FinalizeProcess();
std::vector<std::unique_ptr<vk::raii::Framebuffer>> framebuffers = vk::raii::su::makeUniqueFramebuffers( std::vector<vk::raii::Framebuffer> framebuffers = vk::raii::su::makeFramebuffers(
*device, *renderPass, swapChainData.imageViews, depthBufferData.imageView, surfaceData.extent ); device, renderPass, swapChainData.imageViews, &*depthBufferData.pImageView, surfaceData.extent );
vk::raii::su::BufferData vertexBufferData( vk::raii::su::BufferData vertexBufferData(
*physicalDevice, *device, sizeof( texturedCubeData ), vk::BufferUsageFlagBits::eVertexBuffer ); physicalDevice, device, sizeof( texturedCubeData ), vk::BufferUsageFlagBits::eVertexBuffer );
vk::raii::su::copyToDevice( vk::raii::su::copyToDevice(
*vertexBufferData.deviceMemory, texturedCubeData, sizeof( texturedCubeData ) / sizeof( texturedCubeData[0] ) ); vertexBufferData.deviceMemory, texturedCubeData, sizeof( texturedCubeData ) / sizeof( texturedCubeData[0] ) );
/* VULKAN_KEY_START */ /* VULKAN_KEY_START */
// Create first layout to contain uniform buffer data // Create first layout to contain uniform buffer data
vk::DescriptorSetLayoutBinding uniformBinding( vk::DescriptorSetLayoutBinding uniformBinding(
0, vk::DescriptorType::eUniformBuffer, 1, vk::ShaderStageFlagBits::eVertex ); 0, vk::DescriptorType::eUniformBuffer, 1, vk::ShaderStageFlagBits::eVertex );
vk::DescriptorSetLayoutCreateInfo descriptorSetLayoutCreateInfo( {}, uniformBinding ); vk::DescriptorSetLayoutCreateInfo descriptorSetLayoutCreateInfo( {}, uniformBinding );
std::unique_ptr<vk::raii::DescriptorSetLayout> uniformLayout = vk::raii::DescriptorSetLayout uniformLayout( device, descriptorSetLayoutCreateInfo );
vk::raii::su::make_unique<vk::raii::DescriptorSetLayout>( *device, descriptorSetLayoutCreateInfo );
// Create second layout containing combined sampler/image data // Create second layout containing combined sampler/image data
vk::DescriptorSetLayoutBinding sampler2DBinding( vk::DescriptorSetLayoutBinding sampler2DBinding(
0, vk::DescriptorType::eCombinedImageSampler, 1, vk::ShaderStageFlagBits::eVertex ); 0, vk::DescriptorType::eCombinedImageSampler, 1, vk::ShaderStageFlagBits::eVertex );
descriptorSetLayoutCreateInfo.pBindings = &sampler2DBinding; descriptorSetLayoutCreateInfo.pBindings = &sampler2DBinding;
std::unique_ptr<vk::raii::DescriptorSetLayout> samplerLayout = vk::raii::DescriptorSetLayout samplerLayout( device, descriptorSetLayoutCreateInfo );
vk::raii::su::make_unique<vk::raii::DescriptorSetLayout>( *device, descriptorSetLayoutCreateInfo );
// Create pipeline layout with multiple descriptor sets // Create pipeline layout with multiple descriptor sets
std::array<vk::DescriptorSetLayout, 2> descriptorSetLayouts = { **uniformLayout, **samplerLayout }; std::array<vk::DescriptorSetLayout, 2> descriptorSetLayouts = { *uniformLayout, *samplerLayout };
vk::PipelineLayoutCreateInfo pipelineLayoutCreateInfo( {}, descriptorSetLayouts ); vk::PipelineLayoutCreateInfo pipelineLayoutCreateInfo( {}, descriptorSetLayouts );
std::unique_ptr<vk::raii::PipelineLayout> pipelineLayout = vk::raii::PipelineLayout pipelineLayout( device, pipelineLayoutCreateInfo );
vk::raii::su::make_unique<vk::raii::PipelineLayout>( *device, pipelineLayoutCreateInfo );
// Create a single pool to contain data for our two descriptor sets // Create a single pool to contain data for our two descriptor sets
std::array<vk::DescriptorPoolSize, 2> poolSizes = { vk::DescriptorPoolSize( vk::DescriptorType::eUniformBuffer, 1 ), std::array<vk::DescriptorPoolSize, 2> poolSizes = { vk::DescriptorPoolSize( vk::DescriptorType::eUniformBuffer, 1 ),
@ -191,49 +189,46 @@ int main( int /*argc*/, char ** /*argv*/ )
vk::DescriptorType::eCombinedImageSampler, 1 ) }; vk::DescriptorType::eCombinedImageSampler, 1 ) };
vk::DescriptorPoolCreateInfo descriptorPoolCreateInfo( vk::DescriptorPoolCreateInfo descriptorPoolCreateInfo(
vk::DescriptorPoolCreateFlagBits::eFreeDescriptorSet, 2, poolSizes ); vk::DescriptorPoolCreateFlagBits::eFreeDescriptorSet, 2, poolSizes );
std::unique_ptr<vk::raii::DescriptorPool> descriptorPool = vk::raii::DescriptorPool descriptorPool( device, descriptorPoolCreateInfo );
vk::raii::su::make_unique<vk::raii::DescriptorPool>( *device, descriptorPoolCreateInfo );
// Populate descriptor sets // Populate descriptor sets
vk::DescriptorSetAllocateInfo descriptorSetAllocateInfo( **descriptorPool, descriptorSetLayouts ); vk::DescriptorSetAllocateInfo descriptorSetAllocateInfo( *descriptorPool, descriptorSetLayouts );
vk::raii::DescriptorSets descriptorSets( *device, descriptorSetAllocateInfo ); vk::raii::DescriptorSets descriptorSets( device, descriptorSetAllocateInfo );
// Populate with info about our uniform buffer // Populate with info about our uniform buffer
vk::DescriptorBufferInfo uniformBufferInfo( **uniformBufferData.buffer, 0, sizeof( glm::mat4x4 ) ); vk::DescriptorBufferInfo uniformBufferInfo( *uniformBufferData.buffer, 0, sizeof( glm::mat4x4 ) );
vk::DescriptorImageInfo textureImageInfo( vk::DescriptorImageInfo textureImageInfo(
**textureData.sampler, **textureData.imageData->imageView, vk::ImageLayout::eShaderReadOnlyOptimal ); *textureData.sampler, **textureData.pImageData->pImageView, vk::ImageLayout::eShaderReadOnlyOptimal );
std::array<vk::WriteDescriptorSet, 2> writeDescriptorSets = { std::array<vk::WriteDescriptorSet, 2> writeDescriptorSets = {
{ vk::WriteDescriptorSet( *descriptorSets[0], 0, 0, vk::DescriptorType::eUniformBuffer, {}, uniformBufferInfo ), { vk::WriteDescriptorSet( *descriptorSets[0], 0, 0, vk::DescriptorType::eUniformBuffer, {}, uniformBufferInfo ),
vk::WriteDescriptorSet( vk::WriteDescriptorSet(
*descriptorSets[1], 0, 0, vk::DescriptorType::eCombinedImageSampler, textureImageInfo ) } *descriptorSets[1], 0, 0, vk::DescriptorType::eCombinedImageSampler, textureImageInfo ) }
}; };
device->updateDescriptorSets( writeDescriptorSets, nullptr ); device.updateDescriptorSets( writeDescriptorSets, nullptr );
/* VULKAN_KEY_END */ /* VULKAN_KEY_END */
std::unique_ptr<vk::raii::PipelineCache> pipelineCache = vk::raii::PipelineCache pipelineCache( device, vk::PipelineCacheCreateInfo() );
vk::raii::su::make_unique<vk::raii::PipelineCache>( *device, vk::PipelineCacheCreateInfo() ); vk::raii::Pipeline graphicsPipeline =
std::unique_ptr<vk::raii::Pipeline> graphicsPipeline = vk::raii::su::makeUniqueGraphicsPipeline( vk::raii::su::makeGraphicsPipeline( device,
*device, pipelineCache,
*pipelineCache, vertexShaderModule,
*vertexShaderModule, nullptr,
nullptr, fragmentShaderModule,
*fragmentShaderModule, nullptr,
nullptr, sizeof( texturedCubeData[0] ),
sizeof( texturedCubeData[0] ), { { vk::Format::eR32G32B32A32Sfloat, 0 }, { vk::Format::eR32G32Sfloat, 16 } },
{ { vk::Format::eR32G32B32A32Sfloat, 0 }, { vk::Format::eR32G32Sfloat, 16 } }, vk::FrontFace::eClockwise,
vk::FrontFace::eClockwise, true,
true, pipelineLayout,
*pipelineLayout, renderPass );
*renderPass );
// Get the index of the next available swapchain image: // Get the index of the next available swapchain image:
std::unique_ptr<vk::raii::Semaphore> imageAcquiredSemaphore = vk::raii::Semaphore imageAcquiredSemaphore( device, vk::SemaphoreCreateInfo() );
vk::raii::su::make_unique<vk::raii::Semaphore>( *device, vk::SemaphoreCreateInfo() ); vk::Result result;
vk::Result result; uint32_t imageIndex;
uint32_t imageIndex;
std::tie( result, imageIndex ) = std::tie( result, imageIndex ) =
swapChainData.swapChain->acquireNextImage( vk::su::FenceTimeout, **imageAcquiredSemaphore ); swapChainData.pSwapChain->acquireNextImage( vk::su::FenceTimeout, *imageAcquiredSemaphore );
assert( result == vk::Result::eSuccess ); assert( result == vk::Result::eSuccess );
assert( imageIndex < swapChainData.images.size() ); assert( imageIndex < swapChainData.images.size() );
@ -241,38 +236,38 @@ int main( int /*argc*/, char ** /*argv*/ )
clearValues[0].color = vk::ClearColorValue( std::array<float, 4>( { { 0.2f, 0.2f, 0.2f, 0.2f } } ) ); clearValues[0].color = vk::ClearColorValue( std::array<float, 4>( { { 0.2f, 0.2f, 0.2f, 0.2f } } ) );
clearValues[1].depthStencil = vk::ClearDepthStencilValue( 1.0f, 0 ); clearValues[1].depthStencil = vk::ClearDepthStencilValue( 1.0f, 0 );
vk::RenderPassBeginInfo renderPassBeginInfo( vk::RenderPassBeginInfo renderPassBeginInfo(
**renderPass, **framebuffers[imageIndex], vk::Rect2D( vk::Offset2D( 0, 0 ), surfaceData.extent ), clearValues ); *renderPass, *framebuffers[imageIndex], vk::Rect2D( vk::Offset2D( 0, 0 ), surfaceData.extent ), clearValues );
commandBuffer->beginRenderPass( renderPassBeginInfo, vk::SubpassContents::eInline ); commandBuffer.beginRenderPass( renderPassBeginInfo, vk::SubpassContents::eInline );
commandBuffer->bindPipeline( vk::PipelineBindPoint::eGraphics, **graphicsPipeline ); commandBuffer.bindPipeline( vk::PipelineBindPoint::eGraphics, *graphicsPipeline );
commandBuffer->bindDescriptorSets( commandBuffer.bindDescriptorSets(
vk::PipelineBindPoint::eGraphics, **pipelineLayout, 0, { *descriptorSets[0], *descriptorSets[1] }, nullptr ); vk::PipelineBindPoint::eGraphics, *pipelineLayout, 0, { *descriptorSets[0], *descriptorSets[1] }, nullptr );
vk::Buffer buffer = **vertexBufferData.buffer; vk::Buffer buffer = *vertexBufferData.buffer;
commandBuffer->bindVertexBuffers( 0, buffer, { 0 } ); commandBuffer.bindVertexBuffers( 0, buffer, { 0 } );
commandBuffer->setViewport( 0, commandBuffer.setViewport( 0,
vk::Viewport( 0.0f, vk::Viewport( 0.0f,
0.0f, 0.0f,
static_cast<float>( surfaceData.extent.width ), static_cast<float>( surfaceData.extent.width ),
static_cast<float>( surfaceData.extent.height ), static_cast<float>( surfaceData.extent.height ),
0.0f, 0.0f,
1.0f ) ); 1.0f ) );
commandBuffer->setScissor( 0, vk::Rect2D( vk::Offset2D( 0, 0 ), surfaceData.extent ) ); commandBuffer.setScissor( 0, vk::Rect2D( vk::Offset2D( 0, 0 ), surfaceData.extent ) );
commandBuffer->draw( 12 * 3, 1, 0, 0 ); commandBuffer.draw( 12 * 3, 1, 0, 0 );
commandBuffer->endRenderPass(); commandBuffer.endRenderPass();
commandBuffer->end(); commandBuffer.end();
std::unique_ptr<vk::raii::Fence> drawFence = vk::raii::su::make_unique<vk::raii::Fence>( *device, vk::FenceCreateInfo() ); vk::raii::Fence drawFence( device, vk::FenceCreateInfo() );
vk::PipelineStageFlags waitDestinationStageMask( vk::PipelineStageFlagBits::eColorAttachmentOutput ); vk::PipelineStageFlags waitDestinationStageMask( vk::PipelineStageFlagBits::eColorAttachmentOutput );
vk::SubmitInfo submitInfo( **imageAcquiredSemaphore, waitDestinationStageMask, **commandBuffer ); vk::SubmitInfo submitInfo( *imageAcquiredSemaphore, waitDestinationStageMask, *commandBuffer );
graphicsQueue->submit( submitInfo, **drawFence ); graphicsQueue.submit( submitInfo, *drawFence );
while ( vk::Result::eTimeout == device->waitForFences( { **drawFence }, VK_TRUE, vk::su::FenceTimeout ) ) while ( vk::Result::eTimeout == device.waitForFences( { *drawFence }, VK_TRUE, vk::su::FenceTimeout ) )
; ;
vk::PresentInfoKHR presentInfoKHR( nullptr, **swapChainData.swapChain, imageIndex ); vk::PresentInfoKHR presentInfoKHR( nullptr, **swapChainData.pSwapChain, imageIndex );
result = presentQueue->presentKHR( presentInfoKHR ); result = presentQueue.presentKHR( presentInfoKHR );
switch ( result ) switch ( result )
{ {
case vk::Result::eSuccess: break; case vk::Result::eSuccess: break;
@ -283,7 +278,7 @@ int main( int /*argc*/, char ** /*argv*/ )
} }
std::this_thread::sleep_for( std::chrono::milliseconds( 1000 ) ); std::this_thread::sleep_for( std::chrono::milliseconds( 1000 ) );
device->waitIdle(); device.waitIdle();
} }
catch ( vk::SystemError & err ) catch ( vk::SystemError & err )
{ {

View File

@ -32,35 +32,38 @@ int main( int /*argc*/, char ** /*argv*/ )
{ {
try try
{ {
std::unique_ptr<vk::raii::Context> context = vk::raii::su::make_unique<vk::raii::Context>(); vk::raii::Context context;
std::unique_ptr<vk::raii::Instance> instance = vk::raii::Instance instance =
vk::raii::su::makeUniqueInstance( *context, AppName, EngineName, {}, vk::su::getInstanceExtensions() ); vk::raii::su::makeInstance( context, AppName, EngineName, {}, vk::su::getInstanceExtensions() );
#if !defined( NDEBUG ) #if !defined( NDEBUG )
std::unique_ptr<vk::raii::DebugUtilsMessengerEXT> debugUtilsMessenger = vk::raii::DebugUtilsMessengerEXT debugUtilsMessenger( instance, vk::su::makeDebugUtilsMessengerCreateInfoEXT() );
vk::raii::su::makeUniqueDebugUtilsMessengerEXT( *instance );
#endif #endif
std::unique_ptr<vk::raii::PhysicalDevice> physicalDevice = vk::raii::su::makeUniquePhysicalDevice( *instance ); vk::raii::PhysicalDevice physicalDevice = std::move( vk::raii::PhysicalDevices( instance ).front() );
vk::raii::su::SurfaceData surfaceData( *instance, AppName, vk::Extent2D( 500, 500 ) ); vk::FormatProperties formatProperties = physicalDevice.getFormatProperties( vk::Format::eR8G8B8A8Unorm );
if ( !( formatProperties.optimalTilingFeatures & vk::FormatFeatureFlagBits::eColorAttachment ) )
{
std::cout << "vk::Format::eR8G8B8A8Unorm format unsupported for input attachment\n";
exit( -1 );
}
vk::raii::su::SurfaceData surfaceData( instance, AppName, vk::Extent2D( 500, 500 ) );
std::pair<uint32_t, uint32_t> graphicsAndPresentQueueFamilyIndex = std::pair<uint32_t, uint32_t> graphicsAndPresentQueueFamilyIndex =
vk::raii::su::findGraphicsAndPresentQueueFamilyIndex( *physicalDevice, *surfaceData.surface ); vk::raii::su::findGraphicsAndPresentQueueFamilyIndex( physicalDevice, *surfaceData.pSurface );
std::unique_ptr<vk::raii::Device> device = vk::raii::su::makeUniqueDevice( vk::raii::Device device = vk::raii::su::makeDevice(
*physicalDevice, graphicsAndPresentQueueFamilyIndex.first, vk::su::getDeviceExtensions() ); physicalDevice, graphicsAndPresentQueueFamilyIndex.first, vk::su::getDeviceExtensions() );
std::unique_ptr<vk::raii::CommandPool> commandPool = vk::raii::CommandPool commandPool = vk::raii::CommandPool(
vk::raii::su::makeUniqueCommandPool( *device, graphicsAndPresentQueueFamilyIndex.first ); device, { vk::CommandPoolCreateFlagBits::eResetCommandBuffer, graphicsAndPresentQueueFamilyIndex.first } );
std::unique_ptr<vk::raii::CommandBuffer> commandBuffer = vk::raii::CommandBuffer commandBuffer = vk::raii::su::makeCommandBuffer( device, commandPool );
vk::raii::su::makeUniqueCommandBuffer( *device, *commandPool );
std::unique_ptr<vk::raii::Queue> graphicsQueue = vk::raii::Queue graphicsQueue( device, graphicsAndPresentQueueFamilyIndex.first, 0 );
vk::raii::su::make_unique<vk::raii::Queue>( *device, graphicsAndPresentQueueFamilyIndex.first, 0 ); vk::raii::Queue presentQueue( device, graphicsAndPresentQueueFamilyIndex.second, 0 );
std::unique_ptr<vk::raii::Queue> presentQueue =
vk::raii::su::make_unique<vk::raii::Queue>( *device, graphicsAndPresentQueueFamilyIndex.second, 0 );
vk::raii::su::SwapChainData swapChainData( *physicalDevice, vk::raii::su::SwapChainData swapChainData( physicalDevice,
*device, device,
*surfaceData.surface, *surfaceData.pSurface,
surfaceData.extent, surfaceData.extent,
vk::ImageUsageFlagBits::eColorAttachment | vk::ImageUsageFlagBits::eColorAttachment |
vk::ImageUsageFlagBits::eTransferSrc, vk::ImageUsageFlagBits::eTransferSrc,
@ -68,148 +71,140 @@ int main( int /*argc*/, char ** /*argv*/ )
graphicsAndPresentQueueFamilyIndex.first, graphicsAndPresentQueueFamilyIndex.first,
graphicsAndPresentQueueFamilyIndex.second ); graphicsAndPresentQueueFamilyIndex.second );
vk::raii::su::DepthBufferData depthBufferData( vk::raii::su::DepthBufferData depthBufferData( physicalDevice, device, vk::Format::eD16Unorm, surfaceData.extent );
*physicalDevice, *device, vk::Format::eD16Unorm, surfaceData.extent );
vk::raii::su::BufferData uniformBufferData( vk::raii::su::BufferData uniformBufferData(
*physicalDevice, *device, sizeof( glm::mat4x4 ), vk::BufferUsageFlagBits::eUniformBuffer ); physicalDevice, device, sizeof( glm::mat4x4 ), vk::BufferUsageFlagBits::eUniformBuffer );
glm::mat4x4 mvpcMatrix = vk::su::createModelViewProjectionClipMatrix( surfaceData.extent ); glm::mat4x4 mvpcMatrix = vk::su::createModelViewProjectionClipMatrix( surfaceData.extent );
vk::raii::su::copyToDevice( *uniformBufferData.deviceMemory, mvpcMatrix ); vk::raii::su::copyToDevice( uniformBufferData.deviceMemory, mvpcMatrix );
std::unique_ptr<vk::raii::DescriptorSetLayout> descriptorSetLayout = vk::raii::su::makeUniqueDescriptorSetLayout( vk::raii::DescriptorSetLayout descriptorSetLayout = vk::raii::su::makeDescriptorSetLayout(
*device, { { vk::DescriptorType::eUniformBuffer, 1, vk::ShaderStageFlagBits::eVertex } } ); device, { { vk::DescriptorType::eUniformBuffer, 1, vk::ShaderStageFlagBits::eVertex } } );
std::unique_ptr<vk::raii::PipelineLayout> pipelineLayout = vk::raii::PipelineLayout pipelineLayout( device, { {}, *descriptorSetLayout } );
vk::raii::su::makeUniquePipelineLayout( *device, *descriptorSetLayout );
vk::Format colorFormat = vk::Format colorFormat =
vk::su::pickSurfaceFormat( physicalDevice->getSurfaceFormatsKHR( **surfaceData.surface ) ).format; vk::su::pickSurfaceFormat( physicalDevice.getSurfaceFormatsKHR( **surfaceData.pSurface ) ).format;
std::unique_ptr<vk::raii::RenderPass> renderPass = vk::raii::RenderPass renderPass = vk::raii::su::makeRenderPass( device, colorFormat, depthBufferData.format );
vk::raii::su::makeUniqueRenderPass( *device, colorFormat, depthBufferData.format );
glslang::InitializeProcess(); glslang::InitializeProcess();
std::unique_ptr<vk::raii::ShaderModule> vertexShaderModule = vk::raii::ShaderModule vertexShaderModule =
vk::raii::su::makeUniqueShaderModule( *device, vk::ShaderStageFlagBits::eVertex, vertexShaderText_PC_C ); vk::raii::su::makeShaderModule( device, vk::ShaderStageFlagBits::eVertex, vertexShaderText_PC_C );
std::unique_ptr<vk::raii::ShaderModule> fragmentShaderModule = vk::raii::ShaderModule fragmentShaderModule =
vk::raii::su::makeUniqueShaderModule( *device, vk::ShaderStageFlagBits::eFragment, fragmentShaderText_C_C ); vk::raii::su::makeShaderModule( device, vk::ShaderStageFlagBits::eFragment, fragmentShaderText_C_C );
glslang::FinalizeProcess(); glslang::FinalizeProcess();
std::vector<std::unique_ptr<vk::raii::Framebuffer>> framebuffers = vk::raii::su::makeUniqueFramebuffers( std::vector<vk::raii::Framebuffer> framebuffers = vk::raii::su::makeFramebuffers(
*device, *renderPass, swapChainData.imageViews, depthBufferData.imageView, surfaceData.extent ); device, renderPass, swapChainData.imageViews, &*depthBufferData.pImageView, surfaceData.extent );
vk::raii::su::BufferData vertexBufferData( vk::raii::su::BufferData vertexBufferData(
*physicalDevice, *device, sizeof( coloredCubeData ), vk::BufferUsageFlagBits::eVertexBuffer ); physicalDevice, device, sizeof( coloredCubeData ), vk::BufferUsageFlagBits::eVertexBuffer );
vk::raii::su::copyToDevice( vk::raii::su::copyToDevice(
*vertexBufferData.deviceMemory, coloredCubeData, sizeof( coloredCubeData ) / sizeof( coloredCubeData[0] ) ); vertexBufferData.deviceMemory, coloredCubeData, sizeof( coloredCubeData ) / sizeof( coloredCubeData[0] ) );
std::unique_ptr<vk::raii::DescriptorPool> descriptorPool = vk::raii::DescriptorPool descriptorPool =
vk::raii::su::makeUniqueDescriptorPool( *device, { { vk::DescriptorType::eUniformBuffer, 1 } } ); vk::raii::su::makeDescriptorPool( device, { { vk::DescriptorType::eUniformBuffer, 1 } } );
std::unique_ptr<vk::raii::DescriptorSet> descriptorSet = vk::raii::DescriptorSet descriptorSet =
vk::raii::su::makeUniqueDescriptorSet( *device, *descriptorPool, *descriptorSetLayout ); std::move( vk::raii::DescriptorSets( device, { *descriptorPool, *descriptorSetLayout } ).front() );
vk::raii::su::updateDescriptorSets( vk::raii::su::updateDescriptorSets(
*device, *descriptorSet, { { vk::DescriptorType::eUniformBuffer, *uniformBufferData.buffer, nullptr } }, {} ); device, descriptorSet, { { vk::DescriptorType::eUniformBuffer, uniformBufferData.buffer, nullptr } }, {} );
std::unique_ptr<vk::raii::PipelineCache> pipelineCache = vk::raii::PipelineCache pipelineCache( device, vk::PipelineCacheCreateInfo() );
vk::raii::su::make_unique<vk::raii::PipelineCache>( *device, vk::PipelineCacheCreateInfo() ); vk::raii::Pipeline graphicsPipeline =
std::unique_ptr<vk::raii::Pipeline> graphicsPipeline = vk::raii::su::makeUniqueGraphicsPipeline( vk::raii::su::makeGraphicsPipeline( device,
*device, pipelineCache,
*pipelineCache, vertexShaderModule,
*vertexShaderModule, nullptr,
nullptr, fragmentShaderModule,
*fragmentShaderModule, nullptr,
nullptr, sizeof( coloredCubeData[0] ),
sizeof( coloredCubeData[0] ), { { vk::Format::eR32G32B32A32Sfloat, 0 }, { vk::Format::eR32G32Sfloat, 16 } },
{ { vk::Format::eR32G32B32A32Sfloat, 0 }, { vk::Format::eR32G32Sfloat, 16 } }, vk::FrontFace::eClockwise,
vk::FrontFace::eClockwise, true,
true, pipelineLayout,
*pipelineLayout, renderPass );
*renderPass );
/* VULKAN_KEY_START */ /* VULKAN_KEY_START */
std::unique_ptr<vk::raii::Semaphore> imageAcquiredSemaphore = vk::raii::Semaphore imageAcquiredSemaphore( device, vk::SemaphoreCreateInfo() );
vk::raii::su::make_unique<vk::raii::Semaphore>( *device, vk::SemaphoreCreateInfo() );
// Get the index of the next available swapchain image: // Get the index of the next available swapchain image:
vk::Result result; vk::Result result;
uint32_t imageIndex; uint32_t imageIndex;
std::tie( result, imageIndex ) = std::tie( result, imageIndex ) =
swapChainData.swapChain->acquireNextImage( vk::su::FenceTimeout, **imageAcquiredSemaphore ); swapChainData.pSwapChain->acquireNextImage( vk::su::FenceTimeout, *imageAcquiredSemaphore );
assert( result == vk::Result::eSuccess ); assert( result == vk::Result::eSuccess );
assert( imageIndex < swapChainData.images.size() ); assert( imageIndex < swapChainData.images.size() );
/* Allocate a uniform buffer that will take query results. */ /* Allocate a uniform buffer that will take query results. */
vk::BufferCreateInfo bufferCreateInfo( vk::BufferCreateInfo bufferCreateInfo(
{}, 4 * sizeof( uint64_t ), vk::BufferUsageFlagBits::eUniformBuffer | vk::BufferUsageFlagBits::eTransferDst ); {}, 4 * sizeof( uint64_t ), vk::BufferUsageFlagBits::eUniformBuffer | vk::BufferUsageFlagBits::eTransferDst );
std::unique_ptr<vk::raii::Buffer> queryResultBuffer = vk::raii::Buffer queryResultBuffer( device, bufferCreateInfo );
vk::raii::su::make_unique<vk::raii::Buffer>( *device, bufferCreateInfo );
vk::MemoryRequirements memoryRequirements = queryResultBuffer->getMemoryRequirements(); vk::MemoryRequirements memoryRequirements = queryResultBuffer.getMemoryRequirements();
uint32_t memoryTypeIndex = uint32_t memoryTypeIndex =
vk::su::findMemoryType( physicalDevice->getMemoryProperties(), vk::su::findMemoryType( physicalDevice.getMemoryProperties(),
memoryRequirements.memoryTypeBits, memoryRequirements.memoryTypeBits,
vk::MemoryPropertyFlagBits::eHostVisible | vk::MemoryPropertyFlagBits::eHostCoherent ); vk::MemoryPropertyFlagBits::eHostVisible | vk::MemoryPropertyFlagBits::eHostCoherent );
vk::MemoryAllocateInfo memoryAllocateInfo( memoryRequirements.size, memoryTypeIndex ); vk::MemoryAllocateInfo memoryAllocateInfo( memoryRequirements.size, memoryTypeIndex );
std::unique_ptr<vk::raii::DeviceMemory> queryResultMemory = vk::raii::DeviceMemory queryResultMemory( device, memoryAllocateInfo );
vk::raii::su::make_unique<vk::raii::DeviceMemory>( *device, memoryAllocateInfo );
queryResultBuffer->bindMemory( **queryResultMemory, 0 ); queryResultBuffer.bindMemory( *queryResultMemory, 0 );
vk::QueryPoolCreateInfo queryPoolCreateInfo( {}, vk::QueryType::eOcclusion, 2, {} ); vk::QueryPoolCreateInfo queryPoolCreateInfo( {}, vk::QueryType::eOcclusion, 2, {} );
std::unique_ptr<vk::raii::QueryPool> queryPool = vk::raii::QueryPool queryPool( device, queryPoolCreateInfo );
vk::raii::su::make_unique<vk::raii::QueryPool>( *device, queryPoolCreateInfo );
commandBuffer->begin( {} ); commandBuffer.begin( {} );
commandBuffer->resetQueryPool( **queryPool, 0, 2 ); commandBuffer.resetQueryPool( *queryPool, 0, 2 );
std::array<vk::ClearValue, 2> clearValues; std::array<vk::ClearValue, 2> clearValues;
clearValues[0].color = vk::ClearColorValue( std::array<float, 4>( { { 0.2f, 0.2f, 0.2f, 0.2f } } ) ); clearValues[0].color = vk::ClearColorValue( std::array<float, 4>( { { 0.2f, 0.2f, 0.2f, 0.2f } } ) );
clearValues[1].depthStencil = vk::ClearDepthStencilValue( 1.0f, 0 ); clearValues[1].depthStencil = vk::ClearDepthStencilValue( 1.0f, 0 );
commandBuffer->beginRenderPass( commandBuffer.beginRenderPass(
vk::RenderPassBeginInfo( vk::RenderPassBeginInfo(
**renderPass, **framebuffers[imageIndex], vk::Rect2D( vk::Offset2D(), surfaceData.extent ), clearValues ), *renderPass, *framebuffers[imageIndex], vk::Rect2D( vk::Offset2D(), surfaceData.extent ), clearValues ),
vk::SubpassContents::eInline ); vk::SubpassContents::eInline );
commandBuffer->bindPipeline( vk::PipelineBindPoint::eGraphics, **graphicsPipeline ); commandBuffer.bindPipeline( vk::PipelineBindPoint::eGraphics, *graphicsPipeline );
commandBuffer->bindDescriptorSets( vk::PipelineBindPoint::eGraphics, **pipelineLayout, 0, { **descriptorSet }, {} ); commandBuffer.bindDescriptorSets( vk::PipelineBindPoint::eGraphics, *pipelineLayout, 0, { *descriptorSet }, {} );
commandBuffer->bindVertexBuffers( 0, { **vertexBufferData.buffer }, { 0 } ); commandBuffer.bindVertexBuffers( 0, { *vertexBufferData.buffer }, { 0 } );
commandBuffer->setViewport( 0, commandBuffer.setViewport( 0,
vk::Viewport( 0.0f, vk::Viewport( 0.0f,
0.0f, 0.0f,
static_cast<float>( surfaceData.extent.width ), static_cast<float>( surfaceData.extent.width ),
static_cast<float>( surfaceData.extent.height ), static_cast<float>( surfaceData.extent.height ),
0.0f, 0.0f,
1.0f ) ); 1.0f ) );
commandBuffer->setScissor( 0, vk::Rect2D( vk::Offset2D( 0, 0 ), surfaceData.extent ) ); commandBuffer.setScissor( 0, vk::Rect2D( vk::Offset2D( 0, 0 ), surfaceData.extent ) );
commandBuffer->beginQuery( **queryPool, 0, vk::QueryControlFlags() ); commandBuffer.beginQuery( *queryPool, 0, vk::QueryControlFlags() );
commandBuffer->endQuery( **queryPool, 0 ); commandBuffer.endQuery( *queryPool, 0 );
commandBuffer->beginQuery( **queryPool, 1, vk::QueryControlFlags() ); commandBuffer.beginQuery( *queryPool, 1, vk::QueryControlFlags() );
commandBuffer->draw( 12 * 3, 1, 0, 0 ); commandBuffer.draw( 12 * 3, 1, 0, 0 );
commandBuffer->endRenderPass(); commandBuffer.endRenderPass();
commandBuffer->endQuery( **queryPool, 1 ); commandBuffer.endQuery( *queryPool, 1 );
commandBuffer->copyQueryPoolResults( **queryPool, commandBuffer.copyQueryPoolResults( *queryPool,
0, 0,
2, 2,
**queryResultBuffer, *queryResultBuffer,
0, 0,
sizeof( uint64_t ), sizeof( uint64_t ),
vk::QueryResultFlagBits::e64 | vk::QueryResultFlagBits::eWait ); vk::QueryResultFlagBits::e64 | vk::QueryResultFlagBits::eWait );
commandBuffer->end(); commandBuffer.end();
std::unique_ptr<vk::raii::Fence> drawFence = vk::raii::su::make_unique<vk::raii::Fence>( *device, vk::FenceCreateInfo() ); vk::raii::Fence drawFence( device, vk::FenceCreateInfo() );
vk::PipelineStageFlags waitDestinationStageMask( vk::PipelineStageFlagBits::eColorAttachmentOutput ); vk::PipelineStageFlags waitDestinationStageMask( vk::PipelineStageFlagBits::eColorAttachmentOutput );
vk::SubmitInfo submitInfo( **imageAcquiredSemaphore, waitDestinationStageMask, **commandBuffer ); vk::SubmitInfo submitInfo( *imageAcquiredSemaphore, waitDestinationStageMask, *commandBuffer );
graphicsQueue->submit( submitInfo, **drawFence ); graphicsQueue.submit( submitInfo, *drawFence );
graphicsQueue->waitIdle(); graphicsQueue.waitIdle();
std::vector<uint64_t> poolResults; std::vector<uint64_t> poolResults;
std::tie( result, poolResults ) = queryPool->getResults<uint64_t>( std::tie( result, poolResults ) = queryPool.getResults<uint64_t>(
0, 2, 2 * sizeof( uint64_t ), sizeof( uint64_t ), vk::QueryResultFlagBits::e64 | vk::QueryResultFlagBits::eWait ); 0, 2, 2 * sizeof( uint64_t ), sizeof( uint64_t ), vk::QueryResultFlagBits::e64 | vk::QueryResultFlagBits::eWait );
switch ( result ) switch ( result )
{ {
@ -226,19 +221,19 @@ int main( int /*argc*/, char ** /*argv*/ )
/* Read back query result from buffer */ /* Read back query result from buffer */
uint64_t * samplesPassedPtr = uint64_t * samplesPassedPtr =
static_cast<uint64_t *>( queryResultMemory->mapMemory( 0, memoryRequirements.size, vk::MemoryMapFlags() ) ); static_cast<uint64_t *>( queryResultMemory.mapMemory( 0, memoryRequirements.size, vk::MemoryMapFlags() ) );
std::cout << "vkCmdCopyQueryPoolResults data\n"; std::cout << "vkCmdCopyQueryPoolResults data\n";
std::cout << "samples_passed[0] = " << samplesPassedPtr[0] << "\n"; std::cout << "samples_passed[0] = " << samplesPassedPtr[0] << "\n";
std::cout << "samples_passed[1] = " << samplesPassedPtr[1] << "\n"; std::cout << "samples_passed[1] = " << samplesPassedPtr[1] << "\n";
queryResultMemory->unmapMemory(); queryResultMemory.unmapMemory();
while ( vk::Result::eTimeout == device->waitForFences( { **drawFence }, VK_TRUE, vk::su::FenceTimeout ) ) while ( vk::Result::eTimeout == device.waitForFences( { *drawFence }, VK_TRUE, vk::su::FenceTimeout ) )
; ;
vk::PresentInfoKHR presentInfoKHR( nullptr, **swapChainData.swapChain, imageIndex ); vk::PresentInfoKHR presentInfoKHR( nullptr, **swapChainData.pSwapChain, imageIndex );
result = presentQueue->presentKHR( presentInfoKHR ); result = presentQueue.presentKHR( presentInfoKHR );
switch ( result ) switch ( result )
{ {
case vk::Result::eSuccess: break; case vk::Result::eSuccess: break;

View File

@ -27,15 +27,14 @@ int main( int /*argc*/, char ** /*argv*/ )
{ {
try try
{ {
std::unique_ptr<vk::raii::Context> context = vk::raii::su::make_unique<vk::raii::Context>(); vk::raii::Context context;
std::unique_ptr<vk::raii::Instance> instance = vk::raii::su::makeUniqueInstance( *context, AppName, EngineName ); vk::raii::Instance instance = vk::raii::su::makeInstance( context, AppName, EngineName );
#if !defined( NDEBUG ) #if !defined( NDEBUG )
std::unique_ptr<vk::raii::DebugUtilsMessengerEXT> debugUtilsMessenger = vk::raii::DebugUtilsMessengerEXT debugUtilsMessenger( instance, vk::su::makeDebugUtilsMessengerCreateInfoEXT() );
vk::raii::su::makeUniqueDebugUtilsMessengerEXT( *instance );
#endif #endif
// enumerate the physicalDevices // enumerate the physicalDevices
vk::raii::PhysicalDevices physicalDevices( *instance ); vk::raii::PhysicalDevices physicalDevices( instance );
/* VULKAN_KEY_START */ /* VULKAN_KEY_START */
@ -48,9 +47,8 @@ int main( int /*argc*/, char ** /*argv*/ )
// sort the extensions alphabetically // sort the extensions alphabetically
std::sort( extensionProperties.begin(), std::sort( extensionProperties.begin(),
extensionProperties.end(), extensionProperties.end(),
[]( vk::ExtensionProperties const & a, vk::ExtensionProperties const & b ) { []( vk::ExtensionProperties const & a, vk::ExtensionProperties const & b )
return strcmp( a.extensionName, b.extensionName ) < 0; { return strcmp( a.extensionName, b.extensionName ) < 0; } );
} );
for ( auto const & ep : extensionProperties ) for ( auto const & ep : extensionProperties )
{ {
std::cout << "\t" << ep.extensionName << ":" << std::endl; std::cout << "\t" << ep.extensionName << ":" << std::endl;

View File

@ -36,16 +36,15 @@ int main( int /*argc*/, char ** /*argv*/ )
{ {
try try
{ {
std::unique_ptr<vk::raii::Context> context = vk::raii::su::make_unique<vk::raii::Context>(); vk::raii::Context context;
std::unique_ptr<vk::raii::Instance> instance = vk::raii::Instance instance =
vk::raii::su::makeUniqueInstance( *context, AppName, EngineName, {}, {}, VK_API_VERSION_1_1 ); vk::raii::su::makeInstance( context, AppName, EngineName, {}, {}, VK_API_VERSION_1_1 );
#if !defined( NDEBUG ) #if !defined( NDEBUG )
std::unique_ptr<vk::raii::DebugUtilsMessengerEXT> debugUtilsMessenger = vk::raii::DebugUtilsMessengerEXT debugUtilsMessenger( instance, vk::su::makeDebugUtilsMessengerCreateInfoEXT() );
vk::raii::su::makeUniqueDebugUtilsMessengerEXT( *instance );
#endif #endif
// enumerate the physicalDevices // enumerate the physicalDevices
vk::raii::PhysicalDevices physicalDevices( *instance ); vk::raii::PhysicalDevices physicalDevices( instance );
/* VULKAN_KEY_START */ /* VULKAN_KEY_START */

View File

@ -27,17 +27,16 @@ int main( int /*argc*/, char ** /*argv*/ )
{ {
try try
{ {
std::unique_ptr<vk::raii::Context> context = vk::raii::su::make_unique<vk::raii::Context>(); vk::raii::Context context;
std::unique_ptr<vk::raii::Instance> instance = vk::raii::Instance instance =
vk::raii::su::makeUniqueInstance( *context, AppName, EngineName, {}, {}, VK_API_VERSION_1_1 ); vk::raii::su::makeInstance( context, AppName, EngineName, {}, {}, VK_API_VERSION_1_1 );
#if !defined( NDEBUG ) #if !defined( NDEBUG )
std::unique_ptr<vk::raii::DebugUtilsMessengerEXT> debugUtilsMessenger = vk::raii::DebugUtilsMessengerEXT debugUtilsMessenger( instance, vk::su::makeDebugUtilsMessengerCreateInfoEXT() );
vk::raii::su::makeUniqueDebugUtilsMessengerEXT( *instance );
#endif #endif
/* VULKAN_KEY_START */ /* VULKAN_KEY_START */
std::vector<vk::PhysicalDeviceGroupProperties> groupProperties = instance->enumeratePhysicalDeviceGroups(); std::vector<vk::PhysicalDeviceGroupProperties> groupProperties = instance.enumeratePhysicalDeviceGroups();
std::cout << std::boolalpha; std::cout << std::boolalpha;
for ( size_t i = 0; i < groupProperties.size(); i++ ) for ( size_t i = 0; i < groupProperties.size(); i++ )
@ -49,9 +48,9 @@ int main( int /*argc*/, char ** /*argv*/ )
<< "physicalDevices:\n"; << "physicalDevices:\n";
for ( size_t j = 0; j < groupProperties[i].physicalDeviceCount; j++ ) for ( size_t j = 0; j < groupProperties[i].physicalDeviceCount; j++ )
{ {
std::unique_ptr<vk::raii::PhysicalDevice> physicalDevice = vk::raii::su::make_unique<vk::raii::PhysicalDevice>( vk::raii::PhysicalDevice physicalDevice( static_cast<VkPhysicalDevice>( groupProperties[i].physicalDevices[j] ),
static_cast<VkPhysicalDevice>( groupProperties[i].physicalDevices[j] ), instance->getDispatcher() ); instance.getDispatcher() );
std::cout << "\t\t" << j << " : " << physicalDevice->getProperties().deviceName << "\n"; std::cout << "\t\t" << j << " : " << physicalDevice.getProperties().deviceName << "\n";
} }
std::cout << "\t" std::cout << "\t"
<< "subsetAllocation = " << !!groupProperties[i].subsetAllocation << "\n"; << "subsetAllocation = " << !!groupProperties[i].subsetAllocation << "\n";
@ -59,17 +58,17 @@ int main( int /*argc*/, char ** /*argv*/ )
if ( 1 < groupProperties[i].physicalDeviceCount ) if ( 1 < groupProperties[i].physicalDeviceCount )
{ {
std::unique_ptr<vk::raii::PhysicalDevice> physicalDevice = vk::raii::su::make_unique<vk::raii::PhysicalDevice>( vk::raii::PhysicalDevice physicalDevice( static_cast<VkPhysicalDevice>( groupProperties[i].physicalDevices[0] ),
static_cast<VkPhysicalDevice>( groupProperties[i].physicalDevices[0] ), instance->getDispatcher() ); instance.getDispatcher() );
// get the QueueFamilyProperties of the first PhysicalDevice // get the QueueFamilyProperties of the first PhysicalDevice
std::vector<vk::QueueFamilyProperties> queueFamilyProperties = physicalDevice->getQueueFamilyProperties(); std::vector<vk::QueueFamilyProperties> queueFamilyProperties = physicalDevice.getQueueFamilyProperties();
// get the first index into queueFamiliyProperties which supports graphics // get the first index into queueFamiliyProperties which supports graphics
auto propertyIterator = std::find_if( auto propertyIterator = std::find_if( queueFamilyProperties.begin(),
queueFamilyProperties.begin(), queueFamilyProperties.end(), []( vk::QueueFamilyProperties const & qfp ) { queueFamilyProperties.end(),
return qfp.queueFlags & vk::QueueFlagBits::eGraphics; []( vk::QueueFamilyProperties const & qfp )
} ); { return qfp.queueFlags & vk::QueueFlagBits::eGraphics; } );
size_t graphicsQueueFamilyIndex = std::distance( queueFamilyProperties.begin(), propertyIterator ); size_t graphicsQueueFamilyIndex = std::distance( queueFamilyProperties.begin(), propertyIterator );
assert( graphicsQueueFamilyIndex < queueFamilyProperties.size() ); assert( graphicsQueueFamilyIndex < queueFamilyProperties.size() );
@ -81,8 +80,7 @@ int main( int /*argc*/, char ** /*argv*/ )
{ {}, deviceQueueCreateInfo }, { {}, deviceQueueCreateInfo },
{ groupProperties[i].physicalDeviceCount, groupProperties[i].physicalDevices } ); { groupProperties[i].physicalDeviceCount, groupProperties[i].physicalDevices } );
std::unique_ptr<vk::raii::Device> device = vk::raii::Device device( physicalDevice, deviceCreateInfoChain.get<vk::DeviceCreateInfo>() );
vk::raii::su::make_unique<vk::raii::Device>( *physicalDevice, deviceCreateInfoChain.get<vk::DeviceCreateInfo>() );
} }
} }

View File

@ -101,16 +101,15 @@ int main( int /*argc*/, char ** /*argv*/ )
{ {
try try
{ {
std::unique_ptr<vk::raii::Context> context = vk::raii::su::make_unique<vk::raii::Context>(); vk::raii::Context context;
std::unique_ptr<vk::raii::Instance> instance = vk::raii::Instance instance =
vk::raii::su::makeUniqueInstance( *context, AppName, EngineName, {}, {}, VK_API_VERSION_1_1 ); vk::raii::su::makeInstance( context, AppName, EngineName, {}, {}, VK_API_VERSION_1_1 );
#if !defined( NDEBUG ) #if !defined( NDEBUG )
std::unique_ptr<vk::raii::DebugUtilsMessengerEXT> debugUtilsMessenger = vk::raii::DebugUtilsMessengerEXT debugUtilsMessenger( instance, vk::su::makeDebugUtilsMessengerCreateInfoEXT() );
vk::raii::su::makeUniqueDebugUtilsMessengerEXT( *instance );
#endif #endif
// enumerate the physicalDevices // enumerate the physicalDevices
vk::raii::PhysicalDevices physicalDevices( *instance ); vk::raii::PhysicalDevices physicalDevices( instance );
/* VULKAN_KEY_START */ /* VULKAN_KEY_START */

View File

@ -29,16 +29,15 @@ int main( int /*argc*/, char ** /*argv*/ )
{ {
try try
{ {
std::unique_ptr<vk::raii::Context> context = vk::raii::su::make_unique<vk::raii::Context>(); vk::raii::Context context;
std::unique_ptr<vk::raii::Instance> instance = vk::raii::Instance instance =
vk::raii::su::makeUniqueInstance( *context, AppName, EngineName, {}, {}, VK_API_VERSION_1_1 ); vk::raii::su::makeInstance( context, AppName, EngineName, {}, {}, VK_API_VERSION_1_1 );
#if !defined( NDEBUG ) #if !defined( NDEBUG )
std::unique_ptr<vk::raii::DebugUtilsMessengerEXT> debugUtilsMessenger = vk::raii::DebugUtilsMessengerEXT debugUtilsMessenger( instance, vk::su::makeDebugUtilsMessengerCreateInfoEXT() );
vk::raii::su::makeUniqueDebugUtilsMessengerEXT( *instance );
#endif #endif
// enumerate the physicalDevices // enumerate the physicalDevices
vk::raii::PhysicalDevices physicalDevices( *instance ); vk::raii::PhysicalDevices physicalDevices( instance );
/* VULKAN_KEY_START */ /* VULKAN_KEY_START */

View File

@ -72,37 +72,32 @@ int main( int /*argc*/, char ** /*argv*/ )
{ {
try try
{ {
std::unique_ptr<vk::raii::Context> context = vk::raii::su::make_unique<vk::raii::Context>(); vk::raii::Context context;
std::unique_ptr<vk::raii::Instance> instance = vk::raii::Instance instance =
vk::raii::su::makeUniqueInstance( *context, AppName, EngineName, {}, vk::su::getInstanceExtensions() ); vk::raii::su::makeInstance( context, AppName, EngineName, {}, vk::su::getInstanceExtensions() );
#if !defined( NDEBUG ) #if !defined( NDEBUG )
std::unique_ptr<vk::raii::DebugUtilsMessengerEXT> debugUtilsMessenger = vk::raii::DebugUtilsMessengerEXT debugUtilsMessenger( instance, vk::su::makeDebugUtilsMessengerCreateInfoEXT() );
vk::raii::su::makeUniqueDebugUtilsMessengerEXT( *instance );
#endif #endif
vk::raii::PhysicalDevice physicalDevice = std::move( vk::raii::PhysicalDevices( instance ).front() );
vk::PhysicalDeviceProperties properties = physicalDevice.getProperties();
std::unique_ptr<vk::raii::PhysicalDevice> physicalDevice = vk::raii::su::makeUniquePhysicalDevice( *instance ); vk::raii::su::SurfaceData surfaceData( instance, AppName, vk::Extent2D( 500, 500 ) );
vk::PhysicalDeviceProperties properties = physicalDevice->getProperties();
vk::raii::su::SurfaceData surfaceData( *instance, AppName, vk::Extent2D( 500, 500 ) );
std::pair<uint32_t, uint32_t> graphicsAndPresentQueueFamilyIndex = std::pair<uint32_t, uint32_t> graphicsAndPresentQueueFamilyIndex =
vk::raii::su::findGraphicsAndPresentQueueFamilyIndex( *physicalDevice, *surfaceData.surface ); vk::raii::su::findGraphicsAndPresentQueueFamilyIndex( physicalDevice, *surfaceData.pSurface );
std::unique_ptr<vk::raii::Device> device = vk::raii::su::makeUniqueDevice( vk::raii::Device device = vk::raii::su::makeDevice(
*physicalDevice, graphicsAndPresentQueueFamilyIndex.first, vk::su::getDeviceExtensions() ); physicalDevice, graphicsAndPresentQueueFamilyIndex.first, vk::su::getDeviceExtensions() );
std::unique_ptr<vk::raii::CommandPool> commandPool = vk::raii::CommandPool commandPool = vk::raii::CommandPool(
vk::raii::su::makeUniqueCommandPool( *device, graphicsAndPresentQueueFamilyIndex.first ); device, { vk::CommandPoolCreateFlagBits::eResetCommandBuffer, graphicsAndPresentQueueFamilyIndex.first } );
std::unique_ptr<vk::raii::CommandBuffer> commandBuffer = vk::raii::CommandBuffer commandBuffer = vk::raii::su::makeCommandBuffer( device, commandPool );
vk::raii::su::makeUniqueCommandBuffer( *device, *commandPool );
std::unique_ptr<vk::raii::Queue> graphicsQueue = vk::raii::Queue graphicsQueue( device, graphicsAndPresentQueueFamilyIndex.first, 0 );
vk::raii::su::make_unique<vk::raii::Queue>( *device, graphicsAndPresentQueueFamilyIndex.first, 0 ); vk::raii::Queue presentQueue( device, graphicsAndPresentQueueFamilyIndex.second, 0 );
std::unique_ptr<vk::raii::Queue> presentQueue =
vk::raii::su::make_unique<vk::raii::Queue>( *device, graphicsAndPresentQueueFamilyIndex.second, 0 );
vk::raii::su::SwapChainData swapChainData( *physicalDevice, vk::raii::su::SwapChainData swapChainData( physicalDevice,
*device, device,
*surfaceData.surface, *surfaceData.pSurface,
surfaceData.extent, surfaceData.extent,
vk::ImageUsageFlagBits::eColorAttachment | vk::ImageUsageFlagBits::eColorAttachment |
vk::ImageUsageFlagBits::eTransferSrc, vk::ImageUsageFlagBits::eTransferSrc,
@ -110,54 +105,51 @@ int main( int /*argc*/, char ** /*argv*/ )
graphicsAndPresentQueueFamilyIndex.first, graphicsAndPresentQueueFamilyIndex.first,
graphicsAndPresentQueueFamilyIndex.second ); graphicsAndPresentQueueFamilyIndex.second );
vk::raii::su::DepthBufferData depthBufferData( vk::raii::su::DepthBufferData depthBufferData( physicalDevice, device, vk::Format::eD16Unorm, surfaceData.extent );
*physicalDevice, *device, vk::Format::eD16Unorm, surfaceData.extent );
vk::raii::su::TextureData textureData( *physicalDevice, *device ); vk::raii::su::TextureData textureData( physicalDevice, device );
commandBuffer->begin( vk::CommandBufferBeginInfo() ); commandBuffer.begin( vk::CommandBufferBeginInfo() );
textureData.setImage( *commandBuffer, vk::su::MonochromeImageGenerator( { 118, 185, 0 } ) ); textureData.setImage( commandBuffer, vk::su::MonochromeImageGenerator( { 118, 185, 0 } ) );
vk::raii::su::BufferData uniformBufferData( vk::raii::su::BufferData uniformBufferData(
*physicalDevice, *device, sizeof( glm::mat4x4 ), vk::BufferUsageFlagBits::eUniformBuffer ); physicalDevice, device, sizeof( glm::mat4x4 ), vk::BufferUsageFlagBits::eUniformBuffer );
glm::mat4x4 mvpcMatrix = vk::su::createModelViewProjectionClipMatrix( surfaceData.extent ); glm::mat4x4 mvpcMatrix = vk::su::createModelViewProjectionClipMatrix( surfaceData.extent );
vk::raii::su::copyToDevice( *uniformBufferData.deviceMemory, mvpcMatrix ); vk::raii::su::copyToDevice( uniformBufferData.deviceMemory, mvpcMatrix );
std::unique_ptr<vk::raii::DescriptorSetLayout> descriptorSetLayout = vk::raii::su::makeUniqueDescriptorSetLayout( vk::raii::DescriptorSetLayout descriptorSetLayout = vk::raii::su::makeDescriptorSetLayout(
*device, device,
{ { vk::DescriptorType::eUniformBuffer, 1, vk::ShaderStageFlagBits::eVertex }, { { vk::DescriptorType::eUniformBuffer, 1, vk::ShaderStageFlagBits::eVertex },
{ vk::DescriptorType::eCombinedImageSampler, 1, vk::ShaderStageFlagBits::eFragment } } ); { vk::DescriptorType::eCombinedImageSampler, 1, vk::ShaderStageFlagBits::eFragment } } );
std::unique_ptr<vk::raii::PipelineLayout> pipelineLayout = vk::raii::PipelineLayout pipelineLayout( device, { {}, *descriptorSetLayout } );
vk::raii::su::makeUniquePipelineLayout( *device, *descriptorSetLayout );
vk::Format colorFormat = vk::Format colorFormat =
vk::su::pickSurfaceFormat( physicalDevice->getSurfaceFormatsKHR( **surfaceData.surface ) ).format; vk::su::pickSurfaceFormat( physicalDevice.getSurfaceFormatsKHR( **surfaceData.pSurface ) ).format;
std::unique_ptr<vk::raii::RenderPass> renderPass = vk::raii::RenderPass renderPass = vk::raii::su::makeRenderPass( device, colorFormat, depthBufferData.format );
vk::raii::su::makeUniqueRenderPass( *device, colorFormat, depthBufferData.format );
glslang::InitializeProcess(); glslang::InitializeProcess();
std::unique_ptr<vk::raii::ShaderModule> vertexShaderModule = vk::raii::ShaderModule vertexShaderModule =
vk::raii::su::makeUniqueShaderModule( *device, vk::ShaderStageFlagBits::eVertex, vertexShaderText_PT_T ); vk::raii::su::makeShaderModule( device, vk::ShaderStageFlagBits::eVertex, vertexShaderText_PT_T );
std::unique_ptr<vk::raii::ShaderModule> fragmentShaderModule = vk::raii::ShaderModule fragmentShaderModule =
vk::raii::su::makeUniqueShaderModule( *device, vk::ShaderStageFlagBits::eFragment, fragmentShaderText_T_C ); vk::raii::su::makeShaderModule( device, vk::ShaderStageFlagBits::eFragment, fragmentShaderText_T_C );
glslang::FinalizeProcess(); glslang::FinalizeProcess();
std::vector<std::unique_ptr<vk::raii::Framebuffer>> framebuffers = vk::raii::su::makeUniqueFramebuffers( std::vector<vk::raii::Framebuffer> framebuffers = vk::raii::su::makeFramebuffers(
*device, *renderPass, swapChainData.imageViews, depthBufferData.imageView, surfaceData.extent ); device, renderPass, swapChainData.imageViews, &*depthBufferData.pImageView, surfaceData.extent );
vk::raii::su::BufferData vertexBufferData( vk::raii::su::BufferData vertexBufferData(
*physicalDevice, *device, sizeof( texturedCubeData ), vk::BufferUsageFlagBits::eVertexBuffer ); physicalDevice, device, sizeof( texturedCubeData ), vk::BufferUsageFlagBits::eVertexBuffer );
vk::raii::su::copyToDevice( vk::raii::su::copyToDevice(
*vertexBufferData.deviceMemory, texturedCubeData, sizeof( texturedCubeData ) / sizeof( texturedCubeData[0] ) ); vertexBufferData.deviceMemory, texturedCubeData, sizeof( texturedCubeData ) / sizeof( texturedCubeData[0] ) );
std::unique_ptr<vk::raii::DescriptorPool> descriptorPool = vk::raii::su::makeUniqueDescriptorPool( vk::raii::DescriptorPool descriptorPool = vk::raii::su::makeDescriptorPool(
*device, { { vk::DescriptorType::eUniformBuffer, 1 }, { vk::DescriptorType::eCombinedImageSampler, 1 } } ); device, { { vk::DescriptorType::eUniformBuffer, 1 }, { vk::DescriptorType::eCombinedImageSampler, 1 } } );
std::unique_ptr<vk::raii::DescriptorSet> descriptorSet = vk::raii::DescriptorSet descriptorSet =
vk::raii::su::makeUniqueDescriptorSet( *device, *descriptorPool, *descriptorSetLayout ); std::move( vk::raii::DescriptorSets( device, { *descriptorPool, *descriptorSetLayout } ).front() );
vk::raii::su::updateDescriptorSets( *device, vk::raii::su::updateDescriptorSets( device,
*descriptorSet, descriptorSet,
{ { vk::DescriptorType::eUniformBuffer, *uniformBufferData.buffer, nullptr } }, { { vk::DescriptorType::eUniformBuffer, uniformBufferData.buffer, nullptr } },
{ textureData } ); { textureData } );
/* VULKAN_KEY_START */ /* VULKAN_KEY_START */
@ -293,40 +285,38 @@ int main( int /*argc*/, char ** /*argv*/ )
} }
// Feed the initial cache data into cache creation // Feed the initial cache data into cache creation
vk::PipelineCacheCreateInfo pipelineCacheCreateInfo( {}, startCacheSize, startCacheData ); vk::PipelineCacheCreateInfo pipelineCacheCreateInfo( {}, startCacheSize, startCacheData );
std::unique_ptr<vk::raii::PipelineCache> pipelineCache = vk::raii::PipelineCache pipelineCache( device, pipelineCacheCreateInfo );
vk::raii::su::make_unique<vk::raii::PipelineCache>( *device, pipelineCacheCreateInfo );
// Free our initialData now that pipeline cache has been created // Free our initialData now that pipeline cache has been created
free( startCacheData ); free( startCacheData );
startCacheData = NULL; startCacheData = NULL;
// Time (roughly) taken to create the graphics pipeline // Time (roughly) taken to create the graphics pipeline
timestamp_t start = getMilliseconds(); timestamp_t start = getMilliseconds();
std::unique_ptr<vk::raii::Pipeline> graphicsPipeline = vk::raii::su::makeUniqueGraphicsPipeline( vk::raii::Pipeline graphicsPipeline =
*device, vk::raii::su::makeGraphicsPipeline( device,
*pipelineCache, pipelineCache,
*vertexShaderModule, vertexShaderModule,
nullptr, nullptr,
*fragmentShaderModule, fragmentShaderModule,
nullptr, nullptr,
sizeof( texturedCubeData[0] ), sizeof( texturedCubeData[0] ),
{ { vk::Format::eR32G32B32A32Sfloat, 0 }, { vk::Format::eR32G32Sfloat, 16 } }, { { vk::Format::eR32G32B32A32Sfloat, 0 }, { vk::Format::eR32G32Sfloat, 16 } },
vk::FrontFace::eClockwise, vk::FrontFace::eClockwise,
true, true,
*pipelineLayout, pipelineLayout,
*renderPass ); renderPass );
timestamp_t elapsed = getMilliseconds() - start; timestamp_t elapsed = getMilliseconds() - start;
std::cout << " vkCreateGraphicsPipeline time: " << (double)elapsed << " ms\n"; std::cout << " vkCreateGraphicsPipeline time: " << (double)elapsed << " ms\n";
std::unique_ptr<vk::raii::Semaphore> imageAcquiredSemaphore = vk::raii::Semaphore imageAcquiredSemaphore( device, vk::SemaphoreCreateInfo() );
vk::raii::su::make_unique<vk::raii::Semaphore>( *device, vk::SemaphoreCreateInfo() );
// Get the index of the next available swapchain image: // Get the index of the next available swapchain image:
vk::Result result; vk::Result result;
uint32_t imageIndex; uint32_t imageIndex;
std::tie( result, imageIndex ) = std::tie( result, imageIndex ) =
swapChainData.swapChain->acquireNextImage( vk::su::FenceTimeout, **imageAcquiredSemaphore ); swapChainData.pSwapChain->acquireNextImage( vk::su::FenceTimeout, *imageAcquiredSemaphore );
assert( result == vk::Result::eSuccess ); assert( result == vk::Result::eSuccess );
assert( imageIndex < swapChainData.images.size() ); assert( imageIndex < swapChainData.images.size() );
@ -334,38 +324,38 @@ int main( int /*argc*/, char ** /*argv*/ )
clearValues[0].color = vk::ClearColorValue( std::array<float, 4>( { { 0.2f, 0.2f, 0.2f, 0.2f } } ) ); clearValues[0].color = vk::ClearColorValue( std::array<float, 4>( { { 0.2f, 0.2f, 0.2f, 0.2f } } ) );
clearValues[1].depthStencil = vk::ClearDepthStencilValue( 1.0f, 0 ); clearValues[1].depthStencil = vk::ClearDepthStencilValue( 1.0f, 0 );
commandBuffer->beginRenderPass( commandBuffer.beginRenderPass(
vk::RenderPassBeginInfo( vk::RenderPassBeginInfo(
**renderPass, **framebuffers[imageIndex], vk::Rect2D( vk::Offset2D(), surfaceData.extent ), clearValues ), *renderPass, *framebuffers[imageIndex], vk::Rect2D( vk::Offset2D(), surfaceData.extent ), clearValues ),
vk::SubpassContents::eInline ); vk::SubpassContents::eInline );
commandBuffer->bindPipeline( vk::PipelineBindPoint::eGraphics, **graphicsPipeline ); commandBuffer.bindPipeline( vk::PipelineBindPoint::eGraphics, *graphicsPipeline );
commandBuffer->bindDescriptorSets( vk::PipelineBindPoint::eGraphics, **pipelineLayout, 0, { **descriptorSet }, {} ); commandBuffer.bindDescriptorSets( vk::PipelineBindPoint::eGraphics, *pipelineLayout, 0, { *descriptorSet }, {} );
commandBuffer->bindVertexBuffers( 0, { **vertexBufferData.buffer }, { 0 } ); commandBuffer.bindVertexBuffers( 0, { *vertexBufferData.buffer }, { 0 } );
commandBuffer->setViewport( 0, commandBuffer.setViewport( 0,
vk::Viewport( 0.0f, vk::Viewport( 0.0f,
0.0f, 0.0f,
static_cast<float>( surfaceData.extent.width ), static_cast<float>( surfaceData.extent.width ),
static_cast<float>( surfaceData.extent.height ), static_cast<float>( surfaceData.extent.height ),
0.0f, 0.0f,
1.0f ) ); 1.0f ) );
commandBuffer->setScissor( 0, vk::Rect2D( vk::Offset2D( 0, 0 ), surfaceData.extent ) ); commandBuffer.setScissor( 0, vk::Rect2D( vk::Offset2D( 0, 0 ), surfaceData.extent ) );
commandBuffer->draw( 12 * 3, 1, 0, 0 ); commandBuffer.draw( 12 * 3, 1, 0, 0 );
commandBuffer->endRenderPass(); commandBuffer.endRenderPass();
commandBuffer->end(); commandBuffer.end();
std::unique_ptr<vk::raii::Fence> drawFence = vk::raii::su::make_unique<vk::raii::Fence>( *device, vk::FenceCreateInfo() ); vk::raii::Fence drawFence( device, vk::FenceCreateInfo() );
vk::PipelineStageFlags waitDestinationStageMask( vk::PipelineStageFlagBits::eColorAttachmentOutput ); vk::PipelineStageFlags waitDestinationStageMask( vk::PipelineStageFlagBits::eColorAttachmentOutput );
vk::SubmitInfo submitInfo( **imageAcquiredSemaphore, waitDestinationStageMask, **commandBuffer ); vk::SubmitInfo submitInfo( *imageAcquiredSemaphore, waitDestinationStageMask, *commandBuffer );
graphicsQueue->submit( submitInfo, **drawFence ); graphicsQueue.submit( submitInfo, *drawFence );
while ( vk::Result::eTimeout == device->waitForFences( { **drawFence }, VK_TRUE, vk::su::FenceTimeout ) ) while ( vk::Result::eTimeout == device.waitForFences( { *drawFence }, VK_TRUE, vk::su::FenceTimeout ) )
; ;
vk::PresentInfoKHR presentInfoKHR( nullptr, **swapChainData.swapChain, imageIndex ); vk::PresentInfoKHR presentInfoKHR( nullptr, **swapChainData.pSwapChain, imageIndex );
result = presentQueue->presentKHR( presentInfoKHR ); result = presentQueue.presentKHR( presentInfoKHR );
switch ( result ) switch ( result )
{ {
case vk::Result::eSuccess: break; case vk::Result::eSuccess: break;
@ -379,7 +369,7 @@ int main( int /*argc*/, char ** /*argv*/ )
// Store away the cache that we've populated. This could conceivably happen // Store away the cache that we've populated. This could conceivably happen
// earlier, depends on when the pipeline cache stops being populated // earlier, depends on when the pipeline cache stops being populated
// internally. // internally.
std::vector<uint8_t> endCacheData = pipelineCache->getData(); std::vector<uint8_t> endCacheData = pipelineCache.getData();
// Write the file to disk, overwriting whatever was there // Write the file to disk, overwriting whatever was there
std::ofstream writeCacheStream( cacheFileName, std::ios_base::out | std::ios_base::binary ); std::ofstream writeCacheStream( cacheFileName, std::ios_base::out | std::ios_base::binary );

View File

@ -40,35 +40,31 @@ int main( int /*argc*/, char ** /*argv*/ )
{ {
try try
{ {
std::unique_ptr<vk::raii::Context> context = vk::raii::su::make_unique<vk::raii::Context>(); vk::raii::Context context;
std::unique_ptr<vk::raii::Instance> instance = vk::raii::Instance instance =
vk::raii::su::makeUniqueInstance( *context, AppName, EngineName, {}, vk::su::getInstanceExtensions() ); vk::raii::su::makeInstance( context, AppName, EngineName, {}, vk::su::getInstanceExtensions() );
#if !defined( NDEBUG ) #if !defined( NDEBUG )
std::unique_ptr<vk::raii::DebugUtilsMessengerEXT> debugUtilsMessenger = vk::raii::DebugUtilsMessengerEXT debugUtilsMessenger( instance, vk::su::makeDebugUtilsMessengerCreateInfoEXT() );
vk::raii::su::makeUniqueDebugUtilsMessengerEXT( *instance );
#endif #endif
std::unique_ptr<vk::raii::PhysicalDevice> physicalDevice = vk::raii::su::makeUniquePhysicalDevice( *instance ); vk::raii::PhysicalDevice physicalDevice = std::move( vk::raii::PhysicalDevices( instance ).front() );
vk::raii::su::SurfaceData surfaceData( *instance, AppName, vk::Extent2D( 500, 500 ) ); vk::raii::su::SurfaceData surfaceData( instance, AppName, vk::Extent2D( 500, 500 ) );
std::pair<uint32_t, uint32_t> graphicsAndPresentQueueFamilyIndex = std::pair<uint32_t, uint32_t> graphicsAndPresentQueueFamilyIndex =
vk::raii::su::findGraphicsAndPresentQueueFamilyIndex( *physicalDevice, *surfaceData.surface ); vk::raii::su::findGraphicsAndPresentQueueFamilyIndex( physicalDevice, *surfaceData.pSurface );
std::unique_ptr<vk::raii::Device> device = vk::raii::su::makeUniqueDevice( vk::raii::Device device = vk::raii::su::makeDevice(
*physicalDevice, graphicsAndPresentQueueFamilyIndex.first, vk::su::getDeviceExtensions() ); physicalDevice, graphicsAndPresentQueueFamilyIndex.first, vk::su::getDeviceExtensions() );
std::unique_ptr<vk::raii::CommandPool> commandPool = vk::raii::CommandPool commandPool = vk::raii::CommandPool(
vk::raii::su::makeUniqueCommandPool( *device, graphicsAndPresentQueueFamilyIndex.first ); device, { vk::CommandPoolCreateFlagBits::eResetCommandBuffer, graphicsAndPresentQueueFamilyIndex.first } );
std::unique_ptr<vk::raii::CommandBuffer> commandBuffer = vk::raii::CommandBuffer commandBuffer = vk::raii::su::makeCommandBuffer( device, commandPool );
vk::raii::su::makeUniqueCommandBuffer( *device, *commandPool );
std::unique_ptr<vk::raii::Queue> graphicsQueue = vk::raii::Queue graphicsQueue( device, graphicsAndPresentQueueFamilyIndex.first, 0 );
vk::raii::su::make_unique<vk::raii::Queue>( *device, graphicsAndPresentQueueFamilyIndex.first, 0 ); vk::raii::Queue presentQueue( device, graphicsAndPresentQueueFamilyIndex.second, 0 );
std::unique_ptr<vk::raii::Queue> presentQueue =
vk::raii::su::make_unique<vk::raii::Queue>( *device, graphicsAndPresentQueueFamilyIndex.second, 0 );
vk::raii::su::SwapChainData swapChainData( *physicalDevice, vk::raii::su::SwapChainData swapChainData( physicalDevice,
*device, device,
*surfaceData.surface, *surfaceData.pSurface,
surfaceData.extent, surfaceData.extent,
vk::ImageUsageFlagBits::eColorAttachment | vk::ImageUsageFlagBits::eColorAttachment |
vk::ImageUsageFlagBits::eTransferSrc, vk::ImageUsageFlagBits::eTransferSrc,
@ -76,58 +72,54 @@ int main( int /*argc*/, char ** /*argv*/ )
graphicsAndPresentQueueFamilyIndex.first, graphicsAndPresentQueueFamilyIndex.first,
graphicsAndPresentQueueFamilyIndex.second ); graphicsAndPresentQueueFamilyIndex.second );
vk::raii::su::DepthBufferData depthBufferData( vk::raii::su::DepthBufferData depthBufferData( physicalDevice, device, vk::Format::eD16Unorm, surfaceData.extent );
*physicalDevice, *device, vk::Format::eD16Unorm, surfaceData.extent );
vk::raii::su::TextureData textureData( *physicalDevice, *device ); vk::raii::su::TextureData textureData( physicalDevice, device );
commandBuffer->begin( vk::CommandBufferBeginInfo() ); commandBuffer.begin( vk::CommandBufferBeginInfo() );
textureData.setImage( *commandBuffer, vk::su::CheckerboardImageGenerator() ); textureData.setImage( commandBuffer, vk::su::CheckerboardImageGenerator() );
vk::raii::su::BufferData uniformBufferData( vk::raii::su::BufferData uniformBufferData(
*physicalDevice, *device, sizeof( glm::mat4x4 ), vk::BufferUsageFlagBits::eUniformBuffer ); physicalDevice, device, sizeof( glm::mat4x4 ), vk::BufferUsageFlagBits::eUniformBuffer );
glm::mat4x4 mvpcMatrix = vk::su::createModelViewProjectionClipMatrix( surfaceData.extent ); glm::mat4x4 mvpcMatrix = vk::su::createModelViewProjectionClipMatrix( surfaceData.extent );
vk::raii::su::copyToDevice( *uniformBufferData.deviceMemory, mvpcMatrix ); vk::raii::su::copyToDevice( uniformBufferData.deviceMemory, mvpcMatrix );
std::unique_ptr<vk::raii::DescriptorSetLayout> descriptorSetLayout = vk::raii::su::makeUniqueDescriptorSetLayout( vk::raii::DescriptorSetLayout descriptorSetLayout = vk::raii::su::makeDescriptorSetLayout(
*device, device,
{ { vk::DescriptorType::eUniformBuffer, 1, vk::ShaderStageFlagBits::eVertex }, { { vk::DescriptorType::eUniformBuffer, 1, vk::ShaderStageFlagBits::eVertex },
{ vk::DescriptorType::eCombinedImageSampler, 1, vk::ShaderStageFlagBits::eFragment } } ); { vk::DescriptorType::eCombinedImageSampler, 1, vk::ShaderStageFlagBits::eFragment } } );
std::unique_ptr<vk::raii::PipelineLayout> pipelineLayout = vk::raii::PipelineLayout pipelineLayout( device, { {}, *descriptorSetLayout } );
vk::raii::su::makeUniquePipelineLayout( *device, *descriptorSetLayout );
vk::Format colorFormat = vk::Format colorFormat =
vk::su::pickSurfaceFormat( physicalDevice->getSurfaceFormatsKHR( **surfaceData.surface ) ).format; vk::su::pickSurfaceFormat( physicalDevice.getSurfaceFormatsKHR( **surfaceData.pSurface ) ).format;
std::unique_ptr<vk::raii::RenderPass> renderPass = vk::raii::RenderPass renderPass = vk::raii::su::makeRenderPass( device, colorFormat, depthBufferData.format );
vk::raii::su::makeUniqueRenderPass( *device, colorFormat, depthBufferData.format );
glslang::InitializeProcess(); glslang::InitializeProcess();
std::unique_ptr<vk::raii::ShaderModule> vertexShaderModule = vk::raii::ShaderModule vertexShaderModule =
vk::raii::su::makeUniqueShaderModule( *device, vk::ShaderStageFlagBits::eVertex, vertexShaderText_PT_T ); vk::raii::su::makeShaderModule( device, vk::ShaderStageFlagBits::eVertex, vertexShaderText_PT_T );
std::unique_ptr<vk::raii::ShaderModule> fragmentShaderModule = vk::raii::ShaderModule fragmentShaderModule =
vk::raii::su::makeUniqueShaderModule( *device, vk::ShaderStageFlagBits::eFragment, fragmentShaderText_T_C ); vk::raii::su::makeShaderModule( device, vk::ShaderStageFlagBits::eFragment, fragmentShaderText_T_C );
glslang::FinalizeProcess(); glslang::FinalizeProcess();
std::vector<std::unique_ptr<vk::raii::Framebuffer>> framebuffers = vk::raii::su::makeUniqueFramebuffers( std::vector<vk::raii::Framebuffer> framebuffers = vk::raii::su::makeFramebuffers(
*device, *renderPass, swapChainData.imageViews, depthBufferData.imageView, surfaceData.extent ); device, renderPass, swapChainData.imageViews, &*depthBufferData.pImageView, surfaceData.extent );
vk::raii::su::BufferData vertexBufferData( vk::raii::su::BufferData vertexBufferData(
*physicalDevice, *device, sizeof( texturedCubeData ), vk::BufferUsageFlagBits::eVertexBuffer ); physicalDevice, device, sizeof( texturedCubeData ), vk::BufferUsageFlagBits::eVertexBuffer );
vk::raii::su::copyToDevice( vk::raii::su::copyToDevice(
*vertexBufferData.deviceMemory, texturedCubeData, sizeof( texturedCubeData ) / sizeof( texturedCubeData[0] ) ); vertexBufferData.deviceMemory, texturedCubeData, sizeof( texturedCubeData ) / sizeof( texturedCubeData[0] ) );
std::unique_ptr<vk::raii::DescriptorPool> descriptorPool = vk::raii::su::makeUniqueDescriptorPool( vk::raii::DescriptorPool descriptorPool = vk::raii::su::makeDescriptorPool(
*device, { { vk::DescriptorType::eUniformBuffer, 1 }, { vk::DescriptorType::eCombinedImageSampler, 1 } } ); device, { { vk::DescriptorType::eUniformBuffer, 1 }, { vk::DescriptorType::eCombinedImageSampler, 1 } } );
std::unique_ptr<vk::raii::DescriptorSet> descriptorSet = vk::raii::DescriptorSet descriptorSet =
vk::raii::su::makeUniqueDescriptorSet( *device, *descriptorPool, *descriptorSetLayout ); std::move( vk::raii::DescriptorSets( device, { *descriptorPool, *descriptorSetLayout } ).front() );
vk::raii::su::updateDescriptorSets( *device, vk::raii::su::updateDescriptorSets( device,
*descriptorSet, descriptorSet,
{ { vk::DescriptorType::eUniformBuffer, *uniformBufferData.buffer, nullptr } }, { { vk::DescriptorType::eUniformBuffer, uniformBufferData.buffer, nullptr } },
{ textureData } ); { textureData } );
std::unique_ptr<vk::raii::PipelineCache> pipelineCache = vk::raii::PipelineCache pipelineCache( device, vk::PipelineCacheCreateInfo() );
vk::raii::su::make_unique<vk::raii::PipelineCache>( *device, vk::PipelineCacheCreateInfo() );
/* VULKAN_KEY_START */ /* VULKAN_KEY_START */
@ -137,8 +129,8 @@ int main( int /*argc*/, char ** /*argv*/ )
// Second pipeline has a modified fragment shader and sets the VK_PIPELINE_CREATE_DERIVATIVE_BIT flag. // Second pipeline has a modified fragment shader and sets the VK_PIPELINE_CREATE_DERIVATIVE_BIT flag.
std::array<vk::PipelineShaderStageCreateInfo, 2> pipelineShaderStageCreateInfos = { std::array<vk::PipelineShaderStageCreateInfo, 2> pipelineShaderStageCreateInfos = {
vk::PipelineShaderStageCreateInfo( {}, vk::ShaderStageFlagBits::eVertex, **vertexShaderModule, "main" ), vk::PipelineShaderStageCreateInfo( {}, vk::ShaderStageFlagBits::eVertex, *vertexShaderModule, "main" ),
vk::PipelineShaderStageCreateInfo( {}, vk::ShaderStageFlagBits::eFragment, **fragmentShaderModule, "main" ) vk::PipelineShaderStageCreateInfo( {}, vk::ShaderStageFlagBits::eFragment, *fragmentShaderModule, "main" )
}; };
vk::VertexInputBindingDescription vertexInputBindingDescription( 0, sizeof( texturedCubeData[0] ) ); vk::VertexInputBindingDescription vertexInputBindingDescription( 0, sizeof( texturedCubeData[0] ) );
@ -200,12 +192,11 @@ int main( int /*argc*/, char ** /*argv*/ )
&pipelineDepthStencilStateCreateInfo, &pipelineDepthStencilStateCreateInfo,
&pipelineColorBlendStateCreateInfo, &pipelineColorBlendStateCreateInfo,
&pipelineDynamicStateCreateInfo, &pipelineDynamicStateCreateInfo,
**pipelineLayout, *pipelineLayout,
**renderPass ); *renderPass );
std::unique_ptr<vk::raii::Pipeline> basePipeline = vk::raii::Pipeline basePipeline( device, pipelineCache, graphicsPipelineCreateInfo );
vk::raii::su::make_unique<vk::raii::Pipeline>( *device, *pipelineCache, graphicsPipelineCreateInfo ); switch ( basePipeline.getConstructorSuccessCode() )
switch ( basePipeline->getConstructorSuccessCode() )
{ {
case vk::Result::eSuccess: break; case vk::Result::eSuccess: break;
case vk::Result::ePipelineCompileRequiredEXT: case vk::Result::ePipelineCompileRequiredEXT:
@ -231,21 +222,20 @@ void main()
// Convert GLSL to SPIR-V // Convert GLSL to SPIR-V
glslang::InitializeProcess(); glslang::InitializeProcess();
std::unique_ptr<vk::raii::ShaderModule> fragmentShaderModule2 = vk::raii::ShaderModule fragmentShaderModule2 =
vk::raii::su::makeUniqueShaderModule( *device, vk::ShaderStageFlagBits::eFragment, fragmentShaderText_T_C_2 ); vk::raii::su::makeShaderModule( device, vk::ShaderStageFlagBits::eFragment, fragmentShaderText_T_C_2 );
glslang::FinalizeProcess(); glslang::FinalizeProcess();
// Modify pipeline info to reflect derivation // Modify pipeline info to reflect derivation
pipelineShaderStageCreateInfos[1] = pipelineShaderStageCreateInfos[1] =
vk::PipelineShaderStageCreateInfo( {}, vk::ShaderStageFlagBits::eFragment, **fragmentShaderModule2, "main" ); vk::PipelineShaderStageCreateInfo( {}, vk::ShaderStageFlagBits::eFragment, *fragmentShaderModule2, "main" );
graphicsPipelineCreateInfo.flags = vk::PipelineCreateFlagBits::eDerivative; graphicsPipelineCreateInfo.flags = vk::PipelineCreateFlagBits::eDerivative;
graphicsPipelineCreateInfo.basePipelineHandle = **basePipeline; graphicsPipelineCreateInfo.basePipelineHandle = *basePipeline;
graphicsPipelineCreateInfo.basePipelineIndex = -1; graphicsPipelineCreateInfo.basePipelineIndex = -1;
// And create the derived pipeline // And create the derived pipeline
std::unique_ptr<vk::raii::Pipeline> derivedPipeline = vk::raii::Pipeline derivedPipeline( device, pipelineCache, graphicsPipelineCreateInfo );
vk::raii::su::make_unique<vk::raii::Pipeline>( *device, *pipelineCache, graphicsPipelineCreateInfo ); switch ( derivedPipeline.getConstructorSuccessCode() )
switch ( derivedPipeline->getConstructorSuccessCode() )
{ {
case vk::Result::eSuccess: break; case vk::Result::eSuccess: break;
case vk::Result::ePipelineCompileRequiredEXT: case vk::Result::ePipelineCompileRequiredEXT:
@ -256,14 +246,13 @@ void main()
/* VULKAN_KEY_END */ /* VULKAN_KEY_END */
std::unique_ptr<vk::raii::Semaphore> imageAcquiredSemaphore = vk::raii::Semaphore imageAcquiredSemaphore( device, vk::SemaphoreCreateInfo() );
vk::raii::su::make_unique<vk::raii::Semaphore>( *device, vk::SemaphoreCreateInfo() );
// Get the index of the next available swapchain image // Get the index of the next available swapchain image
vk::Result result; vk::Result result;
uint32_t imageIndex; uint32_t imageIndex;
std::tie( result, imageIndex ) = std::tie( result, imageIndex ) =
swapChainData.swapChain->acquireNextImage( vk::su::FenceTimeout, **imageAcquiredSemaphore ); swapChainData.pSwapChain->acquireNextImage( vk::su::FenceTimeout, *imageAcquiredSemaphore );
assert( result == vk::Result::eSuccess ); assert( result == vk::Result::eSuccess );
assert( imageIndex < swapChainData.images.size() ); assert( imageIndex < swapChainData.images.size() );
@ -271,38 +260,38 @@ void main()
clearValues[0].color = vk::ClearColorValue( std::array<float, 4>( { { 0.2f, 0.2f, 0.2f, 0.2f } } ) ); clearValues[0].color = vk::ClearColorValue( std::array<float, 4>( { { 0.2f, 0.2f, 0.2f, 0.2f } } ) );
clearValues[1].depthStencil = vk::ClearDepthStencilValue( 1.0f, 0 ); clearValues[1].depthStencil = vk::ClearDepthStencilValue( 1.0f, 0 );
commandBuffer->beginRenderPass( commandBuffer.beginRenderPass(
vk::RenderPassBeginInfo( vk::RenderPassBeginInfo(
**renderPass, **framebuffers[imageIndex], vk::Rect2D( vk::Offset2D(), surfaceData.extent ), clearValues ), *renderPass, *framebuffers[imageIndex], vk::Rect2D( vk::Offset2D(), surfaceData.extent ), clearValues ),
vk::SubpassContents::eInline ); vk::SubpassContents::eInline );
commandBuffer->bindPipeline( vk::PipelineBindPoint::eGraphics, **derivedPipeline ); commandBuffer.bindPipeline( vk::PipelineBindPoint::eGraphics, *derivedPipeline );
commandBuffer->bindDescriptorSets( vk::PipelineBindPoint::eGraphics, **pipelineLayout, 0, { **descriptorSet }, {} ); commandBuffer.bindDescriptorSets( vk::PipelineBindPoint::eGraphics, *pipelineLayout, 0, { *descriptorSet }, {} );
commandBuffer->bindVertexBuffers( 0, { **vertexBufferData.buffer }, { 0 } ); commandBuffer.bindVertexBuffers( 0, { *vertexBufferData.buffer }, { 0 } );
commandBuffer->setViewport( 0, commandBuffer.setViewport( 0,
vk::Viewport( 0.0f, vk::Viewport( 0.0f,
0.0f, 0.0f,
static_cast<float>( surfaceData.extent.width ), static_cast<float>( surfaceData.extent.width ),
static_cast<float>( surfaceData.extent.height ), static_cast<float>( surfaceData.extent.height ),
0.0f, 0.0f,
1.0f ) ); 1.0f ) );
commandBuffer->setScissor( 0, vk::Rect2D( vk::Offset2D( 0, 0 ), surfaceData.extent ) ); commandBuffer.setScissor( 0, vk::Rect2D( vk::Offset2D( 0, 0 ), surfaceData.extent ) );
commandBuffer->draw( 12 * 3, 1, 0, 0 ); commandBuffer.draw( 12 * 3, 1, 0, 0 );
commandBuffer->endRenderPass(); commandBuffer.endRenderPass();
commandBuffer->end(); commandBuffer.end();
std::unique_ptr<vk::raii::Fence> drawFence = vk::raii::su::make_unique<vk::raii::Fence>( *device, vk::FenceCreateInfo() ); vk::raii::Fence drawFence( device, vk::FenceCreateInfo() );
vk::PipelineStageFlags waitDestinationStageMask( vk::PipelineStageFlagBits::eColorAttachmentOutput ); vk::PipelineStageFlags waitDestinationStageMask( vk::PipelineStageFlagBits::eColorAttachmentOutput );
vk::SubmitInfo submitInfo( **imageAcquiredSemaphore, waitDestinationStageMask, **commandBuffer ); vk::SubmitInfo submitInfo( *imageAcquiredSemaphore, waitDestinationStageMask, *commandBuffer );
graphicsQueue->submit( submitInfo, **drawFence ); graphicsQueue.submit( submitInfo, *drawFence );
while ( vk::Result::eTimeout == device->waitForFences( { **drawFence }, VK_TRUE, vk::su::FenceTimeout ) ) while ( vk::Result::eTimeout == device.waitForFences( { *drawFence }, VK_TRUE, vk::su::FenceTimeout ) )
; ;
vk::PresentInfoKHR presentInfoKHR( nullptr, **swapChainData.swapChain, imageIndex ); vk::PresentInfoKHR presentInfoKHR( nullptr, **swapChainData.pSwapChain, imageIndex );
result = presentQueue->presentKHR( presentInfoKHR ); result = presentQueue.presentKHR( presentInfoKHR );
switch ( result ) switch ( result )
{ {
case vk::Result::eSuccess: break; case vk::Result::eSuccess: break;

View File

@ -82,35 +82,31 @@ int main( int /*argc*/, char ** /*argv*/ )
{ {
try try
{ {
std::unique_ptr<vk::raii::Context> context = vk::raii::su::make_unique<vk::raii::Context>(); vk::raii::Context context;
std::unique_ptr<vk::raii::Instance> instance = vk::raii::Instance instance =
vk::raii::su::makeUniqueInstance( *context, AppName, EngineName, {}, vk::su::getInstanceExtensions() ); vk::raii::su::makeInstance( context, AppName, EngineName, {}, vk::su::getInstanceExtensions() );
#if !defined( NDEBUG ) #if !defined( NDEBUG )
std::unique_ptr<vk::raii::DebugUtilsMessengerEXT> debugUtilsMessenger = vk::raii::DebugUtilsMessengerEXT debugUtilsMessenger( instance, vk::su::makeDebugUtilsMessengerCreateInfoEXT() );
vk::raii::su::makeUniqueDebugUtilsMessengerEXT( *instance );
#endif #endif
std::unique_ptr<vk::raii::PhysicalDevice> physicalDevice = vk::raii::su::makeUniquePhysicalDevice( *instance ); vk::raii::PhysicalDevice physicalDevice = std::move( vk::raii::PhysicalDevices( instance ).front() );
vk::raii::su::SurfaceData surfaceData( *instance, AppName, vk::Extent2D( 500, 500 ) ); vk::raii::su::SurfaceData surfaceData( instance, AppName, vk::Extent2D( 500, 500 ) );
std::pair<uint32_t, uint32_t> graphicsAndPresentQueueFamilyIndex = std::pair<uint32_t, uint32_t> graphicsAndPresentQueueFamilyIndex =
vk::raii::su::findGraphicsAndPresentQueueFamilyIndex( *physicalDevice, *surfaceData.surface ); vk::raii::su::findGraphicsAndPresentQueueFamilyIndex( physicalDevice, *surfaceData.pSurface );
std::unique_ptr<vk::raii::Device> device = vk::raii::su::makeUniqueDevice( vk::raii::Device device = vk::raii::su::makeDevice(
*physicalDevice, graphicsAndPresentQueueFamilyIndex.first, vk::su::getDeviceExtensions() ); physicalDevice, graphicsAndPresentQueueFamilyIndex.first, vk::su::getDeviceExtensions() );
std::unique_ptr<vk::raii::CommandPool> commandPool = vk::raii::CommandPool commandPool = vk::raii::CommandPool(
vk::raii::su::makeUniqueCommandPool( *device, graphicsAndPresentQueueFamilyIndex.first ); device, { vk::CommandPoolCreateFlagBits::eResetCommandBuffer, graphicsAndPresentQueueFamilyIndex.first } );
std::unique_ptr<vk::raii::CommandBuffer> commandBuffer = vk::raii::CommandBuffer commandBuffer = vk::raii::su::makeCommandBuffer( device, commandPool );
vk::raii::su::makeUniqueCommandBuffer( *device, *commandPool );
std::unique_ptr<vk::raii::Queue> graphicsQueue = vk::raii::Queue graphicsQueue( device, graphicsAndPresentQueueFamilyIndex.first, 0 );
vk::raii::su::make_unique<vk::raii::Queue>( *device, graphicsAndPresentQueueFamilyIndex.first, 0 ); vk::raii::Queue presentQueue( device, graphicsAndPresentQueueFamilyIndex.second, 0 );
std::unique_ptr<vk::raii::Queue> presentQueue =
vk::raii::su::make_unique<vk::raii::Queue>( *device, graphicsAndPresentQueueFamilyIndex.second, 0 );
vk::raii::su::SwapChainData swapChainData( *physicalDevice, vk::raii::su::SwapChainData swapChainData( physicalDevice,
*device, device,
*surfaceData.surface, *surfaceData.pSurface,
surfaceData.extent, surfaceData.extent,
vk::ImageUsageFlagBits::eColorAttachment | vk::ImageUsageFlagBits::eColorAttachment |
vk::ImageUsageFlagBits::eTransferSrc, vk::ImageUsageFlagBits::eTransferSrc,
@ -118,46 +114,43 @@ int main( int /*argc*/, char ** /*argv*/ )
graphicsAndPresentQueueFamilyIndex.first, graphicsAndPresentQueueFamilyIndex.first,
graphicsAndPresentQueueFamilyIndex.second ); graphicsAndPresentQueueFamilyIndex.second );
vk::raii::su::DepthBufferData depthBufferData( vk::raii::su::DepthBufferData depthBufferData( physicalDevice, device, vk::Format::eD16Unorm, surfaceData.extent );
*physicalDevice, *device, vk::Format::eD16Unorm, surfaceData.extent );
vk::raii::su::BufferData uniformBufferData( vk::raii::su::BufferData uniformBufferData(
*physicalDevice, *device, sizeof( glm::mat4x4 ), vk::BufferUsageFlagBits::eUniformBuffer ); physicalDevice, device, sizeof( glm::mat4x4 ), vk::BufferUsageFlagBits::eUniformBuffer );
glm::mat4x4 mvpcMatrix = vk::su::createModelViewProjectionClipMatrix( surfaceData.extent ); glm::mat4x4 mvpcMatrix = vk::su::createModelViewProjectionClipMatrix( surfaceData.extent );
vk::raii::su::copyToDevice( *uniformBufferData.deviceMemory, mvpcMatrix ); vk::raii::su::copyToDevice( uniformBufferData.deviceMemory, mvpcMatrix );
vk::Format colorFormat = vk::Format colorFormat =
vk::su::pickSurfaceFormat( physicalDevice->getSurfaceFormatsKHR( **surfaceData.surface ) ).format; vk::su::pickSurfaceFormat( physicalDevice.getSurfaceFormatsKHR( **surfaceData.pSurface ) ).format;
std::unique_ptr<vk::raii::RenderPass> renderPass = vk::raii::RenderPass renderPass = vk::raii::su::makeRenderPass( device, colorFormat, depthBufferData.format );
vk::raii::su::makeUniqueRenderPass( *device, colorFormat, depthBufferData.format );
glslang::InitializeProcess(); glslang::InitializeProcess();
std::unique_ptr<vk::raii::ShaderModule> vertexShaderModule = vk::raii::ShaderModule vertexShaderModule =
vk::raii::su::makeUniqueShaderModule( *device, vk::ShaderStageFlagBits::eVertex, vertexShaderText_PT_T ); vk::raii::su::makeShaderModule( device, vk::ShaderStageFlagBits::eVertex, vertexShaderText_PT_T );
std::unique_ptr<vk::raii::ShaderModule> fragmentShaderModule = vk::raii::ShaderModule fragmentShaderModule =
vk::raii::su::makeUniqueShaderModule( *device, vk::ShaderStageFlagBits::eFragment, fragmentShaderText ); vk::raii::su::makeShaderModule( device, vk::ShaderStageFlagBits::eFragment, fragmentShaderText );
glslang::FinalizeProcess(); glslang::FinalizeProcess();
std::vector<std::unique_ptr<vk::raii::Framebuffer>> framebuffers = vk::raii::su::makeUniqueFramebuffers( std::vector<vk::raii::Framebuffer> framebuffers = vk::raii::su::makeFramebuffers(
*device, *renderPass, swapChainData.imageViews, depthBufferData.imageView, surfaceData.extent ); device, renderPass, swapChainData.imageViews, &*depthBufferData.pImageView, surfaceData.extent );
vk::raii::su::BufferData vertexBufferData( vk::raii::su::BufferData vertexBufferData(
*physicalDevice, *device, sizeof( texturedCubeData ), vk::BufferUsageFlagBits::eVertexBuffer ); physicalDevice, device, sizeof( texturedCubeData ), vk::BufferUsageFlagBits::eVertexBuffer );
vk::raii::su::copyToDevice( vk::raii::su::copyToDevice(
*vertexBufferData.deviceMemory, texturedCubeData, sizeof( texturedCubeData ) / sizeof( texturedCubeData[0] ) ); vertexBufferData.deviceMemory, texturedCubeData, sizeof( texturedCubeData ) / sizeof( texturedCubeData[0] ) );
// Create binding and layout for the following, matching contents of shader // Create binding and layout for the following, matching contents of shader
// binding 0 = uniform buffer (MVP) // binding 0 = uniform buffer (MVP)
std::unique_ptr<vk::raii::DescriptorSetLayout> descriptorSetLayout = vk::raii::su::makeUniqueDescriptorSetLayout( vk::raii::DescriptorSetLayout descriptorSetLayout = vk::raii::su::makeDescriptorSetLayout(
*device, { { vk::DescriptorType::eUniformBuffer, 1, vk::ShaderStageFlagBits::eVertex } } ); device, { { vk::DescriptorType::eUniformBuffer, 1, vk::ShaderStageFlagBits::eVertex } } );
/* VULKAN_KEY_START */ /* VULKAN_KEY_START */
// Set up our push constant range, which mirrors the declaration of // Set up our push constant range, which mirrors the declaration of
vk::PushConstantRange pushConstantRanges( vk::ShaderStageFlagBits::eFragment, 0, 8 ); vk::PushConstantRange pushConstantRanges( vk::ShaderStageFlagBits::eFragment, 0, 8 );
vk::PipelineLayoutCreateInfo pipelineLayoutCreateInfo( {}, **descriptorSetLayout, pushConstantRanges ); vk::PipelineLayoutCreateInfo pipelineLayoutCreateInfo( {}, *descriptorSetLayout, pushConstantRanges );
std::unique_ptr<vk::raii::PipelineLayout> pipelineLayout = vk::raii::PipelineLayout pipelineLayout( device, pipelineLayoutCreateInfo );
vk::raii::su::make_unique<vk::raii::PipelineLayout>( *device, pipelineLayoutCreateInfo );
// Create a single pool to contain data for our descriptor set // Create a single pool to contain data for our descriptor set
std::array<vk::DescriptorPoolSize, 2> poolSizes = { vk::DescriptorPoolSize( vk::DescriptorType::eUniformBuffer, 1 ), std::array<vk::DescriptorPoolSize, 2> poolSizes = { vk::DescriptorPoolSize( vk::DescriptorType::eUniformBuffer, 1 ),
@ -165,53 +158,50 @@ int main( int /*argc*/, char ** /*argv*/ )
vk::DescriptorType::eCombinedImageSampler, 1 ) }; vk::DescriptorType::eCombinedImageSampler, 1 ) };
vk::DescriptorPoolCreateInfo descriptorPoolCreateInfo( vk::DescriptorPoolCreateInfo descriptorPoolCreateInfo(
vk::DescriptorPoolCreateFlagBits::eFreeDescriptorSet, 1, poolSizes ); vk::DescriptorPoolCreateFlagBits::eFreeDescriptorSet, 1, poolSizes );
std::unique_ptr<vk::raii::DescriptorPool> descriptorPool = vk::raii::DescriptorPool descriptorPool( device, descriptorPoolCreateInfo );
vk::raii::su::make_unique<vk::raii::DescriptorPool>( *device, descriptorPoolCreateInfo );
// Populate descriptor sets // Populate descriptor sets
vk::DescriptorSetAllocateInfo descriptorSetAllocateInfo( **descriptorPool, **descriptorSetLayout ); vk::DescriptorSetAllocateInfo descriptorSetAllocateInfo( *descriptorPool, *descriptorSetLayout );
std::unique_ptr<vk::raii::DescriptorSet> descriptorSet = vk::raii::su::make_unique<vk::raii::DescriptorSet>( vk::raii::DescriptorSet descriptorSet =
std::move( vk::raii::DescriptorSets( *device, descriptorSetAllocateInfo ).front() ) ); std::move( vk::raii::DescriptorSets( device, descriptorSetAllocateInfo ).front() );
// Populate with info about our uniform buffer for MVP // Populate with info about our uniform buffer for MVP
vk::DescriptorBufferInfo bufferInfo( **uniformBufferData.buffer, 0, sizeof( glm::mat4x4 ) ); vk::DescriptorBufferInfo bufferInfo( *uniformBufferData.buffer, 0, sizeof( glm::mat4x4 ) );
vk::WriteDescriptorSet writeDescriptorSet( vk::WriteDescriptorSet writeDescriptorSet(
**descriptorSet, 0, 0, vk::DescriptorType::eUniformBuffer, {}, bufferInfo ); *descriptorSet, 0, 0, vk::DescriptorType::eUniformBuffer, {}, bufferInfo );
device->updateDescriptorSets( writeDescriptorSet, nullptr ); device.updateDescriptorSets( writeDescriptorSet, nullptr );
// Create our push constant data, which matches shader expectations // Create our push constant data, which matches shader expectations
std::array<unsigned, 2> pushConstants = { { (unsigned)2, (unsigned)0x3F800000 } }; std::array<unsigned, 2> pushConstants = { { (unsigned)2, (unsigned)0x3F800000 } };
// Ensure we have enough room for push constant data // Ensure we have enough room for push constant data
assert( ( sizeof( pushConstants ) <= physicalDevice->getProperties().limits.maxPushConstantsSize ) && assert( ( sizeof( pushConstants ) <= physicalDevice.getProperties().limits.maxPushConstantsSize ) &&
"Too many push constants" ); "Too many push constants" );
commandBuffer->begin( vk::CommandBufferBeginInfo() ); commandBuffer.begin( vk::CommandBufferBeginInfo() );
commandBuffer->pushConstants<unsigned>( **pipelineLayout, vk::ShaderStageFlagBits::eFragment, 0, pushConstants ); commandBuffer.pushConstants<unsigned>( *pipelineLayout, vk::ShaderStageFlagBits::eFragment, 0, pushConstants );
/* VULKAN_KEY_END */ /* VULKAN_KEY_END */
std::unique_ptr<vk::raii::PipelineCache> pipelineCache = vk::raii::PipelineCache pipelineCache( device, vk::PipelineCacheCreateInfo() );
vk::raii::su::make_unique<vk::raii::PipelineCache>( *device, vk::PipelineCacheCreateInfo() ); vk::raii::Pipeline graphicsPipeline =
std::unique_ptr<vk::raii::Pipeline> graphicsPipeline = vk::raii::su::makeUniqueGraphicsPipeline( vk::raii::su::makeGraphicsPipeline( device,
*device, pipelineCache,
*pipelineCache, vertexShaderModule,
*vertexShaderModule, nullptr,
nullptr, fragmentShaderModule,
*fragmentShaderModule, nullptr,
nullptr, sizeof( texturedCubeData[0] ),
sizeof( texturedCubeData[0] ), { { vk::Format::eR32G32B32A32Sfloat, 0 }, { vk::Format::eR32G32Sfloat, 16 } },
{ { vk::Format::eR32G32B32A32Sfloat, 0 }, { vk::Format::eR32G32Sfloat, 16 } }, vk::FrontFace::eClockwise,
vk::FrontFace::eClockwise, true,
true, pipelineLayout,
*pipelineLayout, renderPass );
*renderPass );
std::unique_ptr<vk::raii::Semaphore> imageAcquiredSemaphore = vk::raii::Semaphore imageAcquiredSemaphore( device, vk::SemaphoreCreateInfo() );
vk::raii::su::make_unique<vk::raii::Semaphore>( *device, vk::SemaphoreCreateInfo() ); vk::Result result;
vk::Result result; uint32_t imageIndex;
uint32_t imageIndex;
std::tie( result, imageIndex ) = std::tie( result, imageIndex ) =
swapChainData.swapChain->acquireNextImage( vk::su::FenceTimeout, **imageAcquiredSemaphore ); swapChainData.pSwapChain->acquireNextImage( vk::su::FenceTimeout, *imageAcquiredSemaphore );
assert( result == vk::Result::eSuccess ); assert( result == vk::Result::eSuccess );
assert( imageIndex < swapChainData.images.size() ); assert( imageIndex < swapChainData.images.size() );
@ -220,37 +210,37 @@ int main( int /*argc*/, char ** /*argv*/ )
clearValues[1].depthStencil = vk::ClearDepthStencilValue( 1.0f, 0 ); clearValues[1].depthStencil = vk::ClearDepthStencilValue( 1.0f, 0 );
vk::RenderPassBeginInfo renderPassBeginInfo( vk::RenderPassBeginInfo renderPassBeginInfo(
**renderPass, **framebuffers[imageIndex], vk::Rect2D( vk::Offset2D( 0, 0 ), surfaceData.extent ), clearValues ); *renderPass, *framebuffers[imageIndex], vk::Rect2D( vk::Offset2D( 0, 0 ), surfaceData.extent ), clearValues );
commandBuffer->beginRenderPass( renderPassBeginInfo, vk::SubpassContents::eInline ); commandBuffer.beginRenderPass( renderPassBeginInfo, vk::SubpassContents::eInline );
commandBuffer->bindPipeline( vk::PipelineBindPoint::eGraphics, **graphicsPipeline ); commandBuffer.bindPipeline( vk::PipelineBindPoint::eGraphics, *graphicsPipeline );
commandBuffer->bindDescriptorSets( commandBuffer.bindDescriptorSets(
vk::PipelineBindPoint::eGraphics, **pipelineLayout, 0, { **descriptorSet }, nullptr ); vk::PipelineBindPoint::eGraphics, *pipelineLayout, 0, { *descriptorSet }, nullptr );
commandBuffer->bindVertexBuffers( 0, { **vertexBufferData.buffer }, { 0 } ); commandBuffer.bindVertexBuffers( 0, { *vertexBufferData.buffer }, { 0 } );
commandBuffer->setViewport( 0, commandBuffer.setViewport( 0,
vk::Viewport( 0.0f, vk::Viewport( 0.0f,
0.0f, 0.0f,
static_cast<float>( surfaceData.extent.width ), static_cast<float>( surfaceData.extent.width ),
static_cast<float>( surfaceData.extent.height ), static_cast<float>( surfaceData.extent.height ),
0.0f, 0.0f,
1.0f ) ); 1.0f ) );
commandBuffer->setScissor( 0, vk::Rect2D( vk::Offset2D( 0, 0 ), surfaceData.extent ) ); commandBuffer.setScissor( 0, vk::Rect2D( vk::Offset2D( 0, 0 ), surfaceData.extent ) );
commandBuffer->draw( 12 * 3, 1, 0, 0 ); commandBuffer.draw( 12 * 3, 1, 0, 0 );
commandBuffer->endRenderPass(); commandBuffer.endRenderPass();
commandBuffer->end(); commandBuffer.end();
std::unique_ptr<vk::raii::Fence> drawFence = vk::raii::su::make_unique<vk::raii::Fence>( *device, vk::FenceCreateInfo() ); vk::raii::Fence drawFence( device, vk::FenceCreateInfo() );
vk::PipelineStageFlags waitDestinationStageMask( vk::PipelineStageFlagBits::eColorAttachmentOutput ); vk::PipelineStageFlags waitDestinationStageMask( vk::PipelineStageFlagBits::eColorAttachmentOutput );
vk::SubmitInfo submitInfo( **imageAcquiredSemaphore, waitDestinationStageMask, **commandBuffer ); vk::SubmitInfo submitInfo( *imageAcquiredSemaphore, waitDestinationStageMask, *commandBuffer );
graphicsQueue->submit( submitInfo, **drawFence ); graphicsQueue.submit( submitInfo, *drawFence );
while ( vk::Result::eTimeout == device->waitForFences( { **drawFence }, VK_TRUE, vk::su::FenceTimeout ) ) while ( vk::Result::eTimeout == device.waitForFences( { *drawFence }, VK_TRUE, vk::su::FenceTimeout ) )
; ;
vk::PresentInfoKHR presentInfoKHR( nullptr, **swapChainData.swapChain, imageIndex ); vk::PresentInfoKHR presentInfoKHR( nullptr, **swapChainData.pSwapChain, imageIndex );
result = presentQueue->presentKHR( presentInfoKHR ); result = presentQueue.presentKHR( presentInfoKHR );
switch ( result ) switch ( result )
{ {
case vk::Result::eSuccess: break; case vk::Result::eSuccess: break;

View File

@ -1,4 +1,5 @@
// Copyright(c) 2019, NVIDIA CORPORATION. All rights reserved. // Copyright(c) 2019, NVIDIA CORPORATION. All rights reserved.
// Copyright(c) 2019, NVIDIA CORPORATION. All rights reserved.
// //
// Licensed under the Apache License, Version 2.0 (the "License"); // Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License. // you may not use this file except in compliance with the License.
@ -32,12 +33,12 @@ int main( int /*argc*/, char ** /*argv*/ )
{ {
try try
{ {
std::unique_ptr<vk::raii::Context> context = vk::raii::su::make_unique<vk::raii::Context>(); vk::raii::Context context;
/* VULKAN_KEY_START */ /* VULKAN_KEY_START */
// To use PUSH_DESCRIPTOR, you must also specify GET_PHYSICAL_DEVICE_PROPERTIES_2 // To use PUSH_DESCRIPTOR, you must also specify GET_PHYSICAL_DEVICE_PROPERTIES_2
std::vector<vk::ExtensionProperties> extensionProperties = context->enumerateInstanceExtensionProperties(); std::vector<vk::ExtensionProperties> extensionProperties = context.enumerateInstanceExtensionProperties();
auto propertyIterator = auto propertyIterator =
std::find_if( extensionProperties.begin(), extensionProperties.end(), []( vk::ExtensionProperties ep ) { std::find_if( extensionProperties.begin(), extensionProperties.end(), []( vk::ExtensionProperties ep ) {
return ( strcmp( ep.extensionName, VK_KHR_GET_PHYSICAL_DEVICE_PROPERTIES_2_EXTENSION_NAME ) == 0 ); return ( strcmp( ep.extensionName, VK_KHR_GET_PHYSICAL_DEVICE_PROPERTIES_2_EXTENSION_NAME ) == 0 );
@ -51,16 +52,15 @@ int main( int /*argc*/, char ** /*argv*/ )
std::vector<std::string> instanceExtensions = vk::su::getInstanceExtensions(); std::vector<std::string> instanceExtensions = vk::su::getInstanceExtensions();
instanceExtensions.push_back( VK_KHR_GET_PHYSICAL_DEVICE_PROPERTIES_2_EXTENSION_NAME ); instanceExtensions.push_back( VK_KHR_GET_PHYSICAL_DEVICE_PROPERTIES_2_EXTENSION_NAME );
std::unique_ptr<vk::raii::Instance> instance = vk::raii::Instance instance =
vk::raii::su::makeUniqueInstance( *context, AppName, EngineName, {}, instanceExtensions ); vk::raii::su::makeInstance( context, AppName, EngineName, {}, instanceExtensions );
#if !defined( NDEBUG ) #if !defined( NDEBUG )
std::unique_ptr<vk::raii::DebugUtilsMessengerEXT> debugUtilsMessenger = vk::raii::DebugUtilsMessengerEXT debugUtilsMessenger( instance, vk::su::makeDebugUtilsMessengerCreateInfoEXT() );
vk::raii::su::makeUniqueDebugUtilsMessengerEXT( *instance );
#endif #endif
std::unique_ptr<vk::raii::PhysicalDevice> physicalDevice = vk::raii::su::makeUniquePhysicalDevice( *instance ); vk::raii::PhysicalDevice physicalDevice = std::move( vk::raii::PhysicalDevices( instance ).front() );
// Once instance is created, need to make sure the extension is available // Once instance is created, need to make sure the extension is available
extensionProperties = physicalDevice->enumerateDeviceExtensionProperties(); extensionProperties = physicalDevice.enumerateDeviceExtensionProperties();
propertyIterator = propertyIterator =
std::find_if( extensionProperties.begin(), extensionProperties.end(), []( vk::ExtensionProperties ep ) { std::find_if( extensionProperties.begin(), extensionProperties.end(), []( vk::ExtensionProperties ep ) {
return ( strcmp( ep.extensionName, VK_KHR_PUSH_DESCRIPTOR_EXTENSION_NAME ) == 0 ); return ( strcmp( ep.extensionName, VK_KHR_PUSH_DESCRIPTOR_EXTENSION_NAME ) == 0 );
@ -74,26 +74,24 @@ int main( int /*argc*/, char ** /*argv*/ )
std::vector<std::string> deviceExtensions = vk::su::getDeviceExtensions(); std::vector<std::string> deviceExtensions = vk::su::getDeviceExtensions();
deviceExtensions.push_back( VK_KHR_PUSH_DESCRIPTOR_EXTENSION_NAME ); deviceExtensions.push_back( VK_KHR_PUSH_DESCRIPTOR_EXTENSION_NAME );
vk::raii::su::SurfaceData surfaceData( *instance, AppName, vk::Extent2D( 500, 500 ) ); vk::raii::su::SurfaceData surfaceData( instance, AppName, vk::Extent2D( 500, 500 ) );
std::pair<uint32_t, uint32_t> graphicsAndPresentQueueFamilyIndex = std::pair<uint32_t, uint32_t> graphicsAndPresentQueueFamilyIndex =
vk::raii::su::findGraphicsAndPresentQueueFamilyIndex( *physicalDevice, *surfaceData.surface ); vk::raii::su::findGraphicsAndPresentQueueFamilyIndex( physicalDevice, *surfaceData.pSurface );
std::unique_ptr<vk::raii::Device> device = vk::raii::Device device =
vk::raii::su::makeUniqueDevice( *physicalDevice, graphicsAndPresentQueueFamilyIndex.first, deviceExtensions ); vk::raii::su::makeDevice( physicalDevice, graphicsAndPresentQueueFamilyIndex.first, deviceExtensions );
std::unique_ptr<vk::raii::CommandPool> commandPool = vk::raii::CommandPool commandPool = vk::raii::CommandPool(
vk::raii::su::makeUniqueCommandPool( *device, graphicsAndPresentQueueFamilyIndex.first ); device, { vk::CommandPoolCreateFlagBits::eResetCommandBuffer, graphicsAndPresentQueueFamilyIndex.first } );
std::unique_ptr<vk::raii::CommandBuffer> commandBuffer = vk::raii::CommandBuffer commandBuffer =
vk::raii::su::makeUniqueCommandBuffer( *device, *commandPool ); vk::raii::su::makeCommandBuffer( device, commandPool );
std::unique_ptr<vk::raii::Queue> graphicsQueue = vk::raii::Queue graphicsQueue( device, graphicsAndPresentQueueFamilyIndex.first, 0 );
vk::raii::su::make_unique<vk::raii::Queue>( *device, graphicsAndPresentQueueFamilyIndex.first, 0 ); vk::raii::Queue presentQueue( device, graphicsAndPresentQueueFamilyIndex.second, 0 );
std::unique_ptr<vk::raii::Queue> presentQueue =
vk::raii::su::make_unique<vk::raii::Queue>( *device, graphicsAndPresentQueueFamilyIndex.second, 0 );
vk::raii::su::SwapChainData swapChainData( *physicalDevice, vk::raii::su::SwapChainData swapChainData( physicalDevice,
*device, device,
*surfaceData.surface, *surfaceData.pSurface,
surfaceData.extent, surfaceData.extent,
vk::ImageUsageFlagBits::eColorAttachment | vk::ImageUsageFlagBits::eColorAttachment |
vk::ImageUsageFlagBits::eTransferSrc, vk::ImageUsageFlagBits::eTransferSrc,
@ -102,70 +100,67 @@ int main( int /*argc*/, char ** /*argv*/ )
graphicsAndPresentQueueFamilyIndex.second ); graphicsAndPresentQueueFamilyIndex.second );
vk::raii::su::DepthBufferData depthBufferData( vk::raii::su::DepthBufferData depthBufferData(
*physicalDevice, *device, vk::Format::eD16Unorm, surfaceData.extent ); physicalDevice, device, vk::Format::eD16Unorm, surfaceData.extent );
vk::raii::su::TextureData textureData( *physicalDevice, *device ); vk::raii::su::TextureData textureData( physicalDevice, device );
commandBuffer->begin( vk::CommandBufferBeginInfo() ); commandBuffer.begin( vk::CommandBufferBeginInfo() );
textureData.setImage( *commandBuffer, vk::su::CheckerboardImageGenerator() ); textureData.setImage( commandBuffer, vk::su::CheckerboardImageGenerator() );
vk::raii::su::BufferData uniformBufferData( vk::raii::su::BufferData uniformBufferData(
*physicalDevice, *device, sizeof( glm::mat4x4 ), vk::BufferUsageFlagBits::eUniformBuffer ); physicalDevice, device, sizeof( glm::mat4x4 ), vk::BufferUsageFlagBits::eUniformBuffer );
glm::mat4x4 mvpcMatrix = vk::su::createModelViewProjectionClipMatrix( surfaceData.extent ); glm::mat4x4 mvpcMatrix = vk::su::createModelViewProjectionClipMatrix( surfaceData.extent );
vk::raii::su::copyToDevice( *uniformBufferData.deviceMemory, mvpcMatrix ); vk::raii::su::copyToDevice( uniformBufferData.deviceMemory, mvpcMatrix );
// Need to specify that descriptor set layout will be for push descriptors // Need to specify that descriptor set layout will be for push descriptors
std::unique_ptr<vk::raii::DescriptorSetLayout> descriptorSetLayout = vk::raii::su::makeUniqueDescriptorSetLayout( vk::raii::DescriptorSetLayout descriptorSetLayout = vk::raii::su::makeDescriptorSetLayout(
*device, device,
{ { vk::DescriptorType::eUniformBuffer, 1, vk::ShaderStageFlagBits::eVertex }, { { vk::DescriptorType::eUniformBuffer, 1, vk::ShaderStageFlagBits::eVertex },
{ vk::DescriptorType::eCombinedImageSampler, 1, vk::ShaderStageFlagBits::eFragment } }, { vk::DescriptorType::eCombinedImageSampler, 1, vk::ShaderStageFlagBits::eFragment } },
vk::DescriptorSetLayoutCreateFlagBits::ePushDescriptorKHR ); vk::DescriptorSetLayoutCreateFlagBits::ePushDescriptorKHR );
std::unique_ptr<vk::raii::PipelineLayout> pipelineLayout = vk::raii::PipelineLayout pipelineLayout( device, { {}, *descriptorSetLayout } );
vk::raii::su::makeUniquePipelineLayout( *device, *descriptorSetLayout );
vk::Format colorFormat = vk::Format colorFormat =
vk::su::pickSurfaceFormat( physicalDevice->getSurfaceFormatsKHR( **surfaceData.surface ) ).format; vk::su::pickSurfaceFormat( physicalDevice.getSurfaceFormatsKHR( **surfaceData.pSurface ) ).format;
std::unique_ptr<vk::raii::RenderPass> renderPass = vk::raii::RenderPass renderPass =
vk::raii::su::makeUniqueRenderPass( *device, colorFormat, depthBufferData.format ); vk::raii::su::makeRenderPass( device, colorFormat, depthBufferData.format );
glslang::InitializeProcess(); glslang::InitializeProcess();
std::unique_ptr<vk::raii::ShaderModule> vertexShaderModule = vk::raii::ShaderModule vertexShaderModule =
vk::raii::su::makeUniqueShaderModule( *device, vk::ShaderStageFlagBits::eVertex, vertexShaderText_PT_T ); vk::raii::su::makeShaderModule( device, vk::ShaderStageFlagBits::eVertex, vertexShaderText_PT_T );
std::unique_ptr<vk::raii::ShaderModule> fragmentShaderModule = vk::raii::ShaderModule fragmentShaderModule =
vk::raii::su::makeUniqueShaderModule( *device, vk::ShaderStageFlagBits::eFragment, fragmentShaderText_T_C ); vk::raii::su::makeShaderModule( device, vk::ShaderStageFlagBits::eFragment, fragmentShaderText_T_C );
glslang::FinalizeProcess(); glslang::FinalizeProcess();
std::vector<std::unique_ptr<vk::raii::Framebuffer>> framebuffers = vk::raii::su::makeUniqueFramebuffers( std::vector<vk::raii::Framebuffer> framebuffers = vk::raii::su::makeFramebuffers(
*device, *renderPass, swapChainData.imageViews, depthBufferData.imageView, surfaceData.extent ); device, renderPass, swapChainData.imageViews, &*depthBufferData.pImageView, surfaceData.extent );
vk::raii::su::BufferData vertexBufferData( vk::raii::su::BufferData vertexBufferData(
*physicalDevice, *device, sizeof( texturedCubeData ), vk::BufferUsageFlagBits::eVertexBuffer ); physicalDevice, device, sizeof( texturedCubeData ), vk::BufferUsageFlagBits::eVertexBuffer );
vk::raii::su::copyToDevice( vk::raii::su::copyToDevice(
*vertexBufferData.deviceMemory, texturedCubeData, sizeof( texturedCubeData ) / sizeof( texturedCubeData[0] ) ); vertexBufferData.deviceMemory, texturedCubeData, sizeof( texturedCubeData ) / sizeof( texturedCubeData[0] ) );
std::unique_ptr<vk::raii::PipelineCache> pipelineCache = vk::raii::PipelineCache pipelineCache( device, vk::PipelineCacheCreateInfo() );
vk::raii::su::make_unique<vk::raii::PipelineCache>( *device, vk::PipelineCacheCreateInfo() ); vk::raii::Pipeline graphicsPipeline = vk::raii::su::makeGraphicsPipeline(
std::unique_ptr<vk::raii::Pipeline> graphicsPipeline = vk::raii::su::makeUniqueGraphicsPipeline( device,
*device, pipelineCache,
*pipelineCache, vertexShaderModule,
*vertexShaderModule,
nullptr, nullptr,
*fragmentShaderModule, fragmentShaderModule,
nullptr, nullptr,
sizeof( texturedCubeData[0] ), sizeof( texturedCubeData[0] ),
{ { vk::Format::eR32G32B32A32Sfloat, 0 }, { vk::Format::eR32G32Sfloat, 16 } }, { { vk::Format::eR32G32B32A32Sfloat, 0 }, { vk::Format::eR32G32Sfloat, 16 } },
vk::FrontFace::eClockwise, vk::FrontFace::eClockwise,
true, true,
*pipelineLayout, pipelineLayout,
*renderPass ); renderPass );
// Get the index of the next available swapchain image: // Get the index of the next available swapchain image:
std::unique_ptr<vk::raii::Semaphore> imageAcquiredSemaphore = vk::raii::Semaphore imageAcquiredSemaphore( device, vk::SemaphoreCreateInfo() );
vk::raii::su::make_unique<vk::raii::Semaphore>( *device, vk::SemaphoreCreateInfo() );
vk::Result result; vk::Result result;
uint32_t imageIndex; uint32_t imageIndex;
std::tie( result, imageIndex ) = std::tie( result, imageIndex ) =
swapChainData.swapChain->acquireNextImage( vk::su::FenceTimeout, **imageAcquiredSemaphore ); swapChainData.pSwapChain->acquireNextImage( vk::su::FenceTimeout, *imageAcquiredSemaphore );
assert( result == vk::Result::eSuccess ); assert( result == vk::Result::eSuccess );
assert( imageIndex < swapChainData.images.size() ); assert( imageIndex < swapChainData.images.size() );
@ -173,47 +168,47 @@ int main( int /*argc*/, char ** /*argv*/ )
clearValues[0].color = vk::ClearColorValue( std::array<float, 4>( { { 0.2f, 0.2f, 0.2f, 0.2f } } ) ); clearValues[0].color = vk::ClearColorValue( std::array<float, 4>( { { 0.2f, 0.2f, 0.2f, 0.2f } } ) );
clearValues[1].depthStencil = vk::ClearDepthStencilValue( 1.0f, 0 ); clearValues[1].depthStencil = vk::ClearDepthStencilValue( 1.0f, 0 );
vk::RenderPassBeginInfo renderPassBeginInfo( vk::RenderPassBeginInfo renderPassBeginInfo(
**renderPass, **framebuffers[imageIndex], vk::Rect2D( vk::Offset2D( 0, 0 ), surfaceData.extent ), clearValues ); *renderPass, *framebuffers[imageIndex], vk::Rect2D( vk::Offset2D( 0, 0 ), surfaceData.extent ), clearValues );
commandBuffer->beginRenderPass( renderPassBeginInfo, vk::SubpassContents::eInline ); commandBuffer.beginRenderPass( renderPassBeginInfo, vk::SubpassContents::eInline );
commandBuffer->bindPipeline( vk::PipelineBindPoint::eGraphics, **graphicsPipeline ); commandBuffer.bindPipeline( vk::PipelineBindPoint::eGraphics, *graphicsPipeline );
vk::DescriptorBufferInfo bufferInfo( **uniformBufferData.buffer, 0, sizeof( glm::mat4x4 ) ); vk::DescriptorBufferInfo bufferInfo( *uniformBufferData.buffer, 0, sizeof( glm::mat4x4 ) );
vk::DescriptorImageInfo imageInfo( vk::DescriptorImageInfo imageInfo(
**textureData.sampler, **textureData.imageData->imageView, vk::ImageLayout::eShaderReadOnlyOptimal ); *textureData.sampler, **textureData.pImageData->pImageView, vk::ImageLayout::eShaderReadOnlyOptimal );
vk::WriteDescriptorSet writeDescriptorSets[2] = { vk::WriteDescriptorSet writeDescriptorSets[2] = {
vk::WriteDescriptorSet( {}, 0, 0, vk::DescriptorType::eUniformBuffer, {}, bufferInfo ), vk::WriteDescriptorSet( {}, 0, 0, vk::DescriptorType::eUniformBuffer, {}, bufferInfo ),
vk::WriteDescriptorSet( {}, 1, 0, vk::DescriptorType::eCombinedImageSampler, imageInfo ) vk::WriteDescriptorSet( {}, 1, 0, vk::DescriptorType::eCombinedImageSampler, imageInfo )
}; };
// this call is from an extension and needs the dynamic dispatcher !! // this call is from an extension and needs the dynamic dispatcher !!
commandBuffer->pushDescriptorSetKHR( commandBuffer.pushDescriptorSetKHR(
vk::PipelineBindPoint::eGraphics, **pipelineLayout, 0, { 2, writeDescriptorSets } ); vk::PipelineBindPoint::eGraphics, *pipelineLayout, 0, { 2, writeDescriptorSets } );
commandBuffer->bindVertexBuffers( 0, { **vertexBufferData.buffer }, { 0 } ); commandBuffer.bindVertexBuffers( 0, { *vertexBufferData.buffer }, { 0 } );
commandBuffer->setViewport( 0, commandBuffer.setViewport( 0,
vk::Viewport( 0.0f, vk::Viewport( 0.0f,
0.0f, 0.0f,
static_cast<float>( surfaceData.extent.width ), static_cast<float>( surfaceData.extent.width ),
static_cast<float>( surfaceData.extent.height ), static_cast<float>( surfaceData.extent.height ),
0.0f, 0.0f,
1.0f ) ); 1.0f ) );
commandBuffer->setScissor( 0, vk::Rect2D( vk::Offset2D( 0, 0 ), surfaceData.extent ) ); commandBuffer.setScissor( 0, vk::Rect2D( vk::Offset2D( 0, 0 ), surfaceData.extent ) );
commandBuffer->draw( 12 * 3, 1, 0, 0 ); commandBuffer.draw( 12 * 3, 1, 0, 0 );
commandBuffer->endRenderPass(); commandBuffer.endRenderPass();
commandBuffer->end(); commandBuffer.end();
std::unique_ptr<vk::raii::Fence> drawFence = vk::raii::su::make_unique<vk::raii::Fence>( *device, vk::FenceCreateInfo() ); vk::raii::Fence drawFence( device, vk::FenceCreateInfo() );
vk::PipelineStageFlags waitDestinationStageMask( vk::PipelineStageFlagBits::eColorAttachmentOutput ); vk::PipelineStageFlags waitDestinationStageMask( vk::PipelineStageFlagBits::eColorAttachmentOutput );
vk::SubmitInfo submitInfo( **imageAcquiredSemaphore, waitDestinationStageMask, **commandBuffer ); vk::SubmitInfo submitInfo( *imageAcquiredSemaphore, waitDestinationStageMask, *commandBuffer );
graphicsQueue->submit( submitInfo, **drawFence ); graphicsQueue.submit( submitInfo, *drawFence );
while ( vk::Result::eTimeout == device->waitForFences( { **drawFence }, VK_TRUE, vk::su::FenceTimeout ) ) while ( vk::Result::eTimeout == device.waitForFences( { *drawFence }, VK_TRUE, vk::su::FenceTimeout ) )
; ;
vk::PresentInfoKHR presentInfoKHR( nullptr, **swapChainData.swapChain, imageIndex ); vk::PresentInfoKHR presentInfoKHR( nullptr, **swapChainData.pSwapChain, imageIndex );
result = presentQueue->presentKHR( presentInfoKHR ); result = presentQueue.presentKHR( presentInfoKHR );
switch ( result ) switch ( result )
{ {
case vk::Result::eSuccess: break; case vk::Result::eSuccess: break;
@ -226,7 +221,7 @@ int main( int /*argc*/, char ** /*argv*/ )
/* VULKAN_KEY_END */ /* VULKAN_KEY_END */
device->waitIdle(); device.waitIdle();
} }
catch ( vk::SystemError & err ) catch ( vk::SystemError & err )
{ {

View File

@ -81,10 +81,10 @@ static_assert( sizeof( GeometryInstanceData ) == 64, "GeometryInstanceData struc
struct AccelerationStructureData struct AccelerationStructureData
{ {
std::shared_ptr<vk::raii::AccelerationStructureNV> acclerationStructure; std::shared_ptr<vk::raii::AccelerationStructureNV> pAcclerationStructure;
std::unique_ptr<vk::raii::su::BufferData> scratchBufferData; std::unique_ptr<vk::raii::su::BufferData> pScratchBufferData;
std::unique_ptr<vk::raii::su::BufferData> resultBufferData; std::unique_ptr<vk::raii::su::BufferData> pResultBufferData;
std::unique_ptr<vk::raii::su::BufferData> instanceBufferData; std::unique_ptr<vk::raii::su::BufferData> pInstanceBufferData;
}; };
AccelerationStructureData createAccelerationStructureData( AccelerationStructureData createAccelerationStructureData(
@ -103,46 +103,46 @@ AccelerationStructureData createAccelerationStructureData(
vk::AccelerationStructureInfoNV accelerationStructureInfo( vk::AccelerationStructureInfoNV accelerationStructureInfo(
accelerationStructureType, {}, vk::su::checked_cast<uint32_t>( instances.size() ), geometries ); accelerationStructureType, {}, vk::su::checked_cast<uint32_t>( instances.size() ), geometries );
vk::AccelerationStructureCreateInfoNV accelerationStructureCreateInfoNV( 0, accelerationStructureInfo ); vk::AccelerationStructureCreateInfoNV accelerationStructureCreateInfoNV( 0, accelerationStructureInfo );
accelerationStructureData.acclerationStructure = accelerationStructureData.pAcclerationStructure =
std::make_shared<vk::raii::AccelerationStructureNV>( device, accelerationStructureCreateInfoNV ); std::make_shared<vk::raii::AccelerationStructureNV>( device, accelerationStructureCreateInfoNV );
vk::AccelerationStructureMemoryRequirementsInfoNV objectRequirements( vk::AccelerationStructureMemoryRequirementsInfoNV objectRequirements(
vk::AccelerationStructureMemoryRequirementsTypeNV::eObject, **accelerationStructureData.acclerationStructure ); vk::AccelerationStructureMemoryRequirementsTypeNV::eObject, **accelerationStructureData.pAcclerationStructure );
vk::DeviceSize resultSizeInBytes = vk::DeviceSize resultSizeInBytes =
device.getAccelerationStructureMemoryRequirementsNV( objectRequirements ).memoryRequirements.size; device.getAccelerationStructureMemoryRequirementsNV( objectRequirements ).memoryRequirements.size;
assert( 0 < resultSizeInBytes ); assert( 0 < resultSizeInBytes );
accelerationStructureData.resultBufferData = accelerationStructureData.pResultBufferData =
vk::raii::su::make_unique<vk::raii::su::BufferData>( physicalDevice, vk::raii::su::make_unique<vk::raii::su::BufferData>( physicalDevice,
device, device,
resultSizeInBytes, resultSizeInBytes,
vk::BufferUsageFlagBits::eRayTracingNV, vk::BufferUsageFlagBits::eRayTracingNV,
vk::MemoryPropertyFlagBits::eDeviceLocal ); vk::MemoryPropertyFlagBits::eDeviceLocal );
vk::AccelerationStructureMemoryRequirementsInfoNV buildScratchRequirements( vk::AccelerationStructureMemoryRequirementsInfoNV buildScratchRequirements(
vk::AccelerationStructureMemoryRequirementsTypeNV::eBuildScratch, vk::AccelerationStructureMemoryRequirementsTypeNV::eBuildScratch,
**accelerationStructureData.acclerationStructure ); **accelerationStructureData.pAcclerationStructure );
vk::AccelerationStructureMemoryRequirementsInfoNV updateScratchRequirements( vk::AccelerationStructureMemoryRequirementsInfoNV updateScratchRequirements(
vk::AccelerationStructureMemoryRequirementsTypeNV::eUpdateScratch, vk::AccelerationStructureMemoryRequirementsTypeNV::eUpdateScratch,
**accelerationStructureData.acclerationStructure ); **accelerationStructureData.pAcclerationStructure );
vk::DeviceSize scratchSizeInBytes = std::max( vk::DeviceSize scratchSizeInBytes = std::max(
device.getAccelerationStructureMemoryRequirementsNV( buildScratchRequirements ).memoryRequirements.size, device.getAccelerationStructureMemoryRequirementsNV( buildScratchRequirements ).memoryRequirements.size,
device.getAccelerationStructureMemoryRequirementsNV( updateScratchRequirements ).memoryRequirements.size ); device.getAccelerationStructureMemoryRequirementsNV( updateScratchRequirements ).memoryRequirements.size );
assert( 0 < scratchSizeInBytes ); assert( 0 < scratchSizeInBytes );
accelerationStructureData.scratchBufferData = accelerationStructureData.pScratchBufferData =
vk::raii::su::make_unique<vk::raii::su::BufferData>( physicalDevice, vk::raii::su::make_unique<vk::raii::su::BufferData>( physicalDevice,
device, device,
scratchSizeInBytes, scratchSizeInBytes,
vk::BufferUsageFlagBits::eRayTracingNV, vk::BufferUsageFlagBits::eRayTracingNV,
vk::MemoryPropertyFlagBits::eDeviceLocal ); vk::MemoryPropertyFlagBits::eDeviceLocal );
if ( !instances.empty() ) if ( !instances.empty() )
{ {
accelerationStructureData.instanceBufferData = accelerationStructureData.pInstanceBufferData =
vk::raii::su::make_unique<vk::raii::su::BufferData>( physicalDevice, vk::raii::su::make_unique<vk::raii::su::BufferData>( physicalDevice,
device, device,
instances.size() * sizeof( GeometryInstanceData ), instances.size() * sizeof( GeometryInstanceData ),
vk::BufferUsageFlagBits::eRayTracingNV ); vk::BufferUsageFlagBits::eRayTracingNV );
std::vector<GeometryInstanceData> geometryInstanceData; std::vector<GeometryInstanceData> geometryInstanceData;
for ( size_t i = 0; i < instances.size(); i++ ) for ( size_t i = 0; i < instances.size(); i++ )
@ -161,16 +161,16 @@ AccelerationStructureData createAccelerationStructureData(
static_cast<uint8_t>( vk::GeometryInstanceFlagBitsNV::eTriangleCullDisable ), static_cast<uint8_t>( vk::GeometryInstanceFlagBitsNV::eTriangleCullDisable ),
accelerationStructureHandle ); accelerationStructureHandle );
} }
accelerationStructureData.instanceBufferData->upload( geometryInstanceData ); accelerationStructureData.pInstanceBufferData->upload( geometryInstanceData );
} }
device.bindAccelerationStructureMemoryNV( vk::BindAccelerationStructureMemoryInfoNV( device.bindAccelerationStructureMemoryNV( vk::BindAccelerationStructureMemoryInfoNV(
**accelerationStructureData.acclerationStructure, **accelerationStructureData.resultBufferData->deviceMemory ) ); **accelerationStructureData.pAcclerationStructure, *accelerationStructureData.pResultBufferData->deviceMemory ) );
vk::Buffer instanceData; vk::Buffer instanceData;
if ( accelerationStructureData.instanceBufferData ) if ( accelerationStructureData.pInstanceBufferData )
{ {
instanceData = **accelerationStructureData.instanceBufferData->buffer; instanceData = *accelerationStructureData.pInstanceBufferData->buffer;
} }
vk::AccelerationStructureInfoNV accelerationStructureInfoNV( vk::AccelerationStructureInfoNV accelerationStructureInfoNV(
accelerationStructureType, {}, vk::su::checked_cast<uint32_t>( instances.size() ), geometries ); accelerationStructureType, {}, vk::su::checked_cast<uint32_t>( instances.size() ), geometries );
@ -178,9 +178,9 @@ AccelerationStructureData createAccelerationStructureData(
instanceData, instanceData,
0, 0,
false, false,
**accelerationStructureData.acclerationStructure, **accelerationStructureData.pAcclerationStructure,
nullptr, nullptr,
**accelerationStructureData.scratchBufferData->buffer, *accelerationStructureData.pScratchBufferData->buffer,
0 ); 0 );
vk::MemoryBarrier memoryBarrier( vk::MemoryBarrier memoryBarrier(
@ -198,11 +198,19 @@ AccelerationStructureData createAccelerationStructureData(
struct PerFrameData struct PerFrameData
{ {
std::unique_ptr<vk::raii::CommandPool> commandPool; PerFrameData( vk::raii::Device const & device, uint32_t queueFamilyIndex )
std::unique_ptr<vk::raii::CommandBuffer> commandBuffer; : commandPool( device, { vk::CommandPoolCreateFlagBits::eResetCommandBuffer, queueFamilyIndex } )
std::unique_ptr<vk::raii::Fence> fence; , commandBuffer( vk::raii::su::makeCommandBuffer( device, commandPool ) )
std::unique_ptr<vk::raii::Semaphore> presentCompleteSemaphore; , fence( device, vk::FenceCreateInfo( vk::FenceCreateFlagBits::eSignaled ) )
std::unique_ptr<vk::raii::Semaphore> renderCompleteSemaphore; , presentCompleteSemaphore( device, vk::SemaphoreCreateInfo() )
, renderCompleteSemaphore( device, vk::SemaphoreCreateInfo() )
{}
vk::raii::CommandPool commandPool;
vk::raii::CommandBuffer commandBuffer;
vk::raii::Fence fence;
vk::raii::Semaphore presentCompleteSemaphore;
vk::raii::Semaphore renderCompleteSemaphore;
}; };
struct UniformBufferObject struct UniformBufferObject
@ -608,11 +616,11 @@ static void keyCallback( GLFWwindow * window, int key, int /*scancode*/, int act
case GLFW_KEY_ESCAPE: case GLFW_KEY_ESCAPE:
case 'Q': glfwSetWindowShouldClose( window, 1 ); break; case 'Q': glfwSetWindowShouldClose( window, 1 ); break;
case 'R': case 'R':
{ {
AppInfo * appInfo = reinterpret_cast<AppInfo *>( glfwGetWindowUserPointer( window ) ); AppInfo * appInfo = reinterpret_cast<AppInfo *>( glfwGetWindowUserPointer( window ) );
appInfo->useRasterRender = !appInfo->useRasterRender; appInfo->useRasterRender = !appInfo->useRasterRender;
} }
break; break;
} }
} }
} }
@ -715,55 +723,44 @@ int main( int /*argc*/, char ** /*argv*/ )
} }
instanceExtensions.push_back( VK_KHR_GET_PHYSICAL_DEVICE_PROPERTIES_2_EXTENSION_NAME ); instanceExtensions.push_back( VK_KHR_GET_PHYSICAL_DEVICE_PROPERTIES_2_EXTENSION_NAME );
std::unique_ptr<vk::raii::Context> context = vk::raii::su::make_unique<vk::raii::Context>(); vk::raii::Context context;
std::unique_ptr<vk::raii::Instance> instance = vk::raii::Instance instance =
vk::raii::su::makeUniqueInstance( *context, AppName, EngineName, {}, instanceExtensions ); vk::raii::su::makeInstance( context, AppName, EngineName, {}, instanceExtensions, VK_API_VERSION_1_2 );
#if !defined( NDEBUG ) #if !defined( NDEBUG )
std::unique_ptr<vk::raii::DebugUtilsMessengerEXT> debugUtilsMessenger = vk::raii::DebugUtilsMessengerEXT debugUtilsMessenger( instance, vk::su::makeDebugUtilsMessengerCreateInfoEXT() );
vk::raii::su::makeUniqueDebugUtilsMessengerEXT( *instance );
#endif #endif
std::unique_ptr<vk::raii::PhysicalDevice> physicalDevice = vk::raii::su::makeUniquePhysicalDevice( *instance ); vk::raii::PhysicalDevice physicalDevice = std::move( vk::raii::PhysicalDevices( instance ).front() );
// Create Window Surface (using glfw) // Create Window Surface (using glfw)
VkSurfaceKHR glfwSurface; VkSurfaceKHR glfwSurface;
VkResult err = glfwCreateWindowSurface( static_cast<VkInstance>( **instance ), window, nullptr, &glfwSurface ); VkResult err = glfwCreateWindowSurface( static_cast<VkInstance>( *instance ), window, nullptr, &glfwSurface );
check_vk_result( err ); check_vk_result( err );
std::unique_ptr<vk::raii::SurfaceKHR> surface = vk::raii::su::make_unique<vk::raii::SurfaceKHR>( *instance, glfwSurface ); vk::raii::SurfaceKHR surface( instance, glfwSurface );
std::pair<uint32_t, uint32_t> graphicsAndPresentQueueFamilyIndex = std::pair<uint32_t, uint32_t> graphicsAndPresentQueueFamilyIndex =
vk::raii::su::findGraphicsAndPresentQueueFamilyIndex( *physicalDevice, *surface ); vk::raii::su::findGraphicsAndPresentQueueFamilyIndex( physicalDevice, surface );
// Create a Device with ray tracing support (besides some other extensions needed) and needed features // Create a Device with ray tracing support (besides some other extensions needed) and needed features
auto supportedFeatures = auto supportedFeatures =
physicalDevice->getFeatures2<vk::PhysicalDeviceFeatures2, vk::PhysicalDeviceDescriptorIndexingFeaturesEXT>(); physicalDevice.getFeatures2<vk::PhysicalDeviceFeatures2, vk::PhysicalDeviceDescriptorIndexingFeaturesEXT>();
std::unique_ptr<vk::raii::Device> device = vk::raii::Device device =
vk::raii::su::makeUniqueDevice( *physicalDevice, vk::raii::su::makeDevice( physicalDevice,
graphicsAndPresentQueueFamilyIndex.first, graphicsAndPresentQueueFamilyIndex.first,
{ VK_KHR_SWAPCHAIN_EXTENSION_NAME, { VK_KHR_SWAPCHAIN_EXTENSION_NAME,
VK_NV_RAY_TRACING_EXTENSION_NAME, VK_NV_RAY_TRACING_EXTENSION_NAME,
VK_KHR_GET_MEMORY_REQUIREMENTS_2_EXTENSION_NAME }, VK_KHR_GET_MEMORY_REQUIREMENTS_2_EXTENSION_NAME },
&supportedFeatures.get<vk::PhysicalDeviceFeatures2>().features, &supportedFeatures.get<vk::PhysicalDeviceFeatures2>().features,
&supportedFeatures.get<vk::PhysicalDeviceDescriptorIndexingFeaturesEXT>() ); &supportedFeatures.get<vk::PhysicalDeviceDescriptorIndexingFeaturesEXT>() );
// setup stuff per frame // setup stuff per frame
std::array<PerFrameData, IMGUI_VK_QUEUED_FRAMES> perFrameData; std::vector<PerFrameData> perFrameData;
for ( int i = 0; i < IMGUI_VK_QUEUED_FRAMES; i++ ) for ( int i = 0; i < IMGUI_VK_QUEUED_FRAMES; i++ )
{ {
perFrameData[i].commandPool = perFrameData.push_back( PerFrameData( device, graphicsAndPresentQueueFamilyIndex.first ) );
vk::raii::su::makeUniqueCommandPool( *device, graphicsAndPresentQueueFamilyIndex.first );
perFrameData[i].commandBuffer = vk::raii::su::makeUniqueCommandBuffer( *device, *perFrameData[i].commandPool );
perFrameData[i].fence =
vk::raii::su::make_unique<vk::raii::Fence>( *device, vk::FenceCreateInfo( vk::FenceCreateFlagBits::eSignaled ) );
perFrameData[i].presentCompleteSemaphore =
vk::raii::su::make_unique<vk::raii::Semaphore>( *device, vk::SemaphoreCreateInfo() );
perFrameData[i].renderCompleteSemaphore =
vk::raii::su::make_unique<vk::raii::Semaphore>( *device, vk::SemaphoreCreateInfo() );
} }
std::unique_ptr<vk::raii::Queue> graphicsQueue = vk::raii::Queue graphicsQueue( device, graphicsAndPresentQueueFamilyIndex.first, 0 );
vk::raii::su::make_unique<vk::raii::Queue>( *device, graphicsAndPresentQueueFamilyIndex.first, 0 ); vk::raii::Queue presentQueue( device, graphicsAndPresentQueueFamilyIndex.second, 0 );
std::unique_ptr<vk::raii::Queue> presentQueue =
vk::raii::su::make_unique<vk::raii::Queue>( *device, graphicsAndPresentQueueFamilyIndex.second, 0 );
// create a descriptor pool with a number of available descriptors // create a descriptor pool with a number of available descriptors
std::vector<vk::DescriptorPoolSize> poolSizes = { std::vector<vk::DescriptorPoolSize> poolSizes = {
@ -771,29 +768,27 @@ int main( int /*argc*/, char ** /*argv*/ )
{ vk::DescriptorType::eUniformBuffer, 1000 }, { vk::DescriptorType::eUniformBuffer, 1000 },
{ vk::DescriptorType::eStorageBuffer, 1000 }, { vk::DescriptorType::eStorageBuffer, 1000 },
}; };
std::unique_ptr<vk::raii::DescriptorPool> descriptorPool = vk::raii::DescriptorPool descriptorPool = vk::raii::su::makeDescriptorPool( device, poolSizes );
vk::raii::su::makeUniqueDescriptorPool( *device, poolSizes );
// setup swap chain, render pass, depth buffer and the frame buffers // setup swap chain, render pass, depth buffer and the frame buffers
vk::raii::su::SwapChainData swapChainData( *physicalDevice, vk::raii::su::SwapChainData swapChainData( physicalDevice,
*device, device,
*surface, surface,
windowExtent, windowExtent,
vk::ImageUsageFlagBits::eColorAttachment | vk::ImageUsageFlagBits::eColorAttachment |
vk::ImageUsageFlagBits::eStorage, vk::ImageUsageFlagBits::eStorage,
{}, {},
graphicsAndPresentQueueFamilyIndex.first, graphicsAndPresentQueueFamilyIndex.first,
graphicsAndPresentQueueFamilyIndex.second ); graphicsAndPresentQueueFamilyIndex.second );
vk::SurfaceFormatKHR surfaceFormat = vk::su::pickSurfaceFormat( physicalDevice->getSurfaceFormatsKHR( **surface ) ); vk::SurfaceFormatKHR surfaceFormat = vk::su::pickSurfaceFormat( physicalDevice.getSurfaceFormatsKHR( *surface ) );
vk::Format depthFormat = vk::raii::su::pickDepthFormat( *physicalDevice ); vk::Format depthFormat = vk::raii::su::pickDepthFormat( physicalDevice );
// setup a render pass // setup a render pass
std::unique_ptr<vk::raii::RenderPass> renderPass = vk::raii::RenderPass renderPass = vk::raii::su::makeRenderPass( device, surfaceFormat.format, depthFormat );
vk::raii::su::makeUniqueRenderPass( *device, surfaceFormat.format, depthFormat );
vk::raii::su::DepthBufferData depthBufferData( *physicalDevice, *device, depthFormat, windowExtent ); vk::raii::su::DepthBufferData depthBufferData( physicalDevice, device, depthFormat, windowExtent );
std::vector<std::unique_ptr<vk::raii::Framebuffer>> framebuffers = vk::raii::su::makeUniqueFramebuffers( std::vector<vk::raii::Framebuffer> framebuffers = vk::raii::su::makeFramebuffers(
*device, *renderPass, swapChainData.imageViews, depthBufferData.imageView, windowExtent ); device, renderPass, swapChainData.imageViews, &*depthBufferData.pImageView, windowExtent );
bool samplerAnisotropy = !!supportedFeatures.get<vk::PhysicalDeviceFeatures2>().features.samplerAnisotropy; bool samplerAnisotropy = !!supportedFeatures.get<vk::PhysicalDeviceFeatures2>().features.samplerAnisotropy;
@ -803,24 +798,27 @@ int main( int /*argc*/, char ** /*argv*/ )
textures.reserve( textureCount ); textures.reserve( textureCount );
for ( size_t i = 0; i < textureCount; i++ ) for ( size_t i = 0; i < textureCount; i++ )
{ {
textures.emplace_back( *physicalDevice, textures.emplace_back( physicalDevice,
*device, device,
vk::Extent2D( random<uint32_t>( 2, 8 ) * 16, random<uint32_t>( 2, 8 ) * 16 ), vk::Extent2D( random<uint32_t>( 2, 8 ) * 16, random<uint32_t>( 2, 8 ) * 16 ),
vk::ImageUsageFlagBits::eTransferDst | vk::ImageUsageFlagBits::eSampled, vk::ImageUsageFlagBits::eTransferDst | vk::ImageUsageFlagBits::eSampled,
vk::FormatFeatureFlags(), vk::FormatFeatureFlags(),
samplerAnisotropy, samplerAnisotropy,
true ); true );
} }
vk::raii::su::oneTimeSubmit( vk::raii::su::oneTimeSubmit( device,
*device, *perFrameData[0].commandPool, *graphicsQueue, [&]( vk::raii::CommandBuffer const & commandBuffer ) { perFrameData[0].commandPool,
for ( auto & t : textures ) graphicsQueue,
{ [&]( vk::raii::CommandBuffer const & commandBuffer )
t.setImage( {
commandBuffer, for ( auto & t : textures )
vk::su::CheckerboardImageGenerator( { random<uint8_t>(), random<uint8_t>(), random<uint8_t>() }, {
{ random<uint8_t>(), random<uint8_t>(), random<uint8_t>() } ) ); t.setImage( commandBuffer,
} vk::su::CheckerboardImageGenerator(
} ); { random<uint8_t>(), random<uint8_t>(), random<uint8_t>() },
{ random<uint8_t>(), random<uint8_t>(), random<uint8_t>() } ) );
}
} );
// create some materials with a random diffuse color, referencing one of the above textures // create some materials with a random diffuse color, referencing one of the above textures
const size_t materialCount = 10; const size_t materialCount = 10;
@ -832,7 +830,7 @@ int main( int /*argc*/, char ** /*argv*/ )
materials[i].textureID = vk::su::checked_cast<uint32_t>( i ); materials[i].textureID = vk::su::checked_cast<uint32_t>( i );
} }
vk::raii::su::BufferData materialBufferData( vk::raii::su::BufferData materialBufferData(
*physicalDevice, *device, materialCount * MaterialStride, vk::BufferUsageFlagBits::eStorageBuffer ); physicalDevice, device, materialCount * MaterialStride, vk::BufferUsageFlagBits::eStorageBuffer );
materialBufferData.upload( materials, MaterialStride ); materialBufferData.upload( materials, MaterialStride );
// create a a 3D-array of cubes, randomly jittered, using a random material // create a a 3D-array of cubes, randomly jittered, using a random material
@ -865,23 +863,23 @@ int main( int /*argc*/, char ** /*argv*/ )
vk::BufferUsageFlags bufferUsageFlags = vk::BufferUsageFlagBits::eTransferDst | vk::BufferUsageFlags bufferUsageFlags = vk::BufferUsageFlagBits::eTransferDst |
vk::BufferUsageFlagBits::eVertexBuffer | vk::BufferUsageFlagBits::eVertexBuffer |
vk::BufferUsageFlagBits::eStorageBuffer; vk::BufferUsageFlagBits::eStorageBuffer;
vk::raii::su::BufferData vertexBufferData( *physicalDevice, vk::raii::su::BufferData vertexBufferData( physicalDevice,
*device, device,
vertices.size() * VertexStride, vertices.size() * VertexStride,
bufferUsageFlags, bufferUsageFlags,
vk::MemoryPropertyFlagBits::eDeviceLocal ); vk::MemoryPropertyFlagBits::eDeviceLocal );
vertexBufferData.upload( vertexBufferData.upload(
*physicalDevice, *device, *perFrameData[0].commandPool, *graphicsQueue, vertices, VertexStride ); physicalDevice, device, perFrameData[0].commandPool, graphicsQueue, vertices, VertexStride );
bufferUsageFlags = vk::BufferUsageFlagBits::eTransferDst | vk::BufferUsageFlagBits::eIndexBuffer | bufferUsageFlags = vk::BufferUsageFlagBits::eTransferDst | vk::BufferUsageFlagBits::eIndexBuffer |
vk::BufferUsageFlagBits::eStorageBuffer; vk::BufferUsageFlagBits::eStorageBuffer;
vk::raii::su::BufferData indexBufferData( *physicalDevice, vk::raii::su::BufferData indexBufferData( physicalDevice,
*device, device,
indices.size() * sizeof( uint32_t ), indices.size() * sizeof( uint32_t ),
bufferUsageFlags, bufferUsageFlags,
vk::MemoryPropertyFlagBits::eDeviceLocal ); vk::MemoryPropertyFlagBits::eDeviceLocal );
indexBufferData.upload( indexBufferData.upload(
*physicalDevice, *device, *perFrameData[0].commandPool, *graphicsQueue, indices, sizeof( uint32_t ) ); physicalDevice, device, perFrameData[0].commandPool, graphicsQueue, indices, sizeof( uint32_t ) );
// clang-format off // clang-format off
glm::mat4x4 transform( glm::mat4x4( 1.0f, 0.0f, 0.0f, 0.0f, glm::mat4x4 transform( glm::mat4x4( 1.0f, 0.0f, 0.0f, 0.0f,
@ -890,8 +888,8 @@ int main( int /*argc*/, char ** /*argv*/ )
0.0f, 0.0f, 0.0f, 1.0f ) ); 0.0f, 0.0f, 0.0f, 1.0f ) );
// clang-format on // clang-format on
std::unique_ptr<vk::raii::DescriptorSetLayout> descriptorSetLayout = vk::raii::su::makeUniqueDescriptorSetLayout( vk::raii::DescriptorSetLayout descriptorSetLayout = vk::raii::su::makeDescriptorSetLayout(
*device, device,
{ { vk::DescriptorType::eUniformBuffer, 1, vk::ShaderStageFlagBits::eVertex }, { { vk::DescriptorType::eUniformBuffer, 1, vk::ShaderStageFlagBits::eVertex },
{ vk::DescriptorType::eStorageBuffer, { vk::DescriptorType::eStorageBuffer,
1, 1,
@ -899,24 +897,22 @@ int main( int /*argc*/, char ** /*argv*/ )
{ vk::DescriptorType::eCombinedImageSampler, { vk::DescriptorType::eCombinedImageSampler,
static_cast<uint32_t>( textures.size() ), static_cast<uint32_t>( textures.size() ),
vk::ShaderStageFlagBits::eFragment } } ); vk::ShaderStageFlagBits::eFragment } } );
std::unique_ptr<vk::raii::PipelineLayout> pipelineLayout = vk::raii::PipelineLayout pipelineLayout( device, { {}, *descriptorSetLayout } );
vk::raii::su::makeUniquePipelineLayout( *device, *descriptorSetLayout );
glslang::InitializeProcess(); glslang::InitializeProcess();
std::unique_ptr<vk::raii::ShaderModule> vertexShaderModule = vk::raii::ShaderModule vertexShaderModule =
vk::raii::su::makeUniqueShaderModule( *device, vk::ShaderStageFlagBits::eVertex, vertexShaderText ); vk::raii::su::makeShaderModule( device, vk::ShaderStageFlagBits::eVertex, vertexShaderText );
std::unique_ptr<vk::raii::ShaderModule> fragmentShaderModule = vk::raii::ShaderModule fragmentShaderModule =
vk::raii::su::makeUniqueShaderModule( *device, vk::ShaderStageFlagBits::eFragment, fragmentShaderText ); vk::raii::su::makeShaderModule( device, vk::ShaderStageFlagBits::eFragment, fragmentShaderText );
glslang::FinalizeProcess(); glslang::FinalizeProcess();
std::unique_ptr<vk::raii::PipelineCache> pipelineCache = vk::raii::PipelineCache pipelineCache( device, vk::PipelineCacheCreateInfo() );
vk::raii::su::make_unique<vk::raii::PipelineCache>( *device, vk::PipelineCacheCreateInfo() ); vk::raii::Pipeline graphicsPipeline = vk::raii::su::makeGraphicsPipeline(
std::unique_ptr<vk::raii::Pipeline> graphicsPipeline = vk::raii::su::makeUniqueGraphicsPipeline( device,
*device, pipelineCache,
*pipelineCache, vertexShaderModule,
*vertexShaderModule,
nullptr, nullptr,
*fragmentShaderModule, fragmentShaderModule,
nullptr, nullptr,
VertexStride, VertexStride,
{ { vk::Format::eR32G32B32Sfloat, vk::su::checked_cast<uint32_t>( offsetof( Vertex, pos ) ) }, { { vk::Format::eR32G32B32Sfloat, vk::su::checked_cast<uint32_t>( offsetof( Vertex, pos ) ) },
@ -925,18 +921,18 @@ int main( int /*argc*/, char ** /*argv*/ )
{ vk::Format::eR32Sint, vk::su::checked_cast<uint32_t>( offsetof( Vertex, matID ) ) } }, { vk::Format::eR32Sint, vk::su::checked_cast<uint32_t>( offsetof( Vertex, matID ) ) } },
vk::FrontFace::eCounterClockwise, vk::FrontFace::eCounterClockwise,
true, true,
*pipelineLayout, pipelineLayout,
*renderPass ); renderPass );
vk::raii::su::BufferData uniformBufferData( vk::raii::su::BufferData uniformBufferData(
*physicalDevice, *device, sizeof( UniformBufferObject ), vk::BufferUsageFlagBits::eUniformBuffer ); physicalDevice, device, sizeof( UniformBufferObject ), vk::BufferUsageFlagBits::eUniformBuffer );
std::unique_ptr<vk::raii::DescriptorSet> descriptorSet = vk::raii::DescriptorSet descriptorSet =
vk::raii::su::makeUniqueDescriptorSet( *device, *descriptorPool, *descriptorSetLayout ); std::move( vk::raii::DescriptorSets( device, { *descriptorPool, *descriptorSetLayout } ).front() );
vk::raii::su::updateDescriptorSets( *device, vk::raii::su::updateDescriptorSets( device,
*descriptorSet, descriptorSet,
{ { vk::DescriptorType::eUniformBuffer, *uniformBufferData.buffer, {} }, { { vk::DescriptorType::eUniformBuffer, uniformBufferData.buffer, {} },
{ vk::DescriptorType::eStorageBuffer, *materialBufferData.buffer, {} } }, { vk::DescriptorType::eStorageBuffer, materialBufferData.buffer, {} } },
textures ); textures );
// RayTracing specific stuff // RayTracing specific stuff
@ -944,57 +940,64 @@ int main( int /*argc*/, char ** /*argv*/ )
// create acceleration structures: one top-level, and just one bottom-level // create acceleration structures: one top-level, and just one bottom-level
AccelerationStructureData topLevelAS, bottomLevelAS; AccelerationStructureData topLevelAS, bottomLevelAS;
vk::raii::su::oneTimeSubmit( vk::raii::su::oneTimeSubmit(
*device, *perFrameData[0].commandPool, *graphicsQueue, [&]( vk::raii::CommandBuffer const & commandBuffer ) { device,
vk::GeometryDataNV geometryDataNV( vk::GeometryTrianglesNV( **vertexBufferData.buffer, perFrameData[0].commandPool,
graphicsQueue,
[&]( vk::raii::CommandBuffer const & commandBuffer )
{
vk::GeometryDataNV geometryDataNV( vk::GeometryTrianglesNV( *vertexBufferData.buffer,
0, 0,
vk::su::checked_cast<uint32_t>( vertices.size() ), vk::su::checked_cast<uint32_t>( vertices.size() ),
VertexStride, VertexStride,
vk::Format::eR32G32B32Sfloat, vk::Format::eR32G32B32Sfloat,
**indexBufferData.buffer, *indexBufferData.buffer,
0, 0,
vk::su::checked_cast<uint32_t>( indices.size() ), vk::su::checked_cast<uint32_t>( indices.size() ),
vk::IndexType::eUint32 ), vk::IndexType::eUint32 ),
{} ); {} );
bottomLevelAS = bottomLevelAS =
createAccelerationStructureData( *physicalDevice, createAccelerationStructureData( physicalDevice,
*device, device,
commandBuffer, commandBuffer,
{}, {},
{ vk::GeometryNV( vk::GeometryTypeNV::eTriangles, geometryDataNV ) } ); { vk::GeometryNV( vk::GeometryTypeNV::eTriangles, geometryDataNV ) } );
topLevelAS = topLevelAS =
createAccelerationStructureData( *physicalDevice, createAccelerationStructureData( physicalDevice,
*device, device,
commandBuffer, commandBuffer,
{ std::make_pair( bottomLevelAS.acclerationStructure, transform ) }, { std::make_pair( bottomLevelAS.pAcclerationStructure, transform ) },
std::vector<vk::GeometryNV>() ); std::vector<vk::GeometryNV>() );
} ); } );
// create raytracing descriptor set // create raytracing descriptor set
vk::raii::su::oneTimeSubmit( vk::raii::su::oneTimeSubmit( device,
*device, *perFrameData[0].commandPool, *graphicsQueue, [&]( vk::raii::CommandBuffer const & commandBuffer ) { perFrameData[0].commandPool,
vk::BufferMemoryBarrier bufferMemoryBarrier( {}, graphicsQueue,
vk::AccessFlagBits::eShaderRead, [&]( vk::raii::CommandBuffer const & commandBuffer )
VK_QUEUE_FAMILY_IGNORED, {
VK_QUEUE_FAMILY_IGNORED, vk::BufferMemoryBarrier bufferMemoryBarrier( {},
**vertexBufferData.buffer, vk::AccessFlagBits::eShaderRead,
0, VK_QUEUE_FAMILY_IGNORED,
VK_WHOLE_SIZE ); VK_QUEUE_FAMILY_IGNORED,
commandBuffer.pipelineBarrier( vk::PipelineStageFlagBits::eAllCommands, *vertexBufferData.buffer,
vk::PipelineStageFlagBits::eAllCommands, 0,
{}, VK_WHOLE_SIZE );
nullptr, commandBuffer.pipelineBarrier( vk::PipelineStageFlagBits::eAllCommands,
bufferMemoryBarrier, vk::PipelineStageFlagBits::eAllCommands,
nullptr ); {},
nullptr,
bufferMemoryBarrier,
nullptr );
bufferMemoryBarrier.buffer = **indexBufferData.buffer; bufferMemoryBarrier.buffer = *indexBufferData.buffer;
commandBuffer.pipelineBarrier( vk::PipelineStageFlagBits::eAllCommands, commandBuffer.pipelineBarrier( vk::PipelineStageFlagBits::eAllCommands,
vk::PipelineStageFlagBits::eAllCommands, vk::PipelineStageFlagBits::eAllCommands,
{}, {},
nullptr, nullptr,
bufferMemoryBarrier, bufferMemoryBarrier,
nullptr ); nullptr );
} ); } );
std::vector<vk::DescriptorSetLayoutBinding> bindings; std::vector<vk::DescriptorSetLayoutBinding> bindings;
bindings.emplace_back( 0, bindings.emplace_back( 0,
@ -1027,54 +1030,52 @@ int main( int /*argc*/, char ** /*argv*/ )
vk::DescriptorPoolCreateFlagBits::eFreeDescriptorSet, vk::DescriptorPoolCreateFlagBits::eFreeDescriptorSet,
vk::su::checked_cast<uint32_t>( swapChainData.images.size() ), vk::su::checked_cast<uint32_t>( swapChainData.images.size() ),
descriptorPoolSizes ); descriptorPoolSizes );
std::unique_ptr<vk::raii::DescriptorPool> rayTracingDescriptorPool = vk::raii::DescriptorPool rayTracingDescriptorPool( device, descriptorPoolCreateInfo );
vk::raii::su::make_unique<vk::raii::DescriptorPool>( *device, descriptorPoolCreateInfo ); vk::DescriptorSetLayoutCreateInfo descriptorSetLayoutCreateInfo( {}, bindings );
vk::DescriptorSetLayoutCreateInfo descriptorSetLayoutCreateInfo( {}, bindings ); vk::raii::DescriptorSetLayout rayTracingDescriptorSetLayout( device, descriptorSetLayoutCreateInfo );
std::unique_ptr<vk::raii::DescriptorSetLayout> rayTracingDescriptorSetLayout =
vk::raii::su::make_unique<vk::raii::DescriptorSetLayout>( *device, descriptorSetLayoutCreateInfo );
std::vector<vk::DescriptorSetLayout> layouts; std::vector<vk::DescriptorSetLayout> layouts;
for ( size_t i = 0; i < swapChainData.images.size(); i++ ) for ( size_t i = 0; i < swapChainData.images.size(); i++ )
{ {
layouts.push_back( **rayTracingDescriptorSetLayout ); layouts.push_back( *rayTracingDescriptorSetLayout );
} }
vk::DescriptorSetAllocateInfo descriptorSetAllocateInfo( **rayTracingDescriptorPool, layouts ); vk::DescriptorSetAllocateInfo descriptorSetAllocateInfo( *rayTracingDescriptorPool, layouts );
vk::raii::DescriptorSets rayTracingDescriptorSets( *device, descriptorSetAllocateInfo ); vk::raii::DescriptorSets rayTracingDescriptorSets( device, descriptorSetAllocateInfo );
// Bind ray tracing specific descriptor sets into pNext of a vk::WriteDescriptorSet // Bind ray tracing specific descriptor sets into pNext of a vk::WriteDescriptorSet
vk::WriteDescriptorSetAccelerationStructureNV writeDescriptorSetAcceleration( 1, vk::WriteDescriptorSetAccelerationStructureNV writeDescriptorSetAcceleration( 1,
&**topLevelAS.acclerationStructure ); &**topLevelAS.pAcclerationStructure );
std::vector<vk::WriteDescriptorSet> accelerationDescriptionSets; std::vector<vk::WriteDescriptorSet> accelerationDescriptionSets;
for ( size_t i = 0; i < rayTracingDescriptorSets.size(); i++ ) for ( size_t i = 0; i < rayTracingDescriptorSets.size(); i++ )
{ {
accelerationDescriptionSets.emplace_back( *rayTracingDescriptorSets[i], 0, 0, 1, bindings[0].descriptorType ); accelerationDescriptionSets.emplace_back( *rayTracingDescriptorSets[i], 0, 0, 1, bindings[0].descriptorType );
accelerationDescriptionSets.back().pNext = &writeDescriptorSetAcceleration; accelerationDescriptionSets.back().pNext = &writeDescriptorSetAcceleration;
} }
device->updateDescriptorSets( accelerationDescriptionSets, nullptr ); device.updateDescriptorSets( accelerationDescriptionSets, nullptr );
// Bind all the other buffers and images, starting with dstBinding == 2 (dstBinding == 1 is used by the backBuffer // Bind all the other buffers and images, starting with dstBinding == 2 (dstBinding == 1 is used by the backBuffer
// view) // view)
for ( size_t i = 0; i < rayTracingDescriptorSets.size(); i++ ) for ( size_t i = 0; i < rayTracingDescriptorSets.size(); i++ )
{ {
vk::raii::su::updateDescriptorSets( *device, vk::raii::su::updateDescriptorSets( device,
rayTracingDescriptorSets[i], rayTracingDescriptorSets[i],
{ { bindings[2].descriptorType, *uniformBufferData.buffer, {} }, { { bindings[2].descriptorType, uniformBufferData.buffer, {} },
{ bindings[3].descriptorType, *vertexBufferData.buffer, {} }, { bindings[3].descriptorType, vertexBufferData.buffer, {} },
{ bindings[4].descriptorType, *indexBufferData.buffer, {} }, { bindings[4].descriptorType, indexBufferData.buffer, {} },
{ bindings[5].descriptorType, *materialBufferData.buffer, {} } }, { bindings[5].descriptorType, materialBufferData.buffer, {} } },
textures, textures,
2 ); 2 );
} }
// create the ray-tracing shader modules // create the ray-tracing shader modules
glslang::InitializeProcess(); glslang::InitializeProcess();
std::unique_ptr<vk::raii::ShaderModule> raygenShaderModule = vk::raii::ShaderModule raygenShaderModule =
vk::raii::su::makeUniqueShaderModule( *device, vk::ShaderStageFlagBits::eRaygenNV, raygenShaderText ); vk::raii::su::makeShaderModule( device, vk::ShaderStageFlagBits::eRaygenNV, raygenShaderText );
std::unique_ptr<vk::raii::ShaderModule> missShaderModule = vk::raii::ShaderModule missShaderModule =
vk::raii::su::makeUniqueShaderModule( *device, vk::ShaderStageFlagBits::eMissNV, missShaderText ); vk::raii::su::makeShaderModule( device, vk::ShaderStageFlagBits::eMissNV, missShaderText );
std::unique_ptr<vk::raii::ShaderModule> shadowMissShaderModule = vk::raii::ShaderModule shadowMissShaderModule =
vk::raii::su::makeUniqueShaderModule( *device, vk::ShaderStageFlagBits::eMissNV, shadowMissShaderText ); vk::raii::su::makeShaderModule( device, vk::ShaderStageFlagBits::eMissNV, shadowMissShaderText );
std::unique_ptr<vk::raii::ShaderModule> closestHitShaderModule = vk::raii::ShaderModule closestHitShaderModule =
vk::raii::su::makeUniqueShaderModule( *device, vk::ShaderStageFlagBits::eClosestHitNV, closestHitShaderText ); vk::raii::su::makeShaderModule( device, vk::ShaderStageFlagBits::eClosestHitNV, closestHitShaderText );
glslang::FinalizeProcess(); glslang::FinalizeProcess();
// create the ray tracing pipeline // create the ray tracing pipeline
@ -1083,20 +1084,20 @@ int main( int /*argc*/, char ** /*argv*/ )
// We use only one ray generation, that will implement the camera model // We use only one ray generation, that will implement the camera model
shaderStages.emplace_back( shaderStages.emplace_back(
vk::PipelineShaderStageCreateFlags(), vk::ShaderStageFlagBits::eRaygenNV, **raygenShaderModule, "main" ); vk::PipelineShaderStageCreateFlags(), vk::ShaderStageFlagBits::eRaygenNV, *raygenShaderModule, "main" );
shaderGroups.emplace_back( shaderGroups.emplace_back(
vk::RayTracingShaderGroupTypeNV::eGeneral, 0, VK_SHADER_UNUSED_NV, VK_SHADER_UNUSED_NV, VK_SHADER_UNUSED_NV ); vk::RayTracingShaderGroupTypeNV::eGeneral, 0, VK_SHADER_UNUSED_NV, VK_SHADER_UNUSED_NV, VK_SHADER_UNUSED_NV );
// The first miss shader is used to look-up the environment in case the rays from the camera miss the geometry // The first miss shader is used to look-up the environment in case the rays from the camera miss the geometry
shaderStages.emplace_back( shaderStages.emplace_back(
vk::PipelineShaderStageCreateFlags(), vk::ShaderStageFlagBits::eMissNV, **missShaderModule, "main" ); vk::PipelineShaderStageCreateFlags(), vk::ShaderStageFlagBits::eMissNV, *missShaderModule, "main" );
shaderGroups.emplace_back( shaderGroups.emplace_back(
vk::RayTracingShaderGroupTypeNV::eGeneral, 1, VK_SHADER_UNUSED_NV, VK_SHADER_UNUSED_NV, VK_SHADER_UNUSED_NV ); vk::RayTracingShaderGroupTypeNV::eGeneral, 1, VK_SHADER_UNUSED_NV, VK_SHADER_UNUSED_NV, VK_SHADER_UNUSED_NV );
// The second miss shader is invoked when a shadow ray misses the geometry. It simply indicates that no occlusion // The second miss shader is invoked when a shadow ray misses the geometry. It simply indicates that no occlusion
// has been found // has been found
shaderStages.emplace_back( shaderStages.emplace_back(
vk::PipelineShaderStageCreateFlags(), vk::ShaderStageFlagBits::eMissNV, **shadowMissShaderModule, "main" ); vk::PipelineShaderStageCreateFlags(), vk::ShaderStageFlagBits::eMissNV, *shadowMissShaderModule, "main" );
shaderGroups.emplace_back( shaderGroups.emplace_back(
vk::RayTracingShaderGroupTypeNV::eGeneral, 2, VK_SHADER_UNUSED_NV, VK_SHADER_UNUSED_NV, VK_SHADER_UNUSED_NV ); vk::RayTracingShaderGroupTypeNV::eGeneral, 2, VK_SHADER_UNUSED_NV, VK_SHADER_UNUSED_NV, VK_SHADER_UNUSED_NV );
@ -1104,7 +1105,7 @@ int main( int /*argc*/, char ** /*argv*/ )
// only specify the closest hit shader, and rely on the build-in triangle intersection and pass-through any-hit // only specify the closest hit shader, and rely on the build-in triangle intersection and pass-through any-hit
// shader. However, explicit intersection and any hit shaders could be added as well. // shader. However, explicit intersection and any hit shaders could be added as well.
shaderStages.emplace_back( shaderStages.emplace_back(
vk::PipelineShaderStageCreateFlags(), vk::ShaderStageFlagBits::eClosestHitNV, **closestHitShaderModule, "main" ); vk::PipelineShaderStageCreateFlags(), vk::ShaderStageFlagBits::eClosestHitNV, *closestHitShaderModule, "main" );
shaderGroups.emplace_back( vk::RayTracingShaderGroupTypeNV::eTrianglesHitGroup, shaderGroups.emplace_back( vk::RayTracingShaderGroupTypeNV::eTrianglesHitGroup,
VK_SHADER_UNUSED_NV, VK_SHADER_UNUSED_NV,
3, 3,
@ -1120,8 +1121,7 @@ int main( int /*argc*/, char ** /*argv*/ )
VK_SHADER_UNUSED_NV ); VK_SHADER_UNUSED_NV );
// Create the layout of the pipeline following the provided descriptor set layout // Create the layout of the pipeline following the provided descriptor set layout
std::unique_ptr<vk::raii::PipelineLayout> rayTracingPipelineLayout = vk::raii::PipelineLayout rayTracingPipelineLayout( device, { {}, *rayTracingDescriptorSetLayout } );
vk::raii::su::makeUniquePipelineLayout( *device, *rayTracingDescriptorSetLayout );
// Assemble the shader stages and recursion depth info into the raytracing pipeline // Assemble the shader stages and recursion depth info into the raytracing pipeline
// The ray tracing process can shoot rays from the camera, and a shadow ray can be shot from the // The ray tracing process can shoot rays from the camera, and a shadow ray can be shot from the
@ -1130,10 +1130,9 @@ int main( int /*argc*/, char ** /*argv*/ )
// in the ray generation to avoid deep recursion. // in the ray generation to avoid deep recursion.
uint32_t maxRecursionDepth = 2; uint32_t maxRecursionDepth = 2;
vk::RayTracingPipelineCreateInfoNV rayTracingPipelineCreateInfo( vk::RayTracingPipelineCreateInfoNV rayTracingPipelineCreateInfo(
{}, shaderStages, shaderGroups, maxRecursionDepth, **rayTracingPipelineLayout ); {}, shaderStages, shaderGroups, maxRecursionDepth, *rayTracingPipelineLayout );
std::unique_ptr<vk::raii::Pipeline> rayTracingPipeline = vk::raii::Pipeline rayTracingPipeline( device, nullptr, rayTracingPipelineCreateInfo );
vk::raii::su::make_unique<vk::raii::Pipeline>( *device, nullptr, rayTracingPipelineCreateInfo ); switch ( rayTracingPipeline.getConstructorSuccessCode() )
switch ( rayTracingPipeline->getConstructorSuccessCode() )
{ {
case vk::Result::eSuccess: break; case vk::Result::eSuccess: break;
case vk::Result::ePipelineCompileRequiredEXT: case vk::Result::ePipelineCompileRequiredEXT:
@ -1143,7 +1142,7 @@ int main( int /*argc*/, char ** /*argv*/ )
} }
vk::StructureChain<vk::PhysicalDeviceProperties2, vk::PhysicalDeviceRayTracingPropertiesNV> propertiesChain = vk::StructureChain<vk::PhysicalDeviceProperties2, vk::PhysicalDeviceRayTracingPropertiesNV> propertiesChain =
physicalDevice->getProperties2<vk::PhysicalDeviceProperties2, vk::PhysicalDeviceRayTracingPropertiesNV>(); physicalDevice.getProperties2<vk::PhysicalDeviceProperties2, vk::PhysicalDeviceRayTracingPropertiesNV>();
uint32_t shaderGroupBaseAlignment = uint32_t shaderGroupBaseAlignment =
propertiesChain.get<vk::PhysicalDeviceRayTracingPropertiesNV>().shaderGroupBaseAlignment; propertiesChain.get<vk::PhysicalDeviceRayTracingPropertiesNV>().shaderGroupBaseAlignment;
uint32_t shaderGroupHandleSize = uint32_t shaderGroupHandleSize =
@ -1163,18 +1162,18 @@ int main( int /*argc*/, char ** /*argv*/ )
uint32_t shaderBindingTableSize = hitShaderBindingOffset + hitShaderTableSize; uint32_t shaderBindingTableSize = hitShaderBindingOffset + hitShaderTableSize;
std::vector<uint8_t> shaderHandleStorage( shaderBindingTableSize ); std::vector<uint8_t> shaderHandleStorage( shaderBindingTableSize );
memcpy( &shaderHandleStorage[raygenShaderBindingOffset], memcpy( &shaderHandleStorage[raygenShaderBindingOffset],
rayTracingPipeline->getRayTracingShaderGroupHandlesKHR<uint8_t>( 0, 1, raygenShaderTableSize ).data(), rayTracingPipeline.getRayTracingShaderGroupHandlesKHR<uint8_t>( 0, 1, raygenShaderTableSize ).data(),
raygenShaderTableSize ); raygenShaderTableSize );
memcpy( &shaderHandleStorage[missShaderBindingOffset], memcpy( &shaderHandleStorage[missShaderBindingOffset],
rayTracingPipeline->getRayTracingShaderGroupHandlesKHR<uint8_t>( 1, 2, missShaderTableSize ).data(), rayTracingPipeline.getRayTracingShaderGroupHandlesKHR<uint8_t>( 1, 2, missShaderTableSize ).data(),
missShaderTableSize ); missShaderTableSize );
memcpy( &shaderHandleStorage[hitShaderBindingOffset], memcpy( &shaderHandleStorage[hitShaderBindingOffset],
rayTracingPipeline->getRayTracingShaderGroupHandlesKHR<uint8_t>( 3, 2, hitShaderTableSize ).data(), rayTracingPipeline.getRayTracingShaderGroupHandlesKHR<uint8_t>( 3, 2, hitShaderTableSize ).data(),
hitShaderTableSize ); hitShaderTableSize );
assert( shaderHandleStorage.size() == shaderBindingTableSize ); assert( shaderHandleStorage.size() == shaderBindingTableSize );
vk::raii::su::BufferData shaderBindingTableBufferData( *physicalDevice, vk::raii::su::BufferData shaderBindingTableBufferData( physicalDevice,
*device, device,
shaderBindingTableSize, shaderBindingTableSize,
vk::BufferUsageFlagBits::eTransferDst, vk::BufferUsageFlagBits::eTransferDst,
vk::MemoryPropertyFlagBits::eHostVisible ); vk::MemoryPropertyFlagBits::eHostVisible );
@ -1197,7 +1196,7 @@ int main( int /*argc*/, char ** /*argv*/ )
double startTime = glfwGetTime(); double startTime = glfwGetTime();
glfwPollEvents(); glfwPollEvents();
vk::raii::CommandBuffer const & commandBuffer = *perFrameData[frameIndex].commandBuffer; vk::raii::CommandBuffer const & commandBuffer = perFrameData[frameIndex].commandBuffer;
int w, h; int w, h;
glfwGetWindowSize( window, &w, &h ); glfwGetWindowSize( window, &w, &h );
@ -1205,30 +1204,32 @@ int main( int /*argc*/, char ** /*argv*/ )
{ {
windowExtent.width = w; windowExtent.width = w;
windowExtent.height = h; windowExtent.height = h;
device->waitIdle(); device.waitIdle();
swapChainData = swapChainData =
vk::raii::su::SwapChainData( *physicalDevice, vk::raii::su::SwapChainData( physicalDevice,
*device, device,
*surface, surface,
windowExtent, windowExtent,
vk::ImageUsageFlagBits::eColorAttachment | vk::ImageUsageFlagBits::eStorage, vk::ImageUsageFlagBits::eColorAttachment | vk::ImageUsageFlagBits::eStorage,
swapChainData.swapChain, &*swapChainData.pSwapChain,
graphicsAndPresentQueueFamilyIndex.first, graphicsAndPresentQueueFamilyIndex.first,
graphicsAndPresentQueueFamilyIndex.second ); graphicsAndPresentQueueFamilyIndex.second );
depthBufferData = vk::raii::su::DepthBufferData( depthBufferData = vk::raii::su::DepthBufferData(
*physicalDevice, *device, vk::su::pickDepthFormat( **physicalDevice ), windowExtent ); physicalDevice, device, vk::su::pickDepthFormat( *physicalDevice ), windowExtent );
vk::raii::su::oneTimeSubmit( vk::raii::su::oneTimeSubmit( commandBuffer,
commandBuffer, *graphicsQueue, [&]( vk::raii::CommandBuffer const & commandBuffer ) { graphicsQueue,
vk::raii::su::setImageLayout( commandBuffer, [&]( vk::raii::CommandBuffer const & commandBuffer )
**depthBufferData.image, {
depthFormat, vk::raii::su::setImageLayout( commandBuffer,
vk::ImageLayout::eUndefined, *depthBufferData.image,
vk::ImageLayout::eDepthStencilAttachmentOptimal ); depthFormat,
} ); vk::ImageLayout::eUndefined,
vk::ImageLayout::eDepthStencilAttachmentOptimal );
} );
framebuffers = vk::raii::su::makeUniqueFramebuffers( framebuffers = vk::raii::su::makeFramebuffers(
*device, *renderPass, swapChainData.imageViews, depthBufferData.imageView, windowExtent ); device, renderPass, swapChainData.imageViews, &*depthBufferData.pImageView, windowExtent );
} }
// update the uniformBufferObject // update the uniformBufferObject
@ -1244,28 +1245,26 @@ int main( int /*argc*/, char ** /*argv*/ )
// frame begin // frame begin
vk::Result result; vk::Result result;
uint32_t backBufferIndex; uint32_t backBufferIndex;
std::tie( result, backBufferIndex ) = swapChainData.swapChain->acquireNextImage( std::tie( result, backBufferIndex ) = swapChainData.pSwapChain->acquireNextImage(
vk::su::FenceTimeout, **perFrameData[frameIndex].presentCompleteSemaphore ); vk::su::FenceTimeout, *perFrameData[frameIndex].presentCompleteSemaphore );
assert( result == vk::Result::eSuccess ); assert( result == vk::Result::eSuccess );
while ( vk::Result::eTimeout == while ( vk::Result::eTimeout ==
device->waitForFences( { **perFrameData[frameIndex].fence }, VK_TRUE, vk::su::FenceTimeout ) ) device.waitForFences( { *perFrameData[frameIndex].fence }, VK_TRUE, vk::su::FenceTimeout ) )
; ;
device->resetFences( { **perFrameData[frameIndex].fence } ); device.resetFences( { *perFrameData[frameIndex].fence } );
commandBuffer.begin( vk::CommandBufferBeginInfo( vk::CommandBufferUsageFlagBits::eOneTimeSubmit ) ); commandBuffer.begin( vk::CommandBufferBeginInfo( vk::CommandBufferUsageFlagBits::eOneTimeSubmit ) );
if ( appInfo.useRasterRender ) if ( appInfo.useRasterRender )
{ {
vk::RenderPassBeginInfo renderPassBeginInfo( **renderPass, vk::RenderPassBeginInfo renderPassBeginInfo(
**framebuffers[backBufferIndex], *renderPass, *framebuffers[backBufferIndex], vk::Rect2D( vk::Offset2D( 0, 0 ), windowExtent ), clearValues );
vk::Rect2D( vk::Offset2D( 0, 0 ), windowExtent ),
clearValues );
commandBuffer.beginRenderPass( renderPassBeginInfo, vk::SubpassContents::eInline ); commandBuffer.beginRenderPass( renderPassBeginInfo, vk::SubpassContents::eInline );
commandBuffer.bindPipeline( vk::PipelineBindPoint::eGraphics, **graphicsPipeline ); commandBuffer.bindPipeline( vk::PipelineBindPoint::eGraphics, *graphicsPipeline );
commandBuffer.bindDescriptorSets( commandBuffer.bindDescriptorSets(
vk::PipelineBindPoint::eGraphics, **pipelineLayout, 0, { **descriptorSet }, nullptr ); vk::PipelineBindPoint::eGraphics, *pipelineLayout, 0, { *descriptorSet }, nullptr );
commandBuffer.setViewport( 0, commandBuffer.setViewport( 0,
vk::Viewport( 0.0f, vk::Viewport( 0.0f,
@ -1276,8 +1275,8 @@ int main( int /*argc*/, char ** /*argv*/ )
1.0f ) ); 1.0f ) );
commandBuffer.setScissor( 0, vk::Rect2D( vk::Offset2D( 0, 0 ), windowExtent ) ); commandBuffer.setScissor( 0, vk::Rect2D( vk::Offset2D( 0, 0 ), windowExtent ) );
commandBuffer.bindVertexBuffers( 0, { **vertexBufferData.buffer }, { 0 } ); commandBuffer.bindVertexBuffers( 0, { *vertexBufferData.buffer }, { 0 } );
commandBuffer.bindIndexBuffer( **indexBufferData.buffer, 0, vk::IndexType::eUint32 ); commandBuffer.bindIndexBuffer( *indexBufferData.buffer, 0, vk::IndexType::eUint32 );
commandBuffer.drawIndexed( vk::su::checked_cast<uint32_t>( indices.size() ), 1, 0, 0, 0 ); commandBuffer.drawIndexed( vk::su::checked_cast<uint32_t>( indices.size() ), 1, 0, 0, 0 );
commandBuffer.endRenderPass(); commandBuffer.endRenderPass();
@ -1288,7 +1287,7 @@ int main( int /*argc*/, char ** /*argv*/ )
nullptr, *swapChainData.imageViews[backBufferIndex], vk::ImageLayout::eGeneral ); nullptr, *swapChainData.imageViews[backBufferIndex], vk::ImageLayout::eGeneral );
vk::WriteDescriptorSet writeDescriptorSet( vk::WriteDescriptorSet writeDescriptorSet(
*rayTracingDescriptorSets[backBufferIndex], 1, 0, bindings[1].descriptorType, imageInfo ); *rayTracingDescriptorSets[backBufferIndex], 1, 0, bindings[1].descriptorType, imageInfo );
device->updateDescriptorSets( writeDescriptorSet, nullptr ); device.updateDescriptorSets( writeDescriptorSet, nullptr );
vk::raii::su::setImageLayout( commandBuffer, vk::raii::su::setImageLayout( commandBuffer,
static_cast<vk::Image>( swapChainData.images[backBufferIndex] ), static_cast<vk::Image>( swapChainData.images[backBufferIndex] ),
@ -1296,20 +1295,20 @@ int main( int /*argc*/, char ** /*argv*/ )
vk::ImageLayout::eUndefined, vk::ImageLayout::eUndefined,
vk::ImageLayout::eGeneral ); vk::ImageLayout::eGeneral );
commandBuffer.bindPipeline( vk::PipelineBindPoint::eRayTracingNV, **rayTracingPipeline ); commandBuffer.bindPipeline( vk::PipelineBindPoint::eRayTracingNV, *rayTracingPipeline );
commandBuffer.bindDescriptorSets( vk::PipelineBindPoint::eRayTracingNV, commandBuffer.bindDescriptorSets( vk::PipelineBindPoint::eRayTracingNV,
**rayTracingPipelineLayout, *rayTracingPipelineLayout,
0, 0,
{ *rayTracingDescriptorSets[backBufferIndex] }, { *rayTracingDescriptorSets[backBufferIndex] },
nullptr ); nullptr );
commandBuffer.traceRaysNV( **shaderBindingTableBufferData.buffer, commandBuffer.traceRaysNV( *shaderBindingTableBufferData.buffer,
raygenShaderBindingOffset, raygenShaderBindingOffset,
**shaderBindingTableBufferData.buffer, *shaderBindingTableBufferData.buffer,
missShaderBindingOffset, missShaderBindingOffset,
missShaderBindingStride, missShaderBindingStride,
**shaderBindingTableBufferData.buffer, *shaderBindingTableBufferData.buffer,
hitShaderBindingOffset, hitShaderBindingOffset,
hitShaderBindingStride, hitShaderBindingStride,
nullptr, nullptr,
@ -1329,14 +1328,17 @@ int main( int /*argc*/, char ** /*argv*/ )
// frame end // frame end
commandBuffer.end(); commandBuffer.end();
const vk::PipelineStageFlags waitDestinationStageMask = vk::PipelineStageFlagBits::eColorAttachmentOutput; const vk::PipelineStageFlags waitDestinationStageMask = vk::PipelineStageFlagBits::eColorAttachmentOutput;
vk::SubmitInfo submitInfo( **perFrameData[frameIndex].presentCompleteSemaphore, vk::SubmitInfo submitInfo( 1,
waitDestinationStageMask, &*perFrameData[frameIndex].presentCompleteSemaphore,
*commandBuffer, &waitDestinationStageMask,
**perFrameData[frameIndex].renderCompleteSemaphore ); 1,
graphicsQueue->submit( submitInfo, **perFrameData[frameIndex].fence ); &*commandBuffer,
1,
&*perFrameData[frameIndex].renderCompleteSemaphore );
graphicsQueue.submit( submitInfo, *perFrameData[frameIndex].fence );
vk::PresentInfoKHR presentInfoKHR( vk::PresentInfoKHR presentInfoKHR(
**perFrameData[frameIndex].renderCompleteSemaphore, **swapChainData.swapChain, backBufferIndex ); *perFrameData[frameIndex].renderCompleteSemaphore, **swapChainData.pSwapChain, backBufferIndex );
result = presentQueue->presentKHR( presentInfoKHR ); result = presentQueue.presentKHR( presentInfoKHR );
switch ( result ) switch ( result )
{ {
case vk::Result::eSuccess: break; case vk::Result::eSuccess: break;
@ -1366,7 +1368,7 @@ int main( int /*argc*/, char ** /*argv*/ )
} }
// Cleanup // Cleanup
device->waitIdle(); device.waitIdle();
glfwDestroyWindow( window ); glfwDestroyWindow( window );
glfwTerminate(); glfwTerminate();

View File

@ -42,35 +42,31 @@ int main( int /*argc*/, char ** /*argv*/ )
{ {
try try
{ {
std::unique_ptr<vk::raii::Context> context = vk::raii::su::make_unique<vk::raii::Context>(); vk::raii::Context context;
std::unique_ptr<vk::raii::Instance> instance = vk::raii::Instance instance =
vk::raii::su::makeUniqueInstance( *context, AppName, EngineName, {}, vk::su::getInstanceExtensions() ); vk::raii::su::makeInstance( context, AppName, EngineName, {}, vk::su::getInstanceExtensions() );
#if !defined( NDEBUG ) #if !defined( NDEBUG )
std::unique_ptr<vk::raii::DebugUtilsMessengerEXT> debugUtilsMessenger = vk::raii::DebugUtilsMessengerEXT debugUtilsMessenger( instance, vk::su::makeDebugUtilsMessengerCreateInfoEXT() );
vk::raii::su::makeUniqueDebugUtilsMessengerEXT( *instance );
#endif #endif
std::unique_ptr<vk::raii::PhysicalDevice> physicalDevice = vk::raii::su::makeUniquePhysicalDevice( *instance ); vk::raii::PhysicalDevice physicalDevice = std::move( vk::raii::PhysicalDevices( instance ).front() );
vk::raii::su::SurfaceData surfaceData( *instance, AppName, vk::Extent2D( 500, 500 ) ); vk::raii::su::SurfaceData surfaceData( instance, AppName, vk::Extent2D( 500, 500 ) );
std::pair<uint32_t, uint32_t> graphicsAndPresentQueueFamilyIndex = std::pair<uint32_t, uint32_t> graphicsAndPresentQueueFamilyIndex =
vk::raii::su::findGraphicsAndPresentQueueFamilyIndex( *physicalDevice, *surfaceData.surface ); vk::raii::su::findGraphicsAndPresentQueueFamilyIndex( physicalDevice, *surfaceData.pSurface );
std::unique_ptr<vk::raii::Device> device = vk::raii::su::makeUniqueDevice( vk::raii::Device device = vk::raii::su::makeDevice(
*physicalDevice, graphicsAndPresentQueueFamilyIndex.first, vk::su::getDeviceExtensions() ); physicalDevice, graphicsAndPresentQueueFamilyIndex.first, vk::su::getDeviceExtensions() );
std::unique_ptr<vk::raii::CommandPool> commandPool = vk::raii::CommandPool commandPool = vk::raii::CommandPool(
vk::raii::su::makeUniqueCommandPool( *device, graphicsAndPresentQueueFamilyIndex.first ); device, { vk::CommandPoolCreateFlagBits::eResetCommandBuffer, graphicsAndPresentQueueFamilyIndex.first } );
std::unique_ptr<vk::raii::CommandBuffer> commandBuffer = vk::raii::CommandBuffer commandBuffer = vk::raii::su::makeCommandBuffer( device, commandPool );
vk::raii::su::makeUniqueCommandBuffer( *device, *commandPool );
std::unique_ptr<vk::raii::Queue> graphicsQueue = vk::raii::Queue graphicsQueue( device, graphicsAndPresentQueueFamilyIndex.first, 0 );
vk::raii::su::make_unique<vk::raii::Queue>( *device, graphicsAndPresentQueueFamilyIndex.first, 0 ); vk::raii::Queue presentQueue( device, graphicsAndPresentQueueFamilyIndex.second, 0 );
std::unique_ptr<vk::raii::Queue> presentQueue =
vk::raii::su::make_unique<vk::raii::Queue>( *device, graphicsAndPresentQueueFamilyIndex.second, 0 );
vk::raii::su::SwapChainData swapChainData( *physicalDevice, vk::raii::su::SwapChainData swapChainData( physicalDevice,
*device, device,
*surfaceData.surface, *surfaceData.pSurface,
surfaceData.extent, surfaceData.extent,
vk::ImageUsageFlagBits::eColorAttachment | vk::ImageUsageFlagBits::eColorAttachment |
vk::ImageUsageFlagBits::eTransferSrc, vk::ImageUsageFlagBits::eTransferSrc,
@ -78,104 +74,99 @@ int main( int /*argc*/, char ** /*argv*/ )
graphicsAndPresentQueueFamilyIndex.first, graphicsAndPresentQueueFamilyIndex.first,
graphicsAndPresentQueueFamilyIndex.second ); graphicsAndPresentQueueFamilyIndex.second );
vk::raii::su::DepthBufferData depthBufferData( vk::raii::su::DepthBufferData depthBufferData( physicalDevice, device, vk::Format::eD16Unorm, surfaceData.extent );
*physicalDevice, *device, vk::Format::eD16Unorm, surfaceData.extent );
vk::raii::su::BufferData uniformBufferData( vk::raii::su::BufferData uniformBufferData(
*physicalDevice, *device, sizeof( glm::mat4x4 ), vk::BufferUsageFlagBits::eUniformBuffer ); physicalDevice, device, sizeof( glm::mat4x4 ), vk::BufferUsageFlagBits::eUniformBuffer );
glm::mat4x4 mvpcMatrix = vk::su::createModelViewProjectionClipMatrix( surfaceData.extent ); glm::mat4x4 mvpcMatrix = vk::su::createModelViewProjectionClipMatrix( surfaceData.extent );
vk::raii::su::copyToDevice( *uniformBufferData.deviceMemory, mvpcMatrix ); vk::raii::su::copyToDevice( uniformBufferData.deviceMemory, mvpcMatrix );
std::unique_ptr<vk::raii::DescriptorSetLayout> descriptorSetLayout = vk::raii::su::makeUniqueDescriptorSetLayout( vk::raii::DescriptorSetLayout descriptorSetLayout = vk::raii::su::makeDescriptorSetLayout(
*device, device,
{ { vk::DescriptorType::eUniformBuffer, 1, vk::ShaderStageFlagBits::eVertex }, { { vk::DescriptorType::eUniformBuffer, 1, vk::ShaderStageFlagBits::eVertex },
{ vk::DescriptorType::eCombinedImageSampler, 1, vk::ShaderStageFlagBits::eFragment } } ); { vk::DescriptorType::eCombinedImageSampler, 1, vk::ShaderStageFlagBits::eFragment } } );
std::unique_ptr<vk::raii::PipelineLayout> pipelineLayout = vk::raii::PipelineLayout pipelineLayout( device, { {}, *descriptorSetLayout } );
vk::raii::su::makeUniquePipelineLayout( *device, *descriptorSetLayout );
vk::Format colorFormat = vk::Format colorFormat =
vk::su::pickSurfaceFormat( physicalDevice->getSurfaceFormatsKHR( **surfaceData.surface ) ).format; vk::su::pickSurfaceFormat( physicalDevice.getSurfaceFormatsKHR( **surfaceData.pSurface ) ).format;
std::unique_ptr<vk::raii::RenderPass> renderPass = vk::raii::RenderPass renderPass = vk::raii::su::makeRenderPass( device,
vk::raii::su::makeUniqueRenderPass( *device, colorFormat,
colorFormat, depthBufferData.format,
depthBufferData.format, vk::AttachmentLoadOp::eClear,
vk::AttachmentLoadOp::eClear, vk::ImageLayout::eColorAttachmentOptimal );
vk::ImageLayout::eColorAttachmentOptimal );
glslang::InitializeProcess(); glslang::InitializeProcess();
std::unique_ptr<vk::raii::ShaderModule> vertexShaderModule = vk::raii::ShaderModule vertexShaderModule =
vk::raii::su::makeUniqueShaderModule( *device, vk::ShaderStageFlagBits::eVertex, vertexShaderText_PT_T ); vk::raii::su::makeShaderModule( device, vk::ShaderStageFlagBits::eVertex, vertexShaderText_PT_T );
std::unique_ptr<vk::raii::ShaderModule> fragmentShaderModule = vk::raii::ShaderModule fragmentShaderModule =
vk::raii::su::makeUniqueShaderModule( *device, vk::ShaderStageFlagBits::eFragment, fragmentShaderText_T_C ); vk::raii::su::makeShaderModule( device, vk::ShaderStageFlagBits::eFragment, fragmentShaderText_T_C );
glslang::FinalizeProcess(); glslang::FinalizeProcess();
std::vector<std::unique_ptr<vk::raii::Framebuffer>> framebuffers = vk::raii::su::makeUniqueFramebuffers( std::vector<vk::raii::Framebuffer> framebuffers = vk::raii::su::makeFramebuffers(
*device, *renderPass, swapChainData.imageViews, depthBufferData.imageView, surfaceData.extent ); device, renderPass, swapChainData.imageViews, &*depthBufferData.pImageView, surfaceData.extent );
vk::raii::su::BufferData vertexBufferData( vk::raii::su::BufferData vertexBufferData(
*physicalDevice, *device, sizeof( texturedCubeData ), vk::BufferUsageFlagBits::eVertexBuffer ); physicalDevice, device, sizeof( texturedCubeData ), vk::BufferUsageFlagBits::eVertexBuffer );
vk::raii::su::copyToDevice( vk::raii::su::copyToDevice(
*vertexBufferData.deviceMemory, texturedCubeData, sizeof( texturedCubeData ) / sizeof( texturedCubeData[0] ) ); vertexBufferData.deviceMemory, texturedCubeData, sizeof( texturedCubeData ) / sizeof( texturedCubeData[0] ) );
std::unique_ptr<vk::raii::PipelineCache> pipelineCache = vk::raii::PipelineCache pipelineCache( device, vk::PipelineCacheCreateInfo() );
vk::raii::su::make_unique<vk::raii::PipelineCache>( *device, vk::PipelineCacheCreateInfo() ); vk::raii::Pipeline graphicsPipeline =
std::unique_ptr<vk::raii::Pipeline> graphicsPipeline = vk::raii::su::makeUniqueGraphicsPipeline( vk::raii::su::makeGraphicsPipeline( device,
*device, pipelineCache,
*pipelineCache, vertexShaderModule,
*vertexShaderModule, nullptr,
nullptr, fragmentShaderModule,
*fragmentShaderModule, nullptr,
nullptr, sizeof( texturedCubeData[0] ),
sizeof( texturedCubeData[0] ), { { vk::Format::eR32G32B32A32Sfloat, 0 }, { vk::Format::eR32G32Sfloat, 16 } },
{ { vk::Format::eR32G32B32A32Sfloat, 0 }, { vk::Format::eR32G32Sfloat, 16 } }, vk::FrontFace::eClockwise,
vk::FrontFace::eClockwise, true,
true, pipelineLayout,
*pipelineLayout, renderPass );
*renderPass );
commandBuffer->begin( vk::CommandBufferBeginInfo() ); commandBuffer.begin( vk::CommandBufferBeginInfo() );
vk::raii::su::TextureData greenTextureData( *physicalDevice, *device ); vk::raii::su::TextureData greenTextureData( physicalDevice, device );
greenTextureData.setImage( *commandBuffer, vk::su::MonochromeImageGenerator( { 118, 185, 0 } ) ); greenTextureData.setImage( commandBuffer, vk::su::MonochromeImageGenerator( { 118, 185, 0 } ) );
vk::raii::su::TextureData checkeredTextureData( *physicalDevice, *device ); vk::raii::su::TextureData checkeredTextureData( physicalDevice, device );
checkeredTextureData.setImage( *commandBuffer, vk::su::CheckerboardImageGenerator() ); checkeredTextureData.setImage( commandBuffer, vk::su::CheckerboardImageGenerator() );
// create two identical descriptor sets, each with a different texture but identical UBOs // create two identical descriptor sets, each with a different texture but identical UBOs
std::unique_ptr<vk::raii::DescriptorPool> descriptorPool = vk::raii::su::makeUniqueDescriptorPool( vk::raii::DescriptorPool descriptorPool = vk::raii::su::makeDescriptorPool(
*device, { { vk::DescriptorType::eUniformBuffer, 2 }, { vk::DescriptorType::eCombinedImageSampler, 2 } } ); device, { { vk::DescriptorType::eUniformBuffer, 2 }, { vk::DescriptorType::eCombinedImageSampler, 2 } } );
std::array<vk::DescriptorSetLayout, 2> layouts = { **descriptorSetLayout, **descriptorSetLayout }; std::array<vk::DescriptorSetLayout, 2> layouts = { *descriptorSetLayout, *descriptorSetLayout };
vk::DescriptorSetAllocateInfo descriptorSetAllocateInfo( **descriptorPool, layouts ); vk::DescriptorSetAllocateInfo descriptorSetAllocateInfo( *descriptorPool, layouts );
vk::raii::DescriptorSets descriptorSets( *device, descriptorSetAllocateInfo ); vk::raii::DescriptorSets descriptorSets( device, descriptorSetAllocateInfo );
assert( descriptorSets.size() == 2 ); assert( descriptorSets.size() == 2 );
vk::raii::su::updateDescriptorSets( *device, vk::raii::su::updateDescriptorSets( device,
descriptorSets[0], descriptorSets[0],
{ { vk::DescriptorType::eUniformBuffer, *uniformBufferData.buffer, {} } }, { { vk::DescriptorType::eUniformBuffer, uniformBufferData.buffer, {} } },
greenTextureData ); greenTextureData );
vk::raii::su::updateDescriptorSets( *device, vk::raii::su::updateDescriptorSets( device,
descriptorSets[1], descriptorSets[1],
{ { vk::DescriptorType::eUniformBuffer, *uniformBufferData.buffer, {} } }, { { vk::DescriptorType::eUniformBuffer, uniformBufferData.buffer, {} } },
checkeredTextureData ); checkeredTextureData );
/* VULKAN_KEY_START */ /* VULKAN_KEY_START */
// create four secondary command buffers, for each quadrant of the screen // create four secondary command buffers, for each quadrant of the screen
vk::CommandBufferAllocateInfo commandBufferAllocateInfo( **commandPool, vk::CommandBufferLevel::eSecondary, 4 ); vk::CommandBufferAllocateInfo commandBufferAllocateInfo( *commandPool, vk::CommandBufferLevel::eSecondary, 4 );
vk::raii::CommandBuffers secondaryCommandBuffers( *device, commandBufferAllocateInfo ); vk::raii::CommandBuffers secondaryCommandBuffers( device, commandBufferAllocateInfo );
// Get the index of the next available swapchain image: // Get the index of the next available swapchain image:
std::unique_ptr<vk::raii::Semaphore> imageAcquiredSemaphore = vk::raii::Semaphore imageAcquiredSemaphore( device, vk::SemaphoreCreateInfo() );
vk::raii::su::make_unique<vk::raii::Semaphore>( *device, vk::SemaphoreCreateInfo() ); vk::Result result;
vk::Result result; uint32_t imageIndex;
uint32_t imageIndex;
std::tie( result, imageIndex ) = std::tie( result, imageIndex ) =
swapChainData.swapChain->acquireNextImage( vk::su::FenceTimeout, **imageAcquiredSemaphore ); swapChainData.pSwapChain->acquireNextImage( vk::su::FenceTimeout, *imageAcquiredSemaphore );
assert( result == vk::Result::eSuccess ); assert( result == vk::Result::eSuccess );
assert( imageIndex < swapChainData.images.size() ); assert( imageIndex < swapChainData.images.size() );
vk::raii::su::setImageLayout( *commandBuffer, vk::raii::su::setImageLayout( commandBuffer,
static_cast<vk::Image>( swapChainData.images[imageIndex] ), static_cast<vk::Image>( swapChainData.images[imageIndex] ),
swapChainData.colorFormat, swapChainData.colorFormat,
vk::ImageLayout::eUndefined, vk::ImageLayout::eUndefined,
@ -186,7 +177,7 @@ int main( int /*argc*/, char ** /*argv*/ )
vk::Rect2D scissor( vk::Offset2D( 0, 0 ), vk::Extent2D( surfaceData.extent ) ); vk::Rect2D scissor( vk::Offset2D( 0, 0 ), vk::Extent2D( surfaceData.extent ) );
// now we record four separate command buffers, one for each quadrant of the screen // now we record four separate command buffers, one for each quadrant of the screen
vk::CommandBufferInheritanceInfo commandBufferInheritanceInfo( **renderPass, 0, **framebuffers[imageIndex] ); vk::CommandBufferInheritanceInfo commandBufferInheritanceInfo( *renderPass, 0, *framebuffers[imageIndex] );
vk::CommandBufferBeginInfo secondaryBeginInfo( vk::CommandBufferUsageFlagBits::eOneTimeSubmit | vk::CommandBufferBeginInfo secondaryBeginInfo( vk::CommandBufferUsageFlagBits::eOneTimeSubmit |
vk::CommandBufferUsageFlagBits::eRenderPassContinue, vk::CommandBufferUsageFlagBits::eRenderPassContinue,
&commandBufferInheritanceInfo ); &commandBufferInheritanceInfo );
@ -198,10 +189,10 @@ int main( int /*argc*/, char ** /*argv*/ )
viewport.y = 25.0f + 250.0f * ( i / 2 ); viewport.y = 25.0f + 250.0f * ( i / 2 );
secondaryCommandBuffers[i].begin( secondaryBeginInfo ); secondaryCommandBuffers[i].begin( secondaryBeginInfo );
secondaryCommandBuffers[i].bindPipeline( vk::PipelineBindPoint::eGraphics, **graphicsPipeline ); secondaryCommandBuffers[i].bindPipeline( vk::PipelineBindPoint::eGraphics, *graphicsPipeline );
secondaryCommandBuffers[i].bindDescriptorSets( secondaryCommandBuffers[i].bindDescriptorSets(
vk::PipelineBindPoint::eGraphics, **pipelineLayout, 0, { *descriptorSets[i == 0 || i == 3] }, nullptr ); vk::PipelineBindPoint::eGraphics, *pipelineLayout, 0, { *descriptorSets[i == 0 || i == 3] }, nullptr );
secondaryCommandBuffers[i].bindVertexBuffers( 0, { **vertexBufferData.buffer }, offset ); secondaryCommandBuffers[i].bindVertexBuffers( 0, { *vertexBufferData.buffer }, offset );
secondaryCommandBuffers[i].setViewport( 0, viewport ); secondaryCommandBuffers[i].setViewport( 0, viewport );
secondaryCommandBuffers[i].setScissor( 0, scissor ); secondaryCommandBuffers[i].setScissor( 0, scissor );
secondaryCommandBuffers[i].draw( 12 * 3, 1, 0, 0 ); secondaryCommandBuffers[i].draw( 12 * 3, 1, 0, 0 );
@ -214,13 +205,13 @@ int main( int /*argc*/, char ** /*argv*/ )
clearValues[1].depthStencil = vk::ClearDepthStencilValue( 1.0f, 0 ); clearValues[1].depthStencil = vk::ClearDepthStencilValue( 1.0f, 0 );
vk::RenderPassBeginInfo renderPassBeginInfo( vk::RenderPassBeginInfo renderPassBeginInfo(
**renderPass, **framebuffers[imageIndex], vk::Rect2D( vk::Offset2D( 0, 0 ), surfaceData.extent ), clearValues ); *renderPass, *framebuffers[imageIndex], vk::Rect2D( vk::Offset2D( 0, 0 ), surfaceData.extent ), clearValues );
// specifying VK_SUBPASS_CONTENTS_SECONDARY_COMMAND_BUFFERS means this render pass may ONLY call // specifying VK_SUBPASS_CONTENTS_SECONDARY_COMMAND_BUFFERS means this render pass may ONLY call
// vkCmdExecuteCommands // vkCmdExecuteCommands
commandBuffer->beginRenderPass( renderPassBeginInfo, vk::SubpassContents::eSecondaryCommandBuffers ); commandBuffer.beginRenderPass( renderPassBeginInfo, vk::SubpassContents::eSecondaryCommandBuffers );
commandBuffer->executeCommands( executeCommandBuffers ); commandBuffer.executeCommands( executeCommandBuffers );
commandBuffer->endRenderPass(); commandBuffer.endRenderPass();
vk::ImageSubresourceRange imageSubresourceRange( vk::ImageAspectFlagBits::eColor, 0, 1, 0, 1 ); vk::ImageSubresourceRange imageSubresourceRange( vk::ImageAspectFlagBits::eColor, 0, 1, 0, 1 );
vk::ImageMemoryBarrier prePresentBarrier( vk::AccessFlagBits::eColorAttachmentWrite, vk::ImageMemoryBarrier prePresentBarrier( vk::AccessFlagBits::eColorAttachmentWrite,
@ -231,24 +222,24 @@ int main( int /*argc*/, char ** /*argv*/ )
VK_QUEUE_FAMILY_IGNORED, VK_QUEUE_FAMILY_IGNORED,
static_cast<vk::Image>( swapChainData.images[imageIndex] ), static_cast<vk::Image>( swapChainData.images[imageIndex] ),
imageSubresourceRange ); imageSubresourceRange );
commandBuffer->pipelineBarrier( vk::PipelineStageFlagBits::eColorAttachmentOutput, commandBuffer.pipelineBarrier( vk::PipelineStageFlagBits::eColorAttachmentOutput,
vk::PipelineStageFlagBits::eBottomOfPipe, vk::PipelineStageFlagBits::eBottomOfPipe,
vk::DependencyFlags(), vk::DependencyFlags(),
nullptr, nullptr,
nullptr, nullptr,
prePresentBarrier ); prePresentBarrier );
commandBuffer->end(); commandBuffer.end();
std::unique_ptr<vk::raii::Fence> drawFence = vk::raii::su::make_unique<vk::raii::Fence>( *device, vk::FenceCreateInfo() ); vk::raii::Fence drawFence( device, vk::FenceCreateInfo() );
vk::PipelineStageFlags waitDestinationStageMask( vk::PipelineStageFlagBits::eColorAttachmentOutput ); vk::PipelineStageFlags waitDestinationStageMask( vk::PipelineStageFlagBits::eColorAttachmentOutput );
vk::SubmitInfo submitInfo( **imageAcquiredSemaphore, waitDestinationStageMask, **commandBuffer ); vk::SubmitInfo submitInfo( *imageAcquiredSemaphore, waitDestinationStageMask, *commandBuffer );
graphicsQueue->submit( submitInfo, **drawFence ); graphicsQueue.submit( submitInfo, *drawFence );
while ( vk::Result::eTimeout == device->waitForFences( { **drawFence }, VK_TRUE, vk::su::FenceTimeout ) ) while ( vk::Result::eTimeout == device.waitForFences( { *drawFence }, VK_TRUE, vk::su::FenceTimeout ) )
; ;
result = presentQueue->presentKHR( vk::PresentInfoKHR( {}, **swapChainData.swapChain, imageIndex, {} ) ); result = presentQueue.presentKHR( vk::PresentInfoKHR( {}, **swapChainData.pSwapChain, imageIndex, {} ) );
switch ( result ) switch ( result )
{ {
case vk::Result::eSuccess: break; case vk::Result::eSuccess: break;
@ -261,7 +252,7 @@ int main( int /*argc*/, char ** /*argv*/ )
/* VULKAN_KEY_END */ /* VULKAN_KEY_END */
device->waitIdle(); device.waitIdle();
} }
catch ( vk::SystemError & err ) catch ( vk::SystemError & err )
{ {

View File

@ -70,35 +70,31 @@ int main( int /*argc*/, char ** /*argv*/ )
{ {
try try
{ {
std::unique_ptr<vk::raii::Context> context = vk::raii::su::make_unique<vk::raii::Context>(); vk::raii::Context context;
std::unique_ptr<vk::raii::Instance> instance = vk::raii::Instance instance =
vk::raii::su::makeUniqueInstance( *context, AppName, EngineName, {}, vk::su::getInstanceExtensions() ); vk::raii::su::makeInstance( context, AppName, EngineName, {}, vk::su::getInstanceExtensions() );
#if !defined( NDEBUG ) #if !defined( NDEBUG )
std::unique_ptr<vk::raii::DebugUtilsMessengerEXT> debugUtilsMessenger = vk::raii::DebugUtilsMessengerEXT debugUtilsMessenger( instance, vk::su::makeDebugUtilsMessengerCreateInfoEXT() );
vk::raii::su::makeUniqueDebugUtilsMessengerEXT( *instance );
#endif #endif
std::unique_ptr<vk::raii::PhysicalDevice> physicalDevice = vk::raii::su::makeUniquePhysicalDevice( *instance ); vk::raii::PhysicalDevice physicalDevice = std::move( vk::raii::PhysicalDevices( instance ).front() );
vk::raii::su::SurfaceData surfaceData( *instance, AppName, vk::Extent2D( 500, 500 ) ); vk::raii::su::SurfaceData surfaceData( instance, AppName, vk::Extent2D( 500, 500 ) );
std::pair<uint32_t, uint32_t> graphicsAndPresentQueueFamilyIndex = std::pair<uint32_t, uint32_t> graphicsAndPresentQueueFamilyIndex =
vk::raii::su::findGraphicsAndPresentQueueFamilyIndex( *physicalDevice, *surfaceData.surface ); vk::raii::su::findGraphicsAndPresentQueueFamilyIndex( physicalDevice, *surfaceData.pSurface );
std::unique_ptr<vk::raii::Device> device = vk::raii::su::makeUniqueDevice( vk::raii::Device device = vk::raii::su::makeDevice(
*physicalDevice, graphicsAndPresentQueueFamilyIndex.first, vk::su::getDeviceExtensions() ); physicalDevice, graphicsAndPresentQueueFamilyIndex.first, vk::su::getDeviceExtensions() );
std::unique_ptr<vk::raii::CommandPool> commandPool = vk::raii::CommandPool commandPool = vk::raii::CommandPool(
vk::raii::su::makeUniqueCommandPool( *device, graphicsAndPresentQueueFamilyIndex.first ); device, { vk::CommandPoolCreateFlagBits::eResetCommandBuffer, graphicsAndPresentQueueFamilyIndex.first } );
std::unique_ptr<vk::raii::CommandBuffer> commandBuffer = vk::raii::CommandBuffer commandBuffer = vk::raii::su::makeCommandBuffer( device, commandPool );
vk::raii::su::makeUniqueCommandBuffer( *device, *commandPool );
std::unique_ptr<vk::raii::Queue> graphicsQueue = vk::raii::Queue graphicsQueue( device, graphicsAndPresentQueueFamilyIndex.first, 0 );
vk::raii::su::make_unique<vk::raii::Queue>( *device, graphicsAndPresentQueueFamilyIndex.first, 0 ); vk::raii::Queue presentQueue( device, graphicsAndPresentQueueFamilyIndex.second, 0 );
std::unique_ptr<vk::raii::Queue> presentQueue =
vk::raii::su::make_unique<vk::raii::Queue>( *device, graphicsAndPresentQueueFamilyIndex.second, 0 );
vk::raii::su::SwapChainData swapChainData( *physicalDevice, vk::raii::su::SwapChainData swapChainData( physicalDevice,
*device, device,
*surfaceData.surface, *surfaceData.pSurface,
surfaceData.extent, surfaceData.extent,
vk::ImageUsageFlagBits::eColorAttachment | vk::ImageUsageFlagBits::eColorAttachment |
vk::ImageUsageFlagBits::eTransferSrc, vk::ImageUsageFlagBits::eTransferSrc,
@ -106,44 +102,42 @@ int main( int /*argc*/, char ** /*argv*/ )
graphicsAndPresentQueueFamilyIndex.first, graphicsAndPresentQueueFamilyIndex.first,
graphicsAndPresentQueueFamilyIndex.second ); graphicsAndPresentQueueFamilyIndex.second );
vk::raii::su::DepthBufferData depthBufferData( vk::raii::su::DepthBufferData depthBufferData( physicalDevice, device, vk::Format::eD16Unorm, surfaceData.extent );
*physicalDevice, *device, vk::Format::eD16Unorm, surfaceData.extent );
vk::raii::su::BufferData uniformBufferData( vk::raii::su::BufferData uniformBufferData(
*physicalDevice, *device, sizeof( glm::mat4x4 ), vk::BufferUsageFlagBits::eUniformBuffer ); physicalDevice, device, sizeof( glm::mat4x4 ), vk::BufferUsageFlagBits::eUniformBuffer );
glm::mat4x4 mvpcMatrix = vk::su::createModelViewProjectionClipMatrix( surfaceData.extent ); glm::mat4x4 mvpcMatrix = vk::su::createModelViewProjectionClipMatrix( surfaceData.extent );
vk::raii::su::copyToDevice( *uniformBufferData.deviceMemory, mvpcMatrix ); vk::raii::su::copyToDevice( uniformBufferData.deviceMemory, mvpcMatrix );
vk::Format colorFormat = vk::Format colorFormat =
vk::su::pickSurfaceFormat( physicalDevice->getSurfaceFormatsKHR( **surfaceData.surface ) ).format; vk::su::pickSurfaceFormat( physicalDevice.getSurfaceFormatsKHR( **surfaceData.pSurface ) ).format;
std::unique_ptr<vk::raii::RenderPass> renderPass = vk::raii::RenderPass renderPass = vk::raii::su::makeRenderPass( device, colorFormat, depthBufferData.format );
vk::raii::su::makeUniqueRenderPass( *device, colorFormat, depthBufferData.format );
glslang::InitializeProcess(); glslang::InitializeProcess();
std::unique_ptr<vk::raii::ShaderModule> vertexShaderModule = vk::raii::ShaderModule vertexShaderModule =
vk::raii::su::makeUniqueShaderModule( *device, vk::ShaderStageFlagBits::eVertex, vertexShaderText_PT_T ); vk::raii::su::makeShaderModule( device, vk::ShaderStageFlagBits::eVertex, vertexShaderText_PT_T );
std::unique_ptr<vk::raii::ShaderModule> fragmentShaderModule = vk::raii::ShaderModule fragmentShaderModule =
vk::raii::su::makeUniqueShaderModule( *device, vk::ShaderStageFlagBits::eFragment, fragmentShaderTextTS_T_C ); vk::raii::su::makeShaderModule( device, vk::ShaderStageFlagBits::eFragment, fragmentShaderTextTS_T_C );
glslang::FinalizeProcess(); glslang::FinalizeProcess();
std::vector<std::unique_ptr<vk::raii::Framebuffer>> framebuffers = vk::raii::su::makeUniqueFramebuffers( std::vector<vk::raii::Framebuffer> framebuffers = vk::raii::su::makeFramebuffers(
*device, *renderPass, swapChainData.imageViews, depthBufferData.imageView, surfaceData.extent ); device, renderPass, swapChainData.imageViews, &*depthBufferData.pImageView, surfaceData.extent );
vk::raii::su::BufferData vertexBufferData( vk::raii::su::BufferData vertexBufferData(
*physicalDevice, *device, sizeof( texturedCubeData ), vk::BufferUsageFlagBits::eVertexBuffer ); physicalDevice, device, sizeof( texturedCubeData ), vk::BufferUsageFlagBits::eVertexBuffer );
vk::raii::su::copyToDevice( vk::raii::su::copyToDevice(
*vertexBufferData.deviceMemory, texturedCubeData, sizeof( texturedCubeData ) / sizeof( texturedCubeData[0] ) ); vertexBufferData.deviceMemory, texturedCubeData, sizeof( texturedCubeData ) / sizeof( texturedCubeData[0] ) );
/* VULKAN_KEY_START */ /* VULKAN_KEY_START */
commandBuffer->begin( vk::CommandBufferBeginInfo() ); commandBuffer.begin( vk::CommandBufferBeginInfo() );
// Create the separate image // Create the separate image
vk::raii::su::TextureData textureData( *physicalDevice, *device ); vk::raii::su::TextureData textureData( physicalDevice, device );
textureData.setImage( *commandBuffer, vk::su::MonochromeImageGenerator( { 118, 185, 0 } ) ); textureData.setImage( commandBuffer, vk::su::MonochromeImageGenerator( { 118, 185, 0 } ) );
// Create the separate sampler // Create the separate sampler
vk::SamplerCreateInfo samplerCreateInfo( {}, vk::SamplerCreateInfo samplerCreateInfo( {},
vk::Filter::eNearest, vk::Filter::eNearest,
vk::Filter::eNearest, vk::Filter::eNearest,
vk::SamplerMipmapMode::eNearest, vk::SamplerMipmapMode::eNearest,
@ -158,7 +152,7 @@ int main( int /*argc*/, char ** /*argv*/ )
0.0f, 0.0f,
0.0f, 0.0f,
vk::BorderColor::eFloatOpaqueWhite ); vk::BorderColor::eFloatOpaqueWhite );
std::unique_ptr<vk::raii::Sampler> sampler = vk::raii::su::make_unique<vk::raii::Sampler>( *device, samplerCreateInfo ); vk::raii::Sampler sampler( device, samplerCreateInfo );
// Create binding and layout for the following, matching contents of shader // Create binding and layout for the following, matching contents of shader
// binding 0 = uniform buffer (MVP) // binding 0 = uniform buffer (MVP)
@ -169,14 +163,12 @@ int main( int /*argc*/, char ** /*argv*/ )
vk::DescriptorSetLayoutBinding( 1, vk::DescriptorType::eSampledImage, 1, vk::ShaderStageFlagBits::eFragment ), vk::DescriptorSetLayoutBinding( 1, vk::DescriptorType::eSampledImage, 1, vk::ShaderStageFlagBits::eFragment ),
vk::DescriptorSetLayoutBinding( 2, vk::DescriptorType::eSampler, 1, vk::ShaderStageFlagBits::eFragment ) } vk::DescriptorSetLayoutBinding( 2, vk::DescriptorType::eSampler, 1, vk::ShaderStageFlagBits::eFragment ) }
}; };
vk::DescriptorSetLayoutCreateInfo descriptorSetLayoutCreateInfo( {}, resourceBindings ); vk::DescriptorSetLayoutCreateInfo descriptorSetLayoutCreateInfo( {}, resourceBindings );
std::unique_ptr<vk::raii::DescriptorSetLayout> descriptorSetLayout = vk::raii::DescriptorSetLayout descriptorSetLayout( device, descriptorSetLayoutCreateInfo );
vk::raii::su::make_unique<vk::raii::DescriptorSetLayout>( *device, descriptorSetLayoutCreateInfo );
// Create pipeline layout // Create pipeline layout
vk::PipelineLayoutCreateInfo pipelineLayoutCreateInfo( {}, **descriptorSetLayout ); vk::PipelineLayoutCreateInfo pipelineLayoutCreateInfo( {}, *descriptorSetLayout );
std::unique_ptr<vk::raii::PipelineLayout> pipelineLayout = vk::raii::PipelineLayout pipelineLayout( device, pipelineLayoutCreateInfo );
vk::raii::su::make_unique<vk::raii::PipelineLayout>( *device, pipelineLayoutCreateInfo );
// Create a single pool to contain data for the descriptor set // Create a single pool to contain data for the descriptor set
std::array<vk::DescriptorPoolSize, 3> poolSizes = { std::array<vk::DescriptorPoolSize, 3> poolSizes = {
@ -186,51 +178,47 @@ int main( int /*argc*/, char ** /*argv*/ )
}; };
vk::DescriptorPoolCreateInfo descriptorPoolCreateInfo( vk::DescriptorPoolCreateInfo descriptorPoolCreateInfo(
vk::DescriptorPoolCreateFlagBits::eFreeDescriptorSet, 1, poolSizes ); vk::DescriptorPoolCreateFlagBits::eFreeDescriptorSet, 1, poolSizes );
std::unique_ptr<vk::raii::DescriptorPool> descriptorPool = vk::raii::DescriptorPool descriptorPool( device, descriptorPoolCreateInfo );
vk::raii::su::make_unique<vk::raii::DescriptorPool>( *device, descriptorPoolCreateInfo );
// Populate descriptor sets // Populate descriptor sets
vk::DescriptorSetAllocateInfo descriptorSetAllocateInfo( **descriptorPool, **descriptorSetLayout ); vk::DescriptorSetAllocateInfo descriptorSetAllocateInfo( *descriptorPool, *descriptorSetLayout );
std::unique_ptr<vk::raii::DescriptorSet> descriptorSet = vk::raii::su::make_unique<vk::raii::DescriptorSet>( vk::raii::DescriptorSet descriptorSet =
std::move( vk::raii::DescriptorSets( *device, descriptorSetAllocateInfo ).front() ) ); std::move( vk::raii::DescriptorSets( device, descriptorSetAllocateInfo ).front() );
vk::DescriptorBufferInfo bufferInfo( **uniformBufferData.buffer, 0, sizeof( glm::mat4x4 ) ); vk::DescriptorBufferInfo bufferInfo( *uniformBufferData.buffer, 0, sizeof( glm::mat4x4 ) );
vk::DescriptorImageInfo imageInfo( vk::DescriptorImageInfo imageInfo(
**textureData.sampler, **textureData.imageData->imageView, vk::ImageLayout::eShaderReadOnlyOptimal ); *textureData.sampler, **textureData.pImageData->pImageView, vk::ImageLayout::eShaderReadOnlyOptimal );
vk::DescriptorImageInfo samplerInfo( **sampler, {}, {} ); vk::DescriptorImageInfo samplerInfo( *sampler, {}, {} );
std::array<vk::WriteDescriptorSet, 3> descriptorWrites = { std::array<vk::WriteDescriptorSet, 3> descriptorWrites = {
{ vk::WriteDescriptorSet( **descriptorSet, 0, 0, vk::DescriptorType::eUniformBuffer, {}, bufferInfo ), { vk::WriteDescriptorSet( *descriptorSet, 0, 0, vk::DescriptorType::eUniformBuffer, {}, bufferInfo ),
vk::WriteDescriptorSet( **descriptorSet, 1, 0, vk::DescriptorType::eSampledImage, imageInfo ), vk::WriteDescriptorSet( *descriptorSet, 1, 0, vk::DescriptorType::eSampledImage, imageInfo ),
vk::WriteDescriptorSet( **descriptorSet, 2, 0, vk::DescriptorType::eSampler, samplerInfo ) } vk::WriteDescriptorSet( *descriptorSet, 2, 0, vk::DescriptorType::eSampler, samplerInfo ) }
}; };
device->updateDescriptorSets( descriptorWrites, nullptr ); device.updateDescriptorSets( descriptorWrites, nullptr );
/* VULKAN_KEY_END */ /* VULKAN_KEY_END */
std::unique_ptr<vk::raii::PipelineCache> pipelineCache = vk::raii::PipelineCache pipelineCache( device, vk::PipelineCacheCreateInfo() );
vk::raii::su::make_unique<vk::raii::PipelineCache>( *device, vk::PipelineCacheCreateInfo() ); vk::raii::Pipeline graphicsPipeline =
vk::raii::su::makeGraphicsPipeline( device,
std::unique_ptr<vk::raii::Pipeline> graphicsPipeline = vk::raii::su::makeUniqueGraphicsPipeline( pipelineCache,
*device, vertexShaderModule,
*pipelineCache, nullptr,
*vertexShaderModule, fragmentShaderModule,
nullptr, nullptr,
*fragmentShaderModule, sizeof( texturedCubeData[0] ),
nullptr, { { vk::Format::eR32G32B32A32Sfloat, 0 }, { vk::Format::eR32G32Sfloat, 16 } },
sizeof( texturedCubeData[0] ), vk::FrontFace::eClockwise,
{ { vk::Format::eR32G32B32A32Sfloat, 0 }, { vk::Format::eR32G32Sfloat, 16 } }, true,
vk::FrontFace::eClockwise, pipelineLayout,
true, renderPass );
*pipelineLayout,
*renderPass );
// Get the index of the next available swapchain image: // Get the index of the next available swapchain image:
std::unique_ptr<vk::raii::Semaphore> imageAcquiredSemaphore = vk::raii::Semaphore imageAcquiredSemaphore( device, vk::SemaphoreCreateInfo() );
vk::raii::su::make_unique<vk::raii::Semaphore>( *device, vk::SemaphoreCreateInfo() ); vk::Result result;
vk::Result result; uint32_t imageIndex;
uint32_t imageIndex;
std::tie( result, imageIndex ) = std::tie( result, imageIndex ) =
swapChainData.swapChain->acquireNextImage( vk::su::FenceTimeout, **imageAcquiredSemaphore ); swapChainData.pSwapChain->acquireNextImage( vk::su::FenceTimeout, *imageAcquiredSemaphore );
assert( result == vk::Result::eSuccess ); assert( result == vk::Result::eSuccess );
assert( imageIndex < swapChainData.images.size() ); assert( imageIndex < swapChainData.images.size() );
@ -239,38 +227,38 @@ int main( int /*argc*/, char ** /*argv*/ )
clearValues[1].depthStencil = vk::ClearDepthStencilValue( 1.0f, 0 ); clearValues[1].depthStencil = vk::ClearDepthStencilValue( 1.0f, 0 );
vk::RenderPassBeginInfo renderPassBeginInfo( vk::RenderPassBeginInfo renderPassBeginInfo(
**renderPass, **framebuffers[imageIndex], vk::Rect2D( vk::Offset2D( 0, 0 ), surfaceData.extent ), clearValues ); *renderPass, *framebuffers[imageIndex], vk::Rect2D( vk::Offset2D( 0, 0 ), surfaceData.extent ), clearValues );
commandBuffer->beginRenderPass( renderPassBeginInfo, vk::SubpassContents::eInline ); commandBuffer.beginRenderPass( renderPassBeginInfo, vk::SubpassContents::eInline );
commandBuffer->bindPipeline( vk::PipelineBindPoint::eGraphics, **graphicsPipeline ); commandBuffer.bindPipeline( vk::PipelineBindPoint::eGraphics, *graphicsPipeline );
commandBuffer->bindDescriptorSets( commandBuffer.bindDescriptorSets(
vk::PipelineBindPoint::eGraphics, **pipelineLayout, 0, { **descriptorSet }, nullptr ); vk::PipelineBindPoint::eGraphics, *pipelineLayout, 0, { *descriptorSet }, nullptr );
commandBuffer->bindVertexBuffers( 0, { **vertexBufferData.buffer }, { 0 } ); commandBuffer.bindVertexBuffers( 0, { *vertexBufferData.buffer }, { 0 } );
commandBuffer->setViewport( 0, commandBuffer.setViewport( 0,
vk::Viewport( 0.0f, vk::Viewport( 0.0f,
0.0f, 0.0f,
static_cast<float>( surfaceData.extent.width ), static_cast<float>( surfaceData.extent.width ),
static_cast<float>( surfaceData.extent.height ), static_cast<float>( surfaceData.extent.height ),
0.0f, 0.0f,
1.0f ) ); 1.0f ) );
commandBuffer->setScissor( 0, vk::Rect2D( vk::Offset2D( 0, 0 ), surfaceData.extent ) ); commandBuffer.setScissor( 0, vk::Rect2D( vk::Offset2D( 0, 0 ), surfaceData.extent ) );
commandBuffer->draw( 12 * 3, 1, 0, 0 ); commandBuffer.draw( 12 * 3, 1, 0, 0 );
commandBuffer->endRenderPass(); commandBuffer.endRenderPass();
commandBuffer->end(); commandBuffer.end();
std::unique_ptr<vk::raii::Fence> drawFence = vk::raii::su::make_unique<vk::raii::Fence>( *device, vk::FenceCreateInfo() ); vk::raii::Fence drawFence( device, vk::FenceCreateInfo() );
vk::PipelineStageFlags waitDestinationStageMask( vk::PipelineStageFlagBits::eColorAttachmentOutput ); vk::PipelineStageFlags waitDestinationStageMask( vk::PipelineStageFlagBits::eColorAttachmentOutput );
vk::SubmitInfo submitInfo( **imageAcquiredSemaphore, waitDestinationStageMask, **commandBuffer ); vk::SubmitInfo submitInfo( *imageAcquiredSemaphore, waitDestinationStageMask, *commandBuffer );
graphicsQueue->submit( submitInfo, **drawFence ); graphicsQueue.submit( submitInfo, *drawFence );
while ( vk::Result::eTimeout == device->waitForFences( { **drawFence }, VK_TRUE, vk::su::FenceTimeout ) ) while ( vk::Result::eTimeout == device.waitForFences( { *drawFence }, VK_TRUE, vk::su::FenceTimeout ) )
; ;
vk::PresentInfoKHR presentInfoKHR( nullptr, **swapChainData.swapChain, imageIndex ); vk::PresentInfoKHR presentInfoKHR( nullptr, **swapChainData.pSwapChain, imageIndex );
result = presentQueue->presentKHR( presentInfoKHR ); result = presentQueue.presentKHR( presentInfoKHR );
switch ( result ) switch ( result )
{ {
case vk::Result::eSuccess: break; case vk::Result::eSuccess: break;
@ -281,7 +269,7 @@ int main( int /*argc*/, char ** /*argv*/ )
} }
std::this_thread::sleep_for( std::chrono::milliseconds( 1000 ) ); std::this_thread::sleep_for( std::chrono::milliseconds( 1000 ) );
device->waitIdle(); device.waitIdle();
} }
catch ( vk::SystemError & err ) catch ( vk::SystemError & err )
{ {

View File

@ -58,9 +58,9 @@ int main( int /*argc*/, char ** /*argv*/ )
{ {
try try
{ {
std::unique_ptr<vk::raii::Context> context = vk::raii::su::make_unique<vk::raii::Context>(); vk::raii::Context context;
std::vector<vk::ExtensionProperties> instanceExtensionProperties = context->enumerateInstanceExtensionProperties(); std::vector<vk::ExtensionProperties> instanceExtensionProperties = context.enumerateInstanceExtensionProperties();
bool supportsGetSurfaceCapabilities2 = bool supportsGetSurfaceCapabilities2 =
( std::find_if( instanceExtensionProperties.begin(), ( std::find_if( instanceExtensionProperties.begin(),
instanceExtensionProperties.end(), instanceExtensionProperties.end(),
@ -74,17 +74,15 @@ int main( int /*argc*/, char ** /*argv*/ )
extensions.push_back( VK_KHR_GET_SURFACE_CAPABILITIES_2_EXTENSION_NAME ); extensions.push_back( VK_KHR_GET_SURFACE_CAPABILITIES_2_EXTENSION_NAME );
} }
std::unique_ptr<vk::raii::Instance> instance = vk::raii::Instance instance = vk::raii::su::makeInstance( context, AppName, EngineName, {}, extensions );
vk::raii::su::makeUniqueInstance( *context, AppName, EngineName, {}, extensions );
#if !defined( NDEBUG ) #if !defined( NDEBUG )
std::unique_ptr<vk::raii::DebugUtilsMessengerEXT> debugUtilsMessenger = vk::raii::DebugUtilsMessengerEXT debugUtilsMessenger( instance, vk::su::makeDebugUtilsMessengerCreateInfoEXT() );
vk::raii::su::makeUniqueDebugUtilsMessengerEXT( *instance );
#endif #endif
// enumerate the physicalDevices // enumerate the physicalDevices
vk::raii::PhysicalDevices physicalDevices( *instance ); vk::raii::PhysicalDevices physicalDevices( instance );
vk::raii::su::SurfaceData surfaceData( *instance, AppName, vk::Extent2D( 500, 500 ) ); vk::raii::su::SurfaceData surfaceData( instance, AppName, vk::Extent2D( 500, 500 ) );
/* VULKAN_KEY_START */ /* VULKAN_KEY_START */
@ -104,7 +102,7 @@ int main( int /*argc*/, char ** /*argv*/ )
vk::DisplayNativeHdrSurfaceCapabilitiesAMD, vk::DisplayNativeHdrSurfaceCapabilitiesAMD,
vk::SharedPresentSurfaceCapabilitiesKHR, vk::SharedPresentSurfaceCapabilitiesKHR,
vk::SurfaceCapabilitiesFullScreenExclusiveEXT, vk::SurfaceCapabilitiesFullScreenExclusiveEXT,
vk::SurfaceProtectedCapabilitiesKHR>( { **surfaceData.surface } ); vk::SurfaceProtectedCapabilitiesKHR>( { **surfaceData.pSurface } );
vk::SurfaceCapabilitiesKHR const & surfaceCapabilities = vk::SurfaceCapabilitiesKHR const & surfaceCapabilities =
surfaceCapabilities2.get<vk::SurfaceCapabilities2KHR>().surfaceCapabilities; surfaceCapabilities2.get<vk::SurfaceCapabilities2KHR>().surfaceCapabilities;
@ -155,7 +153,7 @@ int main( int /*argc*/, char ** /*argv*/ )
else else
{ {
vk::SurfaceCapabilitiesKHR surfaceCapabilities = vk::SurfaceCapabilitiesKHR surfaceCapabilities =
physicalDevices[i].getSurfaceCapabilitiesKHR( **surfaceData.surface ); physicalDevices[i].getSurfaceCapabilitiesKHR( **surfaceData.pSurface );
cout( surfaceCapabilities ); cout( surfaceCapabilities );
} }
} }

View File

@ -29,18 +29,17 @@ int main( int /*argc*/, char ** /*argv*/ )
{ {
try try
{ {
std::unique_ptr<vk::raii::Context> context = vk::raii::su::make_unique<vk::raii::Context>(); vk::raii::Context context;
std::unique_ptr<vk::raii::Instance> instance = vk::raii::Instance instance =
vk::raii::su::makeUniqueInstance( *context, AppName, EngineName, {}, vk::su::getInstanceExtensions() ); vk::raii::su::makeInstance( context, AppName, EngineName, {}, vk::su::getInstanceExtensions() );
#if !defined( NDEBUG ) #if !defined( NDEBUG )
std::unique_ptr<vk::raii::DebugUtilsMessengerEXT> debugUtilsMessenger = vk::raii::DebugUtilsMessengerEXT debugUtilsMessenger( instance, vk::su::makeDebugUtilsMessengerCreateInfoEXT() );
vk::raii::su::makeUniqueDebugUtilsMessengerEXT( *instance );
#endif #endif
// enumerate the physicalDevices // enumerate the physicalDevices
vk::raii::PhysicalDevices physicalDevices( *instance ); vk::raii::PhysicalDevices physicalDevices( instance );
vk::raii::su::SurfaceData surfaceData( *instance, AppName, vk::Extent2D( 500, 500 ) ); vk::raii::su::SurfaceData surfaceData( instance, AppName, vk::Extent2D( 500, 500 ) );
/* VULKAN_KEY_START */ /* VULKAN_KEY_START */
@ -49,7 +48,7 @@ int main( int /*argc*/, char ** /*argv*/ )
{ {
std::cout << "PhysicalDevice " << i << "\n"; std::cout << "PhysicalDevice " << i << "\n";
std::vector<vk::SurfaceFormatKHR> surfaceFormats = std::vector<vk::SurfaceFormatKHR> surfaceFormats =
physicalDevices[i].getSurfaceFormatsKHR( **surfaceData.surface ); physicalDevices[i].getSurfaceFormatsKHR( **surfaceData.pSurface );
for ( size_t j = 0; j < surfaceFormats.size(); j++ ) for ( size_t j = 0; j < surfaceFormats.size(); j++ )
{ {
std::cout << "\tFormat " << j << "\n"; std::cout << "\tFormat " << j << "\n";

View File

@ -32,35 +32,31 @@ int main( int /*argc*/, char ** /*argv*/ )
{ {
try try
{ {
std::unique_ptr<vk::raii::Context> context = vk::raii::su::make_unique<vk::raii::Context>(); vk::raii::Context context;
std::unique_ptr<vk::raii::Instance> instance = vk::raii::Instance instance =
vk::raii::su::makeUniqueInstance( *context, AppName, EngineName, {}, vk::su::getInstanceExtensions() ); vk::raii::su::makeInstance( context, AppName, EngineName, {}, vk::su::getInstanceExtensions() );
#if !defined( NDEBUG ) #if !defined( NDEBUG )
std::unique_ptr<vk::raii::DebugUtilsMessengerEXT> debugUtilsMessenger = vk::raii::DebugUtilsMessengerEXT debugUtilsMessenger( instance, vk::su::makeDebugUtilsMessengerCreateInfoEXT() );
vk::raii::su::makeUniqueDebugUtilsMessengerEXT( *instance );
#endif #endif
std::unique_ptr<vk::raii::PhysicalDevice> physicalDevice = vk::raii::su::makeUniquePhysicalDevice( *instance ); vk::raii::PhysicalDevice physicalDevice = std::move( vk::raii::PhysicalDevices( instance ).front() );
vk::raii::su::SurfaceData surfaceData( *instance, AppName, vk::Extent2D( 500, 500 ) ); vk::raii::su::SurfaceData surfaceData( instance, AppName, vk::Extent2D( 500, 500 ) );
std::pair<uint32_t, uint32_t> graphicsAndPresentQueueFamilyIndex = std::pair<uint32_t, uint32_t> graphicsAndPresentQueueFamilyIndex =
vk::raii::su::findGraphicsAndPresentQueueFamilyIndex( *physicalDevice, *surfaceData.surface ); vk::raii::su::findGraphicsAndPresentQueueFamilyIndex( physicalDevice, *surfaceData.pSurface );
std::unique_ptr<vk::raii::Device> device = vk::raii::su::makeUniqueDevice( vk::raii::Device device = vk::raii::su::makeDevice(
*physicalDevice, graphicsAndPresentQueueFamilyIndex.first, vk::su::getDeviceExtensions() ); physicalDevice, graphicsAndPresentQueueFamilyIndex.first, vk::su::getDeviceExtensions() );
std::unique_ptr<vk::raii::CommandPool> commandPool = vk::raii::CommandPool commandPool = vk::raii::CommandPool(
vk::raii::su::makeUniqueCommandPool( *device, graphicsAndPresentQueueFamilyIndex.first ); device, { vk::CommandPoolCreateFlagBits::eResetCommandBuffer, graphicsAndPresentQueueFamilyIndex.first } );
std::unique_ptr<vk::raii::CommandBuffer> commandBuffer = vk::raii::CommandBuffer commandBuffer = vk::raii::su::makeCommandBuffer( device, commandPool );
vk::raii::su::makeUniqueCommandBuffer( *device, *commandPool );
std::unique_ptr<vk::raii::Queue> graphicsQueue = vk::raii::Queue graphicsQueue( device, graphicsAndPresentQueueFamilyIndex.first, 0 );
vk::raii::su::make_unique<vk::raii::Queue>( *device, graphicsAndPresentQueueFamilyIndex.first, 0 ); vk::raii::Queue presentQueue( device, graphicsAndPresentQueueFamilyIndex.second, 0 );
std::unique_ptr<vk::raii::Queue> presentQueue =
vk::raii::su::make_unique<vk::raii::Queue>( *device, graphicsAndPresentQueueFamilyIndex.second, 0 );
vk::raii::su::SwapChainData swapChainData( *physicalDevice, vk::raii::su::SwapChainData swapChainData( physicalDevice,
*device, device,
*surfaceData.surface, *surfaceData.pSurface,
surfaceData.extent, surfaceData.extent,
vk::ImageUsageFlagBits::eColorAttachment | vk::ImageUsageFlagBits::eColorAttachment |
vk::ImageUsageFlagBits::eTransferSrc, vk::ImageUsageFlagBits::eTransferSrc,
@ -68,79 +64,74 @@ int main( int /*argc*/, char ** /*argv*/ )
graphicsAndPresentQueueFamilyIndex.first, graphicsAndPresentQueueFamilyIndex.first,
graphicsAndPresentQueueFamilyIndex.second ); graphicsAndPresentQueueFamilyIndex.second );
vk::raii::su::DepthBufferData depthBufferData( vk::raii::su::DepthBufferData depthBufferData( physicalDevice, device, vk::Format::eD16Unorm, surfaceData.extent );
*physicalDevice, *device, vk::Format::eD16Unorm, surfaceData.extent );
vk::raii::su::TextureData textureData( *physicalDevice, *device ); vk::raii::su::TextureData textureData( physicalDevice, device );
commandBuffer->begin( vk::CommandBufferBeginInfo() ); commandBuffer.begin( vk::CommandBufferBeginInfo() );
textureData.setImage( *commandBuffer, vk::su::CheckerboardImageGenerator() ); textureData.setImage( commandBuffer, vk::su::CheckerboardImageGenerator() );
vk::raii::su::BufferData uniformBufferData( vk::raii::su::BufferData uniformBufferData(
*physicalDevice, *device, sizeof( glm::mat4x4 ), vk::BufferUsageFlagBits::eUniformBuffer ); physicalDevice, device, sizeof( glm::mat4x4 ), vk::BufferUsageFlagBits::eUniformBuffer );
glm::mat4x4 mvpcMatrix = vk::su::createModelViewProjectionClipMatrix( surfaceData.extent ); glm::mat4x4 mvpcMatrix = vk::su::createModelViewProjectionClipMatrix( surfaceData.extent );
vk::raii::su::copyToDevice( *uniformBufferData.deviceMemory, mvpcMatrix ); vk::raii::su::copyToDevice( uniformBufferData.deviceMemory, mvpcMatrix );
std::unique_ptr<vk::raii::DescriptorSetLayout> descriptorSetLayout = vk::raii::su::makeUniqueDescriptorSetLayout( vk::raii::DescriptorSetLayout descriptorSetLayout = vk::raii::su::makeDescriptorSetLayout(
*device, device,
{ { vk::DescriptorType::eUniformBuffer, 1, vk::ShaderStageFlagBits::eVertex }, { { vk::DescriptorType::eUniformBuffer, 1, vk::ShaderStageFlagBits::eVertex },
{ vk::DescriptorType::eCombinedImageSampler, 1, vk::ShaderStageFlagBits::eFragment } } ); { vk::DescriptorType::eCombinedImageSampler, 1, vk::ShaderStageFlagBits::eFragment } } );
std::unique_ptr<vk::raii::PipelineLayout> pipelineLayout = vk::raii::PipelineLayout pipelineLayout( device, { {}, *descriptorSetLayout } );
vk::raii::su::makeUniquePipelineLayout( *device, *descriptorSetLayout );
vk::Format colorFormat = vk::Format colorFormat =
vk::su::pickSurfaceFormat( physicalDevice->getSurfaceFormatsKHR( **surfaceData.surface ) ).format; vk::su::pickSurfaceFormat( physicalDevice.getSurfaceFormatsKHR( **surfaceData.pSurface ) ).format;
std::unique_ptr<vk::raii::RenderPass> renderPass = vk::raii::RenderPass renderPass = vk::raii::su::makeRenderPass( device, colorFormat, depthBufferData.format );
vk::raii::su::makeUniqueRenderPass( *device, colorFormat, depthBufferData.format );
glslang::InitializeProcess(); glslang::InitializeProcess();
std::unique_ptr<vk::raii::ShaderModule> vertexShaderModule = vk::raii::ShaderModule vertexShaderModule =
vk::raii::su::makeUniqueShaderModule( *device, vk::ShaderStageFlagBits::eVertex, vertexShaderText_PT_T ); vk::raii::su::makeShaderModule( device, vk::ShaderStageFlagBits::eVertex, vertexShaderText_PT_T );
std::unique_ptr<vk::raii::ShaderModule> fragmentShaderModule = vk::raii::ShaderModule fragmentShaderModule =
vk::raii::su::makeUniqueShaderModule( *device, vk::ShaderStageFlagBits::eFragment, fragmentShaderText_T_C ); vk::raii::su::makeShaderModule( device, vk::ShaderStageFlagBits::eFragment, fragmentShaderText_T_C );
glslang::FinalizeProcess(); glslang::FinalizeProcess();
std::vector<std::unique_ptr<vk::raii::Framebuffer>> framebuffers = vk::raii::su::makeUniqueFramebuffers( std::vector<vk::raii::Framebuffer> framebuffers = vk::raii::su::makeFramebuffers(
*device, *renderPass, swapChainData.imageViews, depthBufferData.imageView, surfaceData.extent ); device, renderPass, swapChainData.imageViews, &*depthBufferData.pImageView, surfaceData.extent );
vk::raii::su::BufferData vertexBufferData( vk::raii::su::BufferData vertexBufferData(
*physicalDevice, *device, sizeof( texturedCubeData ), vk::BufferUsageFlagBits::eVertexBuffer ); physicalDevice, device, sizeof( texturedCubeData ), vk::BufferUsageFlagBits::eVertexBuffer );
vk::raii::su::copyToDevice( vk::raii::su::copyToDevice(
*vertexBufferData.deviceMemory, texturedCubeData, sizeof( texturedCubeData ) / sizeof( texturedCubeData[0] ) ); vertexBufferData.deviceMemory, texturedCubeData, sizeof( texturedCubeData ) / sizeof( texturedCubeData[0] ) );
std::unique_ptr<vk::raii::DescriptorPool> descriptorPool = vk::raii::su::makeUniqueDescriptorPool( vk::raii::DescriptorPool descriptorPool = vk::raii::su::makeDescriptorPool(
*device, { { vk::DescriptorType::eUniformBuffer, 1 }, { vk::DescriptorType::eCombinedImageSampler, 1 } } ); device, { { vk::DescriptorType::eUniformBuffer, 1 }, { vk::DescriptorType::eCombinedImageSampler, 1 } } );
std::unique_ptr<vk::raii::DescriptorSet> descriptorSet = vk::raii::DescriptorSet descriptorSet =
vk::raii::su::makeUniqueDescriptorSet( *device, *descriptorPool, *descriptorSetLayout ); std::move( vk::raii::DescriptorSets( device, { *descriptorPool, *descriptorSetLayout } ).front() );
vk::raii::su::updateDescriptorSets( *device, vk::raii::su::updateDescriptorSets( device,
*descriptorSet, descriptorSet,
{ { vk::DescriptorType::eUniformBuffer, *uniformBufferData.buffer, nullptr } }, { { vk::DescriptorType::eUniformBuffer, uniformBufferData.buffer, nullptr } },
{ textureData } ); { textureData } );
std::unique_ptr<vk::raii::PipelineCache> pipelineCache = vk::raii::PipelineCache pipelineCache( device, vk::PipelineCacheCreateInfo() );
vk::raii::su::make_unique<vk::raii::PipelineCache>( *device, vk::PipelineCacheCreateInfo() ); vk::raii::Pipeline graphicsPipeline =
std::unique_ptr<vk::raii::Pipeline> graphicsPipeline = vk::raii::su::makeUniqueGraphicsPipeline( vk::raii::su::makeGraphicsPipeline( device,
*device, pipelineCache,
*pipelineCache, vertexShaderModule,
*vertexShaderModule, nullptr,
nullptr, fragmentShaderModule,
*fragmentShaderModule, nullptr,
nullptr, sizeof( texturedCubeData[0] ),
sizeof( texturedCubeData[0] ), { { vk::Format::eR32G32B32A32Sfloat, 0 }, { vk::Format::eR32G32Sfloat, 16 } },
{ { vk::Format::eR32G32B32A32Sfloat, 0 }, { vk::Format::eR32G32Sfloat, 16 } }, vk::FrontFace::eClockwise,
vk::FrontFace::eClockwise, true,
true, pipelineLayout,
*pipelineLayout, renderPass );
*renderPass );
// Get the index of the next available swapchain image: // Get the index of the next available swapchain image:
std::unique_ptr<vk::raii::Semaphore> imageAcquiredSemaphore = vk::raii::Semaphore imageAcquiredSemaphore( device, vk::SemaphoreCreateInfo() );
vk::raii::su::make_unique<vk::raii::Semaphore>( *device, vk::SemaphoreCreateInfo() ); vk::Result result;
vk::Result result; uint32_t imageIndex;
uint32_t imageIndex;
std::tie( result, imageIndex ) = std::tie( result, imageIndex ) =
swapChainData.swapChain->acquireNextImage( vk::su::FenceTimeout, **imageAcquiredSemaphore ); swapChainData.pSwapChain->acquireNextImage( vk::su::FenceTimeout, *imageAcquiredSemaphore );
assert( result == vk::Result::eSuccess ); assert( result == vk::Result::eSuccess );
assert( imageIndex < swapChainData.images.size() ); assert( imageIndex < swapChainData.images.size() );
@ -148,38 +139,38 @@ int main( int /*argc*/, char ** /*argv*/ )
clearValues[0].color = vk::ClearColorValue( std::array<float, 4>( { { 0.2f, 0.2f, 0.2f, 0.2f } } ) ); clearValues[0].color = vk::ClearColorValue( std::array<float, 4>( { { 0.2f, 0.2f, 0.2f, 0.2f } } ) );
clearValues[1].depthStencil = vk::ClearDepthStencilValue( 1.0f, 0 ); clearValues[1].depthStencil = vk::ClearDepthStencilValue( 1.0f, 0 );
vk::RenderPassBeginInfo renderPassBeginInfo( vk::RenderPassBeginInfo renderPassBeginInfo(
**renderPass, **framebuffers[imageIndex], vk::Rect2D( vk::Offset2D( 0, 0 ), surfaceData.extent ), clearValues ); *renderPass, *framebuffers[imageIndex], vk::Rect2D( vk::Offset2D( 0, 0 ), surfaceData.extent ), clearValues );
commandBuffer->beginRenderPass( renderPassBeginInfo, vk::SubpassContents::eInline ); commandBuffer.beginRenderPass( renderPassBeginInfo, vk::SubpassContents::eInline );
commandBuffer->bindPipeline( vk::PipelineBindPoint::eGraphics, **graphicsPipeline ); commandBuffer.bindPipeline( vk::PipelineBindPoint::eGraphics, *graphicsPipeline );
commandBuffer->bindDescriptorSets( commandBuffer.bindDescriptorSets(
vk::PipelineBindPoint::eGraphics, **pipelineLayout, 0, { **descriptorSet }, nullptr ); vk::PipelineBindPoint::eGraphics, *pipelineLayout, 0, { *descriptorSet }, nullptr );
commandBuffer->bindVertexBuffers( 0, { **vertexBufferData.buffer }, { 0 } ); commandBuffer.bindVertexBuffers( 0, { *vertexBufferData.buffer }, { 0 } );
commandBuffer->setViewport( 0, commandBuffer.setViewport( 0,
vk::Viewport( 0.0f, vk::Viewport( 0.0f,
0.0f, 0.0f,
static_cast<float>( surfaceData.extent.width ), static_cast<float>( surfaceData.extent.width ),
static_cast<float>( surfaceData.extent.height ), static_cast<float>( surfaceData.extent.height ),
0.0f, 0.0f,
1.0f ) ); 1.0f ) );
commandBuffer->setScissor( 0, vk::Rect2D( vk::Offset2D( 0, 0 ), surfaceData.extent ) ); commandBuffer.setScissor( 0, vk::Rect2D( vk::Offset2D( 0, 0 ), surfaceData.extent ) );
commandBuffer->draw( 12 * 3, 1, 0, 0 ); commandBuffer.draw( 12 * 3, 1, 0, 0 );
commandBuffer->endRenderPass(); commandBuffer.endRenderPass();
commandBuffer->end(); commandBuffer.end();
std::unique_ptr<vk::raii::Fence> drawFence = vk::raii::su::make_unique<vk::raii::Fence>( *device, vk::FenceCreateInfo() ); vk::raii::Fence drawFence( device, vk::FenceCreateInfo() );
vk::PipelineStageFlags waitDestinationStageMask( vk::PipelineStageFlagBits::eColorAttachmentOutput ); vk::PipelineStageFlags waitDestinationStageMask( vk::PipelineStageFlagBits::eColorAttachmentOutput );
vk::SubmitInfo submitInfo( **imageAcquiredSemaphore, waitDestinationStageMask, **commandBuffer ); vk::SubmitInfo submitInfo( *imageAcquiredSemaphore, waitDestinationStageMask, *commandBuffer );
graphicsQueue->submit( submitInfo, **drawFence ); graphicsQueue.submit( submitInfo, *drawFence );
while ( vk::Result::eTimeout == device->waitForFences( { **drawFence }, VK_TRUE, vk::su::FenceTimeout ) ) while ( vk::Result::eTimeout == device.waitForFences( { *drawFence }, VK_TRUE, vk::su::FenceTimeout ) )
; ;
vk::PresentInfoKHR presentInfoKHR( nullptr, **swapChainData.swapChain, imageIndex ); vk::PresentInfoKHR presentInfoKHR( nullptr, **swapChainData.pSwapChain, imageIndex );
result = presentQueue->presentKHR( presentInfoKHR ); result = presentQueue.presentKHR( presentInfoKHR );
switch ( result ) switch ( result )
{ {
case vk::Result::eSuccess: break; case vk::Result::eSuccess: break;
@ -190,7 +181,7 @@ int main( int /*argc*/, char ** /*argv*/ )
} }
std::this_thread::sleep_for( std::chrono::milliseconds( 1000 ) ); std::this_thread::sleep_for( std::chrono::milliseconds( 1000 ) );
device->waitIdle(); device.waitIdle();
} }
catch ( vk::SystemError & err ) catch ( vk::SystemError & err )
{ {

View File

@ -59,16 +59,15 @@ int main( int /*argc*/, char ** /*argv*/ )
try try
{ {
std::unique_ptr<vk::raii::Context> context = vk::raii::su::make_unique<vk::raii::Context>(); vk::raii::Context context;
std::unique_ptr<vk::raii::Instance> instance = vk::raii::Instance instance =
vk::raii::su::makeUniqueInstance( *context, AppName, EngineName, {}, vk::su::getInstanceExtensions() ); vk::raii::su::makeInstance( context, AppName, EngineName, {}, vk::su::getInstanceExtensions() );
#if !defined( NDEBUG ) #if !defined( NDEBUG )
std::unique_ptr<vk::raii::DebugUtilsMessengerEXT> debugUtilsMessenger = vk::raii::DebugUtilsMessengerEXT debugUtilsMessenger( instance, vk::su::makeDebugUtilsMessengerCreateInfoEXT() );
vk::raii::su::makeUniqueDebugUtilsMessengerEXT( *instance );
#endif #endif
std::unique_ptr<vk::raii::PhysicalDevice> physicalDevice = vk::raii::su::makeUniquePhysicalDevice( *instance ); vk::raii::PhysicalDevice physicalDevice = std::move( vk::raii::PhysicalDevices( instance ).front() );
vk::PhysicalDeviceProperties physicalDeviceProperties = physicalDevice->getProperties(); vk::PhysicalDeviceProperties physicalDeviceProperties = physicalDevice.getProperties();
if ( physicalDeviceProperties.limits.maxTexelBufferElements < 4 ) if ( physicalDeviceProperties.limits.maxTexelBufferElements < 4 )
{ {
std::cout << "maxTexelBufferElements too small\n"; std::cout << "maxTexelBufferElements too small\n";
@ -76,33 +75,30 @@ int main( int /*argc*/, char ** /*argv*/ )
} }
vk::Format texelFormat = vk::Format::eR32Sfloat; vk::Format texelFormat = vk::Format::eR32Sfloat;
vk::FormatProperties formatProperties = physicalDevice->getFormatProperties( texelFormat ); vk::FormatProperties formatProperties = physicalDevice.getFormatProperties( texelFormat );
if ( !( formatProperties.bufferFeatures & vk::FormatFeatureFlagBits::eUniformTexelBuffer ) ) if ( !( formatProperties.bufferFeatures & vk::FormatFeatureFlagBits::eUniformTexelBuffer ) )
{ {
std::cout << "R32_SFLOAT format unsupported for texel buffer\n"; std::cout << "R32_SFLOAT format unsupported for texel buffer\n";
exit( -1 ); exit( -1 );
} }
vk::raii::su::SurfaceData surfaceData( *instance, AppName, vk::Extent2D( 500, 500 ) ); vk::raii::su::SurfaceData surfaceData( instance, AppName, vk::Extent2D( 500, 500 ) );
std::pair<uint32_t, uint32_t> graphicsAndPresentQueueFamilyIndex = std::pair<uint32_t, uint32_t> graphicsAndPresentQueueFamilyIndex =
vk::raii::su::findGraphicsAndPresentQueueFamilyIndex( *physicalDevice, *surfaceData.surface ); vk::raii::su::findGraphicsAndPresentQueueFamilyIndex( physicalDevice, *surfaceData.pSurface );
std::unique_ptr<vk::raii::Device> device = vk::raii::su::makeUniqueDevice( vk::raii::Device device = vk::raii::su::makeDevice(
*physicalDevice, graphicsAndPresentQueueFamilyIndex.first, vk::su::getDeviceExtensions() ); physicalDevice, graphicsAndPresentQueueFamilyIndex.first, vk::su::getDeviceExtensions() );
std::unique_ptr<vk::raii::CommandPool> commandPool = vk::raii::CommandPool commandPool = vk::raii::CommandPool(
vk::raii::su::makeUniqueCommandPool( *device, graphicsAndPresentQueueFamilyIndex.first ); device, { vk::CommandPoolCreateFlagBits::eResetCommandBuffer, graphicsAndPresentQueueFamilyIndex.first } );
std::unique_ptr<vk::raii::CommandBuffer> commandBuffer = vk::raii::CommandBuffer commandBuffer = vk::raii::su::makeCommandBuffer( device, commandPool );
vk::raii::su::makeUniqueCommandBuffer( *device, *commandPool );
std::unique_ptr<vk::raii::Queue> graphicsQueue = vk::raii::Queue graphicsQueue( device, graphicsAndPresentQueueFamilyIndex.first, 0 );
vk::raii::su::make_unique<vk::raii::Queue>( *device, graphicsAndPresentQueueFamilyIndex.first, 0 ); vk::raii::Queue presentQueue( device, graphicsAndPresentQueueFamilyIndex.second, 0 );
std::unique_ptr<vk::raii::Queue> presentQueue =
vk::raii::su::make_unique<vk::raii::Queue>( *device, graphicsAndPresentQueueFamilyIndex.second, 0 );
vk::raii::su::SwapChainData swapChainData( *physicalDevice, vk::raii::su::SwapChainData swapChainData( physicalDevice,
*device, device,
*surfaceData.surface, *surfaceData.pSurface,
surfaceData.extent, surfaceData.extent,
vk::ImageUsageFlagBits::eColorAttachment | vk::ImageUsageFlagBits::eColorAttachment |
vk::ImageUsageFlagBits::eTransferSrc, vk::ImageUsageFlagBits::eTransferSrc,
@ -111,108 +107,102 @@ int main( int /*argc*/, char ** /*argv*/ )
graphicsAndPresentQueueFamilyIndex.second ); graphicsAndPresentQueueFamilyIndex.second );
vk::raii::su::BufferData texelBufferData( vk::raii::su::BufferData texelBufferData(
*physicalDevice, *device, sizeof( texels ), vk::BufferUsageFlagBits::eUniformTexelBuffer ); physicalDevice, device, sizeof( texels ), vk::BufferUsageFlagBits::eUniformTexelBuffer );
texelBufferData.upload( texels ); texelBufferData.upload( texels );
vk::BufferViewCreateInfo bufferViewCreateInfo( {}, **texelBufferData.buffer, texelFormat, 0, sizeof( texels ) ); vk::BufferViewCreateInfo bufferViewCreateInfo( {}, *texelBufferData.buffer, texelFormat, 0, sizeof( texels ) );
std::unique_ptr<vk::raii::BufferView> texelBufferView = vk::raii::BufferView texelBufferView( device, bufferViewCreateInfo );
vk::raii::su::make_unique<vk::raii::BufferView>( *device, bufferViewCreateInfo );
std::unique_ptr<vk::raii::DescriptorSetLayout> descriptorSetLayout = vk::raii::su::makeUniqueDescriptorSetLayout( vk::raii::DescriptorSetLayout descriptorSetLayout = vk::raii::su::makeDescriptorSetLayout(
*device, { { vk::DescriptorType::eUniformTexelBuffer, 1, vk::ShaderStageFlagBits::eVertex } } ); device, { { vk::DescriptorType::eUniformTexelBuffer, 1, vk::ShaderStageFlagBits::eVertex } } );
std::unique_ptr<vk::raii::PipelineLayout> pipelineLayout = vk::raii::PipelineLayout pipelineLayout( device, { {}, *descriptorSetLayout } );
vk::raii::su::makeUniquePipelineLayout( *device, *descriptorSetLayout );
vk::Format colorFormat = vk::Format colorFormat =
vk::su::pickSurfaceFormat( physicalDevice->getSurfaceFormatsKHR( **surfaceData.surface ) ).format; vk::su::pickSurfaceFormat( physicalDevice.getSurfaceFormatsKHR( **surfaceData.pSurface ) ).format;
std::unique_ptr<vk::raii::RenderPass> renderPass = vk::raii::RenderPass renderPass = vk::raii::su::makeRenderPass( device, colorFormat, vk::Format::eUndefined );
vk::raii::su::makeUniqueRenderPass( *device, colorFormat, vk::Format::eUndefined );
glslang::InitializeProcess(); glslang::InitializeProcess();
std::unique_ptr<vk::raii::ShaderModule> vertexShaderModule = vk::raii::ShaderModule vertexShaderModule =
vk::raii::su::makeUniqueShaderModule( *device, vk::ShaderStageFlagBits::eVertex, vertexShaderText ); vk::raii::su::makeShaderModule( device, vk::ShaderStageFlagBits::eVertex, vertexShaderText );
std::unique_ptr<vk::raii::ShaderModule> fragmentShaderModule = vk::raii::ShaderModule fragmentShaderModule =
vk::raii::su::makeUniqueShaderModule( *device, vk::ShaderStageFlagBits::eFragment, fragmentShaderText_C_C ); vk::raii::su::makeShaderModule( device, vk::ShaderStageFlagBits::eFragment, fragmentShaderText_C_C );
glslang::FinalizeProcess(); glslang::FinalizeProcess();
std::vector<std::unique_ptr<vk::raii::Framebuffer>> framebuffers = vk::raii::su::makeUniqueFramebuffers( std::vector<vk::raii::Framebuffer> framebuffers =
*device, *renderPass, swapChainData.imageViews, std::unique_ptr<vk::raii::ImageView>(), surfaceData.extent ); vk::raii::su::makeFramebuffers( device, renderPass, swapChainData.imageViews, nullptr, surfaceData.extent );
std::unique_ptr<vk::raii::DescriptorPool> descriptorPool = vk::raii::DescriptorPool descriptorPool =
vk::raii::su::makeUniqueDescriptorPool( *device, { { vk::DescriptorType::eUniformTexelBuffer, 1 } } ); vk::raii::su::makeDescriptorPool( device, { { vk::DescriptorType::eUniformTexelBuffer, 1 } } );
std::unique_ptr<vk::raii::DescriptorSet> descriptorSet = vk::raii::DescriptorSet descriptorSet =
vk::raii::su::makeUniqueDescriptorSet( *device, *descriptorPool, *descriptorSetLayout ); std::move( vk::raii::DescriptorSets( device, { *descriptorPool, *descriptorSetLayout } ).front() );
vk::raii::su::updateDescriptorSets( vk::raii::su::updateDescriptorSets(
*device, device,
*descriptorSet, descriptorSet,
{ { vk::DescriptorType::eUniformTexelBuffer, *texelBufferData.buffer, &*texelBufferView } }, { { vk::DescriptorType::eUniformTexelBuffer, texelBufferData.buffer, &texelBufferView } },
{} ); {} );
std::unique_ptr<vk::raii::PipelineCache> pipelineCache = vk::raii::PipelineCache pipelineCache( device, vk::PipelineCacheCreateInfo() );
vk::raii::su::make_unique<vk::raii::PipelineCache>( *device, vk::PipelineCacheCreateInfo() ); vk::raii::Pipeline graphicsPipeline = vk::raii::su::makeGraphicsPipeline( device,
std::unique_ptr<vk::raii::Pipeline> graphicsPipeline = pipelineCache,
vk::raii::su::makeUniqueGraphicsPipeline( *device, vertexShaderModule,
*pipelineCache, nullptr,
*vertexShaderModule, fragmentShaderModule,
nullptr, nullptr,
*fragmentShaderModule, 0,
nullptr, {},
0, vk::FrontFace::eClockwise,
{}, true,
vk::FrontFace::eClockwise, pipelineLayout,
true, renderPass );
*pipelineLayout,
*renderPass );
/* VULKAN_KEY_START */ /* VULKAN_KEY_START */
// Get the index of the next available swapchain image: // Get the index of the next available swapchain image:
std::unique_ptr<vk::raii::Semaphore> imageAcquiredSemaphore = vk::raii::Semaphore imageAcquiredSemaphore( device, vk::SemaphoreCreateInfo() );
vk::raii::su::make_unique<vk::raii::Semaphore>( *device, vk::SemaphoreCreateInfo() ); vk::Result result;
vk::Result result; uint32_t imageIndex;
uint32_t imageIndex;
std::tie( result, imageIndex ) = std::tie( result, imageIndex ) =
swapChainData.swapChain->acquireNextImage( vk::su::FenceTimeout, **imageAcquiredSemaphore ); swapChainData.pSwapChain->acquireNextImage( vk::su::FenceTimeout, *imageAcquiredSemaphore );
assert( result == vk::Result::eSuccess ); assert( result == vk::Result::eSuccess );
assert( imageIndex < swapChainData.images.size() ); assert( imageIndex < swapChainData.images.size() );
commandBuffer->begin( vk::CommandBufferBeginInfo() ); commandBuffer.begin( vk::CommandBufferBeginInfo() );
vk::ClearValue clearValue; vk::ClearValue clearValue;
clearValue.color = vk::ClearColorValue( std::array<float, 4>( { { 0.2f, 0.2f, 0.2f, 0.2f } } ) ); clearValue.color = vk::ClearColorValue( std::array<float, 4>( { { 0.2f, 0.2f, 0.2f, 0.2f } } ) );
vk::RenderPassBeginInfo renderPassBeginInfo( vk::RenderPassBeginInfo renderPassBeginInfo(
**renderPass, **framebuffers[imageIndex], vk::Rect2D( vk::Offset2D( 0, 0 ), surfaceData.extent ), clearValue ); *renderPass, *framebuffers[imageIndex], vk::Rect2D( vk::Offset2D( 0, 0 ), surfaceData.extent ), clearValue );
commandBuffer->beginRenderPass( renderPassBeginInfo, vk::SubpassContents::eInline ); commandBuffer.beginRenderPass( renderPassBeginInfo, vk::SubpassContents::eInline );
commandBuffer->bindPipeline( vk::PipelineBindPoint::eGraphics, **graphicsPipeline ); commandBuffer.bindPipeline( vk::PipelineBindPoint::eGraphics, *graphicsPipeline );
commandBuffer->bindDescriptorSets( commandBuffer.bindDescriptorSets(
vk::PipelineBindPoint::eGraphics, **pipelineLayout, 0, { **descriptorSet }, nullptr ); vk::PipelineBindPoint::eGraphics, *pipelineLayout, 0, { *descriptorSet }, nullptr );
commandBuffer->setViewport( 0, commandBuffer.setViewport( 0,
vk::Viewport( 0.0f, vk::Viewport( 0.0f,
0.0f, 0.0f,
static_cast<float>( surfaceData.extent.width ), static_cast<float>( surfaceData.extent.width ),
static_cast<float>( surfaceData.extent.height ), static_cast<float>( surfaceData.extent.height ),
0.0f, 0.0f,
1.0f ) ); 1.0f ) );
commandBuffer->setScissor( 0, vk::Rect2D( vk::Offset2D( 0, 0 ), surfaceData.extent ) ); commandBuffer.setScissor( 0, vk::Rect2D( vk::Offset2D( 0, 0 ), surfaceData.extent ) );
commandBuffer->draw( 3, 1, 0, 0 ); commandBuffer.draw( 3, 1, 0, 0 );
commandBuffer->endRenderPass(); commandBuffer.endRenderPass();
commandBuffer->end(); commandBuffer.end();
std::unique_ptr<vk::raii::Fence> drawFence = vk::raii::su::make_unique<vk::raii::Fence>( *device, vk::FenceCreateInfo() ); vk::raii::Fence drawFence( device, vk::FenceCreateInfo() );
vk::PipelineStageFlags waitDestinationStageMask( vk::PipelineStageFlagBits::eColorAttachmentOutput ); vk::PipelineStageFlags waitDestinationStageMask( vk::PipelineStageFlagBits::eColorAttachmentOutput );
vk::SubmitInfo submitInfo( **imageAcquiredSemaphore, waitDestinationStageMask, **commandBuffer ); vk::SubmitInfo submitInfo( *imageAcquiredSemaphore, waitDestinationStageMask, *commandBuffer );
graphicsQueue->submit( submitInfo, **drawFence ); graphicsQueue.submit( submitInfo, *drawFence );
while ( vk::Result::eTimeout == device->waitForFences( { **drawFence }, VK_TRUE, vk::su::FenceTimeout ) ) while ( vk::Result::eTimeout == device.waitForFences( { *drawFence }, VK_TRUE, vk::su::FenceTimeout ) )
; ;
vk::PresentInfoKHR presentInfoKHR( nullptr, **swapChainData.swapChain, imageIndex ); vk::PresentInfoKHR presentInfoKHR( nullptr, **swapChainData.pSwapChain, imageIndex );
result = presentQueue->presentKHR( presentInfoKHR ); result = presentQueue.presentKHR( presentInfoKHR );
switch ( result ) switch ( result )
{ {
case vk::Result::eSuccess: break; case vk::Result::eSuccess: break;
@ -225,7 +215,7 @@ int main( int /*argc*/, char ** /*argv*/ )
/* VULKAN_KEY_END */ /* VULKAN_KEY_END */
device->waitIdle(); device.waitIdle();
} }
catch ( vk::SystemError & err ) catch ( vk::SystemError & err )
{ {

View File

@ -13,8 +13,8 @@
// limitations under the License. // limitations under the License.
// //
#include "utils.hpp"
#include "../../samples/utils/shaders.hpp" #include "../../samples/utils/shaders.hpp"
#include "utils.hpp"
namespace vk namespace vk
{ {
@ -23,9 +23,9 @@ namespace vk
namespace su namespace su
{ {
template <typename Dispatcher = VULKAN_HPP_DEFAULT_DISPATCHER_TYPE> template <typename Dispatcher = VULKAN_HPP_DEFAULT_DISPATCHER_TYPE>
std::unique_ptr<vk::raii::ShaderModule> makeUniqueShaderModule( vk::raii::Device const & device, vk::raii::ShaderModule makeShaderModule( vk::raii::Device const & device,
vk::ShaderStageFlagBits shaderStage, vk::ShaderStageFlagBits shaderStage,
std::string const & shaderText ) std::string const & shaderText )
{ {
std::vector<unsigned int> shaderSPV; std::vector<unsigned int> shaderSPV;
if ( !vk::su::GLSLtoSPV( shaderStage, shaderText, shaderSPV ) ) if ( !vk::su::GLSLtoSPV( shaderStage, shaderText, shaderSPV ) )
@ -33,8 +33,7 @@ namespace vk
throw std::runtime_error( "Could not convert glsl shader to spir-v -> terminating" ); throw std::runtime_error( "Could not convert glsl shader to spir-v -> terminating" );
} }
return vk::raii::su::make_unique<vk::raii::ShaderModule>( return vk::raii::ShaderModule( device, vk::ShaderModuleCreateInfo( vk::ShaderModuleCreateFlags(), shaderSPV ) );
device, vk::ShaderModuleCreateInfo( vk::ShaderModuleCreateFlags(), shaderSPV ) );
} }
} // namespace su } // namespace su
} // namespace raii } // namespace raii

View File

@ -205,14 +205,16 @@ namespace vk
vk::BufferUsageFlags usage, vk::BufferUsageFlags usage,
vk::MemoryPropertyFlags propertyFlags = vk::MemoryPropertyFlagBits::eHostVisible | vk::MemoryPropertyFlags propertyFlags = vk::MemoryPropertyFlagBits::eHostVisible |
vk::MemoryPropertyFlagBits::eHostCoherent ) vk::MemoryPropertyFlagBits::eHostCoherent )
: buffer( device, vk::BufferCreateInfo( {}, size, usage ) )
, deviceMemory( vk::raii::su::allocateDeviceMemory(
device, physicalDevice.getMemoryProperties(), buffer.getMemoryRequirements(), propertyFlags ) )
#if !defined( NDEBUG ) #if !defined( NDEBUG )
: m_size( size ), m_usage( usage ), m_propertyFlags( propertyFlags ) , m_size( size )
, m_usage( usage )
, m_propertyFlags( propertyFlags )
#endif #endif
{ {
buffer = vk::raii::su::make_unique<vk::raii::Buffer>( device, vk::BufferCreateInfo( {}, size, usage ) ); buffer.bindMemory( *deviceMemory, 0 );
deviceMemory = vk::raii::su::make_unique<vk::raii::DeviceMemory>( vk::raii::su::allocateDeviceMemory(
device, physicalDevice.getMemoryProperties(), buffer->getMemoryRequirements(), propertyFlags ) );
buffer->bindMemory( **deviceMemory, 0 );
} }
template <typename DataType> template <typename DataType>
@ -222,9 +224,9 @@ namespace vk
( m_propertyFlags & vk::MemoryPropertyFlagBits::eHostVisible ) ); ( m_propertyFlags & vk::MemoryPropertyFlagBits::eHostVisible ) );
assert( sizeof( DataType ) <= m_size ); assert( sizeof( DataType ) <= m_size );
void * dataPtr = deviceMemory->mapMemory( 0, sizeof( DataType ) ); void * dataPtr = deviceMemory.mapMemory( 0, sizeof( DataType ) );
memcpy( dataPtr, &data, sizeof( DataType ) ); memcpy( dataPtr, &data, sizeof( DataType ) );
deviceMemory->unmapMemory(); deviceMemory.unmapMemory();
} }
template <typename DataType> template <typename DataType>
@ -235,7 +237,7 @@ namespace vk
size_t elementSize = stride ? stride : sizeof( DataType ); size_t elementSize = stride ? stride : sizeof( DataType );
assert( sizeof( DataType ) <= elementSize ); assert( sizeof( DataType ) <= elementSize );
copyToDevice( *deviceMemory, data.data(), data.size(), elementSize ); copyToDevice( deviceMemory, data.data(), data.size(), elementSize );
} }
template <typename DataType> template <typename DataType>
@ -257,18 +259,19 @@ namespace vk
vk::raii::su::BufferData stagingBuffer( vk::raii::su::BufferData stagingBuffer(
physicalDevice, device, dataSize, vk::BufferUsageFlagBits::eTransferSrc ); physicalDevice, device, dataSize, vk::BufferUsageFlagBits::eTransferSrc );
copyToDevice( *stagingBuffer.deviceMemory, data.data(), data.size(), elementSize ); copyToDevice( stagingBuffer.deviceMemory, data.data(), data.size(), elementSize );
vk::raii::su::oneTimeSubmit( vk::raii::su::oneTimeSubmit(
device, device,
commandPool, commandPool,
queue, queue,
[&]( vk::raii::CommandBuffer const & commandBuffer ) [&]( vk::raii::CommandBuffer const & commandBuffer )
{ commandBuffer.copyBuffer( **stagingBuffer.buffer, **this->buffer, vk::BufferCopy( 0, 0, dataSize ) ); } ); { commandBuffer.copyBuffer( *stagingBuffer.buffer, *this->buffer, vk::BufferCopy( 0, 0, dataSize ) ); } );
} }
std::unique_ptr<vk::raii::Buffer> buffer; // the order of buffer and deviceMemory here is important to get the constructor running !
std::unique_ptr<vk::raii::DeviceMemory> deviceMemory; vk::raii::Buffer buffer;
vk::raii::DeviceMemory deviceMemory;
#if !defined( NDEBUG ) #if !defined( NDEBUG )
private: private:
vk::DeviceSize m_size; vk::DeviceSize m_size;
@ -289,38 +292,38 @@ namespace vk
vk::MemoryPropertyFlags memoryProperties, vk::MemoryPropertyFlags memoryProperties,
vk::ImageAspectFlags aspectMask ) vk::ImageAspectFlags aspectMask )
: format( format_ ) : format( format_ )
, image( device,
{ vk::ImageCreateFlags(),
vk::ImageType::e2D,
format,
vk::Extent3D( extent, 1 ),
1,
1,
vk::SampleCountFlagBits::e1,
tiling,
usage | vk::ImageUsageFlagBits::eSampled,
vk::SharingMode::eExclusive,
{},
initialLayout } )
, deviceMemory( vk::raii::su::allocateDeviceMemory(
device, physicalDevice.getMemoryProperties(), image.getMemoryRequirements(), memoryProperties ) )
{ {
vk::ImageCreateInfo imageCreateInfo( vk::ImageCreateFlags(), image.bindMemory( *deviceMemory, 0 );
vk::ImageType::e2D, pImageView = vk::raii::su::make_unique<vk::raii::ImageView>(
format, device,
vk::Extent3D( extent, 1 ), vk::ImageViewCreateInfo(
1, {},
1, *image,
vk::SampleCountFlagBits::e1, vk::ImageViewType::e2D,
tiling, format,
usage | vk::ImageUsageFlagBits::eSampled, { ComponentSwizzle::eR, ComponentSwizzle::eG, ComponentSwizzle::eB, ComponentSwizzle::eA },
vk::SharingMode::eExclusive, { aspectMask, 0, 1, 0, 1 } ) );
{},
initialLayout );
image = vk::raii::su::make_unique<vk::raii::Image>( device, imageCreateInfo );
deviceMemory = vk::raii::su::make_unique<vk::raii::DeviceMemory>( vk::raii::su::allocateDeviceMemory(
device, physicalDevice.getMemoryProperties(), image->getMemoryRequirements(), memoryProperties ) );
image->bindMemory( **deviceMemory, 0 );
vk::ComponentMapping componentMapping(
ComponentSwizzle::eR, ComponentSwizzle::eG, ComponentSwizzle::eB, ComponentSwizzle::eA );
vk::ImageSubresourceRange imageSubresourceRange( aspectMask, 0, 1, 0, 1 );
vk::ImageViewCreateInfo imageViewCreateInfo(
{}, **image, vk::ImageViewType::e2D, format, componentMapping, imageSubresourceRange );
imageView = vk::raii::su::make_unique<vk::raii::ImageView>( device, imageViewCreateInfo );
} }
vk::Format format; vk::Format format;
std::unique_ptr<vk::raii::Image> image; vk::raii::Image image;
std::unique_ptr<vk::raii::DeviceMemory> deviceMemory; vk::raii::DeviceMemory deviceMemory;
std::unique_ptr<vk::raii::ImageView> imageView; std::unique_ptr<vk::raii::ImageView> pImageView;
}; };
struct DepthBufferData : public ImageData struct DepthBufferData : public ImageData
@ -351,24 +354,24 @@ namespace vk
glfwCreateWindowSurface( static_cast<VkInstance>( *instance ), window.handle, nullptr, &_surface ); glfwCreateWindowSurface( static_cast<VkInstance>( *instance ), window.handle, nullptr, &_surface );
if ( err != VK_SUCCESS ) if ( err != VK_SUCCESS )
throw std::runtime_error( "Failed to create window!" ); throw std::runtime_error( "Failed to create window!" );
surface = vk::raii::su::make_unique<vk::raii::SurfaceKHR>( instance, _surface ); pSurface = vk::raii::su::make_unique<vk::raii::SurfaceKHR>( instance, _surface );
} }
vk::Extent2D extent; vk::Extent2D extent;
vk::su::WindowData window; vk::su::WindowData window;
std::unique_ptr<vk::raii::SurfaceKHR> surface; std::unique_ptr<vk::raii::SurfaceKHR> pSurface;
}; };
struct SwapChainData struct SwapChainData
{ {
SwapChainData( vk::raii::PhysicalDevice const & physicalDevice, SwapChainData( vk::raii::PhysicalDevice const & physicalDevice,
vk::raii::Device const & device, vk::raii::Device const & device,
vk::raii::SurfaceKHR const & surface, vk::raii::SurfaceKHR const & surface,
vk::Extent2D const & extent, vk::Extent2D const & extent,
vk::ImageUsageFlags usage, vk::ImageUsageFlags usage,
std::unique_ptr<vk::raii::SwapchainKHR> const & oldSwapchain, vk::raii::SwapchainKHR const * pOldSwapchain,
uint32_t graphicsQueueFamilyIndex, uint32_t graphicsQueueFamilyIndex,
uint32_t presentQueueFamilyIndex ) uint32_t presentQueueFamilyIndex )
{ {
vk::SurfaceFormatKHR surfaceFormat = vk::SurfaceFormatKHR surfaceFormat =
vk::su::pickSurfaceFormat( physicalDevice.getSurfaceFormatsKHR( *surface ) ); vk::su::pickSurfaceFormat( physicalDevice.getSurfaceFormatsKHR( *surface ) );
@ -417,7 +420,7 @@ namespace vk
compositeAlpha, compositeAlpha,
presentMode, presentMode,
true, true,
oldSwapchain ? **oldSwapchain : nullptr ); pOldSwapchain ? **pOldSwapchain : nullptr );
if ( graphicsQueueFamilyIndex != presentQueueFamilyIndex ) if ( graphicsQueueFamilyIndex != presentQueueFamilyIndex )
{ {
uint32_t queueFamilyIndices[2] = { graphicsQueueFamilyIndex, presentQueueFamilyIndex }; uint32_t queueFamilyIndices[2] = { graphicsQueueFamilyIndex, presentQueueFamilyIndex };
@ -428,9 +431,9 @@ namespace vk
swapChainCreateInfo.queueFamilyIndexCount = 2; swapChainCreateInfo.queueFamilyIndexCount = 2;
swapChainCreateInfo.pQueueFamilyIndices = queueFamilyIndices; swapChainCreateInfo.pQueueFamilyIndices = queueFamilyIndices;
} }
swapChain = vk::raii::su::make_unique<vk::raii::SwapchainKHR>( device, swapChainCreateInfo ); pSwapChain = vk::raii::su::make_unique<vk::raii::SwapchainKHR>( device, swapChainCreateInfo );
images = swapChain->getImages(); images = pSwapChain->getImages();
imageViews.reserve( images.size() ); imageViews.reserve( images.size() );
vk::ComponentMapping componentMapping( vk::ComponentMapping componentMapping(
@ -449,7 +452,7 @@ namespace vk
} }
vk::Format colorFormat; vk::Format colorFormat;
std::unique_ptr<vk::raii::SwapchainKHR> swapChain; std::unique_ptr<vk::raii::SwapchainKHR> pSwapChain;
std::vector<VkImage> images; std::vector<VkImage> images;
std::vector<vk::raii::ImageView> imageViews; std::vector<vk::raii::ImageView> imageViews;
}; };
@ -463,7 +466,25 @@ namespace vk
vk::FormatFeatureFlags formatFeatureFlags = {}, vk::FormatFeatureFlags formatFeatureFlags = {},
bool anisotropyEnable = false, bool anisotropyEnable = false,
bool forceStaging = false ) bool forceStaging = false )
: format( vk::Format::eR8G8B8A8Unorm ), extent( extent_ ) : format( vk::Format::eR8G8B8A8Unorm )
, extent( extent_ )
, sampler( device,
{ {},
vk::Filter::eLinear,
vk::Filter::eLinear,
vk::SamplerMipmapMode::eLinear,
vk::SamplerAddressMode::eRepeat,
vk::SamplerAddressMode::eRepeat,
vk::SamplerAddressMode::eRepeat,
0.0f,
anisotropyEnable,
16.0f,
false,
vk::CompareOp::eNever,
0.0f,
0.0f,
vk::BorderColor::eFloatOpaqueBlack } )
{ {
vk::FormatProperties formatProperties = physicalDevice.getFormatProperties( format ); vk::FormatProperties formatProperties = physicalDevice.getFormatProperties( format );
@ -476,7 +497,7 @@ namespace vk
if ( needsStaging ) if ( needsStaging )
{ {
assert( ( formatProperties.optimalTilingFeatures & formatFeatureFlags ) == formatFeatureFlags ); assert( ( formatProperties.optimalTilingFeatures & formatFeatureFlags ) == formatFeatureFlags );
stagingBufferData = vk::raii::su::make_unique<BufferData>( pStagingBufferData = vk::raii::su::make_unique<BufferData>(
physicalDevice, device, extent.width * extent.height * 4, vk::BufferUsageFlagBits::eTransferSrc ); physicalDevice, device, extent.width * extent.height * 4, vk::BufferUsageFlagBits::eTransferSrc );
imageTiling = vk::ImageTiling::eOptimal; imageTiling = vk::ImageTiling::eOptimal;
usageFlags |= vk::ImageUsageFlagBits::eTransferDst; usageFlags |= vk::ImageUsageFlagBits::eTransferDst;
@ -488,33 +509,15 @@ namespace vk
initialLayout = vk::ImageLayout::ePreinitialized; initialLayout = vk::ImageLayout::ePreinitialized;
requirements = vk::MemoryPropertyFlagBits::eHostCoherent | vk::MemoryPropertyFlagBits::eHostVisible; requirements = vk::MemoryPropertyFlagBits::eHostCoherent | vk::MemoryPropertyFlagBits::eHostVisible;
} }
imageData = vk::raii::su::make_unique<ImageData>( physicalDevice, pImageData = vk::raii::su::make_unique<ImageData>( physicalDevice,
device, device,
format, format,
extent, extent,
imageTiling, imageTiling,
usageFlags | vk::ImageUsageFlagBits::eSampled, usageFlags | vk::ImageUsageFlagBits::eSampled,
initialLayout, initialLayout,
requirements, requirements,
vk::ImageAspectFlagBits::eColor ); vk::ImageAspectFlagBits::eColor );
sampler =
vk::raii::su::make_unique<vk::raii::Sampler>( device,
vk::SamplerCreateInfo( vk::SamplerCreateFlags(),
vk::Filter::eLinear,
vk::Filter::eLinear,
vk::SamplerMipmapMode::eLinear,
vk::SamplerAddressMode::eRepeat,
vk::SamplerAddressMode::eRepeat,
vk::SamplerAddressMode::eRepeat,
0.0f,
anisotropyEnable,
16.0f,
false,
vk::CompareOp::eNever,
0.0f,
0.0f,
vk::BorderColor::eFloatOpaqueBlack ) );
} }
template <typename ImageGenerator> template <typename ImageGenerator>
@ -522,17 +525,17 @@ namespace vk
{ {
void * data = void * data =
needsStaging needsStaging
? stagingBufferData->deviceMemory->mapMemory( 0, stagingBufferData->buffer->getMemoryRequirements().size ) ? pStagingBufferData->deviceMemory.mapMemory( 0, pStagingBufferData->buffer.getMemoryRequirements().size )
: imageData->deviceMemory->mapMemory( 0, imageData->image->getMemoryRequirements().size ); : pImageData->deviceMemory.mapMemory( 0, pImageData->image.getMemoryRequirements().size );
imageGenerator( data, extent ); imageGenerator( data, extent );
needsStaging ? stagingBufferData->deviceMemory->unmapMemory() : imageData->deviceMemory->unmapMemory(); needsStaging ? pStagingBufferData->deviceMemory.unmapMemory() : pImageData->deviceMemory.unmapMemory();
if ( needsStaging ) if ( needsStaging )
{ {
// Since we're going to blit to the texture image, set its layout to eTransferDstOptimal // Since we're going to blit to the texture image, set its layout to eTransferDstOptimal
vk::raii::su::setImageLayout( commandBuffer, vk::raii::su::setImageLayout( commandBuffer,
**imageData->image, *pImageData->image,
imageData->format, pImageData->format,
vk::ImageLayout::eUndefined, vk::ImageLayout::eUndefined,
vk::ImageLayout::eTransferDstOptimal ); vk::ImageLayout::eTransferDstOptimal );
vk::BufferImageCopy copyRegion( 0, vk::BufferImageCopy copyRegion( 0,
@ -542,11 +545,11 @@ namespace vk
vk::Offset3D( 0, 0, 0 ), vk::Offset3D( 0, 0, 0 ),
vk::Extent3D( extent, 1 ) ); vk::Extent3D( extent, 1 ) );
commandBuffer.copyBufferToImage( commandBuffer.copyBufferToImage(
**stagingBufferData->buffer, **imageData->image, vk::ImageLayout::eTransferDstOptimal, copyRegion ); *pStagingBufferData->buffer, *pImageData->image, vk::ImageLayout::eTransferDstOptimal, copyRegion );
// Set the layout for the texture image from eTransferDstOptimal to SHADER_READ_ONLY // Set the layout for the texture image from eTransferDstOptimal to SHADER_READ_ONLY
vk::raii::su::setImageLayout( commandBuffer, vk::raii::su::setImageLayout( commandBuffer,
**imageData->image, *pImageData->image,
imageData->format, pImageData->format,
vk::ImageLayout::eTransferDstOptimal, vk::ImageLayout::eTransferDstOptimal,
vk::ImageLayout::eShaderReadOnlyOptimal ); vk::ImageLayout::eShaderReadOnlyOptimal );
} }
@ -554,19 +557,19 @@ namespace vk
{ {
// If we can use the linear tiled image as a texture, just do it // If we can use the linear tiled image as a texture, just do it
vk::raii::su::setImageLayout( commandBuffer, vk::raii::su::setImageLayout( commandBuffer,
**imageData->image, *pImageData->image,
imageData->format, pImageData->format,
vk::ImageLayout::ePreinitialized, vk::ImageLayout::ePreinitialized,
vk::ImageLayout::eShaderReadOnlyOptimal ); vk::ImageLayout::eShaderReadOnlyOptimal );
} }
} }
vk::Format format; vk::Format format;
vk::Extent2D extent; vk::Extent2D extent;
bool needsStaging; bool needsStaging;
std::unique_ptr<BufferData> stagingBufferData; std::unique_ptr<BufferData> pStagingBufferData;
std::unique_ptr<ImageData> imageData; std::unique_ptr<ImageData> pImageData;
std::unique_ptr<vk::raii::Sampler> sampler; vk::raii::Sampler sampler;
}; };
std::pair<uint32_t, uint32_t> std::pair<uint32_t, uint32_t>
@ -608,32 +611,15 @@ namespace vk
throw std::runtime_error( "Could not find queues for both graphics or present -> terminating" ); throw std::runtime_error( "Could not find queues for both graphics or present -> terminating" );
} }
std::unique_ptr<vk::raii::CommandBuffer> makeUniqueCommandBuffer( vk::raii::Device const & device, vk::raii::CommandBuffer makeCommandBuffer( vk::raii::Device const & device,
vk::raii::CommandPool const & commandPool ) vk::raii::CommandPool const & commandPool )
{ {
vk::CommandBufferAllocateInfo commandBufferAllocateInfo( *commandPool, vk::CommandBufferLevel::ePrimary, 1 ); vk::CommandBufferAllocateInfo commandBufferAllocateInfo( *commandPool, vk::CommandBufferLevel::ePrimary, 1 );
return vk::raii::su::make_unique<vk::raii::CommandBuffer>( return std::move( vk::raii::CommandBuffers( device, commandBufferAllocateInfo ).front() );
std::move( vk::raii::CommandBuffers( device, commandBufferAllocateInfo ).front() ) );
} }
std::unique_ptr<vk::raii::CommandPool> makeUniqueCommandPool( vk::raii::Device const & device, vk::raii::DescriptorPool makeDescriptorPool( vk::raii::Device const & device,
uint32_t queueFamilyIndex ) std::vector<vk::DescriptorPoolSize> const & poolSizes )
{
vk::CommandPoolCreateInfo commandPoolCreateInfo( vk::CommandPoolCreateFlagBits::eResetCommandBuffer,
queueFamilyIndex );
return vk::raii::su::make_unique<vk::raii::CommandPool>( device, commandPoolCreateInfo );
}
std::unique_ptr<vk::raii::DebugUtilsMessengerEXT>
makeUniqueDebugUtilsMessengerEXT( vk::raii::Instance const & instance )
{
return vk::raii::su::make_unique<vk::raii::DebugUtilsMessengerEXT>(
instance, vk::su::makeDebugUtilsMessengerCreateInfoEXT() );
}
std::unique_ptr<vk::raii::DescriptorPool>
makeUniqueDescriptorPool( vk::raii::Device const & device,
std::vector<vk::DescriptorPoolSize> const & poolSizes )
{ {
assert( !poolSizes.empty() ); assert( !poolSizes.empty() );
uint32_t maxSets = std::accumulate( poolSizes.begin(), uint32_t maxSets = std::accumulate( poolSizes.begin(),
@ -645,20 +631,10 @@ namespace vk
vk::DescriptorPoolCreateInfo descriptorPoolCreateInfo( vk::DescriptorPoolCreateInfo descriptorPoolCreateInfo(
vk::DescriptorPoolCreateFlagBits::eFreeDescriptorSet, maxSets, poolSizes ); vk::DescriptorPoolCreateFlagBits::eFreeDescriptorSet, maxSets, poolSizes );
return vk::raii::su::make_unique<vk::raii::DescriptorPool>( device, descriptorPoolCreateInfo ); return vk::raii::DescriptorPool( device, descriptorPoolCreateInfo );
} }
std::unique_ptr<vk::raii::DescriptorSet> vk::raii::DescriptorSetLayout makeDescriptorSetLayout(
makeUniqueDescriptorSet( vk::raii::Device const & device,
vk::raii::DescriptorPool const & descriptorPool,
vk::raii::DescriptorSetLayout const & descriptorSetLayout )
{
vk::DescriptorSetAllocateInfo descriptorSetAllocateInfo( *descriptorPool, *descriptorSetLayout );
return vk::raii::su::make_unique<vk::raii::DescriptorSet>(
std::move( vk::raii::DescriptorSets( device, descriptorSetAllocateInfo ).front() ) );
}
std::unique_ptr<vk::raii::DescriptorSetLayout> makeUniqueDescriptorSetLayout(
vk::raii::Device const & device, vk::raii::Device const & device,
std::vector<std::tuple<vk::DescriptorType, uint32_t, vk::ShaderStageFlags>> const & bindingData, std::vector<std::tuple<vk::DescriptorType, uint32_t, vk::ShaderStageFlags>> const & bindingData,
vk::DescriptorSetLayoutCreateFlags flags = {} ) vk::DescriptorSetLayoutCreateFlags flags = {} )
@ -672,15 +648,14 @@ namespace vk
std::get<2>( bindingData[i] ) ); std::get<2>( bindingData[i] ) );
} }
vk::DescriptorSetLayoutCreateInfo descriptorSetLayoutCreateInfo( flags, bindings ); vk::DescriptorSetLayoutCreateInfo descriptorSetLayoutCreateInfo( flags, bindings );
return vk::raii::su::make_unique<vk::raii::DescriptorSetLayout>( device, descriptorSetLayoutCreateInfo ); return vk::raii::DescriptorSetLayout( device, descriptorSetLayoutCreateInfo );
} }
std::unique_ptr<vk::raii::Device> vk::raii::Device makeDevice( vk::raii::PhysicalDevice const & physicalDevice,
makeUniqueDevice( vk::raii::PhysicalDevice const & physicalDevice, uint32_t queueFamilyIndex,
uint32_t queueFamilyIndex, std::vector<std::string> const & extensions = {},
std::vector<std::string> const & extensions = {}, vk::PhysicalDeviceFeatures const * physicalDeviceFeatures = nullptr,
vk::PhysicalDeviceFeatures const * physicalDeviceFeatures = nullptr, void const * pNext = nullptr )
void const * pNext = nullptr )
{ {
std::vector<char const *> enabledExtensions; std::vector<char const *> enabledExtensions;
enabledExtensions.reserve( extensions.size() ); enabledExtensions.reserve( extensions.size() );
@ -695,50 +670,49 @@ namespace vk
vk::DeviceCreateInfo deviceCreateInfo( vk::DeviceCreateInfo deviceCreateInfo(
vk::DeviceCreateFlags(), deviceQueueCreateInfo, {}, enabledExtensions, physicalDeviceFeatures ); vk::DeviceCreateFlags(), deviceQueueCreateInfo, {}, enabledExtensions, physicalDeviceFeatures );
deviceCreateInfo.pNext = pNext; deviceCreateInfo.pNext = pNext;
return vk::raii::su::make_unique<vk::raii::Device>( physicalDevice, deviceCreateInfo ); return vk::raii::Device( physicalDevice, deviceCreateInfo );
} }
std::vector<std::unique_ptr<vk::raii::Framebuffer>> std::vector<vk::raii::Framebuffer> makeFramebuffers( vk::raii::Device const & device,
makeUniqueFramebuffers( vk::raii::Device const & device, vk::raii::RenderPass & renderPass,
vk::raii::RenderPass & renderPass, std::vector<vk::raii::ImageView> const & imageViews,
std::vector<vk::raii::ImageView> const & imageViews, vk::raii::ImageView const * pDepthImageView,
std::unique_ptr<vk::raii::ImageView> const & depthImageView, vk::Extent2D const & extent )
vk::Extent2D const & extent )
{ {
vk::ImageView attachments[2]; vk::ImageView attachments[2];
attachments[1] = depthImageView ? **depthImageView : vk::ImageView(); attachments[1] = pDepthImageView ? **pDepthImageView : vk::ImageView();
vk::FramebufferCreateInfo framebufferCreateInfo( vk::FramebufferCreateFlags(), vk::FramebufferCreateInfo framebufferCreateInfo( vk::FramebufferCreateFlags(),
*renderPass, *renderPass,
depthImageView ? 2 : 1, pDepthImageView ? 2 : 1,
attachments, attachments,
extent.width, extent.width,
extent.height, extent.height,
1 ); 1 );
std::vector<std::unique_ptr<vk::raii::Framebuffer>> framebuffers; std::vector<vk::raii::Framebuffer> framebuffers;
framebuffers.reserve( imageViews.size() ); framebuffers.reserve( imageViews.size() );
for ( auto const & imageView : imageViews ) for ( auto const & imageView : imageViews )
{ {
attachments[0] = *imageView; attachments[0] = *imageView;
framebuffers.push_back( vk::raii::su::make_unique<vk::raii::Framebuffer>( device, framebufferCreateInfo ) ); framebuffers.push_back( vk::raii::Framebuffer( device, framebufferCreateInfo ) );
} }
return framebuffers; return framebuffers;
} }
std::unique_ptr<vk::raii::Pipeline> makeUniqueGraphicsPipeline( vk::raii::Pipeline
vk::raii::Device const & device, makeGraphicsPipeline( vk::raii::Device const & device,
vk::raii::PipelineCache const & pipelineCache, vk::raii::PipelineCache const & pipelineCache,
vk::raii::ShaderModule const & vertexShaderModule, vk::raii::ShaderModule const & vertexShaderModule,
vk::SpecializationInfo const * vertexShaderSpecializationInfo, vk::SpecializationInfo const * vertexShaderSpecializationInfo,
vk::raii::ShaderModule const & fragmentShaderModule, vk::raii::ShaderModule const & fragmentShaderModule,
vk::SpecializationInfo const * fragmentShaderSpecializationInfo, vk::SpecializationInfo const * fragmentShaderSpecializationInfo,
uint32_t vertexStride, uint32_t vertexStride,
std::vector<std::pair<vk::Format, uint32_t>> const & vertexInputAttributeFormatOffset, std::vector<std::pair<vk::Format, uint32_t>> const & vertexInputAttributeFormatOffset,
vk::FrontFace frontFace, vk::FrontFace frontFace,
bool depthBuffered, bool depthBuffered,
vk::raii::PipelineLayout const & pipelineLayout, vk::raii::PipelineLayout const & pipelineLayout,
vk::raii::RenderPass const & renderPass ) vk::raii::RenderPass const & renderPass )
{ {
std::array<vk::PipelineShaderStageCreateInfo, 2> pipelineShaderStageCreateInfos = { std::array<vk::PipelineShaderStageCreateInfo, 2> pipelineShaderStageCreateInfos = {
vk::PipelineShaderStageCreateInfo( vk::PipelineShaderStageCreateInfo(
@ -831,10 +805,10 @@ namespace vk
*pipelineLayout, *pipelineLayout,
*renderPass ); *renderPass );
return vk::raii::su::make_unique<vk::raii::Pipeline>( device, pipelineCache, graphicsPipelineCreateInfo ); return vk::raii::Pipeline( device, pipelineCache, graphicsPipelineCreateInfo );
} }
std::unique_ptr<vk::raii::Image> makeUniqueImage( vk::raii::Device const & device ) vk::raii::Image makeImage( vk::raii::Device const & device )
{ {
vk::ImageCreateInfo imageCreateInfo( {}, vk::ImageCreateInfo imageCreateInfo( {},
vk::ImageType::e2D, vk::ImageType::e2D,
@ -845,15 +819,15 @@ namespace vk
vk::SampleCountFlagBits::e1, vk::SampleCountFlagBits::e1,
vk::ImageTiling::eLinear, vk::ImageTiling::eLinear,
vk::ImageUsageFlagBits::eTransferSrc ); vk::ImageUsageFlagBits::eTransferSrc );
return vk::raii::su::make_unique<vk::raii::Image>( device, imageCreateInfo ); return vk::raii::Image( device, imageCreateInfo );
} }
std::unique_ptr<vk::raii::Instance> makeUniqueInstance( vk::raii::Context const & context, vk::raii::Instance makeInstance( vk::raii::Context const & context,
std::string const & appName, std::string const & appName,
std::string const & engineName, std::string const & engineName,
std::vector<std::string> const & layers = {}, std::vector<std::string> const & layers = {},
std::vector<std::string> const & extensions = {}, std::vector<std::string> const & extensions = {},
uint32_t apiVersion = VK_API_VERSION_1_0 ) uint32_t apiVersion = VK_API_VERSION_1_0 )
{ {
vk::ApplicationInfo applicationInfo( appName.c_str(), 1, engineName.c_str(), 1, apiVersion ); vk::ApplicationInfo applicationInfo( appName.c_str(), 1, engineName.c_str(), 1, apiVersion );
std::vector<char const *> enabledLayers = vk::su::gatherLayers( layers std::vector<char const *> enabledLayers = vk::su::gatherLayers( layers
@ -877,30 +851,14 @@ namespace vk
instanceCreateInfoChain = instanceCreateInfoChain =
vk::su::makeInstanceCreateInfoChain( applicationInfo, enabledLayers, enabledExtensions ); vk::su::makeInstanceCreateInfoChain( applicationInfo, enabledLayers, enabledExtensions );
return vk::raii::su::make_unique<vk::raii::Instance>( context, return vk::raii::Instance( context, instanceCreateInfoChain.get<vk::InstanceCreateInfo>() );
instanceCreateInfoChain.get<vk::InstanceCreateInfo>() );
} }
std::unique_ptr<vk::raii::PhysicalDevice> makeUniquePhysicalDevice( vk::raii::Instance const & instance ) vk::raii::RenderPass makeRenderPass( vk::raii::Device const & device,
{ vk::Format colorFormat,
return vk::raii::su::make_unique<vk::raii::PhysicalDevice>( vk::Format depthFormat,
std::move( vk::raii::PhysicalDevices( instance ).front() ) ); vk::AttachmentLoadOp loadOp = vk::AttachmentLoadOp::eClear,
} vk::ImageLayout colorFinalLayout = vk::ImageLayout::ePresentSrcKHR )
std::unique_ptr<vk::raii::PipelineLayout>
makeUniquePipelineLayout( vk::raii::Device const & device,
vk::raii::DescriptorSetLayout const & descriptorSetLayout )
{
vk::PipelineLayoutCreateInfo pipelineLayoutCreateInfo( vk::PipelineLayoutCreateFlags(), *descriptorSetLayout );
return vk::raii::su::make_unique<vk::raii::PipelineLayout>( device, pipelineLayoutCreateInfo );
}
std::unique_ptr<vk::raii::RenderPass>
makeUniqueRenderPass( vk::raii::Device const & device,
vk::Format colorFormat,
vk::Format depthFormat,
vk::AttachmentLoadOp loadOp = vk::AttachmentLoadOp::eClear,
vk::ImageLayout colorFinalLayout = vk::ImageLayout::ePresentSrcKHR )
{ {
std::vector<vk::AttachmentDescription> attachmentDescriptions; std::vector<vk::AttachmentDescription> attachmentDescriptions;
assert( colorFormat != vk::Format::eUndefined ); assert( colorFormat != vk::Format::eUndefined );
@ -936,7 +894,7 @@ namespace vk
: nullptr ); : nullptr );
vk::RenderPassCreateInfo renderPassCreateInfo( vk::RenderPassCreateInfo renderPassCreateInfo(
vk::RenderPassCreateFlags(), attachmentDescriptions, subpassDescription ); vk::RenderPassCreateFlags(), attachmentDescriptions, subpassDescription );
return vk::raii::su::make_unique<vk::raii::RenderPass>( device, renderPassCreateInfo ); return vk::raii::RenderPass( device, renderPassCreateInfo );
} }
vk::Format pickDepthFormat( vk::raii::PhysicalDevice const & physicalDevice ) vk::Format pickDepthFormat( vk::raii::PhysicalDevice const & physicalDevice )
@ -999,7 +957,7 @@ namespace vk
} }
vk::DescriptorImageInfo imageInfo( vk::DescriptorImageInfo imageInfo(
**textureData.sampler, **textureData.imageData->imageView, vk::ImageLayout::eShaderReadOnlyOptimal ); *textureData.sampler, **textureData.pImageData->pImageView, vk::ImageLayout::eShaderReadOnlyOptimal );
writeDescriptorSets.emplace_back( writeDescriptorSets.emplace_back(
*descriptorSet, dstBinding, 0, vk::DescriptorType::eCombinedImageSampler, imageInfo, nullptr, nullptr ); *descriptorSet, dstBinding, 0, vk::DescriptorType::eCombinedImageSampler, imageInfo, nullptr, nullptr );
@ -1045,7 +1003,7 @@ namespace vk
for ( auto const & thd : textureData ) for ( auto const & thd : textureData )
{ {
imageInfos.emplace_back( imageInfos.emplace_back(
**thd.sampler, **thd.imageData->imageView, vk::ImageLayout::eShaderReadOnlyOptimal ); *thd.sampler, **thd.pImageData->pImageView, vk::ImageLayout::eShaderReadOnlyOptimal );
} }
writeDescriptorSets.emplace_back( *descriptorSet, writeDescriptorSets.emplace_back( *descriptorSet,
dstBinding, dstBinding,

View File

@ -353,30 +353,6 @@ namespace vk
#endif #endif
); );
#if defined( NDEBUG )
// in non-debug mode just use the InstanceCreateInfo for instance creation
vk::StructureChain<vk::InstanceCreateInfo> instanceCreateInfo(
{ {}, &applicationInfo, enabledLayers, enabledExtensions } );
#else
// in debug mode, addionally use the debugUtilsMessengerCallback in instance creation!
vk::DebugUtilsMessageSeverityFlagsEXT severityFlags( vk::DebugUtilsMessageSeverityFlagBitsEXT::eWarning |
vk::DebugUtilsMessageSeverityFlagBitsEXT::eError );
vk::DebugUtilsMessageTypeFlagsEXT messageTypeFlags( vk::DebugUtilsMessageTypeFlagBitsEXT::eGeneral |
vk::DebugUtilsMessageTypeFlagBitsEXT::ePerformance |
vk::DebugUtilsMessageTypeFlagBitsEXT::eValidation );
# if defined( VULKAN_HPP_UTILS_USE_BEST_PRACTICES )
vk::ValidationFeatureEnableEXT validationFeatureEnable = vk::ValidationFeatureEnableEXT::eBestPractices;
vk::StructureChain<vk::InstanceCreateInfo, vk::DebugUtilsMessengerCreateInfoEXT, vk::ValidationFeaturesEXT>
instanceCreateInfo( { {}, &applicationInfo, enabledLayers, enabledExtensions },
{ {}, severityFlags, messageTypeFlags, &vk::su::debugUtilsMessengerCallback },
{ validationFeatureEnable } );
# else
vk::StructureChain<vk::InstanceCreateInfo, vk::DebugUtilsMessengerCreateInfoEXT> instanceCreateInfo(
{ {}, &applicationInfo, enabledLayers, enabledExtensions },
{ {}, severityFlags, messageTypeFlags, &vk::su::debugUtilsMessengerCallback } );
# endif
#endif
vk::Instance instance = vk::Instance instance =
vk::createInstance( makeInstanceCreateInfoChain( applicationInfo, enabledLayers, enabledExtensions ) vk::createInstance( makeInstanceCreateInfoChain( applicationInfo, enabledLayers, enabledExtensions )
.get<vk::InstanceCreateInfo>() ); .get<vk::InstanceCreateInfo>() );