2021-02-17 09:49:59 +00:00
|
|
|
// Copyright(c) 2019, NVIDIA CORPORATION. All rights reserved.
|
|
|
|
//
|
|
|
|
// Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
// you may not use this file except in compliance with the License.
|
|
|
|
// You may obtain a copy of the License at
|
|
|
|
//
|
|
|
|
// http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
//
|
|
|
|
// Unless required by applicable law or agreed to in writing, software
|
|
|
|
// distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
// See the License for the specific language governing permissions and
|
|
|
|
// limitations under the License.
|
|
|
|
//
|
|
|
|
// VulkanHpp Samples : CopyBlitImage
|
|
|
|
// Draw a cube
|
|
|
|
|
|
|
|
#include "../utils/utils.hpp"
|
|
|
|
|
|
|
|
#include <thread>
|
|
|
|
|
|
|
|
static char const * AppName = "CopyBlitImage";
|
|
|
|
static char const * EngineName = "Vulkan.hpp";
|
|
|
|
|
|
|
|
int main( int /*argc*/, char ** /*argv*/ )
|
|
|
|
{
|
|
|
|
try
|
|
|
|
{
|
2021-06-21 09:32:43 +00:00
|
|
|
vk::raii::Context context;
|
2022-02-28 09:11:04 +00:00
|
|
|
vk::raii::Instance instance = vk::raii::su::makeInstance( context, AppName, EngineName, {}, vk::su::getInstanceExtensions() );
|
2021-02-17 09:49:59 +00:00
|
|
|
#if !defined( NDEBUG )
|
2021-06-21 09:32:43 +00:00
|
|
|
vk::raii::DebugUtilsMessengerEXT debugUtilsMessenger( instance, vk::su::makeDebugUtilsMessengerCreateInfoEXT() );
|
2021-02-17 09:49:59 +00:00
|
|
|
#endif
|
2022-03-22 11:26:28 +00:00
|
|
|
vk::raii::PhysicalDevice physicalDevice = vk::raii::PhysicalDevices( instance ).front();
|
2021-02-17 09:49:59 +00:00
|
|
|
|
2021-06-21 09:32:43 +00:00
|
|
|
vk::raii::su::SurfaceData surfaceData( instance, AppName, vk::Extent2D( 640, 640 ) );
|
2021-02-17 09:49:59 +00:00
|
|
|
|
2024-01-16 12:48:53 +00:00
|
|
|
vk::SurfaceCapabilitiesKHR surfaceCapabilities = physicalDevice.getSurfaceCapabilitiesKHR( surfaceData.surface );
|
2021-02-17 09:49:59 +00:00
|
|
|
if ( !( surfaceCapabilities.supportedUsageFlags & vk::ImageUsageFlagBits::eTransferDst ) )
|
|
|
|
{
|
|
|
|
std::cout << "Surface cannot be destination of blit - abort \n";
|
|
|
|
exit( -1 );
|
|
|
|
}
|
|
|
|
|
|
|
|
std::pair<uint32_t, uint32_t> graphicsAndPresentQueueFamilyIndex =
|
2021-10-11 15:49:13 +00:00
|
|
|
vk::raii::su::findGraphicsAndPresentQueueFamilyIndex( physicalDevice, surfaceData.surface );
|
2022-02-28 09:11:04 +00:00
|
|
|
vk::raii::Device device = vk::raii::su::makeDevice( physicalDevice, graphicsAndPresentQueueFamilyIndex.first, vk::su::getDeviceExtensions() );
|
2021-06-21 09:32:43 +00:00
|
|
|
|
2023-08-14 14:09:11 +00:00
|
|
|
vk::raii::CommandPool commandPool = vk::raii::CommandPool( device, { {}, graphicsAndPresentQueueFamilyIndex.first } );
|
2021-06-21 09:32:43 +00:00
|
|
|
vk::raii::CommandBuffer commandBuffer = vk::raii::su::makeCommandBuffer( device, commandPool );
|
|
|
|
|
|
|
|
vk::raii::Queue graphicsQueue( device, graphicsAndPresentQueueFamilyIndex.first, 0 );
|
|
|
|
vk::raii::Queue presentQueue( device, graphicsAndPresentQueueFamilyIndex.second, 0 );
|
|
|
|
|
|
|
|
vk::raii::su::SwapChainData swapChainData( physicalDevice,
|
|
|
|
device,
|
2021-10-11 15:49:13 +00:00
|
|
|
surfaceData.surface,
|
2021-02-17 09:49:59 +00:00
|
|
|
surfaceData.extent,
|
2022-02-28 09:11:04 +00:00
|
|
|
vk::ImageUsageFlagBits::eColorAttachment | vk::ImageUsageFlagBits::eTransferSrc |
|
2021-02-17 09:49:59 +00:00
|
|
|
vk::ImageUsageFlagBits::eTransferDst,
|
|
|
|
{},
|
|
|
|
graphicsAndPresentQueueFamilyIndex.first,
|
|
|
|
graphicsAndPresentQueueFamilyIndex.second );
|
|
|
|
|
|
|
|
/* VULKAN_KEY_START */
|
2021-09-06 08:32:06 +00:00
|
|
|
#if !defined( NDEBUG )
|
2021-06-21 09:32:43 +00:00
|
|
|
vk::FormatProperties formatProperties = physicalDevice.getFormatProperties( swapChainData.colorFormat );
|
2022-02-28 09:11:04 +00:00
|
|
|
assert( ( formatProperties.linearTilingFeatures & vk::FormatFeatureFlagBits::eBlitSrc ) && "Format cannot be used as transfer source" );
|
2021-09-06 08:32:06 +00:00
|
|
|
#endif
|
2021-02-17 09:49:59 +00:00
|
|
|
|
2021-06-21 09:32:43 +00:00
|
|
|
vk::raii::Semaphore imageAcquiredSemaphore( device, vk::SemaphoreCreateInfo() );
|
2021-02-17 09:49:59 +00:00
|
|
|
|
|
|
|
// Get the index of the next available swapchain image:
|
|
|
|
vk::Result result;
|
|
|
|
uint32_t imageIndex;
|
2024-01-16 12:48:53 +00:00
|
|
|
std::tie( result, imageIndex ) = swapChainData.swapChain.acquireNextImage( vk::su::FenceTimeout, imageAcquiredSemaphore );
|
2021-02-17 09:49:59 +00:00
|
|
|
assert( result == vk::Result::eSuccess );
|
|
|
|
assert( imageIndex < swapChainData.images.size() );
|
|
|
|
|
2021-06-21 09:32:43 +00:00
|
|
|
commandBuffer.begin( vk::CommandBufferBeginInfo() );
|
|
|
|
vk::raii::su::setImageLayout( commandBuffer,
|
2021-02-17 09:49:59 +00:00
|
|
|
static_cast<vk::Image>( swapChainData.images[imageIndex] ),
|
|
|
|
swapChainData.colorFormat,
|
|
|
|
vk::ImageLayout::eUndefined,
|
|
|
|
vk::ImageLayout::eTransferDstOptimal );
|
|
|
|
|
2023-08-21 09:35:17 +00:00
|
|
|
// in order to get a clean desctruction sequence, instantiate the DeviceMemory for the image first
|
|
|
|
vk::raii::DeviceMemory deviceMemory( nullptr );
|
|
|
|
|
2021-02-17 09:49:59 +00:00
|
|
|
// Create an image, map it, and write some values to the image
|
2021-06-21 09:32:43 +00:00
|
|
|
vk::ImageCreateInfo imageCreateInfo( {},
|
2021-02-17 09:49:59 +00:00
|
|
|
vk::ImageType::e2D,
|
|
|
|
swapChainData.colorFormat,
|
|
|
|
vk::Extent3D( surfaceData.extent, 1 ),
|
|
|
|
1,
|
|
|
|
1,
|
|
|
|
vk::SampleCountFlagBits::e1,
|
|
|
|
vk::ImageTiling::eLinear,
|
|
|
|
vk::ImageUsageFlagBits::eTransferSrc );
|
2021-06-21 09:32:43 +00:00
|
|
|
vk::raii::Image blitSourceImage( device, imageCreateInfo );
|
2021-02-17 09:49:59 +00:00
|
|
|
|
2021-06-21 09:32:43 +00:00
|
|
|
vk::PhysicalDeviceMemoryProperties memoryProperties = physicalDevice.getMemoryProperties();
|
|
|
|
vk::MemoryRequirements memoryRequirements = blitSourceImage.getMemoryRequirements();
|
2022-02-28 09:11:04 +00:00
|
|
|
uint32_t memoryTypeIndex = vk::su::findMemoryType( memoryProperties, memoryRequirements.memoryTypeBits, vk::MemoryPropertyFlagBits::eHostVisible );
|
2021-02-17 09:49:59 +00:00
|
|
|
|
2021-06-21 09:32:43 +00:00
|
|
|
vk::MemoryAllocateInfo memoryAllocateInfo( memoryRequirements.size, memoryTypeIndex );
|
2023-08-21 09:35:17 +00:00
|
|
|
deviceMemory = vk::raii::DeviceMemory( device, memoryAllocateInfo );
|
2024-01-16 12:48:53 +00:00
|
|
|
blitSourceImage.bindMemory( deviceMemory, 0 );
|
2021-02-17 09:49:59 +00:00
|
|
|
|
2024-01-16 12:48:53 +00:00
|
|
|
vk::raii::su::setImageLayout( commandBuffer, blitSourceImage, swapChainData.colorFormat, vk::ImageLayout::eUndefined, vk::ImageLayout::eGeneral );
|
2021-02-17 09:49:59 +00:00
|
|
|
|
2021-06-21 09:32:43 +00:00
|
|
|
commandBuffer.end();
|
2021-02-17 09:49:59 +00:00
|
|
|
|
|
|
|
/* Queue the command buffer for execution */
|
2021-06-21 09:32:43 +00:00
|
|
|
vk::raii::Fence commandFence( device, vk::FenceCreateInfo() );
|
|
|
|
vk::PipelineStageFlags waitDestinationStageMask( vk::PipelineStageFlagBits::eColorAttachmentOutput );
|
|
|
|
vk::SubmitInfo submitInfo( *imageAcquiredSemaphore, waitDestinationStageMask, *commandBuffer );
|
|
|
|
graphicsQueue.submit( submitInfo, *commandFence );
|
2021-02-17 09:49:59 +00:00
|
|
|
|
|
|
|
/* Make sure command buffer is finished before mapping */
|
2024-01-16 12:48:53 +00:00
|
|
|
while ( device.waitForFences( { commandFence }, true, vk::su::FenceTimeout ) == vk::Result::eTimeout )
|
2021-02-17 09:49:59 +00:00
|
|
|
;
|
|
|
|
|
2021-06-21 09:32:43 +00:00
|
|
|
unsigned char * pImageMemory = static_cast<unsigned char *>( deviceMemory.mapMemory( 0, memoryRequirements.size ) );
|
2021-02-17 09:49:59 +00:00
|
|
|
|
|
|
|
// Checkerboard of 8x8 pixel squares
|
|
|
|
for ( uint32_t row = 0; row < surfaceData.extent.height; row++ )
|
|
|
|
{
|
|
|
|
for ( uint32_t col = 0; col < surfaceData.extent.width; col++ )
|
|
|
|
{
|
|
|
|
unsigned char rgb = ( ( ( row & 0x8 ) == 0 ) ^ ( ( col & 0x8 ) == 0 ) ) * 255;
|
|
|
|
pImageMemory[0] = rgb;
|
|
|
|
pImageMemory[1] = rgb;
|
|
|
|
pImageMemory[2] = rgb;
|
|
|
|
pImageMemory[3] = 255;
|
|
|
|
pImageMemory += 4;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Flush the mapped memory and then unmap it. Assume it isn't coherent since we didn't really confirm
|
2024-01-16 12:48:53 +00:00
|
|
|
vk::MappedMemoryRange mappedMemoryRange( deviceMemory, 0, memoryRequirements.size );
|
2021-06-21 09:32:43 +00:00
|
|
|
device.flushMappedMemoryRanges( mappedMemoryRange );
|
|
|
|
deviceMemory.unmapMemory();
|
2021-02-17 09:49:59 +00:00
|
|
|
|
2023-08-14 14:09:11 +00:00
|
|
|
// reset the command buffer by resetting the complete command pool
|
|
|
|
commandPool.reset();
|
|
|
|
|
2021-06-21 09:32:43 +00:00
|
|
|
commandBuffer.begin( vk::CommandBufferBeginInfo() );
|
2021-02-17 09:49:59 +00:00
|
|
|
|
|
|
|
// Intend to blit from this image, set the layout accordingly
|
2024-01-16 12:48:53 +00:00
|
|
|
vk::raii::su::setImageLayout( commandBuffer, blitSourceImage, swapChainData.colorFormat, vk::ImageLayout::eGeneral, vk::ImageLayout::eTransferSrcOptimal );
|
2021-02-17 09:49:59 +00:00
|
|
|
|
|
|
|
vk::Image blitDestinationImage = static_cast<vk::Image>( 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 );
|
2022-02-28 09:11:04 +00:00
|
|
|
vk::ImageBlit imageBlit( imageSubresourceLayers,
|
2023-08-14 14:09:11 +00:00
|
|
|
{ { vk::Offset3D( 0, 0, 0 ), vk::Offset3D( 32, 32, 1 ) } },
|
2022-02-28 09:11:04 +00:00
|
|
|
imageSubresourceLayers,
|
2023-08-14 14:09:11 +00:00
|
|
|
{ { vk::Offset3D( 0, 0, 0 ), vk::Offset3D( surfaceData.extent.width, surfaceData.extent.height, 1 ) } } );
|
2022-02-28 09:11:04 +00:00
|
|
|
commandBuffer.blitImage(
|
2024-01-16 12:48:53 +00:00
|
|
|
blitSourceImage, vk::ImageLayout::eTransferSrcOptimal, blitDestinationImage, vk::ImageLayout::eTransferDstOptimal, imageBlit, vk::Filter::eLinear );
|
2021-02-17 09:49:59 +00:00
|
|
|
|
|
|
|
// Use a barrier to make sure the blit is finished before the copy starts
|
2023-08-17 12:28:39 +00:00
|
|
|
// Note: for a layout of vk::ImageLayout::eTransferDstOptimal, the access mask is supposed to be vk::AccessFlagBits::eTransferWrite
|
2021-02-17 09:49:59 +00:00
|
|
|
vk::ImageMemoryBarrier memoryBarrier( vk::AccessFlagBits::eTransferWrite,
|
2023-08-17 12:28:39 +00:00
|
|
|
vk::AccessFlagBits::eTransferWrite,
|
2021-02-17 09:49:59 +00:00
|
|
|
vk::ImageLayout::eTransferDstOptimal,
|
|
|
|
vk::ImageLayout::eTransferDstOptimal,
|
|
|
|
VK_QUEUE_FAMILY_IGNORED,
|
|
|
|
VK_QUEUE_FAMILY_IGNORED,
|
|
|
|
blitDestinationImage,
|
|
|
|
vk::ImageSubresourceRange( vk::ImageAspectFlagBits::eColor, 0, 1, 0, 1 ) );
|
2022-02-28 09:11:04 +00:00
|
|
|
commandBuffer.pipelineBarrier( vk::PipelineStageFlagBits::eTransfer, vk::PipelineStageFlagBits::eTransfer, {}, nullptr, nullptr, memoryBarrier );
|
2021-02-17 09:49:59 +00:00
|
|
|
|
|
|
|
// Do a image copy to part of the dst image - checks should stay small
|
2022-02-28 09:11:04 +00:00
|
|
|
vk::ImageCopy imageCopy( imageSubresourceLayers, vk::Offset3D(), imageSubresourceLayers, vk::Offset3D( 256, 256, 0 ), vk::Extent3D( 128, 128, 1 ) );
|
2024-01-16 12:48:53 +00:00
|
|
|
commandBuffer.copyImage( blitSourceImage, vk::ImageLayout::eTransferSrcOptimal, blitDestinationImage, vk::ImageLayout::eTransferDstOptimal, imageCopy );
|
2022-02-28 09:11:04 +00:00
|
|
|
|
2023-08-17 12:28:39 +00:00
|
|
|
// Note: for a layout of vk::ImageLayout::ePresentSrcKHR, the access mask is supposed to be empty
|
2022-02-28 09:11:04 +00:00
|
|
|
vk::ImageMemoryBarrier prePresentBarrier( vk::AccessFlagBits::eTransferWrite,
|
2023-08-17 12:28:39 +00:00
|
|
|
{},
|
2022-02-28 09:11:04 +00:00
|
|
|
vk::ImageLayout::eTransferDstOptimal,
|
|
|
|
vk::ImageLayout::ePresentSrcKHR,
|
|
|
|
VK_QUEUE_FAMILY_IGNORED,
|
|
|
|
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 );
|
2021-06-21 09:32:43 +00:00
|
|
|
commandBuffer.end();
|
|
|
|
|
|
|
|
vk::raii::Fence drawFence( device, vk::FenceCreateInfo() );
|
|
|
|
submitInfo = vk::SubmitInfo( {}, {}, *commandBuffer );
|
2024-01-16 12:48:53 +00:00
|
|
|
graphicsQueue.submit( submitInfo, drawFence );
|
2021-06-21 09:32:43 +00:00
|
|
|
graphicsQueue.waitIdle();
|
2021-02-17 09:49:59 +00:00
|
|
|
|
|
|
|
/* Make sure command buffer is finished before presenting */
|
2024-01-16 12:48:53 +00:00
|
|
|
while ( device.waitForFences( { drawFence }, true, vk::su::FenceTimeout ) == vk::Result::eTimeout )
|
2021-02-17 09:49:59 +00:00
|
|
|
;
|
|
|
|
|
|
|
|
/* Now present the image in the window */
|
2021-10-11 15:49:13 +00:00
|
|
|
vk::PresentInfoKHR presentInfoKHR( nullptr, *swapChainData.swapChain, imageIndex );
|
2021-06-21 09:32:43 +00:00
|
|
|
result = presentQueue.presentKHR( presentInfoKHR );
|
2021-02-17 09:49:59 +00:00
|
|
|
switch ( result )
|
|
|
|
{
|
|
|
|
case vk::Result::eSuccess: break;
|
2022-02-28 09:11:04 +00:00
|
|
|
case vk::Result::eSuboptimalKHR: std::cout << "vk::Queue::presentKHR returned vk::Result::eSuboptimalKHR !\n"; break;
|
2021-02-17 09:49:59 +00:00
|
|
|
default: assert( false ); // an unexpected result is returned !
|
|
|
|
}
|
|
|
|
std::this_thread::sleep_for( std::chrono::milliseconds( 1000 ) );
|
|
|
|
|
|
|
|
/* VULKAN_KEY_END */
|
|
|
|
}
|
|
|
|
catch ( vk::SystemError & err )
|
|
|
|
{
|
|
|
|
std::cout << "vk::SystemError: " << err.what() << std::endl;
|
|
|
|
exit( -1 );
|
|
|
|
}
|
|
|
|
catch ( std::exception & err )
|
|
|
|
{
|
|
|
|
std::cout << "std::exception: " << err.what() << std::endl;
|
|
|
|
exit( -1 );
|
|
|
|
}
|
|
|
|
catch ( ... )
|
|
|
|
{
|
|
|
|
std::cout << "unknown error\n";
|
|
|
|
exit( -1 );
|
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|