From 24ecd1fa2f37f3c4949989b53562e8f85833a8f6 Mon Sep 17 00:00:00 2001 From: Yong He Date: Fri, 2 Oct 2020 12:53:29 -0700 Subject: Use new vulkan debug layer. (#1566) * Use new vulkan debug layer. * Try use VK_LAYER_KHRONOS_validation when it exists. Co-authored-by: Tim Foley --- tools/gfx/vulkan/render-vk.cpp | 49 +++++++++++++++++++++++++++++++++++++++--- tools/gfx/vulkan/vk-api.h | 1 + 2 files changed, 47 insertions(+), 3 deletions(-) diff --git a/tools/gfx/vulkan/render-vk.cpp b/tools/gfx/vulkan/render-vk.cpp index 28567a3b8..b312d4b23 100644 --- a/tools/gfx/vulkan/render-vk.cpp +++ b/tools/gfx/vulkan/render-vk.cpp @@ -944,9 +944,52 @@ SlangResult VKRenderer::initialize(const Desc& desc, void* inWindowHandle) instanceCreateInfo.ppEnabledExtensionNames = &instanceExtensions[0]; #if ENABLE_VALIDATION_LAYER - const char* layerNames[] = { "VK_LAYER_LUNARG_standard_validation" }; - instanceCreateInfo.enabledLayerCount = SLANG_COUNT_OF(layerNames); - instanceCreateInfo.ppEnabledLayerNames = layerNames; + // Depending on driver version, validation layer may or may not exist. + // Newer drivers comes with "VK_LAYER_KHRONOS_validation", while older + // drivers provide only the deprecated + // "VK_LAYER_LUNARG_standard_validation" layer. + // We will check what layers are available, and use the newer + // "VK_LAYER_KHRONOS_validation" layer when possible. + uint32_t layerCount; + m_api.vkEnumerateInstanceLayerProperties(&layerCount, nullptr); + + List availableLayers; + availableLayers.setCount(layerCount); + m_api.vkEnumerateInstanceLayerProperties(&layerCount, availableLayers.getBuffer()); + + const char* layerNames[] = { nullptr }; + for (auto& layer : availableLayers) + { + if (strncmp( + layer.layerName, + "VK_LAYER_KHRONOS_validation", + sizeof("VK_LAYER_KHRONOS_validation")) == 0) + { + layerNames[0] = "VK_LAYER_KHRONOS_validation"; + break; + } + } + // On older drivers, only "VK_LAYER_LUNARG_standard_validation" exists, + // so we try to use it if we can't find "VK_LAYER_KHRONOS_validation". + if (!layerNames[0]) + { + for (auto& layer : availableLayers) + { + if (strncmp( + layer.layerName, + "VK_LAYER_LUNARG_standard_validation", + sizeof("VK_LAYER_LUNARG_standard_validation")) == 0) + { + layerNames[0] = "VK_LAYER_LUNARG_standard_validation"; + break; + } + } + } + if (layerNames[0]) + { + instanceCreateInfo.enabledLayerCount = SLANG_COUNT_OF(layerNames); + instanceCreateInfo.ppEnabledLayerNames = layerNames; + } #endif SLANG_VK_RETURN_ON_FAIL(m_api.vkCreateInstance(&instanceCreateInfo, nullptr, &instance)); diff --git a/tools/gfx/vulkan/vk-api.h b/tools/gfx/vulkan/vk-api.h index 58bcc89fd..3fb4b3909 100644 --- a/tools/gfx/vulkan/vk-api.h +++ b/tools/gfx/vulkan/vk-api.h @@ -8,6 +8,7 @@ namespace gfx { #define VK_API_GLOBAL_PROCS(x) \ x(vkGetInstanceProcAddr) \ x(vkCreateInstance) \ + x(vkEnumerateInstanceLayerProperties) \ /* */ #define VK_API_INSTANCE_PROCS_OPT(x) \ -- cgit v1.2.3