1 /*
2 * Copyright (c) 2021-2022 The Khronos Group Inc.
3 * Copyright (c) 2021-2022 Valve Corporation
4 * Copyright (c) 2021-2022 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 // Verify that the various ways to get vkGetInstanceProcAddr return the same value
TEST(GetProcAddr,VerifyGetInstanceProcAddr)31 TEST(GetProcAddr, VerifyGetInstanceProcAddr) {
32 FrameworkEnvironment env{};
33 env.add_icd(TestICDDetails(TEST_ICD_PATH_VERSION_2_EXPORT_ICD_GPDPA));
34 env.get_test_icd().physical_devices.emplace_back("physical_device_0");
35 {
36 InstWrapper inst{env.vulkan_functions};
37 inst.create_info.set_api_version(VK_API_VERSION_1_1);
38 inst.CheckCreate();
39
40 // NOTE: The vulkan_functions are queried using the platform get proc addr from the loader. So we'll compare
41 // that to what is returned by asking it what the various Vulkan get proc addr functions are.
42 PFN_vkGetInstanceProcAddr gipa_loader = env.vulkan_functions.vkGetInstanceProcAddr;
43 PFN_vkGetInstanceProcAddr gipa_queried = reinterpret_cast<PFN_vkGetInstanceProcAddr>(
44 env.vulkan_functions.vkGetInstanceProcAddr(inst.inst, "vkGetInstanceProcAddr"));
45 ASSERT_EQ(gipa_loader, gipa_queried);
46 }
47
48 {
49 InstWrapper inst{env.vulkan_functions};
50 inst.create_info.set_api_version(VK_API_VERSION_1_3);
51 inst.CheckCreate();
52
53 // NOTE: The vulkan_functions are queried using the platform get proc addr from the loader. So we'll compare
54 // that to what is returned by asking it what the various Vulkan get proc addr functions are.
55 PFN_vkGetInstanceProcAddr gipa_loader = env.vulkan_functions.vkGetInstanceProcAddr;
56 PFN_vkGetInstanceProcAddr gipa_queried = reinterpret_cast<PFN_vkGetInstanceProcAddr>(
57 env.vulkan_functions.vkGetInstanceProcAddr(inst.inst, "vkGetInstanceProcAddr"));
58 ASSERT_EQ(gipa_loader, gipa_queried);
59 }
60 }
61
62 // Verify that the various ways to get vkGetDeviceProcAddr return the same value
TEST(GetProcAddr,VerifyGetDeviceProcAddr)63 TEST(GetProcAddr, VerifyGetDeviceProcAddr) {
64 FrameworkEnvironment env{};
65 env.add_icd(TestICDDetails(TEST_ICD_PATH_VERSION_2_EXPORT_ICD_GPDPA));
66 env.get_test_icd().physical_devices.emplace_back("physical_device_0");
67
68 InstWrapper inst{env.vulkan_functions};
69 inst.create_info.set_api_version(VK_API_VERSION_1_1);
70 inst.CheckCreate();
71 VkPhysicalDevice phys_dev = inst.GetPhysDev();
72
73 // NOTE: The vulkan_functions are queried using the platform get proc addr from the loader. So we'll compare
74 // that to what is returned by asking it what the various Vulkan get proc addr functions are.
75 PFN_vkGetDeviceProcAddr gdpa_loader = env.vulkan_functions.vkGetDeviceProcAddr;
76 PFN_vkGetDeviceProcAddr gdpa_inst_queried =
77 reinterpret_cast<PFN_vkGetDeviceProcAddr>(env.vulkan_functions.vkGetInstanceProcAddr(inst.inst, "vkGetDeviceProcAddr"));
78 ASSERT_EQ(gdpa_loader, gdpa_inst_queried);
79
80 DeviceWrapper dev{inst};
81 dev.create_info.add_device_queue(DeviceQueueCreateInfo{}.add_priority(0.0f));
82 dev.CheckCreate(phys_dev);
83
84 PFN_vkGetDeviceProcAddr gdpa_dev_queried =
85 reinterpret_cast<PFN_vkGetDeviceProcAddr>(env.vulkan_functions.vkGetDeviceProcAddr(dev.dev, "vkGetDeviceProcAddr"));
86 ASSERT_EQ(gdpa_loader, gdpa_dev_queried);
87 }
88
89 // Load the global function pointers with and without a NULL vkInstance handle.
90 // Call the function to make sure it is callable, don't care about what is returned.
TEST(GetProcAddr,GlobalFunctions)91 TEST(GetProcAddr, GlobalFunctions) {
92 FrameworkEnvironment env{};
93 env.add_icd(TestICDDetails(TEST_ICD_PATH_VERSION_2_EXPORT_ICD_GPDPA));
94 env.get_test_icd().physical_devices.emplace_back("physical_device_0");
95
96 auto& gipa = env.vulkan_functions.vkGetInstanceProcAddr;
97 // global entry points with NULL instance handle
98 {
99 auto EnumerateInstanceExtensionProperties =
100 reinterpret_cast<PFN_vkEnumerateInstanceExtensionProperties>(gipa(NULL, "vkEnumerateInstanceExtensionProperties"));
101 handle_assert_has_value(EnumerateInstanceExtensionProperties);
102 uint32_t ext_count = 0;
103 ASSERT_EQ(VK_SUCCESS, EnumerateInstanceExtensionProperties("", &ext_count, nullptr));
104
105 auto EnumerateInstanceLayerProperties =
106 reinterpret_cast<PFN_vkEnumerateInstanceLayerProperties>(gipa(NULL, "vkEnumerateInstanceLayerProperties"));
107 handle_assert_has_value(EnumerateInstanceLayerProperties);
108 uint32_t layer_count = 0;
109 ASSERT_EQ(VK_SUCCESS, EnumerateInstanceLayerProperties(&layer_count, nullptr));
110
111 auto EnumerateInstanceVersion = reinterpret_cast<PFN_vkEnumerateInstanceVersion>(gipa(NULL, "vkEnumerateInstanceVersion"));
112 handle_assert_has_value(EnumerateInstanceVersion);
113 uint32_t api_version = 0;
114 EnumerateInstanceVersion(&api_version);
115
116 auto GetInstanceProcAddr = reinterpret_cast<PFN_vkGetInstanceProcAddr>(gipa(NULL, "vkGetInstanceProcAddr"));
117 ASSERT_EQ(GetInstanceProcAddr,
118 reinterpret_cast<PFN_vkGetInstanceProcAddr>(GetInstanceProcAddr(NULL, "vkGetInstanceProcAddr")));
119
120 auto CreateInstance = reinterpret_cast<PFN_vkCreateInstance>(gipa(NULL, "vkCreateInstance"));
121 handle_assert_has_value(CreateInstance);
122 }
123 // Now create an instance and query the functions again - should work because the instance version is less than 1.2
124 for (int i = 0; i <= 2; i++) {
125 InstWrapper inst{env.vulkan_functions};
126 inst.create_info.api_version = VK_MAKE_API_VERSION(0, 1, i, 0);
127 inst.CheckCreate();
128
129 auto EnumerateInstanceExtensionProperties =
130 reinterpret_cast<PFN_vkEnumerateInstanceExtensionProperties>(gipa(inst, "vkEnumerateInstanceExtensionProperties"));
131 handle_assert_has_value(EnumerateInstanceExtensionProperties);
132 uint32_t ext_count = 0;
133 ASSERT_EQ(VK_SUCCESS, EnumerateInstanceExtensionProperties("", &ext_count, nullptr));
134
135 auto EnumerateInstanceLayerProperties =
136 reinterpret_cast<PFN_vkEnumerateInstanceLayerProperties>(gipa(inst, "vkEnumerateInstanceLayerProperties"));
137 handle_assert_has_value(EnumerateInstanceLayerProperties);
138 uint32_t layer_count = 0;
139 ASSERT_EQ(VK_SUCCESS, EnumerateInstanceLayerProperties(&layer_count, nullptr));
140
141 auto EnumerateInstanceVersion = reinterpret_cast<PFN_vkEnumerateInstanceVersion>(gipa(inst, "vkEnumerateInstanceVersion"));
142 handle_assert_has_value(EnumerateInstanceVersion);
143 uint32_t api_version = 0;
144 EnumerateInstanceVersion(&api_version);
145
146 auto GetInstanceProcAddr = reinterpret_cast<PFN_vkGetInstanceProcAddr>(gipa(inst, "vkGetInstanceProcAddr"));
147 handle_assert_has_value(GetInstanceProcAddr);
148 ASSERT_EQ(GetInstanceProcAddr,
149 reinterpret_cast<PFN_vkGetInstanceProcAddr>(GetInstanceProcAddr(inst, "vkGetInstanceProcAddr")));
150
151 auto CreateInstance = reinterpret_cast<PFN_vkCreateInstance>(gipa(inst, "vkCreateInstance"));
152 handle_assert_has_value(CreateInstance);
153 }
154 {
155 // Create a 1.3 instance - now everything should return NULL
156 InstWrapper inst{env.vulkan_functions};
157 inst.create_info.api_version = VK_MAKE_API_VERSION(0, 1, 3, 0);
158 inst.CheckCreate();
159
160 auto EnumerateInstanceExtensionProperties =
161 reinterpret_cast<PFN_vkEnumerateInstanceExtensionProperties>(gipa(inst, "vkEnumerateInstanceExtensionProperties"));
162 handle_assert_null(EnumerateInstanceExtensionProperties);
163
164 auto EnumerateInstanceLayerProperties =
165 reinterpret_cast<PFN_vkEnumerateInstanceLayerProperties>(gipa(inst, "vkEnumerateInstanceLayerProperties"));
166 handle_assert_null(EnumerateInstanceLayerProperties);
167
168 auto EnumerateInstanceVersion = reinterpret_cast<PFN_vkEnumerateInstanceVersion>(gipa(inst, "vkEnumerateInstanceVersion"));
169 handle_assert_null(EnumerateInstanceVersion);
170
171 auto CreateInstance = reinterpret_cast<PFN_vkCreateInstance>(gipa(inst, "vkCreateInstance"));
172 handle_assert_null(CreateInstance);
173
174 auto GetInstanceProcAddr = reinterpret_cast<PFN_vkGetInstanceProcAddr>(gipa(inst, "vkGetInstanceProcAddr"));
175 handle_assert_equal(env.vulkan_functions.vkGetInstanceProcAddr, GetInstanceProcAddr);
176 ASSERT_EQ(GetInstanceProcAddr,
177 reinterpret_cast<PFN_vkGetInstanceProcAddr>(GetInstanceProcAddr(inst, "vkGetInstanceProcAddr")));
178 ASSERT_EQ(GetInstanceProcAddr,
179 reinterpret_cast<PFN_vkGetInstanceProcAddr>(GetInstanceProcAddr(NULL, "vkGetInstanceProcAddr")));
180 // get a non pre-instance function pointer
181 auto EnumeratePhysicalDevices = reinterpret_cast<PFN_vkGetInstanceProcAddr>(gipa(inst, "vkEnumeratePhysicalDevices"));
182 handle_assert_has_value(EnumeratePhysicalDevices);
183
184 EnumeratePhysicalDevices = reinterpret_cast<PFN_vkGetInstanceProcAddr>(gipa(NULL, "vkEnumeratePhysicalDevices"));
185 handle_assert_null(EnumeratePhysicalDevices);
186 }
187 }