Introduce raii-compliant handle wrapper classes.
This commit is contained in:
@@ -27,17 +27,17 @@ int main( int /*argc*/, char ** /*argv*/ )
|
||||
{
|
||||
try
|
||||
{
|
||||
vk::UniqueInstance instance = vk::su::createInstance( AppName, EngineName, {}, vk::su::getInstanceExtensions() );
|
||||
vk::Instance instance = vk::su::createInstance( AppName, EngineName, {}, vk::su::getInstanceExtensions() );
|
||||
#if !defined( NDEBUG )
|
||||
vk::UniqueDebugUtilsMessengerEXT debugUtilsMessenger = vk::su::createDebugUtilsMessenger( instance );
|
||||
vk::DebugUtilsMessengerEXT debugUtilsMessenger =
|
||||
instance.createDebugUtilsMessengerEXT( vk::su::makeDebugUtilsMessengerCreateInfoEXT() );
|
||||
#endif
|
||||
|
||||
vk::PhysicalDevice physicalDevice = instance->enumeratePhysicalDevices().front();
|
||||
vk::PhysicalDevice physicalDevice = instance.enumeratePhysicalDevices().front();
|
||||
|
||||
vk::su::SurfaceData surfaceData( instance, AppName, vk::Extent2D( 640, 640 ) );
|
||||
|
||||
vk::SurfaceCapabilitiesKHR surfaceCapabilities =
|
||||
physicalDevice.getSurfaceCapabilitiesKHR( surfaceData.surface.get() );
|
||||
vk::SurfaceCapabilitiesKHR surfaceCapabilities = physicalDevice.getSurfaceCapabilitiesKHR( surfaceData.surface );
|
||||
if ( !( surfaceCapabilities.supportedUsageFlags & vk::ImageUsageFlagBits::eTransferDst ) )
|
||||
{
|
||||
std::cout << "Surface cannot be destination of blit - abort \n";
|
||||
@@ -45,26 +45,25 @@ int main( int /*argc*/, char ** /*argv*/ )
|
||||
}
|
||||
|
||||
std::pair<uint32_t, uint32_t> graphicsAndPresentQueueFamilyIndex =
|
||||
vk::su::findGraphicsAndPresentQueueFamilyIndex( physicalDevice, *surfaceData.surface );
|
||||
vk::UniqueDevice device =
|
||||
vk::su::findGraphicsAndPresentQueueFamilyIndex( physicalDevice, surfaceData.surface );
|
||||
vk::Device device =
|
||||
vk::su::createDevice( physicalDevice, graphicsAndPresentQueueFamilyIndex.first, vk::su::getDeviceExtensions() );
|
||||
|
||||
vk::UniqueCommandPool commandPool = vk::su::createCommandPool( device, graphicsAndPresentQueueFamilyIndex.first );
|
||||
vk::UniqueCommandBuffer commandBuffer = std::move( device
|
||||
->allocateCommandBuffersUnique( vk::CommandBufferAllocateInfo(
|
||||
commandPool.get(), vk::CommandBufferLevel::ePrimary, 1 ) )
|
||||
.front() );
|
||||
vk::CommandPool commandPool = vk::su::createCommandPool( device, graphicsAndPresentQueueFamilyIndex.first );
|
||||
vk::CommandBuffer commandBuffer =
|
||||
device.allocateCommandBuffers( vk::CommandBufferAllocateInfo( commandPool, vk::CommandBufferLevel::ePrimary, 1 ) )
|
||||
.front();
|
||||
|
||||
vk::Queue graphicsQueue = device->getQueue( graphicsAndPresentQueueFamilyIndex.first, 0 );
|
||||
vk::Queue presentQueue = device->getQueue( graphicsAndPresentQueueFamilyIndex.second, 0 );
|
||||
vk::Queue graphicsQueue = device.getQueue( graphicsAndPresentQueueFamilyIndex.first, 0 );
|
||||
vk::Queue presentQueue = device.getQueue( graphicsAndPresentQueueFamilyIndex.second, 0 );
|
||||
|
||||
vk::su::SwapChainData swapChainData( physicalDevice,
|
||||
device,
|
||||
*surfaceData.surface,
|
||||
surfaceData.surface,
|
||||
surfaceData.extent,
|
||||
vk::ImageUsageFlagBits::eColorAttachment |
|
||||
vk::ImageUsageFlagBits::eTransferDst,
|
||||
vk::UniqueSwapchainKHR(),
|
||||
{},
|
||||
graphicsAndPresentQueueFamilyIndex.first,
|
||||
graphicsAndPresentQueueFamilyIndex.second );
|
||||
|
||||
@@ -74,18 +73,18 @@ int main( int /*argc*/, char ** /*argv*/ )
|
||||
assert( ( formatProperties.linearTilingFeatures & vk::FormatFeatureFlagBits::eBlitSrc ) &&
|
||||
"Format cannot be used as transfer source" );
|
||||
|
||||
vk::UniqueSemaphore imageAcquiredSemaphore = device->createSemaphoreUnique( vk::SemaphoreCreateInfo() );
|
||||
vk::Semaphore imageAcquiredSemaphore = device.createSemaphore( vk::SemaphoreCreateInfo() );
|
||||
|
||||
// Get the index of the next available swapchain image:
|
||||
vk::ResultValue<uint32_t> nextImage = device->acquireNextImageKHR(
|
||||
swapChainData.swapChain.get(), vk::su::FenceTimeout, imageAcquiredSemaphore.get(), nullptr );
|
||||
vk::ResultValue<uint32_t> nextImage =
|
||||
device.acquireNextImageKHR( swapChainData.swapChain, vk::su::FenceTimeout, imageAcquiredSemaphore, nullptr );
|
||||
assert( nextImage.result == vk::Result::eSuccess );
|
||||
assert( nextImage.value < swapChainData.images.size() );
|
||||
uint32_t currentBuffer = nextImage.value;
|
||||
uint32_t imageIndex = nextImage.value;
|
||||
|
||||
commandBuffer->begin( vk::CommandBufferBeginInfo() );
|
||||
commandBuffer.begin( vk::CommandBufferBeginInfo() );
|
||||
vk::su::setImageLayout( commandBuffer,
|
||||
swapChainData.images[currentBuffer],
|
||||
swapChainData.images[imageIndex],
|
||||
swapChainData.colorFormat,
|
||||
vk::ImageLayout::eUndefined,
|
||||
vk::ImageLayout::eTransferDstOptimal );
|
||||
@@ -100,37 +99,36 @@ int main( int /*argc*/, char ** /*argv*/ )
|
||||
vk::SampleCountFlagBits::e1,
|
||||
vk::ImageTiling::eLinear,
|
||||
vk::ImageUsageFlagBits::eTransferSrc );
|
||||
vk::UniqueImage blitSourceImage = device->createImageUnique( imageCreateInfo );
|
||||
vk::Image blitSourceImage = device.createImage( imageCreateInfo );
|
||||
|
||||
vk::PhysicalDeviceMemoryProperties memoryProperties = physicalDevice.getMemoryProperties();
|
||||
vk::MemoryRequirements memoryRequirements = device->getImageMemoryRequirements( blitSourceImage.get() );
|
||||
vk::MemoryRequirements memoryRequirements = device.getImageMemoryRequirements( blitSourceImage );
|
||||
uint32_t memoryTypeIndex = vk::su::findMemoryType(
|
||||
memoryProperties, memoryRequirements.memoryTypeBits, vk::MemoryPropertyFlagBits::eHostVisible );
|
||||
|
||||
vk::UniqueDeviceMemory deviceMemory =
|
||||
device->allocateMemoryUnique( vk::MemoryAllocateInfo( memoryRequirements.size, memoryTypeIndex ) );
|
||||
device->bindImageMemory( blitSourceImage.get(), deviceMemory.get(), 0 );
|
||||
vk::DeviceMemory deviceMemory =
|
||||
device.allocateMemory( vk::MemoryAllocateInfo( memoryRequirements.size, memoryTypeIndex ) );
|
||||
device.bindImageMemory( blitSourceImage, deviceMemory, 0 );
|
||||
|
||||
vk::su::setImageLayout( commandBuffer,
|
||||
blitSourceImage.get(),
|
||||
blitSourceImage,
|
||||
swapChainData.colorFormat,
|
||||
vk::ImageLayout::eUndefined,
|
||||
vk::ImageLayout::eGeneral );
|
||||
|
||||
commandBuffer->end();
|
||||
commandBuffer.end();
|
||||
|
||||
/* Queue the command buffer for execution */
|
||||
vk::UniqueFence commandFence = device->createFenceUnique( {} );
|
||||
vk::Fence commandFence = device.createFence( {} );
|
||||
vk::PipelineStageFlags pipeStageFlags( vk::PipelineStageFlagBits::eColorAttachmentOutput );
|
||||
graphicsQueue.submit( vk::SubmitInfo( *imageAcquiredSemaphore, pipeStageFlags, *commandBuffer ),
|
||||
commandFence.get() );
|
||||
graphicsQueue.submit( vk::SubmitInfo( imageAcquiredSemaphore, pipeStageFlags, commandBuffer ), commandFence );
|
||||
|
||||
/* Make sure command buffer is finished before mapping */
|
||||
while ( device->waitForFences( commandFence.get(), 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 *>( device->mapMemory( deviceMemory.get(), 0, memoryRequirements.size ) );
|
||||
static_cast<unsigned char *>( device.mapMemory( deviceMemory, 0, memoryRequirements.size ) );
|
||||
|
||||
// Checkerboard of 8x8 pixel squares
|
||||
for ( uint32_t row = 0; row < surfaceData.extent.height; row++ )
|
||||
@@ -147,20 +145,20 @@ 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
|
||||
device->flushMappedMemoryRanges( vk::MappedMemoryRange( deviceMemory.get(), 0, memoryRequirements.size ) );
|
||||
device->unmapMemory( deviceMemory.get() );
|
||||
device.flushMappedMemoryRanges( vk::MappedMemoryRange( deviceMemory, 0, memoryRequirements.size ) );
|
||||
device.unmapMemory( deviceMemory );
|
||||
|
||||
commandBuffer->reset( {} );
|
||||
commandBuffer->begin( vk::CommandBufferBeginInfo() );
|
||||
commandBuffer.reset( {} );
|
||||
commandBuffer.begin( vk::CommandBufferBeginInfo() );
|
||||
|
||||
// Intend to blit from this image, set the layout accordingly
|
||||
vk::su::setImageLayout( commandBuffer,
|
||||
blitSourceImage.get(),
|
||||
blitSourceImage,
|
||||
swapChainData.colorFormat,
|
||||
vk::ImageLayout::eGeneral,
|
||||
vk::ImageLayout::eTransferSrcOptimal );
|
||||
|
||||
vk::Image blitDestinationImage = swapChainData.images[currentBuffer];
|
||||
vk::Image blitDestinationImage = swapChainData.images[imageIndex];
|
||||
|
||||
// Do a 32x32 blit to all of the dst image - should get big squares
|
||||
vk::ImageSubresourceLayers imageSubresourceLayers( vk::ImageAspectFlagBits::eColor, 0, 0, 1 );
|
||||
@@ -169,12 +167,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.get(),
|
||||
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,
|
||||
@@ -185,12 +183,12 @@ 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::eTransfer,
|
||||
vk::DependencyFlags(),
|
||||
nullptr,
|
||||
nullptr,
|
||||
memoryBarrier );
|
||||
commandBuffer.pipelineBarrier( vk::PipelineStageFlagBits::eTransfer,
|
||||
vk::PipelineStageFlagBits::eTransfer,
|
||||
vk::DependencyFlags(),
|
||||
nullptr,
|
||||
nullptr,
|
||||
memoryBarrier );
|
||||
|
||||
// Do a image copy to part of the dst image - checks should stay small
|
||||
vk::ImageCopy imageCopy( imageSubresourceLayers,
|
||||
@@ -198,11 +196,11 @@ int main( int /*argc*/, char ** /*argv*/ )
|
||||
imageSubresourceLayers,
|
||||
vk::Offset3D( 256, 256, 0 ),
|
||||
vk::Extent3D( 128, 128, 1 ) );
|
||||
commandBuffer->copyImage( blitSourceImage.get(),
|
||||
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,
|
||||
@@ -211,27 +209,26 @@ int main( int /*argc*/, char ** /*argv*/ )
|
||||
vk::ImageLayout::ePresentSrcKHR,
|
||||
VK_QUEUE_FAMILY_IGNORED,
|
||||
VK_QUEUE_FAMILY_IGNORED,
|
||||
swapChainData.images[currentBuffer],
|
||||
swapChainData.images[imageIndex],
|
||||
vk::ImageSubresourceRange( vk::ImageAspectFlagBits::eColor, 0, 1, 0, 1 ) );
|
||||
commandBuffer->pipelineBarrier( vk::PipelineStageFlagBits::eTransfer,
|
||||
vk::PipelineStageFlagBits::eTopOfPipe,
|
||||
vk::DependencyFlags(),
|
||||
nullptr,
|
||||
nullptr,
|
||||
prePresentBarrier );
|
||||
commandBuffer->end();
|
||||
commandBuffer.pipelineBarrier( vk::PipelineStageFlagBits::eTransfer,
|
||||
vk::PipelineStageFlagBits::eTopOfPipe,
|
||||
vk::DependencyFlags(),
|
||||
nullptr,
|
||||
nullptr,
|
||||
prePresentBarrier );
|
||||
commandBuffer.end();
|
||||
|
||||
vk::UniqueFence drawFence = device->createFenceUnique( {} );
|
||||
graphicsQueue.submit( vk::SubmitInfo( {}, {}, *commandBuffer ), drawFence.get() );
|
||||
vk::Fence drawFence = device.createFence( {} );
|
||||
graphicsQueue.submit( vk::SubmitInfo( {}, {}, commandBuffer ), drawFence );
|
||||
graphicsQueue.waitIdle();
|
||||
|
||||
/* Make sure command buffer is finished before presenting */
|
||||
while ( device->waitForFences( drawFence.get(), 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::Result result =
|
||||
presentQueue.presentKHR( vk::PresentInfoKHR( {}, *swapChainData.swapChain, currentBuffer, {} ) );
|
||||
vk::Result result = presentQueue.presentKHR( vk::PresentInfoKHR( {}, swapChainData.swapChain, imageIndex, {} ) );
|
||||
switch ( result )
|
||||
{
|
||||
case vk::Result::eSuccess: break;
|
||||
@@ -243,6 +240,19 @@ int main( int /*argc*/, char ** /*argv*/ )
|
||||
std::this_thread::sleep_for( std::chrono::milliseconds( 1000 ) );
|
||||
|
||||
/* VULKAN_KEY_END */
|
||||
|
||||
device.destroyFence( drawFence );
|
||||
device.destroyFence( commandFence );
|
||||
device.freeMemory( deviceMemory );
|
||||
device.destroyImage( blitSourceImage );
|
||||
device.destroySemaphore( imageAcquiredSemaphore );
|
||||
swapChainData.clear( device );
|
||||
device.freeCommandBuffers( commandPool, commandBuffer );
|
||||
device.destroyCommandPool( commandPool );
|
||||
device.destroy();
|
||||
instance.destroySurfaceKHR( surfaceData.surface );
|
||||
instance.destroyDebugUtilsMessengerEXT( debugUtilsMessenger );
|
||||
instance.destroy();
|
||||
}
|
||||
catch ( vk::SystemError & err )
|
||||
{
|
||||
|
||||
Reference in New Issue
Block a user