1 /*
2 * Copyright (c) 2021 The Khronos Group Inc.
3 * Copyright (c) 2021 Valve Corporation
4 * Copyright (c) 2021 LunarG, Inc.
5 *
6 * Permission is hereby granted, free of charge, to any person obtaining a copy
7 * of this software and/or associated documentation files (the "Materials"), to
8 * deal in the Materials without restriction, including without limitation the
9 * rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
10 * sell copies of the Materials, and to permit persons to whom the Materials are
11 * furnished to do so, subject to the following conditions:
12 *
13 * The above copyright notice(s) and this permission notice shall be included in
14 * all copies or substantial portions of the Materials.
15 *
16 * THE MATERIALS ARE PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
19 *
20 * IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
21 * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
22 * OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE MATERIALS OR THE
23 * USE OR OTHER DEALINGS IN THE MATERIALS.
24 *
25 * Author: Charles Giessen <charles@lunarg.com>
26 */
27
28 #include "test_environment.h"
29
30 #include <thread>
31
create_destroy_instance_loop_with_function_queries(FrameworkEnvironment * env,uint32_t num_loops_create_destroy_instance,uint32_t num_loops_try_get_instance_proc_addr,uint32_t num_loops_try_get_device_proc_addr)32 void create_destroy_instance_loop_with_function_queries(FrameworkEnvironment* env, uint32_t num_loops_create_destroy_instance,
33 uint32_t num_loops_try_get_instance_proc_addr,
34 uint32_t num_loops_try_get_device_proc_addr) {
35 for (uint32_t i = 0; i < num_loops_create_destroy_instance; i++) {
36 InstWrapper inst{env->vulkan_functions};
37 inst.CheckCreate();
38 PFN_vkEnumeratePhysicalDevices enum_pd = nullptr;
39 for (uint32_t j = 0; j < num_loops_try_get_instance_proc_addr; j++) {
40 enum_pd = inst.load("vkEnumeratePhysicalDevices");
41 ASSERT_NE(enum_pd, nullptr);
42 }
43 VkPhysicalDevice phys_dev = inst.GetPhysDev();
44
45 DeviceWrapper dev{inst};
46 dev.CheckCreate(phys_dev);
47 for (uint32_t j = 0; j < num_loops_try_get_device_proc_addr; j++) {
48 PFN_vkCmdBindPipeline p = dev.load("vkCmdBindPipeline");
49 p(VK_NULL_HANDLE, VkPipelineBindPoint::VK_PIPELINE_BIND_POINT_GRAPHICS, VK_NULL_HANDLE);
50 }
51 }
52 }
53
create_destroy_device_loop(FrameworkEnvironment * env,uint32_t num_loops_create_destroy_device,uint32_t num_loops_try_get_proc_addr)54 void create_destroy_device_loop(FrameworkEnvironment* env, uint32_t num_loops_create_destroy_device,
55 uint32_t num_loops_try_get_proc_addr) {
56 InstWrapper inst{env->vulkan_functions};
57 inst.CheckCreate();
58 for (uint32_t i = 0; i < num_loops_create_destroy_device; i++) {
59 DeviceWrapper dev{inst};
60 dev.CheckCreate(inst.GetPhysDev());
61
62 for (uint32_t j = 0; j < num_loops_try_get_proc_addr; j++) {
63 PFN_vkCmdBindPipeline p = dev.load("vkCmdBindPipeline");
64 PFN_vkCmdBindDescriptorSets d = dev.load("vkCmdBindDescriptorSets");
65 PFN_vkCmdBindVertexBuffers vb = dev.load("vkCmdBindVertexBuffers");
66 PFN_vkCmdBindIndexBuffer ib = dev.load("vkCmdBindIndexBuffer");
67 PFN_vkCmdDraw c = dev.load("vkCmdDraw");
68 p(VK_NULL_HANDLE, VkPipelineBindPoint::VK_PIPELINE_BIND_POINT_GRAPHICS, VK_NULL_HANDLE);
69 d(VK_NULL_HANDLE, VkPipelineBindPoint::VK_PIPELINE_BIND_POINT_GRAPHICS, VK_NULL_HANDLE, 0, 0, nullptr, 0, nullptr);
70 vb(VK_NULL_HANDLE, 0, 0, nullptr, nullptr);
71 ib(VK_NULL_HANDLE, 0, 0, VkIndexType::VK_INDEX_TYPE_UINT16);
72 c(VK_NULL_HANDLE, 0, 0, 0, 0);
73 }
74 }
75 }
test_vkCmdBindPipeline(VkCommandBuffer,VkPipelineBindPoint,VkPipeline)76 VKAPI_ATTR void VKAPI_CALL test_vkCmdBindPipeline(VkCommandBuffer, VkPipelineBindPoint, VkPipeline) {}
test_vkCmdBindDescriptorSets(VkCommandBuffer,VkPipelineBindPoint,VkPipelineLayout,uint32_t,uint32_t,const VkDescriptorSet *,uint32_t,const uint32_t *)77 VKAPI_ATTR void VKAPI_CALL test_vkCmdBindDescriptorSets(VkCommandBuffer, VkPipelineBindPoint, VkPipelineLayout, uint32_t, uint32_t,
78 const VkDescriptorSet*, uint32_t, const uint32_t*) {}
test_vkCmdBindVertexBuffers(VkCommandBuffer,uint32_t,uint32_t,const VkBuffer *,const VkDeviceSize *)79 VKAPI_ATTR void VKAPI_CALL test_vkCmdBindVertexBuffers(VkCommandBuffer, uint32_t, uint32_t, const VkBuffer*, const VkDeviceSize*) {}
test_vkCmdBindIndexBuffer(VkCommandBuffer,uint32_t,uint32_t,const VkBuffer *,const VkDeviceSize *)80 VKAPI_ATTR void VKAPI_CALL test_vkCmdBindIndexBuffer(VkCommandBuffer, uint32_t, uint32_t, const VkBuffer*, const VkDeviceSize*) {}
test_vkCmdDraw(VkCommandBuffer,uint32_t,uint32_t,uint32_t,uint32_t)81 VKAPI_ATTR void VKAPI_CALL test_vkCmdDraw(VkCommandBuffer, uint32_t, uint32_t, uint32_t, uint32_t) {}
TEST(Threading,InstanceCreateDestroyLoop)82 TEST(Threading, InstanceCreateDestroyLoop) {
83 const auto processor_count = std::thread::hardware_concurrency();
84
85 FrameworkEnvironment env{FrameworkSettings{}.set_log_filter("")};
86 auto& driver = env.add_icd(TestICDDetails(TEST_ICD_PATH_VERSION_2_EXPORT_ICD_GPDPA));
87 uint32_t num_loops_create_destroy_instance = 500;
88 uint32_t num_loops_try_get_instance_proc_addr = 5;
89 uint32_t num_loops_try_get_device_proc_addr = 100;
90
91 driver.physical_devices.emplace_back("physical_device_0")
92 .known_device_functions.push_back({"vkCmdBindPipeline", to_vkVoidFunction(test_vkCmdBindPipeline)});
93
94 std::vector<std::thread> instance_creation_threads;
95 std::vector<std::thread> function_query_threads;
96 for (uint32_t i = 0; i < processor_count; i++) {
97 instance_creation_threads.emplace_back(create_destroy_instance_loop_with_function_queries, &env,
98 num_loops_create_destroy_instance, num_loops_try_get_instance_proc_addr,
99 num_loops_try_get_device_proc_addr);
100 }
101 for (uint32_t i = 0; i < processor_count; i++) {
102 instance_creation_threads[i].join();
103 }
104 }
105
TEST(Threading,DeviceCreateDestroyLoop)106 TEST(Threading, DeviceCreateDestroyLoop) {
107 const auto processor_count = std::thread::hardware_concurrency();
108
109 FrameworkEnvironment env{FrameworkSettings{}.set_log_filter("")};
110 auto& driver = env.add_icd(TestICDDetails(TEST_ICD_PATH_VERSION_2_EXPORT_ICD_GPDPA));
111
112 uint32_t num_loops_create_destroy_device = 1000;
113 uint32_t num_loops_try_get_device_proc_addr = 5;
114
115 driver.physical_devices.emplace_back("physical_device_0").known_device_functions = {
116 {"vkCmdBindPipeline", to_vkVoidFunction(test_vkCmdBindPipeline)},
117 {"vkCmdBindDescriptorSets", to_vkVoidFunction(test_vkCmdBindDescriptorSets)},
118 {"vkCmdBindVertexBuffers", to_vkVoidFunction(test_vkCmdBindVertexBuffers)},
119 {"vkCmdBindIndexBuffer", to_vkVoidFunction(test_vkCmdBindIndexBuffer)},
120 {"vkCmdDraw", to_vkVoidFunction(test_vkCmdDraw)}};
121
122 std::vector<std::thread> device_creation_threads;
123
124 for (uint32_t i = 0; i < processor_count; i++) {
125 device_creation_threads.emplace_back(create_destroy_device_loop, &env, num_loops_create_destroy_device,
126 num_loops_try_get_device_proc_addr);
127 }
128 for (uint32_t i = 0; i < processor_count; i++) {
129 device_creation_threads[i].join();
130 }
131 }
132