2019-04-01 08:06:49 +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 : Events
|
|
|
|
// Use basic events
|
|
|
|
|
|
|
|
#include "../utils/utils.hpp"
|
|
|
|
#include "vulkan/vulkan.hpp"
|
2020-04-12 19:49:12 +00:00
|
|
|
|
2019-04-01 08:06:49 +00:00
|
|
|
#include <iostream>
|
|
|
|
|
2020-04-12 19:49:12 +00:00
|
|
|
static char const * AppName = "Events";
|
|
|
|
static char const * EngineName = "Vulkan.hpp";
|
2019-04-01 08:06:49 +00:00
|
|
|
|
2020-04-12 19:49:12 +00:00
|
|
|
int main( int /*argc*/, char ** /*argv*/ )
|
2019-04-01 08:06:49 +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-04-01 08:06:49 +00:00
|
|
|
#endif
|
|
|
|
|
2019-06-25 07:47:27 +00:00
|
|
|
vk::PhysicalDevice physicalDevice = instance->enumeratePhysicalDevices().front();
|
2019-04-01 08:06:49 +00:00
|
|
|
|
2020-04-12 19:49:12 +00:00
|
|
|
uint32_t graphicsQueueFamilyIndex =
|
|
|
|
vk::su::findGraphicsQueueFamilyIndex( physicalDevice.getQueueFamilyProperties() );
|
|
|
|
vk::UniqueDevice device = vk::su::createDevice( physicalDevice, graphicsQueueFamilyIndex );
|
2019-04-01 08:06:49 +00:00
|
|
|
|
2020-04-12 19:49:12 +00:00
|
|
|
vk::UniqueCommandPool commandPool = vk::su::createCommandPool( device, graphicsQueueFamilyIndex );
|
|
|
|
vk::UniqueCommandBuffer commandBuffer = std::move( device
|
|
|
|
->allocateCommandBuffersUnique( vk::CommandBufferAllocateInfo(
|
|
|
|
commandPool.get(), vk::CommandBufferLevel::ePrimary, 1 ) )
|
|
|
|
.front() );
|
2019-04-01 08:06:49 +00:00
|
|
|
|
2020-04-12 19:49:12 +00:00
|
|
|
vk::Queue graphicsQueue = device->getQueue( graphicsQueueFamilyIndex, 0 );
|
2019-04-01 08:06:49 +00:00
|
|
|
|
|
|
|
/* VULKAN_KEY_START */
|
|
|
|
|
|
|
|
// Start with a trivial command buffer and make sure fence wait doesn't time out
|
2020-04-12 19:49:12 +00:00
|
|
|
commandBuffer->begin( vk::CommandBufferBeginInfo( vk::CommandBufferUsageFlags() ) );
|
|
|
|
commandBuffer->setViewport( 0, vk::Viewport( 0.0f, 0.0f, 10.0f, 10.0f, 0.0f, 1.0f ) );
|
2019-06-25 07:47:27 +00:00
|
|
|
commandBuffer->end();
|
2019-04-01 08:06:49 +00:00
|
|
|
|
2020-04-12 19:49:12 +00:00
|
|
|
vk::UniqueFence fence = device->createFenceUnique( vk::FenceCreateInfo() );
|
2019-04-01 08:06:49 +00:00
|
|
|
|
2020-07-08 08:58:37 +00:00
|
|
|
vk::SubmitInfo submitInfo( {}, {}, *commandBuffer );
|
2020-04-12 19:49:12 +00:00
|
|
|
graphicsQueue.submit( submitInfo, fence.get() );
|
2019-04-01 08:06:49 +00:00
|
|
|
|
|
|
|
// Make sure timeout is long enough for a simple command buffer without waiting for an event
|
|
|
|
vk::Result result;
|
2020-04-12 19:49:12 +00:00
|
|
|
int timeouts = -1;
|
2019-04-01 08:06:49 +00:00
|
|
|
do
|
|
|
|
{
|
2020-04-12 19:49:12 +00:00
|
|
|
result = device->waitForFences( fence.get(), true, vk::su::FenceTimeout );
|
2019-04-01 08:06:49 +00:00
|
|
|
timeouts++;
|
2020-04-12 19:49:12 +00:00
|
|
|
} while ( result == vk::Result::eTimeout );
|
|
|
|
assert( result == vk::Result::eSuccess );
|
|
|
|
if ( timeouts != 0 )
|
2019-04-01 08:06:49 +00:00
|
|
|
{
|
|
|
|
std::cout << "Unsuitable timeout value, exiting\n";
|
2020-04-12 19:49:12 +00:00
|
|
|
exit( -1 );
|
2019-04-01 08:06:49 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Now create an event and wait for it on the GPU
|
2020-04-12 19:49:12 +00:00
|
|
|
vk::UniqueEvent event = device->createEventUnique( vk::EventCreateInfo( vk::EventCreateFlags() ) );
|
|
|
|
|
|
|
|
commandBuffer->reset( vk::CommandBufferResetFlags() );
|
|
|
|
commandBuffer->begin( vk::CommandBufferBeginInfo() );
|
|
|
|
commandBuffer->waitEvents( event.get(),
|
|
|
|
vk::PipelineStageFlagBits::eHost,
|
|
|
|
vk::PipelineStageFlagBits::eBottomOfPipe,
|
|
|
|
nullptr,
|
|
|
|
nullptr,
|
|
|
|
nullptr );
|
2019-06-25 07:47:27 +00:00
|
|
|
commandBuffer->end();
|
2020-04-12 19:49:12 +00:00
|
|
|
device->resetFences( fence.get() );
|
2019-04-01 08:06:49 +00:00
|
|
|
|
|
|
|
// Note that stepping through this code in the debugger is a bad idea because the GPU can TDR waiting for the event.
|
2019-04-09 13:19:18 +00:00
|
|
|
// Execute the code from vk::Queue::submit() through vk::Device::setEvent() without breakpoints
|
2020-04-12 19:49:12 +00:00
|
|
|
graphicsQueue.submit( submitInfo, fence.get() );
|
2019-04-01 08:06:49 +00:00
|
|
|
|
|
|
|
// We should timeout waiting for the fence because the GPU should be waiting on the event
|
2020-04-12 19:49:12 +00:00
|
|
|
result = device->waitForFences( fence.get(), true, vk::su::FenceTimeout );
|
|
|
|
if ( result != vk::Result::eTimeout )
|
2019-04-01 08:06:49 +00:00
|
|
|
{
|
2019-04-09 13:19:18 +00:00
|
|
|
std::cout << "Didn't get expected timeout in vk::Device::waitForFences, exiting\n";
|
2020-04-12 19:49:12 +00:00
|
|
|
exit( -1 );
|
2019-04-01 08:06:49 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Set the event from the CPU and wait for the fence.
|
|
|
|
// This should succeed since we set the event
|
2020-04-12 19:49:12 +00:00
|
|
|
device->setEvent( event.get() );
|
2019-04-01 08:06:49 +00:00
|
|
|
do
|
|
|
|
{
|
2020-04-12 19:49:12 +00:00
|
|
|
result = device->waitForFences( fence.get(), true, vk::su::FenceTimeout );
|
|
|
|
} while ( result == vk::Result::eTimeout );
|
|
|
|
assert( result == vk::Result::eSuccess );
|
2019-04-01 08:06:49 +00:00
|
|
|
|
2020-04-12 19:49:12 +00:00
|
|
|
commandBuffer->reset( {} );
|
|
|
|
device->resetFences( fence.get() );
|
|
|
|
device->resetEvent( event.get() );
|
2019-04-01 08:06:49 +00:00
|
|
|
|
|
|
|
// Now set the event from the GPU and wait on the CPU
|
2020-04-12 19:49:12 +00:00
|
|
|
commandBuffer->begin( vk::CommandBufferBeginInfo() );
|
|
|
|
commandBuffer->setEvent( event.get(), vk::PipelineStageFlagBits::eBottomOfPipe );
|
2019-06-25 07:47:27 +00:00
|
|
|
commandBuffer->end();
|
2019-04-01 08:06:49 +00:00
|
|
|
|
2019-04-09 13:19:18 +00:00
|
|
|
// Look for the event on the CPU. It should be vk::Result::eEventReset since we haven't sent the command buffer yet.
|
2020-04-12 19:49:12 +00:00
|
|
|
result = device->getEventStatus( event.get() );
|
|
|
|
assert( result == vk::Result::eEventReset );
|
2019-04-01 08:06:49 +00:00
|
|
|
|
|
|
|
// Send the command buffer and loop waiting for the event
|
2020-04-12 19:49:12 +00:00
|
|
|
graphicsQueue.submit( submitInfo, fence.get() );
|
2019-04-01 08:06:49 +00:00
|
|
|
|
|
|
|
int polls = 0;
|
|
|
|
do
|
|
|
|
{
|
2020-04-12 19:49:12 +00:00
|
|
|
result = device->getEventStatus( event.get() );
|
2019-04-01 08:06:49 +00:00
|
|
|
polls++;
|
2020-04-12 19:49:12 +00:00
|
|
|
} while ( result != vk::Result::eEventSet );
|
|
|
|
printf( "%d polls to find the event set\n", polls );
|
2019-04-01 08:06:49 +00:00
|
|
|
|
|
|
|
do
|
|
|
|
{
|
2020-04-12 19:49:12 +00:00
|
|
|
result = device->waitForFences( fence.get(), true, vk::su::FenceTimeout );
|
|
|
|
} while ( result == vk::Result::eTimeout );
|
|
|
|
assert( result == vk::Result::eSuccess );
|
2019-04-01 08:06:49 +00:00
|
|
|
|
|
|
|
/* VULKAN_KEY_END */
|
|
|
|
}
|
2020-04-12 19:49:12 +00:00
|
|
|
catch ( vk::SystemError & err )
|
2019-04-01 08:06:49 +00:00
|
|
|
{
|
|
|
|
std::cout << "vk::SystemError: " << err.what() << std::endl;
|
2020-04-12 19:49:12 +00:00
|
|
|
exit( -1 );
|
2019-04-01 08:06:49 +00:00
|
|
|
}
|
2020-07-08 08:58:37 +00:00
|
|
|
catch ( std::exception & err )
|
2019-04-01 08:06:49 +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-04-01 08:06:49 +00:00
|
|
|
}
|
2020-04-12 19:49:12 +00:00
|
|
|
catch ( ... )
|
2019-04-01 08:06:49 +00:00
|
|
|
{
|
|
|
|
std::cout << "unknown error\n";
|
2020-04-12 19:49:12 +00:00
|
|
|
exit( -1 );
|
2019-04-01 08:06:49 +00:00
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|