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 : Events
|
|
|
|
// Use basic events
|
|
|
|
|
|
|
|
#include "../utils/utils.hpp"
|
|
|
|
|
|
|
|
#include <iostream>
|
|
|
|
|
|
|
|
static char const * AppName = "Events";
|
|
|
|
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
|
|
|
|
2022-02-28 09:11:04 +00:00
|
|
|
uint32_t graphicsQueueFamilyIndex = vk::su::findGraphicsQueueFamilyIndex( physicalDevice.getQueueFamilyProperties() );
|
|
|
|
vk::raii::Device device = vk::raii::su::makeDevice( physicalDevice, graphicsQueueFamilyIndex, vk::su::getDeviceExtensions() );
|
2021-02-17 09:49:59 +00:00
|
|
|
|
2023-08-14 14:09:11 +00:00
|
|
|
vk::raii::CommandPool commandPool = vk::raii::CommandPool( device, { {}, graphicsQueueFamilyIndex } );
|
2021-06-21 09:32:43 +00:00
|
|
|
vk::raii::CommandBuffer commandBuffer = vk::raii::su::makeCommandBuffer( device, commandPool );
|
2021-02-17 09:49:59 +00:00
|
|
|
|
2021-06-21 09:32:43 +00:00
|
|
|
vk::raii::Queue graphicsQueue( device, graphicsQueueFamilyIndex, 0 );
|
2021-02-17 09:49:59 +00:00
|
|
|
|
|
|
|
/* VULKAN_KEY_START */
|
|
|
|
|
|
|
|
// Start with a trivial command buffer and make sure fence wait doesn't time out
|
2021-06-21 09:32:43 +00:00
|
|
|
commandBuffer.begin( {} );
|
|
|
|
commandBuffer.setViewport( 0, vk::Viewport( 0.0f, 0.0f, 10.0f, 10.0f, 0.0f, 1.0f ) );
|
|
|
|
commandBuffer.end();
|
2021-02-17 09:49:59 +00:00
|
|
|
|
2021-06-21 09:32:43 +00:00
|
|
|
vk::raii::Fence fence( device, vk::FenceCreateInfo() );
|
2021-02-17 09:49:59 +00:00
|
|
|
|
2021-06-21 09:32:43 +00:00
|
|
|
vk::SubmitInfo submitInfo( {}, {}, *commandBuffer );
|
|
|
|
graphicsQueue.submit( submitInfo, *fence );
|
2021-02-17 09:49:59 +00:00
|
|
|
|
|
|
|
// Make sure timeout is long enough for a simple command buffer without waiting for an event
|
|
|
|
vk::Result result;
|
|
|
|
int timeouts = -1;
|
|
|
|
do
|
|
|
|
{
|
2021-06-21 09:32:43 +00:00
|
|
|
result = device.waitForFences( { *fence }, true, vk::su::FenceTimeout );
|
2021-02-17 09:49:59 +00:00
|
|
|
timeouts++;
|
|
|
|
} while ( result == vk::Result::eTimeout );
|
|
|
|
assert( result == vk::Result::eSuccess );
|
|
|
|
if ( timeouts != 0 )
|
|
|
|
{
|
|
|
|
std::cout << "Unsuitable timeout value, exiting\n";
|
|
|
|
exit( -1 );
|
|
|
|
}
|
|
|
|
|
|
|
|
// Now create an event and wait for it on the GPU
|
2021-06-21 09:32:43 +00:00
|
|
|
vk::raii::Event event( device, vk::EventCreateInfo() );
|
|
|
|
|
2023-08-14 14:09:11 +00:00
|
|
|
// reset the command buffer by resetting the complete command pool of this frame
|
|
|
|
commandPool.reset();
|
|
|
|
|
2021-06-21 09:32:43 +00:00
|
|
|
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-06-21 09:32:43 +00:00
|
|
|
commandBuffer.end();
|
|
|
|
device.resetFences( { *fence } );
|
2021-02-17 09:49:59 +00:00
|
|
|
|
|
|
|
// Note that stepping through this code in the debugger is a bad idea because the GPU can TDR waiting for the event.
|
|
|
|
// Execute the code from vk::Queue::submit() through vk::Device::setEvent() without breakpoints
|
2021-06-21 09:32:43 +00:00
|
|
|
graphicsQueue.submit( submitInfo, *fence );
|
2021-02-17 09:49:59 +00:00
|
|
|
|
|
|
|
// We should timeout waiting for the fence because the GPU should be waiting on the event
|
2021-06-21 09:32:43 +00:00
|
|
|
result = device.waitForFences( { *fence }, true, vk::su::FenceTimeout );
|
2021-02-17 09:49:59 +00:00
|
|
|
if ( result != vk::Result::eTimeout )
|
|
|
|
{
|
|
|
|
std::cout << "Didn't get expected timeout in vk::Device::waitForFences, exiting\n";
|
|
|
|
exit( -1 );
|
|
|
|
}
|
|
|
|
|
|
|
|
// Set the event from the CPU and wait for the fence.
|
|
|
|
// This should succeed since we set the event
|
2021-06-21 09:32:43 +00:00
|
|
|
event.set();
|
2021-02-17 09:49:59 +00:00
|
|
|
do
|
|
|
|
{
|
2021-06-21 09:32:43 +00:00
|
|
|
result = device.waitForFences( { *fence }, true, vk::su::FenceTimeout );
|
2021-02-17 09:49:59 +00:00
|
|
|
} while ( result == vk::Result::eTimeout );
|
|
|
|
assert( result == vk::Result::eSuccess );
|
|
|
|
|
2021-06-21 09:32:43 +00:00
|
|
|
device.resetFences( { *fence } );
|
|
|
|
event.reset();
|
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-02-17 09:49:59 +00:00
|
|
|
// Now set the event from the GPU and wait on the CPU
|
2021-06-21 09:32:43 +00:00
|
|
|
commandBuffer.begin( vk::CommandBufferBeginInfo() );
|
|
|
|
commandBuffer.setEvent( *event, vk::PipelineStageFlagBits::eBottomOfPipe );
|
|
|
|
commandBuffer.end();
|
2021-02-17 09:49:59 +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-06-21 09:32:43 +00:00
|
|
|
result = event.getStatus();
|
2021-02-17 09:49:59 +00:00
|
|
|
assert( result == vk::Result::eEventReset );
|
|
|
|
|
|
|
|
// Send the command buffer and loop waiting for the event
|
2021-06-21 09:32:43 +00:00
|
|
|
graphicsQueue.submit( submitInfo, *fence );
|
2021-02-17 09:49:59 +00:00
|
|
|
|
|
|
|
int polls = 0;
|
|
|
|
do
|
|
|
|
{
|
2021-06-21 09:32:43 +00:00
|
|
|
result = event.getStatus();
|
2021-02-17 09:49:59 +00:00
|
|
|
polls++;
|
|
|
|
} while ( result != vk::Result::eEventSet );
|
|
|
|
printf( "%d polls to find the event set\n", polls );
|
|
|
|
|
|
|
|
do
|
|
|
|
{
|
2021-06-21 09:32:43 +00:00
|
|
|
result = device.waitForFences( { *fence }, true, vk::su::FenceTimeout );
|
2021-02-17 09:49:59 +00:00
|
|
|
} while ( result == vk::Result::eTimeout );
|
|
|
|
assert( result == vk::Result::eSuccess );
|
|
|
|
|
|
|
|
/* 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;
|
|
|
|
}
|