Skip to content

Commit

Permalink
Added support for vulkan validation layers
Browse files Browse the repository at this point in the history
  • Loading branch information
shajder committed Jan 24, 2025
1 parent 5b35180 commit 5cb0036
Show file tree
Hide file tree
Showing 9 changed files with 165 additions and 35 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -1191,14 +1191,15 @@ cl_external_memory_handle_type_khr vkToOpenCLExternalMemoryHandleType(
{
switch (vkExternalMemoryHandleType)
{
default:
case VULKAN_EXTERNAL_MEMORY_HANDLE_TYPE_NONE: return 0;
case VULKAN_EXTERNAL_MEMORY_HANDLE_TYPE_OPAQUE_FD:
return CL_EXTERNAL_MEMORY_HANDLE_OPAQUE_FD_KHR;
case VULKAN_EXTERNAL_MEMORY_HANDLE_TYPE_OPAQUE_WIN32_NT:
return CL_EXTERNAL_MEMORY_HANDLE_OPAQUE_WIN32_KHR;
case VULKAN_EXTERNAL_MEMORY_HANDLE_TYPE_OPAQUE_WIN32_KMT:
case VULKAN_EXTERNAL_MEMORY_HANDLE_TYPE_OPAQUE_WIN32_NT_KMT:
return CL_EXTERNAL_MEMORY_HANDLE_OPAQUE_WIN32_KMT_KHR;
case VULKAN_EXTERNAL_MEMORY_HANDLE_TYPE_NONE: return 0;
}
return 0;
}
Expand Down
6 changes: 5 additions & 1 deletion test_conformance/common/vulkan_wrapper/vulkan_api_list.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,9 @@
VK_FUNC_DECL(vkGetPhysicalDeviceSurfaceSupportKHR) \
VK_FUNC_DECL(vkImportSemaphoreFdKHR) \
VK_FUNC_DECL(vkGetPhysicalDeviceExternalSemaphorePropertiesKHR) \
VK_FUNC_DECL(vkGetImageSubresourceLayout)
VK_FUNC_DECL(vkGetImageSubresourceLayout) \
VK_FUNC_DECL(vkCreateDebugUtilsMessengerEXT) \
VK_FUNC_DECL(vkDestroyDebugUtilsMessengerEXT)
#define VK_WINDOWS_FUNC_LIST \
VK_FUNC_DECL(vkGetMemoryWin32HandleKHR) \
VK_FUNC_DECL(vkGetSemaphoreWin32HandleKHR) \
Expand Down Expand Up @@ -202,5 +204,7 @@
#define vkGetSemaphoreWin32HandleKHR _vkGetSemaphoreWin32HandleKHR
#define vkImportSemaphoreWin32HandleKHR _vkImportSemaphoreWin32HandleKHR
#define vkGetImageSubresourceLayout _vkGetImageSubresourceLayout
#define vkCreateDebugUtilsMessengerEXT _vkCreateDebugUtilsMessengerEXT
#define vkDestroyDebugUtilsMessengerEXT _vkDestroyDebugUtilsMessengerEXT

#endif //_vulkan_api_list_hpp_
19 changes: 11 additions & 8 deletions test_conformance/common/vulkan_wrapper/vulkan_utility.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -32,13 +32,13 @@
#define BUFFERSIZE 3000


const VulkanInstance &getVulkanInstance()
const VulkanInstance &getVulkanInstance(bool useValidationLayers)
{
static VulkanInstance instance;
static VulkanInstance instance(useValidationLayers);
return instance;
}

const VulkanPhysicalDevice &getVulkanPhysicalDevice()
const VulkanPhysicalDevice &getVulkanPhysicalDevice(bool useValidationLayers)
{
size_t pdIdx = 0;
cl_int errNum = 0;
Expand All @@ -47,7 +47,7 @@ const VulkanPhysicalDevice &getVulkanPhysicalDevice()
cl_uint num_devices = 0;
cl_uint device_no = 0;
const size_t bufsize = BUFFERSIZE;
const VulkanInstance &instance = getVulkanInstance();
const VulkanInstance &instance = getVulkanInstance(useValidationLayers);
const VulkanPhysicalDeviceList &physicalDeviceList =
instance.getPhysicalDeviceList();

Expand Down Expand Up @@ -112,12 +112,13 @@ const VulkanPhysicalDevice &getVulkanPhysicalDevice()
}

const VulkanPhysicalDevice &
getAssociatedVulkanPhysicalDevice(cl_device_id deviceId)
getAssociatedVulkanPhysicalDevice(cl_device_id deviceId,
bool useValidationLayers)
{
size_t pdIdx;
cl_int errNum = 0;
cl_uchar uuid[CL_UUID_SIZE_KHR];
const VulkanInstance &instance = getVulkanInstance();
const VulkanInstance &instance = getVulkanInstance(useValidationLayers);
const VulkanPhysicalDeviceList &physicalDeviceList =
instance.getPhysicalDeviceList();

Expand Down Expand Up @@ -188,10 +189,10 @@ getVulkanMemoryType(const VulkanDevice &device,
return memoryTypeList[mtIdx];
}

bool checkVkSupport()
bool checkVkSupport(bool useValidationLayers)
{
bool result = true;
const VulkanInstance &instance = getVulkanInstance();
const VulkanInstance &instance = getVulkanInstance(useValidationLayers);
const VulkanPhysicalDeviceList &physicalDeviceList =
instance.getPhysicalDeviceList();
if (physicalDeviceList() == NULL)
Expand Down Expand Up @@ -695,6 +696,7 @@ operator<<(std::ostream &os,
{
switch (externalMemoryHandleType)
{
default:
case VULKAN_EXTERNAL_MEMORY_HANDLE_TYPE_NONE: return os << "None";
case VULKAN_EXTERNAL_MEMORY_HANDLE_TYPE_OPAQUE_FD:
return os << "Opaque file descriptor";
Expand All @@ -715,6 +717,7 @@ operator<<(std::ostream &os,
{
switch (externalSemaphoreHandleType)
{
default:
case VULKAN_EXTERNAL_SEMAPHORE_HANDLE_TYPE_NONE: return os << "None";
case VULKAN_EXTERNAL_SEMAPHORE_HANDLE_TYPE_OPAQUE_FD:
return os << "Opaque file descriptor";
Expand Down
10 changes: 6 additions & 4 deletions test_conformance/common/vulkan_wrapper/vulkan_utility.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -30,18 +30,20 @@
#define ROUND_UP(n, multiple) \
(((n) + (multiple)-1) - ((((n) + (multiple)-1)) % (multiple)))

const VulkanInstance& getVulkanInstance();
const VulkanPhysicalDevice& getVulkanPhysicalDevice();
const VulkanInstance& getVulkanInstance(bool useValidationLayers = false);
const VulkanPhysicalDevice&
getAssociatedVulkanPhysicalDevice(cl_device_id deviceId);
getVulkanPhysicalDevice(bool useValidationLayers = false);
const VulkanPhysicalDevice&
getAssociatedVulkanPhysicalDevice(cl_device_id deviceId,
bool useValidationLayers = false);
const VulkanQueueFamily& getVulkanQueueFamily(
const VulkanPhysicalDevice& physicalDevice = getVulkanPhysicalDevice(),
uint32_t queueFlags = VULKAN_QUEUE_FLAG_GRAPHICS
| VULKAN_QUEUE_FLAG_COMPUTE);
const VulkanMemoryType&
getVulkanMemoryType(const VulkanDevice& device,
VulkanMemoryTypeProperty memoryTypeProperty);
bool checkVkSupport();
bool checkVkSupport(bool useValidationLayers = false);
const VulkanQueueFamilyList& getEmptyVulkanQueueFamilyList();
const VulkanDescriptorSetLayoutList& getEmptyVulkanDescriptorSetLayoutList();
const VulkanQueueFamilyToQueueCountMap&
Expand Down
102 changes: 98 additions & 4 deletions test_conformance/common/vulkan_wrapper/vulkan_wrapper.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -48,16 +48,38 @@ VK_WINDOWS_FUNC_LIST

#define CHECK_VK(call) \
if (call != VK_SUCCESS) return call;

static VKAPI_ATTR VkBool32 VKAPI_CALL logCallback(
VkDebugUtilsMessageSeverityFlagBitsEXT messageSeverity,
VkDebugUtilsMessageTypeFlagsEXT messageType,
const VkDebugUtilsMessengerCallbackDataEXT *pCallbackData, void *pUserData)
{
switch (messageSeverity)
{
case VK_DEBUG_UTILS_MESSAGE_SEVERITY_ERROR_BIT_EXT:
log_error("Vulkan validation layer: %s\n", pCallbackData->pMessage);
break;
default:
log_info("Vulkan validation layer: %s\n", pCallbackData->pMessage);
break;
}

return VK_FALSE;
}

///////////////////////////////////
// VulkanInstance implementation //
///////////////////////////////////

VulkanInstance::VulkanInstance(const VulkanInstance &instance)
: m_vkInstance(instance.m_vkInstance),
m_physicalDeviceList(instance.m_physicalDeviceList)
m_physicalDeviceList(instance.m_physicalDeviceList),
m_useValidationLayers(instance.m_useValidationLayers),
m_validationLayers(instance.m_validationLayers)
{}

VulkanInstance::VulkanInstance(): m_vkInstance(VK_NULL_HANDLE)
VulkanInstance::VulkanInstance(bool useValidationLayers)
: m_vkInstance(VK_NULL_HANDLE), m_useValidationLayers(useValidationLayers)
{
#if defined(__linux__) && !defined(__ANDROID__)
char *glibcVersion = strdup(gnu_get_libc_version());
Expand Down Expand Up @@ -130,6 +152,25 @@ VulkanInstance::VulkanInstance(): m_vkInstance(VK_NULL_HANDLE)
VK_GET_NULL_INSTANCE_PROC_ADDR(vkCreateInstance);
#undef VK_GET_NULL_INSTANCE_PROC_ADDR

if (m_useValidationLayers)
{
uint32_t layerCount = 0;
vkEnumerateInstanceLayerProperties(&layerCount, nullptr);

std::vector<VkLayerProperties> layers(layerCount);
vkEnumerateInstanceLayerProperties(&layerCount, layers.data());

bool found = false;
for (const char *layerName : m_validationLayers)
for (const auto &layerProps : layers)
if (strcmp(layerName, layerProps.layerName) == 0)
{
found = true;
break;
}
m_useValidationLayers = found;
}

VkApplicationInfo vkApplicationInfo = {};
vkApplicationInfo.sType = VK_STRUCTURE_TYPE_APPLICATION_INFO;
vkApplicationInfo.pNext = NULL;
Expand All @@ -147,6 +188,9 @@ VulkanInstance::VulkanInstance(): m_vkInstance(VK_NULL_HANDLE)
enabledExtensionNameList.push_back(
VK_KHR_EXTERNAL_SEMAPHORE_CAPABILITIES_EXTENSION_NAME);

if (m_useValidationLayers)
enabledExtensionNameList.push_back(VK_EXT_DEBUG_UTILS_EXTENSION_NAME);

std::vector<VkExtensionProperties> vkExtensionPropertiesList(
instanceExtensionPropertiesCount);
vkEnumerateInstanceExtensionProperties(NULL,
Expand Down Expand Up @@ -174,18 +218,57 @@ VulkanInstance::VulkanInstance(): m_vkInstance(VK_NULL_HANDLE)

VkInstanceCreateInfo vkInstanceCreateInfo = {};
vkInstanceCreateInfo.sType = VK_STRUCTURE_TYPE_INSTANCE_CREATE_INFO;
vkInstanceCreateInfo.pNext = NULL;
vkInstanceCreateInfo.flags = 0;
vkInstanceCreateInfo.pApplicationInfo = &vkApplicationInfo;
vkInstanceCreateInfo.enabledLayerCount = 0;
vkInstanceCreateInfo.ppEnabledLayerNames = NULL;
vkInstanceCreateInfo.enabledExtensionCount =
(uint32_t)enabledExtensionNameList.size();
vkInstanceCreateInfo.ppEnabledExtensionNames =
enabledExtensionNameList.data();
vkInstanceCreateInfo.enabledLayerCount = 0;
vkInstanceCreateInfo.pNext = NULL;

VkDebugUtilsMessengerCreateInfoEXT debugCreateInfo{};
if (m_useValidationLayers)
{
vkInstanceCreateInfo.enabledLayerCount =
static_cast<uint32_t>(m_validationLayers.size());
vkInstanceCreateInfo.ppEnabledLayerNames = m_validationLayers.data();

debugCreateInfo.messageType =
VK_DEBUG_UTILS_MESSAGE_TYPE_GENERAL_BIT_EXT
| VK_DEBUG_UTILS_MESSAGE_TYPE_VALIDATION_BIT_EXT
| VK_DEBUG_UTILS_MESSAGE_TYPE_PERFORMANCE_BIT_EXT;

// VK_DEBUG_UTILS_MESSAGE_SEVERITY_VERBOSE_BIT_EXT
// VK_DEBUG_UTILS_MESSAGE_SEVERITY_INFO_BIT_EXT
// VK_DEBUG_UTILS_MESSAGE_SEVERITY_WARNING_BIT_EXT
// VK_DEBUG_UTILS_MESSAGE_SEVERITY_ERROR_BIT_EXT
debugCreateInfo.messageSeverity |=
VK_DEBUG_UTILS_MESSAGE_SEVERITY_ERROR_BIT_EXT;
debugCreateInfo.sType =
VK_STRUCTURE_TYPE_DEBUG_UTILS_MESSENGER_CREATE_INFO_EXT;
debugCreateInfo.messageSeverity =
VK_DEBUG_UTILS_MESSAGE_SEVERITY_WARNING_BIT_EXT
| VK_DEBUG_UTILS_MESSAGE_SEVERITY_ERROR_BIT_EXT;
debugCreateInfo.pfnUserCallback = logCallback;

vkInstanceCreateInfo.pNext =
(VkDebugUtilsMessengerCreateInfoEXT *)&debugCreateInfo;
}

vkCreateInstance(&vkInstanceCreateInfo, NULL, &m_vkInstance);

if (m_useValidationLayers)
{
_vkCreateDebugUtilsMessengerEXT =
(PFN_vkCreateDebugUtilsMessengerEXT)vkGetInstanceProcAddr(
m_vkInstance, "vkCreateDebugUtilsMessengerEXT");
if (_vkCreateDebugUtilsMessengerEXT != nullptr)
vkCreateDebugUtilsMessengerEXT(m_vkInstance, &debugCreateInfo,
nullptr, &m_debugMessenger);
}

#define VK_FUNC_DECL(name) \
_##name = (PFN_##name)vkGetInstanceProcAddr(m_vkInstance, #name); \
// ASSERT_NEQ((unsigned long long)name, 0ULL) << "Couldn't obtain address
Expand Down Expand Up @@ -228,6 +311,17 @@ VulkanInstance::~VulkanInstance()
m_physicalDeviceList[pdIdx];
delete &physicalDevice;
}

_vkDestroyDebugUtilsMessengerEXT =
(PFN_vkDestroyDebugUtilsMessengerEXT)vkGetInstanceProcAddr(
m_vkInstance, "vkDestroyDebugUtilsMessengerEXT");

if (_vkDestroyDebugUtilsMessengerEXT != nullptr)
{
vkDestroyDebugUtilsMessengerEXT(m_vkInstance, m_debugMessenger,
nullptr);
}

if (m_vkInstance)
{
vkDestroyInstance(m_vkInstance, NULL);
Expand Down
9 changes: 8 additions & 1 deletion test_conformance/common/vulkan_wrapper/vulkan_wrapper.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -30,9 +30,16 @@ class VulkanInstance {
protected:
VkInstance m_vkInstance;
VulkanPhysicalDeviceList m_physicalDeviceList;
VkDebugUtilsMessengerEXT m_debugMessenger;
bool m_useValidationLayers;
const std::vector<const char *> m_validationLayers = {
"VK_LAYER_KHRONOS_validation",
};

VulkanInstance();
VulkanInstance(const VulkanInstance &);

public:
VulkanInstance(bool useValidationLayers = false);
virtual ~VulkanInstance();

public:
Expand Down
Loading

0 comments on commit 5cb0036

Please sign in to comment.