Refactor RAII-samples to use std::unique_ptr only if needed.
This commit is contained in:
@@ -26,18 +26,17 @@ int main( int /*argc*/, char ** /*argv*/ )
|
||||
{
|
||||
try
|
||||
{
|
||||
std::unique_ptr<vk::raii::Context> context = vk::raii::su::make_unique<vk::raii::Context>();
|
||||
std::unique_ptr<vk::raii::Instance> instance =
|
||||
vk::raii::su::makeUniqueInstance( *context, AppName, EngineName, {}, vk::su::getInstanceExtensions() );
|
||||
vk::raii::Context context;
|
||||
vk::raii::Instance instance =
|
||||
vk::raii::su::makeInstance( context, AppName, EngineName, {}, vk::su::getInstanceExtensions() );
|
||||
#if !defined( NDEBUG )
|
||||
std::unique_ptr<vk::raii::DebugUtilsMessengerEXT> debugUtilsMessenger =
|
||||
vk::raii::su::makeUniqueDebugUtilsMessengerEXT( *instance );
|
||||
vk::raii::DebugUtilsMessengerEXT debugUtilsMessenger( instance, vk::su::makeDebugUtilsMessengerCreateInfoEXT() );
|
||||
#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 ) )
|
||||
{
|
||||
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 =
|
||||
vk::raii::su::findGraphicsAndPresentQueueFamilyIndex( *physicalDevice, *surfaceData.surface );
|
||||
std::unique_ptr<vk::raii::Device> device = vk::raii::su::makeUniqueDevice(
|
||||
*physicalDevice, graphicsAndPresentQueueFamilyIndex.first, vk::su::getDeviceExtensions() );
|
||||
vk::raii::su::findGraphicsAndPresentQueueFamilyIndex( physicalDevice, *surfaceData.pSurface );
|
||||
vk::raii::Device device = vk::raii::su::makeDevice(
|
||||
physicalDevice, graphicsAndPresentQueueFamilyIndex.first, vk::su::getDeviceExtensions() );
|
||||
|
||||
std::unique_ptr<vk::raii::CommandPool> commandPool =
|
||||
vk::raii::su::makeUniqueCommandPool( *device, graphicsAndPresentQueueFamilyIndex.first );
|
||||
std::unique_ptr<vk::raii::CommandBuffer> commandBuffer =
|
||||
vk::raii::su::makeUniqueCommandBuffer( *device, *commandPool );
|
||||
vk::raii::CommandPool commandPool = vk::raii::CommandPool(
|
||||
device, { vk::CommandPoolCreateFlagBits::eResetCommandBuffer, graphicsAndPresentQueueFamilyIndex.first } );
|
||||
vk::raii::CommandBuffer commandBuffer = vk::raii::su::makeCommandBuffer( device, commandPool );
|
||||
|
||||
std::unique_ptr<vk::raii::Queue> graphicsQueue =
|
||||
vk::raii::su::make_unique<vk::raii::Queue>( *device, graphicsAndPresentQueueFamilyIndex.first, 0 );
|
||||
std::unique_ptr<vk::raii::Queue> presentQueue =
|
||||
vk::raii::su::make_unique<vk::raii::Queue>( *device, graphicsAndPresentQueueFamilyIndex.second, 0 );
|
||||
vk::raii::Queue graphicsQueue( device, graphicsAndPresentQueueFamilyIndex.first, 0 );
|
||||
vk::raii::Queue presentQueue( device, graphicsAndPresentQueueFamilyIndex.second, 0 );
|
||||
|
||||
vk::raii::su::SwapChainData swapChainData( *physicalDevice,
|
||||
*device,
|
||||
*surfaceData.surface,
|
||||
vk::raii::su::SwapChainData swapChainData( physicalDevice,
|
||||
device,
|
||||
*surfaceData.pSurface,
|
||||
surfaceData.extent,
|
||||
vk::ImageUsageFlagBits::eColorAttachment |
|
||||
vk::ImageUsageFlagBits::eTransferSrc |
|
||||
@@ -72,30 +68,29 @@ int main( int /*argc*/, char ** /*argv*/ )
|
||||
|
||||
/* VULKAN_KEY_START */
|
||||
|
||||
vk::FormatProperties formatProperties = physicalDevice->getFormatProperties( swapChainData.colorFormat );
|
||||
vk::FormatProperties formatProperties = physicalDevice.getFormatProperties( swapChainData.colorFormat );
|
||||
assert( ( formatProperties.linearTilingFeatures & vk::FormatFeatureFlagBits::eBlitSrc ) &&
|
||||
"Format cannot be used as transfer source" );
|
||||
|
||||
std::unique_ptr<vk::raii::Semaphore> imageAcquiredSemaphore =
|
||||
vk::raii::su::make_unique<vk::raii::Semaphore>( *device, vk::SemaphoreCreateInfo() );
|
||||
vk::raii::Semaphore imageAcquiredSemaphore( device, vk::SemaphoreCreateInfo() );
|
||||
|
||||
// Get the index of the next available swapchain image:
|
||||
vk::Result result;
|
||||
uint32_t 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( imageIndex < swapChainData.images.size() );
|
||||
|
||||
commandBuffer->begin( vk::CommandBufferBeginInfo() );
|
||||
vk::raii::su::setImageLayout( *commandBuffer,
|
||||
commandBuffer.begin( vk::CommandBufferBeginInfo() );
|
||||
vk::raii::su::setImageLayout( commandBuffer,
|
||||
static_cast<vk::Image>( swapChainData.images[imageIndex] ),
|
||||
swapChainData.colorFormat,
|
||||
vk::ImageLayout::eUndefined,
|
||||
vk::ImageLayout::eTransferDstOptimal );
|
||||
|
||||
// Create an image, map it, and write some values to the image
|
||||
vk::ImageCreateInfo imageCreateInfo( {},
|
||||
vk::ImageCreateInfo imageCreateInfo( {},
|
||||
vk::ImageType::e2D,
|
||||
swapChainData.colorFormat,
|
||||
vk::Extent3D( surfaceData.extent, 1 ),
|
||||
@@ -104,38 +99,36 @@ int main( int /*argc*/, char ** /*argv*/ )
|
||||
vk::SampleCountFlagBits::e1,
|
||||
vk::ImageTiling::eLinear,
|
||||
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::MemoryRequirements memoryRequirements = blitSourceImage->getMemoryRequirements();
|
||||
vk::PhysicalDeviceMemoryProperties memoryProperties = physicalDevice.getMemoryProperties();
|
||||
vk::MemoryRequirements memoryRequirements = blitSourceImage.getMemoryRequirements();
|
||||
uint32_t memoryTypeIndex = vk::su::findMemoryType(
|
||||
memoryProperties, memoryRequirements.memoryTypeBits, vk::MemoryPropertyFlagBits::eHostVisible );
|
||||
|
||||
vk::MemoryAllocateInfo memoryAllocateInfo( memoryRequirements.size, memoryTypeIndex );
|
||||
std::unique_ptr<vk::raii::DeviceMemory> deviceMemory =
|
||||
vk::raii::su::make_unique<vk::raii::DeviceMemory>( *device, memoryAllocateInfo );
|
||||
blitSourceImage->bindMemory( **deviceMemory, 0 );
|
||||
vk::MemoryAllocateInfo memoryAllocateInfo( memoryRequirements.size, memoryTypeIndex );
|
||||
vk::raii::DeviceMemory deviceMemory( device, memoryAllocateInfo );
|
||||
blitSourceImage.bindMemory( *deviceMemory, 0 );
|
||||
|
||||
vk::raii::su::setImageLayout( *commandBuffer,
|
||||
**blitSourceImage,
|
||||
vk::raii::su::setImageLayout( commandBuffer,
|
||||
*blitSourceImage,
|
||||
swapChainData.colorFormat,
|
||||
vk::ImageLayout::eUndefined,
|
||||
vk::ImageLayout::eGeneral );
|
||||
|
||||
commandBuffer->end();
|
||||
commandBuffer.end();
|
||||
|
||||
/* 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::PipelineStageFlags waitDestinationStageMask( vk::PipelineStageFlagBits::eColorAttachmentOutput );
|
||||
vk::SubmitInfo submitInfo( **imageAcquiredSemaphore, waitDestinationStageMask, **commandBuffer );
|
||||
graphicsQueue->submit( submitInfo, **commandFence );
|
||||
vk::raii::Fence commandFence( device, vk::FenceCreateInfo() );
|
||||
vk::PipelineStageFlags waitDestinationStageMask( vk::PipelineStageFlagBits::eColorAttachmentOutput );
|
||||
vk::SubmitInfo submitInfo( *imageAcquiredSemaphore, waitDestinationStageMask, *commandBuffer );
|
||||
graphicsQueue.submit( submitInfo, *commandFence );
|
||||
|
||||
/* 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 =
|
||||
static_cast<unsigned char *>( deviceMemory->mapMemory( 0, memoryRequirements.size ) );
|
||||
unsigned char * pImageMemory = static_cast<unsigned char *>( deviceMemory.mapMemory( 0, memoryRequirements.size ) );
|
||||
|
||||
// Checkerboard of 8x8 pixel squares
|
||||
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
|
||||
vk::MappedMemoryRange mappedMemoryRange( **deviceMemory, 0, memoryRequirements.size );
|
||||
device->flushMappedMemoryRanges( mappedMemoryRange );
|
||||
deviceMemory->unmapMemory();
|
||||
vk::MappedMemoryRange mappedMemoryRange( *deviceMemory, 0, memoryRequirements.size );
|
||||
device.flushMappedMemoryRanges( mappedMemoryRange );
|
||||
deviceMemory.unmapMemory();
|
||||
|
||||
commandBuffer->reset( {} );
|
||||
commandBuffer->begin( vk::CommandBufferBeginInfo() );
|
||||
commandBuffer.reset( {} );
|
||||
commandBuffer.begin( vk::CommandBufferBeginInfo() );
|
||||
|
||||
// Intend to blit from this image, set the layout accordingly
|
||||
vk::raii::su::setImageLayout( *commandBuffer,
|
||||
**blitSourceImage,
|
||||
vk::raii::su::setImageLayout( commandBuffer,
|
||||
*blitSourceImage,
|
||||
swapChainData.colorFormat,
|
||||
vk::ImageLayout::eGeneral,
|
||||
vk::ImageLayout::eTransferSrcOptimal );
|
||||
@@ -175,12 +168,12 @@ int main( int /*argc*/, char ** /*argv*/ )
|
||||
{ { vk::Offset3D( 0, 0, 0 ), vk::Offset3D( 32, 32, 1 ) } },
|
||||
imageSubresourceLayers,
|
||||
{ { vk::Offset3D( 0, 0, 0 ), vk::Offset3D( surfaceData.extent.width, surfaceData.extent.height, 1 ) } } );
|
||||
commandBuffer->blitImage( **blitSourceImage,
|
||||
vk::ImageLayout::eTransferSrcOptimal,
|
||||
blitDestinationImage,
|
||||
vk::ImageLayout::eTransferDstOptimal,
|
||||
imageBlit,
|
||||
vk::Filter::eLinear );
|
||||
commandBuffer.blitImage( *blitSourceImage,
|
||||
vk::ImageLayout::eTransferSrcOptimal,
|
||||
blitDestinationImage,
|
||||
vk::ImageLayout::eTransferDstOptimal,
|
||||
imageBlit,
|
||||
vk::Filter::eLinear );
|
||||
|
||||
// Use a barrier to make sure the blit is finished before the copy starts
|
||||
vk::ImageMemoryBarrier memoryBarrier( vk::AccessFlagBits::eTransferWrite,
|
||||
@@ -191,7 +184,7 @@ int main( int /*argc*/, char ** /*argv*/ )
|
||||
VK_QUEUE_FAMILY_IGNORED,
|
||||
blitDestinationImage,
|
||||
vk::ImageSubresourceRange( vk::ImageAspectFlagBits::eColor, 0, 1, 0, 1 ) );
|
||||
commandBuffer->pipelineBarrier(
|
||||
commandBuffer.pipelineBarrier(
|
||||
vk::PipelineStageFlagBits::eTransfer, vk::PipelineStageFlagBits::eTransfer, {}, nullptr, nullptr, memoryBarrier );
|
||||
|
||||
// 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,
|
||||
vk::Offset3D( 256, 256, 0 ),
|
||||
vk::Extent3D( 128, 128, 1 ) );
|
||||
commandBuffer->copyImage( **blitSourceImage,
|
||||
vk::ImageLayout::eTransferSrcOptimal,
|
||||
blitDestinationImage,
|
||||
vk::ImageLayout::eTransferDstOptimal,
|
||||
imageCopy );
|
||||
commandBuffer.copyImage( *blitSourceImage,
|
||||
vk::ImageLayout::eTransferSrcOptimal,
|
||||
blitDestinationImage,
|
||||
vk::ImageLayout::eTransferDstOptimal,
|
||||
imageCopy );
|
||||
|
||||
vk::ImageMemoryBarrier prePresentBarrier(
|
||||
vk::AccessFlagBits::eTransferWrite,
|
||||
@@ -215,26 +208,26 @@ int main( int /*argc*/, char ** /*argv*/ )
|
||||
VK_QUEUE_FAMILY_IGNORED,
|
||||
blitDestinationImage,
|
||||
vk::ImageSubresourceRange( vk::ImageAspectFlagBits::eColor, 0, 1, 0, 1 ) );
|
||||
commandBuffer->pipelineBarrier( vk::PipelineStageFlagBits::eTransfer,
|
||||
vk::PipelineStageFlagBits::eTopOfPipe,
|
||||
{},
|
||||
nullptr,
|
||||
nullptr,
|
||||
prePresentBarrier );
|
||||
commandBuffer->end();
|
||||
commandBuffer.pipelineBarrier( vk::PipelineStageFlagBits::eTransfer,
|
||||
vk::PipelineStageFlagBits::eTopOfPipe,
|
||||
{},
|
||||
nullptr,
|
||||
nullptr,
|
||||
prePresentBarrier );
|
||||
commandBuffer.end();
|
||||
|
||||
std::unique_ptr<vk::raii::Fence> drawFence = vk::raii::su::make_unique<vk::raii::Fence>( *device, vk::FenceCreateInfo() );
|
||||
submitInfo = vk::SubmitInfo( {}, {}, **commandBuffer );
|
||||
graphicsQueue->submit( submitInfo, **drawFence );
|
||||
graphicsQueue->waitIdle();
|
||||
vk::raii::Fence drawFence( device, vk::FenceCreateInfo() );
|
||||
submitInfo = vk::SubmitInfo( {}, {}, *commandBuffer );
|
||||
graphicsQueue.submit( submitInfo, *drawFence );
|
||||
graphicsQueue.waitIdle();
|
||||
|
||||
/* 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 */
|
||||
vk::PresentInfoKHR presentInfoKHR( nullptr, **swapChainData.swapChain, imageIndex );
|
||||
result = presentQueue->presentKHR( presentInfoKHR );
|
||||
vk::PresentInfoKHR presentInfoKHR( nullptr, **swapChainData.pSwapChain, imageIndex );
|
||||
result = presentQueue.presentKHR( presentInfoKHR );
|
||||
switch ( result )
|
||||
{
|
||||
case vk::Result::eSuccess: break;
|
||||
|
||||
Reference in New Issue
Block a user