blob: b7ad6b7c79ccd6e640df313920ba990208300c73 [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>
27#include <unordered_map>
Ian Elliott81ac44c2015-01-13 17:52:38 -070028#include "loader_platform.h"
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -060029#include "vk_dispatch_table_helper.h"
30#include "vkLayer.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 Ashburn301c5f02015-04-06 10:58:22 -060035static std::unordered_map<void *, VkLayerDispatchTable *> tableMap;
Jon Ashburn227a9422014-11-26 11:10:26 -070036
Jon Ashburn301c5f02015-04-06 10:58:22 -060037static VkLayerDispatchTable * initLayerTable(const VkBaseLayerObject *gpuw)
Jon Ashburn227a9422014-11-26 11:10:26 -070038{
Jon Ashburn301c5f02015-04-06 10:58:22 -060039 VkLayerDispatchTable *pTable;
Jon Ashburn227a9422014-11-26 11:10:26 -070040
41 assert(gpuw);
Jon Ashburn630e44f2015-04-08 21:33:34 -060042 std::unordered_map<void *, VkLayerDispatchTable *>::const_iterator it = tableMap.find((void *) gpuw->baseObject);
Jon Ashburn227a9422014-11-26 11:10:26 -070043 if (it == tableMap.end())
44 {
Jon Ashburn301c5f02015-04-06 10:58:22 -060045 pTable = new VkLayerDispatchTable;
Jon Ashburn630e44f2015-04-08 21:33:34 -060046 tableMap[(void *) gpuw->baseObject] = pTable;
Jon Ashburn227a9422014-11-26 11:10:26 -070047 } else
48 {
49 return it->second;
50 }
Chia-I Wu0f65b1e2015-01-04 23:11:43 +080051
Tony Barbour8205d902015-04-16 15:59:00 -060052 layer_initialize_dispatch_table(pTable, gpuw->pGPA, (VkPhysicalDevice) gpuw->nextObject);
Chia-I Wu0f65b1e2015-01-04 23:11:43 +080053
Jon Ashburn227a9422014-11-26 11:10:26 -070054 return pTable;
55}
56
Courtney Goeltzenleuchter382489d2015-04-10 08:34:15 -060057VK_LAYER_EXPORT VkResult VKAPI vkLayerExtension1(VkDevice device)
Jon Ashburn227a9422014-11-26 11:10:26 -070058{
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -060059 printf("In vkLayerExtension1() call w/ device: %p\n", (void*)device);
60 printf("vkLayerExtension1 returning SUCCESS\n");
61 return VK_SUCCESS;
Jon Ashburn227a9422014-11-26 11:10:26 -070062}
63
Jon Ashburneb2728b2015-04-10 14:33:07 -060064struct extProps {
65 uint32_t version;
66 const char * const name;
67};
68#define BASIC_LAYER_EXT_ARRAY_SIZE 2
69static const struct extProps basicExts[BASIC_LAYER_EXT_ARRAY_SIZE] = {
70 // TODO what is the version?
71 0x10, "Basic",
72 0x10, "vkLayerExtension1"
73};
74
75VK_LAYER_EXPORT VkResult VKAPI vkGetGlobalExtensionInfo(
76 VkExtensionInfoType infoType,
77 uint32_t extensionIndex,
78 size_t* pDataSize,
79 void* pData)
80{
81 VkResult result;
82
83 /* This entrypoint is NOT going to init it's own dispatch table since loader calls here early */
84 VkExtensionProperties *ext_props;
85 uint32_t *count;
86
87 if (pDataSize == NULL)
88 return VK_ERROR_INVALID_POINTER;
89
90 switch (infoType) {
91 case VK_EXTENSION_INFO_TYPE_COUNT:
92 *pDataSize = sizeof(uint32_t);
93 if (pData == NULL)
94 return VK_SUCCESS;
95 count = (uint32_t *) pData;
96 *count = BASIC_LAYER_EXT_ARRAY_SIZE;
97 break;
98 case VK_EXTENSION_INFO_TYPE_PROPERTIES:
99 *pDataSize = sizeof(VkExtensionProperties);
100 if (pData == NULL)
101 return VK_SUCCESS;
102 if (extensionIndex >= BASIC_LAYER_EXT_ARRAY_SIZE)
103 return VK_ERROR_INVALID_VALUE;
104 ext_props = (VkExtensionProperties *) pData;
105 ext_props->version = basicExts[extensionIndex].version;
106 strncpy(ext_props->extName, basicExts[extensionIndex].name,
107 VK_MAX_EXTENSION_NAME);
108 ext_props->extName[VK_MAX_EXTENSION_NAME - 1] = '\0';
109 break;
110 default:
111 return VK_ERROR_INVALID_VALUE;
112 };
113
114 return VK_SUCCESS;
115}
116
Tony Barbour8205d902015-04-16 15:59:00 -0600117VK_LAYER_EXPORT VkResult VKAPI vkCreateDevice(VkPhysicalDevice gpu, const VkDeviceCreateInfo* pCreateInfo, VkDevice* pDevice)
Jon Ashburn227a9422014-11-26 11:10:26 -0700118{
Jon Ashburn630e44f2015-04-08 21:33:34 -0600119 VkLayerDispatchTable* pTable = tableMap[gpu];
Jon Ashburn227a9422014-11-26 11:10:26 -0700120
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -0600121 printf("At start of wrapped vkCreateDevice() call w/ gpu: %p\n", (void*)gpu);
Jon Ashburn630e44f2015-04-08 21:33:34 -0600122 VkResult result = pTable->CreateDevice(gpu, pCreateInfo, pDevice);
Jon Ashburn227a9422014-11-26 11:10:26 -0700123 // create a mapping for the device object into the dispatch table
124 tableMap.emplace(*pDevice, pTable);
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -0600125 printf("Completed wrapped vkCreateDevice() call w/ pDevice, Device %p: %p\n", (void*)pDevice, (void *) *pDevice);
Jon Ashburn227a9422014-11-26 11:10:26 -0700126 return result;
127}
Courtney Goeltzenleuchter382489d2015-04-10 08:34:15 -0600128VK_LAYER_EXPORT VkResult VKAPI vkGetFormatInfo(VkDevice device, VkFormat format, VkFormatInfoType infoType, size_t* pDataSize, void* pData)
Jon Ashburn227a9422014-11-26 11:10:26 -0700129{
Jon Ashburn301c5f02015-04-06 10:58:22 -0600130 VkLayerDispatchTable* pTable = tableMap[device];
Jon Ashburn227a9422014-11-26 11:10:26 -0700131
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -0600132 printf("At start of wrapped vkGetFormatInfo() call w/ device: %p\n", (void*)device);
Courtney Goeltzenleuchter382489d2015-04-10 08:34:15 -0600133 VkResult result = pTable->GetFormatInfo(device, format, infoType, pDataSize, pData);
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -0600134 printf("Completed wrapped vkGetFormatInfo() call w/ device: %p\n", (void*)device);
Jon Ashburn227a9422014-11-26 11:10:26 -0700135 return result;
136}
137
Tony Barbour8205d902015-04-16 15:59:00 -0600138VK_LAYER_EXPORT VkResult VKAPI vkEnumerateLayers(VkPhysicalDevice gpu, size_t maxLayerCount, size_t maxStringSize, size_t* pOutLayerCount, char* const* pOutLayers, void* pReserved)
Jon Ashburn227a9422014-11-26 11:10:26 -0700139{
140 if (gpu != NULL)
141 {
Jon Ashburn630e44f2015-04-08 21:33:34 -0600142 VkLayerDispatchTable* pTable = initLayerTable((const VkBaseLayerObject *) gpu);
Jon Ashburn227a9422014-11-26 11:10:26 -0700143
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -0600144 printf("At start of wrapped vkEnumerateLayers() call w/ gpu: %p\n", gpu);
Jon Ashburn630e44f2015-04-08 21:33:34 -0600145 VkResult result = pTable->EnumerateLayers(gpu, maxLayerCount, maxStringSize, pOutLayerCount, pOutLayers, pReserved);
Jon Ashburn227a9422014-11-26 11:10:26 -0700146 return result;
147 } else
148 {
149 if (pOutLayerCount == NULL || pOutLayers == NULL || pOutLayers[0] == NULL || pReserved == NULL)
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -0600150 return VK_ERROR_INVALID_POINTER;
Jon Ashburn227a9422014-11-26 11:10:26 -0700151
152 // Example of a layer that is only compatible with Intel's GPUs
Jon Ashburn301c5f02015-04-06 10:58:22 -0600153 VkBaseLayerObject* gpuw = (VkBaseLayerObject*) pReserved;
Tony Barbour8205d902015-04-16 15:59:00 -0600154 PFN_vkGetPhysicalDeviceInfo fpGetGpuInfo;
155 VkPhysicalDeviceProperties gpuProps;
156 size_t dataSize = sizeof(VkPhysicalDeviceProperties);
157 fpGetGpuInfo = (PFN_vkGetPhysicalDeviceInfo) gpuw->pGPA((VkPhysicalDevice) gpuw->nextObject, "vkGetPhysicalDeviceInfo");
158 fpGetGpuInfo((VkPhysicalDevice) gpuw->nextObject, VK_PHYSICAL_DEVICE_INFO_TYPE_PROPERTIES, &dataSize, &gpuProps);
Jon Ashburn227a9422014-11-26 11:10:26 -0700159 if (gpuProps.vendorId == 0x8086)
160 {
161 *pOutLayerCount = 1;
162 strncpy((char *) pOutLayers[0], "Basic", maxStringSize);
163 } else
164 {
165 *pOutLayerCount = 0;
166 }
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -0600167 return VK_SUCCESS;
Jon Ashburn227a9422014-11-26 11:10:26 -0700168 }
169}
170
Tony Barbour8205d902015-04-16 15:59:00 -0600171VK_LAYER_EXPORT void * VKAPI vkGetProcAddr(VkPhysicalDevice gpu, const char* pName)
Jon Ashburn8d8dad02014-12-01 14:22:40 -0700172{
Jon Ashburn227a9422014-11-26 11:10:26 -0700173 if (gpu == NULL)
174 return NULL;
Chia-I Wue9ae3882015-01-05 09:41:27 +0800175
Jon Ashburn301c5f02015-04-06 10:58:22 -0600176 initLayerTable((const VkBaseLayerObject *) gpu);
Chia-I Wue9ae3882015-01-05 09:41:27 +0800177
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -0600178 if (!strncmp("vkGetProcAddr", pName, sizeof("vkGetProcAddr")))
179 return (void *) vkGetProcAddr;
180 else if (!strncmp("vkCreateDevice", pName, sizeof ("vkCreateDevice")))
181 return (void *) vkCreateDevice;
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -0600182 else if (!strncmp("vkEnumerateLayers", pName, sizeof ("vkEnumerateLayers")))
183 return (void *) vkEnumerateLayers;
184 else if (!strncmp("vkGetFormatInfo", pName, sizeof ("vkGetFormatInfo")))
185 return (void *) vkGetFormatInfo;
186 else if (!strncmp("vkLayerExtension1", pName, sizeof("vkLayerExtension1")))
187 return (void *) vkLayerExtension1;
Jon Ashburn227a9422014-11-26 11:10:26 -0700188 else {
Jon Ashburn301c5f02015-04-06 10:58:22 -0600189 VkBaseLayerObject* gpuw = (VkBaseLayerObject *) gpu;
Jon Ashburn227a9422014-11-26 11:10:26 -0700190 if (gpuw->pGPA == NULL)
191 return NULL;
Tony Barbour8205d902015-04-16 15:59:00 -0600192 return gpuw->pGPA((VkPhysicalDevice) gpuw->nextObject, pName);
Jon Ashburn227a9422014-11-26 11:10:26 -0700193 }
194}