blob: b3bd9af5f4bd9e72eccf429aebb9bd66abed830c [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;
Jon Ashburn7f9716c2015-12-30 16:42:50 -0700255 submit_info.pWaitDstStageMask = NULL;
Chia-I Wud50a7d72015-10-26 20:48:51 +0800256 submit_info.commandBufferCount = 1;
Chia-I Wu3432a0c2015-10-27 18:04:07 +0800257 submit_info.pCommandBuffers = &commandBuffer;
Chia-I Wud50a7d72015-10-26 20:48:51 +0800258 submit_info.signalSemaphoreCount = 0;
Courtney Goeltzenleuchter806c7002015-10-27 11:22:14 -0600259 submit_info.pSignalSemaphores = NULL;
Courtney Goeltzenleuchter646b9072015-10-20 18:04:07 -0600260
261 err = pTableQueue->QueueSubmit(queue, 1, &submit_info, nullFence);
David Pinedofb5b5382015-06-18 17:03:14 -0600262 assert(!err);
263
264 err = pTableQueue->QueueWaitIdle(queue);
265 assert(!err);
266
267 err = pTableDevice->DeviceWaitIdle(device);
268 assert(!err);
269
Courtney Goeltzenleuchter06d89472015-10-20 16:40:38 -0600270 pTableDevice->GetImageSubresourceLayout(device, image2, &sr, &sr_layout);
David Pinedofb5b5382015-06-18 17:03:14 -0600271
272 err = pTableDevice->MapMemory(device, mem2, 0, 0, 0, (void **) &ptr );
273 assert(!err);
274
275 ptr += sr_layout.offset;
276
277 ofstream file(filename, ios::binary);
278
279 file << "P6\n";
280 file << width << "\n";
281 file << height << "\n";
282 file << 255 << "\n";
283
284 for (y = 0; y < height; y++) {
285 const unsigned int *row = (const unsigned int*) ptr;
286 if (format == VK_FORMAT_B8G8R8A8_UNORM)
287 {
288 for (x = 0; x < width; x++) {
289 unsigned int swapped;
290 swapped = (*row & 0xff00ff00) | (*row & 0x000000ff) << 16 | (*row & 0x00ff0000) >> 16;
291 file.write((char *)&swapped, 3);
292 row++;
293 }
294 }
295 else if (format == VK_FORMAT_R8G8B8A8_UNORM)
296 {
297 for (x = 0; x < width; x++) {
298 file.write((char *)row, 3);
299 row++;
300 }
301 }
302 else
303 {
David Pinedo8897e192015-07-31 10:56:20 -0600304 // TODO: add support for additional formats
David Pinedofb5b5382015-06-18 17:03:14 -0600305 printf("Unrecognized image format\n");
306 break;
307 }
308 ptr += sr_layout.rowPitch;
309 }
310 file.close();
311
312 // Clean up
Mark Lobodzinski2141f652015-09-07 13:59:43 -0600313 pTableDevice->UnmapMemory(device, mem2);
Chia-I Wuf7458c52015-10-26 21:10:41 +0800314 pTableDevice->FreeMemory(device, mem2, NULL);
Chia-I Wu3432a0c2015-10-27 18:04:07 +0800315 pTableDevice->FreeCommandBuffers(device, deviceMap[device]->commandPool, 1, &commandBuffer);
David Pinedofb5b5382015-06-18 17:03:14 -0600316}
317
318
David Pinedobcd0e792015-06-19 13:48:18 -0600319static void createDeviceRegisterExtensions(const VkDeviceCreateInfo* pCreateInfo, VkDevice device)
320{
Cody Northropd2ad0342015-08-05 11:15:02 -0600321 uint32_t i;
David Pinedobcd0e792015-06-19 13:48:18 -0600322 VkLayerDispatchTable *pDisp = get_dispatch_table(screenshot_device_table_map, device);
Jon Ashburn8acd2332015-09-16 18:08:32 -0600323 PFN_vkGetDeviceProcAddr gpa = pDisp->GetDeviceProcAddr;
Jon Ashburn8acd2332015-09-16 18:08:32 -0600324 pDisp->CreateSwapchainKHR = (PFN_vkCreateSwapchainKHR) gpa(device, "vkCreateSwapchainKHR");
Jon Ashburn8acd2332015-09-16 18:08:32 -0600325 pDisp->GetSwapchainImagesKHR = (PFN_vkGetSwapchainImagesKHR) gpa(device, "vkGetSwapchainImagesKHR");
326 pDisp->AcquireNextImageKHR = (PFN_vkAcquireNextImageKHR) gpa(device, "vkAcquireNextImageKHR");
327 pDisp->QueuePresentKHR = (PFN_vkQueuePresentKHR) gpa(device, "vkQueuePresentKHR");
Ian Elliott1064fe32015-07-06 14:31:32 -0600328 deviceExtMap[pDisp].wsi_enabled = false;
Chia-I Wud50a7d72015-10-26 20:48:51 +0800329 for (i = 0; i < pCreateInfo->enabledExtensionNameCount; i++) {
Ian Elliott05846062015-11-20 14:13:17 -0700330 if (strcmp(pCreateInfo->ppEnabledExtensionNames[i], VK_KHR_SWAPCHAIN_EXTENSION_NAME) == 0)
Ian Elliott1064fe32015-07-06 14:31:32 -0600331 deviceExtMap[pDisp].wsi_enabled = true;
David Pinedobcd0e792015-06-19 13:48:18 -0600332 }
333}
334
Chia-I Wu9ab61502015-11-06 06:42:02 +0800335VK_LAYER_EXPORT VKAPI_ATTR VkResult VKAPI_CALL vkCreateDevice(
David Pinedofb5b5382015-06-18 17:03:14 -0600336 VkPhysicalDevice gpu,
337 const VkDeviceCreateInfo *pCreateInfo,
Chia-I Wu3432a0c2015-10-27 18:04:07 +0800338 const VkAllocationCallbacks* pAllocator,
David Pinedofb5b5382015-06-18 17:03:14 -0600339 VkDevice *pDevice)
340{
Courtney Goeltzenleuchterca173b82015-06-25 18:01:43 -0600341 VkLayerDispatchTable *pDisp = get_dispatch_table(screenshot_device_table_map, *pDevice);
Chia-I Wuf7458c52015-10-26 21:10:41 +0800342 VkResult result = pDisp->CreateDevice(gpu, pCreateInfo, pAllocator, pDevice);
David Pinedofb5b5382015-06-18 17:03:14 -0600343
David Pinedobcd0e792015-06-19 13:48:18 -0600344 if (result == VK_SUCCESS) {
Jon Ashburnec105332015-07-06 15:09:36 -0600345 init_screenshot();
David Pinedobcd0e792015-06-19 13:48:18 -0600346 createDeviceRegisterExtensions(pCreateInfo, *pDevice);
Cody Northrop49f885c2015-09-01 10:18:45 -0600347 // Create a mapping from a device to a physicalDevice
348 if (deviceMap[*pDevice] == NULL)
349 {
350 DeviceMapStruct *deviceMapElem = new DeviceMapStruct;
351 deviceMap[*pDevice] = deviceMapElem;
352 }
353 deviceMap[*pDevice]->physicalDevice = gpu;
David Pinedobcd0e792015-06-19 13:48:18 -0600354 }
355
David Pinedofb5b5382015-06-18 17:03:14 -0600356 return result;
357}
358
Chia-I Wu9ab61502015-11-06 06:42:02 +0800359VK_LAYER_EXPORT VKAPI_ATTR VkResult VKAPI_CALL vkEnumeratePhysicalDevices(
Cody Northrop49f885c2015-09-01 10:18:45 -0600360 VkInstance instance,
361 uint32_t* pPhysicalDeviceCount,
362 VkPhysicalDevice* pPhysicalDevices)
363{
364 VkResult result;
365
366 VkLayerInstanceDispatchTable* pTable = instance_dispatch_table(instance);
367 result = pTable->EnumeratePhysicalDevices(instance, pPhysicalDeviceCount, pPhysicalDevices);
Tobin Ehlisbe427742015-09-08 08:59:48 -0600368 if (result==VK_SUCCESS && *pPhysicalDeviceCount > 0 && pPhysicalDevices)
Cody Northrop49f885c2015-09-01 10:18:45 -0600369 {
370 for (uint32_t i=0; i<*pPhysicalDeviceCount ; i++)
371 {
372 // Create a mapping from a physicalDevice to an instance
373 if (physDeviceMap[pPhysicalDevices[i]] == NULL)
374 {
375 PhysDeviceMapStruct *physDeviceMapElem = new PhysDeviceMapStruct;
376 physDeviceMap[pPhysicalDevices[i]] = physDeviceMapElem;
377 }
378 physDeviceMap[pPhysicalDevices[i]]->instance = instance;
379 }
380 }
381 return result;
382}
383
Courtney Goeltzenleuchterca173b82015-06-25 18:01:43 -0600384/* TODO: Probably need a DestroyDevice as well */
385
Courtney Goeltzenleuchtera6db3462015-07-07 11:25:43 -0600386static const VkLayerProperties ss_device_layers[] = {
David Pinedofb5b5382015-06-18 17:03:14 -0600387 {
Mark Lobodzinski0d054fe2015-12-30 08:16:12 -0700388 "screenshot",
Courtney Goeltzenleuchtera6db3462015-07-07 11:25:43 -0600389 VK_API_VERSION,
390 VK_MAKE_VERSION(0, 1, 0),
Mark Lobodzinski0d054fe2015-12-30 08:16:12 -0700391 "Layer: screenshot",
David Pinedobcd0e792015-06-19 13:48:18 -0600392 }
David Pinedofb5b5382015-06-18 17:03:14 -0600393};
David Pinedofb5b5382015-06-18 17:03:14 -0600394
Chia-I Wu9ab61502015-11-06 06:42:02 +0800395VK_LAYER_EXPORT VKAPI_ATTR VkResult VKAPI_CALL vkEnumerateInstanceExtensionProperties(
Courtney Goeltzenleuchtera6db3462015-07-07 11:25:43 -0600396 const char *pLayerName,
397 uint32_t *pCount,
398 VkExtensionProperties* pProperties)
David Pinedofb5b5382015-06-18 17:03:14 -0600399{
Courtney Goeltzenleuchtera6db3462015-07-07 11:25:43 -0600400 /* ScreenShot does not have any global extensions */
401 return util_GetExtensionProperties(0, NULL, pCount, pProperties);
Tony Barbour59a47322015-06-24 16:06:58 -0600402}
Courtney Goeltzenleuchtera6db3462015-07-07 11:25:43 -0600403
Chia-I Wu9ab61502015-11-06 06:42:02 +0800404VK_LAYER_EXPORT VKAPI_ATTR VkResult VKAPI_CALL vkEnumerateInstanceLayerProperties(
Courtney Goeltzenleuchtera6db3462015-07-07 11:25:43 -0600405 uint32_t *pCount,
406 VkLayerProperties* pProperties)
Tony Barbour59a47322015-06-24 16:06:58 -0600407{
Courtney Goeltzenleuchtera6db3462015-07-07 11:25:43 -0600408 /* ScreenShot does not have any global layers */
409 return util_GetLayerProperties(0, NULL,
410 pCount, pProperties);
Tony Barbour59a47322015-06-24 16:06:58 -0600411}
412
Chia-I Wu9ab61502015-11-06 06:42:02 +0800413VK_LAYER_EXPORT VKAPI_ATTR VkResult VKAPI_CALL vkEnumerateDeviceExtensionProperties(
Courtney Goeltzenleuchtera6db3462015-07-07 11:25:43 -0600414 VkPhysicalDevice physicalDevice,
415 const char* pLayerName,
416 uint32_t* pCount,
417 VkExtensionProperties* pProperties)
Tony Barbour59a47322015-06-24 16:06:58 -0600418{
Courtney Goeltzenleuchtera6db3462015-07-07 11:25:43 -0600419 /* ScreenShot does not have any physical device extensions */
Jon Ashburn751c4842015-11-02 17:37:20 -0700420 if (pLayerName == NULL) {
421 VkLayerInstanceDispatchTable* pTable = instance_dispatch_table(physicalDevice);
422 return pTable->EnumerateDeviceExtensionProperties(
423 physicalDevice,
424 NULL,
425 pCount,
426 pProperties);
427 } else {
428 return util_GetExtensionProperties(0, NULL, pCount, pProperties);
429 }
Courtney Goeltzenleuchtera6db3462015-07-07 11:25:43 -0600430}
Tony Barbour59a47322015-06-24 16:06:58 -0600431
Chia-I Wu9ab61502015-11-06 06:42:02 +0800432VK_LAYER_EXPORT VKAPI_ATTR VkResult VKAPI_CALL vkEnumerateDeviceLayerProperties(
Courtney Goeltzenleuchtera6db3462015-07-07 11:25:43 -0600433 VkPhysicalDevice physicalDevice,
434 uint32_t* pCount,
435 VkLayerProperties* pProperties)
436{
437 return util_GetLayerProperties(ARRAY_SIZE(ss_device_layers),
438 ss_device_layers,
439 pCount, pProperties);
David Pinedofb5b5382015-06-18 17:03:14 -0600440}
441
Chia-I Wu9ab61502015-11-06 06:42:02 +0800442VK_LAYER_EXPORT VKAPI_ATTR void VKAPI_CALL vkGetDeviceQueue(
David Pinedofb5b5382015-06-18 17:03:14 -0600443 VkDevice device,
444 uint32_t queueNodeIndex,
445 uint32_t queueIndex,
446 VkQueue *pQueue)
447{
448 VkLayerDispatchTable* pTable = screenshot_device_table_map[device];
Courtney Goeltzenleuchter06d89472015-10-20 16:40:38 -0600449 get_dispatch_table(screenshot_device_table_map, device)->GetDeviceQueue(device, queueNodeIndex, queueIndex, pQueue);
David Pinedofb5b5382015-06-18 17:03:14 -0600450
451 loader_platform_thread_lock_mutex(&globalLock);
452 if (screenshotEnvQueried && screenshotFrames.empty()) {
453 // We are all done taking screenshots, so don't do anything else
454 loader_platform_thread_unlock_mutex(&globalLock);
Courtney Goeltzenleuchter06d89472015-10-20 16:40:38 -0600455 return;
David Pinedofb5b5382015-06-18 17:03:14 -0600456 }
457
Courtney Goeltzenleuchter06d89472015-10-20 16:40:38 -0600458 screenshot_device_table_map.emplace(*pQueue, pTable);
David Pinedofb5b5382015-06-18 17:03:14 -0600459
Courtney Goeltzenleuchter06d89472015-10-20 16:40:38 -0600460 // Create a mapping from a device to a queue
461 if (deviceMap[device] == NULL)
462 {
463 DeviceMapStruct *deviceMapElem = new DeviceMapStruct;
464 deviceMap[device] = deviceMapElem;
David Pinedofb5b5382015-06-18 17:03:14 -0600465 }
Courtney Goeltzenleuchter06d89472015-10-20 16:40:38 -0600466 deviceMap[device]->queue = *pQueue;
David Pinedofb5b5382015-06-18 17:03:14 -0600467 loader_platform_thread_unlock_mutex(&globalLock);
David Pinedofb5b5382015-06-18 17:03:14 -0600468}
469
Chia-I Wu9ab61502015-11-06 06:42:02 +0800470VK_LAYER_EXPORT VKAPI_ATTR VkResult VKAPI_CALL vkCreateCommandPool(
David Pinedo8897e192015-07-31 10:56:20 -0600471 VkDevice device,
Chia-I Wu3432a0c2015-10-27 18:04:07 +0800472 const VkCommandPoolCreateInfo *pCreateInfo,
473 const VkAllocationCallbacks* pAllocator,
474 VkCommandPool *pCommandPool)
David Pinedo8897e192015-07-31 10:56:20 -0600475{
476 VkLayerDispatchTable* pTable = screenshot_device_table_map[device];
Chia-I Wu3432a0c2015-10-27 18:04:07 +0800477 VkResult result = get_dispatch_table(screenshot_device_table_map, device)->CreateCommandPool(device, pCreateInfo, pAllocator, pCommandPool);
David Pinedo8897e192015-07-31 10:56:20 -0600478
479 loader_platform_thread_lock_mutex(&globalLock);
480 if (screenshotEnvQueried && screenshotFrames.empty()) {
481 // We are all done taking screenshots, so don't do anything else
482 loader_platform_thread_unlock_mutex(&globalLock);
483 return result;
484 }
485
Chia-I Wu3432a0c2015-10-27 18:04:07 +0800486 // Create a mapping from a device to a commandPool
Cody Northrop49f885c2015-09-01 10:18:45 -0600487 if (deviceMap[device] == NULL)
488 {
489 DeviceMapStruct *deviceMapElem = new DeviceMapStruct;
490 deviceMap[device] = deviceMapElem;
491 }
Chia-I Wu3432a0c2015-10-27 18:04:07 +0800492 deviceMap[device]->commandPool = *pCommandPool;
David Pinedo8897e192015-07-31 10:56:20 -0600493 loader_platform_thread_unlock_mutex(&globalLock);
494 return result;
495}
496
Chia-I Wu9ab61502015-11-06 06:42:02 +0800497VK_LAYER_EXPORT VKAPI_ATTR VkResult VKAPI_CALL vkCreateSwapchainKHR(
David Pinedofb5b5382015-06-18 17:03:14 -0600498 VkDevice device,
Ian Elliott7e40db92015-08-21 15:09:33 -0600499 const VkSwapchainCreateInfoKHR *pCreateInfo,
Ian Elliott05846062015-11-20 14:13:17 -0700500 const VkAllocationCallbacks *pAllocator,
Ian Elliott7e40db92015-08-21 15:09:33 -0600501 VkSwapchainKHR *pSwapchain)
David Pinedofb5b5382015-06-18 17:03:14 -0600502{
503 VkLayerDispatchTable* pTable = screenshot_device_table_map[device];
Ian Elliott05846062015-11-20 14:13:17 -0700504 VkResult result = get_dispatch_table(screenshot_device_table_map, device)->CreateSwapchainKHR(device, pCreateInfo, pAllocator, pSwapchain);
David Pinedofb5b5382015-06-18 17:03:14 -0600505
506 loader_platform_thread_lock_mutex(&globalLock);
507 if (screenshotEnvQueried && screenshotFrames.empty()) {
508 // We are all done taking screenshots, so don't do anything else
509 loader_platform_thread_unlock_mutex(&globalLock);
510 return result;
511 }
512
513 if (result == VK_SUCCESS)
514 {
515 // Create a mapping for a swapchain to a device, image extent, and format
516 SwapchainMapStruct *swapchainMapElem = new SwapchainMapStruct;
517 swapchainMapElem->device = device;
518 swapchainMapElem->imageExtent = pCreateInfo->imageExtent;
519 swapchainMapElem->format = pCreateInfo->imageFormat;
Chia-I Wue2fc5522015-10-26 20:04:44 +0800520 swapchainMap.insert(make_pair(*pSwapchain, swapchainMapElem));
David Pinedofb5b5382015-06-18 17:03:14 -0600521
522 // Create a mapping for the swapchain object into the dispatch table
Chia-I Wue2fc5522015-10-26 20:04:44 +0800523 screenshot_device_table_map.emplace((void *)pSwapchain, pTable);
David Pinedofb5b5382015-06-18 17:03:14 -0600524 }
525 loader_platform_thread_unlock_mutex(&globalLock);
526
527 return result;
528}
529
Chia-I Wu9ab61502015-11-06 06:42:02 +0800530VK_LAYER_EXPORT VKAPI_ATTR VkResult VKAPI_CALL vkGetSwapchainImagesKHR(
Ian Elliott1064fe32015-07-06 14:31:32 -0600531 VkDevice device,
Ian Elliott7e40db92015-08-21 15:09:33 -0600532 VkSwapchainKHR swapchain,
Ian Elliott2b6b68a2015-08-07 14:11:14 -0600533 uint32_t* pCount,
Ian Elliott7e40db92015-08-21 15:09:33 -0600534 VkImage* pSwapchainImages)
David Pinedofb5b5382015-06-18 17:03:14 -0600535{
Ian Elliott7e40db92015-08-21 15:09:33 -0600536 VkResult result = get_dispatch_table(screenshot_device_table_map, device)->GetSwapchainImagesKHR(device, swapchain, pCount, pSwapchainImages);
David Pinedofb5b5382015-06-18 17:03:14 -0600537
538 loader_platform_thread_lock_mutex(&globalLock);
539 if (screenshotEnvQueried && screenshotFrames.empty()) {
540 // We are all done taking screenshots, so don't do anything else
541 loader_platform_thread_unlock_mutex(&globalLock);
542 return result;
543 }
544
545 if (result == VK_SUCCESS &&
Ian Elliott7e40db92015-08-21 15:09:33 -0600546 pSwapchainImages &&
Chia-I Wue2fc5522015-10-26 20:04:44 +0800547 !swapchainMap.empty() && swapchainMap.find(swapchain) != swapchainMap.end())
Ian Elliott2b6b68a2015-08-07 14:11:14 -0600548 {
Cody Northropbf065822015-09-01 11:48:13 -0600549 unsigned i;
David Pinedo8897e192015-07-31 10:56:20 -0600550
Ian Elliott2b6b68a2015-08-07 14:11:14 -0600551 for (i=0; i<*pCount; i++)
David Pinedofb5b5382015-06-18 17:03:14 -0600552 {
553 // Create a mapping for an image to a device, image extent, and format
Chia-I Wue2fc5522015-10-26 20:04:44 +0800554 if (imageMap[pSwapchainImages[i]] == NULL)
Cody Northrop49f885c2015-09-01 10:18:45 -0600555 {
556 ImageMapStruct *imageMapElem = new ImageMapStruct;
Chia-I Wue2fc5522015-10-26 20:04:44 +0800557 imageMap[pSwapchainImages[i]] = imageMapElem;
Cody Northrop49f885c2015-09-01 10:18:45 -0600558 }
Chia-I Wue2fc5522015-10-26 20:04:44 +0800559 imageMap[pSwapchainImages[i]]->device = swapchainMap[swapchain]->device;
560 imageMap[pSwapchainImages[i]]->imageExtent = swapchainMap[swapchain]->imageExtent;
561 imageMap[pSwapchainImages[i]]->format = swapchainMap[swapchain]->format;
David Pinedofb5b5382015-06-18 17:03:14 -0600562 }
David Pinedo8897e192015-07-31 10:56:20 -0600563
564 // Add list of images to swapchain to image map
Chia-I Wue2fc5522015-10-26 20:04:44 +0800565 SwapchainMapStruct *swapchainMapElem = swapchainMap[swapchain];
David Pinedo8897e192015-07-31 10:56:20 -0600566 if (i >= 1 && swapchainMapElem)
567 {
568 VkImage *imageList = new VkImage[i];
569 swapchainMapElem->imageList = imageList;
Cody Northropbf065822015-09-01 11:48:13 -0600570 for (unsigned j=0; j<i; j++)
David Pinedo8897e192015-07-31 10:56:20 -0600571 {
Chia-I Wue2fc5522015-10-26 20:04:44 +0800572 swapchainMapElem->imageList[j] = pSwapchainImages[j];
David Pinedo8897e192015-07-31 10:56:20 -0600573 }
574 }
575
David Pinedofb5b5382015-06-18 17:03:14 -0600576 }
577 loader_platform_thread_unlock_mutex(&globalLock);
578 return result;
579}
580
Ian Elliott05846062015-11-20 14:13:17 -0700581VK_LAYER_EXPORT VKAPI_ATTR VkResult VKAPI_CALL vkQueuePresentKHR(VkQueue queue, const VkPresentInfoKHR* pPresentInfo)
David Pinedofb5b5382015-06-18 17:03:14 -0600582{
583 static int frameNumber = 0;
David Pinedo8897e192015-07-31 10:56:20 -0600584 if (frameNumber == 10) {fflush(stdout); /* *((int*)0)=0; */ }
Ian Elliott7e40db92015-08-21 15:09:33 -0600585 VkResult result = get_dispatch_table(screenshot_device_table_map, queue)->QueuePresentKHR(queue, pPresentInfo);
David Pinedofb5b5382015-06-18 17:03:14 -0600586
587 loader_platform_thread_lock_mutex(&globalLock);
588
589 if (!screenshotEnvQueried)
590 {
Jon Ashburn38a497f2016-01-04 14:01:38 -0700591 const char *_vk_screenshot = loader_getenv("_VK_SCREENSHOT");
David Pinedofb5b5382015-06-18 17:03:14 -0600592 if (_vk_screenshot && *_vk_screenshot)
593 {
594 string spec(_vk_screenshot), word;
595 size_t start = 0, comma = 0;
596
597 while (start < spec.size()) {
598 int frameToAdd;
599 comma = spec.find(',', start);
600 if (comma == string::npos)
601 word = string(spec, start);
602 else
603 word = string(spec, start, comma - start);
604 frameToAdd=atoi(word.c_str());
605 // Add the frame number to list, but only do it if the word started with a digit and if
606 // it's not already in the list
607 if (*(word.c_str()) >= '0' && *(word.c_str()) <= '9' &&
608 find(screenshotFrames.begin(), screenshotFrames.end(), frameToAdd) == screenshotFrames.end())
609 {
610 screenshotFrames.push_back(frameToAdd);
611 }
612 if (comma == string::npos)
613 break;
614 start = comma + 1;
615 }
616 }
Jon Ashburn38a497f2016-01-04 14:01:38 -0700617 loader_free_getenv(_vk_screenshot);
David Pinedofb5b5382015-06-18 17:03:14 -0600618 screenshotEnvQueried = true;
619 }
620
621
622 if (result == VK_SUCCESS && !screenshotFrames.empty())
623 {
624 vector<int>::iterator it;
625 it = find(screenshotFrames.begin(), screenshotFrames.end(), frameNumber);
626 if (it != screenshotFrames.end())
627 {
628 string fileName;
629 fileName = to_string(frameNumber) + ".ppm";
David Pinedo8897e192015-07-31 10:56:20 -0600630
631 VkImage image;
Chia-I Wue2fc5522015-10-26 20:04:44 +0800632 VkSwapchainKHR swapchain;
David Pinedo8897e192015-07-31 10:56:20 -0600633 // We'll dump only one image: the first
Ian Elliott05846062015-11-20 14:13:17 -0700634 swapchain = pPresentInfo->pSwapchains[0];
635 image = swapchainMap[swapchain]->imageList[pPresentInfo->pImageIndices[0]];
David Pinedo8897e192015-07-31 10:56:20 -0600636 writePPM(fileName.c_str(), image);
David Pinedofb5b5382015-06-18 17:03:14 -0600637 screenshotFrames.erase(it);
638
639 if (screenshotFrames.empty())
640 {
David Pinedo8897e192015-07-31 10:56:20 -0600641 // Free all our maps since we are done with them.
David Pinedofb5b5382015-06-18 17:03:14 -0600642 for (auto it = swapchainMap.begin(); it != swapchainMap.end(); it++)
643 {
644 SwapchainMapStruct *swapchainMapElem = it->second;
645 delete swapchainMapElem;
646 }
647 for (auto it = imageMap.begin(); it != imageMap.end(); it++)
648 {
649 ImageMapStruct *imageMapElem = it->second;
650 delete imageMapElem;
651 }
652 for (auto it = deviceMap.begin(); it != deviceMap.end(); it++)
653 {
654 DeviceMapStruct *deviceMapElem = it->second;
655 delete deviceMapElem;
656 }
Cody Northrop49f885c2015-09-01 10:18:45 -0600657 for (auto it = physDeviceMap.begin(); it != physDeviceMap.end(); it++)
658 {
659 PhysDeviceMapStruct *physDeviceMapElem = it->second;
660 delete physDeviceMapElem;
661 }
David Pinedofb5b5382015-06-18 17:03:14 -0600662 swapchainMap.clear();
663 imageMap.clear();
664 deviceMap.clear();
Cody Northrop49f885c2015-09-01 10:18:45 -0600665 physDeviceMap.clear();
David Pinedofb5b5382015-06-18 17:03:14 -0600666 }
667 }
668 }
669 frameNumber++;
670 loader_platform_thread_unlock_mutex(&globalLock);
671 return result;
672}
673
Chia-I Wu9ab61502015-11-06 06:42:02 +0800674VK_LAYER_EXPORT VKAPI_ATTR PFN_vkVoidFunction VKAPI_CALL vkGetDeviceProcAddr(
David Pinedofb5b5382015-06-18 17:03:14 -0600675 VkDevice dev,
676 const char *funcName)
677{
David Pinedofb5b5382015-06-18 17:03:14 -0600678 if (dev == NULL) {
679 return NULL;
680 }
681
682 /* loader uses this to force layer initialization; device object is wrapped */
683 if (!strcmp(funcName, "vkGetDeviceProcAddr")) {
684 initDeviceTable(screenshot_device_table_map, (const VkBaseLayerObject *) dev);
David Pinedo8897e192015-07-31 10:56:20 -0600685 return (PFN_vkVoidFunction)vkGetDeviceProcAddr;
David Pinedofb5b5382015-06-18 17:03:14 -0600686 }
Courtney Goeltzenleuchterca173b82015-06-25 18:01:43 -0600687 if (!strcmp(funcName, "vkCreateDevice"))
David Pinedo8897e192015-07-31 10:56:20 -0600688 return (PFN_vkVoidFunction) vkCreateDevice;
Courtney Goeltzenleuchterca173b82015-06-25 18:01:43 -0600689
David Pinedofb5b5382015-06-18 17:03:14 -0600690 if (!strcmp(funcName, "vkGetDeviceQueue"))
David Pinedo8897e192015-07-31 10:56:20 -0600691 return (PFN_vkVoidFunction) vkGetDeviceQueue;
692
693 if (!strcmp(funcName, "vkCreateCommandPool"))
694 return (PFN_vkVoidFunction) vkCreateCommandPool;
David Pinedofb5b5382015-06-18 17:03:14 -0600695
David Pinedobcd0e792015-06-19 13:48:18 -0600696 VkLayerDispatchTable *pDisp = get_dispatch_table(screenshot_device_table_map, dev);
Jon Ashburn8acd2332015-09-16 18:08:32 -0600697 if (deviceExtMap.size() != 0 && deviceExtMap[pDisp].wsi_enabled)
David Pinedobcd0e792015-06-19 13:48:18 -0600698 {
Ian Elliott7e40db92015-08-21 15:09:33 -0600699 if (!strcmp(funcName, "vkCreateSwapchainKHR"))
700 return (PFN_vkVoidFunction) vkCreateSwapchainKHR;
701 if (!strcmp(funcName, "vkGetSwapchainImagesKHR"))
702 return (PFN_vkVoidFunction) vkGetSwapchainImagesKHR;
703 if (!strcmp(funcName, "vkQueuePresentKHR"))
704 return (PFN_vkVoidFunction) vkQueuePresentKHR;
David Pinedobcd0e792015-06-19 13:48:18 -0600705 }
706
707 if (pDisp->GetDeviceProcAddr == NULL)
David Pinedofb5b5382015-06-18 17:03:14 -0600708 return NULL;
David Pinedobcd0e792015-06-19 13:48:18 -0600709 return pDisp->GetDeviceProcAddr(dev, funcName);
David Pinedofb5b5382015-06-18 17:03:14 -0600710}
David Pinedo38310942015-07-09 16:23:44 -0600711
712
Chia-I Wu9ab61502015-11-06 06:42:02 +0800713VK_LAYER_EXPORT VKAPI_ATTR PFN_vkVoidFunction VKAPI_CALL vkGetInstanceProcAddr(VkInstance instance, const char* funcName)
David Pinedo38310942015-07-09 16:23:44 -0600714{
David Pinedo38310942015-07-09 16:23:44 -0600715 if (instance == VK_NULL_HANDLE) {
716 return NULL;
717 }
718
719 /* loader uses this to force layer initialization; instance object is wrapped */
720 if (!strcmp("vkGetInstanceProcAddr", funcName)) {
721 initInstanceTable((const VkBaseLayerObject *) instance);
David Pinedo8897e192015-07-31 10:56:20 -0600722 return (PFN_vkVoidFunction) vkGetInstanceProcAddr;
David Pinedo38310942015-07-09 16:23:44 -0600723 }
724
Cody Northrop49f885c2015-09-01 10:18:45 -0600725 if (!strcmp(funcName, "vkEnumeratePhysicalDevices"))
726 return (PFN_vkVoidFunction)vkEnumeratePhysicalDevices;
Jon Ashburn751c4842015-11-02 17:37:20 -0700727 if (!strcmp(funcName, "vkEnumerateDeviceExtensionProperties"))
728 return (PFN_vkVoidFunction)vkEnumerateDeviceExtensionProperties;
Cody Northrop49f885c2015-09-01 10:18:45 -0600729
David Pinedo38310942015-07-09 16:23:44 -0600730 VkLayerInstanceDispatchTable* pTable = instance_dispatch_table(instance);
731 if (pTable->GetInstanceProcAddr == NULL)
732 return NULL;
733 return pTable->GetInstanceProcAddr(instance, funcName);
David Pinedo38310942015-07-09 16:23:44 -0600734}