Add samples DynamicUniform, EnableValidationWithCallback, EnumerateDevicesAdvanced, Events (#313)

+ slightly adjust some other samples.
This commit is contained in:
Andreas Süßenbach
2019-04-01 10:06:49 +02:00
committed by Markus Tavenrath
parent bcc02a1cb0
commit f7ec6041b3
13 changed files with 804 additions and 19 deletions

View File

@@ -0,0 +1,44 @@
# 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.
cmake_minimum_required(VERSION 3.2)
project(Events)
set(HEADERS
../utils/geometries.hpp
../utils/math.hpp
../utils/shaders.hpp
../utils/utils.hpp
)
set(SOURCES
Events.cpp
../utils/math.cpp
../utils/shaders.cpp
../utils/utils.cpp
)
source_group(headers FILES ${HEADERS})
source_group(sources FILES ${SOURCES})
add_executable(Events
${HEADERS}
${SOURCES}
)
set_target_properties(Events PROPERTIES FOLDER "Samples")
target_include_directories(Events PUBLIC ${CMAKE_SOURCE_DIR}/glslang)
target_link_libraries(Events PUBLIC glslang SPIRV "$ENV{VULKAN_SDK}/Lib/vulkan-1.lib"
)

156
samples/Events/Events.cpp Normal file
View File

@@ -0,0 +1,156 @@
// 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/geometries.hpp"
#include "../utils/math.hpp"
#include "../utils/shaders.hpp"
#include "../utils/utils.hpp"
#include "vulkan/vulkan.hpp"
#include "SPIRV/GlslangToSpv.h"
#include <iostream>
static char const* AppName = "Events";
static char const* EngineName = "Vulkan.hpp";
int main(int /*argc*/, char ** /*argv*/)
{
try
{
vk::UniqueInstance instance = vk::su::createInstance(AppName, EngineName, vk::su::getInstanceExtensions());
#if !defined(NDEBUG)
vk::UniqueDebugReportCallbackEXT debugReportCallback = vk::su::createDebugReportCallback(instance);
#endif
std::vector<vk::PhysicalDevice> physicalDevices = instance->enumeratePhysicalDevices();
assert(!physicalDevices.empty());
uint32_t graphicsQueueFamilyIndex = vk::su::findGraphicsQueueFamilyIndex(physicalDevices[0].getQueueFamilyProperties());
vk::UniqueDevice device = vk::su::createDevice(physicalDevices[0], graphicsQueueFamilyIndex);
vk::UniqueCommandPool commandPool = vk::su::createCommandPool(device, graphicsQueueFamilyIndex);
std::vector<vk::UniqueCommandBuffer> commandBuffers = device->allocateCommandBuffersUnique(vk::CommandBufferAllocateInfo(commandPool.get(), vk::CommandBufferLevel::ePrimary, 1));
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
commandBuffers[0]->begin(vk::CommandBufferBeginInfo(vk::CommandBufferUsageFlags()));
commandBuffers[0]->setViewport(0, vk::Viewport(0.0f, 0.0f, 10.0f, 10.0f, 0.0f, 1.0f));
commandBuffers[0]->end();
vk::UniqueFence fence = device->createFenceUnique(vk::FenceCreateInfo());
vk::PipelineStageFlags waitDestinationStageMask(vk::PipelineStageFlagBits::eColorAttachmentOutput);
vk::SubmitInfo submitInfo(0, nullptr, &waitDestinationStageMask, 1, &commandBuffers[0].get());
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()));
commandBuffers[0]->reset(vk::CommandBufferResetFlags());
commandBuffers[0]->begin(vk::CommandBufferBeginInfo());
commandBuffers[0]->waitEvents(event.get(), vk::PipelineStageFlagBits::eHost, vk::PipelineStageFlagBits::eBottomOfPipe, nullptr, nullptr, nullptr);
commandBuffers[0]->end();
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.
// Execute the code from vkQueueSubmit through vkSetEvent without breakpoints
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)
{
std::cout << "Didn't get expected timeout in vkWaitForFences, exiting\n";
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);
commandBuffers[0]->reset({});
device->resetFences(fence.get());
device->resetEvent(event.get());
// Now set the event from the GPU and wait on the CPU
commandBuffers[0]->begin(vk::CommandBufferBeginInfo());
commandBuffers[0]->setEvent(event.get(), vk::PipelineStageFlagBits::eBottomOfPipe);
commandBuffers[0]->end();
// Look for the event on the CPU. It should be RESET since we haven't sent the command buffer yet.
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;
}