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"
|
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
|
|
|
|
{
|
2021-02-17 09:49:59 +00:00
|
|
|
vk::Instance instance = vk::su::createInstance( AppName, EngineName, {}, vk::su::getInstanceExtensions() );
|
2020-04-12 19:49:12 +00:00
|
|
|
#if !defined( NDEBUG )
|
2022-02-28 09:11:04 +00:00
|
|
|
vk::DebugUtilsMessengerEXT debugUtilsMessenger = instance.createDebugUtilsMessengerEXT( vk::su::makeDebugUtilsMessengerCreateInfoEXT() );
|
2019-04-01 08:06:49 +00:00
|
|
|
#endif
|
|
|
|
|
2021-02-17 09:49:59 +00:00
|
|
|
vk::PhysicalDevice physicalDevice = instance.enumeratePhysicalDevices().front();
|
2019-04-01 08:06:49 +00:00
|
|
|
|
2022-02-28 09:11:04 +00:00
|
|
|
uint32_t graphicsQueueFamilyIndex = vk::su::findGraphicsQueueFamilyIndex( physicalDevice.getQueueFamilyProperties() );
|
|
|
|
vk::Device device = vk::su::createDevice( physicalDevice, graphicsQueueFamilyIndex );
|
2019-04-01 08:06:49 +00:00
|
|
|
|
2021-02-17 09:49:59 +00:00
|
|
|
vk::CommandPool commandPool = vk::su::createCommandPool( device, graphicsQueueFamilyIndex );
|
|
|
|
vk::CommandBuffer commandBuffer =
|
2022-02-28 09:11:04 +00:00
|
|
|
device.allocateCommandBuffers( vk::CommandBufferAllocateInfo( commandPool, vk::CommandBufferLevel::ePrimary, 1 ) ).front();
|
2019-04-01 08:06:49 +00:00
|
|
|
|
2021-02-17 09:49:59 +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
|
2021-02-17 09:49:59 +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 ) );
|
|
|
|
commandBuffer.end();
|
2019-04-01 08:06:49 +00:00
|
|
|
|
2021-02-17 09:49:59 +00:00
|
|
|
vk::Fence fence = device.createFence( vk::FenceCreateInfo() );
|
2019-04-01 08:06:49 +00:00
|
|
|
|
2021-02-17 09:49:59 +00:00
|
|
|
vk::SubmitInfo submitInfo( {}, {}, commandBuffer );
|
|
|
|
graphicsQueue.submit( submitInfo, fence );
|
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
|
|
|
|
{
|
2021-02-17 09:49:59 +00:00
|
|
|
result = device.waitForFences( fence, 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
|
2021-02-17 09:49:59 +00:00
|
|
|
vk::Event event = device.createEvent( vk::EventCreateInfo( vk::EventCreateFlags() ) );
|
|
|
|
|
|
|
|
commandBuffer.reset( vk::CommandBufferResetFlags() );
|
|
|
|
commandBuffer.begin( vk::CommandBufferBeginInfo() );
|
2022-02-28 09:11:04 +00:00
|
|
|
commandBuffer.waitEvents( event, vk::PipelineStageFlagBits::eHost, vk::PipelineStageFlagBits::eBottomOfPipe, nullptr, nullptr, nullptr );
|
2021-02-17 09:49:59 +00:00
|
|
|
commandBuffer.end();
|
|
|
|
device.resetFences( fence );
|
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
|
2021-02-17 09:49:59 +00:00
|
|
|
graphicsQueue.submit( submitInfo, fence );
|
2019-04-01 08:06:49 +00:00
|
|
|
|
|
|
|
// We should timeout waiting for the fence because the GPU should be waiting on the event
|
2021-02-17 09:49:59 +00:00
|
|
|
result = device.waitForFences( fence, true, vk::su::FenceTimeout );
|
2020-04-12 19:49:12 +00:00
|
|
|
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
|
2021-02-17 09:49:59 +00:00
|
|
|
device.setEvent( event );
|
2019-04-01 08:06:49 +00:00
|
|
|
do
|
|
|
|
{
|
2021-02-17 09:49:59 +00:00
|
|
|
result = device.waitForFences( fence, true, vk::su::FenceTimeout );
|
2020-04-12 19:49:12 +00:00
|
|
|
} while ( result == vk::Result::eTimeout );
|
|
|
|
assert( result == vk::Result::eSuccess );
|
2019-04-01 08:06:49 +00:00
|
|
|
|
2021-02-17 09:49:59 +00:00
|
|
|
commandBuffer.reset( {} );
|
|
|
|
device.resetFences( fence );
|
|
|
|
device.resetEvent( event );
|
2019-04-01 08:06:49 +00:00
|
|
|
|
|
|
|
// Now set the event from the GPU and wait on the CPU
|
2021-02-17 09:49:59 +00:00
|
|
|
commandBuffer.begin( vk::CommandBufferBeginInfo() );
|
|
|
|
commandBuffer.setEvent( event, vk::PipelineStageFlagBits::eBottomOfPipe );
|
|
|
|
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.
|
2021-02-17 09:49:59 +00:00
|
|
|
result = device.getEventStatus( event );
|
2020-04-12 19:49:12 +00:00
|
|
|
assert( result == vk::Result::eEventReset );
|
2019-04-01 08:06:49 +00:00
|
|
|
|
|
|
|
// Send the command buffer and loop waiting for the event
|
2021-02-17 09:49:59 +00:00
|
|
|
graphicsQueue.submit( submitInfo, fence );
|
2019-04-01 08:06:49 +00:00
|
|
|
|
|
|
|
int polls = 0;
|
|
|
|
do
|
|
|
|
{
|
2021-02-17 09:49:59 +00:00
|
|
|
result = device.getEventStatus( event );
|
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
|
|
|
|
{
|
2021-02-17 09:49:59 +00:00
|
|
|
result = device.waitForFences( fence, true, vk::su::FenceTimeout );
|
2020-04-12 19:49:12 +00:00
|
|
|
} while ( result == vk::Result::eTimeout );
|
|
|
|
assert( result == vk::Result::eSuccess );
|
2019-04-01 08:06:49 +00:00
|
|
|
|
2021-02-17 09:49:59 +00:00
|
|
|
device.destroyEvent( event );
|
|
|
|
device.destroyFence( fence );
|
|
|
|
|
2019-04-01 08:06:49 +00:00
|
|
|
/* VULKAN_KEY_END */
|
2021-02-17 09:49:59 +00:00
|
|
|
|
|
|
|
device.freeCommandBuffers( commandPool, commandBuffer );
|
|
|
|
device.destroyCommandPool( commandPool );
|
|
|
|
device.destroy();
|
2021-05-25 14:14:56 +00:00
|
|
|
#if !defined( NDEBUG )
|
2021-02-17 09:49:59 +00:00
|
|
|
instance.destroyDebugUtilsMessengerEXT( debugUtilsMessenger );
|
2021-05-25 14:14:56 +00:00
|
|
|
#endif
|
2021-02-17 09:49:59 +00:00
|
|
|
instance.destroy();
|
2019-04-01 08:06:49 +00:00
|
|
|
}
|
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;
|
|
|
|
}
|