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"
|
|
|
|
#include <iostream>
|
|
|
|
|
|
|
|
static char const* AppName = "Events";
|
|
|
|
static char const* EngineName = "Vulkan.hpp";
|
|
|
|
|
|
|
|
int main(int /*argc*/, char ** /*argv*/)
|
|
|
|
{
|
|
|
|
try
|
|
|
|
{
|
2019-07-09 07:25:48 +00:00
|
|
|
vk::UniqueInstance instance = vk::su::createInstance(AppName, EngineName, {}, vk::su::getInstanceExtensions());
|
2019-04-01 08:06:49 +00:00
|
|
|
#if !defined(NDEBUG)
|
2019-09-25 09:56:46 +00:00
|
|
|
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
|
|
|
|
2019-06-25 07:47:27 +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
|
|
|
|
|
|
|
vk::UniqueCommandPool commandPool = vk::su::createCommandPool(device, graphicsQueueFamilyIndex);
|
2019-06-25 07:47:27 +00:00
|
|
|
vk::UniqueCommandBuffer commandBuffer = std::move(device->allocateCommandBuffersUnique(vk::CommandBufferAllocateInfo(commandPool.get(), vk::CommandBufferLevel::ePrimary, 1)).front());
|
2019-04-01 08:06:49 +00:00
|
|
|
|
|
|
|
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
|
2019-06-25 07:47:27 +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
|
|
|
|
|
|
|
vk::UniqueFence fence = device->createFenceUnique(vk::FenceCreateInfo());
|
|
|
|
|
|
|
|
vk::PipelineStageFlags waitDestinationStageMask(vk::PipelineStageFlagBits::eColorAttachmentOutput);
|
2019-06-25 07:47:27 +00:00
|
|
|
vk::SubmitInfo submitInfo(0, nullptr, &waitDestinationStageMask, 1, &commandBuffer.get());
|
2019-04-01 08:06:49 +00:00
|
|
|
graphicsQueue.submit(submitInfo, fence.get());
|
|
|
|
|
|
|
|
// 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);
|
|
|
|
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
|
|
|
|
vk::UniqueEvent event = device->createEventUnique(vk::EventCreateInfo(vk::EventCreateFlags()));
|
|
|
|
|
2019-06-25 07:47:27 +00:00
|
|
|
commandBuffer->reset(vk::CommandBufferResetFlags());
|
|
|
|
commandBuffer->begin(vk::CommandBufferBeginInfo());
|
|
|
|
commandBuffer->waitEvents(event.get(), vk::PipelineStageFlagBits::eHost, vk::PipelineStageFlagBits::eBottomOfPipe, nullptr, nullptr, nullptr);
|
|
|
|
commandBuffer->end();
|
2019-04-01 08:06:49 +00:00
|
|
|
device->resetFences(fence.get());
|
|
|
|
|
|
|
|
// 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
|
2019-04-01 08:06:49 +00:00
|
|
|
waitDestinationStageMask = vk::PipelineStageFlagBits::eBottomOfPipe;
|
|
|
|
graphicsQueue.submit(submitInfo, fence.get());
|
|
|
|
|
|
|
|
// 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);
|
|
|
|
if (result != vk::Result::eTimeout)
|
|
|
|
{
|
2019-04-09 13:19:18 +00:00
|
|
|
std::cout << "Didn't get expected timeout in vk::Device::waitForFences, exiting\n";
|
2019-04-01 08:06:49 +00:00
|
|
|
exit(-1);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Set the event from the CPU and wait for the fence.
|
|
|
|
// This should succeed since we set the event
|
|
|
|
device->setEvent(event.get());
|
|
|
|
do
|
|
|
|
{
|
|
|
|
result = device->waitForFences(fence.get(), true, vk::su::FenceTimeout);
|
|
|
|
} while (result == vk::Result::eTimeout);
|
|
|
|
assert(result == vk::Result::eSuccess);
|
|
|
|
|
2019-06-25 07:47:27 +00:00
|
|
|
commandBuffer->reset({});
|
2019-04-01 08:06:49 +00:00
|
|
|
device->resetFences(fence.get());
|
|
|
|
device->resetEvent(event.get());
|
|
|
|
|
|
|
|
// Now set the event from the GPU and wait on the CPU
|
2019-06-25 07:47:27 +00:00
|
|
|
commandBuffer->begin(vk::CommandBufferBeginInfo());
|
|
|
|
commandBuffer->setEvent(event.get(), 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.
|
2019-04-01 08:06:49 +00:00
|
|
|
result = device->getEventStatus(event.get());
|
|
|
|
assert(result == vk::Result::eEventReset);
|
|
|
|
|
|
|
|
// Send the command buffer and loop waiting for the event
|
|
|
|
graphicsQueue.submit(submitInfo, fence.get());
|
|
|
|
|
|
|
|
int polls = 0;
|
|
|
|
do
|
|
|
|
{
|
|
|
|
result = device->getEventStatus(event.get());
|
|
|
|
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);
|
|
|
|
} 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::runtime_error err)
|
|
|
|
{
|
|
|
|
std::cout << "std::runtime_error: " << err.what() << std::endl;
|
|
|
|
exit(-1);
|
|
|
|
}
|
|
|
|
catch (...)
|
|
|
|
{
|
|
|
|
std::cout << "unknown error\n";
|
|
|
|
exit(-1);
|
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|