blob: 45cb9998b85dffc280079a7528d3a654ebd05f34 [file] [log] [blame]
Jon Ashburn8d8dad02014-12-01 14:22:40 -07001/*
Jon Ashburn8d8dad02014-12-01 14:22:40 -07002 *
Courtney Goeltzenleuchter8a17da52015-10-29 13:50:34 -06003 * Copyright (C) 2015 Valve Corporation
Michael Lentine92689442015-09-09 12:39:13 -07004 * Copyright (C) 2015 Google Inc.
Jon Ashburn8d8dad02014-12-01 14:22:40 -07005 *
6 * Permission is hereby granted, free of charge, to any person obtaining a
7 * copy of this software and associated documentation files (the "Software"),
8 * to deal in the Software without restriction, including without limitation
9 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
10 * and/or sell copies of the Software, and to permit persons to whom the
11 * Software is furnished to do so, subject to the following conditions:
12 *
13 * The above copyright notice and this permission notice shall be included
14 * in all copies or substantial portions of the Software.
15 *
16 * THE SOFTWARE IS 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. IN NO EVENT SHALL
19 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
21 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
22 * DEALINGS IN THE SOFTWARE.
Courtney Goeltzenleuchter96cd7952015-10-30 11:14:30 -060023 *
24 * Author: David Pinedo <david@lunarg.com>
25 * Author: Jon Ashburn <jon@lunarg.com>
Jon Ashburn8d8dad02014-12-01 14:22:40 -070026 */
Jon Ashburn227a9422014-11-26 11:10:26 -070027#include <string.h>
28#include <stdlib.h>
29#include <assert.h>
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -060030#include "vk_dispatch_table_helper.h"
David Pinedo329ca9e2015-11-06 12:54:48 -070031#include "vulkan/vk_layer.h"
Tobin Ehlis56d204a2015-07-03 10:15:26 -060032#include "vk_layer_table.h"
Courtney Goeltzenleuchter326075a2015-07-05 11:10:06 -060033#include "vk_layer_extension_utils.h"
Jon Ashburn227a9422014-11-26 11:10:26 -070034
David Pinedo07238d42015-07-09 16:23:44 -060035static const VkLayerProperties globalLayerProps[] = {
36 {
Michael Lentine88467fa2016-01-19 14:19:38 -060037 "VK_LAYER_LUNARG_basic",
David Pinedo07238d42015-07-09 16:23:44 -060038 VK_API_VERSION, // specVersion
Courtney Goeltzenleuchter1335a4c2016-01-08 11:50:04 -070039 1, // implementationVersion
40 "LunarG Sample Layer",
David Pinedo07238d42015-07-09 16:23:44 -060041 }
42};
43
Jon Ashburn227a9422014-11-26 11:10:26 -070044
Chia-I Wuaf9e4fd2015-11-06 06:42:02 +080045VK_LAYER_EXPORT VKAPI_ATTR VkResult VKAPI_CALL vkLayerExtension1(VkDevice device)
Jon Ashburn227a9422014-11-26 11:10:26 -070046{
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -060047 printf("In vkLayerExtension1() call w/ device: %p\n", (void*)device);
48 printf("vkLayerExtension1 returning SUCCESS\n");
49 return VK_SUCCESS;
Jon Ashburn227a9422014-11-26 11:10:26 -070050}
51
Courtney Goeltzenleuchter326075a2015-07-05 11:10:06 -060052static const VkLayerProperties basic_physicaldevice_layers[] = {
Courtney Goeltzenleuchter1c7c65d2015-06-10 17:39:03 -060053 {
Michael Lentine5d96e522015-12-11 10:49:51 -080054 "VK_LAYER_LUNARG_basic",
Courtney Goeltzenleuchter326075a2015-07-05 11:10:06 -060055 VK_API_VERSION,
56 VK_MAKE_VERSION(0, 1, 0),
Mark Lobodzinski2e87e612015-12-30 08:16:12 -070057 "Sample layer: basic, implements vkLayerExtension1",
Courtney Goeltzenleuchter1c7c65d2015-06-10 17:39:03 -060058 }
Jon Ashburneb2728b2015-04-10 14:33:07 -060059};
60
Courtney Goeltzenleuchter326075a2015-07-05 11:10:06 -060061/* Must use Vulkan name so that loader finds it */
Chia-I Wuaf9e4fd2015-11-06 06:42:02 +080062VK_LAYER_EXPORT VKAPI_ATTR VkResult VKAPI_CALL vkEnumerateDeviceLayerProperties(
Courtney Goeltzenleuchter326075a2015-07-05 11:10:06 -060063 VkPhysicalDevice physicalDevice,
64 uint32_t* pCount,
65 VkLayerProperties* pProperties)
Jon Ashburneb2728b2015-04-10 14:33:07 -060066{
Courtney Goeltzenleuchter326075a2015-07-05 11:10:06 -060067 /* Mem tracker's physical device layers are the same as global */
68 return util_GetLayerProperties(ARRAY_SIZE(basic_physicaldevice_layers), basic_physicaldevice_layers,
69 pCount, pProperties);
Tony Barbour426b9052015-06-24 16:06:58 -060070}
Jon Ashburneb2728b2015-04-10 14:33:07 -060071
Courtney Goeltzenleuchter326075a2015-07-05 11:10:06 -060072static const VkExtensionProperties basic_physicaldevice_extensions[] = {
73 {
74 "vkLayerExtension1",
75 VK_MAKE_VERSION(0, 1, 0),
Courtney Goeltzenleuchter326075a2015-07-05 11:10:06 -060076 }
77};
78
Chia-I Wuaf9e4fd2015-11-06 06:42:02 +080079VK_LAYER_EXPORT VKAPI_ATTR VkResult VKAPI_CALL vkEnumerateDeviceExtensionProperties(
Courtney Goeltzenleuchter326075a2015-07-05 11:10:06 -060080 VkPhysicalDevice physicalDevice,
81 const char *pLayerName,
82 uint32_t *pCount,
83 VkExtensionProperties *pProperties)
Tony Barbour426b9052015-06-24 16:06:58 -060084{
Jon Ashburnaff81ff2015-11-02 17:37:20 -070085 if (pLayerName == NULL) {
86 return instance_dispatch_table(physicalDevice)->EnumerateDeviceExtensionProperties(
87 physicalDevice,
88 NULL,
89 pCount,
90 pProperties);
91 } else {
92 return util_GetExtensionProperties(ARRAY_SIZE(basic_physicaldevice_extensions),
93 basic_physicaldevice_extensions,
94 pCount, pProperties);
95 }
Jon Ashburneb2728b2015-04-10 14:33:07 -060096}
97
Courtney Goeltzenleuchter2bdf6da2016-01-08 12:18:43 -070098VK_LAYER_EXPORT VKAPI_ATTR VkResult VKAPI_CALL basic_CreateInstance(
99 const VkInstanceCreateInfo* pCreateInfo,
100 const VkAllocationCallbacks* pAllocator,
101 VkInstance* pInstance)
102{
103 VkLayerInstanceCreateInfo *chain_info = get_chain_info(pCreateInfo, VK_LAYER_LINK_INFO);
104
105 assert(chain_info->u.pLayerInfo);
106 PFN_vkGetInstanceProcAddr fpGetInstanceProcAddr = chain_info->u.pLayerInfo->pfnNextGetInstanceProcAddr;
107 PFN_vkCreateInstance fpCreateInstance = (PFN_vkCreateInstance) fpGetInstanceProcAddr(NULL, "vkCreateInstance");
108 if (fpCreateInstance == NULL) {
109 return VK_ERROR_INITIALIZATION_FAILED;
110 }
111
112 // Advance the link info for the next element on the chain
113 chain_info->u.pLayerInfo = chain_info->u.pLayerInfo->pNext;
114
115 VkResult result = fpCreateInstance(pCreateInfo, pAllocator, pInstance);
116 if (result != VK_SUCCESS)
117 return result;
118
119 initInstanceTable(*pInstance, fpGetInstanceProcAddr);
120
121 return result;
122}
123
Chia-I Wuaf9e4fd2015-11-06 06:42:02 +0800124VK_LAYER_EXPORT VKAPI_ATTR VkResult VKAPI_CALL basic_EnumeratePhysicalDevices(
Jon Ashburn2666e2f2015-05-15 15:09:35 -0600125 VkInstance instance,
126 uint32_t* pPhysicalDeviceCount,
127 VkPhysicalDevice* pPhysicalDevices)
128{
Jon Ashburn2666e2f2015-05-15 15:09:35 -0600129 printf("At start of wrapped vkEnumeratePhysicalDevices() call w/ inst: %p\n", (void*)instance);
Jon Ashburn5a10d212015-06-01 10:02:09 -0600130 VkResult result = instance_dispatch_table(instance)->EnumeratePhysicalDevices(instance, pPhysicalDeviceCount, pPhysicalDevices);
Jon Ashburn2666e2f2015-05-15 15:09:35 -0600131 printf("Completed wrapped vkEnumeratePhysicalDevices() call w/ count %u\n", *pPhysicalDeviceCount);
132 return result;
133}
134
Courtney Goeltzenleuchter2bdf6da2016-01-08 12:18:43 -0700135VK_LAYER_EXPORT VKAPI_ATTR VkResult VKAPI_CALL basic_CreateDevice(VkPhysicalDevice physicalDevice, const VkDeviceCreateInfo* pCreateInfo, const VkAllocationCallbacks* pAllocator, VkDevice* pDevice)
Jon Ashburn227a9422014-11-26 11:10:26 -0700136{
Courtney Goeltzenleuchter2bdf6da2016-01-08 12:18:43 -0700137 printf("VK_LAYER_LUNARG_Basic: At start of vkCreateDevice() call w/ gpu: %p\n", (void*)physicalDevice);
138
139 VkLayerDeviceCreateInfo *chain_info = get_chain_info(pCreateInfo, VK_LAYER_LINK_INFO);
140
141 assert(chain_info->u.pLayerInfo);
142 PFN_vkGetInstanceProcAddr fpGetInstanceProcAddr = chain_info->u.pLayerInfo->pfnNextGetInstanceProcAddr;
143 PFN_vkGetDeviceProcAddr fpGetDeviceProcAddr = chain_info->u.pLayerInfo->pfnNextGetDeviceProcAddr;
144 PFN_vkCreateDevice fpCreateDevice = (PFN_vkCreateDevice) fpGetInstanceProcAddr(NULL, "vkCreateDevice");
145 if (fpCreateDevice == NULL) {
146 return VK_ERROR_INITIALIZATION_FAILED;
147 }
148
149 // Advance the link info for the next element on the chain
150 chain_info->u.pLayerInfo = chain_info->u.pLayerInfo->pNext;
151
152 VkResult result = fpCreateDevice(physicalDevice, pCreateInfo, pAllocator, pDevice);
153 if (result != VK_SUCCESS) {
154 return result;
155 }
156
157 initDeviceTable(*pDevice, fpGetDeviceProcAddr);
158
159 printf("VK_LAYER_LUNARG_Basic: Completed vkCreateDevice() call w/ pDevice, Device %p: %p\n", (void*)pDevice, (void *) *pDevice);
Jon Ashburn227a9422014-11-26 11:10:26 -0700160 return result;
161}
Jon Ashburn2666e2f2015-05-15 15:09:35 -0600162
Jon Ashburn17f37372015-05-19 16:34:53 -0600163/* hook DestroyDevice to remove tableMap entry */
Chia-I Wuaf9e4fd2015-11-06 06:42:02 +0800164VK_LAYER_EXPORT VKAPI_ATTR void VKAPI_CALL basic_DestroyDevice(VkDevice device, const VkAllocationCallbacks* pAllocator)
Jon Ashburn17f37372015-05-19 16:34:53 -0600165{
Courtney Goeltzenleuchter9f171942015-06-13 21:22:12 -0600166 dispatch_key key = get_dispatch_key(device);
Chia-I Wu69f40122015-10-26 21:10:41 +0800167 device_dispatch_table(device)->DestroyDevice(device, pAllocator);
Courtney Goeltzenleuchter9f171942015-06-13 21:22:12 -0600168 destroy_device_dispatch_table(key);
Jon Ashburn17f37372015-05-19 16:34:53 -0600169}
170
171/* hook DestroyInstance to remove tableInstanceMap entry */
Chia-I Wuaf9e4fd2015-11-06 06:42:02 +0800172VK_LAYER_EXPORT VKAPI_ATTR void VKAPI_CALL basic_DestroyInstance(VkInstance instance, const VkAllocationCallbacks* pAllocator)
Jon Ashburn17f37372015-05-19 16:34:53 -0600173{
Courtney Goeltzenleuchter9f171942015-06-13 21:22:12 -0600174 dispatch_key key = get_dispatch_key(instance);
Chia-I Wu69f40122015-10-26 21:10:41 +0800175 instance_dispatch_table(instance)->DestroyInstance(instance, pAllocator);
Courtney Goeltzenleuchter9f171942015-06-13 21:22:12 -0600176 destroy_instance_dispatch_table(key);
Jon Ashburn17f37372015-05-19 16:34:53 -0600177}
178
Chia-I Wuaf9e4fd2015-11-06 06:42:02 +0800179VK_LAYER_EXPORT VKAPI_ATTR void VKAPI_CALL basic_GetPhysicalDeviceFormatProperties(VkPhysicalDevice gpu, VkFormat format, VkFormatProperties *pFormatInfo)
Jon Ashburn227a9422014-11-26 11:10:26 -0700180{
Courtney Goeltzenleuchterab36aa62015-07-12 12:40:29 -0600181 printf("At start of wrapped vkGetPhysicalDeviceFormatProperties() call w/ gpu: %p\n", (void*)gpu);
Courtney Goeltzenleuchter01d2ae12015-10-20 16:40:38 -0600182 instance_dispatch_table(gpu)->GetPhysicalDeviceFormatProperties(gpu, format, pFormatInfo);
Courtney Goeltzenleuchterab36aa62015-07-12 12:40:29 -0600183 printf("Completed wrapped vkGetPhysicalDeviceFormatProperties() call w/ gpu: %p\n", (void*)gpu);
Jon Ashburn227a9422014-11-26 11:10:26 -0700184}
185
Chia-I Wuaf9e4fd2015-11-06 06:42:02 +0800186VK_LAYER_EXPORT VKAPI_ATTR PFN_vkVoidFunction VKAPI_CALL vkGetDeviceProcAddr(VkDevice device, const char* pName)
Jon Ashburn8d8dad02014-12-01 14:22:40 -0700187{
Courtney Goeltzenleuchter2bdf6da2016-01-08 12:18:43 -0700188 if (!strcmp("vkGetDeviceProcAddr", pName))
Courtney Goeltzenleuchtera4c8c712015-07-12 14:35:22 -0600189 return (PFN_vkVoidFunction) vkGetDeviceProcAddr;
Jon Ashburn17f37372015-05-19 16:34:53 -0600190 if (!strcmp("vkDestroyDevice", pName))
Courtney Goeltzenleuchtera4c8c712015-07-12 14:35:22 -0600191 return (PFN_vkVoidFunction) basic_DestroyDevice;
Jon Ashburn79b78ac2015-05-05 14:22:52 -0600192 if (!strcmp("vkLayerExtension1", pName))
Courtney Goeltzenleuchtera4c8c712015-07-12 14:35:22 -0600193 return (PFN_vkVoidFunction) vkLayerExtension1;
Courtney Goeltzenleuchter2bdf6da2016-01-08 12:18:43 -0700194
195 if (device == NULL)
196 return NULL;
197
198 if (device_dispatch_table(device)->GetDeviceProcAddr == NULL)
199 return NULL;
200 return device_dispatch_table(device)->GetDeviceProcAddr(device, pName);
Jon Ashburn79b78ac2015-05-05 14:22:52 -0600201}
202
Chia-I Wuaf9e4fd2015-11-06 06:42:02 +0800203VK_LAYER_EXPORT VKAPI_ATTR PFN_vkVoidFunction VKAPI_CALL vkGetInstanceProcAddr(VkInstance instance, const char* pName)
Jon Ashburn79b78ac2015-05-05 14:22:52 -0600204{
Courtney Goeltzenleuchter2bdf6da2016-01-08 12:18:43 -0700205 if (!strcmp("vkGetInstanceProcAddr", pName))
Courtney Goeltzenleuchtera4c8c712015-07-12 14:35:22 -0600206 return (PFN_vkVoidFunction) vkGetInstanceProcAddr;
Courtney Goeltzenleuchter74c4ce92015-09-14 17:22:16 -0600207 if (!strcmp("vkEnumerateDeviceLayerProperties", pName))
208 return (PFN_vkVoidFunction) vkEnumerateDeviceLayerProperties;
209 if (!strcmp("vkEnumerateDeviceExtensionProperties", pName))
210 return (PFN_vkVoidFunction) vkEnumerateDeviceExtensionProperties;
Courtney Goeltzenleuchterab36aa62015-07-12 12:40:29 -0600211 if (!strcmp("vkGetPhysicalDeviceFormatProperties", pName))
Courtney Goeltzenleuchtera4c8c712015-07-12 14:35:22 -0600212 return (PFN_vkVoidFunction) basic_GetPhysicalDeviceFormatProperties;
Courtney Goeltzenleuchter2bdf6da2016-01-08 12:18:43 -0700213 if (!strcmp("vkCreateInstance", pName))
214 return (PFN_vkVoidFunction) basic_CreateInstance;
Jon Ashburn17f37372015-05-19 16:34:53 -0600215 if (!strcmp("vkDestroyInstance", pName))
Courtney Goeltzenleuchtera4c8c712015-07-12 14:35:22 -0600216 return (PFN_vkVoidFunction) basic_DestroyInstance;
Courtney Goeltzenleuchter2bdf6da2016-01-08 12:18:43 -0700217 if (!strcmp("vkCreateDevice", pName))
218 return (PFN_vkVoidFunction) basic_CreateDevice;
Jon Ashburn2666e2f2015-05-15 15:09:35 -0600219 if (!strcmp("vkEnumeratePhysicalDevices", pName))
Courtney Goeltzenleuchtera4c8c712015-07-12 14:35:22 -0600220 return (PFN_vkVoidFunction) basic_EnumeratePhysicalDevices;
Jon Ashburn4f2575f2015-05-28 16:25:02 -0600221
Courtney Goeltzenleuchter2bdf6da2016-01-08 12:18:43 -0700222 if (instance == NULL)
223 return NULL;
224
Courtney Goeltzenleuchter326075a2015-07-05 11:10:06 -0600225 if (instance_dispatch_table(instance)->GetInstanceProcAddr == NULL)
226 return NULL;
227 return instance_dispatch_table(instance)->GetInstanceProcAddr(instance, pName);
Jon Ashburn227a9422014-11-26 11:10:26 -0700228}
David Pinedo07238d42015-07-09 16:23:44 -0600229
Chia-I Wuaf9e4fd2015-11-06 06:42:02 +0800230VK_LAYER_EXPORT VKAPI_ATTR VkResult VKAPI_CALL vkEnumerateInstanceExtensionProperties(const char *pLayerName, uint32_t *pCount, VkExtensionProperties* pProperties)
David Pinedo07238d42015-07-09 16:23:44 -0600231{
232 return util_GetExtensionProperties(0, NULL, pCount, pProperties);
233}
234
Chia-I Wuaf9e4fd2015-11-06 06:42:02 +0800235VK_LAYER_EXPORT VKAPI_ATTR VkResult VKAPI_CALL vkEnumerateInstanceLayerProperties(uint32_t *pCount, VkLayerProperties* pProperties)
David Pinedo07238d42015-07-09 16:23:44 -0600236{
237 return util_GetLayerProperties(ARRAY_SIZE(globalLayerProps), globalLayerProps, pCount, pProperties);
238}