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 : EnableValidationWithCallback
|
|
|
|
// Show how to enable validation layers and provide callback
|
|
|
|
|
2020-09-30 10:00:32 +00:00
|
|
|
#if defined( _MSC_VER )
|
|
|
|
// no need to ignore any warnings with MSVC
|
|
|
|
#elif defined( __clang__ )
|
|
|
|
# pragma clang diagnostic ignored "-Wunused-variable"
|
|
|
|
#elif defined( __GNUC__ )
|
|
|
|
# pragma GCC diagnostic ignored "-Wunused-but-set-variable"
|
|
|
|
#else
|
|
|
|
// unknow compiler... just ignore the warnings for yourselves ;)
|
|
|
|
#endif
|
|
|
|
|
2019-04-01 08:06:49 +00:00
|
|
|
#include "../utils/utils.hpp"
|
2020-04-12 19:49:12 +00:00
|
|
|
|
2019-04-01 08:06:49 +00:00
|
|
|
#include <algorithm>
|
|
|
|
#include <iostream>
|
|
|
|
#include <sstream>
|
2022-07-26 09:13:43 +00:00
|
|
|
#include <vulkan/vulkan.hpp>
|
|
|
|
#include <vulkan/vulkan_to_string.hpp>
|
2019-04-01 08:06:49 +00:00
|
|
|
|
2020-04-12 19:49:12 +00:00
|
|
|
static char const * AppName = "EnableValidationWithCallback";
|
|
|
|
static char const * EngineName = "Vulkan.hpp";
|
2019-04-01 08:06:49 +00:00
|
|
|
|
2020-04-12 19:49:12 +00:00
|
|
|
PFN_vkCreateDebugUtilsMessengerEXT pfnVkCreateDebugUtilsMessengerEXT;
|
2019-09-25 09:56:46 +00:00
|
|
|
PFN_vkDestroyDebugUtilsMessengerEXT pfnVkDestroyDebugUtilsMessengerEXT;
|
2019-04-01 08:06:49 +00:00
|
|
|
|
2020-04-12 19:49:12 +00:00
|
|
|
VKAPI_ATTR VkResult VKAPI_CALL vkCreateDebugUtilsMessengerEXT( VkInstance instance,
|
|
|
|
const VkDebugUtilsMessengerCreateInfoEXT * pCreateInfo,
|
|
|
|
const VkAllocationCallbacks * pAllocator,
|
|
|
|
VkDebugUtilsMessengerEXT * pMessenger )
|
2019-04-01 08:06:49 +00:00
|
|
|
{
|
2020-04-12 19:49:12 +00:00
|
|
|
return pfnVkCreateDebugUtilsMessengerEXT( instance, pCreateInfo, pAllocator, pMessenger );
|
2019-04-01 08:06:49 +00:00
|
|
|
}
|
|
|
|
|
2022-02-28 09:11:04 +00:00
|
|
|
VKAPI_ATTR void VKAPI_CALL vkDestroyDebugUtilsMessengerEXT( VkInstance instance, VkDebugUtilsMessengerEXT messenger, VkAllocationCallbacks const * pAllocator )
|
2019-04-01 08:06:49 +00:00
|
|
|
{
|
2020-04-12 19:49:12 +00:00
|
|
|
return pfnVkDestroyDebugUtilsMessengerEXT( instance, messenger, pAllocator );
|
2019-04-01 08:06:49 +00:00
|
|
|
}
|
|
|
|
|
2020-04-12 19:49:12 +00:00
|
|
|
VKAPI_ATTR VkBool32 VKAPI_CALL debugMessageFunc( VkDebugUtilsMessageSeverityFlagBitsEXT messageSeverity,
|
|
|
|
VkDebugUtilsMessageTypeFlagsEXT messageTypes,
|
|
|
|
VkDebugUtilsMessengerCallbackDataEXT const * pCallbackData,
|
|
|
|
void * /*pUserData*/ )
|
2019-04-01 08:06:49 +00:00
|
|
|
{
|
2019-09-26 07:55:15 +00:00
|
|
|
std::string message;
|
2019-04-01 08:06:49 +00:00
|
|
|
|
2022-07-26 09:13:43 +00:00
|
|
|
message += vk::to_string( static_cast<vk::DebugUtilsMessageSeverityFlagBitsEXT>( messageSeverity ) ) + ": " +
|
|
|
|
vk::to_string( static_cast<vk::DebugUtilsMessageTypeFlagsEXT>( messageTypes ) ) + ":\n";
|
2020-04-12 19:49:12 +00:00
|
|
|
message += std::string( "\t" ) + "messageIDName = <" + pCallbackData->pMessageIdName + ">\n";
|
|
|
|
message += std::string( "\t" ) + "messageIdNumber = " + std::to_string( pCallbackData->messageIdNumber ) + "\n";
|
|
|
|
message += std::string( "\t" ) + "message = <" + pCallbackData->pMessage + ">\n";
|
|
|
|
if ( 0 < pCallbackData->queueLabelCount )
|
2019-04-01 08:06:49 +00:00
|
|
|
{
|
2020-04-12 19:49:12 +00:00
|
|
|
message += std::string( "\t" ) + "Queue Labels:\n";
|
2022-02-09 07:32:42 +00:00
|
|
|
for ( uint32_t i = 0; i < pCallbackData->queueLabelCount; i++ )
|
2019-09-25 09:56:46 +00:00
|
|
|
{
|
2020-04-12 19:49:12 +00:00
|
|
|
message += std::string( "\t\t" ) + "labelName = <" + pCallbackData->pQueueLabels[i].pLabelName + ">\n";
|
2019-09-25 09:56:46 +00:00
|
|
|
}
|
|
|
|
}
|
2020-04-12 19:49:12 +00:00
|
|
|
if ( 0 < pCallbackData->cmdBufLabelCount )
|
2019-09-25 09:56:46 +00:00
|
|
|
{
|
2020-04-12 19:49:12 +00:00
|
|
|
message += std::string( "\t" ) + "CommandBuffer Labels:\n";
|
2022-02-09 07:32:42 +00:00
|
|
|
for ( uint32_t i = 0; i < pCallbackData->cmdBufLabelCount; i++ )
|
2019-09-25 09:56:46 +00:00
|
|
|
{
|
2020-04-12 19:49:12 +00:00
|
|
|
message += std::string( "\t\t" ) + "labelName = <" + pCallbackData->pCmdBufLabels[i].pLabelName + ">\n";
|
2019-09-25 09:56:46 +00:00
|
|
|
}
|
|
|
|
}
|
2020-04-12 19:49:12 +00:00
|
|
|
if ( 0 < pCallbackData->objectCount )
|
2019-09-25 09:56:46 +00:00
|
|
|
{
|
2022-02-09 07:32:42 +00:00
|
|
|
for ( uint32_t i = 0; i < pCallbackData->objectCount; i++ )
|
2019-09-25 09:56:46 +00:00
|
|
|
{
|
2020-04-12 19:49:12 +00:00
|
|
|
message += std::string( "\t" ) + "Object " + std::to_string( i ) + "\n";
|
2022-07-26 09:13:43 +00:00
|
|
|
message += std::string( "\t\t" ) + "objectType = " + vk::to_string( static_cast<vk::ObjectType>( pCallbackData->pObjects[i].objectType ) ) + "\n";
|
2022-02-28 09:11:04 +00:00
|
|
|
message += std::string( "\t\t" ) + "objectHandle = " + std::to_string( pCallbackData->pObjects[i].objectHandle ) + "\n";
|
2020-04-12 19:49:12 +00:00
|
|
|
if ( pCallbackData->pObjects[i].pObjectName )
|
2019-09-25 09:56:46 +00:00
|
|
|
{
|
2020-04-12 19:49:12 +00:00
|
|
|
message += std::string( "\t\t" ) + "objectName = <" + pCallbackData->pObjects[i].pObjectName + ">\n";
|
2019-09-25 09:56:46 +00:00
|
|
|
}
|
2020-04-12 19:49:12 +00:00
|
|
|
}
|
2019-04-01 08:06:49 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
#ifdef _WIN32
|
2020-04-12 19:49:12 +00:00
|
|
|
MessageBox( NULL, message.c_str(), "Alert", MB_OK );
|
2019-04-01 08:06:49 +00:00
|
|
|
#else
|
2020-01-06 11:16:35 +00:00
|
|
|
std::cout << message << std::endl;
|
2019-04-01 08:06:49 +00:00
|
|
|
#endif
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2020-04-12 19:49:12 +00:00
|
|
|
bool checkLayers( std::vector<char const *> const & layers, std::vector<vk::LayerProperties> const & properties )
|
2019-04-01 08:06:49 +00:00
|
|
|
{
|
|
|
|
// return true if all layers are listed in the properties
|
2022-02-28 09:11:04 +00:00
|
|
|
return std::all_of( layers.begin(),
|
|
|
|
layers.end(),
|
|
|
|
[&properties]( char const * name )
|
|
|
|
{
|
|
|
|
return std::find_if( properties.begin(),
|
|
|
|
properties.end(),
|
|
|
|
[&name]( vk::LayerProperties const & property )
|
|
|
|
{ return strcmp( property.layerName, name ) == 0; } ) != properties.end();
|
|
|
|
} );
|
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
|
|
|
|
{
|
2020-04-12 19:49:12 +00:00
|
|
|
#if ( VULKAN_HPP_DISPATCH_LOADER_DYNAMIC == 1 )
|
2020-01-28 09:16:10 +00:00
|
|
|
// initialize the DipatchLoaderDynamic to use
|
2020-04-12 19:49:12 +00:00
|
|
|
static vk::DynamicLoader dl;
|
2022-02-28 09:11:04 +00:00
|
|
|
PFN_vkGetInstanceProcAddr vkGetInstanceProcAddr = dl.getProcAddress<PFN_vkGetInstanceProcAddr>( "vkGetInstanceProcAddr" );
|
2020-04-12 19:49:12 +00:00
|
|
|
VULKAN_HPP_DEFAULT_DISPATCHER.init( vkGetInstanceProcAddr );
|
2020-01-28 09:16:10 +00:00
|
|
|
#endif
|
|
|
|
|
2019-04-01 08:06:49 +00:00
|
|
|
std::vector<vk::LayerProperties> instanceLayerProperties = vk::enumerateInstanceLayerProperties();
|
|
|
|
|
|
|
|
/* VULKAN_KEY_START */
|
|
|
|
|
|
|
|
// Use standard_validation meta layer that enables all recommended validation layers
|
2020-04-12 19:49:12 +00:00
|
|
|
std::vector<char const *> instanceLayerNames;
|
|
|
|
instanceLayerNames.push_back( "VK_LAYER_KHRONOS_validation" );
|
|
|
|
if ( !checkLayers( instanceLayerNames, instanceLayerProperties ) )
|
2019-04-01 08:06:49 +00:00
|
|
|
{
|
2019-04-30 15:03:26 +00:00
|
|
|
std::cout << "Set the environment variable VK_LAYER_PATH to point to the location of your layers" << std::endl;
|
2020-04-12 19:49:12 +00:00
|
|
|
exit( 1 );
|
2019-04-01 08:06:49 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/* Enable debug callback extension */
|
2020-04-12 19:49:12 +00:00
|
|
|
std::vector<char const *> instanceExtensionNames;
|
|
|
|
instanceExtensionNames.push_back( VK_EXT_DEBUG_UTILS_EXTENSION_NAME );
|
|
|
|
|
|
|
|
vk::ApplicationInfo applicationInfo( AppName, 1, EngineName, 1, VK_API_VERSION_1_1 );
|
2022-02-28 09:11:04 +00:00
|
|
|
vk::InstanceCreateInfo instanceCreateInfo( vk::InstanceCreateFlags(), &applicationInfo, instanceLayerNames, instanceExtensionNames );
|
|
|
|
vk::Instance instance = vk::createInstance( instanceCreateInfo );
|
2020-04-12 19:49:12 +00:00
|
|
|
|
|
|
|
#if ( VULKAN_HPP_DISPATCH_LOADER_DYNAMIC == 1 )
|
2020-01-28 09:16:10 +00:00
|
|
|
// initialize function pointers for instance
|
2021-02-17 09:49:59 +00:00
|
|
|
VULKAN_HPP_DEFAULT_DISPATCHER.init( instance );
|
2020-01-28 09:16:10 +00:00
|
|
|
#endif
|
|
|
|
|
2022-02-28 09:11:04 +00:00
|
|
|
pfnVkCreateDebugUtilsMessengerEXT = reinterpret_cast<PFN_vkCreateDebugUtilsMessengerEXT>( instance.getProcAddr( "vkCreateDebugUtilsMessengerEXT" ) );
|
2020-04-12 19:49:12 +00:00
|
|
|
if ( !pfnVkCreateDebugUtilsMessengerEXT )
|
2019-09-25 09:56:46 +00:00
|
|
|
{
|
|
|
|
std::cout << "GetInstanceProcAddr: Unable to find pfnVkCreateDebugUtilsMessengerEXT function." << std::endl;
|
2020-04-12 19:49:12 +00:00
|
|
|
exit( 1 );
|
2019-09-25 09:56:46 +00:00
|
|
|
}
|
|
|
|
|
2022-02-28 09:11:04 +00:00
|
|
|
pfnVkDestroyDebugUtilsMessengerEXT = reinterpret_cast<PFN_vkDestroyDebugUtilsMessengerEXT>( instance.getProcAddr( "vkDestroyDebugUtilsMessengerEXT" ) );
|
2020-04-12 19:49:12 +00:00
|
|
|
if ( !pfnVkDestroyDebugUtilsMessengerEXT )
|
2019-09-25 09:56:46 +00:00
|
|
|
{
|
|
|
|
std::cout << "GetInstanceProcAddr: Unable to find pfnVkDestroyDebugUtilsMessengerEXT function." << std::endl;
|
2020-04-12 19:49:12 +00:00
|
|
|
exit( 1 );
|
2019-09-25 09:56:46 +00:00
|
|
|
}
|
|
|
|
|
2020-04-12 19:49:12 +00:00
|
|
|
vk::DebugUtilsMessageSeverityFlagsEXT severityFlags( vk::DebugUtilsMessageSeverityFlagBitsEXT::eWarning |
|
|
|
|
vk::DebugUtilsMessageSeverityFlagBitsEXT::eError );
|
2022-02-28 09:11:04 +00:00
|
|
|
vk::DebugUtilsMessageTypeFlagsEXT messageTypeFlags( vk::DebugUtilsMessageTypeFlagBitsEXT::eGeneral | vk::DebugUtilsMessageTypeFlagBitsEXT::ePerformance |
|
2020-04-12 19:49:12 +00:00
|
|
|
vk::DebugUtilsMessageTypeFlagBitsEXT::eValidation );
|
2022-02-28 09:11:04 +00:00
|
|
|
vk::DebugUtilsMessengerEXT debugUtilsMessenger =
|
|
|
|
instance.createDebugUtilsMessengerEXT( vk::DebugUtilsMessengerCreateInfoEXT( {}, severityFlags, messageTypeFlags, &debugMessageFunc ) );
|
2019-09-25 09:56:46 +00:00
|
|
|
|
2021-02-17 09:49:59 +00:00
|
|
|
vk::PhysicalDevice physicalDevice = instance.enumeratePhysicalDevices().front();
|
2019-04-01 08:06:49 +00:00
|
|
|
|
2021-02-17 09:49:59 +00:00
|
|
|
// get the index of the first queue family that supports graphics
|
2022-02-28 09:11:04 +00:00
|
|
|
uint32_t graphicsQueueFamilyIndex = vk::su::findGraphicsQueueFamilyIndex( physicalDevice.getQueueFamilyProperties() );
|
2019-04-01 08:06:49 +00:00
|
|
|
|
2020-04-12 19:49:12 +00:00
|
|
|
float queuePriority = 0.0f;
|
2022-02-28 09:11:04 +00:00
|
|
|
vk::DeviceQueueCreateInfo deviceQueueCreateInfo( vk::DeviceQueueCreateFlags(), graphicsQueueFamilyIndex, 1, &queuePriority );
|
|
|
|
vk::Device device = physicalDevice.createDevice( vk::DeviceCreateInfo( vk::DeviceCreateFlags(), deviceQueueCreateInfo ) );
|
2019-04-01 08:06:49 +00:00
|
|
|
|
2021-02-17 09:49:59 +00:00
|
|
|
// Create a CommandPool and don't destroy it, for testing purposes!
|
2022-02-28 09:11:04 +00:00
|
|
|
vk::CommandPool commandPool = device.createCommandPool( vk::CommandPoolCreateInfo( vk::CommandPoolCreateFlags(), graphicsQueueFamilyIndex ) );
|
2019-04-01 08:06:49 +00:00
|
|
|
|
2021-02-17 09:49:59 +00:00
|
|
|
#if true
|
|
|
|
// The commandPool is not destroyed automatically
|
2019-04-01 08:06:49 +00:00
|
|
|
// That is, the device is destroyed before the commmand pool and will trigger a validation error.
|
2021-02-17 09:49:59 +00:00
|
|
|
std::cout << "*** INTENTIONALLY destroying the Device before destroying a CommandPool ***\n";
|
2019-04-01 08:06:49 +00:00
|
|
|
std::cout << "*** The following error message is EXPECTED ***\n";
|
2021-02-17 09:49:59 +00:00
|
|
|
#else
|
|
|
|
device.destroyCommandPool( commandPool );
|
|
|
|
#endif
|
|
|
|
|
|
|
|
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
|
|
|
|
|
|
|
/* VULKAN_KEY_END */
|
|
|
|
}
|
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;
|
|
|
|
}
|