1 // Copyright 2017 The Dawn Authors
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 "dawn_native/vulkan/VulkanFunctions.h"
16 
17 #include "common/DynamicLib.h"
18 #include "dawn_native/vulkan/VulkanInfo.h"
19 
20 namespace dawn_native { namespace vulkan {
21 
22 #define GET_GLOBAL_PROC(name)                                                              \
23     do {                                                                                   \
24         name = reinterpret_cast<decltype(name)>(GetInstanceProcAddr(nullptr, "vk" #name)); \
25         if (name == nullptr) {                                                             \
26             return DAWN_INTERNAL_ERROR(std::string("Couldn't get proc vk") + #name);       \
27         }                                                                                  \
28     } while (0)
29 
LoadGlobalProcs(const DynamicLib & vulkanLib)30     MaybeError VulkanFunctions::LoadGlobalProcs(const DynamicLib& vulkanLib) {
31         if (!vulkanLib.GetProc(&GetInstanceProcAddr, "vkGetInstanceProcAddr")) {
32             return DAWN_INTERNAL_ERROR("Couldn't get vkGetInstanceProcAddr");
33         }
34 
35         GET_GLOBAL_PROC(CreateInstance);
36         GET_GLOBAL_PROC(EnumerateInstanceExtensionProperties);
37         GET_GLOBAL_PROC(EnumerateInstanceLayerProperties);
38 
39         // Is not available in Vulkan 1.0, so allow nullptr
40         EnumerateInstanceVersion = reinterpret_cast<decltype(EnumerateInstanceVersion)>(
41             GetInstanceProcAddr(nullptr, "vkEnumerateInstanceVersion"));
42 
43         return {};
44     }
45 
46 #define GET_INSTANCE_PROC_BASE(name, procName)                                                  \
47     do {                                                                                        \
48         name = reinterpret_cast<decltype(name)>(GetInstanceProcAddr(instance, "vk" #procName)); \
49         if (name == nullptr) {                                                                  \
50             return DAWN_INTERNAL_ERROR(std::string("Couldn't get proc vk") + #procName);        \
51         }                                                                                       \
52     } while (0)
53 
54 #define GET_INSTANCE_PROC(name) GET_INSTANCE_PROC_BASE(name, name)
55 #define GET_INSTANCE_PROC_VENDOR(name, vendor) GET_INSTANCE_PROC_BASE(name, name##vendor)
56 
LoadInstanceProcs(VkInstance instance,const VulkanGlobalInfo & globalInfo)57     MaybeError VulkanFunctions::LoadInstanceProcs(VkInstance instance,
58                                                   const VulkanGlobalInfo& globalInfo) {
59         // Load this proc first so that we can destroy the instance even if some other
60         // GET_INSTANCE_PROC fails
61         GET_INSTANCE_PROC(DestroyInstance);
62 
63         GET_INSTANCE_PROC(CreateDevice);
64         GET_INSTANCE_PROC(DestroyDevice);
65         GET_INSTANCE_PROC(EnumerateDeviceExtensionProperties);
66         GET_INSTANCE_PROC(EnumerateDeviceLayerProperties);
67         GET_INSTANCE_PROC(EnumeratePhysicalDevices);
68         GET_INSTANCE_PROC(GetDeviceProcAddr);
69         GET_INSTANCE_PROC(GetPhysicalDeviceFeatures);
70         GET_INSTANCE_PROC(GetPhysicalDeviceFormatProperties);
71         GET_INSTANCE_PROC(GetPhysicalDeviceImageFormatProperties);
72         GET_INSTANCE_PROC(GetPhysicalDeviceMemoryProperties);
73         GET_INSTANCE_PROC(GetPhysicalDeviceProperties);
74         GET_INSTANCE_PROC(GetPhysicalDeviceQueueFamilyProperties);
75         GET_INSTANCE_PROC(GetPhysicalDeviceSparseImageFormatProperties);
76 
77         if (globalInfo.HasExt(InstanceExt::DebugReport)) {
78             GET_INSTANCE_PROC(CreateDebugReportCallbackEXT);
79             GET_INSTANCE_PROC(DebugReportMessageEXT);
80             GET_INSTANCE_PROC(DestroyDebugReportCallbackEXT);
81         }
82 
83         // Vulkan 1.1 is not required to report promoted extensions from 1.0 and is not required to
84         // support the vendor entrypoint in GetProcAddress.
85         if (globalInfo.apiVersion >= VK_MAKE_VERSION(1, 1, 0)) {
86             GET_INSTANCE_PROC(GetPhysicalDeviceExternalBufferProperties);
87         } else if (globalInfo.HasExt(InstanceExt::ExternalMemoryCapabilities)) {
88             GET_INSTANCE_PROC_VENDOR(GetPhysicalDeviceExternalBufferProperties, KHR);
89         }
90 
91         if (globalInfo.apiVersion >= VK_MAKE_VERSION(1, 1, 0)) {
92             GET_INSTANCE_PROC(GetPhysicalDeviceExternalSemaphoreProperties);
93         } else if (globalInfo.HasExt(InstanceExt::ExternalSemaphoreCapabilities)) {
94             GET_INSTANCE_PROC_VENDOR(GetPhysicalDeviceExternalSemaphoreProperties, KHR);
95         }
96 
97         if (globalInfo.apiVersion >= VK_MAKE_VERSION(1, 1, 0)) {
98             GET_INSTANCE_PROC(GetPhysicalDeviceFeatures2);
99             GET_INSTANCE_PROC(GetPhysicalDeviceProperties2);
100             GET_INSTANCE_PROC(GetPhysicalDeviceFormatProperties2);
101             GET_INSTANCE_PROC(GetPhysicalDeviceImageFormatProperties2);
102             GET_INSTANCE_PROC(GetPhysicalDeviceQueueFamilyProperties2);
103             GET_INSTANCE_PROC(GetPhysicalDeviceMemoryProperties2);
104             GET_INSTANCE_PROC(GetPhysicalDeviceSparseImageFormatProperties2);
105         } else if (globalInfo.HasExt(InstanceExt::GetPhysicalDeviceProperties2)) {
106             GET_INSTANCE_PROC_VENDOR(GetPhysicalDeviceFeatures2, KHR);
107             GET_INSTANCE_PROC_VENDOR(GetPhysicalDeviceProperties2, KHR);
108             GET_INSTANCE_PROC_VENDOR(GetPhysicalDeviceFormatProperties2, KHR);
109             GET_INSTANCE_PROC_VENDOR(GetPhysicalDeviceImageFormatProperties2, KHR);
110             GET_INSTANCE_PROC_VENDOR(GetPhysicalDeviceQueueFamilyProperties2, KHR);
111             GET_INSTANCE_PROC_VENDOR(GetPhysicalDeviceMemoryProperties2, KHR);
112             GET_INSTANCE_PROC_VENDOR(GetPhysicalDeviceSparseImageFormatProperties2, KHR);
113         }
114 
115         if (globalInfo.HasExt(InstanceExt::Surface)) {
116             GET_INSTANCE_PROC(DestroySurfaceKHR);
117             GET_INSTANCE_PROC(GetPhysicalDeviceSurfaceSupportKHR);
118             GET_INSTANCE_PROC(GetPhysicalDeviceSurfaceCapabilitiesKHR);
119             GET_INSTANCE_PROC(GetPhysicalDeviceSurfaceFormatsKHR);
120             GET_INSTANCE_PROC(GetPhysicalDeviceSurfacePresentModesKHR);
121         }
122 
123 #if defined(VK_USE_PLATFORM_FUCHSIA)
124         if (globalInfo.HasExt(InstanceExt::FuchsiaImagePipeSurface)) {
125             GET_INSTANCE_PROC(CreateImagePipeSurfaceFUCHSIA);
126         }
127 #endif  // defined(VK_USE_PLATFORM_FUCHSIA)
128 
129 #if defined(DAWN_ENABLE_BACKEND_METAL)
130         if (globalInfo.HasExt(InstanceExt::MetalSurface)) {
131             GET_INSTANCE_PROC(CreateMetalSurfaceEXT);
132         }
133 #endif  // defined(DAWN_ENABLE_BACKEND_METAL)
134 
135 #if defined(DAWN_PLATFORM_WINDOWS)
136         if (globalInfo.HasExt(InstanceExt::Win32Surface)) {
137             GET_INSTANCE_PROC(CreateWin32SurfaceKHR);
138             GET_INSTANCE_PROC(GetPhysicalDeviceWin32PresentationSupportKHR);
139         }
140 #endif  // defined(DAWN_PLATFORM_WINDOWS)
141 
142 #if defined(DAWN_USE_X11)
143         if (globalInfo.HasExt(InstanceExt::XlibSurface)) {
144             GET_INSTANCE_PROC(CreateXlibSurfaceKHR);
145             GET_INSTANCE_PROC(GetPhysicalDeviceXlibPresentationSupportKHR);
146         }
147 #endif  // defined(DAWN_USE_X11)
148         return {};
149     }
150 
151 #define GET_DEVICE_PROC(name)                                                           \
152     do {                                                                                \
153         name = reinterpret_cast<decltype(name)>(GetDeviceProcAddr(device, "vk" #name)); \
154         if (name == nullptr) {                                                          \
155             return DAWN_INTERNAL_ERROR(std::string("Couldn't get proc vk") + #name);    \
156         }                                                                               \
157     } while (0)
158 
LoadDeviceProcs(VkDevice device,const VulkanDeviceInfo & deviceInfo)159     MaybeError VulkanFunctions::LoadDeviceProcs(VkDevice device,
160                                                 const VulkanDeviceInfo& deviceInfo) {
161         GET_DEVICE_PROC(AllocateCommandBuffers);
162         GET_DEVICE_PROC(AllocateDescriptorSets);
163         GET_DEVICE_PROC(AllocateMemory);
164         GET_DEVICE_PROC(BeginCommandBuffer);
165         GET_DEVICE_PROC(BindBufferMemory);
166         GET_DEVICE_PROC(BindImageMemory);
167         GET_DEVICE_PROC(CmdBeginQuery);
168         GET_DEVICE_PROC(CmdBeginRenderPass);
169         GET_DEVICE_PROC(CmdBindDescriptorSets);
170         GET_DEVICE_PROC(CmdBindIndexBuffer);
171         GET_DEVICE_PROC(CmdBindPipeline);
172         GET_DEVICE_PROC(CmdBindVertexBuffers);
173         GET_DEVICE_PROC(CmdBlitImage);
174         GET_DEVICE_PROC(CmdClearAttachments);
175         GET_DEVICE_PROC(CmdClearColorImage);
176         GET_DEVICE_PROC(CmdClearDepthStencilImage);
177         GET_DEVICE_PROC(CmdCopyBuffer);
178         GET_DEVICE_PROC(CmdCopyBufferToImage);
179         GET_DEVICE_PROC(CmdCopyImage);
180         GET_DEVICE_PROC(CmdCopyImageToBuffer);
181         GET_DEVICE_PROC(CmdCopyQueryPoolResults);
182         GET_DEVICE_PROC(CmdDispatch);
183         GET_DEVICE_PROC(CmdDispatchIndirect);
184         GET_DEVICE_PROC(CmdDraw);
185         GET_DEVICE_PROC(CmdDrawIndexed);
186         GET_DEVICE_PROC(CmdDrawIndexedIndirect);
187         GET_DEVICE_PROC(CmdDrawIndirect);
188         GET_DEVICE_PROC(CmdEndQuery);
189         GET_DEVICE_PROC(CmdEndRenderPass);
190         GET_DEVICE_PROC(CmdExecuteCommands);
191         GET_DEVICE_PROC(CmdFillBuffer);
192         GET_DEVICE_PROC(CmdNextSubpass);
193         GET_DEVICE_PROC(CmdPipelineBarrier);
194         GET_DEVICE_PROC(CmdPushConstants);
195         GET_DEVICE_PROC(CmdResetEvent);
196         GET_DEVICE_PROC(CmdResetQueryPool);
197         GET_DEVICE_PROC(CmdResolveImage);
198         GET_DEVICE_PROC(CmdSetBlendConstants);
199         GET_DEVICE_PROC(CmdSetDepthBias);
200         GET_DEVICE_PROC(CmdSetDepthBounds);
201         GET_DEVICE_PROC(CmdSetEvent);
202         GET_DEVICE_PROC(CmdSetLineWidth);
203         GET_DEVICE_PROC(CmdSetScissor);
204         GET_DEVICE_PROC(CmdSetStencilCompareMask);
205         GET_DEVICE_PROC(CmdSetStencilReference);
206         GET_DEVICE_PROC(CmdSetStencilWriteMask);
207         GET_DEVICE_PROC(CmdSetViewport);
208         GET_DEVICE_PROC(CmdUpdateBuffer);
209         GET_DEVICE_PROC(CmdWaitEvents);
210         GET_DEVICE_PROC(CmdWriteTimestamp);
211         GET_DEVICE_PROC(CreateBuffer);
212         GET_DEVICE_PROC(CreateBufferView);
213         GET_DEVICE_PROC(CreateCommandPool);
214         GET_DEVICE_PROC(CreateComputePipelines);
215         GET_DEVICE_PROC(CreateDescriptorPool);
216         GET_DEVICE_PROC(CreateDescriptorSetLayout);
217         GET_DEVICE_PROC(CreateEvent);
218         GET_DEVICE_PROC(CreateFence);
219         GET_DEVICE_PROC(CreateFramebuffer);
220         GET_DEVICE_PROC(CreateGraphicsPipelines);
221         GET_DEVICE_PROC(CreateImage);
222         GET_DEVICE_PROC(CreateImageView);
223         GET_DEVICE_PROC(CreatePipelineCache);
224         GET_DEVICE_PROC(CreatePipelineLayout);
225         GET_DEVICE_PROC(CreateQueryPool);
226         GET_DEVICE_PROC(CreateRenderPass);
227         GET_DEVICE_PROC(CreateSampler);
228         GET_DEVICE_PROC(CreateSemaphore);
229         GET_DEVICE_PROC(CreateShaderModule);
230         GET_DEVICE_PROC(DestroyBuffer);
231         GET_DEVICE_PROC(DestroyBufferView);
232         GET_DEVICE_PROC(DestroyCommandPool);
233         GET_DEVICE_PROC(DestroyDescriptorPool);
234         GET_DEVICE_PROC(DestroyDescriptorSetLayout);
235         GET_DEVICE_PROC(DestroyEvent);
236         GET_DEVICE_PROC(DestroyFence);
237         GET_DEVICE_PROC(DestroyFramebuffer);
238         GET_DEVICE_PROC(DestroyImage);
239         GET_DEVICE_PROC(DestroyImageView);
240         GET_DEVICE_PROC(DestroyPipeline);
241         GET_DEVICE_PROC(DestroyPipelineCache);
242         GET_DEVICE_PROC(DestroyPipelineLayout);
243         GET_DEVICE_PROC(DestroyQueryPool);
244         GET_DEVICE_PROC(DestroyRenderPass);
245         GET_DEVICE_PROC(DestroySampler);
246         GET_DEVICE_PROC(DestroySemaphore);
247         GET_DEVICE_PROC(DestroyShaderModule);
248         GET_DEVICE_PROC(DeviceWaitIdle);
249         GET_DEVICE_PROC(EndCommandBuffer);
250         GET_DEVICE_PROC(FlushMappedMemoryRanges);
251         GET_DEVICE_PROC(FreeCommandBuffers);
252         GET_DEVICE_PROC(FreeDescriptorSets);
253         GET_DEVICE_PROC(FreeMemory);
254         GET_DEVICE_PROC(GetBufferMemoryRequirements);
255         GET_DEVICE_PROC(GetDeviceMemoryCommitment);
256         GET_DEVICE_PROC(GetDeviceQueue);
257         GET_DEVICE_PROC(GetEventStatus);
258         GET_DEVICE_PROC(GetFenceStatus);
259         GET_DEVICE_PROC(GetImageMemoryRequirements);
260         GET_DEVICE_PROC(GetImageSparseMemoryRequirements);
261         GET_DEVICE_PROC(GetImageSubresourceLayout);
262         GET_DEVICE_PROC(GetPipelineCacheData);
263         GET_DEVICE_PROC(GetQueryPoolResults);
264         GET_DEVICE_PROC(GetRenderAreaGranularity);
265         GET_DEVICE_PROC(InvalidateMappedMemoryRanges);
266         GET_DEVICE_PROC(MapMemory);
267         GET_DEVICE_PROC(MergePipelineCaches);
268         GET_DEVICE_PROC(QueueBindSparse);
269         GET_DEVICE_PROC(QueueSubmit);
270         GET_DEVICE_PROC(QueueWaitIdle);
271         GET_DEVICE_PROC(ResetCommandBuffer);
272         GET_DEVICE_PROC(ResetCommandPool);
273         GET_DEVICE_PROC(ResetDescriptorPool);
274         GET_DEVICE_PROC(ResetEvent);
275         GET_DEVICE_PROC(ResetFences);
276         GET_DEVICE_PROC(SetEvent);
277         GET_DEVICE_PROC(UnmapMemory);
278         GET_DEVICE_PROC(UpdateDescriptorSets);
279         GET_DEVICE_PROC(WaitForFences);
280 
281         if (deviceInfo.HasExt(DeviceExt::DebugMarker)) {
282             GET_DEVICE_PROC(CmdDebugMarkerBeginEXT);
283             GET_DEVICE_PROC(CmdDebugMarkerEndEXT);
284             GET_DEVICE_PROC(CmdDebugMarkerInsertEXT);
285         }
286 
287         if (deviceInfo.HasExt(DeviceExt::ExternalMemoryFD)) {
288             GET_DEVICE_PROC(GetMemoryFdKHR);
289             GET_DEVICE_PROC(GetMemoryFdPropertiesKHR);
290         }
291 
292         if (deviceInfo.HasExt(DeviceExt::ExternalSemaphoreFD)) {
293             GET_DEVICE_PROC(ImportSemaphoreFdKHR);
294             GET_DEVICE_PROC(GetSemaphoreFdKHR);
295         }
296 
297 #if VK_USE_PLATFORM_FUCHSIA
298         if (deviceInfo.HasExt(DeviceExt::ExternalMemoryZirconHandle)) {
299             GET_DEVICE_PROC(GetMemoryZirconHandleFUCHSIA);
300             GET_DEVICE_PROC(GetMemoryZirconHandlePropertiesFUCHSIA);
301         }
302 
303         if (deviceInfo.HasExt(DeviceExt::ExternalSemaphoreZirconHandle)) {
304             GET_DEVICE_PROC(ImportSemaphoreZirconHandleFUCHSIA);
305             GET_DEVICE_PROC(GetSemaphoreZirconHandleFUCHSIA);
306         }
307 #endif
308 
309         if (deviceInfo.HasExt(DeviceExt::Swapchain)) {
310             GET_DEVICE_PROC(CreateSwapchainKHR);
311             GET_DEVICE_PROC(DestroySwapchainKHR);
312             GET_DEVICE_PROC(GetSwapchainImagesKHR);
313             GET_DEVICE_PROC(AcquireNextImageKHR);
314             GET_DEVICE_PROC(QueuePresentKHR);
315         }
316 
317         return {};
318     }
319 
320 }}  // namespace dawn_native::vulkan
321