blob: 59ec08d4c72b30482b7018039e051782e572010e [file] [log] [blame]
Jon Ashburn8d8dad02014-12-01 14:22:40 -07001/*
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -06002 * Vulkan
Jon Ashburn8d8dad02014-12-01 14:22:40 -07003 *
4 * Copyright (C) 2014 LunarG, Inc.
5 *
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.
23 */
Jon Ashburn227a9422014-11-26 11:10:26 -070024#include <string.h>
25#include <stdlib.h>
26#include <assert.h>
Ian Elliott81ac44c2015-01-13 17:52:38 -070027#include "loader_platform.h"
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -060028#include "vk_dispatch_table_helper.h"
29#include "vkLayer.h"
Jon Ashburn5a10d212015-06-01 10:02:09 -060030#include "layers_table.h"
Ian Elliott20f06872015-02-12 17:08:34 -070031// The following is #included again to catch certain OS-specific functions
32// being used:
33#include "loader_platform.h"
Jon Ashburn227a9422014-11-26 11:10:26 -070034
Jon Ashburn227a9422014-11-26 11:10:26 -070035
Courtney Goeltzenleuchter382489d2015-04-10 08:34:15 -060036VK_LAYER_EXPORT VkResult VKAPI vkLayerExtension1(VkDevice device)
Jon Ashburn227a9422014-11-26 11:10:26 -070037{
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -060038 printf("In vkLayerExtension1() call w/ device: %p\n", (void*)device);
39 printf("vkLayerExtension1 returning SUCCESS\n");
40 return VK_SUCCESS;
Jon Ashburn227a9422014-11-26 11:10:26 -070041}
42
Jon Ashburneb2728b2015-04-10 14:33:07 -060043#define BASIC_LAYER_EXT_ARRAY_SIZE 2
Courtney Goeltzenleuchter1c7c65d2015-06-10 17:39:03 -060044
45static const VkExtensionProperties basicExts[BASIC_LAYER_EXT_ARRAY_SIZE] = {
46 {
47 VK_STRUCTURE_TYPE_EXTENSION_PROPERTIES,
48 "Basic",
49 0x10,
50 "Sample layer: Basic ",
51// 0,
52// NULL,
53 },
54 {
55 VK_STRUCTURE_TYPE_EXTENSION_PROPERTIES,
56 "vkLayerExtension1",
57 0x10,
58 "Sample layer: Basic",
59// 0,
60// NULL,
61 }
Jon Ashburneb2728b2015-04-10 14:33:07 -060062};
63
64VK_LAYER_EXPORT VkResult VKAPI vkGetGlobalExtensionInfo(
65 VkExtensionInfoType infoType,
66 uint32_t extensionIndex,
67 size_t* pDataSize,
68 void* pData)
69{
Jon Ashburneb2728b2015-04-10 14:33:07 -060070 /* This entrypoint is NOT going to init it's own dispatch table since loader calls here early */
Jon Ashburneb2728b2015-04-10 14:33:07 -060071 uint32_t *count;
72
73 if (pDataSize == NULL)
74 return VK_ERROR_INVALID_POINTER;
75
76 switch (infoType) {
77 case VK_EXTENSION_INFO_TYPE_COUNT:
78 *pDataSize = sizeof(uint32_t);
79 if (pData == NULL)
80 return VK_SUCCESS;
81 count = (uint32_t *) pData;
82 *count = BASIC_LAYER_EXT_ARRAY_SIZE;
83 break;
84 case VK_EXTENSION_INFO_TYPE_PROPERTIES:
85 *pDataSize = sizeof(VkExtensionProperties);
86 if (pData == NULL)
87 return VK_SUCCESS;
88 if (extensionIndex >= BASIC_LAYER_EXT_ARRAY_SIZE)
89 return VK_ERROR_INVALID_VALUE;
Courtney Goeltzenleuchter1c7c65d2015-06-10 17:39:03 -060090 memcpy((VkExtensionProperties *) pData, &basicExts[extensionIndex], sizeof(VkExtensionProperties));
Jon Ashburneb2728b2015-04-10 14:33:07 -060091 break;
92 default:
93 return VK_ERROR_INVALID_VALUE;
94 };
95
96 return VK_SUCCESS;
97}
98
Jon Ashburn2666e2f2015-05-15 15:09:35 -060099VK_LAYER_EXPORT VkResult VKAPI vkEnumeratePhysicalDevices(
100 VkInstance instance,
101 uint32_t* pPhysicalDeviceCount,
102 VkPhysicalDevice* pPhysicalDevices)
103{
Jon Ashburn2666e2f2015-05-15 15:09:35 -0600104 printf("At start of wrapped vkEnumeratePhysicalDevices() call w/ inst: %p\n", (void*)instance);
Jon Ashburn5a10d212015-06-01 10:02:09 -0600105 VkResult result = instance_dispatch_table(instance)->EnumeratePhysicalDevices(instance, pPhysicalDeviceCount, pPhysicalDevices);
Jon Ashburn2666e2f2015-05-15 15:09:35 -0600106 printf("Completed wrapped vkEnumeratePhysicalDevices() call w/ count %u\n", *pPhysicalDeviceCount);
107 return result;
108}
109
Tony Barbour8205d902015-04-16 15:59:00 -0600110VK_LAYER_EXPORT VkResult VKAPI vkCreateDevice(VkPhysicalDevice gpu, const VkDeviceCreateInfo* pCreateInfo, VkDevice* pDevice)
Jon Ashburn227a9422014-11-26 11:10:26 -0700111{
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -0600112 printf("At start of wrapped vkCreateDevice() call w/ gpu: %p\n", (void*)gpu);
Jon Ashburn5a10d212015-06-01 10:02:09 -0600113 VkResult result = instance_dispatch_table(gpu)->CreateDevice(gpu, pCreateInfo, pDevice);
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -0600114 printf("Completed wrapped vkCreateDevice() call w/ pDevice, Device %p: %p\n", (void*)pDevice, (void *) *pDevice);
Jon Ashburn227a9422014-11-26 11:10:26 -0700115 return result;
116}
Jon Ashburn2666e2f2015-05-15 15:09:35 -0600117
Jon Ashburn17f37372015-05-19 16:34:53 -0600118/* hook DestroyDevice to remove tableMap entry */
119VK_LAYER_EXPORT VkResult VKAPI vkDestroyDevice(VkDevice device)
120{
Courtney Goeltzenleuchter9f171942015-06-13 21:22:12 -0600121 dispatch_key key = get_dispatch_key(device);
Jon Ashburn5a10d212015-06-01 10:02:09 -0600122 VkResult res = device_dispatch_table(device)->DestroyDevice(device);
Courtney Goeltzenleuchter9f171942015-06-13 21:22:12 -0600123 destroy_device_dispatch_table(key);
Jon Ashburn17f37372015-05-19 16:34:53 -0600124 return res;
125}
126
127/* hook DestroyInstance to remove tableInstanceMap entry */
128VK_LAYER_EXPORT VkResult VKAPI vkDestroyInstance(VkInstance instance)
129{
Courtney Goeltzenleuchter9f171942015-06-13 21:22:12 -0600130 dispatch_key key = get_dispatch_key(instance);
Jon Ashburn5a10d212015-06-01 10:02:09 -0600131 VkResult res = instance_dispatch_table(instance)->DestroyInstance(instance);
Courtney Goeltzenleuchter9f171942015-06-13 21:22:12 -0600132 destroy_instance_dispatch_table(key);
Jon Ashburn17f37372015-05-19 16:34:53 -0600133 return res;
134}
135
Chris Forbesd7576302015-06-21 22:55:02 +1200136VK_LAYER_EXPORT VkResult VKAPI vkGetPhysicalDeviceFormatInfo(VkPhysicalDevice gpu, VkFormat format, VkFormatProperties *pFormatInfo)
Jon Ashburn227a9422014-11-26 11:10:26 -0700137{
Chris Forbesd7576302015-06-21 22:55:02 +1200138 printf("At start of wrapped vkGetPhysicalDeviceFormatInfo() call w/ gpu: %p\n", (void*)gpu);
139 VkResult result = instance_dispatch_table(gpu)->GetPhysicalDeviceFormatInfo(gpu, format, pFormatInfo);
140 printf("Completed wrapped vkGetPhysicalDeviceFormatInfo() call w/ gpu: %p\n", (void*)gpu);
Jon Ashburn227a9422014-11-26 11:10:26 -0700141 return result;
142}
143
Jon Ashburn1245cec2015-05-18 13:20:15 -0600144VK_LAYER_EXPORT void * VKAPI vkGetDeviceProcAddr(VkDevice device, const char* pName)
Jon Ashburn8d8dad02014-12-01 14:22:40 -0700145{
Jon Ashburn1245cec2015-05-18 13:20:15 -0600146 if (device == NULL)
Jon Ashburn227a9422014-11-26 11:10:26 -0700147 return NULL;
Chia-I Wue9ae3882015-01-05 09:41:27 +0800148
Jon Ashburn4f2575f2015-05-28 16:25:02 -0600149 /* loader uses this to force layer initialization; device object is wrapped */
150 if (!strcmp("vkGetDeviceProcAddr", pName)) {
Jon Ashburn5a10d212015-06-01 10:02:09 -0600151 initDeviceTable((const VkBaseLayerObject *) device);
Jon Ashburn1245cec2015-05-18 13:20:15 -0600152 return (void *) vkGetDeviceProcAddr;
Jon Ashburn4f2575f2015-05-28 16:25:02 -0600153 }
154
Jon Ashburn17f37372015-05-19 16:34:53 -0600155 if (!strcmp("vkDestroyDevice", pName))
156 return (void *) vkDestroyDevice;
Jon Ashburn79b78ac2015-05-05 14:22:52 -0600157 if (!strcmp("vkLayerExtension1", pName))
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -0600158 return (void *) vkLayerExtension1;
Jon Ashburn4f2575f2015-05-28 16:25:02 -0600159 else
160 {
Jon Ashburn5a10d212015-06-01 10:02:09 -0600161 if (device_dispatch_table(device)->GetDeviceProcAddr == NULL)
Jon Ashburn227a9422014-11-26 11:10:26 -0700162 return NULL;
Jon Ashburn5a10d212015-06-01 10:02:09 -0600163 return device_dispatch_table(device)->GetDeviceProcAddr(device, pName);
Jon Ashburn79b78ac2015-05-05 14:22:52 -0600164 }
165}
166
167VK_LAYER_EXPORT void * VKAPI vkGetInstanceProcAddr(VkInstance instance, const char* pName)
168{
169 if (instance == NULL)
170 return NULL;
171
Jon Ashburn4f2575f2015-05-28 16:25:02 -0600172 /* loader uses this to force layer initialization; instance object is wrapped */
173 if (!strcmp("vkGetInstanceProcAddr", pName)) {
Jon Ashburn5a10d212015-06-01 10:02:09 -0600174 initInstanceTable((const VkBaseLayerObject *) instance);
Jon Ashburn79b78ac2015-05-05 14:22:52 -0600175 return (void *) vkGetInstanceProcAddr;
Jon Ashburn4f2575f2015-05-28 16:25:02 -0600176 }
Chris Forbesd7576302015-06-21 22:55:02 +1200177 if (!strcmp("vkGetPhysicalDeviceFormatInfo", pName))
178 return (void *) vkGetPhysicalDeviceFormatInfo;
Jon Ashburn4f2575f2015-05-28 16:25:02 -0600179
Jon Ashburn17f37372015-05-19 16:34:53 -0600180 if (!strcmp("vkDestroyInstance", pName))
181 return (void *) vkDestroyInstance;
Jon Ashburn2666e2f2015-05-15 15:09:35 -0600182 if (!strcmp("vkEnumeratePhysicalDevices", pName))
183 return (void*) vkEnumeratePhysicalDevices;
184 if (!strcmp("vkGetGlobalExtensionInfo", pName))
Jon Ashburn79b78ac2015-05-05 14:22:52 -0600185 return (void*) vkGetGlobalExtensionInfo;
186 if (!strcmp("vkCreateDevice", pName))
187 return (void *) vkCreateDevice;
Jon Ashburn4f2575f2015-05-28 16:25:02 -0600188 else
189 {
Jon Ashburn5a10d212015-06-01 10:02:09 -0600190 if (instance_dispatch_table(instance)->GetInstanceProcAddr == NULL)
Jon Ashburn79b78ac2015-05-05 14:22:52 -0600191 return NULL;
Jon Ashburn5a10d212015-06-01 10:02:09 -0600192 return instance_dispatch_table(instance)->GetInstanceProcAddr(instance, pName);
Jon Ashburn227a9422014-11-26 11:10:26 -0700193 }
Jon Ashburn4f2575f2015-05-28 16:25:02 -0600194
Jon Ashburn227a9422014-11-26 11:10:26 -0700195}