blob: d57ce6db8b4476d269701947324b76aa8d678e07 [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 Goeltzenleuchter107a0d22015-11-25 14:07:05 -070061 std::vector<VkDebugReportCallbackLUNARG> 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 Goeltzenleuchter107a0d22015-11-25 14:07:05 -0700108 VkDebugReportCallbackLUNARG 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 Goeltzenleuchter05854bf2015-11-30 12:13:14 -0700117 VkDebugReportCallbackCreateInfoLUNARG dbgCreateInfo;
118 memset(&dbgCreateInfo, 0, sizeof(dbgCreateInfo));
119 dbgCreateInfo.sType = VK_STRUCTURE_TYPE_DEBUG_REPORT_CALLBACK_CREATE_INFO_LUNARG;
120 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 Goeltzenleuchter05854bf2015-11-30 12:13:14 -0700128 VkDebugReportCallbackCreateInfoLUNARG dbgCreateInfo;
129 memset(&dbgCreateInfo, 0, sizeof(dbgCreateInfo));
130 dbgCreateInfo.sType = VK_STRUCTURE_TYPE_DEBUG_REPORT_CALLBACK_CREATE_INFO_LUNARG;
131 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}
Tobin Ehlisfd3843f2015-09-29 12:26:00 -0600149/* DeviceLimits does not have any global extensions */
Chia-I Wu9ab61502015-11-06 06:42:02 +0800150VK_LAYER_EXPORT VKAPI_ATTR VkResult VKAPI_CALL vkEnumerateInstanceExtensionProperties(
Tobin Ehlisfd3843f2015-09-29 12:26:00 -0600151 const char *pLayerName,
152 uint32_t *pCount,
153 VkExtensionProperties* pProperties)
154{
155 return util_GetExtensionProperties(0, NULL, pCount, pProperties);
156}
157
158static const VkLayerProperties dl_global_layers[] = {
159 {
160 "DeviceLimits",
161 VK_API_VERSION,
162 VK_MAKE_VERSION(0, 1, 0),
163 "Validation layer: Device Limits",
164 }
165};
166
Chia-I Wu9ab61502015-11-06 06:42:02 +0800167VK_LAYER_EXPORT VKAPI_ATTR VkResult VKAPI_CALL vkEnumerateInstanceLayerProperties(
Tobin Ehlisfd3843f2015-09-29 12:26:00 -0600168 uint32_t *pCount,
169 VkLayerProperties* pProperties)
170{
171 return util_GetLayerProperties(ARRAY_SIZE(dl_global_layers),
172 dl_global_layers,
173 pCount, pProperties);
174}
Tobin Ehlisb5fc4fb2015-09-03 09:50:06 -0600175
Chia-I Wu9ab61502015-11-06 06:42:02 +0800176VK_LAYER_EXPORT VKAPI_ATTR VkResult VKAPI_CALL vkCreateInstance(const VkInstanceCreateInfo* pCreateInfo, const VkAllocationCallbacks* pAllocator, VkInstance* pInstance)
Tobin Ehlisb5fc4fb2015-09-03 09:50:06 -0600177{
Tobin Ehlis1cb7f572015-10-06 09:09:24 -0600178 layer_data *my_data = get_my_data_ptr(get_dispatch_key(*pInstance), layer_data_map);
179 VkLayerInstanceDispatchTable* pTable = my_data->instance_dispatch_table;
Chia-I Wuf7458c52015-10-26 21:10:41 +0800180 VkResult result = pTable->CreateInstance(pCreateInfo, pAllocator, pInstance);
Tobin Ehlisb5fc4fb2015-09-03 09:50:06 -0600181
182 if (result == VK_SUCCESS) {
Tobin Ehlisb5fc4fb2015-09-03 09:50:06 -0600183 my_data->report_data = debug_report_create_instance(
184 pTable,
185 *pInstance,
Chia-I Wud50a7d72015-10-26 20:48:51 +0800186 pCreateInfo->enabledExtensionNameCount,
Tobin Ehlisb5fc4fb2015-09-03 09:50:06 -0600187 pCreateInfo->ppEnabledExtensionNames);
188
Courtney Goeltzenleuchter6d8e8182015-11-25 14:31:49 -0700189 init_device_limits(my_data, pAllocator);
Tobin Ehlis9da65002015-09-24 15:25:16 -0600190 my_data->instanceState = unique_ptr<INSTANCE_STATE>(new INSTANCE_STATE());
Tobin Ehlisb5fc4fb2015-09-03 09:50:06 -0600191 }
192 return result;
193}
194
195/* hook DestroyInstance to remove tableInstanceMap entry */
Chia-I Wu9ab61502015-11-06 06:42:02 +0800196VK_LAYER_EXPORT VKAPI_ATTR void VKAPI_CALL vkDestroyInstance(VkInstance instance, const VkAllocationCallbacks* pAllocator)
Tobin Ehlisb5fc4fb2015-09-03 09:50:06 -0600197{
198 dispatch_key key = get_dispatch_key(instance);
Tobin Ehlis1cb7f572015-10-06 09:09:24 -0600199 layer_data *my_data = get_my_data_ptr(key, layer_data_map);
200 VkLayerInstanceDispatchTable *pTable = my_data->instance_dispatch_table;
Chia-I Wuf7458c52015-10-26 21:10:41 +0800201 pTable->DestroyInstance(instance, pAllocator);
Tobin Ehlisb5fc4fb2015-09-03 09:50:06 -0600202
203 // Clean up logging callback, if any
Courtney Goeltzenleuchterd6fce632015-10-05 14:51:41 -0600204 while (my_data->logging_callback.size() > 0) {
Courtney Goeltzenleuchter107a0d22015-11-25 14:07:05 -0700205 VkDebugReportCallbackLUNARG callback = my_data->logging_callback.back();
Courtney Goeltzenleuchter05854bf2015-11-30 12:13:14 -0700206 layer_destroy_msg_callback(my_data->report_data, callback, pAllocator);
Courtney Goeltzenleuchterd6fce632015-10-05 14:51:41 -0600207 my_data->logging_callback.pop_back();
Tobin Ehlisb5fc4fb2015-09-03 09:50:06 -0600208 }
209
210 layer_debug_report_destroy_instance(my_data->report_data);
Tobin Ehlis1cb7f572015-10-06 09:09:24 -0600211 delete my_data->instance_dispatch_table;
212 layer_data_map.erase(key);
Tobin Ehlis0b632332015-10-07 09:38:40 -0600213 if (layer_data_map.empty()) {
214 // Release mutex when destroying last instance.
215 loader_platform_thread_delete_mutex(&globalLock);
216 globalLockInitialized = 0;
217 }
Tobin Ehlisb5fc4fb2015-09-03 09:50:06 -0600218}
219
Chia-I Wu9ab61502015-11-06 06:42:02 +0800220VK_LAYER_EXPORT VKAPI_ATTR VkResult VKAPI_CALL vkEnumeratePhysicalDevices(VkInstance instance, uint32_t* pPhysicalDeviceCount, VkPhysicalDevice* pPhysicalDevices)
Tobin Ehlisb5fc4fb2015-09-03 09:50:06 -0600221{
Tobin Ehlis9da65002015-09-24 15:25:16 -0600222 VkBool32 skipCall = VK_FALSE;
223 layer_data *my_data = get_my_data_ptr(get_dispatch_key(instance), layer_data_map);
224 if (my_data->instanceState) {
Tobin Ehlisb5fc4fb2015-09-03 09:50:06 -0600225 // For this instance, flag when vkEnumeratePhysicalDevices goes to QUERY_COUNT and then QUERY_DETAILS
226 if (NULL == pPhysicalDevices) {
Tobin Ehlis9da65002015-09-24 15:25:16 -0600227 my_data->instanceState->vkEnumeratePhysicalDevicesState = QUERY_COUNT;
Tobin Ehlisb5fc4fb2015-09-03 09:50:06 -0600228 } else {
Tobin Ehlis9da65002015-09-24 15:25:16 -0600229 if (UNCALLED == my_data->instanceState->vkEnumeratePhysicalDevicesState) {
Tobin Ehlisb5fc4fb2015-09-03 09:50:06 -0600230 // Flag error here, shouldn't be calling this without having queried count
Courtney Goeltzenleuchterfd4830c2015-11-25 10:30:56 -0700231 skipCall |= log_msg(my_data->report_data, VK_DEBUG_REPORT_ERROR_BIT, VK_OBJECT_TYPE_INSTANCE, 0, 0, DEVLIMITS_MUST_QUERY_COUNT, "DL",
Tobin Ehlisb5fc4fb2015-09-03 09:50:06 -0600232 "Invalid call sequence to vkEnumeratePhysicalDevices() w/ non-NULL pPhysicalDevices. You should first call vkEnumeratePhysicalDevices() w/ NULL pPhysicalDevices to query pPhysicalDeviceCount.");
233 } // TODO : Could also flag a warning if re-calling this function in QUERY_DETAILS state
Tobin Ehlis9da65002015-09-24 15:25:16 -0600234 else if (my_data->instanceState->physicalDevicesCount != *pPhysicalDeviceCount) {
Courtney Goeltzenleuchterfd4830c2015-11-25 10:30:56 -0700235 skipCall |= log_msg(my_data->report_data, VK_DEBUG_REPORT_WARN_BIT, VK_OBJECT_TYPE_PHYSICAL_DEVICE, 0, 0, DEVLIMITS_COUNT_MISMATCH, "DL",
Tobin Ehlis9da65002015-09-24 15:25:16 -0600236 "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 -0600237 }
Tobin Ehlis9da65002015-09-24 15:25:16 -0600238 my_data->instanceState->vkEnumeratePhysicalDevicesState = QUERY_DETAILS;
Tobin Ehlisb5fc4fb2015-09-03 09:50:06 -0600239 }
Tobin Ehlis9da65002015-09-24 15:25:16 -0600240 if (skipCall)
241 return VK_ERROR_VALIDATION_FAILED;
Tobin Ehlis1cb7f572015-10-06 09:09:24 -0600242 VkResult result = my_data->instance_dispatch_table->EnumeratePhysicalDevices(instance, pPhysicalDeviceCount, pPhysicalDevices);
Tobin Ehlisb5fc4fb2015-09-03 09:50:06 -0600243 if (NULL == pPhysicalDevices) {
Tobin Ehlis9da65002015-09-24 15:25:16 -0600244 my_data->instanceState->physicalDevicesCount = *pPhysicalDeviceCount;
Tobin Ehlisb5fc4fb2015-09-03 09:50:06 -0600245 } else { // Save physical devices
246 for (uint32_t i=0; i < *pPhysicalDeviceCount; i++) {
Tobin Ehlis9da65002015-09-24 15:25:16 -0600247 layer_data *phy_dev_data = get_my_data_ptr(get_dispatch_key(pPhysicalDevices[i]), layer_data_map);
248 phy_dev_data->physicalDeviceState = unique_ptr<PHYSICAL_DEVICE_STATE>(new PHYSICAL_DEVICE_STATE());
249 // Init actual features for each physical device
Tobin Ehlis1cb7f572015-10-06 09:09:24 -0600250 my_data->instance_dispatch_table->GetPhysicalDeviceFeatures(pPhysicalDevices[i], &(phy_dev_data->actualPhysicalDeviceFeatures));
Tony Barbour1088c402015-10-09 11:50:32 -0600251 my_data->instance_dispatch_table->GetPhysicalDeviceProperties(pPhysicalDevices[i], &(phy_dev_data->physicalDeviceProperties));
Tobin Ehlisb5fc4fb2015-09-03 09:50:06 -0600252 }
253 }
254 return result;
255 } else {
Courtney Goeltzenleuchterfd4830c2015-11-25 10:30:56 -0700256 log_msg(my_data->report_data, VK_DEBUG_REPORT_ERROR_BIT, VK_OBJECT_TYPE_INSTANCE, 0, 0, DEVLIMITS_INVALID_INSTANCE, "DL",
Michael Lentine010f4692015-11-03 16:19:46 -0800257 "Invalid instance (%#" PRIxLEAST64 ") passed into vkEnumeratePhysicalDevices().", (uint64_t)instance);
Tobin Ehlisb5fc4fb2015-09-03 09:50:06 -0600258 }
Courtney Goeltzenleuchter58f3eff2015-10-07 13:28:58 -0600259 return VK_ERROR_VALIDATION_FAILED;
Tobin Ehlisb5fc4fb2015-09-03 09:50:06 -0600260}
261
Chia-I Wu9ab61502015-11-06 06:42:02 +0800262VK_LAYER_EXPORT VKAPI_ATTR void VKAPI_CALL vkGetPhysicalDeviceFeatures(VkPhysicalDevice physicalDevice, VkPhysicalDeviceFeatures* pFeatures)
Tobin Ehlisb5fc4fb2015-09-03 09:50:06 -0600263{
Tobin Ehlis9da65002015-09-24 15:25:16 -0600264 layer_data *phy_dev_data = get_my_data_ptr(get_dispatch_key(physicalDevice), layer_data_map);
265 phy_dev_data->physicalDeviceState->vkGetPhysicalDeviceFeaturesState = QUERY_DETAILS;
Courtney Goeltzenleuchter06d89472015-10-20 16:40:38 -0600266 phy_dev_data->instance_dispatch_table->GetPhysicalDeviceFeatures(physicalDevice, pFeatures);
Tobin Ehlisb5fc4fb2015-09-03 09:50:06 -0600267}
268
Chia-I Wu9ab61502015-11-06 06:42:02 +0800269VK_LAYER_EXPORT VKAPI_ATTR void VKAPI_CALL vkGetPhysicalDeviceFormatProperties(VkPhysicalDevice physicalDevice, VkFormat format, VkFormatProperties* pFormatProperties)
Tobin Ehlisb5fc4fb2015-09-03 09:50:06 -0600270{
Courtney Goeltzenleuchter06d89472015-10-20 16:40:38 -0600271 get_my_data_ptr(get_dispatch_key(physicalDevice), layer_data_map)->instance_dispatch_table->GetPhysicalDeviceFormatProperties(
272 physicalDevice, format, pFormatProperties);
Tobin Ehlisb5fc4fb2015-09-03 09:50:06 -0600273}
274
Chia-I Wu9ab61502015-11-06 06:42:02 +0800275VK_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 -0600276{
Chia-I Wu17241042015-10-31 00:31:16 +0800277 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 -0600278}
279
Chia-I Wu9ab61502015-11-06 06:42:02 +0800280VK_LAYER_EXPORT VKAPI_ATTR void VKAPI_CALL vkGetPhysicalDeviceProperties(VkPhysicalDevice physicalDevice, VkPhysicalDeviceProperties* pProperties)
Tobin Ehlisb5fc4fb2015-09-03 09:50:06 -0600281{
Tobin Ehlis1cb7f572015-10-06 09:09:24 -0600282 layer_data *phy_dev_data = get_my_data_ptr(get_dispatch_key(physicalDevice), layer_data_map);
Courtney Goeltzenleuchter06d89472015-10-20 16:40:38 -0600283 phy_dev_data->instance_dispatch_table->GetPhysicalDeviceProperties(physicalDevice, pProperties);
Tobin Ehlisb5fc4fb2015-09-03 09:50:06 -0600284}
285
Chia-I Wu9ab61502015-11-06 06:42:02 +0800286VK_LAYER_EXPORT VKAPI_ATTR void VKAPI_CALL vkGetPhysicalDeviceQueueFamilyProperties(VkPhysicalDevice physicalDevice, uint32_t* pCount, VkQueueFamilyProperties* pQueueFamilyProperties)
Tobin Ehlisb5fc4fb2015-09-03 09:50:06 -0600287{
Tobin Ehlis9da65002015-09-24 15:25:16 -0600288 VkBool32 skipCall = VK_FALSE;
289 layer_data *phy_dev_data = get_my_data_ptr(get_dispatch_key(physicalDevice), layer_data_map);
290 if (phy_dev_data->physicalDeviceState) {
Tobin Ehlisb5fc4fb2015-09-03 09:50:06 -0600291 if (NULL == pQueueFamilyProperties) {
Tobin Ehlis9da65002015-09-24 15:25:16 -0600292 phy_dev_data->physicalDeviceState->vkGetPhysicalDeviceQueueFamilyPropertiesState = QUERY_COUNT;
Tobin Ehlisb5fc4fb2015-09-03 09:50:06 -0600293 } else {
294 // 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 -0600295 if (UNCALLED == phy_dev_data->physicalDeviceState->vkGetPhysicalDeviceQueueFamilyPropertiesState) {
Courtney Goeltzenleuchterfd4830c2015-11-25 10:30:56 -0700296 skipCall |= log_msg(phy_dev_data->report_data, VK_DEBUG_REPORT_ERROR_BIT, VK_OBJECT_TYPE_PHYSICAL_DEVICE, 0, 0, DEVLIMITS_MUST_QUERY_COUNT, "DL",
Tobin Ehlisb5fc4fb2015-09-03 09:50:06 -0600297 "Invalid call sequence to vkGetPhysicalDeviceQueueFamilyProperties() w/ non-NULL pQueueFamilyProperties. You should first call vkGetPhysicalDeviceQueueFamilyProperties() w/ NULL pQueueFamilyProperties to query pCount.");
298 }
Tobin Ehlis9da65002015-09-24 15:25:16 -0600299 // Then verify that pCount that is passed in on second call matches what was returned
300 if (phy_dev_data->physicalDeviceState->queueFamilyPropertiesCount != *pCount) {
Courtney Goeltzenleuchterfd4830c2015-11-25 10:30:56 -0700301 skipCall |= log_msg(phy_dev_data->report_data, VK_DEBUG_REPORT_WARN_BIT, VK_OBJECT_TYPE_PHYSICAL_DEVICE, 0, 0, DEVLIMITS_COUNT_MISMATCH, "DL",
Tobin Ehlis9da65002015-09-24 15:25:16 -0600302 "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 -0600303 }
Tobin Ehlis9da65002015-09-24 15:25:16 -0600304 phy_dev_data->physicalDeviceState->vkGetPhysicalDeviceQueueFamilyPropertiesState = QUERY_DETAILS;
Tobin Ehlisb5fc4fb2015-09-03 09:50:06 -0600305 }
Tobin Ehlis9da65002015-09-24 15:25:16 -0600306 if (skipCall)
Courtney Goeltzenleuchter06d89472015-10-20 16:40:38 -0600307 return;
308 phy_dev_data->instance_dispatch_table->GetPhysicalDeviceQueueFamilyProperties(physicalDevice, pCount, pQueueFamilyProperties);
Tobin Ehlisb5fc4fb2015-09-03 09:50:06 -0600309 if (NULL == pQueueFamilyProperties) {
Tobin Ehlis9da65002015-09-24 15:25:16 -0600310 phy_dev_data->physicalDeviceState->queueFamilyPropertiesCount = *pCount;
Tobin Ehlisb5fc4fb2015-09-03 09:50:06 -0600311 } else { // Save queue family properties
Tobin Ehlis9da65002015-09-24 15:25:16 -0600312 phy_dev_data->queueFamilyProperties.reserve(*pCount);
Tobin Ehlisb5fc4fb2015-09-03 09:50:06 -0600313 for (uint32_t i=0; i < *pCount; i++) {
Tobin Ehlis9da65002015-09-24 15:25:16 -0600314 phy_dev_data->queueFamilyProperties.emplace_back(new VkQueueFamilyProperties(pQueueFamilyProperties[i]));
Tobin Ehlisb5fc4fb2015-09-03 09:50:06 -0600315 }
316 }
Courtney Goeltzenleuchter06d89472015-10-20 16:40:38 -0600317 return;
Tobin Ehlisb5fc4fb2015-09-03 09:50:06 -0600318 } else {
Courtney Goeltzenleuchterfd4830c2015-11-25 10:30:56 -0700319 log_msg(phy_dev_data->report_data, VK_DEBUG_REPORT_ERROR_BIT, VK_OBJECT_TYPE_PHYSICAL_DEVICE, 0, 0, DEVLIMITS_INVALID_PHYSICAL_DEVICE, "DL",
Michael Lentine010f4692015-11-03 16:19:46 -0800320 "Invalid physicalDevice (%#" PRIxLEAST64 ") passed into vkGetPhysicalDeviceQueueFamilyProperties().", (uint64_t)physicalDevice);
Tobin Ehlisb5fc4fb2015-09-03 09:50:06 -0600321 }
322}
323
Chia-I Wu9ab61502015-11-06 06:42:02 +0800324VK_LAYER_EXPORT VKAPI_ATTR void VKAPI_CALL vkGetPhysicalDeviceMemoryProperties(VkPhysicalDevice physicalDevice, VkPhysicalDeviceMemoryProperties* pMemoryProperties)
Tobin Ehlisb5fc4fb2015-09-03 09:50:06 -0600325{
Courtney Goeltzenleuchter06d89472015-10-20 16:40:38 -0600326 get_my_data_ptr(get_dispatch_key(physicalDevice), layer_data_map)->instance_dispatch_table->GetPhysicalDeviceMemoryProperties(physicalDevice, pMemoryProperties);
Tobin Ehlisb5fc4fb2015-09-03 09:50:06 -0600327}
328
Chia-I Wu9ab61502015-11-06 06:42:02 +0800329VK_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 -0600330{
Courtney Goeltzenleuchter06d89472015-10-20 16:40:38 -0600331 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 -0600332}
333
Chia-I Wu9ab61502015-11-06 06:42:02 +0800334VK_LAYER_EXPORT VKAPI_ATTR void VKAPI_CALL vkCmdSetViewport(
Chia-I Wu3432a0c2015-10-27 18:04:07 +0800335 VkCommandBuffer commandBuffer,
Courtney Goeltzenleuchter078f8172015-09-21 11:44:06 -0600336 uint32_t viewportCount,
337 const VkViewport* pViewports)
338{
339 VkBool32 skipCall = VK_FALSE;
340 /* TODO: Verify viewportCount < maxViewports from VkPhysicalDeviceLimits */
341 if (VK_FALSE == skipCall) {
Chia-I Wu3432a0c2015-10-27 18:04:07 +0800342 layer_data *my_data = get_my_data_ptr(get_dispatch_key(commandBuffer), layer_data_map);
343 my_data->device_dispatch_table->CmdSetViewport(commandBuffer, viewportCount, pViewports);
Courtney Goeltzenleuchter078f8172015-09-21 11:44:06 -0600344 }
345}
346
Chia-I Wu9ab61502015-11-06 06:42:02 +0800347VK_LAYER_EXPORT VKAPI_ATTR void VKAPI_CALL vkCmdSetScissor(
Chia-I Wu3432a0c2015-10-27 18:04:07 +0800348 VkCommandBuffer commandBuffer,
Courtney Goeltzenleuchter078f8172015-09-21 11:44:06 -0600349 uint32_t scissorCount,
Courtney Goeltzenleuchter49c73082015-09-17 15:06:17 -0600350 const VkRect2D* pScissors)
351{
352 VkBool32 skipCall = VK_FALSE;
Courtney Goeltzenleuchter078f8172015-09-21 11:44:06 -0600353 /* TODO: Verify scissorCount < maxViewports from VkPhysicalDeviceLimits */
354 /* TODO: viewportCount and scissorCount must match at draw time */
Courtney Goeltzenleuchter49c73082015-09-17 15:06:17 -0600355 if (VK_FALSE == skipCall) {
Chia-I Wu3432a0c2015-10-27 18:04:07 +0800356 layer_data *my_data = get_my_data_ptr(get_dispatch_key(commandBuffer), layer_data_map);
357 my_data->device_dispatch_table->CmdSetScissor(commandBuffer, scissorCount, pScissors);
Courtney Goeltzenleuchter49c73082015-09-17 15:06:17 -0600358 }
359}
360
Tobin Ehlisb5fc4fb2015-09-03 09:50:06 -0600361static void createDeviceRegisterExtensions(const VkDeviceCreateInfo* pCreateInfo, VkDevice device)
362{
363 uint32_t i;
Tobin Ehlis1cb7f572015-10-06 09:09:24 -0600364 layer_data *my_data = get_my_data_ptr(get_dispatch_key(device), layer_data_map);
365 my_data->device_extensions.debug_marker_enabled = false;
Tobin Ehlisb5fc4fb2015-09-03 09:50:06 -0600366
Chia-I Wud50a7d72015-10-26 20:48:51 +0800367 for (i = 0; i < pCreateInfo->enabledExtensionNameCount; i++) {
Tobin Ehlisb5fc4fb2015-09-03 09:50:06 -0600368 if (strcmp(pCreateInfo->ppEnabledExtensionNames[i], DEBUG_MARKER_EXTENSION_NAME) == 0) {
369 /* Found a matching extension name, mark it enabled and init dispatch table*/
370 initDebugMarkerTable(device);
Tobin Ehlis1cb7f572015-10-06 09:09:24 -0600371 my_data->device_extensions.debug_marker_enabled = true;
Tobin Ehlisb5fc4fb2015-09-03 09:50:06 -0600372 }
373
374 }
375}
376
Tobin Ehlis9da65002015-09-24 15:25:16 -0600377// Verify that features have been queried and verify that requested features are available
378static VkBool32 validate_features_request(layer_data *phy_dev_data)
379{
380 VkBool32 skipCall = VK_FALSE;
381 // Verify that all of the requested features are available
382 // Get ptrs into actual and requested structs and if requested is 1 but actual is 0, request is invalid
383 VkBool32* actual = (VkBool32*)&(phy_dev_data->actualPhysicalDeviceFeatures);
384 VkBool32* requested = (VkBool32*)&(phy_dev_data->requestedPhysicalDeviceFeatures);
385 // TODO : This is a nice, compact way to loop through struct, but a bad way to report issues
386 // Need to provide the struct member name with the issue. To do that seems like we'll
387 // have to loop through each struct member which should be done w/ codegen to keep in synch.
388 uint32_t errors = 0;
389 uint32_t totalBools = sizeof(VkPhysicalDeviceFeatures)/sizeof(VkBool32);
390 for (uint32_t i = 0; i < totalBools; i++) {
391 if (requested[i] > actual[i]) {
Courtney Goeltzenleuchterfd4830c2015-11-25 10:30:56 -0700392 skipCall |= log_msg(phy_dev_data->report_data, VK_DEBUG_REPORT_ERROR_BIT, VK_OBJECT_TYPE_PHYSICAL_DEVICE, 0, 0, DEVLIMITS_INVALID_FEATURE_REQUESTED, "DL",
Tobin Ehlis9da65002015-09-24 15:25:16 -0600393 "While calling vkCreateDevice(), requesting feature #%u in VkPhysicalDeviceFeatures struct, which is not available on this device.", i);
394 errors++;
395 }
396 }
397 if (errors && (UNCALLED == phy_dev_data->physicalDeviceState->vkGetPhysicalDeviceFeaturesState)) {
398 // If user didn't request features, notify them that they should
Courtney Goeltzenleuchterfd4830c2015-11-25 10:30:56 -0700399 skipCall |= log_msg(phy_dev_data->report_data, VK_DEBUG_REPORT_WARN_BIT, VK_OBJECT_TYPE_PHYSICAL_DEVICE, 0, 0, DEVLIMITS_INVALID_FEATURE_REQUESTED, "DL",
Tobin Ehlis9da65002015-09-24 15:25:16 -0600400 "You requested features that are unavailable on this device. You should first query feature availability by calling vkGetPhysicalDeviceFeatures().");
401 }
Tobin Ehlis72b27cc2015-09-29 11:22:37 -0600402 return skipCall;
Tobin Ehlis9da65002015-09-24 15:25:16 -0600403}
404
Chia-I Wu9ab61502015-11-06 06:42:02 +0800405VK_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 -0600406{
Tobin Ehlis9da65002015-09-24 15:25:16 -0600407 VkBool32 skipCall = VK_FALSE;
408 layer_data *phy_dev_data = get_my_data_ptr(get_dispatch_key(gpu), layer_data_map);
Tobin Ehlis54a6c182015-09-22 14:00:58 -0600409 // First check is app has actually requested queueFamilyProperties
Tobin Ehlis9da65002015-09-24 15:25:16 -0600410 if (!phy_dev_data->physicalDeviceState) {
Courtney Goeltzenleuchterfd4830c2015-11-25 10:30:56 -0700411 skipCall |= log_msg(phy_dev_data->report_data, VK_DEBUG_REPORT_ERROR_BIT, VK_OBJECT_TYPE_PHYSICAL_DEVICE, 0, 0, DEVLIMITS_MUST_QUERY_COUNT, "DL",
Tobin Ehlis54a6c182015-09-22 14:00:58 -0600412 "Invalid call to vkCreateDevice() w/o first calling vkEnumeratePhysicalDevices().");
Tobin Ehlis9da65002015-09-24 15:25:16 -0600413 } else if (QUERY_DETAILS != phy_dev_data->physicalDeviceState->vkGetPhysicalDeviceQueueFamilyPropertiesState) {
Courtney Goeltzenleuchterfd4830c2015-11-25 10:30:56 -0700414 skipCall |= log_msg(phy_dev_data->report_data, VK_DEBUG_REPORT_WARN_BIT, VK_OBJECT_TYPE_PHYSICAL_DEVICE, 0, 0, DEVLIMITS_INVALID_QUEUE_CREATE_REQUEST, "DL",
Tobin Ehlis54a6c182015-09-22 14:00:58 -0600415 "Invalid call to vkCreateDevice() w/o first calling vkGetPhysicalDeviceQueueFamilyProperties().");
416 } else {
417 // Check that the requested queue properties are valid
Chia-I Wu02124482015-11-06 06:42:02 +0800418 for (uint32_t i=0; i<pCreateInfo->queueCreateInfoCount; i++) {
419 uint32_t requestedIndex = pCreateInfo->pQueueCreateInfos[i].queueFamilyIndex;
Tobin Ehlis9da65002015-09-24 15:25:16 -0600420 if (phy_dev_data->queueFamilyProperties.size() <= requestedIndex) { // requested index is out of bounds for this physical device
Courtney Goeltzenleuchterfd4830c2015-11-25 10:30:56 -0700421 skipCall |= log_msg(phy_dev_data->report_data, VK_DEBUG_REPORT_ERROR_BIT, VK_OBJECT_TYPE_PHYSICAL_DEVICE, 0, 0, DEVLIMITS_INVALID_QUEUE_CREATE_REQUEST, "DL",
Tobin Ehlis54a6c182015-09-22 14:00:58 -0600422 "Invalid queue create request in vkCreateDevice(). Invalid queueFamilyIndex %u requested.", requestedIndex);
Chia-I Wu02124482015-11-06 06:42:02 +0800423 } else if (pCreateInfo->pQueueCreateInfos[i].queueCount > phy_dev_data->queueFamilyProperties[requestedIndex]->queueCount) {
Courtney Goeltzenleuchterfd4830c2015-11-25 10:30:56 -0700424 skipCall |= log_msg(phy_dev_data->report_data, VK_DEBUG_REPORT_ERROR_BIT, VK_OBJECT_TYPE_PHYSICAL_DEVICE, 0, 0, DEVLIMITS_INVALID_QUEUE_CREATE_REQUEST, "DL",
Chia-I Wu02124482015-11-06 06:42:02 +0800425 "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 -0600426 }
Tobin Ehlisb5fc4fb2015-09-03 09:50:06 -0600427 }
428 }
Tobin Ehlis9da65002015-09-24 15:25:16 -0600429 // Check that any requested features are available
430 if (pCreateInfo->pEnabledFeatures) {
431 phy_dev_data->requestedPhysicalDeviceFeatures = *(pCreateInfo->pEnabledFeatures);
432 skipCall |= validate_features_request(phy_dev_data);
433 }
434 if (skipCall)
435 return VK_ERROR_VALIDATION_FAILED;
436
Tobin Ehlis1cb7f572015-10-06 09:09:24 -0600437 layer_data *my_device_data = get_my_data_ptr(get_dispatch_key(*pDevice), layer_data_map);
Chia-I Wuf7458c52015-10-26 21:10:41 +0800438 VkResult result = my_device_data->device_dispatch_table->CreateDevice(gpu, pCreateInfo, pAllocator, pDevice);
Tobin Ehlisb5fc4fb2015-09-03 09:50:06 -0600439 if (result == VK_SUCCESS) {
Tobin Ehlis9da65002015-09-24 15:25:16 -0600440 my_device_data->report_data = layer_debug_report_create_device(phy_dev_data->report_data, *pDevice);
Tobin Ehlisb5fc4fb2015-09-03 09:50:06 -0600441 createDeviceRegisterExtensions(pCreateInfo, *pDevice);
Tobin Ehlis1cb7f572015-10-06 09:09:24 -0600442 my_device_data->physicalDevice = gpu;
Tobin Ehlisb5fc4fb2015-09-03 09:50:06 -0600443 }
444 return result;
445}
446
Chia-I Wu9ab61502015-11-06 06:42:02 +0800447VK_LAYER_EXPORT VKAPI_ATTR void VKAPI_CALL vkDestroyDevice(VkDevice device, const VkAllocationCallbacks* pAllocator)
Tobin Ehlisb5fc4fb2015-09-03 09:50:06 -0600448{
449 // Free device lifetime allocations
450 dispatch_key key = get_dispatch_key(device);
Tobin Ehlis1cb7f572015-10-06 09:09:24 -0600451 layer_data *my_device_data = get_my_data_ptr(key, layer_data_map);
Chia-I Wuf7458c52015-10-26 21:10:41 +0800452 my_device_data->device_dispatch_table->DestroyDevice(device, pAllocator);
Tobin Ehlis1cb7f572015-10-06 09:09:24 -0600453 tableDebugMarkerMap.erase(key);
454 delete my_device_data->device_dispatch_table;
455 layer_data_map.erase(key);
Tobin Ehlisb5fc4fb2015-09-03 09:50:06 -0600456}
457
Chia-I Wu9ab61502015-11-06 06:42:02 +0800458VK_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 -0600459{
Tobin Ehlis9da65002015-09-24 15:25:16 -0600460 // TODO : Verify that requested QueueFamilyIndex for this pool exists
Chia-I Wu3432a0c2015-10-27 18:04:07 +0800461 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 -0600462 return result;
Tobin Ehlisb5fc4fb2015-09-03 09:50:06 -0600463}
464
Chia-I Wu9ab61502015-11-06 06:42:02 +0800465VK_LAYER_EXPORT VKAPI_ATTR void VKAPI_CALL vkDestroyCommandPool(VkDevice device, VkCommandPool commandPool, const VkAllocationCallbacks* pAllocator)
Tobin Ehlisb5fc4fb2015-09-03 09:50:06 -0600466{
Chia-I Wu3432a0c2015-10-27 18:04:07 +0800467 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 -0600468}
469
Chia-I Wu9ab61502015-11-06 06:42:02 +0800470VK_LAYER_EXPORT VKAPI_ATTR VkResult VKAPI_CALL vkResetCommandPool(VkDevice device, VkCommandPool commandPool, VkCommandPoolResetFlags flags)
Tobin Ehlisb5fc4fb2015-09-03 09:50:06 -0600471{
Chia-I Wu3432a0c2015-10-27 18:04:07 +0800472 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 -0600473 return result;
Tobin Ehlisb5fc4fb2015-09-03 09:50:06 -0600474}
475
Chia-I Wu9ab61502015-11-06 06:42:02 +0800476VK_LAYER_EXPORT VKAPI_ATTR VkResult VKAPI_CALL vkAllocateCommandBuffers(VkDevice device, const VkCommandBufferAllocateInfo* pCreateInfo, VkCommandBuffer* pCommandBuffer)
Tobin Ehlisb5fc4fb2015-09-03 09:50:06 -0600477{
Chia-I Wu3432a0c2015-10-27 18:04:07 +0800478 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 -0600479 return result;
480}
Mark Lobodzinskifbb130e2015-10-06 11:59:54 -0600481
Chia-I Wu9ab61502015-11-06 06:42:02 +0800482VK_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 -0600483{
Chia-I Wu3432a0c2015-10-27 18:04:07 +0800484 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 -0600485}
486
Chia-I Wu9ab61502015-11-06 06:42:02 +0800487VK_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 -0600488{
Tobin Ehlis9da65002015-09-24 15:25:16 -0600489 VkBool32 skipCall = VK_FALSE;
Tobin Ehlis1cb7f572015-10-06 09:09:24 -0600490 layer_data *dev_data = get_my_data_ptr(get_dispatch_key(device), layer_data_map);
491 VkPhysicalDevice gpu = dev_data->physicalDevice;
Tobin Ehlis9da65002015-09-24 15:25:16 -0600492 layer_data *phy_dev_data = get_my_data_ptr(get_dispatch_key(gpu), layer_data_map);
493 if (queueFamilyIndex >= phy_dev_data->queueFamilyProperties.size()) { // requested index is out of bounds for this physical device
Courtney Goeltzenleuchterfd4830c2015-11-25 10:30:56 -0700494 skipCall |= log_msg(phy_dev_data->report_data, VK_DEBUG_REPORT_ERROR_BIT, VK_OBJECT_TYPE_PHYSICAL_DEVICE, 0, 0, DEVLIMITS_INVALID_QUEUE_CREATE_REQUEST, "DL",
Tobin Ehlisb5fc4fb2015-09-03 09:50:06 -0600495 "Invalid queueFamilyIndex %u requested in vkGetDeviceQueue().", queueFamilyIndex);
Tobin Ehlis9da65002015-09-24 15:25:16 -0600496 } else if (queueIndex >= phy_dev_data->queueFamilyProperties[queueFamilyIndex]->queueCount) {
Courtney Goeltzenleuchterfd4830c2015-11-25 10:30:56 -0700497 skipCall |= log_msg(phy_dev_data->report_data, VK_DEBUG_REPORT_ERROR_BIT, VK_OBJECT_TYPE_PHYSICAL_DEVICE, 0, 0, DEVLIMITS_INVALID_QUEUE_CREATE_REQUEST, "DL",
Tobin Ehlis9da65002015-09-24 15:25:16 -0600498 "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 -0600499 }
Tobin Ehlis9da65002015-09-24 15:25:16 -0600500 if (skipCall)
Courtney Goeltzenleuchter06d89472015-10-20 16:40:38 -0600501 return;
502 dev_data->device_dispatch_table->GetDeviceQueue(device, queueFamilyIndex, queueIndex, pQueue);
Tobin Ehlisb5fc4fb2015-09-03 09:50:06 -0600503}
504
Chia-I Wu9ab61502015-11-06 06:42:02 +0800505VK_LAYER_EXPORT VKAPI_ATTR void VKAPI_CALL vkCmdUpdateBuffer(
Chia-I Wu3432a0c2015-10-27 18:04:07 +0800506 VkCommandBuffer commandBuffer,
507 VkBuffer dstBuffer,
508 VkDeviceSize dstOffset,
Mike Stroyana3082432015-09-25 13:39:21 -0600509 VkDeviceSize dataSize,
510 const uint32_t* pData)
511{
Chia-I Wu3432a0c2015-10-27 18:04:07 +0800512 layer_data *dev_data = get_my_data_ptr(get_dispatch_key(commandBuffer), layer_data_map);
Mike Stroyana3082432015-09-25 13:39:21 -0600513
Chia-I Wu3432a0c2015-10-27 18:04:07 +0800514 // dstOffset is the byte offset into the buffer to start updating and must be a multiple of 4.
515 if (dstOffset & 3) {
516 layer_data *my_data = get_my_data_ptr(get_dispatch_key(commandBuffer), layer_data_map);
Courtney Goeltzenleuchterb19042e2015-11-25 11:38:54 -0700517 if (log_msg(my_data->report_data, VK_DEBUG_REPORT_ERROR_BIT, (VkDebugReportObjectTypeLUNARG)0, 0, 0, 1, "DL",
Chia-I Wu3432a0c2015-10-27 18:04:07 +0800518 "vkCmdUpdateBuffer parameter, VkDeviceSize dstOffset, is not a multiple of 4")) {
Mike Stroyana3082432015-09-25 13:39:21 -0600519 return;
520 }
521 }
522
523 // dataSize is the number of bytes to update, which must be a multiple of 4.
524 if (dataSize & 3) {
Chia-I Wu3432a0c2015-10-27 18:04:07 +0800525 layer_data *my_data = get_my_data_ptr(get_dispatch_key(commandBuffer), layer_data_map);
Courtney Goeltzenleuchterb19042e2015-11-25 11:38:54 -0700526 if (log_msg(my_data->report_data, VK_DEBUG_REPORT_ERROR_BIT, (VkDebugReportObjectTypeLUNARG)0, 0, 0, 1, "DL",
Mike Stroyana3082432015-09-25 13:39:21 -0600527 "vkCmdUpdateBuffer parameter, VkDeviceSize dataSize, is not a multiple of 4")) {
528 return;
529 }
530 }
531
Chia-I Wu3432a0c2015-10-27 18:04:07 +0800532 dev_data->device_dispatch_table->CmdUpdateBuffer(commandBuffer, dstBuffer, dstOffset, dataSize, pData);
Mike Stroyana3082432015-09-25 13:39:21 -0600533}
534
Chia-I Wu9ab61502015-11-06 06:42:02 +0800535VK_LAYER_EXPORT VKAPI_ATTR void VKAPI_CALL vkCmdFillBuffer(
Chia-I Wu3432a0c2015-10-27 18:04:07 +0800536 VkCommandBuffer commandBuffer,
537 VkBuffer dstBuffer,
538 VkDeviceSize dstOffset,
Chia-I Wu2bfb33c2015-10-26 17:24:52 +0800539 VkDeviceSize size,
Mike Stroyana3082432015-09-25 13:39:21 -0600540 uint32_t data)
541{
Chia-I Wu3432a0c2015-10-27 18:04:07 +0800542 layer_data *dev_data = get_my_data_ptr(get_dispatch_key(commandBuffer), layer_data_map);
Mike Stroyana3082432015-09-25 13:39:21 -0600543
Chia-I Wu3432a0c2015-10-27 18:04:07 +0800544 // dstOffset is the byte offset into the buffer to start filling and must be a multiple of 4.
545 if (dstOffset & 3) {
546 layer_data *my_data = get_my_data_ptr(get_dispatch_key(commandBuffer), layer_data_map);
Courtney Goeltzenleuchterb19042e2015-11-25 11:38:54 -0700547 if (log_msg(my_data->report_data, VK_DEBUG_REPORT_ERROR_BIT, (VkDebugReportObjectTypeLUNARG)0, 0, 0, 1, "DL",
Chia-I Wu3432a0c2015-10-27 18:04:07 +0800548 "vkCmdFillBuffer parameter, VkDeviceSize dstOffset, is not a multiple of 4")) {
Mike Stroyana3082432015-09-25 13:39:21 -0600549 return;
550 }
551 }
552
Chia-I Wu2bfb33c2015-10-26 17:24:52 +0800553 // size is the number of bytes to fill, which must be a multiple of 4.
554 if (size & 3) {
Chia-I Wu3432a0c2015-10-27 18:04:07 +0800555 layer_data *my_data = get_my_data_ptr(get_dispatch_key(commandBuffer), layer_data_map);
Courtney Goeltzenleuchterb19042e2015-11-25 11:38:54 -0700556 if (log_msg(my_data->report_data, VK_DEBUG_REPORT_ERROR_BIT, (VkDebugReportObjectTypeLUNARG)0, 0, 0, 1, "DL",
Chia-I Wu2bfb33c2015-10-26 17:24:52 +0800557 "vkCmdFillBuffer parameter, VkDeviceSize size, is not a multiple of 4")) {
Mike Stroyana3082432015-09-25 13:39:21 -0600558 return;
559 }
560 }
561
Chia-I Wu3432a0c2015-10-27 18:04:07 +0800562 dev_data->device_dispatch_table->CmdFillBuffer(commandBuffer, dstBuffer, dstOffset, size, data);
Mike Stroyana3082432015-09-25 13:39:21 -0600563}
564
Courtney Goeltzenleuchter05854bf2015-11-30 12:13:14 -0700565VK_LAYER_EXPORT VKAPI_ATTR VkResult VKAPI_CALL vkCreateDebugReportCallbackLUNARG(
566 VkInstance instance,
567 VkDebugReportCallbackCreateInfoLUNARG* pCreateInfo,
568 const VkAllocationCallbacks* pAllocator,
569 VkDebugReportCallbackLUNARG* pMsgCallback)
Tobin Ehlisb5fc4fb2015-09-03 09:50:06 -0600570{
Tobin Ehlis1cb7f572015-10-06 09:09:24 -0600571 layer_data *my_data = get_my_data_ptr(get_dispatch_key(instance), layer_data_map);
Courtney Goeltzenleuchter05854bf2015-11-30 12:13:14 -0700572 VkResult res = my_data->instance_dispatch_table->CreateDebugReportCallbackLUNARG(instance, pCreateInfo, pAllocator, pMsgCallback);
Tobin Ehlisb5fc4fb2015-09-03 09:50:06 -0600573 if (VK_SUCCESS == res) {
Courtney Goeltzenleuchter05854bf2015-11-30 12:13:14 -0700574 res = layer_create_msg_callback(my_data->report_data, pCreateInfo, pAllocator, pMsgCallback);
Tobin Ehlisb5fc4fb2015-09-03 09:50:06 -0600575 }
576 return res;
577}
578
Courtney Goeltzenleuchter05854bf2015-11-30 12:13:14 -0700579VK_LAYER_EXPORT VKAPI_ATTR void VKAPI_CALL vkDestroyDebugReportCallbackLUNARG(
580 VkInstance instance,
581 VkDebugReportCallbackLUNARG msgCallback,
582 const VkAllocationCallbacks* pAllocator)
Tobin Ehlisb5fc4fb2015-09-03 09:50:06 -0600583{
Tobin Ehlisb5fc4fb2015-09-03 09:50:06 -0600584 layer_data *my_data = get_my_data_ptr(get_dispatch_key(instance), layer_data_map);
Courtney Goeltzenleuchter05854bf2015-11-30 12:13:14 -0700585 my_data->instance_dispatch_table->DestroyDebugReportCallbackLUNARG(instance, msgCallback, pAllocator);
586 layer_destroy_msg_callback(my_data->report_data, msgCallback, pAllocator);
Tobin Ehlisb5fc4fb2015-09-03 09:50:06 -0600587}
588
Chia-I Wu9ab61502015-11-06 06:42:02 +0800589VK_LAYER_EXPORT VKAPI_ATTR PFN_vkVoidFunction VKAPI_CALL vkGetDeviceProcAddr(VkDevice dev, const char* funcName)
Tobin Ehlisb5fc4fb2015-09-03 09:50:06 -0600590{
591 if (dev == NULL)
592 return NULL;
593
Tobin Ehlis1cb7f572015-10-06 09:09:24 -0600594 layer_data *my_data;
Tobin Ehlisb5fc4fb2015-09-03 09:50:06 -0600595 /* loader uses this to force layer initialization; device object is wrapped */
596 if (!strcmp(funcName, "vkGetDeviceProcAddr")) {
Tobin Ehlis1cb7f572015-10-06 09:09:24 -0600597 VkBaseLayerObject* wrapped_dev = (VkBaseLayerObject*) dev;
598 my_data = get_my_data_ptr(get_dispatch_key(wrapped_dev->baseObject), layer_data_map);
599 my_data->device_dispatch_table = new VkLayerDispatchTable;
600 layer_initialize_dispatch_table(my_data->device_dispatch_table, wrapped_dev);
Tobin Ehlisb5fc4fb2015-09-03 09:50:06 -0600601 return (PFN_vkVoidFunction) vkGetDeviceProcAddr;
602 }
Tobin Ehlis1cb7f572015-10-06 09:09:24 -0600603 my_data = get_my_data_ptr(get_dispatch_key(dev), layer_data_map);
Tobin Ehlisb5fc4fb2015-09-03 09:50:06 -0600604 if (!strcmp(funcName, "vkCreateDevice"))
605 return (PFN_vkVoidFunction) vkCreateDevice;
606 if (!strcmp(funcName, "vkDestroyDevice"))
607 return (PFN_vkVoidFunction) vkDestroyDevice;
Tobin Ehlisb5fc4fb2015-09-03 09:50:06 -0600608 if (!strcmp(funcName, "vkGetDeviceQueue"))
609 return (PFN_vkVoidFunction) vkGetDeviceQueue;
Tobin Ehlis9da65002015-09-24 15:25:16 -0600610 if (!strcmp(funcName, "CreateCommandPool"))
611 return (PFN_vkVoidFunction) vkCreateCommandPool;
612 if (!strcmp(funcName, "DestroyCommandPool"))
613 return (PFN_vkVoidFunction) vkDestroyCommandPool;
614 if (!strcmp(funcName, "ResetCommandPool"))
615 return (PFN_vkVoidFunction) vkResetCommandPool;
Chia-I Wu3432a0c2015-10-27 18:04:07 +0800616 if (!strcmp(funcName, "vkAllocateCommandBuffers"))
617 return (PFN_vkVoidFunction) vkAllocateCommandBuffers;
Courtney Goeltzenleuchterbee18a92015-10-23 14:21:05 -0600618 if (!strcmp(funcName, "vkFreeCommandBuffers"))
619 return (PFN_vkVoidFunction) vkFreeCommandBuffers;
Mike Stroyana3082432015-09-25 13:39:21 -0600620 if (!strcmp(funcName, "vkCmdUpdateBuffer"))
621 return (PFN_vkVoidFunction) vkCmdUpdateBuffer;
622 if (!strcmp(funcName, "vkCmdFillBuffer"))
623 return (PFN_vkVoidFunction) vkCmdFillBuffer;
Tobin Ehlisb5fc4fb2015-09-03 09:50:06 -0600624
Tobin Ehlis1cb7f572015-10-06 09:09:24 -0600625 VkLayerDispatchTable* pTable = my_data->device_dispatch_table;
Tobin Ehlisb5fc4fb2015-09-03 09:50:06 -0600626 {
627 if (pTable->GetDeviceProcAddr == NULL)
628 return NULL;
629 return pTable->GetDeviceProcAddr(dev, funcName);
630 }
631}
632
Chia-I Wu9ab61502015-11-06 06:42:02 +0800633VK_LAYER_EXPORT VKAPI_ATTR PFN_vkVoidFunction VKAPI_CALL vkGetInstanceProcAddr(VkInstance instance, const char* funcName)
Tobin Ehlisb5fc4fb2015-09-03 09:50:06 -0600634{
635 PFN_vkVoidFunction fptr;
636 if (instance == NULL)
637 return NULL;
638
Tobin Ehlis1cb7f572015-10-06 09:09:24 -0600639 layer_data *my_data;
Tobin Ehlisb5fc4fb2015-09-03 09:50:06 -0600640 /* loader uses this to force layer initialization; instance object is wrapped */
641 if (!strcmp(funcName, "vkGetInstanceProcAddr")) {
Tobin Ehlis1cb7f572015-10-06 09:09:24 -0600642 VkBaseLayerObject* wrapped_inst = (VkBaseLayerObject*) instance;
643 my_data = get_my_data_ptr(get_dispatch_key(wrapped_inst->baseObject), layer_data_map);
644 my_data->instance_dispatch_table = new VkLayerInstanceDispatchTable;
645 layer_init_instance_dispatch_table(my_data->instance_dispatch_table, wrapped_inst);
Tobin Ehlisb5fc4fb2015-09-03 09:50:06 -0600646 return (PFN_vkVoidFunction) vkGetInstanceProcAddr;
647 }
Tobin Ehlis1cb7f572015-10-06 09:09:24 -0600648 my_data = get_my_data_ptr(get_dispatch_key(instance), layer_data_map);
Tobin Ehlisb5fc4fb2015-09-03 09:50:06 -0600649 if (!strcmp(funcName, "vkCreateInstance"))
650 return (PFN_vkVoidFunction) vkCreateInstance;
651 if (!strcmp(funcName, "vkDestroyInstance"))
652 return (PFN_vkVoidFunction) vkDestroyInstance;
653 if (!strcmp(funcName, "vkEnumeratePhysicalDevices"))
654 return (PFN_vkVoidFunction) vkEnumeratePhysicalDevices;
655 if (!strcmp(funcName, "vkGetPhysicalDeviceFeatures"))
656 return (PFN_vkVoidFunction) vkGetPhysicalDeviceFeatures;
657 if (!strcmp(funcName, "vkGetPhysicalDeviceFormatProperties"))
658 return (PFN_vkVoidFunction) vkGetPhysicalDeviceFormatProperties;
659 if (!strcmp(funcName, "vkGetPhysicalDeviceImageFormatProperties"))
660 return (PFN_vkVoidFunction) vkGetPhysicalDeviceImageFormatProperties;
661 if (!strcmp(funcName, "vkGetPhysicalDeviceProperties"))
662 return (PFN_vkVoidFunction) vkGetPhysicalDeviceProperties;
663 if (!strcmp(funcName, "vkGetPhysicalDeviceQueueFamilyProperties"))
664 return (PFN_vkVoidFunction) vkGetPhysicalDeviceQueueFamilyProperties;
665 if (!strcmp(funcName, "vkGetPhysicalDeviceMemoryProperties"))
666 return (PFN_vkVoidFunction) vkGetPhysicalDeviceMemoryProperties;
Tobin Ehlisb5fc4fb2015-09-03 09:50:06 -0600667 if (!strcmp(funcName, "vkGetPhysicalDeviceSparseImageFormatProperties"))
668 return (PFN_vkVoidFunction) vkGetPhysicalDeviceSparseImageFormatProperties;
Tobin Ehlisfd3843f2015-09-29 12:26:00 -0600669 if (!strcmp(funcName, "vkEnumerateInstanceLayerProperties"))
670 return (PFN_vkVoidFunction) vkEnumerateInstanceLayerProperties;
671 if (!strcmp(funcName, "vkEnumerateInstanceExtensionProperties"))
672 return (PFN_vkVoidFunction) vkEnumerateInstanceExtensionProperties;
Tobin Ehlisb5fc4fb2015-09-03 09:50:06 -0600673
Tobin Ehlisb5fc4fb2015-09-03 09:50:06 -0600674 fptr = debug_report_get_instance_proc_addr(my_data->report_data, funcName);
675 if (fptr)
676 return fptr;
677
678 {
Tobin Ehlis1cb7f572015-10-06 09:09:24 -0600679 VkLayerInstanceDispatchTable* pTable = my_data->instance_dispatch_table;
Tobin Ehlisb5fc4fb2015-09-03 09:50:06 -0600680 if (pTable->GetInstanceProcAddr == NULL)
681 return NULL;
682 return pTable->GetInstanceProcAddr(instance, funcName);
683 }
684}