blob: 202cb855b3f8c0ee50ad5afccbeac7ba21816592 [file] [log] [blame]
David Pinedofb5b5382015-06-18 17:03:14 -06001/*
David Pinedofb5b5382015-06-18 17:03:14 -06002 *
Courtney Goeltzenleuchterfcbe16f2015-10-29 13:50:34 -06003 * Copyright (C) 2015 Valve Corporation
David Pinedofb5b5382015-06-18 17:03:14 -06004 *
5 * Permission is hereby granted, free of charge, to any person obtaining a
6 * copy of this software and associated documentation files (the "Software"),
7 * to deal in the Software without restriction, including without limitation
8 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
9 * and/or sell copies of the Software, and to permit persons to whom the
10 * Software is furnished to do so, subject to the following conditions:
11 *
12 * The above copyright notice and this permission notice shall be included
13 * in all copies or substantial portions of the Software.
14 *
15 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
18 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
20 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
21 * DEALINGS IN THE SOFTWARE.
Courtney Goeltzenleuchter05559522015-10-30 11:14:30 -060022 *
23 * Author: Cody Northrop <cody@lunarg.com>
24 * Author: David Pinedo <david@lunarg.com>
David Pinedofb5b5382015-06-18 17:03:14 -060025 */
26
27#include <inttypes.h>
28#include <stdio.h>
29#include <stdlib.h>
30#include <string.h>
31#include <assert.h>
32#include <unordered_map>
33#include <iostream>
34#include <algorithm>
35#include <list>
36#include <map>
37#include <vector>
38#include <fstream>
39
40using namespace std;
41
Tobin Ehlisb835d1b2015-07-03 10:34:49 -060042#include "vk_loader_platform.h"
David Pinedofb5b5382015-06-18 17:03:14 -060043#include "vk_dispatch_table_helper.h"
44#include "vk_struct_string_helper_cpp.h"
Tobin Ehlisa0cb02e2015-07-03 10:15:26 -060045#include "vk_layer_config.h"
Tobin Ehlisa0cb02e2015-07-03 10:15:26 -060046#include "vk_layer_table.h"
Courtney Goeltzenleuchtera6db3462015-07-07 11:25:43 -060047#include "vk_layer_extension_utils.h"
David Pinedofb5b5382015-06-18 17:03:14 -060048
David Pinedobcd0e792015-06-19 13:48:18 -060049
50struct devExts {
Ian Elliott1064fe32015-07-06 14:31:32 -060051 bool wsi_enabled;
David Pinedobcd0e792015-06-19 13:48:18 -060052};
53static std::unordered_map<void *, struct devExts> deviceExtMap;
David Pinedofb5b5382015-06-18 17:03:14 -060054static device_table_map screenshot_device_table_map;
David Pinedofb5b5382015-06-18 17:03:14 -060055
56static int globalLockInitialized = 0;
57static loader_platform_thread_mutex globalLock;
58
Cody Northrop49f885c2015-09-01 10:18:45 -060059// unordered map: associates a swap chain with a device, image extent, format, and
David Pinedo8897e192015-07-31 10:56:20 -060060// list of images
David Pinedofb5b5382015-06-18 17:03:14 -060061typedef struct
62{
63 VkDevice device;
64 VkExtent2D imageExtent;
65 VkFormat format;
David Pinedo8897e192015-07-31 10:56:20 -060066 VkImage *imageList;
David Pinedofb5b5382015-06-18 17:03:14 -060067} SwapchainMapStruct;
Chia-I Wue2fc5522015-10-26 20:04:44 +080068static unordered_map<VkSwapchainKHR, SwapchainMapStruct *> swapchainMap;
David Pinedofb5b5382015-06-18 17:03:14 -060069
Cody Northrop49f885c2015-09-01 10:18:45 -060070// unordered map: associates an image with a device, image extent, and format
David Pinedofb5b5382015-06-18 17:03:14 -060071typedef struct
72{
73 VkDevice device;
74 VkExtent2D imageExtent;
75 VkFormat format;
76} ImageMapStruct;
Chia-I Wue2fc5522015-10-26 20:04:44 +080077static unordered_map<VkImage, ImageMapStruct *> imageMap;
David Pinedofb5b5382015-06-18 17:03:14 -060078
Chia-I Wu3432a0c2015-10-27 18:04:07 +080079// unordered map: associates a device with a queue, commandPool, and physical device
David Pinedofb5b5382015-06-18 17:03:14 -060080typedef struct
81{
82 VkQueue queue;
Chia-I Wu3432a0c2015-10-27 18:04:07 +080083 VkCommandPool commandPool;
Cody Northrop49f885c2015-09-01 10:18:45 -060084 VkPhysicalDevice physicalDevice;
David Pinedofb5b5382015-06-18 17:03:14 -060085} DeviceMapStruct;
86static unordered_map<VkDevice, DeviceMapStruct *> deviceMap;
87
Cody Northrop49f885c2015-09-01 10:18:45 -060088// unordered map: associates a physical device with an instance
89typedef struct
90{
91 VkInstance instance;
92} PhysDeviceMapStruct;
93static unordered_map<VkPhysicalDevice, PhysDeviceMapStruct *> physDeviceMap;
94
David Pinedofb5b5382015-06-18 17:03:14 -060095// List of frames to we will get a screenshot of
96static vector<int> screenshotFrames;
97
98// Flag indicating we have queried _VK_SCREENSHOT env var
99static bool screenshotEnvQueried = false;
100
Courtney Goeltzenleuchter1d2f0dd2015-10-22 11:03:31 -0600101static bool memory_type_from_properties(
Cody Northrop49f885c2015-09-01 10:18:45 -0600102 VkPhysicalDeviceMemoryProperties *memory_properties,
103 uint32_t typeBits,
Tony Barbouraf392d02015-10-15 12:42:56 -0600104 VkFlags requirements_mask,
Cody Northrop49f885c2015-09-01 10:18:45 -0600105 uint32_t *typeIndex)
106{
107 // Search memtypes to find first index with those properties
108 for (uint32_t i = 0; i < 32; i++) {
109 if ((typeBits & 1) == 1) {
110 // Type is available, does it match user properties?
Tony Barbouraf392d02015-10-15 12:42:56 -0600111 if ((memory_properties->memoryTypes[i].propertyFlags & requirements_mask) == requirements_mask) {
Cody Northrop49f885c2015-09-01 10:18:45 -0600112 *typeIndex = i;
Courtney Goeltzenleuchter1d2f0dd2015-10-22 11:03:31 -0600113 return true;
Cody Northrop49f885c2015-09-01 10:18:45 -0600114 }
115 }
116 typeBits >>= 1;
117 }
118 // No memory types matched, return failure
Courtney Goeltzenleuchter1d2f0dd2015-10-22 11:03:31 -0600119 return false;
Cody Northrop49f885c2015-09-01 10:18:45 -0600120}
121
David Pinedofb5b5382015-06-18 17:03:14 -0600122static void init_screenshot()
123{
David Pinedofb5b5382015-06-18 17:03:14 -0600124 if (!globalLockInitialized)
125 {
126 // TODO/TBD: Need to delete this mutex sometime. How??? One
127 // suggestion is to call this during vkCreateInstance(), and then we
128 // can clean it up during vkDestroyInstance(). However, that requires
129 // that the layer have per-instance locks. We need to come back and
130 // address this soon.
131 loader_platform_thread_create_mutex(&globalLock);
132 globalLockInitialized = 1;
133 }
134}
135
136static void writePPM( const char *filename, VkImage image1)
137{
138 VkImage image2;
139 VkResult err;
Courtney Goeltzenleuchter1d2f0dd2015-10-22 11:03:31 -0600140 bool pass;
David Pinedofb5b5382015-06-18 17:03:14 -0600141 int x, y;
142 const char *ptr;
143 VkDeviceMemory mem2;
Chia-I Wu3432a0c2015-10-27 18:04:07 +0800144 VkCommandBuffer commandBuffer;
Chia-I Wue2fc5522015-10-26 20:04:44 +0800145 VkDevice device = imageMap[image1]->device;
Cody Northrop49f885c2015-09-01 10:18:45 -0600146 VkPhysicalDevice physicalDevice = deviceMap[device]->physicalDevice;
147 VkInstance instance = physDeviceMap[physicalDevice]->instance;
David Pinedofb5b5382015-06-18 17:03:14 -0600148 VkQueue queue = deviceMap[device]->queue;
Chia-I Wue2fc5522015-10-26 20:04:44 +0800149 int width = imageMap[image1]->imageExtent.width;
150 int height = imageMap[image1]->imageExtent.height;
151 VkFormat format = imageMap[image1]->format;
Courtney Goeltzenleuchter908e7672015-10-21 17:00:51 -0600152 const VkImageSubresource sr = {VK_IMAGE_ASPECT_COLOR_BIT, 0, 0};
David Pinedofb5b5382015-06-18 17:03:14 -0600153 VkSubresourceLayout sr_layout;
David Pinedofb5b5382015-06-18 17:03:14 -0600154 const VkImageCreateInfo imgCreateInfo = {
155 VK_STRUCTURE_TYPE_IMAGE_CREATE_INFO,
156 NULL,
Chia-I Wu439c0772015-10-26 17:00:46 +0800157 0,
David Pinedofb5b5382015-06-18 17:03:14 -0600158 VK_IMAGE_TYPE_2D,
159 format,
160 {width, height, 1},
161 1,
162 1,
Chia-I Wu5c17c962015-10-31 00:31:16 +0800163 VK_SAMPLE_COUNT_1_BIT,
David Pinedofb5b5382015-06-18 17:03:14 -0600164 VK_IMAGE_TILING_LINEAR,
Chia-I Wu3432a0c2015-10-27 18:04:07 +0800165 (VK_IMAGE_USAGE_TRANSFER_DST_BIT|VK_IMAGE_USAGE_STORAGE_BIT),
David Pinedofb5b5382015-06-18 17:03:14 -0600166 };
Chia-I Wu3432a0c2015-10-27 18:04:07 +0800167 VkMemoryAllocateInfo memAllocInfo = {
Chia-I Wu00ce5402015-11-10 16:21:09 +0800168 VK_STRUCTURE_TYPE_MEMORY_ALLOCATE_INFO,
David Pinedofb5b5382015-06-18 17:03:14 -0600169 NULL,
170 0, // allocationSize, queried later
Cody Northrop488f5472015-09-01 11:47:50 -0600171 0 // memoryTypeIndex, queried later
David Pinedofb5b5382015-06-18 17:03:14 -0600172 };
Chia-I Wu3432a0c2015-10-27 18:04:07 +0800173 const VkCommandBufferAllocateInfo allocCommandBufferInfo = {
Chia-I Wu00ce5402015-11-10 16:21:09 +0800174 VK_STRUCTURE_TYPE_COMMAND_BUFFER_ALLOCATE_INFO,
David Pinedofb5b5382015-06-18 17:03:14 -0600175 NULL,
Chia-I Wu3432a0c2015-10-27 18:04:07 +0800176 deviceMap[device]->commandPool,
177 VK_COMMAND_BUFFER_LEVEL_PRIMARY,
Courtney Goeltzenleuchterbee18a92015-10-23 14:21:05 -0600178 1
David Pinedofb5b5382015-06-18 17:03:14 -0600179 };
Chia-I Wu3432a0c2015-10-27 18:04:07 +0800180 const VkCommandBufferBeginInfo commandBufferBeginInfo = {
181 VK_STRUCTURE_TYPE_COMMAND_BUFFER_BEGIN_INFO,
David Pinedofb5b5382015-06-18 17:03:14 -0600182 NULL,
Chia-I Wu3432a0c2015-10-27 18:04:07 +0800183 VK_COMMAND_BUFFER_USAGE_ONE_TIME_SUBMIT_BIT,
David Pinedofb5b5382015-06-18 17:03:14 -0600184 };
185 const VkImageCopy imageCopyRegion = {
Courtney Goeltzenleuchter908e7672015-10-21 17:00:51 -0600186 {VK_IMAGE_ASPECT_COLOR_BIT, 0, 0},
David Pinedofb5b5382015-06-18 17:03:14 -0600187 {0, 0, 0},
Courtney Goeltzenleuchter908e7672015-10-21 17:00:51 -0600188 {VK_IMAGE_ASPECT_COLOR_BIT, 0, 0},
David Pinedofb5b5382015-06-18 17:03:14 -0600189 {0, 0, 0},
190 {width, height, 1}
191 };
192 VkMemoryRequirements memRequirements;
David Pinedofb5b5382015-06-18 17:03:14 -0600193 uint32_t num_allocations = 0;
194 size_t num_alloc_size = sizeof(num_allocations);
David Pinedo68295872015-06-30 13:08:37 -0600195 VkLayerDispatchTable* pTableDevice = get_dispatch_table(screenshot_device_table_map, device);
196 VkLayerDispatchTable* pTableQueue = get_dispatch_table(screenshot_device_table_map, queue);
Cody Northrop49f885c2015-09-01 10:18:45 -0600197 VkLayerInstanceDispatchTable* pInstanceTable;
Chia-I Wu3432a0c2015-10-27 18:04:07 +0800198 VkLayerDispatchTable* pTableCommandBuffer;
Cody Northrop49f885c2015-09-01 10:18:45 -0600199 VkPhysicalDeviceMemoryProperties memory_properties;
David Pinedofb5b5382015-06-18 17:03:14 -0600200
Chia-I Wue2fc5522015-10-26 20:04:44 +0800201 if (imageMap.empty() || imageMap.find(image1) == imageMap.end())
David Pinedofb5b5382015-06-18 17:03:14 -0600202 return;
203
204 // The VkImage image1 we are going to dump may not be mappable,
205 // and/or it may have a tiling mode of optimal rather than linear.
206 // To make sure we have an image that we can map and read linearly, we:
207 // create image2 that is mappable and linear
208 // copy image1 to image2
209 // map image2
210 // read from image2's mapped memeory.
211
Chia-I Wuf7458c52015-10-26 21:10:41 +0800212 err = pTableDevice->CreateImage(device, &imgCreateInfo, NULL, &image2);
David Pinedofb5b5382015-06-18 17:03:14 -0600213 assert(!err);
214
Courtney Goeltzenleuchter06d89472015-10-20 16:40:38 -0600215 pTableDevice->GetImageMemoryRequirements(device, image2, &memRequirements);
David Pinedofb5b5382015-06-18 17:03:14 -0600216
217 memAllocInfo.allocationSize = memRequirements.size;
Cody Northrop49f885c2015-09-01 10:18:45 -0600218 pInstanceTable = instance_dispatch_table(instance);
Courtney Goeltzenleuchter06d89472015-10-20 16:40:38 -0600219 pInstanceTable->GetPhysicalDeviceMemoryProperties(physicalDevice, &memory_properties);
Cody Northrop49f885c2015-09-01 10:18:45 -0600220
Courtney Goeltzenleuchter1d2f0dd2015-10-22 11:03:31 -0600221 pass = memory_type_from_properties(&memory_properties,
Cody Northrop49f885c2015-09-01 10:18:45 -0600222 memRequirements.memoryTypeBits,
Cody Northrop488f5472015-09-01 11:47:50 -0600223 VK_MEMORY_PROPERTY_HOST_VISIBLE_BIT,
Cody Northrop49f885c2015-09-01 10:18:45 -0600224 &memAllocInfo.memoryTypeIndex);
Courtney Goeltzenleuchter1d2f0dd2015-10-22 11:03:31 -0600225 assert(pass);
Cody Northrop49f885c2015-09-01 10:18:45 -0600226
Chia-I Wu3432a0c2015-10-27 18:04:07 +0800227 err = pTableDevice->AllocateMemory(device, &memAllocInfo, NULL, &mem2);
David Pinedofb5b5382015-06-18 17:03:14 -0600228 assert(!err);
229
David Pinedo8897e192015-07-31 10:56:20 -0600230 err = pTableQueue->BindImageMemory(device, image2, mem2, 0);
David Pinedofb5b5382015-06-18 17:03:14 -0600231 assert(!err);
232
Chia-I Wu3432a0c2015-10-27 18:04:07 +0800233 err = pTableDevice->AllocateCommandBuffers(device, &allocCommandBufferInfo, &commandBuffer);
David Pinedofb5b5382015-06-18 17:03:14 -0600234 assert(!err);
235
Chia-I Wu3432a0c2015-10-27 18:04:07 +0800236 screenshot_device_table_map.emplace(commandBuffer, pTableDevice);
237 pTableCommandBuffer = screenshot_device_table_map[commandBuffer];
David Pinedofb5b5382015-06-18 17:03:14 -0600238
Chia-I Wu3432a0c2015-10-27 18:04:07 +0800239 err = pTableCommandBuffer->BeginCommandBuffer(commandBuffer, &commandBufferBeginInfo);
David Pinedofb5b5382015-06-18 17:03:14 -0600240 assert(!err);
241
Cody Northrop488f5472015-09-01 11:47:50 -0600242 // TODO: We need to transition images to match these layouts, then restore the original layouts
Chia-I Wu3432a0c2015-10-27 18:04:07 +0800243 pTableCommandBuffer->CmdCopyImage(commandBuffer, image1, VK_IMAGE_LAYOUT_TRANSFER_SRC_OPTIMAL,
244 image2, VK_IMAGE_LAYOUT_TRANSFER_DST_OPTIMAL, 1, &imageCopyRegion);
David Pinedofb5b5382015-06-18 17:03:14 -0600245
Chia-I Wu3432a0c2015-10-27 18:04:07 +0800246 err = pTableCommandBuffer->EndCommandBuffer(commandBuffer);
David Pinedofb5b5382015-06-18 17:03:14 -0600247 assert(!err);
248
Courtney Goeltzenleuchter646b9072015-10-20 18:04:07 -0600249 VkFence nullFence = { VK_NULL_HANDLE };
Courtney Goeltzenleuchter806c7002015-10-27 11:22:14 -0600250 VkSubmitInfo submit_info;
Chia-I Wuf9be13c2015-10-26 20:37:06 +0800251 submit_info.sType = VK_STRUCTURE_TYPE_SUBMIT_INFO;
252 submit_info.pNext = NULL;
Chia-I Wud50a7d72015-10-26 20:48:51 +0800253 submit_info.waitSemaphoreCount = 0;
Courtney Goeltzenleuchter806c7002015-10-27 11:22:14 -0600254 submit_info.pWaitSemaphores = NULL;
Chia-I Wud50a7d72015-10-26 20:48:51 +0800255 submit_info.commandBufferCount = 1;
Chia-I Wu3432a0c2015-10-27 18:04:07 +0800256 submit_info.pCommandBuffers = &commandBuffer;
Chia-I Wud50a7d72015-10-26 20:48:51 +0800257 submit_info.signalSemaphoreCount = 0;
Courtney Goeltzenleuchter806c7002015-10-27 11:22:14 -0600258 submit_info.pSignalSemaphores = NULL;
Courtney Goeltzenleuchter646b9072015-10-20 18:04:07 -0600259
260 err = pTableQueue->QueueSubmit(queue, 1, &submit_info, nullFence);
David Pinedofb5b5382015-06-18 17:03:14 -0600261 assert(!err);
262
263 err = pTableQueue->QueueWaitIdle(queue);
264 assert(!err);
265
266 err = pTableDevice->DeviceWaitIdle(device);
267 assert(!err);
268
Courtney Goeltzenleuchter06d89472015-10-20 16:40:38 -0600269 pTableDevice->GetImageSubresourceLayout(device, image2, &sr, &sr_layout);
David Pinedofb5b5382015-06-18 17:03:14 -0600270
271 err = pTableDevice->MapMemory(device, mem2, 0, 0, 0, (void **) &ptr );
272 assert(!err);
273
274 ptr += sr_layout.offset;
275
276 ofstream file(filename, ios::binary);
277
278 file << "P6\n";
279 file << width << "\n";
280 file << height << "\n";
281 file << 255 << "\n";
282
283 for (y = 0; y < height; y++) {
284 const unsigned int *row = (const unsigned int*) ptr;
285 if (format == VK_FORMAT_B8G8R8A8_UNORM)
286 {
287 for (x = 0; x < width; x++) {
288 unsigned int swapped;
289 swapped = (*row & 0xff00ff00) | (*row & 0x000000ff) << 16 | (*row & 0x00ff0000) >> 16;
290 file.write((char *)&swapped, 3);
291 row++;
292 }
293 }
294 else if (format == VK_FORMAT_R8G8B8A8_UNORM)
295 {
296 for (x = 0; x < width; x++) {
297 file.write((char *)row, 3);
298 row++;
299 }
300 }
301 else
302 {
David Pinedo8897e192015-07-31 10:56:20 -0600303 // TODO: add support for additional formats
David Pinedofb5b5382015-06-18 17:03:14 -0600304 printf("Unrecognized image format\n");
305 break;
306 }
307 ptr += sr_layout.rowPitch;
308 }
309 file.close();
310
311 // Clean up
Mark Lobodzinski2141f652015-09-07 13:59:43 -0600312 pTableDevice->UnmapMemory(device, mem2);
Chia-I Wuf7458c52015-10-26 21:10:41 +0800313 pTableDevice->FreeMemory(device, mem2, NULL);
Chia-I Wu3432a0c2015-10-27 18:04:07 +0800314 pTableDevice->FreeCommandBuffers(device, deviceMap[device]->commandPool, 1, &commandBuffer);
David Pinedofb5b5382015-06-18 17:03:14 -0600315}
316
317
David Pinedobcd0e792015-06-19 13:48:18 -0600318static void createDeviceRegisterExtensions(const VkDeviceCreateInfo* pCreateInfo, VkDevice device)
319{
Cody Northropd2ad0342015-08-05 11:15:02 -0600320 uint32_t i;
David Pinedobcd0e792015-06-19 13:48:18 -0600321 VkLayerDispatchTable *pDisp = get_dispatch_table(screenshot_device_table_map, device);
Jon Ashburn8acd2332015-09-16 18:08:32 -0600322 PFN_vkGetDeviceProcAddr gpa = pDisp->GetDeviceProcAddr;
Jon Ashburn8acd2332015-09-16 18:08:32 -0600323 pDisp->CreateSwapchainKHR = (PFN_vkCreateSwapchainKHR) gpa(device, "vkCreateSwapchainKHR");
Jon Ashburn8acd2332015-09-16 18:08:32 -0600324 pDisp->GetSwapchainImagesKHR = (PFN_vkGetSwapchainImagesKHR) gpa(device, "vkGetSwapchainImagesKHR");
325 pDisp->AcquireNextImageKHR = (PFN_vkAcquireNextImageKHR) gpa(device, "vkAcquireNextImageKHR");
326 pDisp->QueuePresentKHR = (PFN_vkQueuePresentKHR) gpa(device, "vkQueuePresentKHR");
Ian Elliott1064fe32015-07-06 14:31:32 -0600327 deviceExtMap[pDisp].wsi_enabled = false;
Chia-I Wud50a7d72015-10-26 20:48:51 +0800328 for (i = 0; i < pCreateInfo->enabledExtensionNameCount; i++) {
Ian Elliott05846062015-11-20 14:13:17 -0700329 if (strcmp(pCreateInfo->ppEnabledExtensionNames[i], VK_KHR_SWAPCHAIN_EXTENSION_NAME) == 0)
Ian Elliott1064fe32015-07-06 14:31:32 -0600330 deviceExtMap[pDisp].wsi_enabled = true;
David Pinedobcd0e792015-06-19 13:48:18 -0600331 }
332}
333
Chia-I Wu9ab61502015-11-06 06:42:02 +0800334VK_LAYER_EXPORT VKAPI_ATTR VkResult VKAPI_CALL vkCreateDevice(
David Pinedofb5b5382015-06-18 17:03:14 -0600335 VkPhysicalDevice gpu,
336 const VkDeviceCreateInfo *pCreateInfo,
Chia-I Wu3432a0c2015-10-27 18:04:07 +0800337 const VkAllocationCallbacks* pAllocator,
David Pinedofb5b5382015-06-18 17:03:14 -0600338 VkDevice *pDevice)
339{
Courtney Goeltzenleuchterca173b82015-06-25 18:01:43 -0600340 VkLayerDispatchTable *pDisp = get_dispatch_table(screenshot_device_table_map, *pDevice);
Chia-I Wuf7458c52015-10-26 21:10:41 +0800341 VkResult result = pDisp->CreateDevice(gpu, pCreateInfo, pAllocator, pDevice);
David Pinedofb5b5382015-06-18 17:03:14 -0600342
David Pinedobcd0e792015-06-19 13:48:18 -0600343 if (result == VK_SUCCESS) {
Jon Ashburnec105332015-07-06 15:09:36 -0600344 init_screenshot();
David Pinedobcd0e792015-06-19 13:48:18 -0600345 createDeviceRegisterExtensions(pCreateInfo, *pDevice);
Cody Northrop49f885c2015-09-01 10:18:45 -0600346 // Create a mapping from a device to a physicalDevice
347 if (deviceMap[*pDevice] == NULL)
348 {
349 DeviceMapStruct *deviceMapElem = new DeviceMapStruct;
350 deviceMap[*pDevice] = deviceMapElem;
351 }
352 deviceMap[*pDevice]->physicalDevice = gpu;
David Pinedobcd0e792015-06-19 13:48:18 -0600353 }
354
David Pinedofb5b5382015-06-18 17:03:14 -0600355 return result;
356}
357
Chia-I Wu9ab61502015-11-06 06:42:02 +0800358VK_LAYER_EXPORT VKAPI_ATTR VkResult VKAPI_CALL vkEnumeratePhysicalDevices(
Cody Northrop49f885c2015-09-01 10:18:45 -0600359 VkInstance instance,
360 uint32_t* pPhysicalDeviceCount,
361 VkPhysicalDevice* pPhysicalDevices)
362{
363 VkResult result;
364
365 VkLayerInstanceDispatchTable* pTable = instance_dispatch_table(instance);
366 result = pTable->EnumeratePhysicalDevices(instance, pPhysicalDeviceCount, pPhysicalDevices);
Tobin Ehlisbe427742015-09-08 08:59:48 -0600367 if (result==VK_SUCCESS && *pPhysicalDeviceCount > 0 && pPhysicalDevices)
Cody Northrop49f885c2015-09-01 10:18:45 -0600368 {
369 for (uint32_t i=0; i<*pPhysicalDeviceCount ; i++)
370 {
371 // Create a mapping from a physicalDevice to an instance
372 if (physDeviceMap[pPhysicalDevices[i]] == NULL)
373 {
374 PhysDeviceMapStruct *physDeviceMapElem = new PhysDeviceMapStruct;
375 physDeviceMap[pPhysicalDevices[i]] = physDeviceMapElem;
376 }
377 physDeviceMap[pPhysicalDevices[i]]->instance = instance;
378 }
379 }
380 return result;
381}
382
Courtney Goeltzenleuchterca173b82015-06-25 18:01:43 -0600383/* TODO: Probably need a DestroyDevice as well */
384
Courtney Goeltzenleuchtera6db3462015-07-07 11:25:43 -0600385static const VkLayerProperties ss_device_layers[] = {
David Pinedofb5b5382015-06-18 17:03:14 -0600386 {
David Pinedofb5b5382015-06-18 17:03:14 -0600387 "ScreenShot",
Courtney Goeltzenleuchtera6db3462015-07-07 11:25:43 -0600388 VK_API_VERSION,
389 VK_MAKE_VERSION(0, 1, 0),
David Pinedofb5b5382015-06-18 17:03:14 -0600390 "Layer: ScreenShot",
David Pinedobcd0e792015-06-19 13:48:18 -0600391 }
David Pinedofb5b5382015-06-18 17:03:14 -0600392};
David Pinedofb5b5382015-06-18 17:03:14 -0600393
Chia-I Wu9ab61502015-11-06 06:42:02 +0800394VK_LAYER_EXPORT VKAPI_ATTR VkResult VKAPI_CALL vkEnumerateInstanceExtensionProperties(
Courtney Goeltzenleuchtera6db3462015-07-07 11:25:43 -0600395 const char *pLayerName,
396 uint32_t *pCount,
397 VkExtensionProperties* pProperties)
David Pinedofb5b5382015-06-18 17:03:14 -0600398{
Courtney Goeltzenleuchtera6db3462015-07-07 11:25:43 -0600399 /* ScreenShot does not have any global extensions */
400 return util_GetExtensionProperties(0, NULL, pCount, pProperties);
Tony Barbour59a47322015-06-24 16:06:58 -0600401}
Courtney Goeltzenleuchtera6db3462015-07-07 11:25:43 -0600402
Chia-I Wu9ab61502015-11-06 06:42:02 +0800403VK_LAYER_EXPORT VKAPI_ATTR VkResult VKAPI_CALL vkEnumerateInstanceLayerProperties(
Courtney Goeltzenleuchtera6db3462015-07-07 11:25:43 -0600404 uint32_t *pCount,
405 VkLayerProperties* pProperties)
Tony Barbour59a47322015-06-24 16:06:58 -0600406{
Courtney Goeltzenleuchtera6db3462015-07-07 11:25:43 -0600407 /* ScreenShot does not have any global layers */
408 return util_GetLayerProperties(0, NULL,
409 pCount, pProperties);
Tony Barbour59a47322015-06-24 16:06:58 -0600410}
411
Chia-I Wu9ab61502015-11-06 06:42:02 +0800412VK_LAYER_EXPORT VKAPI_ATTR VkResult VKAPI_CALL vkEnumerateDeviceExtensionProperties(
Courtney Goeltzenleuchtera6db3462015-07-07 11:25:43 -0600413 VkPhysicalDevice physicalDevice,
414 const char* pLayerName,
415 uint32_t* pCount,
416 VkExtensionProperties* pProperties)
Tony Barbour59a47322015-06-24 16:06:58 -0600417{
Courtney Goeltzenleuchtera6db3462015-07-07 11:25:43 -0600418 /* ScreenShot does not have any physical device extensions */
Jon Ashburn751c4842015-11-02 17:37:20 -0700419 if (pLayerName == NULL) {
420 VkLayerInstanceDispatchTable* pTable = instance_dispatch_table(physicalDevice);
421 return pTable->EnumerateDeviceExtensionProperties(
422 physicalDevice,
423 NULL,
424 pCount,
425 pProperties);
426 } else {
427 return util_GetExtensionProperties(0, NULL, pCount, pProperties);
428 }
Courtney Goeltzenleuchtera6db3462015-07-07 11:25:43 -0600429}
Tony Barbour59a47322015-06-24 16:06:58 -0600430
Chia-I Wu9ab61502015-11-06 06:42:02 +0800431VK_LAYER_EXPORT VKAPI_ATTR VkResult VKAPI_CALL vkEnumerateDeviceLayerProperties(
Courtney Goeltzenleuchtera6db3462015-07-07 11:25:43 -0600432 VkPhysicalDevice physicalDevice,
433 uint32_t* pCount,
434 VkLayerProperties* pProperties)
435{
436 return util_GetLayerProperties(ARRAY_SIZE(ss_device_layers),
437 ss_device_layers,
438 pCount, pProperties);
David Pinedofb5b5382015-06-18 17:03:14 -0600439}
440
Chia-I Wu9ab61502015-11-06 06:42:02 +0800441VK_LAYER_EXPORT VKAPI_ATTR void VKAPI_CALL vkGetDeviceQueue(
David Pinedofb5b5382015-06-18 17:03:14 -0600442 VkDevice device,
443 uint32_t queueNodeIndex,
444 uint32_t queueIndex,
445 VkQueue *pQueue)
446{
447 VkLayerDispatchTable* pTable = screenshot_device_table_map[device];
Courtney Goeltzenleuchter06d89472015-10-20 16:40:38 -0600448 get_dispatch_table(screenshot_device_table_map, device)->GetDeviceQueue(device, queueNodeIndex, queueIndex, pQueue);
David Pinedofb5b5382015-06-18 17:03:14 -0600449
450 loader_platform_thread_lock_mutex(&globalLock);
451 if (screenshotEnvQueried && screenshotFrames.empty()) {
452 // We are all done taking screenshots, so don't do anything else
453 loader_platform_thread_unlock_mutex(&globalLock);
Courtney Goeltzenleuchter06d89472015-10-20 16:40:38 -0600454 return;
David Pinedofb5b5382015-06-18 17:03:14 -0600455 }
456
Courtney Goeltzenleuchter06d89472015-10-20 16:40:38 -0600457 screenshot_device_table_map.emplace(*pQueue, pTable);
David Pinedofb5b5382015-06-18 17:03:14 -0600458
Courtney Goeltzenleuchter06d89472015-10-20 16:40:38 -0600459 // Create a mapping from a device to a queue
460 if (deviceMap[device] == NULL)
461 {
462 DeviceMapStruct *deviceMapElem = new DeviceMapStruct;
463 deviceMap[device] = deviceMapElem;
David Pinedofb5b5382015-06-18 17:03:14 -0600464 }
Courtney Goeltzenleuchter06d89472015-10-20 16:40:38 -0600465 deviceMap[device]->queue = *pQueue;
David Pinedofb5b5382015-06-18 17:03:14 -0600466 loader_platform_thread_unlock_mutex(&globalLock);
David Pinedofb5b5382015-06-18 17:03:14 -0600467}
468
Chia-I Wu9ab61502015-11-06 06:42:02 +0800469VK_LAYER_EXPORT VKAPI_ATTR VkResult VKAPI_CALL vkCreateCommandPool(
David Pinedo8897e192015-07-31 10:56:20 -0600470 VkDevice device,
Chia-I Wu3432a0c2015-10-27 18:04:07 +0800471 const VkCommandPoolCreateInfo *pCreateInfo,
472 const VkAllocationCallbacks* pAllocator,
473 VkCommandPool *pCommandPool)
David Pinedo8897e192015-07-31 10:56:20 -0600474{
475 VkLayerDispatchTable* pTable = screenshot_device_table_map[device];
Chia-I Wu3432a0c2015-10-27 18:04:07 +0800476 VkResult result = get_dispatch_table(screenshot_device_table_map, device)->CreateCommandPool(device, pCreateInfo, pAllocator, pCommandPool);
David Pinedo8897e192015-07-31 10:56:20 -0600477
478 loader_platform_thread_lock_mutex(&globalLock);
479 if (screenshotEnvQueried && screenshotFrames.empty()) {
480 // We are all done taking screenshots, so don't do anything else
481 loader_platform_thread_unlock_mutex(&globalLock);
482 return result;
483 }
484
Chia-I Wu3432a0c2015-10-27 18:04:07 +0800485 // Create a mapping from a device to a commandPool
Cody Northrop49f885c2015-09-01 10:18:45 -0600486 if (deviceMap[device] == NULL)
487 {
488 DeviceMapStruct *deviceMapElem = new DeviceMapStruct;
489 deviceMap[device] = deviceMapElem;
490 }
Chia-I Wu3432a0c2015-10-27 18:04:07 +0800491 deviceMap[device]->commandPool = *pCommandPool;
David Pinedo8897e192015-07-31 10:56:20 -0600492 loader_platform_thread_unlock_mutex(&globalLock);
493 return result;
494}
495
Chia-I Wu9ab61502015-11-06 06:42:02 +0800496VK_LAYER_EXPORT VKAPI_ATTR VkResult VKAPI_CALL vkCreateSwapchainKHR(
David Pinedofb5b5382015-06-18 17:03:14 -0600497 VkDevice device,
Ian Elliott7e40db92015-08-21 15:09:33 -0600498 const VkSwapchainCreateInfoKHR *pCreateInfo,
Ian Elliott05846062015-11-20 14:13:17 -0700499 const VkAllocationCallbacks *pAllocator,
Ian Elliott7e40db92015-08-21 15:09:33 -0600500 VkSwapchainKHR *pSwapchain)
David Pinedofb5b5382015-06-18 17:03:14 -0600501{
502 VkLayerDispatchTable* pTable = screenshot_device_table_map[device];
Ian Elliott05846062015-11-20 14:13:17 -0700503 VkResult result = get_dispatch_table(screenshot_device_table_map, device)->CreateSwapchainKHR(device, pCreateInfo, pAllocator, pSwapchain);
David Pinedofb5b5382015-06-18 17:03:14 -0600504
505 loader_platform_thread_lock_mutex(&globalLock);
506 if (screenshotEnvQueried && screenshotFrames.empty()) {
507 // We are all done taking screenshots, so don't do anything else
508 loader_platform_thread_unlock_mutex(&globalLock);
509 return result;
510 }
511
512 if (result == VK_SUCCESS)
513 {
514 // Create a mapping for a swapchain to a device, image extent, and format
515 SwapchainMapStruct *swapchainMapElem = new SwapchainMapStruct;
516 swapchainMapElem->device = device;
517 swapchainMapElem->imageExtent = pCreateInfo->imageExtent;
518 swapchainMapElem->format = pCreateInfo->imageFormat;
Chia-I Wue2fc5522015-10-26 20:04:44 +0800519 swapchainMap.insert(make_pair(*pSwapchain, swapchainMapElem));
David Pinedofb5b5382015-06-18 17:03:14 -0600520
521 // Create a mapping for the swapchain object into the dispatch table
Chia-I Wue2fc5522015-10-26 20:04:44 +0800522 screenshot_device_table_map.emplace((void *)pSwapchain, pTable);
David Pinedofb5b5382015-06-18 17:03:14 -0600523 }
524 loader_platform_thread_unlock_mutex(&globalLock);
525
526 return result;
527}
528
Chia-I Wu9ab61502015-11-06 06:42:02 +0800529VK_LAYER_EXPORT VKAPI_ATTR VkResult VKAPI_CALL vkGetSwapchainImagesKHR(
Ian Elliott1064fe32015-07-06 14:31:32 -0600530 VkDevice device,
Ian Elliott7e40db92015-08-21 15:09:33 -0600531 VkSwapchainKHR swapchain,
Ian Elliott2b6b68a2015-08-07 14:11:14 -0600532 uint32_t* pCount,
Ian Elliott7e40db92015-08-21 15:09:33 -0600533 VkImage* pSwapchainImages)
David Pinedofb5b5382015-06-18 17:03:14 -0600534{
Ian Elliott7e40db92015-08-21 15:09:33 -0600535 VkResult result = get_dispatch_table(screenshot_device_table_map, device)->GetSwapchainImagesKHR(device, swapchain, pCount, pSwapchainImages);
David Pinedofb5b5382015-06-18 17:03:14 -0600536
537 loader_platform_thread_lock_mutex(&globalLock);
538 if (screenshotEnvQueried && screenshotFrames.empty()) {
539 // We are all done taking screenshots, so don't do anything else
540 loader_platform_thread_unlock_mutex(&globalLock);
541 return result;
542 }
543
544 if (result == VK_SUCCESS &&
Ian Elliott7e40db92015-08-21 15:09:33 -0600545 pSwapchainImages &&
Chia-I Wue2fc5522015-10-26 20:04:44 +0800546 !swapchainMap.empty() && swapchainMap.find(swapchain) != swapchainMap.end())
Ian Elliott2b6b68a2015-08-07 14:11:14 -0600547 {
Cody Northropbf065822015-09-01 11:48:13 -0600548 unsigned i;
David Pinedo8897e192015-07-31 10:56:20 -0600549
Ian Elliott2b6b68a2015-08-07 14:11:14 -0600550 for (i=0; i<*pCount; i++)
David Pinedofb5b5382015-06-18 17:03:14 -0600551 {
552 // Create a mapping for an image to a device, image extent, and format
Chia-I Wue2fc5522015-10-26 20:04:44 +0800553 if (imageMap[pSwapchainImages[i]] == NULL)
Cody Northrop49f885c2015-09-01 10:18:45 -0600554 {
555 ImageMapStruct *imageMapElem = new ImageMapStruct;
Chia-I Wue2fc5522015-10-26 20:04:44 +0800556 imageMap[pSwapchainImages[i]] = imageMapElem;
Cody Northrop49f885c2015-09-01 10:18:45 -0600557 }
Chia-I Wue2fc5522015-10-26 20:04:44 +0800558 imageMap[pSwapchainImages[i]]->device = swapchainMap[swapchain]->device;
559 imageMap[pSwapchainImages[i]]->imageExtent = swapchainMap[swapchain]->imageExtent;
560 imageMap[pSwapchainImages[i]]->format = swapchainMap[swapchain]->format;
David Pinedofb5b5382015-06-18 17:03:14 -0600561 }
David Pinedo8897e192015-07-31 10:56:20 -0600562
563 // Add list of images to swapchain to image map
Chia-I Wue2fc5522015-10-26 20:04:44 +0800564 SwapchainMapStruct *swapchainMapElem = swapchainMap[swapchain];
David Pinedo8897e192015-07-31 10:56:20 -0600565 if (i >= 1 && swapchainMapElem)
566 {
567 VkImage *imageList = new VkImage[i];
568 swapchainMapElem->imageList = imageList;
Cody Northropbf065822015-09-01 11:48:13 -0600569 for (unsigned j=0; j<i; j++)
David Pinedo8897e192015-07-31 10:56:20 -0600570 {
Chia-I Wue2fc5522015-10-26 20:04:44 +0800571 swapchainMapElem->imageList[j] = pSwapchainImages[j];
David Pinedo8897e192015-07-31 10:56:20 -0600572 }
573 }
574
David Pinedofb5b5382015-06-18 17:03:14 -0600575 }
576 loader_platform_thread_unlock_mutex(&globalLock);
577 return result;
578}
579
Ian Elliott05846062015-11-20 14:13:17 -0700580VK_LAYER_EXPORT VKAPI_ATTR VkResult VKAPI_CALL vkQueuePresentKHR(VkQueue queue, const VkPresentInfoKHR* pPresentInfo)
David Pinedofb5b5382015-06-18 17:03:14 -0600581{
582 static int frameNumber = 0;
David Pinedo8897e192015-07-31 10:56:20 -0600583 if (frameNumber == 10) {fflush(stdout); /* *((int*)0)=0; */ }
Ian Elliott7e40db92015-08-21 15:09:33 -0600584 VkResult result = get_dispatch_table(screenshot_device_table_map, queue)->QueuePresentKHR(queue, pPresentInfo);
David Pinedofb5b5382015-06-18 17:03:14 -0600585
586 loader_platform_thread_lock_mutex(&globalLock);
587
588 if (!screenshotEnvQueried)
589 {
590 const char *_vk_screenshot = getenv("_VK_SCREENSHOT");
591 if (_vk_screenshot && *_vk_screenshot)
592 {
593 string spec(_vk_screenshot), word;
594 size_t start = 0, comma = 0;
595
596 while (start < spec.size()) {
597 int frameToAdd;
598 comma = spec.find(',', start);
599 if (comma == string::npos)
600 word = string(spec, start);
601 else
602 word = string(spec, start, comma - start);
603 frameToAdd=atoi(word.c_str());
604 // Add the frame number to list, but only do it if the word started with a digit and if
605 // it's not already in the list
606 if (*(word.c_str()) >= '0' && *(word.c_str()) <= '9' &&
607 find(screenshotFrames.begin(), screenshotFrames.end(), frameToAdd) == screenshotFrames.end())
608 {
609 screenshotFrames.push_back(frameToAdd);
610 }
611 if (comma == string::npos)
612 break;
613 start = comma + 1;
614 }
615 }
616 screenshotEnvQueried = true;
617 }
618
619
620 if (result == VK_SUCCESS && !screenshotFrames.empty())
621 {
622 vector<int>::iterator it;
623 it = find(screenshotFrames.begin(), screenshotFrames.end(), frameNumber);
624 if (it != screenshotFrames.end())
625 {
626 string fileName;
627 fileName = to_string(frameNumber) + ".ppm";
David Pinedo8897e192015-07-31 10:56:20 -0600628
629 VkImage image;
Chia-I Wue2fc5522015-10-26 20:04:44 +0800630 VkSwapchainKHR swapchain;
David Pinedo8897e192015-07-31 10:56:20 -0600631 // We'll dump only one image: the first
Ian Elliott05846062015-11-20 14:13:17 -0700632 swapchain = pPresentInfo->pSwapchains[0];
633 image = swapchainMap[swapchain]->imageList[pPresentInfo->pImageIndices[0]];
David Pinedo8897e192015-07-31 10:56:20 -0600634 writePPM(fileName.c_str(), image);
David Pinedofb5b5382015-06-18 17:03:14 -0600635 screenshotFrames.erase(it);
636
637 if (screenshotFrames.empty())
638 {
David Pinedo8897e192015-07-31 10:56:20 -0600639 // Free all our maps since we are done with them.
David Pinedofb5b5382015-06-18 17:03:14 -0600640 for (auto it = swapchainMap.begin(); it != swapchainMap.end(); it++)
641 {
642 SwapchainMapStruct *swapchainMapElem = it->second;
643 delete swapchainMapElem;
644 }
645 for (auto it = imageMap.begin(); it != imageMap.end(); it++)
646 {
647 ImageMapStruct *imageMapElem = it->second;
648 delete imageMapElem;
649 }
650 for (auto it = deviceMap.begin(); it != deviceMap.end(); it++)
651 {
652 DeviceMapStruct *deviceMapElem = it->second;
653 delete deviceMapElem;
654 }
Cody Northrop49f885c2015-09-01 10:18:45 -0600655 for (auto it = physDeviceMap.begin(); it != physDeviceMap.end(); it++)
656 {
657 PhysDeviceMapStruct *physDeviceMapElem = it->second;
658 delete physDeviceMapElem;
659 }
David Pinedofb5b5382015-06-18 17:03:14 -0600660 swapchainMap.clear();
661 imageMap.clear();
662 deviceMap.clear();
Cody Northrop49f885c2015-09-01 10:18:45 -0600663 physDeviceMap.clear();
David Pinedofb5b5382015-06-18 17:03:14 -0600664 }
665 }
666 }
667 frameNumber++;
668 loader_platform_thread_unlock_mutex(&globalLock);
669 return result;
670}
671
Chia-I Wu9ab61502015-11-06 06:42:02 +0800672VK_LAYER_EXPORT VKAPI_ATTR PFN_vkVoidFunction VKAPI_CALL vkGetDeviceProcAddr(
David Pinedofb5b5382015-06-18 17:03:14 -0600673 VkDevice dev,
674 const char *funcName)
675{
David Pinedofb5b5382015-06-18 17:03:14 -0600676 if (dev == NULL) {
677 return NULL;
678 }
679
680 /* loader uses this to force layer initialization; device object is wrapped */
681 if (!strcmp(funcName, "vkGetDeviceProcAddr")) {
682 initDeviceTable(screenshot_device_table_map, (const VkBaseLayerObject *) dev);
David Pinedo8897e192015-07-31 10:56:20 -0600683 return (PFN_vkVoidFunction)vkGetDeviceProcAddr;
David Pinedofb5b5382015-06-18 17:03:14 -0600684 }
Courtney Goeltzenleuchterca173b82015-06-25 18:01:43 -0600685 if (!strcmp(funcName, "vkCreateDevice"))
David Pinedo8897e192015-07-31 10:56:20 -0600686 return (PFN_vkVoidFunction) vkCreateDevice;
Courtney Goeltzenleuchterca173b82015-06-25 18:01:43 -0600687
David Pinedofb5b5382015-06-18 17:03:14 -0600688 if (!strcmp(funcName, "vkGetDeviceQueue"))
David Pinedo8897e192015-07-31 10:56:20 -0600689 return (PFN_vkVoidFunction) vkGetDeviceQueue;
690
691 if (!strcmp(funcName, "vkCreateCommandPool"))
692 return (PFN_vkVoidFunction) vkCreateCommandPool;
David Pinedofb5b5382015-06-18 17:03:14 -0600693
David Pinedobcd0e792015-06-19 13:48:18 -0600694 VkLayerDispatchTable *pDisp = get_dispatch_table(screenshot_device_table_map, dev);
Jon Ashburn8acd2332015-09-16 18:08:32 -0600695 if (deviceExtMap.size() != 0 && deviceExtMap[pDisp].wsi_enabled)
David Pinedobcd0e792015-06-19 13:48:18 -0600696 {
Ian Elliott7e40db92015-08-21 15:09:33 -0600697 if (!strcmp(funcName, "vkCreateSwapchainKHR"))
698 return (PFN_vkVoidFunction) vkCreateSwapchainKHR;
699 if (!strcmp(funcName, "vkGetSwapchainImagesKHR"))
700 return (PFN_vkVoidFunction) vkGetSwapchainImagesKHR;
701 if (!strcmp(funcName, "vkQueuePresentKHR"))
702 return (PFN_vkVoidFunction) vkQueuePresentKHR;
David Pinedobcd0e792015-06-19 13:48:18 -0600703 }
704
705 if (pDisp->GetDeviceProcAddr == NULL)
David Pinedofb5b5382015-06-18 17:03:14 -0600706 return NULL;
David Pinedobcd0e792015-06-19 13:48:18 -0600707 return pDisp->GetDeviceProcAddr(dev, funcName);
David Pinedofb5b5382015-06-18 17:03:14 -0600708}
David Pinedo38310942015-07-09 16:23:44 -0600709
710
Chia-I Wu9ab61502015-11-06 06:42:02 +0800711VK_LAYER_EXPORT VKAPI_ATTR PFN_vkVoidFunction VKAPI_CALL vkGetInstanceProcAddr(VkInstance instance, const char* funcName)
David Pinedo38310942015-07-09 16:23:44 -0600712{
David Pinedo38310942015-07-09 16:23:44 -0600713 if (instance == VK_NULL_HANDLE) {
714 return NULL;
715 }
716
717 /* loader uses this to force layer initialization; instance object is wrapped */
718 if (!strcmp("vkGetInstanceProcAddr", funcName)) {
719 initInstanceTable((const VkBaseLayerObject *) instance);
David Pinedo8897e192015-07-31 10:56:20 -0600720 return (PFN_vkVoidFunction) vkGetInstanceProcAddr;
David Pinedo38310942015-07-09 16:23:44 -0600721 }
722
Cody Northrop49f885c2015-09-01 10:18:45 -0600723 if (!strcmp(funcName, "vkEnumeratePhysicalDevices"))
724 return (PFN_vkVoidFunction)vkEnumeratePhysicalDevices;
Jon Ashburn751c4842015-11-02 17:37:20 -0700725 if (!strcmp(funcName, "vkEnumerateDeviceExtensionProperties"))
726 return (PFN_vkVoidFunction)vkEnumerateDeviceExtensionProperties;
Cody Northrop49f885c2015-09-01 10:18:45 -0600727
David Pinedo38310942015-07-09 16:23:44 -0600728 VkLayerInstanceDispatchTable* pTable = instance_dispatch_table(instance);
729 if (pTable->GetInstanceProcAddr == NULL)
730 return NULL;
731 return pTable->GetInstanceProcAddr(instance, funcName);
David Pinedo38310942015-07-09 16:23:44 -0600732}