blob: 524bd40f8c3c203ed1c65e830e9c9999b97de4c3 [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
Courtney Goeltzenleuchter382489d2015-04-10 08:34:15 -060052 layer_initialize_dispatch_table(pTable, gpuw->pGPA, (VkPhysicalGpu) 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
Courtney Goeltzenleuchter382489d2015-04-10 08:34:15 -060064VK_LAYER_EXPORT VkResult VKAPI vkGetExtensionSupport(VkPhysicalGpu gpu, const char* pExtName)
Jon Ashburn227a9422014-11-26 11:10:26 -070065{
Courtney Goeltzenleuchter382489d2015-04-10 08:34:15 -060066 VkResult result;
Jon Ashburn227a9422014-11-26 11:10:26 -070067
Jon Ashburn25566352015-04-02 12:06:28 -060068 /* This entrypoint is NOT going to init it's own dispatch table since loader calls here early */
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -060069 if (!strncmp(pExtName, "vkLayerExtension1", strlen("vkLayerExtension1")))
Jon Ashburn25566352015-04-02 12:06:28 -060070 {
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -060071 result = VK_SUCCESS;
Jon Ashburn25566352015-04-02 12:06:28 -060072 } else if (!strncmp(pExtName, "Basic", strlen("Basic")))
73 {
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -060074 result = VK_SUCCESS;
Jon Ashburn630e44f2015-04-08 21:33:34 -060075 } else if (!tableMap.empty() && (tableMap.find(gpu) != tableMap.end()))
Jon Ashburn25566352015-04-02 12:06:28 -060076 {
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -060077 printf("At start of wrapped vkGetExtensionSupport() call w/ gpu: %p\n", (void*)gpu);
Jon Ashburn630e44f2015-04-08 21:33:34 -060078 VkLayerDispatchTable* pTable = tableMap[gpu];
79 result = pTable->GetExtensionSupport(gpu, pExtName);
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -060080 printf("Completed wrapped vkGetExtensionSupport() call w/ gpu: %p\n", (void*)gpu);
Jon Ashburn25566352015-04-02 12:06:28 -060081 } else
82 {
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -060083 result = VK_ERROR_INVALID_EXTENSION;
Jon Ashburn25566352015-04-02 12:06:28 -060084 }
Jon Ashburn227a9422014-11-26 11:10:26 -070085 return result;
86}
87
Courtney Goeltzenleuchter382489d2015-04-10 08:34:15 -060088VK_LAYER_EXPORT VkResult VKAPI vkCreateDevice(VkPhysicalGpu gpu, const VkDeviceCreateInfo* pCreateInfo, VkDevice* pDevice)
Jon Ashburn227a9422014-11-26 11:10:26 -070089{
Jon Ashburn630e44f2015-04-08 21:33:34 -060090 VkLayerDispatchTable* pTable = tableMap[gpu];
Jon Ashburn227a9422014-11-26 11:10:26 -070091
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -060092 printf("At start of wrapped vkCreateDevice() call w/ gpu: %p\n", (void*)gpu);
Jon Ashburn630e44f2015-04-08 21:33:34 -060093 VkResult result = pTable->CreateDevice(gpu, pCreateInfo, pDevice);
Jon Ashburn227a9422014-11-26 11:10:26 -070094 // create a mapping for the device object into the dispatch table
95 tableMap.emplace(*pDevice, pTable);
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -060096 printf("Completed wrapped vkCreateDevice() call w/ pDevice, Device %p: %p\n", (void*)pDevice, (void *) *pDevice);
Jon Ashburn227a9422014-11-26 11:10:26 -070097 return result;
98}
Courtney Goeltzenleuchter382489d2015-04-10 08:34:15 -060099VK_LAYER_EXPORT VkResult VKAPI vkGetFormatInfo(VkDevice device, VkFormat format, VkFormatInfoType infoType, size_t* pDataSize, void* pData)
Jon Ashburn227a9422014-11-26 11:10:26 -0700100{
Jon Ashburn301c5f02015-04-06 10:58:22 -0600101 VkLayerDispatchTable* pTable = tableMap[device];
Jon Ashburn227a9422014-11-26 11:10:26 -0700102
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -0600103 printf("At start of wrapped vkGetFormatInfo() call w/ device: %p\n", (void*)device);
Courtney Goeltzenleuchter382489d2015-04-10 08:34:15 -0600104 VkResult result = pTable->GetFormatInfo(device, format, infoType, pDataSize, pData);
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -0600105 printf("Completed wrapped vkGetFormatInfo() call w/ device: %p\n", (void*)device);
Jon Ashburn227a9422014-11-26 11:10:26 -0700106 return result;
107}
108
Courtney Goeltzenleuchter382489d2015-04-10 08:34:15 -0600109VK_LAYER_EXPORT VkResult VKAPI vkEnumerateLayers(VkPhysicalGpu gpu, size_t maxLayerCount, size_t maxStringSize, size_t* pOutLayerCount, char* const* pOutLayers, void* pReserved)
Jon Ashburn227a9422014-11-26 11:10:26 -0700110{
111 if (gpu != NULL)
112 {
Jon Ashburn630e44f2015-04-08 21:33:34 -0600113 VkLayerDispatchTable* pTable = initLayerTable((const VkBaseLayerObject *) gpu);
Jon Ashburn227a9422014-11-26 11:10:26 -0700114
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -0600115 printf("At start of wrapped vkEnumerateLayers() call w/ gpu: %p\n", gpu);
Jon Ashburn630e44f2015-04-08 21:33:34 -0600116 VkResult result = pTable->EnumerateLayers(gpu, maxLayerCount, maxStringSize, pOutLayerCount, pOutLayers, pReserved);
Jon Ashburn227a9422014-11-26 11:10:26 -0700117 return result;
118 } else
119 {
120 if (pOutLayerCount == NULL || pOutLayers == NULL || pOutLayers[0] == NULL || pReserved == NULL)
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -0600121 return VK_ERROR_INVALID_POINTER;
Jon Ashburn227a9422014-11-26 11:10:26 -0700122
123 // Example of a layer that is only compatible with Intel's GPUs
Jon Ashburn301c5f02015-04-06 10:58:22 -0600124 VkBaseLayerObject* gpuw = (VkBaseLayerObject*) pReserved;
Courtney Goeltzenleuchter382489d2015-04-10 08:34:15 -0600125 PFN_vkGetGpuInfo fpGetGpuInfo;
126 VkPhysicalGpuProperties gpuProps;
127 size_t dataSize = sizeof(VkPhysicalGpuProperties);
128 fpGetGpuInfo = (PFN_vkGetGpuInfo) gpuw->pGPA((VkPhysicalGpu) gpuw->nextObject, "vkGetGpuInfo");
129 fpGetGpuInfo((VkPhysicalGpu) gpuw->nextObject, VK_INFO_TYPE_PHYSICAL_GPU_PROPERTIES, &dataSize, &gpuProps);
Jon Ashburn227a9422014-11-26 11:10:26 -0700130 if (gpuProps.vendorId == 0x8086)
131 {
132 *pOutLayerCount = 1;
133 strncpy((char *) pOutLayers[0], "Basic", maxStringSize);
134 } else
135 {
136 *pOutLayerCount = 0;
137 }
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -0600138 return VK_SUCCESS;
Jon Ashburn227a9422014-11-26 11:10:26 -0700139 }
140}
141
Courtney Goeltzenleuchter382489d2015-04-10 08:34:15 -0600142VK_LAYER_EXPORT void * VKAPI vkGetProcAddr(VkPhysicalGpu gpu, const char* pName)
Jon Ashburn8d8dad02014-12-01 14:22:40 -0700143{
Jon Ashburn227a9422014-11-26 11:10:26 -0700144 if (gpu == NULL)
145 return NULL;
Chia-I Wue9ae3882015-01-05 09:41:27 +0800146
Jon Ashburn301c5f02015-04-06 10:58:22 -0600147 initLayerTable((const VkBaseLayerObject *) gpu);
Chia-I Wue9ae3882015-01-05 09:41:27 +0800148
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -0600149 if (!strncmp("vkGetProcAddr", pName, sizeof("vkGetProcAddr")))
150 return (void *) vkGetProcAddr;
151 else if (!strncmp("vkCreateDevice", pName, sizeof ("vkCreateDevice")))
152 return (void *) vkCreateDevice;
153 else if (!strncmp("vkGetExtensionSupport", pName, sizeof ("vkGetExtensionSupport")))
154 return (void *) vkGetExtensionSupport;
155 else if (!strncmp("vkEnumerateLayers", pName, sizeof ("vkEnumerateLayers")))
156 return (void *) vkEnumerateLayers;
157 else if (!strncmp("vkGetFormatInfo", pName, sizeof ("vkGetFormatInfo")))
158 return (void *) vkGetFormatInfo;
159 else if (!strncmp("vkLayerExtension1", pName, sizeof("vkLayerExtension1")))
160 return (void *) vkLayerExtension1;
Jon Ashburn227a9422014-11-26 11:10:26 -0700161 else {
Jon Ashburn301c5f02015-04-06 10:58:22 -0600162 VkBaseLayerObject* gpuw = (VkBaseLayerObject *) gpu;
Jon Ashburn227a9422014-11-26 11:10:26 -0700163 if (gpuw->pGPA == NULL)
164 return NULL;
Courtney Goeltzenleuchter382489d2015-04-10 08:34:15 -0600165 return gpuw->pGPA((VkPhysicalGpu) gpuw->nextObject, pName);
Jon Ashburn227a9422014-11-26 11:10:26 -0700166 }
167}