Skip to content

Commit 75be738

Browse files
Speedup tests by putting physical devices in a map
Requires extensive modifications to the tests since previously many tests accessed the physical device array directly.
1 parent 046e658 commit 75be738

13 files changed

+909
-899
lines changed

tests/framework/icd/test_icd.cpp

Lines changed: 71 additions & 56 deletions
Large diffs are not rendered by default.

tests/framework/icd/test_icd.h

Lines changed: 38 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@
3030
#include <array>
3131
#include <filesystem>
3232
#include <ostream>
33+
#include <unordered_map>
3334

3435
#include "util/dispatchable_handle.h"
3536
#include "util/platform_wsi.h"
@@ -130,6 +131,9 @@ struct PhysicalDevice {
130131

131132
PhysicalDevice&& finish() { return std::move(*this); }
132133

134+
// Defines the order this physical device appears in vkEnumeratePhysicalDevices
135+
uint32_t iteration_order = 0;
136+
133137
// Objects created from this physical device
134138
std::vector<VkDevice> device_handles;
135139
std::vector<DeviceCreateInfo> device_create_infos;
@@ -149,9 +153,14 @@ struct PhysicalDevice {
149153
struct PhysicalDeviceGroup {
150154
PhysicalDeviceGroup() {}
151155
PhysicalDeviceGroup(PhysicalDevice const& physical_device) { physical_device_handles.push_back(&physical_device); }
156+
PhysicalDeviceGroup(PhysicalDevice const* physical_device) { physical_device_handles.push_back(physical_device); }
152157
PhysicalDeviceGroup(std::vector<PhysicalDevice*> const& physical_devices) {
153158
physical_device_handles.insert(physical_device_handles.end(), physical_devices.begin(), physical_devices.end());
154159
}
160+
PhysicalDeviceGroup& use_physical_device(PhysicalDevice const* physical_device) {
161+
physical_device_handles.push_back(physical_device);
162+
return *this;
163+
}
155164
PhysicalDeviceGroup& use_physical_device(PhysicalDevice const& physical_device) {
156165
physical_device_handles.push_back(&physical_device);
157166
return *this;
@@ -197,7 +206,31 @@ struct TestICD {
197206
BUILDER_VECTOR(Extension, instance_extensions, instance_extension)
198207
std::vector<Extension> enabled_instance_extensions;
199208

200-
BUILDER_VECTOR_MOVE_ONLY(PhysicalDevice, physical_devices, physical_device);
209+
std::unordered_map<VkPhysicalDevice, PhysicalDevice> physical_devices;
210+
TestICD& add_physical_device(PhysicalDevice&& physical_device) {
211+
physical_device.iteration_order = physical_devices.size();
212+
physical_devices.emplace(physical_device.vk_physical_device.handle, std::move(physical_device));
213+
return *this;
214+
}
215+
216+
PhysicalDevice& add_and_get_physical_device(PhysicalDevice&& physical_device) {
217+
VkPhysicalDevice pd = physical_device.vk_physical_device.handle;
218+
physical_device.iteration_order = physical_devices.size();
219+
physical_devices.emplace(physical_device.vk_physical_device.handle, std::move(physical_device));
220+
return physical_devices.at(pd);
221+
}
222+
223+
PhysicalDevice& add_physical_device_at_index(size_t index, PhysicalDevice&& physical_device) {
224+
VkPhysicalDevice pd = physical_device.vk_physical_device.handle;
225+
physical_device.iteration_order = index;
226+
for (auto& [handle, phys_dev] : physical_devices) {
227+
if (phys_dev.iteration_order >= index) {
228+
phys_dev.iteration_order++;
229+
}
230+
}
231+
physical_devices.emplace(physical_device.vk_physical_device.handle, std::move(physical_device));
232+
return physical_devices.at(pd);
233+
}
201234

202235
BUILDER_VECTOR(PhysicalDeviceGroup, physical_device_groups, physical_device_group);
203236

@@ -237,6 +270,10 @@ struct TestICD {
237270
return info;
238271
}
239272

273+
// Speedup looking for physical devices by not having to iterate through the entire physical_device map to find a particular
274+
// physical device
275+
std::unordered_map<VkDevice, VkPhysicalDevice> device_to_physical_device_map;
276+
240277
#if defined(WIN32)
241278
BUILDER_VALUE(LUID, adapterLUID)
242279
#endif // defined(WIN32)

tests/loader_alloc_callback_tests.cpp

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -714,8 +714,7 @@ TEST(Allocation, CreateInstanceDeviceIntentionalAllocFail) {
714714
.set_library_arch(sizeof(void*) == 8 ? "64" : "32"))
715715
.set_icd_api_version(VK_API_VERSION_1_1)
716716
.add_instance_extension("VK_KHR_get_physical_device_properties2")
717-
.add_physical_device("physical_device_0")
718-
.physical_devices.at(0)
717+
.add_and_get_physical_device("physical_device_0")
719718
.add_queue_family_properties({{VK_QUEUE_GRAPHICS_BIT, 1, 0, {1, 1, 1}}, false})
720719
.add_extensions({"VK_EXT_one", "VK_EXT_two", "VK_EXT_three", "VK_EXT_four", "VK_EXT_five"});
721720
}
@@ -880,7 +879,7 @@ TEST(Allocation, EnumeratePhysicalDevicesIntentionalAllocFail) {
880879
auto& driver = env.reset_icd();
881880

882881
for (uint32_t i = 0; i < physical_dev_count; i++) {
883-
driver.physical_devices.emplace_back(std::string("physical_device_") + std::to_string(i))
882+
driver.add_and_get_physical_device(std::string("physical_device_") + std::to_string(i))
884883
.add_queue_family_properties({{VK_QUEUE_GRAPHICS_BIT, 1, 0, {1, 1, 1}}, false});
885884
}
886885
MemoryTracker tracker{{false, 0, true, fail_index}};
@@ -902,7 +901,7 @@ TEST(Allocation, EnumeratePhysicalDevicesIntentionalAllocFail) {
902901
ASSERT_EQ(physical_dev_count, returned_physical_count);
903902

904903
for (uint32_t i = 0; i < 2; i++) {
905-
driver.physical_devices.emplace_back(std::string("physical_device_") + std::to_string(physical_dev_count))
904+
driver.add_and_get_physical_device(std::string("physical_device_") + std::to_string(physical_dev_count))
906905
.add_queue_family_properties({{VK_QUEUE_GRAPHICS_BIT, 1, 0, {1, 1, 1}}, false});
907906
physical_dev_count += 1;
908907
}
@@ -976,7 +975,7 @@ TEST(Allocation, CreateInstanceDeviceWithDXGIDriverIntentionalAllocFail) {
976975

977976
for (uint32_t i = 0; i < 2; i++) {
978977
auto& driver = env.get_test_icd(i);
979-
driver.physical_devices.emplace_back(std::string("physical_device_") + std::to_string(i))
978+
driver.add_and_get_physical_device(std::string("physical_device_") + std::to_string(i))
980979
.add_queue_family_properties({{VK_QUEUE_GRAPHICS_BIT, 1, 0, {1, 1, 1}}, false});
981980
}
982981

tests/loader_debug_ext_tests.cpp

Lines changed: 15 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -53,8 +53,8 @@ class DebugReportTest : public ::testing::Test {
5353
env = std::unique_ptr<FrameworkEnvironment>(new FrameworkEnvironment());
5454
for (uint32_t icd = 0; icd < 3; ++icd) {
5555
env->add_icd(TestICDDetails(TEST_ICD_PATH_VERSION_2_EXPORT_ICD_GPDPA, VK_API_VERSION_1_0));
56-
env->get_test_icd(icd).physical_devices.push_back({});
57-
env->get_test_icd(icd).physical_devices.push_back({});
56+
env->get_test_icd(icd).add_physical_device({});
57+
env->get_test_icd(icd).add_physical_device({});
5858
}
5959
// Initialize the expected output
6060
allow_any_message = false;
@@ -387,8 +387,8 @@ class DebugUtilTest : public ::testing::Test {
387387
env = std::unique_ptr<FrameworkEnvironment>(new FrameworkEnvironment());
388388
for (uint32_t icd = 0; icd < 3; ++icd) {
389389
env->add_icd(TestICDDetails(TEST_ICD_PATH_VERSION_2_EXPORT_ICD_GPDPA, VK_API_VERSION_1_0));
390-
env->get_test_icd(icd).physical_devices.push_back({});
391-
env->get_test_icd(icd).physical_devices.push_back({});
390+
env->get_test_icd(icd).add_physical_device({});
391+
env->get_test_icd(icd).add_physical_device({});
392392
}
393393
// Initialize the expected output
394394
allow_any_message = false;
@@ -1041,9 +1041,8 @@ void CheckDeviceFunctions(FrameworkEnvironment& env, bool use_GIPA, bool enable_
10411041

10421042
TEST(GetProcAddr, DebugFuncsWithTerminator) {
10431043
FrameworkEnvironment env{};
1044-
auto& driver =
1045-
env.add_icd(TestICDDetails(TEST_ICD_PATH_VERSION_2_EXPORT_ICD_GPDPA)).setup_WSI().add_physical_device("physical_device_0");
1046-
driver.physical_devices.at(0).add_extensions({"VK_KHR_swapchain"});
1044+
auto& driver = env.add_icd(TestICDDetails(TEST_ICD_PATH_VERSION_2_EXPORT_ICD_GPDPA)).setup_WSI();
1045+
auto& phys_dev = driver.add_and_get_physical_device("physical_device_0").add_extensions({"VK_KHR_swapchain"});
10471046
// Hardware doesn't support the debug extensions
10481047

10491048
// Use getDeviceProcAddr & vary enabling the debug extensions
@@ -1056,7 +1055,7 @@ TEST(GetProcAddr, DebugFuncsWithTerminator) {
10561055

10571056
// Now set the hardware to support the extensions and run the situations again
10581057
driver.add_instance_extensions({"VK_EXT_debug_utils", "VK_EXT_debug_report"});
1059-
driver.physical_devices.at(0).add_extensions({"VK_EXT_debug_marker"});
1058+
phys_dev.add_extensions({"VK_EXT_debug_marker"});
10601059

10611060
// Use getDeviceProcAddr & vary enabling the debug extensions
10621061
ASSERT_NO_FATAL_FAILURE(CheckDeviceFunctions(env, false, false, true));
@@ -1069,9 +1068,10 @@ TEST(GetProcAddr, DebugFuncsWithTerminator) {
10691068

10701069
TEST(GetProcAddr, DebugFuncsWithTrampoline) {
10711070
FrameworkEnvironment env{};
1072-
auto& driver =
1073-
env.add_icd(TestICDDetails(TEST_ICD_PATH_VERSION_2_EXPORT_ICD_GPDPA)).setup_WSI().add_physical_device("physical_device_0");
1074-
driver.physical_devices.at(0).add_extensions({"VK_KHR_swapchain"});
1071+
auto& driver = env.add_icd(TestICDDetails(TEST_ICD_PATH_VERSION_2_EXPORT_ICD_GPDPA))
1072+
.setup_WSI()
1073+
.add_and_get_physical_device("physical_device_0")
1074+
.add_extensions({"VK_KHR_swapchain"});
10751075
// Hardware doesn't support the debug extensions
10761076

10771077
// Use getDeviceProcAddr & vary enabling the debug extensions
@@ -1103,9 +1103,10 @@ TEST(GetProcAddr, DebugFuncsWithTrampoline) {
11031103

11041104
TEST(GetProcAddr, DebugFuncsWithDebugExtsForceAdded) {
11051105
FrameworkEnvironment env{};
1106-
auto& driver =
1107-
env.add_icd(TestICDDetails(TEST_ICD_PATH_VERSION_2_EXPORT_ICD_GPDPA)).setup_WSI().add_physical_device("physical_device_0");
1108-
driver.physical_devices.at(0).add_extensions({"VK_KHR_swapchain"});
1106+
auto& driver = env.add_icd(TestICDDetails(TEST_ICD_PATH_VERSION_2_EXPORT_ICD_GPDPA))
1107+
.setup_WSI()
1108+
.add_and_get_physical_device("physical_device_0")
1109+
.add_extensions({"VK_KHR_swapchain"});
11091110
// Hardware doesn't support the debug extensions
11101111

11111112
// Use getDeviceProcAddr & vary enabling the debug extensions

tests/loader_envvar_tests.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -256,7 +256,7 @@ TEST(EnvVarICDOverrideSetup, XDGContainsJsonFile) {
256256
TEST(EnvVarICDOverrideSetup, TestOnlyAddDriverEnvVar) {
257257
FrameworkEnvironment env{};
258258
env.add_icd(TestICDDetails(TEST_ICD_PATH_EXPORT_NONE).set_discovery_type(ManifestDiscoveryType::add_env_var));
259-
env.get_test_icd(0).physical_devices.emplace_back("pd0");
259+
env.get_test_icd(0).add_and_get_physical_device("pd0");
260260

261261
InstWrapper inst{env.vulkan_functions};
262262
FillDebugUtilsCreateDetails(inst.create_info, env.debug_log);
@@ -272,7 +272,7 @@ TEST(EnvVarICDOverrideSetup, TestOnlyAddDriverEnvVar) {
272272
TEST(EnvVarICDOverrideSetup, TestOnlyAddDriverEnvVarRunningWithElevatedPrivileges) {
273273
FrameworkEnvironment env{FrameworkSettings{}.set_run_as_if_with_elevated_privleges(true)};
274274
env.add_icd(TestICDDetails(TEST_ICD_PATH_EXPORT_NONE).set_discovery_type(ManifestDiscoveryType::add_env_var));
275-
env.get_test_icd(0).physical_devices.emplace_back("pd0");
275+
env.get_test_icd(0).add_and_get_physical_device("pd0");
276276

277277
InstWrapper inst{env.vulkan_functions};
278278
FillDebugUtilsCreateDetails(inst.create_info, env.debug_log);

tests/loader_get_proc_addr_tests.cpp

Lines changed: 13 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -202,8 +202,9 @@ TEST(GetProcAddr, Verify10FunctionsLoadWithMultipleDrivers) {
202202
// and return VK_SUCCESS to maintain previous behavior.
203203
TEST(GetDeviceProcAddr, SwapchainFuncsWithTerminator) {
204204
FrameworkEnvironment env{};
205-
auto& driver =
206-
env.add_icd(TestICDDetails(TEST_ICD_PATH_VERSION_2_EXPORT_ICD_GPDPA)).setup_WSI().add_physical_device("physical_device_0");
205+
auto& test_physical_device = env.add_icd(TestICDDetails(TEST_ICD_PATH_VERSION_2_EXPORT_ICD_GPDPA))
206+
.setup_WSI()
207+
.add_and_get_physical_device("physical_device_0");
207208

208209
InstWrapper inst(env.vulkan_functions);
209210
inst.create_info.add_extension("VK_EXT_debug_utils");
@@ -250,7 +251,7 @@ TEST(GetDeviceProcAddr, SwapchainFuncsWithTerminator) {
250251
log.logger.clear();
251252
ASSERT_FALSE(dev_funcs.vkDestroySwapchainKHR);
252253
}
253-
driver.physical_devices.at(0).add_extensions({"VK_KHR_swapchain", "VK_KHR_display_swapchain", "VK_EXT_debug_marker"});
254+
test_physical_device.add_extensions({"VK_KHR_swapchain", "VK_KHR_display_swapchain", "VK_EXT_debug_marker"});
254255
{
255256
DeviceWrapper dev{inst};
256257
dev.create_info.add_extensions({"VK_KHR_swapchain", "VK_KHR_display_swapchain", "VK_EXT_debug_marker"});
@@ -328,15 +329,15 @@ TEST(GetProcAddr, PreserveLayerGettingVkCreateDeviceWithNullInstance) {
328329

329330
TEST(GetDeviceProcAddr, AppQueries11FunctionsWhileOnlyEnabling10) {
330331
FrameworkEnvironment env{};
331-
auto& driver =
332+
auto& test_physical_device =
332333
env.add_icd(TestICDDetails(TEST_ICD_PATH_VERSION_2, VK_API_VERSION_1_1))
333334
.set_icd_api_version(VK_API_VERSION_1_1)
334-
.add_physical_device(
335+
.add_and_get_physical_device(
335336
PhysicalDevice{}.set_api_version(VK_API_VERSION_1_1).add_extension(VK_KHR_MAINTENANCE_5_EXTENSION_NAME).finish());
336337

337338
std::vector<const char*> functions = {"vkGetDeviceQueue2", "vkCmdDispatchBase", "vkCreateDescriptorUpdateTemplate"};
338339
for (const auto& f : functions) {
339-
driver.physical_devices.back().add_device_function(VulkanFunction{f, [] {}});
340+
test_physical_device.add_device_function(VulkanFunction{f, [] {}});
340341
}
341342
{ // doesn't enable the feature or extension
342343
InstWrapper inst{env.vulkan_functions};
@@ -382,15 +383,15 @@ TEST(GetDeviceProcAddr, AppQueries11FunctionsWhileOnlyEnabling10) {
382383

383384
TEST(GetDeviceProcAddr, AppQueries12FunctionsWhileOnlyEnabling11) {
384385
FrameworkEnvironment env{};
385-
auto& driver =
386+
auto& test_physical_device =
386387
env.add_icd(TestICDDetails(TEST_ICD_PATH_VERSION_2, VK_API_VERSION_1_2))
387388
.set_icd_api_version(VK_API_VERSION_1_2)
388-
.add_physical_device(
389+
.add_and_get_physical_device(
389390
PhysicalDevice{}.set_api_version(VK_API_VERSION_1_2).add_extension(VK_KHR_MAINTENANCE_5_EXTENSION_NAME).finish());
390391
std::vector<const char*> functions = {"vkCmdDrawIndirectCount", "vkCmdNextSubpass2", "vkGetBufferDeviceAddress",
391392
"vkGetDeviceMemoryOpaqueCaptureAddress"};
392393
for (const auto& f : functions) {
393-
driver.physical_devices.back().add_device_function(VulkanFunction{f, [] {}});
394+
test_physical_device.add_device_function(VulkanFunction{f, [] {}});
394395
}
395396
{ // doesn't enable the feature or extension
396397
InstWrapper inst{env.vulkan_functions};
@@ -439,16 +440,16 @@ TEST(GetDeviceProcAddr, AppQueries12FunctionsWhileOnlyEnabling11) {
439440

440441
TEST(GetDeviceProcAddr, AppQueries13FunctionsWhileOnlyEnabling12) {
441442
FrameworkEnvironment env{};
442-
auto& driver =
443+
auto& test_physical_device =
443444
env.add_icd(TestICDDetails(TEST_ICD_PATH_VERSION_2, VK_API_VERSION_1_3))
444445
.set_icd_api_version(VK_API_VERSION_1_3)
445-
.add_physical_device(
446+
.add_and_get_physical_device(
446447
PhysicalDevice{}.set_api_version(VK_API_VERSION_1_3).add_extension(VK_KHR_MAINTENANCE_5_EXTENSION_NAME).finish());
447448
std::vector<const char*> functions = {"vkCreatePrivateDataSlot", "vkGetDeviceBufferMemoryRequirements", "vkCmdWaitEvents2",
448449
"vkGetDeviceImageSparseMemoryRequirements"};
449450

450451
for (const auto& f : functions) {
451-
driver.physical_devices.back().add_device_function(VulkanFunction{f, [] {}});
452+
test_physical_device.add_device_function(VulkanFunction{f, [] {}});
452453
}
453454
{ // doesn't enable the feature or extension
454455
InstWrapper inst{env.vulkan_functions};

0 commit comments

Comments
 (0)