blob: fc180fea04ba29e42ca45950110e9e6ef9d5af21 [file] [log] [blame]
Tobin Ehlisb5fc4fb2015-09-03 09:50:06 -06001/*
Tobin Ehlisb5fc4fb2015-09-03 09:50:06 -06002 *
Courtney Goeltzenleuchterfcbe16f2015-10-29 13:50:34 -06003 * Copyright (C) 2015 Valve Corporation
Michael Lentine03107b42015-12-11 10:49:51 -08004 * Copyright (C) 2015 Google Inc.
Tobin Ehlisb5fc4fb2015-09-03 09:50:06 -06005 *
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.
Courtney Goeltzenleuchter05559522015-10-30 11:14:30 -060023 *
24 * Author: Mark Lobodzinski <mark@lunarg.com>
25 * Author: Mike Stroyan <mike@LunarG.com>
26 * Author: Tobin Ehlis <tobin@lunarg.com>
Tobin Ehlisb5fc4fb2015-09-03 09:50:06 -060027 */
28
29#include <stdio.h>
30#include <stdlib.h>
31#include <string.h>
32#include <unordered_map>
33#include <memory>
34
35#include "vk_loader_platform.h"
36#include "vk_dispatch_table_helper.h"
37#include "vk_struct_string_helper_cpp.h"
38#if defined(__GNUC__)
39#pragma GCC diagnostic ignored "-Wwrite-strings"
40#endif
41#if defined(__GNUC__)
42#pragma GCC diagnostic warning "-Wwrite-strings"
43#endif
44#include "vk_struct_size_helper.h"
45#include "device_limits.h"
46#include "vk_layer_config.h"
Michael Lentine03107b42015-12-11 10:49:51 -080047#include "vulkan/vk_debug_marker_layer.h"
Tobin Ehlisb5fc4fb2015-09-03 09:50:06 -060048#include "vk_layer_table.h"
49#include "vk_layer_debug_marker_table.h"
50#include "vk_layer_data.h"
51#include "vk_layer_logging.h"
52#include "vk_layer_extension_utils.h"
Mark Lobodzinski6f2274e2015-09-22 09:33:21 -060053#include "vk_layer_utils.h"
Tobin Ehlisb5fc4fb2015-09-03 09:50:06 -060054
Tobin Ehlis1cb7f572015-10-06 09:09:24 -060055struct devExts {
56 bool debug_marker_enabled;
57};
Tobin Ehlis9da65002015-09-24 15:25:16 -060058
59// This struct will be stored in a map hashed by the dispatchable object
Cody Northrop55443ef2015-09-28 15:09:32 -060060struct layer_data {
Tobin Ehlisb5fc4fb2015-09-03 09:50:06 -060061 debug_report_data *report_data;
Courtney Goeltzenleuchter7415d5a2015-12-09 15:48:16 -070062 std::vector<VkDebugReportCallbackEXT> logging_callback;
Tobin Ehlis1cb7f572015-10-06 09:09:24 -060063 VkLayerDispatchTable* device_dispatch_table;
64 VkLayerInstanceDispatchTable* instance_dispatch_table;
65 devExts device_extensions;
Tobin Ehlis9da65002015-09-24 15:25:16 -060066 // Track state of each instance
67 unique_ptr<INSTANCE_STATE> instanceState;
68 unique_ptr<PHYSICAL_DEVICE_STATE> physicalDeviceState;
69 VkPhysicalDeviceFeatures actualPhysicalDeviceFeatures;
70 VkPhysicalDeviceFeatures requestedPhysicalDeviceFeatures;
Tony Barbour1088c402015-10-09 11:50:32 -060071 VkPhysicalDeviceProperties physicalDeviceProperties;
Tobin Ehlis1cb7f572015-10-06 09:09:24 -060072 // Track physical device per logical device
73 VkPhysicalDevice physicalDevice;
Tobin Ehlis9da65002015-09-24 15:25:16 -060074 // Vector indices correspond to queueFamilyIndex
75 vector<unique_ptr<VkQueueFamilyProperties>> queueFamilyProperties;
Cody Northrop55443ef2015-09-28 15:09:32 -060076
77 layer_data() :
78 report_data(nullptr),
Tobin Ehlis1cb7f572015-10-06 09:09:24 -060079 device_dispatch_table(nullptr),
80 instance_dispatch_table(nullptr),
81 device_extensions(),
Tobin Ehlis9da65002015-09-24 15:25:16 -060082 instanceState(nullptr),
83 physicalDeviceState(nullptr),
Tony Barbour1088c402015-10-09 11:50:32 -060084 physicalDeviceProperties(),
Tobin Ehlis9da65002015-09-24 15:25:16 -060085 actualPhysicalDeviceFeatures(),
Tobin Ehlis1cb7f572015-10-06 09:09:24 -060086 requestedPhysicalDeviceFeatures(),
87 physicalDevice()
Cody Northrop55443ef2015-09-28 15:09:32 -060088 {};
89};
Tobin Ehlisb5fc4fb2015-09-03 09:50:06 -060090
Tobin Ehlis1cb7f572015-10-06 09:09:24 -060091static unordered_map<void *, layer_data *> layer_data_map;
Tobin Ehlisb5fc4fb2015-09-03 09:50:06 -060092
93static LOADER_PLATFORM_THREAD_ONCE_DECLARATION(g_initOnce);
94
95// TODO : This can be much smarter, using separate locks for separate global data
96static int globalLockInitialized = 0;
97static loader_platform_thread_mutex globalLock;
98
99template layer_data *get_my_data_ptr<layer_data>(
100 void *data_key,
101 std::unordered_map<void *, layer_data *> &data_map);
102
Courtney Goeltzenleuchter6d8e8182015-11-25 14:31:49 -0700103static void init_device_limits(layer_data *my_data, const VkAllocationCallbacks *pAllocator)
Tobin Ehlisb5fc4fb2015-09-03 09:50:06 -0600104{
105 uint32_t report_flags = 0;
106 uint32_t debug_action = 0;
107 FILE *log_output = NULL;
108 const char *option_str;
Courtney Goeltzenleuchter7415d5a2015-12-09 15:48:16 -0700109 VkDebugReportCallbackEXT callback;
Tobin Ehlisb5fc4fb2015-09-03 09:50:06 -0600110 // initialize DeviceLimits options
111 report_flags = getLayerOptionFlags("DeviceLimitsReportFlags", 0);
112 getLayerOptionEnum("DeviceLimitsDebugAction", (uint32_t *) &debug_action);
113
114 if (debug_action & VK_DBG_LAYER_ACTION_LOG_MSG)
115 {
116 option_str = getLayerOption("DeviceLimitsLogFilename");
Tobin Ehlisb1df55e2015-09-15 09:55:54 -0600117 log_output = getLayerLogOutput(option_str, "DeviceLimits");
Courtney Goeltzenleuchter7415d5a2015-12-09 15:48:16 -0700118 VkDebugReportCallbackCreateInfoEXT dbgCreateInfo;
Courtney Goeltzenleuchter05854bf2015-11-30 12:13:14 -0700119 memset(&dbgCreateInfo, 0, sizeof(dbgCreateInfo));
Courtney Goeltzenleuchter7415d5a2015-12-09 15:48:16 -0700120 dbgCreateInfo.sType = VK_STRUCTURE_TYPE_DEBUG_REPORT_CREATE_INFO_EXT;
Courtney Goeltzenleuchter05854bf2015-11-30 12:13:14 -0700121 dbgCreateInfo.flags = report_flags;
122 dbgCreateInfo.pfnCallback = log_callback;
123 dbgCreateInfo.pUserData = (void *) log_output;
124 layer_create_msg_callback(my_data->report_data, &dbgCreateInfo, pAllocator, &callback);
Courtney Goeltzenleuchterd6fce632015-10-05 14:51:41 -0600125 my_data->logging_callback.push_back(callback);
126 }
127
128 if (debug_action & VK_DBG_LAYER_ACTION_DEBUG_OUTPUT) {
Courtney Goeltzenleuchter7415d5a2015-12-09 15:48:16 -0700129 VkDebugReportCallbackCreateInfoEXT dbgCreateInfo;
Courtney Goeltzenleuchter05854bf2015-11-30 12:13:14 -0700130 memset(&dbgCreateInfo, 0, sizeof(dbgCreateInfo));
Courtney Goeltzenleuchter7415d5a2015-12-09 15:48:16 -0700131 dbgCreateInfo.sType = VK_STRUCTURE_TYPE_DEBUG_REPORT_CREATE_INFO_EXT;
Courtney Goeltzenleuchter05854bf2015-11-30 12:13:14 -0700132 dbgCreateInfo.flags = report_flags;
133 dbgCreateInfo.pfnCallback = win32_debug_output_msg;
134 dbgCreateInfo.pUserData = NULL;
135 layer_create_msg_callback(my_data->report_data, &dbgCreateInfo, pAllocator, &callback);
Courtney Goeltzenleuchterd6fce632015-10-05 14:51:41 -0600136 my_data->logging_callback.push_back(callback);
Tobin Ehlisb5fc4fb2015-09-03 09:50:06 -0600137 }
138
139 if (!globalLockInitialized)
140 {
141 // TODO/TBD: Need to delete this mutex sometime. How??? One
142 // suggestion is to call this during vkCreateInstance(), and then we
143 // can clean it up during vkDestroyInstance(). However, that requires
144 // that the layer have per-instance locks. We need to come back and
145 // address this soon.
146 loader_platform_thread_create_mutex(&globalLock);
147 globalLockInitialized = 1;
148 }
149}
Courtney Goeltzenleuchter20c35012015-11-30 12:14:06 -0700150
Courtney Goeltzenleuchter52857662015-12-01 14:08:28 -0700151static const VkExtensionProperties instance_extensions[] = {
Courtney Goeltzenleuchter20c35012015-11-30 12:14:06 -0700152 {
Courtney Goeltzenleuchter7415d5a2015-12-09 15:48:16 -0700153 VK_EXT_DEBUG_REPORT_EXTENSION_NAME,
154 VK_EXT_DEBUG_REPORT_REVISION
Courtney Goeltzenleuchter20c35012015-11-30 12:14:06 -0700155 }
156};
157
Chia-I Wu9ab61502015-11-06 06:42:02 +0800158VK_LAYER_EXPORT VKAPI_ATTR VkResult VKAPI_CALL vkEnumerateInstanceExtensionProperties(
Tobin Ehlisfd3843f2015-09-29 12:26:00 -0600159 const char *pLayerName,
160 uint32_t *pCount,
161 VkExtensionProperties* pProperties)
162{
Courtney Goeltzenleuchter52857662015-12-01 14:08:28 -0700163 return util_GetExtensionProperties(1, instance_extensions, pCount, pProperties);
Tobin Ehlisfd3843f2015-09-29 12:26:00 -0600164}
165
166static const VkLayerProperties dl_global_layers[] = {
167 {
Michael Lentine03107b42015-12-11 10:49:51 -0800168 "VK_LAYER_LUNARG_device_limits",
Tobin Ehlisfd3843f2015-09-29 12:26:00 -0600169 VK_API_VERSION,
170 VK_MAKE_VERSION(0, 1, 0),
171 "Validation layer: Device Limits",
172 }
173};
174
Chia-I Wu9ab61502015-11-06 06:42:02 +0800175VK_LAYER_EXPORT VKAPI_ATTR VkResult VKAPI_CALL vkEnumerateInstanceLayerProperties(
Tobin Ehlisfd3843f2015-09-29 12:26:00 -0600176 uint32_t *pCount,
177 VkLayerProperties* pProperties)
178{
179 return util_GetLayerProperties(ARRAY_SIZE(dl_global_layers),
180 dl_global_layers,
181 pCount, pProperties);
182}
Tobin Ehlisb5fc4fb2015-09-03 09:50:06 -0600183
Chia-I Wu9ab61502015-11-06 06:42:02 +0800184VK_LAYER_EXPORT VKAPI_ATTR VkResult VKAPI_CALL vkCreateInstance(const VkInstanceCreateInfo* pCreateInfo, const VkAllocationCallbacks* pAllocator, VkInstance* pInstance)
Tobin Ehlisb5fc4fb2015-09-03 09:50:06 -0600185{
Tobin Ehlis1cb7f572015-10-06 09:09:24 -0600186 layer_data *my_data = get_my_data_ptr(get_dispatch_key(*pInstance), layer_data_map);
187 VkLayerInstanceDispatchTable* pTable = my_data->instance_dispatch_table;
Chia-I Wuf7458c52015-10-26 21:10:41 +0800188 VkResult result = pTable->CreateInstance(pCreateInfo, pAllocator, pInstance);
Tobin Ehlisb5fc4fb2015-09-03 09:50:06 -0600189
190 if (result == VK_SUCCESS) {
Tobin Ehlisb5fc4fb2015-09-03 09:50:06 -0600191 my_data->report_data = debug_report_create_instance(
192 pTable,
193 *pInstance,
Chia-I Wud50a7d72015-10-26 20:48:51 +0800194 pCreateInfo->enabledExtensionNameCount,
Tobin Ehlisb5fc4fb2015-09-03 09:50:06 -0600195 pCreateInfo->ppEnabledExtensionNames);
196
Courtney Goeltzenleuchter6d8e8182015-11-25 14:31:49 -0700197 init_device_limits(my_data, pAllocator);
Tobin Ehlis9da65002015-09-24 15:25:16 -0600198 my_data->instanceState = unique_ptr<INSTANCE_STATE>(new INSTANCE_STATE());
Tobin Ehlisb5fc4fb2015-09-03 09:50:06 -0600199 }
200 return result;
201}
202
203/* hook DestroyInstance to remove tableInstanceMap entry */
Chia-I Wu9ab61502015-11-06 06:42:02 +0800204VK_LAYER_EXPORT VKAPI_ATTR void VKAPI_CALL vkDestroyInstance(VkInstance instance, const VkAllocationCallbacks* pAllocator)
Tobin Ehlisb5fc4fb2015-09-03 09:50:06 -0600205{
206 dispatch_key key = get_dispatch_key(instance);
Tobin Ehlis1cb7f572015-10-06 09:09:24 -0600207 layer_data *my_data = get_my_data_ptr(key, layer_data_map);
208 VkLayerInstanceDispatchTable *pTable = my_data->instance_dispatch_table;
Chia-I Wuf7458c52015-10-26 21:10:41 +0800209 pTable->DestroyInstance(instance, pAllocator);
Tobin Ehlisb5fc4fb2015-09-03 09:50:06 -0600210
211 // Clean up logging callback, if any
Courtney Goeltzenleuchterd6fce632015-10-05 14:51:41 -0600212 while (my_data->logging_callback.size() > 0) {
Courtney Goeltzenleuchter7415d5a2015-12-09 15:48:16 -0700213 VkDebugReportCallbackEXT callback = my_data->logging_callback.back();
Courtney Goeltzenleuchter05854bf2015-11-30 12:13:14 -0700214 layer_destroy_msg_callback(my_data->report_data, callback, pAllocator);
Courtney Goeltzenleuchterd6fce632015-10-05 14:51:41 -0600215 my_data->logging_callback.pop_back();
Tobin Ehlisb5fc4fb2015-09-03 09:50:06 -0600216 }
217
218 layer_debug_report_destroy_instance(my_data->report_data);
Tobin Ehlis1cb7f572015-10-06 09:09:24 -0600219 delete my_data->instance_dispatch_table;
220 layer_data_map.erase(key);
Tobin Ehlis0b632332015-10-07 09:38:40 -0600221 if (layer_data_map.empty()) {
222 // Release mutex when destroying last instance.
223 loader_platform_thread_delete_mutex(&globalLock);
224 globalLockInitialized = 0;
225 }
Tobin Ehlisb5fc4fb2015-09-03 09:50:06 -0600226}
227
Chia-I Wu9ab61502015-11-06 06:42:02 +0800228VK_LAYER_EXPORT VKAPI_ATTR VkResult VKAPI_CALL vkEnumeratePhysicalDevices(VkInstance instance, uint32_t* pPhysicalDeviceCount, VkPhysicalDevice* pPhysicalDevices)
Tobin Ehlisb5fc4fb2015-09-03 09:50:06 -0600229{
Tobin Ehlis9da65002015-09-24 15:25:16 -0600230 VkBool32 skipCall = VK_FALSE;
231 layer_data *my_data = get_my_data_ptr(get_dispatch_key(instance), layer_data_map);
232 if (my_data->instanceState) {
Tobin Ehlisb5fc4fb2015-09-03 09:50:06 -0600233 // For this instance, flag when vkEnumeratePhysicalDevices goes to QUERY_COUNT and then QUERY_DETAILS
234 if (NULL == pPhysicalDevices) {
Tobin Ehlis9da65002015-09-24 15:25:16 -0600235 my_data->instanceState->vkEnumeratePhysicalDevicesState = QUERY_COUNT;
Tobin Ehlisb5fc4fb2015-09-03 09:50:06 -0600236 } else {
Tobin Ehlis9da65002015-09-24 15:25:16 -0600237 if (UNCALLED == my_data->instanceState->vkEnumeratePhysicalDevicesState) {
Tobin Ehlisb5fc4fb2015-09-03 09:50:06 -0600238 // Flag error here, shouldn't be calling this without having queried count
Mark Lobodzinskib66216b2016-01-04 13:18:50 -0700239 skipCall |= log_msg(my_data->report_data, VK_DEBUG_REPORT_ERROR_BIT_EXT, VK_DEBUG_REPORT_OBJECT_TYPE_INSTANCE_EXT, 0, __LINE__, DEVLIMITS_MUST_QUERY_COUNT, "DL",
Tobin Ehlisb5fc4fb2015-09-03 09:50:06 -0600240 "Invalid call sequence to vkEnumeratePhysicalDevices() w/ non-NULL pPhysicalDevices. You should first call vkEnumeratePhysicalDevices() w/ NULL pPhysicalDevices to query pPhysicalDeviceCount.");
241 } // TODO : Could also flag a warning if re-calling this function in QUERY_DETAILS state
Tobin Ehlis9da65002015-09-24 15:25:16 -0600242 else if (my_data->instanceState->physicalDevicesCount != *pPhysicalDeviceCount) {
Courtney Goeltzenleuchter7415d5a2015-12-09 15:48:16 -0700243 // TODO: Having actual count match count from app is not a requirement, so this can be a warning
Mark Lobodzinskib66216b2016-01-04 13:18:50 -0700244 skipCall |= log_msg(my_data->report_data, VK_DEBUG_REPORT_WARN_BIT_EXT, VK_DEBUG_REPORT_OBJECT_TYPE_PHYSICAL_DEVICE_EXT, 0, __LINE__, DEVLIMITS_COUNT_MISMATCH, "DL",
Tobin Ehlis9da65002015-09-24 15:25:16 -0600245 "Call to vkEnumeratePhysicalDevices() w/ pPhysicalDeviceCount value %u, but actual count supported by this instance is %u.", *pPhysicalDeviceCount, my_data->instanceState->physicalDevicesCount);
Tobin Ehlisb5fc4fb2015-09-03 09:50:06 -0600246 }
Tobin Ehlis9da65002015-09-24 15:25:16 -0600247 my_data->instanceState->vkEnumeratePhysicalDevicesState = QUERY_DETAILS;
Tobin Ehlisb5fc4fb2015-09-03 09:50:06 -0600248 }
Tobin Ehlis9da65002015-09-24 15:25:16 -0600249 if (skipCall)
Courtney Goeltzenleuchter52fee652015-12-10 16:41:22 -0700250 return VK_ERROR_VALIDATION_FAILED_EXT;
Tobin Ehlis1cb7f572015-10-06 09:09:24 -0600251 VkResult result = my_data->instance_dispatch_table->EnumeratePhysicalDevices(instance, pPhysicalDeviceCount, pPhysicalDevices);
Tobin Ehlisb5fc4fb2015-09-03 09:50:06 -0600252 if (NULL == pPhysicalDevices) {
Tobin Ehlis9da65002015-09-24 15:25:16 -0600253 my_data->instanceState->physicalDevicesCount = *pPhysicalDeviceCount;
Tobin Ehlisb5fc4fb2015-09-03 09:50:06 -0600254 } else { // Save physical devices
255 for (uint32_t i=0; i < *pPhysicalDeviceCount; i++) {
Tobin Ehlis9da65002015-09-24 15:25:16 -0600256 layer_data *phy_dev_data = get_my_data_ptr(get_dispatch_key(pPhysicalDevices[i]), layer_data_map);
257 phy_dev_data->physicalDeviceState = unique_ptr<PHYSICAL_DEVICE_STATE>(new PHYSICAL_DEVICE_STATE());
258 // Init actual features for each physical device
Tobin Ehlis1cb7f572015-10-06 09:09:24 -0600259 my_data->instance_dispatch_table->GetPhysicalDeviceFeatures(pPhysicalDevices[i], &(phy_dev_data->actualPhysicalDeviceFeatures));
Tony Barbour1088c402015-10-09 11:50:32 -0600260 my_data->instance_dispatch_table->GetPhysicalDeviceProperties(pPhysicalDevices[i], &(phy_dev_data->physicalDeviceProperties));
Tobin Ehlisb5fc4fb2015-09-03 09:50:06 -0600261 }
262 }
263 return result;
264 } else {
Mark Lobodzinskib66216b2016-01-04 13:18:50 -0700265 log_msg(my_data->report_data, VK_DEBUG_REPORT_ERROR_BIT_EXT, VK_DEBUG_REPORT_OBJECT_TYPE_INSTANCE_EXT, 0, __LINE__, DEVLIMITS_INVALID_INSTANCE, "DL",
Michael Lentine010f4692015-11-03 16:19:46 -0800266 "Invalid instance (%#" PRIxLEAST64 ") passed into vkEnumeratePhysicalDevices().", (uint64_t)instance);
Tobin Ehlisb5fc4fb2015-09-03 09:50:06 -0600267 }
Courtney Goeltzenleuchter52fee652015-12-10 16:41:22 -0700268 return VK_ERROR_VALIDATION_FAILED_EXT;
Tobin Ehlisb5fc4fb2015-09-03 09:50:06 -0600269}
270
Chia-I Wu9ab61502015-11-06 06:42:02 +0800271VK_LAYER_EXPORT VKAPI_ATTR void VKAPI_CALL vkGetPhysicalDeviceFeatures(VkPhysicalDevice physicalDevice, VkPhysicalDeviceFeatures* pFeatures)
Tobin Ehlisb5fc4fb2015-09-03 09:50:06 -0600272{
Tobin Ehlis9da65002015-09-24 15:25:16 -0600273 layer_data *phy_dev_data = get_my_data_ptr(get_dispatch_key(physicalDevice), layer_data_map);
274 phy_dev_data->physicalDeviceState->vkGetPhysicalDeviceFeaturesState = QUERY_DETAILS;
Courtney Goeltzenleuchter06d89472015-10-20 16:40:38 -0600275 phy_dev_data->instance_dispatch_table->GetPhysicalDeviceFeatures(physicalDevice, pFeatures);
Tobin Ehlisb5fc4fb2015-09-03 09:50:06 -0600276}
277
Chia-I Wu9ab61502015-11-06 06:42:02 +0800278VK_LAYER_EXPORT VKAPI_ATTR void VKAPI_CALL vkGetPhysicalDeviceFormatProperties(VkPhysicalDevice physicalDevice, VkFormat format, VkFormatProperties* pFormatProperties)
Tobin Ehlisb5fc4fb2015-09-03 09:50:06 -0600279{
Courtney Goeltzenleuchter06d89472015-10-20 16:40:38 -0600280 get_my_data_ptr(get_dispatch_key(physicalDevice), layer_data_map)->instance_dispatch_table->GetPhysicalDeviceFormatProperties(
281 physicalDevice, format, pFormatProperties);
Tobin Ehlisb5fc4fb2015-09-03 09:50:06 -0600282}
283
Chia-I Wu9ab61502015-11-06 06:42:02 +0800284VK_LAYER_EXPORT VKAPI_ATTR VkResult VKAPI_CALL vkGetPhysicalDeviceImageFormatProperties(VkPhysicalDevice physicalDevice, VkFormat format, VkImageType type, VkImageTiling tiling, VkImageUsageFlags usage, VkImageCreateFlags flags, VkImageFormatProperties* pImageFormatProperties)
Tobin Ehlisb5fc4fb2015-09-03 09:50:06 -0600285{
Chia-I Wu17241042015-10-31 00:31:16 +0800286 return get_my_data_ptr(get_dispatch_key(physicalDevice), layer_data_map)->instance_dispatch_table->GetPhysicalDeviceImageFormatProperties(physicalDevice, format, type, tiling, usage, flags, pImageFormatProperties);
Tobin Ehlisb5fc4fb2015-09-03 09:50:06 -0600287}
288
Chia-I Wu9ab61502015-11-06 06:42:02 +0800289VK_LAYER_EXPORT VKAPI_ATTR void VKAPI_CALL vkGetPhysicalDeviceProperties(VkPhysicalDevice physicalDevice, VkPhysicalDeviceProperties* pProperties)
Tobin Ehlisb5fc4fb2015-09-03 09:50:06 -0600290{
Tobin Ehlis1cb7f572015-10-06 09:09:24 -0600291 layer_data *phy_dev_data = get_my_data_ptr(get_dispatch_key(physicalDevice), layer_data_map);
Courtney Goeltzenleuchter06d89472015-10-20 16:40:38 -0600292 phy_dev_data->instance_dispatch_table->GetPhysicalDeviceProperties(physicalDevice, pProperties);
Tobin Ehlisb5fc4fb2015-09-03 09:50:06 -0600293}
294
Chia-I Wu9ab61502015-11-06 06:42:02 +0800295VK_LAYER_EXPORT VKAPI_ATTR void VKAPI_CALL vkGetPhysicalDeviceQueueFamilyProperties(VkPhysicalDevice physicalDevice, uint32_t* pCount, VkQueueFamilyProperties* pQueueFamilyProperties)
Tobin Ehlisb5fc4fb2015-09-03 09:50:06 -0600296{
Tobin Ehlis9da65002015-09-24 15:25:16 -0600297 VkBool32 skipCall = VK_FALSE;
298 layer_data *phy_dev_data = get_my_data_ptr(get_dispatch_key(physicalDevice), layer_data_map);
299 if (phy_dev_data->physicalDeviceState) {
Tobin Ehlisb5fc4fb2015-09-03 09:50:06 -0600300 if (NULL == pQueueFamilyProperties) {
Tobin Ehlis9da65002015-09-24 15:25:16 -0600301 phy_dev_data->physicalDeviceState->vkGetPhysicalDeviceQueueFamilyPropertiesState = QUERY_COUNT;
Tobin Ehlisb5fc4fb2015-09-03 09:50:06 -0600302 } else {
303 // Verify that for each physical device, this function is called first with NULL pQueueFamilyProperties ptr in order to get count
Tobin Ehlis9da65002015-09-24 15:25:16 -0600304 if (UNCALLED == phy_dev_data->physicalDeviceState->vkGetPhysicalDeviceQueueFamilyPropertiesState) {
Mark Lobodzinskib66216b2016-01-04 13:18:50 -0700305 skipCall |= log_msg(phy_dev_data->report_data, VK_DEBUG_REPORT_ERROR_BIT_EXT, VK_DEBUG_REPORT_OBJECT_TYPE_PHYSICAL_DEVICE_EXT, 0, __LINE__, DEVLIMITS_MUST_QUERY_COUNT, "DL",
Tobin Ehlisb5fc4fb2015-09-03 09:50:06 -0600306 "Invalid call sequence to vkGetPhysicalDeviceQueueFamilyProperties() w/ non-NULL pQueueFamilyProperties. You should first call vkGetPhysicalDeviceQueueFamilyProperties() w/ NULL pQueueFamilyProperties to query pCount.");
307 }
Tobin Ehlis9da65002015-09-24 15:25:16 -0600308 // Then verify that pCount that is passed in on second call matches what was returned
309 if (phy_dev_data->physicalDeviceState->queueFamilyPropertiesCount != *pCount) {
Courtney Goeltzenleuchter7415d5a2015-12-09 15:48:16 -0700310
311 // TODO: this is not a requirement of the Valid Usage section for vkGetPhysicalDeviceQueueFamilyProperties, so provide as warning
Mark Lobodzinskib66216b2016-01-04 13:18:50 -0700312 skipCall |= log_msg(phy_dev_data->report_data, VK_DEBUG_REPORT_WARN_BIT_EXT, VK_DEBUG_REPORT_OBJECT_TYPE_PHYSICAL_DEVICE_EXT, 0, __LINE__, DEVLIMITS_COUNT_MISMATCH, "DL",
Tobin Ehlis9da65002015-09-24 15:25:16 -0600313 "Call to vkGetPhysicalDeviceQueueFamilyProperties() w/ pCount value %u, but actual count supported by this physicalDevice is %u.", *pCount, phy_dev_data->physicalDeviceState->queueFamilyPropertiesCount);
Tobin Ehlisb5fc4fb2015-09-03 09:50:06 -0600314 }
Tobin Ehlis9da65002015-09-24 15:25:16 -0600315 phy_dev_data->physicalDeviceState->vkGetPhysicalDeviceQueueFamilyPropertiesState = QUERY_DETAILS;
Tobin Ehlisb5fc4fb2015-09-03 09:50:06 -0600316 }
Tobin Ehlis9da65002015-09-24 15:25:16 -0600317 if (skipCall)
Courtney Goeltzenleuchter06d89472015-10-20 16:40:38 -0600318 return;
319 phy_dev_data->instance_dispatch_table->GetPhysicalDeviceQueueFamilyProperties(physicalDevice, pCount, pQueueFamilyProperties);
Tobin Ehlisb5fc4fb2015-09-03 09:50:06 -0600320 if (NULL == pQueueFamilyProperties) {
Tobin Ehlis9da65002015-09-24 15:25:16 -0600321 phy_dev_data->physicalDeviceState->queueFamilyPropertiesCount = *pCount;
Tobin Ehlisb5fc4fb2015-09-03 09:50:06 -0600322 } else { // Save queue family properties
Tobin Ehlis9da65002015-09-24 15:25:16 -0600323 phy_dev_data->queueFamilyProperties.reserve(*pCount);
Tobin Ehlisb5fc4fb2015-09-03 09:50:06 -0600324 for (uint32_t i=0; i < *pCount; i++) {
Tobin Ehlis9da65002015-09-24 15:25:16 -0600325 phy_dev_data->queueFamilyProperties.emplace_back(new VkQueueFamilyProperties(pQueueFamilyProperties[i]));
Tobin Ehlisb5fc4fb2015-09-03 09:50:06 -0600326 }
327 }
Courtney Goeltzenleuchter06d89472015-10-20 16:40:38 -0600328 return;
Tobin Ehlisb5fc4fb2015-09-03 09:50:06 -0600329 } else {
Mark Lobodzinskib66216b2016-01-04 13:18:50 -0700330 log_msg(phy_dev_data->report_data, VK_DEBUG_REPORT_ERROR_BIT_EXT, VK_DEBUG_REPORT_OBJECT_TYPE_PHYSICAL_DEVICE_EXT, 0, __LINE__, DEVLIMITS_INVALID_PHYSICAL_DEVICE, "DL",
Michael Lentine010f4692015-11-03 16:19:46 -0800331 "Invalid physicalDevice (%#" PRIxLEAST64 ") passed into vkGetPhysicalDeviceQueueFamilyProperties().", (uint64_t)physicalDevice);
Tobin Ehlisb5fc4fb2015-09-03 09:50:06 -0600332 }
333}
334
Chia-I Wu9ab61502015-11-06 06:42:02 +0800335VK_LAYER_EXPORT VKAPI_ATTR void VKAPI_CALL vkGetPhysicalDeviceMemoryProperties(VkPhysicalDevice physicalDevice, VkPhysicalDeviceMemoryProperties* pMemoryProperties)
Tobin Ehlisb5fc4fb2015-09-03 09:50:06 -0600336{
Courtney Goeltzenleuchter06d89472015-10-20 16:40:38 -0600337 get_my_data_ptr(get_dispatch_key(physicalDevice), layer_data_map)->instance_dispatch_table->GetPhysicalDeviceMemoryProperties(physicalDevice, pMemoryProperties);
Tobin Ehlisb5fc4fb2015-09-03 09:50:06 -0600338}
339
Chia-I Wu9ab61502015-11-06 06:42:02 +0800340VK_LAYER_EXPORT VKAPI_ATTR void VKAPI_CALL vkGetPhysicalDeviceSparseImageFormatProperties(VkPhysicalDevice physicalDevice, VkFormat format, VkImageType type, VkSampleCountFlagBits samples, VkImageUsageFlags usage, VkImageTiling tiling, uint32_t* pNumProperties, VkSparseImageFormatProperties* pProperties)
Tobin Ehlisb5fc4fb2015-09-03 09:50:06 -0600341{
Courtney Goeltzenleuchter06d89472015-10-20 16:40:38 -0600342 get_my_data_ptr(get_dispatch_key(physicalDevice), layer_data_map)->instance_dispatch_table->GetPhysicalDeviceSparseImageFormatProperties(physicalDevice, format, type, samples, usage, tiling, pNumProperties, pProperties);
Tobin Ehlisb5fc4fb2015-09-03 09:50:06 -0600343}
344
Chia-I Wu9ab61502015-11-06 06:42:02 +0800345VK_LAYER_EXPORT VKAPI_ATTR void VKAPI_CALL vkCmdSetViewport(
Chia-I Wu3432a0c2015-10-27 18:04:07 +0800346 VkCommandBuffer commandBuffer,
Jon Ashburn19d3bf12015-12-30 14:06:55 -0700347 uint32_t firstViewport,
348 uint32_t viewportCount,
349 const VkViewport* pViewports)
Courtney Goeltzenleuchter078f8172015-09-21 11:44:06 -0600350{
351 VkBool32 skipCall = VK_FALSE;
352 /* TODO: Verify viewportCount < maxViewports from VkPhysicalDeviceLimits */
353 if (VK_FALSE == skipCall) {
Chia-I Wu3432a0c2015-10-27 18:04:07 +0800354 layer_data *my_data = get_my_data_ptr(get_dispatch_key(commandBuffer), layer_data_map);
Jon Ashburn19d3bf12015-12-30 14:06:55 -0700355 my_data->device_dispatch_table->CmdSetViewport(commandBuffer, firstViewport, viewportCount, pViewports);
Courtney Goeltzenleuchter078f8172015-09-21 11:44:06 -0600356 }
357}
358
Chia-I Wu9ab61502015-11-06 06:42:02 +0800359VK_LAYER_EXPORT VKAPI_ATTR void VKAPI_CALL vkCmdSetScissor(
Chia-I Wu3432a0c2015-10-27 18:04:07 +0800360 VkCommandBuffer commandBuffer,
Jon Ashburn19d3bf12015-12-30 14:06:55 -0700361 uint32_t firstScissor,
362 uint32_t scissorCount,
363 const VkRect2D* pScissors)
Courtney Goeltzenleuchter49c73082015-09-17 15:06:17 -0600364{
365 VkBool32 skipCall = VK_FALSE;
Courtney Goeltzenleuchter078f8172015-09-21 11:44:06 -0600366 /* TODO: Verify scissorCount < maxViewports from VkPhysicalDeviceLimits */
367 /* TODO: viewportCount and scissorCount must match at draw time */
Courtney Goeltzenleuchter49c73082015-09-17 15:06:17 -0600368 if (VK_FALSE == skipCall) {
Chia-I Wu3432a0c2015-10-27 18:04:07 +0800369 layer_data *my_data = get_my_data_ptr(get_dispatch_key(commandBuffer), layer_data_map);
Jon Ashburn19d3bf12015-12-30 14:06:55 -0700370 my_data->device_dispatch_table->CmdSetScissor(commandBuffer, firstScissor, scissorCount, pScissors);
Courtney Goeltzenleuchter49c73082015-09-17 15:06:17 -0600371 }
372}
373
Tobin Ehlisb5fc4fb2015-09-03 09:50:06 -0600374static void createDeviceRegisterExtensions(const VkDeviceCreateInfo* pCreateInfo, VkDevice device)
375{
376 uint32_t i;
Tobin Ehlis1cb7f572015-10-06 09:09:24 -0600377 layer_data *my_data = get_my_data_ptr(get_dispatch_key(device), layer_data_map);
378 my_data->device_extensions.debug_marker_enabled = false;
Tobin Ehlisb5fc4fb2015-09-03 09:50:06 -0600379
Chia-I Wud50a7d72015-10-26 20:48:51 +0800380 for (i = 0; i < pCreateInfo->enabledExtensionNameCount; i++) {
Tobin Ehlisb5fc4fb2015-09-03 09:50:06 -0600381 if (strcmp(pCreateInfo->ppEnabledExtensionNames[i], DEBUG_MARKER_EXTENSION_NAME) == 0) {
382 /* Found a matching extension name, mark it enabled and init dispatch table*/
383 initDebugMarkerTable(device);
Tobin Ehlis1cb7f572015-10-06 09:09:24 -0600384 my_data->device_extensions.debug_marker_enabled = true;
Tobin Ehlisb5fc4fb2015-09-03 09:50:06 -0600385 }
386
387 }
388}
389
Tobin Ehlis9da65002015-09-24 15:25:16 -0600390// Verify that features have been queried and verify that requested features are available
391static VkBool32 validate_features_request(layer_data *phy_dev_data)
392{
393 VkBool32 skipCall = VK_FALSE;
394 // Verify that all of the requested features are available
395 // Get ptrs into actual and requested structs and if requested is 1 but actual is 0, request is invalid
396 VkBool32* actual = (VkBool32*)&(phy_dev_data->actualPhysicalDeviceFeatures);
397 VkBool32* requested = (VkBool32*)&(phy_dev_data->requestedPhysicalDeviceFeatures);
398 // TODO : This is a nice, compact way to loop through struct, but a bad way to report issues
399 // Need to provide the struct member name with the issue. To do that seems like we'll
400 // have to loop through each struct member which should be done w/ codegen to keep in synch.
401 uint32_t errors = 0;
402 uint32_t totalBools = sizeof(VkPhysicalDeviceFeatures)/sizeof(VkBool32);
403 for (uint32_t i = 0; i < totalBools; i++) {
404 if (requested[i] > actual[i]) {
Mark Lobodzinskib66216b2016-01-04 13:18:50 -0700405 skipCall |= log_msg(phy_dev_data->report_data, VK_DEBUG_REPORT_ERROR_BIT_EXT, VK_DEBUG_REPORT_OBJECT_TYPE_PHYSICAL_DEVICE_EXT, 0, __LINE__, DEVLIMITS_INVALID_FEATURE_REQUESTED, "DL",
Tobin Ehlis9da65002015-09-24 15:25:16 -0600406 "While calling vkCreateDevice(), requesting feature #%u in VkPhysicalDeviceFeatures struct, which is not available on this device.", i);
407 errors++;
408 }
409 }
410 if (errors && (UNCALLED == phy_dev_data->physicalDeviceState->vkGetPhysicalDeviceFeaturesState)) {
411 // If user didn't request features, notify them that they should
Courtney Goeltzenleuchter7415d5a2015-12-09 15:48:16 -0700412 // TODO: Verify this against the spec. I believe this is an invalid use of the API and should return an error
Mark Lobodzinskib66216b2016-01-04 13:18:50 -0700413 skipCall |= log_msg(phy_dev_data->report_data, VK_DEBUG_REPORT_WARN_BIT_EXT, VK_DEBUG_REPORT_OBJECT_TYPE_PHYSICAL_DEVICE_EXT, 0, __LINE__, DEVLIMITS_INVALID_FEATURE_REQUESTED, "DL",
Tobin Ehlis9da65002015-09-24 15:25:16 -0600414 "You requested features that are unavailable on this device. You should first query feature availability by calling vkGetPhysicalDeviceFeatures().");
415 }
Tobin Ehlis72b27cc2015-09-29 11:22:37 -0600416 return skipCall;
Tobin Ehlis9da65002015-09-24 15:25:16 -0600417}
418
Chia-I Wu9ab61502015-11-06 06:42:02 +0800419VK_LAYER_EXPORT VKAPI_ATTR VkResult VKAPI_CALL vkCreateDevice(VkPhysicalDevice gpu, const VkDeviceCreateInfo* pCreateInfo, const VkAllocationCallbacks* pAllocator, VkDevice* pDevice)
Tobin Ehlisb5fc4fb2015-09-03 09:50:06 -0600420{
Tobin Ehlis9da65002015-09-24 15:25:16 -0600421 VkBool32 skipCall = VK_FALSE;
422 layer_data *phy_dev_data = get_my_data_ptr(get_dispatch_key(gpu), layer_data_map);
Tobin Ehlis54a6c182015-09-22 14:00:58 -0600423 // First check is app has actually requested queueFamilyProperties
Tobin Ehlis9da65002015-09-24 15:25:16 -0600424 if (!phy_dev_data->physicalDeviceState) {
Mark Lobodzinskib66216b2016-01-04 13:18:50 -0700425 skipCall |= log_msg(phy_dev_data->report_data, VK_DEBUG_REPORT_ERROR_BIT_EXT, VK_DEBUG_REPORT_OBJECT_TYPE_PHYSICAL_DEVICE_EXT, 0, __LINE__, DEVLIMITS_MUST_QUERY_COUNT, "DL",
Tobin Ehlis54a6c182015-09-22 14:00:58 -0600426 "Invalid call to vkCreateDevice() w/o first calling vkEnumeratePhysicalDevices().");
Tobin Ehlis9da65002015-09-24 15:25:16 -0600427 } else if (QUERY_DETAILS != phy_dev_data->physicalDeviceState->vkGetPhysicalDeviceQueueFamilyPropertiesState) {
Courtney Goeltzenleuchter7415d5a2015-12-09 15:48:16 -0700428 // TODO: This is not called out as an invalid use in the spec so make more informative recommendation.
Mark Lobodzinskib66216b2016-01-04 13:18:50 -0700429 skipCall |= log_msg(phy_dev_data->report_data, VK_DEBUG_REPORT_WARN_BIT_EXT, VK_DEBUG_REPORT_OBJECT_TYPE_PHYSICAL_DEVICE_EXT, 0, __LINE__, DEVLIMITS_INVALID_QUEUE_CREATE_REQUEST, "DL",
Courtney Goeltzenleuchter7415d5a2015-12-09 15:48:16 -0700430 "Call to vkCreateDevice() w/o first calling vkGetPhysicalDeviceQueueFamilyProperties().");
Tobin Ehlis54a6c182015-09-22 14:00:58 -0600431 } else {
432 // Check that the requested queue properties are valid
Chia-I Wu02124482015-11-06 06:42:02 +0800433 for (uint32_t i=0; i<pCreateInfo->queueCreateInfoCount; i++) {
434 uint32_t requestedIndex = pCreateInfo->pQueueCreateInfos[i].queueFamilyIndex;
Tobin Ehlis9da65002015-09-24 15:25:16 -0600435 if (phy_dev_data->queueFamilyProperties.size() <= requestedIndex) { // requested index is out of bounds for this physical device
Mark Lobodzinskib66216b2016-01-04 13:18:50 -0700436 skipCall |= log_msg(phy_dev_data->report_data, VK_DEBUG_REPORT_ERROR_BIT_EXT, VK_DEBUG_REPORT_OBJECT_TYPE_PHYSICAL_DEVICE_EXT, 0, __LINE__, DEVLIMITS_INVALID_QUEUE_CREATE_REQUEST, "DL",
Tobin Ehlis54a6c182015-09-22 14:00:58 -0600437 "Invalid queue create request in vkCreateDevice(). Invalid queueFamilyIndex %u requested.", requestedIndex);
Chia-I Wu02124482015-11-06 06:42:02 +0800438 } else if (pCreateInfo->pQueueCreateInfos[i].queueCount > phy_dev_data->queueFamilyProperties[requestedIndex]->queueCount) {
Mark Lobodzinskib66216b2016-01-04 13:18:50 -0700439 skipCall |= log_msg(phy_dev_data->report_data, VK_DEBUG_REPORT_ERROR_BIT_EXT, VK_DEBUG_REPORT_OBJECT_TYPE_PHYSICAL_DEVICE_EXT, 0, __LINE__, DEVLIMITS_INVALID_QUEUE_CREATE_REQUEST, "DL",
Chia-I Wu02124482015-11-06 06:42:02 +0800440 "Invalid queue create request in vkCreateDevice(). QueueFamilyIndex %u only has %u queues, but requested queueCount is %u.", requestedIndex, phy_dev_data->queueFamilyProperties[requestedIndex]->queueCount, pCreateInfo->pQueueCreateInfos[i].queueCount);
Tobin Ehlis54a6c182015-09-22 14:00:58 -0600441 }
Tobin Ehlisb5fc4fb2015-09-03 09:50:06 -0600442 }
443 }
Tobin Ehlis9da65002015-09-24 15:25:16 -0600444 // Check that any requested features are available
445 if (pCreateInfo->pEnabledFeatures) {
446 phy_dev_data->requestedPhysicalDeviceFeatures = *(pCreateInfo->pEnabledFeatures);
447 skipCall |= validate_features_request(phy_dev_data);
448 }
449 if (skipCall)
Courtney Goeltzenleuchter52fee652015-12-10 16:41:22 -0700450 return VK_ERROR_VALIDATION_FAILED_EXT;
Tobin Ehlis9da65002015-09-24 15:25:16 -0600451
Tobin Ehlis1cb7f572015-10-06 09:09:24 -0600452 layer_data *my_device_data = get_my_data_ptr(get_dispatch_key(*pDevice), layer_data_map);
Chia-I Wuf7458c52015-10-26 21:10:41 +0800453 VkResult result = my_device_data->device_dispatch_table->CreateDevice(gpu, pCreateInfo, pAllocator, pDevice);
Tobin Ehlisb5fc4fb2015-09-03 09:50:06 -0600454 if (result == VK_SUCCESS) {
Tobin Ehlis9da65002015-09-24 15:25:16 -0600455 my_device_data->report_data = layer_debug_report_create_device(phy_dev_data->report_data, *pDevice);
Tobin Ehlisb5fc4fb2015-09-03 09:50:06 -0600456 createDeviceRegisterExtensions(pCreateInfo, *pDevice);
Tobin Ehlis1cb7f572015-10-06 09:09:24 -0600457 my_device_data->physicalDevice = gpu;
Tobin Ehlisb5fc4fb2015-09-03 09:50:06 -0600458 }
459 return result;
460}
461
Chia-I Wu9ab61502015-11-06 06:42:02 +0800462VK_LAYER_EXPORT VKAPI_ATTR void VKAPI_CALL vkDestroyDevice(VkDevice device, const VkAllocationCallbacks* pAllocator)
Tobin Ehlisb5fc4fb2015-09-03 09:50:06 -0600463{
464 // Free device lifetime allocations
465 dispatch_key key = get_dispatch_key(device);
Tobin Ehlis1cb7f572015-10-06 09:09:24 -0600466 layer_data *my_device_data = get_my_data_ptr(key, layer_data_map);
Chia-I Wuf7458c52015-10-26 21:10:41 +0800467 my_device_data->device_dispatch_table->DestroyDevice(device, pAllocator);
Tobin Ehlis1cb7f572015-10-06 09:09:24 -0600468 tableDebugMarkerMap.erase(key);
469 delete my_device_data->device_dispatch_table;
470 layer_data_map.erase(key);
Tobin Ehlisb5fc4fb2015-09-03 09:50:06 -0600471}
472
Chia-I Wu9ab61502015-11-06 06:42:02 +0800473VK_LAYER_EXPORT VKAPI_ATTR VkResult VKAPI_CALL vkCreateCommandPool(VkDevice device, const VkCommandPoolCreateInfo* pCreateInfo, const VkAllocationCallbacks* pAllocator, VkCommandPool* pCommandPool)
Tobin Ehlisb5fc4fb2015-09-03 09:50:06 -0600474{
Tobin Ehlis9da65002015-09-24 15:25:16 -0600475 // TODO : Verify that requested QueueFamilyIndex for this pool exists
Chia-I Wu3432a0c2015-10-27 18:04:07 +0800476 VkResult result = get_my_data_ptr(get_dispatch_key(device), layer_data_map)->device_dispatch_table->CreateCommandPool(device, pCreateInfo, pAllocator, pCommandPool);
Tobin Ehlis9da65002015-09-24 15:25:16 -0600477 return result;
Tobin Ehlisb5fc4fb2015-09-03 09:50:06 -0600478}
479
Chia-I Wu9ab61502015-11-06 06:42:02 +0800480VK_LAYER_EXPORT VKAPI_ATTR void VKAPI_CALL vkDestroyCommandPool(VkDevice device, VkCommandPool commandPool, const VkAllocationCallbacks* pAllocator)
Tobin Ehlisb5fc4fb2015-09-03 09:50:06 -0600481{
Chia-I Wu3432a0c2015-10-27 18:04:07 +0800482 get_my_data_ptr(get_dispatch_key(device), layer_data_map)->device_dispatch_table->DestroyCommandPool(device, commandPool, pAllocator);
Tobin Ehlisb5fc4fb2015-09-03 09:50:06 -0600483}
484
Chia-I Wu9ab61502015-11-06 06:42:02 +0800485VK_LAYER_EXPORT VKAPI_ATTR VkResult VKAPI_CALL vkResetCommandPool(VkDevice device, VkCommandPool commandPool, VkCommandPoolResetFlags flags)
Tobin Ehlisb5fc4fb2015-09-03 09:50:06 -0600486{
Chia-I Wu3432a0c2015-10-27 18:04:07 +0800487 VkResult result = get_my_data_ptr(get_dispatch_key(device), layer_data_map)->device_dispatch_table->ResetCommandPool(device, commandPool, flags);
Tobin Ehlis9da65002015-09-24 15:25:16 -0600488 return result;
Tobin Ehlisb5fc4fb2015-09-03 09:50:06 -0600489}
490
Chia-I Wu9ab61502015-11-06 06:42:02 +0800491VK_LAYER_EXPORT VKAPI_ATTR VkResult VKAPI_CALL vkAllocateCommandBuffers(VkDevice device, const VkCommandBufferAllocateInfo* pCreateInfo, VkCommandBuffer* pCommandBuffer)
Tobin Ehlisb5fc4fb2015-09-03 09:50:06 -0600492{
Chia-I Wu3432a0c2015-10-27 18:04:07 +0800493 VkResult result = get_my_data_ptr(get_dispatch_key(device), layer_data_map)->device_dispatch_table->AllocateCommandBuffers(device, pCreateInfo, pCommandBuffer);
Tobin Ehlis9da65002015-09-24 15:25:16 -0600494 return result;
495}
Mark Lobodzinskifbb130e2015-10-06 11:59:54 -0600496
Chia-I Wu9ab61502015-11-06 06:42:02 +0800497VK_LAYER_EXPORT VKAPI_ATTR void VKAPI_CALL vkFreeCommandBuffers(VkDevice device, VkCommandPool commandPool, uint32_t count, const VkCommandBuffer* pCommandBuffers)
Tobin Ehlis9da65002015-09-24 15:25:16 -0600498{
Chia-I Wu3432a0c2015-10-27 18:04:07 +0800499 get_my_data_ptr(get_dispatch_key(device), layer_data_map)->device_dispatch_table->FreeCommandBuffers(device, commandPool, count, pCommandBuffers);
Tobin Ehlisb5fc4fb2015-09-03 09:50:06 -0600500}
501
Chia-I Wu9ab61502015-11-06 06:42:02 +0800502VK_LAYER_EXPORT VKAPI_ATTR void VKAPI_CALL vkGetDeviceQueue(VkDevice device, uint32_t queueFamilyIndex, uint32_t queueIndex, VkQueue* pQueue)
Tobin Ehlisb5fc4fb2015-09-03 09:50:06 -0600503{
Tobin Ehlis9da65002015-09-24 15:25:16 -0600504 VkBool32 skipCall = VK_FALSE;
Tobin Ehlis1cb7f572015-10-06 09:09:24 -0600505 layer_data *dev_data = get_my_data_ptr(get_dispatch_key(device), layer_data_map);
506 VkPhysicalDevice gpu = dev_data->physicalDevice;
Tobin Ehlis9da65002015-09-24 15:25:16 -0600507 layer_data *phy_dev_data = get_my_data_ptr(get_dispatch_key(gpu), layer_data_map);
508 if (queueFamilyIndex >= phy_dev_data->queueFamilyProperties.size()) { // requested index is out of bounds for this physical device
Mark Lobodzinskib66216b2016-01-04 13:18:50 -0700509 skipCall |= log_msg(phy_dev_data->report_data, VK_DEBUG_REPORT_ERROR_BIT_EXT, VK_DEBUG_REPORT_OBJECT_TYPE_PHYSICAL_DEVICE_EXT, 0, __LINE__, DEVLIMITS_INVALID_QUEUE_CREATE_REQUEST, "DL",
Tobin Ehlisb5fc4fb2015-09-03 09:50:06 -0600510 "Invalid queueFamilyIndex %u requested in vkGetDeviceQueue().", queueFamilyIndex);
Tobin Ehlis9da65002015-09-24 15:25:16 -0600511 } else if (queueIndex >= phy_dev_data->queueFamilyProperties[queueFamilyIndex]->queueCount) {
Mark Lobodzinskib66216b2016-01-04 13:18:50 -0700512 skipCall |= log_msg(phy_dev_data->report_data, VK_DEBUG_REPORT_ERROR_BIT_EXT, VK_DEBUG_REPORT_OBJECT_TYPE_PHYSICAL_DEVICE_EXT, 0, __LINE__, DEVLIMITS_INVALID_QUEUE_CREATE_REQUEST, "DL",
Tobin Ehlis9da65002015-09-24 15:25:16 -0600513 "Invalid queue request in vkGetDeviceQueue(). QueueFamilyIndex %u only has %u queues, but requested queueIndex is %u.", queueFamilyIndex, phy_dev_data->queueFamilyProperties[queueFamilyIndex]->queueCount, queueIndex);
Tobin Ehlisb5fc4fb2015-09-03 09:50:06 -0600514 }
Tobin Ehlis9da65002015-09-24 15:25:16 -0600515 if (skipCall)
Courtney Goeltzenleuchter06d89472015-10-20 16:40:38 -0600516 return;
517 dev_data->device_dispatch_table->GetDeviceQueue(device, queueFamilyIndex, queueIndex, pQueue);
Tobin Ehlisb5fc4fb2015-09-03 09:50:06 -0600518}
519
Chia-I Wu9ab61502015-11-06 06:42:02 +0800520VK_LAYER_EXPORT VKAPI_ATTR void VKAPI_CALL vkCmdUpdateBuffer(
Chia-I Wu3432a0c2015-10-27 18:04:07 +0800521 VkCommandBuffer commandBuffer,
522 VkBuffer dstBuffer,
523 VkDeviceSize dstOffset,
Mike Stroyana3082432015-09-25 13:39:21 -0600524 VkDeviceSize dataSize,
525 const uint32_t* pData)
526{
Chia-I Wu3432a0c2015-10-27 18:04:07 +0800527 layer_data *dev_data = get_my_data_ptr(get_dispatch_key(commandBuffer), layer_data_map);
Mike Stroyana3082432015-09-25 13:39:21 -0600528
Chia-I Wu3432a0c2015-10-27 18:04:07 +0800529 // dstOffset is the byte offset into the buffer to start updating and must be a multiple of 4.
530 if (dstOffset & 3) {
531 layer_data *my_data = get_my_data_ptr(get_dispatch_key(commandBuffer), layer_data_map);
Mark Lobodzinskib66216b2016-01-04 13:18:50 -0700532 if (log_msg(my_data->report_data, VK_DEBUG_REPORT_ERROR_BIT_EXT, (VkDebugReportObjectTypeEXT)0, 0, __LINE__, 1, "DL",
Chia-I Wu3432a0c2015-10-27 18:04:07 +0800533 "vkCmdUpdateBuffer parameter, VkDeviceSize dstOffset, is not a multiple of 4")) {
Mike Stroyana3082432015-09-25 13:39:21 -0600534 return;
535 }
536 }
537
538 // dataSize is the number of bytes to update, which must be a multiple of 4.
539 if (dataSize & 3) {
Chia-I Wu3432a0c2015-10-27 18:04:07 +0800540 layer_data *my_data = get_my_data_ptr(get_dispatch_key(commandBuffer), layer_data_map);
Mark Lobodzinskib66216b2016-01-04 13:18:50 -0700541 if (log_msg(my_data->report_data, VK_DEBUG_REPORT_ERROR_BIT_EXT, (VkDebugReportObjectTypeEXT)0, 0, __LINE__, 1, "DL",
Mike Stroyana3082432015-09-25 13:39:21 -0600542 "vkCmdUpdateBuffer parameter, VkDeviceSize dataSize, is not a multiple of 4")) {
543 return;
544 }
545 }
546
Chia-I Wu3432a0c2015-10-27 18:04:07 +0800547 dev_data->device_dispatch_table->CmdUpdateBuffer(commandBuffer, dstBuffer, dstOffset, dataSize, pData);
Mike Stroyana3082432015-09-25 13:39:21 -0600548}
549
Chia-I Wu9ab61502015-11-06 06:42:02 +0800550VK_LAYER_EXPORT VKAPI_ATTR void VKAPI_CALL vkCmdFillBuffer(
Chia-I Wu3432a0c2015-10-27 18:04:07 +0800551 VkCommandBuffer commandBuffer,
552 VkBuffer dstBuffer,
553 VkDeviceSize dstOffset,
Chia-I Wu2bfb33c2015-10-26 17:24:52 +0800554 VkDeviceSize size,
Mike Stroyana3082432015-09-25 13:39:21 -0600555 uint32_t data)
556{
Chia-I Wu3432a0c2015-10-27 18:04:07 +0800557 layer_data *dev_data = get_my_data_ptr(get_dispatch_key(commandBuffer), layer_data_map);
Mike Stroyana3082432015-09-25 13:39:21 -0600558
Chia-I Wu3432a0c2015-10-27 18:04:07 +0800559 // dstOffset is the byte offset into the buffer to start filling and must be a multiple of 4.
560 if (dstOffset & 3) {
561 layer_data *my_data = get_my_data_ptr(get_dispatch_key(commandBuffer), layer_data_map);
Mark Lobodzinskib66216b2016-01-04 13:18:50 -0700562 if (log_msg(my_data->report_data, VK_DEBUG_REPORT_ERROR_BIT_EXT, (VkDebugReportObjectTypeEXT)0, 0, __LINE__, 1, "DL",
Chia-I Wu3432a0c2015-10-27 18:04:07 +0800563 "vkCmdFillBuffer parameter, VkDeviceSize dstOffset, is not a multiple of 4")) {
Mike Stroyana3082432015-09-25 13:39:21 -0600564 return;
565 }
566 }
567
Chia-I Wu2bfb33c2015-10-26 17:24:52 +0800568 // size is the number of bytes to fill, which must be a multiple of 4.
569 if (size & 3) {
Chia-I Wu3432a0c2015-10-27 18:04:07 +0800570 layer_data *my_data = get_my_data_ptr(get_dispatch_key(commandBuffer), layer_data_map);
Mark Lobodzinskib66216b2016-01-04 13:18:50 -0700571 if (log_msg(my_data->report_data, VK_DEBUG_REPORT_ERROR_BIT_EXT, (VkDebugReportObjectTypeEXT)0, 0, __LINE__, 1, "DL",
Chia-I Wu2bfb33c2015-10-26 17:24:52 +0800572 "vkCmdFillBuffer parameter, VkDeviceSize size, is not a multiple of 4")) {
Mike Stroyana3082432015-09-25 13:39:21 -0600573 return;
574 }
575 }
576
Chia-I Wu3432a0c2015-10-27 18:04:07 +0800577 dev_data->device_dispatch_table->CmdFillBuffer(commandBuffer, dstBuffer, dstOffset, size, data);
Mike Stroyana3082432015-09-25 13:39:21 -0600578}
579
Courtney Goeltzenleuchter7415d5a2015-12-09 15:48:16 -0700580VK_LAYER_EXPORT VKAPI_ATTR VkResult VKAPI_CALL vkCreateDebugReportCallbackEXT(
581 VkInstance instance,
582 const VkDebugReportCallbackCreateInfoEXT* pCreateInfo,
583 const VkAllocationCallbacks* pAllocator,
584 VkDebugReportCallbackEXT* pMsgCallback)
Tobin Ehlisb5fc4fb2015-09-03 09:50:06 -0600585{
Tobin Ehlis1cb7f572015-10-06 09:09:24 -0600586 layer_data *my_data = get_my_data_ptr(get_dispatch_key(instance), layer_data_map);
Courtney Goeltzenleuchter7415d5a2015-12-09 15:48:16 -0700587 VkResult res = my_data->instance_dispatch_table->CreateDebugReportCallbackEXT(instance, pCreateInfo, pAllocator, pMsgCallback);
Tobin Ehlisb5fc4fb2015-09-03 09:50:06 -0600588 if (VK_SUCCESS == res) {
Courtney Goeltzenleuchter05854bf2015-11-30 12:13:14 -0700589 res = layer_create_msg_callback(my_data->report_data, pCreateInfo, pAllocator, pMsgCallback);
Tobin Ehlisb5fc4fb2015-09-03 09:50:06 -0600590 }
591 return res;
592}
593
Courtney Goeltzenleuchter7415d5a2015-12-09 15:48:16 -0700594VK_LAYER_EXPORT VKAPI_ATTR void VKAPI_CALL vkDestroyDebugReportCallbackEXT(
Courtney Goeltzenleuchter05854bf2015-11-30 12:13:14 -0700595 VkInstance instance,
Courtney Goeltzenleuchter7415d5a2015-12-09 15:48:16 -0700596 VkDebugReportCallbackEXT msgCallback,
Courtney Goeltzenleuchter05854bf2015-11-30 12:13:14 -0700597 const VkAllocationCallbacks* pAllocator)
Tobin Ehlisb5fc4fb2015-09-03 09:50:06 -0600598{
Tobin Ehlisb5fc4fb2015-09-03 09:50:06 -0600599 layer_data *my_data = get_my_data_ptr(get_dispatch_key(instance), layer_data_map);
Courtney Goeltzenleuchter7415d5a2015-12-09 15:48:16 -0700600 my_data->instance_dispatch_table->DestroyDebugReportCallbackEXT(instance, msgCallback, pAllocator);
Courtney Goeltzenleuchter05854bf2015-11-30 12:13:14 -0700601 layer_destroy_msg_callback(my_data->report_data, msgCallback, pAllocator);
Tobin Ehlisb5fc4fb2015-09-03 09:50:06 -0600602}
603
Courtney Goeltzenleuchter7415d5a2015-12-09 15:48:16 -0700604VK_LAYER_EXPORT VKAPI_ATTR void VKAPI_CALL vkDebugReportMessageEXT(
Courtney Goeltzenleuchterf0de7242015-12-01 14:10:55 -0700605 VkInstance instance,
Courtney Goeltzenleuchter7415d5a2015-12-09 15:48:16 -0700606 VkDebugReportFlagsEXT flags,
607 VkDebugReportObjectTypeEXT objType,
Courtney Goeltzenleuchterf0de7242015-12-01 14:10:55 -0700608 uint64_t object,
609 size_t location,
610 int32_t msgCode,
611 const char* pLayerPrefix,
612 const char* pMsg)
613{
614 layer_data *my_data = get_my_data_ptr(get_dispatch_key(instance), layer_data_map);
Courtney Goeltzenleuchter7415d5a2015-12-09 15:48:16 -0700615 my_data->instance_dispatch_table->DebugReportMessageEXT(instance, flags, objType, object, location, msgCode, pLayerPrefix, pMsg);
Courtney Goeltzenleuchterf0de7242015-12-01 14:10:55 -0700616}
617
Chia-I Wu9ab61502015-11-06 06:42:02 +0800618VK_LAYER_EXPORT VKAPI_ATTR PFN_vkVoidFunction VKAPI_CALL vkGetDeviceProcAddr(VkDevice dev, const char* funcName)
Tobin Ehlisb5fc4fb2015-09-03 09:50:06 -0600619{
620 if (dev == NULL)
621 return NULL;
622
Tobin Ehlis1cb7f572015-10-06 09:09:24 -0600623 layer_data *my_data;
Tobin Ehlisb5fc4fb2015-09-03 09:50:06 -0600624 /* loader uses this to force layer initialization; device object is wrapped */
625 if (!strcmp(funcName, "vkGetDeviceProcAddr")) {
Tobin Ehlis1cb7f572015-10-06 09:09:24 -0600626 VkBaseLayerObject* wrapped_dev = (VkBaseLayerObject*) dev;
627 my_data = get_my_data_ptr(get_dispatch_key(wrapped_dev->baseObject), layer_data_map);
628 my_data->device_dispatch_table = new VkLayerDispatchTable;
629 layer_initialize_dispatch_table(my_data->device_dispatch_table, wrapped_dev);
Tobin Ehlisb5fc4fb2015-09-03 09:50:06 -0600630 return (PFN_vkVoidFunction) vkGetDeviceProcAddr;
631 }
Tobin Ehlis1cb7f572015-10-06 09:09:24 -0600632 my_data = get_my_data_ptr(get_dispatch_key(dev), layer_data_map);
Tobin Ehlisb5fc4fb2015-09-03 09:50:06 -0600633 if (!strcmp(funcName, "vkCreateDevice"))
634 return (PFN_vkVoidFunction) vkCreateDevice;
635 if (!strcmp(funcName, "vkDestroyDevice"))
636 return (PFN_vkVoidFunction) vkDestroyDevice;
Tobin Ehlisb5fc4fb2015-09-03 09:50:06 -0600637 if (!strcmp(funcName, "vkGetDeviceQueue"))
638 return (PFN_vkVoidFunction) vkGetDeviceQueue;
Tobin Ehlis9da65002015-09-24 15:25:16 -0600639 if (!strcmp(funcName, "CreateCommandPool"))
640 return (PFN_vkVoidFunction) vkCreateCommandPool;
641 if (!strcmp(funcName, "DestroyCommandPool"))
642 return (PFN_vkVoidFunction) vkDestroyCommandPool;
643 if (!strcmp(funcName, "ResetCommandPool"))
644 return (PFN_vkVoidFunction) vkResetCommandPool;
Chia-I Wu3432a0c2015-10-27 18:04:07 +0800645 if (!strcmp(funcName, "vkAllocateCommandBuffers"))
646 return (PFN_vkVoidFunction) vkAllocateCommandBuffers;
Courtney Goeltzenleuchterbee18a92015-10-23 14:21:05 -0600647 if (!strcmp(funcName, "vkFreeCommandBuffers"))
648 return (PFN_vkVoidFunction) vkFreeCommandBuffers;
Mike Stroyana3082432015-09-25 13:39:21 -0600649 if (!strcmp(funcName, "vkCmdUpdateBuffer"))
650 return (PFN_vkVoidFunction) vkCmdUpdateBuffer;
651 if (!strcmp(funcName, "vkCmdFillBuffer"))
652 return (PFN_vkVoidFunction) vkCmdFillBuffer;
Tobin Ehlisb5fc4fb2015-09-03 09:50:06 -0600653
Tobin Ehlis1cb7f572015-10-06 09:09:24 -0600654 VkLayerDispatchTable* pTable = my_data->device_dispatch_table;
Tobin Ehlisb5fc4fb2015-09-03 09:50:06 -0600655 {
656 if (pTable->GetDeviceProcAddr == NULL)
657 return NULL;
658 return pTable->GetDeviceProcAddr(dev, funcName);
659 }
660}
661
Chia-I Wu9ab61502015-11-06 06:42:02 +0800662VK_LAYER_EXPORT VKAPI_ATTR PFN_vkVoidFunction VKAPI_CALL vkGetInstanceProcAddr(VkInstance instance, const char* funcName)
Tobin Ehlisb5fc4fb2015-09-03 09:50:06 -0600663{
664 PFN_vkVoidFunction fptr;
665 if (instance == NULL)
666 return NULL;
667
Tobin Ehlis1cb7f572015-10-06 09:09:24 -0600668 layer_data *my_data;
Tobin Ehlisb5fc4fb2015-09-03 09:50:06 -0600669 /* loader uses this to force layer initialization; instance object is wrapped */
670 if (!strcmp(funcName, "vkGetInstanceProcAddr")) {
Tobin Ehlis1cb7f572015-10-06 09:09:24 -0600671 VkBaseLayerObject* wrapped_inst = (VkBaseLayerObject*) instance;
672 my_data = get_my_data_ptr(get_dispatch_key(wrapped_inst->baseObject), layer_data_map);
673 my_data->instance_dispatch_table = new VkLayerInstanceDispatchTable;
674 layer_init_instance_dispatch_table(my_data->instance_dispatch_table, wrapped_inst);
Tobin Ehlisb5fc4fb2015-09-03 09:50:06 -0600675 return (PFN_vkVoidFunction) vkGetInstanceProcAddr;
676 }
Tobin Ehlis1cb7f572015-10-06 09:09:24 -0600677 my_data = get_my_data_ptr(get_dispatch_key(instance), layer_data_map);
Tobin Ehlisb5fc4fb2015-09-03 09:50:06 -0600678 if (!strcmp(funcName, "vkCreateInstance"))
679 return (PFN_vkVoidFunction) vkCreateInstance;
680 if (!strcmp(funcName, "vkDestroyInstance"))
681 return (PFN_vkVoidFunction) vkDestroyInstance;
682 if (!strcmp(funcName, "vkEnumeratePhysicalDevices"))
683 return (PFN_vkVoidFunction) vkEnumeratePhysicalDevices;
684 if (!strcmp(funcName, "vkGetPhysicalDeviceFeatures"))
685 return (PFN_vkVoidFunction) vkGetPhysicalDeviceFeatures;
686 if (!strcmp(funcName, "vkGetPhysicalDeviceFormatProperties"))
687 return (PFN_vkVoidFunction) vkGetPhysicalDeviceFormatProperties;
688 if (!strcmp(funcName, "vkGetPhysicalDeviceImageFormatProperties"))
689 return (PFN_vkVoidFunction) vkGetPhysicalDeviceImageFormatProperties;
690 if (!strcmp(funcName, "vkGetPhysicalDeviceProperties"))
691 return (PFN_vkVoidFunction) vkGetPhysicalDeviceProperties;
692 if (!strcmp(funcName, "vkGetPhysicalDeviceQueueFamilyProperties"))
693 return (PFN_vkVoidFunction) vkGetPhysicalDeviceQueueFamilyProperties;
694 if (!strcmp(funcName, "vkGetPhysicalDeviceMemoryProperties"))
695 return (PFN_vkVoidFunction) vkGetPhysicalDeviceMemoryProperties;
Tobin Ehlisb5fc4fb2015-09-03 09:50:06 -0600696 if (!strcmp(funcName, "vkGetPhysicalDeviceSparseImageFormatProperties"))
697 return (PFN_vkVoidFunction) vkGetPhysicalDeviceSparseImageFormatProperties;
Tobin Ehlisfd3843f2015-09-29 12:26:00 -0600698 if (!strcmp(funcName, "vkEnumerateInstanceLayerProperties"))
699 return (PFN_vkVoidFunction) vkEnumerateInstanceLayerProperties;
700 if (!strcmp(funcName, "vkEnumerateInstanceExtensionProperties"))
701 return (PFN_vkVoidFunction) vkEnumerateInstanceExtensionProperties;
Tobin Ehlisb5fc4fb2015-09-03 09:50:06 -0600702
Tobin Ehlisb5fc4fb2015-09-03 09:50:06 -0600703 fptr = debug_report_get_instance_proc_addr(my_data->report_data, funcName);
704 if (fptr)
705 return fptr;
706
707 {
Tobin Ehlis1cb7f572015-10-06 09:09:24 -0600708 VkLayerInstanceDispatchTable* pTable = my_data->instance_dispatch_table;
Tobin Ehlisb5fc4fb2015-09-03 09:50:06 -0600709 if (pTable->GetInstanceProcAddr == NULL)
710 return NULL;
711 return pTable->GetInstanceProcAddr(instance, funcName);
712 }
713}