Introduce raii-compliant handle wrapper classes.
This commit is contained in:
@@ -70,12 +70,13 @@ 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::FormatProperties formatProperties = physicalDevice.getFormatProperties( vk::Format::eR8G8B8A8Unorm );
|
||||
if ( !( formatProperties.optimalTilingFeatures & vk::FormatFeatureFlagBits::eColorAttachment ) )
|
||||
@@ -87,26 +88,25 @@ int main( int /*argc*/, char ** /*argv*/ )
|
||||
vk::su::SurfaceData surfaceData( instance, AppName, vk::Extent2D( 500, 500 ) );
|
||||
|
||||
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::eTransferSrc,
|
||||
vk::UniqueSwapchainKHR(),
|
||||
{},
|
||||
graphicsAndPresentQueueFamilyIndex.first,
|
||||
graphicsAndPresentQueueFamilyIndex.second );
|
||||
|
||||
@@ -118,34 +118,35 @@ int main( int /*argc*/, char ** /*argv*/ )
|
||||
|
||||
// Create the image that will be used as the input attachment
|
||||
// The image for the color attachment is the presentable image already created as part of the SwapChainData
|
||||
vk::UniqueImage inputImage = device->createImageUnique(
|
||||
vk::ImageCreateInfo( vk::ImageCreateFlags(),
|
||||
vk::ImageType::e2D,
|
||||
swapChainData.colorFormat,
|
||||
vk::Extent3D( surfaceData.extent, 1 ),
|
||||
1,
|
||||
1,
|
||||
vk::SampleCountFlagBits::e1,
|
||||
vk::ImageTiling::eOptimal,
|
||||
vk::ImageUsageFlagBits::eInputAttachment | vk::ImageUsageFlagBits::eTransferDst ) );
|
||||
vk::ImageCreateInfo imageCreateInfo( vk::ImageCreateFlags(),
|
||||
vk::ImageType::e2D,
|
||||
swapChainData.colorFormat,
|
||||
vk::Extent3D( surfaceData.extent, 1 ),
|
||||
1,
|
||||
1,
|
||||
vk::SampleCountFlagBits::e1,
|
||||
vk::ImageTiling::eOptimal,
|
||||
vk::ImageUsageFlagBits::eInputAttachment |
|
||||
vk::ImageUsageFlagBits::eTransferDst );
|
||||
vk::Image inputImage = device.createImage( imageCreateInfo );
|
||||
|
||||
vk::MemoryRequirements memoryRequirements = device->getImageMemoryRequirements( inputImage.get() );
|
||||
vk::MemoryRequirements memoryRequirements = device.getImageMemoryRequirements( inputImage );
|
||||
uint32_t memoryTypeIndex = vk::su::findMemoryType(
|
||||
physicalDevice.getMemoryProperties(), memoryRequirements.memoryTypeBits, vk::MemoryPropertyFlags() );
|
||||
vk::UniqueDeviceMemory inputMemory =
|
||||
device->allocateMemoryUnique( vk::MemoryAllocateInfo( memoryRequirements.size, memoryTypeIndex ) );
|
||||
device->bindImageMemory( inputImage.get(), inputMemory.get(), 0 );
|
||||
vk::DeviceMemory inputMemory =
|
||||
device.allocateMemory( vk::MemoryAllocateInfo( memoryRequirements.size, memoryTypeIndex ) );
|
||||
device.bindImageMemory( inputImage, inputMemory, 0 );
|
||||
|
||||
// Set the image layout to TRANSFER_DST_OPTIMAL to be ready for clear
|
||||
commandBuffer->begin( vk::CommandBufferBeginInfo() );
|
||||
commandBuffer.begin( vk::CommandBufferBeginInfo() );
|
||||
vk::su::setImageLayout( commandBuffer,
|
||||
inputImage.get(),
|
||||
inputImage,
|
||||
swapChainData.colorFormat,
|
||||
vk::ImageLayout::eUndefined,
|
||||
vk::ImageLayout::eTransferDstOptimal );
|
||||
|
||||
commandBuffer->clearColorImage(
|
||||
inputImage.get(),
|
||||
commandBuffer.clearColorImage(
|
||||
inputImage,
|
||||
vk::ImageLayout::eTransferDstOptimal,
|
||||
vk::ClearColorValue( std::array<float, 4>( { { 1.0f, 1.0f, 0.0f, 0.0f } } ) ),
|
||||
vk::ImageSubresourceRange(
|
||||
@@ -153,29 +154,25 @@ int main( int /*argc*/, char ** /*argv*/ )
|
||||
|
||||
// Set the image layout to SHADER_READONLY_OPTIMAL for use by the shaders
|
||||
vk::su::setImageLayout( commandBuffer,
|
||||
inputImage.get(),
|
||||
inputImage,
|
||||
swapChainData.colorFormat,
|
||||
vk::ImageLayout::eTransferDstOptimal,
|
||||
vk::ImageLayout::eShaderReadOnlyOptimal );
|
||||
|
||||
vk::ComponentMapping componentMapping(
|
||||
vk::ComponentSwizzle::eR, vk::ComponentSwizzle::eG, vk::ComponentSwizzle::eB, vk::ComponentSwizzle::eA );
|
||||
vk::ImageViewCreateInfo imageViewCreateInfo(
|
||||
vk::ImageViewCreateFlags(),
|
||||
inputImage.get(),
|
||||
vk::ImageViewType::e2D,
|
||||
swapChainData.colorFormat,
|
||||
componentMapping,
|
||||
vk::ImageSubresourceRange( vk::ImageAspectFlagBits::eColor, 0, 1, 0, 1 ) );
|
||||
vk::UniqueImageView inputAttachmentView = device->createImageViewUnique( imageViewCreateInfo );
|
||||
vk::ImageSubresourceRange imageSubresourceRange( vk::ImageAspectFlagBits::eColor, 0, 1, 0, 1 );
|
||||
vk::ImageViewCreateInfo imageViewCreateInfo(
|
||||
{}, inputImage, vk::ImageViewType::e2D, swapChainData.colorFormat, componentMapping, imageSubresourceRange );
|
||||
vk::ImageView inputAttachmentView = device.createImageView( imageViewCreateInfo );
|
||||
|
||||
vk::DescriptorSetLayoutBinding layoutBinding(
|
||||
0, vk::DescriptorType::eInputAttachment, 1, vk::ShaderStageFlagBits::eFragment );
|
||||
vk::UniqueDescriptorSetLayout descriptorSetLayout = device->createDescriptorSetLayoutUnique(
|
||||
vk::DescriptorSetLayout descriptorSetLayout = device.createDescriptorSetLayout(
|
||||
vk::DescriptorSetLayoutCreateInfo( vk::DescriptorSetLayoutCreateFlags(), layoutBinding ) );
|
||||
|
||||
vk::UniquePipelineLayout pipelineLayout = device->createPipelineLayoutUnique(
|
||||
vk::PipelineLayoutCreateInfo( vk::PipelineLayoutCreateFlags(), *descriptorSetLayout ) );
|
||||
vk::PipelineLayout pipelineLayout = device.createPipelineLayout(
|
||||
vk::PipelineLayoutCreateInfo( vk::PipelineLayoutCreateFlags(), descriptorSetLayout ) );
|
||||
|
||||
std::array<vk::AttachmentDescription, 2> attachments = {
|
||||
// First attachment is the color attachment - clear at the beginning of the renderpass and transition layout to
|
||||
@@ -206,82 +203,77 @@ int main( int /*argc*/, char ** /*argv*/ )
|
||||
vk::AttachmentReference inputReference( 1, vk::ImageLayout::eShaderReadOnlyOptimal );
|
||||
vk::SubpassDescription subPass(
|
||||
vk::SubpassDescriptionFlags(), vk::PipelineBindPoint::eGraphics, inputReference, colorReference );
|
||||
vk::UniqueRenderPass renderPass =
|
||||
device->createRenderPassUnique( vk::RenderPassCreateInfo( vk::RenderPassCreateFlags(), attachments, subPass ) );
|
||||
vk::RenderPass renderPass =
|
||||
device.createRenderPass( vk::RenderPassCreateInfo( vk::RenderPassCreateFlags(), attachments, subPass ) );
|
||||
|
||||
glslang::InitializeProcess();
|
||||
vk::UniqueShaderModule vertexShaderModule =
|
||||
vk::ShaderModule vertexShaderModule =
|
||||
vk::su::createShaderModule( device, vk::ShaderStageFlagBits::eVertex, vertexShaderText );
|
||||
vk::UniqueShaderModule fragmentShaderModule =
|
||||
vk::ShaderModule fragmentShaderModule =
|
||||
vk::su::createShaderModule( device, vk::ShaderStageFlagBits::eFragment, fragmentShaderText );
|
||||
glslang::FinalizeProcess();
|
||||
|
||||
std::vector<vk::UniqueFramebuffer> framebuffers = vk::su::createFramebuffers(
|
||||
std::vector<vk::Framebuffer> framebuffers = vk::su::createFramebuffers(
|
||||
device, renderPass, swapChainData.imageViews, inputAttachmentView, surfaceData.extent );
|
||||
|
||||
vk::DescriptorPoolSize poolSize( vk::DescriptorType::eInputAttachment, 1 );
|
||||
vk::UniqueDescriptorPool descriptorPool = device->createDescriptorPoolUnique(
|
||||
vk::DescriptorPoolSize poolSize( vk::DescriptorType::eInputAttachment, 1 );
|
||||
vk::DescriptorPool descriptorPool = device.createDescriptorPool(
|
||||
vk::DescriptorPoolCreateInfo( vk::DescriptorPoolCreateFlagBits::eFreeDescriptorSet, 1, poolSize ) );
|
||||
|
||||
vk::UniqueDescriptorSet descriptorSet = std::move(
|
||||
device->allocateDescriptorSetsUnique( vk::DescriptorSetAllocateInfo( *descriptorPool, *descriptorSetLayout ) )
|
||||
.front() );
|
||||
vk::DescriptorSetAllocateInfo descriptorSetAllocateInfo( descriptorPool, descriptorSetLayout );
|
||||
vk::DescriptorSet descriptorSet = device.allocateDescriptorSets( descriptorSetAllocateInfo ).front();
|
||||
|
||||
vk::DescriptorImageInfo inputImageInfo(
|
||||
nullptr, inputAttachmentView.get(), vk::ImageLayout::eShaderReadOnlyOptimal );
|
||||
vk::WriteDescriptorSet writeDescriptorSet(
|
||||
descriptorSet.get(), 0, 0, vk::DescriptorType::eInputAttachment, inputImageInfo );
|
||||
device->updateDescriptorSets( vk::ArrayProxy<const vk::WriteDescriptorSet>( 1, &writeDescriptorSet ), nullptr );
|
||||
vk::DescriptorImageInfo inputImageInfo( nullptr, inputAttachmentView, vk::ImageLayout::eShaderReadOnlyOptimal );
|
||||
vk::WriteDescriptorSet writeDescriptorSet(
|
||||
descriptorSet, 0, 0, vk::DescriptorType::eInputAttachment, inputImageInfo );
|
||||
device.updateDescriptorSets( writeDescriptorSet, nullptr );
|
||||
|
||||
vk::UniquePipelineCache pipelineCache = device->createPipelineCacheUnique( vk::PipelineCacheCreateInfo() );
|
||||
vk::UniquePipeline graphicsPipeline =
|
||||
vk::su::createGraphicsPipeline( device,
|
||||
pipelineCache,
|
||||
std::make_pair( *vertexShaderModule, nullptr ),
|
||||
std::make_pair( *fragmentShaderModule, nullptr ),
|
||||
0,
|
||||
{},
|
||||
vk::FrontFace::eClockwise,
|
||||
false,
|
||||
pipelineLayout,
|
||||
renderPass );
|
||||
vk::PipelineCache pipelineCache = device.createPipelineCache( vk::PipelineCacheCreateInfo() );
|
||||
vk::Pipeline graphicsPipeline = vk::su::createGraphicsPipeline( device,
|
||||
pipelineCache,
|
||||
std::make_pair( vertexShaderModule, nullptr ),
|
||||
std::make_pair( fragmentShaderModule, nullptr ),
|
||||
0,
|
||||
{},
|
||||
vk::FrontFace::eClockwise,
|
||||
false,
|
||||
pipelineLayout,
|
||||
renderPass );
|
||||
|
||||
vk::UniqueSemaphore imageAcquiredSemaphore = device->createSemaphoreUnique( vk::SemaphoreCreateInfo() );
|
||||
vk::Semaphore imageAcquiredSemaphore = device.createSemaphore( vk::SemaphoreCreateInfo() );
|
||||
|
||||
vk::ResultValue<uint32_t> nexImage = device->acquireNextImage2KHR(
|
||||
vk::AcquireNextImageInfoKHR( swapChainData.swapChain.get(), UINT64_MAX, imageAcquiredSemaphore.get(), {}, 1 ) );
|
||||
vk::ResultValue<uint32_t> nexImage = device.acquireNextImage2KHR(
|
||||
vk::AcquireNextImageInfoKHR( swapChainData.swapChain, UINT64_MAX, imageAcquiredSemaphore, {}, 1 ) );
|
||||
assert( nexImage.result == vk::Result::eSuccess );
|
||||
uint32_t currentBuffer = nexImage.value;
|
||||
|
||||
vk::ClearValue clearValue;
|
||||
clearValue.color = vk::ClearColorValue( std::array<float, 4>( { { 0.2f, 0.2f, 0.2f, 0.2f } } ) );
|
||||
commandBuffer->beginRenderPass( vk::RenderPassBeginInfo( renderPass.get(),
|
||||
framebuffers[currentBuffer].get(),
|
||||
vk::Rect2D( vk::Offset2D( 0, 0 ), surfaceData.extent ),
|
||||
clearValue ),
|
||||
vk::SubpassContents::eInline );
|
||||
commandBuffer->bindPipeline( vk::PipelineBindPoint::eGraphics, graphicsPipeline.get() );
|
||||
commandBuffer->bindDescriptorSets(
|
||||
vk::PipelineBindPoint::eGraphics, pipelineLayout.get(), 0, descriptorSet.get(), nullptr );
|
||||
commandBuffer.beginRenderPass(
|
||||
vk::RenderPassBeginInfo(
|
||||
renderPass, framebuffers[currentBuffer], vk::Rect2D( vk::Offset2D( 0, 0 ), surfaceData.extent ), clearValue ),
|
||||
vk::SubpassContents::eInline );
|
||||
commandBuffer.bindPipeline( vk::PipelineBindPoint::eGraphics, graphicsPipeline );
|
||||
commandBuffer.bindDescriptorSets( vk::PipelineBindPoint::eGraphics, pipelineLayout, 0, descriptorSet, nullptr );
|
||||
|
||||
commandBuffer->setViewport( 0,
|
||||
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 ) );
|
||||
commandBuffer.setViewport( 0,
|
||||
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 ) );
|
||||
|
||||
commandBuffer->draw( 3, 1, 0, 0 );
|
||||
commandBuffer->endRenderPass();
|
||||
commandBuffer->end();
|
||||
commandBuffer.draw( 3, 1, 0, 0 );
|
||||
commandBuffer.endRenderPass();
|
||||
commandBuffer.end();
|
||||
|
||||
/* VULKAN_KEY_END */
|
||||
|
||||
vk::su::submitAndWait( device, graphicsQueue, commandBuffer );
|
||||
|
||||
vk::Result result = presentQueue.presentKHR( vk::PresentInfoKHR( {}, *swapChainData.swapChain, currentBuffer ) );
|
||||
vk::Result result = presentQueue.presentKHR( vk::PresentInfoKHR( {}, swapChainData.swapChain, currentBuffer ) );
|
||||
switch ( result )
|
||||
{
|
||||
case vk::Result::eSuccess: break;
|
||||
@@ -291,6 +283,31 @@ int main( int /*argc*/, char ** /*argv*/ )
|
||||
default: assert( false ); // an unexpected result is returned !
|
||||
}
|
||||
std::this_thread::sleep_for( std::chrono::milliseconds( 1000 ) );
|
||||
|
||||
device.destroySemaphore( imageAcquiredSemaphore );
|
||||
device.destroyPipeline( graphicsPipeline );
|
||||
device.destroyPipelineCache( pipelineCache );
|
||||
device.freeDescriptorSets( descriptorPool, descriptorSet );
|
||||
device.destroyDescriptorPool( descriptorPool );
|
||||
for ( auto framebuffer : framebuffers )
|
||||
{
|
||||
device.destroyFramebuffer( framebuffer );
|
||||
}
|
||||
device.destroyShaderModule( fragmentShaderModule );
|
||||
device.destroyShaderModule( vertexShaderModule );
|
||||
device.destroyRenderPass( renderPass );
|
||||
device.destroyPipelineLayout( pipelineLayout );
|
||||
device.destroyDescriptorSetLayout( descriptorSetLayout );
|
||||
device.destroyImageView( inputAttachmentView );
|
||||
device.freeMemory( inputMemory );
|
||||
device.destroyImage( inputImage );
|
||||
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