blob: e89dd668d77772de6cac3deb33fc23e8c95c5d36 [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
Tobin Ehlisb5fc4fb2015-09-03 09:50:06 -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: Mark Lobodzinski <mark@lunarg.com>
24 * Author: Mike Stroyan <mike@LunarG.com>
25 * Author: Tobin Ehlis <tobin@lunarg.com>
Tobin Ehlisb5fc4fb2015-09-03 09:50:06 -060026 */
27
28#include <stdio.h>
29#include <stdlib.h>
30#include <string.h>
31#include <unordered_map>
32#include <memory>
33
34#include "vk_loader_platform.h"
35#include "vk_dispatch_table_helper.h"
36#include "vk_struct_string_helper_cpp.h"
37#if defined(__GNUC__)
38#pragma GCC diagnostic ignored "-Wwrite-strings"
39#endif
40#if defined(__GNUC__)
41#pragma GCC diagnostic warning "-Wwrite-strings"
42#endif
43#include "vk_struct_size_helper.h"
44#include "device_limits.h"
45#include "vk_layer_config.h"
46#include "vk_debug_marker_layer.h"
Tobin Ehlisb5fc4fb2015-09-03 09:50:06 -060047#include "vk_layer_table.h"
48#include "vk_layer_debug_marker_table.h"
49#include "vk_layer_data.h"
50#include "vk_layer_logging.h"
51#include "vk_layer_extension_utils.h"
Mark Lobodzinski6f2274e2015-09-22 09:33:21 -060052#include "vk_layer_utils.h"
Tobin Ehlisb5fc4fb2015-09-03 09:50:06 -060053
Tobin Ehlis1cb7f572015-10-06 09:09:24 -060054struct devExts {
55 bool debug_marker_enabled;
56};
Tobin Ehlis9da65002015-09-24 15:25:16 -060057
58// This struct will be stored in a map hashed by the dispatchable object
Cody Northrop55443ef2015-09-28 15:09:32 -060059struct layer_data {
Tobin Ehlisb5fc4fb2015-09-03 09:50:06 -060060 debug_report_data *report_data;
Courtney Goeltzenleuchter7415d5a2015-12-09 15:48:16 -070061 std::vector<VkDebugReportCallbackEXT> logging_callback;
Tobin Ehlis1cb7f572015-10-06 09:09:24 -060062 VkLayerDispatchTable* device_dispatch_table;
63 VkLayerInstanceDispatchTable* instance_dispatch_table;
64 devExts device_extensions;
Tobin Ehlis9da65002015-09-24 15:25:16 -060065 // Track state of each instance
66 unique_ptr<INSTANCE_STATE> instanceState;
67 unique_ptr<PHYSICAL_DEVICE_STATE> physicalDeviceState;
68 VkPhysicalDeviceFeatures actualPhysicalDeviceFeatures;
69 VkPhysicalDeviceFeatures requestedPhysicalDeviceFeatures;
Tony Barbour1088c402015-10-09 11:50:32 -060070 VkPhysicalDeviceProperties physicalDeviceProperties;
Tobin Ehlis1cb7f572015-10-06 09:09:24 -060071 // Track physical device per logical device
72 VkPhysicalDevice physicalDevice;
Tobin Ehlis9da65002015-09-24 15:25:16 -060073 // Vector indices correspond to queueFamilyIndex
74 vector<unique_ptr<VkQueueFamilyProperties>> queueFamilyProperties;
Cody Northrop55443ef2015-09-28 15:09:32 -060075
76 layer_data() :
77 report_data(nullptr),
Tobin Ehlis1cb7f572015-10-06 09:09:24 -060078 device_dispatch_table(nullptr),
79 instance_dispatch_table(nullptr),
80 device_extensions(),
Tobin Ehlis9da65002015-09-24 15:25:16 -060081 instanceState(nullptr),
82 physicalDeviceState(nullptr),
Tony Barbour1088c402015-10-09 11:50:32 -060083 physicalDeviceProperties(),
Tobin Ehlis9da65002015-09-24 15:25:16 -060084 actualPhysicalDeviceFeatures(),
Tobin Ehlis1cb7f572015-10-06 09:09:24 -060085 requestedPhysicalDeviceFeatures(),
86 physicalDevice()
Cody Northrop55443ef2015-09-28 15:09:32 -060087 {};
88};
Tobin Ehlisb5fc4fb2015-09-03 09:50:06 -060089
Tobin Ehlis1cb7f572015-10-06 09:09:24 -060090static unordered_map<void *, layer_data *> layer_data_map;
Tobin Ehlisb5fc4fb2015-09-03 09:50:06 -060091
92static LOADER_PLATFORM_THREAD_ONCE_DECLARATION(g_initOnce);
93
94// TODO : This can be much smarter, using separate locks for separate global data
95static int globalLockInitialized = 0;
96static loader_platform_thread_mutex globalLock;
97
98template layer_data *get_my_data_ptr<layer_data>(
99 void *data_key,
100 std::unordered_map<void *, layer_data *> &data_map);
101
Courtney Goeltzenleuchter6d8e8182015-11-25 14:31:49 -0700102static void init_device_limits(layer_data *my_data, const VkAllocationCallbacks *pAllocator)
Tobin Ehlisb5fc4fb2015-09-03 09:50:06 -0600103{
104 uint32_t report_flags = 0;
105 uint32_t debug_action = 0;
106 FILE *log_output = NULL;
107 const char *option_str;
Courtney Goeltzenleuchter7415d5a2015-12-09 15:48:16 -0700108 VkDebugReportCallbackEXT callback;
Tobin Ehlisb5fc4fb2015-09-03 09:50:06 -0600109 // initialize DeviceLimits options
110 report_flags = getLayerOptionFlags("DeviceLimitsReportFlags", 0);
111 getLayerOptionEnum("DeviceLimitsDebugAction", (uint32_t *) &debug_action);
112
113 if (debug_action & VK_DBG_LAYER_ACTION_LOG_MSG)
114 {
115 option_str = getLayerOption("DeviceLimitsLogFilename");
Tobin Ehlisb1df55e2015-09-15 09:55:54 -0600116 log_output = getLayerLogOutput(option_str, "DeviceLimits");
Courtney Goeltzenleuchter7415d5a2015-12-09 15:48:16 -0700117 VkDebugReportCallbackCreateInfoEXT dbgCreateInfo;
Courtney Goeltzenleuchter05854bf2015-11-30 12:13:14 -0700118 memset(&dbgCreateInfo, 0, sizeof(dbgCreateInfo));
Courtney Goeltzenleuchter7415d5a2015-12-09 15:48:16 -0700119 dbgCreateInfo.sType = VK_STRUCTURE_TYPE_DEBUG_REPORT_CREATE_INFO_EXT;
Courtney Goeltzenleuchter05854bf2015-11-30 12:13:14 -0700120 dbgCreateInfo.flags = report_flags;
121 dbgCreateInfo.pfnCallback = log_callback;
122 dbgCreateInfo.pUserData = (void *) log_output;
123 layer_create_msg_callback(my_data->report_data, &dbgCreateInfo, pAllocator, &callback);
Courtney Goeltzenleuchterd6fce632015-10-05 14:51:41 -0600124 my_data->logging_callback.push_back(callback);
125 }
126
127 if (debug_action & VK_DBG_LAYER_ACTION_DEBUG_OUTPUT) {
Courtney Goeltzenleuchter7415d5a2015-12-09 15:48:16 -0700128 VkDebugReportCallbackCreateInfoEXT dbgCreateInfo;
Courtney Goeltzenleuchter05854bf2015-11-30 12:13:14 -0700129 memset(&dbgCreateInfo, 0, sizeof(dbgCreateInfo));
Courtney Goeltzenleuchter7415d5a2015-12-09 15:48:16 -0700130 dbgCreateInfo.sType = VK_STRUCTURE_TYPE_DEBUG_REPORT_CREATE_INFO_EXT;
Courtney Goeltzenleuchter05854bf2015-11-30 12:13:14 -0700131 dbgCreateInfo.flags = report_flags;
132 dbgCreateInfo.pfnCallback = win32_debug_output_msg;
133 dbgCreateInfo.pUserData = NULL;
134 layer_create_msg_callback(my_data->report_data, &dbgCreateInfo, pAllocator, &callback);
Courtney Goeltzenleuchterd6fce632015-10-05 14:51:41 -0600135 my_data->logging_callback.push_back(callback);
Tobin Ehlisb5fc4fb2015-09-03 09:50:06 -0600136 }
137
138 if (!globalLockInitialized)
139 {
140 // TODO/TBD: Need to delete this mutex sometime. How??? One
141 // suggestion is to call this during vkCreateInstance(), and then we
142 // can clean it up during vkDestroyInstance(). However, that requires
143 // that the layer have per-instance locks. We need to come back and
144 // address this soon.
145 loader_platform_thread_create_mutex(&globalLock);
146 globalLockInitialized = 1;
147 }
148}
Courtney Goeltzenleuchter20c35012015-11-30 12:14:06 -0700149
Courtney Goeltzenleuchter52857662015-12-01 14:08:28 -0700150static const VkExtensionProperties instance_extensions[] = {
Courtney Goeltzenleuchter20c35012015-11-30 12:14:06 -0700151 {
Courtney Goeltzenleuchter7415d5a2015-12-09 15:48:16 -0700152 VK_EXT_DEBUG_REPORT_EXTENSION_NAME,
153 VK_EXT_DEBUG_REPORT_REVISION
Courtney Goeltzenleuchter20c35012015-11-30 12:14:06 -0700154 }
155};
156
Chia-I Wu9ab61502015-11-06 06:42:02 +0800157VK_LAYER_EXPORT VKAPI_ATTR VkResult VKAPI_CALL vkEnumerateInstanceExtensionProperties(
Tobin Ehlisfd3843f2015-09-29 12:26:00 -0600158 const char *pLayerName,
159 uint32_t *pCount,
160 VkExtensionProperties* pProperties)
161{
Courtney Goeltzenleuchter52857662015-12-01 14:08:28 -0700162 return util_GetExtensionProperties(1, instance_extensions, pCount, pProperties);
Tobin Ehlisfd3843f2015-09-29 12:26:00 -0600163}
164
165static const VkLayerProperties dl_global_layers[] = {
166 {
167 "DeviceLimits",
168 VK_API_VERSION,
169 VK_MAKE_VERSION(0, 1, 0),
170 "Validation layer: Device Limits",
171 }
172};
173
Chia-I Wu9ab61502015-11-06 06:42:02 +0800174VK_LAYER_EXPORT VKAPI_ATTR VkResult VKAPI_CALL vkEnumerateInstanceLayerProperties(
Tobin Ehlisfd3843f2015-09-29 12:26:00 -0600175 uint32_t *pCount,
176 VkLayerProperties* pProperties)
177{
178 return util_GetLayerProperties(ARRAY_SIZE(dl_global_layers),
179 dl_global_layers,
180 pCount, pProperties);
181}
Tobin Ehlisb5fc4fb2015-09-03 09:50:06 -0600182
Chia-I Wu9ab61502015-11-06 06:42:02 +0800183VK_LAYER_EXPORT VKAPI_ATTR VkResult VKAPI_CALL vkCreateInstance(const VkInstanceCreateInfo* pCreateInfo, const VkAllocationCallbacks* pAllocator, VkInstance* pInstance)
Tobin Ehlisb5fc4fb2015-09-03 09:50:06 -0600184{
Tobin Ehlis1cb7f572015-10-06 09:09:24 -0600185 layer_data *my_data = get_my_data_ptr(get_dispatch_key(*pInstance), layer_data_map);
186 VkLayerInstanceDispatchTable* pTable = my_data->instance_dispatch_table;
Chia-I Wuf7458c52015-10-26 21:10:41 +0800187 VkResult result = pTable->CreateInstance(pCreateInfo, pAllocator, pInstance);
Tobin Ehlisb5fc4fb2015-09-03 09:50:06 -0600188
189 if (result == VK_SUCCESS) {
Tobin Ehlisb5fc4fb2015-09-03 09:50:06 -0600190 my_data->report_data = debug_report_create_instance(
191 pTable,
192 *pInstance,
Chia-I Wud50a7d72015-10-26 20:48:51 +0800193 pCreateInfo->enabledExtensionNameCount,
Tobin Ehlisb5fc4fb2015-09-03 09:50:06 -0600194 pCreateInfo->ppEnabledExtensionNames);
195
Courtney Goeltzenleuchter6d8e8182015-11-25 14:31:49 -0700196 init_device_limits(my_data, pAllocator);
Tobin Ehlis9da65002015-09-24 15:25:16 -0600197 my_data->instanceState = unique_ptr<INSTANCE_STATE>(new INSTANCE_STATE());
Tobin Ehlisb5fc4fb2015-09-03 09:50:06 -0600198 }
199 return result;
200}
201
202/* hook DestroyInstance to remove tableInstanceMap entry */
Chia-I Wu9ab61502015-11-06 06:42:02 +0800203VK_LAYER_EXPORT VKAPI_ATTR void VKAPI_CALL vkDestroyInstance(VkInstance instance, const VkAllocationCallbacks* pAllocator)
Tobin Ehlisb5fc4fb2015-09-03 09:50:06 -0600204{
205 dispatch_key key = get_dispatch_key(instance);
Tobin Ehlis1cb7f572015-10-06 09:09:24 -0600206 layer_data *my_data = get_my_data_ptr(key, layer_data_map);
207 VkLayerInstanceDispatchTable *pTable = my_data->instance_dispatch_table;
Chia-I Wuf7458c52015-10-26 21:10:41 +0800208 pTable->DestroyInstance(instance, pAllocator);
Tobin Ehlisb5fc4fb2015-09-03 09:50:06 -0600209
210 // Clean up logging callback, if any
Courtney Goeltzenleuchterd6fce632015-10-05 14:51:41 -0600211 while (my_data->logging_callback.size() > 0) {
Courtney Goeltzenleuchter7415d5a2015-12-09 15:48:16 -0700212 VkDebugReportCallbackEXT callback = my_data->logging_callback.back();
Courtney Goeltzenleuchter05854bf2015-11-30 12:13:14 -0700213 layer_destroy_msg_callback(my_data->report_data, callback, pAllocator);
Courtney Goeltzenleuchterd6fce632015-10-05 14:51:41 -0600214 my_data->logging_callback.pop_back();
Tobin Ehlisb5fc4fb2015-09-03 09:50:06 -0600215 }
216
217 layer_debug_report_destroy_instance(my_data->report_data);
Tobin Ehlis1cb7f572015-10-06 09:09:24 -0600218 delete my_data->instance_dispatch_table;
219 layer_data_map.erase(key);
Tobin Ehlis0b632332015-10-07 09:38:40 -0600220 if (layer_data_map.empty()) {
221 // Release mutex when destroying last instance.
222 loader_platform_thread_delete_mutex(&globalLock);
223 globalLockInitialized = 0;
224 }
Tobin Ehlisb5fc4fb2015-09-03 09:50:06 -0600225}
226
Chia-I Wu9ab61502015-11-06 06:42:02 +0800227VK_LAYER_EXPORT VKAPI_ATTR VkResult VKAPI_CALL vkEnumeratePhysicalDevices(VkInstance instance, uint32_t* pPhysicalDeviceCount, VkPhysicalDevice* pPhysicalDevices)
Tobin Ehlisb5fc4fb2015-09-03 09:50:06 -0600228{
Tobin Ehlis9da65002015-09-24 15:25:16 -0600229 VkBool32 skipCall = VK_FALSE;
230 layer_data *my_data = get_my_data_ptr(get_dispatch_key(instance), layer_data_map);
231 if (my_data->instanceState) {
Tobin Ehlisb5fc4fb2015-09-03 09:50:06 -0600232 // For this instance, flag when vkEnumeratePhysicalDevices goes to QUERY_COUNT and then QUERY_DETAILS
233 if (NULL == pPhysicalDevices) {
Tobin Ehlis9da65002015-09-24 15:25:16 -0600234 my_data->instanceState->vkEnumeratePhysicalDevicesState = QUERY_COUNT;
Tobin Ehlisb5fc4fb2015-09-03 09:50:06 -0600235 } else {
Tobin Ehlis9da65002015-09-24 15:25:16 -0600236 if (UNCALLED == my_data->instanceState->vkEnumeratePhysicalDevicesState) {
Tobin Ehlisb5fc4fb2015-09-03 09:50:06 -0600237 // Flag error here, shouldn't be calling this without having queried count
Courtney Goeltzenleuchter7415d5a2015-12-09 15:48:16 -0700238 skipCall |= log_msg(my_data->report_data, VK_DEBUG_REPORT_ERROR_BIT_EXT, VK_DEBUG_REPORT_OBJECT_TYPE_INSTANCE_EXT, 0, 0, DEVLIMITS_MUST_QUERY_COUNT, "DL",
Tobin Ehlisb5fc4fb2015-09-03 09:50:06 -0600239 "Invalid call sequence to vkEnumeratePhysicalDevices() w/ non-NULL pPhysicalDevices. You should first call vkEnumeratePhysicalDevices() w/ NULL pPhysicalDevices to query pPhysicalDeviceCount.");
240 } // TODO : Could also flag a warning if re-calling this function in QUERY_DETAILS state
Tobin Ehlis9da65002015-09-24 15:25:16 -0600241 else if (my_data->instanceState->physicalDevicesCount != *pPhysicalDeviceCount) {
Courtney Goeltzenleuchter7415d5a2015-12-09 15:48:16 -0700242 // TODO: Having actual count match count from app is not a requirement, so this can be a warning
243 skipCall |= log_msg(my_data->report_data, VK_DEBUG_REPORT_WARN_BIT_EXT, VK_DEBUG_REPORT_OBJECT_TYPE_PHYSICAL_DEVICE_EXT, 0, 0, DEVLIMITS_COUNT_MISMATCH, "DL",
Tobin Ehlis9da65002015-09-24 15:25:16 -0600244 "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 -0600245 }
Tobin Ehlis9da65002015-09-24 15:25:16 -0600246 my_data->instanceState->vkEnumeratePhysicalDevicesState = QUERY_DETAILS;
Tobin Ehlisb5fc4fb2015-09-03 09:50:06 -0600247 }
Tobin Ehlis9da65002015-09-24 15:25:16 -0600248 if (skipCall)
Courtney Goeltzenleuchter52fee652015-12-10 16:41:22 -0700249 return VK_ERROR_VALIDATION_FAILED_EXT;
Tobin Ehlis1cb7f572015-10-06 09:09:24 -0600250 VkResult result = my_data->instance_dispatch_table->EnumeratePhysicalDevices(instance, pPhysicalDeviceCount, pPhysicalDevices);
Tobin Ehlisb5fc4fb2015-09-03 09:50:06 -0600251 if (NULL == pPhysicalDevices) {
Tobin Ehlis9da65002015-09-24 15:25:16 -0600252 my_data->instanceState->physicalDevicesCount = *pPhysicalDeviceCount;
Tobin Ehlisb5fc4fb2015-09-03 09:50:06 -0600253 } else { // Save physical devices
254 for (uint32_t i=0; i < *pPhysicalDeviceCount; i++) {
Tobin Ehlis9da65002015-09-24 15:25:16 -0600255 layer_data *phy_dev_data = get_my_data_ptr(get_dispatch_key(pPhysicalDevices[i]), layer_data_map);
256 phy_dev_data->physicalDeviceState = unique_ptr<PHYSICAL_DEVICE_STATE>(new PHYSICAL_DEVICE_STATE());
257 // Init actual features for each physical device
Tobin Ehlis1cb7f572015-10-06 09:09:24 -0600258 my_data->instance_dispatch_table->GetPhysicalDeviceFeatures(pPhysicalDevices[i], &(phy_dev_data->actualPhysicalDeviceFeatures));
Tony Barbour1088c402015-10-09 11:50:32 -0600259 my_data->instance_dispatch_table->GetPhysicalDeviceProperties(pPhysicalDevices[i], &(phy_dev_data->physicalDeviceProperties));
Tobin Ehlisb5fc4fb2015-09-03 09:50:06 -0600260 }
261 }
262 return result;
263 } else {
Courtney Goeltzenleuchter7415d5a2015-12-09 15:48:16 -0700264 log_msg(my_data->report_data, VK_DEBUG_REPORT_ERROR_BIT_EXT, VK_DEBUG_REPORT_OBJECT_TYPE_INSTANCE_EXT, 0, 0, DEVLIMITS_INVALID_INSTANCE, "DL",
Michael Lentine010f4692015-11-03 16:19:46 -0800265 "Invalid instance (%#" PRIxLEAST64 ") passed into vkEnumeratePhysicalDevices().", (uint64_t)instance);
Tobin Ehlisb5fc4fb2015-09-03 09:50:06 -0600266 }
Courtney Goeltzenleuchter52fee652015-12-10 16:41:22 -0700267 return VK_ERROR_VALIDATION_FAILED_EXT;
Tobin Ehlisb5fc4fb2015-09-03 09:50:06 -0600268}
269
Chia-I Wu9ab61502015-11-06 06:42:02 +0800270VK_LAYER_EXPORT VKAPI_ATTR void VKAPI_CALL vkGetPhysicalDeviceFeatures(VkPhysicalDevice physicalDevice, VkPhysicalDeviceFeatures* pFeatures)
Tobin Ehlisb5fc4fb2015-09-03 09:50:06 -0600271{
Tobin Ehlis9da65002015-09-24 15:25:16 -0600272 layer_data *phy_dev_data = get_my_data_ptr(get_dispatch_key(physicalDevice), layer_data_map);
273 phy_dev_data->physicalDeviceState->vkGetPhysicalDeviceFeaturesState = QUERY_DETAILS;
Courtney Goeltzenleuchter06d89472015-10-20 16:40:38 -0600274 phy_dev_data->instance_dispatch_table->GetPhysicalDeviceFeatures(physicalDevice, pFeatures);
Tobin Ehlisb5fc4fb2015-09-03 09:50:06 -0600275}
276
Chia-I Wu9ab61502015-11-06 06:42:02 +0800277VK_LAYER_EXPORT VKAPI_ATTR void VKAPI_CALL vkGetPhysicalDeviceFormatProperties(VkPhysicalDevice physicalDevice, VkFormat format, VkFormatProperties* pFormatProperties)
Tobin Ehlisb5fc4fb2015-09-03 09:50:06 -0600278{
Courtney Goeltzenleuchter06d89472015-10-20 16:40:38 -0600279 get_my_data_ptr(get_dispatch_key(physicalDevice), layer_data_map)->instance_dispatch_table->GetPhysicalDeviceFormatProperties(
280 physicalDevice, format, pFormatProperties);
Tobin Ehlisb5fc4fb2015-09-03 09:50:06 -0600281}
282
Chia-I Wu9ab61502015-11-06 06:42:02 +0800283VK_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 -0600284{
Chia-I Wu17241042015-10-31 00:31:16 +0800285 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 -0600286}
287
Chia-I Wu9ab61502015-11-06 06:42:02 +0800288VK_LAYER_EXPORT VKAPI_ATTR void VKAPI_CALL vkGetPhysicalDeviceProperties(VkPhysicalDevice physicalDevice, VkPhysicalDeviceProperties* pProperties)
Tobin Ehlisb5fc4fb2015-09-03 09:50:06 -0600289{
Tobin Ehlis1cb7f572015-10-06 09:09:24 -0600290 layer_data *phy_dev_data = get_my_data_ptr(get_dispatch_key(physicalDevice), layer_data_map);
Courtney Goeltzenleuchter06d89472015-10-20 16:40:38 -0600291 phy_dev_data->instance_dispatch_table->GetPhysicalDeviceProperties(physicalDevice, pProperties);
Tobin Ehlisb5fc4fb2015-09-03 09:50:06 -0600292}
293
Chia-I Wu9ab61502015-11-06 06:42:02 +0800294VK_LAYER_EXPORT VKAPI_ATTR void VKAPI_CALL vkGetPhysicalDeviceQueueFamilyProperties(VkPhysicalDevice physicalDevice, uint32_t* pCount, VkQueueFamilyProperties* pQueueFamilyProperties)
Tobin Ehlisb5fc4fb2015-09-03 09:50:06 -0600295{
Tobin Ehlis9da65002015-09-24 15:25:16 -0600296 VkBool32 skipCall = VK_FALSE;
297 layer_data *phy_dev_data = get_my_data_ptr(get_dispatch_key(physicalDevice), layer_data_map);
298 if (phy_dev_data->physicalDeviceState) {
Tobin Ehlisb5fc4fb2015-09-03 09:50:06 -0600299 if (NULL == pQueueFamilyProperties) {
Tobin Ehlis9da65002015-09-24 15:25:16 -0600300 phy_dev_data->physicalDeviceState->vkGetPhysicalDeviceQueueFamilyPropertiesState = QUERY_COUNT;
Tobin Ehlisb5fc4fb2015-09-03 09:50:06 -0600301 } else {
302 // 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 -0600303 if (UNCALLED == phy_dev_data->physicalDeviceState->vkGetPhysicalDeviceQueueFamilyPropertiesState) {
Courtney Goeltzenleuchter7415d5a2015-12-09 15:48:16 -0700304 skipCall |= log_msg(phy_dev_data->report_data, VK_DEBUG_REPORT_ERROR_BIT_EXT, VK_DEBUG_REPORT_OBJECT_TYPE_PHYSICAL_DEVICE_EXT, 0, 0, DEVLIMITS_MUST_QUERY_COUNT, "DL",
Tobin Ehlisb5fc4fb2015-09-03 09:50:06 -0600305 "Invalid call sequence to vkGetPhysicalDeviceQueueFamilyProperties() w/ non-NULL pQueueFamilyProperties. You should first call vkGetPhysicalDeviceQueueFamilyProperties() w/ NULL pQueueFamilyProperties to query pCount.");
306 }
Tobin Ehlis9da65002015-09-24 15:25:16 -0600307 // Then verify that pCount that is passed in on second call matches what was returned
308 if (phy_dev_data->physicalDeviceState->queueFamilyPropertiesCount != *pCount) {
Courtney Goeltzenleuchter7415d5a2015-12-09 15:48:16 -0700309
310 // TODO: this is not a requirement of the Valid Usage section for vkGetPhysicalDeviceQueueFamilyProperties, so provide as warning
311 skipCall |= log_msg(phy_dev_data->report_data, VK_DEBUG_REPORT_WARN_BIT_EXT, VK_DEBUG_REPORT_OBJECT_TYPE_PHYSICAL_DEVICE_EXT, 0, 0, DEVLIMITS_COUNT_MISMATCH, "DL",
Tobin Ehlis9da65002015-09-24 15:25:16 -0600312 "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 -0600313 }
Tobin Ehlis9da65002015-09-24 15:25:16 -0600314 phy_dev_data->physicalDeviceState->vkGetPhysicalDeviceQueueFamilyPropertiesState = QUERY_DETAILS;
Tobin Ehlisb5fc4fb2015-09-03 09:50:06 -0600315 }
Tobin Ehlis9da65002015-09-24 15:25:16 -0600316 if (skipCall)
Courtney Goeltzenleuchter06d89472015-10-20 16:40:38 -0600317 return;
318 phy_dev_data->instance_dispatch_table->GetPhysicalDeviceQueueFamilyProperties(physicalDevice, pCount, pQueueFamilyProperties);
Tobin Ehlisb5fc4fb2015-09-03 09:50:06 -0600319 if (NULL == pQueueFamilyProperties) {
Tobin Ehlis9da65002015-09-24 15:25:16 -0600320 phy_dev_data->physicalDeviceState->queueFamilyPropertiesCount = *pCount;
Tobin Ehlisb5fc4fb2015-09-03 09:50:06 -0600321 } else { // Save queue family properties
Tobin Ehlis9da65002015-09-24 15:25:16 -0600322 phy_dev_data->queueFamilyProperties.reserve(*pCount);
Tobin Ehlisb5fc4fb2015-09-03 09:50:06 -0600323 for (uint32_t i=0; i < *pCount; i++) {
Tobin Ehlis9da65002015-09-24 15:25:16 -0600324 phy_dev_data->queueFamilyProperties.emplace_back(new VkQueueFamilyProperties(pQueueFamilyProperties[i]));
Tobin Ehlisb5fc4fb2015-09-03 09:50:06 -0600325 }
326 }
Courtney Goeltzenleuchter06d89472015-10-20 16:40:38 -0600327 return;
Tobin Ehlisb5fc4fb2015-09-03 09:50:06 -0600328 } else {
Courtney Goeltzenleuchter7415d5a2015-12-09 15:48:16 -0700329 log_msg(phy_dev_data->report_data, VK_DEBUG_REPORT_ERROR_BIT_EXT, VK_DEBUG_REPORT_OBJECT_TYPE_PHYSICAL_DEVICE_EXT, 0, 0, DEVLIMITS_INVALID_PHYSICAL_DEVICE, "DL",
Michael Lentine010f4692015-11-03 16:19:46 -0800330 "Invalid physicalDevice (%#" PRIxLEAST64 ") passed into vkGetPhysicalDeviceQueueFamilyProperties().", (uint64_t)physicalDevice);
Tobin Ehlisb5fc4fb2015-09-03 09:50:06 -0600331 }
332}
333
Chia-I Wu9ab61502015-11-06 06:42:02 +0800334VK_LAYER_EXPORT VKAPI_ATTR void VKAPI_CALL vkGetPhysicalDeviceMemoryProperties(VkPhysicalDevice physicalDevice, VkPhysicalDeviceMemoryProperties* pMemoryProperties)
Tobin Ehlisb5fc4fb2015-09-03 09:50:06 -0600335{
Courtney Goeltzenleuchter06d89472015-10-20 16:40:38 -0600336 get_my_data_ptr(get_dispatch_key(physicalDevice), layer_data_map)->instance_dispatch_table->GetPhysicalDeviceMemoryProperties(physicalDevice, pMemoryProperties);
Tobin Ehlisb5fc4fb2015-09-03 09:50:06 -0600337}
338
Chia-I Wu9ab61502015-11-06 06:42:02 +0800339VK_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 -0600340{
Courtney Goeltzenleuchter06d89472015-10-20 16:40:38 -0600341 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 -0600342}
343
Chia-I Wu9ab61502015-11-06 06:42:02 +0800344VK_LAYER_EXPORT VKAPI_ATTR void VKAPI_CALL vkCmdSetViewport(
Chia-I Wu3432a0c2015-10-27 18:04:07 +0800345 VkCommandBuffer commandBuffer,
Courtney Goeltzenleuchter078f8172015-09-21 11:44:06 -0600346 uint32_t viewportCount,
347 const VkViewport* pViewports)
348{
349 VkBool32 skipCall = VK_FALSE;
350 /* TODO: Verify viewportCount < maxViewports from VkPhysicalDeviceLimits */
351 if (VK_FALSE == skipCall) {
Chia-I Wu3432a0c2015-10-27 18:04:07 +0800352 layer_data *my_data = get_my_data_ptr(get_dispatch_key(commandBuffer), layer_data_map);
353 my_data->device_dispatch_table->CmdSetViewport(commandBuffer, viewportCount, pViewports);
Courtney Goeltzenleuchter078f8172015-09-21 11:44:06 -0600354 }
355}
356
Chia-I Wu9ab61502015-11-06 06:42:02 +0800357VK_LAYER_EXPORT VKAPI_ATTR void VKAPI_CALL vkCmdSetScissor(
Chia-I Wu3432a0c2015-10-27 18:04:07 +0800358 VkCommandBuffer commandBuffer,
Courtney Goeltzenleuchter078f8172015-09-21 11:44:06 -0600359 uint32_t scissorCount,
Courtney Goeltzenleuchter49c73082015-09-17 15:06:17 -0600360 const VkRect2D* pScissors)
361{
362 VkBool32 skipCall = VK_FALSE;
Courtney Goeltzenleuchter078f8172015-09-21 11:44:06 -0600363 /* TODO: Verify scissorCount < maxViewports from VkPhysicalDeviceLimits */
364 /* TODO: viewportCount and scissorCount must match at draw time */
Courtney Goeltzenleuchter49c73082015-09-17 15:06:17 -0600365 if (VK_FALSE == skipCall) {
Chia-I Wu3432a0c2015-10-27 18:04:07 +0800366 layer_data *my_data = get_my_data_ptr(get_dispatch_key(commandBuffer), layer_data_map);
367 my_data->device_dispatch_table->CmdSetScissor(commandBuffer, scissorCount, pScissors);
Courtney Goeltzenleuchter49c73082015-09-17 15:06:17 -0600368 }
369}
370
Tobin Ehlisb5fc4fb2015-09-03 09:50:06 -0600371static void createDeviceRegisterExtensions(const VkDeviceCreateInfo* pCreateInfo, VkDevice device)
372{
373 uint32_t i;
Tobin Ehlis1cb7f572015-10-06 09:09:24 -0600374 layer_data *my_data = get_my_data_ptr(get_dispatch_key(device), layer_data_map);
375 my_data->device_extensions.debug_marker_enabled = false;
Tobin Ehlisb5fc4fb2015-09-03 09:50:06 -0600376
Chia-I Wud50a7d72015-10-26 20:48:51 +0800377 for (i = 0; i < pCreateInfo->enabledExtensionNameCount; i++) {
Tobin Ehlisb5fc4fb2015-09-03 09:50:06 -0600378 if (strcmp(pCreateInfo->ppEnabledExtensionNames[i], DEBUG_MARKER_EXTENSION_NAME) == 0) {
379 /* Found a matching extension name, mark it enabled and init dispatch table*/
380 initDebugMarkerTable(device);
Tobin Ehlis1cb7f572015-10-06 09:09:24 -0600381 my_data->device_extensions.debug_marker_enabled = true;
Tobin Ehlisb5fc4fb2015-09-03 09:50:06 -0600382 }
383
384 }
385}
386
Tobin Ehlis9da65002015-09-24 15:25:16 -0600387// Verify that features have been queried and verify that requested features are available
388static VkBool32 validate_features_request(layer_data *phy_dev_data)
389{
390 VkBool32 skipCall = VK_FALSE;
391 // Verify that all of the requested features are available
392 // Get ptrs into actual and requested structs and if requested is 1 but actual is 0, request is invalid
393 VkBool32* actual = (VkBool32*)&(phy_dev_data->actualPhysicalDeviceFeatures);
394 VkBool32* requested = (VkBool32*)&(phy_dev_data->requestedPhysicalDeviceFeatures);
395 // TODO : This is a nice, compact way to loop through struct, but a bad way to report issues
396 // Need to provide the struct member name with the issue. To do that seems like we'll
397 // have to loop through each struct member which should be done w/ codegen to keep in synch.
398 uint32_t errors = 0;
399 uint32_t totalBools = sizeof(VkPhysicalDeviceFeatures)/sizeof(VkBool32);
400 for (uint32_t i = 0; i < totalBools; i++) {
401 if (requested[i] > actual[i]) {
Courtney Goeltzenleuchter7415d5a2015-12-09 15:48:16 -0700402 skipCall |= log_msg(phy_dev_data->report_data, VK_DEBUG_REPORT_ERROR_BIT_EXT, VK_DEBUG_REPORT_OBJECT_TYPE_PHYSICAL_DEVICE_EXT, 0, 0, DEVLIMITS_INVALID_FEATURE_REQUESTED, "DL",
Tobin Ehlis9da65002015-09-24 15:25:16 -0600403 "While calling vkCreateDevice(), requesting feature #%u in VkPhysicalDeviceFeatures struct, which is not available on this device.", i);
404 errors++;
405 }
406 }
407 if (errors && (UNCALLED == phy_dev_data->physicalDeviceState->vkGetPhysicalDeviceFeaturesState)) {
408 // If user didn't request features, notify them that they should
Courtney Goeltzenleuchter7415d5a2015-12-09 15:48:16 -0700409 // TODO: Verify this against the spec. I believe this is an invalid use of the API and should return an error
410 skipCall |= log_msg(phy_dev_data->report_data, VK_DEBUG_REPORT_WARN_BIT_EXT, VK_DEBUG_REPORT_OBJECT_TYPE_PHYSICAL_DEVICE_EXT, 0, 0, DEVLIMITS_INVALID_FEATURE_REQUESTED, "DL",
Tobin Ehlis9da65002015-09-24 15:25:16 -0600411 "You requested features that are unavailable on this device. You should first query feature availability by calling vkGetPhysicalDeviceFeatures().");
412 }
Tobin Ehlis72b27cc2015-09-29 11:22:37 -0600413 return skipCall;
Tobin Ehlis9da65002015-09-24 15:25:16 -0600414}
415
Chia-I Wu9ab61502015-11-06 06:42:02 +0800416VK_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 -0600417{
Tobin Ehlis9da65002015-09-24 15:25:16 -0600418 VkBool32 skipCall = VK_FALSE;
419 layer_data *phy_dev_data = get_my_data_ptr(get_dispatch_key(gpu), layer_data_map);
Tobin Ehlis54a6c182015-09-22 14:00:58 -0600420 // First check is app has actually requested queueFamilyProperties
Tobin Ehlis9da65002015-09-24 15:25:16 -0600421 if (!phy_dev_data->physicalDeviceState) {
Courtney Goeltzenleuchter7415d5a2015-12-09 15:48:16 -0700422 skipCall |= log_msg(phy_dev_data->report_data, VK_DEBUG_REPORT_ERROR_BIT_EXT, VK_DEBUG_REPORT_OBJECT_TYPE_PHYSICAL_DEVICE_EXT, 0, 0, DEVLIMITS_MUST_QUERY_COUNT, "DL",
Tobin Ehlis54a6c182015-09-22 14:00:58 -0600423 "Invalid call to vkCreateDevice() w/o first calling vkEnumeratePhysicalDevices().");
Tobin Ehlis9da65002015-09-24 15:25:16 -0600424 } else if (QUERY_DETAILS != phy_dev_data->physicalDeviceState->vkGetPhysicalDeviceQueueFamilyPropertiesState) {
Courtney Goeltzenleuchter7415d5a2015-12-09 15:48:16 -0700425 // TODO: This is not called out as an invalid use in the spec so make more informative recommendation.
426 skipCall |= log_msg(phy_dev_data->report_data, VK_DEBUG_REPORT_WARN_BIT_EXT, VK_DEBUG_REPORT_OBJECT_TYPE_PHYSICAL_DEVICE_EXT, 0, 0, DEVLIMITS_INVALID_QUEUE_CREATE_REQUEST, "DL",
427 "Call to vkCreateDevice() w/o first calling vkGetPhysicalDeviceQueueFamilyProperties().");
Tobin Ehlis54a6c182015-09-22 14:00:58 -0600428 } else {
429 // Check that the requested queue properties are valid
Chia-I Wu02124482015-11-06 06:42:02 +0800430 for (uint32_t i=0; i<pCreateInfo->queueCreateInfoCount; i++) {
431 uint32_t requestedIndex = pCreateInfo->pQueueCreateInfos[i].queueFamilyIndex;
Tobin Ehlis9da65002015-09-24 15:25:16 -0600432 if (phy_dev_data->queueFamilyProperties.size() <= requestedIndex) { // requested index is out of bounds for this physical device
Courtney Goeltzenleuchter7415d5a2015-12-09 15:48:16 -0700433 skipCall |= log_msg(phy_dev_data->report_data, VK_DEBUG_REPORT_ERROR_BIT_EXT, VK_DEBUG_REPORT_OBJECT_TYPE_PHYSICAL_DEVICE_EXT, 0, 0, DEVLIMITS_INVALID_QUEUE_CREATE_REQUEST, "DL",
Tobin Ehlis54a6c182015-09-22 14:00:58 -0600434 "Invalid queue create request in vkCreateDevice(). Invalid queueFamilyIndex %u requested.", requestedIndex);
Chia-I Wu02124482015-11-06 06:42:02 +0800435 } else if (pCreateInfo->pQueueCreateInfos[i].queueCount > phy_dev_data->queueFamilyProperties[requestedIndex]->queueCount) {
Courtney Goeltzenleuchter7415d5a2015-12-09 15:48:16 -0700436 skipCall |= log_msg(phy_dev_data->report_data, VK_DEBUG_REPORT_ERROR_BIT_EXT, VK_DEBUG_REPORT_OBJECT_TYPE_PHYSICAL_DEVICE_EXT, 0, 0, DEVLIMITS_INVALID_QUEUE_CREATE_REQUEST, "DL",
Chia-I Wu02124482015-11-06 06:42:02 +0800437 "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 -0600438 }
Tobin Ehlisb5fc4fb2015-09-03 09:50:06 -0600439 }
440 }
Tobin Ehlis9da65002015-09-24 15:25:16 -0600441 // Check that any requested features are available
442 if (pCreateInfo->pEnabledFeatures) {
443 phy_dev_data->requestedPhysicalDeviceFeatures = *(pCreateInfo->pEnabledFeatures);
444 skipCall |= validate_features_request(phy_dev_data);
445 }
446 if (skipCall)
Courtney Goeltzenleuchter52fee652015-12-10 16:41:22 -0700447 return VK_ERROR_VALIDATION_FAILED_EXT;
Tobin Ehlis9da65002015-09-24 15:25:16 -0600448
Tobin Ehlis1cb7f572015-10-06 09:09:24 -0600449 layer_data *my_device_data = get_my_data_ptr(get_dispatch_key(*pDevice), layer_data_map);
Chia-I Wuf7458c52015-10-26 21:10:41 +0800450 VkResult result = my_device_data->device_dispatch_table->CreateDevice(gpu, pCreateInfo, pAllocator, pDevice);
Tobin Ehlisb5fc4fb2015-09-03 09:50:06 -0600451 if (result == VK_SUCCESS) {
Tobin Ehlis9da65002015-09-24 15:25:16 -0600452 my_device_data->report_data = layer_debug_report_create_device(phy_dev_data->report_data, *pDevice);
Tobin Ehlisb5fc4fb2015-09-03 09:50:06 -0600453 createDeviceRegisterExtensions(pCreateInfo, *pDevice);
Tobin Ehlis1cb7f572015-10-06 09:09:24 -0600454 my_device_data->physicalDevice = gpu;
Tobin Ehlisb5fc4fb2015-09-03 09:50:06 -0600455 }
456 return result;
457}
458
Chia-I Wu9ab61502015-11-06 06:42:02 +0800459VK_LAYER_EXPORT VKAPI_ATTR void VKAPI_CALL vkDestroyDevice(VkDevice device, const VkAllocationCallbacks* pAllocator)
Tobin Ehlisb5fc4fb2015-09-03 09:50:06 -0600460{
461 // Free device lifetime allocations
462 dispatch_key key = get_dispatch_key(device);
Tobin Ehlis1cb7f572015-10-06 09:09:24 -0600463 layer_data *my_device_data = get_my_data_ptr(key, layer_data_map);
Chia-I Wuf7458c52015-10-26 21:10:41 +0800464 my_device_data->device_dispatch_table->DestroyDevice(device, pAllocator);
Tobin Ehlis1cb7f572015-10-06 09:09:24 -0600465 tableDebugMarkerMap.erase(key);
466 delete my_device_data->device_dispatch_table;
467 layer_data_map.erase(key);
Tobin Ehlisb5fc4fb2015-09-03 09:50:06 -0600468}
469
Chia-I Wu9ab61502015-11-06 06:42:02 +0800470VK_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 -0600471{
Tobin Ehlis9da65002015-09-24 15:25:16 -0600472 // TODO : Verify that requested QueueFamilyIndex for this pool exists
Chia-I Wu3432a0c2015-10-27 18:04:07 +0800473 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 -0600474 return result;
Tobin Ehlisb5fc4fb2015-09-03 09:50:06 -0600475}
476
Chia-I Wu9ab61502015-11-06 06:42:02 +0800477VK_LAYER_EXPORT VKAPI_ATTR void VKAPI_CALL vkDestroyCommandPool(VkDevice device, VkCommandPool commandPool, const VkAllocationCallbacks* pAllocator)
Tobin Ehlisb5fc4fb2015-09-03 09:50:06 -0600478{
Chia-I Wu3432a0c2015-10-27 18:04:07 +0800479 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 -0600480}
481
Chia-I Wu9ab61502015-11-06 06:42:02 +0800482VK_LAYER_EXPORT VKAPI_ATTR VkResult VKAPI_CALL vkResetCommandPool(VkDevice device, VkCommandPool commandPool, VkCommandPoolResetFlags flags)
Tobin Ehlisb5fc4fb2015-09-03 09:50:06 -0600483{
Chia-I Wu3432a0c2015-10-27 18:04:07 +0800484 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 -0600485 return result;
Tobin Ehlisb5fc4fb2015-09-03 09:50:06 -0600486}
487
Chia-I Wu9ab61502015-11-06 06:42:02 +0800488VK_LAYER_EXPORT VKAPI_ATTR VkResult VKAPI_CALL vkAllocateCommandBuffers(VkDevice device, const VkCommandBufferAllocateInfo* pCreateInfo, VkCommandBuffer* pCommandBuffer)
Tobin Ehlisb5fc4fb2015-09-03 09:50:06 -0600489{
Chia-I Wu3432a0c2015-10-27 18:04:07 +0800490 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 -0600491 return result;
492}
Mark Lobodzinskifbb130e2015-10-06 11:59:54 -0600493
Chia-I Wu9ab61502015-11-06 06:42:02 +0800494VK_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 -0600495{
Chia-I Wu3432a0c2015-10-27 18:04:07 +0800496 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 -0600497}
498
Chia-I Wu9ab61502015-11-06 06:42:02 +0800499VK_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 -0600500{
Tobin Ehlis9da65002015-09-24 15:25:16 -0600501 VkBool32 skipCall = VK_FALSE;
Tobin Ehlis1cb7f572015-10-06 09:09:24 -0600502 layer_data *dev_data = get_my_data_ptr(get_dispatch_key(device), layer_data_map);
503 VkPhysicalDevice gpu = dev_data->physicalDevice;
Tobin Ehlis9da65002015-09-24 15:25:16 -0600504 layer_data *phy_dev_data = get_my_data_ptr(get_dispatch_key(gpu), layer_data_map);
505 if (queueFamilyIndex >= phy_dev_data->queueFamilyProperties.size()) { // requested index is out of bounds for this physical device
Courtney Goeltzenleuchter7415d5a2015-12-09 15:48:16 -0700506 skipCall |= log_msg(phy_dev_data->report_data, VK_DEBUG_REPORT_ERROR_BIT_EXT, VK_DEBUG_REPORT_OBJECT_TYPE_PHYSICAL_DEVICE_EXT, 0, 0, DEVLIMITS_INVALID_QUEUE_CREATE_REQUEST, "DL",
Tobin Ehlisb5fc4fb2015-09-03 09:50:06 -0600507 "Invalid queueFamilyIndex %u requested in vkGetDeviceQueue().", queueFamilyIndex);
Tobin Ehlis9da65002015-09-24 15:25:16 -0600508 } else if (queueIndex >= phy_dev_data->queueFamilyProperties[queueFamilyIndex]->queueCount) {
Courtney Goeltzenleuchter7415d5a2015-12-09 15:48:16 -0700509 skipCall |= log_msg(phy_dev_data->report_data, VK_DEBUG_REPORT_ERROR_BIT_EXT, VK_DEBUG_REPORT_OBJECT_TYPE_PHYSICAL_DEVICE_EXT, 0, 0, DEVLIMITS_INVALID_QUEUE_CREATE_REQUEST, "DL",
Tobin Ehlis9da65002015-09-24 15:25:16 -0600510 "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 -0600511 }
Tobin Ehlis9da65002015-09-24 15:25:16 -0600512 if (skipCall)
Courtney Goeltzenleuchter06d89472015-10-20 16:40:38 -0600513 return;
514 dev_data->device_dispatch_table->GetDeviceQueue(device, queueFamilyIndex, queueIndex, pQueue);
Tobin Ehlisb5fc4fb2015-09-03 09:50:06 -0600515}
516
Chia-I Wu9ab61502015-11-06 06:42:02 +0800517VK_LAYER_EXPORT VKAPI_ATTR void VKAPI_CALL vkCmdUpdateBuffer(
Chia-I Wu3432a0c2015-10-27 18:04:07 +0800518 VkCommandBuffer commandBuffer,
519 VkBuffer dstBuffer,
520 VkDeviceSize dstOffset,
Mike Stroyana3082432015-09-25 13:39:21 -0600521 VkDeviceSize dataSize,
522 const uint32_t* pData)
523{
Chia-I Wu3432a0c2015-10-27 18:04:07 +0800524 layer_data *dev_data = get_my_data_ptr(get_dispatch_key(commandBuffer), layer_data_map);
Mike Stroyana3082432015-09-25 13:39:21 -0600525
Chia-I Wu3432a0c2015-10-27 18:04:07 +0800526 // dstOffset is the byte offset into the buffer to start updating and must be a multiple of 4.
527 if (dstOffset & 3) {
528 layer_data *my_data = get_my_data_ptr(get_dispatch_key(commandBuffer), layer_data_map);
Courtney Goeltzenleuchter7415d5a2015-12-09 15:48:16 -0700529 if (log_msg(my_data->report_data, VK_DEBUG_REPORT_ERROR_BIT_EXT, (VkDebugReportObjectTypeEXT)0, 0, 0, 1, "DL",
Chia-I Wu3432a0c2015-10-27 18:04:07 +0800530 "vkCmdUpdateBuffer parameter, VkDeviceSize dstOffset, is not a multiple of 4")) {
Mike Stroyana3082432015-09-25 13:39:21 -0600531 return;
532 }
533 }
534
535 // dataSize is the number of bytes to update, which must be a multiple of 4.
536 if (dataSize & 3) {
Chia-I Wu3432a0c2015-10-27 18:04:07 +0800537 layer_data *my_data = get_my_data_ptr(get_dispatch_key(commandBuffer), layer_data_map);
Courtney Goeltzenleuchter7415d5a2015-12-09 15:48:16 -0700538 if (log_msg(my_data->report_data, VK_DEBUG_REPORT_ERROR_BIT_EXT, (VkDebugReportObjectTypeEXT)0, 0, 0, 1, "DL",
Mike Stroyana3082432015-09-25 13:39:21 -0600539 "vkCmdUpdateBuffer parameter, VkDeviceSize dataSize, is not a multiple of 4")) {
540 return;
541 }
542 }
543
Chia-I Wu3432a0c2015-10-27 18:04:07 +0800544 dev_data->device_dispatch_table->CmdUpdateBuffer(commandBuffer, dstBuffer, dstOffset, dataSize, pData);
Mike Stroyana3082432015-09-25 13:39:21 -0600545}
546
Chia-I Wu9ab61502015-11-06 06:42:02 +0800547VK_LAYER_EXPORT VKAPI_ATTR void VKAPI_CALL vkCmdFillBuffer(
Chia-I Wu3432a0c2015-10-27 18:04:07 +0800548 VkCommandBuffer commandBuffer,
549 VkBuffer dstBuffer,
550 VkDeviceSize dstOffset,
Chia-I Wu2bfb33c2015-10-26 17:24:52 +0800551 VkDeviceSize size,
Mike Stroyana3082432015-09-25 13:39:21 -0600552 uint32_t data)
553{
Chia-I Wu3432a0c2015-10-27 18:04:07 +0800554 layer_data *dev_data = get_my_data_ptr(get_dispatch_key(commandBuffer), layer_data_map);
Mike Stroyana3082432015-09-25 13:39:21 -0600555
Chia-I Wu3432a0c2015-10-27 18:04:07 +0800556 // dstOffset is the byte offset into the buffer to start filling and must be a multiple of 4.
557 if (dstOffset & 3) {
558 layer_data *my_data = get_my_data_ptr(get_dispatch_key(commandBuffer), layer_data_map);
Courtney Goeltzenleuchter7415d5a2015-12-09 15:48:16 -0700559 if (log_msg(my_data->report_data, VK_DEBUG_REPORT_ERROR_BIT_EXT, (VkDebugReportObjectTypeEXT)0, 0, 0, 1, "DL",
Chia-I Wu3432a0c2015-10-27 18:04:07 +0800560 "vkCmdFillBuffer parameter, VkDeviceSize dstOffset, is not a multiple of 4")) {
Mike Stroyana3082432015-09-25 13:39:21 -0600561 return;
562 }
563 }
564
Chia-I Wu2bfb33c2015-10-26 17:24:52 +0800565 // size is the number of bytes to fill, which must be a multiple of 4.
566 if (size & 3) {
Chia-I Wu3432a0c2015-10-27 18:04:07 +0800567 layer_data *my_data = get_my_data_ptr(get_dispatch_key(commandBuffer), layer_data_map);
Courtney Goeltzenleuchter7415d5a2015-12-09 15:48:16 -0700568 if (log_msg(my_data->report_data, VK_DEBUG_REPORT_ERROR_BIT_EXT, (VkDebugReportObjectTypeEXT)0, 0, 0, 1, "DL",
Chia-I Wu2bfb33c2015-10-26 17:24:52 +0800569 "vkCmdFillBuffer parameter, VkDeviceSize size, is not a multiple of 4")) {
Mike Stroyana3082432015-09-25 13:39:21 -0600570 return;
571 }
572 }
573
Chia-I Wu3432a0c2015-10-27 18:04:07 +0800574 dev_data->device_dispatch_table->CmdFillBuffer(commandBuffer, dstBuffer, dstOffset, size, data);
Mike Stroyana3082432015-09-25 13:39:21 -0600575}
576
Courtney Goeltzenleuchter7415d5a2015-12-09 15:48:16 -0700577VK_LAYER_EXPORT VKAPI_ATTR VkResult VKAPI_CALL vkCreateDebugReportCallbackEXT(
578 VkInstance instance,
579 const VkDebugReportCallbackCreateInfoEXT* pCreateInfo,
580 const VkAllocationCallbacks* pAllocator,
581 VkDebugReportCallbackEXT* pMsgCallback)
Tobin Ehlisb5fc4fb2015-09-03 09:50:06 -0600582{
Tobin Ehlis1cb7f572015-10-06 09:09:24 -0600583 layer_data *my_data = get_my_data_ptr(get_dispatch_key(instance), layer_data_map);
Courtney Goeltzenleuchter7415d5a2015-12-09 15:48:16 -0700584 VkResult res = my_data->instance_dispatch_table->CreateDebugReportCallbackEXT(instance, pCreateInfo, pAllocator, pMsgCallback);
Tobin Ehlisb5fc4fb2015-09-03 09:50:06 -0600585 if (VK_SUCCESS == res) {
Courtney Goeltzenleuchter05854bf2015-11-30 12:13:14 -0700586 res = layer_create_msg_callback(my_data->report_data, pCreateInfo, pAllocator, pMsgCallback);
Tobin Ehlisb5fc4fb2015-09-03 09:50:06 -0600587 }
588 return res;
589}
590
Courtney Goeltzenleuchter7415d5a2015-12-09 15:48:16 -0700591VK_LAYER_EXPORT VKAPI_ATTR void VKAPI_CALL vkDestroyDebugReportCallbackEXT(
Courtney Goeltzenleuchter05854bf2015-11-30 12:13:14 -0700592 VkInstance instance,
Courtney Goeltzenleuchter7415d5a2015-12-09 15:48:16 -0700593 VkDebugReportCallbackEXT msgCallback,
Courtney Goeltzenleuchter05854bf2015-11-30 12:13:14 -0700594 const VkAllocationCallbacks* pAllocator)
Tobin Ehlisb5fc4fb2015-09-03 09:50:06 -0600595{
Tobin Ehlisb5fc4fb2015-09-03 09:50:06 -0600596 layer_data *my_data = get_my_data_ptr(get_dispatch_key(instance), layer_data_map);
Courtney Goeltzenleuchter7415d5a2015-12-09 15:48:16 -0700597 my_data->instance_dispatch_table->DestroyDebugReportCallbackEXT(instance, msgCallback, pAllocator);
Courtney Goeltzenleuchter05854bf2015-11-30 12:13:14 -0700598 layer_destroy_msg_callback(my_data->report_data, msgCallback, pAllocator);
Tobin Ehlisb5fc4fb2015-09-03 09:50:06 -0600599}
600
Courtney Goeltzenleuchter7415d5a2015-12-09 15:48:16 -0700601VK_LAYER_EXPORT VKAPI_ATTR void VKAPI_CALL vkDebugReportMessageEXT(
Courtney Goeltzenleuchterf0de7242015-12-01 14:10:55 -0700602 VkInstance instance,
Courtney Goeltzenleuchter7415d5a2015-12-09 15:48:16 -0700603 VkDebugReportFlagsEXT flags,
604 VkDebugReportObjectTypeEXT objType,
Courtney Goeltzenleuchterf0de7242015-12-01 14:10:55 -0700605 uint64_t object,
606 size_t location,
607 int32_t msgCode,
608 const char* pLayerPrefix,
609 const char* pMsg)
610{
611 layer_data *my_data = get_my_data_ptr(get_dispatch_key(instance), layer_data_map);
Courtney Goeltzenleuchter7415d5a2015-12-09 15:48:16 -0700612 my_data->instance_dispatch_table->DebugReportMessageEXT(instance, flags, objType, object, location, msgCode, pLayerPrefix, pMsg);
Courtney Goeltzenleuchterf0de7242015-12-01 14:10:55 -0700613}
614
Chia-I Wu9ab61502015-11-06 06:42:02 +0800615VK_LAYER_EXPORT VKAPI_ATTR PFN_vkVoidFunction VKAPI_CALL vkGetDeviceProcAddr(VkDevice dev, const char* funcName)
Tobin Ehlisb5fc4fb2015-09-03 09:50:06 -0600616{
617 if (dev == NULL)
618 return NULL;
619
Tobin Ehlis1cb7f572015-10-06 09:09:24 -0600620 layer_data *my_data;
Tobin Ehlisb5fc4fb2015-09-03 09:50:06 -0600621 /* loader uses this to force layer initialization; device object is wrapped */
622 if (!strcmp(funcName, "vkGetDeviceProcAddr")) {
Tobin Ehlis1cb7f572015-10-06 09:09:24 -0600623 VkBaseLayerObject* wrapped_dev = (VkBaseLayerObject*) dev;
624 my_data = get_my_data_ptr(get_dispatch_key(wrapped_dev->baseObject), layer_data_map);
625 my_data->device_dispatch_table = new VkLayerDispatchTable;
626 layer_initialize_dispatch_table(my_data->device_dispatch_table, wrapped_dev);
Tobin Ehlisb5fc4fb2015-09-03 09:50:06 -0600627 return (PFN_vkVoidFunction) vkGetDeviceProcAddr;
628 }
Tobin Ehlis1cb7f572015-10-06 09:09:24 -0600629 my_data = get_my_data_ptr(get_dispatch_key(dev), layer_data_map);
Tobin Ehlisb5fc4fb2015-09-03 09:50:06 -0600630 if (!strcmp(funcName, "vkCreateDevice"))
631 return (PFN_vkVoidFunction) vkCreateDevice;
632 if (!strcmp(funcName, "vkDestroyDevice"))
633 return (PFN_vkVoidFunction) vkDestroyDevice;
Tobin Ehlisb5fc4fb2015-09-03 09:50:06 -0600634 if (!strcmp(funcName, "vkGetDeviceQueue"))
635 return (PFN_vkVoidFunction) vkGetDeviceQueue;
Tobin Ehlis9da65002015-09-24 15:25:16 -0600636 if (!strcmp(funcName, "CreateCommandPool"))
637 return (PFN_vkVoidFunction) vkCreateCommandPool;
638 if (!strcmp(funcName, "DestroyCommandPool"))
639 return (PFN_vkVoidFunction) vkDestroyCommandPool;
640 if (!strcmp(funcName, "ResetCommandPool"))
641 return (PFN_vkVoidFunction) vkResetCommandPool;
Chia-I Wu3432a0c2015-10-27 18:04:07 +0800642 if (!strcmp(funcName, "vkAllocateCommandBuffers"))
643 return (PFN_vkVoidFunction) vkAllocateCommandBuffers;
Courtney Goeltzenleuchterbee18a92015-10-23 14:21:05 -0600644 if (!strcmp(funcName, "vkFreeCommandBuffers"))
645 return (PFN_vkVoidFunction) vkFreeCommandBuffers;
Mike Stroyana3082432015-09-25 13:39:21 -0600646 if (!strcmp(funcName, "vkCmdUpdateBuffer"))
647 return (PFN_vkVoidFunction) vkCmdUpdateBuffer;
648 if (!strcmp(funcName, "vkCmdFillBuffer"))
649 return (PFN_vkVoidFunction) vkCmdFillBuffer;
Tobin Ehlisb5fc4fb2015-09-03 09:50:06 -0600650
Tobin Ehlis1cb7f572015-10-06 09:09:24 -0600651 VkLayerDispatchTable* pTable = my_data->device_dispatch_table;
Tobin Ehlisb5fc4fb2015-09-03 09:50:06 -0600652 {
653 if (pTable->GetDeviceProcAddr == NULL)
654 return NULL;
655 return pTable->GetDeviceProcAddr(dev, funcName);
656 }
657}
658
Chia-I Wu9ab61502015-11-06 06:42:02 +0800659VK_LAYER_EXPORT VKAPI_ATTR PFN_vkVoidFunction VKAPI_CALL vkGetInstanceProcAddr(VkInstance instance, const char* funcName)
Tobin Ehlisb5fc4fb2015-09-03 09:50:06 -0600660{
661 PFN_vkVoidFunction fptr;
662 if (instance == NULL)
663 return NULL;
664
Tobin Ehlis1cb7f572015-10-06 09:09:24 -0600665 layer_data *my_data;
Tobin Ehlisb5fc4fb2015-09-03 09:50:06 -0600666 /* loader uses this to force layer initialization; instance object is wrapped */
667 if (!strcmp(funcName, "vkGetInstanceProcAddr")) {
Tobin Ehlis1cb7f572015-10-06 09:09:24 -0600668 VkBaseLayerObject* wrapped_inst = (VkBaseLayerObject*) instance;
669 my_data = get_my_data_ptr(get_dispatch_key(wrapped_inst->baseObject), layer_data_map);
670 my_data->instance_dispatch_table = new VkLayerInstanceDispatchTable;
671 layer_init_instance_dispatch_table(my_data->instance_dispatch_table, wrapped_inst);
Tobin Ehlisb5fc4fb2015-09-03 09:50:06 -0600672 return (PFN_vkVoidFunction) vkGetInstanceProcAddr;
673 }
Tobin Ehlis1cb7f572015-10-06 09:09:24 -0600674 my_data = get_my_data_ptr(get_dispatch_key(instance), layer_data_map);
Tobin Ehlisb5fc4fb2015-09-03 09:50:06 -0600675 if (!strcmp(funcName, "vkCreateInstance"))
676 return (PFN_vkVoidFunction) vkCreateInstance;
677 if (!strcmp(funcName, "vkDestroyInstance"))
678 return (PFN_vkVoidFunction) vkDestroyInstance;
679 if (!strcmp(funcName, "vkEnumeratePhysicalDevices"))
680 return (PFN_vkVoidFunction) vkEnumeratePhysicalDevices;
681 if (!strcmp(funcName, "vkGetPhysicalDeviceFeatures"))
682 return (PFN_vkVoidFunction) vkGetPhysicalDeviceFeatures;
683 if (!strcmp(funcName, "vkGetPhysicalDeviceFormatProperties"))
684 return (PFN_vkVoidFunction) vkGetPhysicalDeviceFormatProperties;
685 if (!strcmp(funcName, "vkGetPhysicalDeviceImageFormatProperties"))
686 return (PFN_vkVoidFunction) vkGetPhysicalDeviceImageFormatProperties;
687 if (!strcmp(funcName, "vkGetPhysicalDeviceProperties"))
688 return (PFN_vkVoidFunction) vkGetPhysicalDeviceProperties;
689 if (!strcmp(funcName, "vkGetPhysicalDeviceQueueFamilyProperties"))
690 return (PFN_vkVoidFunction) vkGetPhysicalDeviceQueueFamilyProperties;
691 if (!strcmp(funcName, "vkGetPhysicalDeviceMemoryProperties"))
692 return (PFN_vkVoidFunction) vkGetPhysicalDeviceMemoryProperties;
Tobin Ehlisb5fc4fb2015-09-03 09:50:06 -0600693 if (!strcmp(funcName, "vkGetPhysicalDeviceSparseImageFormatProperties"))
694 return (PFN_vkVoidFunction) vkGetPhysicalDeviceSparseImageFormatProperties;
Tobin Ehlisfd3843f2015-09-29 12:26:00 -0600695 if (!strcmp(funcName, "vkEnumerateInstanceLayerProperties"))
696 return (PFN_vkVoidFunction) vkEnumerateInstanceLayerProperties;
697 if (!strcmp(funcName, "vkEnumerateInstanceExtensionProperties"))
698 return (PFN_vkVoidFunction) vkEnumerateInstanceExtensionProperties;
Tobin Ehlisb5fc4fb2015-09-03 09:50:06 -0600699
Tobin Ehlisb5fc4fb2015-09-03 09:50:06 -0600700 fptr = debug_report_get_instance_proc_addr(my_data->report_data, funcName);
701 if (fptr)
702 return fptr;
703
704 {
Tobin Ehlis1cb7f572015-10-06 09:09:24 -0600705 VkLayerInstanceDispatchTable* pTable = my_data->instance_dispatch_table;
Tobin Ehlisb5fc4fb2015-09-03 09:50:06 -0600706 if (pTable->GetInstanceProcAddr == NULL)
707 return NULL;
708 return pTable->GetInstanceProcAddr(instance, funcName);
709 }
710}