blob: 2403e268dd39143ac91d24b5c420767dbf627fd3 [file] [log] [blame]
Tobin Ehlis5b5e7bc2016-03-09 16:12:48 -07001/* Copyright (c) 2015-2016 The Khronos Group Inc.
2 * Copyright (c) 2015-2016 Valve Corporation
3 * Copyright (c) 2015-2016 LunarG, Inc.
4 * Copyright (C) 2015-2016 Google Inc.
5 *
6 * Permission is hereby granted, free of charge, to any person obtaining a copy
7 * of this software and/or associated documentation files (the "Materials"), to
8 * deal in the Materials without restriction, including without limitation the
9 * rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
10 * sell copies of the Materials, and to permit persons to whom the Materials
11 * are furnished to do so, subject to the following conditions:
12 *
13 * The above copyright notice(s) and this permission notice shall be included
14 * in all copies or substantial portions of the Materials.
15 *
16 * THE MATERIALS ARE PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
19 *
20 * IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
21 * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
22 * OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE MATERIALS OR THE
23 * USE OR OTHER DEALINGS IN THE MATERIALS
24 *
25 * Author: Cody Northrop <cnorthrop@google.com>
26 * Author: Michael Lentine <mlentine@google.com>
27 * Author: Tobin Ehlis <tobine@google.com>
28 * Author: Chia-I Wu <olv@google.com>
29 * Author: Chris Forbes <chrisf@ijw.co.nz>
30 * Author: Mark Lobodzinski <mark@lunarg.com>
31 * Author: Ian Elliott <ianelliott@google.com>
32 */
33
34// Allow use of STL min and max functions in Windows
35#define NOMINMAX
36
37// Turn on mem_tracker merged code
Mark Lobodzinski03b71512016-03-23 14:33:02 -060038#define MTMERGESOURCE 1
Tobin Ehlis5b5e7bc2016-03-09 16:12:48 -070039
Tobin Ehlis94c53c02016-04-05 13:33:00 -060040#include <SPIRV/spirv.hpp>
41#include <algorithm>
42#include <assert.h>
43#include <iostream>
44#include <list>
45#include <map>
Jeremy Hayesb9e99232016-04-13 16:20:24 -060046#include <mutex>
Tobin Ehlis94c53c02016-04-05 13:33:00 -060047#include <set>
Tobin Ehlis5b5e7bc2016-03-09 16:12:48 -070048#include <stdio.h>
49#include <stdlib.h>
50#include <string.h>
Tobin Ehlis94c53c02016-04-05 13:33:00 -060051#include <string>
Tobin Ehlis5b5e7bc2016-03-09 16:12:48 -070052#include <unordered_map>
53#include <unordered_set>
Tobin Ehlis5b5e7bc2016-03-09 16:12:48 -070054
55#include "vk_loader_platform.h"
56#include "vk_dispatch_table_helper.h"
57#include "vk_struct_string_helper_cpp.h"
58#if defined(__GNUC__)
59#pragma GCC diagnostic ignored "-Wwrite-strings"
60#endif
61#if defined(__GNUC__)
62#pragma GCC diagnostic warning "-Wwrite-strings"
63#endif
64#include "vk_struct_size_helper.h"
Tobin Ehlis5b5e7bc2016-03-09 16:12:48 -070065#include "core_validation.h"
Tobin Ehlis5b5e7bc2016-03-09 16:12:48 -070066#include "vk_layer_config.h"
67#include "vk_layer_table.h"
68#include "vk_layer_data.h"
69#include "vk_layer_logging.h"
70#include "vk_layer_extension_utils.h"
71#include "vk_layer_utils.h"
72
73#if defined __ANDROID__
74#include <android/log.h>
75#define LOGCONSOLE(...) ((void)__android_log_print(ANDROID_LOG_INFO, "DS", __VA_ARGS__))
76#else
77#define LOGCONSOLE(...) printf(__VA_ARGS__)
78#endif
79
80using std::unordered_map;
81using std::unordered_set;
82
Tobin Ehlis5b5e7bc2016-03-09 16:12:48 -070083// WSI Image Objects bypass usual Image Object creation methods. A special Memory
84// Object value will be used to identify them internally.
85static const VkDeviceMemory MEMTRACKER_SWAP_CHAIN_IMAGE_KEY = (VkDeviceMemory)(-1);
Tobin Ehlisb1b606d2016-04-11 14:49:55 -060086
Tobin Ehlis5b5e7bc2016-03-09 16:12:48 -070087// Track command pools and their command buffers
88struct CMD_POOL_INFO {
89 VkCommandPoolCreateFlags createFlags;
90 uint32_t queueFamilyIndex;
91 list<VkCommandBuffer> commandBuffers; // list container of cmd buffers allocated from this pool
92};
93
94struct devExts {
Dustin Gravese3319182016-04-05 09:41:17 -060095 bool wsi_enabled;
Tobin Ehlis5b5e7bc2016-03-09 16:12:48 -070096 unordered_map<VkSwapchainKHR, SWAPCHAIN_NODE *> swapchainMap;
97 unordered_map<VkImage, VkSwapchainKHR> imageToSwapchainMap;
98};
99
100// fwd decls
101struct shader_module;
Tobin Ehlis5b5e7bc2016-03-09 16:12:48 -0700102
Tobin Ehlisb1b606d2016-04-11 14:49:55 -0600103// TODO : Split this into separate structs for instance and device level data?
Tobin Ehlis5b5e7bc2016-03-09 16:12:48 -0700104struct layer_data {
105 debug_report_data *report_data;
106 std::vector<VkDebugReportCallbackEXT> logging_callback;
107 VkLayerDispatchTable *device_dispatch_table;
108 VkLayerInstanceDispatchTable *instance_dispatch_table;
Tobin Ehlis94c53c02016-04-05 13:33:00 -0600109
Tobin Ehlis5b5e7bc2016-03-09 16:12:48 -0700110 devExts device_extensions;
Tobin Ehlis94c53c02016-04-05 13:33:00 -0600111 uint64_t currentFenceId;
Mark Lobodzinskib376eda2016-03-29 09:49:15 -0600112 unordered_set<VkQueue> queues; // all queues under given device
Tobin Ehlis5b5e7bc2016-03-09 16:12:48 -0700113 // Global set of all cmdBuffers that are inFlight on this device
114 unordered_set<VkCommandBuffer> globalInFlightCmdBuffers;
115 // Layer specific data
116 unordered_map<VkSampler, unique_ptr<SAMPLER_NODE>> sampleMap;
117 unordered_map<VkImageView, VkImageViewCreateInfo> imageViewMap;
118 unordered_map<VkImage, IMAGE_NODE> imageMap;
119 unordered_map<VkBufferView, VkBufferViewCreateInfo> bufferViewMap;
120 unordered_map<VkBuffer, BUFFER_NODE> bufferMap;
121 unordered_map<VkPipeline, PIPELINE_NODE *> pipelineMap;
122 unordered_map<VkCommandPool, CMD_POOL_INFO> commandPoolMap;
123 unordered_map<VkDescriptorPool, DESCRIPTOR_POOL_NODE *> descriptorPoolMap;
124 unordered_map<VkDescriptorSet, SET_NODE *> setMap;
125 unordered_map<VkDescriptorSetLayout, LAYOUT_NODE *> descriptorSetLayoutMap;
126 unordered_map<VkPipelineLayout, PIPELINE_LAYOUT_NODE> pipelineLayoutMap;
Tobin Ehlis58070a62016-03-16 16:00:36 -0600127 unordered_map<VkDeviceMemory, DEVICE_MEM_INFO> memObjMap;
Tobin Ehlis5b5e7bc2016-03-09 16:12:48 -0700128 unordered_map<VkFence, FENCE_NODE> fenceMap;
129 unordered_map<VkQueue, QUEUE_NODE> queueMap;
130 unordered_map<VkEvent, EVENT_NODE> eventMap;
131 unordered_map<QueryObject, bool> queryToStateMap;
132 unordered_map<VkQueryPool, QUERY_POOL_NODE> queryPoolMap;
133 unordered_map<VkSemaphore, SEMAPHORE_NODE> semaphoreMap;
Tobin Ehlis72d66f02016-03-21 14:14:44 -0600134 unordered_map<VkCommandBuffer, GLOBAL_CB_NODE *> commandBufferMap;
Tobin Ehlis5b5e7bc2016-03-09 16:12:48 -0700135 unordered_map<VkFramebuffer, FRAMEBUFFER_NODE> frameBufferMap;
136 unordered_map<VkImage, vector<ImageSubresourcePair>> imageSubresourceMap;
137 unordered_map<ImageSubresourcePair, IMAGE_LAYOUT_NODE> imageLayoutMap;
138 unordered_map<VkRenderPass, RENDER_PASS_NODE *> renderPassMap;
Chris Forbes918c2832016-03-18 16:30:03 +1300139 unordered_map<VkShaderModule, unique_ptr<shader_module>> shaderModuleMap;
Chris Forbes6c94fb92016-03-30 11:35:21 +1300140 VkDevice device;
Tobin Ehlis5b5e7bc2016-03-09 16:12:48 -0700141
142 // Device specific data
Tobin Ehlisb1b606d2016-04-11 14:49:55 -0600143 PHYS_DEV_PROPERTIES_NODE phys_dev_properties;
144 VkPhysicalDeviceMemoryProperties phys_dev_mem_props;
145
Tobin Ehlis5b5e7bc2016-03-09 16:12:48 -0700146 layer_data()
Tobin Ehlis94c53c02016-04-05 13:33:00 -0600147 : report_data(nullptr), device_dispatch_table(nullptr), instance_dispatch_table(nullptr), device_extensions(),
Chris Forbesdcf917f2016-04-13 16:56:05 +1200148 currentFenceId(1), device(VK_NULL_HANDLE), phys_dev_properties{},
Tobin Ehlisb1b606d2016-04-11 14:49:55 -0600149 phys_dev_mem_props{} {};
Tobin Ehlis5b5e7bc2016-03-09 16:12:48 -0700150};
151
Tobin Ehlisb1b606d2016-04-11 14:49:55 -0600152// TODO : Do we need to guard access to layer_data_map w/ lock?
153static unordered_map<void *, layer_data *> layer_data_map;
154
Tobin Ehlis5b5e7bc2016-03-09 16:12:48 -0700155static const VkLayerProperties cv_global_layers[] = {{
Jon Ashburnf1ea4182016-03-22 12:57:13 -0600156 "VK_LAYER_LUNARG_core_validation", VK_LAYER_API_VERSION, 1, "LunarG Validation Layer",
Tobin Ehlis5b5e7bc2016-03-09 16:12:48 -0700157}};
158
159template <class TCreateInfo> void ValidateLayerOrdering(const TCreateInfo &createInfo) {
160 bool foundLayer = false;
161 for (uint32_t i = 0; i < createInfo.enabledLayerCount; ++i) {
162 if (!strcmp(createInfo.ppEnabledLayerNames[i], cv_global_layers[0].layerName)) {
163 foundLayer = true;
164 }
165 // This has to be logged to console as we don't have a callback at this point.
166 if (!foundLayer && !strcmp(createInfo.ppEnabledLayerNames[0], "VK_LAYER_GOOGLE_unique_objects")) {
167 LOGCONSOLE("Cannot activate layer VK_LAYER_GOOGLE_unique_objects prior to activating %s.",
168 cv_global_layers[0].layerName);
169 }
170 }
171}
172
173// Code imported from shader_checker
174static void build_def_index(shader_module *);
175
176// A forward iterator over spirv instructions. Provides easy access to len, opcode, and content words
177// without the caller needing to care too much about the physical SPIRV module layout.
178struct spirv_inst_iter {
179 std::vector<uint32_t>::const_iterator zero;
180 std::vector<uint32_t>::const_iterator it;
181
182 uint32_t len() { return *it >> 16; }
183 uint32_t opcode() { return *it & 0x0ffffu; }
184 uint32_t const &word(unsigned n) { return it[n]; }
185 uint32_t offset() { return (uint32_t)(it - zero); }
186
187 spirv_inst_iter() {}
188
189 spirv_inst_iter(std::vector<uint32_t>::const_iterator zero, std::vector<uint32_t>::const_iterator it) : zero(zero), it(it) {}
190
191 bool operator==(spirv_inst_iter const &other) { return it == other.it; }
192
193 bool operator!=(spirv_inst_iter const &other) { return it != other.it; }
194
195 spirv_inst_iter operator++(int) { /* x++ */
196 spirv_inst_iter ii = *this;
197 it += len();
198 return ii;
199 }
200
201 spirv_inst_iter operator++() { /* ++x; */
202 it += len();
203 return *this;
204 }
205
206 /* The iterator and the value are the same thing. */
207 spirv_inst_iter &operator*() { return *this; }
208 spirv_inst_iter const &operator*() const { return *this; }
209};
210
211struct shader_module {
212 /* the spirv image itself */
213 vector<uint32_t> words;
214 /* a mapping of <id> to the first word of its def. this is useful because walking type
215 * trees, constant expressions, etc requires jumping all over the instruction stream.
216 */
217 unordered_map<unsigned, unsigned> def_index;
218
219 shader_module(VkShaderModuleCreateInfo const *pCreateInfo)
220 : words((uint32_t *)pCreateInfo->pCode, (uint32_t *)pCreateInfo->pCode + pCreateInfo->codeSize / sizeof(uint32_t)),
221 def_index() {
222
223 build_def_index(this);
224 }
225
226 /* expose begin() / end() to enable range-based for */
227 spirv_inst_iter begin() const { return spirv_inst_iter(words.begin(), words.begin() + 5); } /* first insn */
228 spirv_inst_iter end() const { return spirv_inst_iter(words.begin(), words.end()); } /* just past last insn */
229 /* given an offset into the module, produce an iterator there. */
230 spirv_inst_iter at(unsigned offset) const { return spirv_inst_iter(words.begin(), words.begin() + offset); }
231
232 /* gets an iterator to the definition of an id */
233 spirv_inst_iter get_def(unsigned id) const {
234 auto it = def_index.find(id);
235 if (it == def_index.end()) {
236 return end();
237 }
238 return at(it->second);
239 }
240};
241
Tobin Ehlis5b5e7bc2016-03-09 16:12:48 -0700242// TODO : This can be much smarter, using separate locks for separate global data
Jeremy Hayesb9e99232016-04-13 16:20:24 -0600243static std::mutex global_lock;
Mark Lobodzinski03b71512016-03-23 14:33:02 -0600244#if MTMERGESOURCE
245// MTMERGESOURCE - start of direct pull
Tobin Ehlis94c53c02016-04-05 13:33:00 -0600246static VkDeviceMemory *get_object_mem_binding(layer_data *my_data, uint64_t handle, VkDebugReportObjectTypeEXT type) {
Tobin Ehlis5b5e7bc2016-03-09 16:12:48 -0700247 switch (type) {
248 case VK_DEBUG_REPORT_OBJECT_TYPE_IMAGE_EXT: {
Tobin Ehlis94c53c02016-04-05 13:33:00 -0600249 auto it = my_data->imageMap.find(VkImage(handle));
250 if (it != my_data->imageMap.end())
251 return &(*it).second.mem;
Tobin Ehlis5b5e7bc2016-03-09 16:12:48 -0700252 break;
253 }
254 case VK_DEBUG_REPORT_OBJECT_TYPE_BUFFER_EXT: {
Tobin Ehlis94c53c02016-04-05 13:33:00 -0600255 auto it = my_data->bufferMap.find(VkBuffer(handle));
256 if (it != my_data->bufferMap.end())
257 return &(*it).second.mem;
Tobin Ehlis5b5e7bc2016-03-09 16:12:48 -0700258 break;
259 }
260 default:
261 break;
262 }
Tobin Ehlis94c53c02016-04-05 13:33:00 -0600263 return nullptr;
Tobin Ehlis5b5e7bc2016-03-09 16:12:48 -0700264}
Mark Lobodzinski03b71512016-03-23 14:33:02 -0600265// MTMERGESOURCE - end section
Tobin Ehlis5b5e7bc2016-03-09 16:12:48 -0700266#endif
267template layer_data *get_my_data_ptr<layer_data>(void *data_key, std::unordered_map<void *, layer_data *> &data_map);
268
Tobin Ehlis72d66f02016-03-21 14:14:44 -0600269// prototype
270static GLOBAL_CB_NODE *getCBNode(layer_data *, const VkCommandBuffer);
271
Mark Lobodzinski03b71512016-03-23 14:33:02 -0600272#if MTMERGESOURCE
Tobin Ehlis5b5e7bc2016-03-09 16:12:48 -0700273// Add a fence, creating one if necessary to our list of fences/fenceIds
Dustin Gravese3319182016-04-05 09:41:17 -0600274static bool add_fence_info(layer_data *my_data, VkFence fence, VkQueue queue, uint64_t *fenceId) {
275 bool skipCall = false;
Tobin Ehlis5b5e7bc2016-03-09 16:12:48 -0700276 *fenceId = my_data->currentFenceId++;
277
278 // If no fence, create an internal fence to track the submissions
279 if (fence != VK_NULL_HANDLE) {
280 my_data->fenceMap[fence].fenceId = *fenceId;
281 my_data->fenceMap[fence].queue = queue;
282 // Validate that fence is in UNSIGNALED state
283 VkFenceCreateInfo *pFenceCI = &(my_data->fenceMap[fence].createInfo);
284 if (pFenceCI->flags & VK_FENCE_CREATE_SIGNALED_BIT) {
285 skipCall = log_msg(my_data->report_data, VK_DEBUG_REPORT_ERROR_BIT_EXT, VK_DEBUG_REPORT_OBJECT_TYPE_FENCE_EXT,
286 (uint64_t)fence, __LINE__, MEMTRACK_INVALID_FENCE_STATE, "MEM",
287 "Fence %#" PRIxLEAST64 " submitted in SIGNALED state. Fences must be reset before being submitted",
288 (uint64_t)fence);
289 }
290 } else {
291 // TODO : Do we need to create an internal fence here for tracking purposes?
292 }
293 // Update most recently submitted fence and fenceId for Queue
294 my_data->queueMap[queue].lastSubmittedId = *fenceId;
295 return skipCall;
296}
297
Tobin Ehlis5b5e7bc2016-03-09 16:12:48 -0700298// Record information when a fence is known to be signalled
299static void update_fence_tracking(layer_data *my_data, VkFence fence) {
300 auto fence_item = my_data->fenceMap.find(fence);
301 if (fence_item != my_data->fenceMap.end()) {
302 FENCE_NODE *pCurFenceInfo = &(*fence_item).second;
303 VkQueue queue = pCurFenceInfo->queue;
304 auto queue_item = my_data->queueMap.find(queue);
305 if (queue_item != my_data->queueMap.end()) {
306 QUEUE_NODE *pQueueInfo = &(*queue_item).second;
307 if (pQueueInfo->lastRetiredId < pCurFenceInfo->fenceId) {
308 pQueueInfo->lastRetiredId = pCurFenceInfo->fenceId;
309 }
310 }
311 }
312
313 // Update fence state in fenceCreateInfo structure
314 auto pFCI = &(my_data->fenceMap[fence].createInfo);
315 pFCI->flags = static_cast<VkFenceCreateFlags>(pFCI->flags | VK_FENCE_CREATE_SIGNALED_BIT);
316}
317
318// Helper routine that updates the fence list for a specific queue to all-retired
319static void retire_queue_fences(layer_data *my_data, VkQueue queue) {
320 QUEUE_NODE *pQueueInfo = &my_data->queueMap[queue];
321 // Set queue's lastRetired to lastSubmitted indicating all fences completed
322 pQueueInfo->lastRetiredId = pQueueInfo->lastSubmittedId;
323}
324
325// Helper routine that updates all queues to all-retired
326static void retire_device_fences(layer_data *my_data, VkDevice device) {
327 // Process each queue for device
328 // TODO: Add multiple device support
329 for (auto ii = my_data->queueMap.begin(); ii != my_data->queueMap.end(); ++ii) {
330 // Set queue's lastRetired to lastSubmitted indicating all fences completed
331 QUEUE_NODE *pQueueInfo = &(*ii).second;
332 pQueueInfo->lastRetiredId = pQueueInfo->lastSubmittedId;
333 }
334}
335
336// Helper function to validate correct usage bits set for buffers or images
337// Verify that (actual & desired) flags != 0 or,
338// if strict is true, verify that (actual & desired) flags == desired
339// In case of error, report it via dbg callbacks
Chris Forbes0a1ce3d2016-04-06 15:16:26 +1200340static bool validate_usage_flags(layer_data *my_data, VkFlags actual, VkFlags desired, VkBool32 strict,
341 uint64_t obj_handle, VkDebugReportObjectTypeEXT obj_type, char const *ty_str,
342 char const *func_name, char const *usage_str) {
Dustin Gravese3319182016-04-05 09:41:17 -0600343 bool correct_usage = false;
344 bool skipCall = false;
Tobin Ehlis5b5e7bc2016-03-09 16:12:48 -0700345 if (strict)
346 correct_usage = ((actual & desired) == desired);
347 else
348 correct_usage = ((actual & desired) != 0);
349 if (!correct_usage) {
350 skipCall = log_msg(my_data->report_data, VK_DEBUG_REPORT_ERROR_BIT_EXT, obj_type, obj_handle, __LINE__,
351 MEMTRACK_INVALID_USAGE_FLAG, "MEM", "Invalid usage flag for %s %#" PRIxLEAST64
352 " used by %s. In this case, %s should have %s set during creation.",
353 ty_str, obj_handle, func_name, ty_str, usage_str);
354 }
355 return skipCall;
356}
357
358// Helper function to validate usage flags for images
359// Pulls image info and then sends actual vs. desired usage off to helper above where
360// an error will be flagged if usage is not correct
Chris Forbes0a1ce3d2016-04-06 15:16:26 +1200361static bool validate_image_usage_flags(layer_data *dev_data, VkImage image, VkFlags desired, VkBool32 strict,
Tobin Ehlis94c53c02016-04-05 13:33:00 -0600362 char const *func_name, char const *usage_string) {
Dustin Gravese3319182016-04-05 09:41:17 -0600363 bool skipCall = false;
Tobin Ehlis94c53c02016-04-05 13:33:00 -0600364 auto const image_node = dev_data->imageMap.find(image);
365 if (image_node != dev_data->imageMap.end()) {
Chris Forbes0a1ce3d2016-04-06 15:16:26 +1200366 skipCall = validate_usage_flags(dev_data, image_node->second.createInfo.usage, desired, strict, (uint64_t)image,
Tobin Ehlis5b5e7bc2016-03-09 16:12:48 -0700367 VK_DEBUG_REPORT_OBJECT_TYPE_IMAGE_EXT, "image", func_name, usage_string);
368 }
369 return skipCall;
370}
371
372// Helper function to validate usage flags for buffers
373// Pulls buffer info and then sends actual vs. desired usage off to helper above where
374// an error will be flagged if usage is not correct
Chris Forbes0a1ce3d2016-04-06 15:16:26 +1200375static bool validate_buffer_usage_flags(layer_data *dev_data, VkBuffer buffer, VkFlags desired, VkBool32 strict,
Tobin Ehlis94c53c02016-04-05 13:33:00 -0600376 char const *func_name, char const *usage_string) {
Dustin Gravese3319182016-04-05 09:41:17 -0600377 bool skipCall = false;
Tobin Ehlis94c53c02016-04-05 13:33:00 -0600378 auto const buffer_node = dev_data->bufferMap.find(buffer);
379 if (buffer_node != dev_data->bufferMap.end()) {
Chris Forbes0a1ce3d2016-04-06 15:16:26 +1200380 skipCall = validate_usage_flags(dev_data, buffer_node->second.createInfo.usage, desired, strict, (uint64_t)buffer,
Tobin Ehlis5b5e7bc2016-03-09 16:12:48 -0700381 VK_DEBUG_REPORT_OBJECT_TYPE_BUFFER_EXT, "buffer", func_name, usage_string);
382 }
383 return skipCall;
384}
385
386// Return ptr to info in map container containing mem, or NULL if not found
387// Calls to this function should be wrapped in mutex
Tobin Ehlis58070a62016-03-16 16:00:36 -0600388static DEVICE_MEM_INFO *get_mem_obj_info(layer_data *dev_data, const VkDeviceMemory mem) {
389 auto item = dev_data->memObjMap.find(mem);
390 if (item != dev_data->memObjMap.end()) {
Tobin Ehlis5b5e7bc2016-03-09 16:12:48 -0700391 return &(*item).second;
392 } else {
393 return NULL;
394 }
395}
396
397static void add_mem_obj_info(layer_data *my_data, void *object, const VkDeviceMemory mem,
398 const VkMemoryAllocateInfo *pAllocateInfo) {
399 assert(object != NULL);
400
401 memcpy(&my_data->memObjMap[mem].allocInfo, pAllocateInfo, sizeof(VkMemoryAllocateInfo));
402 // TODO: Update for real hardware, actually process allocation info structures
403 my_data->memObjMap[mem].allocInfo.pNext = NULL;
404 my_data->memObjMap[mem].object = object;
Tobin Ehlis5b5e7bc2016-03-09 16:12:48 -0700405 my_data->memObjMap[mem].mem = mem;
Tobin Ehlis58070a62016-03-16 16:00:36 -0600406 my_data->memObjMap[mem].image = VK_NULL_HANDLE;
Tobin Ehlis5b5e7bc2016-03-09 16:12:48 -0700407 my_data->memObjMap[mem].memRange.offset = 0;
408 my_data->memObjMap[mem].memRange.size = 0;
409 my_data->memObjMap[mem].pData = 0;
410 my_data->memObjMap[mem].pDriverData = 0;
411 my_data->memObjMap[mem].valid = false;
412}
413
Dustin Gravese3319182016-04-05 09:41:17 -0600414static bool validate_memory_is_valid(layer_data *dev_data, VkDeviceMemory mem, const char *functionName,
415 VkImage image = VK_NULL_HANDLE) {
Tobin Ehlis5b5e7bc2016-03-09 16:12:48 -0700416 if (mem == MEMTRACKER_SWAP_CHAIN_IMAGE_KEY) {
Tobin Ehlis94c53c02016-04-05 13:33:00 -0600417 auto const image_node = dev_data->imageMap.find(image);
418 if (image_node != dev_data->imageMap.end() && !image_node->second.valid) {
Tobin Ehlis58070a62016-03-16 16:00:36 -0600419 return log_msg(dev_data->report_data, VK_DEBUG_REPORT_ERROR_BIT_EXT, VK_DEBUG_REPORT_OBJECT_TYPE_DEVICE_MEMORY_EXT,
Tobin Ehlis5b5e7bc2016-03-09 16:12:48 -0700420 (uint64_t)(mem), __LINE__, MEMTRACK_INVALID_USAGE_FLAG, "MEM",
421 "%s: Cannot read invalid swapchain image %" PRIx64 ", please fill the memory before using.",
422 functionName, (uint64_t)(image));
423 }
424 } else {
Tobin Ehlis58070a62016-03-16 16:00:36 -0600425 DEVICE_MEM_INFO *pMemObj = get_mem_obj_info(dev_data, mem);
Tobin Ehlis5b5e7bc2016-03-09 16:12:48 -0700426 if (pMemObj && !pMemObj->valid) {
Tobin Ehlis58070a62016-03-16 16:00:36 -0600427 return log_msg(dev_data->report_data, VK_DEBUG_REPORT_ERROR_BIT_EXT, VK_DEBUG_REPORT_OBJECT_TYPE_DEVICE_MEMORY_EXT,
Tobin Ehlis5b5e7bc2016-03-09 16:12:48 -0700428 (uint64_t)(mem), __LINE__, MEMTRACK_INVALID_USAGE_FLAG, "MEM",
429 "%s: Cannot read invalid memory %" PRIx64 ", please fill the memory before using.", functionName,
430 (uint64_t)(mem));
431 }
432 }
433 return false;
434}
435
Tobin Ehlis58070a62016-03-16 16:00:36 -0600436static void set_memory_valid(layer_data *dev_data, VkDeviceMemory mem, bool valid, VkImage image = VK_NULL_HANDLE) {
Tobin Ehlis5b5e7bc2016-03-09 16:12:48 -0700437 if (mem == MEMTRACKER_SWAP_CHAIN_IMAGE_KEY) {
Tobin Ehlis94c53c02016-04-05 13:33:00 -0600438 auto image_node = dev_data->imageMap.find(image);
439 if (image_node != dev_data->imageMap.end()) {
440 image_node->second.valid = valid;
Tobin Ehlis5b5e7bc2016-03-09 16:12:48 -0700441 }
442 } else {
Tobin Ehlis58070a62016-03-16 16:00:36 -0600443 DEVICE_MEM_INFO *pMemObj = get_mem_obj_info(dev_data, mem);
Tobin Ehlis5b5e7bc2016-03-09 16:12:48 -0700444 if (pMemObj) {
445 pMemObj->valid = valid;
446 }
447 }
448}
449
450// Find CB Info and add mem reference to list container
451// Find Mem Obj Info and add CB reference to list container
Dustin Gravese3319182016-04-05 09:41:17 -0600452static bool update_cmd_buf_and_mem_references(layer_data *dev_data, const VkCommandBuffer cb, const VkDeviceMemory mem,
453 const char *apiName) {
454 bool skipCall = false;
Tobin Ehlis5b5e7bc2016-03-09 16:12:48 -0700455
456 // Skip validation if this image was created through WSI
457 if (mem != MEMTRACKER_SWAP_CHAIN_IMAGE_KEY) {
458
459 // First update CB binding in MemObj mini CB list
Tobin Ehlis58070a62016-03-16 16:00:36 -0600460 DEVICE_MEM_INFO *pMemInfo = get_mem_obj_info(dev_data, mem);
Tobin Ehlis5b5e7bc2016-03-09 16:12:48 -0700461 if (pMemInfo) {
Chris Forbesde886ba2016-03-31 17:58:13 +1300462 pMemInfo->commandBufferBindings.insert(cb);
Tobin Ehlis5b5e7bc2016-03-09 16:12:48 -0700463 // Now update CBInfo's Mem reference list
Tobin Ehlis72d66f02016-03-21 14:14:44 -0600464 GLOBAL_CB_NODE *pCBNode = getCBNode(dev_data, cb);
Tobin Ehlis5b5e7bc2016-03-09 16:12:48 -0700465 // TODO: keep track of all destroyed CBs so we know if this is a stale or simply invalid object
Tobin Ehlis72d66f02016-03-21 14:14:44 -0600466 if (pCBNode) {
Chris Forbesc7196ad2016-03-31 18:11:28 +1300467 pCBNode->memObjs.insert(mem);
Tobin Ehlis5b5e7bc2016-03-09 16:12:48 -0700468 }
469 }
470 }
471 return skipCall;
472}
Tobin Ehlis400cff92016-04-11 16:39:29 -0600473// For every mem obj bound to particular CB, free bindings related to that CB
474static void clear_cmd_buf_and_mem_references(layer_data *dev_data, GLOBAL_CB_NODE *pCBNode) {
Tobin Ehlis72d66f02016-03-21 14:14:44 -0600475 if (pCBNode) {
Chris Forbesc7196ad2016-03-31 18:11:28 +1300476 if (pCBNode->memObjs.size() > 0) {
477 for (auto mem : pCBNode->memObjs) {
Chris Forbes20b3e282016-03-31 18:03:56 +1300478 DEVICE_MEM_INFO *pInfo = get_mem_obj_info(dev_data, mem);
Tobin Ehlis5b5e7bc2016-03-09 16:12:48 -0700479 if (pInfo) {
Tobin Ehlis400cff92016-04-11 16:39:29 -0600480 pInfo->commandBufferBindings.erase(pCBNode->commandBuffer);
Tobin Ehlis5b5e7bc2016-03-09 16:12:48 -0700481 }
482 }
Chris Forbesc7196ad2016-03-31 18:11:28 +1300483 pCBNode->memObjs.clear();
Tobin Ehlis5b5e7bc2016-03-09 16:12:48 -0700484 }
Tobin Ehlis72d66f02016-03-21 14:14:44 -0600485 pCBNode->validate_functions.clear();
Tobin Ehlis5b5e7bc2016-03-09 16:12:48 -0700486 }
Tobin Ehlis5b5e7bc2016-03-09 16:12:48 -0700487}
Tobin Ehlis400cff92016-04-11 16:39:29 -0600488// Overloaded call to above function when GLOBAL_CB_NODE has not already been looked-up
489static void clear_cmd_buf_and_mem_references(layer_data *dev_data, const VkCommandBuffer cb) {
490 clear_cmd_buf_and_mem_references(dev_data, getCBNode(dev_data, cb));
Tobin Ehlis5b5e7bc2016-03-09 16:12:48 -0700491}
492
493// For given MemObjInfo, report Obj & CB bindings
Dustin Gravese3319182016-04-05 09:41:17 -0600494static bool reportMemReferencesAndCleanUp(layer_data *dev_data, DEVICE_MEM_INFO *pMemObjInfo) {
495 bool skipCall = false;
Chris Forbesde886ba2016-03-31 17:58:13 +1300496 size_t cmdBufRefCount = pMemObjInfo->commandBufferBindings.size();
Chris Forbes896fc322016-03-31 17:37:36 +1300497 size_t objRefCount = pMemObjInfo->objBindings.size();
Tobin Ehlis5b5e7bc2016-03-09 16:12:48 -0700498
Chris Forbesde886ba2016-03-31 17:58:13 +1300499 if ((pMemObjInfo->commandBufferBindings.size()) != 0) {
Tobin Ehlis58070a62016-03-16 16:00:36 -0600500 skipCall = log_msg(dev_data->report_data, VK_DEBUG_REPORT_ERROR_BIT_EXT, VK_DEBUG_REPORT_OBJECT_TYPE_DEVICE_MEMORY_EXT,
Tobin Ehlis5b5e7bc2016-03-09 16:12:48 -0700501 (uint64_t)pMemObjInfo->mem, __LINE__, MEMTRACK_FREED_MEM_REF, "MEM",
502 "Attempting to free memory object %#" PRIxLEAST64 " which still contains " PRINTF_SIZE_T_SPECIFIER
503 " references",
504 (uint64_t)pMemObjInfo->mem, (cmdBufRefCount + objRefCount));
505 }
506
Chris Forbesde886ba2016-03-31 17:58:13 +1300507 if (cmdBufRefCount > 0 && pMemObjInfo->commandBufferBindings.size() > 0) {
508 for (auto cb : pMemObjInfo->commandBufferBindings) {
Tobin Ehlis58070a62016-03-16 16:00:36 -0600509 log_msg(dev_data->report_data, VK_DEBUG_REPORT_INFORMATION_BIT_EXT, VK_DEBUG_REPORT_OBJECT_TYPE_COMMAND_BUFFER_EXT,
Chris Forbesde886ba2016-03-31 17:58:13 +1300510 (uint64_t)cb, __LINE__, MEMTRACK_FREED_MEM_REF, "MEM",
511 "Command Buffer %p still has a reference to mem obj %#" PRIxLEAST64, cb, (uint64_t)pMemObjInfo->mem);
Tobin Ehlis5b5e7bc2016-03-09 16:12:48 -0700512 }
513 // Clear the list of hanging references
Chris Forbesde886ba2016-03-31 17:58:13 +1300514 pMemObjInfo->commandBufferBindings.clear();
Tobin Ehlis5b5e7bc2016-03-09 16:12:48 -0700515 }
516
Chris Forbes896fc322016-03-31 17:37:36 +1300517 if (objRefCount > 0 && pMemObjInfo->objBindings.size() > 0) {
518 for (auto obj : pMemObjInfo->objBindings) {
519 log_msg(dev_data->report_data, VK_DEBUG_REPORT_INFORMATION_BIT_EXT, obj.type, obj.handle, __LINE__,
Tobin Ehlis5b5e7bc2016-03-09 16:12:48 -0700520 MEMTRACK_FREED_MEM_REF, "MEM", "VK Object %#" PRIxLEAST64 " still has a reference to mem obj %#" PRIxLEAST64,
Chris Forbes896fc322016-03-31 17:37:36 +1300521 obj.handle, (uint64_t)pMemObjInfo->mem);
Tobin Ehlis5b5e7bc2016-03-09 16:12:48 -0700522 }
523 // Clear the list of hanging references
Chris Forbes896fc322016-03-31 17:37:36 +1300524 pMemObjInfo->objBindings.clear();
Tobin Ehlis5b5e7bc2016-03-09 16:12:48 -0700525 }
526 return skipCall;
527}
528
Dustin Gravese3319182016-04-05 09:41:17 -0600529static bool deleteMemObjInfo(layer_data *my_data, void *object, VkDeviceMemory mem) {
530 bool skipCall = false;
Tobin Ehlis5b5e7bc2016-03-09 16:12:48 -0700531 auto item = my_data->memObjMap.find(mem);
532 if (item != my_data->memObjMap.end()) {
533 my_data->memObjMap.erase(item);
534 } else {
535 skipCall = log_msg(my_data->report_data, VK_DEBUG_REPORT_ERROR_BIT_EXT, VK_DEBUG_REPORT_OBJECT_TYPE_DEVICE_MEMORY_EXT,
536 (uint64_t)mem, __LINE__, MEMTRACK_INVALID_MEM_OBJ, "MEM",
537 "Request to delete memory object %#" PRIxLEAST64 " not present in memory Object Map", (uint64_t)mem);
538 }
539 return skipCall;
540}
541
542// Check if fence for given CB is completed
543static bool checkCBCompleted(layer_data *my_data, const VkCommandBuffer cb, bool *complete) {
Tobin Ehlis72d66f02016-03-21 14:14:44 -0600544 GLOBAL_CB_NODE *pCBNode = getCBNode(my_data, cb);
Dustin Gravese3319182016-04-05 09:41:17 -0600545 bool skipCall = false;
Tobin Ehlis5b5e7bc2016-03-09 16:12:48 -0700546 *complete = true;
547
Tobin Ehlis72d66f02016-03-21 14:14:44 -0600548 if (pCBNode) {
549 if (pCBNode->lastSubmittedQueue != NULL) {
550 VkQueue queue = pCBNode->lastSubmittedQueue;
Tobin Ehlis5b5e7bc2016-03-09 16:12:48 -0700551 QUEUE_NODE *pQueueInfo = &my_data->queueMap[queue];
Tobin Ehlis72d66f02016-03-21 14:14:44 -0600552 if (pCBNode->fenceId > pQueueInfo->lastRetiredId) {
Tobin Ehlis5b5e7bc2016-03-09 16:12:48 -0700553 skipCall = log_msg(my_data->report_data, VK_DEBUG_REPORT_INFORMATION_BIT_EXT,
554 VK_DEBUG_REPORT_OBJECT_TYPE_COMMAND_BUFFER_EXT, (uint64_t)cb, __LINE__, MEMTRACK_NONE, "MEM",
555 "fence %#" PRIxLEAST64 " for CB %p has not been checked for completion",
Tobin Ehlis72d66f02016-03-21 14:14:44 -0600556 (uint64_t)pCBNode->lastSubmittedFence, cb);
Tobin Ehlis5b5e7bc2016-03-09 16:12:48 -0700557 *complete = false;
558 }
559 }
560 }
561 return skipCall;
562}
563
Dustin Gravese3319182016-04-05 09:41:17 -0600564static bool freeMemObjInfo(layer_data *dev_data, void *object, VkDeviceMemory mem, bool internal) {
565 bool skipCall = false;
Tobin Ehlis5b5e7bc2016-03-09 16:12:48 -0700566 // Parse global list to find info w/ mem
Tobin Ehlis58070a62016-03-16 16:00:36 -0600567 DEVICE_MEM_INFO *pInfo = get_mem_obj_info(dev_data, mem);
Tobin Ehlis5b5e7bc2016-03-09 16:12:48 -0700568 if (pInfo) {
569 if (pInfo->allocInfo.allocationSize == 0 && !internal) {
570 // TODO: Verify against Valid Use section
Tobin Ehlis58070a62016-03-16 16:00:36 -0600571 skipCall = log_msg(dev_data->report_data, VK_DEBUG_REPORT_ERROR_BIT_EXT, VK_DEBUG_REPORT_OBJECT_TYPE_DEVICE_MEMORY_EXT,
Tobin Ehlis5b5e7bc2016-03-09 16:12:48 -0700572 (uint64_t)mem, __LINE__, MEMTRACK_INVALID_MEM_OBJ, "MEM",
573 "Attempting to free memory associated with a Persistent Image, %#" PRIxLEAST64 ", "
574 "this should not be explicitly freed\n",
575 (uint64_t)mem);
576 } else {
577 // Clear any CB bindings for completed CBs
578 // TODO : Is there a better place to do this?
579
Tobin Ehlis5b5e7bc2016-03-09 16:12:48 -0700580 assert(pInfo->object != VK_NULL_HANDLE);
Chris Forbesde886ba2016-03-31 17:58:13 +1300581 // clear_cmd_buf_and_mem_references removes elements from
582 // pInfo->commandBufferBindings -- this copy not needed in c++14,
583 // and probably not needed in practice in c++11
584 auto bindings = pInfo->commandBufferBindings;
585 for (auto cb : bindings) {
586 bool commandBufferComplete = false;
587 skipCall |= checkCBCompleted(dev_data, cb, &commandBufferComplete);
Tobin Ehlis5b5e7bc2016-03-09 16:12:48 -0700588 if (commandBufferComplete) {
Chris Forbesde886ba2016-03-31 17:58:13 +1300589 clear_cmd_buf_and_mem_references(dev_data, cb);
Tobin Ehlis5b5e7bc2016-03-09 16:12:48 -0700590 }
591 }
592
593 // Now verify that no references to this mem obj remain and remove bindings
Chris Forbesde886ba2016-03-31 17:58:13 +1300594 if (pInfo->commandBufferBindings.size() || pInfo->objBindings.size()) {
Tobin Ehlis58070a62016-03-16 16:00:36 -0600595 skipCall |= reportMemReferencesAndCleanUp(dev_data, pInfo);
Tobin Ehlis5b5e7bc2016-03-09 16:12:48 -0700596 }
597 // Delete mem obj info
Tobin Ehlis58070a62016-03-16 16:00:36 -0600598 skipCall |= deleteMemObjInfo(dev_data, object, mem);
Tobin Ehlis5b5e7bc2016-03-09 16:12:48 -0700599 }
600 }
601 return skipCall;
602}
603
604static const char *object_type_to_string(VkDebugReportObjectTypeEXT type) {
605 switch (type) {
606 case VK_DEBUG_REPORT_OBJECT_TYPE_IMAGE_EXT:
607 return "image";
Tobin Ehlis5b5e7bc2016-03-09 16:12:48 -0700608 case VK_DEBUG_REPORT_OBJECT_TYPE_BUFFER_EXT:
609 return "buffer";
Tobin Ehlis5b5e7bc2016-03-09 16:12:48 -0700610 case VK_DEBUG_REPORT_OBJECT_TYPE_SWAPCHAIN_KHR_EXT:
611 return "swapchain";
Tobin Ehlis5b5e7bc2016-03-09 16:12:48 -0700612 default:
613 return "unknown";
614 }
615}
616
617// Remove object binding performs 3 tasks:
618// 1. Remove ObjectInfo from MemObjInfo list container of obj bindings & free it
Chris Forbesc025ba32016-03-31 17:06:52 +1300619// 2. Clear mem binding for image/buffer by setting its handle to 0
Tobin Ehlis5b5e7bc2016-03-09 16:12:48 -0700620// TODO : This only applied to Buffer, Image, and Swapchain objects now, how should it be updated/customized?
Chris Forbes0a1ce3d2016-04-06 15:16:26 +1200621static bool clear_object_binding(layer_data *dev_data, uint64_t handle, VkDebugReportObjectTypeEXT type) {
Tobin Ehlis5b5e7bc2016-03-09 16:12:48 -0700622 // TODO : Need to customize images/buffers/swapchains to track mem binding and clear it here appropriately
Dustin Gravese3319182016-04-05 09:41:17 -0600623 bool skipCall = false;
Tobin Ehlis94c53c02016-04-05 13:33:00 -0600624 VkDeviceMemory *pMemBinding = get_object_mem_binding(dev_data, handle, type);
625 if (pMemBinding) {
626 DEVICE_MEM_INFO *pMemObjInfo = get_mem_obj_info(dev_data, *pMemBinding);
Tobin Ehlis5b5e7bc2016-03-09 16:12:48 -0700627 // TODO : Make sure this is a reasonable way to reset mem binding
Tobin Ehlis94c53c02016-04-05 13:33:00 -0600628 *pMemBinding = VK_NULL_HANDLE;
Tobin Ehlis5b5e7bc2016-03-09 16:12:48 -0700629 if (pMemObjInfo) {
Chris Forbesc025ba32016-03-31 17:06:52 +1300630 // This obj is bound to a memory object. Remove the reference to this object in that memory object's list,
Tobin Ehlis5b5e7bc2016-03-09 16:12:48 -0700631 // and set the objects memory binding pointer to NULL.
Chris Forbes896fc322016-03-31 17:37:36 +1300632 if (!pMemObjInfo->objBindings.erase({handle, type})) {
Tobin Ehlis5b5e7bc2016-03-09 16:12:48 -0700633 skipCall |=
Tobin Ehlis58070a62016-03-16 16:00:36 -0600634 log_msg(dev_data->report_data, VK_DEBUG_REPORT_ERROR_BIT_EXT, type, handle, __LINE__, MEMTRACK_INVALID_OBJECT,
Tobin Ehlis5b5e7bc2016-03-09 16:12:48 -0700635 "MEM", "While trying to clear mem binding for %s obj %#" PRIxLEAST64
636 ", unable to find that object referenced by mem obj %#" PRIxLEAST64,
637 object_type_to_string(type), handle, (uint64_t)pMemObjInfo->mem);
638 }
639 }
640 }
641 return skipCall;
642}
643
644// For NULL mem case, output warning
645// Make sure given object is in global object map
646// IF a previous binding existed, output validation error
647// Otherwise, add reference from objectInfo to memoryInfo
648// Add reference off of objInfo
Tobin Ehlis94c53c02016-04-05 13:33:00 -0600649static bool set_mem_binding(layer_data *dev_data, VkDeviceMemory mem, uint64_t handle,
650 VkDebugReportObjectTypeEXT type, const char *apiName) {
Dustin Gravese3319182016-04-05 09:41:17 -0600651 bool skipCall = false;
Tobin Ehlis5b5e7bc2016-03-09 16:12:48 -0700652 // Handle NULL case separately, just clear previous binding & decrement reference
653 if (mem == VK_NULL_HANDLE) {
654 // TODO: Verify against Valid Use section of spec.
Tobin Ehlis58070a62016-03-16 16:00:36 -0600655 skipCall = log_msg(dev_data->report_data, VK_DEBUG_REPORT_ERROR_BIT_EXT, type, handle, __LINE__, MEMTRACK_INVALID_MEM_OBJ,
Tobin Ehlis5b5e7bc2016-03-09 16:12:48 -0700656 "MEM", "In %s, attempting to Bind Obj(%#" PRIxLEAST64 ") to NULL", apiName, handle);
657 } else {
Tobin Ehlis94c53c02016-04-05 13:33:00 -0600658 VkDeviceMemory *pMemBinding = get_object_mem_binding(dev_data, handle, type);
659 if (!pMemBinding) {
Tobin Ehlis5b5e7bc2016-03-09 16:12:48 -0700660 skipCall |=
Tobin Ehlis58070a62016-03-16 16:00:36 -0600661 log_msg(dev_data->report_data, VK_DEBUG_REPORT_ERROR_BIT_EXT, type, handle, __LINE__, MEMTRACK_MISSING_MEM_BINDINGS,
Chris Forbes4f085d92016-03-31 18:02:29 +1300662 "MEM", "In %s, attempting to update Binding of %s Obj(%#" PRIxLEAST64 ") that's not in global list",
Tobin Ehlis5b5e7bc2016-03-09 16:12:48 -0700663 object_type_to_string(type), apiName, handle);
664 } else {
665 // non-null case so should have real mem obj
Tobin Ehlis58070a62016-03-16 16:00:36 -0600666 DEVICE_MEM_INFO *pMemInfo = get_mem_obj_info(dev_data, mem);
Tobin Ehlis5b5e7bc2016-03-09 16:12:48 -0700667 if (pMemInfo) {
Tobin Ehlis94c53c02016-04-05 13:33:00 -0600668 DEVICE_MEM_INFO *pPrevBinding = get_mem_obj_info(dev_data, *pMemBinding);
Tobin Ehlis5b5e7bc2016-03-09 16:12:48 -0700669 if (pPrevBinding != NULL) {
670 skipCall |=
Tobin Ehlis58070a62016-03-16 16:00:36 -0600671 log_msg(dev_data->report_data, VK_DEBUG_REPORT_ERROR_BIT_EXT, VK_DEBUG_REPORT_OBJECT_TYPE_DEVICE_MEMORY_EXT,
Tobin Ehlis5b5e7bc2016-03-09 16:12:48 -0700672 (uint64_t)mem, __LINE__, MEMTRACK_REBIND_OBJECT, "MEM",
673 "In %s, attempting to bind memory (%#" PRIxLEAST64 ") to object (%#" PRIxLEAST64
674 ") which has already been bound to mem object %#" PRIxLEAST64,
675 apiName, (uint64_t)mem, handle, (uint64_t)pPrevBinding->mem);
676 } else {
Chris Forbes896fc322016-03-31 17:37:36 +1300677 pMemInfo->objBindings.insert({handle, type});
Tobin Ehlis5b5e7bc2016-03-09 16:12:48 -0700678 // For image objects, make sure default memory state is correctly set
679 // TODO : What's the best/correct way to handle this?
680 if (VK_DEBUG_REPORT_OBJECT_TYPE_IMAGE_EXT == type) {
Tobin Ehlis94c53c02016-04-05 13:33:00 -0600681 auto const image_node = dev_data->imageMap.find(VkImage(handle));
682 if (image_node != dev_data->imageMap.end()) {
683 VkImageCreateInfo ici = image_node->second.createInfo;
684 if (ici.usage & (VK_IMAGE_USAGE_COLOR_ATTACHMENT_BIT | VK_IMAGE_USAGE_DEPTH_STENCIL_ATTACHMENT_BIT)) {
685 // TODO:: More memory state transition stuff.
686 }
Tobin Ehlis5b5e7bc2016-03-09 16:12:48 -0700687 }
688 }
Tobin Ehlis94c53c02016-04-05 13:33:00 -0600689 *pMemBinding = mem;
Tobin Ehlis5b5e7bc2016-03-09 16:12:48 -0700690 }
691 }
692 }
693 }
694 return skipCall;
695}
696
697// For NULL mem case, clear any previous binding Else...
698// Make sure given object is in its object map
699// IF a previous binding existed, update binding
700// Add reference from objectInfo to memoryInfo
701// Add reference off of object's binding info
Chris Forbes0a1ce3d2016-04-06 15:16:26 +1200702// Return VK_TRUE if addition is successful, VK_FALSE otherwise
703static bool set_sparse_mem_binding(layer_data *dev_data, VkDeviceMemory mem, uint64_t handle,
704 VkDebugReportObjectTypeEXT type, const char *apiName) {
705 bool skipCall = VK_FALSE;
Tobin Ehlis5b5e7bc2016-03-09 16:12:48 -0700706 // Handle NULL case separately, just clear previous binding & decrement reference
707 if (mem == VK_NULL_HANDLE) {
Chris Forbes0a1ce3d2016-04-06 15:16:26 +1200708 skipCall = clear_object_binding(dev_data, handle, type);
Tobin Ehlis5b5e7bc2016-03-09 16:12:48 -0700709 } else {
Tobin Ehlis94c53c02016-04-05 13:33:00 -0600710 VkDeviceMemory *pMemBinding = get_object_mem_binding(dev_data, handle, type);
711 if (!pMemBinding) {
Tobin Ehlis5b5e7bc2016-03-09 16:12:48 -0700712 skipCall |= log_msg(
Tobin Ehlis58070a62016-03-16 16:00:36 -0600713 dev_data->report_data, VK_DEBUG_REPORT_ERROR_BIT_EXT, type, handle, __LINE__, MEMTRACK_MISSING_MEM_BINDINGS, "MEM",
Tobin Ehlis5b5e7bc2016-03-09 16:12:48 -0700714 "In %s, attempting to update Binding of Obj(%#" PRIxLEAST64 ") that's not in global list()", apiName, handle);
Tobin Ehlis94c53c02016-04-05 13:33:00 -0600715 } else {
716 // non-null case so should have real mem obj
717 DEVICE_MEM_INFO *pInfo = get_mem_obj_info(dev_data, mem);
718 if (pInfo) {
719 pInfo->objBindings.insert({handle, type});
720 // Need to set mem binding for this object
721 *pMemBinding = mem;
722 }
Tobin Ehlis5b5e7bc2016-03-09 16:12:48 -0700723 }
724 }
725 return skipCall;
726}
727
Tobin Ehlis5b5e7bc2016-03-09 16:12:48 -0700728// For given Object, get 'mem' obj that it's bound to or NULL if no binding
Chris Forbes0a1ce3d2016-04-06 15:16:26 +1200729static bool get_mem_binding_from_object(layer_data *dev_data, const uint64_t handle,
Tobin Ehlis94c53c02016-04-05 13:33:00 -0600730 const VkDebugReportObjectTypeEXT type, VkDeviceMemory *mem) {
Dustin Gravese3319182016-04-05 09:41:17 -0600731 bool skipCall = false;
Tobin Ehlis5b5e7bc2016-03-09 16:12:48 -0700732 *mem = VK_NULL_HANDLE;
Tobin Ehlis94c53c02016-04-05 13:33:00 -0600733 VkDeviceMemory *pMemBinding = get_object_mem_binding(dev_data, handle, type);
734 if (pMemBinding) {
735 *mem = *pMemBinding;
Tobin Ehlis5b5e7bc2016-03-09 16:12:48 -0700736 } else {
Tobin Ehlis94c53c02016-04-05 13:33:00 -0600737 skipCall = log_msg(dev_data->report_data, VK_DEBUG_REPORT_ERROR_BIT_EXT, type, handle, __LINE__, MEMTRACK_INVALID_OBJECT,
Tobin Ehlis5b5e7bc2016-03-09 16:12:48 -0700738 "MEM", "Trying to get mem binding for object %#" PRIxLEAST64 " but no such object in %s list", handle,
739 object_type_to_string(type));
740 }
741 return skipCall;
742}
743
744// Print details of MemObjInfo list
Chris Forbes0a1ce3d2016-04-06 15:16:26 +1200745static void print_mem_list(layer_data *dev_data) {
Tobin Ehlis58070a62016-03-16 16:00:36 -0600746 DEVICE_MEM_INFO *pInfo = NULL;
Tobin Ehlis5b5e7bc2016-03-09 16:12:48 -0700747
748 // Early out if info is not requested
Tobin Ehlis58070a62016-03-16 16:00:36 -0600749 if (!(dev_data->report_data->active_flags & VK_DEBUG_REPORT_INFORMATION_BIT_EXT)) {
Tobin Ehlis5b5e7bc2016-03-09 16:12:48 -0700750 return;
751 }
752
753 // Just printing each msg individually for now, may want to package these into single large print
Tobin Ehlis58070a62016-03-16 16:00:36 -0600754 log_msg(dev_data->report_data, VK_DEBUG_REPORT_INFORMATION_BIT_EXT, VK_DEBUG_REPORT_OBJECT_TYPE_DEVICE_MEMORY_EXT, 0, __LINE__,
Tobin Ehlis5b5e7bc2016-03-09 16:12:48 -0700755 MEMTRACK_NONE, "MEM", "Details of Memory Object list (of size " PRINTF_SIZE_T_SPECIFIER " elements)",
Tobin Ehlis58070a62016-03-16 16:00:36 -0600756 dev_data->memObjMap.size());
757 log_msg(dev_data->report_data, VK_DEBUG_REPORT_INFORMATION_BIT_EXT, VK_DEBUG_REPORT_OBJECT_TYPE_DEVICE_MEMORY_EXT, 0, __LINE__,
Tobin Ehlis5b5e7bc2016-03-09 16:12:48 -0700758 MEMTRACK_NONE, "MEM", "=============================");
759
Tobin Ehlis58070a62016-03-16 16:00:36 -0600760 if (dev_data->memObjMap.size() <= 0)
Tobin Ehlis5b5e7bc2016-03-09 16:12:48 -0700761 return;
762
Tobin Ehlis58070a62016-03-16 16:00:36 -0600763 for (auto ii = dev_data->memObjMap.begin(); ii != dev_data->memObjMap.end(); ++ii) {
Tobin Ehlis5b5e7bc2016-03-09 16:12:48 -0700764 pInfo = &(*ii).second;
765
Tobin Ehlis58070a62016-03-16 16:00:36 -0600766 log_msg(dev_data->report_data, VK_DEBUG_REPORT_INFORMATION_BIT_EXT, VK_DEBUG_REPORT_OBJECT_TYPE_DEVICE_MEMORY_EXT, 0,
Tobin Ehlis5b5e7bc2016-03-09 16:12:48 -0700767 __LINE__, MEMTRACK_NONE, "MEM", " ===MemObjInfo at %p===", (void *)pInfo);
Tobin Ehlis58070a62016-03-16 16:00:36 -0600768 log_msg(dev_data->report_data, VK_DEBUG_REPORT_INFORMATION_BIT_EXT, VK_DEBUG_REPORT_OBJECT_TYPE_DEVICE_MEMORY_EXT, 0,
Tobin Ehlis5b5e7bc2016-03-09 16:12:48 -0700769 __LINE__, MEMTRACK_NONE, "MEM", " Mem object: %#" PRIxLEAST64, (uint64_t)(pInfo->mem));
Tobin Ehlis58070a62016-03-16 16:00:36 -0600770 log_msg(dev_data->report_data, VK_DEBUG_REPORT_INFORMATION_BIT_EXT, VK_DEBUG_REPORT_OBJECT_TYPE_DEVICE_MEMORY_EXT, 0,
Chris Forbesc025ba32016-03-31 17:06:52 +1300771 __LINE__, MEMTRACK_NONE, "MEM", " Ref Count: " PRINTF_SIZE_T_SPECIFIER,
Chris Forbesde886ba2016-03-31 17:58:13 +1300772 pInfo->commandBufferBindings.size() + pInfo->objBindings.size());
Tobin Ehlis5b5e7bc2016-03-09 16:12:48 -0700773 if (0 != pInfo->allocInfo.allocationSize) {
774 string pAllocInfoMsg = vk_print_vkmemoryallocateinfo(&pInfo->allocInfo, "MEM(INFO): ");
Tobin Ehlis58070a62016-03-16 16:00:36 -0600775 log_msg(dev_data->report_data, VK_DEBUG_REPORT_INFORMATION_BIT_EXT, VK_DEBUG_REPORT_OBJECT_TYPE_DEVICE_MEMORY_EXT, 0,
Tobin Ehlis5b5e7bc2016-03-09 16:12:48 -0700776 __LINE__, MEMTRACK_NONE, "MEM", " Mem Alloc info:\n%s", pAllocInfoMsg.c_str());
777 } else {
Tobin Ehlis58070a62016-03-16 16:00:36 -0600778 log_msg(dev_data->report_data, VK_DEBUG_REPORT_INFORMATION_BIT_EXT, VK_DEBUG_REPORT_OBJECT_TYPE_DEVICE_MEMORY_EXT, 0,
Tobin Ehlis5b5e7bc2016-03-09 16:12:48 -0700779 __LINE__, MEMTRACK_NONE, "MEM", " Mem Alloc info is NULL (alloc done by vkCreateSwapchainKHR())");
780 }
781
Tobin Ehlis58070a62016-03-16 16:00:36 -0600782 log_msg(dev_data->report_data, VK_DEBUG_REPORT_INFORMATION_BIT_EXT, VK_DEBUG_REPORT_OBJECT_TYPE_DEVICE_MEMORY_EXT, 0,
Tobin Ehlis5b5e7bc2016-03-09 16:12:48 -0700783 __LINE__, MEMTRACK_NONE, "MEM", " VK OBJECT Binding list of size " PRINTF_SIZE_T_SPECIFIER " elements:",
Chris Forbes896fc322016-03-31 17:37:36 +1300784 pInfo->objBindings.size());
785 if (pInfo->objBindings.size() > 0) {
786 for (auto obj : pInfo->objBindings) {
Tobin Ehlis58070a62016-03-16 16:00:36 -0600787 log_msg(dev_data->report_data, VK_DEBUG_REPORT_INFORMATION_BIT_EXT, VK_DEBUG_REPORT_OBJECT_TYPE_DEVICE_MEMORY_EXT,
Chris Forbes896fc322016-03-31 17:37:36 +1300788 0, __LINE__, MEMTRACK_NONE, "MEM", " VK OBJECT %" PRIu64, obj.handle);
Tobin Ehlis5b5e7bc2016-03-09 16:12:48 -0700789 }
790 }
791
Tobin Ehlis58070a62016-03-16 16:00:36 -0600792 log_msg(dev_data->report_data, VK_DEBUG_REPORT_INFORMATION_BIT_EXT, VK_DEBUG_REPORT_OBJECT_TYPE_DEVICE_MEMORY_EXT, 0,
Tobin Ehlis5b5e7bc2016-03-09 16:12:48 -0700793 __LINE__, MEMTRACK_NONE, "MEM",
794 " VK Command Buffer (CB) binding list of size " PRINTF_SIZE_T_SPECIFIER " elements",
Chris Forbesde886ba2016-03-31 17:58:13 +1300795 pInfo->commandBufferBindings.size());
796 if (pInfo->commandBufferBindings.size() > 0) {
797 for (auto cb : pInfo->commandBufferBindings) {
Tobin Ehlis58070a62016-03-16 16:00:36 -0600798 log_msg(dev_data->report_data, VK_DEBUG_REPORT_INFORMATION_BIT_EXT, VK_DEBUG_REPORT_OBJECT_TYPE_DEVICE_MEMORY_EXT,
Chris Forbesde886ba2016-03-31 17:58:13 +1300799 0, __LINE__, MEMTRACK_NONE, "MEM", " VK CB %p", cb);
Tobin Ehlis5b5e7bc2016-03-09 16:12:48 -0700800 }
801 }
802 }
803}
804
Chris Forbes0a1ce3d2016-04-06 15:16:26 +1200805static void printCBList(layer_data *my_data) {
Tobin Ehlis72d66f02016-03-21 14:14:44 -0600806 GLOBAL_CB_NODE *pCBInfo = NULL;
Tobin Ehlis5b5e7bc2016-03-09 16:12:48 -0700807
808 // Early out if info is not requested
809 if (!(my_data->report_data->active_flags & VK_DEBUG_REPORT_INFORMATION_BIT_EXT)) {
810 return;
811 }
812
813 log_msg(my_data->report_data, VK_DEBUG_REPORT_INFORMATION_BIT_EXT, VK_DEBUG_REPORT_OBJECT_TYPE_DEVICE_MEMORY_EXT, 0, __LINE__,
Tobin Ehlis72d66f02016-03-21 14:14:44 -0600814 MEMTRACK_NONE, "MEM", "Details of CB list (of size " PRINTF_SIZE_T_SPECIFIER " elements)",
815 my_data->commandBufferMap.size());
Tobin Ehlis5b5e7bc2016-03-09 16:12:48 -0700816 log_msg(my_data->report_data, VK_DEBUG_REPORT_INFORMATION_BIT_EXT, VK_DEBUG_REPORT_OBJECT_TYPE_DEVICE_MEMORY_EXT, 0, __LINE__,
817 MEMTRACK_NONE, "MEM", "==================");
818
Tobin Ehlis72d66f02016-03-21 14:14:44 -0600819 if (my_data->commandBufferMap.size() <= 0)
Tobin Ehlis5b5e7bc2016-03-09 16:12:48 -0700820 return;
821
Tobin Ehlis72d66f02016-03-21 14:14:44 -0600822 for (auto &cb_node : my_data->commandBufferMap) {
823 pCBInfo = cb_node.second;
Tobin Ehlis5b5e7bc2016-03-09 16:12:48 -0700824
825 log_msg(my_data->report_data, VK_DEBUG_REPORT_INFORMATION_BIT_EXT, VK_DEBUG_REPORT_OBJECT_TYPE_DEVICE_MEMORY_EXT, 0,
826 __LINE__, MEMTRACK_NONE, "MEM", " CB Info (%p) has CB %p, fenceId %" PRIx64 ", and fence %#" PRIxLEAST64,
827 (void *)pCBInfo, (void *)pCBInfo->commandBuffer, pCBInfo->fenceId, (uint64_t)pCBInfo->lastSubmittedFence);
828
Chris Forbesc7196ad2016-03-31 18:11:28 +1300829 if (pCBInfo->memObjs.size() <= 0)
Tobin Ehlis5b5e7bc2016-03-09 16:12:48 -0700830 continue;
Chris Forbesc7196ad2016-03-31 18:11:28 +1300831 for (auto obj : pCBInfo->memObjs) {
Tobin Ehlis5b5e7bc2016-03-09 16:12:48 -0700832 log_msg(my_data->report_data, VK_DEBUG_REPORT_INFORMATION_BIT_EXT, VK_DEBUG_REPORT_OBJECT_TYPE_DEVICE_MEMORY_EXT, 0,
Chris Forbesc7196ad2016-03-31 18:11:28 +1300833 __LINE__, MEMTRACK_NONE, "MEM", " Mem obj %" PRIu64, (uint64_t)obj);
Tobin Ehlis5b5e7bc2016-03-09 16:12:48 -0700834 }
835 }
836}
837
838#endif
839
Tobin Ehlis5b5e7bc2016-03-09 16:12:48 -0700840// Return a string representation of CMD_TYPE enum
841static string cmdTypeToString(CMD_TYPE cmd) {
842 switch (cmd) {
843 case CMD_BINDPIPELINE:
844 return "CMD_BINDPIPELINE";
845 case CMD_BINDPIPELINEDELTA:
846 return "CMD_BINDPIPELINEDELTA";
847 case CMD_SETVIEWPORTSTATE:
848 return "CMD_SETVIEWPORTSTATE";
849 case CMD_SETLINEWIDTHSTATE:
850 return "CMD_SETLINEWIDTHSTATE";
851 case CMD_SETDEPTHBIASSTATE:
852 return "CMD_SETDEPTHBIASSTATE";
853 case CMD_SETBLENDSTATE:
854 return "CMD_SETBLENDSTATE";
855 case CMD_SETDEPTHBOUNDSSTATE:
856 return "CMD_SETDEPTHBOUNDSSTATE";
857 case CMD_SETSTENCILREADMASKSTATE:
858 return "CMD_SETSTENCILREADMASKSTATE";
859 case CMD_SETSTENCILWRITEMASKSTATE:
860 return "CMD_SETSTENCILWRITEMASKSTATE";
861 case CMD_SETSTENCILREFERENCESTATE:
862 return "CMD_SETSTENCILREFERENCESTATE";
863 case CMD_BINDDESCRIPTORSETS:
864 return "CMD_BINDDESCRIPTORSETS";
865 case CMD_BINDINDEXBUFFER:
866 return "CMD_BINDINDEXBUFFER";
867 case CMD_BINDVERTEXBUFFER:
868 return "CMD_BINDVERTEXBUFFER";
869 case CMD_DRAW:
870 return "CMD_DRAW";
871 case CMD_DRAWINDEXED:
872 return "CMD_DRAWINDEXED";
873 case CMD_DRAWINDIRECT:
874 return "CMD_DRAWINDIRECT";
875 case CMD_DRAWINDEXEDINDIRECT:
876 return "CMD_DRAWINDEXEDINDIRECT";
877 case CMD_DISPATCH:
878 return "CMD_DISPATCH";
879 case CMD_DISPATCHINDIRECT:
880 return "CMD_DISPATCHINDIRECT";
881 case CMD_COPYBUFFER:
882 return "CMD_COPYBUFFER";
883 case CMD_COPYIMAGE:
884 return "CMD_COPYIMAGE";
885 case CMD_BLITIMAGE:
886 return "CMD_BLITIMAGE";
887 case CMD_COPYBUFFERTOIMAGE:
888 return "CMD_COPYBUFFERTOIMAGE";
889 case CMD_COPYIMAGETOBUFFER:
890 return "CMD_COPYIMAGETOBUFFER";
891 case CMD_CLONEIMAGEDATA:
892 return "CMD_CLONEIMAGEDATA";
893 case CMD_UPDATEBUFFER:
894 return "CMD_UPDATEBUFFER";
895 case CMD_FILLBUFFER:
896 return "CMD_FILLBUFFER";
897 case CMD_CLEARCOLORIMAGE:
898 return "CMD_CLEARCOLORIMAGE";
899 case CMD_CLEARATTACHMENTS:
900 return "CMD_CLEARCOLORATTACHMENT";
901 case CMD_CLEARDEPTHSTENCILIMAGE:
902 return "CMD_CLEARDEPTHSTENCILIMAGE";
903 case CMD_RESOLVEIMAGE:
904 return "CMD_RESOLVEIMAGE";
905 case CMD_SETEVENT:
906 return "CMD_SETEVENT";
907 case CMD_RESETEVENT:
908 return "CMD_RESETEVENT";
909 case CMD_WAITEVENTS:
910 return "CMD_WAITEVENTS";
911 case CMD_PIPELINEBARRIER:
912 return "CMD_PIPELINEBARRIER";
913 case CMD_BEGINQUERY:
914 return "CMD_BEGINQUERY";
915 case CMD_ENDQUERY:
916 return "CMD_ENDQUERY";
917 case CMD_RESETQUERYPOOL:
918 return "CMD_RESETQUERYPOOL";
919 case CMD_COPYQUERYPOOLRESULTS:
920 return "CMD_COPYQUERYPOOLRESULTS";
921 case CMD_WRITETIMESTAMP:
922 return "CMD_WRITETIMESTAMP";
923 case CMD_INITATOMICCOUNTERS:
924 return "CMD_INITATOMICCOUNTERS";
925 case CMD_LOADATOMICCOUNTERS:
926 return "CMD_LOADATOMICCOUNTERS";
927 case CMD_SAVEATOMICCOUNTERS:
928 return "CMD_SAVEATOMICCOUNTERS";
929 case CMD_BEGINRENDERPASS:
930 return "CMD_BEGINRENDERPASS";
931 case CMD_ENDRENDERPASS:
932 return "CMD_ENDRENDERPASS";
933 default:
934 return "UNKNOWN";
935 }
936}
937
938// SPIRV utility functions
939static void build_def_index(shader_module *module) {
940 for (auto insn : *module) {
941 switch (insn.opcode()) {
942 /* Types */
943 case spv::OpTypeVoid:
944 case spv::OpTypeBool:
945 case spv::OpTypeInt:
946 case spv::OpTypeFloat:
947 case spv::OpTypeVector:
948 case spv::OpTypeMatrix:
949 case spv::OpTypeImage:
950 case spv::OpTypeSampler:
951 case spv::OpTypeSampledImage:
952 case spv::OpTypeArray:
953 case spv::OpTypeRuntimeArray:
954 case spv::OpTypeStruct:
955 case spv::OpTypeOpaque:
956 case spv::OpTypePointer:
957 case spv::OpTypeFunction:
958 case spv::OpTypeEvent:
959 case spv::OpTypeDeviceEvent:
960 case spv::OpTypeReserveId:
961 case spv::OpTypeQueue:
962 case spv::OpTypePipe:
963 module->def_index[insn.word(1)] = insn.offset();
964 break;
965
966 /* Fixed constants */
967 case spv::OpConstantTrue:
968 case spv::OpConstantFalse:
969 case spv::OpConstant:
970 case spv::OpConstantComposite:
971 case spv::OpConstantSampler:
972 case spv::OpConstantNull:
973 module->def_index[insn.word(2)] = insn.offset();
974 break;
975
976 /* Specialization constants */
977 case spv::OpSpecConstantTrue:
978 case spv::OpSpecConstantFalse:
979 case spv::OpSpecConstant:
980 case spv::OpSpecConstantComposite:
981 case spv::OpSpecConstantOp:
982 module->def_index[insn.word(2)] = insn.offset();
983 break;
984
985 /* Variables */
986 case spv::OpVariable:
987 module->def_index[insn.word(2)] = insn.offset();
988 break;
989
990 /* Functions */
991 case spv::OpFunction:
992 module->def_index[insn.word(2)] = insn.offset();
993 break;
994
995 default:
996 /* We don't care about any other defs for now. */
997 break;
998 }
999 }
1000}
1001
1002static spirv_inst_iter find_entrypoint(shader_module *src, char const *name, VkShaderStageFlagBits stageBits) {
1003 for (auto insn : *src) {
1004 if (insn.opcode() == spv::OpEntryPoint) {
1005 auto entrypointName = (char const *)&insn.word(3);
1006 auto entrypointStageBits = 1u << insn.word(1);
1007
1008 if (!strcmp(entrypointName, name) && (entrypointStageBits & stageBits)) {
1009 return insn;
1010 }
1011 }
1012 }
1013
1014 return src->end();
1015}
1016
1017bool shader_is_spirv(VkShaderModuleCreateInfo const *pCreateInfo) {
1018 uint32_t *words = (uint32_t *)pCreateInfo->pCode;
1019 size_t sizeInWords = pCreateInfo->codeSize / sizeof(uint32_t);
1020
1021 /* Just validate that the header makes sense. */
1022 return sizeInWords >= 5 && words[0] == spv::MagicNumber && words[1] == spv::Version;
1023}
1024
1025static char const *storage_class_name(unsigned sc) {
1026 switch (sc) {
1027 case spv::StorageClassInput:
1028 return "input";
1029 case spv::StorageClassOutput:
1030 return "output";
1031 case spv::StorageClassUniformConstant:
1032 return "const uniform";
1033 case spv::StorageClassUniform:
1034 return "uniform";
1035 case spv::StorageClassWorkgroup:
1036 return "workgroup local";
1037 case spv::StorageClassCrossWorkgroup:
1038 return "workgroup global";
1039 case spv::StorageClassPrivate:
1040 return "private global";
1041 case spv::StorageClassFunction:
1042 return "function";
1043 case spv::StorageClassGeneric:
1044 return "generic";
1045 case spv::StorageClassAtomicCounter:
1046 return "atomic counter";
1047 case spv::StorageClassImage:
1048 return "image";
1049 case spv::StorageClassPushConstant:
1050 return "push constant";
1051 default:
1052 return "unknown";
1053 }
1054}
1055
1056/* get the value of an integral constant */
1057unsigned get_constant_value(shader_module const *src, unsigned id) {
1058 auto value = src->get_def(id);
1059 assert(value != src->end());
1060
1061 if (value.opcode() != spv::OpConstant) {
1062 /* TODO: Either ensure that the specialization transform is already performed on a module we're
1063 considering here, OR -- specialize on the fly now.
1064 */
1065 return 1;
1066 }
1067
1068 return value.word(3);
1069}
1070
Chris Forbes9ccb7b12016-03-18 14:59:39 +13001071
1072static void describe_type_inner(std::ostringstream &ss, shader_module const *src, unsigned type) {
Tobin Ehlis5b5e7bc2016-03-09 16:12:48 -07001073 auto insn = src->get_def(type);
1074 assert(insn != src->end());
1075
1076 switch (insn.opcode()) {
1077 case spv::OpTypeBool:
Chris Forbes9ccb7b12016-03-18 14:59:39 +13001078 ss << "bool";
1079 break;
Tobin Ehlis5b5e7bc2016-03-09 16:12:48 -07001080 case spv::OpTypeInt:
Chris Forbes9ccb7b12016-03-18 14:59:39 +13001081 ss << (insn.word(3) ? 's' : 'u') << "int" << insn.word(2);
1082 break;
Tobin Ehlis5b5e7bc2016-03-09 16:12:48 -07001083 case spv::OpTypeFloat:
Chris Forbes9ccb7b12016-03-18 14:59:39 +13001084 ss << "float" << insn.word(2);
1085 break;
Tobin Ehlis5b5e7bc2016-03-09 16:12:48 -07001086 case spv::OpTypeVector:
Chris Forbes9ccb7b12016-03-18 14:59:39 +13001087 ss << "vec" << insn.word(3) << " of ";
1088 describe_type_inner(ss, src, insn.word(2));
1089 break;
Tobin Ehlis5b5e7bc2016-03-09 16:12:48 -07001090 case spv::OpTypeMatrix:
Chris Forbes9ccb7b12016-03-18 14:59:39 +13001091 ss << "mat" << insn.word(3) << " of ";
1092 describe_type_inner(ss, src, insn.word(2));
1093 break;
Tobin Ehlis5b5e7bc2016-03-09 16:12:48 -07001094 case spv::OpTypeArray:
Chris Forbes9ccb7b12016-03-18 14:59:39 +13001095 ss << "arr[" << get_constant_value(src, insn.word(3)) << "] of ";
1096 describe_type_inner(ss, src, insn.word(2));
1097 break;
Tobin Ehlis5b5e7bc2016-03-09 16:12:48 -07001098 case spv::OpTypePointer:
Chris Forbes9ccb7b12016-03-18 14:59:39 +13001099 ss << "ptr to " << storage_class_name(insn.word(2)) << " ";
1100 describe_type_inner(ss, src, insn.word(3));
1101 break;
Tobin Ehlis5b5e7bc2016-03-09 16:12:48 -07001102 case spv::OpTypeStruct: {
Chris Forbes9ccb7b12016-03-18 14:59:39 +13001103 ss << "struct of (";
Tobin Ehlis5b5e7bc2016-03-09 16:12:48 -07001104 for (unsigned i = 2; i < insn.len(); i++) {
Chris Forbes9ccb7b12016-03-18 14:59:39 +13001105 describe_type_inner(ss, src, insn.word(i));
1106 if (i == insn.len() - 1) {
1107 ss << ")";
1108 } else {
1109 ss << ", ";
1110 }
Tobin Ehlis5b5e7bc2016-03-09 16:12:48 -07001111 }
Chris Forbes9ccb7b12016-03-18 14:59:39 +13001112 break;
Tobin Ehlis5b5e7bc2016-03-09 16:12:48 -07001113 }
1114 case spv::OpTypeSampler:
Chris Forbes9ccb7b12016-03-18 14:59:39 +13001115 ss << "sampler";
1116 break;
Tobin Ehlis5b5e7bc2016-03-09 16:12:48 -07001117 case spv::OpTypeSampledImage:
Chris Forbes9ccb7b12016-03-18 14:59:39 +13001118 ss << "sampler+";
1119 describe_type_inner(ss, src, insn.word(2));
1120 break;
Tobin Ehlis5b5e7bc2016-03-09 16:12:48 -07001121 case spv::OpTypeImage:
Chris Forbes9ccb7b12016-03-18 14:59:39 +13001122 ss << "image(dim=" << insn.word(3) << ", sampled=" << insn.word(7) << ")";
1123 break;
Tobin Ehlis5b5e7bc2016-03-09 16:12:48 -07001124 default:
Chris Forbes9ccb7b12016-03-18 14:59:39 +13001125 ss << "oddtype";
1126 break;
Tobin Ehlis5b5e7bc2016-03-09 16:12:48 -07001127 }
1128}
1129
Chris Forbes9ccb7b12016-03-18 14:59:39 +13001130
1131static std::string describe_type(shader_module const *src, unsigned type) {
1132 std::ostringstream ss;
1133 describe_type_inner(ss, src, type);
1134 return ss.str();
1135}
1136
1137
Chris Forbes37576f92016-04-05 17:51:35 +12001138static bool is_narrow_numeric_type(spirv_inst_iter type)
1139{
1140 if (type.opcode() != spv::OpTypeInt && type.opcode() != spv::OpTypeFloat)
1141 return false;
1142 return type.word(2) < 64;
1143}
1144
1145
1146static bool types_match(shader_module const *a, shader_module const *b, unsigned a_type, unsigned b_type, bool a_arrayed, bool b_arrayed, bool relaxed) {
Tobin Ehlis5b5e7bc2016-03-09 16:12:48 -07001147 /* walk two type trees together, and complain about differences */
1148 auto a_insn = a->get_def(a_type);
1149 auto b_insn = b->get_def(b_type);
1150 assert(a_insn != a->end());
1151 assert(b_insn != b->end());
1152
Chris Forbes7c755c82016-03-29 16:38:44 +13001153 if (a_arrayed && a_insn.opcode() == spv::OpTypeArray) {
Chris Forbes37576f92016-04-05 17:51:35 +12001154 return types_match(a, b, a_insn.word(2), b_type, false, b_arrayed, relaxed);
Chris Forbes7c755c82016-03-29 16:38:44 +13001155 }
1156
Tobin Ehlis5b5e7bc2016-03-09 16:12:48 -07001157 if (b_arrayed && b_insn.opcode() == spv::OpTypeArray) {
1158 /* we probably just found the extra level of arrayness in b_type: compare the type inside it to a_type */
Chris Forbes37576f92016-04-05 17:51:35 +12001159 return types_match(a, b, a_type, b_insn.word(2), a_arrayed, false, relaxed);
1160 }
1161
1162 if (a_insn.opcode() == spv::OpTypeVector && relaxed && is_narrow_numeric_type(b_insn)) {
1163 return types_match(a, b, a_insn.word(2), b_type, a_arrayed, b_arrayed, false);
Tobin Ehlis5b5e7bc2016-03-09 16:12:48 -07001164 }
1165
1166 if (a_insn.opcode() != b_insn.opcode()) {
1167 return false;
1168 }
1169
Chris Forbes7c755c82016-03-29 16:38:44 +13001170 if (a_insn.opcode() == spv::OpTypePointer) {
1171 /* match on pointee type. storage class is expected to differ */
Chris Forbes37576f92016-04-05 17:51:35 +12001172 return types_match(a, b, a_insn.word(3), b_insn.word(3), a_arrayed, b_arrayed, relaxed);
Chris Forbes7c755c82016-03-29 16:38:44 +13001173 }
1174
1175 if (a_arrayed || b_arrayed) {
1176 /* if we havent resolved array-of-verts by here, we're not going to. */
1177 return false;
1178 }
1179
Tobin Ehlis5b5e7bc2016-03-09 16:12:48 -07001180 switch (a_insn.opcode()) {
Tobin Ehlis5b5e7bc2016-03-09 16:12:48 -07001181 case spv::OpTypeBool:
Chris Forbes7c755c82016-03-29 16:38:44 +13001182 return true;
Tobin Ehlis5b5e7bc2016-03-09 16:12:48 -07001183 case spv::OpTypeInt:
1184 /* match on width, signedness */
Chris Forbes7c755c82016-03-29 16:38:44 +13001185 return a_insn.word(2) == b_insn.word(2) && a_insn.word(3) == b_insn.word(3);
Tobin Ehlis5b5e7bc2016-03-09 16:12:48 -07001186 case spv::OpTypeFloat:
1187 /* match on width */
Chris Forbes7c755c82016-03-29 16:38:44 +13001188 return a_insn.word(2) == b_insn.word(2);
Tobin Ehlis5b5e7bc2016-03-09 16:12:48 -07001189 case spv::OpTypeVector:
Chris Forbes37576f92016-04-05 17:51:35 +12001190 /* match on element type, count. */
1191 if (!types_match(a, b, a_insn.word(2), b_insn.word(2), a_arrayed, b_arrayed, false))
1192 return false;
1193 if (relaxed && is_narrow_numeric_type(a->get_def(a_insn.word(2)))) {
1194 return a_insn.word(3) >= b_insn.word(3);
1195 }
1196 else {
1197 return a_insn.word(3) == b_insn.word(3);
1198 }
Tobin Ehlis5b5e7bc2016-03-09 16:12:48 -07001199 case spv::OpTypeMatrix:
Chris Forbes37576f92016-04-05 17:51:35 +12001200 /* match on element type, count. */
1201 return types_match(a, b, a_insn.word(2), b_insn.word(2), a_arrayed, b_arrayed, false) && a_insn.word(3) == b_insn.word(3);
Tobin Ehlis5b5e7bc2016-03-09 16:12:48 -07001202 case spv::OpTypeArray:
1203 /* match on element type, count. these all have the same layout. we don't get here if
1204 * b_arrayed. This differs from vector & matrix types in that the array size is the id of a constant instruction,
1205 * not a literal within OpTypeArray */
Chris Forbes37576f92016-04-05 17:51:35 +12001206 return types_match(a, b, a_insn.word(2), b_insn.word(2), a_arrayed, b_arrayed, false) &&
Tobin Ehlis5b5e7bc2016-03-09 16:12:48 -07001207 get_constant_value(a, a_insn.word(3)) == get_constant_value(b, b_insn.word(3));
1208 case spv::OpTypeStruct:
1209 /* match on all element types */
1210 {
Tobin Ehlis5b5e7bc2016-03-09 16:12:48 -07001211 if (a_insn.len() != b_insn.len()) {
1212 return false; /* structs cannot match if member counts differ */
1213 }
1214
1215 for (unsigned i = 2; i < a_insn.len(); i++) {
Chris Forbes37576f92016-04-05 17:51:35 +12001216 if (!types_match(a, b, a_insn.word(i), b_insn.word(i), a_arrayed, b_arrayed, false)) {
Tobin Ehlis5b5e7bc2016-03-09 16:12:48 -07001217 return false;
1218 }
1219 }
1220
1221 return true;
1222 }
Tobin Ehlis5b5e7bc2016-03-09 16:12:48 -07001223 default:
1224 /* remaining types are CLisms, or may not appear in the interfaces we
1225 * are interested in. Just claim no match.
1226 */
1227 return false;
1228 }
1229}
1230
1231static int value_or_default(std::unordered_map<unsigned, unsigned> const &map, unsigned id, int def) {
1232 auto it = map.find(id);
1233 if (it == map.end())
1234 return def;
1235 else
1236 return it->second;
1237}
1238
1239static unsigned get_locations_consumed_by_type(shader_module const *src, unsigned type, bool strip_array_level) {
1240 auto insn = src->get_def(type);
1241 assert(insn != src->end());
1242
1243 switch (insn.opcode()) {
1244 case spv::OpTypePointer:
1245 /* see through the ptr -- this is only ever at the toplevel for graphics shaders;
1246 * we're never actually passing pointers around. */
1247 return get_locations_consumed_by_type(src, insn.word(3), strip_array_level);
1248 case spv::OpTypeArray:
1249 if (strip_array_level) {
1250 return get_locations_consumed_by_type(src, insn.word(2), false);
1251 } else {
1252 return get_constant_value(src, insn.word(3)) * get_locations_consumed_by_type(src, insn.word(2), false);
1253 }
1254 case spv::OpTypeMatrix:
1255 /* num locations is the dimension * element size */
1256 return insn.word(3) * get_locations_consumed_by_type(src, insn.word(2), false);
1257 default:
1258 /* everything else is just 1. */
1259 return 1;
1260
1261 /* TODO: extend to handle 64bit scalar types, whose vectors may need
1262 * multiple locations. */
1263 }
1264}
1265
1266typedef std::pair<unsigned, unsigned> location_t;
1267typedef std::pair<unsigned, unsigned> descriptor_slot_t;
1268
1269struct interface_var {
1270 uint32_t id;
1271 uint32_t type_id;
1272 uint32_t offset;
Chris Forbesb934cb22016-03-29 16:14:02 +13001273 bool is_patch;
Tobin Ehlis5b5e7bc2016-03-09 16:12:48 -07001274 /* TODO: collect the name, too? Isn't required to be present. */
1275};
1276
Chris Forbes031261d2016-03-29 16:41:07 +13001277struct shader_stage_attributes {
1278 char const *const name;
1279 bool arrayed_input;
1280 bool arrayed_output;
1281};
1282
1283static shader_stage_attributes shader_stage_attribs[] = {
1284 {"vertex shader", false, false},
1285 {"tessellation control shader", true, true},
1286 {"tessellation evaluation shader", true, false},
1287 {"geometry shader", true, false},
1288 {"fragment shader", false, false},
1289};
1290
Tobin Ehlis5b5e7bc2016-03-09 16:12:48 -07001291static spirv_inst_iter get_struct_type(shader_module const *src, spirv_inst_iter def, bool is_array_of_verts) {
1292 while (true) {
1293
1294 if (def.opcode() == spv::OpTypePointer) {
1295 def = src->get_def(def.word(3));
1296 } else if (def.opcode() == spv::OpTypeArray && is_array_of_verts) {
1297 def = src->get_def(def.word(2));
1298 is_array_of_verts = false;
1299 } else if (def.opcode() == spv::OpTypeStruct) {
1300 return def;
1301 } else {
1302 return src->end();
1303 }
1304 }
1305}
1306
Chris Forbes6c94fb92016-03-30 11:35:21 +13001307static void collect_interface_block_members(layer_data *my_data, shader_module const *src,
Tobin Ehlis5b5e7bc2016-03-09 16:12:48 -07001308 std::map<location_t, interface_var> &out,
1309 std::unordered_map<unsigned, unsigned> const &blocks, bool is_array_of_verts,
Chris Forbesb934cb22016-03-29 16:14:02 +13001310 uint32_t id, uint32_t type_id, bool is_patch) {
Tobin Ehlis5b5e7bc2016-03-09 16:12:48 -07001311 /* Walk down the type_id presented, trying to determine whether it's actually an interface block. */
Chris Forbes031261d2016-03-29 16:41:07 +13001312 auto type = get_struct_type(src, src->get_def(type_id), is_array_of_verts && !is_patch);
Tobin Ehlis5b5e7bc2016-03-09 16:12:48 -07001313 if (type == src->end() || blocks.find(type.word(1)) == blocks.end()) {
1314 /* this isn't an interface block. */
1315 return;
1316 }
1317
1318 std::unordered_map<unsigned, unsigned> member_components;
1319
1320 /* Walk all the OpMemberDecorate for type's result id -- first pass, collect components. */
1321 for (auto insn : *src) {
1322 if (insn.opcode() == spv::OpMemberDecorate && insn.word(1) == type.word(1)) {
1323 unsigned member_index = insn.word(2);
1324
1325 if (insn.word(3) == spv::DecorationComponent) {
1326 unsigned component = insn.word(4);
1327 member_components[member_index] = component;
1328 }
1329 }
1330 }
1331
1332 /* Second pass -- produce the output, from Location decorations */
1333 for (auto insn : *src) {
1334 if (insn.opcode() == spv::OpMemberDecorate && insn.word(1) == type.word(1)) {
1335 unsigned member_index = insn.word(2);
1336 unsigned member_type_id = type.word(2 + member_index);
1337
1338 if (insn.word(3) == spv::DecorationLocation) {
1339 unsigned location = insn.word(4);
1340 unsigned num_locations = get_locations_consumed_by_type(src, member_type_id, false);
1341 auto component_it = member_components.find(member_index);
1342 unsigned component = component_it == member_components.end() ? 0 : component_it->second;
1343
1344 for (unsigned int offset = 0; offset < num_locations; offset++) {
1345 interface_var v;
1346 v.id = id;
1347 /* TODO: member index in interface_var too? */
1348 v.type_id = member_type_id;
1349 v.offset = offset;
Chris Forbesb934cb22016-03-29 16:14:02 +13001350 v.is_patch = is_patch;
Tobin Ehlis5b5e7bc2016-03-09 16:12:48 -07001351 out[std::make_pair(location + offset, component)] = v;
1352 }
1353 }
1354 }
1355 }
1356}
1357
Chris Forbes6c94fb92016-03-30 11:35:21 +13001358static void collect_interface_by_location(layer_data *my_data, shader_module const *src, spirv_inst_iter entrypoint,
Tobin Ehlis5b5e7bc2016-03-09 16:12:48 -07001359 spv::StorageClass sinterface, std::map<location_t, interface_var> &out,
1360 bool is_array_of_verts) {
1361 std::unordered_map<unsigned, unsigned> var_locations;
1362 std::unordered_map<unsigned, unsigned> var_builtins;
1363 std::unordered_map<unsigned, unsigned> var_components;
1364 std::unordered_map<unsigned, unsigned> blocks;
Chris Forbesb934cb22016-03-29 16:14:02 +13001365 std::unordered_map<unsigned, unsigned> var_patch;
Tobin Ehlis5b5e7bc2016-03-09 16:12:48 -07001366
1367 for (auto insn : *src) {
1368
1369 /* We consider two interface models: SSO rendezvous-by-location, and
1370 * builtins. Complain about anything that fits neither model.
1371 */
1372 if (insn.opcode() == spv::OpDecorate) {
1373 if (insn.word(2) == spv::DecorationLocation) {
1374 var_locations[insn.word(1)] = insn.word(3);
1375 }
1376
1377 if (insn.word(2) == spv::DecorationBuiltIn) {
1378 var_builtins[insn.word(1)] = insn.word(3);
1379 }
1380
1381 if (insn.word(2) == spv::DecorationComponent) {
1382 var_components[insn.word(1)] = insn.word(3);
1383 }
1384
1385 if (insn.word(2) == spv::DecorationBlock) {
1386 blocks[insn.word(1)] = 1;
1387 }
Chris Forbesb934cb22016-03-29 16:14:02 +13001388
1389 if (insn.word(2) == spv::DecorationPatch) {
1390 var_patch[insn.word(1)] = 1;
1391 }
Tobin Ehlis5b5e7bc2016-03-09 16:12:48 -07001392 }
1393 }
1394
1395 /* TODO: handle grouped decorations */
1396 /* TODO: handle index=1 dual source outputs from FS -- two vars will
Eric Engestrom81264dd2016-04-02 22:06:13 +01001397 * have the same location, and we DON'T want to clobber. */
Tobin Ehlis5b5e7bc2016-03-09 16:12:48 -07001398
1399 /* find the end of the entrypoint's name string. additional zero bytes follow the actual null
1400 terminator, to fill out the rest of the word - so we only need to look at the last byte in
1401 the word to determine which word contains the terminator. */
Michael Mc Donnellc15b8012016-04-03 14:47:51 -07001402 uint32_t word = 3;
Tobin Ehlis5b5e7bc2016-03-09 16:12:48 -07001403 while (entrypoint.word(word) & 0xff000000u) {
1404 ++word;
1405 }
1406 ++word;
1407
1408 for (; word < entrypoint.len(); word++) {
1409 auto insn = src->get_def(entrypoint.word(word));
1410 assert(insn != src->end());
1411 assert(insn.opcode() == spv::OpVariable);
1412
Jamie Madill1d5109d2016-04-04 15:09:51 -04001413 if (insn.word(3) == static_cast<uint32_t>(sinterface)) {
Tobin Ehlis5b5e7bc2016-03-09 16:12:48 -07001414 unsigned id = insn.word(2);
1415 unsigned type = insn.word(1);
1416
1417 int location = value_or_default(var_locations, id, -1);
1418 int builtin = value_or_default(var_builtins, id, -1);
1419 unsigned component = value_or_default(var_components, id, 0); /* unspecified is OK, is 0 */
Chris Forbesb934cb22016-03-29 16:14:02 +13001420 bool is_patch = var_patch.find(id) != var_patch.end();
Tobin Ehlis5b5e7bc2016-03-09 16:12:48 -07001421
1422 /* All variables and interface block members in the Input or Output storage classes
1423 * must be decorated with either a builtin or an explicit location.
1424 *
1425 * TODO: integrate the interface block support here. For now, don't complain --
1426 * a valid SPIRV module will only hit this path for the interface block case, as the
1427 * individual members of the type are decorated, rather than variable declarations.
1428 */
1429
1430 if (location != -1) {
1431 /* A user-defined interface variable, with a location. Where a variable
1432 * occupied multiple locations, emit one result for each. */
Chris Forbes7c755c82016-03-29 16:38:44 +13001433 unsigned num_locations = get_locations_consumed_by_type(src, type, is_array_of_verts && !is_patch);
Tobin Ehlis5b5e7bc2016-03-09 16:12:48 -07001434 for (unsigned int offset = 0; offset < num_locations; offset++) {
1435 interface_var v;
1436 v.id = id;
1437 v.type_id = type;
1438 v.offset = offset;
Chris Forbesb934cb22016-03-29 16:14:02 +13001439 v.is_patch = is_patch;
Tobin Ehlis5b5e7bc2016-03-09 16:12:48 -07001440 out[std::make_pair(location + offset, component)] = v;
1441 }
1442 } else if (builtin == -1) {
1443 /* An interface block instance */
Chris Forbesb934cb22016-03-29 16:14:02 +13001444 collect_interface_block_members(my_data, src, out, blocks, is_array_of_verts, id, type, is_patch);
Tobin Ehlis5b5e7bc2016-03-09 16:12:48 -07001445 }
1446 }
1447 }
1448}
1449
Chris Forbes6c94fb92016-03-30 11:35:21 +13001450static void collect_interface_by_descriptor_slot(layer_data *my_data, shader_module const *src,
Tobin Ehlis5b5e7bc2016-03-09 16:12:48 -07001451 std::unordered_set<uint32_t> const &accessible_ids,
1452 std::map<descriptor_slot_t, interface_var> &out) {
1453
1454 std::unordered_map<unsigned, unsigned> var_sets;
1455 std::unordered_map<unsigned, unsigned> var_bindings;
1456
1457 for (auto insn : *src) {
1458 /* All variables in the Uniform or UniformConstant storage classes are required to be decorated with both
1459 * DecorationDescriptorSet and DecorationBinding.
1460 */
1461 if (insn.opcode() == spv::OpDecorate) {
1462 if (insn.word(2) == spv::DecorationDescriptorSet) {
1463 var_sets[insn.word(1)] = insn.word(3);
1464 }
1465
1466 if (insn.word(2) == spv::DecorationBinding) {
1467 var_bindings[insn.word(1)] = insn.word(3);
1468 }
1469 }
1470 }
1471
1472 for (auto id : accessible_ids) {
1473 auto insn = src->get_def(id);
1474 assert(insn != src->end());
1475
1476 if (insn.opcode() == spv::OpVariable &&
1477 (insn.word(3) == spv::StorageClassUniform || insn.word(3) == spv::StorageClassUniformConstant)) {
1478 unsigned set = value_or_default(var_sets, insn.word(2), 0);
1479 unsigned binding = value_or_default(var_bindings, insn.word(2), 0);
1480
1481 auto existing_it = out.find(std::make_pair(set, binding));
1482 if (existing_it != out.end()) {
1483 /* conflict within spv image */
Chris Forbes080afef2016-03-30 13:14:22 +13001484 log_msg(my_data->report_data, VK_DEBUG_REPORT_ERROR_BIT_EXT, VkDebugReportObjectTypeEXT(0), 0,
Tobin Ehlis5b5e7bc2016-03-09 16:12:48 -07001485 __LINE__, SHADER_CHECKER_INCONSISTENT_SPIRV, "SC",
1486 "var %d (type %d) in %s interface in descriptor slot (%u,%u) conflicts with existing definition",
1487 insn.word(2), insn.word(1), storage_class_name(insn.word(3)), existing_it->first.first,
1488 existing_it->first.second);
1489 }
1490
1491 interface_var v;
1492 v.id = insn.word(2);
1493 v.type_id = insn.word(1);
Chris Forbesb934cb22016-03-29 16:14:02 +13001494 v.offset = 0;
1495 v.is_patch = false;
Tobin Ehlis5b5e7bc2016-03-09 16:12:48 -07001496 out[std::make_pair(set, binding)] = v;
1497 }
1498 }
1499}
1500
Chris Forbes6c94fb92016-03-30 11:35:21 +13001501static bool validate_interface_between_stages(layer_data *my_data, shader_module const *producer,
Chris Forbes031261d2016-03-29 16:41:07 +13001502 spirv_inst_iter producer_entrypoint, shader_stage_attributes const *producer_stage,
Tobin Ehlis5b5e7bc2016-03-09 16:12:48 -07001503 shader_module const *consumer, spirv_inst_iter consumer_entrypoint,
Chris Forbes031261d2016-03-29 16:41:07 +13001504 shader_stage_attributes const *consumer_stage) {
Tobin Ehlis5b5e7bc2016-03-09 16:12:48 -07001505 std::map<location_t, interface_var> outputs;
1506 std::map<location_t, interface_var> inputs;
1507
1508 bool pass = true;
1509
Chris Forbes031261d2016-03-29 16:41:07 +13001510 collect_interface_by_location(my_data, producer, producer_entrypoint, spv::StorageClassOutput, outputs, producer_stage->arrayed_output);
1511 collect_interface_by_location(my_data, consumer, consumer_entrypoint, spv::StorageClassInput, inputs, consumer_stage->arrayed_input);
Tobin Ehlis5b5e7bc2016-03-09 16:12:48 -07001512
1513 auto a_it = outputs.begin();
1514 auto b_it = inputs.begin();
1515
1516 /* maps sorted by key (location); walk them together to find mismatches */
1517 while ((outputs.size() > 0 && a_it != outputs.end()) || (inputs.size() && b_it != inputs.end())) {
1518 bool a_at_end = outputs.size() == 0 || a_it == outputs.end();
1519 bool b_at_end = inputs.size() == 0 || b_it == inputs.end();
1520 auto a_first = a_at_end ? std::make_pair(0u, 0u) : a_it->first;
1521 auto b_first = b_at_end ? std::make_pair(0u, 0u) : b_it->first;
1522
1523 if (b_at_end || ((!a_at_end) && (a_first < b_first))) {
Chris Forbes080afef2016-03-30 13:14:22 +13001524 if (log_msg(my_data->report_data, VK_DEBUG_REPORT_PERFORMANCE_WARNING_BIT_EXT, VkDebugReportObjectTypeEXT(0), 0,
1525 __LINE__, SHADER_CHECKER_OUTPUT_NOT_CONSUMED, "SC",
Chris Forbes031261d2016-03-29 16:41:07 +13001526 "%s writes to output location %u.%u which is not consumed by %s", producer_stage->name, a_first.first,
1527 a_first.second, consumer_stage->name)) {
Tobin Ehlis5b5e7bc2016-03-09 16:12:48 -07001528 pass = false;
1529 }
1530 a_it++;
1531 } else if (a_at_end || a_first > b_first) {
Chris Forbes080afef2016-03-30 13:14:22 +13001532 if (log_msg(my_data->report_data, VK_DEBUG_REPORT_ERROR_BIT_EXT, VkDebugReportObjectTypeEXT(0), 0,
Tobin Ehlis5b5e7bc2016-03-09 16:12:48 -07001533 __LINE__, SHADER_CHECKER_INPUT_NOT_PRODUCED, "SC",
Chris Forbes031261d2016-03-29 16:41:07 +13001534 "%s consumes input location %u.%u which is not written by %s", consumer_stage->name, b_first.first, b_first.second,
1535 producer_stage->name)) {
Tobin Ehlis5b5e7bc2016-03-09 16:12:48 -07001536 pass = false;
1537 }
1538 b_it++;
1539 } else {
Chris Forbes0f44c682016-03-29 16:57:02 +13001540 if (!types_match(producer, consumer, a_it->second.type_id, b_it->second.type_id,
Chris Forbes031261d2016-03-29 16:41:07 +13001541 producer_stage->arrayed_output && !a_it->second.is_patch,
Chris Forbes37576f92016-04-05 17:51:35 +12001542 consumer_stage->arrayed_input && !b_it->second.is_patch,
1543 true)) {
Chris Forbes080afef2016-03-30 13:14:22 +13001544 if (log_msg(my_data->report_data, VK_DEBUG_REPORT_ERROR_BIT_EXT, VkDebugReportObjectTypeEXT(0), 0,
Tobin Ehlis5b5e7bc2016-03-09 16:12:48 -07001545 __LINE__, SHADER_CHECKER_INTERFACE_TYPE_MISMATCH, "SC", "Type mismatch on location %u.%u: '%s' vs '%s'",
Chris Forbes9ccb7b12016-03-18 14:59:39 +13001546 a_first.first, a_first.second,
1547 describe_type(producer, a_it->second.type_id).c_str(),
1548 describe_type(consumer, b_it->second.type_id).c_str())) {
Tobin Ehlis5b5e7bc2016-03-09 16:12:48 -07001549 pass = false;
1550 }
1551 }
Chris Forbes0f44c682016-03-29 16:57:02 +13001552 if (a_it->second.is_patch != b_it->second.is_patch) {
1553 if (log_msg(my_data->report_data, VK_DEBUG_REPORT_ERROR_BIT_EXT, VK_DEBUG_REPORT_OBJECT_TYPE_DEVICE_EXT, /*dev*/ 0,
1554 __LINE__, SHADER_CHECKER_INTERFACE_TYPE_MISMATCH, "SC",
Chris Forbesf706c502016-04-04 19:19:47 +12001555 "Decoration mismatch on location %u.%u: is per-%s in %s stage but "
Chris Forbes0f44c682016-03-29 16:57:02 +13001556 "per-%s in %s stage", a_first.first, a_first.second,
1557 a_it->second.is_patch ? "patch" : "vertex", producer_stage->name,
1558 b_it->second.is_patch ? "patch" : "vertex", consumer_stage->name)) {
1559 pass = false;
1560 }
1561 }
Tobin Ehlis5b5e7bc2016-03-09 16:12:48 -07001562 a_it++;
1563 b_it++;
1564 }
1565 }
1566
1567 return pass;
1568}
1569
1570enum FORMAT_TYPE {
1571 FORMAT_TYPE_UNDEFINED,
1572 FORMAT_TYPE_FLOAT, /* UNORM, SNORM, FLOAT, USCALED, SSCALED, SRGB -- anything we consider float in the shader */
1573 FORMAT_TYPE_SINT,
1574 FORMAT_TYPE_UINT,
1575};
1576
1577static unsigned get_format_type(VkFormat fmt) {
1578 switch (fmt) {
1579 case VK_FORMAT_UNDEFINED:
1580 return FORMAT_TYPE_UNDEFINED;
1581 case VK_FORMAT_R8_SINT:
1582 case VK_FORMAT_R8G8_SINT:
1583 case VK_FORMAT_R8G8B8_SINT:
1584 case VK_FORMAT_R8G8B8A8_SINT:
1585 case VK_FORMAT_R16_SINT:
1586 case VK_FORMAT_R16G16_SINT:
1587 case VK_FORMAT_R16G16B16_SINT:
1588 case VK_FORMAT_R16G16B16A16_SINT:
1589 case VK_FORMAT_R32_SINT:
1590 case VK_FORMAT_R32G32_SINT:
1591 case VK_FORMAT_R32G32B32_SINT:
1592 case VK_FORMAT_R32G32B32A32_SINT:
1593 case VK_FORMAT_B8G8R8_SINT:
1594 case VK_FORMAT_B8G8R8A8_SINT:
1595 case VK_FORMAT_A2B10G10R10_SINT_PACK32:
1596 case VK_FORMAT_A2R10G10B10_SINT_PACK32:
1597 return FORMAT_TYPE_SINT;
1598 case VK_FORMAT_R8_UINT:
1599 case VK_FORMAT_R8G8_UINT:
1600 case VK_FORMAT_R8G8B8_UINT:
1601 case VK_FORMAT_R8G8B8A8_UINT:
1602 case VK_FORMAT_R16_UINT:
1603 case VK_FORMAT_R16G16_UINT:
1604 case VK_FORMAT_R16G16B16_UINT:
1605 case VK_FORMAT_R16G16B16A16_UINT:
1606 case VK_FORMAT_R32_UINT:
1607 case VK_FORMAT_R32G32_UINT:
1608 case VK_FORMAT_R32G32B32_UINT:
1609 case VK_FORMAT_R32G32B32A32_UINT:
1610 case VK_FORMAT_B8G8R8_UINT:
1611 case VK_FORMAT_B8G8R8A8_UINT:
1612 case VK_FORMAT_A2B10G10R10_UINT_PACK32:
1613 case VK_FORMAT_A2R10G10B10_UINT_PACK32:
1614 return FORMAT_TYPE_UINT;
1615 default:
1616 return FORMAT_TYPE_FLOAT;
1617 }
1618}
1619
1620/* characterizes a SPIR-V type appearing in an interface to a FF stage,
1621 * for comparison to a VkFormat's characterization above. */
1622static unsigned get_fundamental_type(shader_module const *src, unsigned type) {
1623 auto insn = src->get_def(type);
1624 assert(insn != src->end());
1625
1626 switch (insn.opcode()) {
1627 case spv::OpTypeInt:
1628 return insn.word(3) ? FORMAT_TYPE_SINT : FORMAT_TYPE_UINT;
1629 case spv::OpTypeFloat:
1630 return FORMAT_TYPE_FLOAT;
1631 case spv::OpTypeVector:
1632 return get_fundamental_type(src, insn.word(2));
1633 case spv::OpTypeMatrix:
1634 return get_fundamental_type(src, insn.word(2));
1635 case spv::OpTypeArray:
1636 return get_fundamental_type(src, insn.word(2));
1637 case spv::OpTypePointer:
1638 return get_fundamental_type(src, insn.word(3));
1639 default:
1640 return FORMAT_TYPE_UNDEFINED;
1641 }
1642}
1643
1644static uint32_t get_shader_stage_id(VkShaderStageFlagBits stage) {
1645 uint32_t bit_pos = u_ffs(stage);
1646 return bit_pos - 1;
1647}
1648
Chris Forbes6c94fb92016-03-30 11:35:21 +13001649static bool validate_vi_consistency(layer_data *my_data, VkPipelineVertexInputStateCreateInfo const *vi) {
Tobin Ehlis5b5e7bc2016-03-09 16:12:48 -07001650 /* walk the binding descriptions, which describe the step rate and stride of each vertex buffer.
1651 * each binding should be specified only once.
1652 */
1653 std::unordered_map<uint32_t, VkVertexInputBindingDescription const *> bindings;
1654 bool pass = true;
1655
1656 for (unsigned i = 0; i < vi->vertexBindingDescriptionCount; i++) {
1657 auto desc = &vi->pVertexBindingDescriptions[i];
1658 auto &binding = bindings[desc->binding];
1659 if (binding) {
Chris Forbes080afef2016-03-30 13:14:22 +13001660 if (log_msg(my_data->report_data, VK_DEBUG_REPORT_ERROR_BIT_EXT, VkDebugReportObjectTypeEXT(0), 0,
Tobin Ehlis5b5e7bc2016-03-09 16:12:48 -07001661 __LINE__, SHADER_CHECKER_INCONSISTENT_VI, "SC",
1662 "Duplicate vertex input binding descriptions for binding %d", desc->binding)) {
1663 pass = false;
1664 }
1665 } else {
1666 binding = desc;
1667 }
1668 }
1669
1670 return pass;
1671}
1672
Chris Forbes6c94fb92016-03-30 11:35:21 +13001673static bool validate_vi_against_vs_inputs(layer_data *my_data, VkPipelineVertexInputStateCreateInfo const *vi,
Tobin Ehlis5b5e7bc2016-03-09 16:12:48 -07001674 shader_module const *vs, spirv_inst_iter entrypoint) {
1675 std::map<location_t, interface_var> inputs;
1676 bool pass = true;
1677
Chris Forbes6c94fb92016-03-30 11:35:21 +13001678 collect_interface_by_location(my_data, vs, entrypoint, spv::StorageClassInput, inputs, false);
Tobin Ehlis5b5e7bc2016-03-09 16:12:48 -07001679
1680 /* Build index by location */
1681 std::map<uint32_t, VkVertexInputAttributeDescription const *> attribs;
1682 if (vi) {
1683 for (unsigned i = 0; i < vi->vertexAttributeDescriptionCount; i++)
1684 attribs[vi->pVertexAttributeDescriptions[i].location] = &vi->pVertexAttributeDescriptions[i];
1685 }
1686
1687 auto it_a = attribs.begin();
1688 auto it_b = inputs.begin();
1689
1690 while ((attribs.size() > 0 && it_a != attribs.end()) || (inputs.size() > 0 && it_b != inputs.end())) {
1691 bool a_at_end = attribs.size() == 0 || it_a == attribs.end();
1692 bool b_at_end = inputs.size() == 0 || it_b == inputs.end();
1693 auto a_first = a_at_end ? 0 : it_a->first;
1694 auto b_first = b_at_end ? 0 : it_b->first.first;
1695 if (!a_at_end && (b_at_end || a_first < b_first)) {
Chris Forbes080afef2016-03-30 13:14:22 +13001696 if (log_msg(my_data->report_data, VK_DEBUG_REPORT_PERFORMANCE_WARNING_BIT_EXT, VkDebugReportObjectTypeEXT(0), 0,
1697 __LINE__, SHADER_CHECKER_OUTPUT_NOT_CONSUMED, "SC",
Tobin Ehlis5b5e7bc2016-03-09 16:12:48 -07001698 "Vertex attribute at location %d not consumed by VS", a_first)) {
1699 pass = false;
1700 }
1701 it_a++;
1702 } else if (!b_at_end && (a_at_end || b_first < a_first)) {
1703 if (log_msg(my_data->report_data, VK_DEBUG_REPORT_ERROR_BIT_EXT, VK_DEBUG_REPORT_OBJECT_TYPE_DEVICE_EXT, /*dev*/ 0,
1704 __LINE__, SHADER_CHECKER_INPUT_NOT_PRODUCED, "SC", "VS consumes input at location %d but not provided",
1705 b_first)) {
1706 pass = false;
1707 }
1708 it_b++;
1709 } else {
1710 unsigned attrib_type = get_format_type(it_a->second->format);
1711 unsigned input_type = get_fundamental_type(vs, it_b->second.type_id);
1712
1713 /* type checking */
1714 if (attrib_type != FORMAT_TYPE_UNDEFINED && input_type != FORMAT_TYPE_UNDEFINED && attrib_type != input_type) {
Chris Forbes080afef2016-03-30 13:14:22 +13001715 if (log_msg(my_data->report_data, VK_DEBUG_REPORT_ERROR_BIT_EXT, VkDebugReportObjectTypeEXT(0), 0,
Tobin Ehlis5b5e7bc2016-03-09 16:12:48 -07001716 __LINE__, SHADER_CHECKER_INTERFACE_TYPE_MISMATCH, "SC",
1717 "Attribute type of `%s` at location %d does not match VS input type of `%s`",
Chris Forbes9ccb7b12016-03-18 14:59:39 +13001718 string_VkFormat(it_a->second->format), a_first,
1719 describe_type(vs, it_b->second.type_id).c_str())) {
Tobin Ehlis5b5e7bc2016-03-09 16:12:48 -07001720 pass = false;
1721 }
1722 }
1723
1724 /* OK! */
1725 it_a++;
1726 it_b++;
1727 }
1728 }
1729
1730 return pass;
1731}
1732
Chris Forbes6c94fb92016-03-30 11:35:21 +13001733static bool validate_fs_outputs_against_render_pass(layer_data *my_data, shader_module const *fs,
Tobin Ehlis5b5e7bc2016-03-09 16:12:48 -07001734 spirv_inst_iter entrypoint, RENDER_PASS_NODE const *rp, uint32_t subpass) {
1735 const std::vector<VkFormat> &color_formats = rp->subpassColorFormats[subpass];
1736 std::map<location_t, interface_var> outputs;
1737 bool pass = true;
1738
1739 /* TODO: dual source blend index (spv::DecIndex, zero if not provided) */
1740
Chris Forbes6c94fb92016-03-30 11:35:21 +13001741 collect_interface_by_location(my_data, fs, entrypoint, spv::StorageClassOutput, outputs, false);
Tobin Ehlis5b5e7bc2016-03-09 16:12:48 -07001742
1743 auto it = outputs.begin();
1744 uint32_t attachment = 0;
1745
1746 /* Walk attachment list and outputs together -- this is a little overpowered since attachments
1747 * are currently dense, but the parallel with matching between shader stages is nice.
1748 */
1749
1750 while ((outputs.size() > 0 && it != outputs.end()) || attachment < color_formats.size()) {
1751 if (attachment == color_formats.size() || (it != outputs.end() && it->first.first < attachment)) {
Chris Forbes080afef2016-03-30 13:14:22 +13001752 if (log_msg(my_data->report_data, VK_DEBUG_REPORT_WARNING_BIT_EXT, VkDebugReportObjectTypeEXT(0), 0,
Tobin Ehlis5b5e7bc2016-03-09 16:12:48 -07001753 __LINE__, SHADER_CHECKER_OUTPUT_NOT_CONSUMED, "SC",
1754 "FS writes to output location %d with no matching attachment", it->first.first)) {
1755 pass = false;
1756 }
1757 it++;
1758 } else if (it == outputs.end() || it->first.first > attachment) {
Chris Forbes080afef2016-03-30 13:14:22 +13001759 if (log_msg(my_data->report_data, VK_DEBUG_REPORT_ERROR_BIT_EXT, VkDebugReportObjectTypeEXT(0), 0,
Tobin Ehlis5b5e7bc2016-03-09 16:12:48 -07001760 __LINE__, SHADER_CHECKER_INPUT_NOT_PRODUCED, "SC", "Attachment %d not written by FS", attachment)) {
1761 pass = false;
1762 }
1763 attachment++;
1764 } else {
1765 unsigned output_type = get_fundamental_type(fs, it->second.type_id);
1766 unsigned att_type = get_format_type(color_formats[attachment]);
1767
1768 /* type checking */
1769 if (att_type != FORMAT_TYPE_UNDEFINED && output_type != FORMAT_TYPE_UNDEFINED && att_type != output_type) {
Chris Forbes080afef2016-03-30 13:14:22 +13001770 if (log_msg(my_data->report_data, VK_DEBUG_REPORT_ERROR_BIT_EXT, VkDebugReportObjectTypeEXT(0), 0,
Tobin Ehlis5b5e7bc2016-03-09 16:12:48 -07001771 __LINE__, SHADER_CHECKER_INTERFACE_TYPE_MISMATCH, "SC",
1772 "Attachment %d of type `%s` does not match FS output type of `%s`", attachment,
Chris Forbes9ccb7b12016-03-18 14:59:39 +13001773 string_VkFormat(color_formats[attachment]),
1774 describe_type(fs, it->second.type_id).c_str())) {
Tobin Ehlis5b5e7bc2016-03-09 16:12:48 -07001775 pass = false;
1776 }
1777 }
1778
1779 /* OK! */
1780 it++;
1781 attachment++;
1782 }
1783 }
1784
1785 return pass;
1786}
1787
1788/* For some analyses, we need to know about all ids referenced by the static call tree of a particular
1789 * entrypoint. This is important for identifying the set of shader resources actually used by an entrypoint,
1790 * for example.
1791 * Note: we only explore parts of the image which might actually contain ids we care about for the above analyses.
1792 * - NOT the shader input/output interfaces.
1793 *
1794 * TODO: The set of interesting opcodes here was determined by eyeballing the SPIRV spec. It might be worth
1795 * converting parts of this to be generated from the machine-readable spec instead.
1796 */
1797static void mark_accessible_ids(shader_module const *src, spirv_inst_iter entrypoint, std::unordered_set<uint32_t> &ids) {
1798 std::unordered_set<uint32_t> worklist;
1799 worklist.insert(entrypoint.word(2));
1800
1801 while (!worklist.empty()) {
1802 auto id_iter = worklist.begin();
1803 auto id = *id_iter;
1804 worklist.erase(id_iter);
1805
1806 auto insn = src->get_def(id);
1807 if (insn == src->end()) {
Eric Engestrom81264dd2016-04-02 22:06:13 +01001808 /* id is something we didn't collect in build_def_index. that's OK -- we'll stumble
Tobin Ehlis5b5e7bc2016-03-09 16:12:48 -07001809 * across all kinds of things here that we may not care about. */
1810 continue;
1811 }
1812
1813 /* try to add to the output set */
1814 if (!ids.insert(id).second) {
1815 continue; /* if we already saw this id, we don't want to walk it again. */
1816 }
1817
1818 switch (insn.opcode()) {
1819 case spv::OpFunction:
1820 /* scan whole body of the function, enlisting anything interesting */
1821 while (++insn, insn.opcode() != spv::OpFunctionEnd) {
1822 switch (insn.opcode()) {
1823 case spv::OpLoad:
1824 case spv::OpAtomicLoad:
1825 case spv::OpAtomicExchange:
1826 case spv::OpAtomicCompareExchange:
1827 case spv::OpAtomicCompareExchangeWeak:
1828 case spv::OpAtomicIIncrement:
1829 case spv::OpAtomicIDecrement:
1830 case spv::OpAtomicIAdd:
1831 case spv::OpAtomicISub:
1832 case spv::OpAtomicSMin:
1833 case spv::OpAtomicUMin:
1834 case spv::OpAtomicSMax:
1835 case spv::OpAtomicUMax:
1836 case spv::OpAtomicAnd:
1837 case spv::OpAtomicOr:
1838 case spv::OpAtomicXor:
1839 worklist.insert(insn.word(3)); /* ptr */
1840 break;
1841 case spv::OpStore:
1842 case spv::OpAtomicStore:
1843 worklist.insert(insn.word(1)); /* ptr */
1844 break;
1845 case spv::OpAccessChain:
1846 case spv::OpInBoundsAccessChain:
1847 worklist.insert(insn.word(3)); /* base ptr */
1848 break;
1849 case spv::OpSampledImage:
1850 case spv::OpImageSampleImplicitLod:
1851 case spv::OpImageSampleExplicitLod:
1852 case spv::OpImageSampleDrefImplicitLod:
1853 case spv::OpImageSampleDrefExplicitLod:
1854 case spv::OpImageSampleProjImplicitLod:
1855 case spv::OpImageSampleProjExplicitLod:
1856 case spv::OpImageSampleProjDrefImplicitLod:
1857 case spv::OpImageSampleProjDrefExplicitLod:
1858 case spv::OpImageFetch:
1859 case spv::OpImageGather:
1860 case spv::OpImageDrefGather:
1861 case spv::OpImageRead:
1862 case spv::OpImage:
1863 case spv::OpImageQueryFormat:
1864 case spv::OpImageQueryOrder:
1865 case spv::OpImageQuerySizeLod:
1866 case spv::OpImageQuerySize:
1867 case spv::OpImageQueryLod:
1868 case spv::OpImageQueryLevels:
1869 case spv::OpImageQuerySamples:
1870 case spv::OpImageSparseSampleImplicitLod:
1871 case spv::OpImageSparseSampleExplicitLod:
1872 case spv::OpImageSparseSampleDrefImplicitLod:
1873 case spv::OpImageSparseSampleDrefExplicitLod:
1874 case spv::OpImageSparseSampleProjImplicitLod:
1875 case spv::OpImageSparseSampleProjExplicitLod:
1876 case spv::OpImageSparseSampleProjDrefImplicitLod:
1877 case spv::OpImageSparseSampleProjDrefExplicitLod:
1878 case spv::OpImageSparseFetch:
1879 case spv::OpImageSparseGather:
1880 case spv::OpImageSparseDrefGather:
1881 case spv::OpImageTexelPointer:
1882 worklist.insert(insn.word(3)); /* image or sampled image */
1883 break;
1884 case spv::OpImageWrite:
1885 worklist.insert(insn.word(1)); /* image -- different operand order to above */
1886 break;
1887 case spv::OpFunctionCall:
Michael Mc Donnellc15b8012016-04-03 14:47:51 -07001888 for (uint32_t i = 3; i < insn.len(); i++) {
Tobin Ehlis5b5e7bc2016-03-09 16:12:48 -07001889 worklist.insert(insn.word(i)); /* fn itself, and all args */
1890 }
1891 break;
1892
1893 case spv::OpExtInst:
Michael Mc Donnellc15b8012016-04-03 14:47:51 -07001894 for (uint32_t i = 5; i < insn.len(); i++) {
Tobin Ehlis5b5e7bc2016-03-09 16:12:48 -07001895 worklist.insert(insn.word(i)); /* operands to ext inst */
1896 }
1897 break;
1898 }
1899 }
1900 break;
1901 }
1902 }
1903}
1904
Chris Forbes6c94fb92016-03-30 11:35:21 +13001905static bool validate_push_constant_block_against_pipeline(layer_data *my_data,
Tobin Ehlis5b5e7bc2016-03-09 16:12:48 -07001906 std::vector<VkPushConstantRange> const *pushConstantRanges,
1907 shader_module const *src, spirv_inst_iter type,
1908 VkShaderStageFlagBits stage) {
1909 bool pass = true;
1910
1911 /* strip off ptrs etc */
1912 type = get_struct_type(src, type, false);
1913 assert(type != src->end());
1914
1915 /* validate directly off the offsets. this isn't quite correct for arrays
1916 * and matrices, but is a good first step. TODO: arrays, matrices, weird
1917 * sizes */
1918 for (auto insn : *src) {
1919 if (insn.opcode() == spv::OpMemberDecorate && insn.word(1) == type.word(1)) {
1920
1921 if (insn.word(3) == spv::DecorationOffset) {
1922 unsigned offset = insn.word(4);
1923 auto size = 4; /* bytes; TODO: calculate this based on the type */
1924
1925 bool found_range = false;
1926 for (auto const &range : *pushConstantRanges) {
1927 if (range.offset <= offset && range.offset + range.size >= offset + size) {
1928 found_range = true;
1929
1930 if ((range.stageFlags & stage) == 0) {
Chris Forbes080afef2016-03-30 13:14:22 +13001931 if (log_msg(my_data->report_data, VK_DEBUG_REPORT_ERROR_BIT_EXT, VkDebugReportObjectTypeEXT(0), 0,
1932 __LINE__, SHADER_CHECKER_PUSH_CONSTANT_NOT_ACCESSIBLE_FROM_STAGE, "SC",
Tobin Ehlis5b5e7bc2016-03-09 16:12:48 -07001933 "Push constant range covering variable starting at "
1934 "offset %u not accessible from stage %s",
1935 offset, string_VkShaderStageFlagBits(stage))) {
1936 pass = false;
1937 }
1938 }
1939
1940 break;
1941 }
1942 }
1943
1944 if (!found_range) {
Chris Forbes080afef2016-03-30 13:14:22 +13001945 if (log_msg(my_data->report_data, VK_DEBUG_REPORT_ERROR_BIT_EXT, VkDebugReportObjectTypeEXT(0), 0,
1946 __LINE__, SHADER_CHECKER_PUSH_CONSTANT_OUT_OF_RANGE, "SC",
Tobin Ehlis5b5e7bc2016-03-09 16:12:48 -07001947 "Push constant range covering variable starting at "
1948 "offset %u not declared in layout",
1949 offset)) {
1950 pass = false;
1951 }
1952 }
1953 }
1954 }
1955 }
1956
1957 return pass;
1958}
1959
Chris Forbes6c94fb92016-03-30 11:35:21 +13001960static bool validate_push_constant_usage(layer_data *my_data,
Tobin Ehlis5b5e7bc2016-03-09 16:12:48 -07001961 std::vector<VkPushConstantRange> const *pushConstantRanges, shader_module const *src,
1962 std::unordered_set<uint32_t> accessible_ids, VkShaderStageFlagBits stage) {
1963 bool pass = true;
1964
1965 for (auto id : accessible_ids) {
1966 auto def_insn = src->get_def(id);
1967 if (def_insn.opcode() == spv::OpVariable && def_insn.word(3) == spv::StorageClassPushConstant) {
Chris Forbes080afef2016-03-30 13:14:22 +13001968 pass &= validate_push_constant_block_against_pipeline(my_data, pushConstantRanges, src,
1969 src->get_def(def_insn.word(1)), stage);
Tobin Ehlis5b5e7bc2016-03-09 16:12:48 -07001970 }
1971 }
1972
1973 return pass;
1974}
1975
1976// For given pipelineLayout verify that the setLayout at slot.first
1977// has the requested binding at slot.second
Chris Forbes78be5012016-03-30 12:12:01 +13001978static VkDescriptorSetLayoutBinding const * get_descriptor_binding(layer_data *my_data, PIPELINE_LAYOUT_NODE *pipelineLayout, descriptor_slot_t slot) {
Tobin Ehlis5b5e7bc2016-03-09 16:12:48 -07001979
1980 if (!pipelineLayout)
Chris Forbes37083de2016-03-18 11:14:27 +13001981 return nullptr;
Tobin Ehlis5b5e7bc2016-03-09 16:12:48 -07001982
Chris Forbes78be5012016-03-30 12:12:01 +13001983 if (slot.first >= pipelineLayout->descriptorSetLayouts.size())
Chris Forbes37083de2016-03-18 11:14:27 +13001984 return nullptr;
Tobin Ehlis5b5e7bc2016-03-09 16:12:48 -07001985
Chris Forbes78be5012016-03-30 12:12:01 +13001986 auto const layout_node = my_data->descriptorSetLayoutMap[pipelineLayout->descriptorSetLayouts[slot.first]];
Tobin Ehlis5b5e7bc2016-03-09 16:12:48 -07001987
1988 auto bindingIt = layout_node->bindingToIndexMap.find(slot.second);
Tobin Ehlis14fbd082016-03-15 15:52:11 -06001989 if ((bindingIt == layout_node->bindingToIndexMap.end()) || (layout_node->createInfo.pBindings == NULL))
Chris Forbes37083de2016-03-18 11:14:27 +13001990 return nullptr;
Tobin Ehlis5b5e7bc2016-03-09 16:12:48 -07001991
Tobin Ehlis14fbd082016-03-15 15:52:11 -06001992 assert(bindingIt->second < layout_node->createInfo.bindingCount);
Chris Forbes37083de2016-03-18 11:14:27 +13001993 return &layout_node->createInfo.pBindings[bindingIt->second];
Tobin Ehlis5b5e7bc2016-03-09 16:12:48 -07001994}
1995
1996// Block of code at start here for managing/tracking Pipeline state that this layer cares about
1997
1998static uint64_t g_drawCount[NUM_DRAW_TYPES] = {0, 0, 0, 0};
1999
2000// TODO : Should be tracking lastBound per commandBuffer and when draws occur, report based on that cmd buffer lastBound
2001// Then need to synchronize the accesses based on cmd buffer so that if I'm reading state on one cmd buffer, updates
2002// to that same cmd buffer by separate thread are not changing state from underneath us
2003// Track the last cmd buffer touched by this thread
2004
Dustin Gravese3319182016-04-05 09:41:17 -06002005static bool hasDrawCmd(GLOBAL_CB_NODE *pCB) {
Tobin Ehlis5b5e7bc2016-03-09 16:12:48 -07002006 for (uint32_t i = 0; i < NUM_DRAW_TYPES; i++) {
2007 if (pCB->drawCount[i])
Dustin Gravese3319182016-04-05 09:41:17 -06002008 return true;
Tobin Ehlis5b5e7bc2016-03-09 16:12:48 -07002009 }
Dustin Gravese3319182016-04-05 09:41:17 -06002010 return false;
Tobin Ehlis5b5e7bc2016-03-09 16:12:48 -07002011}
2012
2013// Check object status for selected flag state
Dustin Gravese3319182016-04-05 09:41:17 -06002014static bool validate_status(layer_data *my_data, GLOBAL_CB_NODE *pNode, CBStatusFlags status_mask, VkFlags msg_flags,
2015 DRAW_STATE_ERROR error_code, const char *fail_msg) {
Tobin Ehlis3d3e06b2016-03-28 11:18:19 -06002016 if (!(pNode->status & status_mask)) {
2017 return log_msg(my_data->report_data, msg_flags, VK_DEBUG_REPORT_OBJECT_TYPE_COMMAND_BUFFER_EXT,
2018 reinterpret_cast<const uint64_t &>(pNode->commandBuffer), __LINE__, error_code, "DS",
2019 "CB object %#" PRIxLEAST64 ": %s", reinterpret_cast<const uint64_t &>(pNode->commandBuffer), fail_msg);
Tobin Ehlis5b5e7bc2016-03-09 16:12:48 -07002020 }
Dustin Gravese3319182016-04-05 09:41:17 -06002021 return false;
Tobin Ehlis5b5e7bc2016-03-09 16:12:48 -07002022}
2023
2024// Retrieve pipeline node ptr for given pipeline object
2025static PIPELINE_NODE *getPipeline(layer_data *my_data, const VkPipeline pipeline) {
2026 if (my_data->pipelineMap.find(pipeline) == my_data->pipelineMap.end()) {
2027 return NULL;
2028 }
2029 return my_data->pipelineMap[pipeline];
2030}
2031
Dustin Gravese3319182016-04-05 09:41:17 -06002032// Return true if for a given PSO, the given state enum is dynamic, else return false
2033static bool isDynamic(const PIPELINE_NODE *pPipeline, const VkDynamicState state) {
Tobin Ehlis5b5e7bc2016-03-09 16:12:48 -07002034 if (pPipeline && pPipeline->graphicsPipelineCI.pDynamicState) {
2035 for (uint32_t i = 0; i < pPipeline->graphicsPipelineCI.pDynamicState->dynamicStateCount; i++) {
2036 if (state == pPipeline->graphicsPipelineCI.pDynamicState->pDynamicStates[i])
Dustin Gravese3319182016-04-05 09:41:17 -06002037 return true;
Tobin Ehlis5b5e7bc2016-03-09 16:12:48 -07002038 }
2039 }
Dustin Gravese3319182016-04-05 09:41:17 -06002040 return false;
Tobin Ehlis5b5e7bc2016-03-09 16:12:48 -07002041}
2042
2043// Validate state stored as flags at time of draw call
Dustin Gravese3319182016-04-05 09:41:17 -06002044static bool validate_draw_state_flags(layer_data *dev_data, GLOBAL_CB_NODE *pCB, const PIPELINE_NODE *pPipe, bool indexedDraw) {
2045 bool result;
Tobin Ehlis3d3e06b2016-03-28 11:18:19 -06002046 result = validate_status(dev_data, pCB, CBSTATUS_VIEWPORT_SET, VK_DEBUG_REPORT_ERROR_BIT_EXT, DRAWSTATE_VIEWPORT_NOT_BOUND,
2047 "Dynamic viewport state not set for this command buffer");
2048 result |= validate_status(dev_data, pCB, CBSTATUS_SCISSOR_SET, VK_DEBUG_REPORT_ERROR_BIT_EXT, DRAWSTATE_SCISSOR_NOT_BOUND,
2049 "Dynamic scissor state not set for this command buffer");
Tobin Ehlisca546212016-04-01 13:51:33 -06002050 if (pPipe->graphicsPipelineCI.pInputAssemblyState &&
2051 ((pPipe->graphicsPipelineCI.pInputAssemblyState->topology == VK_PRIMITIVE_TOPOLOGY_LINE_LIST) ||
2052 (pPipe->graphicsPipelineCI.pInputAssemblyState->topology == VK_PRIMITIVE_TOPOLOGY_LINE_STRIP))) {
Tobin Ehlis3d3e06b2016-03-28 11:18:19 -06002053 result |= validate_status(dev_data, pCB, CBSTATUS_LINE_WIDTH_SET, VK_DEBUG_REPORT_ERROR_BIT_EXT,
2054 DRAWSTATE_LINE_WIDTH_NOT_BOUND, "Dynamic line width state not set for this command buffer");
2055 }
Dustin Graves45824952016-04-05 15:15:40 -06002056 if (pPipe->graphicsPipelineCI.pRasterizationState &&
2057 (pPipe->graphicsPipelineCI.pRasterizationState->depthBiasEnable == VK_TRUE)) {
Tobin Ehlis3d3e06b2016-03-28 11:18:19 -06002058 result |= validate_status(dev_data, pCB, CBSTATUS_DEPTH_BIAS_SET, VK_DEBUG_REPORT_ERROR_BIT_EXT,
2059 DRAWSTATE_DEPTH_BIAS_NOT_BOUND, "Dynamic depth bias state not set for this command buffer");
2060 }
2061 if (pPipe->blendConstantsEnabled) {
2062 result |= validate_status(dev_data, pCB, CBSTATUS_BLEND_CONSTANTS_SET, VK_DEBUG_REPORT_ERROR_BIT_EXT,
2063 DRAWSTATE_BLEND_NOT_BOUND, "Dynamic blend constants state not set for this command buffer");
2064 }
Dustin Graves45824952016-04-05 15:15:40 -06002065 if (pPipe->graphicsPipelineCI.pDepthStencilState &&
2066 (pPipe->graphicsPipelineCI.pDepthStencilState->depthBoundsTestEnable == VK_TRUE)) {
Tobin Ehlis3d3e06b2016-03-28 11:18:19 -06002067 result |= validate_status(dev_data, pCB, CBSTATUS_DEPTH_BOUNDS_SET, VK_DEBUG_REPORT_ERROR_BIT_EXT,
2068 DRAWSTATE_DEPTH_BOUNDS_NOT_BOUND, "Dynamic depth bounds state not set for this command buffer");
2069 }
Dustin Graves45824952016-04-05 15:15:40 -06002070 if (pPipe->graphicsPipelineCI.pDepthStencilState &&
2071 (pPipe->graphicsPipelineCI.pDepthStencilState->stencilTestEnable == VK_TRUE)) {
Tobin Ehlis3d3e06b2016-03-28 11:18:19 -06002072 result |= validate_status(dev_data, pCB, CBSTATUS_STENCIL_READ_MASK_SET, VK_DEBUG_REPORT_ERROR_BIT_EXT,
2073 DRAWSTATE_STENCIL_NOT_BOUND, "Dynamic stencil read mask state not set for this command buffer");
2074 result |= validate_status(dev_data, pCB, CBSTATUS_STENCIL_WRITE_MASK_SET, VK_DEBUG_REPORT_ERROR_BIT_EXT,
2075 DRAWSTATE_STENCIL_NOT_BOUND, "Dynamic stencil write mask state not set for this command buffer");
2076 result |= validate_status(dev_data, pCB, CBSTATUS_STENCIL_REFERENCE_SET, VK_DEBUG_REPORT_ERROR_BIT_EXT,
2077 DRAWSTATE_STENCIL_NOT_BOUND, "Dynamic stencil reference state not set for this command buffer");
2078 }
2079 if (indexedDraw) {
2080 result |= validate_status(dev_data, pCB, CBSTATUS_INDEX_BUFFER_BOUND, VK_DEBUG_REPORT_ERROR_BIT_EXT,
2081 DRAWSTATE_INDEX_BUFFER_NOT_BOUND,
Tobin Ehlis5b5e7bc2016-03-09 16:12:48 -07002082 "Index buffer object not bound to this command buffer when Indexed Draw attempted");
Tobin Ehlis3d3e06b2016-03-28 11:18:19 -06002083 }
Tobin Ehlis5b5e7bc2016-03-09 16:12:48 -07002084 return result;
2085}
2086
2087// Verify attachment reference compatibility according to spec
2088// If one array is larger, treat missing elements of shorter array as VK_ATTACHMENT_UNUSED & other array much match this
2089// If both AttachmentReference arrays have requested index, check their corresponding AttachementDescriptions
2090// to make sure that format and samples counts match.
2091// If not, they are not compatible.
2092static bool attachment_references_compatible(const uint32_t index, const VkAttachmentReference *pPrimary,
2093 const uint32_t primaryCount, const VkAttachmentDescription *pPrimaryAttachments,
2094 const VkAttachmentReference *pSecondary, const uint32_t secondaryCount,
2095 const VkAttachmentDescription *pSecondaryAttachments) {
2096 if (index >= primaryCount) { // Check secondary as if primary is VK_ATTACHMENT_UNUSED
Mark Young06e96352016-03-24 10:14:35 -06002097 if (VK_ATTACHMENT_UNUSED == pSecondary[index].attachment)
2098 return true;
Tobin Ehlis5b5e7bc2016-03-09 16:12:48 -07002099 } else if (index >= secondaryCount) { // Check primary as if secondary is VK_ATTACHMENT_UNUSED
Mark Young06e96352016-03-24 10:14:35 -06002100 if (VK_ATTACHMENT_UNUSED == pPrimary[index].attachment)
2101 return true;
Tobin Ehlis5b5e7bc2016-03-09 16:12:48 -07002102 } else { // format and sample count must match
2103 if ((pPrimaryAttachments[pPrimary[index].attachment].format ==
2104 pSecondaryAttachments[pSecondary[index].attachment].format) &&
2105 (pPrimaryAttachments[pPrimary[index].attachment].samples ==
2106 pSecondaryAttachments[pSecondary[index].attachment].samples))
2107 return true;
2108 }
2109 // Format and sample counts didn't match
2110 return false;
2111}
2112
2113// For give primary and secondary RenderPass objects, verify that they're compatible
2114static bool verify_renderpass_compatibility(layer_data *my_data, const VkRenderPass primaryRP, const VkRenderPass secondaryRP,
2115 string &errorMsg) {
Tobin Ehlis5b5e7bc2016-03-09 16:12:48 -07002116 if (my_data->renderPassMap.find(primaryRP) == my_data->renderPassMap.end()) {
Chris Forbesc07fa782016-04-14 10:30:01 +12002117 stringstream errorStr;
Tobin Ehlis5b5e7bc2016-03-09 16:12:48 -07002118 errorStr << "invalid VkRenderPass (" << primaryRP << ")";
2119 errorMsg = errorStr.str();
2120 return false;
2121 } else if (my_data->renderPassMap.find(secondaryRP) == my_data->renderPassMap.end()) {
Chris Forbesc07fa782016-04-14 10:30:01 +12002122 stringstream errorStr;
Tobin Ehlis5b5e7bc2016-03-09 16:12:48 -07002123 errorStr << "invalid VkRenderPass (" << secondaryRP << ")";
2124 errorMsg = errorStr.str();
2125 return false;
2126 }
2127 // Trivial pass case is exact same RP
2128 if (primaryRP == secondaryRP) {
2129 return true;
2130 }
2131 const VkRenderPassCreateInfo *primaryRPCI = my_data->renderPassMap[primaryRP]->pCreateInfo;
2132 const VkRenderPassCreateInfo *secondaryRPCI = my_data->renderPassMap[secondaryRP]->pCreateInfo;
2133 if (primaryRPCI->subpassCount != secondaryRPCI->subpassCount) {
Chris Forbesc07fa782016-04-14 10:30:01 +12002134 stringstream errorStr;
Tobin Ehlis5b5e7bc2016-03-09 16:12:48 -07002135 errorStr << "RenderPass for primary cmdBuffer has " << primaryRPCI->subpassCount
2136 << " subpasses but renderPass for secondary cmdBuffer has " << secondaryRPCI->subpassCount << " subpasses.";
2137 errorMsg = errorStr.str();
2138 return false;
2139 }
2140 uint32_t spIndex = 0;
2141 for (spIndex = 0; spIndex < primaryRPCI->subpassCount; ++spIndex) {
2142 // For each subpass, verify that corresponding color, input, resolve & depth/stencil attachment references are compatible
2143 uint32_t primaryColorCount = primaryRPCI->pSubpasses[spIndex].colorAttachmentCount;
2144 uint32_t secondaryColorCount = secondaryRPCI->pSubpasses[spIndex].colorAttachmentCount;
2145 uint32_t colorMax = std::max(primaryColorCount, secondaryColorCount);
2146 for (uint32_t cIdx = 0; cIdx < colorMax; ++cIdx) {
2147 if (!attachment_references_compatible(cIdx, primaryRPCI->pSubpasses[spIndex].pColorAttachments, primaryColorCount,
2148 primaryRPCI->pAttachments, secondaryRPCI->pSubpasses[spIndex].pColorAttachments,
2149 secondaryColorCount, secondaryRPCI->pAttachments)) {
Chris Forbesc07fa782016-04-14 10:30:01 +12002150 stringstream errorStr;
Tobin Ehlis5b5e7bc2016-03-09 16:12:48 -07002151 errorStr << "color attachments at index " << cIdx << " of subpass index " << spIndex << " are not compatible.";
2152 errorMsg = errorStr.str();
2153 return false;
2154 } else if (!attachment_references_compatible(cIdx, primaryRPCI->pSubpasses[spIndex].pResolveAttachments,
2155 primaryColorCount, primaryRPCI->pAttachments,
2156 secondaryRPCI->pSubpasses[spIndex].pResolveAttachments,
2157 secondaryColorCount, secondaryRPCI->pAttachments)) {
Chris Forbesc07fa782016-04-14 10:30:01 +12002158 stringstream errorStr;
Tobin Ehlis5b5e7bc2016-03-09 16:12:48 -07002159 errorStr << "resolve attachments at index " << cIdx << " of subpass index " << spIndex << " are not compatible.";
2160 errorMsg = errorStr.str();
2161 return false;
Tobin Ehlis5b5e7bc2016-03-09 16:12:48 -07002162 }
2163 }
Chris Forbesfd506832016-04-11 18:32:23 +12002164
2165 if (!attachment_references_compatible(0, primaryRPCI->pSubpasses[spIndex].pDepthStencilAttachment,
2166 1, primaryRPCI->pAttachments,
2167 secondaryRPCI->pSubpasses[spIndex].pDepthStencilAttachment,
2168 1, secondaryRPCI->pAttachments)) {
Chris Forbesc07fa782016-04-14 10:30:01 +12002169 stringstream errorStr;
Chris Forbesfd506832016-04-11 18:32:23 +12002170 errorStr << "depth/stencil attachments of subpass index " << spIndex << " are not compatible.";
2171 errorMsg = errorStr.str();
2172 return false;
2173 }
2174
Tobin Ehlis5b5e7bc2016-03-09 16:12:48 -07002175 uint32_t primaryInputCount = primaryRPCI->pSubpasses[spIndex].inputAttachmentCount;
2176 uint32_t secondaryInputCount = secondaryRPCI->pSubpasses[spIndex].inputAttachmentCount;
2177 uint32_t inputMax = std::max(primaryInputCount, secondaryInputCount);
2178 for (uint32_t i = 0; i < inputMax; ++i) {
2179 if (!attachment_references_compatible(i, primaryRPCI->pSubpasses[spIndex].pInputAttachments, primaryColorCount,
2180 primaryRPCI->pAttachments, secondaryRPCI->pSubpasses[spIndex].pInputAttachments,
2181 secondaryColorCount, secondaryRPCI->pAttachments)) {
Chris Forbesc07fa782016-04-14 10:30:01 +12002182 stringstream errorStr;
Tobin Ehlis5b5e7bc2016-03-09 16:12:48 -07002183 errorStr << "input attachments at index " << i << " of subpass index " << spIndex << " are not compatible.";
2184 errorMsg = errorStr.str();
2185 return false;
2186 }
2187 }
2188 }
2189 return true;
2190}
2191
2192// For give SET_NODE, verify that its Set is compatible w/ the setLayout corresponding to pipelineLayout[layoutIndex]
2193static bool verify_set_layout_compatibility(layer_data *my_data, const SET_NODE *pSet, const VkPipelineLayout layout,
2194 const uint32_t layoutIndex, string &errorMsg) {
Chris Forbesea261402016-03-18 15:49:58 +13002195 auto pipeline_layout_it = my_data->pipelineLayoutMap.find(layout);
2196 if (pipeline_layout_it == my_data->pipelineLayoutMap.end()) {
Chris Forbesc07fa782016-04-14 10:30:01 +12002197 stringstream errorStr;
Tobin Ehlis5b5e7bc2016-03-09 16:12:48 -07002198 errorStr << "invalid VkPipelineLayout (" << layout << ")";
2199 errorMsg = errorStr.str();
2200 return false;
2201 }
Chris Forbesea261402016-03-18 15:49:58 +13002202 if (layoutIndex >= pipeline_layout_it->second.descriptorSetLayouts.size()) {
Chris Forbesc07fa782016-04-14 10:30:01 +12002203 stringstream errorStr;
Chris Forbesea261402016-03-18 15:49:58 +13002204 errorStr << "VkPipelineLayout (" << layout << ") only contains " << pipeline_layout_it->second.descriptorSetLayouts.size()
2205 << " setLayouts corresponding to sets 0-" << pipeline_layout_it->second.descriptorSetLayouts.size() - 1
Tobin Ehlis5b5e7bc2016-03-09 16:12:48 -07002206 << ", but you're attempting to bind set to index " << layoutIndex;
2207 errorMsg = errorStr.str();
2208 return false;
2209 }
2210 // Get the specific setLayout from PipelineLayout that overlaps this set
Chris Forbesea261402016-03-18 15:49:58 +13002211 LAYOUT_NODE *pLayoutNode = my_data->descriptorSetLayoutMap[pipeline_layout_it->second.descriptorSetLayouts[layoutIndex]];
Tobin Ehlis5b5e7bc2016-03-09 16:12:48 -07002212 if (pLayoutNode->layout == pSet->pLayout->layout) { // trivial pass case
2213 return true;
2214 }
2215 size_t descriptorCount = pLayoutNode->descriptorTypes.size();
2216 if (descriptorCount != pSet->pLayout->descriptorTypes.size()) {
Chris Forbesc07fa782016-04-14 10:30:01 +12002217 stringstream errorStr;
Tobin Ehlis5b5e7bc2016-03-09 16:12:48 -07002218 errorStr << "setLayout " << layoutIndex << " from pipelineLayout " << layout << " has " << descriptorCount
2219 << " descriptors, but corresponding set being bound has " << pSet->pLayout->descriptorTypes.size()
2220 << " descriptors.";
2221 errorMsg = errorStr.str();
2222 return false; // trivial fail case
2223 }
2224 // Now need to check set against corresponding pipelineLayout to verify compatibility
2225 for (size_t i = 0; i < descriptorCount; ++i) {
2226 // Need to verify that layouts are identically defined
2227 // TODO : Is below sufficient? Making sure that types & stageFlags match per descriptor
2228 // do we also need to check immutable samplers?
2229 if (pLayoutNode->descriptorTypes[i] != pSet->pLayout->descriptorTypes[i]) {
Chris Forbesc07fa782016-04-14 10:30:01 +12002230 stringstream errorStr;
Tobin Ehlis5b5e7bc2016-03-09 16:12:48 -07002231 errorStr << "descriptor " << i << " for descriptorSet being bound is type '"
2232 << string_VkDescriptorType(pSet->pLayout->descriptorTypes[i])
2233 << "' but corresponding descriptor from pipelineLayout is type '"
2234 << string_VkDescriptorType(pLayoutNode->descriptorTypes[i]) << "'";
2235 errorMsg = errorStr.str();
2236 return false;
2237 }
2238 if (pLayoutNode->stageFlags[i] != pSet->pLayout->stageFlags[i]) {
Chris Forbesc07fa782016-04-14 10:30:01 +12002239 stringstream errorStr;
Tobin Ehlis5b5e7bc2016-03-09 16:12:48 -07002240 errorStr << "stageFlags " << i << " for descriptorSet being bound is " << pSet->pLayout->stageFlags[i]
2241 << "' but corresponding descriptor from pipelineLayout has stageFlags " << pLayoutNode->stageFlags[i];
2242 errorMsg = errorStr.str();
2243 return false;
2244 }
2245 }
2246 return true;
2247}
2248
2249// Validate that data for each specialization entry is fully contained within the buffer.
Dustin Gravese3319182016-04-05 09:41:17 -06002250static bool validate_specialization_offsets(layer_data *my_data, VkPipelineShaderStageCreateInfo const *info) {
2251 bool pass = true;
Tobin Ehlis5b5e7bc2016-03-09 16:12:48 -07002252
2253 VkSpecializationInfo const *spec = info->pSpecializationInfo;
2254
2255 if (spec) {
2256 for (auto i = 0u; i < spec->mapEntryCount; i++) {
2257 if (spec->pMapEntries[i].offset + spec->pMapEntries[i].size > spec->dataSize) {
2258 if (log_msg(my_data->report_data, VK_DEBUG_REPORT_ERROR_BIT_EXT, VK_DEBUG_REPORT_OBJECT_TYPE_DEVICE_EXT,
2259 /*dev*/ 0, __LINE__, SHADER_CHECKER_BAD_SPECIALIZATION, "SC",
2260 "Specialization entry %u (for constant id %u) references memory outside provided "
2261 "specialization data (bytes %u.." PRINTF_SIZE_T_SPECIFIER "; " PRINTF_SIZE_T_SPECIFIER
2262 " bytes provided)",
2263 i, spec->pMapEntries[i].constantID, spec->pMapEntries[i].offset,
2264 spec->pMapEntries[i].offset + spec->pMapEntries[i].size - 1, spec->dataSize)) {
2265
Dustin Gravese3319182016-04-05 09:41:17 -06002266 pass = false;
Tobin Ehlis5b5e7bc2016-03-09 16:12:48 -07002267 }
2268 }
2269 }
2270 }
2271
2272 return pass;
2273}
2274
2275static bool descriptor_type_match(layer_data *my_data, shader_module const *module, uint32_t type_id,
Chris Forbes1b8c5812016-03-18 11:21:35 +13002276 VkDescriptorType descriptor_type, unsigned &descriptor_count) {
Tobin Ehlis5b5e7bc2016-03-09 16:12:48 -07002277 auto type = module->get_def(type_id);
2278
Chris Forbes1b8c5812016-03-18 11:21:35 +13002279 descriptor_count = 1;
2280
Chris Forbes7b892d22016-03-18 11:26:06 +13002281 /* Strip off any array or ptrs. Where we remove array levels, adjust the
2282 * descriptor count for each dimension. */
Tobin Ehlis5b5e7bc2016-03-09 16:12:48 -07002283 while (type.opcode() == spv::OpTypeArray || type.opcode() == spv::OpTypePointer) {
Chris Forbes7b892d22016-03-18 11:26:06 +13002284 if (type.opcode() == spv::OpTypeArray) {
2285 descriptor_count *= get_constant_value(module, type.word(3));
2286 type = module->get_def(type.word(2));
2287 }
2288 else {
2289 type = module->get_def(type.word(3));
2290 }
Tobin Ehlis5b5e7bc2016-03-09 16:12:48 -07002291 }
2292
2293 switch (type.opcode()) {
2294 case spv::OpTypeStruct: {
2295 for (auto insn : *module) {
2296 if (insn.opcode() == spv::OpDecorate && insn.word(1) == type.word(1)) {
2297 if (insn.word(2) == spv::DecorationBlock) {
2298 return descriptor_type == VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER ||
2299 descriptor_type == VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER_DYNAMIC;
2300 } else if (insn.word(2) == spv::DecorationBufferBlock) {
2301 return descriptor_type == VK_DESCRIPTOR_TYPE_STORAGE_BUFFER ||
2302 descriptor_type == VK_DESCRIPTOR_TYPE_STORAGE_BUFFER_DYNAMIC;
2303 }
2304 }
2305 }
2306
2307 /* Invalid */
2308 return false;
2309 }
2310
2311 case spv::OpTypeSampler:
2312 return descriptor_type == VK_DESCRIPTOR_TYPE_SAMPLER;
2313
2314 case spv::OpTypeSampledImage:
Chris Forbes35cd1752016-03-24 14:14:45 +13002315 if (descriptor_type == VK_DESCRIPTOR_TYPE_UNIFORM_TEXEL_BUFFER) {
2316 /* Slight relaxation for some GLSL historical madness: samplerBuffer
2317 * doesn't really have a sampler, and a texel buffer descriptor
2318 * doesn't really provide one. Allow this slight mismatch.
2319 */
2320 auto image_type = module->get_def(type.word(2));
2321 auto dim = image_type.word(3);
2322 auto sampled = image_type.word(7);
2323 return dim == spv::DimBuffer && sampled == 1;
2324 }
Tobin Ehlis5b5e7bc2016-03-09 16:12:48 -07002325 return descriptor_type == VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER;
2326
2327 case spv::OpTypeImage: {
2328 /* Many descriptor types backing image types-- depends on dimension
2329 * and whether the image will be used with a sampler. SPIRV for
2330 * Vulkan requires that sampled be 1 or 2 -- leaving the decision to
2331 * runtime is unacceptable.
2332 */
2333 auto dim = type.word(3);
2334 auto sampled = type.word(7);
2335
2336 if (dim == spv::DimSubpassData) {
2337 return descriptor_type == VK_DESCRIPTOR_TYPE_INPUT_ATTACHMENT;
2338 } else if (dim == spv::DimBuffer) {
2339 if (sampled == 1) {
2340 return descriptor_type == VK_DESCRIPTOR_TYPE_UNIFORM_TEXEL_BUFFER;
2341 } else {
2342 return descriptor_type == VK_DESCRIPTOR_TYPE_STORAGE_TEXEL_BUFFER;
2343 }
2344 } else if (sampled == 1) {
2345 return descriptor_type == VK_DESCRIPTOR_TYPE_SAMPLED_IMAGE;
2346 } else {
2347 return descriptor_type == VK_DESCRIPTOR_TYPE_STORAGE_IMAGE;
2348 }
2349 }
2350
2351 /* We shouldn't really see any other junk types -- but if we do, they're
2352 * a mismatch.
2353 */
2354 default:
2355 return false; /* Mismatch */
2356 }
2357}
2358
Dustin Gravese3319182016-04-05 09:41:17 -06002359static bool require_feature(layer_data *my_data, VkBool32 feature, char const *feature_name) {
Chris Forbesa3c20a22016-03-15 10:12:48 +13002360 if (!feature) {
Chris Forbes080afef2016-03-30 13:14:22 +13002361 if (log_msg(my_data->report_data, VK_DEBUG_REPORT_ERROR_BIT_EXT, VkDebugReportObjectTypeEXT(0), 0,
2362 __LINE__, SHADER_CHECKER_FEATURE_NOT_ENABLED, "SC",
Chris Forbesa3c20a22016-03-15 10:12:48 +13002363 "Shader requires VkPhysicalDeviceFeatures::%s but is not "
2364 "enabled on the device",
2365 feature_name)) {
2366 return false;
2367 }
2368 }
2369
2370 return true;
2371}
2372
Dustin Gravese3319182016-04-05 09:41:17 -06002373static bool validate_shader_capabilities(layer_data *my_data, shader_module const *src) {
2374 bool pass = true;
Chris Forbesa3c20a22016-03-15 10:12:48 +13002375
Tobin Ehlisb1b606d2016-04-11 14:49:55 -06002376 auto enabledFeatures = &my_data->phys_dev_properties.features;
Chris Forbesa3c20a22016-03-15 10:12:48 +13002377
2378 for (auto insn : *src) {
2379 if (insn.opcode() == spv::OpCapability) {
2380 switch (insn.word(1)) {
2381 case spv::CapabilityMatrix:
2382 case spv::CapabilityShader:
2383 case spv::CapabilityInputAttachment:
2384 case spv::CapabilitySampled1D:
2385 case spv::CapabilityImage1D:
2386 case spv::CapabilitySampledBuffer:
2387 case spv::CapabilityImageBuffer:
2388 case spv::CapabilityImageQuery:
2389 case spv::CapabilityDerivativeControl:
2390 // Always supported by a Vulkan 1.0 implementation -- no feature bits.
2391 break;
2392
2393 case spv::CapabilityGeometry:
2394 pass &= require_feature(my_data, enabledFeatures->geometryShader, "geometryShader");
2395 break;
2396
2397 case spv::CapabilityTessellation:
2398 pass &= require_feature(my_data, enabledFeatures->tessellationShader, "tessellationShader");
2399 break;
2400
2401 case spv::CapabilityFloat64:
2402 pass &= require_feature(my_data, enabledFeatures->shaderFloat64, "shaderFloat64");
2403 break;
2404
2405 case spv::CapabilityInt64:
2406 pass &= require_feature(my_data, enabledFeatures->shaderInt64, "shaderInt64");
2407 break;
2408
2409 case spv::CapabilityTessellationPointSize:
2410 case spv::CapabilityGeometryPointSize:
2411 pass &= require_feature(my_data, enabledFeatures->shaderTessellationAndGeometryPointSize,
2412 "shaderTessellationAndGeometryPointSize");
2413 break;
2414
2415 case spv::CapabilityImageGatherExtended:
2416 pass &= require_feature(my_data, enabledFeatures->shaderImageGatherExtended, "shaderImageGatherExtended");
2417 break;
2418
2419 case spv::CapabilityStorageImageMultisample:
2420 pass &= require_feature(my_data, enabledFeatures->shaderStorageImageMultisample, "shaderStorageImageMultisample");
2421 break;
2422
2423 case spv::CapabilityUniformBufferArrayDynamicIndexing:
2424 pass &= require_feature(my_data, enabledFeatures->shaderUniformBufferArrayDynamicIndexing,
2425 "shaderUniformBufferArrayDynamicIndexing");
2426 break;
2427
2428 case spv::CapabilitySampledImageArrayDynamicIndexing:
2429 pass &= require_feature(my_data, enabledFeatures->shaderSampledImageArrayDynamicIndexing,
2430 "shaderSampledImageArrayDynamicIndexing");
2431 break;
2432
2433 case spv::CapabilityStorageBufferArrayDynamicIndexing:
2434 pass &= require_feature(my_data, enabledFeatures->shaderStorageBufferArrayDynamicIndexing,
2435 "shaderStorageBufferArrayDynamicIndexing");
2436 break;
2437
2438 case spv::CapabilityStorageImageArrayDynamicIndexing:
2439 pass &= require_feature(my_data, enabledFeatures->shaderStorageImageArrayDynamicIndexing,
2440 "shaderStorageImageArrayDynamicIndexing");
2441 break;
2442
2443 case spv::CapabilityClipDistance:
2444 pass &= require_feature(my_data, enabledFeatures->shaderClipDistance, "shaderClipDistance");
2445 break;
2446
2447 case spv::CapabilityCullDistance:
2448 pass &= require_feature(my_data, enabledFeatures->shaderCullDistance, "shaderCullDistance");
2449 break;
2450
2451 case spv::CapabilityImageCubeArray:
2452 pass &= require_feature(my_data, enabledFeatures->imageCubeArray, "imageCubeArray");
2453 break;
2454
2455 case spv::CapabilitySampleRateShading:
2456 pass &= require_feature(my_data, enabledFeatures->sampleRateShading, "sampleRateShading");
2457 break;
2458
2459 case spv::CapabilitySparseResidency:
2460 pass &= require_feature(my_data, enabledFeatures->shaderResourceResidency, "shaderResourceResidency");
2461 break;
2462
2463 case spv::CapabilityMinLod:
2464 pass &= require_feature(my_data, enabledFeatures->shaderResourceMinLod, "shaderResourceMinLod");
2465 break;
2466
2467 case spv::CapabilitySampledCubeArray:
2468 pass &= require_feature(my_data, enabledFeatures->imageCubeArray, "imageCubeArray");
2469 break;
2470
2471 case spv::CapabilityImageMSArray:
2472 pass &= require_feature(my_data, enabledFeatures->shaderStorageImageMultisample, "shaderStorageImageMultisample");
2473 break;
2474
2475 case spv::CapabilityStorageImageExtendedFormats:
2476 pass &= require_feature(my_data, enabledFeatures->shaderStorageImageExtendedFormats,
2477 "shaderStorageImageExtendedFormats");
2478 break;
2479
2480 case spv::CapabilityInterpolationFunction:
2481 pass &= require_feature(my_data, enabledFeatures->sampleRateShading, "sampleRateShading");
2482 break;
2483
2484 case spv::CapabilityStorageImageReadWithoutFormat:
2485 pass &= require_feature(my_data, enabledFeatures->shaderStorageImageReadWithoutFormat,
2486 "shaderStorageImageReadWithoutFormat");
2487 break;
2488
2489 case spv::CapabilityStorageImageWriteWithoutFormat:
2490 pass &= require_feature(my_data, enabledFeatures->shaderStorageImageWriteWithoutFormat,
2491 "shaderStorageImageWriteWithoutFormat");
2492 break;
2493
2494 case spv::CapabilityMultiViewport:
2495 pass &= require_feature(my_data, enabledFeatures->multiViewport, "multiViewport");
2496 break;
2497
2498 default:
Chris Forbes080afef2016-03-30 13:14:22 +13002499 if (log_msg(my_data->report_data, VK_DEBUG_REPORT_ERROR_BIT_EXT, VkDebugReportObjectTypeEXT(0), 0,
Chris Forbesa3c20a22016-03-15 10:12:48 +13002500 __LINE__, SHADER_CHECKER_BAD_CAPABILITY, "SC",
2501 "Shader declares capability %u, not supported in Vulkan.",
2502 insn.word(1)))
Dustin Gravese3319182016-04-05 09:41:17 -06002503 pass = false;
Chris Forbesa3c20a22016-03-15 10:12:48 +13002504 break;
2505 }
2506 }
2507 }
2508
2509 return pass;
2510}
2511
Dustin Gravese3319182016-04-05 09:41:17 -06002512static bool validate_pipeline_shader_stage(layer_data *dev_data, VkPipelineShaderStageCreateInfo const *pStage,
2513 PIPELINE_NODE *pipeline, PIPELINE_LAYOUT_NODE *pipelineLayout,
2514 shader_module **out_module, spirv_inst_iter *out_entrypoint) {
2515 bool pass = true;
Chris Forbes78be5012016-03-30 12:12:01 +13002516 auto module = *out_module = dev_data->shaderModuleMap[pStage->module].get();
2517 pass &= validate_specialization_offsets(dev_data, pStage);
2518
2519 /* find the entrypoint */
2520 auto entrypoint = *out_entrypoint = find_entrypoint(module, pStage->pName, pStage->stage);
2521 if (entrypoint == module->end()) {
Chris Forbes080afef2016-03-30 13:14:22 +13002522 if (log_msg(dev_data->report_data, VK_DEBUG_REPORT_ERROR_BIT_EXT, VkDebugReportObjectTypeEXT(0), 0,
2523 __LINE__, SHADER_CHECKER_MISSING_ENTRYPOINT, "SC",
Chris Forbes78be5012016-03-30 12:12:01 +13002524 "No entrypoint found named `%s` for stage %s", pStage->pName,
2525 string_VkShaderStageFlagBits(pStage->stage))) {
Dustin Gravese3319182016-04-05 09:41:17 -06002526 pass = false;
Chris Forbes78be5012016-03-30 12:12:01 +13002527 }
2528 }
2529
2530 /* validate shader capabilities against enabled device features */
2531 pass &= validate_shader_capabilities(dev_data, module);
2532
2533 /* mark accessible ids */
2534 std::unordered_set<uint32_t> accessible_ids;
2535 mark_accessible_ids(module, entrypoint, accessible_ids);
2536
2537 /* validate descriptor set layout against what the entrypoint actually uses */
2538 std::map<descriptor_slot_t, interface_var> descriptor_uses;
2539 collect_interface_by_descriptor_slot(dev_data, module, accessible_ids, descriptor_uses);
2540
2541 /* validate push constant usage */
2542 pass &= validate_push_constant_usage(dev_data, &pipelineLayout->pushConstantRanges,
2543 module, accessible_ids, pStage->stage);
2544
2545 /* validate descriptor use */
2546 for (auto use : descriptor_uses) {
2547 // While validating shaders capture which slots are used by the pipeline
2548 pipeline->active_slots[use.first.first].insert(use.first.second);
2549
2550 /* find the matching binding */
2551 auto binding = get_descriptor_binding(dev_data, pipelineLayout, use.first);
2552 unsigned required_descriptor_count;
2553
2554 if (!binding) {
Chris Forbes080afef2016-03-30 13:14:22 +13002555 if (log_msg(dev_data->report_data, VK_DEBUG_REPORT_ERROR_BIT_EXT, VkDebugReportObjectTypeEXT(0), 0,
2556 __LINE__, SHADER_CHECKER_MISSING_DESCRIPTOR, "SC",
Chris Forbes78be5012016-03-30 12:12:01 +13002557 "Shader uses descriptor slot %u.%u (used as type `%s`) but not declared in pipeline layout",
2558 use.first.first, use.first.second, describe_type(module, use.second.type_id).c_str())) {
Dustin Gravese3319182016-04-05 09:41:17 -06002559 pass = false;
Chris Forbes78be5012016-03-30 12:12:01 +13002560 }
2561 } else if (~binding->stageFlags & pStage->stage) {
2562 if (log_msg(dev_data->report_data, VK_DEBUG_REPORT_ERROR_BIT_EXT, VK_DEBUG_REPORT_OBJECT_TYPE_DEVICE_EXT,
2563 /*dev*/ 0, __LINE__, SHADER_CHECKER_DESCRIPTOR_NOT_ACCESSIBLE_FROM_STAGE, "SC",
2564 "Shader uses descriptor slot %u.%u (used "
2565 "as type `%s`) but descriptor not "
2566 "accessible from stage %s",
2567 use.first.first, use.first.second,
2568 describe_type(module, use.second.type_id).c_str(),
2569 string_VkShaderStageFlagBits(pStage->stage))) {
Dustin Gravese3319182016-04-05 09:41:17 -06002570 pass = false;
Chris Forbes78be5012016-03-30 12:12:01 +13002571 }
2572 } else if (!descriptor_type_match(dev_data, module, use.second.type_id, binding->descriptorType, /*out*/ required_descriptor_count)) {
Chris Forbes080afef2016-03-30 13:14:22 +13002573 if (log_msg(dev_data->report_data, VK_DEBUG_REPORT_ERROR_BIT_EXT, VkDebugReportObjectTypeEXT(0), 0,
2574 __LINE__, SHADER_CHECKER_DESCRIPTOR_TYPE_MISMATCH, "SC",
Chris Forbes78be5012016-03-30 12:12:01 +13002575 "Type mismatch on descriptor slot "
2576 "%u.%u (used as type `%s`) but "
2577 "descriptor of type %s",
2578 use.first.first, use.first.second,
2579 describe_type(module, use.second.type_id).c_str(),
2580 string_VkDescriptorType(binding->descriptorType))) {
Dustin Gravese3319182016-04-05 09:41:17 -06002581 pass = false;
Chris Forbes78be5012016-03-30 12:12:01 +13002582 }
2583 } else if (binding->descriptorCount < required_descriptor_count) {
Chris Forbes080afef2016-03-30 13:14:22 +13002584 if (log_msg(dev_data->report_data, VK_DEBUG_REPORT_ERROR_BIT_EXT, VkDebugReportObjectTypeEXT(0), 0,
2585 __LINE__, SHADER_CHECKER_DESCRIPTOR_TYPE_MISMATCH, "SC",
Chris Forbes78be5012016-03-30 12:12:01 +13002586 "Shader expects at least %u descriptors for binding %u.%u (used as type `%s`) but only %u provided",
2587 required_descriptor_count, use.first.first, use.first.second,
2588 describe_type(module, use.second.type_id).c_str(),
2589 binding->descriptorCount)) {
Dustin Gravese3319182016-04-05 09:41:17 -06002590 pass = false;
Chris Forbes78be5012016-03-30 12:12:01 +13002591 }
2592 }
2593 }
2594
2595 return pass;
2596}
2597
2598
Tobin Ehlisa61b5372016-03-24 09:17:25 -06002599// Validate that the shaders used by the given pipeline and store the active_slots
2600// that are actually used by the pipeline into pPipeline->active_slots
Dustin Gravese3319182016-04-05 09:41:17 -06002601static bool validate_and_capture_pipeline_shader_state(layer_data *my_data, PIPELINE_NODE *pPipeline) {
Tobin Ehlisca546212016-04-01 13:51:33 -06002602 auto pCreateInfo = reinterpret_cast<VkGraphicsPipelineCreateInfo const *>(&pPipeline->graphicsPipelineCI);
Tobin Ehlis5b5e7bc2016-03-09 16:12:48 -07002603 int vertex_stage = get_shader_stage_id(VK_SHADER_STAGE_VERTEX_BIT);
2604 int fragment_stage = get_shader_stage_id(VK_SHADER_STAGE_FRAGMENT_BIT);
2605
2606 shader_module *shaders[5];
2607 memset(shaders, 0, sizeof(shaders));
2608 spirv_inst_iter entrypoints[5];
2609 memset(entrypoints, 0, sizeof(entrypoints));
Tobin Ehlis5b5e7bc2016-03-09 16:12:48 -07002610 VkPipelineVertexInputStateCreateInfo const *vi = 0;
Dustin Gravese3319182016-04-05 09:41:17 -06002611 bool pass = true;
Tobin Ehlis5b5e7bc2016-03-09 16:12:48 -07002612
Chris Forbes78be5012016-03-30 12:12:01 +13002613 auto pipelineLayout = pCreateInfo->layout != VK_NULL_HANDLE ? &my_data->pipelineLayoutMap[pCreateInfo->layout] : nullptr;
2614
Tobin Ehlis5b5e7bc2016-03-09 16:12:48 -07002615 for (uint32_t i = 0; i < pCreateInfo->stageCount; i++) {
Tobin Ehlisca546212016-04-01 13:51:33 -06002616 VkPipelineShaderStageCreateInfo const *pStage =
2617 reinterpret_cast<VkPipelineShaderStageCreateInfo const *>(&pCreateInfo->pStages[i]);
Chris Forbes78be5012016-03-30 12:12:01 +13002618 auto stage_id = get_shader_stage_id(pStage->stage);
2619 pass &= validate_pipeline_shader_stage(my_data, pStage, pPipeline, pipelineLayout,
2620 &shaders[stage_id], &entrypoints[stage_id]);
Tobin Ehlis5b5e7bc2016-03-09 16:12:48 -07002621 }
2622
Tobin Ehlis5b5e7bc2016-03-09 16:12:48 -07002623 vi = pCreateInfo->pVertexInputState;
2624
2625 if (vi) {
Chris Forbes09042aa2016-03-30 13:21:20 +13002626 pass &= validate_vi_consistency(my_data, vi);
Tobin Ehlis5b5e7bc2016-03-09 16:12:48 -07002627 }
2628
2629 if (shaders[vertex_stage]) {
Chris Forbes09042aa2016-03-30 13:21:20 +13002630 pass &= validate_vi_against_vs_inputs(my_data, vi, shaders[vertex_stage], entrypoints[vertex_stage]);
Tobin Ehlis5b5e7bc2016-03-09 16:12:48 -07002631 }
2632
Tobin Ehlis5b5e7bc2016-03-09 16:12:48 -07002633 int producer = get_shader_stage_id(VK_SHADER_STAGE_VERTEX_BIT);
2634 int consumer = get_shader_stage_id(VK_SHADER_STAGE_TESSELLATION_CONTROL_BIT);
2635
2636 while (!shaders[producer] && producer != fragment_stage) {
2637 producer++;
2638 consumer++;
2639 }
2640
2641 for (; producer != fragment_stage && consumer <= fragment_stage; consumer++) {
2642 assert(shaders[producer]);
2643 if (shaders[consumer]) {
Chris Forbes031261d2016-03-29 16:41:07 +13002644 pass &= validate_interface_between_stages(my_data,
2645 shaders[producer], entrypoints[producer], &shader_stage_attribs[producer],
2646 shaders[consumer], entrypoints[consumer], &shader_stage_attribs[consumer]);
Tobin Ehlis5b5e7bc2016-03-09 16:12:48 -07002647
2648 producer = consumer;
2649 }
2650 }
2651
Chris Forbes78be5012016-03-30 12:12:01 +13002652 auto rp = pCreateInfo->renderPass != VK_NULL_HANDLE ? my_data->renderPassMap[pCreateInfo->renderPass] : nullptr;
2653
Tobin Ehlis5b5e7bc2016-03-09 16:12:48 -07002654 if (shaders[fragment_stage] && rp) {
Chris Forbes09042aa2016-03-30 13:21:20 +13002655 pass &= validate_fs_outputs_against_render_pass(my_data, shaders[fragment_stage], entrypoints[fragment_stage], rp,
2656 pCreateInfo->subpass);
Tobin Ehlis5b5e7bc2016-03-09 16:12:48 -07002657 }
2658
2659 return pass;
2660}
2661
Dustin Gravese3319182016-04-05 09:41:17 -06002662static bool validate_compute_pipeline(layer_data *my_data, PIPELINE_NODE *pPipeline) {
Tobin Ehlisca546212016-04-01 13:51:33 -06002663 auto pCreateInfo = reinterpret_cast<VkComputePipelineCreateInfo const *>(&pPipeline->computePipelineCI);
Chris Forbes03857e82016-03-30 14:04:36 +13002664
2665 auto pipelineLayout = pCreateInfo->layout != VK_NULL_HANDLE ? &my_data->pipelineLayoutMap[pCreateInfo->layout] : nullptr;
2666
2667 shader_module *module;
2668 spirv_inst_iter entrypoint;
2669
2670 return validate_pipeline_shader_stage(my_data, &pCreateInfo->stage, pPipeline, pipelineLayout,
2671 &module, &entrypoint);
2672}
2673
Tobin Ehlis5b5e7bc2016-03-09 16:12:48 -07002674// Return Set node ptr for specified set or else NULL
2675static SET_NODE *getSetNode(layer_data *my_data, const VkDescriptorSet set) {
2676 if (my_data->setMap.find(set) == my_data->setMap.end()) {
2677 return NULL;
2678 }
2679 return my_data->setMap[set];
2680}
Tobin Ehlisa61b5372016-03-24 09:17:25 -06002681
2682// For given Layout Node and binding, return index where that binding begins
2683static uint32_t getBindingStartIndex(const LAYOUT_NODE *pLayout, const uint32_t binding) {
2684 uint32_t offsetIndex = 0;
2685 for (uint32_t i = 0; i < pLayout->createInfo.bindingCount; i++) {
2686 if (pLayout->createInfo.pBindings[i].binding == binding)
2687 break;
2688 offsetIndex += pLayout->createInfo.pBindings[i].descriptorCount;
2689 }
2690 return offsetIndex;
2691}
2692
2693// For given layout node and binding, return last index that is updated
2694static uint32_t getBindingEndIndex(const LAYOUT_NODE *pLayout, const uint32_t binding) {
2695 uint32_t offsetIndex = 0;
2696 for (uint32_t i = 0; i < pLayout->createInfo.bindingCount; i++) {
2697 offsetIndex += pLayout->createInfo.pBindings[i].descriptorCount;
2698 if (pLayout->createInfo.pBindings[i].binding == binding)
2699 break;
2700 }
2701 return offsetIndex - 1;
2702}
2703
Tobin Ehlis7a3985a2016-03-25 11:49:51 -06002704// For the given command buffer, verify and update the state for activeSetBindingsPairs
2705// This includes:
2706// 1. Verifying that any dynamic descriptor in that set has a valid dynamic offset bound.
2707// To be valid, the dynamic offset combined with the offset and range from its
2708// descriptor update must not overflow the size of its buffer being updated
2709// 2. Grow updateImages for given pCB to include any bound STORAGE_IMAGE descriptor images
2710// 3. Grow updateBuffers for pCB to include buffers from STORAGE*_BUFFER descriptor buffers
Dustin Gravese3319182016-04-05 09:41:17 -06002711static bool validate_and_update_drawtime_descriptor_state(
Tobin Ehlis7a3985a2016-03-25 11:49:51 -06002712 layer_data *dev_data, GLOBAL_CB_NODE *pCB,
2713 const vector<std::pair<SET_NODE *, unordered_set<uint32_t>>> &activeSetBindingsPairs) {
Dustin Gravese3319182016-04-05 09:41:17 -06002714 bool result = false;
Tobin Ehlis5b5e7bc2016-03-09 16:12:48 -07002715
2716 VkWriteDescriptorSet *pWDS = NULL;
2717 uint32_t dynOffsetIndex = 0;
2718 VkDeviceSize bufferSize = 0;
Tobin Ehlisa61b5372016-03-24 09:17:25 -06002719 for (auto set_bindings_pair : activeSetBindingsPairs) {
2720 SET_NODE *set_node = set_bindings_pair.first;
2721 LAYOUT_NODE *layout_node = set_node->pLayout;
2722 for (auto binding : set_bindings_pair.second) {
2723 uint32_t startIdx = getBindingStartIndex(layout_node, binding);
2724 uint32_t endIdx = getBindingEndIndex(layout_node, binding);
2725 for (uint32_t i = startIdx; i <= endIdx; ++i) {
Tobin Ehlise1e6c092016-03-31 18:02:51 -06002726 // We did check earlier to verify that set was updated, but now make sure given slot was updated
2727 // TODO : Would be better to store set# that set is bound to so we can report set.binding[index] not updated
2728 if (!set_node->pDescriptorUpdates[i]) {
2729 result |= log_msg(dev_data->report_data, VK_DEBUG_REPORT_ERROR_BIT_EXT,
2730 VK_DEBUG_REPORT_OBJECT_TYPE_DESCRIPTOR_SET_EXT, reinterpret_cast<const uint64_t &>(set_node->set), __LINE__,
2731 DRAWSTATE_DESCRIPTOR_SET_NOT_UPDATED, "DS",
2732 "DS %#" PRIxLEAST64 " bound and active but it never had binding %u updated. It is now being used to draw so "
2733 "this will result in undefined behavior.",
2734 reinterpret_cast<const uint64_t &>(set_node->set), binding);
2735 } else {
2736 switch (set_node->pDescriptorUpdates[i]->sType) {
2737 case VK_STRUCTURE_TYPE_WRITE_DESCRIPTOR_SET:
2738 pWDS = (VkWriteDescriptorSet *)set_node->pDescriptorUpdates[i];
Mark Youngdfc3ecf2016-04-11 17:45:37 -06002739
2740 // Verify uniform and storage buffers actually are bound to valid memory at draw time.
2741 if ((pWDS->descriptorType == VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER) ||
2742 (pWDS->descriptorType == VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER_DYNAMIC) ||
2743 (pWDS->descriptorType == VK_DESCRIPTOR_TYPE_STORAGE_BUFFER) ||
Tobin Ehlise1e6c092016-03-31 18:02:51 -06002744 (pWDS->descriptorType == VK_DESCRIPTOR_TYPE_STORAGE_BUFFER_DYNAMIC)) {
2745 for (uint32_t j = 0; j < pWDS->descriptorCount; ++j) {
Mark Youngdfc3ecf2016-04-11 17:45:37 -06002746 auto buffer_node = dev_data->bufferMap.find(pWDS->pBufferInfo[j].buffer);
2747 if (buffer_node == dev_data->bufferMap.end()) {
Tobin Ehlis7a3985a2016-03-25 11:49:51 -06002748 result |= log_msg(dev_data->report_data, VK_DEBUG_REPORT_ERROR_BIT_EXT,
Tobin Ehlisc7f36562016-03-21 07:39:14 -06002749 VK_DEBUG_REPORT_OBJECT_TYPE_DESCRIPTOR_SET_EXT,
2750 reinterpret_cast<const uint64_t &>(set_node->set), __LINE__,
Mark Youngdfc3ecf2016-04-11 17:45:37 -06002751 DRAWSTATE_INVALID_BUFFER, "DS",
2752 "VkDescriptorSet (%#" PRIxLEAST64 ") %s (%#" PRIxLEAST64 ") at index #%u"
2753 " is not defined! Has vkCreateBuffer been called?",
2754 reinterpret_cast<const uint64_t &>(set_node->set),
2755 string_VkDescriptorType(pWDS->descriptorType),
2756 reinterpret_cast<const uint64_t &>(pWDS->pBufferInfo[j].buffer), i);
2757 } else {
2758 auto mem_entry = dev_data->memObjMap.find(buffer_node->second.mem);
2759 if (mem_entry == dev_data->memObjMap.end()) {
2760 result |= log_msg(dev_data->report_data, VK_DEBUG_REPORT_ERROR_BIT_EXT,
2761 VK_DEBUG_REPORT_OBJECT_TYPE_DESCRIPTOR_SET_EXT,
2762 reinterpret_cast<const uint64_t &>(set_node->set), __LINE__,
2763 DRAWSTATE_INVALID_BUFFER, "DS",
2764 "VkDescriptorSet (%#" PRIxLEAST64 ") %s (%#" PRIxLEAST64 ") at index"
2765 " #%u, has no memory bound to it!",
2766 reinterpret_cast<const uint64_t &>(set_node->set),
2767 string_VkDescriptorType(pWDS->descriptorType),
2768 reinterpret_cast<const uint64_t &>(pWDS->pBufferInfo[j].buffer), i);
2769 }
Tobin Ehlis5b5e7bc2016-03-09 16:12:48 -07002770 }
Mark Youngdfc3ecf2016-04-11 17:45:37 -06002771 // If it's a dynamic buffer, make sure the offsets are within the buffer.
2772 if ((pWDS->descriptorType == VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER_DYNAMIC) ||
2773 (pWDS->descriptorType == VK_DESCRIPTOR_TYPE_STORAGE_BUFFER_DYNAMIC)) {
2774 bufferSize = dev_data->bufferMap[pWDS->pBufferInfo[j].buffer].createInfo.size;
2775 uint32_t dynOffset =
2776 pCB->lastBound[VK_PIPELINE_BIND_POINT_GRAPHICS].dynamicOffsets[dynOffsetIndex];
2777 if (pWDS->pBufferInfo[j].range == VK_WHOLE_SIZE) {
2778 if ((dynOffset + pWDS->pBufferInfo[j].offset) > bufferSize) {
2779 result |= log_msg(
2780 dev_data->report_data, VK_DEBUG_REPORT_ERROR_BIT_EXT,
2781 VK_DEBUG_REPORT_OBJECT_TYPE_DESCRIPTOR_SET_EXT,
2782 reinterpret_cast<const uint64_t &>(set_node->set), __LINE__,
2783 DRAWSTATE_DYNAMIC_OFFSET_OVERFLOW, "DS",
2784 "VkDescriptorSet (%#" PRIxLEAST64 ") bound as set #%u has range of "
2785 "VK_WHOLE_SIZE but dynamic offset %#" PRIxLEAST32 ". "
2786 "combined with offset %#" PRIxLEAST64 " oversteps its buffer (%#" PRIxLEAST64
2787 ") which has a size of %#" PRIxLEAST64 ".",
2788 reinterpret_cast<const uint64_t &>(set_node->set), i, dynOffset,
2789 pWDS->pBufferInfo[j].offset,
2790 reinterpret_cast<const uint64_t &>(pWDS->pBufferInfo[j].buffer), bufferSize);
2791 }
2792 } else if ((dynOffset + pWDS->pBufferInfo[j].offset + pWDS->pBufferInfo[j].range) >
2793 bufferSize) {
2794 result |=
2795 log_msg(dev_data->report_data, VK_DEBUG_REPORT_ERROR_BIT_EXT,
2796 VK_DEBUG_REPORT_OBJECT_TYPE_DESCRIPTOR_SET_EXT,
2797 reinterpret_cast<const uint64_t &>(set_node->set), __LINE__,
2798 DRAWSTATE_DYNAMIC_OFFSET_OVERFLOW, "DS",
2799 "VkDescriptorSet (%#" PRIxLEAST64
2800 ") bound as set #%u has dynamic offset %#" PRIxLEAST32 ". "
2801 "Combined with offset %#" PRIxLEAST64 " and range %#" PRIxLEAST64
2802 " from its update, this oversteps its buffer "
2803 "(%#" PRIxLEAST64 ") which has a size of %#" PRIxLEAST64 ".",
2804 reinterpret_cast<const uint64_t &>(set_node->set), i, dynOffset,
2805 pWDS->pBufferInfo[j].offset, pWDS->pBufferInfo[j].range,
2806 reinterpret_cast<const uint64_t &>(pWDS->pBufferInfo[j].buffer), bufferSize);
2807 }
2808 dynOffsetIndex++;
2809 }
Tobin Ehlis5b5e7bc2016-03-09 16:12:48 -07002810 }
Mark Youngdfc3ecf2016-04-11 17:45:37 -06002811 }
2812 if (pWDS->descriptorType == VK_DESCRIPTOR_TYPE_STORAGE_IMAGE) {
Tobin Ehlise1e6c092016-03-31 18:02:51 -06002813 for (uint32_t j = 0; j < pWDS->descriptorCount; ++j) {
2814 pCB->updateImages.insert(pWDS->pImageInfo[j].imageView);
2815 }
2816 } else if (pWDS->descriptorType == VK_DESCRIPTOR_TYPE_STORAGE_TEXEL_BUFFER) {
2817 for (uint32_t j = 0; j < pWDS->descriptorCount; ++j) {
2818 assert(dev_data->bufferViewMap.find(pWDS->pTexelBufferView[j]) != dev_data->bufferViewMap.end());
2819 pCB->updateBuffers.insert(dev_data->bufferViewMap[pWDS->pTexelBufferView[j]].buffer);
2820 }
2821 } else if (pWDS->descriptorType == VK_DESCRIPTOR_TYPE_STORAGE_BUFFER ||
2822 pWDS->descriptorType == VK_DESCRIPTOR_TYPE_STORAGE_BUFFER_DYNAMIC) {
2823 for (uint32_t j = 0; j < pWDS->descriptorCount; ++j) {
2824 pCB->updateBuffers.insert(pWDS->pBufferInfo[j].buffer);
2825 }
Tobin Ehlis7a3985a2016-03-25 11:49:51 -06002826 }
Tobin Ehlise1e6c092016-03-31 18:02:51 -06002827 i += pWDS->descriptorCount; // Advance i to end of this set of descriptors (++i at end of for loop will move 1
2828 // index past last of these descriptors)
2829 break;
2830 default: // Currently only shadowing Write update nodes so shouldn't get here
2831 assert(0);
2832 continue;
Tobin Ehlis5b5e7bc2016-03-09 16:12:48 -07002833 }
Tobin Ehlis5b5e7bc2016-03-09 16:12:48 -07002834 }
2835 }
2836 }
2837 }
2838 return result;
2839}
Tobin Ehlis7a3985a2016-03-25 11:49:51 -06002840// TODO : This is a temp function that naively updates bound storage images and buffers based on which descriptor sets are bound.
2841// When validate_and_update_draw_state() handles computer shaders so that active_slots is correct for compute pipelines, this
2842// function can be killed and validate_and_update_draw_state() used instead
2843static void update_shader_storage_images_and_buffers(layer_data *dev_data, GLOBAL_CB_NODE *pCB) {
2844 VkWriteDescriptorSet *pWDS = nullptr;
2845 SET_NODE *pSet = nullptr;
2846 // For the bound descriptor sets, pull off any storage images and buffers
2847 // This may be more than are actually updated depending on which are active, but for now this is a stop-gap for compute
2848 // pipelines
2849 for (auto set : pCB->lastBound[VK_PIPELINE_BIND_POINT_COMPUTE].uniqueBoundSets) {
2850 // Get the set node
2851 pSet = getSetNode(dev_data, set);
2852 // For each update in the set
2853 for (auto pUpdate : pSet->pDescriptorUpdates) {
2854 // If it's a write update to STORAGE type capture image/buffer being updated
2855 if (pUpdate && (pUpdate->sType == VK_STRUCTURE_TYPE_WRITE_DESCRIPTOR_SET)) {
2856 pWDS = reinterpret_cast<VkWriteDescriptorSet *>(pUpdate);
2857 if (pWDS->descriptorType == VK_DESCRIPTOR_TYPE_STORAGE_IMAGE) {
2858 for (uint32_t j = 0; j < pWDS->descriptorCount; ++j) {
2859 pCB->updateImages.insert(pWDS->pImageInfo[j].imageView);
2860 }
2861 } else if (pWDS->descriptorType == VK_DESCRIPTOR_TYPE_STORAGE_TEXEL_BUFFER) {
2862 for (uint32_t j = 0; j < pWDS->descriptorCount; ++j) {
2863 pCB->updateBuffers.insert(dev_data->bufferViewMap[pWDS->pTexelBufferView[j]].buffer);
2864 }
2865 } else if (pWDS->descriptorType == VK_DESCRIPTOR_TYPE_STORAGE_BUFFER ||
2866 pWDS->descriptorType == VK_DESCRIPTOR_TYPE_STORAGE_BUFFER_DYNAMIC) {
2867 for (uint32_t j = 0; j < pWDS->descriptorCount; ++j) {
2868 pCB->updateBuffers.insert(pWDS->pBufferInfo[j].buffer);
2869 }
2870 }
2871 }
2872 }
2873 }
2874}
Tobin Ehlis5b5e7bc2016-03-09 16:12:48 -07002875
2876// Validate overall state at the time of a draw call
Dustin Gravese3319182016-04-05 09:41:17 -06002877static bool validate_and_update_draw_state(layer_data *my_data, GLOBAL_CB_NODE *pCB, const bool indexedDraw,
2878 const VkPipelineBindPoint bindPoint) {
2879 bool result = false;
Tobin Ehlis7a3985a2016-03-25 11:49:51 -06002880 auto const &state = pCB->lastBound[bindPoint];
2881 PIPELINE_NODE *pPipe = getPipeline(my_data, state.pipeline);
Tobin Ehlis3d3e06b2016-03-28 11:18:19 -06002882 // First check flag states
Tobin Ehlis7a3985a2016-03-25 11:49:51 -06002883 if (VK_PIPELINE_BIND_POINT_GRAPHICS == bindPoint)
2884 result = validate_draw_state_flags(my_data, pCB, pPipe, indexedDraw);
2885
Tobin Ehlis5b5e7bc2016-03-09 16:12:48 -07002886 // Now complete other state checks
2887 // TODO : Currently only performing next check if *something* was bound (non-zero last bound)
2888 // There is probably a better way to gate when this check happens, and to know if something *should* have been bound
2889 // We should have that check separately and then gate this check based on that check
2890 if (pPipe) {
Tobin Ehlis72d66f02016-03-21 14:14:44 -06002891 if (state.pipelineLayout) {
Tobin Ehlis5b5e7bc2016-03-09 16:12:48 -07002892 string errorString;
2893 // Need a vector (vs. std::set) of active Sets for dynamicOffset validation in case same set bound w/ different offsets
Tobin Ehlisa61b5372016-03-24 09:17:25 -06002894 vector<std::pair<SET_NODE *, unordered_set<uint32_t>>> activeSetBindingsPairs;
2895 for (auto setBindingPair : pPipe->active_slots) {
2896 uint32_t setIndex = setBindingPair.first;
Tobin Ehlis5b5e7bc2016-03-09 16:12:48 -07002897 // If valid set is not bound throw an error
Tobin Ehlis72d66f02016-03-21 14:14:44 -06002898 if ((state.boundDescriptorSets.size() <= setIndex) || (!state.boundDescriptorSets[setIndex])) {
Tobin Ehlis5b5e7bc2016-03-09 16:12:48 -07002899 result |= log_msg(my_data->report_data, VK_DEBUG_REPORT_ERROR_BIT_EXT, (VkDebugReportObjectTypeEXT)0, 0,
2900 __LINE__, DRAWSTATE_DESCRIPTOR_SET_NOT_BOUND, "DS",
2901 "VkPipeline %#" PRIxLEAST64 " uses set #%u but that set is not bound.",
2902 (uint64_t)pPipe->pipeline, setIndex);
Tobin Ehlis72d66f02016-03-21 14:14:44 -06002903 } else if (!verify_set_layout_compatibility(my_data, my_data->setMap[state.boundDescriptorSets[setIndex]],
Tobin Ehlis5b5e7bc2016-03-09 16:12:48 -07002904 pPipe->graphicsPipelineCI.layout, setIndex, errorString)) {
2905 // Set is bound but not compatible w/ overlapping pipelineLayout from PSO
Tobin Ehlis72d66f02016-03-21 14:14:44 -06002906 VkDescriptorSet setHandle = my_data->setMap[state.boundDescriptorSets[setIndex]]->set;
Tobin Ehlis5b5e7bc2016-03-09 16:12:48 -07002907 result |= log_msg(
2908 my_data->report_data, VK_DEBUG_REPORT_ERROR_BIT_EXT, VK_DEBUG_REPORT_OBJECT_TYPE_DESCRIPTOR_SET_EXT,
2909 (uint64_t)setHandle, __LINE__, DRAWSTATE_PIPELINE_LAYOUTS_INCOMPATIBLE, "DS",
2910 "VkDescriptorSet (%#" PRIxLEAST64
2911 ") bound as set #%u is not compatible with overlapping VkPipelineLayout %#" PRIxLEAST64 " due to: %s",
2912 (uint64_t)setHandle, setIndex, (uint64_t)pPipe->graphicsPipelineCI.layout, errorString.c_str());
Tobin Ehlis7a3985a2016-03-25 11:49:51 -06002913 } else { // Valid set is bound and layout compatible, validate that it's updated
Tobin Ehlis5b5e7bc2016-03-09 16:12:48 -07002914 // Pull the set node
Tobin Ehlis72d66f02016-03-21 14:14:44 -06002915 SET_NODE *pSet = my_data->setMap[state.boundDescriptorSets[setIndex]];
Tobin Ehlis5b5e7bc2016-03-09 16:12:48 -07002916 // Save vector of all active sets to verify dynamicOffsets below
Tobin Ehlisa61b5372016-03-24 09:17:25 -06002917 // activeSetNodes.push_back(pSet);
2918 activeSetBindingsPairs.push_back(std::make_pair(pSet, setBindingPair.second));
Tobin Ehlis5b5e7bc2016-03-09 16:12:48 -07002919 // Make sure set has been updated
2920 if (!pSet->pUpdateStructs) {
2921 result |= log_msg(my_data->report_data, VK_DEBUG_REPORT_ERROR_BIT_EXT,
2922 VK_DEBUG_REPORT_OBJECT_TYPE_DESCRIPTOR_SET_EXT, (uint64_t)pSet->set, __LINE__,
2923 DRAWSTATE_DESCRIPTOR_SET_NOT_UPDATED, "DS",
2924 "DS %#" PRIxLEAST64 " bound but it was never updated. It is now being used to draw so "
2925 "this will result in undefined behavior.",
2926 (uint64_t)pSet->set);
2927 }
2928 }
2929 }
Tobin Ehlis7a3985a2016-03-25 11:49:51 -06002930 // For given active slots, verify any dynamic descriptors and record updated images & buffers
2931 result |= validate_and_update_drawtime_descriptor_state(my_data, pCB, activeSetBindingsPairs);
Tobin Ehlis5b5e7bc2016-03-09 16:12:48 -07002932 }
Tobin Ehlis7a3985a2016-03-25 11:49:51 -06002933 if (VK_PIPELINE_BIND_POINT_GRAPHICS == bindPoint) {
2934 // Verify Vtx binding
2935 if (pPipe->vertexBindingDescriptions.size() > 0) {
2936 for (size_t i = 0; i < pPipe->vertexBindingDescriptions.size(); i++) {
2937 if ((pCB->currentDrawData.buffers.size() < (i + 1)) || (pCB->currentDrawData.buffers[i] == VK_NULL_HANDLE)) {
2938 result |= log_msg(my_data->report_data, VK_DEBUG_REPORT_ERROR_BIT_EXT, (VkDebugReportObjectTypeEXT)0, 0,
2939 __LINE__, DRAWSTATE_VTX_INDEX_OUT_OF_BOUNDS, "DS",
2940 "The Pipeline State Object (%#" PRIxLEAST64
2941 ") expects that this Command Buffer's vertex binding Index " PRINTF_SIZE_T_SPECIFIER
2942 " should be set via vkCmdBindVertexBuffers.",
2943 (uint64_t)state.pipeline, i);
2944 }
2945 }
2946 } else {
2947 if (!pCB->currentDrawData.buffers.empty()) {
2948 result |= log_msg(my_data->report_data, VK_DEBUG_REPORT_PERFORMANCE_WARNING_BIT_EXT,
2949 (VkDebugReportObjectTypeEXT)0, 0, __LINE__, DRAWSTATE_VTX_INDEX_OUT_OF_BOUNDS, "DS",
2950 "Vertex buffers are bound to command buffer (%#" PRIxLEAST64
2951 ") but no vertex buffers are attached to this Pipeline State Object (%#" PRIxLEAST64 ").",
2952 (uint64_t)pCB->commandBuffer, (uint64_t)state.pipeline);
Tobin Ehlis5b5e7bc2016-03-09 16:12:48 -07002953 }
2954 }
Tobin Ehlis7a3985a2016-03-25 11:49:51 -06002955 // If Viewport or scissors are dynamic, verify that dynamic count matches PSO count.
2956 // Skip check if rasterization is disabled or there is no viewport.
2957 if ((!pPipe->graphicsPipelineCI.pRasterizationState ||
Dustin Graves45824952016-04-05 15:15:40 -06002958 (pPipe->graphicsPipelineCI.pRasterizationState->rasterizerDiscardEnable == VK_FALSE)) &&
Tobin Ehlis7a3985a2016-03-25 11:49:51 -06002959 pPipe->graphicsPipelineCI.pViewportState) {
Dustin Gravese3319182016-04-05 09:41:17 -06002960 bool dynViewport = isDynamic(pPipe, VK_DYNAMIC_STATE_VIEWPORT);
2961 bool dynScissor = isDynamic(pPipe, VK_DYNAMIC_STATE_SCISSOR);
Tobin Ehlis7a3985a2016-03-25 11:49:51 -06002962 if (dynViewport) {
2963 if (pCB->viewports.size() != pPipe->graphicsPipelineCI.pViewportState->viewportCount) {
2964 result |= log_msg(my_data->report_data, VK_DEBUG_REPORT_ERROR_BIT_EXT, (VkDebugReportObjectTypeEXT)0, 0,
2965 __LINE__, DRAWSTATE_VIEWPORT_SCISSOR_MISMATCH, "DS",
2966 "Dynamic viewportCount from vkCmdSetViewport() is " PRINTF_SIZE_T_SPECIFIER
2967 ", but PSO viewportCount is %u. These counts must match.",
2968 pCB->viewports.size(), pPipe->graphicsPipelineCI.pViewportState->viewportCount);
2969 }
Tobin Ehlis5b5e7bc2016-03-09 16:12:48 -07002970 }
Tobin Ehlis7a3985a2016-03-25 11:49:51 -06002971 if (dynScissor) {
2972 if (pCB->scissors.size() != pPipe->graphicsPipelineCI.pViewportState->scissorCount) {
2973 result |= log_msg(my_data->report_data, VK_DEBUG_REPORT_ERROR_BIT_EXT, (VkDebugReportObjectTypeEXT)0, 0,
2974 __LINE__, DRAWSTATE_VIEWPORT_SCISSOR_MISMATCH, "DS",
2975 "Dynamic scissorCount from vkCmdSetScissor() is " PRINTF_SIZE_T_SPECIFIER
2976 ", but PSO scissorCount is %u. These counts must match.",
2977 pCB->scissors.size(), pPipe->graphicsPipelineCI.pViewportState->scissorCount);
2978 }
Tobin Ehlis5b5e7bc2016-03-09 16:12:48 -07002979 }
2980 }
2981 }
2982 }
2983 return result;
2984}
2985
2986// Verify that create state for a pipeline is valid
Dustin Gravese3319182016-04-05 09:41:17 -06002987static bool verifyPipelineCreateState(layer_data *my_data, const VkDevice device, std::vector<PIPELINE_NODE *> pPipelines,
2988 int pipelineIndex) {
2989 bool skipCall = false;
Tobin Ehlis5b5e7bc2016-03-09 16:12:48 -07002990
2991 PIPELINE_NODE *pPipeline = pPipelines[pipelineIndex];
2992
2993 // If create derivative bit is set, check that we've specified a base
2994 // pipeline correctly, and that the base pipeline was created to allow
2995 // derivatives.
2996 if (pPipeline->graphicsPipelineCI.flags & VK_PIPELINE_CREATE_DERIVATIVE_BIT) {
2997 PIPELINE_NODE *pBasePipeline = nullptr;
2998 if (!((pPipeline->graphicsPipelineCI.basePipelineHandle != VK_NULL_HANDLE) ^
2999 (pPipeline->graphicsPipelineCI.basePipelineIndex != -1))) {
3000 skipCall |= log_msg(my_data->report_data, VK_DEBUG_REPORT_ERROR_BIT_EXT, (VkDebugReportObjectTypeEXT)0, 0, __LINE__,
3001 DRAWSTATE_INVALID_PIPELINE_CREATE_STATE, "DS",
3002 "Invalid Pipeline CreateInfo: exactly one of base pipeline index and handle must be specified");
3003 } else if (pPipeline->graphicsPipelineCI.basePipelineIndex != -1) {
3004 if (pPipeline->graphicsPipelineCI.basePipelineIndex >= pipelineIndex) {
3005 skipCall |=
3006 log_msg(my_data->report_data, VK_DEBUG_REPORT_ERROR_BIT_EXT, (VkDebugReportObjectTypeEXT)0, 0, __LINE__,
3007 DRAWSTATE_INVALID_PIPELINE_CREATE_STATE, "DS",
3008 "Invalid Pipeline CreateInfo: base pipeline must occur earlier in array than derivative pipeline.");
3009 } else {
3010 pBasePipeline = pPipelines[pPipeline->graphicsPipelineCI.basePipelineIndex];
3011 }
3012 } else if (pPipeline->graphicsPipelineCI.basePipelineHandle != VK_NULL_HANDLE) {
3013 pBasePipeline = getPipeline(my_data, pPipeline->graphicsPipelineCI.basePipelineHandle);
3014 }
3015
3016 if (pBasePipeline && !(pBasePipeline->graphicsPipelineCI.flags & VK_PIPELINE_CREATE_ALLOW_DERIVATIVES_BIT)) {
3017 skipCall |= log_msg(my_data->report_data, VK_DEBUG_REPORT_ERROR_BIT_EXT, (VkDebugReportObjectTypeEXT)0, 0, __LINE__,
3018 DRAWSTATE_INVALID_PIPELINE_CREATE_STATE, "DS",
3019 "Invalid Pipeline CreateInfo: base pipeline does not allow derivatives.");
3020 }
3021 }
3022
3023 if (pPipeline->graphicsPipelineCI.pColorBlendState != NULL) {
Tobin Ehlisb1b606d2016-04-11 14:49:55 -06003024 if (!my_data->phys_dev_properties.features.independentBlend) {
Tobin Ehlis3d3e06b2016-03-28 11:18:19 -06003025 if (pPipeline->attachments.size() > 1) {
Chris Forbes26c54882016-03-24 11:42:09 +13003026 VkPipelineColorBlendAttachmentState *pAttachments = &pPipeline->attachments[0];
Mark Lobodzinskic7bd67f2016-03-23 14:34:52 -06003027 for (size_t i = 1; i < pPipeline->attachments.size(); i++) {
3028 if ((pAttachments[0].blendEnable != pAttachments[i].blendEnable) ||
3029 (pAttachments[0].srcColorBlendFactor != pAttachments[i].srcColorBlendFactor) ||
3030 (pAttachments[0].dstColorBlendFactor != pAttachments[i].dstColorBlendFactor) ||
3031 (pAttachments[0].colorBlendOp != pAttachments[i].colorBlendOp) ||
3032 (pAttachments[0].srcAlphaBlendFactor != pAttachments[i].srcAlphaBlendFactor) ||
3033 (pAttachments[0].dstAlphaBlendFactor != pAttachments[i].dstAlphaBlendFactor) ||
3034 (pAttachments[0].alphaBlendOp != pAttachments[i].alphaBlendOp) ||
3035 (pAttachments[0].colorWriteMask != pAttachments[i].colorWriteMask)) {
3036 skipCall |=
3037 log_msg(my_data->report_data, VK_DEBUG_REPORT_ERROR_BIT_EXT, (VkDebugReportObjectTypeEXT)0, 0, __LINE__,
3038 DRAWSTATE_INDEPENDENT_BLEND, "DS", "Invalid Pipeline CreateInfo: If independent blend feature not "
3039 "enabled, all elements of pAttachments must be identical");
3040 }
Tobin Ehlis5b5e7bc2016-03-09 16:12:48 -07003041 }
3042 }
3043 }
Tobin Ehlisb1b606d2016-04-11 14:49:55 -06003044 if (!my_data->phys_dev_properties.features.logicOp &&
Tobin Ehlis5b5e7bc2016-03-09 16:12:48 -07003045 (pPipeline->graphicsPipelineCI.pColorBlendState->logicOpEnable != VK_FALSE)) {
3046 skipCall |=
3047 log_msg(my_data->report_data, VK_DEBUG_REPORT_ERROR_BIT_EXT, (VkDebugReportObjectTypeEXT)0, 0, __LINE__,
3048 DRAWSTATE_DISABLED_LOGIC_OP, "DS",
3049 "Invalid Pipeline CreateInfo: If logic operations feature not enabled, logicOpEnable must be VK_FALSE");
3050 }
3051 if ((pPipeline->graphicsPipelineCI.pColorBlendState->logicOpEnable == VK_TRUE) &&
3052 ((pPipeline->graphicsPipelineCI.pColorBlendState->logicOp < VK_LOGIC_OP_CLEAR) ||
3053 (pPipeline->graphicsPipelineCI.pColorBlendState->logicOp > VK_LOGIC_OP_SET))) {
3054 skipCall |=
3055 log_msg(my_data->report_data, VK_DEBUG_REPORT_ERROR_BIT_EXT, (VkDebugReportObjectTypeEXT)0, 0, __LINE__,
3056 DRAWSTATE_INVALID_LOGIC_OP, "DS",
3057 "Invalid Pipeline CreateInfo: If logicOpEnable is VK_TRUE, logicOp must be a valid VkLogicOp value");
3058 }
3059 }
3060
Tobin Ehlisa61b5372016-03-24 09:17:25 -06003061 // Ensure the subpass index is valid. If not, then validate_and_capture_pipeline_shader_state
Tobin Ehlis5b5e7bc2016-03-09 16:12:48 -07003062 // produces nonsense errors that confuse users. Other layers should already
3063 // emit errors for renderpass being invalid.
3064 auto rp_data = my_data->renderPassMap.find(pPipeline->graphicsPipelineCI.renderPass);
3065 if (rp_data != my_data->renderPassMap.end() &&
3066 pPipeline->graphicsPipelineCI.subpass >= rp_data->second->pCreateInfo->subpassCount) {
3067 skipCall |= log_msg(my_data->report_data, VK_DEBUG_REPORT_ERROR_BIT_EXT, (VkDebugReportObjectTypeEXT)0, 0, __LINE__,
3068 DRAWSTATE_INVALID_PIPELINE_CREATE_STATE, "DS", "Invalid Pipeline CreateInfo State: Subpass index %u "
3069 "is out of range for this renderpass (0..%u)",
3070 pPipeline->graphicsPipelineCI.subpass, rp_data->second->pCreateInfo->subpassCount - 1);
3071 }
3072
Chris Forbes6c94fb92016-03-30 11:35:21 +13003073 if (!validate_and_capture_pipeline_shader_state(my_data, pPipeline)) {
Dustin Gravese3319182016-04-05 09:41:17 -06003074 skipCall = true;
Tobin Ehlis5b5e7bc2016-03-09 16:12:48 -07003075 }
Chris Forbes52156ec2016-04-06 11:21:28 +12003076 // Each shader's stage must be unique
3077 if (pPipeline->duplicate_shaders) {
3078 for (uint32_t stage = VK_SHADER_STAGE_VERTEX_BIT; stage & VK_SHADER_STAGE_ALL_GRAPHICS; stage <<= 1) {
3079 if (pPipeline->duplicate_shaders & stage) {
3080 skipCall |= log_msg(my_data->report_data, VK_DEBUG_REPORT_ERROR_BIT_EXT, VkDebugReportObjectTypeEXT(0), 0,
3081 __LINE__, DRAWSTATE_INVALID_PIPELINE_CREATE_STATE, "DS",
3082 "Invalid Pipeline CreateInfo State: Multiple shaders provided for stage %s",
3083 string_VkShaderStageFlagBits(VkShaderStageFlagBits(stage)));
3084 }
3085 }
3086 }
Tobin Ehlis5b5e7bc2016-03-09 16:12:48 -07003087 // VS is required
3088 if (!(pPipeline->active_shaders & VK_SHADER_STAGE_VERTEX_BIT)) {
3089 skipCall |=
3090 log_msg(my_data->report_data, VK_DEBUG_REPORT_ERROR_BIT_EXT, (VkDebugReportObjectTypeEXT)0, 0, __LINE__,
3091 DRAWSTATE_INVALID_PIPELINE_CREATE_STATE, "DS", "Invalid Pipeline CreateInfo State: Vtx Shader required");
3092 }
3093 // Either both or neither TC/TE shaders should be defined
3094 if (((pPipeline->active_shaders & VK_SHADER_STAGE_TESSELLATION_CONTROL_BIT) == 0) !=
3095 ((pPipeline->active_shaders & VK_SHADER_STAGE_TESSELLATION_EVALUATION_BIT) == 0)) {
3096 skipCall |= log_msg(my_data->report_data, VK_DEBUG_REPORT_ERROR_BIT_EXT, (VkDebugReportObjectTypeEXT)0, 0, __LINE__,
3097 DRAWSTATE_INVALID_PIPELINE_CREATE_STATE, "DS",
3098 "Invalid Pipeline CreateInfo State: TE and TC shaders must be included or excluded as a pair");
3099 }
3100 // Compute shaders should be specified independent of Gfx shaders
3101 if ((pPipeline->active_shaders & VK_SHADER_STAGE_COMPUTE_BIT) &&
3102 (pPipeline->active_shaders &
3103 (VK_SHADER_STAGE_VERTEX_BIT | VK_SHADER_STAGE_TESSELLATION_CONTROL_BIT | VK_SHADER_STAGE_TESSELLATION_EVALUATION_BIT |
3104 VK_SHADER_STAGE_GEOMETRY_BIT | VK_SHADER_STAGE_FRAGMENT_BIT))) {
3105 skipCall |= log_msg(my_data->report_data, VK_DEBUG_REPORT_ERROR_BIT_EXT, (VkDebugReportObjectTypeEXT)0, 0, __LINE__,
3106 DRAWSTATE_INVALID_PIPELINE_CREATE_STATE, "DS",
3107 "Invalid Pipeline CreateInfo State: Do not specify Compute Shader for Gfx Pipeline");
3108 }
3109 // VK_PRIMITIVE_TOPOLOGY_PATCH_LIST primitive topology is only valid for tessellation pipelines.
3110 // Mismatching primitive topology and tessellation fails graphics pipeline creation.
3111 if (pPipeline->active_shaders & (VK_SHADER_STAGE_TESSELLATION_CONTROL_BIT | VK_SHADER_STAGE_TESSELLATION_EVALUATION_BIT) &&
Tobin Ehlisca546212016-04-01 13:51:33 -06003112 (!pPipeline->graphicsPipelineCI.pInputAssemblyState ||
3113 pPipeline->graphicsPipelineCI.pInputAssemblyState->topology != VK_PRIMITIVE_TOPOLOGY_PATCH_LIST)) {
Tobin Ehlis5b5e7bc2016-03-09 16:12:48 -07003114 skipCall |= log_msg(my_data->report_data, VK_DEBUG_REPORT_ERROR_BIT_EXT, (VkDebugReportObjectTypeEXT)0, 0, __LINE__,
3115 DRAWSTATE_INVALID_PIPELINE_CREATE_STATE, "DS", "Invalid Pipeline CreateInfo State: "
3116 "VK_PRIMITIVE_TOPOLOGY_PATCH_LIST must be set as IA "
3117 "topology for tessellation pipelines");
3118 }
Tobin Ehlisca546212016-04-01 13:51:33 -06003119 if (pPipeline->graphicsPipelineCI.pInputAssemblyState &&
3120 pPipeline->graphicsPipelineCI.pInputAssemblyState->topology == VK_PRIMITIVE_TOPOLOGY_PATCH_LIST) {
Tobin Ehlis5b5e7bc2016-03-09 16:12:48 -07003121 if (~pPipeline->active_shaders & VK_SHADER_STAGE_TESSELLATION_CONTROL_BIT) {
3122 skipCall |= log_msg(my_data->report_data, VK_DEBUG_REPORT_ERROR_BIT_EXT, (VkDebugReportObjectTypeEXT)0, 0, __LINE__,
3123 DRAWSTATE_INVALID_PIPELINE_CREATE_STATE, "DS", "Invalid Pipeline CreateInfo State: "
3124 "VK_PRIMITIVE_TOPOLOGY_PATCH_LIST primitive "
3125 "topology is only valid for tessellation pipelines");
3126 }
Tobin Ehlisca546212016-04-01 13:51:33 -06003127 if (!pPipeline->graphicsPipelineCI.pTessellationState) {
3128 skipCall |= log_msg(my_data->report_data, VK_DEBUG_REPORT_ERROR_BIT_EXT, (VkDebugReportObjectTypeEXT)0, 0, __LINE__,
3129 DRAWSTATE_INVALID_PIPELINE_CREATE_STATE, "DS",
3130 "Invalid Pipeline CreateInfo State: "
3131 "pTessellationState is NULL when VK_PRIMITIVE_TOPOLOGY_PATCH_LIST primitive "
3132 "topology used. pTessellationState must not be NULL in this case.");
3133 } else if (!pPipeline->graphicsPipelineCI.pTessellationState->patchControlPoints ||
3134 (pPipeline->graphicsPipelineCI.pTessellationState->patchControlPoints > 32)) {
Tobin Ehlis5b5e7bc2016-03-09 16:12:48 -07003135 skipCall |= log_msg(my_data->report_data, VK_DEBUG_REPORT_ERROR_BIT_EXT, (VkDebugReportObjectTypeEXT)0, 0, __LINE__,
3136 DRAWSTATE_INVALID_PIPELINE_CREATE_STATE, "DS", "Invalid Pipeline CreateInfo State: "
3137 "VK_PRIMITIVE_TOPOLOGY_PATCH_LIST primitive "
3138 "topology used with patchControlPoints value %u."
3139 " patchControlPoints should be >0 and <=32.",
Tobin Ehlisca546212016-04-01 13:51:33 -06003140 pPipeline->graphicsPipelineCI.pTessellationState->patchControlPoints);
Tobin Ehlis5b5e7bc2016-03-09 16:12:48 -07003141 }
3142 }
3143 // Viewport state must be included if rasterization is enabled.
3144 // If the viewport state is included, the viewport and scissor counts should always match.
3145 // NOTE : Even if these are flagged as dynamic, counts need to be set correctly for shader compiler
3146 if (!pPipeline->graphicsPipelineCI.pRasterizationState ||
Dustin Graves45824952016-04-05 15:15:40 -06003147 (pPipeline->graphicsPipelineCI.pRasterizationState->rasterizerDiscardEnable == VK_FALSE)) {
Tobin Ehlis5b5e7bc2016-03-09 16:12:48 -07003148 if (!pPipeline->graphicsPipelineCI.pViewportState) {
3149 skipCall |= log_msg(my_data->report_data, VK_DEBUG_REPORT_ERROR_BIT_EXT, (VkDebugReportObjectTypeEXT)0, 0, __LINE__,
3150 DRAWSTATE_VIEWPORT_SCISSOR_MISMATCH, "DS", "Gfx Pipeline pViewportState is null. Even if viewport "
3151 "and scissors are dynamic PSO must include "
3152 "viewportCount and scissorCount in pViewportState.");
3153 } else if (pPipeline->graphicsPipelineCI.pViewportState->scissorCount !=
3154 pPipeline->graphicsPipelineCI.pViewportState->viewportCount) {
3155 skipCall |= log_msg(my_data->report_data, VK_DEBUG_REPORT_ERROR_BIT_EXT, (VkDebugReportObjectTypeEXT)0, 0, __LINE__,
3156 DRAWSTATE_VIEWPORT_SCISSOR_MISMATCH, "DS",
3157 "Gfx Pipeline viewport count (%u) must match scissor count (%u).",
Tobin Ehlisca546212016-04-01 13:51:33 -06003158 pPipeline->graphicsPipelineCI.pViewportState->viewportCount,
3159 pPipeline->graphicsPipelineCI.pViewportState->scissorCount);
Tobin Ehlis5b5e7bc2016-03-09 16:12:48 -07003160 } else {
3161 // If viewport or scissor are not dynamic, then verify that data is appropriate for count
Dustin Gravese3319182016-04-05 09:41:17 -06003162 bool dynViewport = isDynamic(pPipeline, VK_DYNAMIC_STATE_VIEWPORT);
3163 bool dynScissor = isDynamic(pPipeline, VK_DYNAMIC_STATE_SCISSOR);
Tobin Ehlis5b5e7bc2016-03-09 16:12:48 -07003164 if (!dynViewport) {
3165 if (pPipeline->graphicsPipelineCI.pViewportState->viewportCount &&
3166 !pPipeline->graphicsPipelineCI.pViewportState->pViewports) {
3167 skipCall |= log_msg(my_data->report_data, VK_DEBUG_REPORT_ERROR_BIT_EXT, (VkDebugReportObjectTypeEXT)0, 0,
3168 __LINE__, DRAWSTATE_VIEWPORT_SCISSOR_MISMATCH, "DS",
3169 "Gfx Pipeline viewportCount is %u, but pViewports is NULL. For non-zero viewportCount, you "
3170 "must either include pViewports data, or include viewport in pDynamicState and set it with "
3171 "vkCmdSetViewport().",
3172 pPipeline->graphicsPipelineCI.pViewportState->viewportCount);
3173 }
3174 }
3175 if (!dynScissor) {
3176 if (pPipeline->graphicsPipelineCI.pViewportState->scissorCount &&
3177 !pPipeline->graphicsPipelineCI.pViewportState->pScissors) {
3178 skipCall |= log_msg(my_data->report_data, VK_DEBUG_REPORT_ERROR_BIT_EXT, (VkDebugReportObjectTypeEXT)0, 0,
3179 __LINE__, DRAWSTATE_VIEWPORT_SCISSOR_MISMATCH, "DS",
3180 "Gfx Pipeline scissorCount is %u, but pScissors is NULL. For non-zero scissorCount, you "
3181 "must either include pScissors data, or include scissor in pDynamicState and set it with "
3182 "vkCmdSetScissor().",
3183 pPipeline->graphicsPipelineCI.pViewportState->scissorCount);
3184 }
3185 }
3186 }
3187 }
3188 return skipCall;
3189}
3190
Tobin Ehlis5b5e7bc2016-03-09 16:12:48 -07003191// Free the Pipeline nodes
3192static void deletePipelines(layer_data *my_data) {
3193 if (my_data->pipelineMap.size() <= 0)
3194 return;
Tobin Ehlisca546212016-04-01 13:51:33 -06003195 for (auto &pipe_map_pair : my_data->pipelineMap) {
3196 delete pipe_map_pair.second;
Tobin Ehlis5b5e7bc2016-03-09 16:12:48 -07003197 }
3198 my_data->pipelineMap.clear();
3199}
3200
3201// For given pipeline, return number of MSAA samples, or one if MSAA disabled
3202static VkSampleCountFlagBits getNumSamples(layer_data *my_data, const VkPipeline pipeline) {
3203 PIPELINE_NODE *pPipe = my_data->pipelineMap[pipeline];
Tobin Ehlisca546212016-04-01 13:51:33 -06003204 if (pPipe->graphicsPipelineCI.pMultisampleState &&
3205 (VK_STRUCTURE_TYPE_PIPELINE_MULTISAMPLE_STATE_CREATE_INFO == pPipe->graphicsPipelineCI.pMultisampleState->sType)) {
3206 return pPipe->graphicsPipelineCI.pMultisampleState->rasterizationSamples;
Tobin Ehlis5b5e7bc2016-03-09 16:12:48 -07003207 }
3208 return VK_SAMPLE_COUNT_1_BIT;
3209}
3210
3211// Validate state related to the PSO
Dustin Gravese3319182016-04-05 09:41:17 -06003212static bool validatePipelineState(layer_data *my_data, const GLOBAL_CB_NODE *pCB, const VkPipelineBindPoint pipelineBindPoint,
3213 const VkPipeline pipeline) {
3214 bool skipCall = false;
Tobin Ehlis5b5e7bc2016-03-09 16:12:48 -07003215 if (VK_PIPELINE_BIND_POINT_GRAPHICS == pipelineBindPoint) {
3216 // Verify that any MSAA request in PSO matches sample# in bound FB
3217 // Skip the check if rasterization is disabled.
3218 PIPELINE_NODE *pPipeline = my_data->pipelineMap[pipeline];
3219 if (!pPipeline->graphicsPipelineCI.pRasterizationState ||
Dustin Graves45824952016-04-05 15:15:40 -06003220 (pPipeline->graphicsPipelineCI.pRasterizationState->rasterizerDiscardEnable == VK_FALSE)) {
Tobin Ehlis5b5e7bc2016-03-09 16:12:48 -07003221 VkSampleCountFlagBits psoNumSamples = getNumSamples(my_data, pipeline);
3222 if (pCB->activeRenderPass) {
3223 const VkRenderPassCreateInfo *pRPCI = my_data->renderPassMap[pCB->activeRenderPass]->pCreateInfo;
3224 const VkSubpassDescription *pSD = &pRPCI->pSubpasses[pCB->activeSubpass];
3225 VkSampleCountFlagBits subpassNumSamples = (VkSampleCountFlagBits)0;
3226 uint32_t i;
3227
Mark Young6f65b422016-04-11 14:11:46 -06003228 const VkPipelineColorBlendStateCreateInfo *pColorBlendState = pPipeline->graphicsPipelineCI.pColorBlendState;
3229 if ((pColorBlendState != NULL) && (pCB->activeSubpass == pPipeline->graphicsPipelineCI.subpass) &&
3230 (pColorBlendState->attachmentCount != pSD->colorAttachmentCount)) {
3231 return log_msg(my_data->report_data, VK_DEBUG_REPORT_ERROR_BIT_EXT, VK_DEBUG_REPORT_OBJECT_TYPE_PIPELINE_EXT,
3232 reinterpret_cast<const uint64_t &>(pipeline), __LINE__, DRAWSTATE_INVALID_RENDERPASS, "DS",
3233 "Render pass subpass %u mismatch with blending state defined and blend state attachment "
3234 "count %u but subpass color attachment count %u! These must be the same.",
3235 pCB->activeSubpass, pColorBlendState->attachmentCount, pSD->colorAttachmentCount);
3236 }
3237
Tobin Ehlis5b5e7bc2016-03-09 16:12:48 -07003238 for (i = 0; i < pSD->colorAttachmentCount; i++) {
3239 VkSampleCountFlagBits samples;
3240
3241 if (pSD->pColorAttachments[i].attachment == VK_ATTACHMENT_UNUSED)
3242 continue;
3243
3244 samples = pRPCI->pAttachments[pSD->pColorAttachments[i].attachment].samples;
3245 if (subpassNumSamples == (VkSampleCountFlagBits)0) {
3246 subpassNumSamples = samples;
3247 } else if (subpassNumSamples != samples) {
3248 subpassNumSamples = (VkSampleCountFlagBits)-1;
3249 break;
3250 }
3251 }
3252 if (pSD->pDepthStencilAttachment && pSD->pDepthStencilAttachment->attachment != VK_ATTACHMENT_UNUSED) {
3253 const VkSampleCountFlagBits samples = pRPCI->pAttachments[pSD->pDepthStencilAttachment->attachment].samples;
3254 if (subpassNumSamples == (VkSampleCountFlagBits)0)
3255 subpassNumSamples = samples;
3256 else if (subpassNumSamples != samples)
3257 subpassNumSamples = (VkSampleCountFlagBits)-1;
3258 }
3259
Dominik Witczak04826a02016-04-01 13:19:49 +02003260 if ((pSD->colorAttachmentCount > 0 || pSD->pDepthStencilAttachment) &&
3261 psoNumSamples != subpassNumSamples) {
Mark Young0292df52016-03-31 16:03:20 -06003262 skipCall |= log_msg(my_data->report_data, VK_DEBUG_REPORT_ERROR_BIT_EXT, VK_DEBUG_REPORT_OBJECT_TYPE_PIPELINE_EXT,
3263 (uint64_t)pipeline, __LINE__, DRAWSTATE_NUM_SAMPLES_MISMATCH, "DS",
3264 "Num samples mismatch! Binding PSO (%#" PRIxLEAST64
3265 ") with %u samples while current RenderPass (%#" PRIxLEAST64 ") w/ %u samples!",
3266 (uint64_t)pipeline, psoNumSamples, (uint64_t)pCB->activeRenderPass, subpassNumSamples);
Tobin Ehlis5b5e7bc2016-03-09 16:12:48 -07003267 }
3268 } else {
3269 // TODO : I believe it's an error if we reach this point and don't have an activeRenderPass
3270 // Verify and flag error as appropriate
3271 }
3272 }
3273 // TODO : Add more checks here
3274 } else {
3275 // TODO : Validate non-gfx pipeline updates
3276 }
Mark Young0292df52016-03-31 16:03:20 -06003277 return skipCall;
Tobin Ehlis5b5e7bc2016-03-09 16:12:48 -07003278}
3279
3280// Block of code at start here specifically for managing/tracking DSs
3281
3282// Return Pool node ptr for specified pool or else NULL
3283static DESCRIPTOR_POOL_NODE *getPoolNode(layer_data *my_data, const VkDescriptorPool pool) {
3284 if (my_data->descriptorPoolMap.find(pool) == my_data->descriptorPoolMap.end()) {
3285 return NULL;
3286 }
3287 return my_data->descriptorPoolMap[pool];
3288}
3289
3290static LAYOUT_NODE *getLayoutNode(layer_data *my_data, const VkDescriptorSetLayout layout) {
3291 if (my_data->descriptorSetLayoutMap.find(layout) == my_data->descriptorSetLayoutMap.end()) {
3292 return NULL;
3293 }
3294 return my_data->descriptorSetLayoutMap[layout];
3295}
3296
Dustin Gravese3319182016-04-05 09:41:17 -06003297// Return false if update struct is of valid type, otherwise flag error and return code from callback
3298static bool validUpdateStruct(layer_data *my_data, const VkDevice device, const GENERIC_HEADER *pUpdateStruct) {
Tobin Ehlis5b5e7bc2016-03-09 16:12:48 -07003299 switch (pUpdateStruct->sType) {
3300 case VK_STRUCTURE_TYPE_WRITE_DESCRIPTOR_SET:
3301 case VK_STRUCTURE_TYPE_COPY_DESCRIPTOR_SET:
Dustin Gravese3319182016-04-05 09:41:17 -06003302 return false;
Tobin Ehlis5b5e7bc2016-03-09 16:12:48 -07003303 default:
3304 return log_msg(my_data->report_data, VK_DEBUG_REPORT_ERROR_BIT_EXT, (VkDebugReportObjectTypeEXT)0, 0, __LINE__,
3305 DRAWSTATE_INVALID_UPDATE_STRUCT, "DS",
3306 "Unexpected UPDATE struct of type %s (value %u) in vkUpdateDescriptors() struct tree",
3307 string_VkStructureType(pUpdateStruct->sType), pUpdateStruct->sType);
3308 }
3309}
3310
3311// Set count for given update struct in the last parameter
Tobin Ehlis5b5e7bc2016-03-09 16:12:48 -07003312static uint32_t getUpdateCount(layer_data *my_data, const VkDevice device, const GENERIC_HEADER *pUpdateStruct) {
3313 switch (pUpdateStruct->sType) {
3314 case VK_STRUCTURE_TYPE_WRITE_DESCRIPTOR_SET:
3315 return ((VkWriteDescriptorSet *)pUpdateStruct)->descriptorCount;
3316 case VK_STRUCTURE_TYPE_COPY_DESCRIPTOR_SET:
3317 // TODO : Need to understand this case better and make sure code is correct
3318 return ((VkCopyDescriptorSet *)pUpdateStruct)->descriptorCount;
3319 default:
3320 return 0;
3321 }
Tobin Ehlis5b5e7bc2016-03-09 16:12:48 -07003322}
3323
Tobin Ehlis5b5e7bc2016-03-09 16:12:48 -07003324// For given layout and update, return the first overall index of the layout that is updated
3325static uint32_t getUpdateStartIndex(layer_data *my_data, const VkDevice device, const LAYOUT_NODE *pLayout, const uint32_t binding,
3326 const uint32_t arrayIndex, const GENERIC_HEADER *pUpdateStruct) {
3327 return getBindingStartIndex(pLayout, binding) + arrayIndex;
3328}
3329
3330// For given layout and update, return the last overall index of the layout that is updated
3331static uint32_t getUpdateEndIndex(layer_data *my_data, const VkDevice device, const LAYOUT_NODE *pLayout, const uint32_t binding,
3332 const uint32_t arrayIndex, const GENERIC_HEADER *pUpdateStruct) {
3333 uint32_t count = getUpdateCount(my_data, device, pUpdateStruct);
3334 return getBindingStartIndex(pLayout, binding) + arrayIndex + count - 1;
3335}
3336
3337// Verify that the descriptor type in the update struct matches what's expected by the layout
Dustin Gravese3319182016-04-05 09:41:17 -06003338static bool validateUpdateConsistency(layer_data *my_data, const VkDevice device, const LAYOUT_NODE *pLayout,
3339 const GENERIC_HEADER *pUpdateStruct, uint32_t startIndex, uint32_t endIndex) {
Tobin Ehlis5b5e7bc2016-03-09 16:12:48 -07003340 // First get actual type of update
Dustin Gravese3319182016-04-05 09:41:17 -06003341 bool skipCall = false;
Jamie Madill1d5109d2016-04-04 15:09:51 -04003342 VkDescriptorType actualType = VK_DESCRIPTOR_TYPE_MAX_ENUM;
Tobin Ehlis5b5e7bc2016-03-09 16:12:48 -07003343 uint32_t i = 0;
3344 switch (pUpdateStruct->sType) {
3345 case VK_STRUCTURE_TYPE_WRITE_DESCRIPTOR_SET:
3346 actualType = ((VkWriteDescriptorSet *)pUpdateStruct)->descriptorType;
3347 break;
3348 case VK_STRUCTURE_TYPE_COPY_DESCRIPTOR_SET:
3349 /* no need to validate */
Dustin Gravese3319182016-04-05 09:41:17 -06003350 return false;
Tobin Ehlis5b5e7bc2016-03-09 16:12:48 -07003351 break;
3352 default:
3353 skipCall |= log_msg(my_data->report_data, VK_DEBUG_REPORT_ERROR_BIT_EXT, (VkDebugReportObjectTypeEXT)0, 0, __LINE__,
3354 DRAWSTATE_INVALID_UPDATE_STRUCT, "DS",
3355 "Unexpected UPDATE struct of type %s (value %u) in vkUpdateDescriptors() struct tree",
3356 string_VkStructureType(pUpdateStruct->sType), pUpdateStruct->sType);
3357 }
Dustin Gravese3319182016-04-05 09:41:17 -06003358 if (!skipCall) {
Tobin Ehlis5b5e7bc2016-03-09 16:12:48 -07003359 // Set first stageFlags as reference and verify that all other updates match it
3360 VkShaderStageFlags refStageFlags = pLayout->stageFlags[startIndex];
3361 for (i = startIndex; i <= endIndex; i++) {
3362 if (pLayout->descriptorTypes[i] != actualType) {
3363 skipCall |= log_msg(
3364 my_data->report_data, VK_DEBUG_REPORT_ERROR_BIT_EXT, (VkDebugReportObjectTypeEXT)0, 0, __LINE__,
3365 DRAWSTATE_DESCRIPTOR_TYPE_MISMATCH, "DS",
3366 "Write descriptor update has descriptor type %s that does not match overlapping binding descriptor type of %s!",
3367 string_VkDescriptorType(actualType), string_VkDescriptorType(pLayout->descriptorTypes[i]));
3368 }
3369 if (pLayout->stageFlags[i] != refStageFlags) {
3370 skipCall |= log_msg(
3371 my_data->report_data, VK_DEBUG_REPORT_ERROR_BIT_EXT, (VkDebugReportObjectTypeEXT)0, 0, __LINE__,
3372 DRAWSTATE_DESCRIPTOR_STAGEFLAGS_MISMATCH, "DS",
3373 "Write descriptor update has stageFlags %x that do not match overlapping binding descriptor stageFlags of %x!",
3374 refStageFlags, pLayout->stageFlags[i]);
3375 }
3376 }
3377 }
3378 return skipCall;
3379}
3380
3381// Determine the update type, allocate a new struct of that type, shadow the given pUpdate
Dustin Gravese3319182016-04-05 09:41:17 -06003382// struct into the pNewNode param. Return true if error condition encountered and callback signals early exit.
Tobin Ehlis5b5e7bc2016-03-09 16:12:48 -07003383// NOTE : Calls to this function should be wrapped in mutex
Dustin Gravese3319182016-04-05 09:41:17 -06003384static bool shadowUpdateNode(layer_data *my_data, const VkDevice device, GENERIC_HEADER *pUpdate, GENERIC_HEADER **pNewNode) {
3385 bool skipCall = false;
Tobin Ehlis5b5e7bc2016-03-09 16:12:48 -07003386 VkWriteDescriptorSet *pWDS = NULL;
3387 VkCopyDescriptorSet *pCDS = NULL;
3388 switch (pUpdate->sType) {
3389 case VK_STRUCTURE_TYPE_WRITE_DESCRIPTOR_SET:
3390 pWDS = new VkWriteDescriptorSet;
3391 *pNewNode = (GENERIC_HEADER *)pWDS;
3392 memcpy(pWDS, pUpdate, sizeof(VkWriteDescriptorSet));
3393
3394 switch (pWDS->descriptorType) {
3395 case VK_DESCRIPTOR_TYPE_SAMPLER:
3396 case VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER:
3397 case VK_DESCRIPTOR_TYPE_SAMPLED_IMAGE:
3398 case VK_DESCRIPTOR_TYPE_STORAGE_IMAGE: {
3399 VkDescriptorImageInfo *info = new VkDescriptorImageInfo[pWDS->descriptorCount];
3400 memcpy(info, pWDS->pImageInfo, pWDS->descriptorCount * sizeof(VkDescriptorImageInfo));
3401 pWDS->pImageInfo = info;
3402 } break;
3403 case VK_DESCRIPTOR_TYPE_UNIFORM_TEXEL_BUFFER:
3404 case VK_DESCRIPTOR_TYPE_STORAGE_TEXEL_BUFFER: {
3405 VkBufferView *info = new VkBufferView[pWDS->descriptorCount];
3406 memcpy(info, pWDS->pTexelBufferView, pWDS->descriptorCount * sizeof(VkBufferView));
3407 pWDS->pTexelBufferView = info;
3408 } break;
3409 case VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER:
3410 case VK_DESCRIPTOR_TYPE_STORAGE_BUFFER:
3411 case VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER_DYNAMIC:
3412 case VK_DESCRIPTOR_TYPE_STORAGE_BUFFER_DYNAMIC: {
3413 VkDescriptorBufferInfo *info = new VkDescriptorBufferInfo[pWDS->descriptorCount];
3414 memcpy(info, pWDS->pBufferInfo, pWDS->descriptorCount * sizeof(VkDescriptorBufferInfo));
3415 pWDS->pBufferInfo = info;
3416 } break;
3417 default:
Dustin Gravese3319182016-04-05 09:41:17 -06003418 return true;
Tobin Ehlis5b5e7bc2016-03-09 16:12:48 -07003419 break;
3420 }
3421 break;
3422 case VK_STRUCTURE_TYPE_COPY_DESCRIPTOR_SET:
3423 pCDS = new VkCopyDescriptorSet;
3424 *pNewNode = (GENERIC_HEADER *)pCDS;
3425 memcpy(pCDS, pUpdate, sizeof(VkCopyDescriptorSet));
3426 break;
3427 default:
3428 if (log_msg(my_data->report_data, VK_DEBUG_REPORT_ERROR_BIT_EXT, (VkDebugReportObjectTypeEXT)0, 0, __LINE__,
3429 DRAWSTATE_INVALID_UPDATE_STRUCT, "DS",
3430 "Unexpected UPDATE struct of type %s (value %u) in vkUpdateDescriptors() struct tree",
3431 string_VkStructureType(pUpdate->sType), pUpdate->sType))
Dustin Gravese3319182016-04-05 09:41:17 -06003432 return true;
Tobin Ehlis5b5e7bc2016-03-09 16:12:48 -07003433 }
3434 // Make sure that pNext for the end of shadow copy is NULL
3435 (*pNewNode)->pNext = NULL;
3436 return skipCall;
3437}
3438
3439// Verify that given sampler is valid
Dustin Gravese3319182016-04-05 09:41:17 -06003440static bool validateSampler(const layer_data *my_data, const VkSampler *pSampler, const bool immutable) {
3441 bool skipCall = false;
Tobin Ehlis5b5e7bc2016-03-09 16:12:48 -07003442 auto sampIt = my_data->sampleMap.find(*pSampler);
3443 if (sampIt == my_data->sampleMap.end()) {
3444 if (!immutable) {
3445 skipCall |= log_msg(my_data->report_data, VK_DEBUG_REPORT_ERROR_BIT_EXT, VK_DEBUG_REPORT_OBJECT_TYPE_SAMPLER_EXT,
3446 (uint64_t)*pSampler, __LINE__, DRAWSTATE_SAMPLER_DESCRIPTOR_ERROR, "DS",
3447 "vkUpdateDescriptorSets: Attempt to update descriptor with invalid sampler %#" PRIxLEAST64,
3448 (uint64_t)*pSampler);
3449 } else { // immutable
3450 skipCall |= log_msg(my_data->report_data, VK_DEBUG_REPORT_ERROR_BIT_EXT, VK_DEBUG_REPORT_OBJECT_TYPE_SAMPLER_EXT,
3451 (uint64_t)*pSampler, __LINE__, DRAWSTATE_SAMPLER_DESCRIPTOR_ERROR, "DS",
3452 "vkUpdateDescriptorSets: Attempt to update descriptor whose binding has an invalid immutable "
3453 "sampler %#" PRIxLEAST64,
3454 (uint64_t)*pSampler);
3455 }
3456 } else {
3457 // TODO : Any further checks we want to do on the sampler?
3458 }
3459 return skipCall;
3460}
3461
Michael Lentine4a3279b2016-03-30 15:57:52 -05003462//TODO: Consolidate functions
3463bool FindLayout(const GLOBAL_CB_NODE *pCB, ImageSubresourcePair imgpair, IMAGE_CMD_BUF_LAYOUT_NODE &node, const VkImageAspectFlags aspectMask) {
3464 layer_data *my_data = get_my_data_ptr(get_dispatch_key(pCB->commandBuffer), layer_data_map);
3465 if (!(imgpair.subresource.aspectMask & aspectMask)) {
3466 return false;
3467 }
3468 VkImageAspectFlags oldAspectMask = imgpair.subresource.aspectMask;
3469 imgpair.subresource.aspectMask = aspectMask;
Tobin Ehlis5b5e7bc2016-03-09 16:12:48 -07003470 auto imgsubIt = pCB->imageLayoutMap.find(imgpair);
3471 if (imgsubIt == pCB->imageLayoutMap.end()) {
Michael Lentine4a3279b2016-03-30 15:57:52 -05003472 return false;
3473 }
3474 if (node.layout != VK_IMAGE_LAYOUT_MAX_ENUM && node.layout != imgsubIt->second.layout) {
3475 log_msg(my_data->report_data, VK_DEBUG_REPORT_ERROR_BIT_EXT, VK_DEBUG_REPORT_OBJECT_TYPE_IMAGE_EXT,
3476 reinterpret_cast<uint64_t&>(imgpair.image), __LINE__, DRAWSTATE_INVALID_LAYOUT, "DS",
3477 "Cannot query for VkImage 0x%" PRIx64 " layout when combined aspect mask %d has multiple layout types: %s and %s",
3478 reinterpret_cast<uint64_t&>(imgpair.image), oldAspectMask, string_VkImageLayout(node.layout), string_VkImageLayout(imgsubIt->second.layout));
3479 }
3480 if (node.initialLayout != VK_IMAGE_LAYOUT_MAX_ENUM && node.initialLayout != imgsubIt->second.initialLayout) {
3481 log_msg(my_data->report_data, VK_DEBUG_REPORT_ERROR_BIT_EXT, VK_DEBUG_REPORT_OBJECT_TYPE_IMAGE_EXT,
3482 reinterpret_cast<uint64_t&>(imgpair.image), __LINE__, DRAWSTATE_INVALID_LAYOUT, "DS",
3483 "Cannot query for VkImage 0x%" PRIx64 " layout when combined aspect mask %d has multiple initial layout types: %s and %s",
3484 reinterpret_cast<uint64_t&>(imgpair.image), oldAspectMask, string_VkImageLayout(node.initialLayout), string_VkImageLayout(imgsubIt->second.initialLayout));
Tobin Ehlis5b5e7bc2016-03-09 16:12:48 -07003485 }
3486 node = imgsubIt->second;
3487 return true;
3488}
3489
Michael Lentine4a3279b2016-03-30 15:57:52 -05003490bool FindLayout(const layer_data *my_data, ImageSubresourcePair imgpair, VkImageLayout &layout, const VkImageAspectFlags aspectMask) {
3491 if (!(imgpair.subresource.aspectMask & aspectMask)) {
3492 return false;
3493 }
3494 VkImageAspectFlags oldAspectMask = imgpair.subresource.aspectMask;
3495 imgpair.subresource.aspectMask = aspectMask;
Tobin Ehlis5b5e7bc2016-03-09 16:12:48 -07003496 auto imgsubIt = my_data->imageLayoutMap.find(imgpair);
3497 if (imgsubIt == my_data->imageLayoutMap.end()) {
Michael Lentine4a3279b2016-03-30 15:57:52 -05003498 return false;
3499 }
3500 if (layout != VK_IMAGE_LAYOUT_MAX_ENUM && layout != imgsubIt->second.layout) {
3501 log_msg(my_data->report_data, VK_DEBUG_REPORT_ERROR_BIT_EXT, VK_DEBUG_REPORT_OBJECT_TYPE_IMAGE_EXT,
3502 reinterpret_cast<uint64_t&>(imgpair.image), __LINE__, DRAWSTATE_INVALID_LAYOUT, "DS",
3503 "Cannot query for VkImage 0x%" PRIx64 " layout when combined aspect mask %d has multiple layout types: %s and %s",
3504 reinterpret_cast<uint64_t&>(imgpair.image), oldAspectMask, string_VkImageLayout(layout), string_VkImageLayout(imgsubIt->second.layout));
Tobin Ehlis5b5e7bc2016-03-09 16:12:48 -07003505 }
3506 layout = imgsubIt->second.layout;
3507 return true;
3508}
3509
Michael Lentine4a3279b2016-03-30 15:57:52 -05003510// find layout(s) on the cmd buf level
3511bool FindLayout(const GLOBAL_CB_NODE *pCB, VkImage image, VkImageSubresource range, IMAGE_CMD_BUF_LAYOUT_NODE &node) {
3512 ImageSubresourcePair imgpair = {image, true, range};
3513 node = IMAGE_CMD_BUF_LAYOUT_NODE(VK_IMAGE_LAYOUT_MAX_ENUM, VK_IMAGE_LAYOUT_MAX_ENUM);
3514 FindLayout(pCB, imgpair, node, VK_IMAGE_ASPECT_COLOR_BIT);
3515 FindLayout(pCB, imgpair, node, VK_IMAGE_ASPECT_DEPTH_BIT);
3516 FindLayout(pCB, imgpair, node, VK_IMAGE_ASPECT_STENCIL_BIT);
3517 FindLayout(pCB, imgpair, node, VK_IMAGE_ASPECT_METADATA_BIT);
3518 if (node.layout == VK_IMAGE_LAYOUT_MAX_ENUM) {
3519 imgpair = {image, false, VkImageSubresource()};
3520 auto imgsubIt = pCB->imageLayoutMap.find(imgpair);
3521 if (imgsubIt == pCB->imageLayoutMap.end())
3522 return false;
3523 node = imgsubIt->second;
3524 }
3525 return true;
3526}
3527
3528// find layout(s) on the global level
3529bool FindLayout(const layer_data *my_data, ImageSubresourcePair imgpair, VkImageLayout &layout) {
3530 layout = VK_IMAGE_LAYOUT_MAX_ENUM;
3531 FindLayout(my_data, imgpair, layout, VK_IMAGE_ASPECT_COLOR_BIT);
3532 FindLayout(my_data, imgpair, layout, VK_IMAGE_ASPECT_DEPTH_BIT);
3533 FindLayout(my_data, imgpair, layout, VK_IMAGE_ASPECT_STENCIL_BIT);
3534 FindLayout(my_data, imgpair, layout, VK_IMAGE_ASPECT_METADATA_BIT);
3535 if (layout == VK_IMAGE_LAYOUT_MAX_ENUM) {
3536 imgpair = {imgpair.image, false, VkImageSubresource()};
3537 auto imgsubIt = my_data->imageLayoutMap.find(imgpair);
3538 if (imgsubIt == my_data->imageLayoutMap.end())
3539 return false;
3540 layout = imgsubIt->second.layout;
3541 }
3542 return true;
3543}
3544
Tobin Ehlis5b5e7bc2016-03-09 16:12:48 -07003545bool FindLayout(const layer_data *my_data, VkImage image, VkImageSubresource range, VkImageLayout &layout) {
3546 ImageSubresourcePair imgpair = {image, true, range};
3547 return FindLayout(my_data, imgpair, layout);
3548}
3549
3550bool FindLayouts(const layer_data *my_data, VkImage image, std::vector<VkImageLayout> &layouts) {
3551 auto sub_data = my_data->imageSubresourceMap.find(image);
3552 if (sub_data == my_data->imageSubresourceMap.end())
3553 return false;
3554 auto imgIt = my_data->imageMap.find(image);
3555 if (imgIt == my_data->imageMap.end())
3556 return false;
3557 bool ignoreGlobal = false;
3558 // TODO: Make this robust for >1 aspect mask. Now it will just say ignore
3559 // potential errors in this case.
3560 if (sub_data->second.size() >= (imgIt->second.createInfo.arrayLayers * imgIt->second.createInfo.mipLevels + 1)) {
3561 ignoreGlobal = true;
3562 }
3563 for (auto imgsubpair : sub_data->second) {
3564 if (ignoreGlobal && !imgsubpair.hasSubresource)
3565 continue;
3566 auto img_data = my_data->imageLayoutMap.find(imgsubpair);
3567 if (img_data != my_data->imageLayoutMap.end()) {
3568 layouts.push_back(img_data->second.layout);
3569 }
3570 }
3571 return true;
3572}
3573
3574// Set the layout on the global level
3575void SetLayout(layer_data *my_data, ImageSubresourcePair imgpair, const VkImageLayout &layout) {
3576 VkImage &image = imgpair.image;
3577 // TODO (mlentine): Maybe set format if new? Not used atm.
3578 my_data->imageLayoutMap[imgpair].layout = layout;
3579 // TODO (mlentine): Maybe make vector a set?
3580 auto subresource = std::find(my_data->imageSubresourceMap[image].begin(), my_data->imageSubresourceMap[image].end(), imgpair);
3581 if (subresource == my_data->imageSubresourceMap[image].end()) {
3582 my_data->imageSubresourceMap[image].push_back(imgpair);
3583 }
3584}
3585
Tobin Ehlis5b5e7bc2016-03-09 16:12:48 -07003586// Set the layout on the cmdbuf level
Michael Lentine08682cd2016-03-24 15:36:27 -05003587void SetLayout(GLOBAL_CB_NODE *pCB, ImageSubresourcePair imgpair, const IMAGE_CMD_BUF_LAYOUT_NODE &node) {
Tobin Ehlis5b5e7bc2016-03-09 16:12:48 -07003588 pCB->imageLayoutMap[imgpair] = node;
3589 // TODO (mlentine): Maybe make vector a set?
Michael Lentine08682cd2016-03-24 15:36:27 -05003590 auto subresource =
3591 std::find(pCB->imageSubresourceMap[imgpair.image].begin(), pCB->imageSubresourceMap[imgpair.image].end(), imgpair);
3592 if (subresource == pCB->imageSubresourceMap[imgpair.image].end()) {
3593 pCB->imageSubresourceMap[imgpair.image].push_back(imgpair);
Tobin Ehlis5b5e7bc2016-03-09 16:12:48 -07003594 }
3595}
3596
Michael Lentine08682cd2016-03-24 15:36:27 -05003597void SetLayout(GLOBAL_CB_NODE *pCB, ImageSubresourcePair imgpair, const VkImageLayout &layout) {
Tobin Ehlis5b5e7bc2016-03-09 16:12:48 -07003598 // TODO (mlentine): Maybe make vector a set?
Michael Lentine08682cd2016-03-24 15:36:27 -05003599 if (std::find(pCB->imageSubresourceMap[imgpair.image].begin(), pCB->imageSubresourceMap[imgpair.image].end(), imgpair) !=
3600 pCB->imageSubresourceMap[imgpair.image].end()) {
Tobin Ehlis5b5e7bc2016-03-09 16:12:48 -07003601 pCB->imageLayoutMap[imgpair].layout = layout;
3602 } else {
3603 // TODO (mlentine): Could be expensive and might need to be removed.
3604 assert(imgpair.hasSubresource);
3605 IMAGE_CMD_BUF_LAYOUT_NODE node;
Mark Lobodzinski7068a422016-03-28 14:34:40 -06003606 if (!FindLayout(pCB, imgpair.image, imgpair.subresource, node)) {
3607 node.initialLayout = layout;
3608 }
Michael Lentine08682cd2016-03-24 15:36:27 -05003609 SetLayout(pCB, imgpair, {node.initialLayout, layout});
Tobin Ehlis5b5e7bc2016-03-09 16:12:48 -07003610 }
3611}
3612
Michael Lentine08682cd2016-03-24 15:36:27 -05003613template <class OBJECT, class LAYOUT>
3614void SetLayout(OBJECT *pObject, ImageSubresourcePair imgpair, const LAYOUT &layout, VkImageAspectFlags aspectMask) {
3615 if (imgpair.subresource.aspectMask & aspectMask) {
3616 imgpair.subresource.aspectMask = aspectMask;
3617 SetLayout(pObject, imgpair, layout);
3618 }
Tobin Ehlis5b5e7bc2016-03-09 16:12:48 -07003619}
3620
Michael Lentine08682cd2016-03-24 15:36:27 -05003621template <class OBJECT, class LAYOUT>
3622void SetLayout(OBJECT *pObject, VkImage image, VkImageSubresource range, const LAYOUT &layout) {
Tobin Ehlis5b5e7bc2016-03-09 16:12:48 -07003623 ImageSubresourcePair imgpair = {image, true, range};
Michael Lentine08682cd2016-03-24 15:36:27 -05003624 SetLayout(pObject, imgpair, layout, VK_IMAGE_ASPECT_COLOR_BIT);
3625 SetLayout(pObject, imgpair, layout, VK_IMAGE_ASPECT_DEPTH_BIT);
3626 SetLayout(pObject, imgpair, layout, VK_IMAGE_ASPECT_STENCIL_BIT);
3627 SetLayout(pObject, imgpair, layout, VK_IMAGE_ASPECT_METADATA_BIT);
Tobin Ehlis5b5e7bc2016-03-09 16:12:48 -07003628}
3629
Michael Lentine08682cd2016-03-24 15:36:27 -05003630template <class OBJECT, class LAYOUT> void SetLayout(OBJECT *pObject, VkImage image, const LAYOUT &layout) {
Tobin Ehlis5b5e7bc2016-03-09 16:12:48 -07003631 ImageSubresourcePair imgpair = {image, false, VkImageSubresource()};
Michael Lentine08682cd2016-03-24 15:36:27 -05003632 SetLayout(pObject, image, imgpair, layout);
Tobin Ehlis5b5e7bc2016-03-09 16:12:48 -07003633}
3634
3635void SetLayout(const layer_data *dev_data, GLOBAL_CB_NODE *pCB, VkImageView imageView, const VkImageLayout &layout) {
3636 auto image_view_data = dev_data->imageViewMap.find(imageView);
3637 assert(image_view_data != dev_data->imageViewMap.end());
3638 const VkImage &image = image_view_data->second.image;
3639 const VkImageSubresourceRange &subRange = image_view_data->second.subresourceRange;
3640 // TODO: Do not iterate over every possibility - consolidate where possible
3641 for (uint32_t j = 0; j < subRange.levelCount; j++) {
3642 uint32_t level = subRange.baseMipLevel + j;
3643 for (uint32_t k = 0; k < subRange.layerCount; k++) {
3644 uint32_t layer = subRange.baseArrayLayer + k;
3645 VkImageSubresource sub = {subRange.aspectMask, level, layer};
3646 SetLayout(pCB, image, sub, layout);
3647 }
3648 }
3649}
3650
3651// Verify that given imageView is valid
Dustin Gravese3319182016-04-05 09:41:17 -06003652static bool validateImageView(const layer_data *my_data, const VkImageView *pImageView, const VkImageLayout imageLayout) {
3653 bool skipCall = false;
Tobin Ehlis5b5e7bc2016-03-09 16:12:48 -07003654 auto ivIt = my_data->imageViewMap.find(*pImageView);
3655 if (ivIt == my_data->imageViewMap.end()) {
3656 skipCall |= log_msg(my_data->report_data, VK_DEBUG_REPORT_ERROR_BIT_EXT, VK_DEBUG_REPORT_OBJECT_TYPE_IMAGE_VIEW_EXT,
3657 (uint64_t)*pImageView, __LINE__, DRAWSTATE_IMAGEVIEW_DESCRIPTOR_ERROR, "DS",
3658 "vkUpdateDescriptorSets: Attempt to update descriptor with invalid imageView %#" PRIxLEAST64,
3659 (uint64_t)*pImageView);
3660 } else {
3661 // Validate that imageLayout is compatible with aspectMask and image format
3662 VkImageAspectFlags aspectMask = ivIt->second.subresourceRange.aspectMask;
3663 VkImage image = ivIt->second.image;
3664 // TODO : Check here in case we have a bad image
3665 VkFormat format = VK_FORMAT_MAX_ENUM;
3666 auto imgIt = my_data->imageMap.find(image);
3667 if (imgIt != my_data->imageMap.end()) {
3668 format = (*imgIt).second.createInfo.format;
3669 } else {
3670 // Also need to check the swapchains.
3671 auto swapchainIt = my_data->device_extensions.imageToSwapchainMap.find(image);
3672 if (swapchainIt != my_data->device_extensions.imageToSwapchainMap.end()) {
3673 VkSwapchainKHR swapchain = swapchainIt->second;
3674 auto swapchain_nodeIt = my_data->device_extensions.swapchainMap.find(swapchain);
3675 if (swapchain_nodeIt != my_data->device_extensions.swapchainMap.end()) {
3676 SWAPCHAIN_NODE *pswapchain_node = swapchain_nodeIt->second;
3677 format = pswapchain_node->createInfo.imageFormat;
3678 }
3679 }
3680 }
3681 if (format == VK_FORMAT_MAX_ENUM) {
3682 skipCall |= log_msg(my_data->report_data, VK_DEBUG_REPORT_ERROR_BIT_EXT, VK_DEBUG_REPORT_OBJECT_TYPE_IMAGE_EXT,
3683 (uint64_t)image, __LINE__, DRAWSTATE_IMAGEVIEW_DESCRIPTOR_ERROR, "DS",
3684 "vkUpdateDescriptorSets: Attempt to update descriptor with invalid image %#" PRIxLEAST64
3685 " in imageView %#" PRIxLEAST64,
3686 (uint64_t)image, (uint64_t)*pImageView);
3687 } else {
Dustin Gravese3319182016-04-05 09:41:17 -06003688 bool ds = vk_format_is_depth_or_stencil(format);
Tobin Ehlis5b5e7bc2016-03-09 16:12:48 -07003689 switch (imageLayout) {
3690 case VK_IMAGE_LAYOUT_COLOR_ATTACHMENT_OPTIMAL:
3691 // Only Color bit must be set
3692 if ((aspectMask & VK_IMAGE_ASPECT_COLOR_BIT) != VK_IMAGE_ASPECT_COLOR_BIT) {
3693 skipCall |=
3694 log_msg(my_data->report_data, VK_DEBUG_REPORT_ERROR_BIT_EXT, VK_DEBUG_REPORT_OBJECT_TYPE_IMAGE_VIEW_EXT,
3695 (uint64_t)*pImageView, __LINE__, DRAWSTATE_INVALID_IMAGE_ASPECT, "DS",
3696 "vkUpdateDescriptorSets: Updating descriptor with layout VK_IMAGE_LAYOUT_COLOR_ATTACHMENT_OPTIMAL "
3697 "and imageView %#" PRIxLEAST64 ""
3698 " that does not have VK_IMAGE_ASPECT_COLOR_BIT set.",
3699 (uint64_t)*pImageView);
3700 }
3701 // format must NOT be DS
3702 if (ds) {
3703 skipCall |=
3704 log_msg(my_data->report_data, VK_DEBUG_REPORT_ERROR_BIT_EXT, VK_DEBUG_REPORT_OBJECT_TYPE_IMAGE_VIEW_EXT,
3705 (uint64_t)*pImageView, __LINE__, DRAWSTATE_IMAGEVIEW_DESCRIPTOR_ERROR, "DS",
3706 "vkUpdateDescriptorSets: Updating descriptor with layout VK_IMAGE_LAYOUT_COLOR_ATTACHMENT_OPTIMAL "
3707 "and imageView %#" PRIxLEAST64 ""
3708 " but the image format is %s which is not a color format.",
3709 (uint64_t)*pImageView, string_VkFormat(format));
3710 }
3711 break;
3712 case VK_IMAGE_LAYOUT_DEPTH_STENCIL_ATTACHMENT_OPTIMAL:
3713 case VK_IMAGE_LAYOUT_DEPTH_STENCIL_READ_ONLY_OPTIMAL:
3714 // Depth or stencil bit must be set, but both must NOT be set
3715 if (aspectMask & VK_IMAGE_ASPECT_DEPTH_BIT) {
3716 if (aspectMask & VK_IMAGE_ASPECT_STENCIL_BIT) {
3717 // both must NOT be set
3718 skipCall |=
3719 log_msg(my_data->report_data, VK_DEBUG_REPORT_ERROR_BIT_EXT, VK_DEBUG_REPORT_OBJECT_TYPE_IMAGE_VIEW_EXT,
3720 (uint64_t)*pImageView, __LINE__, DRAWSTATE_INVALID_IMAGE_ASPECT, "DS",
3721 "vkUpdateDescriptorSets: Updating descriptor with imageView %#" PRIxLEAST64 ""
3722 " that has both STENCIL and DEPTH aspects set",
3723 (uint64_t)*pImageView);
3724 }
3725 } else if (!(aspectMask & VK_IMAGE_ASPECT_STENCIL_BIT)) {
3726 // Neither were set
3727 skipCall |=
3728 log_msg(my_data->report_data, VK_DEBUG_REPORT_ERROR_BIT_EXT, VK_DEBUG_REPORT_OBJECT_TYPE_IMAGE_VIEW_EXT,
3729 (uint64_t)*pImageView, __LINE__, DRAWSTATE_INVALID_IMAGE_ASPECT, "DS",
3730 "vkUpdateDescriptorSets: Updating descriptor with layout %s and imageView %#" PRIxLEAST64 ""
3731 " that does not have STENCIL or DEPTH aspect set.",
3732 string_VkImageLayout(imageLayout), (uint64_t)*pImageView);
3733 }
3734 // format must be DS
3735 if (!ds) {
3736 skipCall |=
3737 log_msg(my_data->report_data, VK_DEBUG_REPORT_ERROR_BIT_EXT, VK_DEBUG_REPORT_OBJECT_TYPE_IMAGE_VIEW_EXT,
3738 (uint64_t)*pImageView, __LINE__, DRAWSTATE_IMAGEVIEW_DESCRIPTOR_ERROR, "DS",
3739 "vkUpdateDescriptorSets: Updating descriptor with layout %s and imageView %#" PRIxLEAST64 ""
3740 " but the image format is %s which is not a depth/stencil format.",
3741 string_VkImageLayout(imageLayout), (uint64_t)*pImageView, string_VkFormat(format));
3742 }
3743 break;
3744 default:
3745 // anything to check for other layouts?
3746 break;
3747 }
3748 }
3749 }
3750 return skipCall;
3751}
3752
3753// Verify that given bufferView is valid
Dustin Gravese3319182016-04-05 09:41:17 -06003754static bool validateBufferView(const layer_data *my_data, const VkBufferView *pBufferView) {
3755 bool skipCall = false;
Tobin Ehlis5b5e7bc2016-03-09 16:12:48 -07003756 auto sampIt = my_data->bufferViewMap.find(*pBufferView);
3757 if (sampIt == my_data->bufferViewMap.end()) {
3758 skipCall |= log_msg(my_data->report_data, VK_DEBUG_REPORT_ERROR_BIT_EXT, VK_DEBUG_REPORT_OBJECT_TYPE_BUFFER_VIEW_EXT,
3759 (uint64_t)*pBufferView, __LINE__, DRAWSTATE_BUFFERVIEW_DESCRIPTOR_ERROR, "DS",
3760 "vkUpdateDescriptorSets: Attempt to update descriptor with invalid bufferView %#" PRIxLEAST64,
3761 (uint64_t)*pBufferView);
3762 } else {
3763 // TODO : Any further checks we want to do on the bufferView?
3764 }
3765 return skipCall;
3766}
3767
3768// Verify that given bufferInfo is valid
Dustin Gravese3319182016-04-05 09:41:17 -06003769static bool validateBufferInfo(const layer_data *my_data, const VkDescriptorBufferInfo *pBufferInfo) {
3770 bool skipCall = false;
Tobin Ehlis5b5e7bc2016-03-09 16:12:48 -07003771 auto sampIt = my_data->bufferMap.find(pBufferInfo->buffer);
3772 if (sampIt == my_data->bufferMap.end()) {
3773 skipCall |=
3774 log_msg(my_data->report_data, VK_DEBUG_REPORT_ERROR_BIT_EXT, VK_DEBUG_REPORT_OBJECT_TYPE_BUFFER_EXT,
3775 (uint64_t)pBufferInfo->buffer, __LINE__, DRAWSTATE_BUFFERINFO_DESCRIPTOR_ERROR, "DS",
3776 "vkUpdateDescriptorSets: Attempt to update descriptor where bufferInfo has invalid buffer %#" PRIxLEAST64,
3777 (uint64_t)pBufferInfo->buffer);
3778 } else {
3779 // TODO : Any further checks we want to do on the bufferView?
3780 }
3781 return skipCall;
3782}
3783
Dustin Gravese3319182016-04-05 09:41:17 -06003784static bool validateUpdateContents(const layer_data *my_data, const VkWriteDescriptorSet *pWDS,
3785 const VkDescriptorSetLayoutBinding *pLayoutBinding) {
3786 bool skipCall = false;
Tobin Ehlis5b5e7bc2016-03-09 16:12:48 -07003787 // First verify that for the given Descriptor type, the correct DescriptorInfo data is supplied
3788 const VkSampler *pSampler = NULL;
Dustin Gravese3319182016-04-05 09:41:17 -06003789 bool immutable = false;
Tobin Ehlis5b5e7bc2016-03-09 16:12:48 -07003790 uint32_t i = 0;
3791 // For given update type, verify that update contents are correct
3792 switch (pWDS->descriptorType) {
3793 case VK_DESCRIPTOR_TYPE_SAMPLER:
3794 for (i = 0; i < pWDS->descriptorCount; ++i) {
3795 skipCall |= validateSampler(my_data, &(pWDS->pImageInfo[i].sampler), immutable);
3796 }
3797 break;
3798 case VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER:
3799 for (i = 0; i < pWDS->descriptorCount; ++i) {
3800 if (NULL == pLayoutBinding->pImmutableSamplers) {
3801 pSampler = &(pWDS->pImageInfo[i].sampler);
3802 if (immutable) {
3803 skipCall |= log_msg(
3804 my_data->report_data, VK_DEBUG_REPORT_ERROR_BIT_EXT, VK_DEBUG_REPORT_OBJECT_TYPE_SAMPLER_EXT,
3805 (uint64_t)*pSampler, __LINE__, DRAWSTATE_INCONSISTENT_IMMUTABLE_SAMPLER_UPDATE, "DS",
3806 "vkUpdateDescriptorSets: Update #%u is not an immutable sampler %#" PRIxLEAST64
3807 ", but previous update(s) from this "
3808 "VkWriteDescriptorSet struct used an immutable sampler. All updates from a single struct must either "
3809 "use immutable or non-immutable samplers.",
3810 i, (uint64_t)*pSampler);
3811 }
3812 } else {
3813 if (i > 0 && !immutable) {
3814 skipCall |= log_msg(
3815 my_data->report_data, VK_DEBUG_REPORT_ERROR_BIT_EXT, VK_DEBUG_REPORT_OBJECT_TYPE_SAMPLER_EXT,
3816 (uint64_t)*pSampler, __LINE__, DRAWSTATE_INCONSISTENT_IMMUTABLE_SAMPLER_UPDATE, "DS",
3817 "vkUpdateDescriptorSets: Update #%u is an immutable sampler, but previous update(s) from this "
3818 "VkWriteDescriptorSet struct used a non-immutable sampler. All updates from a single struct must either "
3819 "use immutable or non-immutable samplers.",
3820 i);
3821 }
Dustin Gravese3319182016-04-05 09:41:17 -06003822 immutable = true;
Tobin Ehlis5b5e7bc2016-03-09 16:12:48 -07003823 pSampler = &(pLayoutBinding->pImmutableSamplers[i]);
3824 }
3825 skipCall |= validateSampler(my_data, pSampler, immutable);
3826 }
3827 // Intentionally fall through here to also validate image stuff
3828 case VK_DESCRIPTOR_TYPE_SAMPLED_IMAGE:
3829 case VK_DESCRIPTOR_TYPE_STORAGE_IMAGE:
3830 case VK_DESCRIPTOR_TYPE_INPUT_ATTACHMENT:
3831 for (i = 0; i < pWDS->descriptorCount; ++i) {
3832 skipCall |= validateImageView(my_data, &(pWDS->pImageInfo[i].imageView), pWDS->pImageInfo[i].imageLayout);
3833 }
3834 break;
3835 case VK_DESCRIPTOR_TYPE_UNIFORM_TEXEL_BUFFER:
3836 case VK_DESCRIPTOR_TYPE_STORAGE_TEXEL_BUFFER:
3837 for (i = 0; i < pWDS->descriptorCount; ++i) {
3838 skipCall |= validateBufferView(my_data, &(pWDS->pTexelBufferView[i]));
3839 }
3840 break;
3841 case VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER:
3842 case VK_DESCRIPTOR_TYPE_STORAGE_BUFFER:
3843 case VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER_DYNAMIC:
3844 case VK_DESCRIPTOR_TYPE_STORAGE_BUFFER_DYNAMIC:
3845 for (i = 0; i < pWDS->descriptorCount; ++i) {
3846 skipCall |= validateBufferInfo(my_data, &(pWDS->pBufferInfo[i]));
3847 }
3848 break;
3849 default:
3850 break;
3851 }
3852 return skipCall;
3853}
3854// Validate that given set is valid and that it's not being used by an in-flight CmdBuffer
3855// func_str is the name of the calling function
Dustin Gravese3319182016-04-05 09:41:17 -06003856// Return false if no errors occur
3857// Return true if validation error occurs and callback returns true (to skip upcoming API call down the chain)
3858static bool validateIdleDescriptorSet(const layer_data *my_data, VkDescriptorSet set, std::string func_str) {
3859 bool skip_call = false;
Tobin Ehlis5b5e7bc2016-03-09 16:12:48 -07003860 auto set_node = my_data->setMap.find(set);
3861 if (set_node == my_data->setMap.end()) {
3862 skip_call |= log_msg(my_data->report_data, VK_DEBUG_REPORT_ERROR_BIT_EXT, VK_DEBUG_REPORT_OBJECT_TYPE_DESCRIPTOR_SET_EXT,
3863 (uint64_t)(set), __LINE__, DRAWSTATE_DOUBLE_DESTROY, "DS",
3864 "Cannot call %s() on descriptor set %" PRIxLEAST64 " that has not been allocated.", func_str.c_str(),
3865 (uint64_t)(set));
3866 } else {
3867 if (set_node->second->in_use.load()) {
3868 skip_call |= log_msg(my_data->report_data, VK_DEBUG_REPORT_ERROR_BIT_EXT,
3869 VK_DEBUG_REPORT_OBJECT_TYPE_DESCRIPTOR_SET_EXT, (uint64_t)(set), __LINE__, DRAWSTATE_OBJECT_INUSE,
3870 "DS", "Cannot call %s() on descriptor set %" PRIxLEAST64 " that is in use by a command buffer.",
3871 func_str.c_str(), (uint64_t)(set));
3872 }
3873 }
3874 return skip_call;
3875}
3876static void invalidateBoundCmdBuffers(layer_data *dev_data, const SET_NODE *pSet) {
3877 // Flag any CBs this set is bound to as INVALID
3878 for (auto cb : pSet->boundCmdBuffers) {
3879 auto cb_node = dev_data->commandBufferMap.find(cb);
3880 if (cb_node != dev_data->commandBufferMap.end()) {
3881 cb_node->second->state = CB_INVALID;
3882 }
3883 }
3884}
3885// update DS mappings based on write and copy update arrays
Dustin Gravese3319182016-04-05 09:41:17 -06003886static bool dsUpdate(layer_data *my_data, VkDevice device, uint32_t descriptorWriteCount, const VkWriteDescriptorSet *pWDS,
3887 uint32_t descriptorCopyCount, const VkCopyDescriptorSet *pCDS) {
3888 bool skipCall = false;
Tobin Ehlis5b5e7bc2016-03-09 16:12:48 -07003889
3890 LAYOUT_NODE *pLayout = NULL;
3891 VkDescriptorSetLayoutCreateInfo *pLayoutCI = NULL;
3892 // Validate Write updates
3893 uint32_t i = 0;
3894 for (i = 0; i < descriptorWriteCount; i++) {
3895 VkDescriptorSet ds = pWDS[i].dstSet;
3896 SET_NODE *pSet = my_data->setMap[ds];
3897 // Set being updated cannot be in-flight
Dustin Gravese3319182016-04-05 09:41:17 -06003898 if ((skipCall = validateIdleDescriptorSet(my_data, ds, "VkUpdateDescriptorSets")) == true)
Tobin Ehlis5b5e7bc2016-03-09 16:12:48 -07003899 return skipCall;
3900 // If set is bound to any cmdBuffers, mark them invalid
3901 invalidateBoundCmdBuffers(my_data, pSet);
3902 GENERIC_HEADER *pUpdate = (GENERIC_HEADER *)&pWDS[i];
3903 pLayout = pSet->pLayout;
3904 // First verify valid update struct
Dustin Gravese3319182016-04-05 09:41:17 -06003905 if ((skipCall = validUpdateStruct(my_data, device, pUpdate)) == true) {
Tobin Ehlis5b5e7bc2016-03-09 16:12:48 -07003906 break;
3907 }
3908 uint32_t binding = 0, endIndex = 0;
3909 binding = pWDS[i].dstBinding;
3910 auto bindingToIndex = pLayout->bindingToIndexMap.find(binding);
3911 // Make sure that layout being updated has the binding being updated
3912 if (bindingToIndex == pLayout->bindingToIndexMap.end()) {
3913 skipCall |= log_msg(my_data->report_data, VK_DEBUG_REPORT_ERROR_BIT_EXT, VK_DEBUG_REPORT_OBJECT_TYPE_DESCRIPTOR_SET_EXT,
3914 (uint64_t)(ds), __LINE__, DRAWSTATE_INVALID_UPDATE_INDEX, "DS",
3915 "Descriptor Set %" PRIu64 " does not have binding to match "
3916 "update binding %u for update type "
3917 "%s!",
3918 (uint64_t)(ds), binding, string_VkStructureType(pUpdate->sType));
3919 } else {
3920 // Next verify that update falls within size of given binding
3921 endIndex = getUpdateEndIndex(my_data, device, pLayout, binding, pWDS[i].dstArrayElement, pUpdate);
3922 if (getBindingEndIndex(pLayout, binding) < endIndex) {
3923 pLayoutCI = &pLayout->createInfo;
3924 string DSstr = vk_print_vkdescriptorsetlayoutcreateinfo(pLayoutCI, "{DS} ");
3925 skipCall |=
3926 log_msg(my_data->report_data, VK_DEBUG_REPORT_ERROR_BIT_EXT, VK_DEBUG_REPORT_OBJECT_TYPE_DESCRIPTOR_SET_EXT,
3927 (uint64_t)(ds), __LINE__, DRAWSTATE_DESCRIPTOR_UPDATE_OUT_OF_BOUNDS, "DS",
3928 "Descriptor update type of %s is out of bounds for matching binding %u in Layout w/ CI:\n%s!",
3929 string_VkStructureType(pUpdate->sType), binding, DSstr.c_str());
3930 } else { // TODO : should we skip update on a type mismatch or force it?
3931 uint32_t startIndex;
3932 startIndex = getUpdateStartIndex(my_data, device, pLayout, binding, pWDS[i].dstArrayElement, pUpdate);
3933 // Layout bindings match w/ update, now verify that update type
3934 // & stageFlags are the same for entire update
Dustin Gravese3319182016-04-05 09:41:17 -06003935 if ((skipCall = validateUpdateConsistency(my_data, device, pLayout, pUpdate, startIndex, endIndex)) == false) {
Tobin Ehlis5b5e7bc2016-03-09 16:12:48 -07003936 // The update is within bounds and consistent, but need to
3937 // make sure contents make sense as well
3938 if ((skipCall = validateUpdateContents(my_data, &pWDS[i],
Dustin Gravese3319182016-04-05 09:41:17 -06003939 &pLayout->createInfo.pBindings[bindingToIndex->second])) == false) {
Tobin Ehlis5b5e7bc2016-03-09 16:12:48 -07003940 // Update is good. Save the update info
3941 // Create new update struct for this set's shadow copy
3942 GENERIC_HEADER *pNewNode = NULL;
3943 skipCall |= shadowUpdateNode(my_data, device, pUpdate, &pNewNode);
3944 if (NULL == pNewNode) {
3945 skipCall |= log_msg(
3946 my_data->report_data, VK_DEBUG_REPORT_ERROR_BIT_EXT, VK_DEBUG_REPORT_OBJECT_TYPE_DESCRIPTOR_SET_EXT,
3947 (uint64_t)(ds), __LINE__, DRAWSTATE_OUT_OF_MEMORY, "DS",
3948 "Out of memory while attempting to allocate UPDATE struct in vkUpdateDescriptors()");
3949 } else {
3950 // Insert shadow node into LL of updates for this set
3951 pNewNode->pNext = pSet->pUpdateStructs;
3952 pSet->pUpdateStructs = pNewNode;
3953 // Now update appropriate descriptor(s) to point to new Update node
3954 for (uint32_t j = startIndex; j <= endIndex; j++) {
3955 assert(j < pSet->descriptorCount);
Tobin Ehlis3a417a52016-03-24 10:16:09 -06003956 pSet->pDescriptorUpdates[j] = pNewNode;
Tobin Ehlis5b5e7bc2016-03-09 16:12:48 -07003957 }
3958 }
3959 }
3960 }
3961 }
3962 }
3963 }
3964 // Now validate copy updates
3965 for (i = 0; i < descriptorCopyCount; ++i) {
3966 SET_NODE *pSrcSet = NULL, *pDstSet = NULL;
3967 LAYOUT_NODE *pSrcLayout = NULL, *pDstLayout = NULL;
3968 uint32_t srcStartIndex = 0, srcEndIndex = 0, dstStartIndex = 0, dstEndIndex = 0;
3969 // For each copy make sure that update falls within given layout and that types match
3970 pSrcSet = my_data->setMap[pCDS[i].srcSet];
3971 pDstSet = my_data->setMap[pCDS[i].dstSet];
3972 // Set being updated cannot be in-flight
Dustin Gravese3319182016-04-05 09:41:17 -06003973 if ((skipCall = validateIdleDescriptorSet(my_data, pDstSet->set, "VkUpdateDescriptorSets")) == true)
Tobin Ehlis5b5e7bc2016-03-09 16:12:48 -07003974 return skipCall;
3975 invalidateBoundCmdBuffers(my_data, pDstSet);
3976 pSrcLayout = pSrcSet->pLayout;
3977 pDstLayout = pDstSet->pLayout;
3978 // Validate that src binding is valid for src set layout
3979 if (pSrcLayout->bindingToIndexMap.find(pCDS[i].srcBinding) == pSrcLayout->bindingToIndexMap.end()) {
3980 skipCall |= log_msg(my_data->report_data, VK_DEBUG_REPORT_ERROR_BIT_EXT, VK_DEBUG_REPORT_OBJECT_TYPE_DESCRIPTOR_SET_EXT,
3981 (uint64_t)pSrcSet->set, __LINE__, DRAWSTATE_INVALID_UPDATE_INDEX, "DS",
3982 "Copy descriptor update %u has srcBinding %u "
3983 "which is out of bounds for underlying SetLayout "
3984 "%#" PRIxLEAST64 " which only has bindings 0-%u.",
3985 i, pCDS[i].srcBinding, (uint64_t)pSrcLayout->layout, pSrcLayout->createInfo.bindingCount - 1);
3986 } else if (pDstLayout->bindingToIndexMap.find(pCDS[i].dstBinding) == pDstLayout->bindingToIndexMap.end()) {
3987 skipCall |= log_msg(my_data->report_data, VK_DEBUG_REPORT_ERROR_BIT_EXT, VK_DEBUG_REPORT_OBJECT_TYPE_DESCRIPTOR_SET_EXT,
3988 (uint64_t)pDstSet->set, __LINE__, DRAWSTATE_INVALID_UPDATE_INDEX, "DS",
3989 "Copy descriptor update %u has dstBinding %u "
3990 "which is out of bounds for underlying SetLayout "
3991 "%#" PRIxLEAST64 " which only has bindings 0-%u.",
3992 i, pCDS[i].dstBinding, (uint64_t)pDstLayout->layout, pDstLayout->createInfo.bindingCount - 1);
3993 } else {
3994 // Proceed with validation. Bindings are ok, but make sure update is within bounds of given layout
3995 srcEndIndex = getUpdateEndIndex(my_data, device, pSrcLayout, pCDS[i].srcBinding, pCDS[i].srcArrayElement,
3996 (const GENERIC_HEADER *)&(pCDS[i]));
3997 dstEndIndex = getUpdateEndIndex(my_data, device, pDstLayout, pCDS[i].dstBinding, pCDS[i].dstArrayElement,
3998 (const GENERIC_HEADER *)&(pCDS[i]));
3999 if (getBindingEndIndex(pSrcLayout, pCDS[i].srcBinding) < srcEndIndex) {
4000 pLayoutCI = &pSrcLayout->createInfo;
4001 string DSstr = vk_print_vkdescriptorsetlayoutcreateinfo(pLayoutCI, "{DS} ");
4002 skipCall |=
4003 log_msg(my_data->report_data, VK_DEBUG_REPORT_ERROR_BIT_EXT, VK_DEBUG_REPORT_OBJECT_TYPE_DESCRIPTOR_SET_EXT,
4004 (uint64_t)pSrcSet->set, __LINE__, DRAWSTATE_DESCRIPTOR_UPDATE_OUT_OF_BOUNDS, "DS",
4005 "Copy descriptor src update is out of bounds for matching binding %u in Layout w/ CI:\n%s!",
4006 pCDS[i].srcBinding, DSstr.c_str());
4007 } else if (getBindingEndIndex(pDstLayout, pCDS[i].dstBinding) < dstEndIndex) {
4008 pLayoutCI = &pDstLayout->createInfo;
4009 string DSstr = vk_print_vkdescriptorsetlayoutcreateinfo(pLayoutCI, "{DS} ");
4010 skipCall |=
4011 log_msg(my_data->report_data, VK_DEBUG_REPORT_ERROR_BIT_EXT, VK_DEBUG_REPORT_OBJECT_TYPE_DESCRIPTOR_SET_EXT,
4012 (uint64_t)pDstSet->set, __LINE__, DRAWSTATE_DESCRIPTOR_UPDATE_OUT_OF_BOUNDS, "DS",
4013 "Copy descriptor dest update is out of bounds for matching binding %u in Layout w/ CI:\n%s!",
4014 pCDS[i].dstBinding, DSstr.c_str());
4015 } else {
4016 srcStartIndex = getUpdateStartIndex(my_data, device, pSrcLayout, pCDS[i].srcBinding, pCDS[i].srcArrayElement,
4017 (const GENERIC_HEADER *)&(pCDS[i]));
4018 dstStartIndex = getUpdateStartIndex(my_data, device, pDstLayout, pCDS[i].dstBinding, pCDS[i].dstArrayElement,
4019 (const GENERIC_HEADER *)&(pCDS[i]));
4020 for (uint32_t j = 0; j < pCDS[i].descriptorCount; ++j) {
4021 // For copy just make sure that the types match and then perform the update
4022 if (pSrcLayout->descriptorTypes[srcStartIndex + j] != pDstLayout->descriptorTypes[dstStartIndex + j]) {
4023 skipCall |= log_msg(my_data->report_data, VK_DEBUG_REPORT_ERROR_BIT_EXT, (VkDebugReportObjectTypeEXT)0, 0,
4024 __LINE__, DRAWSTATE_DESCRIPTOR_TYPE_MISMATCH, "DS",
4025 "Copy descriptor update index %u, update count #%u, has src update descriptor type %s "
4026 "that does not match overlapping dest descriptor type of %s!",
4027 i, j + 1, string_VkDescriptorType(pSrcLayout->descriptorTypes[srcStartIndex + j]),
4028 string_VkDescriptorType(pDstLayout->descriptorTypes[dstStartIndex + j]));
4029 } else {
4030 // point dst descriptor at corresponding src descriptor
4031 // TODO : This may be a hole. I believe copy should be its own copy,
4032 // otherwise a subsequent write update to src will incorrectly affect the copy
Tobin Ehlis3a417a52016-03-24 10:16:09 -06004033 pDstSet->pDescriptorUpdates[j + dstStartIndex] = pSrcSet->pDescriptorUpdates[j + srcStartIndex];
Tobin Ehlis5b5e7bc2016-03-09 16:12:48 -07004034 pDstSet->pUpdateStructs = pSrcSet->pUpdateStructs;
4035 }
4036 }
4037 }
4038 }
4039 }
4040 return skipCall;
4041}
4042
Mark Lobodzinskia2d5d612016-03-21 16:32:53 -06004043// Verify that given pool has descriptors that are being requested for allocation.
4044// NOTE : Calls to this function should be wrapped in mutex
Dustin Gravese3319182016-04-05 09:41:17 -06004045static bool validate_descriptor_availability_in_pool(layer_data *dev_data, DESCRIPTOR_POOL_NODE *pPoolNode, uint32_t count,
4046 const VkDescriptorSetLayout *pSetLayouts) {
4047 bool skipCall = false;
Mark Lobodzinskia2d5d612016-03-21 16:32:53 -06004048 uint32_t i = 0;
4049 uint32_t j = 0;
4050
4051 // Track number of descriptorSets allowable in this pool
4052 if (pPoolNode->availableSets < count) {
4053 skipCall |= log_msg(dev_data->report_data, VK_DEBUG_REPORT_ERROR_BIT_EXT, VK_DEBUG_REPORT_OBJECT_TYPE_DESCRIPTOR_POOL_EXT,
4054 reinterpret_cast<uint64_t &>(pPoolNode->pool), __LINE__, DRAWSTATE_DESCRIPTOR_POOL_EMPTY, "DS",
4055 "Unable to allocate %u descriptorSets from pool %#" PRIxLEAST64
4056 ". This pool only has %d descriptorSets remaining.",
4057 count, reinterpret_cast<uint64_t &>(pPoolNode->pool), pPoolNode->availableSets);
4058 } else {
4059 pPoolNode->availableSets -= count;
4060 }
4061
Tobin Ehlis5b5e7bc2016-03-09 16:12:48 -07004062 for (i = 0; i < count; ++i) {
4063 LAYOUT_NODE *pLayout = getLayoutNode(dev_data, pSetLayouts[i]);
4064 if (NULL == pLayout) {
4065 skipCall |=
4066 log_msg(dev_data->report_data, VK_DEBUG_REPORT_ERROR_BIT_EXT, VK_DEBUG_REPORT_OBJECT_TYPE_DESCRIPTOR_SET_LAYOUT_EXT,
4067 (uint64_t)pSetLayouts[i], __LINE__, DRAWSTATE_INVALID_LAYOUT, "DS",
4068 "Unable to find set layout node for layout %#" PRIxLEAST64 " specified in vkAllocateDescriptorSets() call",
4069 (uint64_t)pSetLayouts[i]);
4070 } else {
4071 uint32_t typeIndex = 0, poolSizeCount = 0;
4072 for (j = 0; j < pLayout->createInfo.bindingCount; ++j) {
4073 typeIndex = static_cast<uint32_t>(pLayout->createInfo.pBindings[j].descriptorType);
4074 poolSizeCount = pLayout->createInfo.pBindings[j].descriptorCount;
4075 if (poolSizeCount > pPoolNode->availableDescriptorTypeCount[typeIndex]) {
4076 skipCall |= log_msg(dev_data->report_data, VK_DEBUG_REPORT_ERROR_BIT_EXT,
4077 VK_DEBUG_REPORT_OBJECT_TYPE_DESCRIPTOR_SET_LAYOUT_EXT, (uint64_t)pLayout->layout, __LINE__,
4078 DRAWSTATE_DESCRIPTOR_POOL_EMPTY, "DS",
4079 "Unable to allocate %u descriptors of type %s from pool %#" PRIxLEAST64
Mark Lobodzinskia2d5d612016-03-21 16:32:53 -06004080 ". This pool only has %d descriptors of this type remaining.",
Tobin Ehlis5b5e7bc2016-03-09 16:12:48 -07004081 poolSizeCount, string_VkDescriptorType(pLayout->createInfo.pBindings[j].descriptorType),
4082 (uint64_t)pPoolNode->pool, pPoolNode->availableDescriptorTypeCount[typeIndex]);
4083 } else { // Decrement available descriptors of this type
4084 pPoolNode->availableDescriptorTypeCount[typeIndex] -= poolSizeCount;
4085 }
4086 }
4087 }
4088 }
4089 return skipCall;
4090}
4091
4092// Free the shadowed update node for this Set
4093// NOTE : Calls to this function should be wrapped in mutex
4094static void freeShadowUpdateTree(SET_NODE *pSet) {
4095 GENERIC_HEADER *pShadowUpdate = pSet->pUpdateStructs;
4096 pSet->pUpdateStructs = NULL;
4097 GENERIC_HEADER *pFreeUpdate = pShadowUpdate;
4098 // Clear the descriptor mappings as they will now be invalid
Tobin Ehlis3a417a52016-03-24 10:16:09 -06004099 pSet->pDescriptorUpdates.clear();
Tobin Ehlis5b5e7bc2016-03-09 16:12:48 -07004100 while (pShadowUpdate) {
4101 pFreeUpdate = pShadowUpdate;
4102 pShadowUpdate = (GENERIC_HEADER *)pShadowUpdate->pNext;
4103 VkWriteDescriptorSet *pWDS = NULL;
4104 switch (pFreeUpdate->sType) {
4105 case VK_STRUCTURE_TYPE_WRITE_DESCRIPTOR_SET:
4106 pWDS = (VkWriteDescriptorSet *)pFreeUpdate;
4107 switch (pWDS->descriptorType) {
4108 case VK_DESCRIPTOR_TYPE_SAMPLER:
4109 case VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER:
4110 case VK_DESCRIPTOR_TYPE_SAMPLED_IMAGE:
4111 case VK_DESCRIPTOR_TYPE_STORAGE_IMAGE: {
4112 delete[] pWDS->pImageInfo;
4113 } break;
4114 case VK_DESCRIPTOR_TYPE_UNIFORM_TEXEL_BUFFER:
4115 case VK_DESCRIPTOR_TYPE_STORAGE_TEXEL_BUFFER: {
4116 delete[] pWDS->pTexelBufferView;
4117 } break;
4118 case VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER:
4119 case VK_DESCRIPTOR_TYPE_STORAGE_BUFFER:
4120 case VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER_DYNAMIC:
4121 case VK_DESCRIPTOR_TYPE_STORAGE_BUFFER_DYNAMIC: {
4122 delete[] pWDS->pBufferInfo;
4123 } break;
4124 default:
4125 break;
4126 }
4127 break;
4128 case VK_STRUCTURE_TYPE_COPY_DESCRIPTOR_SET:
4129 break;
4130 default:
4131 assert(0);
4132 break;
4133 }
4134 delete pFreeUpdate;
4135 }
4136}
4137
4138// Free all DS Pools including their Sets & related sub-structs
4139// NOTE : Calls to this function should be wrapped in mutex
4140static void deletePools(layer_data *my_data) {
4141 if (my_data->descriptorPoolMap.size() <= 0)
4142 return;
4143 for (auto ii = my_data->descriptorPoolMap.begin(); ii != my_data->descriptorPoolMap.end(); ++ii) {
4144 SET_NODE *pSet = (*ii).second->pSets;
4145 SET_NODE *pFreeSet = pSet;
4146 while (pSet) {
4147 pFreeSet = pSet;
4148 pSet = pSet->pNext;
4149 // Freeing layouts handled in deleteLayouts() function
4150 // Free Update shadow struct tree
4151 freeShadowUpdateTree(pFreeSet);
Tobin Ehlis5b5e7bc2016-03-09 16:12:48 -07004152 delete pFreeSet;
4153 }
4154 delete (*ii).second;
4155 }
4156 my_data->descriptorPoolMap.clear();
4157}
4158
4159// WARN : Once deleteLayouts() called, any layout ptrs in Pool/Set data structure will be invalid
4160// NOTE : Calls to this function should be wrapped in mutex
4161static void deleteLayouts(layer_data *my_data) {
4162 if (my_data->descriptorSetLayoutMap.size() <= 0)
4163 return;
4164 for (auto ii = my_data->descriptorSetLayoutMap.begin(); ii != my_data->descriptorSetLayoutMap.end(); ++ii) {
4165 LAYOUT_NODE *pLayout = (*ii).second;
4166 if (pLayout->createInfo.pBindings) {
4167 for (uint32_t i = 0; i < pLayout->createInfo.bindingCount; i++) {
4168 delete[] pLayout->createInfo.pBindings[i].pImmutableSamplers;
4169 }
4170 delete[] pLayout->createInfo.pBindings;
4171 }
4172 delete pLayout;
4173 }
4174 my_data->descriptorSetLayoutMap.clear();
4175}
4176
4177// Currently clearing a set is removing all previous updates to that set
4178// TODO : Validate if this is correct clearing behavior
4179static void clearDescriptorSet(layer_data *my_data, VkDescriptorSet set) {
4180 SET_NODE *pSet = getSetNode(my_data, set);
4181 if (!pSet) {
4182 // TODO : Return error
4183 } else {
4184 freeShadowUpdateTree(pSet);
4185 }
4186}
4187
4188static void clearDescriptorPool(layer_data *my_data, const VkDevice device, const VkDescriptorPool pool,
4189 VkDescriptorPoolResetFlags flags) {
4190 DESCRIPTOR_POOL_NODE *pPool = getPoolNode(my_data, pool);
4191 if (!pPool) {
4192 log_msg(my_data->report_data, VK_DEBUG_REPORT_ERROR_BIT_EXT, VK_DEBUG_REPORT_OBJECT_TYPE_DESCRIPTOR_POOL_EXT,
4193 (uint64_t)pool, __LINE__, DRAWSTATE_INVALID_POOL, "DS",
4194 "Unable to find pool node for pool %#" PRIxLEAST64 " specified in vkResetDescriptorPool() call", (uint64_t)pool);
4195 } else {
4196 // TODO: validate flags
Tobin Ehlis51db9742016-04-15 11:59:39 -06004197 // For every set off of this pool, clear it, remove from setMap, and free SET_NODE
Tobin Ehlis5b5e7bc2016-03-09 16:12:48 -07004198 SET_NODE *pSet = pPool->pSets;
Tobin Ehlis51db9742016-04-15 11:59:39 -06004199 SET_NODE *pFreeSet = pSet;
Tobin Ehlis5b5e7bc2016-03-09 16:12:48 -07004200 while (pSet) {
4201 clearDescriptorSet(my_data, pSet->set);
Tobin Ehlis51db9742016-04-15 11:59:39 -06004202 my_data->setMap.erase(pSet->set);
4203 pFreeSet = pSet;
Tobin Ehlis5b5e7bc2016-03-09 16:12:48 -07004204 pSet = pSet->pNext;
Tobin Ehlis51db9742016-04-15 11:59:39 -06004205 delete pFreeSet;
Tobin Ehlis5b5e7bc2016-03-09 16:12:48 -07004206 }
Tobin Ehlis51db9742016-04-15 11:59:39 -06004207 pPool->pSets = nullptr;
Tobin Ehlise9fc72c2016-03-30 12:20:53 -06004208 // Reset available count for each type and available sets for this pool
Tobin Ehlis5b5e7bc2016-03-09 16:12:48 -07004209 for (uint32_t i = 0; i < pPool->availableDescriptorTypeCount.size(); ++i) {
4210 pPool->availableDescriptorTypeCount[i] = pPool->maxDescriptorTypeCount[i];
4211 }
Tobin Ehlise9fc72c2016-03-30 12:20:53 -06004212 pPool->availableSets = pPool->maxSets;
Tobin Ehlis5b5e7bc2016-03-09 16:12:48 -07004213 }
4214}
4215
4216// For given CB object, fetch associated CB Node from map
4217static GLOBAL_CB_NODE *getCBNode(layer_data *my_data, const VkCommandBuffer cb) {
4218 if (my_data->commandBufferMap.count(cb) == 0) {
Tobin Ehlis72d66f02016-03-21 14:14:44 -06004219 log_msg(my_data->report_data, VK_DEBUG_REPORT_ERROR_BIT_EXT, VK_DEBUG_REPORT_OBJECT_TYPE_COMMAND_BUFFER_EXT,
4220 reinterpret_cast<const uint64_t &>(cb), __LINE__, DRAWSTATE_INVALID_COMMAND_BUFFER, "DS",
4221 "Attempt to use CommandBuffer %#" PRIxLEAST64 " that doesn't exist!", (uint64_t)(cb));
Tobin Ehlis5b5e7bc2016-03-09 16:12:48 -07004222 return NULL;
4223 }
4224 return my_data->commandBufferMap[cb];
4225}
4226
4227// Free all CB Nodes
4228// NOTE : Calls to this function should be wrapped in mutex
4229static void deleteCommandBuffers(layer_data *my_data) {
Tobin Ehlis400cff92016-04-11 16:39:29 -06004230 if (my_data->commandBufferMap.empty()) {
Tobin Ehlis5b5e7bc2016-03-09 16:12:48 -07004231 return;
4232 }
4233 for (auto ii = my_data->commandBufferMap.begin(); ii != my_data->commandBufferMap.end(); ++ii) {
4234 delete (*ii).second;
4235 }
4236 my_data->commandBufferMap.clear();
4237}
4238
Dustin Gravese3319182016-04-05 09:41:17 -06004239static bool report_error_no_cb_begin(const layer_data *dev_data, const VkCommandBuffer cb, const char *caller_name) {
Tobin Ehlis5b5e7bc2016-03-09 16:12:48 -07004240 return log_msg(dev_data->report_data, VK_DEBUG_REPORT_ERROR_BIT_EXT, VK_DEBUG_REPORT_OBJECT_TYPE_COMMAND_BUFFER_EXT,
4241 (uint64_t)cb, __LINE__, DRAWSTATE_NO_BEGIN_COMMAND_BUFFER, "DS",
4242 "You must call vkBeginCommandBuffer() before this call to %s", caller_name);
4243}
4244
Dustin Gravese3319182016-04-05 09:41:17 -06004245bool validateCmdsInCmdBuffer(const layer_data *dev_data, const GLOBAL_CB_NODE *pCB, const CMD_TYPE cmd_type) {
Tobin Ehlis5b5e7bc2016-03-09 16:12:48 -07004246 if (!pCB->activeRenderPass)
Dustin Gravese3319182016-04-05 09:41:17 -06004247 return false;
4248 bool skip_call = false;
Tobin Ehlis5b5e7bc2016-03-09 16:12:48 -07004249 if (pCB->activeSubpassContents == VK_SUBPASS_CONTENTS_SECONDARY_COMMAND_BUFFERS && cmd_type != CMD_EXECUTECOMMANDS) {
4250 skip_call |= log_msg(dev_data->report_data, VK_DEBUG_REPORT_ERROR_BIT_EXT, (VkDebugReportObjectTypeEXT)0, 0, __LINE__,
4251 DRAWSTATE_INVALID_COMMAND_BUFFER, "DS",
4252 "Commands cannot be called in a subpass using secondary command buffers.");
4253 } else if (pCB->activeSubpassContents == VK_SUBPASS_CONTENTS_INLINE && cmd_type == CMD_EXECUTECOMMANDS) {
4254 skip_call |= log_msg(dev_data->report_data, VK_DEBUG_REPORT_ERROR_BIT_EXT, (VkDebugReportObjectTypeEXT)0, 0, __LINE__,
4255 DRAWSTATE_INVALID_COMMAND_BUFFER, "DS",
4256 "vkCmdExecuteCommands() cannot be called in a subpass using inline commands.");
4257 }
4258 return skip_call;
4259}
4260
4261static bool checkGraphicsBit(const layer_data *my_data, VkQueueFlags flags, const char *name) {
4262 if (!(flags & VK_QUEUE_GRAPHICS_BIT))
4263 return log_msg(my_data->report_data, VK_DEBUG_REPORT_ERROR_BIT_EXT, (VkDebugReportObjectTypeEXT)0, 0, __LINE__,
4264 DRAWSTATE_INVALID_COMMAND_BUFFER, "DS",
4265 "Cannot call %s on a command buffer allocated from a pool without graphics capabilities.", name);
4266 return false;
4267}
4268
4269static bool checkComputeBit(const layer_data *my_data, VkQueueFlags flags, const char *name) {
4270 if (!(flags & VK_QUEUE_COMPUTE_BIT))
4271 return log_msg(my_data->report_data, VK_DEBUG_REPORT_ERROR_BIT_EXT, (VkDebugReportObjectTypeEXT)0, 0, __LINE__,
4272 DRAWSTATE_INVALID_COMMAND_BUFFER, "DS",
4273 "Cannot call %s on a command buffer allocated from a pool without compute capabilities.", name);
4274 return false;
4275}
4276
4277static bool checkGraphicsOrComputeBit(const layer_data *my_data, VkQueueFlags flags, const char *name) {
4278 if (!((flags & VK_QUEUE_GRAPHICS_BIT) || (flags & VK_QUEUE_COMPUTE_BIT)))
4279 return log_msg(my_data->report_data, VK_DEBUG_REPORT_ERROR_BIT_EXT, (VkDebugReportObjectTypeEXT)0, 0, __LINE__,
4280 DRAWSTATE_INVALID_COMMAND_BUFFER, "DS",
4281 "Cannot call %s on a command buffer allocated from a pool without graphics capabilities.", name);
4282 return false;
4283}
4284
4285// Add specified CMD to the CmdBuffer in given pCB, flagging errors if CB is not
4286// in the recording state or if there's an issue with the Cmd ordering
Dustin Gravese3319182016-04-05 09:41:17 -06004287static bool addCmd(const layer_data *my_data, GLOBAL_CB_NODE *pCB, const CMD_TYPE cmd, const char *caller_name) {
4288 bool skipCall = false;
Tobin Ehlis5b5e7bc2016-03-09 16:12:48 -07004289 auto pool_data = my_data->commandPoolMap.find(pCB->createInfo.commandPool);
4290 if (pool_data != my_data->commandPoolMap.end()) {
Tobin Ehlisb1b606d2016-04-11 14:49:55 -06004291 VkQueueFlags flags = my_data->phys_dev_properties.queue_family_properties[pool_data->second.queueFamilyIndex].queueFlags;
Tobin Ehlis5b5e7bc2016-03-09 16:12:48 -07004292 switch (cmd) {
4293 case CMD_BINDPIPELINE:
4294 case CMD_BINDPIPELINEDELTA:
4295 case CMD_BINDDESCRIPTORSETS:
4296 case CMD_FILLBUFFER:
4297 case CMD_CLEARCOLORIMAGE:
4298 case CMD_SETEVENT:
4299 case CMD_RESETEVENT:
4300 case CMD_WAITEVENTS:
4301 case CMD_BEGINQUERY:
4302 case CMD_ENDQUERY:
4303 case CMD_RESETQUERYPOOL:
4304 case CMD_COPYQUERYPOOLRESULTS:
4305 case CMD_WRITETIMESTAMP:
4306 skipCall |= checkGraphicsOrComputeBit(my_data, flags, cmdTypeToString(cmd).c_str());
4307 break;
4308 case CMD_SETVIEWPORTSTATE:
4309 case CMD_SETSCISSORSTATE:
4310 case CMD_SETLINEWIDTHSTATE:
4311 case CMD_SETDEPTHBIASSTATE:
4312 case CMD_SETBLENDSTATE:
4313 case CMD_SETDEPTHBOUNDSSTATE:
4314 case CMD_SETSTENCILREADMASKSTATE:
4315 case CMD_SETSTENCILWRITEMASKSTATE:
4316 case CMD_SETSTENCILREFERENCESTATE:
4317 case CMD_BINDINDEXBUFFER:
4318 case CMD_BINDVERTEXBUFFER:
4319 case CMD_DRAW:
4320 case CMD_DRAWINDEXED:
4321 case CMD_DRAWINDIRECT:
4322 case CMD_DRAWINDEXEDINDIRECT:
4323 case CMD_BLITIMAGE:
4324 case CMD_CLEARATTACHMENTS:
4325 case CMD_CLEARDEPTHSTENCILIMAGE:
4326 case CMD_RESOLVEIMAGE:
4327 case CMD_BEGINRENDERPASS:
4328 case CMD_NEXTSUBPASS:
4329 case CMD_ENDRENDERPASS:
4330 skipCall |= checkGraphicsBit(my_data, flags, cmdTypeToString(cmd).c_str());
4331 break;
4332 case CMD_DISPATCH:
4333 case CMD_DISPATCHINDIRECT:
4334 skipCall |= checkComputeBit(my_data, flags, cmdTypeToString(cmd).c_str());
4335 break;
4336 case CMD_COPYBUFFER:
4337 case CMD_COPYIMAGE:
4338 case CMD_COPYBUFFERTOIMAGE:
4339 case CMD_COPYIMAGETOBUFFER:
4340 case CMD_CLONEIMAGEDATA:
4341 case CMD_UPDATEBUFFER:
4342 case CMD_PIPELINEBARRIER:
4343 case CMD_EXECUTECOMMANDS:
4344 break;
4345 default:
4346 break;
4347 }
4348 }
4349 if (pCB->state != CB_RECORDING) {
4350 skipCall |= report_error_no_cb_begin(my_data, pCB->commandBuffer, caller_name);
4351 skipCall |= validateCmdsInCmdBuffer(my_data, pCB, cmd);
4352 CMD_NODE cmdNode = {};
4353 // init cmd node and append to end of cmd LL
4354 cmdNode.cmdNumber = ++pCB->numCmds;
4355 cmdNode.type = cmd;
4356 pCB->cmds.push_back(cmdNode);
4357 }
4358 return skipCall;
4359}
4360// Reset the command buffer state
4361// Maintain the createInfo and set state to CB_NEW, but clear all other state
Tobin Ehlis400cff92016-04-11 16:39:29 -06004362static void resetCB(layer_data *dev_data, const VkCommandBuffer cb) {
4363 GLOBAL_CB_NODE *pCB = dev_data->commandBufferMap[cb];
Tobin Ehlis5b5e7bc2016-03-09 16:12:48 -07004364 if (pCB) {
4365 pCB->cmds.clear();
4366 // Reset CB state (note that createInfo is not cleared)
4367 pCB->commandBuffer = cb;
4368 memset(&pCB->beginInfo, 0, sizeof(VkCommandBufferBeginInfo));
4369 memset(&pCB->inheritanceInfo, 0, sizeof(VkCommandBufferInheritanceInfo));
Tobin Ehlis5b5e7bc2016-03-09 16:12:48 -07004370 pCB->numCmds = 0;
4371 memset(pCB->drawCount, 0, NUM_DRAW_TYPES * sizeof(uint64_t));
4372 pCB->state = CB_NEW;
4373 pCB->submitCount = 0;
4374 pCB->status = 0;
Tobin Ehlis5b5e7bc2016-03-09 16:12:48 -07004375 pCB->viewports.clear();
4376 pCB->scissors.clear();
Mark Lobodzinski93c396d2016-04-12 10:41:59 -06004377
Tobin Ehlis72d66f02016-03-21 14:14:44 -06004378 for (uint32_t i = 0; i < VK_PIPELINE_BIND_POINT_RANGE_SIZE; ++i) {
4379 // Before clearing lastBoundState, remove any CB bindings from all uniqueBoundSets
4380 for (auto set : pCB->lastBound[i].uniqueBoundSets) {
Tobin Ehlis400cff92016-04-11 16:39:29 -06004381 auto set_node = dev_data->setMap.find(set);
4382 if (set_node != dev_data->setMap.end()) {
Tobin Ehlis72d66f02016-03-21 14:14:44 -06004383 set_node->second->boundCmdBuffers.erase(pCB->commandBuffer);
4384 }
4385 }
4386 pCB->lastBound[i].reset();
4387 }
Mark Lobodzinski93c396d2016-04-12 10:41:59 -06004388
Tobin Ehlis5b5e7bc2016-03-09 16:12:48 -07004389 memset(&pCB->activeRenderPassBeginInfo, 0, sizeof(pCB->activeRenderPassBeginInfo));
4390 pCB->activeRenderPass = 0;
4391 pCB->activeSubpassContents = VK_SUBPASS_CONTENTS_INLINE;
4392 pCB->activeSubpass = 0;
Tobin Ehlis72d66f02016-03-21 14:14:44 -06004393 pCB->fenceId = 0;
4394 pCB->lastSubmittedFence = VK_NULL_HANDLE;
4395 pCB->lastSubmittedQueue = VK_NULL_HANDLE;
Tobin Ehlis5b5e7bc2016-03-09 16:12:48 -07004396 pCB->destroyedSets.clear();
4397 pCB->updatedSets.clear();
4398 pCB->destroyedFramebuffers.clear();
Tobin Ehlis5b5e7bc2016-03-09 16:12:48 -07004399 pCB->waitedEvents.clear();
4400 pCB->semaphores.clear();
4401 pCB->events.clear();
4402 pCB->waitedEventsBeforeQueryReset.clear();
4403 pCB->queryToStateMap.clear();
4404 pCB->activeQueries.clear();
4405 pCB->startedQueries.clear();
4406 pCB->imageLayoutMap.clear();
4407 pCB->eventToStageMap.clear();
4408 pCB->drawData.clear();
4409 pCB->currentDrawData.buffers.clear();
4410 pCB->primaryCommandBuffer = VK_NULL_HANDLE;
Tobin Ehlisbc300ac2016-04-14 12:22:03 -06004411 // Make sure any secondaryCommandBuffers are removed from globalInFlight
4412 for (auto secondary_cb : pCB->secondaryCommandBuffers) {
4413 dev_data->globalInFlightCmdBuffers.erase(secondary_cb);
4414 }
Tobin Ehlis5b5e7bc2016-03-09 16:12:48 -07004415 pCB->secondaryCommandBuffers.clear();
Tobin Ehlis7a3985a2016-03-25 11:49:51 -06004416 pCB->updateImages.clear();
4417 pCB->updateBuffers.clear();
Tobin Ehlis400cff92016-04-11 16:39:29 -06004418 clear_cmd_buf_and_mem_references(dev_data, pCB);
Michael Lentineb4cc5212016-03-18 14:11:44 -05004419 pCB->eventUpdates.clear();
Mark Lobodzinski93c396d2016-04-12 10:41:59 -06004420
4421 // Remove this cmdBuffer's reference from each FrameBuffer's CB ref list
4422 for (auto framebuffer : pCB->framebuffers) {
4423 auto fbNode = dev_data->frameBufferMap.find(framebuffer);
4424 if (fbNode != dev_data->frameBufferMap.end()) {
4425 fbNode->second.referencingCmdBuffers.erase(pCB->commandBuffer);
4426 }
4427 }
4428 pCB->framebuffers.clear();
4429
Tobin Ehlis5b5e7bc2016-03-09 16:12:48 -07004430 }
4431}
4432
4433// Set PSO-related status bits for CB, including dynamic state set via PSO
4434static void set_cb_pso_status(GLOBAL_CB_NODE *pCB, const PIPELINE_NODE *pPipe) {
Tobin Ehlis5b5e7bc2016-03-09 16:12:48 -07004435 // Account for any dynamic state not set via this PSO
Tobin Ehlisca546212016-04-01 13:51:33 -06004436 if (!pPipe->graphicsPipelineCI.pDynamicState ||
4437 !pPipe->graphicsPipelineCI.pDynamicState->dynamicStateCount) { // All state is static
Tobin Ehlis5b5e7bc2016-03-09 16:12:48 -07004438 pCB->status = CBSTATUS_ALL;
4439 } else {
4440 // First consider all state on
4441 // Then unset any state that's noted as dynamic in PSO
4442 // Finally OR that into CB statemask
4443 CBStatusFlags psoDynStateMask = CBSTATUS_ALL;
Tobin Ehlisca546212016-04-01 13:51:33 -06004444 for (uint32_t i = 0; i < pPipe->graphicsPipelineCI.pDynamicState->dynamicStateCount; i++) {
4445 switch (pPipe->graphicsPipelineCI.pDynamicState->pDynamicStates[i]) {
Tobin Ehlis5b5e7bc2016-03-09 16:12:48 -07004446 case VK_DYNAMIC_STATE_VIEWPORT:
4447 psoDynStateMask &= ~CBSTATUS_VIEWPORT_SET;
4448 break;
4449 case VK_DYNAMIC_STATE_SCISSOR:
4450 psoDynStateMask &= ~CBSTATUS_SCISSOR_SET;
4451 break;
4452 case VK_DYNAMIC_STATE_LINE_WIDTH:
4453 psoDynStateMask &= ~CBSTATUS_LINE_WIDTH_SET;
4454 break;
4455 case VK_DYNAMIC_STATE_DEPTH_BIAS:
4456 psoDynStateMask &= ~CBSTATUS_DEPTH_BIAS_SET;
4457 break;
4458 case VK_DYNAMIC_STATE_BLEND_CONSTANTS:
Tobin Ehlis3d3e06b2016-03-28 11:18:19 -06004459 psoDynStateMask &= ~CBSTATUS_BLEND_CONSTANTS_SET;
Tobin Ehlis5b5e7bc2016-03-09 16:12:48 -07004460 break;
4461 case VK_DYNAMIC_STATE_DEPTH_BOUNDS:
4462 psoDynStateMask &= ~CBSTATUS_DEPTH_BOUNDS_SET;
4463 break;
4464 case VK_DYNAMIC_STATE_STENCIL_COMPARE_MASK:
4465 psoDynStateMask &= ~CBSTATUS_STENCIL_READ_MASK_SET;
4466 break;
4467 case VK_DYNAMIC_STATE_STENCIL_WRITE_MASK:
4468 psoDynStateMask &= ~CBSTATUS_STENCIL_WRITE_MASK_SET;
4469 break;
4470 case VK_DYNAMIC_STATE_STENCIL_REFERENCE:
4471 psoDynStateMask &= ~CBSTATUS_STENCIL_REFERENCE_SET;
4472 break;
4473 default:
4474 // TODO : Flag error here
4475 break;
4476 }
4477 }
4478 pCB->status |= psoDynStateMask;
4479 }
4480}
4481
4482// Print the last bound Gfx Pipeline
Dustin Gravese3319182016-04-05 09:41:17 -06004483static bool printPipeline(layer_data *my_data, const VkCommandBuffer cb) {
4484 bool skipCall = false;
Tobin Ehlis5b5e7bc2016-03-09 16:12:48 -07004485 GLOBAL_CB_NODE *pCB = getCBNode(my_data, cb);
4486 if (pCB) {
Tobin Ehlis72d66f02016-03-21 14:14:44 -06004487 PIPELINE_NODE *pPipeTrav = getPipeline(my_data, pCB->lastBound[VK_PIPELINE_BIND_POINT_GRAPHICS].pipeline);
Tobin Ehlis5b5e7bc2016-03-09 16:12:48 -07004488 if (!pPipeTrav) {
4489 // nothing to print
4490 } else {
4491 skipCall |= log_msg(my_data->report_data, VK_DEBUG_REPORT_INFORMATION_BIT_EXT, (VkDebugReportObjectTypeEXT)0, 0,
4492 __LINE__, DRAWSTATE_NONE, "DS", "%s",
Tobin Ehlisca546212016-04-01 13:51:33 -06004493 vk_print_vkgraphicspipelinecreateinfo(
4494 reinterpret_cast<const VkGraphicsPipelineCreateInfo *>(&pPipeTrav->graphicsPipelineCI), "{DS}")
4495 .c_str());
Tobin Ehlis5b5e7bc2016-03-09 16:12:48 -07004496 }
4497 }
4498 return skipCall;
4499}
4500
Tobin Ehlis5b5e7bc2016-03-09 16:12:48 -07004501static void printCB(layer_data *my_data, const VkCommandBuffer cb) {
4502 GLOBAL_CB_NODE *pCB = getCBNode(my_data, cb);
4503 if (pCB && pCB->cmds.size() > 0) {
4504 log_msg(my_data->report_data, VK_DEBUG_REPORT_INFORMATION_BIT_EXT, (VkDebugReportObjectTypeEXT)0, 0, __LINE__,
4505 DRAWSTATE_NONE, "DS", "Cmds in CB %p", (void *)cb);
4506 vector<CMD_NODE> cmds = pCB->cmds;
4507 for (auto ii = cmds.begin(); ii != cmds.end(); ++ii) {
4508 // TODO : Need to pass cb as srcObj here
4509 log_msg(my_data->report_data, VK_DEBUG_REPORT_INFORMATION_BIT_EXT, VK_DEBUG_REPORT_OBJECT_TYPE_COMMAND_BUFFER_EXT, 0,
4510 __LINE__, DRAWSTATE_NONE, "DS", " CMD#%" PRIu64 ": %s", (*ii).cmdNumber, cmdTypeToString((*ii).type).c_str());
4511 }
4512 } else {
4513 // Nothing to print
4514 }
4515}
4516
Dustin Gravese3319182016-04-05 09:41:17 -06004517static bool synchAndPrintDSConfig(layer_data *my_data, const VkCommandBuffer cb) {
4518 bool skipCall = false;
Tobin Ehlis5b5e7bc2016-03-09 16:12:48 -07004519 if (!(my_data->report_data->active_flags & VK_DEBUG_REPORT_INFORMATION_BIT_EXT)) {
4520 return skipCall;
4521 }
Tobin Ehlis5b5e7bc2016-03-09 16:12:48 -07004522 skipCall |= printPipeline(my_data, cb);
4523 return skipCall;
4524}
4525
4526// Flags validation error if the associated call is made inside a render pass. The apiName
4527// routine should ONLY be called outside a render pass.
Dustin Gravese3319182016-04-05 09:41:17 -06004528static bool insideRenderPass(const layer_data *my_data, GLOBAL_CB_NODE *pCB, const char *apiName) {
4529 bool inside = false;
Tobin Ehlis5b5e7bc2016-03-09 16:12:48 -07004530 if (pCB->activeRenderPass) {
4531 inside = log_msg(my_data->report_data, VK_DEBUG_REPORT_ERROR_BIT_EXT, VK_DEBUG_REPORT_OBJECT_TYPE_COMMAND_BUFFER_EXT,
4532 (uint64_t)pCB->commandBuffer, __LINE__, DRAWSTATE_INVALID_RENDERPASS_CMD, "DS",
4533 "%s: It is invalid to issue this call inside an active render pass (%#" PRIxLEAST64 ")", apiName,
4534 (uint64_t)pCB->activeRenderPass);
4535 }
4536 return inside;
4537}
4538
4539// Flags validation error if the associated call is made outside a render pass. The apiName
4540// routine should ONLY be called inside a render pass.
Dustin Gravese3319182016-04-05 09:41:17 -06004541static bool outsideRenderPass(const layer_data *my_data, GLOBAL_CB_NODE *pCB, const char *apiName) {
4542 bool outside = false;
Tobin Ehlis5b5e7bc2016-03-09 16:12:48 -07004543 if (((pCB->createInfo.level == VK_COMMAND_BUFFER_LEVEL_PRIMARY) && (!pCB->activeRenderPass)) ||
4544 ((pCB->createInfo.level == VK_COMMAND_BUFFER_LEVEL_SECONDARY) && (!pCB->activeRenderPass) &&
4545 !(pCB->beginInfo.flags & VK_COMMAND_BUFFER_USAGE_RENDER_PASS_CONTINUE_BIT))) {
4546 outside = log_msg(my_data->report_data, VK_DEBUG_REPORT_ERROR_BIT_EXT, VK_DEBUG_REPORT_OBJECT_TYPE_COMMAND_BUFFER_EXT,
4547 (uint64_t)pCB->commandBuffer, __LINE__, DRAWSTATE_NO_ACTIVE_RENDERPASS, "DS",
4548 "%s: This call must be issued inside an active render pass.", apiName);
4549 }
4550 return outside;
4551}
4552
Tobin Ehlisb1b606d2016-04-11 14:49:55 -06004553static void init_core_validation(layer_data *instance_data, const VkAllocationCallbacks *pAllocator) {
Tobin Ehlis5b5e7bc2016-03-09 16:12:48 -07004554
Tobin Ehlisb1b606d2016-04-11 14:49:55 -06004555 layer_debug_actions(instance_data->report_data, instance_data->logging_callback, pAllocator, "lunarg_core_validation");
Tobin Ehlis5b5e7bc2016-03-09 16:12:48 -07004556
Tobin Ehlis5b5e7bc2016-03-09 16:12:48 -07004557}
4558
4559VK_LAYER_EXPORT VKAPI_ATTR VkResult VKAPI_CALL
4560vkCreateInstance(const VkInstanceCreateInfo *pCreateInfo, const VkAllocationCallbacks *pAllocator, VkInstance *pInstance) {
4561 VkLayerInstanceCreateInfo *chain_info = get_chain_info(pCreateInfo, VK_LAYER_LINK_INFO);
4562
4563 assert(chain_info->u.pLayerInfo);
4564 PFN_vkGetInstanceProcAddr fpGetInstanceProcAddr = chain_info->u.pLayerInfo->pfnNextGetInstanceProcAddr;
4565 PFN_vkCreateInstance fpCreateInstance = (PFN_vkCreateInstance)fpGetInstanceProcAddr(NULL, "vkCreateInstance");
4566 if (fpCreateInstance == NULL)
4567 return VK_ERROR_INITIALIZATION_FAILED;
4568
4569 // Advance the link info for the next element on the chain
4570 chain_info->u.pLayerInfo = chain_info->u.pLayerInfo->pNext;
4571
4572 VkResult result = fpCreateInstance(pCreateInfo, pAllocator, pInstance);
4573 if (result != VK_SUCCESS)
4574 return result;
4575
Tobin Ehlisb1b606d2016-04-11 14:49:55 -06004576 layer_data *instance_data = get_my_data_ptr(get_dispatch_key(*pInstance), layer_data_map);
4577 instance_data->instance_dispatch_table = new VkLayerInstanceDispatchTable;
4578 layer_init_instance_dispatch_table(*pInstance, instance_data->instance_dispatch_table, fpGetInstanceProcAddr);
Tobin Ehlis5b5e7bc2016-03-09 16:12:48 -07004579
Tobin Ehlisb1b606d2016-04-11 14:49:55 -06004580 instance_data->report_data =
4581 debug_report_create_instance(instance_data->instance_dispatch_table, *pInstance, pCreateInfo->enabledExtensionCount,
4582 pCreateInfo->ppEnabledExtensionNames);
Tobin Ehlis5b5e7bc2016-03-09 16:12:48 -07004583
Tobin Ehlisb1b606d2016-04-11 14:49:55 -06004584 init_core_validation(instance_data, pAllocator);
Tobin Ehlis5b5e7bc2016-03-09 16:12:48 -07004585
4586 ValidateLayerOrdering(*pCreateInfo);
4587
4588 return result;
4589}
4590
4591/* hook DestroyInstance to remove tableInstanceMap entry */
4592VK_LAYER_EXPORT VKAPI_ATTR void VKAPI_CALL vkDestroyInstance(VkInstance instance, const VkAllocationCallbacks *pAllocator) {
4593 // TODOSC : Shouldn't need any customization here
4594 dispatch_key key = get_dispatch_key(instance);
4595 // TBD: Need any locking this early, in case this function is called at the
4596 // same time by more than one thread?
4597 layer_data *my_data = get_my_data_ptr(key, layer_data_map);
4598 VkLayerInstanceDispatchTable *pTable = my_data->instance_dispatch_table;
4599 pTable->DestroyInstance(instance, pAllocator);
4600
Jeremy Hayesb9e99232016-04-13 16:20:24 -06004601 std::lock_guard<std::mutex> lock(global_lock);
Tobin Ehlis5b5e7bc2016-03-09 16:12:48 -07004602 // Clean up logging callback, if any
4603 while (my_data->logging_callback.size() > 0) {
4604 VkDebugReportCallbackEXT callback = my_data->logging_callback.back();
4605 layer_destroy_msg_callback(my_data->report_data, callback, pAllocator);
4606 my_data->logging_callback.pop_back();
4607 }
4608
4609 layer_debug_report_destroy_instance(my_data->report_data);
4610 delete my_data->instance_dispatch_table;
4611 layer_data_map.erase(key);
Tobin Ehlis5b5e7bc2016-03-09 16:12:48 -07004612}
4613
4614static void createDeviceRegisterExtensions(const VkDeviceCreateInfo *pCreateInfo, VkDevice device) {
4615 uint32_t i;
4616 // TBD: Need any locking, in case this function is called at the same time
4617 // by more than one thread?
4618 layer_data *dev_data = get_my_data_ptr(get_dispatch_key(device), layer_data_map);
4619 dev_data->device_extensions.wsi_enabled = false;
4620
4621 VkLayerDispatchTable *pDisp = dev_data->device_dispatch_table;
4622 PFN_vkGetDeviceProcAddr gpa = pDisp->GetDeviceProcAddr;
4623 pDisp->CreateSwapchainKHR = (PFN_vkCreateSwapchainKHR)gpa(device, "vkCreateSwapchainKHR");
4624 pDisp->DestroySwapchainKHR = (PFN_vkDestroySwapchainKHR)gpa(device, "vkDestroySwapchainKHR");
4625 pDisp->GetSwapchainImagesKHR = (PFN_vkGetSwapchainImagesKHR)gpa(device, "vkGetSwapchainImagesKHR");
4626 pDisp->AcquireNextImageKHR = (PFN_vkAcquireNextImageKHR)gpa(device, "vkAcquireNextImageKHR");
4627 pDisp->QueuePresentKHR = (PFN_vkQueuePresentKHR)gpa(device, "vkQueuePresentKHR");
4628
4629 for (i = 0; i < pCreateInfo->enabledExtensionCount; i++) {
4630 if (strcmp(pCreateInfo->ppEnabledExtensionNames[i], VK_KHR_SWAPCHAIN_EXTENSION_NAME) == 0)
4631 dev_data->device_extensions.wsi_enabled = true;
4632 }
4633}
4634
4635VK_LAYER_EXPORT VKAPI_ATTR VkResult VKAPI_CALL vkCreateDevice(VkPhysicalDevice gpu, const VkDeviceCreateInfo *pCreateInfo,
4636 const VkAllocationCallbacks *pAllocator, VkDevice *pDevice) {
4637 VkLayerDeviceCreateInfo *chain_info = get_chain_info(pCreateInfo, VK_LAYER_LINK_INFO);
4638
4639 assert(chain_info->u.pLayerInfo);
4640 PFN_vkGetInstanceProcAddr fpGetInstanceProcAddr = chain_info->u.pLayerInfo->pfnNextGetInstanceProcAddr;
4641 PFN_vkGetDeviceProcAddr fpGetDeviceProcAddr = chain_info->u.pLayerInfo->pfnNextGetDeviceProcAddr;
4642 PFN_vkCreateDevice fpCreateDevice = (PFN_vkCreateDevice)fpGetInstanceProcAddr(NULL, "vkCreateDevice");
4643 if (fpCreateDevice == NULL) {
4644 return VK_ERROR_INITIALIZATION_FAILED;
4645 }
4646
4647 // Advance the link info for the next element on the chain
4648 chain_info->u.pLayerInfo = chain_info->u.pLayerInfo->pNext;
4649
4650 VkResult result = fpCreateDevice(gpu, pCreateInfo, pAllocator, pDevice);
4651 if (result != VK_SUCCESS) {
4652 return result;
4653 }
4654
Jeremy Hayesb9e99232016-04-13 16:20:24 -06004655 std::unique_lock<std::mutex> lock(global_lock);
Tobin Ehlis5b5e7bc2016-03-09 16:12:48 -07004656 layer_data *my_instance_data = get_my_data_ptr(get_dispatch_key(gpu), layer_data_map);
4657 layer_data *my_device_data = get_my_data_ptr(get_dispatch_key(*pDevice), layer_data_map);
4658
4659 // Setup device dispatch table
4660 my_device_data->device_dispatch_table = new VkLayerDispatchTable;
4661 layer_init_device_dispatch_table(*pDevice, my_device_data->device_dispatch_table, fpGetDeviceProcAddr);
Chris Forbes6c94fb92016-03-30 11:35:21 +13004662 my_device_data->device = *pDevice;
Tobin Ehlis5b5e7bc2016-03-09 16:12:48 -07004663
4664 my_device_data->report_data = layer_debug_report_create_device(my_instance_data->report_data, *pDevice);
4665 createDeviceRegisterExtensions(pCreateInfo, *pDevice);
4666 // Get physical device limits for this device
Tobin Ehlisb1b606d2016-04-11 14:49:55 -06004667 my_instance_data->instance_dispatch_table->GetPhysicalDeviceProperties(gpu, &(my_device_data->phys_dev_properties.properties));
Tobin Ehlis5b5e7bc2016-03-09 16:12:48 -07004668 uint32_t count;
4669 my_instance_data->instance_dispatch_table->GetPhysicalDeviceQueueFamilyProperties(gpu, &count, nullptr);
Tobin Ehlisb1b606d2016-04-11 14:49:55 -06004670 my_device_data->phys_dev_properties.queue_family_properties.resize(count);
Tobin Ehlis5b5e7bc2016-03-09 16:12:48 -07004671 my_instance_data->instance_dispatch_table->GetPhysicalDeviceQueueFamilyProperties(
Tobin Ehlisb1b606d2016-04-11 14:49:55 -06004672 gpu, &count, &my_device_data->phys_dev_properties.queue_family_properties[0]);
Tobin Ehlis5b5e7bc2016-03-09 16:12:48 -07004673 // TODO: device limits should make sure these are compatible
4674 if (pCreateInfo->pEnabledFeatures) {
Tobin Ehlisb1b606d2016-04-11 14:49:55 -06004675 my_device_data->phys_dev_properties.features = *pCreateInfo->pEnabledFeatures;
Tobin Ehlis5b5e7bc2016-03-09 16:12:48 -07004676 } else {
Tobin Ehlisb1b606d2016-04-11 14:49:55 -06004677 memset(&my_device_data->phys_dev_properties.features, 0, sizeof(VkPhysicalDeviceFeatures));
Tobin Ehlis5b5e7bc2016-03-09 16:12:48 -07004678 }
Tobin Ehlisb1b606d2016-04-11 14:49:55 -06004679 // Store physical device mem limits into device layer_data struct
4680 my_instance_data->instance_dispatch_table->GetPhysicalDeviceMemoryProperties(gpu, &my_device_data->phys_dev_mem_props);
Jeremy Hayesb9e99232016-04-13 16:20:24 -06004681 lock.unlock();
Tobin Ehlis5b5e7bc2016-03-09 16:12:48 -07004682
4683 ValidateLayerOrdering(*pCreateInfo);
4684
4685 return result;
4686}
4687
4688// prototype
4689static void deleteRenderPasses(layer_data *);
4690VK_LAYER_EXPORT VKAPI_ATTR void VKAPI_CALL vkDestroyDevice(VkDevice device, const VkAllocationCallbacks *pAllocator) {
4691 // TODOSC : Shouldn't need any customization here
4692 dispatch_key key = get_dispatch_key(device);
4693 layer_data *dev_data = get_my_data_ptr(key, layer_data_map);
4694 // Free all the memory
Jeremy Hayesb9e99232016-04-13 16:20:24 -06004695 std::unique_lock<std::mutex> lock(global_lock);
Tobin Ehlis5b5e7bc2016-03-09 16:12:48 -07004696 deletePipelines(dev_data);
4697 deleteRenderPasses(dev_data);
4698 deleteCommandBuffers(dev_data);
4699 deletePools(dev_data);
4700 deleteLayouts(dev_data);
4701 dev_data->imageViewMap.clear();
4702 dev_data->imageMap.clear();
4703 dev_data->imageSubresourceMap.clear();
4704 dev_data->imageLayoutMap.clear();
Tobin Ehlis5b5e7bc2016-03-09 16:12:48 -07004705 dev_data->bufferViewMap.clear();
4706 dev_data->bufferMap.clear();
Tobin Ehlis13443022016-04-12 10:49:41 -06004707 // Queues persist until device is destroyed
4708 dev_data->queueMap.clear();
Jeremy Hayesb9e99232016-04-13 16:20:24 -06004709 lock.unlock();
Mark Lobodzinski03b71512016-03-23 14:33:02 -06004710#if MTMERGESOURCE
Dustin Gravese3319182016-04-05 09:41:17 -06004711 bool skipCall = false;
Jeremy Hayesb9e99232016-04-13 16:20:24 -06004712 lock.lock();
Tobin Ehlis5b5e7bc2016-03-09 16:12:48 -07004713 log_msg(dev_data->report_data, VK_DEBUG_REPORT_INFORMATION_BIT_EXT, VK_DEBUG_REPORT_OBJECT_TYPE_DEVICE_EXT,
4714 (uint64_t)device, __LINE__, MEMTRACK_NONE, "MEM", "Printing List details prior to vkDestroyDevice()");
4715 log_msg(dev_data->report_data, VK_DEBUG_REPORT_INFORMATION_BIT_EXT, VK_DEBUG_REPORT_OBJECT_TYPE_DEVICE_EXT,
4716 (uint64_t)device, __LINE__, MEMTRACK_NONE, "MEM", "================================================");
Chris Forbes0a1ce3d2016-04-06 15:16:26 +12004717 print_mem_list(dev_data);
4718 printCBList(dev_data);
Tobin Ehlis5b5e7bc2016-03-09 16:12:48 -07004719 // Report any memory leaks
Tobin Ehlis58070a62016-03-16 16:00:36 -06004720 DEVICE_MEM_INFO *pInfo = NULL;
Tobin Ehlis400cff92016-04-11 16:39:29 -06004721 if (!dev_data->memObjMap.empty()) {
Tobin Ehlis5b5e7bc2016-03-09 16:12:48 -07004722 for (auto ii = dev_data->memObjMap.begin(); ii != dev_data->memObjMap.end(); ++ii) {
4723 pInfo = &(*ii).second;
4724 if (pInfo->allocInfo.allocationSize != 0) {
4725 // Valid Usage: All child objects created on device must have been destroyed prior to destroying device
4726 skipCall |=
4727 log_msg(dev_data->report_data, VK_DEBUG_REPORT_ERROR_BIT_EXT,
4728 VK_DEBUG_REPORT_OBJECT_TYPE_DEVICE_MEMORY_EXT, (uint64_t)pInfo->mem, __LINE__, MEMTRACK_MEMORY_LEAK,
4729 "MEM", "Mem Object %" PRIu64 " has not been freed. You should clean up this memory by calling "
4730 "vkFreeMemory(%" PRIu64 ") prior to vkDestroyDevice().",
4731 (uint64_t)(pInfo->mem), (uint64_t)(pInfo->mem));
4732 }
4733 }
4734 }
Tobin Ehlis5b5e7bc2016-03-09 16:12:48 -07004735 layer_debug_report_destroy_device(device);
Jeremy Hayesb9e99232016-04-13 16:20:24 -06004736 lock.unlock();
Tobin Ehlis5b5e7bc2016-03-09 16:12:48 -07004737
4738#if DISPATCH_MAP_DEBUG
4739 fprintf(stderr, "Device: %p, key: %p\n", device, key);
4740#endif
4741 VkLayerDispatchTable *pDisp = dev_data->device_dispatch_table;
Dustin Gravese3319182016-04-05 09:41:17 -06004742 if (!skipCall) {
Tobin Ehlis5b5e7bc2016-03-09 16:12:48 -07004743 pDisp->DestroyDevice(device, pAllocator);
4744 }
4745#else
4746 dev_data->device_dispatch_table->DestroyDevice(device, pAllocator);
4747#endif
4748 delete dev_data->device_dispatch_table;
4749 layer_data_map.erase(key);
4750}
4751
Tobin Ehlis5b5e7bc2016-03-09 16:12:48 -07004752static const VkExtensionProperties instance_extensions[] = {{VK_EXT_DEBUG_REPORT_EXTENSION_NAME, VK_EXT_DEBUG_REPORT_SPEC_VERSION}};
4753
4754VK_LAYER_EXPORT VKAPI_ATTR VkResult VKAPI_CALL
4755vkEnumerateInstanceExtensionProperties(const char *pLayerName, uint32_t *pCount, VkExtensionProperties *pProperties) {
4756 return util_GetExtensionProperties(1, instance_extensions, pCount, pProperties);
4757}
4758
4759VK_LAYER_EXPORT VKAPI_ATTR VkResult VKAPI_CALL
4760vkEnumerateInstanceLayerProperties(uint32_t *pCount, VkLayerProperties *pProperties) {
4761 return util_GetLayerProperties(ARRAY_SIZE(cv_global_layers), cv_global_layers, pCount, pProperties);
4762}
4763
4764// TODO: Why does this exist - can we just use global?
4765static const VkLayerProperties cv_device_layers[] = {{
Jon Ashburnf1ea4182016-03-22 12:57:13 -06004766 "VK_LAYER_LUNARG_core_validation", VK_LAYER_API_VERSION, 1, "LunarG Validation Layer",
Tobin Ehlis5b5e7bc2016-03-09 16:12:48 -07004767}};
4768
4769VK_LAYER_EXPORT VKAPI_ATTR VkResult VKAPI_CALL vkEnumerateDeviceExtensionProperties(VkPhysicalDevice physicalDevice,
4770 const char *pLayerName, uint32_t *pCount,
4771 VkExtensionProperties *pProperties) {
4772 if (pLayerName == NULL) {
4773 dispatch_key key = get_dispatch_key(physicalDevice);
4774 layer_data *my_data = get_my_data_ptr(key, layer_data_map);
4775 return my_data->instance_dispatch_table->EnumerateDeviceExtensionProperties(physicalDevice, NULL, pCount, pProperties);
4776 } else {
4777 return util_GetExtensionProperties(0, NULL, pCount, pProperties);
4778 }
4779}
4780
4781VK_LAYER_EXPORT VKAPI_ATTR VkResult VKAPI_CALL
4782vkEnumerateDeviceLayerProperties(VkPhysicalDevice physicalDevice, uint32_t *pCount, VkLayerProperties *pProperties) {
4783 /* draw_state physical device layers are the same as global */
4784 return util_GetLayerProperties(ARRAY_SIZE(cv_device_layers), cv_device_layers, pCount, pProperties);
4785}
4786
4787// This validates that the initial layout specified in the command buffer for
4788// the IMAGE is the same
4789// as the global IMAGE layout
Dustin Gravese3319182016-04-05 09:41:17 -06004790static bool ValidateCmdBufImageLayouts(VkCommandBuffer cmdBuffer) {
4791 bool skip_call = false;
Tobin Ehlis5b5e7bc2016-03-09 16:12:48 -07004792 layer_data *dev_data = get_my_data_ptr(get_dispatch_key(cmdBuffer), layer_data_map);
4793 GLOBAL_CB_NODE *pCB = getCBNode(dev_data, cmdBuffer);
4794 for (auto cb_image_data : pCB->imageLayoutMap) {
4795 VkImageLayout imageLayout;
4796 if (!FindLayout(dev_data, cb_image_data.first, imageLayout)) {
4797 skip_call |=
4798 log_msg(dev_data->report_data, VK_DEBUG_REPORT_ERROR_BIT_EXT, VK_DEBUG_REPORT_OBJECT_TYPE_COMMAND_BUFFER_EXT, 0,
4799 __LINE__, DRAWSTATE_INVALID_IMAGE_LAYOUT, "DS", "Cannot submit cmd buffer using deleted image %" PRIu64 ".",
4800 reinterpret_cast<const uint64_t &>(cb_image_data.first));
4801 } else {
4802 if (cb_image_data.second.initialLayout == VK_IMAGE_LAYOUT_UNDEFINED) {
4803 // TODO: Set memory invalid which is in mem_tracker currently
4804 } else if (imageLayout != cb_image_data.second.initialLayout) {
Mark Young856ecb42016-04-11 16:53:53 -06004805 if (cb_image_data.first.hasSubresource) {
4806 skip_call |= log_msg(
4807 dev_data->report_data, VK_DEBUG_REPORT_ERROR_BIT_EXT, VK_DEBUG_REPORT_OBJECT_TYPE_COMMAND_BUFFER_EXT,
4808 reinterpret_cast<uint64_t &>(cmdBuffer), __LINE__, DRAWSTATE_INVALID_IMAGE_LAYOUT, "DS",
4809 "Cannot submit cmd buffer using image (%" PRIx64 ") [sub-resource: array layer %u, mip level %u], "
4810 "with layout %s when first use is %s.",
4811 reinterpret_cast<const uint64_t &>(cb_image_data.first.image), cb_image_data.first.subresource.arrayLayer,
4812 cb_image_data.first.subresource.mipLevel, string_VkImageLayout(imageLayout),
4813 string_VkImageLayout(cb_image_data.second.initialLayout));
4814 } else {
4815 skip_call |= log_msg(
4816 dev_data->report_data, VK_DEBUG_REPORT_ERROR_BIT_EXT, VK_DEBUG_REPORT_OBJECT_TYPE_COMMAND_BUFFER_EXT,
4817 reinterpret_cast<uint64_t &>(cmdBuffer), __LINE__, DRAWSTATE_INVALID_IMAGE_LAYOUT, "DS",
4818 "Cannot submit cmd buffer using image (%" PRIx64 ") with layout %s when "
4819 "first use is %s.",
4820 reinterpret_cast<const uint64_t &>(cb_image_data.first.image), string_VkImageLayout(imageLayout),
4821 string_VkImageLayout(cb_image_data.second.initialLayout));
4822 }
Tobin Ehlis5b5e7bc2016-03-09 16:12:48 -07004823 }
4824 SetLayout(dev_data, cb_image_data.first, cb_image_data.second.layout);
4825 }
4826 }
4827 return skip_call;
4828}
Mark Lobodzinski39d15352016-03-25 15:22:33 -06004829
Tobin Ehlis5b5e7bc2016-03-09 16:12:48 -07004830// Track which resources are in-flight by atomically incrementing their "in_use" count
Dustin Gravese3319182016-04-05 09:41:17 -06004831static bool validateAndIncrementResources(layer_data *my_data, GLOBAL_CB_NODE *pCB) {
4832 bool skip_call = false;
Tobin Ehlis5b5e7bc2016-03-09 16:12:48 -07004833 for (auto drawDataElement : pCB->drawData) {
4834 for (auto buffer : drawDataElement.buffers) {
4835 auto buffer_data = my_data->bufferMap.find(buffer);
4836 if (buffer_data == my_data->bufferMap.end()) {
4837 skip_call |= log_msg(my_data->report_data, VK_DEBUG_REPORT_ERROR_BIT_EXT, VK_DEBUG_REPORT_OBJECT_TYPE_BUFFER_EXT,
4838 (uint64_t)(buffer), __LINE__, DRAWSTATE_INVALID_BUFFER, "DS",
4839 "Cannot submit cmd buffer using deleted buffer %" PRIu64 ".", (uint64_t)(buffer));
4840 } else {
4841 buffer_data->second.in_use.fetch_add(1);
4842 }
4843 }
4844 }
Tobin Ehlis72d66f02016-03-21 14:14:44 -06004845 for (uint32_t i = 0; i < VK_PIPELINE_BIND_POINT_RANGE_SIZE; ++i) {
4846 for (auto set : pCB->lastBound[i].uniqueBoundSets) {
4847 auto setNode = my_data->setMap.find(set);
4848 if (setNode == my_data->setMap.end()) {
4849 skip_call |=
4850 log_msg(my_data->report_data, VK_DEBUG_REPORT_ERROR_BIT_EXT, VK_DEBUG_REPORT_OBJECT_TYPE_DESCRIPTOR_SET_EXT,
4851 (uint64_t)(set), __LINE__, DRAWSTATE_INVALID_DESCRIPTOR_SET, "DS",
4852 "Cannot submit cmd buffer using deleted descriptor set %" PRIu64 ".", (uint64_t)(set));
4853 } else {
4854 setNode->second->in_use.fetch_add(1);
4855 }
Tobin Ehlis5b5e7bc2016-03-09 16:12:48 -07004856 }
4857 }
4858 for (auto semaphore : pCB->semaphores) {
4859 auto semaphoreNode = my_data->semaphoreMap.find(semaphore);
4860 if (semaphoreNode == my_data->semaphoreMap.end()) {
4861 skip_call |=
4862 log_msg(my_data->report_data, VK_DEBUG_REPORT_ERROR_BIT_EXT, VK_DEBUG_REPORT_OBJECT_TYPE_DESCRIPTOR_SET_EXT,
4863 reinterpret_cast<uint64_t &>(semaphore), __LINE__, DRAWSTATE_INVALID_SEMAPHORE, "DS",
4864 "Cannot submit cmd buffer using deleted semaphore %" PRIu64 ".", reinterpret_cast<uint64_t &>(semaphore));
4865 } else {
4866 semaphoreNode->second.in_use.fetch_add(1);
4867 }
4868 }
4869 for (auto event : pCB->events) {
4870 auto eventNode = my_data->eventMap.find(event);
4871 if (eventNode == my_data->eventMap.end()) {
4872 skip_call |=
4873 log_msg(my_data->report_data, VK_DEBUG_REPORT_ERROR_BIT_EXT, VK_DEBUG_REPORT_OBJECT_TYPE_DESCRIPTOR_SET_EXT,
4874 reinterpret_cast<uint64_t &>(event), __LINE__, DRAWSTATE_INVALID_EVENT, "DS",
4875 "Cannot submit cmd buffer using deleted event %" PRIu64 ".", reinterpret_cast<uint64_t &>(event));
4876 } else {
4877 eventNode->second.in_use.fetch_add(1);
4878 }
4879 }
4880 return skip_call;
4881}
4882
Dustin Gravese3319182016-04-05 09:41:17 -06004883static void decrementResources(layer_data *my_data, VkCommandBuffer cmdBuffer) {
Tobin Ehlis5b5e7bc2016-03-09 16:12:48 -07004884 GLOBAL_CB_NODE *pCB = getCBNode(my_data, cmdBuffer);
4885 for (auto drawDataElement : pCB->drawData) {
4886 for (auto buffer : drawDataElement.buffers) {
4887 auto buffer_data = my_data->bufferMap.find(buffer);
4888 if (buffer_data != my_data->bufferMap.end()) {
4889 buffer_data->second.in_use.fetch_sub(1);
4890 }
4891 }
4892 }
Tobin Ehlis72d66f02016-03-21 14:14:44 -06004893 for (uint32_t i = 0; i < VK_PIPELINE_BIND_POINT_RANGE_SIZE; ++i) {
4894 for (auto set : pCB->lastBound[i].uniqueBoundSets) {
4895 auto setNode = my_data->setMap.find(set);
4896 if (setNode != my_data->setMap.end()) {
4897 setNode->second->in_use.fetch_sub(1);
4898 }
Tobin Ehlis5b5e7bc2016-03-09 16:12:48 -07004899 }
4900 }
4901 for (auto semaphore : pCB->semaphores) {
4902 auto semaphoreNode = my_data->semaphoreMap.find(semaphore);
4903 if (semaphoreNode != my_data->semaphoreMap.end()) {
4904 semaphoreNode->second.in_use.fetch_sub(1);
4905 }
4906 }
4907 for (auto event : pCB->events) {
4908 auto eventNode = my_data->eventMap.find(event);
4909 if (eventNode != my_data->eventMap.end()) {
4910 eventNode->second.in_use.fetch_sub(1);
4911 }
4912 }
4913 for (auto queryStatePair : pCB->queryToStateMap) {
4914 my_data->queryToStateMap[queryStatePair.first] = queryStatePair.second;
4915 }
4916 for (auto eventStagePair : pCB->eventToStageMap) {
4917 my_data->eventMap[eventStagePair.first].stageMask = eventStagePair.second;
4918 }
4919}
4920
Dustin Gravese3319182016-04-05 09:41:17 -06004921static void decrementResources(layer_data *my_data, uint32_t fenceCount, const VkFence *pFences) {
Tobin Ehlis5b5e7bc2016-03-09 16:12:48 -07004922 for (uint32_t i = 0; i < fenceCount; ++i) {
4923 auto fence_data = my_data->fenceMap.find(pFences[i]);
4924 if (fence_data == my_data->fenceMap.end() || !fence_data->second.needsSignaled)
4925 return;
4926 fence_data->second.needsSignaled = false;
4927 fence_data->second.in_use.fetch_sub(1);
Jamie Madill1d5109d2016-04-04 15:09:51 -04004928 decrementResources(my_data, static_cast<uint32_t>(fence_data->second.priorFences.size()),
4929 fence_data->second.priorFences.data());
Tobin Ehlis5b5e7bc2016-03-09 16:12:48 -07004930 for (auto cmdBuffer : fence_data->second.cmdBuffers) {
4931 decrementResources(my_data, cmdBuffer);
4932 }
4933 }
4934}
4935
Dustin Gravese3319182016-04-05 09:41:17 -06004936static void decrementResources(layer_data *my_data, VkQueue queue) {
Tobin Ehlis5b5e7bc2016-03-09 16:12:48 -07004937 auto queue_data = my_data->queueMap.find(queue);
4938 if (queue_data != my_data->queueMap.end()) {
4939 for (auto cmdBuffer : queue_data->second.untrackedCmdBuffers) {
4940 decrementResources(my_data, cmdBuffer);
4941 }
4942 queue_data->second.untrackedCmdBuffers.clear();
Jamie Madill1d5109d2016-04-04 15:09:51 -04004943 decrementResources(my_data, static_cast<uint32_t>(queue_data->second.lastFences.size()),
4944 queue_data->second.lastFences.data());
Michael Lentine0a32ed72016-03-17 16:34:32 -05004945 }
4946}
4947
Dustin Gravese3319182016-04-05 09:41:17 -06004948static void updateTrackedCommandBuffers(layer_data *dev_data, VkQueue queue, VkQueue other_queue, VkFence fence) {
Michael Lentine0a32ed72016-03-17 16:34:32 -05004949 if (queue == other_queue) {
4950 return;
4951 }
4952 auto queue_data = dev_data->queueMap.find(queue);
4953 auto other_queue_data = dev_data->queueMap.find(other_queue);
4954 if (queue_data == dev_data->queueMap.end() || other_queue_data == dev_data->queueMap.end()) {
4955 return;
4956 }
Jamie Madillca32a762016-04-04 14:39:53 -04004957 for (auto fenceInner : other_queue_data->second.lastFences) {
4958 queue_data->second.lastFences.push_back(fenceInner);
Michael Lentine0a32ed72016-03-17 16:34:32 -05004959 }
4960 if (fence != VK_NULL_HANDLE) {
4961 auto fence_data = dev_data->fenceMap.find(fence);
4962 if (fence_data == dev_data->fenceMap.end()) {
4963 return;
4964 }
4965 for (auto cmdbuffer : other_queue_data->second.untrackedCmdBuffers) {
4966 fence_data->second.cmdBuffers.push_back(cmdbuffer);
4967 }
4968 other_queue_data->second.untrackedCmdBuffers.clear();
4969 } else {
4970 for (auto cmdbuffer : other_queue_data->second.untrackedCmdBuffers) {
4971 queue_data->second.untrackedCmdBuffers.push_back(cmdbuffer);
4972 }
4973 other_queue_data->second.untrackedCmdBuffers.clear();
Tobin Ehlis5b5e7bc2016-03-09 16:12:48 -07004974 }
Michael Lentineb4cc5212016-03-18 14:11:44 -05004975 for (auto eventStagePair : other_queue_data->second.eventToStageMap) {
4976 queue_data->second.eventToStageMap[eventStagePair.first] = eventStagePair.second;
4977 }
Tobin Ehlis5b5e7bc2016-03-09 16:12:48 -07004978}
4979
Dustin Gravese3319182016-04-05 09:41:17 -06004980static void trackCommandBuffers(layer_data *my_data, VkQueue queue, uint32_t submitCount, const VkSubmitInfo *pSubmits,
4981 VkFence fence) {
Tobin Ehlis5b5e7bc2016-03-09 16:12:48 -07004982 auto queue_data = my_data->queueMap.find(queue);
4983 if (fence != VK_NULL_HANDLE) {
Michael Lentine0a32ed72016-03-17 16:34:32 -05004984 vector<VkFence> prior_fences;
Tobin Ehlis5b5e7bc2016-03-09 16:12:48 -07004985 auto fence_data = my_data->fenceMap.find(fence);
4986 if (fence_data == my_data->fenceMap.end()) {
4987 return;
4988 }
4989 if (queue_data != my_data->queueMap.end()) {
Michael Lentine0a32ed72016-03-17 16:34:32 -05004990 prior_fences = queue_data->second.lastFences;
4991 queue_data->second.lastFences.clear();
4992 queue_data->second.lastFences.push_back(fence);
4993 for (auto cmdbuffer : queue_data->second.untrackedCmdBuffers) {
4994 fence_data->second.cmdBuffers.push_back(cmdbuffer);
Tobin Ehlis5b5e7bc2016-03-09 16:12:48 -07004995 }
4996 queue_data->second.untrackedCmdBuffers.clear();
4997 }
4998 fence_data->second.cmdBuffers.clear();
Michael Lentine0a32ed72016-03-17 16:34:32 -05004999 fence_data->second.priorFences = prior_fences;
Tobin Ehlis5b5e7bc2016-03-09 16:12:48 -07005000 fence_data->second.needsSignaled = true;
5001 fence_data->second.queue = queue;
5002 fence_data->second.in_use.fetch_add(1);
5003 for (uint32_t submit_idx = 0; submit_idx < submitCount; submit_idx++) {
5004 const VkSubmitInfo *submit = &pSubmits[submit_idx];
5005 for (uint32_t i = 0; i < submit->commandBufferCount; ++i) {
5006 for (auto secondaryCmdBuffer : my_data->commandBufferMap[submit->pCommandBuffers[i]]->secondaryCommandBuffers) {
5007 fence_data->second.cmdBuffers.push_back(secondaryCmdBuffer);
5008 }
5009 fence_data->second.cmdBuffers.push_back(submit->pCommandBuffers[i]);
5010 }
5011 }
5012 } else {
5013 if (queue_data != my_data->queueMap.end()) {
5014 for (uint32_t submit_idx = 0; submit_idx < submitCount; submit_idx++) {
5015 const VkSubmitInfo *submit = &pSubmits[submit_idx];
5016 for (uint32_t i = 0; i < submit->commandBufferCount; ++i) {
5017 for (auto secondaryCmdBuffer : my_data->commandBufferMap[submit->pCommandBuffers[i]]->secondaryCommandBuffers) {
5018 queue_data->second.untrackedCmdBuffers.push_back(secondaryCmdBuffer);
5019 }
5020 queue_data->second.untrackedCmdBuffers.push_back(submit->pCommandBuffers[i]);
5021 }
5022 }
5023 }
5024 }
5025 if (queue_data != my_data->queueMap.end()) {
5026 for (uint32_t submit_idx = 0; submit_idx < submitCount; submit_idx++) {
5027 const VkSubmitInfo *submit = &pSubmits[submit_idx];
5028 for (uint32_t i = 0; i < submit->commandBufferCount; ++i) {
5029 // Add cmdBuffers to both the global set and queue set
5030 for (auto secondaryCmdBuffer : my_data->commandBufferMap[submit->pCommandBuffers[i]]->secondaryCommandBuffers) {
5031 my_data->globalInFlightCmdBuffers.insert(secondaryCmdBuffer);
5032 queue_data->second.inFlightCmdBuffers.insert(secondaryCmdBuffer);
5033 }
5034 my_data->globalInFlightCmdBuffers.insert(submit->pCommandBuffers[i]);
5035 queue_data->second.inFlightCmdBuffers.insert(submit->pCommandBuffers[i]);
5036 }
5037 }
5038 }
5039}
5040
Dustin Gravese3319182016-04-05 09:41:17 -06005041static bool validateCommandBufferSimultaneousUse(layer_data *dev_data, GLOBAL_CB_NODE *pCB) {
Tobin Ehlis5b5e7bc2016-03-09 16:12:48 -07005042 bool skip_call = false;
5043 if (dev_data->globalInFlightCmdBuffers.count(pCB->commandBuffer) &&
5044 !(pCB->beginInfo.flags & VK_COMMAND_BUFFER_USAGE_SIMULTANEOUS_USE_BIT)) {
5045 skip_call |=
5046 log_msg(dev_data->report_data, VK_DEBUG_REPORT_ERROR_BIT_EXT, VK_DEBUG_REPORT_OBJECT_TYPE_COMMAND_BUFFER_EXT, 0,
Chris Forbes27c3e0d2016-03-31 11:47:29 +13005047 __LINE__, DRAWSTATE_INVALID_CB_SIMULTANEOUS_USE, "DS",
5048 "Command Buffer %#" PRIx64 " is already in use and is not marked for simultaneous use.",
Tobin Ehlis5b5e7bc2016-03-09 16:12:48 -07005049 reinterpret_cast<uint64_t>(pCB->commandBuffer));
5050 }
5051 return skip_call;
5052}
5053
5054static bool validateCommandBufferState(layer_data *dev_data, GLOBAL_CB_NODE *pCB) {
5055 bool skipCall = false;
Tobin Ehlis0a59acd2016-04-14 15:44:20 -06005056 // Validate ONE_TIME_SUBMIT_BIT CB is not being submitted more than once
5057 if ((pCB->beginInfo.flags & VK_COMMAND_BUFFER_USAGE_ONE_TIME_SUBMIT_BIT) && (pCB->submitCount > 1)) {
5058 skipCall |= log_msg(dev_data->report_data, VK_DEBUG_REPORT_ERROR_BIT_EXT, VK_DEBUG_REPORT_OBJECT_TYPE_COMMAND_BUFFER_EXT, 0,
5059 __LINE__, DRAWSTATE_COMMAND_BUFFER_SINGLE_SUBMIT_VIOLATION, "DS",
5060 "CB %#" PRIxLEAST64 " was begun w/ VK_COMMAND_BUFFER_USAGE_ONE_TIME_SUBMIT_BIT "
5061 "set, but has been submitted %#" PRIxLEAST64 " times.",
5062 (uint64_t)(pCB->commandBuffer), pCB->submitCount);
5063 }
Tobin Ehlis5b5e7bc2016-03-09 16:12:48 -07005064 // Validate that cmd buffers have been updated
5065 if (CB_RECORDED != pCB->state) {
5066 if (CB_INVALID == pCB->state) {
5067 // Inform app of reason CB invalid
5068 bool causeReported = false;
5069 if (!pCB->destroyedSets.empty()) {
5070 std::stringstream set_string;
5071 for (auto set : pCB->destroyedSets)
5072 set_string << " " << set;
5073
5074 skipCall |=
5075 log_msg(dev_data->report_data, VK_DEBUG_REPORT_ERROR_BIT_EXT, VK_DEBUG_REPORT_OBJECT_TYPE_COMMAND_BUFFER_EXT,
5076 (uint64_t)(pCB->commandBuffer), __LINE__, DRAWSTATE_INVALID_COMMAND_BUFFER, "DS",
5077 "You are submitting command buffer %#" PRIxLEAST64
5078 " that is invalid because it had the following bound descriptor set(s) destroyed: %s",
5079 (uint64_t)(pCB->commandBuffer), set_string.str().c_str());
5080 causeReported = true;
5081 }
5082 if (!pCB->updatedSets.empty()) {
5083 std::stringstream set_string;
5084 for (auto set : pCB->updatedSets)
5085 set_string << " " << set;
5086
5087 skipCall |=
5088 log_msg(dev_data->report_data, VK_DEBUG_REPORT_ERROR_BIT_EXT, VK_DEBUG_REPORT_OBJECT_TYPE_COMMAND_BUFFER_EXT,
5089 (uint64_t)(pCB->commandBuffer), __LINE__, DRAWSTATE_INVALID_COMMAND_BUFFER, "DS",
5090 "You are submitting command buffer %#" PRIxLEAST64
5091 " that is invalid because it had the following bound descriptor set(s) updated: %s",
5092 (uint64_t)(pCB->commandBuffer), set_string.str().c_str());
5093 causeReported = true;
5094 }
5095 if (!pCB->destroyedFramebuffers.empty()) {
5096 std::stringstream fb_string;
5097 for (auto fb : pCB->destroyedFramebuffers)
5098 fb_string << " " << fb;
5099
5100 skipCall |=
5101 log_msg(dev_data->report_data, VK_DEBUG_REPORT_ERROR_BIT_EXT, VK_DEBUG_REPORT_OBJECT_TYPE_COMMAND_BUFFER_EXT,
5102 reinterpret_cast<uint64_t &>(pCB->commandBuffer), __LINE__, DRAWSTATE_INVALID_COMMAND_BUFFER, "DS",
5103 "You are submitting command buffer %#" PRIxLEAST64 " that is invalid because it had the following "
5104 "referenced framebuffers destroyed: %s",
5105 reinterpret_cast<uint64_t &>(pCB->commandBuffer), fb_string.str().c_str());
5106 causeReported = true;
5107 }
5108 // TODO : This is defensive programming to make sure an error is
5109 // flagged if we hit this INVALID cmd buffer case and none of the
5110 // above cases are hit. As the number of INVALID cases grows, this
5111 // code should be updated to seemlessly handle all the cases.
5112 if (!causeReported) {
5113 skipCall |= log_msg(
5114 dev_data->report_data, VK_DEBUG_REPORT_ERROR_BIT_EXT, VK_DEBUG_REPORT_OBJECT_TYPE_COMMAND_BUFFER_EXT,
5115 reinterpret_cast<uint64_t &>(pCB->commandBuffer), __LINE__, DRAWSTATE_INVALID_COMMAND_BUFFER, "DS",
5116 "You are submitting command buffer %#" PRIxLEAST64 " that is invalid due to an unknown cause. Validation "
5117 "should "
5118 "be improved to report the exact cause.",
5119 reinterpret_cast<uint64_t &>(pCB->commandBuffer));
5120 }
5121 } else { // Flag error for using CB w/o vkEndCommandBuffer() called
5122 skipCall |=
5123 log_msg(dev_data->report_data, VK_DEBUG_REPORT_ERROR_BIT_EXT, VK_DEBUG_REPORT_OBJECT_TYPE_COMMAND_BUFFER_EXT,
5124 (uint64_t)(pCB->commandBuffer), __LINE__, DRAWSTATE_NO_END_COMMAND_BUFFER, "DS",
5125 "You must call vkEndCommandBuffer() on CB %#" PRIxLEAST64 " before this call to vkQueueSubmit()!",
5126 (uint64_t)(pCB->commandBuffer));
5127 }
5128 }
5129 return skipCall;
5130}
5131
Dustin Gravese3319182016-04-05 09:41:17 -06005132static bool validatePrimaryCommandBufferState(layer_data *dev_data, GLOBAL_CB_NODE *pCB) {
Tobin Ehlis5b5e7bc2016-03-09 16:12:48 -07005133 // Track in-use for resources off of primary and any secondary CBs
Dustin Gravese3319182016-04-05 09:41:17 -06005134 bool skipCall = validateAndIncrementResources(dev_data, pCB);
Tobin Ehlis5b5e7bc2016-03-09 16:12:48 -07005135 if (!pCB->secondaryCommandBuffers.empty()) {
5136 for (auto secondaryCmdBuffer : pCB->secondaryCommandBuffers) {
5137 skipCall |= validateAndIncrementResources(dev_data, dev_data->commandBufferMap[secondaryCmdBuffer]);
5138 GLOBAL_CB_NODE *pSubCB = getCBNode(dev_data, secondaryCmdBuffer);
5139 if (pSubCB->primaryCommandBuffer != pCB->commandBuffer) {
5140 log_msg(dev_data->report_data, VK_DEBUG_REPORT_ERROR_BIT_EXT, VK_DEBUG_REPORT_OBJECT_TYPE_COMMAND_BUFFER_EXT, 0,
5141 __LINE__, DRAWSTATE_COMMAND_BUFFER_SINGLE_SUBMIT_VIOLATION, "DS",
5142 "CB %#" PRIxLEAST64 " was submitted with secondary buffer %#" PRIxLEAST64
5143 " but that buffer has subsequently been bound to "
5144 "primary cmd buffer %#" PRIxLEAST64 ".",
5145 reinterpret_cast<uint64_t>(pCB->commandBuffer), reinterpret_cast<uint64_t>(secondaryCmdBuffer),
5146 reinterpret_cast<uint64_t>(pSubCB->primaryCommandBuffer));
5147 }
5148 }
5149 }
Tobin Ehlis5b5e7bc2016-03-09 16:12:48 -07005150 skipCall |= validateCommandBufferState(dev_data, pCB);
5151 // If USAGE_SIMULTANEOUS_USE_BIT not set then CB cannot already be executing
5152 // on device
5153 skipCall |= validateCommandBufferSimultaneousUse(dev_data, pCB);
5154 return skipCall;
5155}
5156
5157VK_LAYER_EXPORT VKAPI_ATTR VkResult VKAPI_CALL
5158vkQueueSubmit(VkQueue queue, uint32_t submitCount, const VkSubmitInfo *pSubmits, VkFence fence) {
Dustin Gravese3319182016-04-05 09:41:17 -06005159 bool skipCall = false;
Tobin Ehlis72d66f02016-03-21 14:14:44 -06005160 GLOBAL_CB_NODE *pCBNode = NULL;
Tobin Ehlis5b5e7bc2016-03-09 16:12:48 -07005161 layer_data *dev_data = get_my_data_ptr(get_dispatch_key(queue), layer_data_map);
5162 VkResult result = VK_ERROR_VALIDATION_FAILED_EXT;
Jeremy Hayesb9e99232016-04-13 16:20:24 -06005163 std::unique_lock<std::mutex> lock(global_lock);
Tobin Ehlis13443022016-04-12 10:49:41 -06005164 // First verify that fence is not in use
5165 if ((fence != VK_NULL_HANDLE) && (submitCount != 0) && dev_data->fenceMap[fence].in_use.load()) {
5166 skipCall |= log_msg(dev_data->report_data, VK_DEBUG_REPORT_ERROR_BIT_EXT, VK_DEBUG_REPORT_OBJECT_TYPE_FENCE_EXT,
5167 (uint64_t)(fence), __LINE__, DRAWSTATE_INVALID_FENCE, "DS",
5168 "Fence %#" PRIx64 " is already in use by another submission.", (uint64_t)(fence));
5169 }
Tobin Ehlis5b5e7bc2016-03-09 16:12:48 -07005170 uint64_t fenceId = 0;
5171 skipCall = add_fence_info(dev_data, fence, queue, &fenceId);
Tobin Ehlis13443022016-04-12 10:49:41 -06005172 // TODO : Review these old print functions and clean up as appropriate
Chris Forbes0a1ce3d2016-04-06 15:16:26 +12005173 print_mem_list(dev_data);
5174 printCBList(dev_data);
Tobin Ehlis13443022016-04-12 10:49:41 -06005175 // Now verify each individual submit
5176 std::unordered_set<VkQueue> processed_other_queues;
Tobin Ehlis5b5e7bc2016-03-09 16:12:48 -07005177 for (uint32_t submit_idx = 0; submit_idx < submitCount; submit_idx++) {
5178 const VkSubmitInfo *submit = &pSubmits[submit_idx];
Tobin Ehlis13443022016-04-12 10:49:41 -06005179 vector<VkSemaphore> semaphoreList;
5180 for (uint32_t i = 0; i < submit->waitSemaphoreCount; ++i) {
5181 const VkSemaphore &semaphore = submit->pWaitSemaphores[i];
Tobin Ehlis41bb2362016-04-13 14:36:16 -06005182 semaphoreList.push_back(semaphore);
Tobin Ehlis13443022016-04-12 10:49:41 -06005183 if (dev_data->semaphoreMap.find(semaphore) != dev_data->semaphoreMap.end()) {
5184 if (dev_data->semaphoreMap[semaphore].signaled) {
5185 dev_data->semaphoreMap[semaphore].signaled = false;
Tobin Ehlis13443022016-04-12 10:49:41 -06005186 } else {
5187 skipCall |=
5188 log_msg(dev_data->report_data, VK_DEBUG_REPORT_ERROR_BIT_EXT, VK_DEBUG_REPORT_OBJECT_TYPE_SEMAPHORE_EXT,
5189 reinterpret_cast<const uint64_t &>(semaphore), __LINE__, DRAWSTATE_QUEUE_FORWARD_PROGRESS, "DS",
5190 "Queue %#" PRIx64 " is waiting on semaphore %#" PRIx64 " that has no way to be signaled.",
5191 reinterpret_cast<uint64_t &>(queue), reinterpret_cast<const uint64_t &>(semaphore));
5192 }
5193 const VkQueue &other_queue = dev_data->semaphoreMap[semaphore].queue;
5194 if (other_queue != VK_NULL_HANDLE && !processed_other_queues.count(other_queue)) {
5195 updateTrackedCommandBuffers(dev_data, queue, other_queue, fence);
5196 processed_other_queues.insert(other_queue);
5197 }
5198 }
5199 }
5200 for (uint32_t i = 0; i < submit->signalSemaphoreCount; ++i) {
5201 const VkSemaphore &semaphore = submit->pSignalSemaphores[i];
5202 if (dev_data->semaphoreMap.find(semaphore) != dev_data->semaphoreMap.end()) {
5203 semaphoreList.push_back(semaphore);
5204 if (dev_data->semaphoreMap[semaphore].signaled) {
5205 skipCall |=
5206 log_msg(dev_data->report_data, VK_DEBUG_REPORT_ERROR_BIT_EXT, VK_DEBUG_REPORT_OBJECT_TYPE_SEMAPHORE_EXT,
5207 reinterpret_cast<const uint64_t &>(semaphore), __LINE__, DRAWSTATE_QUEUE_FORWARD_PROGRESS, "DS",
5208 "Queue %#" PRIx64 " is signaling semaphore %#" PRIx64
5209 " that has already been signaled but not waited on by queue %#" PRIx64 ".",
5210 reinterpret_cast<uint64_t &>(queue), reinterpret_cast<const uint64_t &>(semaphore),
5211 reinterpret_cast<uint64_t &>(dev_data->semaphoreMap[semaphore].queue));
5212 } else {
5213 dev_data->semaphoreMap[semaphore].signaled = true;
5214 dev_data->semaphoreMap[semaphore].queue = queue;
5215 }
5216 }
5217 }
Tobin Ehlis5b5e7bc2016-03-09 16:12:48 -07005218 for (uint32_t i = 0; i < submit->commandBufferCount; i++) {
Tobin Ehlis13443022016-04-12 10:49:41 -06005219 skipCall |= ValidateCmdBufImageLayouts(submit->pCommandBuffers[i]);
Tobin Ehlis72d66f02016-03-21 14:14:44 -06005220 pCBNode = getCBNode(dev_data, submit->pCommandBuffers[i]);
5221 if (pCBNode) {
Tobin Ehlis13443022016-04-12 10:49:41 -06005222 pCBNode->semaphores = semaphoreList;
5223 pCBNode->submitCount++; // increment submit count
Tobin Ehlis72d66f02016-03-21 14:14:44 -06005224 pCBNode->fenceId = fenceId;
5225 pCBNode->lastSubmittedFence = fence;
5226 pCBNode->lastSubmittedQueue = queue;
Tobin Ehlis13443022016-04-12 10:49:41 -06005227 skipCall |= validatePrimaryCommandBufferState(dev_data, pCBNode);
5228 // Call submit-time functions to validate/update state
Tobin Ehlis72d66f02016-03-21 14:14:44 -06005229 for (auto &function : pCBNode->validate_functions) {
Tobin Ehlis5b5e7bc2016-03-09 16:12:48 -07005230 skipCall |= function();
5231 }
Michael Lentineb4cc5212016-03-18 14:11:44 -05005232 for (auto &function : pCBNode->eventUpdates) {
Dustin Gravese3319182016-04-05 09:41:17 -06005233 skipCall |= function(queue);
Michael Lentineb4cc5212016-03-18 14:11:44 -05005234 }
Tobin Ehlis5b5e7bc2016-03-09 16:12:48 -07005235 }
5236 }
Tobin Ehlis5b5e7bc2016-03-09 16:12:48 -07005237 }
5238 // Update cmdBuffer-related data structs and mark fence in-use
5239 trackCommandBuffers(dev_data, queue, submitCount, pSubmits, fence);
Jeremy Hayesb9e99232016-04-13 16:20:24 -06005240 lock.unlock();
Dustin Gravese3319182016-04-05 09:41:17 -06005241 if (!skipCall)
Tobin Ehlis5b5e7bc2016-03-09 16:12:48 -07005242 result = dev_data->device_dispatch_table->QueueSubmit(queue, submitCount, pSubmits, fence);
Tobin Ehlis5b5e7bc2016-03-09 16:12:48 -07005243
Tobin Ehlis5b5e7bc2016-03-09 16:12:48 -07005244 return result;
5245}
5246
Mark Lobodzinski03b71512016-03-23 14:33:02 -06005247#if MTMERGESOURCE
Tobin Ehlis5b5e7bc2016-03-09 16:12:48 -07005248VK_LAYER_EXPORT VKAPI_ATTR VkResult VKAPI_CALL vkAllocateMemory(VkDevice device, const VkMemoryAllocateInfo *pAllocateInfo,
5249 const VkAllocationCallbacks *pAllocator, VkDeviceMemory *pMemory) {
5250 layer_data *my_data = get_my_data_ptr(get_dispatch_key(device), layer_data_map);
5251 VkResult result = my_data->device_dispatch_table->AllocateMemory(device, pAllocateInfo, pAllocator, pMemory);
5252 // TODO : Track allocations and overall size here
Jeremy Hayesb9e99232016-04-13 16:20:24 -06005253 std::lock_guard<std::mutex> lock(global_lock);
Tobin Ehlis5b5e7bc2016-03-09 16:12:48 -07005254 add_mem_obj_info(my_data, device, *pMemory, pAllocateInfo);
Chris Forbes0a1ce3d2016-04-06 15:16:26 +12005255 print_mem_list(my_data);
Tobin Ehlis5b5e7bc2016-03-09 16:12:48 -07005256 return result;
5257}
5258
5259VK_LAYER_EXPORT VKAPI_ATTR void VKAPI_CALL
5260vkFreeMemory(VkDevice device, VkDeviceMemory mem, const VkAllocationCallbacks *pAllocator) {
5261 layer_data *my_data = get_my_data_ptr(get_dispatch_key(device), layer_data_map);
Tobin Ehlis5b5e7bc2016-03-09 16:12:48 -07005262
5263 // From spec : A memory object is freed by calling vkFreeMemory() when it is no longer needed.
5264 // Before freeing a memory object, an application must ensure the memory object is no longer
5265 // in use by the device—for example by command buffers queued for execution. The memory need
5266 // not yet be unbound from all images and buffers, but any further use of those images or
5267 // buffers (on host or device) for anything other than destroying those objects will result in
5268 // undefined behavior.
5269
Jeremy Hayesb9e99232016-04-13 16:20:24 -06005270 std::unique_lock<std::mutex> lock(global_lock);
Dustin Gravese3319182016-04-05 09:41:17 -06005271 freeMemObjInfo(my_data, device, mem, false);
Chris Forbes0a1ce3d2016-04-06 15:16:26 +12005272 print_mem_list(my_data);
5273 printCBList(my_data);
Jeremy Hayesb9e99232016-04-13 16:20:24 -06005274 lock.unlock();
Tobin Ehlis5b5e7bc2016-03-09 16:12:48 -07005275 my_data->device_dispatch_table->FreeMemory(device, mem, pAllocator);
5276}
5277
Dustin Gravese3319182016-04-05 09:41:17 -06005278static bool validateMemRange(layer_data *my_data, VkDeviceMemory mem, VkDeviceSize offset, VkDeviceSize size) {
5279 bool skipCall = false;
Tobin Ehlis5b5e7bc2016-03-09 16:12:48 -07005280
5281 if (size == 0) {
5282 // TODO: a size of 0 is not listed as an invalid use in the spec, should it be?
5283 skipCall = log_msg(my_data->report_data, VK_DEBUG_REPORT_WARNING_BIT_EXT, VK_DEBUG_REPORT_OBJECT_TYPE_DEVICE_MEMORY_EXT,
5284 (uint64_t)mem, __LINE__, MEMTRACK_INVALID_MAP, "MEM",
5285 "VkMapMemory: Attempting to map memory range of size zero");
5286 }
5287
5288 auto mem_element = my_data->memObjMap.find(mem);
5289 if (mem_element != my_data->memObjMap.end()) {
5290 // It is an application error to call VkMapMemory on an object that is already mapped
5291 if (mem_element->second.memRange.size != 0) {
5292 skipCall = log_msg(my_data->report_data, VK_DEBUG_REPORT_ERROR_BIT_EXT, VK_DEBUG_REPORT_OBJECT_TYPE_DEVICE_MEMORY_EXT,
5293 (uint64_t)mem, __LINE__, MEMTRACK_INVALID_MAP, "MEM",
5294 "VkMapMemory: Attempting to map memory on an already-mapped object %#" PRIxLEAST64, (uint64_t)mem);
5295 }
5296
5297 // Validate that offset + size is within object's allocationSize
5298 if (size == VK_WHOLE_SIZE) {
5299 if (offset >= mem_element->second.allocInfo.allocationSize) {
5300 skipCall = log_msg(my_data->report_data, VK_DEBUG_REPORT_ERROR_BIT_EXT,
5301 VK_DEBUG_REPORT_OBJECT_TYPE_DEVICE_MEMORY_EXT, (uint64_t)mem, __LINE__, MEMTRACK_INVALID_MAP,
5302 "MEM", "Mapping Memory from %" PRIu64 " to %" PRIu64 " with total array size %" PRIu64, offset,
5303 mem_element->second.allocInfo.allocationSize, mem_element->second.allocInfo.allocationSize);
5304 }
5305 } else {
5306 if ((offset + size) > mem_element->second.allocInfo.allocationSize) {
5307 skipCall = log_msg(my_data->report_data, VK_DEBUG_REPORT_ERROR_BIT_EXT,
5308 VK_DEBUG_REPORT_OBJECT_TYPE_DEVICE_MEMORY_EXT, (uint64_t)mem, __LINE__, MEMTRACK_INVALID_MAP,
5309 "MEM", "Mapping Memory from %" PRIu64 " to %" PRIu64 " with total array size %" PRIu64, offset,
5310 size + offset, mem_element->second.allocInfo.allocationSize);
5311 }
5312 }
5313 }
5314 return skipCall;
5315}
5316
Dustin Gravese3319182016-04-05 09:41:17 -06005317static void storeMemRanges(layer_data *my_data, VkDeviceMemory mem, VkDeviceSize offset, VkDeviceSize size) {
Tobin Ehlis5b5e7bc2016-03-09 16:12:48 -07005318 auto mem_element = my_data->memObjMap.find(mem);
5319 if (mem_element != my_data->memObjMap.end()) {
5320 MemRange new_range;
5321 new_range.offset = offset;
5322 new_range.size = size;
5323 mem_element->second.memRange = new_range;
5324 }
5325}
5326
Dustin Gravese3319182016-04-05 09:41:17 -06005327static bool deleteMemRanges(layer_data *my_data, VkDeviceMemory mem) {
5328 bool skipCall = false;
Tobin Ehlis5b5e7bc2016-03-09 16:12:48 -07005329 auto mem_element = my_data->memObjMap.find(mem);
5330 if (mem_element != my_data->memObjMap.end()) {
5331 if (!mem_element->second.memRange.size) {
5332 // Valid Usage: memory must currently be mapped
5333 skipCall = log_msg(my_data->report_data, VK_DEBUG_REPORT_ERROR_BIT_EXT, VK_DEBUG_REPORT_OBJECT_TYPE_DEVICE_MEMORY_EXT,
5334 (uint64_t)mem, __LINE__, MEMTRACK_INVALID_MAP, "MEM",
5335 "Unmapping Memory without memory being mapped: mem obj %#" PRIxLEAST64, (uint64_t)mem);
5336 }
5337 mem_element->second.memRange.size = 0;
5338 if (mem_element->second.pData) {
5339 free(mem_element->second.pData);
5340 mem_element->second.pData = 0;
5341 }
5342 }
5343 return skipCall;
5344}
5345
5346static char NoncoherentMemoryFillValue = 0xb;
5347
Tobin Ehlisb1b606d2016-04-11 14:49:55 -06005348static void initializeAndTrackMemory(layer_data *dev_data, VkDeviceMemory mem, VkDeviceSize size, void **ppData) {
5349 auto mem_element = dev_data->memObjMap.find(mem);
5350 if (mem_element != dev_data->memObjMap.end()) {
Tobin Ehlis5b5e7bc2016-03-09 16:12:48 -07005351 mem_element->second.pDriverData = *ppData;
5352 uint32_t index = mem_element->second.allocInfo.memoryTypeIndex;
Tobin Ehlisb1b606d2016-04-11 14:49:55 -06005353 if (dev_data->phys_dev_mem_props.memoryTypes[index].propertyFlags & VK_MEMORY_PROPERTY_HOST_COHERENT_BIT) {
Tobin Ehlis5b5e7bc2016-03-09 16:12:48 -07005354 mem_element->second.pData = 0;
5355 } else {
5356 if (size == VK_WHOLE_SIZE) {
5357 size = mem_element->second.allocInfo.allocationSize;
5358 }
5359 size_t convSize = (size_t)(size);
5360 mem_element->second.pData = malloc(2 * convSize);
5361 memset(mem_element->second.pData, NoncoherentMemoryFillValue, 2 * convSize);
5362 *ppData = static_cast<char *>(mem_element->second.pData) + (convSize / 2);
5363 }
5364 }
5365}
5366#endif
5367// Note: This function assumes that the global lock is held by the calling
5368// thread.
Dustin Gravese3319182016-04-05 09:41:17 -06005369static bool cleanInFlightCmdBuffer(layer_data *my_data, VkCommandBuffer cmdBuffer) {
5370 bool skip_call = false;
Tobin Ehlis5b5e7bc2016-03-09 16:12:48 -07005371 GLOBAL_CB_NODE *pCB = getCBNode(my_data, cmdBuffer);
5372 if (pCB) {
5373 for (auto queryEventsPair : pCB->waitedEventsBeforeQueryReset) {
5374 for (auto event : queryEventsPair.second) {
5375 if (my_data->eventMap[event].needsSignaled) {
5376 skip_call |= log_msg(my_data->report_data, VK_DEBUG_REPORT_ERROR_BIT_EXT,
5377 VK_DEBUG_REPORT_OBJECT_TYPE_QUERY_POOL_EXT, 0, 0, DRAWSTATE_INVALID_QUERY, "DS",
5378 "Cannot get query results on queryPool %" PRIu64
5379 " with index %d which was guarded by unsignaled event %" PRIu64 ".",
5380 (uint64_t)(queryEventsPair.first.pool), queryEventsPair.first.index, (uint64_t)(event));
5381 }
5382 }
5383 }
5384 }
5385 return skip_call;
5386}
5387// Remove given cmd_buffer from the global inFlight set.
5388// Also, if given queue is valid, then remove the cmd_buffer from that queues
5389// inFlightCmdBuffer set. Finally, check all other queues and if given cmd_buffer
5390// is still in flight on another queue, add it back into the global set.
5391// Note: This function assumes that the global lock is held by the calling
5392// thread.
5393static inline void removeInFlightCmdBuffer(layer_data *dev_data, VkCommandBuffer cmd_buffer, VkQueue queue) {
5394 // Pull it off of global list initially, but if we find it in any other queue list, add it back in
5395 dev_data->globalInFlightCmdBuffers.erase(cmd_buffer);
5396 if (dev_data->queueMap.find(queue) != dev_data->queueMap.end()) {
5397 dev_data->queueMap[queue].inFlightCmdBuffers.erase(cmd_buffer);
5398 for (auto q : dev_data->queues) {
5399 if ((q != queue) &&
5400 (dev_data->queueMap[q].inFlightCmdBuffers.find(cmd_buffer) != dev_data->queueMap[q].inFlightCmdBuffers.end())) {
5401 dev_data->globalInFlightCmdBuffers.insert(cmd_buffer);
5402 break;
5403 }
5404 }
5405 }
5406}
Mark Lobodzinski03b71512016-03-23 14:33:02 -06005407#if MTMERGESOURCE
Tobin Ehlis5b5e7bc2016-03-09 16:12:48 -07005408static inline bool verifyFenceStatus(VkDevice device, VkFence fence, const char *apiCall) {
5409 layer_data *my_data = get_my_data_ptr(get_dispatch_key(device), layer_data_map);
Dustin Gravese3319182016-04-05 09:41:17 -06005410 bool skipCall = false;
Tobin Ehlis5b5e7bc2016-03-09 16:12:48 -07005411 auto pFenceInfo = my_data->fenceMap.find(fence);
5412 if (pFenceInfo != my_data->fenceMap.end()) {
Dustin Gravese3319182016-04-05 09:41:17 -06005413 if (!pFenceInfo->second.firstTimeFlag) {
5414 if ((pFenceInfo->second.createInfo.flags & VK_FENCE_CREATE_SIGNALED_BIT) && !pFenceInfo->second.firstTimeFlag) {
Tobin Ehlis5b5e7bc2016-03-09 16:12:48 -07005415 skipCall |=
5416 log_msg(my_data->report_data, VK_DEBUG_REPORT_INFORMATION_BIT_EXT, VK_DEBUG_REPORT_OBJECT_TYPE_FENCE_EXT,
5417 (uint64_t)fence, __LINE__, MEMTRACK_INVALID_FENCE_STATE, "MEM",
5418 "%s specified fence %#" PRIxLEAST64 " already in SIGNALED state.", apiCall, (uint64_t)fence);
5419 }
5420 if (!pFenceInfo->second.queue && !pFenceInfo->second.swapchain) { // Checking status of unsubmitted fence
5421 skipCall |= log_msg(my_data->report_data, VK_DEBUG_REPORT_WARNING_BIT_EXT, VK_DEBUG_REPORT_OBJECT_TYPE_FENCE_EXT,
5422 reinterpret_cast<uint64_t &>(fence), __LINE__, MEMTRACK_INVALID_FENCE_STATE, "MEM",
5423 "%s called for fence %#" PRIxLEAST64 " which has not been submitted on a Queue or during "
5424 "acquire next image.",
5425 apiCall, reinterpret_cast<uint64_t &>(fence));
5426 }
5427 } else {
Dustin Gravese3319182016-04-05 09:41:17 -06005428 pFenceInfo->second.firstTimeFlag = false;
Tobin Ehlis5b5e7bc2016-03-09 16:12:48 -07005429 }
5430 }
5431 return skipCall;
5432}
5433#endif
5434VK_LAYER_EXPORT VKAPI_ATTR VkResult VKAPI_CALL
5435vkWaitForFences(VkDevice device, uint32_t fenceCount, const VkFence *pFences, VkBool32 waitAll, uint64_t timeout) {
5436 layer_data *dev_data = get_my_data_ptr(get_dispatch_key(device), layer_data_map);
Dustin Gravese3319182016-04-05 09:41:17 -06005437 bool skip_call = false;
Mark Lobodzinski03b71512016-03-23 14:33:02 -06005438#if MTMERGESOURCE
Tobin Ehlis5b5e7bc2016-03-09 16:12:48 -07005439 // Verify fence status of submitted fences
Jeremy Hayesb9e99232016-04-13 16:20:24 -06005440 std::unique_lock<std::mutex> lock(global_lock);
Tobin Ehlis5b5e7bc2016-03-09 16:12:48 -07005441 for (uint32_t i = 0; i < fenceCount; i++) {
5442 skip_call |= verifyFenceStatus(device, pFences[i], "vkWaitForFences");
5443 }
Jeremy Hayesb9e99232016-04-13 16:20:24 -06005444 lock.unlock();
Tobin Ehlis5b5e7bc2016-03-09 16:12:48 -07005445 if (skip_call)
5446 return VK_ERROR_VALIDATION_FAILED_EXT;
5447#endif
5448 VkResult result = dev_data->device_dispatch_table->WaitForFences(device, fenceCount, pFences, waitAll, timeout);
5449
5450 if (result == VK_SUCCESS) {
Jeremy Hayesb9e99232016-04-13 16:20:24 -06005451 lock.lock();
Tobin Ehlis5b5e7bc2016-03-09 16:12:48 -07005452 // When we know that all fences are complete we can clean/remove their CBs
5453 if (waitAll || fenceCount == 1) {
5454 for (uint32_t i = 0; i < fenceCount; ++i) {
Mark Lobodzinski03b71512016-03-23 14:33:02 -06005455#if MTMERGESOURCE
Tobin Ehlis5b5e7bc2016-03-09 16:12:48 -07005456 update_fence_tracking(dev_data, pFences[i]);
5457#endif
5458 VkQueue fence_queue = dev_data->fenceMap[pFences[i]].queue;
5459 for (auto cmdBuffer : dev_data->fenceMap[pFences[i]].cmdBuffers) {
5460 skip_call |= cleanInFlightCmdBuffer(dev_data, cmdBuffer);
5461 removeInFlightCmdBuffer(dev_data, cmdBuffer, fence_queue);
5462 }
5463 }
5464 decrementResources(dev_data, fenceCount, pFences);
5465 }
5466 // NOTE : Alternate case not handled here is when some fences have completed. In
5467 // this case for app to guarantee which fences completed it will have to call
5468 // vkGetFenceStatus() at which point we'll clean/remove their CBs if complete.
Jeremy Hayesb9e99232016-04-13 16:20:24 -06005469 lock.unlock();
Tobin Ehlis5b5e7bc2016-03-09 16:12:48 -07005470 }
Dustin Gravese3319182016-04-05 09:41:17 -06005471 if (skip_call)
Tobin Ehlis5b5e7bc2016-03-09 16:12:48 -07005472 return VK_ERROR_VALIDATION_FAILED_EXT;
5473 return result;
5474}
5475
5476VK_LAYER_EXPORT VKAPI_ATTR VkResult VKAPI_CALL vkGetFenceStatus(VkDevice device, VkFence fence) {
5477 layer_data *dev_data = get_my_data_ptr(get_dispatch_key(device), layer_data_map);
5478 bool skipCall = false;
5479 VkResult result = VK_ERROR_VALIDATION_FAILED_EXT;
Mark Lobodzinski03b71512016-03-23 14:33:02 -06005480#if MTMERGESOURCE
Jeremy Hayesb9e99232016-04-13 16:20:24 -06005481 std::unique_lock<std::mutex> lock(global_lock);
Tobin Ehlis5b5e7bc2016-03-09 16:12:48 -07005482 skipCall = verifyFenceStatus(device, fence, "vkGetFenceStatus");
Jeremy Hayesb9e99232016-04-13 16:20:24 -06005483 lock.unlock();
Tobin Ehlis5b5e7bc2016-03-09 16:12:48 -07005484 if (skipCall)
5485 return result;
5486#endif
5487 result = dev_data->device_dispatch_table->GetFenceStatus(device, fence);
Dustin Gravese3319182016-04-05 09:41:17 -06005488 bool skip_call = false;
Jeremy Hayesb9e99232016-04-13 16:20:24 -06005489 lock.lock();
Tobin Ehlis5b5e7bc2016-03-09 16:12:48 -07005490 if (result == VK_SUCCESS) {
Mark Lobodzinski03b71512016-03-23 14:33:02 -06005491#if MTMERGESOURCE
Tobin Ehlis5b5e7bc2016-03-09 16:12:48 -07005492 update_fence_tracking(dev_data, fence);
5493#endif
5494 auto fence_queue = dev_data->fenceMap[fence].queue;
5495 for (auto cmdBuffer : dev_data->fenceMap[fence].cmdBuffers) {
5496 skip_call |= cleanInFlightCmdBuffer(dev_data, cmdBuffer);
5497 removeInFlightCmdBuffer(dev_data, cmdBuffer, fence_queue);
5498 }
5499 decrementResources(dev_data, 1, &fence);
5500 }
Jeremy Hayesb9e99232016-04-13 16:20:24 -06005501 lock.unlock();
Dustin Gravese3319182016-04-05 09:41:17 -06005502 if (skip_call)
Tobin Ehlis5b5e7bc2016-03-09 16:12:48 -07005503 return VK_ERROR_VALIDATION_FAILED_EXT;
5504 return result;
5505}
5506
Mark Lobodzinskib376eda2016-03-29 09:49:15 -06005507VK_LAYER_EXPORT VKAPI_ATTR void VKAPI_CALL vkGetDeviceQueue(VkDevice device, uint32_t queueFamilyIndex, uint32_t queueIndex,
5508 VkQueue *pQueue) {
Tobin Ehlis5b5e7bc2016-03-09 16:12:48 -07005509 layer_data *dev_data = get_my_data_ptr(get_dispatch_key(device), layer_data_map);
5510 dev_data->device_dispatch_table->GetDeviceQueue(device, queueFamilyIndex, queueIndex, pQueue);
Jeremy Hayesb9e99232016-04-13 16:20:24 -06005511 std::lock_guard<std::mutex> lock(global_lock);
Mark Lobodzinskib376eda2016-03-29 09:49:15 -06005512
5513 // Add queue to tracking set only if it is new
5514 auto result = dev_data->queues.emplace(*pQueue);
5515 if (result.second == true) {
5516 QUEUE_NODE *pQNode = &dev_data->queueMap[*pQueue];
5517 pQNode->device = device;
Mark Lobodzinski03b71512016-03-23 14:33:02 -06005518#if MTMERGESOURCE
Mark Lobodzinskib376eda2016-03-29 09:49:15 -06005519 pQNode->lastRetiredId = 0;
5520 pQNode->lastSubmittedId = 0;
Tobin Ehlis5b5e7bc2016-03-09 16:12:48 -07005521#endif
Mark Lobodzinskib376eda2016-03-29 09:49:15 -06005522 }
Tobin Ehlis5b5e7bc2016-03-09 16:12:48 -07005523}
5524
5525VK_LAYER_EXPORT VKAPI_ATTR VkResult VKAPI_CALL vkQueueWaitIdle(VkQueue queue) {
5526 layer_data *dev_data = get_my_data_ptr(get_dispatch_key(queue), layer_data_map);
5527 decrementResources(dev_data, queue);
Dustin Gravese3319182016-04-05 09:41:17 -06005528 bool skip_call = false;
Jeremy Hayesb9e99232016-04-13 16:20:24 -06005529 std::unique_lock<std::mutex> lock(global_lock);
Tobin Ehlis5b5e7bc2016-03-09 16:12:48 -07005530 // Iterate over local set since we erase set members as we go in for loop
5531 auto local_cb_set = dev_data->queueMap[queue].inFlightCmdBuffers;
5532 for (auto cmdBuffer : local_cb_set) {
5533 skip_call |= cleanInFlightCmdBuffer(dev_data, cmdBuffer);
5534 removeInFlightCmdBuffer(dev_data, cmdBuffer, queue);
5535 }
5536 dev_data->queueMap[queue].inFlightCmdBuffers.clear();
Jeremy Hayesb9e99232016-04-13 16:20:24 -06005537 lock.unlock();
Dustin Gravese3319182016-04-05 09:41:17 -06005538 if (skip_call)
Tobin Ehlis5b5e7bc2016-03-09 16:12:48 -07005539 return VK_ERROR_VALIDATION_FAILED_EXT;
5540 VkResult result = dev_data->device_dispatch_table->QueueWaitIdle(queue);
Mark Lobodzinski03b71512016-03-23 14:33:02 -06005541#if MTMERGESOURCE
Tobin Ehlis5b5e7bc2016-03-09 16:12:48 -07005542 if (VK_SUCCESS == result) {
Jeremy Hayesb9e99232016-04-13 16:20:24 -06005543 lock.lock();
Tobin Ehlis5b5e7bc2016-03-09 16:12:48 -07005544 retire_queue_fences(dev_data, queue);
Jeremy Hayesb9e99232016-04-13 16:20:24 -06005545 lock.unlock();
Tobin Ehlis5b5e7bc2016-03-09 16:12:48 -07005546 }
5547#endif
5548 return result;
5549}
5550
5551VK_LAYER_EXPORT VKAPI_ATTR VkResult VKAPI_CALL vkDeviceWaitIdle(VkDevice device) {
Dustin Gravese3319182016-04-05 09:41:17 -06005552 bool skip_call = false;
Tobin Ehlis5b5e7bc2016-03-09 16:12:48 -07005553 layer_data *dev_data = get_my_data_ptr(get_dispatch_key(device), layer_data_map);
Jeremy Hayesb9e99232016-04-13 16:20:24 -06005554 std::unique_lock<std::mutex> lock(global_lock);
Tobin Ehlis5b5e7bc2016-03-09 16:12:48 -07005555 for (auto queue : dev_data->queues) {
5556 decrementResources(dev_data, queue);
5557 if (dev_data->queueMap.find(queue) != dev_data->queueMap.end()) {
5558 // Clear all of the queue inFlightCmdBuffers (global set cleared below)
5559 dev_data->queueMap[queue].inFlightCmdBuffers.clear();
5560 }
5561 }
5562 for (auto cmdBuffer : dev_data->globalInFlightCmdBuffers) {
5563 skip_call |= cleanInFlightCmdBuffer(dev_data, cmdBuffer);
5564 }
5565 dev_data->globalInFlightCmdBuffers.clear();
Jeremy Hayesb9e99232016-04-13 16:20:24 -06005566 lock.unlock();
Dustin Gravese3319182016-04-05 09:41:17 -06005567 if (skip_call)
Tobin Ehlis5b5e7bc2016-03-09 16:12:48 -07005568 return VK_ERROR_VALIDATION_FAILED_EXT;
5569 VkResult result = dev_data->device_dispatch_table->DeviceWaitIdle(device);
Mark Lobodzinski03b71512016-03-23 14:33:02 -06005570#if MTMERGESOURCE
Tobin Ehlis5b5e7bc2016-03-09 16:12:48 -07005571 if (VK_SUCCESS == result) {
Jeremy Hayesb9e99232016-04-13 16:20:24 -06005572 lock.lock();
Tobin Ehlis5b5e7bc2016-03-09 16:12:48 -07005573 retire_device_fences(dev_data, device);
Jeremy Hayesb9e99232016-04-13 16:20:24 -06005574 lock.unlock();
Tobin Ehlis5b5e7bc2016-03-09 16:12:48 -07005575 }
5576#endif
5577 return result;
5578}
5579
5580VK_LAYER_EXPORT VKAPI_ATTR void VKAPI_CALL vkDestroyFence(VkDevice device, VkFence fence, const VkAllocationCallbacks *pAllocator) {
5581 layer_data *dev_data = get_my_data_ptr(get_dispatch_key(device), layer_data_map);
5582 bool skipCall = false;
Jeremy Hayesb9e99232016-04-13 16:20:24 -06005583 std::unique_lock<std::mutex> lock(global_lock);
Tobin Ehlis13443022016-04-12 10:49:41 -06005584 auto fence_pair = dev_data->fenceMap.find(fence);
5585 if (fence_pair != dev_data->fenceMap.end()) {
5586 if (fence_pair->second.in_use.load()) {
5587 skipCall |= log_msg(dev_data->report_data, VK_DEBUG_REPORT_ERROR_BIT_EXT, VK_DEBUG_REPORT_OBJECT_TYPE_FENCE_EXT,
5588 (uint64_t)(fence), __LINE__, DRAWSTATE_INVALID_FENCE, "DS",
5589 "Fence %#" PRIx64 " is in use by a command buffer.", (uint64_t)(fence));
5590 }
5591 dev_data->fenceMap.erase(fence_pair);
Tobin Ehlis5b5e7bc2016-03-09 16:12:48 -07005592 }
Jeremy Hayesb9e99232016-04-13 16:20:24 -06005593 lock.unlock();
Tobin Ehlis13443022016-04-12 10:49:41 -06005594
Tobin Ehlis5b5e7bc2016-03-09 16:12:48 -07005595 if (!skipCall)
5596 dev_data->device_dispatch_table->DestroyFence(device, fence, pAllocator);
5597}
5598
5599VK_LAYER_EXPORT VKAPI_ATTR void VKAPI_CALL
5600vkDestroySemaphore(VkDevice device, VkSemaphore semaphore, const VkAllocationCallbacks *pAllocator) {
5601 layer_data *dev_data = get_my_data_ptr(get_dispatch_key(device), layer_data_map);
5602 dev_data->device_dispatch_table->DestroySemaphore(device, semaphore, pAllocator);
Jeremy Hayesb9e99232016-04-13 16:20:24 -06005603 std::lock_guard<std::mutex> lock(global_lock);
Tobin Ehlis5b5e7bc2016-03-09 16:12:48 -07005604 auto item = dev_data->semaphoreMap.find(semaphore);
5605 if (item != dev_data->semaphoreMap.end()) {
5606 if (item->second.in_use.load()) {
5607 log_msg(dev_data->report_data, VK_DEBUG_REPORT_ERROR_BIT_EXT, VK_DEBUG_REPORT_OBJECT_TYPE_SEMAPHORE_EXT,
5608 reinterpret_cast<uint64_t &>(semaphore), __LINE__, DRAWSTATE_INVALID_SEMAPHORE, "DS",
5609 "Cannot delete semaphore %" PRIx64 " which is in use.", reinterpret_cast<uint64_t &>(semaphore));
5610 }
5611 dev_data->semaphoreMap.erase(semaphore);
5612 }
Tobin Ehlis5b5e7bc2016-03-09 16:12:48 -07005613 // TODO : Clean up any internal data structures using this obj.
5614}
5615
5616VK_LAYER_EXPORT VKAPI_ATTR void VKAPI_CALL vkDestroyEvent(VkDevice device, VkEvent event, const VkAllocationCallbacks *pAllocator) {
5617 layer_data *dev_data = get_my_data_ptr(get_dispatch_key(device), layer_data_map);
5618 bool skip_call = false;
Jeremy Hayesb9e99232016-04-13 16:20:24 -06005619 std::unique_lock<std::mutex> lock(global_lock);
Tobin Ehlis5b5e7bc2016-03-09 16:12:48 -07005620 auto event_data = dev_data->eventMap.find(event);
5621 if (event_data != dev_data->eventMap.end()) {
5622 if (event_data->second.in_use.load()) {
5623 skip_call |= log_msg(
5624 dev_data->report_data, VK_DEBUG_REPORT_ERROR_BIT_EXT, VK_DEBUG_REPORT_OBJECT_TYPE_DESCRIPTOR_SET_EXT,
5625 reinterpret_cast<uint64_t &>(event), __LINE__, DRAWSTATE_INVALID_EVENT, "DS",
5626 "Cannot delete event %" PRIx64 " which is in use by a command buffer.", reinterpret_cast<uint64_t &>(event));
5627 }
5628 dev_data->eventMap.erase(event_data);
5629 }
Jeremy Hayesb9e99232016-04-13 16:20:24 -06005630 lock.unlock();
Tobin Ehlis5b5e7bc2016-03-09 16:12:48 -07005631 if (!skip_call)
5632 dev_data->device_dispatch_table->DestroyEvent(device, event, pAllocator);
5633 // TODO : Clean up any internal data structures using this obj.
5634}
5635
5636VK_LAYER_EXPORT VKAPI_ATTR void VKAPI_CALL
5637vkDestroyQueryPool(VkDevice device, VkQueryPool queryPool, const VkAllocationCallbacks *pAllocator) {
5638 get_my_data_ptr(get_dispatch_key(device), layer_data_map)
5639 ->device_dispatch_table->DestroyQueryPool(device, queryPool, pAllocator);
5640 // TODO : Clean up any internal data structures using this obj.
5641}
5642
5643VKAPI_ATTR VkResult VKAPI_CALL vkGetQueryPoolResults(VkDevice device, VkQueryPool queryPool, uint32_t firstQuery,
5644 uint32_t queryCount, size_t dataSize, void *pData, VkDeviceSize stride,
5645 VkQueryResultFlags flags) {
5646 layer_data *dev_data = get_my_data_ptr(get_dispatch_key(device), layer_data_map);
5647 unordered_map<QueryObject, vector<VkCommandBuffer>> queriesInFlight;
5648 GLOBAL_CB_NODE *pCB = nullptr;
Jeremy Hayesb9e99232016-04-13 16:20:24 -06005649 std::unique_lock<std::mutex> lock(global_lock);
Tobin Ehlis5b5e7bc2016-03-09 16:12:48 -07005650 for (auto cmdBuffer : dev_data->globalInFlightCmdBuffers) {
5651 pCB = getCBNode(dev_data, cmdBuffer);
5652 for (auto queryStatePair : pCB->queryToStateMap) {
5653 queriesInFlight[queryStatePair.first].push_back(cmdBuffer);
5654 }
5655 }
Dustin Gravese3319182016-04-05 09:41:17 -06005656 bool skip_call = false;
Tobin Ehlis5b5e7bc2016-03-09 16:12:48 -07005657 for (uint32_t i = 0; i < queryCount; ++i) {
5658 QueryObject query = {queryPool, firstQuery + i};
5659 auto queryElement = queriesInFlight.find(query);
5660 auto queryToStateElement = dev_data->queryToStateMap.find(query);
5661 if (queryToStateElement != dev_data->queryToStateMap.end()) {
Mark Lobodzinskiec27ab72016-04-01 15:58:32 -06005662 // Available and in flight
5663 if (queryElement != queriesInFlight.end() && queryToStateElement != dev_data->queryToStateMap.end() &&
5664 queryToStateElement->second) {
5665 for (auto cmdBuffer : queryElement->second) {
5666 pCB = getCBNode(dev_data, cmdBuffer);
5667 auto queryEventElement = pCB->waitedEventsBeforeQueryReset.find(query);
5668 if (queryEventElement == pCB->waitedEventsBeforeQueryReset.end()) {
5669 skip_call |= log_msg(dev_data->report_data, VK_DEBUG_REPORT_ERROR_BIT_EXT,
5670 VK_DEBUG_REPORT_OBJECT_TYPE_QUERY_POOL_EXT, 0, __LINE__, DRAWSTATE_INVALID_QUERY, "DS",
5671 "Cannot get query results on queryPool %" PRIu64 " with index %d which is in flight.",
5672 (uint64_t)(queryPool), firstQuery + i);
5673 } else {
5674 for (auto event : queryEventElement->second) {
5675 dev_data->eventMap[event].needsSignaled = true;
5676 }
Tobin Ehlis5b5e7bc2016-03-09 16:12:48 -07005677 }
5678 }
Mark Lobodzinskiec27ab72016-04-01 15:58:32 -06005679 // Unavailable and in flight
5680 } else if (queryElement != queriesInFlight.end() && queryToStateElement != dev_data->queryToStateMap.end() &&
5681 !queryToStateElement->second) {
5682 // TODO : Can there be the same query in use by multiple command buffers in flight?
5683 bool make_available = false;
5684 for (auto cmdBuffer : queryElement->second) {
5685 pCB = getCBNode(dev_data, cmdBuffer);
5686 make_available |= pCB->queryToStateMap[query];
5687 }
5688 if (!(((flags & VK_QUERY_RESULT_PARTIAL_BIT) || (flags & VK_QUERY_RESULT_WAIT_BIT)) && make_available)) {
5689 skip_call |= log_msg(dev_data->report_data, VK_DEBUG_REPORT_ERROR_BIT_EXT,
5690 VK_DEBUG_REPORT_OBJECT_TYPE_QUERY_POOL_EXT, 0, __LINE__, DRAWSTATE_INVALID_QUERY, "DS",
5691 "Cannot get query results on queryPool %" PRIu64 " with index %d which is unavailable.",
5692 (uint64_t)(queryPool), firstQuery + i);
5693 }
5694 // Unavailable
5695 } else if (queryToStateElement != dev_data->queryToStateMap.end() && !queryToStateElement->second) {
Tobin Ehlis5b5e7bc2016-03-09 16:12:48 -07005696 skip_call |= log_msg(dev_data->report_data, VK_DEBUG_REPORT_ERROR_BIT_EXT,
5697 VK_DEBUG_REPORT_OBJECT_TYPE_QUERY_POOL_EXT, 0, __LINE__, DRAWSTATE_INVALID_QUERY, "DS",
5698 "Cannot get query results on queryPool %" PRIu64 " with index %d which is unavailable.",
5699 (uint64_t)(queryPool), firstQuery + i);
Mark Lobodzinskiec27ab72016-04-01 15:58:32 -06005700 // Unitialized
5701 } else if (queryToStateElement == dev_data->queryToStateMap.end()) {
5702 skip_call |= log_msg(dev_data->report_data, VK_DEBUG_REPORT_ERROR_BIT_EXT,
5703 VK_DEBUG_REPORT_OBJECT_TYPE_QUERY_POOL_EXT, 0, __LINE__, DRAWSTATE_INVALID_QUERY, "DS",
5704 "Cannot get query results on queryPool %" PRIu64
5705 " with index %d as data has not been collected for this index.",
5706 (uint64_t)(queryPool), firstQuery + i);
Tobin Ehlis5b5e7bc2016-03-09 16:12:48 -07005707 }
Tobin Ehlis5b5e7bc2016-03-09 16:12:48 -07005708 }
5709 }
Jeremy Hayesb9e99232016-04-13 16:20:24 -06005710 lock.unlock();
Tobin Ehlis5b5e7bc2016-03-09 16:12:48 -07005711 if (skip_call)
5712 return VK_ERROR_VALIDATION_FAILED_EXT;
5713 return dev_data->device_dispatch_table->GetQueryPoolResults(device, queryPool, firstQuery, queryCount, dataSize, pData, stride,
5714 flags);
5715}
5716
Dustin Gravese3319182016-04-05 09:41:17 -06005717static bool validateIdleBuffer(const layer_data *my_data, VkBuffer buffer) {
5718 bool skip_call = false;
Tobin Ehlis5b5e7bc2016-03-09 16:12:48 -07005719 auto buffer_data = my_data->bufferMap.find(buffer);
5720 if (buffer_data == my_data->bufferMap.end()) {
5721 skip_call |= log_msg(my_data->report_data, VK_DEBUG_REPORT_ERROR_BIT_EXT, VK_DEBUG_REPORT_OBJECT_TYPE_BUFFER_EXT,
5722 (uint64_t)(buffer), __LINE__, DRAWSTATE_DOUBLE_DESTROY, "DS",
5723 "Cannot free buffer %" PRIxLEAST64 " that has not been allocated.", (uint64_t)(buffer));
5724 } else {
5725 if (buffer_data->second.in_use.load()) {
5726 skip_call |= log_msg(my_data->report_data, VK_DEBUG_REPORT_ERROR_BIT_EXT, VK_DEBUG_REPORT_OBJECT_TYPE_BUFFER_EXT,
5727 (uint64_t)(buffer), __LINE__, DRAWSTATE_OBJECT_INUSE, "DS",
5728 "Cannot free buffer %" PRIxLEAST64 " that is in use by a command buffer.", (uint64_t)(buffer));
5729 }
5730 }
5731 return skip_call;
5732}
5733
5734VK_LAYER_EXPORT VKAPI_ATTR void VKAPI_CALL
5735vkDestroyBuffer(VkDevice device, VkBuffer buffer, const VkAllocationCallbacks *pAllocator) {
5736 layer_data *dev_data = get_my_data_ptr(get_dispatch_key(device), layer_data_map);
Dustin Gravese3319182016-04-05 09:41:17 -06005737 bool skipCall = false;
Jeremy Hayesb9e99232016-04-13 16:20:24 -06005738 std::unique_lock<std::mutex> lock(global_lock);
Dustin Gravese3319182016-04-05 09:41:17 -06005739 if (!validateIdleBuffer(dev_data, buffer) && !skipCall) {
Jeremy Hayesb9e99232016-04-13 16:20:24 -06005740 lock.unlock();
Tobin Ehlis5b5e7bc2016-03-09 16:12:48 -07005741 dev_data->device_dispatch_table->DestroyBuffer(device, buffer, pAllocator);
Jeremy Hayesb9e99232016-04-13 16:20:24 -06005742 lock.lock();
Tobin Ehlis5b5e7bc2016-03-09 16:12:48 -07005743 }
5744 dev_data->bufferMap.erase(buffer);
Tobin Ehlis5b5e7bc2016-03-09 16:12:48 -07005745}
5746
5747VK_LAYER_EXPORT VKAPI_ATTR void VKAPI_CALL
5748vkDestroyBufferView(VkDevice device, VkBufferView bufferView, const VkAllocationCallbacks *pAllocator) {
5749 layer_data *dev_data = get_my_data_ptr(get_dispatch_key(device), layer_data_map);
5750 dev_data->device_dispatch_table->DestroyBufferView(device, bufferView, pAllocator);
Jeremy Hayesb9e99232016-04-13 16:20:24 -06005751 std::lock_guard<std::mutex> lock(global_lock);
Tobin Ehlis5b5e7bc2016-03-09 16:12:48 -07005752 auto item = dev_data->bufferViewMap.find(bufferView);
5753 if (item != dev_data->bufferViewMap.end()) {
5754 dev_data->bufferViewMap.erase(item);
5755 }
Tobin Ehlis5b5e7bc2016-03-09 16:12:48 -07005756}
5757
5758VK_LAYER_EXPORT VKAPI_ATTR void VKAPI_CALL vkDestroyImage(VkDevice device, VkImage image, const VkAllocationCallbacks *pAllocator) {
5759 layer_data *dev_data = get_my_data_ptr(get_dispatch_key(device), layer_data_map);
Dustin Gravese3319182016-04-05 09:41:17 -06005760 bool skipCall = false;
Dustin Gravese3319182016-04-05 09:41:17 -06005761 if (!skipCall)
Tobin Ehlis5b5e7bc2016-03-09 16:12:48 -07005762 dev_data->device_dispatch_table->DestroyImage(device, image, pAllocator);
5763
Jeremy Hayesb9e99232016-04-13 16:20:24 -06005764 std::lock_guard<std::mutex> lock(global_lock);
Tobin Ehlis5b5e7bc2016-03-09 16:12:48 -07005765 const auto& entry = dev_data->imageMap.find(image);
5766 if (entry != dev_data->imageMap.end()) {
Tobin Ehlis58070a62016-03-16 16:00:36 -06005767 // Clear any memory mapping for this image
Tobin Ehlis94c53c02016-04-05 13:33:00 -06005768 auto mem_entry = dev_data->memObjMap.find(entry->second.mem);
Tobin Ehlis58070a62016-03-16 16:00:36 -06005769 if (mem_entry != dev_data->memObjMap.end())
5770 mem_entry->second.image = VK_NULL_HANDLE;
5771
5772 // Remove image from imageMap
Tobin Ehlis5b5e7bc2016-03-09 16:12:48 -07005773 dev_data->imageMap.erase(entry);
5774 }
5775 const auto& subEntry = dev_data->imageSubresourceMap.find(image);
5776 if (subEntry != dev_data->imageSubresourceMap.end()) {
5777 for (const auto& pair : subEntry->second) {
5778 dev_data->imageLayoutMap.erase(pair);
5779 }
5780 dev_data->imageSubresourceMap.erase(subEntry);
5781 }
Tobin Ehlis5b5e7bc2016-03-09 16:12:48 -07005782}
Mark Lobodzinski03b71512016-03-23 14:33:02 -06005783#if MTMERGESOURCE
Dustin Gravese3319182016-04-05 09:41:17 -06005784static bool print_memory_range_error(layer_data *dev_data, const uint64_t object_handle, const uint64_t other_handle,
5785 VkDebugReportObjectTypeEXT object_type) {
Tobin Ehlis5b5e7bc2016-03-09 16:12:48 -07005786 if (object_type == VK_DEBUG_REPORT_OBJECT_TYPE_BUFFER_EXT) {
Tobin Ehlis58070a62016-03-16 16:00:36 -06005787 return log_msg(dev_data->report_data, VK_DEBUG_REPORT_ERROR_BIT_EXT, object_type, object_handle, 0,
Tobin Ehlis5b5e7bc2016-03-09 16:12:48 -07005788 MEMTRACK_INVALID_ALIASING, "MEM", "Buffer %" PRIx64 " is alised with image %" PRIx64, object_handle,
5789 other_handle);
5790 } else {
Tobin Ehlis58070a62016-03-16 16:00:36 -06005791 return log_msg(dev_data->report_data, VK_DEBUG_REPORT_ERROR_BIT_EXT, object_type, object_handle, 0,
Tobin Ehlis5b5e7bc2016-03-09 16:12:48 -07005792 MEMTRACK_INVALID_ALIASING, "MEM", "Image %" PRIx64 " is alised with buffer %" PRIx64, object_handle,
5793 other_handle);
5794 }
5795}
5796
Dustin Gravese3319182016-04-05 09:41:17 -06005797static bool validate_memory_range(layer_data *dev_data, const vector<MEMORY_RANGE> &ranges, const MEMORY_RANGE &new_range,
5798 VkDebugReportObjectTypeEXT object_type) {
5799 bool skip_call = false;
Tobin Ehlis58070a62016-03-16 16:00:36 -06005800
Tobin Ehlis5b5e7bc2016-03-09 16:12:48 -07005801 for (auto range : ranges) {
Tobin Ehlisb1b606d2016-04-11 14:49:55 -06005802 if ((range.end & ~(dev_data->phys_dev_properties.properties.limits.bufferImageGranularity - 1)) <
5803 (new_range.start & ~(dev_data->phys_dev_properties.properties.limits.bufferImageGranularity - 1)))
Tobin Ehlis5b5e7bc2016-03-09 16:12:48 -07005804 continue;
Tobin Ehlisb1b606d2016-04-11 14:49:55 -06005805 if ((range.start & ~(dev_data->phys_dev_properties.properties.limits.bufferImageGranularity - 1)) >
5806 (new_range.end & ~(dev_data->phys_dev_properties.properties.limits.bufferImageGranularity - 1)))
Tobin Ehlis5b5e7bc2016-03-09 16:12:48 -07005807 continue;
Tobin Ehlis58070a62016-03-16 16:00:36 -06005808 skip_call |= print_memory_range_error(dev_data, new_range.handle, range.handle, object_type);
Tobin Ehlis5b5e7bc2016-03-09 16:12:48 -07005809 }
5810 return skip_call;
5811}
5812
Dustin Gravese3319182016-04-05 09:41:17 -06005813static bool validate_buffer_image_aliasing(layer_data *dev_data, uint64_t handle, VkDeviceMemory mem, VkDeviceSize memoryOffset,
5814 VkMemoryRequirements memRequirements, vector<MEMORY_RANGE> &ranges,
5815 const vector<MEMORY_RANGE> &other_ranges, VkDebugReportObjectTypeEXT object_type) {
Tobin Ehlis5b5e7bc2016-03-09 16:12:48 -07005816 MEMORY_RANGE range;
5817 range.handle = handle;
5818 range.memory = mem;
5819 range.start = memoryOffset;
5820 range.end = memoryOffset + memRequirements.size - 1;
Tobin Ehlis58070a62016-03-16 16:00:36 -06005821 ranges.push_back(range);
5822 return validate_memory_range(dev_data, other_ranges, range, object_type);
Tobin Ehlis5b5e7bc2016-03-09 16:12:48 -07005823}
5824
5825VK_LAYER_EXPORT VKAPI_ATTR VkResult VKAPI_CALL
5826vkBindBufferMemory(VkDevice device, VkBuffer buffer, VkDeviceMemory mem, VkDeviceSize memoryOffset) {
Tobin Ehlis58070a62016-03-16 16:00:36 -06005827 layer_data *dev_data = get_my_data_ptr(get_dispatch_key(device), layer_data_map);
Tobin Ehlis5b5e7bc2016-03-09 16:12:48 -07005828 VkResult result = VK_ERROR_VALIDATION_FAILED_EXT;
Jeremy Hayesb9e99232016-04-13 16:20:24 -06005829 std::unique_lock<std::mutex> lock(global_lock);
Tobin Ehlis5b5e7bc2016-03-09 16:12:48 -07005830 // Track objects tied to memory
5831 uint64_t buffer_handle = (uint64_t)(buffer);
Dustin Gravese3319182016-04-05 09:41:17 -06005832 bool skipCall =
Tobin Ehlis94c53c02016-04-05 13:33:00 -06005833 set_mem_binding(dev_data, mem, buffer_handle, VK_DEBUG_REPORT_OBJECT_TYPE_BUFFER_EXT, "vkBindBufferMemory");
5834 auto buffer_node = dev_data->bufferMap.find(buffer);
5835 if (buffer_node != dev_data->bufferMap.end()) {
5836 buffer_node->second.mem = mem;
Tobin Ehlis5b5e7bc2016-03-09 16:12:48 -07005837 VkMemoryRequirements memRequirements;
Tobin Ehlis94c53c02016-04-05 13:33:00 -06005838 dev_data->device_dispatch_table->GetBufferMemoryRequirements(device, buffer, &memRequirements);
Tobin Ehlis58070a62016-03-16 16:00:36 -06005839 skipCall |= validate_buffer_image_aliasing(dev_data, buffer_handle, mem, memoryOffset, memRequirements,
5840 dev_data->memObjMap[mem].bufferRanges, dev_data->memObjMap[mem].imageRanges,
5841 VK_DEBUG_REPORT_OBJECT_TYPE_BUFFER_EXT);
Dustin Graves2c905c82016-03-31 18:01:37 -06005842 // Validate memory requirements alignment
5843 if (vk_safe_modulo(memoryOffset, memRequirements.alignment) != 0) {
5844 skipCall |=
5845 log_msg(dev_data->report_data, VK_DEBUG_REPORT_ERROR_BIT_EXT, VK_DEBUG_REPORT_OBJECT_TYPE_PHYSICAL_DEVICE_EXT, 0,
5846 __LINE__, DRAWSTATE_INVALID_BUFFER_MEMORY_OFFSET, "DS",
5847 "vkBindBufferMemory(): memoryOffset is %#" PRIxLEAST64 " but must be an integer multiple of the "
5848 "VkMemoryRequirements::alignment value %#" PRIxLEAST64
5849 ", returned from a call to vkGetBufferMemoryRequirements with buffer",
5850 memoryOffset, memRequirements.alignment);
5851 }
5852 // Validate device limits alignments
Tobin Ehlis94c53c02016-04-05 13:33:00 -06005853 VkBufferUsageFlags usage = dev_data->bufferMap[buffer].createInfo.usage;
Dustin Graves2c905c82016-03-31 18:01:37 -06005854 if (usage & (VK_BUFFER_USAGE_UNIFORM_TEXEL_BUFFER_BIT | VK_BUFFER_USAGE_STORAGE_TEXEL_BUFFER_BIT)) {
Tobin Ehlisb1b606d2016-04-11 14:49:55 -06005855 if (vk_safe_modulo(memoryOffset, dev_data->phys_dev_properties.properties.limits.minTexelBufferOffsetAlignment) != 0) {
Dustin Graves2c905c82016-03-31 18:01:37 -06005856 skipCall |=
5857 log_msg(dev_data->report_data, VK_DEBUG_REPORT_ERROR_BIT_EXT, VK_DEBUG_REPORT_OBJECT_TYPE_PHYSICAL_DEVICE_EXT,
5858 0, __LINE__, DRAWSTATE_INVALID_TEXEL_BUFFER_OFFSET, "DS",
5859 "vkBindBufferMemory(): memoryOffset is %#" PRIxLEAST64 " but must be a multiple of "
5860 "device limit minTexelBufferOffsetAlignment %#" PRIxLEAST64,
Tobin Ehlisb1b606d2016-04-11 14:49:55 -06005861 memoryOffset, dev_data->phys_dev_properties.properties.limits.minTexelBufferOffsetAlignment);
Dustin Graves2c905c82016-03-31 18:01:37 -06005862 }
5863 }
5864 if (usage & VK_BUFFER_USAGE_UNIFORM_BUFFER_BIT) {
Tobin Ehlisb1b606d2016-04-11 14:49:55 -06005865 if (vk_safe_modulo(memoryOffset, dev_data->phys_dev_properties.properties.limits.minUniformBufferOffsetAlignment) !=
5866 0) {
Dustin Graves2c905c82016-03-31 18:01:37 -06005867 skipCall |=
5868 log_msg(dev_data->report_data, VK_DEBUG_REPORT_ERROR_BIT_EXT, VK_DEBUG_REPORT_OBJECT_TYPE_PHYSICAL_DEVICE_EXT,
5869 0, __LINE__, DRAWSTATE_INVALID_UNIFORM_BUFFER_OFFSET, "DS",
5870 "vkBindBufferMemory(): memoryOffset is %#" PRIxLEAST64 " but must be a multiple of "
5871 "device limit minUniformBufferOffsetAlignment %#" PRIxLEAST64,
Tobin Ehlisb1b606d2016-04-11 14:49:55 -06005872 memoryOffset, dev_data->phys_dev_properties.properties.limits.minUniformBufferOffsetAlignment);
Dustin Graves2c905c82016-03-31 18:01:37 -06005873 }
5874 }
5875 if (usage & VK_BUFFER_USAGE_STORAGE_BUFFER_BIT) {
Tobin Ehlisb1b606d2016-04-11 14:49:55 -06005876 if (vk_safe_modulo(memoryOffset, dev_data->phys_dev_properties.properties.limits.minStorageBufferOffsetAlignment) !=
5877 0) {
Dustin Graves2c905c82016-03-31 18:01:37 -06005878 skipCall |=
5879 log_msg(dev_data->report_data, VK_DEBUG_REPORT_ERROR_BIT_EXT, VK_DEBUG_REPORT_OBJECT_TYPE_PHYSICAL_DEVICE_EXT,
5880 0, __LINE__, DRAWSTATE_INVALID_STORAGE_BUFFER_OFFSET, "DS",
5881 "vkBindBufferMemory(): memoryOffset is %#" PRIxLEAST64 " but must be a multiple of "
5882 "device limit minStorageBufferOffsetAlignment %#" PRIxLEAST64,
Tobin Ehlisb1b606d2016-04-11 14:49:55 -06005883 memoryOffset, dev_data->phys_dev_properties.properties.limits.minStorageBufferOffsetAlignment);
Dustin Graves2c905c82016-03-31 18:01:37 -06005884 }
5885 }
Tobin Ehlis5b5e7bc2016-03-09 16:12:48 -07005886 }
Chris Forbes0a1ce3d2016-04-06 15:16:26 +12005887 print_mem_list(dev_data);
Jeremy Hayesb9e99232016-04-13 16:20:24 -06005888 lock.unlock();
Dustin Gravese3319182016-04-05 09:41:17 -06005889 if (!skipCall) {
Tobin Ehlis58070a62016-03-16 16:00:36 -06005890 result = dev_data->device_dispatch_table->BindBufferMemory(device, buffer, mem, memoryOffset);
Tobin Ehlis5b5e7bc2016-03-09 16:12:48 -07005891 }
5892 return result;
5893}
5894
5895VK_LAYER_EXPORT VKAPI_ATTR void VKAPI_CALL
5896vkGetBufferMemoryRequirements(VkDevice device, VkBuffer buffer, VkMemoryRequirements *pMemoryRequirements) {
5897 layer_data *my_data = get_my_data_ptr(get_dispatch_key(device), layer_data_map);
5898 // TODO : What to track here?
5899 // Could potentially save returned mem requirements and validate values passed into BindBufferMemory
5900 my_data->device_dispatch_table->GetBufferMemoryRequirements(device, buffer, pMemoryRequirements);
5901}
5902
5903VK_LAYER_EXPORT VKAPI_ATTR void VKAPI_CALL
5904vkGetImageMemoryRequirements(VkDevice device, VkImage image, VkMemoryRequirements *pMemoryRequirements) {
5905 layer_data *my_data = get_my_data_ptr(get_dispatch_key(device), layer_data_map);
5906 // TODO : What to track here?
5907 // Could potentially save returned mem requirements and validate values passed into BindImageMemory
5908 my_data->device_dispatch_table->GetImageMemoryRequirements(device, image, pMemoryRequirements);
5909}
5910#endif
5911VK_LAYER_EXPORT VKAPI_ATTR void VKAPI_CALL
5912vkDestroyImageView(VkDevice device, VkImageView imageView, const VkAllocationCallbacks *pAllocator) {
5913 get_my_data_ptr(get_dispatch_key(device), layer_data_map)
5914 ->device_dispatch_table->DestroyImageView(device, imageView, pAllocator);
5915 // TODO : Clean up any internal data structures using this obj.
5916}
5917
5918VK_LAYER_EXPORT VKAPI_ATTR void VKAPI_CALL
5919vkDestroyShaderModule(VkDevice device, VkShaderModule shaderModule, const VkAllocationCallbacks *pAllocator) {
Chris Forbes918c2832016-03-18 16:30:03 +13005920 layer_data *my_data = get_my_data_ptr(get_dispatch_key(device), layer_data_map);
5921
Jeremy Hayesb9e99232016-04-13 16:20:24 -06005922 std::unique_lock<std::mutex> lock(global_lock);
Chris Forbes918c2832016-03-18 16:30:03 +13005923 my_data->shaderModuleMap.erase(shaderModule);
Jeremy Hayesb9e99232016-04-13 16:20:24 -06005924 lock.unlock();
Chris Forbes918c2832016-03-18 16:30:03 +13005925
5926 my_data->device_dispatch_table->DestroyShaderModule(device, shaderModule, pAllocator);
Tobin Ehlis5b5e7bc2016-03-09 16:12:48 -07005927}
5928
5929VK_LAYER_EXPORT VKAPI_ATTR void VKAPI_CALL
5930vkDestroyPipeline(VkDevice device, VkPipeline pipeline, const VkAllocationCallbacks *pAllocator) {
5931 get_my_data_ptr(get_dispatch_key(device), layer_data_map)->device_dispatch_table->DestroyPipeline(device, pipeline, pAllocator);
5932 // TODO : Clean up any internal data structures using this obj.
5933}
5934
5935VK_LAYER_EXPORT VKAPI_ATTR void VKAPI_CALL
5936vkDestroyPipelineLayout(VkDevice device, VkPipelineLayout pipelineLayout, const VkAllocationCallbacks *pAllocator) {
5937 get_my_data_ptr(get_dispatch_key(device), layer_data_map)
5938 ->device_dispatch_table->DestroyPipelineLayout(device, pipelineLayout, pAllocator);
5939 // TODO : Clean up any internal data structures using this obj.
5940}
5941
5942VK_LAYER_EXPORT VKAPI_ATTR void VKAPI_CALL
5943vkDestroySampler(VkDevice device, VkSampler sampler, const VkAllocationCallbacks *pAllocator) {
5944 get_my_data_ptr(get_dispatch_key(device), layer_data_map)->device_dispatch_table->DestroySampler(device, sampler, pAllocator);
5945 // TODO : Clean up any internal data structures using this obj.
5946}
5947
5948VK_LAYER_EXPORT VKAPI_ATTR void VKAPI_CALL
5949vkDestroyDescriptorSetLayout(VkDevice device, VkDescriptorSetLayout descriptorSetLayout, const VkAllocationCallbacks *pAllocator) {
5950 get_my_data_ptr(get_dispatch_key(device), layer_data_map)
5951 ->device_dispatch_table->DestroyDescriptorSetLayout(device, descriptorSetLayout, pAllocator);
5952 // TODO : Clean up any internal data structures using this obj.
5953}
5954
5955VK_LAYER_EXPORT VKAPI_ATTR void VKAPI_CALL
5956vkDestroyDescriptorPool(VkDevice device, VkDescriptorPool descriptorPool, const VkAllocationCallbacks *pAllocator) {
5957 get_my_data_ptr(get_dispatch_key(device), layer_data_map)
5958 ->device_dispatch_table->DestroyDescriptorPool(device, descriptorPool, pAllocator);
5959 // TODO : Clean up any internal data structures using this obj.
5960}
Tobin Ehlisbc300ac2016-04-14 12:22:03 -06005961// Verify cmdBuffer in given cb_node is not in global in-flight set, and return skip_call result
5962// If this is a secondary command buffer, then make sure its primary is also in-flight
5963// If primary is not in-flight, then remove secondary from global in-flight set
5964// This function is only valid at a point when cmdBuffer is being reset or freed
5965static bool checkAndClearCommandBufferInFlight(layer_data *dev_data, const GLOBAL_CB_NODE *cb_node, const char *action) {
5966 bool skip_call = false;
5967 if (dev_data->globalInFlightCmdBuffers.count(cb_node->commandBuffer)) {
5968 // Primary CB or secondary where primary is also in-flight is an error
5969 if ((cb_node->createInfo.level != VK_COMMAND_BUFFER_LEVEL_SECONDARY) ||
5970 (dev_data->globalInFlightCmdBuffers.count(cb_node->primaryCommandBuffer))) {
5971 skip_call |= log_msg(
5972 dev_data->report_data, VK_DEBUG_REPORT_ERROR_BIT_EXT, VK_DEBUG_REPORT_OBJECT_TYPE_COMMAND_BUFFER_EXT,
5973 reinterpret_cast<const uint64_t &>(cb_node->commandBuffer), __LINE__, DRAWSTATE_INVALID_COMMAND_BUFFER_RESET, "DS",
5974 "Attempt to %s command buffer (%#" PRIxLEAST64 ") which is in use.", action,
5975 reinterpret_cast<const uint64_t &>(cb_node->commandBuffer));
5976 } else { // Secondary CB w/o primary in-flight, remove from in-flight
5977 dev_data->globalInFlightCmdBuffers.erase(cb_node->commandBuffer);
5978 }
5979 }
5980 return skip_call;
5981}
5982// Iterate over all cmdBuffers in given commandPool and verify that each is not in use
5983static bool checkAndClearCommandBuffersInFlight(layer_data *dev_data, const VkCommandPool commandPool, const char *action) {
5984 bool skip_call = false;
5985 auto pool_data = dev_data->commandPoolMap.find(commandPool);
5986 if (pool_data != dev_data->commandPoolMap.end()) {
5987 for (auto cmd_buffer : pool_data->second.commandBuffers) {
5988 if (dev_data->globalInFlightCmdBuffers.count(cmd_buffer)) {
5989 skip_call |= checkAndClearCommandBufferInFlight(dev_data, getCBNode(dev_data, cmd_buffer), action);
5990 }
5991 }
5992 }
5993 return skip_call;
5994}
Tobin Ehlis5b5e7bc2016-03-09 16:12:48 -07005995
5996VK_LAYER_EXPORT VKAPI_ATTR void VKAPI_CALL
5997vkFreeCommandBuffers(VkDevice device, VkCommandPool commandPool, uint32_t commandBufferCount, const VkCommandBuffer *pCommandBuffers) {
5998 layer_data *dev_data = get_my_data_ptr(get_dispatch_key(device), layer_data_map);
5999
6000 bool skip_call = false;
Jeremy Hayesb9e99232016-04-13 16:20:24 -06006001 std::unique_lock<std::mutex> lock(global_lock);
Tobin Ehlis5b5e7bc2016-03-09 16:12:48 -07006002 for (uint32_t i = 0; i < commandBufferCount; i++) {
Tobin Ehlisbc300ac2016-04-14 12:22:03 -06006003 auto cb_pair = dev_data->commandBufferMap.find(pCommandBuffers[i]);
6004 skip_call |= checkAndClearCommandBufferInFlight(dev_data, cb_pair->second, "free");
Tobin Ehlis5b5e7bc2016-03-09 16:12:48 -07006005 // Delete CB information structure, and remove from commandBufferMap
Tobin Ehlisbc300ac2016-04-14 12:22:03 -06006006 if (cb_pair != dev_data->commandBufferMap.end()) {
Tobin Ehlis5b5e7bc2016-03-09 16:12:48 -07006007 // reset prior to delete for data clean-up
Tobin Ehlisbc300ac2016-04-14 12:22:03 -06006008 resetCB(dev_data, (*cb_pair).second->commandBuffer);
6009 delete (*cb_pair).second;
6010 dev_data->commandBufferMap.erase(cb_pair);
Tobin Ehlis5b5e7bc2016-03-09 16:12:48 -07006011 }
6012
6013 // Remove commandBuffer reference from commandPoolMap
6014 dev_data->commandPoolMap[commandPool].commandBuffers.remove(pCommandBuffers[i]);
6015 }
Mark Lobodzinski03b71512016-03-23 14:33:02 -06006016#if MTMERGESOURCE
Chris Forbes0a1ce3d2016-04-06 15:16:26 +12006017 printCBList(dev_data);
Tobin Ehlis5b5e7bc2016-03-09 16:12:48 -07006018#endif
Jeremy Hayesb9e99232016-04-13 16:20:24 -06006019 lock.unlock();
Tobin Ehlis5b5e7bc2016-03-09 16:12:48 -07006020
6021 if (!skip_call)
6022 dev_data->device_dispatch_table->FreeCommandBuffers(device, commandPool, commandBufferCount, pCommandBuffers);
6023}
6024
6025VK_LAYER_EXPORT VKAPI_ATTR VkResult VKAPI_CALL vkCreateCommandPool(VkDevice device, const VkCommandPoolCreateInfo *pCreateInfo,
6026 const VkAllocationCallbacks *pAllocator,
6027 VkCommandPool *pCommandPool) {
6028 layer_data *dev_data = get_my_data_ptr(get_dispatch_key(device), layer_data_map);
6029
6030 VkResult result = dev_data->device_dispatch_table->CreateCommandPool(device, pCreateInfo, pAllocator, pCommandPool);
6031
6032 if (VK_SUCCESS == result) {
Jeremy Hayesb9e99232016-04-13 16:20:24 -06006033 std::lock_guard<std::mutex> lock(global_lock);
Tobin Ehlis5b5e7bc2016-03-09 16:12:48 -07006034 dev_data->commandPoolMap[*pCommandPool].createFlags = pCreateInfo->flags;
6035 dev_data->commandPoolMap[*pCommandPool].queueFamilyIndex = pCreateInfo->queueFamilyIndex;
Tobin Ehlis5b5e7bc2016-03-09 16:12:48 -07006036 }
6037 return result;
6038}
6039
6040VK_LAYER_EXPORT VKAPI_ATTR VkResult VKAPI_CALL vkCreateQueryPool(VkDevice device, const VkQueryPoolCreateInfo *pCreateInfo,
6041 const VkAllocationCallbacks *pAllocator, VkQueryPool *pQueryPool) {
6042
6043 layer_data *dev_data = get_my_data_ptr(get_dispatch_key(device), layer_data_map);
6044 VkResult result = dev_data->device_dispatch_table->CreateQueryPool(device, pCreateInfo, pAllocator, pQueryPool);
6045 if (result == VK_SUCCESS) {
Jeremy Hayesb9e99232016-04-13 16:20:24 -06006046 std::lock_guard<std::mutex> lock(global_lock);
Tobin Ehlis5b5e7bc2016-03-09 16:12:48 -07006047 dev_data->queryPoolMap[*pQueryPool].createInfo = *pCreateInfo;
Tobin Ehlis5b5e7bc2016-03-09 16:12:48 -07006048 }
6049 return result;
6050}
6051
Tobin Ehlis5b5e7bc2016-03-09 16:12:48 -07006052// Destroy commandPool along with all of the commandBuffers allocated from that pool
6053VK_LAYER_EXPORT VKAPI_ATTR void VKAPI_CALL
6054vkDestroyCommandPool(VkDevice device, VkCommandPool commandPool, const VkAllocationCallbacks *pAllocator) {
6055 layer_data *dev_data = get_my_data_ptr(get_dispatch_key(device), layer_data_map);
Tobin Ehlis5b5e7bc2016-03-09 16:12:48 -07006056 bool skipCall = false;
Jeremy Hayesb9e99232016-04-13 16:20:24 -06006057 std::unique_lock<std::mutex> lock(global_lock);
Tobin Ehlis5b5e7bc2016-03-09 16:12:48 -07006058 // Verify that command buffers in pool are complete (not in-flight)
Tobin Ehlisbc300ac2016-04-14 12:22:03 -06006059 VkBool32 result = checkAndClearCommandBuffersInFlight(dev_data, commandPool, "destroy command pool with");
Tobin Ehlis5b5e7bc2016-03-09 16:12:48 -07006060 // Must remove cmdpool from cmdpoolmap, after removing all cmdbuffers in its list from the commandPoolMap
6061 if (dev_data->commandPoolMap.find(commandPool) != dev_data->commandPoolMap.end()) {
6062 for (auto poolCb = dev_data->commandPoolMap[commandPool].commandBuffers.begin();
6063 poolCb != dev_data->commandPoolMap[commandPool].commandBuffers.end();) {
Tobin Ehlis400cff92016-04-11 16:39:29 -06006064 clear_cmd_buf_and_mem_references(dev_data, *poolCb);
Tobin Ehlis5b5e7bc2016-03-09 16:12:48 -07006065 auto del_cb = dev_data->commandBufferMap.find(*poolCb);
6066 delete (*del_cb).second; // delete CB info structure
Tobin Ehlis72d66f02016-03-21 14:14:44 -06006067 dev_data->commandBufferMap.erase(del_cb); // Remove this command buffer
Tobin Ehlis5b5e7bc2016-03-09 16:12:48 -07006068 poolCb = dev_data->commandPoolMap[commandPool].commandBuffers.erase(
6069 poolCb); // Remove CB reference from commandPoolMap's list
6070 }
6071 }
6072 dev_data->commandPoolMap.erase(commandPool);
6073
Jeremy Hayesb9e99232016-04-13 16:20:24 -06006074 lock.unlock();
Tobin Ehlis5b5e7bc2016-03-09 16:12:48 -07006075
Tony Barbourf9083082016-04-06 18:17:57 -06006076 if (result)
Tobin Ehlis5b5e7bc2016-03-09 16:12:48 -07006077 return;
6078
6079 if (!skipCall)
6080 dev_data->device_dispatch_table->DestroyCommandPool(device, commandPool, pAllocator);
Tobin Ehlis5b5e7bc2016-03-09 16:12:48 -07006081}
6082
6083VK_LAYER_EXPORT VKAPI_ATTR VkResult VKAPI_CALL
6084vkResetCommandPool(VkDevice device, VkCommandPool commandPool, VkCommandPoolResetFlags flags) {
6085 layer_data *dev_data = get_my_data_ptr(get_dispatch_key(device), layer_data_map);
Tobin Ehlis5b5e7bc2016-03-09 16:12:48 -07006086 bool skipCall = false;
6087 VkResult result = VK_ERROR_VALIDATION_FAILED_EXT;
Tobin Ehlis400cff92016-04-11 16:39:29 -06006088
Tobin Ehlisbc300ac2016-04-14 12:22:03 -06006089 if (checkAndClearCommandBuffersInFlight(dev_data, commandPool, "reset command pool with"))
Tobin Ehlis5b5e7bc2016-03-09 16:12:48 -07006090 return VK_ERROR_VALIDATION_FAILED_EXT;
6091
6092 if (!skipCall)
6093 result = dev_data->device_dispatch_table->ResetCommandPool(device, commandPool, flags);
6094
6095 // Reset all of the CBs allocated from this pool
6096 if (VK_SUCCESS == result) {
Jeremy Hayesb9e99232016-04-13 16:20:24 -06006097 std::lock_guard<std::mutex> lock(global_lock);
Tobin Ehlis5b5e7bc2016-03-09 16:12:48 -07006098 auto it = dev_data->commandPoolMap[commandPool].commandBuffers.begin();
6099 while (it != dev_data->commandPoolMap[commandPool].commandBuffers.end()) {
6100 resetCB(dev_data, (*it));
6101 ++it;
6102 }
Tobin Ehlis5b5e7bc2016-03-09 16:12:48 -07006103 }
6104 return result;
6105}
6106
6107VK_LAYER_EXPORT VKAPI_ATTR VkResult VKAPI_CALL vkResetFences(VkDevice device, uint32_t fenceCount, const VkFence *pFences) {
6108 layer_data *dev_data = get_my_data_ptr(get_dispatch_key(device), layer_data_map);
6109 VkResult result = VK_ERROR_VALIDATION_FAILED_EXT;
6110 bool skipCall = false;
Jeremy Hayesb9e99232016-04-13 16:20:24 -06006111 std::unique_lock<std::mutex> lock(global_lock);
Tobin Ehlis5b5e7bc2016-03-09 16:12:48 -07006112 for (uint32_t i = 0; i < fenceCount; ++i) {
Mark Lobodzinski03b71512016-03-23 14:33:02 -06006113#if MTMERGESOURCE
Tobin Ehlis5b5e7bc2016-03-09 16:12:48 -07006114 // Reset fence state in fenceCreateInfo structure
6115 // MTMTODO : Merge with code below
6116 auto fence_item = dev_data->fenceMap.find(pFences[i]);
6117 if (fence_item != dev_data->fenceMap.end()) {
6118 // Validate fences in SIGNALED state
6119 if (!(fence_item->second.createInfo.flags & VK_FENCE_CREATE_SIGNALED_BIT)) {
6120 // TODO: I don't see a Valid Usage section for ResetFences. This behavior should be documented there.
6121 skipCall = log_msg(dev_data->report_data, VK_DEBUG_REPORT_WARNING_BIT_EXT, VK_DEBUG_REPORT_OBJECT_TYPE_FENCE_EXT,
6122 (uint64_t)pFences[i], __LINE__, MEMTRACK_INVALID_FENCE_STATE, "MEM",
6123 "Fence %#" PRIxLEAST64 " submitted to VkResetFences in UNSIGNALED STATE", (uint64_t)pFences[i]);
6124 } else {
6125 fence_item->second.createInfo.flags =
6126 static_cast<VkFenceCreateFlags>(fence_item->second.createInfo.flags & ~VK_FENCE_CREATE_SIGNALED_BIT);
6127 }
6128 }
6129#endif
6130 if (dev_data->fenceMap[pFences[i]].in_use.load()) {
6131 skipCall |=
6132 log_msg(dev_data->report_data, VK_DEBUG_REPORT_ERROR_BIT_EXT, VK_DEBUG_REPORT_OBJECT_TYPE_FENCE_EXT,
6133 reinterpret_cast<const uint64_t &>(pFences[i]), __LINE__, DRAWSTATE_INVALID_FENCE, "DS",
6134 "Fence %#" PRIx64 " is in use by a command buffer.", reinterpret_cast<const uint64_t &>(pFences[i]));
6135 }
6136 }
Jeremy Hayesb9e99232016-04-13 16:20:24 -06006137 lock.unlock();
Tobin Ehlis5b5e7bc2016-03-09 16:12:48 -07006138 if (!skipCall)
6139 result = dev_data->device_dispatch_table->ResetFences(device, fenceCount, pFences);
6140 return result;
6141}
6142
6143VK_LAYER_EXPORT VKAPI_ATTR void VKAPI_CALL
6144vkDestroyFramebuffer(VkDevice device, VkFramebuffer framebuffer, const VkAllocationCallbacks *pAllocator) {
6145 layer_data *dev_data = get_my_data_ptr(get_dispatch_key(device), layer_data_map);
Jeremy Hayesb9e99232016-04-13 16:20:24 -06006146 std::unique_lock<std::mutex> lock(global_lock);
Tobin Ehlis5b5e7bc2016-03-09 16:12:48 -07006147 auto fbNode = dev_data->frameBufferMap.find(framebuffer);
6148 if (fbNode != dev_data->frameBufferMap.end()) {
6149 for (auto cb : fbNode->second.referencingCmdBuffers) {
6150 auto cbNode = dev_data->commandBufferMap.find(cb);
6151 if (cbNode != dev_data->commandBufferMap.end()) {
6152 // Set CB as invalid and record destroyed framebuffer
6153 cbNode->second->state = CB_INVALID;
6154 cbNode->second->destroyedFramebuffers.insert(framebuffer);
6155 }
6156 }
Chris Forbesf05c35f2016-03-31 16:13:36 +13006157 delete [] fbNode->second.createInfo.pAttachments;
6158 dev_data->frameBufferMap.erase(fbNode);
Tobin Ehlis5b5e7bc2016-03-09 16:12:48 -07006159 }
Jeremy Hayesb9e99232016-04-13 16:20:24 -06006160 lock.unlock();
Tobin Ehlis5b5e7bc2016-03-09 16:12:48 -07006161 dev_data->device_dispatch_table->DestroyFramebuffer(device, framebuffer, pAllocator);
6162}
6163
6164VK_LAYER_EXPORT VKAPI_ATTR void VKAPI_CALL
6165vkDestroyRenderPass(VkDevice device, VkRenderPass renderPass, const VkAllocationCallbacks *pAllocator) {
6166 layer_data *dev_data = get_my_data_ptr(get_dispatch_key(device), layer_data_map);
6167 dev_data->device_dispatch_table->DestroyRenderPass(device, renderPass, pAllocator);
Jeremy Hayesb9e99232016-04-13 16:20:24 -06006168 std::lock_guard<std::mutex> lock(global_lock);
Tobin Ehlis5b5e7bc2016-03-09 16:12:48 -07006169 dev_data->renderPassMap.erase(renderPass);
Tobin Ehlis5b5e7bc2016-03-09 16:12:48 -07006170}
6171
Mark Lobodzinski47892022016-03-22 10:07:26 -06006172VK_LAYER_EXPORT VKAPI_ATTR VkResult VKAPI_CALL vkCreateBuffer(VkDevice device, const VkBufferCreateInfo *pCreateInfo,
6173 const VkAllocationCallbacks *pAllocator, VkBuffer *pBuffer) {
Tobin Ehlis5b5e7bc2016-03-09 16:12:48 -07006174 layer_data *dev_data = get_my_data_ptr(get_dispatch_key(device), layer_data_map);
Mark Lobodzinski47892022016-03-22 10:07:26 -06006175
6176 VkResult result = dev_data->device_dispatch_table->CreateBuffer(device, pCreateInfo, pAllocator, pBuffer);
Tobin Ehlis5b5e7bc2016-03-09 16:12:48 -07006177
6178 if (VK_SUCCESS == result) {
Jeremy Hayesb9e99232016-04-13 16:20:24 -06006179 std::lock_guard<std::mutex> lock(global_lock);
Tobin Ehlis5b5e7bc2016-03-09 16:12:48 -07006180 // TODO : This doesn't create deep copy of pQueueFamilyIndices so need to fix that if/when we want that data to be valid
Tobin Ehlis94c53c02016-04-05 13:33:00 -06006181 dev_data->bufferMap[*pBuffer].createInfo = *pCreateInfo;
Tobin Ehlis5b5e7bc2016-03-09 16:12:48 -07006182 dev_data->bufferMap[*pBuffer].in_use.store(0);
Tobin Ehlis5b5e7bc2016-03-09 16:12:48 -07006183 }
6184 return result;
6185}
6186
6187VK_LAYER_EXPORT VKAPI_ATTR VkResult VKAPI_CALL vkCreateBufferView(VkDevice device, const VkBufferViewCreateInfo *pCreateInfo,
6188 const VkAllocationCallbacks *pAllocator, VkBufferView *pView) {
6189 layer_data *dev_data = get_my_data_ptr(get_dispatch_key(device), layer_data_map);
6190 VkResult result = dev_data->device_dispatch_table->CreateBufferView(device, pCreateInfo, pAllocator, pView);
6191 if (VK_SUCCESS == result) {
Jeremy Hayesb9e99232016-04-13 16:20:24 -06006192 std::lock_guard<std::mutex> lock(global_lock);
Tobin Ehlis5b5e7bc2016-03-09 16:12:48 -07006193 dev_data->bufferViewMap[*pView] = VkBufferViewCreateInfo(*pCreateInfo);
Mark Lobodzinski03b71512016-03-23 14:33:02 -06006194#if MTMERGESOURCE
Tobin Ehlis5b5e7bc2016-03-09 16:12:48 -07006195 // In order to create a valid buffer view, the buffer must have been created with at least one of the
6196 // following flags: UNIFORM_TEXEL_BUFFER_BIT or STORAGE_TEXEL_BUFFER_BIT
Chris Forbes0a1ce3d2016-04-06 15:16:26 +12006197 validate_buffer_usage_flags(dev_data, pCreateInfo->buffer,
Dustin Gravese3319182016-04-05 09:41:17 -06006198 VK_BUFFER_USAGE_UNIFORM_TEXEL_BUFFER_BIT | VK_BUFFER_USAGE_STORAGE_TEXEL_BUFFER_BIT, false,
Tobin Ehlis5b5e7bc2016-03-09 16:12:48 -07006199 "vkCreateBufferView()", "VK_BUFFER_USAGE_[STORAGE|UNIFORM]_TEXEL_BUFFER_BIT");
6200#endif
Tobin Ehlis5b5e7bc2016-03-09 16:12:48 -07006201 }
6202 return result;
6203}
6204
Mark Lobodzinski47892022016-03-22 10:07:26 -06006205VK_LAYER_EXPORT VKAPI_ATTR VkResult VKAPI_CALL vkCreateImage(VkDevice device, const VkImageCreateInfo *pCreateInfo,
6206 const VkAllocationCallbacks *pAllocator, VkImage *pImage) {
Tobin Ehlis5b5e7bc2016-03-09 16:12:48 -07006207 layer_data *dev_data = get_my_data_ptr(get_dispatch_key(device), layer_data_map);
Mark Lobodzinski47892022016-03-22 10:07:26 -06006208
6209 VkResult result = dev_data->device_dispatch_table->CreateImage(device, pCreateInfo, pAllocator, pImage);
Tobin Ehlis5b5e7bc2016-03-09 16:12:48 -07006210
6211 if (VK_SUCCESS == result) {
Jeremy Hayesb9e99232016-04-13 16:20:24 -06006212 std::lock_guard<std::mutex> lock(global_lock);
Tobin Ehlis5b5e7bc2016-03-09 16:12:48 -07006213 IMAGE_LAYOUT_NODE image_node;
6214 image_node.layout = pCreateInfo->initialLayout;
6215 image_node.format = pCreateInfo->format;
Tobin Ehlis5b5e7bc2016-03-09 16:12:48 -07006216 dev_data->imageMap[*pImage].createInfo = *pCreateInfo;
6217 ImageSubresourcePair subpair = {*pImage, false, VkImageSubresource()};
6218 dev_data->imageSubresourceMap[*pImage].push_back(subpair);
6219 dev_data->imageLayoutMap[subpair] = image_node;
Tobin Ehlis5b5e7bc2016-03-09 16:12:48 -07006220 }
6221 return result;
6222}
6223
6224static void ResolveRemainingLevelsLayers(layer_data *dev_data, VkImageSubresourceRange *range, VkImage image) {
Jeremy Hayesb9e99232016-04-13 16:20:24 -06006225 /* expects global_lock to be held by caller */
Tobin Ehlis5b5e7bc2016-03-09 16:12:48 -07006226
6227 auto image_node_it = dev_data->imageMap.find(image);
6228 if (image_node_it != dev_data->imageMap.end()) {
6229 /* If the caller used the special values VK_REMAINING_MIP_LEVELS and
6230 * VK_REMAINING_ARRAY_LAYERS, resolve them now in our internal state to
6231 * the actual values.
6232 */
6233 if (range->levelCount == VK_REMAINING_MIP_LEVELS) {
6234 range->levelCount = image_node_it->second.createInfo.mipLevels - range->baseMipLevel;
6235 }
6236
6237 if (range->layerCount == VK_REMAINING_ARRAY_LAYERS) {
6238 range->layerCount = image_node_it->second.createInfo.arrayLayers - range->baseArrayLayer;
6239 }
6240 }
6241}
6242
6243// Return the correct layer/level counts if the caller used the special
6244// values VK_REMAINING_MIP_LEVELS or VK_REMAINING_ARRAY_LAYERS.
6245static void ResolveRemainingLevelsLayers(layer_data *dev_data, uint32_t *levels, uint32_t *layers, VkImageSubresourceRange range,
6246 VkImage image) {
Jeremy Hayesb9e99232016-04-13 16:20:24 -06006247 /* expects global_lock to be held by caller */
Tobin Ehlis5b5e7bc2016-03-09 16:12:48 -07006248
6249 *levels = range.levelCount;
6250 *layers = range.layerCount;
6251 auto image_node_it = dev_data->imageMap.find(image);
6252 if (image_node_it != dev_data->imageMap.end()) {
6253 if (range.levelCount == VK_REMAINING_MIP_LEVELS) {
6254 *levels = image_node_it->second.createInfo.mipLevels - range.baseMipLevel;
6255 }
6256 if (range.layerCount == VK_REMAINING_ARRAY_LAYERS) {
6257 *layers = image_node_it->second.createInfo.arrayLayers - range.baseArrayLayer;
6258 }
6259 }
6260}
6261
6262VK_LAYER_EXPORT VKAPI_ATTR VkResult VKAPI_CALL vkCreateImageView(VkDevice device, const VkImageViewCreateInfo *pCreateInfo,
6263 const VkAllocationCallbacks *pAllocator, VkImageView *pView) {
6264 layer_data *dev_data = get_my_data_ptr(get_dispatch_key(device), layer_data_map);
6265 VkResult result = dev_data->device_dispatch_table->CreateImageView(device, pCreateInfo, pAllocator, pView);
6266 if (VK_SUCCESS == result) {
Jeremy Hayesb9e99232016-04-13 16:20:24 -06006267 std::lock_guard<std::mutex> lock(global_lock);
Tobin Ehlis5b5e7bc2016-03-09 16:12:48 -07006268 VkImageViewCreateInfo localCI = VkImageViewCreateInfo(*pCreateInfo);
6269 ResolveRemainingLevelsLayers(dev_data, &localCI.subresourceRange, pCreateInfo->image);
6270 dev_data->imageViewMap[*pView] = localCI;
Mark Lobodzinski03b71512016-03-23 14:33:02 -06006271#if MTMERGESOURCE
Tobin Ehlis5b5e7bc2016-03-09 16:12:48 -07006272 // Validate that img has correct usage flags set
Chris Forbes0a1ce3d2016-04-06 15:16:26 +12006273 validate_image_usage_flags(dev_data, pCreateInfo->image,
Tobin Ehlis5b5e7bc2016-03-09 16:12:48 -07006274 VK_IMAGE_USAGE_SAMPLED_BIT | VK_IMAGE_USAGE_STORAGE_BIT |
6275 VK_IMAGE_USAGE_DEPTH_STENCIL_ATTACHMENT_BIT | VK_IMAGE_USAGE_COLOR_ATTACHMENT_BIT,
Dustin Gravese3319182016-04-05 09:41:17 -06006276 false, "vkCreateImageView()", "VK_IMAGE_USAGE_[SAMPLED|STORAGE|COLOR_ATTACHMENT]_BIT");
Tobin Ehlis5b5e7bc2016-03-09 16:12:48 -07006277#endif
Tobin Ehlis5b5e7bc2016-03-09 16:12:48 -07006278 }
6279 return result;
6280}
6281
6282VK_LAYER_EXPORT VKAPI_ATTR VkResult VKAPI_CALL
6283vkCreateFence(VkDevice device, const VkFenceCreateInfo *pCreateInfo, const VkAllocationCallbacks *pAllocator, VkFence *pFence) {
6284 layer_data *dev_data = get_my_data_ptr(get_dispatch_key(device), layer_data_map);
6285 VkResult result = dev_data->device_dispatch_table->CreateFence(device, pCreateInfo, pAllocator, pFence);
6286 if (VK_SUCCESS == result) {
Jeremy Hayesb9e99232016-04-13 16:20:24 -06006287 std::lock_guard<std::mutex> lock(global_lock);
Tobin Ehlis5b5e7bc2016-03-09 16:12:48 -07006288 FENCE_NODE *pFN = &dev_data->fenceMap[*pFence];
Mark Lobodzinski03b71512016-03-23 14:33:02 -06006289#if MTMERGESOURCE
Tobin Ehlis5b5e7bc2016-03-09 16:12:48 -07006290 memset(pFN, 0, sizeof(MT_FENCE_INFO));
6291 memcpy(&(pFN->createInfo), pCreateInfo, sizeof(VkFenceCreateInfo));
6292 if (pCreateInfo->flags & VK_FENCE_CREATE_SIGNALED_BIT) {
Dustin Gravese3319182016-04-05 09:41:17 -06006293 pFN->firstTimeFlag = true;
Tobin Ehlis5b5e7bc2016-03-09 16:12:48 -07006294 }
6295#endif
6296 pFN->in_use.store(0);
Tobin Ehlis5b5e7bc2016-03-09 16:12:48 -07006297 }
6298 return result;
6299}
6300
6301// TODO handle pipeline caches
6302VKAPI_ATTR VkResult VKAPI_CALL vkCreatePipelineCache(VkDevice device, const VkPipelineCacheCreateInfo *pCreateInfo,
6303 const VkAllocationCallbacks *pAllocator, VkPipelineCache *pPipelineCache) {
6304 layer_data *dev_data = get_my_data_ptr(get_dispatch_key(device), layer_data_map);
6305 VkResult result = dev_data->device_dispatch_table->CreatePipelineCache(device, pCreateInfo, pAllocator, pPipelineCache);
6306 return result;
6307}
6308
6309VKAPI_ATTR void VKAPI_CALL
6310vkDestroyPipelineCache(VkDevice device, VkPipelineCache pipelineCache, const VkAllocationCallbacks *pAllocator) {
6311 layer_data *dev_data = get_my_data_ptr(get_dispatch_key(device), layer_data_map);
6312 dev_data->device_dispatch_table->DestroyPipelineCache(device, pipelineCache, pAllocator);
6313}
6314
6315VKAPI_ATTR VkResult VKAPI_CALL
6316vkGetPipelineCacheData(VkDevice device, VkPipelineCache pipelineCache, size_t *pDataSize, void *pData) {
6317 layer_data *dev_data = get_my_data_ptr(get_dispatch_key(device), layer_data_map);
6318 VkResult result = dev_data->device_dispatch_table->GetPipelineCacheData(device, pipelineCache, pDataSize, pData);
6319 return result;
6320}
6321
6322VKAPI_ATTR VkResult VKAPI_CALL
6323vkMergePipelineCaches(VkDevice device, VkPipelineCache dstCache, uint32_t srcCacheCount, const VkPipelineCache *pSrcCaches) {
6324 layer_data *dev_data = get_my_data_ptr(get_dispatch_key(device), layer_data_map);
6325 VkResult result = dev_data->device_dispatch_table->MergePipelineCaches(device, dstCache, srcCacheCount, pSrcCaches);
6326 return result;
6327}
6328
Tobin Ehlis3d3e06b2016-03-28 11:18:19 -06006329// utility function to set collective state for pipeline
6330void set_pipeline_state(PIPELINE_NODE *pPipe) {
6331 // If any attachment used by this pipeline has blendEnable, set top-level blendEnable
6332 if (pPipe->graphicsPipelineCI.pColorBlendState) {
6333 for (size_t i = 0; i < pPipe->attachments.size(); ++i) {
6334 if (VK_TRUE == pPipe->attachments[i].blendEnable) {
6335 if (((pPipe->attachments[i].dstAlphaBlendFactor >= VK_BLEND_FACTOR_CONSTANT_COLOR) &&
6336 (pPipe->attachments[i].dstAlphaBlendFactor <= VK_BLEND_FACTOR_ONE_MINUS_CONSTANT_ALPHA)) ||
6337 ((pPipe->attachments[i].dstColorBlendFactor >= VK_BLEND_FACTOR_CONSTANT_COLOR) &&
6338 (pPipe->attachments[i].dstColorBlendFactor <= VK_BLEND_FACTOR_ONE_MINUS_CONSTANT_ALPHA)) ||
6339 ((pPipe->attachments[i].srcAlphaBlendFactor >= VK_BLEND_FACTOR_CONSTANT_COLOR) &&
6340 (pPipe->attachments[i].srcAlphaBlendFactor <= VK_BLEND_FACTOR_ONE_MINUS_CONSTANT_ALPHA)) ||
6341 ((pPipe->attachments[i].srcColorBlendFactor >= VK_BLEND_FACTOR_CONSTANT_COLOR) &&
6342 (pPipe->attachments[i].srcColorBlendFactor <= VK_BLEND_FACTOR_ONE_MINUS_CONSTANT_ALPHA))) {
6343 pPipe->blendConstantsEnabled = true;
6344 }
6345 }
6346 }
6347 }
6348}
6349
Tobin Ehlis5b5e7bc2016-03-09 16:12:48 -07006350VK_LAYER_EXPORT VKAPI_ATTR VkResult VKAPI_CALL
6351vkCreateGraphicsPipelines(VkDevice device, VkPipelineCache pipelineCache, uint32_t count,
6352 const VkGraphicsPipelineCreateInfo *pCreateInfos, const VkAllocationCallbacks *pAllocator,
6353 VkPipeline *pPipelines) {
6354 VkResult result = VK_SUCCESS;
6355 // TODO What to do with pipelineCache?
6356 // The order of operations here is a little convoluted but gets the job done
6357 // 1. Pipeline create state is first shadowed into PIPELINE_NODE struct
6358 // 2. Create state is then validated (which uses flags setup during shadowing)
6359 // 3. If everything looks good, we'll then create the pipeline and add NODE to pipelineMap
Dustin Gravese3319182016-04-05 09:41:17 -06006360 bool skipCall = false;
Tobin Ehlis5b5e7bc2016-03-09 16:12:48 -07006361 // TODO : Improve this data struct w/ unique_ptrs so cleanup below is automatic
6362 vector<PIPELINE_NODE *> pPipeNode(count);
6363 layer_data *dev_data = get_my_data_ptr(get_dispatch_key(device), layer_data_map);
6364
6365 uint32_t i = 0;
Jeremy Hayesb9e99232016-04-13 16:20:24 -06006366 std::unique_lock<std::mutex> lock(global_lock);
Tobin Ehlis5b5e7bc2016-03-09 16:12:48 -07006367
6368 for (i = 0; i < count; i++) {
Tobin Ehlisca546212016-04-01 13:51:33 -06006369 pPipeNode[i] = new PIPELINE_NODE;
6370 pPipeNode[i]->initGraphicsPipeline(&pCreateInfos[i]);
Tobin Ehlis5b5e7bc2016-03-09 16:12:48 -07006371 skipCall |= verifyPipelineCreateState(dev_data, device, pPipeNode, i);
6372 }
6373
Dustin Gravese3319182016-04-05 09:41:17 -06006374 if (!skipCall) {
Jeremy Hayesb9e99232016-04-13 16:20:24 -06006375 lock.unlock();
Tobin Ehlis5b5e7bc2016-03-09 16:12:48 -07006376 result = dev_data->device_dispatch_table->CreateGraphicsPipelines(device, pipelineCache, count, pCreateInfos, pAllocator,
6377 pPipelines);
Jeremy Hayesb9e99232016-04-13 16:20:24 -06006378 lock.lock();
Tobin Ehlis5b5e7bc2016-03-09 16:12:48 -07006379 for (i = 0; i < count; i++) {
6380 pPipeNode[i]->pipeline = pPipelines[i];
6381 dev_data->pipelineMap[pPipeNode[i]->pipeline] = pPipeNode[i];
6382 }
Jeremy Hayesb9e99232016-04-13 16:20:24 -06006383 lock.unlock();
Tobin Ehlis5b5e7bc2016-03-09 16:12:48 -07006384 } else {
6385 for (i = 0; i < count; i++) {
Chris Forbes5188e312016-03-18 11:07:59 +13006386 delete pPipeNode[i];
Tobin Ehlis5b5e7bc2016-03-09 16:12:48 -07006387 }
Jeremy Hayesb9e99232016-04-13 16:20:24 -06006388 lock.unlock();
Tobin Ehlis5b5e7bc2016-03-09 16:12:48 -07006389 return VK_ERROR_VALIDATION_FAILED_EXT;
6390 }
6391 return result;
6392}
6393
6394VK_LAYER_EXPORT VKAPI_ATTR VkResult VKAPI_CALL
6395vkCreateComputePipelines(VkDevice device, VkPipelineCache pipelineCache, uint32_t count,
6396 const VkComputePipelineCreateInfo *pCreateInfos, const VkAllocationCallbacks *pAllocator,
6397 VkPipeline *pPipelines) {
6398 VkResult result = VK_SUCCESS;
Dustin Gravese3319182016-04-05 09:41:17 -06006399 bool skipCall = false;
Tobin Ehlis5b5e7bc2016-03-09 16:12:48 -07006400
6401 // TODO : Improve this data struct w/ unique_ptrs so cleanup below is automatic
6402 vector<PIPELINE_NODE *> pPipeNode(count);
6403 layer_data *dev_data = get_my_data_ptr(get_dispatch_key(device), layer_data_map);
6404
6405 uint32_t i = 0;
Jeremy Hayesb9e99232016-04-13 16:20:24 -06006406 std::unique_lock<std::mutex> lock(global_lock);
Tobin Ehlis5b5e7bc2016-03-09 16:12:48 -07006407 for (i = 0; i < count; i++) {
6408 // TODO: Verify compute stage bits
6409
6410 // Create and initialize internal tracking data structure
6411 pPipeNode[i] = new PIPELINE_NODE;
Tobin Ehlisca546212016-04-01 13:51:33 -06006412 pPipeNode[i]->initComputePipeline(&pCreateInfos[i]);
6413 // memcpy(&pPipeNode[i]->computePipelineCI, (const void *)&pCreateInfos[i], sizeof(VkComputePipelineCreateInfo));
Tobin Ehlis5b5e7bc2016-03-09 16:12:48 -07006414
6415 // TODO: Add Compute Pipeline Verification
6416 // skipCall |= verifyPipelineCreateState(dev_data, device, pPipeNode[i]);
6417 }
6418
Dustin Gravese3319182016-04-05 09:41:17 -06006419 if (!skipCall) {
Jeremy Hayesb9e99232016-04-13 16:20:24 -06006420 lock.unlock();
Tobin Ehlis5b5e7bc2016-03-09 16:12:48 -07006421 result = dev_data->device_dispatch_table->CreateComputePipelines(device, pipelineCache, count, pCreateInfos, pAllocator,
6422 pPipelines);
Jeremy Hayesb9e99232016-04-13 16:20:24 -06006423 lock.lock();
Tobin Ehlis5b5e7bc2016-03-09 16:12:48 -07006424 for (i = 0; i < count; i++) {
6425 pPipeNode[i]->pipeline = pPipelines[i];
6426 dev_data->pipelineMap[pPipeNode[i]->pipeline] = pPipeNode[i];
6427 }
Jeremy Hayesb9e99232016-04-13 16:20:24 -06006428 lock.unlock();
Tobin Ehlis5b5e7bc2016-03-09 16:12:48 -07006429 } else {
6430 for (i = 0; i < count; i++) {
6431 // Clean up any locally allocated data structures
6432 delete pPipeNode[i];
6433 }
Jeremy Hayesb9e99232016-04-13 16:20:24 -06006434 lock.unlock();
Tobin Ehlis5b5e7bc2016-03-09 16:12:48 -07006435 return VK_ERROR_VALIDATION_FAILED_EXT;
6436 }
6437 return result;
6438}
6439
6440VK_LAYER_EXPORT VKAPI_ATTR VkResult VKAPI_CALL vkCreateSampler(VkDevice device, const VkSamplerCreateInfo *pCreateInfo,
6441 const VkAllocationCallbacks *pAllocator, VkSampler *pSampler) {
6442 layer_data *dev_data = get_my_data_ptr(get_dispatch_key(device), layer_data_map);
6443 VkResult result = dev_data->device_dispatch_table->CreateSampler(device, pCreateInfo, pAllocator, pSampler);
6444 if (VK_SUCCESS == result) {
Jeremy Hayesb9e99232016-04-13 16:20:24 -06006445 std::lock_guard<std::mutex> lock(global_lock);
Tobin Ehlis5b5e7bc2016-03-09 16:12:48 -07006446 dev_data->sampleMap[*pSampler] = unique_ptr<SAMPLER_NODE>(new SAMPLER_NODE(pSampler, pCreateInfo));
Tobin Ehlis5b5e7bc2016-03-09 16:12:48 -07006447 }
6448 return result;
6449}
6450
6451VK_LAYER_EXPORT VKAPI_ATTR VkResult VKAPI_CALL
6452vkCreateDescriptorSetLayout(VkDevice device, const VkDescriptorSetLayoutCreateInfo *pCreateInfo,
6453 const VkAllocationCallbacks *pAllocator, VkDescriptorSetLayout *pSetLayout) {
6454 layer_data *dev_data = get_my_data_ptr(get_dispatch_key(device), layer_data_map);
6455 VkResult result = dev_data->device_dispatch_table->CreateDescriptorSetLayout(device, pCreateInfo, pAllocator, pSetLayout);
6456 if (VK_SUCCESS == result) {
6457 // TODOSC : Capture layout bindings set
6458 LAYOUT_NODE *pNewNode = new LAYOUT_NODE;
6459 if (NULL == pNewNode) {
6460 if (log_msg(dev_data->report_data, VK_DEBUG_REPORT_ERROR_BIT_EXT, VK_DEBUG_REPORT_OBJECT_TYPE_DESCRIPTOR_SET_LAYOUT_EXT,
6461 (uint64_t)*pSetLayout, __LINE__, DRAWSTATE_OUT_OF_MEMORY, "DS",
6462 "Out of memory while attempting to allocate LAYOUT_NODE in vkCreateDescriptorSetLayout()"))
6463 return VK_ERROR_VALIDATION_FAILED_EXT;
6464 }
6465 memcpy((void *)&pNewNode->createInfo, pCreateInfo, sizeof(VkDescriptorSetLayoutCreateInfo));
6466 pNewNode->createInfo.pBindings = new VkDescriptorSetLayoutBinding[pCreateInfo->bindingCount];
6467 memcpy((void *)pNewNode->createInfo.pBindings, pCreateInfo->pBindings,
6468 sizeof(VkDescriptorSetLayoutBinding) * pCreateInfo->bindingCount);
6469 // g++ does not like reserve with size 0
6470 if (pCreateInfo->bindingCount)
6471 pNewNode->bindingToIndexMap.reserve(pCreateInfo->bindingCount);
6472 uint32_t totalCount = 0;
6473 for (uint32_t i = 0; i < pCreateInfo->bindingCount; i++) {
6474 if (!pNewNode->bindingToIndexMap.emplace(pCreateInfo->pBindings[i].binding, i).second) {
6475 if (log_msg(dev_data->report_data, VK_DEBUG_REPORT_ERROR_BIT_EXT,
6476 VK_DEBUG_REPORT_OBJECT_TYPE_DESCRIPTOR_SET_LAYOUT_EXT, (uint64_t)*pSetLayout, __LINE__,
6477 DRAWSTATE_INVALID_LAYOUT, "DS", "duplicated binding number in "
6478 "VkDescriptorSetLayoutBinding"))
6479 return VK_ERROR_VALIDATION_FAILED_EXT;
6480 } else {
6481 pNewNode->bindingToIndexMap[pCreateInfo->pBindings[i].binding] = i;
6482 }
6483 totalCount += pCreateInfo->pBindings[i].descriptorCount;
6484 if (pCreateInfo->pBindings[i].pImmutableSamplers) {
6485 VkSampler **ppIS = (VkSampler **)&pNewNode->createInfo.pBindings[i].pImmutableSamplers;
6486 *ppIS = new VkSampler[pCreateInfo->pBindings[i].descriptorCount];
6487 memcpy(*ppIS, pCreateInfo->pBindings[i].pImmutableSamplers,
6488 pCreateInfo->pBindings[i].descriptorCount * sizeof(VkSampler));
6489 }
6490 }
6491 pNewNode->layout = *pSetLayout;
6492 pNewNode->startIndex = 0;
6493 if (totalCount > 0) {
6494 pNewNode->descriptorTypes.resize(totalCount);
6495 pNewNode->stageFlags.resize(totalCount);
6496 uint32_t offset = 0;
6497 uint32_t j = 0;
6498 VkDescriptorType dType;
6499 for (uint32_t i = 0; i < pCreateInfo->bindingCount; i++) {
6500 dType = pCreateInfo->pBindings[i].descriptorType;
6501 for (j = 0; j < pCreateInfo->pBindings[i].descriptorCount; j++) {
6502 pNewNode->descriptorTypes[offset + j] = dType;
6503 pNewNode->stageFlags[offset + j] = pCreateInfo->pBindings[i].stageFlags;
6504 if ((dType == VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER_DYNAMIC) ||
6505 (dType == VK_DESCRIPTOR_TYPE_STORAGE_BUFFER_DYNAMIC)) {
6506 pNewNode->dynamicDescriptorCount++;
6507 }
6508 }
6509 offset += j;
6510 }
6511 pNewNode->endIndex = pNewNode->startIndex + totalCount - 1;
6512 } else { // no descriptors
6513 pNewNode->endIndex = 0;
6514 }
6515 // Put new node at Head of global Layer list
Jeremy Hayesb9e99232016-04-13 16:20:24 -06006516 std::lock_guard<std::mutex> lock(global_lock);
Tobin Ehlis5b5e7bc2016-03-09 16:12:48 -07006517 dev_data->descriptorSetLayoutMap[*pSetLayout] = pNewNode;
Tobin Ehlis5b5e7bc2016-03-09 16:12:48 -07006518 }
6519 return result;
6520}
6521
6522static bool validatePushConstantSize(const layer_data *dev_data, const uint32_t offset, const uint32_t size,
6523 const char *caller_name) {
6524 bool skipCall = false;
Tobin Ehlisb1b606d2016-04-11 14:49:55 -06006525 if ((offset + size) > dev_data->phys_dev_properties.properties.limits.maxPushConstantsSize) {
Tobin Ehlis5b5e7bc2016-03-09 16:12:48 -07006526 skipCall = log_msg(dev_data->report_data, VK_DEBUG_REPORT_ERROR_BIT_EXT, (VkDebugReportObjectTypeEXT)0, 0, __LINE__,
6527 DRAWSTATE_PUSH_CONSTANTS_ERROR, "DS", "%s call has push constants with offset %u and size %u that "
6528 "exceeds this device's maxPushConstantSize of %u.",
Tobin Ehlisb1b606d2016-04-11 14:49:55 -06006529 caller_name, offset, size, dev_data->phys_dev_properties.properties.limits.maxPushConstantsSize);
Tobin Ehlis5b5e7bc2016-03-09 16:12:48 -07006530 }
6531 return skipCall;
6532}
6533
6534VKAPI_ATTR VkResult VKAPI_CALL vkCreatePipelineLayout(VkDevice device, const VkPipelineLayoutCreateInfo *pCreateInfo,
6535 const VkAllocationCallbacks *pAllocator, VkPipelineLayout *pPipelineLayout) {
6536 bool skipCall = false;
6537 layer_data *dev_data = get_my_data_ptr(get_dispatch_key(device), layer_data_map);
6538 uint32_t i = 0;
6539 for (i = 0; i < pCreateInfo->pushConstantRangeCount; ++i) {
6540 skipCall |= validatePushConstantSize(dev_data, pCreateInfo->pPushConstantRanges[i].offset,
6541 pCreateInfo->pPushConstantRanges[i].size, "vkCreatePipelineLayout()");
6542 if ((pCreateInfo->pPushConstantRanges[i].size == 0) || ((pCreateInfo->pPushConstantRanges[i].size & 0x3) != 0)) {
6543 skipCall |=
6544 log_msg(dev_data->report_data, VK_DEBUG_REPORT_ERROR_BIT_EXT, (VkDebugReportObjectTypeEXT)0, 0, __LINE__,
6545 DRAWSTATE_PUSH_CONSTANTS_ERROR, "DS", "vkCreatePipelineLayout() call has push constant index %u with "
6546 "size %u. Size must be greater than zero and a multiple of 4.",
6547 i, pCreateInfo->pPushConstantRanges[i].size);
6548 }
6549 // TODO : Add warning if ranges overlap
6550 }
6551 VkResult result = dev_data->device_dispatch_table->CreatePipelineLayout(device, pCreateInfo, pAllocator, pPipelineLayout);
6552 if (VK_SUCCESS == result) {
Jeremy Hayesb9e99232016-04-13 16:20:24 -06006553 std::lock_guard<std::mutex> lock(global_lock);
Tobin Ehlis5b5e7bc2016-03-09 16:12:48 -07006554 // TODOSC : Merge capture of the setLayouts per pipeline
6555 PIPELINE_LAYOUT_NODE &plNode = dev_data->pipelineLayoutMap[*pPipelineLayout];
6556 plNode.descriptorSetLayouts.resize(pCreateInfo->setLayoutCount);
6557 for (i = 0; i < pCreateInfo->setLayoutCount; ++i) {
6558 plNode.descriptorSetLayouts[i] = pCreateInfo->pSetLayouts[i];
6559 }
6560 plNode.pushConstantRanges.resize(pCreateInfo->pushConstantRangeCount);
6561 for (i = 0; i < pCreateInfo->pushConstantRangeCount; ++i) {
6562 plNode.pushConstantRanges[i] = pCreateInfo->pPushConstantRanges[i];
6563 }
Tobin Ehlis5b5e7bc2016-03-09 16:12:48 -07006564 }
6565 return result;
6566}
6567
6568VK_LAYER_EXPORT VKAPI_ATTR VkResult VKAPI_CALL
6569vkCreateDescriptorPool(VkDevice device, const VkDescriptorPoolCreateInfo *pCreateInfo, const VkAllocationCallbacks *pAllocator,
6570 VkDescriptorPool *pDescriptorPool) {
6571 layer_data *dev_data = get_my_data_ptr(get_dispatch_key(device), layer_data_map);
6572 VkResult result = dev_data->device_dispatch_table->CreateDescriptorPool(device, pCreateInfo, pAllocator, pDescriptorPool);
6573 if (VK_SUCCESS == result) {
6574 // Insert this pool into Global Pool LL at head
6575 if (log_msg(dev_data->report_data, VK_DEBUG_REPORT_INFORMATION_BIT_EXT, VK_DEBUG_REPORT_OBJECT_TYPE_DESCRIPTOR_POOL_EXT,
6576 (uint64_t)*pDescriptorPool, __LINE__, DRAWSTATE_OUT_OF_MEMORY, "DS", "Created Descriptor Pool %#" PRIxLEAST64,
6577 (uint64_t)*pDescriptorPool))
6578 return VK_ERROR_VALIDATION_FAILED_EXT;
6579 DESCRIPTOR_POOL_NODE *pNewNode = new DESCRIPTOR_POOL_NODE(*pDescriptorPool, pCreateInfo);
6580 if (NULL == pNewNode) {
6581 if (log_msg(dev_data->report_data, VK_DEBUG_REPORT_ERROR_BIT_EXT, VK_DEBUG_REPORT_OBJECT_TYPE_DESCRIPTOR_POOL_EXT,
6582 (uint64_t)*pDescriptorPool, __LINE__, DRAWSTATE_OUT_OF_MEMORY, "DS",
6583 "Out of memory while attempting to allocate DESCRIPTOR_POOL_NODE in vkCreateDescriptorPool()"))
6584 return VK_ERROR_VALIDATION_FAILED_EXT;
6585 } else {
Jeremy Hayesb9e99232016-04-13 16:20:24 -06006586 std::lock_guard<std::mutex> lock(global_lock);
Tobin Ehlis5b5e7bc2016-03-09 16:12:48 -07006587 dev_data->descriptorPoolMap[*pDescriptorPool] = pNewNode;
Tobin Ehlis5b5e7bc2016-03-09 16:12:48 -07006588 }
6589 } else {
6590 // Need to do anything if pool create fails?
6591 }
6592 return result;
6593}
6594
6595VK_LAYER_EXPORT VKAPI_ATTR VkResult VKAPI_CALL
6596vkResetDescriptorPool(VkDevice device, VkDescriptorPool descriptorPool, VkDescriptorPoolResetFlags flags) {
6597 layer_data *dev_data = get_my_data_ptr(get_dispatch_key(device), layer_data_map);
6598 VkResult result = dev_data->device_dispatch_table->ResetDescriptorPool(device, descriptorPool, flags);
6599 if (VK_SUCCESS == result) {
Jeremy Hayesb9e99232016-04-13 16:20:24 -06006600 std::lock_guard<std::mutex> lock(global_lock);
Tobin Ehlis5b5e7bc2016-03-09 16:12:48 -07006601 clearDescriptorPool(dev_data, device, descriptorPool, flags);
Tobin Ehlis5b5e7bc2016-03-09 16:12:48 -07006602 }
6603 return result;
6604}
6605
6606VK_LAYER_EXPORT VKAPI_ATTR VkResult VKAPI_CALL
6607vkAllocateDescriptorSets(VkDevice device, const VkDescriptorSetAllocateInfo *pAllocateInfo, VkDescriptorSet *pDescriptorSets) {
Dustin Gravese3319182016-04-05 09:41:17 -06006608 bool skipCall = false;
Tobin Ehlis5b5e7bc2016-03-09 16:12:48 -07006609 layer_data *dev_data = get_my_data_ptr(get_dispatch_key(device), layer_data_map);
6610
Jeremy Hayesb9e99232016-04-13 16:20:24 -06006611 std::unique_lock<std::mutex> lock(global_lock);
Tobin Ehlis5b5e7bc2016-03-09 16:12:48 -07006612 // Verify that requested descriptorSets are available in pool
6613 DESCRIPTOR_POOL_NODE *pPoolNode = getPoolNode(dev_data, pAllocateInfo->descriptorPool);
6614 if (!pPoolNode) {
6615 skipCall |= log_msg(dev_data->report_data, VK_DEBUG_REPORT_ERROR_BIT_EXT, VK_DEBUG_REPORT_OBJECT_TYPE_DESCRIPTOR_POOL_EXT,
6616 (uint64_t)pAllocateInfo->descriptorPool, __LINE__, DRAWSTATE_INVALID_POOL, "DS",
6617 "Unable to find pool node for pool %#" PRIxLEAST64 " specified in vkAllocateDescriptorSets() call",
6618 (uint64_t)pAllocateInfo->descriptorPool);
6619 } else { // Make sure pool has all the available descriptors before calling down chain
6620 skipCall |= validate_descriptor_availability_in_pool(dev_data, pPoolNode, pAllocateInfo->descriptorSetCount,
6621 pAllocateInfo->pSetLayouts);
6622 }
Jeremy Hayesb9e99232016-04-13 16:20:24 -06006623 lock.unlock();
Tobin Ehlis5b5e7bc2016-03-09 16:12:48 -07006624 if (skipCall)
6625 return VK_ERROR_VALIDATION_FAILED_EXT;
6626 VkResult result = dev_data->device_dispatch_table->AllocateDescriptorSets(device, pAllocateInfo, pDescriptorSets);
6627 if (VK_SUCCESS == result) {
Jeremy Hayesb9e99232016-04-13 16:20:24 -06006628 lock.lock();
Tobin Ehlis5b5e7bc2016-03-09 16:12:48 -07006629 DESCRIPTOR_POOL_NODE *pPoolNode = getPoolNode(dev_data, pAllocateInfo->descriptorPool);
6630 if (pPoolNode) {
6631 if (pAllocateInfo->descriptorSetCount == 0) {
6632 log_msg(dev_data->report_data, VK_DEBUG_REPORT_INFORMATION_BIT_EXT, VK_DEBUG_REPORT_OBJECT_TYPE_DESCRIPTOR_SET_EXT,
6633 pAllocateInfo->descriptorSetCount, __LINE__, DRAWSTATE_NONE, "DS",
6634 "AllocateDescriptorSets called with 0 count");
6635 }
6636 for (uint32_t i = 0; i < pAllocateInfo->descriptorSetCount; i++) {
6637 log_msg(dev_data->report_data, VK_DEBUG_REPORT_INFORMATION_BIT_EXT, VK_DEBUG_REPORT_OBJECT_TYPE_DESCRIPTOR_SET_EXT,
6638 (uint64_t)pDescriptorSets[i], __LINE__, DRAWSTATE_NONE, "DS", "Created Descriptor Set %#" PRIxLEAST64,
6639 (uint64_t)pDescriptorSets[i]);
6640 // Create new set node and add to head of pool nodes
6641 SET_NODE *pNewNode = new SET_NODE;
6642 if (NULL == pNewNode) {
6643 if (log_msg(dev_data->report_data, VK_DEBUG_REPORT_ERROR_BIT_EXT,
6644 VK_DEBUG_REPORT_OBJECT_TYPE_DESCRIPTOR_SET_EXT, (uint64_t)pDescriptorSets[i], __LINE__,
6645 DRAWSTATE_OUT_OF_MEMORY, "DS",
Tobin Ehlisada0fec2016-03-24 12:50:14 -06006646 "Out of memory while attempting to allocate SET_NODE in vkAllocateDescriptorSets()")) {
Jeremy Hayesb9e99232016-04-13 16:20:24 -06006647 lock.unlock();
Tobin Ehlis5b5e7bc2016-03-09 16:12:48 -07006648 return VK_ERROR_VALIDATION_FAILED_EXT;
Tobin Ehlisada0fec2016-03-24 12:50:14 -06006649 }
Tobin Ehlis5b5e7bc2016-03-09 16:12:48 -07006650 } else {
6651 // TODO : Pool should store a total count of each type of Descriptor available
6652 // When descriptors are allocated, decrement the count and validate here
6653 // that the count doesn't go below 0. One reset/free need to bump count back up.
6654 // Insert set at head of Set LL for this pool
6655 pNewNode->pNext = pPoolNode->pSets;
6656 pNewNode->in_use.store(0);
6657 pPoolNode->pSets = pNewNode;
6658 LAYOUT_NODE *pLayout = getLayoutNode(dev_data, pAllocateInfo->pSetLayouts[i]);
6659 if (NULL == pLayout) {
6660 if (log_msg(dev_data->report_data, VK_DEBUG_REPORT_ERROR_BIT_EXT,
6661 VK_DEBUG_REPORT_OBJECT_TYPE_DESCRIPTOR_SET_LAYOUT_EXT, (uint64_t)pAllocateInfo->pSetLayouts[i],
6662 __LINE__, DRAWSTATE_INVALID_LAYOUT, "DS",
6663 "Unable to find set layout node for layout %#" PRIxLEAST64
6664 " specified in vkAllocateDescriptorSets() call",
Tobin Ehlisada0fec2016-03-24 12:50:14 -06006665 (uint64_t)pAllocateInfo->pSetLayouts[i])) {
Jeremy Hayesb9e99232016-04-13 16:20:24 -06006666 lock.unlock();
Tobin Ehlis5b5e7bc2016-03-09 16:12:48 -07006667 return VK_ERROR_VALIDATION_FAILED_EXT;
Tobin Ehlisada0fec2016-03-24 12:50:14 -06006668 }
Tobin Ehlis5b5e7bc2016-03-09 16:12:48 -07006669 }
6670 pNewNode->pLayout = pLayout;
6671 pNewNode->pool = pAllocateInfo->descriptorPool;
6672 pNewNode->set = pDescriptorSets[i];
6673 pNewNode->descriptorCount = (pLayout->createInfo.bindingCount != 0) ? pLayout->endIndex + 1 : 0;
6674 if (pNewNode->descriptorCount) {
Karl Schultz656b1a52016-03-25 13:21:25 -06006675 pNewNode->pDescriptorUpdates.resize(pNewNode->descriptorCount);
Tobin Ehlis5b5e7bc2016-03-09 16:12:48 -07006676 }
6677 dev_data->setMap[pDescriptorSets[i]] = pNewNode;
6678 }
6679 }
6680 }
Jeremy Hayesb9e99232016-04-13 16:20:24 -06006681 lock.unlock();
Tobin Ehlis5b5e7bc2016-03-09 16:12:48 -07006682 }
6683 return result;
6684}
6685
6686VK_LAYER_EXPORT VKAPI_ATTR VkResult VKAPI_CALL
6687vkFreeDescriptorSets(VkDevice device, VkDescriptorPool descriptorPool, uint32_t count, const VkDescriptorSet *pDescriptorSets) {
Dustin Gravese3319182016-04-05 09:41:17 -06006688 bool skipCall = false;
Tobin Ehlis5b5e7bc2016-03-09 16:12:48 -07006689 layer_data *dev_data = get_my_data_ptr(get_dispatch_key(device), layer_data_map);
6690 // Make sure that no sets being destroyed are in-flight
Jeremy Hayesb9e99232016-04-13 16:20:24 -06006691 std::unique_lock<std::mutex> lock(global_lock);
Tobin Ehlis5b5e7bc2016-03-09 16:12:48 -07006692 for (uint32_t i = 0; i < count; ++i)
Mark Muellerce62d9f2016-04-14 11:01:58 -06006693 skipCall |= validateIdleDescriptorSet(dev_data, pDescriptorSets[i], "vkFreeDescriptorSets");
Tobin Ehlis5b5e7bc2016-03-09 16:12:48 -07006694 DESCRIPTOR_POOL_NODE *pPoolNode = getPoolNode(dev_data, descriptorPool);
6695 if (pPoolNode && !(VK_DESCRIPTOR_POOL_CREATE_FREE_DESCRIPTOR_SET_BIT & pPoolNode->createInfo.flags)) {
6696 // Can't Free from a NON_FREE pool
6697 skipCall |= log_msg(dev_data->report_data, VK_DEBUG_REPORT_ERROR_BIT_EXT, VK_DEBUG_REPORT_OBJECT_TYPE_DEVICE_EXT,
6698 (uint64_t)device, __LINE__, DRAWSTATE_CANT_FREE_FROM_NON_FREE_POOL, "DS",
6699 "It is invalid to call vkFreeDescriptorSets() with a pool created without setting "
6700 "VK_DESCRIPTOR_POOL_CREATE_FREE_DESCRIPTOR_SET_BIT.");
6701 }
Jeremy Hayesb9e99232016-04-13 16:20:24 -06006702 lock.unlock();
Dustin Gravese3319182016-04-05 09:41:17 -06006703 if (skipCall)
Tobin Ehlis5b5e7bc2016-03-09 16:12:48 -07006704 return VK_ERROR_VALIDATION_FAILED_EXT;
6705 VkResult result = dev_data->device_dispatch_table->FreeDescriptorSets(device, descriptorPool, count, pDescriptorSets);
6706 if (VK_SUCCESS == result) {
Jeremy Hayesb9e99232016-04-13 16:20:24 -06006707 lock.lock();
Mark Lobodzinskia2d5d612016-03-21 16:32:53 -06006708
6709 // Update available descriptor sets in pool
6710 pPoolNode->availableSets += count;
6711
6712 // For each freed descriptor add it back into the pool as available
Tobin Ehlis5b5e7bc2016-03-09 16:12:48 -07006713 for (uint32_t i = 0; i < count; ++i) {
6714 SET_NODE *pSet = dev_data->setMap[pDescriptorSets[i]]; // getSetNode() without locking
6715 invalidateBoundCmdBuffers(dev_data, pSet);
6716 LAYOUT_NODE *pLayout = pSet->pLayout;
6717 uint32_t typeIndex = 0, poolSizeCount = 0;
6718 for (uint32_t j = 0; j < pLayout->createInfo.bindingCount; ++j) {
6719 typeIndex = static_cast<uint32_t>(pLayout->createInfo.pBindings[j].descriptorType);
6720 poolSizeCount = pLayout->createInfo.pBindings[j].descriptorCount;
6721 pPoolNode->availableDescriptorTypeCount[typeIndex] += poolSizeCount;
6722 }
6723 }
Jeremy Hayesb9e99232016-04-13 16:20:24 -06006724 lock.unlock();
Tobin Ehlis5b5e7bc2016-03-09 16:12:48 -07006725 }
6726 // TODO : Any other clean-up or book-keeping to do here?
6727 return result;
6728}
6729
6730VK_LAYER_EXPORT VKAPI_ATTR void VKAPI_CALL
6731vkUpdateDescriptorSets(VkDevice device, uint32_t descriptorWriteCount, const VkWriteDescriptorSet *pDescriptorWrites,
6732 uint32_t descriptorCopyCount, const VkCopyDescriptorSet *pDescriptorCopies) {
Dustin Gravese3319182016-04-05 09:41:17 -06006733 // dsUpdate will return true only if a bailout error occurs, so we want to call down tree when update returns false
Tobin Ehlis5b5e7bc2016-03-09 16:12:48 -07006734 layer_data *dev_data = get_my_data_ptr(get_dispatch_key(device), layer_data_map);
Jeremy Hayesb9e99232016-04-13 16:20:24 -06006735 std::unique_lock<std::mutex> lock(global_lock);
Dustin Gravese3319182016-04-05 09:41:17 -06006736 bool rtn = dsUpdate(dev_data, device, descriptorWriteCount, pDescriptorWrites, descriptorCopyCount, pDescriptorCopies);
Jeremy Hayesb9e99232016-04-13 16:20:24 -06006737 lock.unlock();
Tobin Ehlis5b5e7bc2016-03-09 16:12:48 -07006738 if (!rtn) {
6739 dev_data->device_dispatch_table->UpdateDescriptorSets(device, descriptorWriteCount, pDescriptorWrites, descriptorCopyCount,
6740 pDescriptorCopies);
6741 }
6742}
6743
6744VK_LAYER_EXPORT VKAPI_ATTR VkResult VKAPI_CALL
6745vkAllocateCommandBuffers(VkDevice device, const VkCommandBufferAllocateInfo *pCreateInfo, VkCommandBuffer *pCommandBuffer) {
6746 layer_data *dev_data = get_my_data_ptr(get_dispatch_key(device), layer_data_map);
6747 VkResult result = dev_data->device_dispatch_table->AllocateCommandBuffers(device, pCreateInfo, pCommandBuffer);
6748 if (VK_SUCCESS == result) {
Jeremy Hayesb9e99232016-04-13 16:20:24 -06006749 std::unique_lock<std::mutex> lock(global_lock);
Tobin Ehlis72d66f02016-03-21 14:14:44 -06006750 auto const &cp_it = dev_data->commandPoolMap.find(pCreateInfo->commandPool);
6751 if (cp_it != dev_data->commandPoolMap.end()) {
6752 for (uint32_t i = 0; i < pCreateInfo->commandBufferCount; i++) {
Tobin Ehlis5b5e7bc2016-03-09 16:12:48 -07006753 // Add command buffer to its commandPool map
Tobin Ehlis72d66f02016-03-21 14:14:44 -06006754 cp_it->second.commandBuffers.push_back(pCommandBuffer[i]);
Tobin Ehlis5b5e7bc2016-03-09 16:12:48 -07006755 GLOBAL_CB_NODE *pCB = new GLOBAL_CB_NODE;
6756 // Add command buffer to map
6757 dev_data->commandBufferMap[pCommandBuffer[i]] = pCB;
6758 resetCB(dev_data, pCommandBuffer[i]);
6759 pCB->createInfo = *pCreateInfo;
6760 pCB->device = device;
6761 }
6762 }
Mark Lobodzinski03b71512016-03-23 14:33:02 -06006763#if MTMERGESOURCE
Chris Forbes0a1ce3d2016-04-06 15:16:26 +12006764 printCBList(dev_data);
Tobin Ehlis5b5e7bc2016-03-09 16:12:48 -07006765#endif
Jeremy Hayesb9e99232016-04-13 16:20:24 -06006766 lock.unlock();
Tobin Ehlis5b5e7bc2016-03-09 16:12:48 -07006767 }
6768 return result;
6769}
6770
6771VK_LAYER_EXPORT VKAPI_ATTR VkResult VKAPI_CALL
6772vkBeginCommandBuffer(VkCommandBuffer commandBuffer, const VkCommandBufferBeginInfo *pBeginInfo) {
Dustin Gravese3319182016-04-05 09:41:17 -06006773 bool skipCall = false;
Tobin Ehlis5b5e7bc2016-03-09 16:12:48 -07006774 layer_data *dev_data = get_my_data_ptr(get_dispatch_key(commandBuffer), layer_data_map);
Jeremy Hayesb9e99232016-04-13 16:20:24 -06006775 std::unique_lock<std::mutex> lock(global_lock);
Tobin Ehlis5b5e7bc2016-03-09 16:12:48 -07006776 // Validate command buffer level
6777 GLOBAL_CB_NODE *pCB = getCBNode(dev_data, commandBuffer);
6778 if (pCB) {
Tobin Ehlis5b5e7bc2016-03-09 16:12:48 -07006779 bool commandBufferComplete = false;
Tobin Ehlis5b5e7bc2016-03-09 16:12:48 -07006780 // This implicitly resets the Cmd Buffer so make sure any fence is done and then clear memory references
6781 skipCall = checkCBCompleted(dev_data, commandBuffer, &commandBufferComplete);
Tobin Ehlis400cff92016-04-11 16:39:29 -06006782 clear_cmd_buf_and_mem_references(dev_data, pCB);
Tobin Ehlis5b5e7bc2016-03-09 16:12:48 -07006783
6784 if (!commandBufferComplete) {
6785 skipCall |= log_msg(dev_data->report_data, VK_DEBUG_REPORT_ERROR_BIT_EXT, VK_DEBUG_REPORT_OBJECT_TYPE_COMMAND_BUFFER_EXT,
6786 (uint64_t)commandBuffer, __LINE__, MEMTRACK_RESET_CB_WHILE_IN_FLIGHT, "MEM",
6787 "Calling vkBeginCommandBuffer() on active CB %p before it has completed. "
6788 "You must check CB flag before this call.",
6789 commandBuffer);
6790 }
Tobin Ehlis5b5e7bc2016-03-09 16:12:48 -07006791 if (pCB->createInfo.level != VK_COMMAND_BUFFER_LEVEL_PRIMARY) {
6792 // Secondary Command Buffer
6793 const VkCommandBufferInheritanceInfo *pInfo = pBeginInfo->pInheritanceInfo;
6794 if (!pInfo) {
6795 skipCall |=
6796 log_msg(dev_data->report_data, VK_DEBUG_REPORT_ERROR_BIT_EXT, VK_DEBUG_REPORT_OBJECT_TYPE_COMMAND_BUFFER_EXT,
6797 reinterpret_cast<uint64_t>(commandBuffer), __LINE__, DRAWSTATE_BEGIN_CB_INVALID_STATE, "DS",
6798 "vkBeginCommandBuffer(): Secondary Command Buffer (%p) must have inheritance info.",
6799 reinterpret_cast<void *>(commandBuffer));
6800 } else {
6801 if (pBeginInfo->flags & VK_COMMAND_BUFFER_USAGE_RENDER_PASS_CONTINUE_BIT) {
Tobin Ehlis400cff92016-04-11 16:39:29 -06006802 if (!pInfo->renderPass) { // renderpass should NOT be null for a Secondary CB
Tobin Ehlis5b5e7bc2016-03-09 16:12:48 -07006803 skipCall |= log_msg(
6804 dev_data->report_data, VK_DEBUG_REPORT_ERROR_BIT_EXT, VK_DEBUG_REPORT_OBJECT_TYPE_COMMAND_BUFFER_EXT,
6805 reinterpret_cast<uint64_t>(commandBuffer), __LINE__, DRAWSTATE_BEGIN_CB_INVALID_STATE, "DS",
6806 "vkBeginCommandBuffer(): Secondary Command Buffers (%p) must specify a valid renderpass parameter.",
6807 reinterpret_cast<void *>(commandBuffer));
6808 }
Tobin Ehlis400cff92016-04-11 16:39:29 -06006809 if (!pInfo->framebuffer) { // framebuffer may be null for a Secondary CB, but this affects perf
Tobin Ehlis5b5e7bc2016-03-09 16:12:48 -07006810 skipCall |= log_msg(dev_data->report_data, VK_DEBUG_REPORT_WARNING_BIT_EXT,
6811 VK_DEBUG_REPORT_OBJECT_TYPE_COMMAND_BUFFER_EXT,
6812 reinterpret_cast<uint64_t>(commandBuffer), __LINE__, DRAWSTATE_BEGIN_CB_INVALID_STATE,
6813 "DS", "vkBeginCommandBuffer(): Secondary Command Buffers (%p) may perform better if a "
6814 "valid framebuffer parameter is specified.",
6815 reinterpret_cast<void *>(commandBuffer));
6816 } else {
6817 string errorString = "";
6818 auto fbNode = dev_data->frameBufferMap.find(pInfo->framebuffer);
6819 if (fbNode != dev_data->frameBufferMap.end()) {
6820 VkRenderPass fbRP = fbNode->second.createInfo.renderPass;
6821 if (!verify_renderpass_compatibility(dev_data, fbRP, pInfo->renderPass, errorString)) {
Tobin Ehlis400cff92016-04-11 16:39:29 -06006822 // renderPass that framebuffer was created with must be compatible with local renderPass
Tobin Ehlis5b5e7bc2016-03-09 16:12:48 -07006823 skipCall |=
6824 log_msg(dev_data->report_data, VK_DEBUG_REPORT_ERROR_BIT_EXT,
6825 VK_DEBUG_REPORT_OBJECT_TYPE_COMMAND_BUFFER_EXT,
6826 reinterpret_cast<uint64_t>(commandBuffer), __LINE__, DRAWSTATE_RENDERPASS_INCOMPATIBLE,
6827 "DS", "vkBeginCommandBuffer(): Secondary Command "
6828 "Buffer (%p) renderPass (%#" PRIxLEAST64 ") is incompatible w/ framebuffer "
6829 "(%#" PRIxLEAST64 ") w/ render pass (%#" PRIxLEAST64 ") due to: %s",
6830 reinterpret_cast<void *>(commandBuffer), (uint64_t)(pInfo->renderPass),
6831 (uint64_t)(pInfo->framebuffer), (uint64_t)(fbRP), errorString.c_str());
6832 }
6833 // Connect this framebuffer to this cmdBuffer
6834 fbNode->second.referencingCmdBuffers.insert(pCB->commandBuffer);
6835 }
6836 }
6837 }
6838 if ((pInfo->occlusionQueryEnable == VK_FALSE ||
Tobin Ehlisb1b606d2016-04-11 14:49:55 -06006839 dev_data->phys_dev_properties.features.occlusionQueryPrecise == VK_FALSE) &&
Tobin Ehlis5b5e7bc2016-03-09 16:12:48 -07006840 (pInfo->queryFlags & VK_QUERY_CONTROL_PRECISE_BIT)) {
6841 skipCall |= log_msg(dev_data->report_data, VK_DEBUG_REPORT_ERROR_BIT_EXT,
6842 VK_DEBUG_REPORT_OBJECT_TYPE_COMMAND_BUFFER_EXT, reinterpret_cast<uint64_t>(commandBuffer),
6843 __LINE__, DRAWSTATE_BEGIN_CB_INVALID_STATE, "DS",
6844 "vkBeginCommandBuffer(): Secondary Command Buffer (%p) must not have "
6845 "VK_QUERY_CONTROL_PRECISE_BIT if occulusionQuery is disabled or the device does not "
6846 "support precise occlusion queries.",
6847 reinterpret_cast<void *>(commandBuffer));
6848 }
6849 }
6850 if (pInfo && pInfo->renderPass != VK_NULL_HANDLE) {
6851 auto rp_data = dev_data->renderPassMap.find(pInfo->renderPass);
6852 if (rp_data != dev_data->renderPassMap.end() && rp_data->second && rp_data->second->pCreateInfo) {
6853 if (pInfo->subpass >= rp_data->second->pCreateInfo->subpassCount) {
6854 skipCall |= log_msg(dev_data->report_data, VK_DEBUG_REPORT_ERROR_BIT_EXT,
6855 VK_DEBUG_REPORT_OBJECT_TYPE_COMMAND_BUFFER_EXT, (uint64_t)commandBuffer, __LINE__,
6856 DRAWSTATE_BEGIN_CB_INVALID_STATE, "DS",
6857 "vkBeginCommandBuffer(): Secondary Command Buffers (%p) must has a subpass index (%d) "
6858 "that is less than the number of subpasses (%d).",
6859 (void *)commandBuffer, pInfo->subpass, rp_data->second->pCreateInfo->subpassCount);
6860 }
6861 }
6862 }
6863 }
6864 if (CB_RECORDING == pCB->state) {
6865 skipCall |=
6866 log_msg(dev_data->report_data, VK_DEBUG_REPORT_ERROR_BIT_EXT, VK_DEBUG_REPORT_OBJECT_TYPE_COMMAND_BUFFER_EXT,
6867 (uint64_t)commandBuffer, __LINE__, DRAWSTATE_BEGIN_CB_INVALID_STATE, "DS",
6868 "vkBeginCommandBuffer(): Cannot call Begin on CB (%#" PRIxLEAST64
6869 ") in the RECORDING state. Must first call vkEndCommandBuffer().",
6870 (uint64_t)commandBuffer);
6871 } else if (CB_RECORDED == pCB->state) {
6872 VkCommandPool cmdPool = pCB->createInfo.commandPool;
6873 if (!(VK_COMMAND_POOL_CREATE_RESET_COMMAND_BUFFER_BIT & dev_data->commandPoolMap[cmdPool].createFlags)) {
6874 skipCall |=
6875 log_msg(dev_data->report_data, VK_DEBUG_REPORT_ERROR_BIT_EXT, VK_DEBUG_REPORT_OBJECT_TYPE_COMMAND_BUFFER_EXT,
6876 (uint64_t)commandBuffer, __LINE__, DRAWSTATE_INVALID_COMMAND_BUFFER_RESET, "DS",
6877 "Call to vkBeginCommandBuffer() on command buffer (%#" PRIxLEAST64
6878 ") attempts to implicitly reset cmdBuffer created from command pool (%#" PRIxLEAST64
6879 ") that does NOT have the VK_COMMAND_POOL_CREATE_RESET_COMMAND_BUFFER_BIT bit set.",
6880 (uint64_t)commandBuffer, (uint64_t)cmdPool);
6881 }
6882 resetCB(dev_data, commandBuffer);
6883 }
6884 // Set updated state here in case implicit reset occurs above
6885 pCB->state = CB_RECORDING;
6886 pCB->beginInfo = *pBeginInfo;
6887 if (pCB->beginInfo.pInheritanceInfo) {
6888 pCB->inheritanceInfo = *(pCB->beginInfo.pInheritanceInfo);
6889 pCB->beginInfo.pInheritanceInfo = &pCB->inheritanceInfo;
6890 }
6891 } else {
6892 skipCall |= log_msg(dev_data->report_data, VK_DEBUG_REPORT_ERROR_BIT_EXT, VK_DEBUG_REPORT_OBJECT_TYPE_COMMAND_BUFFER_EXT,
6893 (uint64_t)commandBuffer, __LINE__, DRAWSTATE_INVALID_COMMAND_BUFFER, "DS",
6894 "In vkBeginCommandBuffer() and unable to find CommandBuffer Node for CB %p!", (void *)commandBuffer);
6895 }
Jeremy Hayesb9e99232016-04-13 16:20:24 -06006896 lock.unlock();
Dustin Gravese3319182016-04-05 09:41:17 -06006897 if (skipCall) {
Tobin Ehlis5b5e7bc2016-03-09 16:12:48 -07006898 return VK_ERROR_VALIDATION_FAILED_EXT;
6899 }
6900 VkResult result = dev_data->device_dispatch_table->BeginCommandBuffer(commandBuffer, pBeginInfo);
Tobin Ehlis400cff92016-04-11 16:39:29 -06006901
Tobin Ehlis5b5e7bc2016-03-09 16:12:48 -07006902 return result;
6903}
6904
6905VK_LAYER_EXPORT VKAPI_ATTR VkResult VKAPI_CALL vkEndCommandBuffer(VkCommandBuffer commandBuffer) {
Dustin Gravese3319182016-04-05 09:41:17 -06006906 bool skipCall = false;
Tobin Ehlis5b5e7bc2016-03-09 16:12:48 -07006907 VkResult result = VK_SUCCESS;
6908 layer_data *dev_data = get_my_data_ptr(get_dispatch_key(commandBuffer), layer_data_map);
Jeremy Hayesb9e99232016-04-13 16:20:24 -06006909 std::unique_lock<std::mutex> lock(global_lock);
Tobin Ehlis5b5e7bc2016-03-09 16:12:48 -07006910 GLOBAL_CB_NODE *pCB = getCBNode(dev_data, commandBuffer);
6911 if (pCB) {
6912 if (pCB->state != CB_RECORDING) {
6913 skipCall |= report_error_no_cb_begin(dev_data, commandBuffer, "vkEndCommandBuffer()");
6914 }
6915 for (auto query : pCB->activeQueries) {
6916 skipCall |= log_msg(dev_data->report_data, VK_DEBUG_REPORT_ERROR_BIT_EXT, (VkDebugReportObjectTypeEXT)0, 0, __LINE__,
6917 DRAWSTATE_INVALID_QUERY, "DS",
6918 "Ending command buffer with in progress query: queryPool %" PRIu64 ", index %d",
6919 (uint64_t)(query.pool), query.index);
6920 }
6921 }
Dustin Gravese3319182016-04-05 09:41:17 -06006922 if (!skipCall) {
Jeremy Hayesb9e99232016-04-13 16:20:24 -06006923 lock.unlock();
Tobin Ehlis5b5e7bc2016-03-09 16:12:48 -07006924 result = dev_data->device_dispatch_table->EndCommandBuffer(commandBuffer);
Jeremy Hayesb9e99232016-04-13 16:20:24 -06006925 lock.lock();
Tobin Ehlis5b5e7bc2016-03-09 16:12:48 -07006926 if (VK_SUCCESS == result) {
6927 pCB->state = CB_RECORDED;
6928 // Reset CB status flags
6929 pCB->status = 0;
6930 printCB(dev_data, commandBuffer);
6931 }
6932 } else {
6933 result = VK_ERROR_VALIDATION_FAILED_EXT;
6934 }
Jeremy Hayesb9e99232016-04-13 16:20:24 -06006935 lock.unlock();
Tobin Ehlis5b5e7bc2016-03-09 16:12:48 -07006936 return result;
6937}
6938
6939VK_LAYER_EXPORT VKAPI_ATTR VkResult VKAPI_CALL
6940vkResetCommandBuffer(VkCommandBuffer commandBuffer, VkCommandBufferResetFlags flags) {
Tobin Ehlisbc300ac2016-04-14 12:22:03 -06006941 bool skip_call = false;
Tobin Ehlis5b5e7bc2016-03-09 16:12:48 -07006942 layer_data *dev_data = get_my_data_ptr(get_dispatch_key(commandBuffer), layer_data_map);
Jeremy Hayesb9e99232016-04-13 16:20:24 -06006943 std::unique_lock<std::mutex> lock(global_lock);
Tobin Ehlis5b5e7bc2016-03-09 16:12:48 -07006944 GLOBAL_CB_NODE *pCB = getCBNode(dev_data, commandBuffer);
6945 VkCommandPool cmdPool = pCB->createInfo.commandPool;
6946 if (!(VK_COMMAND_POOL_CREATE_RESET_COMMAND_BUFFER_BIT & dev_data->commandPoolMap[cmdPool].createFlags)) {
Tobin Ehlisbc300ac2016-04-14 12:22:03 -06006947 skip_call |= log_msg(dev_data->report_data, VK_DEBUG_REPORT_ERROR_BIT_EXT, VK_DEBUG_REPORT_OBJECT_TYPE_COMMAND_BUFFER_EXT,
6948 (uint64_t)commandBuffer, __LINE__, DRAWSTATE_INVALID_COMMAND_BUFFER_RESET, "DS",
6949 "Attempt to reset command buffer (%#" PRIxLEAST64 ") created from command pool (%#" PRIxLEAST64
6950 ") that does NOT have the VK_COMMAND_POOL_CREATE_RESET_COMMAND_BUFFER_BIT bit set.",
6951 (uint64_t)commandBuffer, (uint64_t)cmdPool);
Tobin Ehlis5b5e7bc2016-03-09 16:12:48 -07006952 }
Tobin Ehlisbc300ac2016-04-14 12:22:03 -06006953 skip_call |= checkAndClearCommandBufferInFlight(dev_data, pCB, "reset");
Jeremy Hayesb9e99232016-04-13 16:20:24 -06006954 lock.unlock();
Tobin Ehlisbc300ac2016-04-14 12:22:03 -06006955 if (skip_call)
Tobin Ehlis5b5e7bc2016-03-09 16:12:48 -07006956 return VK_ERROR_VALIDATION_FAILED_EXT;
6957 VkResult result = dev_data->device_dispatch_table->ResetCommandBuffer(commandBuffer, flags);
6958 if (VK_SUCCESS == result) {
Jeremy Hayesb9e99232016-04-13 16:20:24 -06006959 lock.lock();
Tobin Ehlis5b5e7bc2016-03-09 16:12:48 -07006960 resetCB(dev_data, commandBuffer);
Jeremy Hayesb9e99232016-04-13 16:20:24 -06006961 lock.unlock();
Tobin Ehlis5b5e7bc2016-03-09 16:12:48 -07006962 }
6963 return result;
6964}
Mark Lobodzinski93c396d2016-04-12 10:41:59 -06006965
Mark Lobodzinski03b71512016-03-23 14:33:02 -06006966#if MTMERGESOURCE
Tobin Ehlis5b5e7bc2016-03-09 16:12:48 -07006967// TODO : For any vkCmdBind* calls that include an object which has mem bound to it,
6968// need to account for that mem now having binding to given commandBuffer
6969#endif
6970VK_LAYER_EXPORT VKAPI_ATTR void VKAPI_CALL
6971vkCmdBindPipeline(VkCommandBuffer commandBuffer, VkPipelineBindPoint pipelineBindPoint, VkPipeline pipeline) {
Dustin Gravese3319182016-04-05 09:41:17 -06006972 bool skipCall = false;
Tobin Ehlis5b5e7bc2016-03-09 16:12:48 -07006973 layer_data *dev_data = get_my_data_ptr(get_dispatch_key(commandBuffer), layer_data_map);
Jeremy Hayesb9e99232016-04-13 16:20:24 -06006974 std::unique_lock<std::mutex> lock(global_lock);
Tobin Ehlis5b5e7bc2016-03-09 16:12:48 -07006975 GLOBAL_CB_NODE *pCB = getCBNode(dev_data, commandBuffer);
6976 if (pCB) {
6977 skipCall |= addCmd(dev_data, pCB, CMD_BINDPIPELINE, "vkCmdBindPipeline()");
6978 if ((VK_PIPELINE_BIND_POINT_COMPUTE == pipelineBindPoint) && (pCB->activeRenderPass)) {
6979 skipCall |=
6980 log_msg(dev_data->report_data, VK_DEBUG_REPORT_ERROR_BIT_EXT, VK_DEBUG_REPORT_OBJECT_TYPE_PIPELINE_EXT,
6981 (uint64_t)pipeline, __LINE__, DRAWSTATE_INVALID_RENDERPASS_CMD, "DS",
6982 "Incorrectly binding compute pipeline (%#" PRIxLEAST64 ") during active RenderPass (%#" PRIxLEAST64 ")",
6983 (uint64_t)pipeline, (uint64_t)pCB->activeRenderPass);
6984 }
6985
6986 PIPELINE_NODE *pPN = getPipeline(dev_data, pipeline);
6987 if (pPN) {
Tobin Ehlis72d66f02016-03-21 14:14:44 -06006988 pCB->lastBound[pipelineBindPoint].pipeline = pipeline;
Tobin Ehlis5b5e7bc2016-03-09 16:12:48 -07006989 set_cb_pso_status(pCB, pPN);
Tobin Ehlis3d3e06b2016-03-28 11:18:19 -06006990 set_pipeline_state(pPN);
Tobin Ehlis5b5e7bc2016-03-09 16:12:48 -07006991 skipCall |= validatePipelineState(dev_data, pCB, pipelineBindPoint, pipeline);
6992 } else {
6993 skipCall |= log_msg(dev_data->report_data, VK_DEBUG_REPORT_ERROR_BIT_EXT, VK_DEBUG_REPORT_OBJECT_TYPE_PIPELINE_EXT,
6994 (uint64_t)pipeline, __LINE__, DRAWSTATE_INVALID_PIPELINE, "DS",
6995 "Attempt to bind Pipeline %#" PRIxLEAST64 " that doesn't exist!", (uint64_t)(pipeline));
6996 }
6997 }
Jeremy Hayesb9e99232016-04-13 16:20:24 -06006998 lock.unlock();
Dustin Gravese3319182016-04-05 09:41:17 -06006999 if (!skipCall)
Tobin Ehlis5b5e7bc2016-03-09 16:12:48 -07007000 dev_data->device_dispatch_table->CmdBindPipeline(commandBuffer, pipelineBindPoint, pipeline);
7001}
7002
7003VK_LAYER_EXPORT VKAPI_ATTR void VKAPI_CALL
7004vkCmdSetViewport(VkCommandBuffer commandBuffer, uint32_t firstViewport, uint32_t viewportCount, const VkViewport *pViewports) {
Dustin Gravese3319182016-04-05 09:41:17 -06007005 bool skipCall = false;
Tobin Ehlis5b5e7bc2016-03-09 16:12:48 -07007006 layer_data *dev_data = get_my_data_ptr(get_dispatch_key(commandBuffer), layer_data_map);
Jeremy Hayesb9e99232016-04-13 16:20:24 -06007007 std::unique_lock<std::mutex> lock(global_lock);
Tobin Ehlis5b5e7bc2016-03-09 16:12:48 -07007008 GLOBAL_CB_NODE *pCB = getCBNode(dev_data, commandBuffer);
7009 if (pCB) {
7010 skipCall |= addCmd(dev_data, pCB, CMD_SETVIEWPORTSTATE, "vkCmdSetViewport()");
7011 pCB->status |= CBSTATUS_VIEWPORT_SET;
7012 pCB->viewports.resize(viewportCount);
7013 memcpy(pCB->viewports.data(), pViewports, viewportCount * sizeof(VkViewport));
7014 }
Jeremy Hayesb9e99232016-04-13 16:20:24 -06007015 lock.unlock();
Dustin Gravese3319182016-04-05 09:41:17 -06007016 if (!skipCall)
Tobin Ehlis5b5e7bc2016-03-09 16:12:48 -07007017 dev_data->device_dispatch_table->CmdSetViewport(commandBuffer, firstViewport, viewportCount, pViewports);
7018}
7019
7020VK_LAYER_EXPORT VKAPI_ATTR void VKAPI_CALL
7021vkCmdSetScissor(VkCommandBuffer commandBuffer, uint32_t firstScissor, uint32_t scissorCount, const VkRect2D *pScissors) {
Dustin Gravese3319182016-04-05 09:41:17 -06007022 bool skipCall = false;
Tobin Ehlis5b5e7bc2016-03-09 16:12:48 -07007023 layer_data *dev_data = get_my_data_ptr(get_dispatch_key(commandBuffer), layer_data_map);
Jeremy Hayesb9e99232016-04-13 16:20:24 -06007024 std::unique_lock<std::mutex> lock(global_lock);
Tobin Ehlis5b5e7bc2016-03-09 16:12:48 -07007025 GLOBAL_CB_NODE *pCB = getCBNode(dev_data, commandBuffer);
7026 if (pCB) {
7027 skipCall |= addCmd(dev_data, pCB, CMD_SETSCISSORSTATE, "vkCmdSetScissor()");
7028 pCB->status |= CBSTATUS_SCISSOR_SET;
7029 pCB->scissors.resize(scissorCount);
7030 memcpy(pCB->scissors.data(), pScissors, scissorCount * sizeof(VkRect2D));
7031 }
Jeremy Hayesb9e99232016-04-13 16:20:24 -06007032 lock.unlock();
Dustin Gravese3319182016-04-05 09:41:17 -06007033 if (!skipCall)
Tobin Ehlis5b5e7bc2016-03-09 16:12:48 -07007034 dev_data->device_dispatch_table->CmdSetScissor(commandBuffer, firstScissor, scissorCount, pScissors);
7035}
7036
7037VK_LAYER_EXPORT VKAPI_ATTR void VKAPI_CALL vkCmdSetLineWidth(VkCommandBuffer commandBuffer, float lineWidth) {
Dustin Gravese3319182016-04-05 09:41:17 -06007038 bool skipCall = false;
Tobin Ehlis5b5e7bc2016-03-09 16:12:48 -07007039 layer_data *dev_data = get_my_data_ptr(get_dispatch_key(commandBuffer), layer_data_map);
Jeremy Hayesb9e99232016-04-13 16:20:24 -06007040 std::unique_lock<std::mutex> lock(global_lock);
Tobin Ehlis5b5e7bc2016-03-09 16:12:48 -07007041 GLOBAL_CB_NODE *pCB = getCBNode(dev_data, commandBuffer);
7042 if (pCB) {
7043 skipCall |= addCmd(dev_data, pCB, CMD_SETLINEWIDTHSTATE, "vkCmdSetLineWidth()");
7044 pCB->status |= CBSTATUS_LINE_WIDTH_SET;
Tobin Ehlis5b5e7bc2016-03-09 16:12:48 -07007045 }
Jeremy Hayesb9e99232016-04-13 16:20:24 -06007046 lock.unlock();
Dustin Gravese3319182016-04-05 09:41:17 -06007047 if (!skipCall)
Tobin Ehlis5b5e7bc2016-03-09 16:12:48 -07007048 dev_data->device_dispatch_table->CmdSetLineWidth(commandBuffer, lineWidth);
7049}
7050
7051VK_LAYER_EXPORT VKAPI_ATTR void VKAPI_CALL
7052vkCmdSetDepthBias(VkCommandBuffer commandBuffer, float depthBiasConstantFactor, float depthBiasClamp, float depthBiasSlopeFactor) {
Dustin Gravese3319182016-04-05 09:41:17 -06007053 bool skipCall = false;
Tobin Ehlis5b5e7bc2016-03-09 16:12:48 -07007054 layer_data *dev_data = get_my_data_ptr(get_dispatch_key(commandBuffer), layer_data_map);
Jeremy Hayesb9e99232016-04-13 16:20:24 -06007055 std::unique_lock<std::mutex> lock(global_lock);
Tobin Ehlis5b5e7bc2016-03-09 16:12:48 -07007056 GLOBAL_CB_NODE *pCB = getCBNode(dev_data, commandBuffer);
7057 if (pCB) {
7058 skipCall |= addCmd(dev_data, pCB, CMD_SETDEPTHBIASSTATE, "vkCmdSetDepthBias()");
7059 pCB->status |= CBSTATUS_DEPTH_BIAS_SET;
Tobin Ehlis5b5e7bc2016-03-09 16:12:48 -07007060 }
Jeremy Hayesb9e99232016-04-13 16:20:24 -06007061 lock.unlock();
Dustin Gravese3319182016-04-05 09:41:17 -06007062 if (!skipCall)
Tobin Ehlis5b5e7bc2016-03-09 16:12:48 -07007063 dev_data->device_dispatch_table->CmdSetDepthBias(commandBuffer, depthBiasConstantFactor, depthBiasClamp,
7064 depthBiasSlopeFactor);
7065}
7066
7067VK_LAYER_EXPORT VKAPI_ATTR void VKAPI_CALL vkCmdSetBlendConstants(VkCommandBuffer commandBuffer, const float blendConstants[4]) {
Dustin Gravese3319182016-04-05 09:41:17 -06007068 bool skipCall = false;
Tobin Ehlis5b5e7bc2016-03-09 16:12:48 -07007069 layer_data *dev_data = get_my_data_ptr(get_dispatch_key(commandBuffer), layer_data_map);
Jeremy Hayesb9e99232016-04-13 16:20:24 -06007070 std::unique_lock<std::mutex> lock(global_lock);
Tobin Ehlis5b5e7bc2016-03-09 16:12:48 -07007071 GLOBAL_CB_NODE *pCB = getCBNode(dev_data, commandBuffer);
7072 if (pCB) {
7073 skipCall |= addCmd(dev_data, pCB, CMD_SETBLENDSTATE, "vkCmdSetBlendConstants()");
Tobin Ehlis3d3e06b2016-03-28 11:18:19 -06007074 pCB->status |= CBSTATUS_BLEND_CONSTANTS_SET;
Tobin Ehlis5b5e7bc2016-03-09 16:12:48 -07007075 }
Jeremy Hayesb9e99232016-04-13 16:20:24 -06007076 lock.unlock();
Dustin Gravese3319182016-04-05 09:41:17 -06007077 if (!skipCall)
Tobin Ehlis5b5e7bc2016-03-09 16:12:48 -07007078 dev_data->device_dispatch_table->CmdSetBlendConstants(commandBuffer, blendConstants);
7079}
7080
7081VK_LAYER_EXPORT VKAPI_ATTR void VKAPI_CALL
7082vkCmdSetDepthBounds(VkCommandBuffer commandBuffer, float minDepthBounds, float maxDepthBounds) {
Dustin Gravese3319182016-04-05 09:41:17 -06007083 bool skipCall = false;
Tobin Ehlis5b5e7bc2016-03-09 16:12:48 -07007084 layer_data *dev_data = get_my_data_ptr(get_dispatch_key(commandBuffer), layer_data_map);
Jeremy Hayesb9e99232016-04-13 16:20:24 -06007085 std::unique_lock<std::mutex> lock(global_lock);
Tobin Ehlis5b5e7bc2016-03-09 16:12:48 -07007086 GLOBAL_CB_NODE *pCB = getCBNode(dev_data, commandBuffer);
7087 if (pCB) {
7088 skipCall |= addCmd(dev_data, pCB, CMD_SETDEPTHBOUNDSSTATE, "vkCmdSetDepthBounds()");
7089 pCB->status |= CBSTATUS_DEPTH_BOUNDS_SET;
Tobin Ehlis5b5e7bc2016-03-09 16:12:48 -07007090 }
Jeremy Hayesb9e99232016-04-13 16:20:24 -06007091 lock.unlock();
Dustin Gravese3319182016-04-05 09:41:17 -06007092 if (!skipCall)
Tobin Ehlis5b5e7bc2016-03-09 16:12:48 -07007093 dev_data->device_dispatch_table->CmdSetDepthBounds(commandBuffer, minDepthBounds, maxDepthBounds);
7094}
7095
7096VK_LAYER_EXPORT VKAPI_ATTR void VKAPI_CALL
7097vkCmdSetStencilCompareMask(VkCommandBuffer commandBuffer, VkStencilFaceFlags faceMask, uint32_t compareMask) {
Dustin Gravese3319182016-04-05 09:41:17 -06007098 bool skipCall = false;
Tobin Ehlis5b5e7bc2016-03-09 16:12:48 -07007099 layer_data *dev_data = get_my_data_ptr(get_dispatch_key(commandBuffer), layer_data_map);
Jeremy Hayesb9e99232016-04-13 16:20:24 -06007100 std::unique_lock<std::mutex> lock(global_lock);
Tobin Ehlis5b5e7bc2016-03-09 16:12:48 -07007101 GLOBAL_CB_NODE *pCB = getCBNode(dev_data, commandBuffer);
7102 if (pCB) {
7103 skipCall |= addCmd(dev_data, pCB, CMD_SETSTENCILREADMASKSTATE, "vkCmdSetStencilCompareMask()");
Tobin Ehlis5b5e7bc2016-03-09 16:12:48 -07007104 pCB->status |= CBSTATUS_STENCIL_READ_MASK_SET;
7105 }
Jeremy Hayesb9e99232016-04-13 16:20:24 -06007106 lock.unlock();
Dustin Gravese3319182016-04-05 09:41:17 -06007107 if (!skipCall)
Tobin Ehlis5b5e7bc2016-03-09 16:12:48 -07007108 dev_data->device_dispatch_table->CmdSetStencilCompareMask(commandBuffer, faceMask, compareMask);
7109}
7110
7111VK_LAYER_EXPORT VKAPI_ATTR void VKAPI_CALL
7112vkCmdSetStencilWriteMask(VkCommandBuffer commandBuffer, VkStencilFaceFlags faceMask, uint32_t writeMask) {
Dustin Gravese3319182016-04-05 09:41:17 -06007113 bool skipCall = false;
Tobin Ehlis5b5e7bc2016-03-09 16:12:48 -07007114 layer_data *dev_data = get_my_data_ptr(get_dispatch_key(commandBuffer), layer_data_map);
Jeremy Hayesb9e99232016-04-13 16:20:24 -06007115 std::unique_lock<std::mutex> lock(global_lock);
Tobin Ehlis5b5e7bc2016-03-09 16:12:48 -07007116 GLOBAL_CB_NODE *pCB = getCBNode(dev_data, commandBuffer);
7117 if (pCB) {
7118 skipCall |= addCmd(dev_data, pCB, CMD_SETSTENCILWRITEMASKSTATE, "vkCmdSetStencilWriteMask()");
Tobin Ehlis5b5e7bc2016-03-09 16:12:48 -07007119 pCB->status |= CBSTATUS_STENCIL_WRITE_MASK_SET;
7120 }
Jeremy Hayesb9e99232016-04-13 16:20:24 -06007121 lock.unlock();
Dustin Gravese3319182016-04-05 09:41:17 -06007122 if (!skipCall)
Tobin Ehlis5b5e7bc2016-03-09 16:12:48 -07007123 dev_data->device_dispatch_table->CmdSetStencilWriteMask(commandBuffer, faceMask, writeMask);
7124}
7125
7126VK_LAYER_EXPORT VKAPI_ATTR void VKAPI_CALL
7127vkCmdSetStencilReference(VkCommandBuffer commandBuffer, VkStencilFaceFlags faceMask, uint32_t reference) {
Dustin Gravese3319182016-04-05 09:41:17 -06007128 bool skipCall = false;
Tobin Ehlis5b5e7bc2016-03-09 16:12:48 -07007129 layer_data *dev_data = get_my_data_ptr(get_dispatch_key(commandBuffer), layer_data_map);
Jeremy Hayesb9e99232016-04-13 16:20:24 -06007130 std::unique_lock<std::mutex> lock(global_lock);
Tobin Ehlis5b5e7bc2016-03-09 16:12:48 -07007131 GLOBAL_CB_NODE *pCB = getCBNode(dev_data, commandBuffer);
7132 if (pCB) {
7133 skipCall |= addCmd(dev_data, pCB, CMD_SETSTENCILREFERENCESTATE, "vkCmdSetStencilReference()");
Tobin Ehlis5b5e7bc2016-03-09 16:12:48 -07007134 pCB->status |= CBSTATUS_STENCIL_REFERENCE_SET;
7135 }
Jeremy Hayesb9e99232016-04-13 16:20:24 -06007136 lock.unlock();
Dustin Gravese3319182016-04-05 09:41:17 -06007137 if (!skipCall)
Tobin Ehlis5b5e7bc2016-03-09 16:12:48 -07007138 dev_data->device_dispatch_table->CmdSetStencilReference(commandBuffer, faceMask, reference);
7139}
7140
7141VK_LAYER_EXPORT VKAPI_ATTR void VKAPI_CALL
7142vkCmdBindDescriptorSets(VkCommandBuffer commandBuffer, VkPipelineBindPoint pipelineBindPoint, VkPipelineLayout layout,
7143 uint32_t firstSet, uint32_t setCount, const VkDescriptorSet *pDescriptorSets, uint32_t dynamicOffsetCount,
7144 const uint32_t *pDynamicOffsets) {
Dustin Gravese3319182016-04-05 09:41:17 -06007145 bool skipCall = false;
Tobin Ehlis5b5e7bc2016-03-09 16:12:48 -07007146 layer_data *dev_data = get_my_data_ptr(get_dispatch_key(commandBuffer), layer_data_map);
Jeremy Hayesb9e99232016-04-13 16:20:24 -06007147 std::unique_lock<std::mutex> lock(global_lock);
Tobin Ehlis5b5e7bc2016-03-09 16:12:48 -07007148 GLOBAL_CB_NODE *pCB = getCBNode(dev_data, commandBuffer);
7149 if (pCB) {
7150 if (pCB->state == CB_RECORDING) {
Tobin Ehlis787f29d2016-03-17 13:37:40 -06007151 // Track total count of dynamic descriptor types to make sure we have an offset for each one
7152 uint32_t totalDynamicDescriptors = 0;
7153 string errorString = "";
7154 uint32_t lastSetIndex = firstSet + setCount - 1;
Tobin Ehlis72d66f02016-03-21 14:14:44 -06007155 if (lastSetIndex >= pCB->lastBound[pipelineBindPoint].boundDescriptorSets.size())
7156 pCB->lastBound[pipelineBindPoint].boundDescriptorSets.resize(lastSetIndex + 1);
7157 VkDescriptorSet oldFinalBoundSet = pCB->lastBound[pipelineBindPoint].boundDescriptorSets[lastSetIndex];
Tobin Ehlis787f29d2016-03-17 13:37:40 -06007158 for (uint32_t i = 0; i < setCount; i++) {
7159 SET_NODE *pSet = getSetNode(dev_data, pDescriptorSets[i]);
7160 if (pSet) {
Tobin Ehlis72d66f02016-03-21 14:14:44 -06007161 pCB->lastBound[pipelineBindPoint].uniqueBoundSets.insert(pDescriptorSets[i]);
Tobin Ehlis787f29d2016-03-17 13:37:40 -06007162 pSet->boundCmdBuffers.insert(commandBuffer);
Tobin Ehlis72d66f02016-03-21 14:14:44 -06007163 pCB->lastBound[pipelineBindPoint].pipelineLayout = layout;
7164 pCB->lastBound[pipelineBindPoint].boundDescriptorSets[i + firstSet] = pDescriptorSets[i];
Tobin Ehlis787f29d2016-03-17 13:37:40 -06007165 skipCall |= log_msg(dev_data->report_data, VK_DEBUG_REPORT_INFORMATION_BIT_EXT,
7166 VK_DEBUG_REPORT_OBJECT_TYPE_DESCRIPTOR_SET_EXT, (uint64_t)pDescriptorSets[i], __LINE__,
7167 DRAWSTATE_NONE, "DS", "DS %#" PRIxLEAST64 " bound on pipeline %s",
7168 (uint64_t)pDescriptorSets[i], string_VkPipelineBindPoint(pipelineBindPoint));
7169 if (!pSet->pUpdateStructs && (pSet->descriptorCount != 0)) {
7170 skipCall |= log_msg(dev_data->report_data, VK_DEBUG_REPORT_WARNING_BIT_EXT,
7171 VK_DEBUG_REPORT_OBJECT_TYPE_DESCRIPTOR_SET_EXT, (uint64_t)pDescriptorSets[i],
7172 __LINE__, DRAWSTATE_DESCRIPTOR_SET_NOT_UPDATED, "DS",
7173 "DS %#" PRIxLEAST64
7174 " bound but it was never updated. You may want to either update it or not bind it.",
Tobin Ehlis5b5e7bc2016-03-09 16:12:48 -07007175 (uint64_t)pDescriptorSets[i]);
7176 }
Tobin Ehlis787f29d2016-03-17 13:37:40 -06007177 // Verify that set being bound is compatible with overlapping setLayout of pipelineLayout
7178 if (!verify_set_layout_compatibility(dev_data, pSet, layout, i + firstSet, errorString)) {
7179 skipCall |= log_msg(dev_data->report_data, VK_DEBUG_REPORT_ERROR_BIT_EXT,
7180 VK_DEBUG_REPORT_OBJECT_TYPE_DESCRIPTOR_SET_EXT, (uint64_t)pDescriptorSets[i],
7181 __LINE__, DRAWSTATE_PIPELINE_LAYOUTS_INCOMPATIBLE, "DS",
7182 "descriptorSet #%u being bound is not compatible with overlapping layout in "
7183 "pipelineLayout due to: %s",
7184 i, errorString.c_str());
7185 }
7186 if (pSet->pLayout->dynamicDescriptorCount) {
7187 // First make sure we won't overstep bounds of pDynamicOffsets array
7188 if ((totalDynamicDescriptors + pSet->pLayout->dynamicDescriptorCount) > dynamicOffsetCount) {
7189 skipCall |=
7190 log_msg(dev_data->report_data, VK_DEBUG_REPORT_ERROR_BIT_EXT,
7191 VK_DEBUG_REPORT_OBJECT_TYPE_DESCRIPTOR_SET_EXT, (uint64_t)pDescriptorSets[i], __LINE__,
7192 DRAWSTATE_INVALID_DYNAMIC_OFFSET_COUNT, "DS",
7193 "descriptorSet #%u (%#" PRIxLEAST64
7194 ") requires %u dynamicOffsets, but only %u dynamicOffsets are left in pDynamicOffsets "
7195 "array. There must be one dynamic offset for each dynamic descriptor being bound.",
7196 i, (uint64_t)pDescriptorSets[i], pSet->pLayout->dynamicDescriptorCount,
7197 (dynamicOffsetCount - totalDynamicDescriptors));
7198 } else { // Validate and store dynamic offsets with the set
7199 // Validate Dynamic Offset Minimums
7200 uint32_t cur_dyn_offset = totalDynamicDescriptors;
7201 for (uint32_t d = 0; d < pSet->descriptorCount; d++) {
7202 if (pSet->pLayout->descriptorTypes[d] == VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER_DYNAMIC) {
7203 if (vk_safe_modulo(
7204 pDynamicOffsets[cur_dyn_offset],
Tobin Ehlisb1b606d2016-04-11 14:49:55 -06007205 dev_data->phys_dev_properties.properties.limits.minUniformBufferOffsetAlignment) != 0) {
Tobin Ehlis787f29d2016-03-17 13:37:40 -06007206 skipCall |= log_msg(
7207 dev_data->report_data, VK_DEBUG_REPORT_ERROR_BIT_EXT,
7208 VK_DEBUG_REPORT_OBJECT_TYPE_PHYSICAL_DEVICE_EXT, 0, __LINE__,
7209 DRAWSTATE_INVALID_UNIFORM_BUFFER_OFFSET, "DS",
7210 "vkCmdBindDescriptorSets(): pDynamicOffsets[%d] is %d but must be a multiple of "
7211 "device limit minUniformBufferOffsetAlignment %#" PRIxLEAST64,
7212 cur_dyn_offset, pDynamicOffsets[cur_dyn_offset],
Tobin Ehlisb1b606d2016-04-11 14:49:55 -06007213 dev_data->phys_dev_properties.properties.limits.minUniformBufferOffsetAlignment);
Tobin Ehlis787f29d2016-03-17 13:37:40 -06007214 }
7215 cur_dyn_offset++;
7216 } else if (pSet->pLayout->descriptorTypes[d] == VK_DESCRIPTOR_TYPE_STORAGE_BUFFER_DYNAMIC) {
7217 if (vk_safe_modulo(
7218 pDynamicOffsets[cur_dyn_offset],
Tobin Ehlisb1b606d2016-04-11 14:49:55 -06007219 dev_data->phys_dev_properties.properties.limits.minStorageBufferOffsetAlignment) != 0) {
Tobin Ehlis787f29d2016-03-17 13:37:40 -06007220 skipCall |= log_msg(
7221 dev_data->report_data, VK_DEBUG_REPORT_ERROR_BIT_EXT,
7222 VK_DEBUG_REPORT_OBJECT_TYPE_PHYSICAL_DEVICE_EXT, 0, __LINE__,
7223 DRAWSTATE_INVALID_STORAGE_BUFFER_OFFSET, "DS",
7224 "vkCmdBindDescriptorSets(): pDynamicOffsets[%d] is %d but must be a multiple of "
7225 "device limit minStorageBufferOffsetAlignment %#" PRIxLEAST64,
7226 cur_dyn_offset, pDynamicOffsets[cur_dyn_offset],
Tobin Ehlisb1b606d2016-04-11 14:49:55 -06007227 dev_data->phys_dev_properties.properties.limits.minStorageBufferOffsetAlignment);
Tobin Ehlis787f29d2016-03-17 13:37:40 -06007228 }
7229 cur_dyn_offset++;
7230 }
7231 }
7232 // Keep running total of dynamic descriptor count to verify at the end
7233 totalDynamicDescriptors += pSet->pLayout->dynamicDescriptorCount;
Tobin Ehlis5b5e7bc2016-03-09 16:12:48 -07007234 }
7235 }
Tobin Ehlis787f29d2016-03-17 13:37:40 -06007236 } else {
7237 skipCall |= log_msg(dev_data->report_data, VK_DEBUG_REPORT_ERROR_BIT_EXT,
7238 VK_DEBUG_REPORT_OBJECT_TYPE_DESCRIPTOR_SET_EXT, (uint64_t)pDescriptorSets[i], __LINE__,
7239 DRAWSTATE_INVALID_SET, "DS", "Attempt to bind DS %#" PRIxLEAST64 " that doesn't exist!",
7240 (uint64_t)pDescriptorSets[i]);
Tobin Ehlis5b5e7bc2016-03-09 16:12:48 -07007241 }
Tobin Ehlis72d66f02016-03-21 14:14:44 -06007242 skipCall |= addCmd(dev_data, pCB, CMD_BINDDESCRIPTORSETS, "vkCmdBindDescriptorSets()");
7243 // For any previously bound sets, need to set them to "invalid" if they were disturbed by this update
7244 if (firstSet > 0) { // Check set #s below the first bound set
7245 for (uint32_t i = 0; i < firstSet; ++i) {
7246 if (pCB->lastBound[pipelineBindPoint].boundDescriptorSets[i] &&
7247 !verify_set_layout_compatibility(
7248 dev_data, dev_data->setMap[pCB->lastBound[pipelineBindPoint].boundDescriptorSets[i]], layout, i,
7249 errorString)) {
7250 skipCall |= log_msg(
7251 dev_data->report_data, VK_DEBUG_REPORT_PERFORMANCE_WARNING_BIT_EXT,
7252 VK_DEBUG_REPORT_OBJECT_TYPE_DESCRIPTOR_SET_EXT,
7253 (uint64_t)pCB->lastBound[pipelineBindPoint].boundDescriptorSets[i], __LINE__, DRAWSTATE_NONE, "DS",
7254 "DescriptorSetDS %#" PRIxLEAST64
7255 " previously bound as set #%u was disturbed by newly bound pipelineLayout (%#" PRIxLEAST64 ")",
7256 (uint64_t)pCB->lastBound[pipelineBindPoint].boundDescriptorSets[i], i, (uint64_t)layout);
7257 pCB->lastBound[pipelineBindPoint].boundDescriptorSets[i] = VK_NULL_HANDLE;
7258 }
Tobin Ehlis5b5e7bc2016-03-09 16:12:48 -07007259 }
7260 }
Tobin Ehlis72d66f02016-03-21 14:14:44 -06007261 // Check if newly last bound set invalidates any remaining bound sets
7262 if ((pCB->lastBound[pipelineBindPoint].boundDescriptorSets.size() - 1) > (lastSetIndex)) {
7263 if (oldFinalBoundSet &&
7264 !verify_set_layout_compatibility(dev_data, dev_data->setMap[oldFinalBoundSet], layout, lastSetIndex,
7265 errorString)) {
7266 skipCall |=
7267 log_msg(dev_data->report_data, VK_DEBUG_REPORT_PERFORMANCE_WARNING_BIT_EXT,
7268 VK_DEBUG_REPORT_OBJECT_TYPE_DESCRIPTOR_SET_EXT, (uint64_t)oldFinalBoundSet, __LINE__,
7269 DRAWSTATE_NONE, "DS", "DescriptorSetDS %#" PRIxLEAST64
7270 " previously bound as set #%u is incompatible with set %#" PRIxLEAST64
7271 " newly bound as set #%u so set #%u and any subsequent sets were "
7272 "disturbed by newly bound pipelineLayout (%#" PRIxLEAST64 ")",
7273 (uint64_t)oldFinalBoundSet, lastSetIndex,
7274 (uint64_t)pCB->lastBound[pipelineBindPoint].boundDescriptorSets[lastSetIndex], lastSetIndex,
7275 lastSetIndex + 1, (uint64_t)layout);
7276 pCB->lastBound[pipelineBindPoint].boundDescriptorSets.resize(lastSetIndex + 1);
7277 }
7278 }
Tobin Ehlis787f29d2016-03-17 13:37:40 -06007279 }
7280 // dynamicOffsetCount must equal the total number of dynamic descriptors in the sets being bound
7281 if (totalDynamicDescriptors != dynamicOffsetCount) {
7282 skipCall |= log_msg(dev_data->report_data, VK_DEBUG_REPORT_ERROR_BIT_EXT,
7283 VK_DEBUG_REPORT_OBJECT_TYPE_COMMAND_BUFFER_EXT, (uint64_t)commandBuffer, __LINE__,
7284 DRAWSTATE_INVALID_DYNAMIC_OFFSET_COUNT, "DS",
7285 "Attempting to bind %u descriptorSets with %u dynamic descriptors, but dynamicOffsetCount "
7286 "is %u. It should exactly match the number of dynamic descriptors.",
7287 setCount, totalDynamicDescriptors, dynamicOffsetCount);
7288 }
7289 // Save dynamicOffsets bound to this CB
7290 for (uint32_t i = 0; i < dynamicOffsetCount; i++) {
Tobin Ehlise5767982016-03-30 09:32:19 -06007291 pCB->lastBound[pipelineBindPoint].dynamicOffsets.emplace_back(pDynamicOffsets[i]);
Tobin Ehlis5b5e7bc2016-03-09 16:12:48 -07007292 }
7293 } else {
7294 skipCall |= report_error_no_cb_begin(dev_data, commandBuffer, "vkCmdBindDescriptorSets()");
7295 }
7296 }
Jeremy Hayesb9e99232016-04-13 16:20:24 -06007297 lock.unlock();
Dustin Gravese3319182016-04-05 09:41:17 -06007298 if (!skipCall)
Tobin Ehlis5b5e7bc2016-03-09 16:12:48 -07007299 dev_data->device_dispatch_table->CmdBindDescriptorSets(commandBuffer, pipelineBindPoint, layout, firstSet, setCount,
7300 pDescriptorSets, dynamicOffsetCount, pDynamicOffsets);
7301}
7302
7303VK_LAYER_EXPORT VKAPI_ATTR void VKAPI_CALL
7304vkCmdBindIndexBuffer(VkCommandBuffer commandBuffer, VkBuffer buffer, VkDeviceSize offset, VkIndexType indexType) {
Dustin Gravese3319182016-04-05 09:41:17 -06007305 bool skipCall = false;
Tobin Ehlis5b5e7bc2016-03-09 16:12:48 -07007306 layer_data *dev_data = get_my_data_ptr(get_dispatch_key(commandBuffer), layer_data_map);
Jeremy Hayesb9e99232016-04-13 16:20:24 -06007307 std::unique_lock<std::mutex> lock(global_lock);
Mark Lobodzinski03b71512016-03-23 14:33:02 -06007308#if MTMERGESOURCE
Tobin Ehlis5b5e7bc2016-03-09 16:12:48 -07007309 VkDeviceMemory mem;
7310 skipCall =
Chris Forbes0a1ce3d2016-04-06 15:16:26 +12007311 get_mem_binding_from_object(dev_data, (uint64_t)buffer, VK_DEBUG_REPORT_OBJECT_TYPE_BUFFER_EXT, &mem);
Tobin Ehlis72d66f02016-03-21 14:14:44 -06007312 auto cb_data = dev_data->commandBufferMap.find(commandBuffer);
7313 if (cb_data != dev_data->commandBufferMap.end()) {
Dustin Gravese3319182016-04-05 09:41:17 -06007314 std::function<bool()> function = [=]() { return validate_memory_is_valid(dev_data, mem, "vkCmdBindIndexBuffer()"); };
Tobin Ehlis72d66f02016-03-21 14:14:44 -06007315 cb_data->second->validate_functions.push_back(function);
Tobin Ehlis5b5e7bc2016-03-09 16:12:48 -07007316 }
7317 // TODO : Somewhere need to verify that IBs have correct usage state flagged
7318#endif
7319 GLOBAL_CB_NODE *pCB = getCBNode(dev_data, commandBuffer);
7320 if (pCB) {
7321 skipCall |= addCmd(dev_data, pCB, CMD_BINDINDEXBUFFER, "vkCmdBindIndexBuffer()");
7322 VkDeviceSize offset_align = 0;
7323 switch (indexType) {
7324 case VK_INDEX_TYPE_UINT16:
7325 offset_align = 2;
7326 break;
7327 case VK_INDEX_TYPE_UINT32:
7328 offset_align = 4;
7329 break;
7330 default:
7331 // ParamChecker should catch bad enum, we'll also throw alignment error below if offset_align stays 0
7332 break;
7333 }
7334 if (!offset_align || (offset % offset_align)) {
7335 skipCall |= log_msg(dev_data->report_data, VK_DEBUG_REPORT_ERROR_BIT_EXT, (VkDebugReportObjectTypeEXT)0, 0, __LINE__,
7336 DRAWSTATE_VTX_INDEX_ALIGNMENT_ERROR, "DS",
7337 "vkCmdBindIndexBuffer() offset (%#" PRIxLEAST64 ") does not fall on alignment (%s) boundary.",
7338 offset, string_VkIndexType(indexType));
7339 }
7340 pCB->status |= CBSTATUS_INDEX_BUFFER_BOUND;
7341 }
Jeremy Hayesb9e99232016-04-13 16:20:24 -06007342 lock.unlock();
Dustin Gravese3319182016-04-05 09:41:17 -06007343 if (!skipCall)
Tobin Ehlis5b5e7bc2016-03-09 16:12:48 -07007344 dev_data->device_dispatch_table->CmdBindIndexBuffer(commandBuffer, buffer, offset, indexType);
7345}
7346
7347void updateResourceTracking(GLOBAL_CB_NODE *pCB, uint32_t firstBinding, uint32_t bindingCount, const VkBuffer *pBuffers) {
7348 uint32_t end = firstBinding + bindingCount;
7349 if (pCB->currentDrawData.buffers.size() < end) {
7350 pCB->currentDrawData.buffers.resize(end);
7351 }
7352 for (uint32_t i = 0; i < bindingCount; ++i) {
7353 pCB->currentDrawData.buffers[i + firstBinding] = pBuffers[i];
7354 }
7355}
7356
Dustin Gravese3319182016-04-05 09:41:17 -06007357static inline void updateResourceTrackingOnDraw(GLOBAL_CB_NODE *pCB) { pCB->drawData.push_back(pCB->currentDrawData); }
Tobin Ehlis5b5e7bc2016-03-09 16:12:48 -07007358
7359VK_LAYER_EXPORT VKAPI_ATTR void VKAPI_CALL vkCmdBindVertexBuffers(VkCommandBuffer commandBuffer, uint32_t firstBinding,
7360 uint32_t bindingCount, const VkBuffer *pBuffers,
7361 const VkDeviceSize *pOffsets) {
Dustin Gravese3319182016-04-05 09:41:17 -06007362 bool skipCall = false;
Tobin Ehlis5b5e7bc2016-03-09 16:12:48 -07007363 layer_data *dev_data = get_my_data_ptr(get_dispatch_key(commandBuffer), layer_data_map);
Jeremy Hayesb9e99232016-04-13 16:20:24 -06007364 std::unique_lock<std::mutex> lock(global_lock);
Mark Lobodzinski03b71512016-03-23 14:33:02 -06007365#if MTMERGESOURCE
Tobin Ehlis5b5e7bc2016-03-09 16:12:48 -07007366 for (uint32_t i = 0; i < bindingCount; ++i) {
7367 VkDeviceMemory mem;
Chris Forbes0a1ce3d2016-04-06 15:16:26 +12007368 skipCall |= get_mem_binding_from_object(dev_data, (uint64_t)pBuffers[i], VK_DEBUG_REPORT_OBJECT_TYPE_BUFFER_EXT, &mem);
Tobin Ehlis72d66f02016-03-21 14:14:44 -06007369 auto cb_data = dev_data->commandBufferMap.find(commandBuffer);
7370 if (cb_data != dev_data->commandBufferMap.end()) {
Dustin Gravese3319182016-04-05 09:41:17 -06007371 std::function<bool()> function = [=]() { return validate_memory_is_valid(dev_data, mem, "vkCmdBindVertexBuffers()"); };
Tobin Ehlis72d66f02016-03-21 14:14:44 -06007372 cb_data->second->validate_functions.push_back(function);
Tobin Ehlis5b5e7bc2016-03-09 16:12:48 -07007373 }
7374 }
7375 // TODO : Somewhere need to verify that VBs have correct usage state flagged
7376#endif
7377 GLOBAL_CB_NODE *pCB = getCBNode(dev_data, commandBuffer);
7378 if (pCB) {
7379 addCmd(dev_data, pCB, CMD_BINDVERTEXBUFFER, "vkCmdBindVertexBuffer()");
7380 updateResourceTracking(pCB, firstBinding, bindingCount, pBuffers);
7381 } else {
7382 skipCall |= report_error_no_cb_begin(dev_data, commandBuffer, "vkCmdBindVertexBuffer()");
7383 }
Jeremy Hayesb9e99232016-04-13 16:20:24 -06007384 lock.unlock();
Dustin Gravese3319182016-04-05 09:41:17 -06007385 if (!skipCall)
Tobin Ehlis5b5e7bc2016-03-09 16:12:48 -07007386 dev_data->device_dispatch_table->CmdBindVertexBuffers(commandBuffer, firstBinding, bindingCount, pBuffers, pOffsets);
7387}
7388
Jeremy Hayesb9e99232016-04-13 16:20:24 -06007389/* expects global_lock to be held by caller */
Dustin Gravese3319182016-04-05 09:41:17 -06007390static bool markStoreImagesAndBuffersAsWritten(layer_data *dev_data, GLOBAL_CB_NODE *pCB) {
Tobin Ehlis5b5e7bc2016-03-09 16:12:48 -07007391 bool skip_call = false;
Tobin Ehlis7a3985a2016-03-25 11:49:51 -06007392
7393 for (auto imageView : pCB->updateImages) {
7394 auto iv_data = dev_data->imageViewMap.find(imageView);
7395 if (iv_data == dev_data->imageViewMap.end())
Tobin Ehlis5b5e7bc2016-03-09 16:12:48 -07007396 continue;
Tobin Ehlis7a3985a2016-03-25 11:49:51 -06007397 VkImage image = iv_data->second.image;
7398 VkDeviceMemory mem;
7399 skip_call |=
Chris Forbes0a1ce3d2016-04-06 15:16:26 +12007400 get_mem_binding_from_object(dev_data, (uint64_t)image, VK_DEBUG_REPORT_OBJECT_TYPE_IMAGE_EXT, &mem);
Dustin Gravese3319182016-04-05 09:41:17 -06007401 std::function<bool()> function = [=]() {
Tobin Ehlis7a3985a2016-03-25 11:49:51 -06007402 set_memory_valid(dev_data, mem, true, image);
Dustin Gravese3319182016-04-05 09:41:17 -06007403 return false;
Tobin Ehlis7a3985a2016-03-25 11:49:51 -06007404 };
7405 pCB->validate_functions.push_back(function);
7406 }
7407 for (auto buffer : pCB->updateBuffers) {
7408 VkDeviceMemory mem;
Chris Forbes0a1ce3d2016-04-06 15:16:26 +12007409 skip_call |= get_mem_binding_from_object(dev_data, (uint64_t)buffer,
Tobin Ehlis7a3985a2016-03-25 11:49:51 -06007410 VK_DEBUG_REPORT_OBJECT_TYPE_BUFFER_EXT, &mem);
Dustin Gravese3319182016-04-05 09:41:17 -06007411 std::function<bool()> function = [=]() {
Tobin Ehlis7a3985a2016-03-25 11:49:51 -06007412 set_memory_valid(dev_data, mem, true);
Dustin Gravese3319182016-04-05 09:41:17 -06007413 return false;
Tobin Ehlis7a3985a2016-03-25 11:49:51 -06007414 };
7415 pCB->validate_functions.push_back(function);
Tobin Ehlis5b5e7bc2016-03-09 16:12:48 -07007416 }
7417 return skip_call;
7418}
Tobin Ehlis5b5e7bc2016-03-09 16:12:48 -07007419
7420VK_LAYER_EXPORT VKAPI_ATTR void VKAPI_CALL vkCmdDraw(VkCommandBuffer commandBuffer, uint32_t vertexCount, uint32_t instanceCount,
7421 uint32_t firstVertex, uint32_t firstInstance) {
Dustin Gravese3319182016-04-05 09:41:17 -06007422 bool skipCall = false;
Tobin Ehlis5b5e7bc2016-03-09 16:12:48 -07007423 layer_data *dev_data = get_my_data_ptr(get_dispatch_key(commandBuffer), layer_data_map);
Jeremy Hayesb9e99232016-04-13 16:20:24 -06007424 std::unique_lock<std::mutex> lock(global_lock);
Tobin Ehlis5b5e7bc2016-03-09 16:12:48 -07007425 GLOBAL_CB_NODE *pCB = getCBNode(dev_data, commandBuffer);
7426 if (pCB) {
7427 skipCall |= addCmd(dev_data, pCB, CMD_DRAW, "vkCmdDraw()");
7428 pCB->drawCount[DRAW]++;
Dustin Gravese3319182016-04-05 09:41:17 -06007429 skipCall |= validate_and_update_draw_state(dev_data, pCB, false, VK_PIPELINE_BIND_POINT_GRAPHICS);
Tobin Ehlis7a3985a2016-03-25 11:49:51 -06007430 skipCall |= markStoreImagesAndBuffersAsWritten(dev_data, pCB);
Tobin Ehlis5b5e7bc2016-03-09 16:12:48 -07007431 // TODO : Need to pass commandBuffer as srcObj here
7432 skipCall |=
7433 log_msg(dev_data->report_data, VK_DEBUG_REPORT_INFORMATION_BIT_EXT, VK_DEBUG_REPORT_OBJECT_TYPE_COMMAND_BUFFER_EXT, 0,
7434 __LINE__, DRAWSTATE_NONE, "DS", "vkCmdDraw() call #%" PRIu64 ", reporting DS state:", g_drawCount[DRAW]++);
7435 skipCall |= synchAndPrintDSConfig(dev_data, commandBuffer);
Dustin Gravese3319182016-04-05 09:41:17 -06007436 if (!skipCall) {
Tobin Ehlis5b5e7bc2016-03-09 16:12:48 -07007437 updateResourceTrackingOnDraw(pCB);
7438 }
7439 skipCall |= outsideRenderPass(dev_data, pCB, "vkCmdDraw");
7440 }
Jeremy Hayesb9e99232016-04-13 16:20:24 -06007441 lock.unlock();
Dustin Gravese3319182016-04-05 09:41:17 -06007442 if (!skipCall)
Tobin Ehlis5b5e7bc2016-03-09 16:12:48 -07007443 dev_data->device_dispatch_table->CmdDraw(commandBuffer, vertexCount, instanceCount, firstVertex, firstInstance);
7444}
7445
7446VK_LAYER_EXPORT VKAPI_ATTR void VKAPI_CALL vkCmdDrawIndexed(VkCommandBuffer commandBuffer, uint32_t indexCount,
7447 uint32_t instanceCount, uint32_t firstIndex, int32_t vertexOffset,
7448 uint32_t firstInstance) {
7449 layer_data *dev_data = get_my_data_ptr(get_dispatch_key(commandBuffer), layer_data_map);
Dustin Gravese3319182016-04-05 09:41:17 -06007450 bool skipCall = false;
Jeremy Hayesb9e99232016-04-13 16:20:24 -06007451 std::unique_lock<std::mutex> lock(global_lock);
Tobin Ehlis5b5e7bc2016-03-09 16:12:48 -07007452 GLOBAL_CB_NODE *pCB = getCBNode(dev_data, commandBuffer);
7453 if (pCB) {
7454 skipCall |= addCmd(dev_data, pCB, CMD_DRAWINDEXED, "vkCmdDrawIndexed()");
7455 pCB->drawCount[DRAW_INDEXED]++;
Dustin Gravese3319182016-04-05 09:41:17 -06007456 skipCall |= validate_and_update_draw_state(dev_data, pCB, true, VK_PIPELINE_BIND_POINT_GRAPHICS);
Tobin Ehlis7a3985a2016-03-25 11:49:51 -06007457 skipCall |= markStoreImagesAndBuffersAsWritten(dev_data, pCB);
Tobin Ehlis5b5e7bc2016-03-09 16:12:48 -07007458 // TODO : Need to pass commandBuffer as srcObj here
7459 skipCall |= log_msg(dev_data->report_data, VK_DEBUG_REPORT_INFORMATION_BIT_EXT,
7460 VK_DEBUG_REPORT_OBJECT_TYPE_COMMAND_BUFFER_EXT, 0, __LINE__, DRAWSTATE_NONE, "DS",
7461 "vkCmdDrawIndexed() call #%" PRIu64 ", reporting DS state:", g_drawCount[DRAW_INDEXED]++);
7462 skipCall |= synchAndPrintDSConfig(dev_data, commandBuffer);
Dustin Gravese3319182016-04-05 09:41:17 -06007463 if (!skipCall) {
Tobin Ehlis5b5e7bc2016-03-09 16:12:48 -07007464 updateResourceTrackingOnDraw(pCB);
7465 }
7466 skipCall |= outsideRenderPass(dev_data, pCB, "vkCmdDrawIndexed");
7467 }
Jeremy Hayesb9e99232016-04-13 16:20:24 -06007468 lock.unlock();
Dustin Gravese3319182016-04-05 09:41:17 -06007469 if (!skipCall)
Tobin Ehlis5b5e7bc2016-03-09 16:12:48 -07007470 dev_data->device_dispatch_table->CmdDrawIndexed(commandBuffer, indexCount, instanceCount, firstIndex, vertexOffset,
7471 firstInstance);
7472}
7473
7474VK_LAYER_EXPORT VKAPI_ATTR void VKAPI_CALL
7475vkCmdDrawIndirect(VkCommandBuffer commandBuffer, VkBuffer buffer, VkDeviceSize offset, uint32_t count, uint32_t stride) {
7476 layer_data *dev_data = get_my_data_ptr(get_dispatch_key(commandBuffer), layer_data_map);
Dustin Gravese3319182016-04-05 09:41:17 -06007477 bool skipCall = false;
Jeremy Hayesb9e99232016-04-13 16:20:24 -06007478 std::unique_lock<std::mutex> lock(global_lock);
Mark Lobodzinski03b71512016-03-23 14:33:02 -06007479#if MTMERGESOURCE
Tobin Ehlis5b5e7bc2016-03-09 16:12:48 -07007480 VkDeviceMemory mem;
7481 // MTMTODO : merge with code below
7482 skipCall =
Chris Forbes0a1ce3d2016-04-06 15:16:26 +12007483 get_mem_binding_from_object(dev_data, (uint64_t)buffer, VK_DEBUG_REPORT_OBJECT_TYPE_BUFFER_EXT, &mem);
Tobin Ehlis5b5e7bc2016-03-09 16:12:48 -07007484 skipCall |= update_cmd_buf_and_mem_references(dev_data, commandBuffer, mem, "vkCmdDrawIndirect");
Tobin Ehlis5b5e7bc2016-03-09 16:12:48 -07007485#endif
7486 GLOBAL_CB_NODE *pCB = getCBNode(dev_data, commandBuffer);
7487 if (pCB) {
7488 skipCall |= addCmd(dev_data, pCB, CMD_DRAWINDIRECT, "vkCmdDrawIndirect()");
7489 pCB->drawCount[DRAW_INDIRECT]++;
Dustin Gravese3319182016-04-05 09:41:17 -06007490 skipCall |= validate_and_update_draw_state(dev_data, pCB, false, VK_PIPELINE_BIND_POINT_GRAPHICS);
Tobin Ehlis7a3985a2016-03-25 11:49:51 -06007491 skipCall |= markStoreImagesAndBuffersAsWritten(dev_data, pCB);
Tobin Ehlis5b5e7bc2016-03-09 16:12:48 -07007492 // TODO : Need to pass commandBuffer as srcObj here
7493 skipCall |= log_msg(dev_data->report_data, VK_DEBUG_REPORT_INFORMATION_BIT_EXT,
7494 VK_DEBUG_REPORT_OBJECT_TYPE_COMMAND_BUFFER_EXT, 0, __LINE__, DRAWSTATE_NONE, "DS",
7495 "vkCmdDrawIndirect() call #%" PRIu64 ", reporting DS state:", g_drawCount[DRAW_INDIRECT]++);
7496 skipCall |= synchAndPrintDSConfig(dev_data, commandBuffer);
Dustin Gravese3319182016-04-05 09:41:17 -06007497 if (!skipCall) {
Tobin Ehlis5b5e7bc2016-03-09 16:12:48 -07007498 updateResourceTrackingOnDraw(pCB);
7499 }
7500 skipCall |= outsideRenderPass(dev_data, pCB, "vkCmdDrawIndirect");
7501 }
Jeremy Hayesb9e99232016-04-13 16:20:24 -06007502 lock.unlock();
Dustin Gravese3319182016-04-05 09:41:17 -06007503 if (!skipCall)
Tobin Ehlis5b5e7bc2016-03-09 16:12:48 -07007504 dev_data->device_dispatch_table->CmdDrawIndirect(commandBuffer, buffer, offset, count, stride);
7505}
7506
7507VK_LAYER_EXPORT VKAPI_ATTR void VKAPI_CALL
7508vkCmdDrawIndexedIndirect(VkCommandBuffer commandBuffer, VkBuffer buffer, VkDeviceSize offset, uint32_t count, uint32_t stride) {
Dustin Gravese3319182016-04-05 09:41:17 -06007509 bool skipCall = false;
Tobin Ehlis5b5e7bc2016-03-09 16:12:48 -07007510 layer_data *dev_data = get_my_data_ptr(get_dispatch_key(commandBuffer), layer_data_map);
Jeremy Hayesb9e99232016-04-13 16:20:24 -06007511 std::unique_lock<std::mutex> lock(global_lock);
Mark Lobodzinski03b71512016-03-23 14:33:02 -06007512#if MTMERGESOURCE
Tobin Ehlis5b5e7bc2016-03-09 16:12:48 -07007513 VkDeviceMemory mem;
7514 // MTMTODO : merge with code below
7515 skipCall =
Chris Forbes0a1ce3d2016-04-06 15:16:26 +12007516 get_mem_binding_from_object(dev_data, (uint64_t)buffer, VK_DEBUG_REPORT_OBJECT_TYPE_BUFFER_EXT, &mem);
Tobin Ehlis5b5e7bc2016-03-09 16:12:48 -07007517 skipCall |= update_cmd_buf_and_mem_references(dev_data, commandBuffer, mem, "vkCmdDrawIndexedIndirect");
Tobin Ehlis5b5e7bc2016-03-09 16:12:48 -07007518#endif
7519 GLOBAL_CB_NODE *pCB = getCBNode(dev_data, commandBuffer);
7520 if (pCB) {
7521 skipCall |= addCmd(dev_data, pCB, CMD_DRAWINDEXEDINDIRECT, "vkCmdDrawIndexedIndirect()");
7522 pCB->drawCount[DRAW_INDEXED_INDIRECT]++;
Dustin Gravese3319182016-04-05 09:41:17 -06007523 skipCall |= validate_and_update_draw_state(dev_data, pCB, true, VK_PIPELINE_BIND_POINT_GRAPHICS);
Tobin Ehlis7a3985a2016-03-25 11:49:51 -06007524 skipCall |= markStoreImagesAndBuffersAsWritten(dev_data, pCB);
Tobin Ehlis5b5e7bc2016-03-09 16:12:48 -07007525 // TODO : Need to pass commandBuffer as srcObj here
7526 skipCall |=
7527 log_msg(dev_data->report_data, VK_DEBUG_REPORT_INFORMATION_BIT_EXT, VK_DEBUG_REPORT_OBJECT_TYPE_COMMAND_BUFFER_EXT, 0,
7528 __LINE__, DRAWSTATE_NONE, "DS", "vkCmdDrawIndexedIndirect() call #%" PRIu64 ", reporting DS state:",
7529 g_drawCount[DRAW_INDEXED_INDIRECT]++);
7530 skipCall |= synchAndPrintDSConfig(dev_data, commandBuffer);
Dustin Gravese3319182016-04-05 09:41:17 -06007531 if (!skipCall) {
Tobin Ehlis5b5e7bc2016-03-09 16:12:48 -07007532 updateResourceTrackingOnDraw(pCB);
7533 }
7534 skipCall |= outsideRenderPass(dev_data, pCB, "vkCmdDrawIndexedIndirect");
7535 }
Jeremy Hayesb9e99232016-04-13 16:20:24 -06007536 lock.unlock();
Dustin Gravese3319182016-04-05 09:41:17 -06007537 if (!skipCall)
Tobin Ehlis5b5e7bc2016-03-09 16:12:48 -07007538 dev_data->device_dispatch_table->CmdDrawIndexedIndirect(commandBuffer, buffer, offset, count, stride);
7539}
7540
7541VK_LAYER_EXPORT VKAPI_ATTR void VKAPI_CALL vkCmdDispatch(VkCommandBuffer commandBuffer, uint32_t x, uint32_t y, uint32_t z) {
Dustin Gravese3319182016-04-05 09:41:17 -06007542 bool skipCall = false;
Tobin Ehlis5b5e7bc2016-03-09 16:12:48 -07007543 layer_data *dev_data = get_my_data_ptr(get_dispatch_key(commandBuffer), layer_data_map);
Jeremy Hayesb9e99232016-04-13 16:20:24 -06007544 std::unique_lock<std::mutex> lock(global_lock);
Tobin Ehlis5b5e7bc2016-03-09 16:12:48 -07007545 GLOBAL_CB_NODE *pCB = getCBNode(dev_data, commandBuffer);
7546 if (pCB) {
Tobin Ehlis7a3985a2016-03-25 11:49:51 -06007547 // TODO : Re-enable validate_and_update_draw_state() when it supports compute shaders
Dustin Gravese3319182016-04-05 09:41:17 -06007548 // skipCall |= validate_and_update_draw_state(dev_data, pCB, false, VK_PIPELINE_BIND_POINT_COMPUTE);
Tobin Ehlis7a3985a2016-03-25 11:49:51 -06007549 // TODO : Call below is temporary until call above can be re-enabled
7550 update_shader_storage_images_and_buffers(dev_data, pCB);
7551 skipCall |= markStoreImagesAndBuffersAsWritten(dev_data, pCB);
Tobin Ehlis5b5e7bc2016-03-09 16:12:48 -07007552 skipCall |= addCmd(dev_data, pCB, CMD_DISPATCH, "vkCmdDispatch()");
7553 skipCall |= insideRenderPass(dev_data, pCB, "vkCmdDispatch");
7554 }
Jeremy Hayesb9e99232016-04-13 16:20:24 -06007555 lock.unlock();
Dustin Gravese3319182016-04-05 09:41:17 -06007556 if (!skipCall)
Tobin Ehlis5b5e7bc2016-03-09 16:12:48 -07007557 dev_data->device_dispatch_table->CmdDispatch(commandBuffer, x, y, z);
7558}
7559
7560VK_LAYER_EXPORT VKAPI_ATTR void VKAPI_CALL
7561vkCmdDispatchIndirect(VkCommandBuffer commandBuffer, VkBuffer buffer, VkDeviceSize offset) {
Dustin Gravese3319182016-04-05 09:41:17 -06007562 bool skipCall = false;
Tobin Ehlis5b5e7bc2016-03-09 16:12:48 -07007563 layer_data *dev_data = get_my_data_ptr(get_dispatch_key(commandBuffer), layer_data_map);
Jeremy Hayesb9e99232016-04-13 16:20:24 -06007564 std::unique_lock<std::mutex> lock(global_lock);
Mark Lobodzinski03b71512016-03-23 14:33:02 -06007565#if MTMERGESOURCE
Tobin Ehlis5b5e7bc2016-03-09 16:12:48 -07007566 VkDeviceMemory mem;
7567 skipCall =
Chris Forbes0a1ce3d2016-04-06 15:16:26 +12007568 get_mem_binding_from_object(dev_data, (uint64_t)buffer, VK_DEBUG_REPORT_OBJECT_TYPE_BUFFER_EXT, &mem);
Tobin Ehlis5b5e7bc2016-03-09 16:12:48 -07007569 skipCall |= update_cmd_buf_and_mem_references(dev_data, commandBuffer, mem, "vkCmdDispatchIndirect");
Tobin Ehlis5b5e7bc2016-03-09 16:12:48 -07007570#endif
7571 GLOBAL_CB_NODE *pCB = getCBNode(dev_data, commandBuffer);
7572 if (pCB) {
Tobin Ehlis7a3985a2016-03-25 11:49:51 -06007573 // TODO : Re-enable validate_and_update_draw_state() when it supports compute shaders
Dustin Gravese3319182016-04-05 09:41:17 -06007574 // skipCall |= validate_and_update_draw_state(dev_data, pCB, false, VK_PIPELINE_BIND_POINT_COMPUTE);
Tobin Ehlis7a3985a2016-03-25 11:49:51 -06007575 // TODO : Call below is temporary until call above can be re-enabled
7576 update_shader_storage_images_and_buffers(dev_data, pCB);
7577 skipCall |= markStoreImagesAndBuffersAsWritten(dev_data, pCB);
Tobin Ehlis5b5e7bc2016-03-09 16:12:48 -07007578 skipCall |= addCmd(dev_data, pCB, CMD_DISPATCHINDIRECT, "vkCmdDispatchIndirect()");
7579 skipCall |= insideRenderPass(dev_data, pCB, "vkCmdDispatchIndirect");
7580 }
Jeremy Hayesb9e99232016-04-13 16:20:24 -06007581 lock.unlock();
Dustin Gravese3319182016-04-05 09:41:17 -06007582 if (!skipCall)
Tobin Ehlis5b5e7bc2016-03-09 16:12:48 -07007583 dev_data->device_dispatch_table->CmdDispatchIndirect(commandBuffer, buffer, offset);
7584}
7585
7586VK_LAYER_EXPORT VKAPI_ATTR void VKAPI_CALL vkCmdCopyBuffer(VkCommandBuffer commandBuffer, VkBuffer srcBuffer, VkBuffer dstBuffer,
7587 uint32_t regionCount, const VkBufferCopy *pRegions) {
Dustin Gravese3319182016-04-05 09:41:17 -06007588 bool skipCall = false;
Tobin Ehlis5b5e7bc2016-03-09 16:12:48 -07007589 layer_data *dev_data = get_my_data_ptr(get_dispatch_key(commandBuffer), layer_data_map);
Jeremy Hayesb9e99232016-04-13 16:20:24 -06007590 std::unique_lock<std::mutex> lock(global_lock);
Mark Lobodzinski03b71512016-03-23 14:33:02 -06007591#if MTMERGESOURCE
Tobin Ehlis5b5e7bc2016-03-09 16:12:48 -07007592 VkDeviceMemory mem;
Tobin Ehlis72d66f02016-03-21 14:14:44 -06007593 auto cb_data = dev_data->commandBufferMap.find(commandBuffer);
Tobin Ehlis5b5e7bc2016-03-09 16:12:48 -07007594 skipCall =
Chris Forbes0a1ce3d2016-04-06 15:16:26 +12007595 get_mem_binding_from_object(dev_data, (uint64_t)srcBuffer, VK_DEBUG_REPORT_OBJECT_TYPE_BUFFER_EXT, &mem);
Tobin Ehlis72d66f02016-03-21 14:14:44 -06007596 if (cb_data != dev_data->commandBufferMap.end()) {
Dustin Gravese3319182016-04-05 09:41:17 -06007597 std::function<bool()> function = [=]() { return validate_memory_is_valid(dev_data, mem, "vkCmdCopyBuffer()"); };
Tobin Ehlis72d66f02016-03-21 14:14:44 -06007598 cb_data->second->validate_functions.push_back(function);
Tobin Ehlis5b5e7bc2016-03-09 16:12:48 -07007599 }
7600 skipCall |= update_cmd_buf_and_mem_references(dev_data, commandBuffer, mem, "vkCmdCopyBuffer");
7601 skipCall |=
Chris Forbes0a1ce3d2016-04-06 15:16:26 +12007602 get_mem_binding_from_object(dev_data, (uint64_t)dstBuffer, VK_DEBUG_REPORT_OBJECT_TYPE_BUFFER_EXT, &mem);
Tobin Ehlis72d66f02016-03-21 14:14:44 -06007603 if (cb_data != dev_data->commandBufferMap.end()) {
Dustin Gravese3319182016-04-05 09:41:17 -06007604 std::function<bool()> function = [=]() {
Tobin Ehlis5b5e7bc2016-03-09 16:12:48 -07007605 set_memory_valid(dev_data, mem, true);
Dustin Gravese3319182016-04-05 09:41:17 -06007606 return false;
Tobin Ehlis5b5e7bc2016-03-09 16:12:48 -07007607 };
Tobin Ehlis72d66f02016-03-21 14:14:44 -06007608 cb_data->second->validate_functions.push_back(function);
Tobin Ehlis5b5e7bc2016-03-09 16:12:48 -07007609 }
7610 skipCall |= update_cmd_buf_and_mem_references(dev_data, commandBuffer, mem, "vkCmdCopyBuffer");
7611 // Validate that SRC & DST buffers have correct usage flags set
Chris Forbes0a1ce3d2016-04-06 15:16:26 +12007612 skipCall |= validate_buffer_usage_flags(dev_data, srcBuffer, VK_BUFFER_USAGE_TRANSFER_SRC_BIT, true,
Tobin Ehlis5b5e7bc2016-03-09 16:12:48 -07007613 "vkCmdCopyBuffer()", "VK_BUFFER_USAGE_TRANSFER_SRC_BIT");
Chris Forbes0a1ce3d2016-04-06 15:16:26 +12007614 skipCall |= validate_buffer_usage_flags(dev_data, dstBuffer, VK_BUFFER_USAGE_TRANSFER_DST_BIT, true,
Tobin Ehlis5b5e7bc2016-03-09 16:12:48 -07007615 "vkCmdCopyBuffer()", "VK_BUFFER_USAGE_TRANSFER_DST_BIT");
7616#endif
7617 GLOBAL_CB_NODE *pCB = getCBNode(dev_data, commandBuffer);
7618 if (pCB) {
7619 skipCall |= addCmd(dev_data, pCB, CMD_COPYBUFFER, "vkCmdCopyBuffer()");
7620 skipCall |= insideRenderPass(dev_data, pCB, "vkCmdCopyBuffer");
7621 }
Jeremy Hayesb9e99232016-04-13 16:20:24 -06007622 lock.unlock();
Dustin Gravese3319182016-04-05 09:41:17 -06007623 if (!skipCall)
Tobin Ehlis5b5e7bc2016-03-09 16:12:48 -07007624 dev_data->device_dispatch_table->CmdCopyBuffer(commandBuffer, srcBuffer, dstBuffer, regionCount, pRegions);
7625}
7626
Dustin Gravese3319182016-04-05 09:41:17 -06007627static bool VerifySourceImageLayout(VkCommandBuffer cmdBuffer, VkImage srcImage, VkImageSubresourceLayers subLayers,
7628 VkImageLayout srcImageLayout) {
7629 bool skip_call = false;
Tobin Ehlis5b5e7bc2016-03-09 16:12:48 -07007630
7631 layer_data *dev_data = get_my_data_ptr(get_dispatch_key(cmdBuffer), layer_data_map);
7632 GLOBAL_CB_NODE *pCB = getCBNode(dev_data, cmdBuffer);
7633 for (uint32_t i = 0; i < subLayers.layerCount; ++i) {
7634 uint32_t layer = i + subLayers.baseArrayLayer;
7635 VkImageSubresource sub = {subLayers.aspectMask, subLayers.mipLevel, layer};
7636 IMAGE_CMD_BUF_LAYOUT_NODE node;
7637 if (!FindLayout(pCB, srcImage, sub, node)) {
Michael Lentine08682cd2016-03-24 15:36:27 -05007638 SetLayout(pCB, srcImage, sub, IMAGE_CMD_BUF_LAYOUT_NODE(srcImageLayout, srcImageLayout));
Tobin Ehlis5b5e7bc2016-03-09 16:12:48 -07007639 continue;
7640 }
7641 if (node.layout != srcImageLayout) {
7642 // TODO: Improve log message in the next pass
7643 skip_call |=
7644 log_msg(dev_data->report_data, VK_DEBUG_REPORT_ERROR_BIT_EXT, VK_DEBUG_REPORT_OBJECT_TYPE_COMMAND_BUFFER_EXT, 0,
7645 __LINE__, DRAWSTATE_INVALID_IMAGE_LAYOUT, "DS", "Cannot copy from an image whose source layout is %s "
7646 "and doesn't match the current layout %s.",
7647 string_VkImageLayout(srcImageLayout), string_VkImageLayout(node.layout));
7648 }
7649 }
7650 if (srcImageLayout != VK_IMAGE_LAYOUT_TRANSFER_SRC_OPTIMAL) {
7651 if (srcImageLayout == VK_IMAGE_LAYOUT_GENERAL) {
7652 // LAYOUT_GENERAL is allowed, but may not be performance optimal, flag as perf warning.
7653 skip_call |= log_msg(dev_data->report_data, VK_DEBUG_REPORT_PERFORMANCE_WARNING_BIT_EXT, (VkDebugReportObjectTypeEXT)0,
7654 0, __LINE__, DRAWSTATE_INVALID_IMAGE_LAYOUT, "DS",
7655 "Layout for input image should be TRANSFER_SRC_OPTIMAL instead of GENERAL.");
7656 } else {
7657 skip_call |= log_msg(dev_data->report_data, VK_DEBUG_REPORT_ERROR_BIT_EXT, (VkDebugReportObjectTypeEXT)0, 0, __LINE__,
7658 DRAWSTATE_INVALID_IMAGE_LAYOUT, "DS", "Layout for input image is %s but can only be "
7659 "TRANSFER_SRC_OPTIMAL or GENERAL.",
7660 string_VkImageLayout(srcImageLayout));
7661 }
7662 }
7663 return skip_call;
7664}
7665
Dustin Gravese3319182016-04-05 09:41:17 -06007666static bool VerifyDestImageLayout(VkCommandBuffer cmdBuffer, VkImage destImage, VkImageSubresourceLayers subLayers,
7667 VkImageLayout destImageLayout) {
7668 bool skip_call = false;
Tobin Ehlis5b5e7bc2016-03-09 16:12:48 -07007669
7670 layer_data *dev_data = get_my_data_ptr(get_dispatch_key(cmdBuffer), layer_data_map);
7671 GLOBAL_CB_NODE *pCB = getCBNode(dev_data, cmdBuffer);
7672 for (uint32_t i = 0; i < subLayers.layerCount; ++i) {
7673 uint32_t layer = i + subLayers.baseArrayLayer;
7674 VkImageSubresource sub = {subLayers.aspectMask, subLayers.mipLevel, layer};
7675 IMAGE_CMD_BUF_LAYOUT_NODE node;
7676 if (!FindLayout(pCB, destImage, sub, node)) {
Michael Lentine08682cd2016-03-24 15:36:27 -05007677 SetLayout(pCB, destImage, sub, IMAGE_CMD_BUF_LAYOUT_NODE(destImageLayout, destImageLayout));
Tobin Ehlis5b5e7bc2016-03-09 16:12:48 -07007678 continue;
7679 }
7680 if (node.layout != destImageLayout) {
7681 skip_call |=
7682 log_msg(dev_data->report_data, VK_DEBUG_REPORT_ERROR_BIT_EXT, VK_DEBUG_REPORT_OBJECT_TYPE_COMMAND_BUFFER_EXT, 0,
7683 __LINE__, DRAWSTATE_INVALID_IMAGE_LAYOUT, "DS", "Cannot copy from an image whose dest layout is %s and "
7684 "doesn't match the current layout %s.",
7685 string_VkImageLayout(destImageLayout), string_VkImageLayout(node.layout));
7686 }
7687 }
7688 if (destImageLayout != VK_IMAGE_LAYOUT_TRANSFER_DST_OPTIMAL) {
7689 if (destImageLayout == VK_IMAGE_LAYOUT_GENERAL) {
7690 // LAYOUT_GENERAL is allowed, but may not be performance optimal, flag as perf warning.
7691 skip_call |= log_msg(dev_data->report_data, VK_DEBUG_REPORT_PERFORMANCE_WARNING_BIT_EXT, (VkDebugReportObjectTypeEXT)0,
7692 0, __LINE__, DRAWSTATE_INVALID_IMAGE_LAYOUT, "DS",
7693 "Layout for output image should be TRANSFER_DST_OPTIMAL instead of GENERAL.");
7694 } else {
7695 skip_call |= log_msg(dev_data->report_data, VK_DEBUG_REPORT_ERROR_BIT_EXT, (VkDebugReportObjectTypeEXT)0, 0, __LINE__,
7696 DRAWSTATE_INVALID_IMAGE_LAYOUT, "DS", "Layout for output image is %s but can only be "
7697 "TRANSFER_DST_OPTIMAL or GENERAL.",
7698 string_VkImageLayout(destImageLayout));
7699 }
7700 }
7701 return skip_call;
7702}
7703
7704VK_LAYER_EXPORT VKAPI_ATTR void VKAPI_CALL
7705vkCmdCopyImage(VkCommandBuffer commandBuffer, VkImage srcImage, VkImageLayout srcImageLayout, VkImage dstImage,
7706 VkImageLayout dstImageLayout, uint32_t regionCount, const VkImageCopy *pRegions) {
Dustin Gravese3319182016-04-05 09:41:17 -06007707 bool skipCall = false;
Tobin Ehlis5b5e7bc2016-03-09 16:12:48 -07007708 layer_data *dev_data = get_my_data_ptr(get_dispatch_key(commandBuffer), layer_data_map);
Jeremy Hayesb9e99232016-04-13 16:20:24 -06007709 std::unique_lock<std::mutex> lock(global_lock);
Mark Lobodzinski03b71512016-03-23 14:33:02 -06007710#if MTMERGESOURCE
Tobin Ehlis5b5e7bc2016-03-09 16:12:48 -07007711 VkDeviceMemory mem;
Tobin Ehlis72d66f02016-03-21 14:14:44 -06007712 auto cb_data = dev_data->commandBufferMap.find(commandBuffer);
Tobin Ehlis5b5e7bc2016-03-09 16:12:48 -07007713 // Validate that src & dst images have correct usage flags set
Chris Forbes0a1ce3d2016-04-06 15:16:26 +12007714 skipCall = get_mem_binding_from_object(dev_data, (uint64_t)srcImage, VK_DEBUG_REPORT_OBJECT_TYPE_IMAGE_EXT, &mem);
Tobin Ehlis72d66f02016-03-21 14:14:44 -06007715 if (cb_data != dev_data->commandBufferMap.end()) {
Dustin Gravese3319182016-04-05 09:41:17 -06007716 std::function<bool()> function = [=]() { return validate_memory_is_valid(dev_data, mem, "vkCmdCopyImage()", srcImage); };
Tobin Ehlis72d66f02016-03-21 14:14:44 -06007717 cb_data->second->validate_functions.push_back(function);
Tobin Ehlis5b5e7bc2016-03-09 16:12:48 -07007718 }
7719 skipCall |= update_cmd_buf_and_mem_references(dev_data, commandBuffer, mem, "vkCmdCopyImage");
7720 skipCall |=
Chris Forbes0a1ce3d2016-04-06 15:16:26 +12007721 get_mem_binding_from_object(dev_data, (uint64_t)dstImage, VK_DEBUG_REPORT_OBJECT_TYPE_IMAGE_EXT, &mem);
Tobin Ehlis72d66f02016-03-21 14:14:44 -06007722 if (cb_data != dev_data->commandBufferMap.end()) {
Dustin Gravese3319182016-04-05 09:41:17 -06007723 std::function<bool()> function = [=]() {
Tobin Ehlis5b5e7bc2016-03-09 16:12:48 -07007724 set_memory_valid(dev_data, mem, true, dstImage);
Dustin Gravese3319182016-04-05 09:41:17 -06007725 return false;
Tobin Ehlis5b5e7bc2016-03-09 16:12:48 -07007726 };
Tobin Ehlis72d66f02016-03-21 14:14:44 -06007727 cb_data->second->validate_functions.push_back(function);
Tobin Ehlis5b5e7bc2016-03-09 16:12:48 -07007728 }
7729 skipCall |= update_cmd_buf_and_mem_references(dev_data, commandBuffer, mem, "vkCmdCopyImage");
Chris Forbes0a1ce3d2016-04-06 15:16:26 +12007730 skipCall |= validate_image_usage_flags(dev_data, srcImage, VK_IMAGE_USAGE_TRANSFER_SRC_BIT, true,
Tobin Ehlis5b5e7bc2016-03-09 16:12:48 -07007731 "vkCmdCopyImage()", "VK_IMAGE_USAGE_TRANSFER_SRC_BIT");
Chris Forbes0a1ce3d2016-04-06 15:16:26 +12007732 skipCall |= validate_image_usage_flags(dev_data, dstImage, VK_IMAGE_USAGE_TRANSFER_DST_BIT, true,
Tobin Ehlis5b5e7bc2016-03-09 16:12:48 -07007733 "vkCmdCopyImage()", "VK_IMAGE_USAGE_TRANSFER_DST_BIT");
7734#endif
7735 GLOBAL_CB_NODE *pCB = getCBNode(dev_data, commandBuffer);
7736 if (pCB) {
7737 skipCall |= addCmd(dev_data, pCB, CMD_COPYIMAGE, "vkCmdCopyImage()");
7738 skipCall |= insideRenderPass(dev_data, pCB, "vkCmdCopyImage");
7739 for (uint32_t i = 0; i < regionCount; ++i) {
7740 skipCall |= VerifySourceImageLayout(commandBuffer, srcImage, pRegions[i].srcSubresource, srcImageLayout);
7741 skipCall |= VerifyDestImageLayout(commandBuffer, dstImage, pRegions[i].dstSubresource, dstImageLayout);
7742 }
7743 }
Jeremy Hayesb9e99232016-04-13 16:20:24 -06007744 lock.unlock();
Dustin Gravese3319182016-04-05 09:41:17 -06007745 if (!skipCall)
Tobin Ehlis5b5e7bc2016-03-09 16:12:48 -07007746 dev_data->device_dispatch_table->CmdCopyImage(commandBuffer, srcImage, srcImageLayout, dstImage, dstImageLayout,
7747 regionCount, pRegions);
7748}
7749
7750VK_LAYER_EXPORT VKAPI_ATTR void VKAPI_CALL
7751vkCmdBlitImage(VkCommandBuffer commandBuffer, VkImage srcImage, VkImageLayout srcImageLayout, VkImage dstImage,
7752 VkImageLayout dstImageLayout, uint32_t regionCount, const VkImageBlit *pRegions, VkFilter filter) {
Dustin Gravese3319182016-04-05 09:41:17 -06007753 bool skipCall = false;
Tobin Ehlis5b5e7bc2016-03-09 16:12:48 -07007754 layer_data *dev_data = get_my_data_ptr(get_dispatch_key(commandBuffer), layer_data_map);
Jeremy Hayesb9e99232016-04-13 16:20:24 -06007755 std::unique_lock<std::mutex> lock(global_lock);
Mark Lobodzinski03b71512016-03-23 14:33:02 -06007756#if MTMERGESOURCE
Tobin Ehlis5b5e7bc2016-03-09 16:12:48 -07007757 VkDeviceMemory mem;
Tobin Ehlis72d66f02016-03-21 14:14:44 -06007758 auto cb_data = dev_data->commandBufferMap.find(commandBuffer);
Tobin Ehlis5b5e7bc2016-03-09 16:12:48 -07007759 // Validate that src & dst images have correct usage flags set
Chris Forbes0a1ce3d2016-04-06 15:16:26 +12007760 skipCall = get_mem_binding_from_object(dev_data, (uint64_t)srcImage, VK_DEBUG_REPORT_OBJECT_TYPE_IMAGE_EXT, &mem);
Tobin Ehlis72d66f02016-03-21 14:14:44 -06007761 if (cb_data != dev_data->commandBufferMap.end()) {
Dustin Gravese3319182016-04-05 09:41:17 -06007762 std::function<bool()> function = [=]() { return validate_memory_is_valid(dev_data, mem, "vkCmdBlitImage()", srcImage); };
Tobin Ehlis72d66f02016-03-21 14:14:44 -06007763 cb_data->second->validate_functions.push_back(function);
Tobin Ehlis5b5e7bc2016-03-09 16:12:48 -07007764 }
7765 skipCall |= update_cmd_buf_and_mem_references(dev_data, commandBuffer, mem, "vkCmdBlitImage");
7766 skipCall |=
Chris Forbes0a1ce3d2016-04-06 15:16:26 +12007767 get_mem_binding_from_object(dev_data, (uint64_t)dstImage, VK_DEBUG_REPORT_OBJECT_TYPE_IMAGE_EXT, &mem);
Tobin Ehlis72d66f02016-03-21 14:14:44 -06007768 if (cb_data != dev_data->commandBufferMap.end()) {
Dustin Gravese3319182016-04-05 09:41:17 -06007769 std::function<bool()> function = [=]() {
Tobin Ehlis5b5e7bc2016-03-09 16:12:48 -07007770 set_memory_valid(dev_data, mem, true, dstImage);
Dustin Gravese3319182016-04-05 09:41:17 -06007771 return false;
Tobin Ehlis5b5e7bc2016-03-09 16:12:48 -07007772 };
Tobin Ehlis72d66f02016-03-21 14:14:44 -06007773 cb_data->second->validate_functions.push_back(function);
Tobin Ehlis5b5e7bc2016-03-09 16:12:48 -07007774 }
7775 skipCall |= update_cmd_buf_and_mem_references(dev_data, commandBuffer, mem, "vkCmdBlitImage");
Chris Forbes0a1ce3d2016-04-06 15:16:26 +12007776 skipCall |= validate_image_usage_flags(dev_data, srcImage, VK_IMAGE_USAGE_TRANSFER_SRC_BIT, true,
Tobin Ehlis5b5e7bc2016-03-09 16:12:48 -07007777 "vkCmdBlitImage()", "VK_IMAGE_USAGE_TRANSFER_SRC_BIT");
Chris Forbes0a1ce3d2016-04-06 15:16:26 +12007778 skipCall |= validate_image_usage_flags(dev_data, dstImage, VK_IMAGE_USAGE_TRANSFER_DST_BIT, true,
Tobin Ehlis5b5e7bc2016-03-09 16:12:48 -07007779 "vkCmdBlitImage()", "VK_IMAGE_USAGE_TRANSFER_DST_BIT");
7780#endif
7781 GLOBAL_CB_NODE *pCB = getCBNode(dev_data, commandBuffer);
7782 if (pCB) {
7783 skipCall |= addCmd(dev_data, pCB, CMD_BLITIMAGE, "vkCmdBlitImage()");
7784 skipCall |= insideRenderPass(dev_data, pCB, "vkCmdBlitImage");
7785 }
Jeremy Hayesb9e99232016-04-13 16:20:24 -06007786 lock.unlock();
Dustin Gravese3319182016-04-05 09:41:17 -06007787 if (!skipCall)
Tobin Ehlis5b5e7bc2016-03-09 16:12:48 -07007788 dev_data->device_dispatch_table->CmdBlitImage(commandBuffer, srcImage, srcImageLayout, dstImage, dstImageLayout,
7789 regionCount, pRegions, filter);
7790}
7791
7792VK_LAYER_EXPORT VKAPI_ATTR void VKAPI_CALL vkCmdCopyBufferToImage(VkCommandBuffer commandBuffer, VkBuffer srcBuffer,
7793 VkImage dstImage, VkImageLayout dstImageLayout,
7794 uint32_t regionCount, const VkBufferImageCopy *pRegions) {
Dustin Gravese3319182016-04-05 09:41:17 -06007795 bool skipCall = false;
Tobin Ehlis5b5e7bc2016-03-09 16:12:48 -07007796 layer_data *dev_data = get_my_data_ptr(get_dispatch_key(commandBuffer), layer_data_map);
Jeremy Hayesb9e99232016-04-13 16:20:24 -06007797 std::unique_lock<std::mutex> lock(global_lock);
Mark Lobodzinski03b71512016-03-23 14:33:02 -06007798#if MTMERGESOURCE
Tobin Ehlis5b5e7bc2016-03-09 16:12:48 -07007799 VkDeviceMemory mem;
Tobin Ehlis72d66f02016-03-21 14:14:44 -06007800 auto cb_data = dev_data->commandBufferMap.find(commandBuffer);
Chris Forbes0a1ce3d2016-04-06 15:16:26 +12007801 skipCall = get_mem_binding_from_object(dev_data, (uint64_t)dstImage, VK_DEBUG_REPORT_OBJECT_TYPE_IMAGE_EXT, &mem);
Tobin Ehlis72d66f02016-03-21 14:14:44 -06007802 if (cb_data != dev_data->commandBufferMap.end()) {
Dustin Gravese3319182016-04-05 09:41:17 -06007803 std::function<bool()> function = [=]() {
Tobin Ehlis5b5e7bc2016-03-09 16:12:48 -07007804 set_memory_valid(dev_data, mem, true, dstImage);
Dustin Gravese3319182016-04-05 09:41:17 -06007805 return false;
Tobin Ehlis5b5e7bc2016-03-09 16:12:48 -07007806 };
Tobin Ehlis72d66f02016-03-21 14:14:44 -06007807 cb_data->second->validate_functions.push_back(function);
Tobin Ehlis5b5e7bc2016-03-09 16:12:48 -07007808 }
7809 skipCall |= update_cmd_buf_and_mem_references(dev_data, commandBuffer, mem, "vkCmdCopyBufferToImage");
7810 skipCall |=
Chris Forbes0a1ce3d2016-04-06 15:16:26 +12007811 get_mem_binding_from_object(dev_data, (uint64_t)srcBuffer, VK_DEBUG_REPORT_OBJECT_TYPE_BUFFER_EXT, &mem);
Tobin Ehlis72d66f02016-03-21 14:14:44 -06007812 if (cb_data != dev_data->commandBufferMap.end()) {
Dustin Gravese3319182016-04-05 09:41:17 -06007813 std::function<bool()> function = [=]() { return validate_memory_is_valid(dev_data, mem, "vkCmdCopyBufferToImage()"); };
Tobin Ehlis72d66f02016-03-21 14:14:44 -06007814 cb_data->second->validate_functions.push_back(function);
Tobin Ehlis5b5e7bc2016-03-09 16:12:48 -07007815 }
7816 skipCall |= update_cmd_buf_and_mem_references(dev_data, commandBuffer, mem, "vkCmdCopyBufferToImage");
7817 // Validate that src buff & dst image have correct usage flags set
Chris Forbes0a1ce3d2016-04-06 15:16:26 +12007818 skipCall |= validate_buffer_usage_flags(dev_data, srcBuffer, VK_BUFFER_USAGE_TRANSFER_SRC_BIT, true,
Tobin Ehlis5b5e7bc2016-03-09 16:12:48 -07007819 "vkCmdCopyBufferToImage()", "VK_BUFFER_USAGE_TRANSFER_SRC_BIT");
Chris Forbes0a1ce3d2016-04-06 15:16:26 +12007820 skipCall |= validate_image_usage_flags(dev_data, dstImage, VK_IMAGE_USAGE_TRANSFER_DST_BIT, true,
Tobin Ehlis5b5e7bc2016-03-09 16:12:48 -07007821 "vkCmdCopyBufferToImage()", "VK_IMAGE_USAGE_TRANSFER_DST_BIT");
7822#endif
7823 GLOBAL_CB_NODE *pCB = getCBNode(dev_data, commandBuffer);
7824 if (pCB) {
7825 skipCall |= addCmd(dev_data, pCB, CMD_COPYBUFFERTOIMAGE, "vkCmdCopyBufferToImage()");
7826 skipCall |= insideRenderPass(dev_data, pCB, "vkCmdCopyBufferToImage");
7827 for (uint32_t i = 0; i < regionCount; ++i) {
7828 skipCall |= VerifyDestImageLayout(commandBuffer, dstImage, pRegions[i].imageSubresource, dstImageLayout);
7829 }
7830 }
Jeremy Hayesb9e99232016-04-13 16:20:24 -06007831 lock.unlock();
Dustin Gravese3319182016-04-05 09:41:17 -06007832 if (!skipCall)
Tobin Ehlis5b5e7bc2016-03-09 16:12:48 -07007833 dev_data->device_dispatch_table->CmdCopyBufferToImage(commandBuffer, srcBuffer, dstImage, dstImageLayout, regionCount,
7834 pRegions);
7835}
7836
7837VK_LAYER_EXPORT VKAPI_ATTR void VKAPI_CALL vkCmdCopyImageToBuffer(VkCommandBuffer commandBuffer, VkImage srcImage,
7838 VkImageLayout srcImageLayout, VkBuffer dstBuffer,
7839 uint32_t regionCount, const VkBufferImageCopy *pRegions) {
Dustin Gravese3319182016-04-05 09:41:17 -06007840 bool skipCall = false;
Tobin Ehlis5b5e7bc2016-03-09 16:12:48 -07007841 layer_data *dev_data = get_my_data_ptr(get_dispatch_key(commandBuffer), layer_data_map);
Jeremy Hayesb9e99232016-04-13 16:20:24 -06007842 std::unique_lock<std::mutex> lock(global_lock);
Mark Lobodzinski03b71512016-03-23 14:33:02 -06007843#if MTMERGESOURCE
Tobin Ehlis5b5e7bc2016-03-09 16:12:48 -07007844 VkDeviceMemory mem;
Tobin Ehlis72d66f02016-03-21 14:14:44 -06007845 auto cb_data = dev_data->commandBufferMap.find(commandBuffer);
Chris Forbes0a1ce3d2016-04-06 15:16:26 +12007846 skipCall = get_mem_binding_from_object(dev_data, (uint64_t)srcImage, VK_DEBUG_REPORT_OBJECT_TYPE_IMAGE_EXT, &mem);
Tobin Ehlis72d66f02016-03-21 14:14:44 -06007847 if (cb_data != dev_data->commandBufferMap.end()) {
Dustin Gravese3319182016-04-05 09:41:17 -06007848 std::function<bool()> function = [=]() {
7849 return validate_memory_is_valid(dev_data, mem, "vkCmdCopyImageToBuffer()", srcImage);
7850 };
Tobin Ehlis72d66f02016-03-21 14:14:44 -06007851 cb_data->second->validate_functions.push_back(function);
Tobin Ehlis5b5e7bc2016-03-09 16:12:48 -07007852 }
7853 skipCall |= update_cmd_buf_and_mem_references(dev_data, commandBuffer, mem, "vkCmdCopyImageToBuffer");
7854 skipCall |=
Chris Forbes0a1ce3d2016-04-06 15:16:26 +12007855 get_mem_binding_from_object(dev_data, (uint64_t)dstBuffer, VK_DEBUG_REPORT_OBJECT_TYPE_BUFFER_EXT, &mem);
Tobin Ehlis72d66f02016-03-21 14:14:44 -06007856 if (cb_data != dev_data->commandBufferMap.end()) {
Dustin Gravese3319182016-04-05 09:41:17 -06007857 std::function<bool()> function = [=]() {
Tobin Ehlis5b5e7bc2016-03-09 16:12:48 -07007858 set_memory_valid(dev_data, mem, true);
Dustin Gravese3319182016-04-05 09:41:17 -06007859 return false;
Tobin Ehlis5b5e7bc2016-03-09 16:12:48 -07007860 };
Tobin Ehlis72d66f02016-03-21 14:14:44 -06007861 cb_data->second->validate_functions.push_back(function);
Tobin Ehlis5b5e7bc2016-03-09 16:12:48 -07007862 }
7863 skipCall |= update_cmd_buf_and_mem_references(dev_data, commandBuffer, mem, "vkCmdCopyImageToBuffer");
7864 // Validate that dst buff & src image have correct usage flags set
Chris Forbes0a1ce3d2016-04-06 15:16:26 +12007865 skipCall |= validate_image_usage_flags(dev_data, srcImage, VK_IMAGE_USAGE_TRANSFER_SRC_BIT, true,
Tobin Ehlis5b5e7bc2016-03-09 16:12:48 -07007866 "vkCmdCopyImageToBuffer()", "VK_IMAGE_USAGE_TRANSFER_SRC_BIT");
Chris Forbes0a1ce3d2016-04-06 15:16:26 +12007867 skipCall |= validate_buffer_usage_flags(dev_data, dstBuffer, VK_BUFFER_USAGE_TRANSFER_DST_BIT, true,
Tobin Ehlis5b5e7bc2016-03-09 16:12:48 -07007868 "vkCmdCopyImageToBuffer()", "VK_BUFFER_USAGE_TRANSFER_DST_BIT");
7869#endif
7870 GLOBAL_CB_NODE *pCB = getCBNode(dev_data, commandBuffer);
7871 if (pCB) {
7872 skipCall |= addCmd(dev_data, pCB, CMD_COPYIMAGETOBUFFER, "vkCmdCopyImageToBuffer()");
7873 skipCall |= insideRenderPass(dev_data, pCB, "vkCmdCopyImageToBuffer");
7874 for (uint32_t i = 0; i < regionCount; ++i) {
7875 skipCall |= VerifySourceImageLayout(commandBuffer, srcImage, pRegions[i].imageSubresource, srcImageLayout);
7876 }
7877 }
Jeremy Hayesb9e99232016-04-13 16:20:24 -06007878 lock.unlock();
Dustin Gravese3319182016-04-05 09:41:17 -06007879 if (!skipCall)
Tobin Ehlis5b5e7bc2016-03-09 16:12:48 -07007880 dev_data->device_dispatch_table->CmdCopyImageToBuffer(commandBuffer, srcImage, srcImageLayout, dstBuffer, regionCount,
7881 pRegions);
7882}
7883
7884VK_LAYER_EXPORT VKAPI_ATTR void VKAPI_CALL vkCmdUpdateBuffer(VkCommandBuffer commandBuffer, VkBuffer dstBuffer,
7885 VkDeviceSize dstOffset, VkDeviceSize dataSize, const uint32_t *pData) {
Dustin Gravese3319182016-04-05 09:41:17 -06007886 bool skipCall = false;
Tobin Ehlis5b5e7bc2016-03-09 16:12:48 -07007887 layer_data *dev_data = get_my_data_ptr(get_dispatch_key(commandBuffer), layer_data_map);
Jeremy Hayesb9e99232016-04-13 16:20:24 -06007888 std::unique_lock<std::mutex> lock(global_lock);
Mark Lobodzinski03b71512016-03-23 14:33:02 -06007889#if MTMERGESOURCE
Tobin Ehlis5b5e7bc2016-03-09 16:12:48 -07007890 VkDeviceMemory mem;
Tobin Ehlis72d66f02016-03-21 14:14:44 -06007891 auto cb_data = dev_data->commandBufferMap.find(commandBuffer);
Tobin Ehlis5b5e7bc2016-03-09 16:12:48 -07007892 skipCall =
Chris Forbes0a1ce3d2016-04-06 15:16:26 +12007893 get_mem_binding_from_object(dev_data, (uint64_t)dstBuffer, VK_DEBUG_REPORT_OBJECT_TYPE_BUFFER_EXT, &mem);
Tobin Ehlis72d66f02016-03-21 14:14:44 -06007894 if (cb_data != dev_data->commandBufferMap.end()) {
Dustin Gravese3319182016-04-05 09:41:17 -06007895 std::function<bool()> function = [=]() {
Tobin Ehlis5b5e7bc2016-03-09 16:12:48 -07007896 set_memory_valid(dev_data, mem, true);
Dustin Gravese3319182016-04-05 09:41:17 -06007897 return false;
Tobin Ehlis5b5e7bc2016-03-09 16:12:48 -07007898 };
Tobin Ehlis72d66f02016-03-21 14:14:44 -06007899 cb_data->second->validate_functions.push_back(function);
Tobin Ehlis5b5e7bc2016-03-09 16:12:48 -07007900 }
7901 skipCall |= update_cmd_buf_and_mem_references(dev_data, commandBuffer, mem, "vkCmdUpdateBuffer");
7902 // Validate that dst buff has correct usage flags set
Chris Forbes0a1ce3d2016-04-06 15:16:26 +12007903 skipCall |= validate_buffer_usage_flags(dev_data, dstBuffer, VK_BUFFER_USAGE_TRANSFER_DST_BIT, true,
Tobin Ehlis5b5e7bc2016-03-09 16:12:48 -07007904 "vkCmdUpdateBuffer()", "VK_BUFFER_USAGE_TRANSFER_DST_BIT");
7905#endif
7906 GLOBAL_CB_NODE *pCB = getCBNode(dev_data, commandBuffer);
7907 if (pCB) {
7908 skipCall |= addCmd(dev_data, pCB, CMD_UPDATEBUFFER, "vkCmdUpdateBuffer()");
7909 skipCall |= insideRenderPass(dev_data, pCB, "vkCmdCopyUpdateBuffer");
7910 }
Jeremy Hayesb9e99232016-04-13 16:20:24 -06007911 lock.unlock();
Dustin Gravese3319182016-04-05 09:41:17 -06007912 if (!skipCall)
Tobin Ehlis5b5e7bc2016-03-09 16:12:48 -07007913 dev_data->device_dispatch_table->CmdUpdateBuffer(commandBuffer, dstBuffer, dstOffset, dataSize, pData);
7914}
7915
7916VK_LAYER_EXPORT VKAPI_ATTR void VKAPI_CALL
7917vkCmdFillBuffer(VkCommandBuffer commandBuffer, VkBuffer dstBuffer, VkDeviceSize dstOffset, VkDeviceSize size, uint32_t data) {
Dustin Gravese3319182016-04-05 09:41:17 -06007918 bool skipCall = false;
Tobin Ehlis5b5e7bc2016-03-09 16:12:48 -07007919 layer_data *dev_data = get_my_data_ptr(get_dispatch_key(commandBuffer), layer_data_map);
Jeremy Hayesb9e99232016-04-13 16:20:24 -06007920 std::unique_lock<std::mutex> lock(global_lock);
Mark Lobodzinski03b71512016-03-23 14:33:02 -06007921#if MTMERGESOURCE
Tobin Ehlis5b5e7bc2016-03-09 16:12:48 -07007922 VkDeviceMemory mem;
Tobin Ehlis72d66f02016-03-21 14:14:44 -06007923 auto cb_data = dev_data->commandBufferMap.find(commandBuffer);
Tobin Ehlis5b5e7bc2016-03-09 16:12:48 -07007924 skipCall =
Chris Forbes0a1ce3d2016-04-06 15:16:26 +12007925 get_mem_binding_from_object(dev_data, (uint64_t)dstBuffer, VK_DEBUG_REPORT_OBJECT_TYPE_BUFFER_EXT, &mem);
Tobin Ehlis72d66f02016-03-21 14:14:44 -06007926 if (cb_data != dev_data->commandBufferMap.end()) {
Dustin Gravese3319182016-04-05 09:41:17 -06007927 std::function<bool()> function = [=]() {
Tobin Ehlis5b5e7bc2016-03-09 16:12:48 -07007928 set_memory_valid(dev_data, mem, true);
Dustin Gravese3319182016-04-05 09:41:17 -06007929 return false;
Tobin Ehlis5b5e7bc2016-03-09 16:12:48 -07007930 };
Tobin Ehlis72d66f02016-03-21 14:14:44 -06007931 cb_data->second->validate_functions.push_back(function);
Tobin Ehlis5b5e7bc2016-03-09 16:12:48 -07007932 }
7933 skipCall |= update_cmd_buf_and_mem_references(dev_data, commandBuffer, mem, "vkCmdFillBuffer");
7934 // Validate that dst buff has correct usage flags set
Chris Forbes0a1ce3d2016-04-06 15:16:26 +12007935 skipCall |= validate_buffer_usage_flags(dev_data, dstBuffer, VK_BUFFER_USAGE_TRANSFER_DST_BIT, true,
Tobin Ehlis5b5e7bc2016-03-09 16:12:48 -07007936 "vkCmdFillBuffer()", "VK_BUFFER_USAGE_TRANSFER_DST_BIT");
7937#endif
7938 GLOBAL_CB_NODE *pCB = getCBNode(dev_data, commandBuffer);
7939 if (pCB) {
7940 skipCall |= addCmd(dev_data, pCB, CMD_FILLBUFFER, "vkCmdFillBuffer()");
7941 skipCall |= insideRenderPass(dev_data, pCB, "vkCmdCopyFillBuffer");
7942 }
Jeremy Hayesb9e99232016-04-13 16:20:24 -06007943 lock.unlock();
Dustin Gravese3319182016-04-05 09:41:17 -06007944 if (!skipCall)
Tobin Ehlis5b5e7bc2016-03-09 16:12:48 -07007945 dev_data->device_dispatch_table->CmdFillBuffer(commandBuffer, dstBuffer, dstOffset, size, data);
7946}
7947
7948VK_LAYER_EXPORT VKAPI_ATTR void VKAPI_CALL vkCmdClearAttachments(VkCommandBuffer commandBuffer, uint32_t attachmentCount,
7949 const VkClearAttachment *pAttachments, uint32_t rectCount,
7950 const VkClearRect *pRects) {
Dustin Gravese3319182016-04-05 09:41:17 -06007951 bool skipCall = false;
Tobin Ehlis5b5e7bc2016-03-09 16:12:48 -07007952 layer_data *dev_data = get_my_data_ptr(get_dispatch_key(commandBuffer), layer_data_map);
Jeremy Hayesb9e99232016-04-13 16:20:24 -06007953 std::unique_lock<std::mutex> lock(global_lock);
Tobin Ehlis5b5e7bc2016-03-09 16:12:48 -07007954 GLOBAL_CB_NODE *pCB = getCBNode(dev_data, commandBuffer);
7955 if (pCB) {
7956 skipCall |= addCmd(dev_data, pCB, CMD_CLEARATTACHMENTS, "vkCmdClearAttachments()");
7957 // Warn if this is issued prior to Draw Cmd and clearing the entire attachment
7958 if (!hasDrawCmd(pCB) && (pCB->activeRenderPassBeginInfo.renderArea.extent.width == pRects[0].rect.extent.width) &&
7959 (pCB->activeRenderPassBeginInfo.renderArea.extent.height == pRects[0].rect.extent.height)) {
7960 // TODO : commandBuffer should be srcObj
7961 // There are times where app needs to use ClearAttachments (generally when reusing a buffer inside of a render pass)
7962 // Can we make this warning more specific? I'd like to avoid triggering this test if we can tell it's a use that must
7963 // call CmdClearAttachments
7964 // Otherwise this seems more like a performance warning.
7965 skipCall |= log_msg(dev_data->report_data, VK_DEBUG_REPORT_PERFORMANCE_WARNING_BIT_EXT,
7966 VK_DEBUG_REPORT_OBJECT_TYPE_COMMAND_BUFFER_EXT, 0, 0, DRAWSTATE_CLEAR_CMD_BEFORE_DRAW, "DS",
7967 "vkCmdClearAttachments() issued on CB object 0x%" PRIxLEAST64 " prior to any Draw Cmds."
7968 " It is recommended you use RenderPass LOAD_OP_CLEAR on Attachments prior to any Draw.",
7969 (uint64_t)(commandBuffer));
7970 }
7971 skipCall |= outsideRenderPass(dev_data, pCB, "vkCmdClearAttachments");
7972 }
7973
7974 // Validate that attachment is in reference list of active subpass
7975 if (pCB->activeRenderPass) {
7976 const VkRenderPassCreateInfo *pRPCI = dev_data->renderPassMap[pCB->activeRenderPass]->pCreateInfo;
7977 const VkSubpassDescription *pSD = &pRPCI->pSubpasses[pCB->activeSubpass];
7978
7979 for (uint32_t attachment_idx = 0; attachment_idx < attachmentCount; attachment_idx++) {
7980 const VkClearAttachment *attachment = &pAttachments[attachment_idx];
7981 if (attachment->aspectMask & VK_IMAGE_ASPECT_COLOR_BIT) {
Dustin Gravese3319182016-04-05 09:41:17 -06007982 bool found = false;
Tobin Ehlis5b5e7bc2016-03-09 16:12:48 -07007983 for (uint32_t i = 0; i < pSD->colorAttachmentCount; i++) {
7984 if (attachment->colorAttachment == pSD->pColorAttachments[i].attachment) {
Dustin Gravese3319182016-04-05 09:41:17 -06007985 found = true;
Tobin Ehlis5b5e7bc2016-03-09 16:12:48 -07007986 break;
7987 }
7988 }
Dustin Gravese3319182016-04-05 09:41:17 -06007989 if (!found) {
Tobin Ehlis5b5e7bc2016-03-09 16:12:48 -07007990 skipCall |= log_msg(
7991 dev_data->report_data, VK_DEBUG_REPORT_ERROR_BIT_EXT, VK_DEBUG_REPORT_OBJECT_TYPE_COMMAND_BUFFER_EXT,
7992 (uint64_t)commandBuffer, __LINE__, DRAWSTATE_MISSING_ATTACHMENT_REFERENCE, "DS",
7993 "vkCmdClearAttachments() attachment index %d not found in attachment reference array of active subpass %d",
7994 attachment->colorAttachment, pCB->activeSubpass);
7995 }
7996 } else if (attachment->aspectMask & (VK_IMAGE_ASPECT_DEPTH_BIT | VK_IMAGE_ASPECT_STENCIL_BIT)) {
7997 if (!pSD->pDepthStencilAttachment || // Says no DS will be used in active subpass
7998 (pSD->pDepthStencilAttachment->attachment ==
7999 VK_ATTACHMENT_UNUSED)) { // Says no DS will be used in active subpass
8000
8001 skipCall |= log_msg(
8002 dev_data->report_data, VK_DEBUG_REPORT_ERROR_BIT_EXT, VK_DEBUG_REPORT_OBJECT_TYPE_COMMAND_BUFFER_EXT,
8003 (uint64_t)commandBuffer, __LINE__, DRAWSTATE_MISSING_ATTACHMENT_REFERENCE, "DS",
8004 "vkCmdClearAttachments() attachment index %d does not match depthStencilAttachment.attachment (%d) found "
8005 "in active subpass %d",
8006 attachment->colorAttachment,
8007 (pSD->pDepthStencilAttachment) ? pSD->pDepthStencilAttachment->attachment : VK_ATTACHMENT_UNUSED,
8008 pCB->activeSubpass);
8009 }
8010 }
8011 }
8012 }
Jeremy Hayesb9e99232016-04-13 16:20:24 -06008013 lock.unlock();
Dustin Gravese3319182016-04-05 09:41:17 -06008014 if (!skipCall)
Tobin Ehlis5b5e7bc2016-03-09 16:12:48 -07008015 dev_data->device_dispatch_table->CmdClearAttachments(commandBuffer, attachmentCount, pAttachments, rectCount, pRects);
8016}
8017
8018VK_LAYER_EXPORT VKAPI_ATTR void VKAPI_CALL vkCmdClearColorImage(VkCommandBuffer commandBuffer, VkImage image,
8019 VkImageLayout imageLayout, const VkClearColorValue *pColor,
8020 uint32_t rangeCount, const VkImageSubresourceRange *pRanges) {
Dustin Gravese3319182016-04-05 09:41:17 -06008021 bool skipCall = false;
Tobin Ehlis5b5e7bc2016-03-09 16:12:48 -07008022 layer_data *dev_data = get_my_data_ptr(get_dispatch_key(commandBuffer), layer_data_map);
Jeremy Hayesb9e99232016-04-13 16:20:24 -06008023 std::unique_lock<std::mutex> lock(global_lock);
Mark Lobodzinski03b71512016-03-23 14:33:02 -06008024#if MTMERGESOURCE
Tobin Ehlis5b5e7bc2016-03-09 16:12:48 -07008025 // TODO : Verify memory is in VK_IMAGE_STATE_CLEAR state
8026 VkDeviceMemory mem;
Tobin Ehlis72d66f02016-03-21 14:14:44 -06008027 auto cb_data = dev_data->commandBufferMap.find(commandBuffer);
Chris Forbes0a1ce3d2016-04-06 15:16:26 +12008028 skipCall = get_mem_binding_from_object(dev_data, (uint64_t)image, VK_DEBUG_REPORT_OBJECT_TYPE_IMAGE_EXT, &mem);
Tobin Ehlis72d66f02016-03-21 14:14:44 -06008029 if (cb_data != dev_data->commandBufferMap.end()) {
Dustin Gravese3319182016-04-05 09:41:17 -06008030 std::function<bool()> function = [=]() {
Tobin Ehlis5b5e7bc2016-03-09 16:12:48 -07008031 set_memory_valid(dev_data, mem, true, image);
Dustin Gravese3319182016-04-05 09:41:17 -06008032 return false;
Tobin Ehlis5b5e7bc2016-03-09 16:12:48 -07008033 };
Tobin Ehlis72d66f02016-03-21 14:14:44 -06008034 cb_data->second->validate_functions.push_back(function);
Tobin Ehlis5b5e7bc2016-03-09 16:12:48 -07008035 }
8036 skipCall |= update_cmd_buf_and_mem_references(dev_data, commandBuffer, mem, "vkCmdClearColorImage");
8037#endif
8038 GLOBAL_CB_NODE *pCB = getCBNode(dev_data, commandBuffer);
8039 if (pCB) {
8040 skipCall |= addCmd(dev_data, pCB, CMD_CLEARCOLORIMAGE, "vkCmdClearColorImage()");
8041 skipCall |= insideRenderPass(dev_data, pCB, "vkCmdClearColorImage");
8042 }
Jeremy Hayesb9e99232016-04-13 16:20:24 -06008043 lock.unlock();
Dustin Gravese3319182016-04-05 09:41:17 -06008044 if (!skipCall)
Tobin Ehlis5b5e7bc2016-03-09 16:12:48 -07008045 dev_data->device_dispatch_table->CmdClearColorImage(commandBuffer, image, imageLayout, pColor, rangeCount, pRanges);
8046}
8047
8048VK_LAYER_EXPORT VKAPI_ATTR void VKAPI_CALL
8049vkCmdClearDepthStencilImage(VkCommandBuffer commandBuffer, VkImage image, VkImageLayout imageLayout,
8050 const VkClearDepthStencilValue *pDepthStencil, uint32_t rangeCount,
8051 const VkImageSubresourceRange *pRanges) {
Dustin Gravese3319182016-04-05 09:41:17 -06008052 bool skipCall = false;
Tobin Ehlis5b5e7bc2016-03-09 16:12:48 -07008053 layer_data *dev_data = get_my_data_ptr(get_dispatch_key(commandBuffer), layer_data_map);
Jeremy Hayesb9e99232016-04-13 16:20:24 -06008054 std::unique_lock<std::mutex> lock(global_lock);
Mark Lobodzinski03b71512016-03-23 14:33:02 -06008055#if MTMERGESOURCE
Tobin Ehlis5b5e7bc2016-03-09 16:12:48 -07008056 // TODO : Verify memory is in VK_IMAGE_STATE_CLEAR state
8057 VkDeviceMemory mem;
Tobin Ehlis72d66f02016-03-21 14:14:44 -06008058 auto cb_data = dev_data->commandBufferMap.find(commandBuffer);
Chris Forbes0a1ce3d2016-04-06 15:16:26 +12008059 skipCall = get_mem_binding_from_object(dev_data, (uint64_t)image, VK_DEBUG_REPORT_OBJECT_TYPE_IMAGE_EXT, &mem);
Tobin Ehlis72d66f02016-03-21 14:14:44 -06008060 if (cb_data != dev_data->commandBufferMap.end()) {
Dustin Gravese3319182016-04-05 09:41:17 -06008061 std::function<bool()> function = [=]() {
Tobin Ehlis5b5e7bc2016-03-09 16:12:48 -07008062 set_memory_valid(dev_data, mem, true, image);
Dustin Gravese3319182016-04-05 09:41:17 -06008063 return false;
Tobin Ehlis5b5e7bc2016-03-09 16:12:48 -07008064 };
Tobin Ehlis72d66f02016-03-21 14:14:44 -06008065 cb_data->second->validate_functions.push_back(function);
Tobin Ehlis5b5e7bc2016-03-09 16:12:48 -07008066 }
8067 skipCall |= update_cmd_buf_and_mem_references(dev_data, commandBuffer, mem, "vkCmdClearDepthStencilImage");
8068#endif
8069 GLOBAL_CB_NODE *pCB = getCBNode(dev_data, commandBuffer);
8070 if (pCB) {
8071 skipCall |= addCmd(dev_data, pCB, CMD_CLEARDEPTHSTENCILIMAGE, "vkCmdClearDepthStencilImage()");
8072 skipCall |= insideRenderPass(dev_data, pCB, "vkCmdClearDepthStencilImage");
8073 }
Jeremy Hayesb9e99232016-04-13 16:20:24 -06008074 lock.unlock();
Dustin Gravese3319182016-04-05 09:41:17 -06008075 if (!skipCall)
Tobin Ehlis5b5e7bc2016-03-09 16:12:48 -07008076 dev_data->device_dispatch_table->CmdClearDepthStencilImage(commandBuffer, image, imageLayout, pDepthStencil, rangeCount,
8077 pRanges);
8078}
8079
8080VK_LAYER_EXPORT VKAPI_ATTR void VKAPI_CALL
8081vkCmdResolveImage(VkCommandBuffer commandBuffer, VkImage srcImage, VkImageLayout srcImageLayout, VkImage dstImage,
8082 VkImageLayout dstImageLayout, uint32_t regionCount, const VkImageResolve *pRegions) {
Dustin Gravese3319182016-04-05 09:41:17 -06008083 bool skipCall = false;
Tobin Ehlis5b5e7bc2016-03-09 16:12:48 -07008084 layer_data *dev_data = get_my_data_ptr(get_dispatch_key(commandBuffer), layer_data_map);
Jeremy Hayesb9e99232016-04-13 16:20:24 -06008085 std::unique_lock<std::mutex> lock(global_lock);
Mark Lobodzinski03b71512016-03-23 14:33:02 -06008086#if MTMERGESOURCE
Tobin Ehlis72d66f02016-03-21 14:14:44 -06008087 auto cb_data = dev_data->commandBufferMap.find(commandBuffer);
Tobin Ehlis5b5e7bc2016-03-09 16:12:48 -07008088 VkDeviceMemory mem;
Chris Forbes0a1ce3d2016-04-06 15:16:26 +12008089 skipCall = get_mem_binding_from_object(dev_data, (uint64_t)srcImage, VK_DEBUG_REPORT_OBJECT_TYPE_IMAGE_EXT, &mem);
Tobin Ehlis72d66f02016-03-21 14:14:44 -06008090 if (cb_data != dev_data->commandBufferMap.end()) {
Dustin Gravese3319182016-04-05 09:41:17 -06008091 std::function<bool()> function = [=]() { return validate_memory_is_valid(dev_data, mem, "vkCmdResolveImage()", srcImage); };
Tobin Ehlis72d66f02016-03-21 14:14:44 -06008092 cb_data->second->validate_functions.push_back(function);
Tobin Ehlis5b5e7bc2016-03-09 16:12:48 -07008093 }
8094 skipCall |= update_cmd_buf_and_mem_references(dev_data, commandBuffer, mem, "vkCmdResolveImage");
8095 skipCall |=
Chris Forbes0a1ce3d2016-04-06 15:16:26 +12008096 get_mem_binding_from_object(dev_data, (uint64_t)dstImage, VK_DEBUG_REPORT_OBJECT_TYPE_IMAGE_EXT, &mem);
Tobin Ehlis72d66f02016-03-21 14:14:44 -06008097 if (cb_data != dev_data->commandBufferMap.end()) {
Dustin Gravese3319182016-04-05 09:41:17 -06008098 std::function<bool()> function = [=]() {
Tobin Ehlis5b5e7bc2016-03-09 16:12:48 -07008099 set_memory_valid(dev_data, mem, true, dstImage);
Dustin Gravese3319182016-04-05 09:41:17 -06008100 return false;
Tobin Ehlis5b5e7bc2016-03-09 16:12:48 -07008101 };
Tobin Ehlis72d66f02016-03-21 14:14:44 -06008102 cb_data->second->validate_functions.push_back(function);
Tobin Ehlis5b5e7bc2016-03-09 16:12:48 -07008103 }
8104 skipCall |= update_cmd_buf_and_mem_references(dev_data, commandBuffer, mem, "vkCmdResolveImage");
8105#endif
8106 GLOBAL_CB_NODE *pCB = getCBNode(dev_data, commandBuffer);
8107 if (pCB) {
8108 skipCall |= addCmd(dev_data, pCB, CMD_RESOLVEIMAGE, "vkCmdResolveImage()");
8109 skipCall |= insideRenderPass(dev_data, pCB, "vkCmdResolveImage");
8110 }
Jeremy Hayesb9e99232016-04-13 16:20:24 -06008111 lock.unlock();
Dustin Gravese3319182016-04-05 09:41:17 -06008112 if (!skipCall)
Tobin Ehlis5b5e7bc2016-03-09 16:12:48 -07008113 dev_data->device_dispatch_table->CmdResolveImage(commandBuffer, srcImage, srcImageLayout, dstImage, dstImageLayout,
8114 regionCount, pRegions);
8115}
8116
Michael Lentineb4cc5212016-03-18 14:11:44 -05008117bool setEventStageMask(VkQueue queue, VkCommandBuffer commandBuffer, VkEvent event, VkPipelineStageFlags stageMask) {
8118 layer_data *dev_data = get_my_data_ptr(get_dispatch_key(commandBuffer), layer_data_map);
8119 GLOBAL_CB_NODE *pCB = getCBNode(dev_data, commandBuffer);
8120 if (pCB) {
8121 pCB->eventToStageMap[event] = stageMask;
8122 }
8123 auto queue_data = dev_data->queueMap.find(queue);
8124 if (queue_data != dev_data->queueMap.end()) {
8125 queue_data->second.eventToStageMap[event] = stageMask;
8126 }
8127 return false;
8128}
8129
Tobin Ehlis5b5e7bc2016-03-09 16:12:48 -07008130VK_LAYER_EXPORT VKAPI_ATTR void VKAPI_CALL
8131vkCmdSetEvent(VkCommandBuffer commandBuffer, VkEvent event, VkPipelineStageFlags stageMask) {
Dustin Gravese3319182016-04-05 09:41:17 -06008132 bool skipCall = false;
Tobin Ehlis5b5e7bc2016-03-09 16:12:48 -07008133 layer_data *dev_data = get_my_data_ptr(get_dispatch_key(commandBuffer), layer_data_map);
Jeremy Hayesb9e99232016-04-13 16:20:24 -06008134 std::unique_lock<std::mutex> lock(global_lock);
Tobin Ehlis5b5e7bc2016-03-09 16:12:48 -07008135 GLOBAL_CB_NODE *pCB = getCBNode(dev_data, commandBuffer);
8136 if (pCB) {
8137 skipCall |= addCmd(dev_data, pCB, CMD_SETEVENT, "vkCmdSetEvent()");
8138 skipCall |= insideRenderPass(dev_data, pCB, "vkCmdSetEvent");
8139 pCB->events.push_back(event);
Michael Lentineb4cc5212016-03-18 14:11:44 -05008140 std::function<bool(VkQueue)> eventUpdate =
8141 std::bind(setEventStageMask, std::placeholders::_1, commandBuffer, event, stageMask);
8142 pCB->eventUpdates.push_back(eventUpdate);
Tobin Ehlis5b5e7bc2016-03-09 16:12:48 -07008143 }
Jeremy Hayesb9e99232016-04-13 16:20:24 -06008144 lock.unlock();
Dustin Gravese3319182016-04-05 09:41:17 -06008145 if (!skipCall)
Tobin Ehlis5b5e7bc2016-03-09 16:12:48 -07008146 dev_data->device_dispatch_table->CmdSetEvent(commandBuffer, event, stageMask);
8147}
8148
8149VK_LAYER_EXPORT VKAPI_ATTR void VKAPI_CALL
8150vkCmdResetEvent(VkCommandBuffer commandBuffer, VkEvent event, VkPipelineStageFlags stageMask) {
Dustin Gravese3319182016-04-05 09:41:17 -06008151 bool skipCall = false;
Tobin Ehlis5b5e7bc2016-03-09 16:12:48 -07008152 layer_data *dev_data = get_my_data_ptr(get_dispatch_key(commandBuffer), layer_data_map);
Jeremy Hayesb9e99232016-04-13 16:20:24 -06008153 std::unique_lock<std::mutex> lock(global_lock);
Tobin Ehlis5b5e7bc2016-03-09 16:12:48 -07008154 GLOBAL_CB_NODE *pCB = getCBNode(dev_data, commandBuffer);
8155 if (pCB) {
8156 skipCall |= addCmd(dev_data, pCB, CMD_RESETEVENT, "vkCmdResetEvent()");
8157 skipCall |= insideRenderPass(dev_data, pCB, "vkCmdResetEvent");
8158 pCB->events.push_back(event);
Michael Lentineb4cc5212016-03-18 14:11:44 -05008159 std::function<bool(VkQueue)> eventUpdate =
8160 std::bind(setEventStageMask, std::placeholders::_1, commandBuffer, event, VkPipelineStageFlags(0));
8161 pCB->eventUpdates.push_back(eventUpdate);
Tobin Ehlis5b5e7bc2016-03-09 16:12:48 -07008162 }
Jeremy Hayesb9e99232016-04-13 16:20:24 -06008163 lock.unlock();
Dustin Gravese3319182016-04-05 09:41:17 -06008164 if (!skipCall)
Tobin Ehlis5b5e7bc2016-03-09 16:12:48 -07008165 dev_data->device_dispatch_table->CmdResetEvent(commandBuffer, event, stageMask);
8166}
8167
Dustin Gravese3319182016-04-05 09:41:17 -06008168static bool TransitionImageLayouts(VkCommandBuffer cmdBuffer, uint32_t memBarrierCount,
8169 const VkImageMemoryBarrier *pImgMemBarriers) {
Tobin Ehlis5b5e7bc2016-03-09 16:12:48 -07008170 layer_data *dev_data = get_my_data_ptr(get_dispatch_key(cmdBuffer), layer_data_map);
8171 GLOBAL_CB_NODE *pCB = getCBNode(dev_data, cmdBuffer);
Dustin Gravese3319182016-04-05 09:41:17 -06008172 bool skip = false;
Tobin Ehlis5b5e7bc2016-03-09 16:12:48 -07008173 uint32_t levelCount = 0;
8174 uint32_t layerCount = 0;
8175
8176 for (uint32_t i = 0; i < memBarrierCount; ++i) {
8177 auto mem_barrier = &pImgMemBarriers[i];
8178 if (!mem_barrier)
8179 continue;
8180 // TODO: Do not iterate over every possibility - consolidate where
8181 // possible
8182 ResolveRemainingLevelsLayers(dev_data, &levelCount, &layerCount, mem_barrier->subresourceRange, mem_barrier->image);
8183
8184 for (uint32_t j = 0; j < levelCount; j++) {
8185 uint32_t level = mem_barrier->subresourceRange.baseMipLevel + j;
8186 for (uint32_t k = 0; k < layerCount; k++) {
8187 uint32_t layer = mem_barrier->subresourceRange.baseArrayLayer + k;
8188 VkImageSubresource sub = {mem_barrier->subresourceRange.aspectMask, level, layer};
8189 IMAGE_CMD_BUF_LAYOUT_NODE node;
8190 if (!FindLayout(pCB, mem_barrier->image, sub, node)) {
Michael Lentine08682cd2016-03-24 15:36:27 -05008191 SetLayout(pCB, mem_barrier->image, sub,
8192 IMAGE_CMD_BUF_LAYOUT_NODE(mem_barrier->oldLayout, mem_barrier->newLayout));
Tobin Ehlis5b5e7bc2016-03-09 16:12:48 -07008193 continue;
8194 }
8195 if (mem_barrier->oldLayout == VK_IMAGE_LAYOUT_UNDEFINED) {
8196 // TODO: Set memory invalid which is in mem_tracker currently
8197 } else if (node.layout != mem_barrier->oldLayout) {
8198 skip |= log_msg(dev_data->report_data, VK_DEBUG_REPORT_ERROR_BIT_EXT, (VkDebugReportObjectTypeEXT)0, 0,
8199 __LINE__, DRAWSTATE_INVALID_IMAGE_LAYOUT, "DS", "You cannot transition the layout from %s "
8200 "when current layout is %s.",
8201 string_VkImageLayout(mem_barrier->oldLayout), string_VkImageLayout(node.layout));
8202 }
8203 SetLayout(pCB, mem_barrier->image, sub, mem_barrier->newLayout);
8204 }
8205 }
8206 }
8207 return skip;
8208}
8209
8210// Print readable FlagBits in FlagMask
Dustin Gravese3319182016-04-05 09:41:17 -06008211static std::string string_VkAccessFlags(VkAccessFlags accessMask) {
Tobin Ehlis5b5e7bc2016-03-09 16:12:48 -07008212 std::string result;
8213 std::string separator;
8214
8215 if (accessMask == 0) {
8216 result = "[None]";
8217 } else {
8218 result = "[";
8219 for (auto i = 0; i < 32; i++) {
8220 if (accessMask & (1 << i)) {
8221 result = result + separator + string_VkAccessFlagBits((VkAccessFlagBits)(1 << i));
8222 separator = " | ";
8223 }
8224 }
8225 result = result + "]";
8226 }
8227 return result;
8228}
8229
8230// AccessFlags MUST have 'required_bit' set, and may have one or more of 'optional_bits' set.
8231// If required_bit is zero, accessMask must have at least one of 'optional_bits' set
8232// TODO: Add tracking to ensure that at least one barrier has been set for these layout transitions
Dustin Gravese3319182016-04-05 09:41:17 -06008233static bool ValidateMaskBits(const layer_data *my_data, VkCommandBuffer cmdBuffer, const VkAccessFlags &accessMask,
8234 const VkImageLayout &layout, VkAccessFlags required_bit, VkAccessFlags optional_bits,
8235 const char *type) {
8236 bool skip_call = false;
Tobin Ehlis5b5e7bc2016-03-09 16:12:48 -07008237
8238 if ((accessMask & required_bit) || (!required_bit && (accessMask & optional_bits))) {
8239 if (accessMask & !(required_bit | optional_bits)) {
8240 // TODO: Verify against Valid Use
8241 skip_call |=
8242 log_msg(my_data->report_data, VK_DEBUG_REPORT_WARNING_BIT_EXT, (VkDebugReportObjectTypeEXT)0, 0, __LINE__,
8243 DRAWSTATE_INVALID_BARRIER, "DS", "Additional bits in %s accessMask %d %s are specified when layout is %s.",
8244 type, accessMask, string_VkAccessFlags(accessMask).c_str(), string_VkImageLayout(layout));
8245 }
8246 } else {
8247 if (!required_bit) {
Michael Lentine6044e942016-04-13 17:12:57 -05008248 skip_call |= log_msg(my_data->report_data, VK_DEBUG_REPORT_WARNING_BIT_EXT, (VkDebugReportObjectTypeEXT)0, 0, __LINE__,
Tobin Ehlis5b5e7bc2016-03-09 16:12:48 -07008249 DRAWSTATE_INVALID_BARRIER, "DS", "%s AccessMask %d %s must contain at least one of access bits %d "
8250 "%s when layout is %s, unless the app has previously added a "
8251 "barrier for this transition.",
8252 type, accessMask, string_VkAccessFlags(accessMask).c_str(), optional_bits,
8253 string_VkAccessFlags(optional_bits).c_str(), string_VkImageLayout(layout));
8254 } else {
8255 std::string opt_bits;
8256 if (optional_bits != 0) {
8257 std::stringstream ss;
8258 ss << optional_bits;
8259 opt_bits = "and may have optional bits " + ss.str() + ' ' + string_VkAccessFlags(optional_bits);
8260 }
Michael Lentine6044e942016-04-13 17:12:57 -05008261 skip_call |= log_msg(my_data->report_data, VK_DEBUG_REPORT_WARNING_BIT_EXT, (VkDebugReportObjectTypeEXT)0, 0, __LINE__,
Tobin Ehlis5b5e7bc2016-03-09 16:12:48 -07008262 DRAWSTATE_INVALID_BARRIER, "DS", "%s AccessMask %d %s must have required access bit %d %s %s when "
8263 "layout is %s, unless the app has previously added a barrier for "
8264 "this transition.",
8265 type, accessMask, string_VkAccessFlags(accessMask).c_str(), required_bit,
8266 string_VkAccessFlags(required_bit).c_str(), opt_bits.c_str(), string_VkImageLayout(layout));
8267 }
8268 }
8269 return skip_call;
8270}
8271
Dustin Gravese3319182016-04-05 09:41:17 -06008272static bool ValidateMaskBitsFromLayouts(const layer_data *my_data, VkCommandBuffer cmdBuffer, const VkAccessFlags &accessMask,
8273 const VkImageLayout &layout, const char *type) {
8274 bool skip_call = false;
Tobin Ehlis5b5e7bc2016-03-09 16:12:48 -07008275 switch (layout) {
8276 case VK_IMAGE_LAYOUT_COLOR_ATTACHMENT_OPTIMAL: {
8277 skip_call |= ValidateMaskBits(my_data, cmdBuffer, accessMask, layout, VK_ACCESS_COLOR_ATTACHMENT_WRITE_BIT,
8278 VK_ACCESS_COLOR_ATTACHMENT_READ_BIT, type);
8279 break;
8280 }
8281 case VK_IMAGE_LAYOUT_DEPTH_STENCIL_ATTACHMENT_OPTIMAL: {
8282 skip_call |= ValidateMaskBits(my_data, cmdBuffer, accessMask, layout, VK_ACCESS_DEPTH_STENCIL_ATTACHMENT_WRITE_BIT,
8283 VK_ACCESS_DEPTH_STENCIL_ATTACHMENT_READ_BIT, type);
8284 break;
8285 }
8286 case VK_IMAGE_LAYOUT_TRANSFER_DST_OPTIMAL: {
8287 skip_call |= ValidateMaskBits(my_data, cmdBuffer, accessMask, layout, VK_ACCESS_TRANSFER_WRITE_BIT, 0, type);
8288 break;
8289 }
8290 case VK_IMAGE_LAYOUT_PREINITIALIZED: {
8291 skip_call |= ValidateMaskBits(my_data, cmdBuffer, accessMask, layout, VK_ACCESS_HOST_WRITE_BIT, 0, type);
8292 break;
8293 }
8294 case VK_IMAGE_LAYOUT_DEPTH_STENCIL_READ_ONLY_OPTIMAL: {
8295 skip_call |= ValidateMaskBits(my_data, cmdBuffer, accessMask, layout, 0,
8296 VK_ACCESS_DEPTH_STENCIL_ATTACHMENT_READ_BIT | VK_ACCESS_SHADER_READ_BIT, type);
8297 break;
8298 }
8299 case VK_IMAGE_LAYOUT_SHADER_READ_ONLY_OPTIMAL: {
8300 skip_call |= ValidateMaskBits(my_data, cmdBuffer, accessMask, layout, 0,
8301 VK_ACCESS_INPUT_ATTACHMENT_READ_BIT | VK_ACCESS_SHADER_READ_BIT, type);
8302 break;
8303 }
8304 case VK_IMAGE_LAYOUT_TRANSFER_SRC_OPTIMAL: {
8305 skip_call |= ValidateMaskBits(my_data, cmdBuffer, accessMask, layout, VK_ACCESS_TRANSFER_READ_BIT, 0, type);
8306 break;
8307 }
8308 case VK_IMAGE_LAYOUT_UNDEFINED: {
8309 if (accessMask != 0) {
8310 // TODO: Verify against Valid Use section spec
8311 skip_call |=
8312 log_msg(my_data->report_data, VK_DEBUG_REPORT_WARNING_BIT_EXT, (VkDebugReportObjectTypeEXT)0, 0, __LINE__,
8313 DRAWSTATE_INVALID_BARRIER, "DS", "Additional bits in %s accessMask %d %s are specified when layout is %s.",
8314 type, accessMask, string_VkAccessFlags(accessMask).c_str(), string_VkImageLayout(layout));
8315 }
8316 break;
8317 }
8318 case VK_IMAGE_LAYOUT_GENERAL:
8319 default: { break; }
8320 }
8321 return skip_call;
8322}
8323
Dustin Gravese3319182016-04-05 09:41:17 -06008324static bool ValidateBarriers(const char *funcName, VkCommandBuffer cmdBuffer, uint32_t memBarrierCount,
8325 const VkMemoryBarrier *pMemBarriers, uint32_t bufferBarrierCount,
8326 const VkBufferMemoryBarrier *pBufferMemBarriers, uint32_t imageMemBarrierCount,
8327 const VkImageMemoryBarrier *pImageMemBarriers) {
8328 bool skip_call = false;
Tobin Ehlis5b5e7bc2016-03-09 16:12:48 -07008329 layer_data *dev_data = get_my_data_ptr(get_dispatch_key(cmdBuffer), layer_data_map);
8330 GLOBAL_CB_NODE *pCB = getCBNode(dev_data, cmdBuffer);
8331 if (pCB->activeRenderPass && memBarrierCount) {
8332 if (!dev_data->renderPassMap[pCB->activeRenderPass]->hasSelfDependency[pCB->activeSubpass]) {
8333 skip_call |= log_msg(dev_data->report_data, VK_DEBUG_REPORT_ERROR_BIT_EXT, (VkDebugReportObjectTypeEXT)0, 0, __LINE__,
8334 DRAWSTATE_INVALID_BARRIER, "DS", "%s: Barriers cannot be set during subpass %d "
8335 "with no self dependency specified.",
8336 funcName, pCB->activeSubpass);
8337 }
8338 }
8339 for (uint32_t i = 0; i < imageMemBarrierCount; ++i) {
8340 auto mem_barrier = &pImageMemBarriers[i];
8341 auto image_data = dev_data->imageMap.find(mem_barrier->image);
8342 if (image_data != dev_data->imageMap.end()) {
8343 uint32_t src_q_f_index = mem_barrier->srcQueueFamilyIndex;
8344 uint32_t dst_q_f_index = mem_barrier->dstQueueFamilyIndex;
8345 if (image_data->second.createInfo.sharingMode == VK_SHARING_MODE_CONCURRENT) {
8346 // srcQueueFamilyIndex and dstQueueFamilyIndex must both
8347 // be VK_QUEUE_FAMILY_IGNORED
8348 if ((src_q_f_index != VK_QUEUE_FAMILY_IGNORED) || (dst_q_f_index != VK_QUEUE_FAMILY_IGNORED)) {
8349 skip_call |= log_msg(dev_data->report_data, VK_DEBUG_REPORT_ERROR_BIT_EXT, (VkDebugReportObjectTypeEXT)0, 0,
8350 __LINE__, DRAWSTATE_INVALID_QUEUE_INDEX, "DS",
8351 "%s: Image Barrier for image 0x%" PRIx64 " was created with sharingMode of "
8352 "VK_SHARING_MODE_CONCURRENT. Src and dst "
8353 " queueFamilyIndices must be VK_QUEUE_FAMILY_IGNORED.",
8354 funcName, reinterpret_cast<const uint64_t &>(mem_barrier->image));
8355 }
8356 } else {
8357 // Sharing mode is VK_SHARING_MODE_EXCLUSIVE. srcQueueFamilyIndex and
8358 // dstQueueFamilyIndex must either both be VK_QUEUE_FAMILY_IGNORED,
8359 // or both be a valid queue family
8360 if (((src_q_f_index == VK_QUEUE_FAMILY_IGNORED) || (dst_q_f_index == VK_QUEUE_FAMILY_IGNORED)) &&
8361 (src_q_f_index != dst_q_f_index)) {
8362 skip_call |=
8363 log_msg(dev_data->report_data, VK_DEBUG_REPORT_ERROR_BIT_EXT, (VkDebugReportObjectTypeEXT)0, 0, __LINE__,
8364 DRAWSTATE_INVALID_QUEUE_INDEX, "DS", "%s: Image 0x%" PRIx64 " was created with sharingMode "
8365 "of VK_SHARING_MODE_EXCLUSIVE. If one of src- or "
8366 "dstQueueFamilyIndex is VK_QUEUE_FAMILY_IGNORED, both "
8367 "must be.",
8368 funcName, reinterpret_cast<const uint64_t &>(mem_barrier->image));
8369 } else if (((src_q_f_index != VK_QUEUE_FAMILY_IGNORED) && (dst_q_f_index != VK_QUEUE_FAMILY_IGNORED)) &&
Tobin Ehlisb1b606d2016-04-11 14:49:55 -06008370 ((src_q_f_index >= dev_data->phys_dev_properties.queue_family_properties.size()) ||
8371 (dst_q_f_index >= dev_data->phys_dev_properties.queue_family_properties.size()))) {
Tobin Ehlis5b5e7bc2016-03-09 16:12:48 -07008372 skip_call |= log_msg(dev_data->report_data, VK_DEBUG_REPORT_ERROR_BIT_EXT, (VkDebugReportObjectTypeEXT)0, 0,
8373 __LINE__, DRAWSTATE_INVALID_QUEUE_INDEX, "DS",
8374 "%s: Image 0x%" PRIx64 " was created with sharingMode "
8375 "of VK_SHARING_MODE_EXCLUSIVE, but srcQueueFamilyIndex %d"
8376 " or dstQueueFamilyIndex %d is greater than " PRINTF_SIZE_T_SPECIFIER
8377 "queueFamilies crated for this device.",
8378 funcName, reinterpret_cast<const uint64_t &>(mem_barrier->image), src_q_f_index,
Tobin Ehlisb1b606d2016-04-11 14:49:55 -06008379 dst_q_f_index, dev_data->phys_dev_properties.queue_family_properties.size());
Tobin Ehlis5b5e7bc2016-03-09 16:12:48 -07008380 }
8381 }
8382 }
8383
8384 if (mem_barrier) {
8385 skip_call |=
8386 ValidateMaskBitsFromLayouts(dev_data, cmdBuffer, mem_barrier->srcAccessMask, mem_barrier->oldLayout, "Source");
8387 skip_call |=
8388 ValidateMaskBitsFromLayouts(dev_data, cmdBuffer, mem_barrier->dstAccessMask, mem_barrier->newLayout, "Dest");
8389 if (mem_barrier->newLayout == VK_IMAGE_LAYOUT_UNDEFINED || mem_barrier->newLayout == VK_IMAGE_LAYOUT_PREINITIALIZED) {
8390 log_msg(dev_data->report_data, VK_DEBUG_REPORT_ERROR_BIT_EXT, (VkDebugReportObjectTypeEXT)0, 0, __LINE__,
8391 DRAWSTATE_INVALID_BARRIER, "DS", "%s: Image Layout cannot be transitioned to UNDEFINED or "
8392 "PREINITIALIZED.",
8393 funcName);
8394 }
8395 auto image_data = dev_data->imageMap.find(mem_barrier->image);
Jamie Madill1d5109d2016-04-04 15:09:51 -04008396 VkFormat format = VK_FORMAT_UNDEFINED;
8397 uint32_t arrayLayers = 0, mipLevels = 0;
Tobin Ehlis5b5e7bc2016-03-09 16:12:48 -07008398 bool imageFound = false;
8399 if (image_data != dev_data->imageMap.end()) {
8400 format = image_data->second.createInfo.format;
8401 arrayLayers = image_data->second.createInfo.arrayLayers;
8402 mipLevels = image_data->second.createInfo.mipLevels;
8403 imageFound = true;
8404 } else if (dev_data->device_extensions.wsi_enabled) {
8405 auto imageswap_data = dev_data->device_extensions.imageToSwapchainMap.find(mem_barrier->image);
8406 if (imageswap_data != dev_data->device_extensions.imageToSwapchainMap.end()) {
8407 auto swapchain_data = dev_data->device_extensions.swapchainMap.find(imageswap_data->second);
8408 if (swapchain_data != dev_data->device_extensions.swapchainMap.end()) {
8409 format = swapchain_data->second->createInfo.imageFormat;
8410 arrayLayers = swapchain_data->second->createInfo.imageArrayLayers;
8411 mipLevels = 1;
8412 imageFound = true;
8413 }
8414 }
8415 }
8416 if (imageFound) {
8417 if (vk_format_is_depth_and_stencil(format) &&
8418 (!(mem_barrier->subresourceRange.aspectMask & VK_IMAGE_ASPECT_DEPTH_BIT) ||
8419 !(mem_barrier->subresourceRange.aspectMask & VK_IMAGE_ASPECT_STENCIL_BIT))) {
8420 log_msg(dev_data->report_data, VK_DEBUG_REPORT_ERROR_BIT_EXT, (VkDebugReportObjectTypeEXT)0, 0, __LINE__,
8421 DRAWSTATE_INVALID_BARRIER, "DS", "%s: Image is a depth and stencil format and thus must "
8422 "have both VK_IMAGE_ASPECT_DEPTH_BIT and "
8423 "VK_IMAGE_ASPECT_STENCIL_BIT set.",
8424 funcName);
8425 }
Michael Lentinef0d091d2016-03-18 14:49:09 -05008426 int layerCount = (mem_barrier->subresourceRange.layerCount == VK_REMAINING_ARRAY_LAYERS)
8427 ? 1
8428 : mem_barrier->subresourceRange.layerCount;
8429 if ((mem_barrier->subresourceRange.baseArrayLayer + layerCount) > arrayLayers) {
Tobin Ehlis5b5e7bc2016-03-09 16:12:48 -07008430 log_msg(dev_data->report_data, VK_DEBUG_REPORT_ERROR_BIT_EXT, (VkDebugReportObjectTypeEXT)0, 0, __LINE__,
8431 DRAWSTATE_INVALID_BARRIER, "DS", "%s: Subresource must have the sum of the "
8432 "baseArrayLayer (%d) and layerCount (%d) be less "
8433 "than or equal to the total number of layers (%d).",
8434 funcName, mem_barrier->subresourceRange.baseArrayLayer, mem_barrier->subresourceRange.layerCount,
8435 arrayLayers);
8436 }
Michael Lentinef0d091d2016-03-18 14:49:09 -05008437 int levelCount = (mem_barrier->subresourceRange.levelCount == VK_REMAINING_MIP_LEVELS)
8438 ? 1
8439 : mem_barrier->subresourceRange.levelCount;
8440 if ((mem_barrier->subresourceRange.baseMipLevel + levelCount) > mipLevels) {
Tobin Ehlis5b5e7bc2016-03-09 16:12:48 -07008441 log_msg(dev_data->report_data, VK_DEBUG_REPORT_ERROR_BIT_EXT, (VkDebugReportObjectTypeEXT)0, 0, __LINE__,
8442 DRAWSTATE_INVALID_BARRIER, "DS", "%s: Subresource must have the sum of the baseMipLevel "
8443 "(%d) and levelCount (%d) be less than or equal to "
8444 "the total number of levels (%d).",
8445 funcName, mem_barrier->subresourceRange.baseMipLevel, mem_barrier->subresourceRange.levelCount,
8446 mipLevels);
8447 }
8448 }
8449 }
8450 }
8451 for (uint32_t i = 0; i < bufferBarrierCount; ++i) {
8452 auto mem_barrier = &pBufferMemBarriers[i];
8453 if (pCB->activeRenderPass) {
8454 skip_call |=
8455 log_msg(dev_data->report_data, VK_DEBUG_REPORT_ERROR_BIT_EXT, (VkDebugReportObjectTypeEXT)0, 0, __LINE__,
8456 DRAWSTATE_INVALID_BARRIER, "DS", "%s: Buffer Barriers cannot be used during a render pass.", funcName);
8457 }
8458 if (!mem_barrier)
8459 continue;
8460
8461 // Validate buffer barrier queue family indices
8462 if ((mem_barrier->srcQueueFamilyIndex != VK_QUEUE_FAMILY_IGNORED &&
Tobin Ehlisb1b606d2016-04-11 14:49:55 -06008463 mem_barrier->srcQueueFamilyIndex >= dev_data->phys_dev_properties.queue_family_properties.size()) ||
Tobin Ehlis5b5e7bc2016-03-09 16:12:48 -07008464 (mem_barrier->dstQueueFamilyIndex != VK_QUEUE_FAMILY_IGNORED &&
Tobin Ehlisb1b606d2016-04-11 14:49:55 -06008465 mem_barrier->dstQueueFamilyIndex >= dev_data->phys_dev_properties.queue_family_properties.size())) {
Tobin Ehlis5b5e7bc2016-03-09 16:12:48 -07008466 skip_call |= log_msg(dev_data->report_data, VK_DEBUG_REPORT_ERROR_BIT_EXT, (VkDebugReportObjectTypeEXT)0, 0, __LINE__,
8467 DRAWSTATE_INVALID_QUEUE_INDEX, "DS",
8468 "%s: Buffer Barrier 0x%" PRIx64 " has QueueFamilyIndex greater "
8469 "than the number of QueueFamilies (" PRINTF_SIZE_T_SPECIFIER ") for this device.",
8470 funcName, reinterpret_cast<const uint64_t &>(mem_barrier->buffer),
Tobin Ehlisb1b606d2016-04-11 14:49:55 -06008471 dev_data->phys_dev_properties.queue_family_properties.size());
Tobin Ehlis5b5e7bc2016-03-09 16:12:48 -07008472 }
8473
8474 auto buffer_data = dev_data->bufferMap.find(mem_barrier->buffer);
Tobin Ehlis94c53c02016-04-05 13:33:00 -06008475 VkDeviceSize buffer_size = (buffer_data->second.createInfo.sType == VK_STRUCTURE_TYPE_BUFFER_CREATE_INFO)
8476 ? buffer_data->second.createInfo.size
8477 : 0;
Tobin Ehlis5b5e7bc2016-03-09 16:12:48 -07008478 if (buffer_data != dev_data->bufferMap.end()) {
8479 if (mem_barrier->offset >= buffer_size) {
Tobin Ehlis94c53c02016-04-05 13:33:00 -06008480 skip_call |= log_msg(
8481 dev_data->report_data, VK_DEBUG_REPORT_ERROR_BIT_EXT, (VkDebugReportObjectTypeEXT)0, 0, __LINE__,
8482 DRAWSTATE_INVALID_BARRIER, "DS",
8483 "%s: Buffer Barrier 0x%" PRIx64 " has offset %" PRIu64 " which is not less than total size %" PRIu64 ".",
8484 funcName, reinterpret_cast<const uint64_t &>(mem_barrier->buffer),
8485 reinterpret_cast<const uint64_t &>(mem_barrier->offset), reinterpret_cast<const uint64_t &>(buffer_size));
Tobin Ehlis5b5e7bc2016-03-09 16:12:48 -07008486 } else if (mem_barrier->size != VK_WHOLE_SIZE && (mem_barrier->offset + mem_barrier->size > buffer_size)) {
Tobin Ehlis94c53c02016-04-05 13:33:00 -06008487 skip_call |= log_msg(
8488 dev_data->report_data, VK_DEBUG_REPORT_ERROR_BIT_EXT, (VkDebugReportObjectTypeEXT)0, 0, __LINE__,
8489 DRAWSTATE_INVALID_BARRIER, "DS", "%s: Buffer Barrier 0x%" PRIx64 " has offset %" PRIu64 " and size %" PRIu64
8490 " whose sum is greater than total size %" PRIu64 ".",
8491 funcName, reinterpret_cast<const uint64_t &>(mem_barrier->buffer),
8492 reinterpret_cast<const uint64_t &>(mem_barrier->offset), reinterpret_cast<const uint64_t &>(mem_barrier->size),
8493 reinterpret_cast<const uint64_t &>(buffer_size));
Tobin Ehlis5b5e7bc2016-03-09 16:12:48 -07008494 }
8495 }
8496 }
8497 return skip_call;
8498}
8499
Chris Forbes2ab14382016-03-31 11:22:37 +13008500bool validateEventStageMask(VkQueue queue, GLOBAL_CB_NODE *pCB, uint32_t eventCount, size_t firstEventIndex, VkPipelineStageFlags sourceStageMask) {
Michael Lentineb4cc5212016-03-18 14:11:44 -05008501 bool skip_call = false;
8502 VkPipelineStageFlags stageMask = 0;
8503 layer_data *dev_data = get_my_data_ptr(get_dispatch_key(queue), layer_data_map);
8504 for (uint32_t i = 0; i < eventCount; ++i) {
Chris Forbes2ab14382016-03-31 11:22:37 +13008505 auto event = pCB->events[firstEventIndex + i];
Michael Lentineb4cc5212016-03-18 14:11:44 -05008506 auto queue_data = dev_data->queueMap.find(queue);
8507 if (queue_data == dev_data->queueMap.end())
8508 return false;
Chris Forbes2ab14382016-03-31 11:22:37 +13008509 auto event_data = queue_data->second.eventToStageMap.find(event);
Michael Lentineb4cc5212016-03-18 14:11:44 -05008510 if (event_data != queue_data->second.eventToStageMap.end()) {
8511 stageMask |= event_data->second;
8512 } else {
Chris Forbes2ab14382016-03-31 11:22:37 +13008513 auto global_event_data = dev_data->eventMap.find(event);
Michael Lentineb4cc5212016-03-18 14:11:44 -05008514 if (global_event_data == dev_data->eventMap.end()) {
8515 skip_call |= log_msg(dev_data->report_data, VK_DEBUG_REPORT_ERROR_BIT_EXT, VK_DEBUG_REPORT_OBJECT_TYPE_EVENT_EXT,
Chris Forbes27c3e0d2016-03-31 11:47:29 +13008516 reinterpret_cast<const uint64_t &>(event), __LINE__, DRAWSTATE_INVALID_EVENT, "DS",
8517 "Event 0x%" PRIx64 " cannot be waited on if it has never been set.",
Chris Forbes2ab14382016-03-31 11:22:37 +13008518 reinterpret_cast<const uint64_t &>(event));
Michael Lentineb4cc5212016-03-18 14:11:44 -05008519 } else {
8520 stageMask |= global_event_data->second.stageMask;
8521 }
8522 }
8523 }
8524 if (sourceStageMask != stageMask) {
8525 skip_call |=
8526 log_msg(dev_data->report_data, VK_DEBUG_REPORT_ERROR_BIT_EXT, (VkDebugReportObjectTypeEXT)0, 0, __LINE__,
Chris Forbes27c3e0d2016-03-31 11:47:29 +13008527 DRAWSTATE_INVALID_EVENT, "DS",
Michael Lentineb4cc5212016-03-18 14:11:44 -05008528 "Submitting cmdbuffer with call to VkCmdWaitEvents using srcStageMask 0x%x which must be the bitwise OR of the "
8529 "stageMask parameters used in calls to vkCmdSetEvent and VK_PIPELINE_STAGE_HOST_BIT if used with vkSetEvent.",
8530 sourceStageMask);
8531 }
8532 return skip_call;
8533}
8534
Tobin Ehlis5b5e7bc2016-03-09 16:12:48 -07008535VK_LAYER_EXPORT VKAPI_ATTR void VKAPI_CALL
8536vkCmdWaitEvents(VkCommandBuffer commandBuffer, uint32_t eventCount, const VkEvent *pEvents, VkPipelineStageFlags sourceStageMask,
8537 VkPipelineStageFlags dstStageMask, uint32_t memoryBarrierCount, const VkMemoryBarrier *pMemoryBarriers,
8538 uint32_t bufferMemoryBarrierCount, const VkBufferMemoryBarrier *pBufferMemoryBarriers,
8539 uint32_t imageMemoryBarrierCount, const VkImageMemoryBarrier *pImageMemoryBarriers) {
Dustin Gravese3319182016-04-05 09:41:17 -06008540 bool skipCall = false;
Tobin Ehlis5b5e7bc2016-03-09 16:12:48 -07008541 layer_data *dev_data = get_my_data_ptr(get_dispatch_key(commandBuffer), layer_data_map);
Jeremy Hayesb9e99232016-04-13 16:20:24 -06008542 std::unique_lock<std::mutex> lock(global_lock);
Tobin Ehlis5b5e7bc2016-03-09 16:12:48 -07008543 GLOBAL_CB_NODE *pCB = getCBNode(dev_data, commandBuffer);
8544 if (pCB) {
Chris Forbes2ab14382016-03-31 11:22:37 +13008545 auto firstEventIndex = pCB->events.size();
Tobin Ehlis5b5e7bc2016-03-09 16:12:48 -07008546 for (uint32_t i = 0; i < eventCount; ++i) {
8547 pCB->waitedEvents.push_back(pEvents[i]);
8548 pCB->events.push_back(pEvents[i]);
Tobin Ehlis5b5e7bc2016-03-09 16:12:48 -07008549 }
Michael Lentineb4cc5212016-03-18 14:11:44 -05008550 std::function<bool(VkQueue)> eventUpdate =
Chris Forbes2ab14382016-03-31 11:22:37 +13008551 std::bind(validateEventStageMask, std::placeholders::_1, pCB, eventCount, firstEventIndex, sourceStageMask);
Michael Lentineb4cc5212016-03-18 14:11:44 -05008552 pCB->eventUpdates.push_back(eventUpdate);
Tobin Ehlis5b5e7bc2016-03-09 16:12:48 -07008553 if (pCB->state == CB_RECORDING) {
8554 skipCall |= addCmd(dev_data, pCB, CMD_WAITEVENTS, "vkCmdWaitEvents()");
8555 } else {
8556 skipCall |= report_error_no_cb_begin(dev_data, commandBuffer, "vkCmdWaitEvents()");
8557 }
8558 skipCall |= TransitionImageLayouts(commandBuffer, imageMemoryBarrierCount, pImageMemoryBarriers);
8559 skipCall |=
8560 ValidateBarriers("vkCmdWaitEvents", commandBuffer, memoryBarrierCount, pMemoryBarriers, bufferMemoryBarrierCount,
8561 pBufferMemoryBarriers, imageMemoryBarrierCount, pImageMemoryBarriers);
8562 }
Jeremy Hayesb9e99232016-04-13 16:20:24 -06008563 lock.unlock();
Dustin Gravese3319182016-04-05 09:41:17 -06008564 if (!skipCall)
Tobin Ehlis5b5e7bc2016-03-09 16:12:48 -07008565 dev_data->device_dispatch_table->CmdWaitEvents(commandBuffer, eventCount, pEvents, sourceStageMask, dstStageMask,
8566 memoryBarrierCount, pMemoryBarriers, bufferMemoryBarrierCount,
8567 pBufferMemoryBarriers, imageMemoryBarrierCount, pImageMemoryBarriers);
8568}
8569
8570VK_LAYER_EXPORT VKAPI_ATTR void VKAPI_CALL
8571vkCmdPipelineBarrier(VkCommandBuffer commandBuffer, VkPipelineStageFlags srcStageMask, VkPipelineStageFlags dstStageMask,
8572 VkDependencyFlags dependencyFlags, uint32_t memoryBarrierCount, const VkMemoryBarrier *pMemoryBarriers,
8573 uint32_t bufferMemoryBarrierCount, const VkBufferMemoryBarrier *pBufferMemoryBarriers,
8574 uint32_t imageMemoryBarrierCount, const VkImageMemoryBarrier *pImageMemoryBarriers) {
Dustin Gravese3319182016-04-05 09:41:17 -06008575 bool skipCall = false;
Tobin Ehlis5b5e7bc2016-03-09 16:12:48 -07008576 layer_data *dev_data = get_my_data_ptr(get_dispatch_key(commandBuffer), layer_data_map);
Jeremy Hayesb9e99232016-04-13 16:20:24 -06008577 std::unique_lock<std::mutex> lock(global_lock);
Tobin Ehlis5b5e7bc2016-03-09 16:12:48 -07008578 GLOBAL_CB_NODE *pCB = getCBNode(dev_data, commandBuffer);
8579 if (pCB) {
8580 skipCall |= addCmd(dev_data, pCB, CMD_PIPELINEBARRIER, "vkCmdPipelineBarrier()");
8581 skipCall |= TransitionImageLayouts(commandBuffer, imageMemoryBarrierCount, pImageMemoryBarriers);
8582 skipCall |=
8583 ValidateBarriers("vkCmdPipelineBarrier", commandBuffer, memoryBarrierCount, pMemoryBarriers, bufferMemoryBarrierCount,
8584 pBufferMemoryBarriers, imageMemoryBarrierCount, pImageMemoryBarriers);
8585 }
Jeremy Hayesb9e99232016-04-13 16:20:24 -06008586 lock.unlock();
Dustin Gravese3319182016-04-05 09:41:17 -06008587 if (!skipCall)
Tobin Ehlis5b5e7bc2016-03-09 16:12:48 -07008588 dev_data->device_dispatch_table->CmdPipelineBarrier(commandBuffer, srcStageMask, dstStageMask, dependencyFlags,
8589 memoryBarrierCount, pMemoryBarriers, bufferMemoryBarrierCount,
8590 pBufferMemoryBarriers, imageMemoryBarrierCount, pImageMemoryBarriers);
8591}
8592
8593VK_LAYER_EXPORT VKAPI_ATTR void VKAPI_CALL
8594vkCmdBeginQuery(VkCommandBuffer commandBuffer, VkQueryPool queryPool, uint32_t slot, VkFlags flags) {
Dustin Gravese3319182016-04-05 09:41:17 -06008595 bool skipCall = false;
Tobin Ehlis5b5e7bc2016-03-09 16:12:48 -07008596 layer_data *dev_data = get_my_data_ptr(get_dispatch_key(commandBuffer), layer_data_map);
Jeremy Hayesb9e99232016-04-13 16:20:24 -06008597 std::unique_lock<std::mutex> lock(global_lock);
Tobin Ehlis5b5e7bc2016-03-09 16:12:48 -07008598 GLOBAL_CB_NODE *pCB = getCBNode(dev_data, commandBuffer);
8599 if (pCB) {
8600 QueryObject query = {queryPool, slot};
8601 pCB->activeQueries.insert(query);
8602 if (!pCB->startedQueries.count(query)) {
8603 pCB->startedQueries.insert(query);
8604 }
8605 skipCall |= addCmd(dev_data, pCB, CMD_BEGINQUERY, "vkCmdBeginQuery()");
8606 }
Jeremy Hayesb9e99232016-04-13 16:20:24 -06008607 lock.unlock();
Dustin Gravese3319182016-04-05 09:41:17 -06008608 if (!skipCall)
Tobin Ehlis5b5e7bc2016-03-09 16:12:48 -07008609 dev_data->device_dispatch_table->CmdBeginQuery(commandBuffer, queryPool, slot, flags);
8610}
8611
8612VK_LAYER_EXPORT VKAPI_ATTR void VKAPI_CALL vkCmdEndQuery(VkCommandBuffer commandBuffer, VkQueryPool queryPool, uint32_t slot) {
Dustin Gravese3319182016-04-05 09:41:17 -06008613 bool skipCall = false;
Tobin Ehlis5b5e7bc2016-03-09 16:12:48 -07008614 layer_data *dev_data = get_my_data_ptr(get_dispatch_key(commandBuffer), layer_data_map);
Jeremy Hayesb9e99232016-04-13 16:20:24 -06008615 std::unique_lock<std::mutex> lock(global_lock);
Tobin Ehlis5b5e7bc2016-03-09 16:12:48 -07008616 GLOBAL_CB_NODE *pCB = getCBNode(dev_data, commandBuffer);
8617 if (pCB) {
8618 QueryObject query = {queryPool, slot};
8619 if (!pCB->activeQueries.count(query)) {
8620 skipCall |=
8621 log_msg(dev_data->report_data, VK_DEBUG_REPORT_ERROR_BIT_EXT, (VkDebugReportObjectTypeEXT)0, 0, __LINE__,
8622 DRAWSTATE_INVALID_QUERY, "DS", "Ending a query before it was started: queryPool %" PRIu64 ", index %d",
8623 (uint64_t)(queryPool), slot);
8624 } else {
8625 pCB->activeQueries.erase(query);
8626 }
8627 pCB->queryToStateMap[query] = 1;
8628 if (pCB->state == CB_RECORDING) {
8629 skipCall |= addCmd(dev_data, pCB, CMD_ENDQUERY, "VkCmdEndQuery()");
8630 } else {
8631 skipCall |= report_error_no_cb_begin(dev_data, commandBuffer, "vkCmdEndQuery()");
8632 }
8633 }
Jeremy Hayesb9e99232016-04-13 16:20:24 -06008634 lock.unlock();
Dustin Gravese3319182016-04-05 09:41:17 -06008635 if (!skipCall)
Tobin Ehlis5b5e7bc2016-03-09 16:12:48 -07008636 dev_data->device_dispatch_table->CmdEndQuery(commandBuffer, queryPool, slot);
8637}
8638
8639VK_LAYER_EXPORT VKAPI_ATTR void VKAPI_CALL
8640vkCmdResetQueryPool(VkCommandBuffer commandBuffer, VkQueryPool queryPool, uint32_t firstQuery, uint32_t queryCount) {
Dustin Gravese3319182016-04-05 09:41:17 -06008641 bool skipCall = false;
Tobin Ehlis5b5e7bc2016-03-09 16:12:48 -07008642 layer_data *dev_data = get_my_data_ptr(get_dispatch_key(commandBuffer), layer_data_map);
Jeremy Hayesb9e99232016-04-13 16:20:24 -06008643 std::unique_lock<std::mutex> lock(global_lock);
Tobin Ehlis5b5e7bc2016-03-09 16:12:48 -07008644 GLOBAL_CB_NODE *pCB = getCBNode(dev_data, commandBuffer);
8645 if (pCB) {
8646 for (uint32_t i = 0; i < queryCount; i++) {
8647 QueryObject query = {queryPool, firstQuery + i};
8648 pCB->waitedEventsBeforeQueryReset[query] = pCB->waitedEvents;
8649 pCB->queryToStateMap[query] = 0;
8650 }
8651 if (pCB->state == CB_RECORDING) {
8652 skipCall |= addCmd(dev_data, pCB, CMD_RESETQUERYPOOL, "VkCmdResetQueryPool()");
8653 } else {
8654 skipCall |= report_error_no_cb_begin(dev_data, commandBuffer, "vkCmdResetQueryPool()");
8655 }
8656 skipCall |= insideRenderPass(dev_data, pCB, "vkCmdQueryPool");
8657 }
Jeremy Hayesb9e99232016-04-13 16:20:24 -06008658 lock.unlock();
Dustin Gravese3319182016-04-05 09:41:17 -06008659 if (!skipCall)
Tobin Ehlis5b5e7bc2016-03-09 16:12:48 -07008660 dev_data->device_dispatch_table->CmdResetQueryPool(commandBuffer, queryPool, firstQuery, queryCount);
8661}
8662
8663VK_LAYER_EXPORT VKAPI_ATTR void VKAPI_CALL
8664vkCmdCopyQueryPoolResults(VkCommandBuffer commandBuffer, VkQueryPool queryPool, uint32_t firstQuery, uint32_t queryCount,
8665 VkBuffer dstBuffer, VkDeviceSize dstOffset, VkDeviceSize stride, VkQueryResultFlags flags) {
Dustin Gravese3319182016-04-05 09:41:17 -06008666 bool skipCall = false;
Tobin Ehlis5b5e7bc2016-03-09 16:12:48 -07008667 layer_data *dev_data = get_my_data_ptr(get_dispatch_key(commandBuffer), layer_data_map);
Jeremy Hayesb9e99232016-04-13 16:20:24 -06008668 std::unique_lock<std::mutex> lock(global_lock);
Tobin Ehlis5b5e7bc2016-03-09 16:12:48 -07008669 GLOBAL_CB_NODE *pCB = getCBNode(dev_data, commandBuffer);
Mark Lobodzinski03b71512016-03-23 14:33:02 -06008670#if MTMERGESOURCE
Tobin Ehlis5b5e7bc2016-03-09 16:12:48 -07008671 VkDeviceMemory mem;
Tobin Ehlis72d66f02016-03-21 14:14:44 -06008672 auto cb_data = dev_data->commandBufferMap.find(commandBuffer);
Tobin Ehlis5b5e7bc2016-03-09 16:12:48 -07008673 skipCall |=
Chris Forbes0a1ce3d2016-04-06 15:16:26 +12008674 get_mem_binding_from_object(dev_data, (uint64_t)dstBuffer, VK_DEBUG_REPORT_OBJECT_TYPE_BUFFER_EXT, &mem);
Tobin Ehlis72d66f02016-03-21 14:14:44 -06008675 if (cb_data != dev_data->commandBufferMap.end()) {
Dustin Gravese3319182016-04-05 09:41:17 -06008676 std::function<bool()> function = [=]() {
Tobin Ehlis5b5e7bc2016-03-09 16:12:48 -07008677 set_memory_valid(dev_data, mem, true);
Dustin Gravese3319182016-04-05 09:41:17 -06008678 return false;
Tobin Ehlis5b5e7bc2016-03-09 16:12:48 -07008679 };
Tobin Ehlis72d66f02016-03-21 14:14:44 -06008680 cb_data->second->validate_functions.push_back(function);
Tobin Ehlis5b5e7bc2016-03-09 16:12:48 -07008681 }
8682 skipCall |= update_cmd_buf_and_mem_references(dev_data, commandBuffer, mem, "vkCmdCopyQueryPoolResults");
8683 // Validate that DST buffer has correct usage flags set
Chris Forbes0a1ce3d2016-04-06 15:16:26 +12008684 skipCall |= validate_buffer_usage_flags(dev_data, dstBuffer, VK_BUFFER_USAGE_TRANSFER_DST_BIT, true,
Tobin Ehlis5b5e7bc2016-03-09 16:12:48 -07008685 "vkCmdCopyQueryPoolResults()", "VK_BUFFER_USAGE_TRANSFER_DST_BIT");
8686#endif
8687 if (pCB) {
8688 for (uint32_t i = 0; i < queryCount; i++) {
8689 QueryObject query = {queryPool, firstQuery + i};
8690 if (!pCB->queryToStateMap[query]) {
8691 skipCall |= log_msg(dev_data->report_data, VK_DEBUG_REPORT_ERROR_BIT_EXT, (VkDebugReportObjectTypeEXT)0, 0,
8692 __LINE__, DRAWSTATE_INVALID_QUERY, "DS",
8693 "Requesting a copy from query to buffer with invalid query: queryPool %" PRIu64 ", index %d",
8694 (uint64_t)(queryPool), firstQuery + i);
8695 }
8696 }
8697 if (pCB->state == CB_RECORDING) {
8698 skipCall |= addCmd(dev_data, pCB, CMD_COPYQUERYPOOLRESULTS, "vkCmdCopyQueryPoolResults()");
8699 } else {
8700 skipCall |= report_error_no_cb_begin(dev_data, commandBuffer, "vkCmdCopyQueryPoolResults()");
8701 }
8702 skipCall |= insideRenderPass(dev_data, pCB, "vkCmdCopyQueryPoolResults");
8703 }
Jeremy Hayesb9e99232016-04-13 16:20:24 -06008704 lock.unlock();
Dustin Gravese3319182016-04-05 09:41:17 -06008705 if (!skipCall)
Tobin Ehlis5b5e7bc2016-03-09 16:12:48 -07008706 dev_data->device_dispatch_table->CmdCopyQueryPoolResults(commandBuffer, queryPool, firstQuery, queryCount, dstBuffer,
8707 dstOffset, stride, flags);
8708}
8709
8710VK_LAYER_EXPORT VKAPI_ATTR void VKAPI_CALL vkCmdPushConstants(VkCommandBuffer commandBuffer, VkPipelineLayout layout,
8711 VkShaderStageFlags stageFlags, uint32_t offset, uint32_t size,
8712 const void *pValues) {
8713 bool skipCall = false;
8714 layer_data *dev_data = get_my_data_ptr(get_dispatch_key(commandBuffer), layer_data_map);
Jeremy Hayesb9e99232016-04-13 16:20:24 -06008715 std::unique_lock<std::mutex> lock(global_lock);
Tobin Ehlis5b5e7bc2016-03-09 16:12:48 -07008716 GLOBAL_CB_NODE *pCB = getCBNode(dev_data, commandBuffer);
8717 if (pCB) {
8718 if (pCB->state == CB_RECORDING) {
8719 skipCall |= addCmd(dev_data, pCB, CMD_PUSHCONSTANTS, "vkCmdPushConstants()");
8720 } else {
8721 skipCall |= report_error_no_cb_begin(dev_data, commandBuffer, "vkCmdPushConstants()");
8722 }
8723 }
Tobin Ehlisb1b606d2016-04-11 14:49:55 -06008724 if ((offset + size) > dev_data->phys_dev_properties.properties.limits.maxPushConstantsSize) {
Tobin Ehlis5b5e7bc2016-03-09 16:12:48 -07008725 skipCall |= validatePushConstantSize(dev_data, offset, size, "vkCmdPushConstants()");
8726 }
8727 // TODO : Add warning if push constant update doesn't align with range
Jeremy Hayesb9e99232016-04-13 16:20:24 -06008728 lock.unlock();
Tobin Ehlis5b5e7bc2016-03-09 16:12:48 -07008729 if (!skipCall)
8730 dev_data->device_dispatch_table->CmdPushConstants(commandBuffer, layout, stageFlags, offset, size, pValues);
8731}
8732
8733VK_LAYER_EXPORT VKAPI_ATTR void VKAPI_CALL
8734vkCmdWriteTimestamp(VkCommandBuffer commandBuffer, VkPipelineStageFlagBits pipelineStage, VkQueryPool queryPool, uint32_t slot) {
Dustin Gravese3319182016-04-05 09:41:17 -06008735 bool skipCall = false;
Tobin Ehlis5b5e7bc2016-03-09 16:12:48 -07008736 layer_data *dev_data = get_my_data_ptr(get_dispatch_key(commandBuffer), layer_data_map);
Jeremy Hayesb9e99232016-04-13 16:20:24 -06008737 std::unique_lock<std::mutex> lock(global_lock);
Tobin Ehlis5b5e7bc2016-03-09 16:12:48 -07008738 GLOBAL_CB_NODE *pCB = getCBNode(dev_data, commandBuffer);
8739 if (pCB) {
8740 QueryObject query = {queryPool, slot};
8741 pCB->queryToStateMap[query] = 1;
8742 if (pCB->state == CB_RECORDING) {
8743 skipCall |= addCmd(dev_data, pCB, CMD_WRITETIMESTAMP, "vkCmdWriteTimestamp()");
8744 } else {
8745 skipCall |= report_error_no_cb_begin(dev_data, commandBuffer, "vkCmdWriteTimestamp()");
8746 }
8747 }
Jeremy Hayesb9e99232016-04-13 16:20:24 -06008748 lock.unlock();
Dustin Gravese3319182016-04-05 09:41:17 -06008749 if (!skipCall)
Tobin Ehlis5b5e7bc2016-03-09 16:12:48 -07008750 dev_data->device_dispatch_table->CmdWriteTimestamp(commandBuffer, pipelineStage, queryPool, slot);
8751}
8752
8753VK_LAYER_EXPORT VKAPI_ATTR VkResult VKAPI_CALL vkCreateFramebuffer(VkDevice device, const VkFramebufferCreateInfo *pCreateInfo,
8754 const VkAllocationCallbacks *pAllocator,
8755 VkFramebuffer *pFramebuffer) {
8756 layer_data *dev_data = get_my_data_ptr(get_dispatch_key(device), layer_data_map);
8757 VkResult result = dev_data->device_dispatch_table->CreateFramebuffer(device, pCreateInfo, pAllocator, pFramebuffer);
8758 if (VK_SUCCESS == result) {
8759 // Shadow create info and store in map
Jeremy Hayesb9e99232016-04-13 16:20:24 -06008760 std::lock_guard<std::mutex> lock(global_lock);
Chris Forbesfcaf4f82016-03-31 16:05:02 +13008761
8762 auto & fbNode = dev_data->frameBufferMap[*pFramebuffer];
8763 fbNode.createInfo = *pCreateInfo;
8764 if (pCreateInfo->pAttachments) {
8765 auto attachments = new VkImageView[pCreateInfo->attachmentCount];
8766 memcpy(attachments,
8767 pCreateInfo->pAttachments,
8768 pCreateInfo->attachmentCount * sizeof(VkImageView));
8769 fbNode.createInfo.pAttachments = attachments;
8770 }
Tobin Ehlis5b5e7bc2016-03-09 16:12:48 -07008771 for (uint32_t i = 0; i < pCreateInfo->attachmentCount; ++i) {
8772 VkImageView view = pCreateInfo->pAttachments[i];
8773 auto view_data = dev_data->imageViewMap.find(view);
8774 if (view_data == dev_data->imageViewMap.end()) {
8775 continue;
8776 }
8777 MT_FB_ATTACHMENT_INFO fb_info;
Chris Forbes0a1ce3d2016-04-06 15:16:26 +12008778 get_mem_binding_from_object(dev_data, (uint64_t)(view_data->second.image), VK_DEBUG_REPORT_OBJECT_TYPE_IMAGE_EXT,
Tobin Ehlis5b5e7bc2016-03-09 16:12:48 -07008779 &fb_info.mem);
8780 fb_info.image = view_data->second.image;
Chris Forbesfcaf4f82016-03-31 16:05:02 +13008781 fbNode.attachments.push_back(fb_info);
Tobin Ehlis5b5e7bc2016-03-09 16:12:48 -07008782 }
Tobin Ehlis5b5e7bc2016-03-09 16:12:48 -07008783 }
8784 return result;
8785}
8786
Dustin Gravese3319182016-04-05 09:41:17 -06008787static bool FindDependency(const int index, const int dependent, const std::vector<DAGNode> &subpass_to_node,
8788 std::unordered_set<uint32_t> &processed_nodes) {
Tobin Ehlis5b5e7bc2016-03-09 16:12:48 -07008789 // If we have already checked this node we have not found a dependency path so return false.
8790 if (processed_nodes.count(index))
Dustin Gravese3319182016-04-05 09:41:17 -06008791 return false;
Tobin Ehlis5b5e7bc2016-03-09 16:12:48 -07008792 processed_nodes.insert(index);
8793 const DAGNode &node = subpass_to_node[index];
8794 // Look for a dependency path. If one exists return true else recurse on the previous nodes.
8795 if (std::find(node.prev.begin(), node.prev.end(), dependent) == node.prev.end()) {
8796 for (auto elem : node.prev) {
8797 if (FindDependency(elem, dependent, subpass_to_node, processed_nodes))
Dustin Gravese3319182016-04-05 09:41:17 -06008798 return true;
Tobin Ehlis5b5e7bc2016-03-09 16:12:48 -07008799 }
8800 } else {
Dustin Gravese3319182016-04-05 09:41:17 -06008801 return true;
Tobin Ehlis5b5e7bc2016-03-09 16:12:48 -07008802 }
Dustin Gravese3319182016-04-05 09:41:17 -06008803 return false;
Tobin Ehlis5b5e7bc2016-03-09 16:12:48 -07008804}
8805
Dustin Gravese3319182016-04-05 09:41:17 -06008806static bool CheckDependencyExists(const layer_data *my_data, const int subpass, const std::vector<uint32_t> &dependent_subpasses,
8807 const std::vector<DAGNode> &subpass_to_node, bool &skip_call) {
8808 bool result = true;
Tobin Ehlis5b5e7bc2016-03-09 16:12:48 -07008809 // Loop through all subpasses that share the same attachment and make sure a dependency exists
8810 for (uint32_t k = 0; k < dependent_subpasses.size(); ++k) {
Jamie Madill1d5109d2016-04-04 15:09:51 -04008811 if (static_cast<uint32_t>(subpass) == dependent_subpasses[k])
Tobin Ehlis5b5e7bc2016-03-09 16:12:48 -07008812 continue;
8813 const DAGNode &node = subpass_to_node[subpass];
8814 // Check for a specified dependency between the two nodes. If one exists we are done.
8815 auto prev_elem = std::find(node.prev.begin(), node.prev.end(), dependent_subpasses[k]);
8816 auto next_elem = std::find(node.next.begin(), node.next.end(), dependent_subpasses[k]);
8817 if (prev_elem == node.prev.end() && next_elem == node.next.end()) {
8818 // If no dependency exits an implicit dependency still might. If so, warn and if not throw an error.
8819 std::unordered_set<uint32_t> processed_nodes;
8820 if (FindDependency(subpass, dependent_subpasses[k], subpass_to_node, processed_nodes) ||
8821 FindDependency(dependent_subpasses[k], subpass, subpass_to_node, processed_nodes)) {
Michael Lentine3da84682016-04-14 14:46:28 -05008822 skip_call |= log_msg(my_data->report_data, VK_DEBUG_REPORT_WARNING_BIT_EXT, (VkDebugReportObjectTypeEXT)0, 0,
Tobin Ehlis5b5e7bc2016-03-09 16:12:48 -07008823 __LINE__, DRAWSTATE_INVALID_RENDERPASS, "DS",
8824 "A dependency between subpasses %d and %d must exist but only an implicit one is specified.",
8825 subpass, dependent_subpasses[k]);
8826 } else {
8827 skip_call |= log_msg(my_data->report_data, VK_DEBUG_REPORT_ERROR_BIT_EXT, (VkDebugReportObjectTypeEXT)0, 0,
8828 __LINE__, DRAWSTATE_INVALID_RENDERPASS, "DS",
8829 "A dependency between subpasses %d and %d must exist but one is not specified.", subpass,
8830 dependent_subpasses[k]);
Dustin Gravese3319182016-04-05 09:41:17 -06008831 result = false;
Tobin Ehlis5b5e7bc2016-03-09 16:12:48 -07008832 }
8833 }
8834 }
8835 return result;
8836}
8837
Dustin Gravese3319182016-04-05 09:41:17 -06008838static bool CheckPreserved(const layer_data *my_data, const VkRenderPassCreateInfo *pCreateInfo, const int index,
8839 const uint32_t attachment, const std::vector<DAGNode> &subpass_to_node, int depth, bool &skip_call) {
Tobin Ehlis5b5e7bc2016-03-09 16:12:48 -07008840 const DAGNode &node = subpass_to_node[index];
8841 // If this node writes to the attachment return true as next nodes need to preserve the attachment.
8842 const VkSubpassDescription &subpass = pCreateInfo->pSubpasses[index];
8843 for (uint32_t j = 0; j < subpass.colorAttachmentCount; ++j) {
8844 if (attachment == subpass.pColorAttachments[j].attachment)
Dustin Gravese3319182016-04-05 09:41:17 -06008845 return true;
Tobin Ehlis5b5e7bc2016-03-09 16:12:48 -07008846 }
8847 if (subpass.pDepthStencilAttachment && subpass.pDepthStencilAttachment->attachment != VK_ATTACHMENT_UNUSED) {
8848 if (attachment == subpass.pDepthStencilAttachment->attachment)
Dustin Gravese3319182016-04-05 09:41:17 -06008849 return true;
Tobin Ehlis5b5e7bc2016-03-09 16:12:48 -07008850 }
Dustin Gravese3319182016-04-05 09:41:17 -06008851 bool result = false;
Tobin Ehlis5b5e7bc2016-03-09 16:12:48 -07008852 // Loop through previous nodes and see if any of them write to the attachment.
8853 for (auto elem : node.prev) {
8854 result |= CheckPreserved(my_data, pCreateInfo, elem, attachment, subpass_to_node, depth + 1, skip_call);
8855 }
8856 // If the attachment was written to by a previous node than this node needs to preserve it.
8857 if (result && depth > 0) {
8858 const VkSubpassDescription &subpass = pCreateInfo->pSubpasses[index];
Dustin Gravese3319182016-04-05 09:41:17 -06008859 bool has_preserved = false;
Tobin Ehlis5b5e7bc2016-03-09 16:12:48 -07008860 for (uint32_t j = 0; j < subpass.preserveAttachmentCount; ++j) {
8861 if (subpass.pPreserveAttachments[j] == attachment) {
Dustin Gravese3319182016-04-05 09:41:17 -06008862 has_preserved = true;
Tobin Ehlis5b5e7bc2016-03-09 16:12:48 -07008863 break;
8864 }
8865 }
Dustin Gravese3319182016-04-05 09:41:17 -06008866 if (!has_preserved) {
Tobin Ehlis5b5e7bc2016-03-09 16:12:48 -07008867 skip_call |=
8868 log_msg(my_data->report_data, VK_DEBUG_REPORT_ERROR_BIT_EXT, (VkDebugReportObjectTypeEXT)0, 0, __LINE__,
8869 DRAWSTATE_INVALID_RENDERPASS, "DS",
8870 "Attachment %d is used by a later subpass and must be preserved in subpass %d.", attachment, index);
8871 }
8872 }
8873 return result;
8874}
8875
8876template <class T> bool isRangeOverlapping(T offset1, T size1, T offset2, T size2) {
8877 return (((offset1 + size1) > offset2) && ((offset1 + size1) < (offset2 + size2))) ||
8878 ((offset1 > offset2) && (offset1 < (offset2 + size2)));
8879}
8880
8881bool isRegionOverlapping(VkImageSubresourceRange range1, VkImageSubresourceRange range2) {
8882 return (isRangeOverlapping(range1.baseMipLevel, range1.levelCount, range2.baseMipLevel, range2.levelCount) &&
8883 isRangeOverlapping(range1.baseArrayLayer, range1.layerCount, range2.baseArrayLayer, range2.layerCount));
8884}
8885
Dustin Gravese3319182016-04-05 09:41:17 -06008886static bool ValidateDependencies(const layer_data *my_data, const VkRenderPassBeginInfo *pRenderPassBegin,
8887 const std::vector<DAGNode> &subpass_to_node) {
8888 bool skip_call = false;
Tobin Ehlis5b5e7bc2016-03-09 16:12:48 -07008889 const VkFramebufferCreateInfo *pFramebufferInfo = &my_data->frameBufferMap.at(pRenderPassBegin->framebuffer).createInfo;
8890 const VkRenderPassCreateInfo *pCreateInfo = my_data->renderPassMap.at(pRenderPassBegin->renderPass)->pCreateInfo;
8891 std::vector<std::vector<uint32_t>> output_attachment_to_subpass(pCreateInfo->attachmentCount);
8892 std::vector<std::vector<uint32_t>> input_attachment_to_subpass(pCreateInfo->attachmentCount);
8893 std::vector<std::vector<uint32_t>> overlapping_attachments(pCreateInfo->attachmentCount);
8894 // Find overlapping attachments
8895 for (uint32_t i = 0; i < pCreateInfo->attachmentCount; ++i) {
8896 for (uint32_t j = i + 1; j < pCreateInfo->attachmentCount; ++j) {
8897 VkImageView viewi = pFramebufferInfo->pAttachments[i];
8898 VkImageView viewj = pFramebufferInfo->pAttachments[j];
8899 if (viewi == viewj) {
8900 overlapping_attachments[i].push_back(j);
8901 overlapping_attachments[j].push_back(i);
8902 continue;
8903 }
8904 auto view_data_i = my_data->imageViewMap.find(viewi);
8905 auto view_data_j = my_data->imageViewMap.find(viewj);
8906 if (view_data_i == my_data->imageViewMap.end() || view_data_j == my_data->imageViewMap.end()) {
8907 continue;
8908 }
8909 if (view_data_i->second.image == view_data_j->second.image &&
8910 isRegionOverlapping(view_data_i->second.subresourceRange, view_data_j->second.subresourceRange)) {
8911 overlapping_attachments[i].push_back(j);
8912 overlapping_attachments[j].push_back(i);
8913 continue;
8914 }
8915 auto image_data_i = my_data->imageMap.find(view_data_i->second.image);
8916 auto image_data_j = my_data->imageMap.find(view_data_j->second.image);
8917 if (image_data_i == my_data->imageMap.end() || image_data_j == my_data->imageMap.end()) {
8918 continue;
8919 }
8920 if (image_data_i->second.mem == image_data_j->second.mem &&
8921 isRangeOverlapping(image_data_i->second.memOffset, image_data_i->second.memSize, image_data_j->second.memOffset,
8922 image_data_j->second.memSize)) {
8923 overlapping_attachments[i].push_back(j);
8924 overlapping_attachments[j].push_back(i);
8925 }
8926 }
8927 }
8928 for (uint32_t i = 0; i < overlapping_attachments.size(); ++i) {
8929 uint32_t attachment = i;
8930 for (auto other_attachment : overlapping_attachments[i]) {
8931 if (!(pCreateInfo->pAttachments[attachment].flags & VK_ATTACHMENT_DESCRIPTION_MAY_ALIAS_BIT)) {
8932 skip_call |=
8933 log_msg(my_data->report_data, VK_DEBUG_REPORT_ERROR_BIT_EXT, (VkDebugReportObjectTypeEXT)0, 0, __LINE__,
8934 DRAWSTATE_INVALID_RENDERPASS, "DS", "Attachment %d aliases attachment %d but doesn't "
8935 "set VK_ATTACHMENT_DESCRIPTION_MAY_ALIAS_BIT.",
8936 attachment, other_attachment);
8937 }
8938 if (!(pCreateInfo->pAttachments[other_attachment].flags & VK_ATTACHMENT_DESCRIPTION_MAY_ALIAS_BIT)) {
8939 skip_call |=
8940 log_msg(my_data->report_data, VK_DEBUG_REPORT_ERROR_BIT_EXT, (VkDebugReportObjectTypeEXT)0, 0, __LINE__,
8941 DRAWSTATE_INVALID_RENDERPASS, "DS", "Attachment %d aliases attachment %d but doesn't "
8942 "set VK_ATTACHMENT_DESCRIPTION_MAY_ALIAS_BIT.",
8943 other_attachment, attachment);
8944 }
8945 }
8946 }
8947 // Find for each attachment the subpasses that use them.
Mark Young1c1ee7b2016-03-30 02:23:18 -06008948 unordered_set<uint32_t> attachmentIndices;
Tobin Ehlis5b5e7bc2016-03-09 16:12:48 -07008949 for (uint32_t i = 0; i < pCreateInfo->subpassCount; ++i) {
8950 const VkSubpassDescription &subpass = pCreateInfo->pSubpasses[i];
Mark Young1c1ee7b2016-03-30 02:23:18 -06008951 attachmentIndices.clear();
Tobin Ehlis5b5e7bc2016-03-09 16:12:48 -07008952 for (uint32_t j = 0; j < subpass.inputAttachmentCount; ++j) {
8953 uint32_t attachment = subpass.pInputAttachments[j].attachment;
8954 input_attachment_to_subpass[attachment].push_back(i);
8955 for (auto overlapping_attachment : overlapping_attachments[attachment]) {
8956 input_attachment_to_subpass[overlapping_attachment].push_back(i);
8957 }
8958 }
8959 for (uint32_t j = 0; j < subpass.colorAttachmentCount; ++j) {
8960 uint32_t attachment = subpass.pColorAttachments[j].attachment;
8961 output_attachment_to_subpass[attachment].push_back(i);
8962 for (auto overlapping_attachment : overlapping_attachments[attachment]) {
8963 output_attachment_to_subpass[overlapping_attachment].push_back(i);
8964 }
Mark Young1c1ee7b2016-03-30 02:23:18 -06008965 attachmentIndices.insert(attachment);
Tobin Ehlis5b5e7bc2016-03-09 16:12:48 -07008966 }
8967 if (subpass.pDepthStencilAttachment && subpass.pDepthStencilAttachment->attachment != VK_ATTACHMENT_UNUSED) {
8968 uint32_t attachment = subpass.pDepthStencilAttachment->attachment;
8969 output_attachment_to_subpass[attachment].push_back(i);
8970 for (auto overlapping_attachment : overlapping_attachments[attachment]) {
8971 output_attachment_to_subpass[overlapping_attachment].push_back(i);
8972 }
Mark Young1c1ee7b2016-03-30 02:23:18 -06008973
8974 if (attachmentIndices.count(attachment)) {
8975 skip_call |=
8976 log_msg(my_data->report_data, VK_DEBUG_REPORT_ERROR_BIT_EXT, (VkDebugReportObjectTypeEXT)0,
8977 0, __LINE__, DRAWSTATE_INVALID_RENDERPASS, "DS",
8978 "Cannot use same attachment (%u) as both color and depth output in same subpass (%u).",
8979 attachment, i);
8980 }
Tobin Ehlis5b5e7bc2016-03-09 16:12:48 -07008981 }
8982 }
8983 // If there is a dependency needed make sure one exists
8984 for (uint32_t i = 0; i < pCreateInfo->subpassCount; ++i) {
8985 const VkSubpassDescription &subpass = pCreateInfo->pSubpasses[i];
8986 // If the attachment is an input then all subpasses that output must have a dependency relationship
8987 for (uint32_t j = 0; j < subpass.inputAttachmentCount; ++j) {
8988 const uint32_t &attachment = subpass.pInputAttachments[j].attachment;
8989 CheckDependencyExists(my_data, i, output_attachment_to_subpass[attachment], subpass_to_node, skip_call);
8990 }
8991 // If the attachment is an output then all subpasses that use the attachment must have a dependency relationship
8992 for (uint32_t j = 0; j < subpass.colorAttachmentCount; ++j) {
8993 const uint32_t &attachment = subpass.pColorAttachments[j].attachment;
8994 CheckDependencyExists(my_data, i, output_attachment_to_subpass[attachment], subpass_to_node, skip_call);
8995 CheckDependencyExists(my_data, i, input_attachment_to_subpass[attachment], subpass_to_node, skip_call);
8996 }
8997 if (subpass.pDepthStencilAttachment && subpass.pDepthStencilAttachment->attachment != VK_ATTACHMENT_UNUSED) {
8998 const uint32_t &attachment = subpass.pDepthStencilAttachment->attachment;
8999 CheckDependencyExists(my_data, i, output_attachment_to_subpass[attachment], subpass_to_node, skip_call);
9000 CheckDependencyExists(my_data, i, input_attachment_to_subpass[attachment], subpass_to_node, skip_call);
9001 }
9002 }
9003 // Loop through implicit dependencies, if this pass reads make sure the attachment is preserved for all passes after it was
9004 // written.
9005 for (uint32_t i = 0; i < pCreateInfo->subpassCount; ++i) {
9006 const VkSubpassDescription &subpass = pCreateInfo->pSubpasses[i];
9007 for (uint32_t j = 0; j < subpass.inputAttachmentCount; ++j) {
9008 CheckPreserved(my_data, pCreateInfo, i, subpass.pInputAttachments[j].attachment, subpass_to_node, 0, skip_call);
9009 }
9010 }
9011 return skip_call;
9012}
9013
Dustin Gravese3319182016-04-05 09:41:17 -06009014static bool ValidateLayouts(const layer_data *my_data, VkDevice device, const VkRenderPassCreateInfo *pCreateInfo) {
9015 bool skip = false;
Tobin Ehlis5b5e7bc2016-03-09 16:12:48 -07009016
9017 for (uint32_t i = 0; i < pCreateInfo->subpassCount; ++i) {
9018 const VkSubpassDescription &subpass = pCreateInfo->pSubpasses[i];
9019 for (uint32_t j = 0; j < subpass.inputAttachmentCount; ++j) {
9020 if (subpass.pInputAttachments[j].layout != VK_IMAGE_LAYOUT_DEPTH_STENCIL_READ_ONLY_OPTIMAL &&
9021 subpass.pInputAttachments[j].layout != VK_IMAGE_LAYOUT_SHADER_READ_ONLY_OPTIMAL) {
9022 if (subpass.pInputAttachments[j].layout == VK_IMAGE_LAYOUT_GENERAL) {
9023 // TODO: Verify Valid Use in spec. I believe this is allowed (valid) but may not be optimal performance
9024 skip |= log_msg(my_data->report_data, VK_DEBUG_REPORT_PERFORMANCE_WARNING_BIT_EXT,
9025 (VkDebugReportObjectTypeEXT)0, 0, __LINE__, DRAWSTATE_INVALID_IMAGE_LAYOUT, "DS",
9026 "Layout for input attachment is GENERAL but should be READ_ONLY_OPTIMAL.");
9027 } else {
Michael Lentine4d23e012016-03-25 17:06:04 -05009028 skip |= log_msg(my_data->report_data, VK_DEBUG_REPORT_ERROR_BIT_EXT, (VkDebugReportObjectTypeEXT)0, 0, __LINE__,
Tobin Ehlis5b5e7bc2016-03-09 16:12:48 -07009029 DRAWSTATE_INVALID_IMAGE_LAYOUT, "DS",
9030 "Layout for input attachment is %s but can only be READ_ONLY_OPTIMAL or GENERAL.",
9031 string_VkImageLayout(subpass.pInputAttachments[j].layout));
9032 }
9033 }
9034 }
9035 for (uint32_t j = 0; j < subpass.colorAttachmentCount; ++j) {
9036 if (subpass.pColorAttachments[j].layout != VK_IMAGE_LAYOUT_COLOR_ATTACHMENT_OPTIMAL) {
9037 if (subpass.pColorAttachments[j].layout == VK_IMAGE_LAYOUT_GENERAL) {
9038 // TODO: Verify Valid Use in spec. I believe this is allowed (valid) but may not be optimal performance
9039 skip |= log_msg(my_data->report_data, VK_DEBUG_REPORT_PERFORMANCE_WARNING_BIT_EXT,
9040 (VkDebugReportObjectTypeEXT)0, 0, __LINE__, DRAWSTATE_INVALID_IMAGE_LAYOUT, "DS",
9041 "Layout for color attachment is GENERAL but should be COLOR_ATTACHMENT_OPTIMAL.");
9042 } else {
Michael Lentine4d23e012016-03-25 17:06:04 -05009043 skip |= log_msg(my_data->report_data, VK_DEBUG_REPORT_ERROR_BIT_EXT, (VkDebugReportObjectTypeEXT)0, 0, __LINE__,
Tobin Ehlis5b5e7bc2016-03-09 16:12:48 -07009044 DRAWSTATE_INVALID_IMAGE_LAYOUT, "DS",
9045 "Layout for color attachment is %s but can only be COLOR_ATTACHMENT_OPTIMAL or GENERAL.",
9046 string_VkImageLayout(subpass.pColorAttachments[j].layout));
9047 }
9048 }
9049 }
9050 if ((subpass.pDepthStencilAttachment != NULL) && (subpass.pDepthStencilAttachment->attachment != VK_ATTACHMENT_UNUSED)) {
9051 if (subpass.pDepthStencilAttachment->layout != VK_IMAGE_LAYOUT_DEPTH_STENCIL_ATTACHMENT_OPTIMAL) {
9052 if (subpass.pDepthStencilAttachment->layout == VK_IMAGE_LAYOUT_GENERAL) {
9053 // TODO: Verify Valid Use in spec. I believe this is allowed (valid) but may not be optimal performance
9054 skip |= log_msg(my_data->report_data, VK_DEBUG_REPORT_PERFORMANCE_WARNING_BIT_EXT,
9055 (VkDebugReportObjectTypeEXT)0, 0, __LINE__, DRAWSTATE_INVALID_IMAGE_LAYOUT, "DS",
9056 "Layout for depth attachment is GENERAL but should be DEPTH_STENCIL_ATTACHMENT_OPTIMAL.");
9057 } else {
9058 skip |=
Michael Lentine4d23e012016-03-25 17:06:04 -05009059 log_msg(my_data->report_data, VK_DEBUG_REPORT_ERROR_BIT_EXT, (VkDebugReportObjectTypeEXT)0, 0, __LINE__,
Tobin Ehlis5b5e7bc2016-03-09 16:12:48 -07009060 DRAWSTATE_INVALID_IMAGE_LAYOUT, "DS",
9061 "Layout for depth attachment is %s but can only be DEPTH_STENCIL_ATTACHMENT_OPTIMAL or GENERAL.",
9062 string_VkImageLayout(subpass.pDepthStencilAttachment->layout));
9063 }
9064 }
9065 }
9066 }
9067 return skip;
9068}
9069
Dustin Gravese3319182016-04-05 09:41:17 -06009070static bool CreatePassDAG(const layer_data *my_data, VkDevice device, const VkRenderPassCreateInfo *pCreateInfo,
9071 std::vector<DAGNode> &subpass_to_node, std::vector<bool> &has_self_dependency) {
9072 bool skip_call = false;
Tobin Ehlis5b5e7bc2016-03-09 16:12:48 -07009073 for (uint32_t i = 0; i < pCreateInfo->subpassCount; ++i) {
9074 DAGNode &subpass_node = subpass_to_node[i];
9075 subpass_node.pass = i;
9076 }
9077 for (uint32_t i = 0; i < pCreateInfo->dependencyCount; ++i) {
9078 const VkSubpassDependency &dependency = pCreateInfo->pDependencies[i];
9079 if (dependency.srcSubpass > dependency.dstSubpass && dependency.srcSubpass != VK_SUBPASS_EXTERNAL &&
9080 dependency.dstSubpass != VK_SUBPASS_EXTERNAL) {
9081 skip_call |= log_msg(my_data->report_data, VK_DEBUG_REPORT_ERROR_BIT_EXT, (VkDebugReportObjectTypeEXT)0, 0, __LINE__,
9082 DRAWSTATE_INVALID_RENDERPASS, "DS",
9083 "Depedency graph must be specified such that an earlier pass cannot depend on a later pass.");
9084 } else if (dependency.srcSubpass == VK_SUBPASS_EXTERNAL && dependency.dstSubpass == VK_SUBPASS_EXTERNAL) {
9085 skip_call |= log_msg(my_data->report_data, VK_DEBUG_REPORT_ERROR_BIT_EXT, (VkDebugReportObjectTypeEXT)0, 0, __LINE__,
9086 DRAWSTATE_INVALID_RENDERPASS, "DS", "The src and dest subpasses cannot both be external.");
9087 } else if (dependency.srcSubpass == dependency.dstSubpass) {
9088 has_self_dependency[dependency.srcSubpass] = true;
9089 }
9090 if (dependency.dstSubpass != VK_SUBPASS_EXTERNAL) {
9091 subpass_to_node[dependency.dstSubpass].prev.push_back(dependency.srcSubpass);
9092 }
9093 if (dependency.srcSubpass != VK_SUBPASS_EXTERNAL) {
9094 subpass_to_node[dependency.srcSubpass].next.push_back(dependency.dstSubpass);
9095 }
9096 }
9097 return skip_call;
9098}
Chris Forbes918c2832016-03-18 16:30:03 +13009099
Tobin Ehlis5b5e7bc2016-03-09 16:12:48 -07009100
9101VK_LAYER_EXPORT VKAPI_ATTR VkResult VKAPI_CALL vkCreateShaderModule(VkDevice device, const VkShaderModuleCreateInfo *pCreateInfo,
9102 const VkAllocationCallbacks *pAllocator,
9103 VkShaderModule *pShaderModule) {
9104 layer_data *my_data = get_my_data_ptr(get_dispatch_key(device), layer_data_map);
Dustin Gravese3319182016-04-05 09:41:17 -06009105 bool skip_call = false;
Tobin Ehlis5b5e7bc2016-03-09 16:12:48 -07009106 if (!shader_is_spirv(pCreateInfo)) {
9107 skip_call |= log_msg(my_data->report_data, VK_DEBUG_REPORT_ERROR_BIT_EXT, VK_DEBUG_REPORT_OBJECT_TYPE_DEVICE_EXT,
9108 /* dev */ 0, __LINE__, SHADER_CHECKER_NON_SPIRV_SHADER, "SC", "Shader is not SPIR-V");
9109 }
9110
Dustin Gravese3319182016-04-05 09:41:17 -06009111 if (skip_call)
Tobin Ehlis5b5e7bc2016-03-09 16:12:48 -07009112 return VK_ERROR_VALIDATION_FAILED_EXT;
9113
9114 VkResult res = my_data->device_dispatch_table->CreateShaderModule(device, pCreateInfo, pAllocator, pShaderModule);
9115
9116 if (res == VK_SUCCESS) {
Jeremy Hayesb9e99232016-04-13 16:20:24 -06009117 std::lock_guard<std::mutex> lock(global_lock);
Chris Forbes918c2832016-03-18 16:30:03 +13009118 my_data->shaderModuleMap[*pShaderModule] = unique_ptr<shader_module>(new shader_module(pCreateInfo));
Tobin Ehlis5b5e7bc2016-03-09 16:12:48 -07009119 }
9120 return res;
9121}
9122
9123VK_LAYER_EXPORT VKAPI_ATTR VkResult VKAPI_CALL vkCreateRenderPass(VkDevice device, const VkRenderPassCreateInfo *pCreateInfo,
9124 const VkAllocationCallbacks *pAllocator,
9125 VkRenderPass *pRenderPass) {
Dustin Gravese3319182016-04-05 09:41:17 -06009126 bool skip_call = false;
Tobin Ehlis5b5e7bc2016-03-09 16:12:48 -07009127 layer_data *dev_data = get_my_data_ptr(get_dispatch_key(device), layer_data_map);
Jeremy Hayesb9e99232016-04-13 16:20:24 -06009128 std::unique_lock<std::mutex> lock(global_lock);
Tobin Ehlis5b5e7bc2016-03-09 16:12:48 -07009129 // Create DAG
9130 std::vector<bool> has_self_dependency(pCreateInfo->subpassCount);
9131 std::vector<DAGNode> subpass_to_node(pCreateInfo->subpassCount);
9132 skip_call |= CreatePassDAG(dev_data, device, pCreateInfo, subpass_to_node, has_self_dependency);
9133 // Validate
9134 skip_call |= ValidateLayouts(dev_data, device, pCreateInfo);
Dustin Gravese3319182016-04-05 09:41:17 -06009135 if (skip_call) {
Jeremy Hayesb9e99232016-04-13 16:20:24 -06009136 lock.unlock();
Tobin Ehlis5b5e7bc2016-03-09 16:12:48 -07009137 return VK_ERROR_VALIDATION_FAILED_EXT;
9138 }
Jeremy Hayesb9e99232016-04-13 16:20:24 -06009139 lock.unlock();
Tobin Ehlis5b5e7bc2016-03-09 16:12:48 -07009140 VkResult result = dev_data->device_dispatch_table->CreateRenderPass(device, pCreateInfo, pAllocator, pRenderPass);
9141 if (VK_SUCCESS == result) {
Jeremy Hayesb9e99232016-04-13 16:20:24 -06009142 lock.lock();
Tobin Ehlis5b5e7bc2016-03-09 16:12:48 -07009143 // TODOSC : Merge in tracking of renderpass from shader_checker
9144 // Shadow create info and store in map
9145 VkRenderPassCreateInfo *localRPCI = new VkRenderPassCreateInfo(*pCreateInfo);
9146 if (pCreateInfo->pAttachments) {
9147 localRPCI->pAttachments = new VkAttachmentDescription[localRPCI->attachmentCount];
9148 memcpy((void *)localRPCI->pAttachments, pCreateInfo->pAttachments,
9149 localRPCI->attachmentCount * sizeof(VkAttachmentDescription));
9150 }
9151 if (pCreateInfo->pSubpasses) {
9152 localRPCI->pSubpasses = new VkSubpassDescription[localRPCI->subpassCount];
9153 memcpy((void *)localRPCI->pSubpasses, pCreateInfo->pSubpasses, localRPCI->subpassCount * sizeof(VkSubpassDescription));
9154
9155 for (uint32_t i = 0; i < localRPCI->subpassCount; i++) {
9156 VkSubpassDescription *subpass = (VkSubpassDescription *)&localRPCI->pSubpasses[i];
9157 const uint32_t attachmentCount = subpass->inputAttachmentCount +
9158 subpass->colorAttachmentCount * (1 + (subpass->pResolveAttachments ? 1 : 0)) +
9159 ((subpass->pDepthStencilAttachment) ? 1 : 0) + subpass->preserveAttachmentCount;
9160 VkAttachmentReference *attachments = new VkAttachmentReference[attachmentCount];
9161
9162 memcpy(attachments, subpass->pInputAttachments, sizeof(attachments[0]) * subpass->inputAttachmentCount);
9163 subpass->pInputAttachments = attachments;
9164 attachments += subpass->inputAttachmentCount;
9165
9166 memcpy(attachments, subpass->pColorAttachments, sizeof(attachments[0]) * subpass->colorAttachmentCount);
9167 subpass->pColorAttachments = attachments;
9168 attachments += subpass->colorAttachmentCount;
9169
9170 if (subpass->pResolveAttachments) {
9171 memcpy(attachments, subpass->pResolveAttachments, sizeof(attachments[0]) * subpass->colorAttachmentCount);
9172 subpass->pResolveAttachments = attachments;
9173 attachments += subpass->colorAttachmentCount;
9174 }
9175
9176 if (subpass->pDepthStencilAttachment) {
9177 memcpy(attachments, subpass->pDepthStencilAttachment, sizeof(attachments[0]) * 1);
9178 subpass->pDepthStencilAttachment = attachments;
9179 attachments += 1;
9180 }
9181
9182 memcpy(attachments, subpass->pPreserveAttachments, sizeof(attachments[0]) * subpass->preserveAttachmentCount);
9183 subpass->pPreserveAttachments = &attachments->attachment;
9184 }
9185 }
9186 if (pCreateInfo->pDependencies) {
9187 localRPCI->pDependencies = new VkSubpassDependency[localRPCI->dependencyCount];
9188 memcpy((void *)localRPCI->pDependencies, pCreateInfo->pDependencies,
9189 localRPCI->dependencyCount * sizeof(VkSubpassDependency));
9190 }
9191 dev_data->renderPassMap[*pRenderPass] = new RENDER_PASS_NODE(localRPCI);
9192 dev_data->renderPassMap[*pRenderPass]->hasSelfDependency = has_self_dependency;
9193 dev_data->renderPassMap[*pRenderPass]->subpassToNode = subpass_to_node;
Tobin Ehlis87e0afc2016-03-22 13:50:21 -06009194#if MTMERGESOURCE
9195 // MTMTODO : Merge with code from above to eliminate duplication
9196 for (uint32_t i = 0; i < pCreateInfo->attachmentCount; ++i) {
9197 VkAttachmentDescription desc = pCreateInfo->pAttachments[i];
9198 MT_PASS_ATTACHMENT_INFO pass_info;
9199 pass_info.load_op = desc.loadOp;
9200 pass_info.store_op = desc.storeOp;
9201 pass_info.attachment = i;
9202 dev_data->renderPassMap[*pRenderPass]->attachments.push_back(pass_info);
9203 }
9204 // TODO: Maybe fill list and then copy instead of locking
9205 std::unordered_map<uint32_t, bool> &attachment_first_read = dev_data->renderPassMap[*pRenderPass]->attachment_first_read;
9206 std::unordered_map<uint32_t, VkImageLayout> &attachment_first_layout =
9207 dev_data->renderPassMap[*pRenderPass]->attachment_first_layout;
9208 for (uint32_t i = 0; i < pCreateInfo->subpassCount; ++i) {
9209 const VkSubpassDescription &subpass = pCreateInfo->pSubpasses[i];
Michael Lentineb5d4c162016-04-06 13:15:46 -05009210 if (subpass.pipelineBindPoint != VK_PIPELINE_BIND_POINT_GRAPHICS) {
9211 skip_call |= log_msg(dev_data->report_data, VK_DEBUG_REPORT_ERROR_BIT_EXT, (VkDebugReportObjectTypeEXT)0, 0,
9212 __LINE__, DRAWSTATE_INVALID_RENDERPASS, "DS",
9213 "Pipeline bind point for subpass %d must be VK_PIPELINE_BIND_POINT_GRAPHICS.", i);
9214 }
Michael Lentine0d615f02016-04-05 11:38:12 -05009215 for (uint32_t j = 0; j < subpass.preserveAttachmentCount; ++j) {
9216 uint32_t attachment = subpass.pPreserveAttachments[j];
9217 if (attachment >= pCreateInfo->attachmentCount) {
9218 skip_call |= log_msg(dev_data->report_data, VK_DEBUG_REPORT_ERROR_BIT_EXT, (VkDebugReportObjectTypeEXT)0, 0,
9219 __LINE__, DRAWSTATE_INVALID_RENDERPASS, "DS",
9220 "Preserve attachment %d cannot be greater than the total number of attachments %d.",
9221 attachment, pCreateInfo->attachmentCount);
9222 }
9223 }
Tobin Ehlis87e0afc2016-03-22 13:50:21 -06009224 for (uint32_t j = 0; j < subpass.colorAttachmentCount; ++j) {
Michael Lentine0d615f02016-04-05 11:38:12 -05009225 uint32_t attachment;
9226 if (subpass.pResolveAttachments) {
9227 attachment = subpass.pResolveAttachments[j].attachment;
9228 if (attachment >= pCreateInfo->attachmentCount && attachment != VK_ATTACHMENT_UNUSED) {
9229 skip_call |= log_msg(dev_data->report_data, VK_DEBUG_REPORT_ERROR_BIT_EXT, (VkDebugReportObjectTypeEXT)0, 0,
9230 __LINE__, DRAWSTATE_INVALID_RENDERPASS, "DS",
9231 "Color attachment %d cannot be greater than the total number of attachments %d.",
9232 attachment, pCreateInfo->attachmentCount);
9233 continue;
9234 }
9235 }
9236 attachment = subpass.pColorAttachments[j].attachment;
Michael Lentine24991fb2016-03-31 14:45:20 -05009237 if (attachment >= pCreateInfo->attachmentCount) {
9238 skip_call |= log_msg(dev_data->report_data, VK_DEBUG_REPORT_ERROR_BIT_EXT, (VkDebugReportObjectTypeEXT)0, 0,
9239 __LINE__, DRAWSTATE_INVALID_RENDERPASS, "DS",
9240 "Color attachment %d cannot be greater than the total number of attachments %d.",
9241 attachment, pCreateInfo->attachmentCount);
9242 continue;
9243 }
Tobin Ehlis87e0afc2016-03-22 13:50:21 -06009244 if (attachment_first_read.count(attachment))
9245 continue;
9246 attachment_first_read.insert(std::make_pair(attachment, false));
9247 attachment_first_layout.insert(std::make_pair(attachment, subpass.pColorAttachments[j].layout));
9248 }
9249 if (subpass.pDepthStencilAttachment && subpass.pDepthStencilAttachment->attachment != VK_ATTACHMENT_UNUSED) {
9250 uint32_t attachment = subpass.pDepthStencilAttachment->attachment;
Michael Lentine24991fb2016-03-31 14:45:20 -05009251 if (attachment >= pCreateInfo->attachmentCount) {
9252 skip_call |= log_msg(dev_data->report_data, VK_DEBUG_REPORT_ERROR_BIT_EXT, (VkDebugReportObjectTypeEXT)0, 0,
9253 __LINE__, DRAWSTATE_INVALID_RENDERPASS, "DS",
9254 "Depth stencil attachment %d cannot be greater than the total number of attachments %d.",
9255 attachment, pCreateInfo->attachmentCount);
9256 continue;
9257 }
Tobin Ehlis87e0afc2016-03-22 13:50:21 -06009258 if (attachment_first_read.count(attachment))
9259 continue;
9260 attachment_first_read.insert(std::make_pair(attachment, false));
9261 attachment_first_layout.insert(std::make_pair(attachment, subpass.pDepthStencilAttachment->layout));
9262 }
Michael Lentinea5ce96d2016-03-31 13:46:44 -05009263 for (uint32_t j = 0; j < subpass.inputAttachmentCount; ++j) {
9264 uint32_t attachment = subpass.pInputAttachments[j].attachment;
Michael Lentine24991fb2016-03-31 14:45:20 -05009265 if (attachment >= pCreateInfo->attachmentCount) {
9266 skip_call |= log_msg(dev_data->report_data, VK_DEBUG_REPORT_ERROR_BIT_EXT, (VkDebugReportObjectTypeEXT)0, 0,
9267 __LINE__, DRAWSTATE_INVALID_RENDERPASS, "DS",
9268 "Input attachment %d cannot be greater than the total number of attachments %d.",
9269 attachment, pCreateInfo->attachmentCount);
9270 continue;
9271 }
Michael Lentinea5ce96d2016-03-31 13:46:44 -05009272 if (attachment_first_read.count(attachment))
9273 continue;
9274 attachment_first_read.insert(std::make_pair(attachment, true));
9275 attachment_first_layout.insert(std::make_pair(attachment, subpass.pInputAttachments[j].layout));
9276 }
Tobin Ehlis87e0afc2016-03-22 13:50:21 -06009277 }
9278#endif
Jeremy Hayesb9e99232016-04-13 16:20:24 -06009279 lock.unlock();
Tobin Ehlis5b5e7bc2016-03-09 16:12:48 -07009280 }
9281 return result;
9282}
9283// Free the renderpass shadow
9284static void deleteRenderPasses(layer_data *my_data) {
9285 if (my_data->renderPassMap.size() <= 0)
9286 return;
9287 for (auto ii = my_data->renderPassMap.begin(); ii != my_data->renderPassMap.end(); ++ii) {
9288 const VkRenderPassCreateInfo *pRenderPassInfo = (*ii).second->pCreateInfo;
9289 delete[] pRenderPassInfo->pAttachments;
9290 if (pRenderPassInfo->pSubpasses) {
9291 for (uint32_t i = 0; i < pRenderPassInfo->subpassCount; ++i) {
9292 // Attachements are all allocated in a block, so just need to
9293 // find the first non-null one to delete
9294 if (pRenderPassInfo->pSubpasses[i].pInputAttachments) {
9295 delete[] pRenderPassInfo->pSubpasses[i].pInputAttachments;
9296 } else if (pRenderPassInfo->pSubpasses[i].pColorAttachments) {
9297 delete[] pRenderPassInfo->pSubpasses[i].pColorAttachments;
9298 } else if (pRenderPassInfo->pSubpasses[i].pResolveAttachments) {
9299 delete[] pRenderPassInfo->pSubpasses[i].pResolveAttachments;
9300 } else if (pRenderPassInfo->pSubpasses[i].pPreserveAttachments) {
9301 delete[] pRenderPassInfo->pSubpasses[i].pPreserveAttachments;
9302 }
9303 }
9304 delete[] pRenderPassInfo->pSubpasses;
9305 }
9306 delete[] pRenderPassInfo->pDependencies;
9307 delete pRenderPassInfo;
9308 delete (*ii).second;
9309 }
9310 my_data->renderPassMap.clear();
9311}
9312
Dustin Gravese3319182016-04-05 09:41:17 -06009313static bool VerifyFramebufferAndRenderPassLayouts(VkCommandBuffer cmdBuffer, const VkRenderPassBeginInfo *pRenderPassBegin) {
9314 bool skip_call = false;
Tobin Ehlis5b5e7bc2016-03-09 16:12:48 -07009315 layer_data *dev_data = get_my_data_ptr(get_dispatch_key(cmdBuffer), layer_data_map);
9316 GLOBAL_CB_NODE *pCB = getCBNode(dev_data, cmdBuffer);
9317 const VkRenderPassCreateInfo *pRenderPassInfo = dev_data->renderPassMap[pRenderPassBegin->renderPass]->pCreateInfo;
9318 const VkFramebufferCreateInfo framebufferInfo = dev_data->frameBufferMap[pRenderPassBegin->framebuffer].createInfo;
9319 if (pRenderPassInfo->attachmentCount != framebufferInfo.attachmentCount) {
9320 skip_call |= log_msg(dev_data->report_data, VK_DEBUG_REPORT_ERROR_BIT_EXT, (VkDebugReportObjectTypeEXT)0, 0, __LINE__,
9321 DRAWSTATE_INVALID_RENDERPASS, "DS", "You cannot start a render pass using a framebuffer "
9322 "with a different number of attachments.");
9323 }
9324 for (uint32_t i = 0; i < pRenderPassInfo->attachmentCount; ++i) {
9325 const VkImageView &image_view = framebufferInfo.pAttachments[i];
9326 auto image_data = dev_data->imageViewMap.find(image_view);
9327 assert(image_data != dev_data->imageViewMap.end());
9328 const VkImage &image = image_data->second.image;
9329 const VkImageSubresourceRange &subRange = image_data->second.subresourceRange;
9330 IMAGE_CMD_BUF_LAYOUT_NODE newNode = {pRenderPassInfo->pAttachments[i].initialLayout,
9331 pRenderPassInfo->pAttachments[i].initialLayout};
9332 // TODO: Do not iterate over every possibility - consolidate where possible
9333 for (uint32_t j = 0; j < subRange.levelCount; j++) {
9334 uint32_t level = subRange.baseMipLevel + j;
9335 for (uint32_t k = 0; k < subRange.layerCount; k++) {
9336 uint32_t layer = subRange.baseArrayLayer + k;
9337 VkImageSubresource sub = {subRange.aspectMask, level, layer};
9338 IMAGE_CMD_BUF_LAYOUT_NODE node;
9339 if (!FindLayout(pCB, image, sub, node)) {
9340 SetLayout(pCB, image, sub, newNode);
9341 continue;
9342 }
9343 if (newNode.layout != node.layout) {
9344 skip_call |=
9345 log_msg(dev_data->report_data, VK_DEBUG_REPORT_ERROR_BIT_EXT, (VkDebugReportObjectTypeEXT)0, 0, __LINE__,
9346 DRAWSTATE_INVALID_RENDERPASS, "DS", "You cannot start a render pass using attachment %i "
9347 "where the "
Karl Schultzd6c18822016-03-25 09:10:34 -06009348 "initial layout is %s and the layout of the attachment at the "
Michael Lentine1c0f4ce2016-03-24 21:36:53 -05009349 "start of the render pass is %s. The layouts must match.",
9350 i, string_VkImageLayout(newNode.layout), string_VkImageLayout(node.layout));
Tobin Ehlis5b5e7bc2016-03-09 16:12:48 -07009351 }
9352 }
9353 }
9354 }
9355 return skip_call;
9356}
9357
Dustin Gravese3319182016-04-05 09:41:17 -06009358static void TransitionSubpassLayouts(VkCommandBuffer cmdBuffer, const VkRenderPassBeginInfo *pRenderPassBegin,
9359 const int subpass_index) {
Tobin Ehlis5b5e7bc2016-03-09 16:12:48 -07009360 layer_data *dev_data = get_my_data_ptr(get_dispatch_key(cmdBuffer), layer_data_map);
9361 GLOBAL_CB_NODE *pCB = getCBNode(dev_data, cmdBuffer);
9362 auto render_pass_data = dev_data->renderPassMap.find(pRenderPassBegin->renderPass);
9363 if (render_pass_data == dev_data->renderPassMap.end()) {
9364 return;
9365 }
9366 const VkRenderPassCreateInfo *pRenderPassInfo = render_pass_data->second->pCreateInfo;
9367 auto framebuffer_data = dev_data->frameBufferMap.find(pRenderPassBegin->framebuffer);
9368 if (framebuffer_data == dev_data->frameBufferMap.end()) {
9369 return;
9370 }
9371 const VkFramebufferCreateInfo framebufferInfo = framebuffer_data->second.createInfo;
9372 const VkSubpassDescription &subpass = pRenderPassInfo->pSubpasses[subpass_index];
9373 for (uint32_t j = 0; j < subpass.inputAttachmentCount; ++j) {
9374 const VkImageView &image_view = framebufferInfo.pAttachments[subpass.pInputAttachments[j].attachment];
9375 SetLayout(dev_data, pCB, image_view, subpass.pInputAttachments[j].layout);
9376 }
9377 for (uint32_t j = 0; j < subpass.colorAttachmentCount; ++j) {
9378 const VkImageView &image_view = framebufferInfo.pAttachments[subpass.pColorAttachments[j].attachment];
9379 SetLayout(dev_data, pCB, image_view, subpass.pColorAttachments[j].layout);
9380 }
9381 if ((subpass.pDepthStencilAttachment != NULL) && (subpass.pDepthStencilAttachment->attachment != VK_ATTACHMENT_UNUSED)) {
9382 const VkImageView &image_view = framebufferInfo.pAttachments[subpass.pDepthStencilAttachment->attachment];
9383 SetLayout(dev_data, pCB, image_view, subpass.pDepthStencilAttachment->layout);
9384 }
9385}
9386
Dustin Gravese3319182016-04-05 09:41:17 -06009387static bool validatePrimaryCommandBuffer(const layer_data *my_data, const GLOBAL_CB_NODE *pCB, const std::string &cmd_name) {
9388 bool skip_call = false;
Tobin Ehlis5b5e7bc2016-03-09 16:12:48 -07009389 if (pCB->createInfo.level != VK_COMMAND_BUFFER_LEVEL_PRIMARY) {
9390 skip_call |= log_msg(my_data->report_data, VK_DEBUG_REPORT_ERROR_BIT_EXT, (VkDebugReportObjectTypeEXT)0, 0, __LINE__,
9391 DRAWSTATE_INVALID_COMMAND_BUFFER, "DS", "Cannot execute command %s on a secondary command buffer.",
9392 cmd_name.c_str());
9393 }
9394 return skip_call;
9395}
9396
Dustin Gravese3319182016-04-05 09:41:17 -06009397static void TransitionFinalSubpassLayouts(VkCommandBuffer cmdBuffer, const VkRenderPassBeginInfo *pRenderPassBegin) {
Tobin Ehlis5b5e7bc2016-03-09 16:12:48 -07009398 layer_data *dev_data = get_my_data_ptr(get_dispatch_key(cmdBuffer), layer_data_map);
9399 GLOBAL_CB_NODE *pCB = getCBNode(dev_data, cmdBuffer);
9400 auto render_pass_data = dev_data->renderPassMap.find(pRenderPassBegin->renderPass);
9401 if (render_pass_data == dev_data->renderPassMap.end()) {
9402 return;
9403 }
9404 const VkRenderPassCreateInfo *pRenderPassInfo = render_pass_data->second->pCreateInfo;
9405 auto framebuffer_data = dev_data->frameBufferMap.find(pRenderPassBegin->framebuffer);
9406 if (framebuffer_data == dev_data->frameBufferMap.end()) {
9407 return;
9408 }
9409 const VkFramebufferCreateInfo framebufferInfo = framebuffer_data->second.createInfo;
9410 for (uint32_t i = 0; i < pRenderPassInfo->attachmentCount; ++i) {
9411 const VkImageView &image_view = framebufferInfo.pAttachments[i];
9412 SetLayout(dev_data, pCB, image_view, pRenderPassInfo->pAttachments[i].finalLayout);
9413 }
9414}
9415
Dustin Gravese3319182016-04-05 09:41:17 -06009416static bool VerifyRenderAreaBounds(const layer_data *my_data, const VkRenderPassBeginInfo *pRenderPassBegin) {
Michael Lentine885537d2016-03-24 20:48:59 -05009417 bool skip_call = false;
9418 const VkFramebufferCreateInfo *pFramebufferInfo = &my_data->frameBufferMap.at(pRenderPassBegin->framebuffer).createInfo;
9419 if (pRenderPassBegin->renderArea.offset.x < 0 ||
9420 (pRenderPassBegin->renderArea.offset.x + pRenderPassBegin->renderArea.extent.width) > pFramebufferInfo->width ||
9421 pRenderPassBegin->renderArea.offset.y < 0 ||
9422 (pRenderPassBegin->renderArea.offset.y + pRenderPassBegin->renderArea.extent.height) > pFramebufferInfo->height) {
9423 skip_call |= static_cast<bool>(log_msg(
9424 my_data->report_data, VK_DEBUG_REPORT_ERROR_BIT_EXT, (VkDebugReportObjectTypeEXT)0, 0, __LINE__,
9425 DRAWSTATE_INVALID_RENDER_AREA, "CORE",
9426 "Cannot execute a render pass with renderArea not within the bound of the "
9427 "framebuffer. RenderArea: x %d, y %d, width %d, height %d. Framebuffer: width %d, "
9428 "height %d.",
9429 pRenderPassBegin->renderArea.offset.x, pRenderPassBegin->renderArea.offset.y, pRenderPassBegin->renderArea.extent.width,
9430 pRenderPassBegin->renderArea.extent.height, pFramebufferInfo->width, pFramebufferInfo->height));
9431 }
9432 return skip_call;
9433}
9434
Tobin Ehlis5b5e7bc2016-03-09 16:12:48 -07009435VK_LAYER_EXPORT VKAPI_ATTR void VKAPI_CALL
9436vkCmdBeginRenderPass(VkCommandBuffer commandBuffer, const VkRenderPassBeginInfo *pRenderPassBegin, VkSubpassContents contents) {
Dustin Gravese3319182016-04-05 09:41:17 -06009437 bool skipCall = false;
Tobin Ehlis5b5e7bc2016-03-09 16:12:48 -07009438 layer_data *dev_data = get_my_data_ptr(get_dispatch_key(commandBuffer), layer_data_map);
Jeremy Hayesb9e99232016-04-13 16:20:24 -06009439 std::unique_lock<std::mutex> lock(global_lock);
Tobin Ehlis5b5e7bc2016-03-09 16:12:48 -07009440 GLOBAL_CB_NODE *pCB = getCBNode(dev_data, commandBuffer);
9441 if (pCB) {
9442 if (pRenderPassBegin && pRenderPassBegin->renderPass) {
Tobin Ehlis87e0afc2016-03-22 13:50:21 -06009443#if MTMERGE
9444 auto pass_data = dev_data->renderPassMap.find(pRenderPassBegin->renderPass);
9445 if (pass_data != dev_data->renderPassMap.end()) {
9446 RENDER_PASS_NODE* pRPNode = pass_data->second;
9447 pRPNode->fb = pRenderPassBegin->framebuffer;
Tobin Ehlis72d66f02016-03-21 14:14:44 -06009448 auto cb_data = dev_data->commandBufferMap.find(commandBuffer);
Tobin Ehlis87e0afc2016-03-22 13:50:21 -06009449 for (size_t i = 0; i < pRPNode->attachments.size(); ++i) {
9450 MT_FB_ATTACHMENT_INFO &fb_info = dev_data->frameBufferMap[pRPNode->fb].attachments[i];
9451 if (pRPNode->attachments[i].load_op == VK_ATTACHMENT_LOAD_OP_CLEAR) {
Tobin Ehlis72d66f02016-03-21 14:14:44 -06009452 if (cb_data != dev_data->commandBufferMap.end()) {
Dustin Gravese3319182016-04-05 09:41:17 -06009453 std::function<bool()> function = [=]() {
Tobin Ehlis5b5e7bc2016-03-09 16:12:48 -07009454 set_memory_valid(dev_data, fb_info.mem, true, fb_info.image);
Dustin Gravese3319182016-04-05 09:41:17 -06009455 return false;
Tobin Ehlis5b5e7bc2016-03-09 16:12:48 -07009456 };
Tobin Ehlis72d66f02016-03-21 14:14:44 -06009457 cb_data->second->validate_functions.push_back(function);
Tobin Ehlis5b5e7bc2016-03-09 16:12:48 -07009458 }
Tobin Ehlis87e0afc2016-03-22 13:50:21 -06009459 VkImageLayout &attachment_layout = pRPNode->attachment_first_layout[pRPNode->attachments[i].attachment];
Tobin Ehlis5b5e7bc2016-03-09 16:12:48 -07009460 if (attachment_layout == VK_IMAGE_LAYOUT_DEPTH_STENCIL_READ_ONLY_OPTIMAL ||
9461 attachment_layout == VK_IMAGE_LAYOUT_SHADER_READ_ONLY_OPTIMAL) {
9462 skipCall |=
9463 log_msg(dev_data->report_data, VK_DEBUG_REPORT_ERROR_BIT_EXT,
9464 VK_DEBUG_REPORT_OBJECT_TYPE_RENDER_PASS_EXT, (uint64_t)(pRenderPassBegin->renderPass), __LINE__,
9465 MEMTRACK_INVALID_LAYOUT, "MEM", "Cannot clear attachment %d with invalid first layout %d.",
Tobin Ehlis87e0afc2016-03-22 13:50:21 -06009466 pRPNode->attachments[i].attachment, attachment_layout);
Tobin Ehlis5b5e7bc2016-03-09 16:12:48 -07009467 }
Tobin Ehlis87e0afc2016-03-22 13:50:21 -06009468 } else if (pRPNode->attachments[i].load_op == VK_ATTACHMENT_LOAD_OP_DONT_CARE) {
Tobin Ehlis72d66f02016-03-21 14:14:44 -06009469 if (cb_data != dev_data->commandBufferMap.end()) {
Dustin Gravese3319182016-04-05 09:41:17 -06009470 std::function<bool()> function = [=]() {
Tobin Ehlis5b5e7bc2016-03-09 16:12:48 -07009471 set_memory_valid(dev_data, fb_info.mem, false, fb_info.image);
Dustin Gravese3319182016-04-05 09:41:17 -06009472 return false;
Tobin Ehlis5b5e7bc2016-03-09 16:12:48 -07009473 };
Tobin Ehlis72d66f02016-03-21 14:14:44 -06009474 cb_data->second->validate_functions.push_back(function);
Tobin Ehlis5b5e7bc2016-03-09 16:12:48 -07009475 }
Tobin Ehlis87e0afc2016-03-22 13:50:21 -06009476 } else if (pRPNode->attachments[i].load_op == VK_ATTACHMENT_LOAD_OP_LOAD) {
Tobin Ehlis72d66f02016-03-21 14:14:44 -06009477 if (cb_data != dev_data->commandBufferMap.end()) {
Dustin Gravese3319182016-04-05 09:41:17 -06009478 std::function<bool()> function = [=]() {
Tobin Ehlis5b5e7bc2016-03-09 16:12:48 -07009479 return validate_memory_is_valid(dev_data, fb_info.mem, "vkCmdBeginRenderPass()", fb_info.image);
9480 };
Tobin Ehlis72d66f02016-03-21 14:14:44 -06009481 cb_data->second->validate_functions.push_back(function);
Tobin Ehlis5b5e7bc2016-03-09 16:12:48 -07009482 }
9483 }
Tobin Ehlis87e0afc2016-03-22 13:50:21 -06009484 if (pRPNode->attachment_first_read[pRPNode->attachments[i].attachment]) {
Tobin Ehlis72d66f02016-03-21 14:14:44 -06009485 if (cb_data != dev_data->commandBufferMap.end()) {
Dustin Gravese3319182016-04-05 09:41:17 -06009486 std::function<bool()> function = [=]() {
Tobin Ehlis5b5e7bc2016-03-09 16:12:48 -07009487 return validate_memory_is_valid(dev_data, fb_info.mem, "vkCmdBeginRenderPass()", fb_info.image);
9488 };
Tobin Ehlis72d66f02016-03-21 14:14:44 -06009489 cb_data->second->validate_functions.push_back(function);
Tobin Ehlis5b5e7bc2016-03-09 16:12:48 -07009490 }
9491 }
9492 }
Tobin Ehlis5b5e7bc2016-03-09 16:12:48 -07009493 }
9494#endif
Dustin Gravese3319182016-04-05 09:41:17 -06009495 skipCall |= VerifyRenderAreaBounds(dev_data, pRenderPassBegin);
Tobin Ehlis5b5e7bc2016-03-09 16:12:48 -07009496 skipCall |= VerifyFramebufferAndRenderPassLayouts(commandBuffer, pRenderPassBegin);
9497 auto render_pass_data = dev_data->renderPassMap.find(pRenderPassBegin->renderPass);
9498 if (render_pass_data != dev_data->renderPassMap.end()) {
9499 skipCall |= ValidateDependencies(dev_data, pRenderPassBegin, render_pass_data->second->subpassToNode);
9500 }
9501 skipCall |= insideRenderPass(dev_data, pCB, "vkCmdBeginRenderPass");
9502 skipCall |= validatePrimaryCommandBuffer(dev_data, pCB, "vkCmdBeginRenderPass");
9503 skipCall |= addCmd(dev_data, pCB, CMD_BEGINRENDERPASS, "vkCmdBeginRenderPass()");
9504 pCB->activeRenderPass = pRenderPassBegin->renderPass;
9505 // This is a shallow copy as that is all that is needed for now
9506 pCB->activeRenderPassBeginInfo = *pRenderPassBegin;
9507 pCB->activeSubpass = 0;
9508 pCB->activeSubpassContents = contents;
Mark Lobodzinski93c396d2016-04-12 10:41:59 -06009509 pCB->framebuffers.insert(pRenderPassBegin->framebuffer);
Tobin Ehlis5b5e7bc2016-03-09 16:12:48 -07009510 // Connect this framebuffer to this cmdBuffer
Mark Lobodzinski93c396d2016-04-12 10:41:59 -06009511 dev_data->frameBufferMap[pRenderPassBegin->framebuffer].referencingCmdBuffers.insert(pCB->commandBuffer);
Tobin Ehlis5b5e7bc2016-03-09 16:12:48 -07009512 } else {
9513 skipCall |=
9514 log_msg(dev_data->report_data, VK_DEBUG_REPORT_ERROR_BIT_EXT, (VkDebugReportObjectTypeEXT)0, 0, __LINE__,
9515 DRAWSTATE_INVALID_RENDERPASS, "DS", "You cannot use a NULL RenderPass object in vkCmdBeginRenderPass()");
9516 }
9517 }
Jeremy Hayesb9e99232016-04-13 16:20:24 -06009518 lock.unlock();
Dustin Gravese3319182016-04-05 09:41:17 -06009519 if (!skipCall) {
Tobin Ehlis5b5e7bc2016-03-09 16:12:48 -07009520 dev_data->device_dispatch_table->CmdBeginRenderPass(commandBuffer, pRenderPassBegin, contents);
Tobin Ehlis5b5e7bc2016-03-09 16:12:48 -07009521 }
9522}
9523
9524VK_LAYER_EXPORT VKAPI_ATTR void VKAPI_CALL vkCmdNextSubpass(VkCommandBuffer commandBuffer, VkSubpassContents contents) {
Dustin Gravese3319182016-04-05 09:41:17 -06009525 bool skipCall = false;
Tobin Ehlis5b5e7bc2016-03-09 16:12:48 -07009526 layer_data *dev_data = get_my_data_ptr(get_dispatch_key(commandBuffer), layer_data_map);
Jeremy Hayesb9e99232016-04-13 16:20:24 -06009527 std::unique_lock<std::mutex> lock(global_lock);
Tobin Ehlis5b5e7bc2016-03-09 16:12:48 -07009528 GLOBAL_CB_NODE *pCB = getCBNode(dev_data, commandBuffer);
Tobin Ehlis5b5e7bc2016-03-09 16:12:48 -07009529 if (pCB) {
9530 skipCall |= validatePrimaryCommandBuffer(dev_data, pCB, "vkCmdNextSubpass");
9531 skipCall |= addCmd(dev_data, pCB, CMD_NEXTSUBPASS, "vkCmdNextSubpass()");
9532 pCB->activeSubpass++;
9533 pCB->activeSubpassContents = contents;
9534 TransitionSubpassLayouts(commandBuffer, &pCB->activeRenderPassBeginInfo, pCB->activeSubpass);
Tobin Ehlis72d66f02016-03-21 14:14:44 -06009535 if (pCB->lastBound[VK_PIPELINE_BIND_POINT_GRAPHICS].pipeline) {
9536 skipCall |= validatePipelineState(dev_data, pCB, VK_PIPELINE_BIND_POINT_GRAPHICS,
9537 pCB->lastBound[VK_PIPELINE_BIND_POINT_GRAPHICS].pipeline);
Tobin Ehlis5b5e7bc2016-03-09 16:12:48 -07009538 }
9539 skipCall |= outsideRenderPass(dev_data, pCB, "vkCmdNextSubpass");
9540 }
Jeremy Hayesb9e99232016-04-13 16:20:24 -06009541 lock.unlock();
Dustin Gravese3319182016-04-05 09:41:17 -06009542 if (!skipCall)
Tobin Ehlis5b5e7bc2016-03-09 16:12:48 -07009543 dev_data->device_dispatch_table->CmdNextSubpass(commandBuffer, contents);
9544}
9545
9546VK_LAYER_EXPORT VKAPI_ATTR void VKAPI_CALL vkCmdEndRenderPass(VkCommandBuffer commandBuffer) {
Dustin Gravese3319182016-04-05 09:41:17 -06009547 bool skipCall = false;
Tobin Ehlis5b5e7bc2016-03-09 16:12:48 -07009548 layer_data *dev_data = get_my_data_ptr(get_dispatch_key(commandBuffer), layer_data_map);
Jeremy Hayesb9e99232016-04-13 16:20:24 -06009549 std::unique_lock<std::mutex> lock(global_lock);
Mark Lobodzinski03b71512016-03-23 14:33:02 -06009550#if MTMERGESOURCE
Tobin Ehlis72d66f02016-03-21 14:14:44 -06009551 auto cb_data = dev_data->commandBufferMap.find(commandBuffer);
9552 if (cb_data != dev_data->commandBufferMap.end()) {
Tobin Ehlis87e0afc2016-03-22 13:50:21 -06009553 auto pass_data = dev_data->renderPassMap.find(cb_data->second->activeRenderPass);
9554 if (pass_data != dev_data->renderPassMap.end()) {
9555 RENDER_PASS_NODE* pRPNode = pass_data->second;
9556 for (size_t i = 0; i < pRPNode->attachments.size(); ++i) {
9557 MT_FB_ATTACHMENT_INFO &fb_info = dev_data->frameBufferMap[pRPNode->fb].attachments[i];
9558 if (pRPNode->attachments[i].store_op == VK_ATTACHMENT_STORE_OP_STORE) {
Tobin Ehlis72d66f02016-03-21 14:14:44 -06009559 if (cb_data != dev_data->commandBufferMap.end()) {
Dustin Gravese3319182016-04-05 09:41:17 -06009560 std::function<bool()> function = [=]() {
Tobin Ehlis5b5e7bc2016-03-09 16:12:48 -07009561 set_memory_valid(dev_data, fb_info.mem, true, fb_info.image);
Dustin Gravese3319182016-04-05 09:41:17 -06009562 return false;
Tobin Ehlis5b5e7bc2016-03-09 16:12:48 -07009563 };
Tobin Ehlis72d66f02016-03-21 14:14:44 -06009564 cb_data->second->validate_functions.push_back(function);
Tobin Ehlis5b5e7bc2016-03-09 16:12:48 -07009565 }
Tobin Ehlis87e0afc2016-03-22 13:50:21 -06009566 } else if (pRPNode->attachments[i].store_op == VK_ATTACHMENT_STORE_OP_DONT_CARE) {
Tobin Ehlis72d66f02016-03-21 14:14:44 -06009567 if (cb_data != dev_data->commandBufferMap.end()) {
Dustin Gravese3319182016-04-05 09:41:17 -06009568 std::function<bool()> function = [=]() {
Tobin Ehlis5b5e7bc2016-03-09 16:12:48 -07009569 set_memory_valid(dev_data, fb_info.mem, false, fb_info.image);
Dustin Gravese3319182016-04-05 09:41:17 -06009570 return false;
Tobin Ehlis5b5e7bc2016-03-09 16:12:48 -07009571 };
Tobin Ehlis72d66f02016-03-21 14:14:44 -06009572 cb_data->second->validate_functions.push_back(function);
Tobin Ehlis5b5e7bc2016-03-09 16:12:48 -07009573 }
9574 }
9575 }
9576 }
9577 }
9578#endif
9579 GLOBAL_CB_NODE *pCB = getCBNode(dev_data, commandBuffer);
Tobin Ehlis5b5e7bc2016-03-09 16:12:48 -07009580 if (pCB) {
9581 skipCall |= outsideRenderPass(dev_data, pCB, "vkCmdEndRenderpass");
9582 skipCall |= validatePrimaryCommandBuffer(dev_data, pCB, "vkCmdEndRenderPass");
9583 skipCall |= addCmd(dev_data, pCB, CMD_ENDRENDERPASS, "vkCmdEndRenderPass()");
9584 TransitionFinalSubpassLayouts(commandBuffer, &pCB->activeRenderPassBeginInfo);
9585 pCB->activeRenderPass = 0;
9586 pCB->activeSubpass = 0;
9587 }
Jeremy Hayesb9e99232016-04-13 16:20:24 -06009588 lock.unlock();
Dustin Gravese3319182016-04-05 09:41:17 -06009589 if (!skipCall)
Tobin Ehlis5b5e7bc2016-03-09 16:12:48 -07009590 dev_data->device_dispatch_table->CmdEndRenderPass(commandBuffer);
9591}
9592
Dustin Gravese3319182016-04-05 09:41:17 -06009593static bool logInvalidAttachmentMessage(layer_data *dev_data, VkCommandBuffer secondaryBuffer, VkRenderPass secondaryPass,
9594 VkRenderPass primaryPass, uint32_t primaryAttach, uint32_t secondaryAttach,
9595 const char *msg) {
Tobin Ehlis5b5e7bc2016-03-09 16:12:48 -07009596 return log_msg(dev_data->report_data, VK_DEBUG_REPORT_ERROR_BIT_EXT, (VkDebugReportObjectTypeEXT)0, 0, __LINE__,
9597 DRAWSTATE_INVALID_SECONDARY_COMMAND_BUFFER, "DS",
9598 "vkCmdExecuteCommands() called w/ invalid Cmd Buffer %p which has a render pass %" PRIx64
9599 " that is not compatible with the current render pass %" PRIx64 "."
Eric Engestrom81264dd2016-04-02 22:06:13 +01009600 "Attachment %" PRIu32 " is not compatible with %" PRIu32 ". %s",
Tobin Ehlis5b5e7bc2016-03-09 16:12:48 -07009601 (void *)secondaryBuffer, (uint64_t)(secondaryPass), (uint64_t)(primaryPass), primaryAttach, secondaryAttach,
9602 msg);
9603}
9604
Dustin Gravese3319182016-04-05 09:41:17 -06009605static bool validateAttachmentCompatibility(layer_data *dev_data, VkCommandBuffer primaryBuffer, VkRenderPass primaryPass,
9606 uint32_t primaryAttach, VkCommandBuffer secondaryBuffer, VkRenderPass secondaryPass,
9607 uint32_t secondaryAttach, bool is_multi) {
Tobin Ehlis5b5e7bc2016-03-09 16:12:48 -07009608 bool skip_call = false;
9609 auto primary_data = dev_data->renderPassMap.find(primaryPass);
9610 auto secondary_data = dev_data->renderPassMap.find(secondaryPass);
9611 if (primary_data->second->pCreateInfo->attachmentCount <= primaryAttach) {
9612 primaryAttach = VK_ATTACHMENT_UNUSED;
9613 }
9614 if (secondary_data->second->pCreateInfo->attachmentCount <= secondaryAttach) {
9615 secondaryAttach = VK_ATTACHMENT_UNUSED;
9616 }
9617 if (primaryAttach == VK_ATTACHMENT_UNUSED && secondaryAttach == VK_ATTACHMENT_UNUSED) {
9618 return skip_call;
9619 }
9620 if (primaryAttach == VK_ATTACHMENT_UNUSED) {
9621 skip_call |= logInvalidAttachmentMessage(dev_data, secondaryBuffer, secondaryPass, primaryPass, primaryAttach,
9622 secondaryAttach, "The first is unused while the second is not.");
9623 return skip_call;
9624 }
9625 if (secondaryAttach == VK_ATTACHMENT_UNUSED) {
9626 skip_call |= logInvalidAttachmentMessage(dev_data, secondaryBuffer, secondaryPass, primaryPass, primaryAttach,
9627 secondaryAttach, "The second is unused while the first is not.");
9628 return skip_call;
9629 }
9630 if (primary_data->second->pCreateInfo->pAttachments[primaryAttach].format !=
9631 secondary_data->second->pCreateInfo->pAttachments[secondaryAttach].format) {
9632 skip_call |= logInvalidAttachmentMessage(dev_data, secondaryBuffer, secondaryPass, primaryPass, primaryAttach,
9633 secondaryAttach, "They have different formats.");
9634 }
9635 if (primary_data->second->pCreateInfo->pAttachments[primaryAttach].samples !=
9636 secondary_data->second->pCreateInfo->pAttachments[secondaryAttach].samples) {
9637 skip_call |= logInvalidAttachmentMessage(dev_data, secondaryBuffer, secondaryPass, primaryPass, primaryAttach,
9638 secondaryAttach, "They have different samples.");
9639 }
9640 if (is_multi &&
9641 primary_data->second->pCreateInfo->pAttachments[primaryAttach].flags !=
9642 secondary_data->second->pCreateInfo->pAttachments[secondaryAttach].flags) {
9643 skip_call |= logInvalidAttachmentMessage(dev_data, secondaryBuffer, secondaryPass, primaryPass, primaryAttach,
9644 secondaryAttach, "They have different flags.");
9645 }
9646 return skip_call;
9647}
9648
Dustin Gravese3319182016-04-05 09:41:17 -06009649static bool validateSubpassCompatibility(layer_data *dev_data, VkCommandBuffer primaryBuffer, VkRenderPass primaryPass,
9650 VkCommandBuffer secondaryBuffer, VkRenderPass secondaryPass, const int subpass,
9651 bool is_multi) {
Tobin Ehlis5b5e7bc2016-03-09 16:12:48 -07009652 bool skip_call = false;
9653 auto primary_data = dev_data->renderPassMap.find(primaryPass);
9654 auto secondary_data = dev_data->renderPassMap.find(secondaryPass);
9655 const VkSubpassDescription &primary_desc = primary_data->second->pCreateInfo->pSubpasses[subpass];
9656 const VkSubpassDescription &secondary_desc = secondary_data->second->pCreateInfo->pSubpasses[subpass];
9657 uint32_t maxInputAttachmentCount = std::max(primary_desc.inputAttachmentCount, secondary_desc.inputAttachmentCount);
9658 for (uint32_t i = 0; i < maxInputAttachmentCount; ++i) {
9659 uint32_t primary_input_attach = VK_ATTACHMENT_UNUSED, secondary_input_attach = VK_ATTACHMENT_UNUSED;
9660 if (i < primary_desc.inputAttachmentCount) {
9661 primary_input_attach = primary_desc.pInputAttachments[i].attachment;
9662 }
9663 if (i < secondary_desc.inputAttachmentCount) {
9664 secondary_input_attach = secondary_desc.pInputAttachments[i].attachment;
9665 }
9666 skip_call |= validateAttachmentCompatibility(dev_data, primaryBuffer, primaryPass, primary_input_attach, secondaryBuffer,
9667 secondaryPass, secondary_input_attach, is_multi);
9668 }
9669 uint32_t maxColorAttachmentCount = std::max(primary_desc.colorAttachmentCount, secondary_desc.colorAttachmentCount);
9670 for (uint32_t i = 0; i < maxColorAttachmentCount; ++i) {
9671 uint32_t primary_color_attach = VK_ATTACHMENT_UNUSED, secondary_color_attach = VK_ATTACHMENT_UNUSED;
9672 if (i < primary_desc.colorAttachmentCount) {
9673 primary_color_attach = primary_desc.pColorAttachments[i].attachment;
9674 }
9675 if (i < secondary_desc.colorAttachmentCount) {
9676 secondary_color_attach = secondary_desc.pColorAttachments[i].attachment;
9677 }
9678 skip_call |= validateAttachmentCompatibility(dev_data, primaryBuffer, primaryPass, primary_color_attach, secondaryBuffer,
9679 secondaryPass, secondary_color_attach, is_multi);
9680 uint32_t primary_resolve_attach = VK_ATTACHMENT_UNUSED, secondary_resolve_attach = VK_ATTACHMENT_UNUSED;
9681 if (i < primary_desc.colorAttachmentCount && primary_desc.pResolveAttachments) {
9682 primary_resolve_attach = primary_desc.pResolveAttachments[i].attachment;
9683 }
9684 if (i < secondary_desc.colorAttachmentCount && secondary_desc.pResolveAttachments) {
9685 secondary_resolve_attach = secondary_desc.pResolveAttachments[i].attachment;
9686 }
9687 skip_call |= validateAttachmentCompatibility(dev_data, primaryBuffer, primaryPass, primary_resolve_attach, secondaryBuffer,
9688 secondaryPass, secondary_resolve_attach, is_multi);
9689 }
9690 uint32_t primary_depthstencil_attach = VK_ATTACHMENT_UNUSED, secondary_depthstencil_attach = VK_ATTACHMENT_UNUSED;
9691 if (primary_desc.pDepthStencilAttachment) {
9692 primary_depthstencil_attach = primary_desc.pDepthStencilAttachment[0].attachment;
9693 }
9694 if (secondary_desc.pDepthStencilAttachment) {
9695 secondary_depthstencil_attach = secondary_desc.pDepthStencilAttachment[0].attachment;
9696 }
9697 skip_call |= validateAttachmentCompatibility(dev_data, primaryBuffer, primaryPass, primary_depthstencil_attach, secondaryBuffer,
9698 secondaryPass, secondary_depthstencil_attach, is_multi);
9699 return skip_call;
9700}
9701
Dustin Gravese3319182016-04-05 09:41:17 -06009702static bool validateRenderPassCompatibility(layer_data *dev_data, VkCommandBuffer primaryBuffer, VkRenderPass primaryPass,
9703 VkCommandBuffer secondaryBuffer, VkRenderPass secondaryPass) {
Tobin Ehlis5b5e7bc2016-03-09 16:12:48 -07009704 bool skip_call = false;
9705 // Early exit if renderPass objects are identical (and therefore compatible)
9706 if (primaryPass == secondaryPass)
9707 return skip_call;
9708 auto primary_data = dev_data->renderPassMap.find(primaryPass);
9709 auto secondary_data = dev_data->renderPassMap.find(secondaryPass);
9710 if (primary_data == dev_data->renderPassMap.end() || primary_data->second == nullptr) {
9711 skip_call |=
9712 log_msg(dev_data->report_data, VK_DEBUG_REPORT_ERROR_BIT_EXT, (VkDebugReportObjectTypeEXT)0, 0, __LINE__,
9713 DRAWSTATE_INVALID_SECONDARY_COMMAND_BUFFER, "DS",
9714 "vkCmdExecuteCommands() called w/ invalid current Cmd Buffer %p which has invalid render pass %" PRIx64 ".",
9715 (void *)primaryBuffer, (uint64_t)(primaryPass));
9716 return skip_call;
9717 }
9718 if (secondary_data == dev_data->renderPassMap.end() || secondary_data->second == nullptr) {
9719 skip_call |=
9720 log_msg(dev_data->report_data, VK_DEBUG_REPORT_ERROR_BIT_EXT, (VkDebugReportObjectTypeEXT)0, 0, __LINE__,
9721 DRAWSTATE_INVALID_SECONDARY_COMMAND_BUFFER, "DS",
9722 "vkCmdExecuteCommands() called w/ invalid secondary Cmd Buffer %p which has invalid render pass %" PRIx64 ".",
9723 (void *)secondaryBuffer, (uint64_t)(secondaryPass));
9724 return skip_call;
9725 }
9726 if (primary_data->second->pCreateInfo->subpassCount != secondary_data->second->pCreateInfo->subpassCount) {
9727 skip_call |= log_msg(dev_data->report_data, VK_DEBUG_REPORT_ERROR_BIT_EXT, (VkDebugReportObjectTypeEXT)0, 0, __LINE__,
9728 DRAWSTATE_INVALID_SECONDARY_COMMAND_BUFFER, "DS",
9729 "vkCmdExecuteCommands() called w/ invalid Cmd Buffer %p which has a render pass %" PRIx64
9730 " that is not compatible with the current render pass %" PRIx64 "."
9731 "They have a different number of subpasses.",
9732 (void *)secondaryBuffer, (uint64_t)(secondaryPass), (uint64_t)(primaryPass));
9733 return skip_call;
9734 }
9735 bool is_multi = primary_data->second->pCreateInfo->subpassCount > 1;
9736 for (uint32_t i = 0; i < primary_data->second->pCreateInfo->subpassCount; ++i) {
9737 skip_call |=
9738 validateSubpassCompatibility(dev_data, primaryBuffer, primaryPass, secondaryBuffer, secondaryPass, i, is_multi);
9739 }
9740 return skip_call;
9741}
9742
Dustin Gravese3319182016-04-05 09:41:17 -06009743static bool validateFramebuffer(layer_data *dev_data, VkCommandBuffer primaryBuffer, const GLOBAL_CB_NODE *pCB,
9744 VkCommandBuffer secondaryBuffer, const GLOBAL_CB_NODE *pSubCB) {
Tobin Ehlis5b5e7bc2016-03-09 16:12:48 -07009745 bool skip_call = false;
9746 if (!pSubCB->beginInfo.pInheritanceInfo) {
9747 return skip_call;
9748 }
Mark Lobodzinski93c396d2016-04-12 10:41:59 -06009749 VkFramebuffer primary_fb = dev_data->renderPassMap[pCB->activeRenderPass]->fb;
Tobin Ehlis5b5e7bc2016-03-09 16:12:48 -07009750 VkFramebuffer secondary_fb = pSubCB->beginInfo.pInheritanceInfo->framebuffer;
9751 if (secondary_fb != VK_NULL_HANDLE) {
9752 if (primary_fb != secondary_fb) {
9753 skip_call |= log_msg(dev_data->report_data, VK_DEBUG_REPORT_ERROR_BIT_EXT, (VkDebugReportObjectTypeEXT)0, 0, __LINE__,
9754 DRAWSTATE_INVALID_SECONDARY_COMMAND_BUFFER, "DS",
9755 "vkCmdExecuteCommands() called w/ invalid Cmd Buffer %p which has a framebuffer %" PRIx64
9756 " that is not compatible with the current framebuffer %" PRIx64 ".",
9757 (void *)secondaryBuffer, (uint64_t)(secondary_fb), (uint64_t)(primary_fb));
9758 }
9759 auto fb_data = dev_data->frameBufferMap.find(secondary_fb);
9760 if (fb_data == dev_data->frameBufferMap.end()) {
9761 skip_call |=
9762 log_msg(dev_data->report_data, VK_DEBUG_REPORT_ERROR_BIT_EXT, (VkDebugReportObjectTypeEXT)0, 0, __LINE__,
9763 DRAWSTATE_INVALID_SECONDARY_COMMAND_BUFFER, "DS", "vkCmdExecuteCommands() called w/ invalid Cmd Buffer %p "
9764 "which has invalid framebuffer %" PRIx64 ".",
9765 (void *)secondaryBuffer, (uint64_t)(secondary_fb));
9766 return skip_call;
9767 }
9768 skip_call |= validateRenderPassCompatibility(dev_data, secondaryBuffer, fb_data->second.createInfo.renderPass,
9769 secondaryBuffer, pSubCB->beginInfo.pInheritanceInfo->renderPass);
9770 }
9771 return skip_call;
9772}
9773
Dustin Gravese3319182016-04-05 09:41:17 -06009774static bool validateSecondaryCommandBufferState(layer_data *dev_data, GLOBAL_CB_NODE *pCB, GLOBAL_CB_NODE *pSubCB) {
Tobin Ehlis5b5e7bc2016-03-09 16:12:48 -07009775 bool skipCall = false;
9776 unordered_set<int> activeTypes;
9777 for (auto queryObject : pCB->activeQueries) {
9778 auto queryPoolData = dev_data->queryPoolMap.find(queryObject.pool);
9779 if (queryPoolData != dev_data->queryPoolMap.end()) {
9780 if (queryPoolData->second.createInfo.queryType == VK_QUERY_TYPE_PIPELINE_STATISTICS &&
9781 pSubCB->beginInfo.pInheritanceInfo) {
9782 VkQueryPipelineStatisticFlags cmdBufStatistics = pSubCB->beginInfo.pInheritanceInfo->pipelineStatistics;
9783 if ((cmdBufStatistics & queryPoolData->second.createInfo.pipelineStatistics) != cmdBufStatistics) {
9784 skipCall |= log_msg(
9785 dev_data->report_data, VK_DEBUG_REPORT_ERROR_BIT_EXT, (VkDebugReportObjectTypeEXT)0, 0, __LINE__,
9786 DRAWSTATE_INVALID_SECONDARY_COMMAND_BUFFER, "DS",
9787 "vkCmdExecuteCommands() called w/ invalid Cmd Buffer %p "
9788 "which has invalid active query pool %" PRIx64 ". Pipeline statistics is being queried so the command "
9789 "buffer must have all bits set on the queryPool.",
9790 reinterpret_cast<void *>(pCB->commandBuffer), reinterpret_cast<const uint64_t &>(queryPoolData->first));
9791 }
9792 }
9793 activeTypes.insert(queryPoolData->second.createInfo.queryType);
9794 }
9795 }
9796 for (auto queryObject : pSubCB->startedQueries) {
9797 auto queryPoolData = dev_data->queryPoolMap.find(queryObject.pool);
9798 if (queryPoolData != dev_data->queryPoolMap.end() && activeTypes.count(queryPoolData->second.createInfo.queryType)) {
9799 skipCall |=
9800 log_msg(dev_data->report_data, VK_DEBUG_REPORT_ERROR_BIT_EXT, (VkDebugReportObjectTypeEXT)0, 0, __LINE__,
9801 DRAWSTATE_INVALID_SECONDARY_COMMAND_BUFFER, "DS",
9802 "vkCmdExecuteCommands() called w/ invalid Cmd Buffer %p "
9803 "which has invalid active query pool %" PRIx64 "of type %d but a query of that type has been started on "
9804 "secondary Cmd Buffer %p.",
9805 reinterpret_cast<void *>(pCB->commandBuffer), reinterpret_cast<const uint64_t &>(queryPoolData->first),
9806 queryPoolData->second.createInfo.queryType, reinterpret_cast<void *>(pSubCB->commandBuffer));
9807 }
9808 }
9809 return skipCall;
9810}
9811
9812VK_LAYER_EXPORT VKAPI_ATTR void VKAPI_CALL
9813vkCmdExecuteCommands(VkCommandBuffer commandBuffer, uint32_t commandBuffersCount, const VkCommandBuffer *pCommandBuffers) {
Dustin Gravese3319182016-04-05 09:41:17 -06009814 bool skipCall = false;
Tobin Ehlis5b5e7bc2016-03-09 16:12:48 -07009815 layer_data *dev_data = get_my_data_ptr(get_dispatch_key(commandBuffer), layer_data_map);
Jeremy Hayesb9e99232016-04-13 16:20:24 -06009816 std::unique_lock<std::mutex> lock(global_lock);
Tobin Ehlis5b5e7bc2016-03-09 16:12:48 -07009817 GLOBAL_CB_NODE *pCB = getCBNode(dev_data, commandBuffer);
9818 if (pCB) {
9819 GLOBAL_CB_NODE *pSubCB = NULL;
9820 for (uint32_t i = 0; i < commandBuffersCount; i++) {
9821 pSubCB = getCBNode(dev_data, pCommandBuffers[i]);
9822 if (!pSubCB) {
9823 skipCall |=
9824 log_msg(dev_data->report_data, VK_DEBUG_REPORT_ERROR_BIT_EXT, (VkDebugReportObjectTypeEXT)0, 0, __LINE__,
9825 DRAWSTATE_INVALID_SECONDARY_COMMAND_BUFFER, "DS",
9826 "vkCmdExecuteCommands() called w/ invalid Cmd Buffer %p in element %u of pCommandBuffers array.",
9827 (void *)pCommandBuffers[i], i);
9828 } else if (VK_COMMAND_BUFFER_LEVEL_PRIMARY == pSubCB->createInfo.level) {
9829 skipCall |= log_msg(dev_data->report_data, VK_DEBUG_REPORT_ERROR_BIT_EXT, (VkDebugReportObjectTypeEXT)0, 0,
9830 __LINE__, DRAWSTATE_INVALID_SECONDARY_COMMAND_BUFFER, "DS",
9831 "vkCmdExecuteCommands() called w/ Primary Cmd Buffer %p in element %u of pCommandBuffers "
9832 "array. All cmd buffers in pCommandBuffers array must be secondary.",
9833 (void *)pCommandBuffers[i], i);
9834 } else if (pCB->activeRenderPass) { // Secondary CB w/i RenderPass must have *CONTINUE_BIT set
9835 if (!(pSubCB->beginInfo.flags & VK_COMMAND_BUFFER_USAGE_RENDER_PASS_CONTINUE_BIT)) {
9836 skipCall |= log_msg(
9837 dev_data->report_data, VK_DEBUG_REPORT_ERROR_BIT_EXT, VK_DEBUG_REPORT_OBJECT_TYPE_COMMAND_BUFFER_EXT,
9838 (uint64_t)pCommandBuffers[i], __LINE__, DRAWSTATE_BEGIN_CB_INVALID_STATE, "DS",
9839 "vkCmdExecuteCommands(): Secondary Command Buffer (%p) executed within render pass (%#" PRIxLEAST64
9840 ") must have had vkBeginCommandBuffer() called w/ VK_COMMAND_BUFFER_USAGE_RENDER_PASS_CONTINUE_BIT set.",
9841 (void *)pCommandBuffers[i], (uint64_t)pCB->activeRenderPass);
9842 } else {
9843 // Make sure render pass is compatible with parent command buffer pass if has continue
9844 skipCall |= validateRenderPassCompatibility(dev_data, commandBuffer, pCB->activeRenderPass, pCommandBuffers[i],
9845 pSubCB->beginInfo.pInheritanceInfo->renderPass);
9846 skipCall |= validateFramebuffer(dev_data, commandBuffer, pCB, pCommandBuffers[i], pSubCB);
9847 }
9848 string errorString = "";
9849 if (!verify_renderpass_compatibility(dev_data, pCB->activeRenderPass,
9850 pSubCB->beginInfo.pInheritanceInfo->renderPass, errorString)) {
9851 skipCall |= log_msg(
9852 dev_data->report_data, VK_DEBUG_REPORT_ERROR_BIT_EXT, VK_DEBUG_REPORT_OBJECT_TYPE_COMMAND_BUFFER_EXT,
9853 (uint64_t)pCommandBuffers[i], __LINE__, DRAWSTATE_RENDERPASS_INCOMPATIBLE, "DS",
9854 "vkCmdExecuteCommands(): Secondary Command Buffer (%p) w/ render pass (%#" PRIxLEAST64
9855 ") is incompatible w/ primary command buffer (%p) w/ render pass (%#" PRIxLEAST64 ") due to: %s",
9856 (void *)pCommandBuffers[i], (uint64_t)pSubCB->beginInfo.pInheritanceInfo->renderPass, (void *)commandBuffer,
9857 (uint64_t)pCB->activeRenderPass, errorString.c_str());
9858 }
9859 // If framebuffer for secondary CB is not NULL, then it must match FB from vkCmdBeginRenderPass()
9860 // that this CB will be executed in AND framebuffer must have been created w/ RP compatible w/ renderpass
9861 if (pSubCB->beginInfo.pInheritanceInfo->framebuffer) {
9862 if (pSubCB->beginInfo.pInheritanceInfo->framebuffer != pCB->activeRenderPassBeginInfo.framebuffer) {
9863 skipCall |= log_msg(
9864 dev_data->report_data, VK_DEBUG_REPORT_ERROR_BIT_EXT, VK_DEBUG_REPORT_OBJECT_TYPE_COMMAND_BUFFER_EXT,
9865 (uint64_t)pCommandBuffers[i], __LINE__, DRAWSTATE_FRAMEBUFFER_INCOMPATIBLE, "DS",
9866 "vkCmdExecuteCommands(): Secondary Command Buffer (%p) references framebuffer (%#" PRIxLEAST64
9867 ") that does not match framebuffer (%#" PRIxLEAST64 ") in active renderpass (%#" PRIxLEAST64 ").",
9868 (void *)pCommandBuffers[i], (uint64_t)pSubCB->beginInfo.pInheritanceInfo->framebuffer,
9869 (uint64_t)pCB->activeRenderPassBeginInfo.framebuffer, (uint64_t)pCB->activeRenderPass);
9870 }
9871 }
9872 }
9873 // TODO(mlentine): Move more logic into this method
9874 skipCall |= validateSecondaryCommandBufferState(dev_data, pCB, pSubCB);
9875 skipCall |= validateCommandBufferState(dev_data, pSubCB);
9876 // Secondary cmdBuffers are considered pending execution starting w/
9877 // being recorded
9878 if (!(pSubCB->beginInfo.flags & VK_COMMAND_BUFFER_USAGE_SIMULTANEOUS_USE_BIT)) {
9879 if (dev_data->globalInFlightCmdBuffers.find(pSubCB->commandBuffer) != dev_data->globalInFlightCmdBuffers.end()) {
9880 skipCall |= log_msg(
9881 dev_data->report_data, VK_DEBUG_REPORT_ERROR_BIT_EXT, VK_DEBUG_REPORT_OBJECT_TYPE_COMMAND_BUFFER_EXT,
9882 (uint64_t)(pCB->commandBuffer), __LINE__, DRAWSTATE_INVALID_CB_SIMULTANEOUS_USE, "DS",
9883 "Attempt to simultaneously execute CB %#" PRIxLEAST64 " w/o VK_COMMAND_BUFFER_USAGE_SIMULTANEOUS_USE_BIT "
9884 "set!",
9885 (uint64_t)(pCB->commandBuffer));
9886 }
9887 if (pCB->beginInfo.flags & VK_COMMAND_BUFFER_USAGE_SIMULTANEOUS_USE_BIT) {
9888 // Warn that non-simultaneous secondary cmd buffer renders primary non-simultaneous
9889 skipCall |= log_msg(
9890 dev_data->report_data, VK_DEBUG_REPORT_WARNING_BIT_EXT, VK_DEBUG_REPORT_OBJECT_TYPE_COMMAND_BUFFER_EXT,
9891 (uint64_t)(pCommandBuffers[i]), __LINE__, DRAWSTATE_INVALID_CB_SIMULTANEOUS_USE, "DS",
9892 "vkCmdExecuteCommands(): Secondary Command Buffer (%#" PRIxLEAST64
9893 ") does not have VK_COMMAND_BUFFER_USAGE_SIMULTANEOUS_USE_BIT set and will cause primary command buffer "
9894 "(%#" PRIxLEAST64 ") to be treated as if it does not have VK_COMMAND_BUFFER_USAGE_SIMULTANEOUS_USE_BIT "
9895 "set, even though it does.",
9896 (uint64_t)(pCommandBuffers[i]), (uint64_t)(pCB->commandBuffer));
9897 pCB->beginInfo.flags &= ~VK_COMMAND_BUFFER_USAGE_SIMULTANEOUS_USE_BIT;
9898 }
9899 }
Tobin Ehlisb1b606d2016-04-11 14:49:55 -06009900 if (!pCB->activeQueries.empty() && !dev_data->phys_dev_properties.features.inheritedQueries) {
Tobin Ehlis5b5e7bc2016-03-09 16:12:48 -07009901 skipCall |=
9902 log_msg(dev_data->report_data, VK_DEBUG_REPORT_ERROR_BIT_EXT, VK_DEBUG_REPORT_OBJECT_TYPE_COMMAND_BUFFER_EXT,
9903 reinterpret_cast<uint64_t>(pCommandBuffers[i]), __LINE__, DRAWSTATE_INVALID_COMMAND_BUFFER, "DS",
9904 "vkCmdExecuteCommands(): Secondary Command Buffer "
9905 "(%#" PRIxLEAST64 ") cannot be submitted with a query in "
9906 "flight and inherited queries not "
9907 "supported on this device.",
9908 reinterpret_cast<uint64_t>(pCommandBuffers[i]));
9909 }
9910 pSubCB->primaryCommandBuffer = pCB->commandBuffer;
9911 pCB->secondaryCommandBuffers.insert(pSubCB->commandBuffer);
9912 dev_data->globalInFlightCmdBuffers.insert(pSubCB->commandBuffer);
9913 }
9914 skipCall |= validatePrimaryCommandBuffer(dev_data, pCB, "vkCmdExecuteComands");
9915 skipCall |= addCmd(dev_data, pCB, CMD_EXECUTECOMMANDS, "vkCmdExecuteComands()");
9916 }
Jeremy Hayesb9e99232016-04-13 16:20:24 -06009917 lock.unlock();
Dustin Gravese3319182016-04-05 09:41:17 -06009918 if (!skipCall)
Tobin Ehlis5b5e7bc2016-03-09 16:12:48 -07009919 dev_data->device_dispatch_table->CmdExecuteCommands(commandBuffer, commandBuffersCount, pCommandBuffers);
9920}
9921
Dustin Gravese3319182016-04-05 09:41:17 -06009922static bool ValidateMapImageLayouts(VkDevice device, VkDeviceMemory mem) {
9923 bool skip_call = false;
Tobin Ehlis5b5e7bc2016-03-09 16:12:48 -07009924 layer_data *dev_data = get_my_data_ptr(get_dispatch_key(device), layer_data_map);
Tobin Ehlis58070a62016-03-16 16:00:36 -06009925 auto mem_data = dev_data->memObjMap.find(mem);
9926 if ((mem_data != dev_data->memObjMap.end()) && (mem_data->second.image != VK_NULL_HANDLE)) {
Tobin Ehlis5b5e7bc2016-03-09 16:12:48 -07009927 std::vector<VkImageLayout> layouts;
Tobin Ehlis58070a62016-03-16 16:00:36 -06009928 if (FindLayouts(dev_data, mem_data->second.image, layouts)) {
Tobin Ehlis5b5e7bc2016-03-09 16:12:48 -07009929 for (auto layout : layouts) {
9930 if (layout != VK_IMAGE_LAYOUT_PREINITIALIZED && layout != VK_IMAGE_LAYOUT_GENERAL) {
9931 skip_call |= log_msg(dev_data->report_data, VK_DEBUG_REPORT_ERROR_BIT_EXT, (VkDebugReportObjectTypeEXT)0, 0,
9932 __LINE__, DRAWSTATE_INVALID_IMAGE_LAYOUT, "DS", "Cannot map an image with layout %s. Only "
9933 "GENERAL or PREINITIALIZED are supported.",
9934 string_VkImageLayout(layout));
9935 }
9936 }
9937 }
9938 }
9939 return skip_call;
9940}
9941
9942VK_LAYER_EXPORT VKAPI_ATTR VkResult VKAPI_CALL
9943vkMapMemory(VkDevice device, VkDeviceMemory mem, VkDeviceSize offset, VkDeviceSize size, VkFlags flags, void **ppData) {
9944 layer_data *dev_data = get_my_data_ptr(get_dispatch_key(device), layer_data_map);
9945
Dustin Gravese3319182016-04-05 09:41:17 -06009946 bool skip_call = false;
Tobin Ehlis5b5e7bc2016-03-09 16:12:48 -07009947 VkResult result = VK_ERROR_VALIDATION_FAILED_EXT;
Jeremy Hayesb9e99232016-04-13 16:20:24 -06009948 std::unique_lock<std::mutex> lock(global_lock);
Mark Lobodzinski03b71512016-03-23 14:33:02 -06009949#if MTMERGESOURCE
Tobin Ehlis58070a62016-03-16 16:00:36 -06009950 DEVICE_MEM_INFO *pMemObj = get_mem_obj_info(dev_data, mem);
Tobin Ehlis5b5e7bc2016-03-09 16:12:48 -07009951 if (pMemObj) {
9952 pMemObj->valid = true;
Tobin Ehlisb1b606d2016-04-11 14:49:55 -06009953 if ((dev_data->phys_dev_mem_props.memoryTypes[pMemObj->allocInfo.memoryTypeIndex].propertyFlags &
9954 VK_MEMORY_PROPERTY_HOST_VISIBLE_BIT) == 0) {
Tobin Ehlis5b5e7bc2016-03-09 16:12:48 -07009955 skip_call =
9956 log_msg(dev_data->report_data, VK_DEBUG_REPORT_ERROR_BIT_EXT, VK_DEBUG_REPORT_OBJECT_TYPE_DEVICE_MEMORY_EXT,
9957 (uint64_t)mem, __LINE__, MEMTRACK_INVALID_STATE, "MEM",
9958 "Mapping Memory without VK_MEMORY_PROPERTY_HOST_VISIBLE_BIT set: mem obj %#" PRIxLEAST64, (uint64_t)mem);
9959 }
9960 }
9961 skip_call |= validateMemRange(dev_data, mem, offset, size);
9962 storeMemRanges(dev_data, mem, offset, size);
9963#endif
Tobin Ehlis58070a62016-03-16 16:00:36 -06009964 skip_call |= ValidateMapImageLayouts(device, mem);
Jeremy Hayesb9e99232016-04-13 16:20:24 -06009965 lock.unlock();
Tobin Ehlis5b5e7bc2016-03-09 16:12:48 -07009966
Dustin Gravese3319182016-04-05 09:41:17 -06009967 if (!skip_call) {
Tobin Ehlis5b5e7bc2016-03-09 16:12:48 -07009968 result = dev_data->device_dispatch_table->MapMemory(device, mem, offset, size, flags, ppData);
Mark Lobodzinski03b71512016-03-23 14:33:02 -06009969#if MTMERGESOURCE
Jeremy Hayesb9e99232016-04-13 16:20:24 -06009970 lock.lock();
Tobin Ehlis5b5e7bc2016-03-09 16:12:48 -07009971 initializeAndTrackMemory(dev_data, mem, size, ppData);
Jeremy Hayesb9e99232016-04-13 16:20:24 -06009972 lock.unlock();
Tobin Ehlis5b5e7bc2016-03-09 16:12:48 -07009973#endif
9974 }
9975 return result;
9976}
9977
Mark Lobodzinski03b71512016-03-23 14:33:02 -06009978#if MTMERGESOURCE
Tobin Ehlis5b5e7bc2016-03-09 16:12:48 -07009979VK_LAYER_EXPORT VKAPI_ATTR void VKAPI_CALL vkUnmapMemory(VkDevice device, VkDeviceMemory mem) {
9980 layer_data *my_data = get_my_data_ptr(get_dispatch_key(device), layer_data_map);
Dustin Gravese3319182016-04-05 09:41:17 -06009981 bool skipCall = false;
Tobin Ehlis5b5e7bc2016-03-09 16:12:48 -07009982
Jeremy Hayesb9e99232016-04-13 16:20:24 -06009983 std::unique_lock<std::mutex> lock(global_lock);
Tobin Ehlis5b5e7bc2016-03-09 16:12:48 -07009984 skipCall |= deleteMemRanges(my_data, mem);
Jeremy Hayesb9e99232016-04-13 16:20:24 -06009985 lock.unlock();
Dustin Gravese3319182016-04-05 09:41:17 -06009986 if (!skipCall) {
Tobin Ehlis5b5e7bc2016-03-09 16:12:48 -07009987 my_data->device_dispatch_table->UnmapMemory(device, mem);
9988 }
9989}
9990
Dustin Gravese3319182016-04-05 09:41:17 -06009991static bool validateMemoryIsMapped(layer_data *my_data, const char *funcName, uint32_t memRangeCount,
9992 const VkMappedMemoryRange *pMemRanges) {
9993 bool skipCall = false;
Tobin Ehlis5b5e7bc2016-03-09 16:12:48 -07009994 for (uint32_t i = 0; i < memRangeCount; ++i) {
9995 auto mem_element = my_data->memObjMap.find(pMemRanges[i].memory);
9996 if (mem_element != my_data->memObjMap.end()) {
9997 if (mem_element->second.memRange.offset > pMemRanges[i].offset) {
9998 skipCall |= log_msg(
9999 my_data->report_data, VK_DEBUG_REPORT_ERROR_BIT_EXT, VK_DEBUG_REPORT_OBJECT_TYPE_DEVICE_MEMORY_EXT,
10000 (uint64_t)pMemRanges[i].memory, __LINE__, MEMTRACK_INVALID_MAP, "MEM",
10001 "%s: Flush/Invalidate offset (" PRINTF_SIZE_T_SPECIFIER ") is less than Memory Object's offset "
10002 "(" PRINTF_SIZE_T_SPECIFIER ").",
10003 funcName, static_cast<size_t>(pMemRanges[i].offset), static_cast<size_t>(mem_element->second.memRange.offset));
10004 }
10005 if ((mem_element->second.memRange.size != VK_WHOLE_SIZE) &&
10006 ((mem_element->second.memRange.offset + mem_element->second.memRange.size) <
10007 (pMemRanges[i].offset + pMemRanges[i].size))) {
10008 skipCall |= log_msg(my_data->report_data, VK_DEBUG_REPORT_ERROR_BIT_EXT,
10009 VK_DEBUG_REPORT_OBJECT_TYPE_DEVICE_MEMORY_EXT, (uint64_t)pMemRanges[i].memory, __LINE__,
10010 MEMTRACK_INVALID_MAP, "MEM", "%s: Flush/Invalidate upper-bound (" PRINTF_SIZE_T_SPECIFIER
10011 ") exceeds the Memory Object's upper-bound "
10012 "(" PRINTF_SIZE_T_SPECIFIER ").",
10013 funcName, static_cast<size_t>(pMemRanges[i].offset + pMemRanges[i].size),
10014 static_cast<size_t>(mem_element->second.memRange.offset + mem_element->second.memRange.size));
10015 }
10016 }
10017 }
10018 return skipCall;
10019}
10020
Dustin Gravese3319182016-04-05 09:41:17 -060010021static bool validateAndCopyNoncoherentMemoryToDriver(layer_data *my_data, uint32_t memRangeCount,
10022 const VkMappedMemoryRange *pMemRanges) {
10023 bool skipCall = false;
Tobin Ehlis5b5e7bc2016-03-09 16:12:48 -070010024 for (uint32_t i = 0; i < memRangeCount; ++i) {
10025 auto mem_element = my_data->memObjMap.find(pMemRanges[i].memory);
10026 if (mem_element != my_data->memObjMap.end()) {
10027 if (mem_element->second.pData) {
10028 VkDeviceSize size = mem_element->second.memRange.size;
10029 VkDeviceSize half_size = (size / 2);
10030 char *data = static_cast<char *>(mem_element->second.pData);
10031 for (auto j = 0; j < half_size; ++j) {
10032 if (data[j] != NoncoherentMemoryFillValue) {
10033 skipCall |= log_msg(my_data->report_data, VK_DEBUG_REPORT_ERROR_BIT_EXT,
10034 VK_DEBUG_REPORT_OBJECT_TYPE_DEVICE_MEMORY_EXT, (uint64_t)pMemRanges[i].memory, __LINE__,
10035 MEMTRACK_INVALID_MAP, "MEM", "Memory overflow was detected on mem obj %" PRIxLEAST64,
10036 (uint64_t)pMemRanges[i].memory);
10037 }
10038 }
10039 for (auto j = size + half_size; j < 2 * size; ++j) {
10040 if (data[j] != NoncoherentMemoryFillValue) {
10041 skipCall |= log_msg(my_data->report_data, VK_DEBUG_REPORT_ERROR_BIT_EXT,
10042 VK_DEBUG_REPORT_OBJECT_TYPE_DEVICE_MEMORY_EXT, (uint64_t)pMemRanges[i].memory, __LINE__,
10043 MEMTRACK_INVALID_MAP, "MEM", "Memory overflow was detected on mem obj %" PRIxLEAST64,
10044 (uint64_t)pMemRanges[i].memory);
10045 }
10046 }
10047 memcpy(mem_element->second.pDriverData, static_cast<void *>(data + (size_t)(half_size)), (size_t)(size));
10048 }
10049 }
10050 }
10051 return skipCall;
10052}
10053
10054VK_LAYER_EXPORT VkResult VKAPI_CALL
10055vkFlushMappedMemoryRanges(VkDevice device, uint32_t memRangeCount, const VkMappedMemoryRange *pMemRanges) {
10056 VkResult result = VK_ERROR_VALIDATION_FAILED_EXT;
Dustin Gravese3319182016-04-05 09:41:17 -060010057 bool skipCall = false;
Tobin Ehlis5b5e7bc2016-03-09 16:12:48 -070010058 layer_data *my_data = get_my_data_ptr(get_dispatch_key(device), layer_data_map);
10059
Jeremy Hayesb9e99232016-04-13 16:20:24 -060010060 std::unique_lock<std::mutex> lock(global_lock);
Tobin Ehlis5b5e7bc2016-03-09 16:12:48 -070010061 skipCall |= validateAndCopyNoncoherentMemoryToDriver(my_data, memRangeCount, pMemRanges);
10062 skipCall |= validateMemoryIsMapped(my_data, "vkFlushMappedMemoryRanges", memRangeCount, pMemRanges);
Jeremy Hayesb9e99232016-04-13 16:20:24 -060010063 lock.unlock();
Dustin Gravese3319182016-04-05 09:41:17 -060010064 if (!skipCall) {
Tobin Ehlis5b5e7bc2016-03-09 16:12:48 -070010065 result = my_data->device_dispatch_table->FlushMappedMemoryRanges(device, memRangeCount, pMemRanges);
10066 }
10067 return result;
10068}
10069
10070VK_LAYER_EXPORT VkResult VKAPI_CALL
10071vkInvalidateMappedMemoryRanges(VkDevice device, uint32_t memRangeCount, const VkMappedMemoryRange *pMemRanges) {
10072 VkResult result = VK_ERROR_VALIDATION_FAILED_EXT;
Dustin Gravese3319182016-04-05 09:41:17 -060010073 bool skipCall = false;
Tobin Ehlis5b5e7bc2016-03-09 16:12:48 -070010074 layer_data *my_data = get_my_data_ptr(get_dispatch_key(device), layer_data_map);
10075
Jeremy Hayesb9e99232016-04-13 16:20:24 -060010076 std::unique_lock<std::mutex> lock(global_lock);
Tobin Ehlis5b5e7bc2016-03-09 16:12:48 -070010077 skipCall |= validateMemoryIsMapped(my_data, "vkInvalidateMappedMemoryRanges", memRangeCount, pMemRanges);
Jeremy Hayesb9e99232016-04-13 16:20:24 -060010078 lock.unlock();
Dustin Gravese3319182016-04-05 09:41:17 -060010079 if (!skipCall) {
Tobin Ehlis5b5e7bc2016-03-09 16:12:48 -070010080 result = my_data->device_dispatch_table->InvalidateMappedMemoryRanges(device, memRangeCount, pMemRanges);
10081 }
10082 return result;
10083}
10084#endif
10085
10086VKAPI_ATTR VkResult VKAPI_CALL vkBindImageMemory(VkDevice device, VkImage image, VkDeviceMemory mem, VkDeviceSize memoryOffset) {
10087 layer_data *dev_data = get_my_data_ptr(get_dispatch_key(device), layer_data_map);
10088 VkResult result = VK_ERROR_VALIDATION_FAILED_EXT;
Dustin Gravese3319182016-04-05 09:41:17 -060010089 bool skipCall = false;
Jeremy Hayesb9e99232016-04-13 16:20:24 -060010090 std::unique_lock<std::mutex> lock(global_lock);
Tobin Ehlis94c53c02016-04-05 13:33:00 -060010091 auto image_node = dev_data->imageMap.find(image);
10092 if (image_node != dev_data->imageMap.end()) {
10093 // Track objects tied to memory
Dustin Graves09df2b62016-04-06 16:47:10 -060010094 uint64_t image_handle = reinterpret_cast<uint64_t&>(image);
Tobin Ehlis94c53c02016-04-05 13:33:00 -060010095 skipCall = set_mem_binding(dev_data, mem, image_handle, VK_DEBUG_REPORT_OBJECT_TYPE_IMAGE_EXT, "vkBindImageMemory");
Tobin Ehlis5b5e7bc2016-03-09 16:12:48 -070010096 VkMemoryRequirements memRequirements;
Jeremy Hayesb9e99232016-04-13 16:20:24 -060010097 lock.unlock();
Tobin Ehlis94c53c02016-04-05 13:33:00 -060010098 dev_data->device_dispatch_table->GetImageMemoryRequirements(device, image, &memRequirements);
Jeremy Hayesb9e99232016-04-13 16:20:24 -060010099 lock.lock();
Tobin Ehlis58070a62016-03-16 16:00:36 -060010100 skipCall |= validate_buffer_image_aliasing(dev_data, image_handle, mem, memoryOffset, memRequirements,
10101 dev_data->memObjMap[mem].imageRanges, dev_data->memObjMap[mem].bufferRanges,
10102 VK_DEBUG_REPORT_OBJECT_TYPE_IMAGE_EXT);
Chris Forbes0a1ce3d2016-04-06 15:16:26 +120010103 print_mem_list(dev_data);
Jeremy Hayesb9e99232016-04-13 16:20:24 -060010104 lock.unlock();
Tobin Ehlis94c53c02016-04-05 13:33:00 -060010105 if (!skipCall) {
10106 result = dev_data->device_dispatch_table->BindImageMemory(device, image, mem, memoryOffset);
Jeremy Hayesb9e99232016-04-13 16:20:24 -060010107 lock.lock();
Tobin Ehlis94c53c02016-04-05 13:33:00 -060010108 dev_data->memObjMap[mem].image = image;
10109 image_node->second.mem = mem;
10110 image_node->second.memOffset = memoryOffset;
10111 image_node->second.memSize = memRequirements.size;
Jeremy Hayesb9e99232016-04-13 16:20:24 -060010112 lock.unlock();
Tobin Ehlis94c53c02016-04-05 13:33:00 -060010113 }
10114 } else {
10115 log_msg(dev_data->report_data, VK_DEBUG_REPORT_ERROR_BIT_EXT, VK_DEBUG_REPORT_OBJECT_TYPE_DEVICE_MEMORY_EXT,
10116 reinterpret_cast<const uint64_t &>(image), __LINE__, MEMTRACK_INVALID_OBJECT, "MT",
10117 "vkBindImageMemory: Cannot find invalid image %" PRIx64 ", has it already been deleted?",
10118 reinterpret_cast<const uint64_t &>(image));
Tobin Ehlis5b5e7bc2016-03-09 16:12:48 -070010119 }
10120 return result;
10121}
10122
10123VKAPI_ATTR VkResult VKAPI_CALL vkSetEvent(VkDevice device, VkEvent event) {
Tobin Ehlis3ee1d602016-04-13 16:18:28 -060010124 bool skip_call = false;
10125 VkResult result = VK_ERROR_VALIDATION_FAILED_EXT;
Tobin Ehlis5b5e7bc2016-03-09 16:12:48 -070010126 layer_data *dev_data = get_my_data_ptr(get_dispatch_key(device), layer_data_map);
Jeremy Hayesb9e99232016-04-13 16:20:24 -060010127 std::unique_lock<std::mutex> lock(global_lock);
Tobin Ehlis3ee1d602016-04-13 16:18:28 -060010128 auto event_node = dev_data->eventMap.find(event);
10129 if (event_node != dev_data->eventMap.end()) {
10130 event_node->second.needsSignaled = false;
10131 event_node->second.stageMask = VK_PIPELINE_STAGE_HOST_BIT;
10132 if (event_node->second.in_use.load()) {
10133 skip_call |= log_msg(dev_data->report_data, VK_DEBUG_REPORT_ERROR_BIT_EXT, VK_DEBUG_REPORT_OBJECT_TYPE_EVENT_EXT,
10134 reinterpret_cast<const uint64_t &>(event), __LINE__, DRAWSTATE_QUEUE_FORWARD_PROGRESS, "DS",
10135 "Cannot call vkSetEvent() on event %" PRIxLEAST64 " that is already in use by a command buffer.",
10136 reinterpret_cast<const uint64_t &>(event));
10137 }
10138 }
Jeremy Hayesb9e99232016-04-13 16:20:24 -060010139 lock.unlock();
Tobin Ehlis6fcff212016-04-07 11:35:46 -060010140 // Host setting event is visible to all queues immediately so update stageMask for any queue that's seen this event
10141 // TODO : For correctness this needs separate fix to verify that app doesn't make incorrect assumptions about the
10142 // ordering of this command in relation to vkCmd[Set|Reset]Events (see GH297)
10143 for (auto queue_data : dev_data->queueMap) {
10144 auto event_entry = queue_data.second.eventToStageMap.find(event);
10145 if (event_entry != queue_data.second.eventToStageMap.end()) {
10146 event_entry->second |= VK_PIPELINE_STAGE_HOST_BIT;
10147 }
10148 }
Tobin Ehlis3ee1d602016-04-13 16:18:28 -060010149 if (!skip_call)
10150 result = dev_data->device_dispatch_table->SetEvent(device, event);
Tobin Ehlis5b5e7bc2016-03-09 16:12:48 -070010151 return result;
10152}
10153
10154VKAPI_ATTR VkResult VKAPI_CALL
10155vkQueueBindSparse(VkQueue queue, uint32_t bindInfoCount, const VkBindSparseInfo *pBindInfo, VkFence fence) {
10156 layer_data *dev_data = get_my_data_ptr(get_dispatch_key(queue), layer_data_map);
10157 VkResult result = VK_ERROR_VALIDATION_FAILED_EXT;
Dustin Gravese3319182016-04-05 09:41:17 -060010158 bool skip_call = false;
Jeremy Hayesb9e99232016-04-13 16:20:24 -060010159 std::unique_lock<std::mutex> lock(global_lock);
Tobin Ehlis4b38d3a2016-04-14 07:02:43 -060010160 // First verify that fence is not in use
10161 if ((fence != VK_NULL_HANDLE) && (bindInfoCount != 0) && dev_data->fenceMap[fence].in_use.load()) {
10162 skip_call |= log_msg(dev_data->report_data, VK_DEBUG_REPORT_ERROR_BIT_EXT, VK_DEBUG_REPORT_OBJECT_TYPE_FENCE_EXT,
Mark Lobodzinski47c4ec02016-04-14 11:30:32 -060010163 reinterpret_cast<uint64_t &>(fence), __LINE__, DRAWSTATE_INVALID_FENCE, "DS",
10164 "Fence %#" PRIx64 " is already in use by another submission.", reinterpret_cast<uint64_t &>(fence));
Tobin Ehlis4b38d3a2016-04-14 07:02:43 -060010165 }
10166 uint64_t fenceId = 0;
10167 skip_call = add_fence_info(dev_data, fence, queue, &fenceId);
Tobin Ehlis5b5e7bc2016-03-09 16:12:48 -070010168 for (uint32_t bindIdx = 0; bindIdx < bindInfoCount; ++bindIdx) {
10169 const VkBindSparseInfo &bindInfo = pBindInfo[bindIdx];
Tobin Ehlis13443022016-04-12 10:49:41 -060010170 // Track objects tied to memory
10171 for (uint32_t j = 0; j < bindInfo.bufferBindCount; j++) {
10172 for (uint32_t k = 0; k < bindInfo.pBufferBinds[j].bindCount; k++) {
10173 if (set_sparse_mem_binding(dev_data, bindInfo.pBufferBinds[j].pBinds[k].memory,
10174 (uint64_t)bindInfo.pBufferBinds[j].buffer, VK_DEBUG_REPORT_OBJECT_TYPE_BUFFER_EXT,
10175 "vkQueueBindSparse"))
10176 skip_call = true;
10177 }
10178 }
10179 for (uint32_t j = 0; j < bindInfo.imageOpaqueBindCount; j++) {
10180 for (uint32_t k = 0; k < bindInfo.pImageOpaqueBinds[j].bindCount; k++) {
10181 if (set_sparse_mem_binding(dev_data, bindInfo.pImageOpaqueBinds[j].pBinds[k].memory,
10182 (uint64_t)bindInfo.pImageOpaqueBinds[j].image, VK_DEBUG_REPORT_OBJECT_TYPE_IMAGE_EXT,
10183 "vkQueueBindSparse"))
10184 skip_call = true;
10185 }
10186 }
10187 for (uint32_t j = 0; j < bindInfo.imageBindCount; j++) {
10188 for (uint32_t k = 0; k < bindInfo.pImageBinds[j].bindCount; k++) {
10189 if (set_sparse_mem_binding(dev_data, bindInfo.pImageBinds[j].pBinds[k].memory,
10190 (uint64_t)bindInfo.pImageBinds[j].image, VK_DEBUG_REPORT_OBJECT_TYPE_IMAGE_EXT,
10191 "vkQueueBindSparse"))
10192 skip_call = true;
10193 }
10194 }
Tobin Ehlis5b5e7bc2016-03-09 16:12:48 -070010195 for (uint32_t i = 0; i < bindInfo.waitSemaphoreCount; ++i) {
Tobin Ehlis13443022016-04-12 10:49:41 -060010196 const VkSemaphore &semaphore = bindInfo.pWaitSemaphores[i];
10197 if (dev_data->semaphoreMap.find(semaphore) != dev_data->semaphoreMap.end()) {
10198 if (dev_data->semaphoreMap[semaphore].signaled) {
10199 dev_data->semaphoreMap[semaphore].signaled = false;
10200 } else {
10201 skip_call |=
10202 log_msg(dev_data->report_data, VK_DEBUG_REPORT_ERROR_BIT_EXT, VK_DEBUG_REPORT_OBJECT_TYPE_SEMAPHORE_EXT,
10203 reinterpret_cast<const uint64_t &>(semaphore), __LINE__, DRAWSTATE_QUEUE_FORWARD_PROGRESS, "DS",
10204 "vkQueueBindSparse: Queue %#" PRIx64 " is waiting on semaphore %#" PRIx64
10205 " that has no way to be signaled.",
10206 reinterpret_cast<const uint64_t &>(queue), reinterpret_cast<const uint64_t &>(semaphore));
10207 }
Tobin Ehlis5b5e7bc2016-03-09 16:12:48 -070010208 }
10209 }
10210 for (uint32_t i = 0; i < bindInfo.signalSemaphoreCount; ++i) {
Tobin Ehlis13443022016-04-12 10:49:41 -060010211 const VkSemaphore &semaphore = bindInfo.pSignalSemaphores[i];
10212 if (dev_data->semaphoreMap.find(semaphore) != dev_data->semaphoreMap.end()) {
10213 if (dev_data->semaphoreMap[semaphore].signaled) {
10214 skip_call =
10215 log_msg(dev_data->report_data, VK_DEBUG_REPORT_ERROR_BIT_EXT, VK_DEBUG_REPORT_OBJECT_TYPE_SEMAPHORE_EXT,
10216 reinterpret_cast<const uint64_t &>(semaphore), __LINE__, DRAWSTATE_QUEUE_FORWARD_PROGRESS, "DS",
10217 "vkQueueBindSparse: Queue %#" PRIx64 " is signaling semaphore %#" PRIx64
10218 ", but that semaphore is already signaled.",
10219 reinterpret_cast<const uint64_t &>(queue), reinterpret_cast<const uint64_t &>(semaphore));
10220 }
10221 dev_data->semaphoreMap[semaphore].signaled = true;
10222 }
Tobin Ehlis5b5e7bc2016-03-09 16:12:48 -070010223 }
10224 }
Tobin Ehlis13443022016-04-12 10:49:41 -060010225 print_mem_list(dev_data);
Jeremy Hayesb9e99232016-04-13 16:20:24 -060010226 lock.unlock();
Tobin Ehlis5b5e7bc2016-03-09 16:12:48 -070010227
Dustin Gravese3319182016-04-05 09:41:17 -060010228 if (!skip_call)
Tobin Ehlis5b5e7bc2016-03-09 16:12:48 -070010229 return dev_data->device_dispatch_table->QueueBindSparse(queue, bindInfoCount, pBindInfo, fence);
Tobin Ehlis5b5e7bc2016-03-09 16:12:48 -070010230
10231 return result;
10232}
10233
10234VKAPI_ATTR VkResult VKAPI_CALL vkCreateSemaphore(VkDevice device, const VkSemaphoreCreateInfo *pCreateInfo,
10235 const VkAllocationCallbacks *pAllocator, VkSemaphore *pSemaphore) {
10236 layer_data *dev_data = get_my_data_ptr(get_dispatch_key(device), layer_data_map);
10237 VkResult result = dev_data->device_dispatch_table->CreateSemaphore(device, pCreateInfo, pAllocator, pSemaphore);
10238 if (result == VK_SUCCESS) {
Jeremy Hayesb9e99232016-04-13 16:20:24 -060010239 std::lock_guard<std::mutex> lock(global_lock);
Tobin Ehlis5b5e7bc2016-03-09 16:12:48 -070010240 SEMAPHORE_NODE* sNode = &dev_data->semaphoreMap[*pSemaphore];
Tobin Ehlis13443022016-04-12 10:49:41 -060010241 sNode->signaled = false;
Michael Lentine0a32ed72016-03-17 16:34:32 -050010242 sNode->queue = VK_NULL_HANDLE;
Tobin Ehlis5b5e7bc2016-03-09 16:12:48 -070010243 sNode->in_use.store(0);
Tobin Ehlis5b5e7bc2016-03-09 16:12:48 -070010244 }
10245 return result;
10246}
10247
10248VKAPI_ATTR VkResult VKAPI_CALL
10249vkCreateEvent(VkDevice device, const VkEventCreateInfo *pCreateInfo, const VkAllocationCallbacks *pAllocator, VkEvent *pEvent) {
10250 layer_data *dev_data = get_my_data_ptr(get_dispatch_key(device), layer_data_map);
10251 VkResult result = dev_data->device_dispatch_table->CreateEvent(device, pCreateInfo, pAllocator, pEvent);
10252 if (result == VK_SUCCESS) {
Jeremy Hayesb9e99232016-04-13 16:20:24 -060010253 std::lock_guard<std::mutex> lock(global_lock);
Tobin Ehlis5b5e7bc2016-03-09 16:12:48 -070010254 dev_data->eventMap[*pEvent].needsSignaled = false;
10255 dev_data->eventMap[*pEvent].in_use.store(0);
10256 dev_data->eventMap[*pEvent].stageMask = VkPipelineStageFlags(0);
Tobin Ehlis5b5e7bc2016-03-09 16:12:48 -070010257 }
10258 return result;
10259}
10260
10261VK_LAYER_EXPORT VKAPI_ATTR VkResult VKAPI_CALL vkCreateSwapchainKHR(VkDevice device, const VkSwapchainCreateInfoKHR *pCreateInfo,
10262 const VkAllocationCallbacks *pAllocator,
10263 VkSwapchainKHR *pSwapchain) {
10264 layer_data *dev_data = get_my_data_ptr(get_dispatch_key(device), layer_data_map);
10265 VkResult result = dev_data->device_dispatch_table->CreateSwapchainKHR(device, pCreateInfo, pAllocator, pSwapchain);
10266
10267 if (VK_SUCCESS == result) {
Tobin Ehlis08017632016-03-16 13:52:20 -060010268 SWAPCHAIN_NODE *psc_node = new SWAPCHAIN_NODE(pCreateInfo);
Jeremy Hayesb9e99232016-04-13 16:20:24 -060010269 std::lock_guard<std::mutex> lock(global_lock);
Tobin Ehlis08017632016-03-16 13:52:20 -060010270 dev_data->device_extensions.swapchainMap[*pSwapchain] = psc_node;
Tobin Ehlis5b5e7bc2016-03-09 16:12:48 -070010271 }
10272
10273 return result;
10274}
10275
10276VK_LAYER_EXPORT VKAPI_ATTR void VKAPI_CALL
10277vkDestroySwapchainKHR(VkDevice device, VkSwapchainKHR swapchain, const VkAllocationCallbacks *pAllocator) {
10278 layer_data *dev_data = get_my_data_ptr(get_dispatch_key(device), layer_data_map);
10279 bool skipCall = false;
10280
Jeremy Hayesb9e99232016-04-13 16:20:24 -060010281 std::unique_lock<std::mutex> lock(global_lock);
Tobin Ehlis5b5e7bc2016-03-09 16:12:48 -070010282 auto swapchain_data = dev_data->device_extensions.swapchainMap.find(swapchain);
10283 if (swapchain_data != dev_data->device_extensions.swapchainMap.end()) {
10284 if (swapchain_data->second->images.size() > 0) {
10285 for (auto swapchain_image : swapchain_data->second->images) {
10286 auto image_sub = dev_data->imageSubresourceMap.find(swapchain_image);
10287 if (image_sub != dev_data->imageSubresourceMap.end()) {
10288 for (auto imgsubpair : image_sub->second) {
10289 auto image_item = dev_data->imageLayoutMap.find(imgsubpair);
10290 if (image_item != dev_data->imageLayoutMap.end()) {
10291 dev_data->imageLayoutMap.erase(image_item);
10292 }
10293 }
10294 dev_data->imageSubresourceMap.erase(image_sub);
10295 }
Chris Forbes0a1ce3d2016-04-06 15:16:26 +120010296 skipCall = clear_object_binding(dev_data, (uint64_t)swapchain_image,
Tobin Ehlis08017632016-03-16 13:52:20 -060010297 VK_DEBUG_REPORT_OBJECT_TYPE_SWAPCHAIN_KHR_EXT);
Tobin Ehlis94c53c02016-04-05 13:33:00 -060010298 dev_data->imageMap.erase(swapchain_image);
Tobin Ehlis5b5e7bc2016-03-09 16:12:48 -070010299 }
10300 }
10301 delete swapchain_data->second;
10302 dev_data->device_extensions.swapchainMap.erase(swapchain);
10303 }
Jeremy Hayesb9e99232016-04-13 16:20:24 -060010304 lock.unlock();
Tobin Ehlis5b5e7bc2016-03-09 16:12:48 -070010305 if (!skipCall)
10306 dev_data->device_dispatch_table->DestroySwapchainKHR(device, swapchain, pAllocator);
10307}
10308
10309VK_LAYER_EXPORT VKAPI_ATTR VkResult VKAPI_CALL
10310vkGetSwapchainImagesKHR(VkDevice device, VkSwapchainKHR swapchain, uint32_t *pCount, VkImage *pSwapchainImages) {
10311 layer_data *dev_data = get_my_data_ptr(get_dispatch_key(device), layer_data_map);
10312 VkResult result = dev_data->device_dispatch_table->GetSwapchainImagesKHR(device, swapchain, pCount, pSwapchainImages);
10313
10314 if (result == VK_SUCCESS && pSwapchainImages != NULL) {
10315 // This should never happen and is checked by param checker.
10316 if (!pCount)
10317 return result;
Jeremy Hayesb9e99232016-04-13 16:20:24 -060010318 std::lock_guard<std::mutex> lock(global_lock);
Tobin Ehlis5b5e7bc2016-03-09 16:12:48 -070010319 const size_t count = *pCount;
Tobin Ehlis08017632016-03-16 13:52:20 -060010320 auto swapchain_node = dev_data->device_extensions.swapchainMap[swapchain];
10321 if (!swapchain_node->images.empty()) {
10322 // TODO : Not sure I like the memcmp here, but it works
10323 const bool mismatch = (swapchain_node->images.size() != count ||
10324 memcmp(&swapchain_node->images[0], pSwapchainImages, sizeof(swapchain_node->images[0]) * count));
Tobin Ehlis5b5e7bc2016-03-09 16:12:48 -070010325 if (mismatch) {
10326 // TODO: Verify against Valid Usage section of extension
10327 log_msg(dev_data->report_data, VK_DEBUG_REPORT_WARNING_BIT_EXT, VK_DEBUG_REPORT_OBJECT_TYPE_SWAPCHAIN_KHR_EXT,
10328 (uint64_t)swapchain, __LINE__, MEMTRACK_NONE, "SWAP_CHAIN",
10329 "vkGetSwapchainInfoKHR(%" PRIu64
10330 ", VK_SWAP_CHAIN_INFO_TYPE_PERSISTENT_IMAGES_KHR) returned mismatching data",
10331 (uint64_t)(swapchain));
10332 }
10333 }
Tobin Ehlis5b5e7bc2016-03-09 16:12:48 -070010334 for (uint32_t i = 0; i < *pCount; ++i) {
10335 IMAGE_LAYOUT_NODE image_layout_node;
10336 image_layout_node.layout = VK_IMAGE_LAYOUT_UNDEFINED;
Tobin Ehlis5b5e7bc2016-03-09 16:12:48 -070010337 image_layout_node.format = swapchain_node->createInfo.imageFormat;
Tobin Ehlis94c53c02016-04-05 13:33:00 -060010338 auto &image_node = dev_data->imageMap[pSwapchainImages[i]];
10339 image_node.createInfo.mipLevels = 1;
10340 image_node.createInfo.arrayLayers = swapchain_node->createInfo.imageArrayLayers;
10341 image_node.createInfo.usage = swapchain_node->createInfo.imageUsage;
10342 image_node.valid = false;
10343 image_node.mem = MEMTRACKER_SWAP_CHAIN_IMAGE_KEY;
Tobin Ehlis5b5e7bc2016-03-09 16:12:48 -070010344 swapchain_node->images.push_back(pSwapchainImages[i]);
10345 ImageSubresourcePair subpair = {pSwapchainImages[i], false, VkImageSubresource()};
10346 dev_data->imageSubresourceMap[pSwapchainImages[i]].push_back(subpair);
10347 dev_data->imageLayoutMap[subpair] = image_layout_node;
10348 dev_data->device_extensions.imageToSwapchainMap[pSwapchainImages[i]] = swapchain;
10349 }
Tobin Ehlis5b5e7bc2016-03-09 16:12:48 -070010350 }
10351 return result;
10352}
10353
10354VK_LAYER_EXPORT VKAPI_ATTR VkResult VKAPI_CALL vkQueuePresentKHR(VkQueue queue, const VkPresentInfoKHR *pPresentInfo) {
10355 layer_data *dev_data = get_my_data_ptr(get_dispatch_key(queue), layer_data_map);
10356 VkResult result = VK_ERROR_VALIDATION_FAILED_EXT;
10357 bool skip_call = false;
10358
10359 if (pPresentInfo) {
Jeremy Hayesb9e99232016-04-13 16:20:24 -060010360 std::lock_guard<std::mutex> lock(global_lock);
Tobin Ehlis5b5e7bc2016-03-09 16:12:48 -070010361 for (uint32_t i = 0; i < pPresentInfo->waitSemaphoreCount; ++i) {
Tobin Ehlis13443022016-04-12 10:49:41 -060010362 const VkSemaphore &semaphore = pPresentInfo->pWaitSemaphores[i];
10363 if (dev_data->semaphoreMap.find(semaphore) != dev_data->semaphoreMap.end()) {
10364 if (dev_data->semaphoreMap[semaphore].signaled) {
10365 dev_data->semaphoreMap[semaphore].signaled = false;
10366 } else {
10367 skip_call |=
10368 log_msg(dev_data->report_data, VK_DEBUG_REPORT_ERROR_BIT_EXT,
10369 VK_DEBUG_REPORT_OBJECT_TYPE_COMMAND_BUFFER_EXT, 0, __LINE__, DRAWSTATE_QUEUE_FORWARD_PROGRESS, "DS",
10370 "Queue %#" PRIx64 " is waiting on semaphore %#" PRIx64 " that has no way to be signaled.",
10371 reinterpret_cast<uint64_t &>(queue), reinterpret_cast<const uint64_t &>(semaphore));
10372 }
Tobin Ehlis5b5e7bc2016-03-09 16:12:48 -070010373 }
10374 }
Tobin Ehlis08017632016-03-16 13:52:20 -060010375 VkDeviceMemory mem;
Tobin Ehlis5b5e7bc2016-03-09 16:12:48 -070010376 for (uint32_t i = 0; i < pPresentInfo->swapchainCount; ++i) {
10377 auto swapchain_data = dev_data->device_extensions.swapchainMap.find(pPresentInfo->pSwapchains[i]);
10378 if (swapchain_data != dev_data->device_extensions.swapchainMap.end() &&
10379 pPresentInfo->pImageIndices[i] < swapchain_data->second->images.size()) {
10380 VkImage image = swapchain_data->second->images[pPresentInfo->pImageIndices[i]];
Mark Lobodzinski03b71512016-03-23 14:33:02 -060010381#if MTMERGESOURCE
Tobin Ehlis08017632016-03-16 13:52:20 -060010382 skip_call |=
Chris Forbes0a1ce3d2016-04-06 15:16:26 +120010383 get_mem_binding_from_object(dev_data, (uint64_t)(image), VK_DEBUG_REPORT_OBJECT_TYPE_IMAGE_EXT, &mem);
Tobin Ehlis08017632016-03-16 13:52:20 -060010384 skip_call |= validate_memory_is_valid(dev_data, mem, "vkQueuePresentKHR()", image);
Mark Lobodzinski03b71512016-03-23 14:33:02 -060010385#endif
Tobin Ehlis5b5e7bc2016-03-09 16:12:48 -070010386 vector<VkImageLayout> layouts;
10387 if (FindLayouts(dev_data, image, layouts)) {
10388 for (auto layout : layouts) {
10389 if (layout != VK_IMAGE_LAYOUT_PRESENT_SRC_KHR) {
10390 skip_call |=
10391 log_msg(dev_data->report_data, VK_DEBUG_REPORT_ERROR_BIT_EXT, VK_DEBUG_REPORT_OBJECT_TYPE_QUEUE_EXT,
10392 reinterpret_cast<uint64_t &>(queue), __LINE__, DRAWSTATE_INVALID_IMAGE_LAYOUT, "DS",
10393 "Images passed to present must be in layout "
10394 "PRESENT_SOURCE_KHR but is in %s",
10395 string_VkImageLayout(layout));
10396 }
10397 }
10398 }
10399 }
10400 }
Tobin Ehlis5b5e7bc2016-03-09 16:12:48 -070010401 }
10402
10403 if (!skip_call)
10404 result = dev_data->device_dispatch_table->QueuePresentKHR(queue, pPresentInfo);
Tobin Ehlis13443022016-04-12 10:49:41 -060010405
Tobin Ehlis5b5e7bc2016-03-09 16:12:48 -070010406 return result;
10407}
10408
10409VKAPI_ATTR VkResult VKAPI_CALL vkAcquireNextImageKHR(VkDevice device, VkSwapchainKHR swapchain, uint64_t timeout,
10410 VkSemaphore semaphore, VkFence fence, uint32_t *pImageIndex) {
10411 layer_data *dev_data = get_my_data_ptr(get_dispatch_key(device), layer_data_map);
10412 VkResult result = VK_ERROR_VALIDATION_FAILED_EXT;
10413 bool skipCall = false;
Tobin Ehlis13443022016-04-12 10:49:41 -060010414
Jeremy Hayesb9e99232016-04-13 16:20:24 -060010415 std::unique_lock<std::mutex> lock(global_lock);
Dominik Witczak16574612016-03-30 13:59:48 +020010416 if (semaphore != VK_NULL_HANDLE &&
10417 dev_data->semaphoreMap.find(semaphore) != dev_data->semaphoreMap.end()) {
Tobin Ehlis13443022016-04-12 10:49:41 -060010418 if (dev_data->semaphoreMap[semaphore].signaled) {
Tobin Ehlis5b5e7bc2016-03-09 16:12:48 -070010419 skipCall = log_msg(dev_data->report_data, VK_DEBUG_REPORT_ERROR_BIT_EXT, VK_DEBUG_REPORT_OBJECT_TYPE_SEMAPHORE_EXT,
Tobin Ehlis13443022016-04-12 10:49:41 -060010420 reinterpret_cast<const uint64_t &>(semaphore), __LINE__, DRAWSTATE_QUEUE_FORWARD_PROGRESS, "DS",
Tobin Ehlis5b5e7bc2016-03-09 16:12:48 -070010421 "vkAcquireNextImageKHR: Semaphore must not be currently signaled or in a wait state");
10422 }
Tobin Ehlis13443022016-04-12 10:49:41 -060010423 dev_data->semaphoreMap[semaphore].signaled = true;
Tobin Ehlis5b5e7bc2016-03-09 16:12:48 -070010424 }
10425 auto fence_data = dev_data->fenceMap.find(fence);
10426 if (fence_data != dev_data->fenceMap.end()) {
10427 fence_data->second.swapchain = swapchain;
10428 }
Jeremy Hayesb9e99232016-04-13 16:20:24 -060010429 lock.unlock();
Tobin Ehlis13443022016-04-12 10:49:41 -060010430
Tobin Ehlis5b5e7bc2016-03-09 16:12:48 -070010431 if (!skipCall) {
10432 result =
10433 dev_data->device_dispatch_table->AcquireNextImageKHR(device, swapchain, timeout, semaphore, fence, pImageIndex);
10434 }
Tobin Ehlis13443022016-04-12 10:49:41 -060010435
Tobin Ehlis5b5e7bc2016-03-09 16:12:48 -070010436 return result;
10437}
10438
10439VK_LAYER_EXPORT VKAPI_ATTR VkResult VKAPI_CALL
10440vkCreateDebugReportCallbackEXT(VkInstance instance, const VkDebugReportCallbackCreateInfoEXT *pCreateInfo,
10441 const VkAllocationCallbacks *pAllocator, VkDebugReportCallbackEXT *pMsgCallback) {
10442 layer_data *my_data = get_my_data_ptr(get_dispatch_key(instance), layer_data_map);
10443 VkLayerInstanceDispatchTable *pTable = my_data->instance_dispatch_table;
10444 VkResult res = pTable->CreateDebugReportCallbackEXT(instance, pCreateInfo, pAllocator, pMsgCallback);
10445 if (VK_SUCCESS == res) {
Jeremy Hayesb9e99232016-04-13 16:20:24 -060010446 std::lock_guard<std::mutex> lock(global_lock);
Tobin Ehlis5b5e7bc2016-03-09 16:12:48 -070010447 res = layer_create_msg_callback(my_data->report_data, pCreateInfo, pAllocator, pMsgCallback);
Tobin Ehlis5b5e7bc2016-03-09 16:12:48 -070010448 }
10449 return res;
10450}
10451
10452VK_LAYER_EXPORT VKAPI_ATTR void VKAPI_CALL vkDestroyDebugReportCallbackEXT(VkInstance instance,
10453 VkDebugReportCallbackEXT msgCallback,
10454 const VkAllocationCallbacks *pAllocator) {
10455 layer_data *my_data = get_my_data_ptr(get_dispatch_key(instance), layer_data_map);
10456 VkLayerInstanceDispatchTable *pTable = my_data->instance_dispatch_table;
10457 pTable->DestroyDebugReportCallbackEXT(instance, msgCallback, pAllocator);
Jeremy Hayesb9e99232016-04-13 16:20:24 -060010458 std::lock_guard<std::mutex> lock(global_lock);
Tobin Ehlis5b5e7bc2016-03-09 16:12:48 -070010459 layer_destroy_msg_callback(my_data->report_data, msgCallback, pAllocator);
Tobin Ehlis5b5e7bc2016-03-09 16:12:48 -070010460}
10461
10462VK_LAYER_EXPORT VKAPI_ATTR void VKAPI_CALL
10463vkDebugReportMessageEXT(VkInstance instance, VkDebugReportFlagsEXT flags, VkDebugReportObjectTypeEXT objType, uint64_t object,
10464 size_t location, int32_t msgCode, const char *pLayerPrefix, const char *pMsg) {
10465 layer_data *my_data = get_my_data_ptr(get_dispatch_key(instance), layer_data_map);
10466 my_data->instance_dispatch_table->DebugReportMessageEXT(instance, flags, objType, object, location, msgCode, pLayerPrefix,
10467 pMsg);
10468}
10469
10470VK_LAYER_EXPORT VKAPI_ATTR PFN_vkVoidFunction VKAPI_CALL vkGetDeviceProcAddr(VkDevice dev, const char *funcName) {
10471 if (!strcmp(funcName, "vkGetDeviceProcAddr"))
10472 return (PFN_vkVoidFunction)vkGetDeviceProcAddr;
10473 if (!strcmp(funcName, "vkDestroyDevice"))
10474 return (PFN_vkVoidFunction)vkDestroyDevice;
10475 if (!strcmp(funcName, "vkQueueSubmit"))
10476 return (PFN_vkVoidFunction)vkQueueSubmit;
10477 if (!strcmp(funcName, "vkWaitForFences"))
10478 return (PFN_vkVoidFunction)vkWaitForFences;
10479 if (!strcmp(funcName, "vkGetFenceStatus"))
10480 return (PFN_vkVoidFunction)vkGetFenceStatus;
10481 if (!strcmp(funcName, "vkQueueWaitIdle"))
10482 return (PFN_vkVoidFunction)vkQueueWaitIdle;
10483 if (!strcmp(funcName, "vkDeviceWaitIdle"))
10484 return (PFN_vkVoidFunction)vkDeviceWaitIdle;
10485 if (!strcmp(funcName, "vkGetDeviceQueue"))
10486 return (PFN_vkVoidFunction)vkGetDeviceQueue;
10487 if (!strcmp(funcName, "vkDestroyInstance"))
10488 return (PFN_vkVoidFunction)vkDestroyInstance;
10489 if (!strcmp(funcName, "vkDestroyDevice"))
10490 return (PFN_vkVoidFunction)vkDestroyDevice;
10491 if (!strcmp(funcName, "vkDestroyFence"))
10492 return (PFN_vkVoidFunction)vkDestroyFence;
10493 if (!strcmp(funcName, "vkResetFences"))
10494 return (PFN_vkVoidFunction)vkResetFences;
10495 if (!strcmp(funcName, "vkDestroySemaphore"))
10496 return (PFN_vkVoidFunction)vkDestroySemaphore;
10497 if (!strcmp(funcName, "vkDestroyEvent"))
10498 return (PFN_vkVoidFunction)vkDestroyEvent;
10499 if (!strcmp(funcName, "vkDestroyQueryPool"))
10500 return (PFN_vkVoidFunction)vkDestroyQueryPool;
10501 if (!strcmp(funcName, "vkDestroyBuffer"))
10502 return (PFN_vkVoidFunction)vkDestroyBuffer;
10503 if (!strcmp(funcName, "vkDestroyBufferView"))
10504 return (PFN_vkVoidFunction)vkDestroyBufferView;
10505 if (!strcmp(funcName, "vkDestroyImage"))
10506 return (PFN_vkVoidFunction)vkDestroyImage;
10507 if (!strcmp(funcName, "vkDestroyImageView"))
10508 return (PFN_vkVoidFunction)vkDestroyImageView;
10509 if (!strcmp(funcName, "vkDestroyShaderModule"))
10510 return (PFN_vkVoidFunction)vkDestroyShaderModule;
10511 if (!strcmp(funcName, "vkDestroyPipeline"))
10512 return (PFN_vkVoidFunction)vkDestroyPipeline;
10513 if (!strcmp(funcName, "vkDestroyPipelineLayout"))
10514 return (PFN_vkVoidFunction)vkDestroyPipelineLayout;
10515 if (!strcmp(funcName, "vkDestroySampler"))
10516 return (PFN_vkVoidFunction)vkDestroySampler;
10517 if (!strcmp(funcName, "vkDestroyDescriptorSetLayout"))
10518 return (PFN_vkVoidFunction)vkDestroyDescriptorSetLayout;
10519 if (!strcmp(funcName, "vkDestroyDescriptorPool"))
10520 return (PFN_vkVoidFunction)vkDestroyDescriptorPool;
10521 if (!strcmp(funcName, "vkDestroyFramebuffer"))
10522 return (PFN_vkVoidFunction)vkDestroyFramebuffer;
10523 if (!strcmp(funcName, "vkDestroyRenderPass"))
10524 return (PFN_vkVoidFunction)vkDestroyRenderPass;
10525 if (!strcmp(funcName, "vkCreateBuffer"))
10526 return (PFN_vkVoidFunction)vkCreateBuffer;
10527 if (!strcmp(funcName, "vkCreateBufferView"))
10528 return (PFN_vkVoidFunction)vkCreateBufferView;
10529 if (!strcmp(funcName, "vkCreateImage"))
10530 return (PFN_vkVoidFunction)vkCreateImage;
10531 if (!strcmp(funcName, "vkCreateImageView"))
10532 return (PFN_vkVoidFunction)vkCreateImageView;
10533 if (!strcmp(funcName, "vkCreateFence"))
10534 return (PFN_vkVoidFunction)vkCreateFence;
10535 if (!strcmp(funcName, "CreatePipelineCache"))
10536 return (PFN_vkVoidFunction)vkCreatePipelineCache;
10537 if (!strcmp(funcName, "DestroyPipelineCache"))
10538 return (PFN_vkVoidFunction)vkDestroyPipelineCache;
10539 if (!strcmp(funcName, "GetPipelineCacheData"))
10540 return (PFN_vkVoidFunction)vkGetPipelineCacheData;
10541 if (!strcmp(funcName, "MergePipelineCaches"))
10542 return (PFN_vkVoidFunction)vkMergePipelineCaches;
10543 if (!strcmp(funcName, "vkCreateGraphicsPipelines"))
10544 return (PFN_vkVoidFunction)vkCreateGraphicsPipelines;
10545 if (!strcmp(funcName, "vkCreateComputePipelines"))
10546 return (PFN_vkVoidFunction)vkCreateComputePipelines;
10547 if (!strcmp(funcName, "vkCreateSampler"))
10548 return (PFN_vkVoidFunction)vkCreateSampler;
10549 if (!strcmp(funcName, "vkCreateDescriptorSetLayout"))
10550 return (PFN_vkVoidFunction)vkCreateDescriptorSetLayout;
10551 if (!strcmp(funcName, "vkCreatePipelineLayout"))
10552 return (PFN_vkVoidFunction)vkCreatePipelineLayout;
10553 if (!strcmp(funcName, "vkCreateDescriptorPool"))
10554 return (PFN_vkVoidFunction)vkCreateDescriptorPool;
10555 if (!strcmp(funcName, "vkResetDescriptorPool"))
10556 return (PFN_vkVoidFunction)vkResetDescriptorPool;
10557 if (!strcmp(funcName, "vkAllocateDescriptorSets"))
10558 return (PFN_vkVoidFunction)vkAllocateDescriptorSets;
10559 if (!strcmp(funcName, "vkFreeDescriptorSets"))
10560 return (PFN_vkVoidFunction)vkFreeDescriptorSets;
10561 if (!strcmp(funcName, "vkUpdateDescriptorSets"))
10562 return (PFN_vkVoidFunction)vkUpdateDescriptorSets;
10563 if (!strcmp(funcName, "vkCreateCommandPool"))
10564 return (PFN_vkVoidFunction)vkCreateCommandPool;
10565 if (!strcmp(funcName, "vkDestroyCommandPool"))
10566 return (PFN_vkVoidFunction)vkDestroyCommandPool;
10567 if (!strcmp(funcName, "vkResetCommandPool"))
10568 return (PFN_vkVoidFunction)vkResetCommandPool;
10569 if (!strcmp(funcName, "vkCreateQueryPool"))
10570 return (PFN_vkVoidFunction)vkCreateQueryPool;
10571 if (!strcmp(funcName, "vkAllocateCommandBuffers"))
10572 return (PFN_vkVoidFunction)vkAllocateCommandBuffers;
10573 if (!strcmp(funcName, "vkFreeCommandBuffers"))
10574 return (PFN_vkVoidFunction)vkFreeCommandBuffers;
10575 if (!strcmp(funcName, "vkBeginCommandBuffer"))
10576 return (PFN_vkVoidFunction)vkBeginCommandBuffer;
10577 if (!strcmp(funcName, "vkEndCommandBuffer"))
10578 return (PFN_vkVoidFunction)vkEndCommandBuffer;
10579 if (!strcmp(funcName, "vkResetCommandBuffer"))
10580 return (PFN_vkVoidFunction)vkResetCommandBuffer;
10581 if (!strcmp(funcName, "vkCmdBindPipeline"))
10582 return (PFN_vkVoidFunction)vkCmdBindPipeline;
10583 if (!strcmp(funcName, "vkCmdSetViewport"))
10584 return (PFN_vkVoidFunction)vkCmdSetViewport;
10585 if (!strcmp(funcName, "vkCmdSetScissor"))
10586 return (PFN_vkVoidFunction)vkCmdSetScissor;
10587 if (!strcmp(funcName, "vkCmdSetLineWidth"))
10588 return (PFN_vkVoidFunction)vkCmdSetLineWidth;
10589 if (!strcmp(funcName, "vkCmdSetDepthBias"))
10590 return (PFN_vkVoidFunction)vkCmdSetDepthBias;
10591 if (!strcmp(funcName, "vkCmdSetBlendConstants"))
10592 return (PFN_vkVoidFunction)vkCmdSetBlendConstants;
10593 if (!strcmp(funcName, "vkCmdSetDepthBounds"))
10594 return (PFN_vkVoidFunction)vkCmdSetDepthBounds;
10595 if (!strcmp(funcName, "vkCmdSetStencilCompareMask"))
10596 return (PFN_vkVoidFunction)vkCmdSetStencilCompareMask;
10597 if (!strcmp(funcName, "vkCmdSetStencilWriteMask"))
10598 return (PFN_vkVoidFunction)vkCmdSetStencilWriteMask;
10599 if (!strcmp(funcName, "vkCmdSetStencilReference"))
10600 return (PFN_vkVoidFunction)vkCmdSetStencilReference;
10601 if (!strcmp(funcName, "vkCmdBindDescriptorSets"))
10602 return (PFN_vkVoidFunction)vkCmdBindDescriptorSets;
10603 if (!strcmp(funcName, "vkCmdBindVertexBuffers"))
10604 return (PFN_vkVoidFunction)vkCmdBindVertexBuffers;
10605 if (!strcmp(funcName, "vkCmdBindIndexBuffer"))
10606 return (PFN_vkVoidFunction)vkCmdBindIndexBuffer;
10607 if (!strcmp(funcName, "vkCmdDraw"))
10608 return (PFN_vkVoidFunction)vkCmdDraw;
10609 if (!strcmp(funcName, "vkCmdDrawIndexed"))
10610 return (PFN_vkVoidFunction)vkCmdDrawIndexed;
10611 if (!strcmp(funcName, "vkCmdDrawIndirect"))
10612 return (PFN_vkVoidFunction)vkCmdDrawIndirect;
10613 if (!strcmp(funcName, "vkCmdDrawIndexedIndirect"))
10614 return (PFN_vkVoidFunction)vkCmdDrawIndexedIndirect;
10615 if (!strcmp(funcName, "vkCmdDispatch"))
10616 return (PFN_vkVoidFunction)vkCmdDispatch;
10617 if (!strcmp(funcName, "vkCmdDispatchIndirect"))
10618 return (PFN_vkVoidFunction)vkCmdDispatchIndirect;
10619 if (!strcmp(funcName, "vkCmdCopyBuffer"))
10620 return (PFN_vkVoidFunction)vkCmdCopyBuffer;
10621 if (!strcmp(funcName, "vkCmdCopyImage"))
10622 return (PFN_vkVoidFunction)vkCmdCopyImage;
10623 if (!strcmp(funcName, "vkCmdBlitImage"))
10624 return (PFN_vkVoidFunction)vkCmdBlitImage;
10625 if (!strcmp(funcName, "vkCmdCopyBufferToImage"))
10626 return (PFN_vkVoidFunction)vkCmdCopyBufferToImage;
10627 if (!strcmp(funcName, "vkCmdCopyImageToBuffer"))
10628 return (PFN_vkVoidFunction)vkCmdCopyImageToBuffer;
10629 if (!strcmp(funcName, "vkCmdUpdateBuffer"))
10630 return (PFN_vkVoidFunction)vkCmdUpdateBuffer;
10631 if (!strcmp(funcName, "vkCmdFillBuffer"))
10632 return (PFN_vkVoidFunction)vkCmdFillBuffer;
10633 if (!strcmp(funcName, "vkCmdClearColorImage"))
10634 return (PFN_vkVoidFunction)vkCmdClearColorImage;
10635 if (!strcmp(funcName, "vkCmdClearDepthStencilImage"))
10636 return (PFN_vkVoidFunction)vkCmdClearDepthStencilImage;
10637 if (!strcmp(funcName, "vkCmdClearAttachments"))
10638 return (PFN_vkVoidFunction)vkCmdClearAttachments;
10639 if (!strcmp(funcName, "vkCmdResolveImage"))
10640 return (PFN_vkVoidFunction)vkCmdResolveImage;
10641 if (!strcmp(funcName, "vkCmdSetEvent"))
10642 return (PFN_vkVoidFunction)vkCmdSetEvent;
10643 if (!strcmp(funcName, "vkCmdResetEvent"))
10644 return (PFN_vkVoidFunction)vkCmdResetEvent;
10645 if (!strcmp(funcName, "vkCmdWaitEvents"))
10646 return (PFN_vkVoidFunction)vkCmdWaitEvents;
10647 if (!strcmp(funcName, "vkCmdPipelineBarrier"))
10648 return (PFN_vkVoidFunction)vkCmdPipelineBarrier;
10649 if (!strcmp(funcName, "vkCmdBeginQuery"))
10650 return (PFN_vkVoidFunction)vkCmdBeginQuery;
10651 if (!strcmp(funcName, "vkCmdEndQuery"))
10652 return (PFN_vkVoidFunction)vkCmdEndQuery;
10653 if (!strcmp(funcName, "vkCmdResetQueryPool"))
10654 return (PFN_vkVoidFunction)vkCmdResetQueryPool;
10655 if (!strcmp(funcName, "vkCmdCopyQueryPoolResults"))
10656 return (PFN_vkVoidFunction)vkCmdCopyQueryPoolResults;
10657 if (!strcmp(funcName, "vkCmdPushConstants"))
10658 return (PFN_vkVoidFunction)vkCmdPushConstants;
10659 if (!strcmp(funcName, "vkCmdWriteTimestamp"))
10660 return (PFN_vkVoidFunction)vkCmdWriteTimestamp;
10661 if (!strcmp(funcName, "vkCreateFramebuffer"))
10662 return (PFN_vkVoidFunction)vkCreateFramebuffer;
10663 if (!strcmp(funcName, "vkCreateShaderModule"))
10664 return (PFN_vkVoidFunction)vkCreateShaderModule;
10665 if (!strcmp(funcName, "vkCreateRenderPass"))
10666 return (PFN_vkVoidFunction)vkCreateRenderPass;
10667 if (!strcmp(funcName, "vkCmdBeginRenderPass"))
10668 return (PFN_vkVoidFunction)vkCmdBeginRenderPass;
10669 if (!strcmp(funcName, "vkCmdNextSubpass"))
10670 return (PFN_vkVoidFunction)vkCmdNextSubpass;
10671 if (!strcmp(funcName, "vkCmdEndRenderPass"))
10672 return (PFN_vkVoidFunction)vkCmdEndRenderPass;
10673 if (!strcmp(funcName, "vkCmdExecuteCommands"))
10674 return (PFN_vkVoidFunction)vkCmdExecuteCommands;
10675 if (!strcmp(funcName, "vkSetEvent"))
10676 return (PFN_vkVoidFunction)vkSetEvent;
10677 if (!strcmp(funcName, "vkMapMemory"))
10678 return (PFN_vkVoidFunction)vkMapMemory;
Mark Lobodzinski03b71512016-03-23 14:33:02 -060010679#if MTMERGESOURCE
Tobin Ehlis5b5e7bc2016-03-09 16:12:48 -070010680 if (!strcmp(funcName, "vkUnmapMemory"))
10681 return (PFN_vkVoidFunction)vkUnmapMemory;
10682 if (!strcmp(funcName, "vkAllocateMemory"))
10683 return (PFN_vkVoidFunction)vkAllocateMemory;
10684 if (!strcmp(funcName, "vkFreeMemory"))
10685 return (PFN_vkVoidFunction)vkFreeMemory;
10686 if (!strcmp(funcName, "vkFlushMappedMemoryRanges"))
10687 return (PFN_vkVoidFunction)vkFlushMappedMemoryRanges;
10688 if (!strcmp(funcName, "vkInvalidateMappedMemoryRanges"))
10689 return (PFN_vkVoidFunction)vkInvalidateMappedMemoryRanges;
10690 if (!strcmp(funcName, "vkBindBufferMemory"))
10691 return (PFN_vkVoidFunction)vkBindBufferMemory;
10692 if (!strcmp(funcName, "vkGetBufferMemoryRequirements"))
10693 return (PFN_vkVoidFunction)vkGetBufferMemoryRequirements;
10694 if (!strcmp(funcName, "vkGetImageMemoryRequirements"))
10695 return (PFN_vkVoidFunction)vkGetImageMemoryRequirements;
10696#endif
10697 if (!strcmp(funcName, "vkGetQueryPoolResults"))
10698 return (PFN_vkVoidFunction)vkGetQueryPoolResults;
10699 if (!strcmp(funcName, "vkBindImageMemory"))
10700 return (PFN_vkVoidFunction)vkBindImageMemory;
10701 if (!strcmp(funcName, "vkQueueBindSparse"))
10702 return (PFN_vkVoidFunction)vkQueueBindSparse;
10703 if (!strcmp(funcName, "vkCreateSemaphore"))
10704 return (PFN_vkVoidFunction)vkCreateSemaphore;
10705 if (!strcmp(funcName, "vkCreateEvent"))
10706 return (PFN_vkVoidFunction)vkCreateEvent;
10707
10708 if (dev == NULL)
10709 return NULL;
10710
10711 layer_data *dev_data;
10712 dev_data = get_my_data_ptr(get_dispatch_key(dev), layer_data_map);
10713
10714 if (dev_data->device_extensions.wsi_enabled) {
10715 if (!strcmp(funcName, "vkCreateSwapchainKHR"))
10716 return (PFN_vkVoidFunction)vkCreateSwapchainKHR;
10717 if (!strcmp(funcName, "vkDestroySwapchainKHR"))
10718 return (PFN_vkVoidFunction)vkDestroySwapchainKHR;
10719 if (!strcmp(funcName, "vkGetSwapchainImagesKHR"))
10720 return (PFN_vkVoidFunction)vkGetSwapchainImagesKHR;
10721 if (!strcmp(funcName, "vkAcquireNextImageKHR"))
10722 return (PFN_vkVoidFunction)vkAcquireNextImageKHR;
10723 if (!strcmp(funcName, "vkQueuePresentKHR"))
10724 return (PFN_vkVoidFunction)vkQueuePresentKHR;
10725 }
10726
10727 VkLayerDispatchTable *pTable = dev_data->device_dispatch_table;
10728 {
10729 if (pTable->GetDeviceProcAddr == NULL)
10730 return NULL;
10731 return pTable->GetDeviceProcAddr(dev, funcName);
10732 }
10733}
10734
10735VK_LAYER_EXPORT VKAPI_ATTR PFN_vkVoidFunction VKAPI_CALL vkGetInstanceProcAddr(VkInstance instance, const char *funcName) {
10736 if (!strcmp(funcName, "vkGetInstanceProcAddr"))
10737 return (PFN_vkVoidFunction)vkGetInstanceProcAddr;
10738 if (!strcmp(funcName, "vkGetDeviceProcAddr"))
10739 return (PFN_vkVoidFunction)vkGetDeviceProcAddr;
10740 if (!strcmp(funcName, "vkCreateInstance"))
10741 return (PFN_vkVoidFunction)vkCreateInstance;
10742 if (!strcmp(funcName, "vkCreateDevice"))
10743 return (PFN_vkVoidFunction)vkCreateDevice;
10744 if (!strcmp(funcName, "vkDestroyInstance"))
10745 return (PFN_vkVoidFunction)vkDestroyInstance;
Tobin Ehlis5b5e7bc2016-03-09 16:12:48 -070010746 if (!strcmp(funcName, "vkEnumerateInstanceLayerProperties"))
10747 return (PFN_vkVoidFunction)vkEnumerateInstanceLayerProperties;
10748 if (!strcmp(funcName, "vkEnumerateInstanceExtensionProperties"))
10749 return (PFN_vkVoidFunction)vkEnumerateInstanceExtensionProperties;
10750 if (!strcmp(funcName, "vkEnumerateDeviceLayerProperties"))
10751 return (PFN_vkVoidFunction)vkEnumerateDeviceLayerProperties;
10752 if (!strcmp(funcName, "vkEnumerateDeviceExtensionProperties"))
10753 return (PFN_vkVoidFunction)vkEnumerateDeviceExtensionProperties;
10754
10755 if (instance == NULL)
10756 return NULL;
10757
10758 PFN_vkVoidFunction fptr;
10759
10760 layer_data *my_data;
10761 my_data = get_my_data_ptr(get_dispatch_key(instance), layer_data_map);
10762 fptr = debug_report_get_instance_proc_addr(my_data->report_data, funcName);
10763 if (fptr)
10764 return fptr;
10765
10766 VkLayerInstanceDispatchTable *pTable = my_data->instance_dispatch_table;
10767 if (pTable->GetInstanceProcAddr == NULL)
10768 return NULL;
10769 return pTable->GetInstanceProcAddr(instance, funcName);
10770}