blob: 643911dd463d1e3501ed4036687d1166b36cd3df [file] [log] [blame]
Mark Lobodzinski317574e2016-08-29 14:21:14 -06001/*
2 * Copyright (c) 2015-2016 The Khronos Group Inc.
3 * Copyright (c) 2015-2016 Valve Corporation
4 * Copyright (c) 2015-2016 LunarG, Inc.
5 *
6 * Licensed under the Apache License, Version 2.0 (the "License");
7 * you may not use this file except in compliance with the License.
8 * You may obtain a copy of the License at
9 *
10 * http://www.apache.org/licenses/LICENSE-2.0
11 *
12 * Unless required by applicable law or agreed to in writing, software
13 * distributed under the License is distributed on an "AS IS" BASIS,
14 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15 * See the License for the specific language governing permissions and
16 * limitations under the License.
17 *
18 * Author: Mark Lobodzinski <mark@lunarg.com>
19 */
20
21#define _GNU_SOURCE
22#include <stdio.h>
23#include <stdlib.h>
24#include <string.h>
25#include "vk_loader_platform.h"
26#include "loader.h"
27#include "extensions.h"
28#include <vulkan/vk_icd.h>
Mark Young65cb3662016-11-07 13:27:02 -070029#include "wsi.h"
30
31// Definitions for the VK_EXT_debug_marker extension commands which
32// need to have a terminator function
33
34VKAPI_ATTR VkResult VKAPI_CALL vkDebugMarkerSetObjectTagEXT(
35 VkDevice device, VkDebugMarkerObjectTagInfoEXT *pTagInfo) {
36 const VkLayerDispatchTable *disp = loader_get_dispatch(device);
37 // If this is a physical device, we have to replace it with the proper one
38 // for the next call.
39 if (pTagInfo->objectType ==
40 VK_DEBUG_REPORT_OBJECT_TYPE_PHYSICAL_DEVICE_EXT) {
41 struct loader_physical_device_tramp *phys_dev_tramp =
42 (struct loader_physical_device_tramp *)pTagInfo->object;
43 pTagInfo->object = (uint64_t)phys_dev_tramp->phys_dev;
44 }
45 return disp->DebugMarkerSetObjectTagEXT(device, pTagInfo);
46}
47
48VKAPI_ATTR VkResult VKAPI_CALL terminator_DebugMarkerSetObjectTagEXT(
49 VkDevice device, VkDebugMarkerObjectTagInfoEXT *pTagInfo) {
50 uint32_t icd_index = 0;
51 struct loader_device *dev;
52 struct loader_icd_term *icd_term =
53 loader_get_icd_and_device(device, &dev, &icd_index);
54 // If this is a physical device, we have to replace it with the proper one
55 // for the next call.
56 if (pTagInfo->objectType ==
57 VK_DEBUG_REPORT_OBJECT_TYPE_PHYSICAL_DEVICE_EXT) {
58 struct loader_physical_device_term *phys_dev_term =
59 (struct loader_physical_device_term *)pTagInfo->object;
60 pTagInfo->object = (uint64_t)phys_dev_term->phys_dev;
61
62 // If this is a KHR_surface, and the ICD has created its own, we have to
63 // replace it with the proper one for the next call.
64 } else if (pTagInfo->objectType ==
65 VK_DEBUG_REPORT_OBJECT_TYPE_SURFACE_KHR_EXT) {
66 if (NULL != icd_term && NULL != icd_term->CreateSwapchainKHR) {
67 VkIcdSurface *icd_surface = (VkIcdSurface *)(pTagInfo->object);
68 if (NULL != icd_surface->real_icd_surfaces) {
69 pTagInfo->object =
70 (uint64_t)icd_surface->real_icd_surfaces[icd_index];
71 }
72 }
73 }
74 assert(icd_term != NULL && icd_term->DebugMarkerSetObjectTagEXT &&
75 "loader: null DebugMarkerSetObjectTagEXT ICD pointer");
76 return icd_term->DebugMarkerSetObjectTagEXT(device, pTagInfo);
77}
78
79VKAPI_ATTR VkResult VKAPI_CALL vkDebugMarkerSetObjectNameEXT(
80 VkDevice device, VkDebugMarkerObjectNameInfoEXT *pNameInfo) {
81 const VkLayerDispatchTable *disp = loader_get_dispatch(device);
82 // If this is a physical device, we have to replace it with the proper one
83 // for the next call.
84 if (pNameInfo->objectType ==
85 VK_DEBUG_REPORT_OBJECT_TYPE_PHYSICAL_DEVICE_EXT) {
86 struct loader_physical_device_tramp *phys_dev_tramp =
87 (struct loader_physical_device_tramp *)pNameInfo->object;
88 pNameInfo->object = (uint64_t)phys_dev_tramp->phys_dev;
89 }
90 return disp->DebugMarkerSetObjectNameEXT(device, pNameInfo);
91}
92
93VKAPI_ATTR VkResult VKAPI_CALL terminator_DebugMarkerSetObjectNameEXT(
94 VkDevice device, VkDebugMarkerObjectNameInfoEXT *pNameInfo) {
95 uint32_t icd_index = 0;
96 struct loader_device *dev;
97 struct loader_icd_term *icd_term =
98 loader_get_icd_and_device(device, &dev, &icd_index);
99 // If this is a physical device, we have to replace it with the proper one
100 // for the next call.
101 if (pNameInfo->objectType ==
102 VK_DEBUG_REPORT_OBJECT_TYPE_PHYSICAL_DEVICE_EXT) {
103 struct loader_physical_device_term *phys_dev_term =
104 (struct loader_physical_device_term *)pNameInfo->object;
105 pNameInfo->object = (uint64_t)phys_dev_term->phys_dev;
106
107 // If this is a KHR_surface, and the ICD has created its own, we have to
108 // replace it with the proper one for the next call.
109 } else if (pNameInfo->objectType ==
110 VK_DEBUG_REPORT_OBJECT_TYPE_SURFACE_KHR_EXT) {
111 if (NULL != icd_term && NULL != icd_term->CreateSwapchainKHR) {
112 VkIcdSurface *icd_surface = (VkIcdSurface *)(pNameInfo->object);
113 if (NULL != icd_surface->real_icd_surfaces) {
114 pNameInfo->object =
115 (uint64_t)icd_surface->real_icd_surfaces[icd_index];
116 }
117 }
118 }
119 assert(icd_term != NULL && icd_term->DebugMarkerSetObjectNameEXT &&
120 "loader: null DebugMarkerSetObjectNameEXT ICD pointer");
121 return icd_term->DebugMarkerSetObjectNameEXT(device, pNameInfo);
122}
Mark Lobodzinski317574e2016-08-29 14:21:14 -0600123
Mark Young35159af2016-09-07 08:50:32 -0600124// Definitions for the VK_NV_external_memory_capabilities extension
Mark Lobodzinski317574e2016-08-29 14:21:14 -0600125
126LOADER_EXPORT VKAPI_ATTR VkResult VKAPI_CALL
127vkGetPhysicalDeviceExternalImageFormatPropertiesNV(
128 VkPhysicalDevice physicalDevice, VkFormat format, VkImageType type,
129 VkImageTiling tiling, VkImageUsageFlags usage, VkImageCreateFlags flags,
130 VkExternalMemoryHandleTypeFlagsNV externalHandleType,
131 VkExternalImageFormatPropertiesNV *pExternalImageFormatProperties) {
Mark Lobodzinski317574e2016-08-29 14:21:14 -0600132 const VkLayerInstanceDispatchTable *disp;
133 VkPhysicalDevice unwrapped_phys_dev =
134 loader_unwrap_physical_device(physicalDevice);
135 disp = loader_get_instance_dispatch(physicalDevice);
136
Mark Young5210abc2016-09-08 18:36:32 -0600137 return disp->GetPhysicalDeviceExternalImageFormatPropertiesNV(
138 unwrapped_phys_dev, format, type, tiling, usage, flags,
139 externalHandleType, pExternalImageFormatProperties);
Mark Lobodzinski317574e2016-08-29 14:21:14 -0600140}
141
142VKAPI_ATTR VkResult VKAPI_CALL
143terminator_GetPhysicalDeviceExternalImageFormatPropertiesNV(
144 VkPhysicalDevice physicalDevice, VkFormat format, VkImageType type,
145 VkImageTiling tiling, VkImageUsageFlags usage, VkImageCreateFlags flags,
146 VkExternalMemoryHandleTypeFlagsNV externalHandleType,
147 VkExternalImageFormatPropertiesNV *pExternalImageFormatProperties) {
Mark Young0153e0b2016-11-03 14:27:13 -0600148 struct loader_physical_device_term *phys_dev_term =
149 (struct loader_physical_device_term *)physicalDevice;
150 struct loader_icd_term *icd_term = phys_dev_term->this_icd_term;
Mark Lobodzinski317574e2016-08-29 14:21:14 -0600151
Mark Young0153e0b2016-11-03 14:27:13 -0600152 if (!icd_term->GetPhysicalDeviceExternalImageFormatPropertiesNV) {
James Jones389dc0c2016-08-18 23:41:19 +0100153 if (externalHandleType) {
154 return VK_ERROR_FORMAT_NOT_SUPPORTED;
155 }
Mark Lobodzinski317574e2016-08-29 14:21:14 -0600156
Mark Young0153e0b2016-11-03 14:27:13 -0600157 if (!icd_term->GetPhysicalDeviceImageFormatProperties) {
James Jones389dc0c2016-08-18 23:41:19 +0100158 return VK_ERROR_INITIALIZATION_FAILED;
159 }
160
161 pExternalImageFormatProperties->externalMemoryFeatures = 0;
162 pExternalImageFormatProperties->exportFromImportedHandleTypes = 0;
163 pExternalImageFormatProperties->compatibleHandleTypes = 0;
164
Mark Young0153e0b2016-11-03 14:27:13 -0600165 return icd_term->GetPhysicalDeviceImageFormatProperties(
166 phys_dev_term->phys_dev, format, type, tiling, usage, flags,
James Jones389dc0c2016-08-18 23:41:19 +0100167 &pExternalImageFormatProperties->imageFormatProperties);
168 }
Mark Lobodzinski317574e2016-08-29 14:21:14 -0600169
Mark Young0153e0b2016-11-03 14:27:13 -0600170 return icd_term->GetPhysicalDeviceExternalImageFormatPropertiesNV(
171 phys_dev_term->phys_dev, format, type, tiling, usage, flags,
Mark Lobodzinski317574e2016-08-29 14:21:14 -0600172 externalHandleType, pExternalImageFormatProperties);
173}
174
Mark Lobodzinski0853ad52016-08-29 14:54:34 -0600175// Definitions for the VK_AMD_draw_indirect_count extension
176
Mark Lobodzinski0853ad52016-08-29 14:54:34 -0600177VKAPI_ATTR void VKAPI_CALL vkCmdDrawIndirectCountAMD(
178 VkCommandBuffer commandBuffer, VkBuffer buffer, VkDeviceSize offset,
179 VkBuffer countBuffer, VkDeviceSize countBufferOffset, uint32_t maxDrawCount,
180 uint32_t stride) {
Mark Young5210abc2016-09-08 18:36:32 -0600181 const VkLayerDispatchTable *disp = loader_get_dispatch(commandBuffer);
182 disp->CmdDrawIndirectCountAMD(commandBuffer, buffer, offset, countBuffer,
183 countBufferOffset, maxDrawCount, stride);
Mark Lobodzinski0853ad52016-08-29 14:54:34 -0600184}
185
186VKAPI_ATTR void VKAPI_CALL vkCmdDrawIndexedIndirectCountAMD(
187 VkCommandBuffer commandBuffer, VkBuffer buffer, VkDeviceSize offset,
188 VkBuffer countBuffer, VkDeviceSize countBufferOffset, uint32_t maxDrawCount,
189 uint32_t stride) {
Mark Young5210abc2016-09-08 18:36:32 -0600190 const VkLayerDispatchTable *disp = loader_get_dispatch(commandBuffer);
191 disp->CmdDrawIndexedIndirectCountAMD(commandBuffer, buffer, offset,
192 countBuffer, countBufferOffset,
193 maxDrawCount, stride);
Mark Lobodzinski0853ad52016-08-29 14:54:34 -0600194}
195
Mark Lobodzinski5f208522016-08-29 15:36:23 -0600196#ifdef VK_USE_PLATFORM_WIN32_KHR
197
198// Definitions for the VK_NV_external_memory_win32 extension
199
Mark Lobodzinski5f208522016-08-29 15:36:23 -0600200VKAPI_ATTR VkResult VKAPI_CALL vkGetMemoryWin32HandleNV(
201 VkDevice device, VkDeviceMemory memory,
202 VkExternalMemoryHandleTypeFlagsNV handleType, HANDLE *pHandle) {
Mark Young5210abc2016-09-08 18:36:32 -0600203 const VkLayerDispatchTable *disp = loader_get_dispatch(device);
204 return disp->GetMemoryWin32HandleNV(device, memory, handleType, pHandle);
Mark Lobodzinski5f208522016-08-29 15:36:23 -0600205}
206
207#endif // VK_USE_PLATFORM_WIN32_KHR
208
209// GPA helpers for non-KHR extensions
210
Mark Lobodzinski317574e2016-08-29 14:21:14 -0600211bool extension_instance_gpa(struct loader_instance *ptr_instance,
212 const char *name, void **addr) {
213 *addr = NULL;
214
Mark Young65cb3662016-11-07 13:27:02 -0700215 // Definitions for the VK_EXT_debug_marker extension commands which
216 // need to have a terminator function. Since these are device
217 // commands, we always need to return a valid value for them.
218
219 if (!strcmp("vkDebugMarkerSetObjectTagEXT", name)) {
220 *addr = (void *)vkDebugMarkerSetObjectTagEXT;
221 return true;
222 }
223 if (!strcmp("vkDebugMarkerSetObjectNameEXT", name)) {
224 *addr = (void *)vkDebugMarkerSetObjectNameEXT;
225 return true;
226 }
227
Mark Lobodzinski317574e2016-08-29 14:21:14 -0600228 // Functions for the VK_NV_external_memory_capabilities extension
Mark Lobodzinski0853ad52016-08-29 14:54:34 -0600229
Mark Lobodzinski317574e2016-08-29 14:21:14 -0600230 if (!strcmp("vkGetPhysicalDeviceExternalImageFormatPropertiesNV", name)) {
Mark Young35159af2016-09-07 08:50:32 -0600231 *addr = (ptr_instance->enabled_known_extensions
232 .nv_external_memory_capabilities == 1)
233 ? (void *)vkGetPhysicalDeviceExternalImageFormatPropertiesNV
234 : NULL;
Mark Lobodzinski317574e2016-08-29 14:21:14 -0600235 return true;
236 }
237
Mark Young3ea0ff52016-09-09 07:26:48 -0600238 // Functions for the VK_AMD_draw_indirect_count extension
239
240 if (!strcmp("vkCmdDrawIndirectCountAMD", name)) {
241 *addr = (void *)vkCmdDrawIndirectCountAMD;
242 return true;
243 }
244
245 if (!strcmp("vkCmdDrawIndexedIndirectCountAMD", name)) {
246 *addr = (void *)vkCmdDrawIndexedIndirectCountAMD;
247 return true;
248 }
249
250#ifdef VK_USE_PLATFORM_WIN32_KHR
251
252 // Functions for the VK_NV_external_memory_win32 extension
253
254 if (!strcmp("vkGetMemoryWin32HandleNV", name)) {
255 *addr = (void *)vkGetMemoryWin32HandleNV;
256 return true;
257 }
258
259#endif // VK_USE_PLATFORM_WIN32_KHR
260
Mark Lobodzinski317574e2016-08-29 14:21:14 -0600261 return false;
262}
263
264void extensions_create_instance(struct loader_instance *ptr_instance,
265 const VkInstanceCreateInfo *pCreateInfo) {
Mark Young35159af2016-09-07 08:50:32 -0600266 ptr_instance->enabled_known_extensions.nv_external_memory_capabilities = 0;
Mark Lobodzinski317574e2016-08-29 14:21:14 -0600267
268 for (uint32_t i = 0; i < pCreateInfo->enabledExtensionCount; i++) {
269 if (strcmp(pCreateInfo->ppEnabledExtensionNames[i],
270 VK_NV_EXTERNAL_MEMORY_CAPABILITIES_EXTENSION_NAME) == 0) {
Mark Young35159af2016-09-07 08:50:32 -0600271 ptr_instance->enabled_known_extensions
272 .nv_external_memory_capabilities = 1;
273 return;
274 }
275 }
276}