• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 // Copyright 2018 The SwiftShader Authors. All Rights Reserved.
2 //
3 // Licensed under the Apache License, Version 2.0 (the "License");
4 // you may not use this file except in compliance with the License.
5 // You may obtain a copy of the License at
6 //
7 //    http://www.apache.org/licenses/LICENSE-2.0
8 //
9 // Unless required by applicable law or agreed to in writing, software
10 // distributed under the License is distributed on an "AS IS" BASIS,
11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 // See the License for the specific language governing permissions and
13 // limitations under the License.
14 
15 #include "VkGetProcAddress.hpp"
16 #include "VkDevice.hpp"
17 
18 #include <string>
19 #include <unordered_map>
20 #include <vector>
21 
22 #ifdef __ANDROID__
23 #	include <hardware/hwvulkan.h>
24 #	include <vulkan/vk_android_native_buffer.h>
25 #	include <cerrno>
26 #endif
27 
28 namespace vk {
29 
30 #define MAKE_VULKAN_GLOBAL_ENTRY(aFunction)                           \
31 	{                                                                 \
32 #		aFunction, reinterpret_cast < PFN_vkVoidFunction>(aFunction) \
33 	}
34 static const std::unordered_map<std::string, PFN_vkVoidFunction> globalFunctionPointers = {
35 	MAKE_VULKAN_GLOBAL_ENTRY(vkCreateInstance),
36 	MAKE_VULKAN_GLOBAL_ENTRY(vkEnumerateInstanceExtensionProperties),
37 	MAKE_VULKAN_GLOBAL_ENTRY(vkEnumerateInstanceLayerProperties),
38 	MAKE_VULKAN_GLOBAL_ENTRY(vkEnumerateInstanceVersion),
39 };
40 #undef MAKE_VULKAN_GLOBAL_ENTRY
41 
42 #define MAKE_VULKAN_INSTANCE_ENTRY(aFunction)                         \
43 	{                                                                 \
44 #		aFunction, reinterpret_cast < PFN_vkVoidFunction>(aFunction) \
45 	}
46 static const std::unordered_map<std::string, PFN_vkVoidFunction> instanceFunctionPointers = {
47 	MAKE_VULKAN_INSTANCE_ENTRY(vkDestroyInstance),
48 	MAKE_VULKAN_INSTANCE_ENTRY(vkEnumeratePhysicalDevices),
49 	MAKE_VULKAN_INSTANCE_ENTRY(vkGetPhysicalDeviceFeatures),
50 	MAKE_VULKAN_INSTANCE_ENTRY(vkGetPhysicalDeviceFormatProperties),
51 	MAKE_VULKAN_INSTANCE_ENTRY(vkGetPhysicalDeviceImageFormatProperties),
52 	MAKE_VULKAN_INSTANCE_ENTRY(vkGetPhysicalDeviceProperties),
53 	MAKE_VULKAN_INSTANCE_ENTRY(vkGetPhysicalDeviceQueueFamilyProperties),
54 	MAKE_VULKAN_INSTANCE_ENTRY(vkGetPhysicalDeviceMemoryProperties),
55 	MAKE_VULKAN_INSTANCE_ENTRY(vkCreateDevice),
56 	MAKE_VULKAN_INSTANCE_ENTRY(vkEnumerateDeviceExtensionProperties),
57 	MAKE_VULKAN_INSTANCE_ENTRY(vkEnumerateDeviceLayerProperties),
58 	MAKE_VULKAN_INSTANCE_ENTRY(vkGetPhysicalDeviceSparseImageFormatProperties),
59 	MAKE_VULKAN_INSTANCE_ENTRY(vkEnumeratePhysicalDeviceGroups),
60 	MAKE_VULKAN_INSTANCE_ENTRY(vkGetPhysicalDeviceFeatures2),
61 	MAKE_VULKAN_INSTANCE_ENTRY(vkGetPhysicalDeviceProperties2),
62 	MAKE_VULKAN_INSTANCE_ENTRY(vkGetPhysicalDeviceFormatProperties2),
63 	MAKE_VULKAN_INSTANCE_ENTRY(vkGetPhysicalDeviceImageFormatProperties2),
64 	MAKE_VULKAN_INSTANCE_ENTRY(vkGetPhysicalDeviceQueueFamilyProperties2),
65 	MAKE_VULKAN_INSTANCE_ENTRY(vkGetPhysicalDeviceMemoryProperties2),
66 	MAKE_VULKAN_INSTANCE_ENTRY(vkGetPhysicalDeviceSparseImageFormatProperties2),
67 	MAKE_VULKAN_INSTANCE_ENTRY(vkGetPhysicalDeviceExternalBufferProperties),
68 	MAKE_VULKAN_INSTANCE_ENTRY(vkGetPhysicalDeviceExternalFenceProperties),
69 	MAKE_VULKAN_INSTANCE_ENTRY(vkGetPhysicalDeviceExternalSemaphoreProperties),
70 	// VK_KHR_device_group_creation
71 	MAKE_VULKAN_INSTANCE_ENTRY(vkEnumeratePhysicalDeviceGroupsKHR),
72 	// VK_KHR_external_fence_capabilities
73 	MAKE_VULKAN_INSTANCE_ENTRY(vkGetPhysicalDeviceExternalFencePropertiesKHR),
74 	// VK_KHR_external_memory_capabilities
75 	MAKE_VULKAN_INSTANCE_ENTRY(vkGetPhysicalDeviceExternalBufferPropertiesKHR),
76 	// VK_KHR_external_semaphore_capabilities
77 	MAKE_VULKAN_INSTANCE_ENTRY(vkGetPhysicalDeviceExternalSemaphorePropertiesKHR),
78 	// VK_KHR_get_physical_device_properties2
79 	MAKE_VULKAN_INSTANCE_ENTRY(vkGetPhysicalDeviceFeatures2KHR),
80 	MAKE_VULKAN_INSTANCE_ENTRY(vkGetPhysicalDeviceProperties2KHR),
81 	MAKE_VULKAN_INSTANCE_ENTRY(vkGetPhysicalDeviceFormatProperties2KHR),
82 	MAKE_VULKAN_INSTANCE_ENTRY(vkGetPhysicalDeviceImageFormatProperties2KHR),
83 	MAKE_VULKAN_INSTANCE_ENTRY(vkGetPhysicalDeviceQueueFamilyProperties2KHR),
84 	MAKE_VULKAN_INSTANCE_ENTRY(vkGetPhysicalDeviceMemoryProperties2KHR),
85 	MAKE_VULKAN_INSTANCE_ENTRY(vkGetPhysicalDeviceSparseImageFormatProperties2KHR),
86 	// VK_EXT_debug_utils
87 	MAKE_VULKAN_INSTANCE_ENTRY(vkCmdBeginDebugUtilsLabelEXT),
88 	MAKE_VULKAN_INSTANCE_ENTRY(vkCmdEndDebugUtilsLabelEXT),
89 	MAKE_VULKAN_INSTANCE_ENTRY(vkCmdInsertDebugUtilsLabelEXT),
90 	MAKE_VULKAN_INSTANCE_ENTRY(vkCreateDebugUtilsMessengerEXT),
91 	MAKE_VULKAN_INSTANCE_ENTRY(vkDestroyDebugUtilsMessengerEXT),
92 	MAKE_VULKAN_INSTANCE_ENTRY(vkQueueBeginDebugUtilsLabelEXT),
93 	MAKE_VULKAN_INSTANCE_ENTRY(vkQueueEndDebugUtilsLabelEXT),
94 	MAKE_VULKAN_INSTANCE_ENTRY(vkQueueInsertDebugUtilsLabelEXT),
95 	MAKE_VULKAN_INSTANCE_ENTRY(vkSetDebugUtilsObjectNameEXT),
96 	MAKE_VULKAN_INSTANCE_ENTRY(vkSetDebugUtilsObjectTagEXT),
97 	MAKE_VULKAN_INSTANCE_ENTRY(vkSubmitDebugUtilsMessageEXT),
98 #ifndef __ANDROID__
99 	// VK_KHR_surface
100 	MAKE_VULKAN_INSTANCE_ENTRY(vkDestroySurfaceKHR),
101 	MAKE_VULKAN_INSTANCE_ENTRY(vkGetPhysicalDeviceSurfaceSupportKHR),
102 	MAKE_VULKAN_INSTANCE_ENTRY(vkGetPhysicalDeviceSurfaceCapabilitiesKHR),
103 	MAKE_VULKAN_INSTANCE_ENTRY(vkGetPhysicalDeviceSurfaceFormatsKHR),
104 	MAKE_VULKAN_INSTANCE_ENTRY(vkGetPhysicalDeviceSurfacePresentModesKHR),
105 	MAKE_VULKAN_INSTANCE_ENTRY(vkGetPhysicalDevicePresentRectanglesKHR),
106 #endif
107 #ifdef VK_USE_PLATFORM_XCB_KHR
108 	// VK_KHR_Xcb_surface
109 	MAKE_VULKAN_INSTANCE_ENTRY(vkCreateXcbSurfaceKHR),
110 	MAKE_VULKAN_INSTANCE_ENTRY(vkGetPhysicalDeviceXcbPresentationSupportKHR),
111 #endif
112 #ifdef VK_USE_PLATFORM_XLIB_KHR
113 	// VK_KHR_xlib_surface
114 	MAKE_VULKAN_INSTANCE_ENTRY(vkCreateXlibSurfaceKHR),
115 	MAKE_VULKAN_INSTANCE_ENTRY(vkGetPhysicalDeviceXlibPresentationSupportKHR),
116 #endif
117 #ifdef VK_USE_PLATFORM_WAYLAND_KHR
118 	// VK_KHR_wayland_surface
119 	MAKE_VULKAN_INSTANCE_ENTRY(vkCreateWaylandSurfaceKHR),
120 	MAKE_VULKAN_INSTANCE_ENTRY(vkGetPhysicalDeviceWaylandPresentationSupportKHR),
121 #endif
122 #ifdef VK_USE_PLATFORM_DIRECTFB_EXT
123 	// VK_EXT_directfb_surface
124 	MAKE_VULKAN_INSTANCE_ENTRY(vkCreateDirectFBSurfaceEXT),
125 	MAKE_VULKAN_INSTANCE_ENTRY(vkGetPhysicalDeviceDirectFBPresentationSupportEXT),
126 #endif
127 #ifdef VK_USE_PLATFORM_DISPLAY_KHR
128 	// VK_KHR_display
129 	MAKE_VULKAN_INSTANCE_ENTRY(vkCreateDisplayModeKHR),
130 	MAKE_VULKAN_INSTANCE_ENTRY(vkCreateDisplayPlaneSurfaceKHR),
131 	MAKE_VULKAN_INSTANCE_ENTRY(vkGetDisplayModePropertiesKHR),
132 	MAKE_VULKAN_INSTANCE_ENTRY(vkGetDisplayPlaneCapabilitiesKHR),
133 	MAKE_VULKAN_INSTANCE_ENTRY(vkGetDisplayPlaneSupportedDisplaysKHR),
134 	MAKE_VULKAN_INSTANCE_ENTRY(vkGetPhysicalDeviceDisplayPlanePropertiesKHR),
135 	MAKE_VULKAN_INSTANCE_ENTRY(vkGetPhysicalDeviceDisplayPropertiesKHR),
136 #endif
137 #ifdef VK_USE_PLATFORM_MACOS_MVK
138 	// VK_MVK_macos_surface
139 	MAKE_VULKAN_INSTANCE_ENTRY(vkCreateMacOSSurfaceMVK),
140 #endif
141 #ifdef VK_USE_PLATFORM_METAL_EXT
142 	// VK_EXT_metal_surface
143 	MAKE_VULKAN_INSTANCE_ENTRY(vkCreateMetalSurfaceEXT),
144 #endif
145 #ifdef VK_USE_PLATFORM_WIN32_KHR
146 	// VK_KHR_win32_surface
147 	MAKE_VULKAN_INSTANCE_ENTRY(vkCreateWin32SurfaceKHR),
148 	MAKE_VULKAN_INSTANCE_ENTRY(vkGetPhysicalDeviceWin32PresentationSupportKHR),
149 #endif
150 	MAKE_VULKAN_INSTANCE_ENTRY(vkCreateHeadlessSurfaceEXT),
151 };
152 #undef MAKE_VULKAN_INSTANCE_ENTRY
153 
154 #define MAKE_VULKAN_DEVICE_ENTRY(aFunction)                           \
155 	{                                                                 \
156 #		aFunction, reinterpret_cast < PFN_vkVoidFunction>(aFunction) \
157 	}
158 static const std::unordered_map<std::string, PFN_vkVoidFunction> deviceFunctionPointers = {
159 	MAKE_VULKAN_DEVICE_ENTRY(vkGetInstanceProcAddr),
160 	MAKE_VULKAN_DEVICE_ENTRY(vkGetDeviceProcAddr),
161 	MAKE_VULKAN_DEVICE_ENTRY(vkDestroyDevice),
162 	MAKE_VULKAN_DEVICE_ENTRY(vkGetDeviceQueue),
163 	MAKE_VULKAN_DEVICE_ENTRY(vkQueueSubmit),
164 	MAKE_VULKAN_DEVICE_ENTRY(vkQueueWaitIdle),
165 	MAKE_VULKAN_DEVICE_ENTRY(vkDeviceWaitIdle),
166 	MAKE_VULKAN_DEVICE_ENTRY(vkAllocateMemory),
167 	MAKE_VULKAN_DEVICE_ENTRY(vkFreeMemory),
168 	MAKE_VULKAN_DEVICE_ENTRY(vkMapMemory),
169 	MAKE_VULKAN_DEVICE_ENTRY(vkUnmapMemory),
170 	MAKE_VULKAN_DEVICE_ENTRY(vkFlushMappedMemoryRanges),
171 	MAKE_VULKAN_DEVICE_ENTRY(vkInvalidateMappedMemoryRanges),
172 	MAKE_VULKAN_DEVICE_ENTRY(vkGetDeviceMemoryCommitment),
173 	MAKE_VULKAN_DEVICE_ENTRY(vkBindBufferMemory),
174 	MAKE_VULKAN_DEVICE_ENTRY(vkBindImageMemory),
175 	MAKE_VULKAN_DEVICE_ENTRY(vkGetBufferMemoryRequirements),
176 	MAKE_VULKAN_DEVICE_ENTRY(vkGetImageMemoryRequirements),
177 	MAKE_VULKAN_DEVICE_ENTRY(vkGetImageSparseMemoryRequirements),
178 	MAKE_VULKAN_DEVICE_ENTRY(vkQueueBindSparse),
179 	MAKE_VULKAN_DEVICE_ENTRY(vkCreateFence),
180 	MAKE_VULKAN_DEVICE_ENTRY(vkDestroyFence),
181 	MAKE_VULKAN_DEVICE_ENTRY(vkResetFences),
182 	MAKE_VULKAN_DEVICE_ENTRY(vkGetFenceStatus),
183 	MAKE_VULKAN_DEVICE_ENTRY(vkWaitForFences),
184 	MAKE_VULKAN_DEVICE_ENTRY(vkCreateSemaphore),
185 	MAKE_VULKAN_DEVICE_ENTRY(vkDestroySemaphore),
186 	MAKE_VULKAN_DEVICE_ENTRY(vkCreateEvent),
187 	MAKE_VULKAN_DEVICE_ENTRY(vkDestroyEvent),
188 	MAKE_VULKAN_DEVICE_ENTRY(vkGetEventStatus),
189 	MAKE_VULKAN_DEVICE_ENTRY(vkSetEvent),
190 	MAKE_VULKAN_DEVICE_ENTRY(vkResetEvent),
191 	MAKE_VULKAN_DEVICE_ENTRY(vkCreateQueryPool),
192 	MAKE_VULKAN_DEVICE_ENTRY(vkDestroyQueryPool),
193 	MAKE_VULKAN_DEVICE_ENTRY(vkGetQueryPoolResults),
194 	MAKE_VULKAN_DEVICE_ENTRY(vkCreateBuffer),
195 	MAKE_VULKAN_DEVICE_ENTRY(vkDestroyBuffer),
196 	MAKE_VULKAN_DEVICE_ENTRY(vkCreateBufferView),
197 	MAKE_VULKAN_DEVICE_ENTRY(vkDestroyBufferView),
198 	MAKE_VULKAN_DEVICE_ENTRY(vkCreateImage),
199 	MAKE_VULKAN_DEVICE_ENTRY(vkDestroyImage),
200 	MAKE_VULKAN_DEVICE_ENTRY(vkGetImageSubresourceLayout),
201 	MAKE_VULKAN_DEVICE_ENTRY(vkCreateImageView),
202 	MAKE_VULKAN_DEVICE_ENTRY(vkDestroyImageView),
203 	MAKE_VULKAN_DEVICE_ENTRY(vkCreateShaderModule),
204 	MAKE_VULKAN_DEVICE_ENTRY(vkDestroyShaderModule),
205 	MAKE_VULKAN_DEVICE_ENTRY(vkCreatePipelineCache),
206 	MAKE_VULKAN_DEVICE_ENTRY(vkDestroyPipelineCache),
207 	MAKE_VULKAN_DEVICE_ENTRY(vkGetPipelineCacheData),
208 	MAKE_VULKAN_DEVICE_ENTRY(vkMergePipelineCaches),
209 	MAKE_VULKAN_DEVICE_ENTRY(vkCreateGraphicsPipelines),
210 	MAKE_VULKAN_DEVICE_ENTRY(vkCreateComputePipelines),
211 	MAKE_VULKAN_DEVICE_ENTRY(vkDestroyPipeline),
212 	MAKE_VULKAN_DEVICE_ENTRY(vkCreatePipelineLayout),
213 	MAKE_VULKAN_DEVICE_ENTRY(vkDestroyPipelineLayout),
214 	MAKE_VULKAN_DEVICE_ENTRY(vkCreateSampler),
215 	MAKE_VULKAN_DEVICE_ENTRY(vkDestroySampler),
216 	MAKE_VULKAN_DEVICE_ENTRY(vkCreateDescriptorSetLayout),
217 	MAKE_VULKAN_DEVICE_ENTRY(vkDestroyDescriptorSetLayout),
218 	MAKE_VULKAN_DEVICE_ENTRY(vkCreateDescriptorPool),
219 	MAKE_VULKAN_DEVICE_ENTRY(vkDestroyDescriptorPool),
220 	MAKE_VULKAN_DEVICE_ENTRY(vkResetDescriptorPool),
221 	MAKE_VULKAN_DEVICE_ENTRY(vkAllocateDescriptorSets),
222 	MAKE_VULKAN_DEVICE_ENTRY(vkFreeDescriptorSets),
223 	MAKE_VULKAN_DEVICE_ENTRY(vkUpdateDescriptorSets),
224 	MAKE_VULKAN_DEVICE_ENTRY(vkCreateFramebuffer),
225 	MAKE_VULKAN_DEVICE_ENTRY(vkDestroyFramebuffer),
226 	MAKE_VULKAN_DEVICE_ENTRY(vkCreateRenderPass),
227 	MAKE_VULKAN_DEVICE_ENTRY(vkDestroyRenderPass),
228 	MAKE_VULKAN_DEVICE_ENTRY(vkGetRenderAreaGranularity),
229 	MAKE_VULKAN_DEVICE_ENTRY(vkCreateCommandPool),
230 	MAKE_VULKAN_DEVICE_ENTRY(vkDestroyCommandPool),
231 	MAKE_VULKAN_DEVICE_ENTRY(vkResetCommandPool),
232 	MAKE_VULKAN_DEVICE_ENTRY(vkAllocateCommandBuffers),
233 	MAKE_VULKAN_DEVICE_ENTRY(vkFreeCommandBuffers),
234 	MAKE_VULKAN_DEVICE_ENTRY(vkBeginCommandBuffer),
235 	MAKE_VULKAN_DEVICE_ENTRY(vkEndCommandBuffer),
236 	MAKE_VULKAN_DEVICE_ENTRY(vkResetCommandBuffer),
237 	MAKE_VULKAN_DEVICE_ENTRY(vkCmdBindPipeline),
238 	MAKE_VULKAN_DEVICE_ENTRY(vkCmdSetViewport),
239 	MAKE_VULKAN_DEVICE_ENTRY(vkCmdSetScissor),
240 	MAKE_VULKAN_DEVICE_ENTRY(vkCmdSetLineWidth),
241 	MAKE_VULKAN_DEVICE_ENTRY(vkCmdSetDepthBias),
242 	MAKE_VULKAN_DEVICE_ENTRY(vkCmdSetBlendConstants),
243 	MAKE_VULKAN_DEVICE_ENTRY(vkCmdSetDepthBounds),
244 	MAKE_VULKAN_DEVICE_ENTRY(vkCmdSetStencilCompareMask),
245 	MAKE_VULKAN_DEVICE_ENTRY(vkCmdSetStencilWriteMask),
246 	MAKE_VULKAN_DEVICE_ENTRY(vkCmdSetStencilReference),
247 	MAKE_VULKAN_DEVICE_ENTRY(vkCmdBindDescriptorSets),
248 	MAKE_VULKAN_DEVICE_ENTRY(vkCmdBindIndexBuffer),
249 	MAKE_VULKAN_DEVICE_ENTRY(vkCmdBindVertexBuffers),
250 	MAKE_VULKAN_DEVICE_ENTRY(vkCmdDraw),
251 	MAKE_VULKAN_DEVICE_ENTRY(vkCmdDrawIndexed),
252 	MAKE_VULKAN_DEVICE_ENTRY(vkCmdDrawIndirect),
253 	MAKE_VULKAN_DEVICE_ENTRY(vkCmdDrawIndexedIndirect),
254 	MAKE_VULKAN_DEVICE_ENTRY(vkCmdDispatch),
255 	MAKE_VULKAN_DEVICE_ENTRY(vkCmdDispatchIndirect),
256 	MAKE_VULKAN_DEVICE_ENTRY(vkCmdCopyBuffer),
257 	MAKE_VULKAN_DEVICE_ENTRY(vkCmdCopyImage),
258 	MAKE_VULKAN_DEVICE_ENTRY(vkCmdBlitImage),
259 	MAKE_VULKAN_DEVICE_ENTRY(vkCmdCopyBufferToImage),
260 	MAKE_VULKAN_DEVICE_ENTRY(vkCmdCopyImageToBuffer),
261 	MAKE_VULKAN_DEVICE_ENTRY(vkCmdUpdateBuffer),
262 	MAKE_VULKAN_DEVICE_ENTRY(vkCmdFillBuffer),
263 	MAKE_VULKAN_DEVICE_ENTRY(vkCmdClearColorImage),
264 	MAKE_VULKAN_DEVICE_ENTRY(vkCmdClearDepthStencilImage),
265 	MAKE_VULKAN_DEVICE_ENTRY(vkCmdClearAttachments),
266 	MAKE_VULKAN_DEVICE_ENTRY(vkCmdResolveImage),
267 	MAKE_VULKAN_DEVICE_ENTRY(vkCmdSetEvent),
268 	MAKE_VULKAN_DEVICE_ENTRY(vkCmdResetEvent),
269 	MAKE_VULKAN_DEVICE_ENTRY(vkCmdWaitEvents),
270 	MAKE_VULKAN_DEVICE_ENTRY(vkCmdPipelineBarrier),
271 	MAKE_VULKAN_DEVICE_ENTRY(vkCmdBeginQuery),
272 	MAKE_VULKAN_DEVICE_ENTRY(vkCmdEndQuery),
273 	MAKE_VULKAN_DEVICE_ENTRY(vkCmdResetQueryPool),
274 	MAKE_VULKAN_DEVICE_ENTRY(vkCmdWriteTimestamp),
275 	MAKE_VULKAN_DEVICE_ENTRY(vkCmdCopyQueryPoolResults),
276 	MAKE_VULKAN_DEVICE_ENTRY(vkCmdPushConstants),
277 	MAKE_VULKAN_DEVICE_ENTRY(vkCmdBeginRenderPass),
278 	MAKE_VULKAN_DEVICE_ENTRY(vkCmdNextSubpass),
279 	MAKE_VULKAN_DEVICE_ENTRY(vkCmdEndRenderPass),
280 	MAKE_VULKAN_DEVICE_ENTRY(vkCmdExecuteCommands),
281 	MAKE_VULKAN_DEVICE_ENTRY(vkBindBufferMemory2),
282 	MAKE_VULKAN_DEVICE_ENTRY(vkBindImageMemory2),
283 	MAKE_VULKAN_DEVICE_ENTRY(vkGetDeviceGroupPeerMemoryFeatures),
284 	MAKE_VULKAN_DEVICE_ENTRY(vkCmdSetDeviceMask),
285 	MAKE_VULKAN_DEVICE_ENTRY(vkCmdDispatchBase),
286 	MAKE_VULKAN_DEVICE_ENTRY(vkGetImageMemoryRequirements2),
287 	MAKE_VULKAN_DEVICE_ENTRY(vkGetBufferMemoryRequirements2),
288 	MAKE_VULKAN_DEVICE_ENTRY(vkGetImageSparseMemoryRequirements2),
289 	MAKE_VULKAN_DEVICE_ENTRY(vkTrimCommandPool),
290 	MAKE_VULKAN_DEVICE_ENTRY(vkGetDeviceQueue2),
291 	MAKE_VULKAN_DEVICE_ENTRY(vkCreateSamplerYcbcrConversion),
292 	MAKE_VULKAN_DEVICE_ENTRY(vkDestroySamplerYcbcrConversion),
293 	MAKE_VULKAN_DEVICE_ENTRY(vkCreateDescriptorUpdateTemplate),
294 	MAKE_VULKAN_DEVICE_ENTRY(vkDestroyDescriptorUpdateTemplate),
295 	MAKE_VULKAN_DEVICE_ENTRY(vkUpdateDescriptorSetWithTemplate),
296 	MAKE_VULKAN_DEVICE_ENTRY(vkGetDescriptorSetLayoutSupport),
297 	// Device level VK_EXT_debug_utils functions
298 	MAKE_VULKAN_DEVICE_ENTRY(vkCmdBeginDebugUtilsLabelEXT),
299 	MAKE_VULKAN_DEVICE_ENTRY(vkCmdEndDebugUtilsLabelEXT),
300 	MAKE_VULKAN_DEVICE_ENTRY(vkCmdInsertDebugUtilsLabelEXT),
301 	MAKE_VULKAN_DEVICE_ENTRY(vkQueueBeginDebugUtilsLabelEXT),
302 	MAKE_VULKAN_DEVICE_ENTRY(vkQueueEndDebugUtilsLabelEXT),
303 	MAKE_VULKAN_DEVICE_ENTRY(vkQueueInsertDebugUtilsLabelEXT),
304 	MAKE_VULKAN_DEVICE_ENTRY(vkSetDebugUtilsObjectNameEXT),
305 	MAKE_VULKAN_DEVICE_ENTRY(vkSetDebugUtilsObjectTagEXT),
306 #ifdef __ANDROID__
307 	MAKE_VULKAN_DEVICE_ENTRY(vkGetSwapchainGrallocUsageANDROID),
308 	MAKE_VULKAN_DEVICE_ENTRY(vkGetSwapchainGrallocUsage2ANDROID),
309 	MAKE_VULKAN_DEVICE_ENTRY(vkAcquireImageANDROID),
310 	MAKE_VULKAN_DEVICE_ENTRY(vkQueueSignalReleaseImageANDROID),
311 #endif
312 	// Vulkan 1.2 Entry point functions
313 	MAKE_VULKAN_DEVICE_ENTRY(vkCreateRenderPass2),
314 	MAKE_VULKAN_DEVICE_ENTRY(vkCmdBeginRenderPass2),
315 	MAKE_VULKAN_DEVICE_ENTRY(vkCmdEndRenderPass2),
316 	MAKE_VULKAN_DEVICE_ENTRY(vkCmdNextSubpass2),
317 	MAKE_VULKAN_DEVICE_ENTRY(vkResetQueryPool),
318 	// VK_KHR_timeline_semaphore
319 	MAKE_VULKAN_DEVICE_ENTRY(vkGetSemaphoreCounterValue),
320 	MAKE_VULKAN_DEVICE_ENTRY(vkSignalSemaphore),
321 	MAKE_VULKAN_DEVICE_ENTRY(vkWaitSemaphores),
322 	// VK_KHR_buffer_device_address
323 	MAKE_VULKAN_DEVICE_ENTRY(vkGetBufferDeviceAddress),
324 	MAKE_VULKAN_DEVICE_ENTRY(vkGetBufferOpaqueCaptureAddress),
325 	MAKE_VULKAN_DEVICE_ENTRY(vkGetDeviceMemoryOpaqueCaptureAddress),
326 	// VK_KHR_draw_indirect_count
327 	MAKE_VULKAN_DEVICE_ENTRY(vkCmdDrawIndirectCount),
328 	MAKE_VULKAN_DEVICE_ENTRY(vkCmdDrawIndexedIndirectCount),
329 };
330 
331 static const std::vector<std::pair<const char *, std::unordered_map<std::string, PFN_vkVoidFunction>>> deviceExtensionFunctionPointers = {
332 	// VK_KHR_descriptor_update_template
333 	{
334 	    VK_KHR_DESCRIPTOR_UPDATE_TEMPLATE_EXTENSION_NAME,
335 	    {
336 	        MAKE_VULKAN_DEVICE_ENTRY(vkCreateDescriptorUpdateTemplateKHR),
337 	        MAKE_VULKAN_DEVICE_ENTRY(vkDestroyDescriptorUpdateTemplateKHR),
338 	        MAKE_VULKAN_DEVICE_ENTRY(vkUpdateDescriptorSetWithTemplateKHR),
339 	    } },
340 	// VK_KHR_device_group
341 	{
342 	    VK_KHR_DEVICE_GROUP_EXTENSION_NAME,
343 	    {
344 	        MAKE_VULKAN_DEVICE_ENTRY(vkGetDeviceGroupPeerMemoryFeaturesKHR),
345 	        MAKE_VULKAN_DEVICE_ENTRY(vkCmdSetDeviceMaskKHR),
346 	        MAKE_VULKAN_DEVICE_ENTRY(vkCmdDispatchBaseKHR),
347 	    } },
348 	// VK_KHR_maintenance1
349 	{
350 	    VK_KHR_MAINTENANCE1_EXTENSION_NAME,
351 	    {
352 	        MAKE_VULKAN_DEVICE_ENTRY(vkTrimCommandPoolKHR),
353 	    } },
354 	// VK_KHR_sampler_ycbcr_conversion
355 	{
356 	    VK_KHR_SAMPLER_YCBCR_CONVERSION_EXTENSION_NAME,
357 	    {
358 	        MAKE_VULKAN_DEVICE_ENTRY(vkCreateSamplerYcbcrConversionKHR),
359 	        MAKE_VULKAN_DEVICE_ENTRY(vkDestroySamplerYcbcrConversionKHR),
360 	    } },
361 	// VK_KHR_bind_memory2
362 	{
363 	    VK_KHR_BIND_MEMORY_2_EXTENSION_NAME,
364 	    {
365 	        MAKE_VULKAN_DEVICE_ENTRY(vkBindBufferMemory2KHR),
366 	        MAKE_VULKAN_DEVICE_ENTRY(vkBindImageMemory2KHR),
367 	    } },
368 	// VK_KHR_get_memory_requirements2
369 	{
370 	    VK_KHR_GET_MEMORY_REQUIREMENTS_2_EXTENSION_NAME,
371 	    {
372 	        MAKE_VULKAN_DEVICE_ENTRY(vkGetImageMemoryRequirements2KHR),
373 	        MAKE_VULKAN_DEVICE_ENTRY(vkGetBufferMemoryRequirements2KHR),
374 	        MAKE_VULKAN_DEVICE_ENTRY(vkGetImageSparseMemoryRequirements2KHR),
375 	    } },
376 	// VK_KHR_maintenance3
377 	{
378 	    VK_KHR_MAINTENANCE3_EXTENSION_NAME,
379 	    {
380 	        MAKE_VULKAN_DEVICE_ENTRY(vkGetDescriptorSetLayoutSupportKHR),
381 	    } },
382 	// VK_KHR_create_renderpass2
383 	{
384 	    VK_KHR_CREATE_RENDERPASS_2_EXTENSION_NAME,
385 	    {
386 	        MAKE_VULKAN_DEVICE_ENTRY(vkCreateRenderPass2KHR),
387 	        MAKE_VULKAN_DEVICE_ENTRY(vkCmdBeginRenderPass2KHR),
388 	        MAKE_VULKAN_DEVICE_ENTRY(vkCmdNextSubpass2KHR),
389 	        MAKE_VULKAN_DEVICE_ENTRY(vkCmdEndRenderPass2KHR),
390 	    } },
391 	// VK_KHR_timeline_semaphore
392 	{
393 	    VK_KHR_TIMELINE_SEMAPHORE_EXTENSION_NAME,
394 	    {
395 	        MAKE_VULKAN_DEVICE_ENTRY(vkGetSemaphoreCounterValueKHR),
396 	        MAKE_VULKAN_DEVICE_ENTRY(vkSignalSemaphoreKHR),
397 	        MAKE_VULKAN_DEVICE_ENTRY(vkWaitSemaphoresKHR),
398 	    } },
399 	// VK_KHR_buffer_device_address
400 	{
401 	    VK_KHR_BUFFER_DEVICE_ADDRESS_EXTENSION_NAME,
402 	    {
403 
404 	        MAKE_VULKAN_DEVICE_ENTRY(vkGetBufferDeviceAddressKHR),
405 	        MAKE_VULKAN_DEVICE_ENTRY(vkGetBufferOpaqueCaptureAddressKHR),
406 	        MAKE_VULKAN_DEVICE_ENTRY(vkGetDeviceMemoryOpaqueCaptureAddressKHR),
407 	    } },
408 	// VK_KHR_draw_indirect_count
409 	{
410 	    VK_KHR_DRAW_INDIRECT_COUNT_EXTENSION_NAME,
411 	    {
412 
413 	        MAKE_VULKAN_DEVICE_ENTRY(vkCmdDrawIndirectCountKHR),
414 	        MAKE_VULKAN_DEVICE_ENTRY(vkCmdDrawIndexedIndirectCountKHR),
415 	    } },
416 	// VK_EXT_line_rasterization
417 	{
418 	    VK_EXT_LINE_RASTERIZATION_EXTENSION_NAME,
419 	    {
420 	        MAKE_VULKAN_DEVICE_ENTRY(vkCmdSetLineStippleEXT),
421 	    } },
422 	// VK_EXT_host_query_reset
423 	{
424 	    VK_EXT_HOST_QUERY_RESET_EXTENSION_NAME,
425 	    {
426 	        MAKE_VULKAN_DEVICE_ENTRY(vkResetQueryPoolEXT),
427 	    } },
428 #ifndef __ANDROID__
429 	// VK_KHR_swapchain
430 	{
431 	    VK_KHR_SWAPCHAIN_EXTENSION_NAME,
432 	    {
433 	        MAKE_VULKAN_DEVICE_ENTRY(vkCreateSwapchainKHR),
434 	        MAKE_VULKAN_DEVICE_ENTRY(vkDestroySwapchainKHR),
435 	        MAKE_VULKAN_DEVICE_ENTRY(vkGetSwapchainImagesKHR),
436 	        MAKE_VULKAN_DEVICE_ENTRY(vkAcquireNextImageKHR),
437 	        MAKE_VULKAN_DEVICE_ENTRY(vkAcquireNextImage2KHR),
438 	        MAKE_VULKAN_DEVICE_ENTRY(vkQueuePresentKHR),
439 	        MAKE_VULKAN_DEVICE_ENTRY(vkGetDeviceGroupPresentCapabilitiesKHR),
440 	        MAKE_VULKAN_DEVICE_ENTRY(vkGetDeviceGroupSurfacePresentModesKHR),
441 	    } },
442 #endif
443 
444 #if SWIFTSHADER_EXTERNAL_SEMAPHORE_OPAQUE_FD
445 	// VK_KHR_external_semaphore_fd
446 	{
447 	    VK_KHR_EXTERNAL_SEMAPHORE_FD_EXTENSION_NAME,
448 	    {
449 	        MAKE_VULKAN_DEVICE_ENTRY(vkGetSemaphoreFdKHR),
450 	        MAKE_VULKAN_DEVICE_ENTRY(vkImportSemaphoreFdKHR),
451 	    } },
452 #endif
453 
454 #if VK_USE_PLATFORM_FUCHSIA
455 	// VK_FUCHSIA_external_semaphore
456 	{
457 	    VK_FUCHSIA_EXTERNAL_SEMAPHORE_EXTENSION_NAME,
458 	    {
459 	        MAKE_VULKAN_DEVICE_ENTRY(vkGetSemaphoreZirconHandleFUCHSIA),
460 	        MAKE_VULKAN_DEVICE_ENTRY(vkImportSemaphoreZirconHandleFUCHSIA),
461 	    } },
462 #endif
463 
464 #if SWIFTSHADER_EXTERNAL_MEMORY_OPAQUE_FD
465 	// VK_KHR_external_memory_fd
466 	{
467 	    VK_KHR_EXTERNAL_MEMORY_FD_EXTENSION_NAME,
468 	    {
469 	        MAKE_VULKAN_DEVICE_ENTRY(vkGetMemoryFdKHR),
470 	        MAKE_VULKAN_DEVICE_ENTRY(vkGetMemoryFdPropertiesKHR),
471 	    } },
472 #endif
473 
474 	// VK_EXT_external_memory_host
475 	{
476 	    VK_EXT_EXTERNAL_MEMORY_HOST_EXTENSION_NAME,
477 	    {
478 	        MAKE_VULKAN_DEVICE_ENTRY(vkGetMemoryHostPointerPropertiesEXT),
479 	    } },
480 
481 #if SWIFTSHADER_EXTERNAL_MEMORY_ANDROID_HARDWARE_BUFFER
482 	// VK_ANDROID_external_memory_android_hardware_buffer
483 	{
484 	    VK_ANDROID_EXTERNAL_MEMORY_ANDROID_HARDWARE_BUFFER_EXTENSION_NAME,
485 	    {
486 	        MAKE_VULKAN_DEVICE_ENTRY(vkGetAndroidHardwareBufferPropertiesANDROID),
487 	        MAKE_VULKAN_DEVICE_ENTRY(vkGetMemoryAndroidHardwareBufferANDROID),
488 	    } },
489 #endif
490 
491 #if VK_USE_PLATFORM_FUCHSIA
492 	// VK_FUCHSIA_external_memory
493 	{
494 	    VK_FUCHSIA_EXTERNAL_MEMORY_EXTENSION_NAME,
495 	    {
496 	        MAKE_VULKAN_DEVICE_ENTRY(vkGetMemoryZirconHandleFUCHSIA),
497 	        MAKE_VULKAN_DEVICE_ENTRY(vkGetMemoryZirconHandlePropertiesFUCHSIA),
498 	    } },
499 #endif
500 };
501 
502 #undef MAKE_VULKAN_DEVICE_ENTRY
503 
GetInstanceProcAddr(Instance * instance,const char * pName)504 PFN_vkVoidFunction GetInstanceProcAddr(Instance *instance, const char *pName)
505 {
506 	auto globalFunction = globalFunctionPointers.find(std::string(pName));
507 	if(globalFunction != globalFunctionPointers.end())
508 	{
509 		return globalFunction->second;
510 	}
511 
512 	if(instance)
513 	{
514 		auto instanceFunction = instanceFunctionPointers.find(std::string(pName));
515 		if(instanceFunction != instanceFunctionPointers.end())
516 		{
517 			return instanceFunction->second;
518 		}
519 
520 		auto deviceFunction = deviceFunctionPointers.find(std::string(pName));
521 		if(deviceFunction != deviceFunctionPointers.end())
522 		{
523 			return deviceFunction->second;
524 		}
525 
526 		for(const auto &deviceExtensionFunctions : deviceExtensionFunctionPointers)
527 		{
528 			deviceFunction = deviceExtensionFunctions.second.find(std::string(pName));
529 			if(deviceFunction != deviceExtensionFunctions.second.end())
530 			{
531 				return deviceFunction->second;
532 			}
533 		}
534 	}
535 
536 	return nullptr;
537 }
538 
GetDeviceProcAddr(Device * device,const char * pName)539 PFN_vkVoidFunction GetDeviceProcAddr(Device *device, const char *pName)
540 {
541 	auto deviceFunction = deviceFunctionPointers.find(std::string(pName));
542 	if(deviceFunction != deviceFunctionPointers.end())
543 	{
544 		return deviceFunction->second;
545 	}
546 
547 	for(const auto &deviceExtensionFunctions : deviceExtensionFunctionPointers)
548 	{
549 		if(device->hasExtension(deviceExtensionFunctions.first))
550 		{
551 			deviceFunction = deviceExtensionFunctions.second.find(std::string(pName));
552 			if(deviceFunction != deviceExtensionFunctions.second.end())
553 			{
554 				return deviceFunction->second;
555 			}
556 		}
557 	}
558 
559 	return nullptr;
560 }
561 
562 }  // namespace vk
563 
564 #ifdef __ANDROID__
565 
566 extern "C" hwvulkan_module_t HAL_MODULE_INFO_SYM;
567 
568 namespace {
569 
CloseDevice(struct hw_device_t *)570 int CloseDevice(struct hw_device_t *)
571 {
572 	return 0;
573 }
574 
575 hwvulkan_device_t hal_device = {
576 	.common = {
577 	    .tag = HARDWARE_DEVICE_TAG,
578 	    .version = HWVULKAN_DEVICE_API_VERSION_0_1,
579 	    .module = &HAL_MODULE_INFO_SYM.common,
580 	    .close = CloseDevice,
581 	},
582 	.EnumerateInstanceExtensionProperties = vkEnumerateInstanceExtensionProperties,
583 	.CreateInstance = vkCreateInstance,
584 	.GetInstanceProcAddr = vkGetInstanceProcAddr,
585 };
586 
OpenDevice(const hw_module_t * module,const char * id,hw_device_t ** device)587 int OpenDevice(const hw_module_t *module, const char *id, hw_device_t **device)
588 {
589 	if(strcmp(id, HWVULKAN_DEVICE_0) != 0) return -ENOENT;
590 	*device = &hal_device.common;
591 	return 0;
592 }
593 
594 hw_module_methods_t module_methods = { .open = OpenDevice };
595 
596 }  // namespace
597 
598 extern "C" hwvulkan_module_t HAL_MODULE_INFO_SYM = {
599 	.common = {
600 	    .tag = HARDWARE_MODULE_TAG,
601 	    .module_api_version = HWVULKAN_MODULE_API_VERSION_0_1,
602 	    .hal_api_version = HARDWARE_HAL_API_VERSION,
603 	    .id = HWVULKAN_HARDWARE_MODULE_ID,
604 	    .name = "Swiftshader Pastel",
605 	    .author = "Google",
606 	    .methods = &module_methods,
607 	}
608 };
609 
610 #endif  // __ANDROID__
611 
612 #if VK_USE_PLATFORM_FUCHSIA
613 PFN_vkConnectToService vk::icdFuchsiaServiceConnectCallback = nullptr;
614 #endif
615