From 8f9d58416934cbf850f0f01e5fefbdfe1b02d4d6 Mon Sep 17 00:00:00 2001 From: skallweitNV <64953474+skallweitNV@users.noreply.github.com> Date: Thu, 3 Nov 2022 16:58:49 +0100 Subject: Add gfxGetAdapters function (currently implemented for D3D12/Vulkan) (#2486) * Add gfxGetAdapters function (currently implemented for D3D12/Vulkan) * Extend to handle DirectX11 and CUDA * Use blob to return adapter list and add AdapterList helper * Replace strncpy with memcpy Co-authored-by: jsmall-nvidia --- tools/gfx/vulkan/vk-helper-functions.cpp | 39 ++++++++++++++++++++++++++++++++ 1 file changed, 39 insertions(+) (limited to 'tools/gfx/vulkan/vk-helper-functions.cpp') diff --git a/tools/gfx/vulkan/vk-helper-functions.cpp b/tools/gfx/vulkan/vk-helper-functions.cpp index aa6c42ec5..da2b55fd2 100644 --- a/tools/gfx/vulkan/vk-helper-functions.cpp +++ b/tools/gfx/vulkan/vk-helper-functions.cpp @@ -2,6 +2,7 @@ #include "vk-helper-functions.h" #include "vk-device.h" +#include "vk-util.h" namespace gfx { @@ -451,6 +452,44 @@ VkImageAspectFlags getAspectMaskFromFormat(VkFormat format) } // namespace vk +Result SLANG_MCALL getVKAdapters(List& outAdapters) +{ + VulkanModule module; + SLANG_RETURN_ON_FAIL(module.init(false)); + VulkanApi api; + SLANG_RETURN_ON_FAIL(api.initGlobalProcs(module)); + + VkInstanceCreateInfo instanceCreateInfo = { VK_STRUCTURE_TYPE_INSTANCE_CREATE_INFO }; + VkInstance instance; + SLANG_VK_RETURN_ON_FAIL(api.vkCreateInstance(&instanceCreateInfo, nullptr, &instance)); + + // This will fail due to not loading any extensions. + api.initInstanceProcs(instance); + // Make sure required functions for enumerating physical devices were loaded. + if (!api.vkEnumeratePhysicalDevices || !api.vkGetPhysicalDeviceProperties) + return SLANG_FAIL; + + uint32_t numPhysicalDevices = 0; + SLANG_VK_RETURN_ON_FAIL(api.vkEnumeratePhysicalDevices(instance, &numPhysicalDevices, nullptr)); + + List physicalDevices; + physicalDevices.setCount(numPhysicalDevices); + SLANG_VK_RETURN_ON_FAIL(api.vkEnumeratePhysicalDevices(instance, &numPhysicalDevices, physicalDevices.getBuffer())); + + for (const auto& physicalDevice : physicalDevices) + { + VkPhysicalDeviceProperties props; + api.vkGetPhysicalDeviceProperties(physicalDevice, &props); + AdapterInfo info = {}; + memcpy(info.name, props.deviceName, Math::Min(strlen(props.deviceName), sizeof(AdapterInfo::name) - 1)); + info.vendorID = props.vendorID; + info.deviceID = props.deviceID; + outAdapters.add(info); + } + + return SLANG_OK; +} + Result SLANG_MCALL createVKDevice(const IDevice::Desc* desc, IDevice** outRenderer) { RefPtr result = new vk::DeviceImpl(); -- cgit v1.2.3