Introduce raii-compliant handle wrapper classes.
This commit is contained in:
@@ -27,43 +27,43 @@ int main( int /*argc*/, char ** /*argv*/ )
|
||||
{
|
||||
try
|
||||
{
|
||||
vk::UniqueInstance instance = vk::su::createInstance( AppName, EngineName, {}, vk::su::getInstanceExtensions() );
|
||||
vk::Instance instance = vk::su::createInstance( AppName, EngineName, {}, vk::su::getInstanceExtensions() );
|
||||
#if !defined( NDEBUG )
|
||||
vk::UniqueDebugUtilsMessengerEXT debugUtilsMessenger = vk::su::createDebugUtilsMessenger( instance );
|
||||
vk::DebugUtilsMessengerEXT debugUtilsMessenger =
|
||||
instance.createDebugUtilsMessengerEXT( vk::su::makeDebugUtilsMessengerCreateInfoEXT() );
|
||||
#endif
|
||||
|
||||
vk::PhysicalDevice physicalDevice = instance->enumeratePhysicalDevices().front();
|
||||
vk::PhysicalDevice physicalDevice = instance.enumeratePhysicalDevices().front();
|
||||
|
||||
uint32_t graphicsQueueFamilyIndex =
|
||||
vk::su::findGraphicsQueueFamilyIndex( physicalDevice.getQueueFamilyProperties() );
|
||||
vk::UniqueDevice device = vk::su::createDevice( physicalDevice, graphicsQueueFamilyIndex );
|
||||
vk::Device device = vk::su::createDevice( physicalDevice, graphicsQueueFamilyIndex );
|
||||
|
||||
vk::UniqueCommandPool commandPool = vk::su::createCommandPool( device, graphicsQueueFamilyIndex );
|
||||
vk::UniqueCommandBuffer commandBuffer = std::move( device
|
||||
->allocateCommandBuffersUnique( vk::CommandBufferAllocateInfo(
|
||||
commandPool.get(), vk::CommandBufferLevel::ePrimary, 1 ) )
|
||||
.front() );
|
||||
vk::CommandPool commandPool = vk::su::createCommandPool( device, graphicsQueueFamilyIndex );
|
||||
vk::CommandBuffer commandBuffer =
|
||||
device.allocateCommandBuffers( vk::CommandBufferAllocateInfo( commandPool, vk::CommandBufferLevel::ePrimary, 1 ) )
|
||||
.front();
|
||||
|
||||
vk::Queue graphicsQueue = device->getQueue( graphicsQueueFamilyIndex, 0 );
|
||||
vk::Queue graphicsQueue = device.getQueue( graphicsQueueFamilyIndex, 0 );
|
||||
|
||||
/* VULKAN_KEY_START */
|
||||
|
||||
// Start with a trivial command buffer and make sure fence wait doesn't time out
|
||||
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();
|
||||
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();
|
||||
|
||||
vk::UniqueFence fence = device->createFenceUnique( vk::FenceCreateInfo() );
|
||||
vk::Fence fence = device.createFence( vk::FenceCreateInfo() );
|
||||
|
||||
vk::SubmitInfo submitInfo( {}, {}, *commandBuffer );
|
||||
graphicsQueue.submit( submitInfo, fence.get() );
|
||||
vk::SubmitInfo submitInfo( {}, {}, commandBuffer );
|
||||
graphicsQueue.submit( submitInfo, fence );
|
||||
|
||||
// Make sure timeout is long enough for a simple command buffer without waiting for an event
|
||||
vk::Result result;
|
||||
int timeouts = -1;
|
||||
do
|
||||
{
|
||||
result = device->waitForFences( fence.get(), true, vk::su::FenceTimeout );
|
||||
result = device.waitForFences( fence, true, vk::su::FenceTimeout );
|
||||
timeouts++;
|
||||
} while ( result == vk::Result::eTimeout );
|
||||
assert( result == vk::Result::eSuccess );
|
||||
@@ -74,25 +74,21 @@ int main( int /*argc*/, char ** /*argv*/ )
|
||||
}
|
||||
|
||||
// Now create an event and wait for it on the GPU
|
||||
vk::UniqueEvent event = device->createEventUnique( vk::EventCreateInfo( vk::EventCreateFlags() ) );
|
||||
vk::Event event = device.createEvent( 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 );
|
||||
commandBuffer->end();
|
||||
device->resetFences( fence.get() );
|
||||
commandBuffer.reset( vk::CommandBufferResetFlags() );
|
||||
commandBuffer.begin( vk::CommandBufferBeginInfo() );
|
||||
commandBuffer.waitEvents(
|
||||
event, vk::PipelineStageFlagBits::eHost, vk::PipelineStageFlagBits::eBottomOfPipe, nullptr, nullptr, nullptr );
|
||||
commandBuffer.end();
|
||||
device.resetFences( fence );
|
||||
|
||||
// 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
|
||||
graphicsQueue.submit( submitInfo, fence.get() );
|
||||
graphicsQueue.submit( submitInfo, fence );
|
||||
|
||||
// We should timeout waiting for the fence because the GPU should be waiting on the event
|
||||
result = device->waitForFences( fence.get(), true, vk::su::FenceTimeout );
|
||||
result = device.waitForFences( fence, true, vk::su::FenceTimeout );
|
||||
if ( result != vk::Result::eTimeout )
|
||||
{
|
||||
std::cout << "Didn't get expected timeout in vk::Device::waitForFences, exiting\n";
|
||||
@@ -101,44 +97,53 @@ int main( int /*argc*/, char ** /*argv*/ )
|
||||
|
||||
// Set the event from the CPU and wait for the fence.
|
||||
// This should succeed since we set the event
|
||||
device->setEvent( event.get() );
|
||||
device.setEvent( event );
|
||||
do
|
||||
{
|
||||
result = device->waitForFences( fence.get(), true, vk::su::FenceTimeout );
|
||||
result = device.waitForFences( fence, true, vk::su::FenceTimeout );
|
||||
} while ( result == vk::Result::eTimeout );
|
||||
assert( result == vk::Result::eSuccess );
|
||||
|
||||
commandBuffer->reset( {} );
|
||||
device->resetFences( fence.get() );
|
||||
device->resetEvent( event.get() );
|
||||
commandBuffer.reset( {} );
|
||||
device.resetFences( fence );
|
||||
device.resetEvent( event );
|
||||
|
||||
// Now set the event from the GPU and wait on the CPU
|
||||
commandBuffer->begin( vk::CommandBufferBeginInfo() );
|
||||
commandBuffer->setEvent( event.get(), vk::PipelineStageFlagBits::eBottomOfPipe );
|
||||
commandBuffer->end();
|
||||
commandBuffer.begin( vk::CommandBufferBeginInfo() );
|
||||
commandBuffer.setEvent( event, vk::PipelineStageFlagBits::eBottomOfPipe );
|
||||
commandBuffer.end();
|
||||
|
||||
// Look for the event on the CPU. It should be vk::Result::eEventReset since we haven't sent the command buffer yet.
|
||||
result = device->getEventStatus( event.get() );
|
||||
result = device.getEventStatus( event );
|
||||
assert( result == vk::Result::eEventReset );
|
||||
|
||||
// Send the command buffer and loop waiting for the event
|
||||
graphicsQueue.submit( submitInfo, fence.get() );
|
||||
graphicsQueue.submit( submitInfo, fence );
|
||||
|
||||
int polls = 0;
|
||||
do
|
||||
{
|
||||
result = device->getEventStatus( event.get() );
|
||||
result = device.getEventStatus( event );
|
||||
polls++;
|
||||
} while ( result != vk::Result::eEventSet );
|
||||
printf( "%d polls to find the event set\n", polls );
|
||||
|
||||
do
|
||||
{
|
||||
result = device->waitForFences( fence.get(), true, vk::su::FenceTimeout );
|
||||
result = device.waitForFences( fence, true, vk::su::FenceTimeout );
|
||||
} while ( result == vk::Result::eTimeout );
|
||||
assert( result == vk::Result::eSuccess );
|
||||
|
||||
device.destroyEvent( event );
|
||||
device.destroyFence( fence );
|
||||
|
||||
/* VULKAN_KEY_END */
|
||||
|
||||
device.freeCommandBuffers( commandPool, commandBuffer );
|
||||
device.destroyCommandPool( commandPool );
|
||||
device.destroy();
|
||||
instance.destroyDebugUtilsMessengerEXT( debugUtilsMessenger );
|
||||
instance.destroy();
|
||||
}
|
||||
catch ( vk::SystemError & err )
|
||||
{
|
||||
|
||||
Reference in New Issue
Block a user