2019-03-26 11:24:36 +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 "vulkan/vulkan.hpp"
|
|
|
|
|
2020-01-28 09:16:10 +00:00
|
|
|
#include <thread>
|
|
|
|
|
2020-04-12 19:49:12 +00:00
|
|
|
static char const * AppName = "CopyBlitImage";
|
|
|
|
static char const * EngineName = "Vulkan.hpp";
|
2019-03-26 11:24:36 +00:00
|
|
|
|
2020-04-12 19:49:12 +00:00
|
|
|
int main( int /*argc*/, char ** /*argv*/ )
|
2019-03-26 11:24:36 +00:00
|
|
|
{
|
|
|
|
try
|
|
|
|
{
|
2020-04-12 19:49:12 +00:00
|
|
|
vk::UniqueInstance instance = vk::su::createInstance( AppName, EngineName, {}, vk::su::getInstanceExtensions() );
|
|
|
|
#if !defined( NDEBUG )
|
|
|
|
vk::UniqueDebugUtilsMessengerEXT debugUtilsMessenger = vk::su::createDebugUtilsMessenger( instance );
|
2019-03-26 11:24:36 +00:00
|
|
|
#endif
|
|
|
|
|
2019-06-25 07:47:27 +00:00
|
|
|
vk::PhysicalDevice physicalDevice = instance->enumeratePhysicalDevices().front();
|
2019-03-26 11:24:36 +00:00
|
|
|
|
2020-04-12 19:49:12 +00:00
|
|
|
vk::su::SurfaceData surfaceData( instance, AppName, vk::Extent2D( 640, 640 ) );
|
2019-03-26 11:24:36 +00:00
|
|
|
|
2020-04-12 19:49:12 +00:00
|
|
|
vk::SurfaceCapabilitiesKHR surfaceCapabilities =
|
|
|
|
physicalDevice.getSurfaceCapabilitiesKHR( surfaceData.surface.get() );
|
|
|
|
if ( !( surfaceCapabilities.supportedUsageFlags & vk::ImageUsageFlagBits::eTransferDst ) )
|
2019-03-26 11:24:36 +00:00
|
|
|
{
|
|
|
|
std::cout << "Surface cannot be destination of blit - abort \n";
|
2020-04-12 19:49:12 +00:00
|
|
|
exit( -1 );
|
2019-03-26 11:24:36 +00:00
|
|
|
}
|
|
|
|
|
2020-04-12 19:49:12 +00:00
|
|
|
std::pair<uint32_t, uint32_t> graphicsAndPresentQueueFamilyIndex =
|
|
|
|
vk::su::findGraphicsAndPresentQueueFamilyIndex( physicalDevice, *surfaceData.surface );
|
|
|
|
vk::UniqueDevice 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::Queue graphicsQueue = device->getQueue( graphicsAndPresentQueueFamilyIndex.first, 0 );
|
|
|
|
vk::Queue presentQueue = device->getQueue( graphicsAndPresentQueueFamilyIndex.second, 0 );
|
|
|
|
|
|
|
|
vk::su::SwapChainData swapChainData( physicalDevice,
|
|
|
|
device,
|
|
|
|
*surfaceData.surface,
|
|
|
|
surfaceData.extent,
|
|
|
|
vk::ImageUsageFlagBits::eColorAttachment |
|
|
|
|
vk::ImageUsageFlagBits::eTransferDst,
|
|
|
|
vk::UniqueSwapchainKHR(),
|
|
|
|
graphicsAndPresentQueueFamilyIndex.first,
|
|
|
|
graphicsAndPresentQueueFamilyIndex.second );
|
2019-03-26 11:24:36 +00:00
|
|
|
|
|
|
|
/* VULKAN_KEY_START */
|
|
|
|
|
2020-04-12 19:49:12 +00:00
|
|
|
vk::FormatProperties formatProperties = physicalDevice.getFormatProperties( swapChainData.colorFormat );
|
|
|
|
assert( ( formatProperties.linearTilingFeatures & vk::FormatFeatureFlagBits::eBlitSrc ) &&
|
|
|
|
"Format cannot be used as transfer source" );
|
2019-03-26 11:24:36 +00:00
|
|
|
|
2020-04-12 19:49:12 +00:00
|
|
|
vk::UniqueSemaphore imageAcquiredSemaphore = device->createSemaphoreUnique( vk::SemaphoreCreateInfo() );
|
2019-03-26 11:24:36 +00:00
|
|
|
|
|
|
|
// Get the index of the next available swapchain image:
|
2020-04-12 19:49:12 +00:00
|
|
|
vk::ResultValue<uint32_t> nextImage = device->acquireNextImageKHR(
|
|
|
|
swapChainData.swapChain.get(), vk::su::FenceTimeout, imageAcquiredSemaphore.get(), nullptr );
|
|
|
|
assert( nextImage.result == vk::Result::eSuccess );
|
|
|
|
assert( nextImage.value < swapChainData.images.size() );
|
2019-03-26 11:24:36 +00:00
|
|
|
uint32_t currentBuffer = nextImage.value;
|
|
|
|
|
2020-04-12 19:49:12 +00:00
|
|
|
commandBuffer->begin( vk::CommandBufferBeginInfo() );
|
|
|
|
vk::su::setImageLayout( commandBuffer,
|
|
|
|
swapChainData.images[currentBuffer],
|
|
|
|
swapChainData.colorFormat,
|
|
|
|
vk::ImageLayout::eUndefined,
|
|
|
|
vk::ImageLayout::eTransferDstOptimal );
|
2019-03-26 11:24:36 +00:00
|
|
|
|
|
|
|
// Create an image, map it, and write some values to the image
|
2020-04-12 19:49:12 +00:00
|
|
|
vk::ImageCreateInfo imageCreateInfo( vk::ImageCreateFlags(),
|
|
|
|
vk::ImageType::e2D,
|
|
|
|
swapChainData.colorFormat,
|
|
|
|
vk::Extent3D( surfaceData.extent, 1 ),
|
|
|
|
1,
|
|
|
|
1,
|
|
|
|
vk::SampleCountFlagBits::e1,
|
|
|
|
vk::ImageTiling::eLinear,
|
|
|
|
vk::ImageUsageFlagBits::eTransferSrc );
|
|
|
|
vk::UniqueImage blitSourceImage = device->createImageUnique( imageCreateInfo );
|
|
|
|
|
|
|
|
vk::PhysicalDeviceMemoryProperties memoryProperties = physicalDevice.getMemoryProperties();
|
|
|
|
vk::MemoryRequirements memoryRequirements = device->getImageMemoryRequirements( blitSourceImage.get() );
|
|
|
|
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::su::setImageLayout( commandBuffer,
|
|
|
|
blitSourceImage.get(),
|
|
|
|
swapChainData.colorFormat,
|
|
|
|
vk::ImageLayout::eUndefined,
|
|
|
|
vk::ImageLayout::eGeneral );
|
2019-03-26 11:24:36 +00:00
|
|
|
|
2019-06-25 07:47:27 +00:00
|
|
|
commandBuffer->end();
|
2019-03-26 11:24:36 +00:00
|
|
|
|
|
|
|
/* Queue the command buffer for execution */
|
2020-04-12 19:49:12 +00:00
|
|
|
vk::UniqueFence commandFence = device->createFenceUnique( {} );
|
|
|
|
vk::PipelineStageFlags pipeStageFlags( vk::PipelineStageFlagBits::eColorAttachmentOutput );
|
2020-07-08 08:58:37 +00:00
|
|
|
graphicsQueue.submit( vk::SubmitInfo( *imageAcquiredSemaphore, pipeStageFlags, *commandBuffer ),
|
2020-04-12 19:49:12 +00:00
|
|
|
commandFence.get() );
|
2019-03-26 11:24:36 +00:00
|
|
|
|
|
|
|
/* Make sure command buffer is finished before mapping */
|
2020-04-12 19:49:12 +00:00
|
|
|
while ( device->waitForFences( commandFence.get(), true, vk::su::FenceTimeout ) == vk::Result::eTimeout )
|
2019-03-26 11:24:36 +00:00
|
|
|
;
|
|
|
|
|
2020-04-12 19:49:12 +00:00
|
|
|
unsigned char * pImageMemory =
|
|
|
|
static_cast<unsigned char *>( device->mapMemory( deviceMemory.get(), 0, memoryRequirements.size ) );
|
2019-03-26 11:24:36 +00:00
|
|
|
|
|
|
|
// Checkerboard of 8x8 pixel squares
|
2020-04-12 19:49:12 +00:00
|
|
|
for ( uint32_t row = 0; row < surfaceData.extent.height; row++ )
|
2019-03-26 11:24:36 +00:00
|
|
|
{
|
2020-04-12 19:49:12 +00:00
|
|
|
for ( uint32_t col = 0; col < surfaceData.extent.width; col++ )
|
2019-03-26 11:24:36 +00:00
|
|
|
{
|
2020-04-12 19:49:12 +00:00
|
|
|
unsigned char rgb = ( ( ( row & 0x8 ) == 0 ) ^ ( ( col & 0x8 ) == 0 ) ) * 255;
|
|
|
|
pImageMemory[0] = rgb;
|
|
|
|
pImageMemory[1] = rgb;
|
|
|
|
pImageMemory[2] = rgb;
|
|
|
|
pImageMemory[3] = 255;
|
2019-03-26 11:24:36 +00:00
|
|
|
pImageMemory += 4;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Flush the mapped memory and then unmap it. Assume it isn't coherent since we didn't really confirm
|
2020-04-12 19:49:12 +00:00
|
|
|
device->flushMappedMemoryRanges( vk::MappedMemoryRange( deviceMemory.get(), 0, memoryRequirements.size ) );
|
|
|
|
device->unmapMemory( deviceMemory.get() );
|
2019-03-26 11:24:36 +00:00
|
|
|
|
2020-04-12 19:49:12 +00:00
|
|
|
commandBuffer->reset( {} );
|
|
|
|
commandBuffer->begin( vk::CommandBufferBeginInfo() );
|
2019-03-26 11:24:36 +00:00
|
|
|
|
|
|
|
// Intend to blit from this image, set the layout accordingly
|
2020-04-12 19:49:12 +00:00
|
|
|
vk::su::setImageLayout( commandBuffer,
|
|
|
|
blitSourceImage.get(),
|
|
|
|
swapChainData.colorFormat,
|
|
|
|
vk::ImageLayout::eGeneral,
|
|
|
|
vk::ImageLayout::eTransferSrcOptimal );
|
2019-03-26 11:24:36 +00:00
|
|
|
|
|
|
|
vk::Image blitDestinationImage = swapChainData.images[currentBuffer];
|
|
|
|
|
|
|
|
// Do a 32x32 blit to all of the dst image - should get big squares
|
2020-04-12 19:49:12 +00:00
|
|
|
vk::ImageSubresourceLayers imageSubresourceLayers( vk::ImageAspectFlagBits::eColor, 0, 0, 1 );
|
|
|
|
vk::ImageBlit imageBlit(
|
|
|
|
imageSubresourceLayers,
|
|
|
|
{ { 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 );
|
2019-03-26 11:24:36 +00:00
|
|
|
|
|
|
|
// Use a barrier to make sure the blit is finished before the copy starts
|
2020-04-12 19:49:12 +00:00
|
|
|
vk::ImageMemoryBarrier memoryBarrier( vk::AccessFlagBits::eTransferWrite,
|
|
|
|
vk::AccessFlagBits::eMemoryRead,
|
|
|
|
vk::ImageLayout::eTransferDstOptimal,
|
|
|
|
vk::ImageLayout::eTransferDstOptimal,
|
|
|
|
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::eTransfer,
|
|
|
|
vk::DependencyFlags(),
|
|
|
|
nullptr,
|
|
|
|
nullptr,
|
|
|
|
memoryBarrier );
|
2019-03-26 11:24:36 +00:00
|
|
|
|
|
|
|
// Do a image copy to part of the dst image - checks should stay small
|
2020-04-12 19:49:12 +00:00
|
|
|
vk::ImageCopy imageCopy( imageSubresourceLayers,
|
|
|
|
vk::Offset3D(),
|
|
|
|
imageSubresourceLayers,
|
|
|
|
vk::Offset3D( 256, 256, 0 ),
|
|
|
|
vk::Extent3D( 128, 128, 1 ) );
|
|
|
|
commandBuffer->copyImage( blitSourceImage.get(),
|
|
|
|
vk::ImageLayout::eTransferSrcOptimal,
|
|
|
|
blitDestinationImage,
|
|
|
|
vk::ImageLayout::eTransferDstOptimal,
|
|
|
|
imageCopy );
|
|
|
|
|
|
|
|
vk::ImageMemoryBarrier prePresentBarrier(
|
|
|
|
vk::AccessFlagBits::eTransferWrite,
|
|
|
|
vk::AccessFlagBits::eMemoryRead,
|
|
|
|
vk::ImageLayout::eTransferDstOptimal,
|
|
|
|
vk::ImageLayout::ePresentSrcKHR,
|
|
|
|
VK_QUEUE_FAMILY_IGNORED,
|
|
|
|
VK_QUEUE_FAMILY_IGNORED,
|
|
|
|
swapChainData.images[currentBuffer],
|
|
|
|
vk::ImageSubresourceRange( vk::ImageAspectFlagBits::eColor, 0, 1, 0, 1 ) );
|
|
|
|
commandBuffer->pipelineBarrier( vk::PipelineStageFlagBits::eTransfer,
|
|
|
|
vk::PipelineStageFlagBits::eTopOfPipe,
|
|
|
|
vk::DependencyFlags(),
|
|
|
|
nullptr,
|
|
|
|
nullptr,
|
|
|
|
prePresentBarrier );
|
2019-06-25 07:47:27 +00:00
|
|
|
commandBuffer->end();
|
2019-03-26 11:24:36 +00:00
|
|
|
|
2020-04-12 19:49:12 +00:00
|
|
|
vk::UniqueFence drawFence = device->createFenceUnique( {} );
|
2020-07-08 08:58:37 +00:00
|
|
|
graphicsQueue.submit( vk::SubmitInfo( {}, {}, *commandBuffer ), drawFence.get() );
|
2019-03-26 11:24:36 +00:00
|
|
|
graphicsQueue.waitIdle();
|
|
|
|
|
|
|
|
/* Make sure command buffer is finished before presenting */
|
2020-04-12 19:49:12 +00:00
|
|
|
while ( device->waitForFences( drawFence.get(), true, vk::su::FenceTimeout ) == vk::Result::eTimeout )
|
2019-03-26 11:24:36 +00:00
|
|
|
;
|
|
|
|
|
|
|
|
/* Now present the image in the window */
|
2020-07-15 06:55:47 +00:00
|
|
|
vk::Result result =
|
|
|
|
presentQueue.presentKHR( vk::PresentInfoKHR( {}, *swapChainData.swapChain, currentBuffer, {} ) );
|
|
|
|
switch ( result )
|
|
|
|
{
|
|
|
|
case vk::Result::eSuccess: break;
|
|
|
|
case vk::Result::eSuboptimalKHR: std::cout << "vk::Queue::presentKHR returned vk::Result::eSuboptimalKHR !\n";
|
|
|
|
default: assert( false ); // an unexpected result is returned !
|
|
|
|
}
|
2020-04-12 19:49:12 +00:00
|
|
|
std::this_thread::sleep_for( std::chrono::milliseconds( 1000 ) );
|
2019-03-26 11:24:36 +00:00
|
|
|
|
|
|
|
/* VULKAN_KEY_END */
|
|
|
|
}
|
2020-04-12 19:49:12 +00:00
|
|
|
catch ( vk::SystemError & err )
|
2019-03-26 11:24:36 +00:00
|
|
|
{
|
|
|
|
std::cout << "vk::SystemError: " << err.what() << std::endl;
|
2020-04-12 19:49:12 +00:00
|
|
|
exit( -1 );
|
2019-03-26 11:24:36 +00:00
|
|
|
}
|
2020-07-08 08:58:37 +00:00
|
|
|
catch ( std::exception & err )
|
2019-03-26 11:24:36 +00:00
|
|
|
{
|
2020-07-08 08:58:37 +00:00
|
|
|
std::cout << "std::exception: " << err.what() << std::endl;
|
2020-04-12 19:49:12 +00:00
|
|
|
exit( -1 );
|
2019-03-26 11:24:36 +00:00
|
|
|
}
|
2020-04-12 19:49:12 +00:00
|
|
|
catch ( ... )
|
2019-03-26 11:24:36 +00:00
|
|
|
{
|
|
|
|
std::cout << "unknown error\n";
|
2020-04-12 19:49:12 +00:00
|
|
|
exit( -1 );
|
2019-03-26 11:24:36 +00:00
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|