From: Petr Kraus Date: Tue, 14 Apr 2020 22:29:33 +0000 (+0200) Subject: icd: Make VkPhysicalDevice unique X-Git-Tag: upstream/1.2.179~107 X-Git-Url: http://review.tizen.org/git/?a=commitdiff_plain;h=0d4857ea576af2207b6debc965a242cdcadc1b15;p=platform%2Fupstream%2FVulkan-Tools.git icd: Make VkPhysicalDevice unique --- diff --git a/icd/generated/mock_icd.cpp b/icd/generated/mock_icd.cpp index 3a4f0d6..11f4689 100644 --- a/icd/generated/mock_icd.cpp +++ b/icd/generated/mock_icd.cpp @@ -22,6 +22,7 @@ #include "mock_icd.h" #include #include +#include #include #include "vk_typemap_helper.h" namespace vkmock { @@ -29,18 +30,20 @@ namespace vkmock { using std::unordered_map; +static constexpr uint32_t icd_physical_device_count = 1; +static unordered_map> physical_device_map; + // Map device memory handle to any mapped allocations that we'll need to free on unmap static unordered_map> mapped_memory_map; // Map device memory allocation handle to the size static unordered_map allocated_memory_size_map; -static VkPhysicalDevice physical_device = (VkPhysicalDevice)CreateDispObjHandle(); static unordered_map>> queue_map; static unordered_map> buffer_map; static constexpr uint32_t icd_swapchain_image_count = 1; -static std::unordered_map swapchain_image_map; +static unordered_map swapchain_image_map; // TODO: Would like to codegen this but limits aren't in XML static VkPhysicalDeviceLimits SetLimits(VkPhysicalDeviceLimits *limits) { @@ -184,6 +187,8 @@ static VKAPI_ATTR VkResult VKAPI_CALL CreateInstance( return VK_ERROR_INCOMPATIBLE_DRIVER; } *pInstance = (VkInstance)CreateDispObjHandle(); + for (auto& physical_device : physical_device_map[*pInstance]) + physical_device = (VkPhysicalDevice)CreateDispObjHandle(); // TODO: If emulating specific device caps, will need to add intelligence here return VK_SUCCESS; } @@ -193,7 +198,12 @@ static VKAPI_ATTR void VKAPI_CALL DestroyInstance( const VkAllocationCallbacks* pAllocator) { - DestroyDispObjHandle((void*)instance); + if (instance) { + for (const auto physical_device : physical_device_map.at(instance)) + DestroyDispObjHandle((void*)physical_device); + physical_device_map.erase(instance); + DestroyDispObjHandle((void*)instance); + } } static VKAPI_ATTR VkResult VKAPI_CALL EnumeratePhysicalDevices( @@ -201,12 +211,16 @@ static VKAPI_ATTR VkResult VKAPI_CALL EnumeratePhysicalDevices( uint32_t* pPhysicalDeviceCount, VkPhysicalDevice* pPhysicalDevices) { + VkResult result_code = VK_SUCCESS; if (pPhysicalDevices) { - *pPhysicalDevices = physical_device; + const auto return_count = (std::min)(*pPhysicalDeviceCount, icd_physical_device_count); + for (uint32_t i = 0; i < return_count; ++i) pPhysicalDevices[i] = physical_device_map.at(instance)[i]; + if (return_count < icd_physical_device_count) result_code = VK_INCOMPLETE; + *pPhysicalDeviceCount = return_count; } else { - *pPhysicalDeviceCount = 1; + *pPhysicalDeviceCount = icd_physical_device_count; } - return VK_SUCCESS; + return result_code; } static VKAPI_ATTR void VKAPI_CALL GetPhysicalDeviceFeatures( diff --git a/scripts/mock_icd_generator.py b/scripts/mock_icd_generator.py index 0b1062c..75227b4 100644 --- a/scripts/mock_icd_generator.py +++ b/scripts/mock_icd_generator.py @@ -54,18 +54,20 @@ static void DestroyDispObjHandle(void* handle) { SOURCE_CPP_PREFIX = ''' using std::unordered_map; +static constexpr uint32_t icd_physical_device_count = 1; +static unordered_map> physical_device_map; + // Map device memory handle to any mapped allocations that we'll need to free on unmap static unordered_map> mapped_memory_map; // Map device memory allocation handle to the size static unordered_map allocated_memory_size_map; -static VkPhysicalDevice physical_device = (VkPhysicalDevice)CreateDispObjHandle(); static unordered_map>> queue_map; static unordered_map> buffer_map; static constexpr uint32_t icd_swapchain_image_count = 1; -static std::unordered_map swapchain_image_map; +static unordered_map swapchain_image_map; // TODO: Would like to codegen this but limits aren't in XML static VkPhysicalDeviceLimits SetLimits(VkPhysicalDeviceLimits *limits) { @@ -427,19 +429,30 @@ CUSTOM_C_INTERCEPTS = { return VK_ERROR_INCOMPATIBLE_DRIVER; } *pInstance = (VkInstance)CreateDispObjHandle(); + for (auto& physical_device : physical_device_map[*pInstance]) + physical_device = (VkPhysicalDevice)CreateDispObjHandle(); // TODO: If emulating specific device caps, will need to add intelligence here return VK_SUCCESS; ''', 'vkDestroyInstance': ''' - DestroyDispObjHandle((void*)instance); + if (instance) { + for (const auto physical_device : physical_device_map.at(instance)) + DestroyDispObjHandle((void*)physical_device); + physical_device_map.erase(instance); + DestroyDispObjHandle((void*)instance); + } ''', 'vkEnumeratePhysicalDevices': ''' + VkResult result_code = VK_SUCCESS; if (pPhysicalDevices) { - *pPhysicalDevices = physical_device; + const auto return_count = (std::min)(*pPhysicalDeviceCount, icd_physical_device_count); + for (uint32_t i = 0; i < return_count; ++i) pPhysicalDevices[i] = physical_device_map.at(instance)[i]; + if (return_count < icd_physical_device_count) result_code = VK_INCOMPLETE; + *pPhysicalDeviceCount = return_count; } else { - *pPhysicalDeviceCount = 1; + *pPhysicalDeviceCount = icd_physical_device_count; } - return VK_SUCCESS; + return result_code; ''', 'vkCreateDevice': ''' *pDevice = (VkDevice)CreateDispObjHandle(); @@ -1093,6 +1106,7 @@ class MockICDOutputGenerator(OutputGenerator): write('#include "mock_icd.h"', file=self.outFile) write('#include ', file=self.outFile) write('#include ', file=self.outFile) + write('#include ', file=self.outFile) write('#include ', file=self.outFile) write('#include "vk_typemap_helper.h"', file=self.outFile)