blob: 0a9f918bbd581114a61c7848e064e5dc911f240d [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 =
Karl Schultz94971292016-11-19 09:02:27 -070042 (struct loader_physical_device_tramp *)(uintptr_t)pTagInfo->object;
43 pTagInfo->object = (uint64_t)(uintptr_t)phys_dev_tramp->phys_dev;
Mark Young65cb3662016-11-07 13:27:02 -070044 }
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 =
Karl Schultz94971292016-11-19 09:02:27 -070059 (struct loader_physical_device_term *)(uintptr_t)pTagInfo->object;
60 pTagInfo->object = (uint64_t)(uintptr_t)phys_dev_term->phys_dev;
Mark Young65cb3662016-11-07 13:27:02 -070061
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) {
Karl Schultz94971292016-11-19 09:02:27 -070067 VkIcdSurface *icd_surface =
68 (VkIcdSurface *)(uintptr_t)pTagInfo->object;
Mark Young65cb3662016-11-07 13:27:02 -070069 if (NULL != icd_surface->real_icd_surfaces) {
70 pTagInfo->object =
71 (uint64_t)icd_surface->real_icd_surfaces[icd_index];
72 }
73 }
74 }
75 assert(icd_term != NULL && icd_term->DebugMarkerSetObjectTagEXT &&
76 "loader: null DebugMarkerSetObjectTagEXT ICD pointer");
77 return icd_term->DebugMarkerSetObjectTagEXT(device, pTagInfo);
78}
79
80VKAPI_ATTR VkResult VKAPI_CALL vkDebugMarkerSetObjectNameEXT(
81 VkDevice device, VkDebugMarkerObjectNameInfoEXT *pNameInfo) {
82 const VkLayerDispatchTable *disp = loader_get_dispatch(device);
83 // If this is a physical device, we have to replace it with the proper one
84 // for the next call.
85 if (pNameInfo->objectType ==
86 VK_DEBUG_REPORT_OBJECT_TYPE_PHYSICAL_DEVICE_EXT) {
87 struct loader_physical_device_tramp *phys_dev_tramp =
Karl Schultz94971292016-11-19 09:02:27 -070088 (struct loader_physical_device_tramp *)(uintptr_t)pNameInfo->object;
89 pNameInfo->object = (uint64_t)(uintptr_t)phys_dev_tramp->phys_dev;
Mark Young65cb3662016-11-07 13:27:02 -070090 }
91 return disp->DebugMarkerSetObjectNameEXT(device, pNameInfo);
92}
93
94VKAPI_ATTR VkResult VKAPI_CALL terminator_DebugMarkerSetObjectNameEXT(
95 VkDevice device, VkDebugMarkerObjectNameInfoEXT *pNameInfo) {
96 uint32_t icd_index = 0;
97 struct loader_device *dev;
98 struct loader_icd_term *icd_term =
99 loader_get_icd_and_device(device, &dev, &icd_index);
100 // If this is a physical device, we have to replace it with the proper one
101 // for the next call.
102 if (pNameInfo->objectType ==
103 VK_DEBUG_REPORT_OBJECT_TYPE_PHYSICAL_DEVICE_EXT) {
104 struct loader_physical_device_term *phys_dev_term =
Karl Schultz94971292016-11-19 09:02:27 -0700105 (struct loader_physical_device_term *)(uintptr_t)pNameInfo->object;
106 pNameInfo->object = (uint64_t)(uintptr_t)phys_dev_term->phys_dev;
Mark Young65cb3662016-11-07 13:27:02 -0700107
108 // If this is a KHR_surface, and the ICD has created its own, we have to
109 // replace it with the proper one for the next call.
110 } else if (pNameInfo->objectType ==
111 VK_DEBUG_REPORT_OBJECT_TYPE_SURFACE_KHR_EXT) {
112 if (NULL != icd_term && NULL != icd_term->CreateSwapchainKHR) {
Karl Schultz94971292016-11-19 09:02:27 -0700113 VkIcdSurface *icd_surface =
114 (VkIcdSurface *)(uintptr_t)pNameInfo->object;
Mark Young65cb3662016-11-07 13:27:02 -0700115 if (NULL != icd_surface->real_icd_surfaces) {
116 pNameInfo->object =
Karl Schultz94971292016-11-19 09:02:27 -0700117 (uint64_t)(
118 uintptr_t)icd_surface->real_icd_surfaces[icd_index];
Mark Young65cb3662016-11-07 13:27:02 -0700119 }
120 }
121 }
122 assert(icd_term != NULL && icd_term->DebugMarkerSetObjectNameEXT &&
123 "loader: null DebugMarkerSetObjectNameEXT ICD pointer");
124 return icd_term->DebugMarkerSetObjectNameEXT(device, pNameInfo);
125}
Mark Lobodzinski317574e2016-08-29 14:21:14 -0600126
Mark Young35159af2016-09-07 08:50:32 -0600127// Definitions for the VK_NV_external_memory_capabilities extension
Mark Lobodzinski317574e2016-08-29 14:21:14 -0600128
129LOADER_EXPORT VKAPI_ATTR VkResult VKAPI_CALL
130vkGetPhysicalDeviceExternalImageFormatPropertiesNV(
131 VkPhysicalDevice physicalDevice, VkFormat format, VkImageType type,
132 VkImageTiling tiling, VkImageUsageFlags usage, VkImageCreateFlags flags,
133 VkExternalMemoryHandleTypeFlagsNV externalHandleType,
134 VkExternalImageFormatPropertiesNV *pExternalImageFormatProperties) {
Mark Lobodzinski317574e2016-08-29 14:21:14 -0600135 const VkLayerInstanceDispatchTable *disp;
136 VkPhysicalDevice unwrapped_phys_dev =
137 loader_unwrap_physical_device(physicalDevice);
138 disp = loader_get_instance_dispatch(physicalDevice);
139
Mark Young5210abc2016-09-08 18:36:32 -0600140 return disp->GetPhysicalDeviceExternalImageFormatPropertiesNV(
141 unwrapped_phys_dev, format, type, tiling, usage, flags,
142 externalHandleType, pExternalImageFormatProperties);
Mark Lobodzinski317574e2016-08-29 14:21:14 -0600143}
144
145VKAPI_ATTR VkResult VKAPI_CALL
146terminator_GetPhysicalDeviceExternalImageFormatPropertiesNV(
147 VkPhysicalDevice physicalDevice, VkFormat format, VkImageType type,
148 VkImageTiling tiling, VkImageUsageFlags usage, VkImageCreateFlags flags,
149 VkExternalMemoryHandleTypeFlagsNV externalHandleType,
150 VkExternalImageFormatPropertiesNV *pExternalImageFormatProperties) {
Mark Young0153e0b2016-11-03 14:27:13 -0600151 struct loader_physical_device_term *phys_dev_term =
152 (struct loader_physical_device_term *)physicalDevice;
153 struct loader_icd_term *icd_term = phys_dev_term->this_icd_term;
Mark Lobodzinski317574e2016-08-29 14:21:14 -0600154
Mark Young0153e0b2016-11-03 14:27:13 -0600155 if (!icd_term->GetPhysicalDeviceExternalImageFormatPropertiesNV) {
James Jones389dc0c2016-08-18 23:41:19 +0100156 if (externalHandleType) {
157 return VK_ERROR_FORMAT_NOT_SUPPORTED;
158 }
Mark Lobodzinski317574e2016-08-29 14:21:14 -0600159
Mark Young0153e0b2016-11-03 14:27:13 -0600160 if (!icd_term->GetPhysicalDeviceImageFormatProperties) {
James Jones389dc0c2016-08-18 23:41:19 +0100161 return VK_ERROR_INITIALIZATION_FAILED;
162 }
163
164 pExternalImageFormatProperties->externalMemoryFeatures = 0;
165 pExternalImageFormatProperties->exportFromImportedHandleTypes = 0;
166 pExternalImageFormatProperties->compatibleHandleTypes = 0;
167
Mark Young0153e0b2016-11-03 14:27:13 -0600168 return icd_term->GetPhysicalDeviceImageFormatProperties(
169 phys_dev_term->phys_dev, format, type, tiling, usage, flags,
James Jones389dc0c2016-08-18 23:41:19 +0100170 &pExternalImageFormatProperties->imageFormatProperties);
171 }
Mark Lobodzinski317574e2016-08-29 14:21:14 -0600172
Mark Young0153e0b2016-11-03 14:27:13 -0600173 return icd_term->GetPhysicalDeviceExternalImageFormatPropertiesNV(
174 phys_dev_term->phys_dev, format, type, tiling, usage, flags,
Mark Lobodzinski317574e2016-08-29 14:21:14 -0600175 externalHandleType, pExternalImageFormatProperties);
176}
177
Mark Lobodzinski0853ad52016-08-29 14:54:34 -0600178// Definitions for the VK_AMD_draw_indirect_count extension
179
Mark Lobodzinski0853ad52016-08-29 14:54:34 -0600180VKAPI_ATTR void VKAPI_CALL vkCmdDrawIndirectCountAMD(
181 VkCommandBuffer commandBuffer, VkBuffer buffer, VkDeviceSize offset,
182 VkBuffer countBuffer, VkDeviceSize countBufferOffset, uint32_t maxDrawCount,
183 uint32_t stride) {
Mark Young5210abc2016-09-08 18:36:32 -0600184 const VkLayerDispatchTable *disp = loader_get_dispatch(commandBuffer);
185 disp->CmdDrawIndirectCountAMD(commandBuffer, buffer, offset, countBuffer,
186 countBufferOffset, maxDrawCount, stride);
Mark Lobodzinski0853ad52016-08-29 14:54:34 -0600187}
188
189VKAPI_ATTR void VKAPI_CALL vkCmdDrawIndexedIndirectCountAMD(
190 VkCommandBuffer commandBuffer, VkBuffer buffer, VkDeviceSize offset,
191 VkBuffer countBuffer, VkDeviceSize countBufferOffset, uint32_t maxDrawCount,
192 uint32_t stride) {
Mark Young5210abc2016-09-08 18:36:32 -0600193 const VkLayerDispatchTable *disp = loader_get_dispatch(commandBuffer);
194 disp->CmdDrawIndexedIndirectCountAMD(commandBuffer, buffer, offset,
195 countBuffer, countBufferOffset,
196 maxDrawCount, stride);
Mark Lobodzinski0853ad52016-08-29 14:54:34 -0600197}
198
Mark Lobodzinski5f208522016-08-29 15:36:23 -0600199#ifdef VK_USE_PLATFORM_WIN32_KHR
200
201// Definitions for the VK_NV_external_memory_win32 extension
202
Mark Lobodzinski5f208522016-08-29 15:36:23 -0600203VKAPI_ATTR VkResult VKAPI_CALL vkGetMemoryWin32HandleNV(
204 VkDevice device, VkDeviceMemory memory,
205 VkExternalMemoryHandleTypeFlagsNV handleType, HANDLE *pHandle) {
Mark Young5210abc2016-09-08 18:36:32 -0600206 const VkLayerDispatchTable *disp = loader_get_dispatch(device);
207 return disp->GetMemoryWin32HandleNV(device, memory, handleType, pHandle);
Mark Lobodzinski5f208522016-08-29 15:36:23 -0600208}
209
210#endif // VK_USE_PLATFORM_WIN32_KHR
211
212// GPA helpers for non-KHR extensions
213
Mark Lobodzinski317574e2016-08-29 14:21:14 -0600214bool extension_instance_gpa(struct loader_instance *ptr_instance,
215 const char *name, void **addr) {
216 *addr = NULL;
217
Mark Young65cb3662016-11-07 13:27:02 -0700218 // Definitions for the VK_EXT_debug_marker extension commands which
219 // need to have a terminator function. Since these are device
220 // commands, we always need to return a valid value for them.
221
222 if (!strcmp("vkDebugMarkerSetObjectTagEXT", name)) {
223 *addr = (void *)vkDebugMarkerSetObjectTagEXT;
224 return true;
225 }
226 if (!strcmp("vkDebugMarkerSetObjectNameEXT", name)) {
227 *addr = (void *)vkDebugMarkerSetObjectNameEXT;
228 return true;
229 }
230
Mark Lobodzinski317574e2016-08-29 14:21:14 -0600231 // Functions for the VK_NV_external_memory_capabilities extension
Mark Lobodzinski0853ad52016-08-29 14:54:34 -0600232
Mark Lobodzinski317574e2016-08-29 14:21:14 -0600233 if (!strcmp("vkGetPhysicalDeviceExternalImageFormatPropertiesNV", name)) {
Mark Young35159af2016-09-07 08:50:32 -0600234 *addr = (ptr_instance->enabled_known_extensions
235 .nv_external_memory_capabilities == 1)
236 ? (void *)vkGetPhysicalDeviceExternalImageFormatPropertiesNV
237 : NULL;
Mark Lobodzinski317574e2016-08-29 14:21:14 -0600238 return true;
239 }
240
Mark Young3ea0ff52016-09-09 07:26:48 -0600241 // Functions for the VK_AMD_draw_indirect_count extension
242
243 if (!strcmp("vkCmdDrawIndirectCountAMD", name)) {
244 *addr = (void *)vkCmdDrawIndirectCountAMD;
245 return true;
246 }
247
248 if (!strcmp("vkCmdDrawIndexedIndirectCountAMD", name)) {
249 *addr = (void *)vkCmdDrawIndexedIndirectCountAMD;
250 return true;
251 }
252
253#ifdef VK_USE_PLATFORM_WIN32_KHR
254
255 // Functions for the VK_NV_external_memory_win32 extension
256
257 if (!strcmp("vkGetMemoryWin32HandleNV", name)) {
258 *addr = (void *)vkGetMemoryWin32HandleNV;
259 return true;
260 }
261
262#endif // VK_USE_PLATFORM_WIN32_KHR
263
Mark Lobodzinski317574e2016-08-29 14:21:14 -0600264 return false;
265}
266
267void extensions_create_instance(struct loader_instance *ptr_instance,
268 const VkInstanceCreateInfo *pCreateInfo) {
Mark Young35159af2016-09-07 08:50:32 -0600269 ptr_instance->enabled_known_extensions.nv_external_memory_capabilities = 0;
Mark Lobodzinski317574e2016-08-29 14:21:14 -0600270
271 for (uint32_t i = 0; i < pCreateInfo->enabledExtensionCount; i++) {
272 if (strcmp(pCreateInfo->ppEnabledExtensionNames[i],
273 VK_NV_EXTERNAL_MEMORY_CAPABILITIES_EXTENSION_NAME) == 0) {
Mark Young35159af2016-09-07 08:50:32 -0600274 ptr_instance->enabled_known_extensions
275 .nv_external_memory_capabilities = 1;
276 return;
277 }
278 }
279}